diff --git a/Dockerfile b/Dockerfile index dbfbeda..9e2de83 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,7 +3,7 @@ FROM golang:1.13-stretch AS build-env COPY . /src WORKDIR /src -ENV GOPROXY=http://192.168.0.143:3000 +#ENV GOPROXY=http://192.168.0.143:3000 ENV GO113MODULE=on RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go test ./... -cover -coverprofile=c.out #gosetup diff --git a/channels/verifiychecker.go b/channels/verifiychecker.go new file mode 100644 index 0000000..eee1cf7 --- /dev/null +++ b/channels/verifiychecker.go @@ -0,0 +1,134 @@ +package channels + +import ( + "git.dragon-labs.de/alphyron/group_helper/logic" + "git.dragon-labs.de/alphyron/group_helper/models" + "git.dragon-labs.de/alphyron/group_helper/obj" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" + "log" + "strconv" + "sync" + "time" +) + +func CheckUnverifiedUser(ticker *time.Ticker, quitChannel <-chan struct{}, groupHelperService logic.GroupHelperService, bot *tgbotapi.BotAPI, data *obj.VerifyData) { + var wg sync.WaitGroup + wg.Add(len(data.Users)) + + for { + select { + case <-ticker.C: + + delete := make([]*obj.VerifyUser, 0) + + groups := make(map[int64]*models.Group, 0) + users := make(map[int]*models.User, 0) + + for _, user := range data.Users { + defer wg.Done() + user.LeftTime -= 1 + + if user.LeftTime <= 0 { + log.Println(len(data.Users)) + + if _, ok := groups[user.GroupID]; !ok { + group, err := groupHelperService.GetGroupByID(user.GroupID) + + if err != nil { + log.Println("Error while getting the Group in the verifychecker for id " + strconv.FormatInt(user.GroupID, 10)) + log.Println(err) + return + } + + groups[user.GroupID] = group + } + + if _, ok := users[user.UserID]; !ok { + user_db, err := groupHelperService.GetUserByID(int64(user.UserID)) + + if err != nil { + log.Println("Error while getting the User in the verifychecker for id " + strconv.Itoa(user.UserID)) + log.Println(err) + return + } + + users[user.UserID] = user_db + } + + userconfig := tgbotapi.ChatConfigWithUser{ + ChatID: user.GroupID, + UserID: user.UserID, + } + + member, _ := bot.GetChatMember(userconfig) + delete = append(delete, user) + _, err := groupHelperService.UserLeaveGroup(users[user.UserID], groups[user.GroupID]) + + if err != nil { + log.Printf("Error while removing the user from group in db in the verifier (user/group) (%d/%d)", user.UserID, user.GroupID) + log.Println(err) + } + + if member.HasLeft() { + bot.DeleteMessage(tgbotapi.DeleteMessageConfig{ + ChatID: user.GroupID, + MessageID: user.MessageID, + }) + return + } + + kickMessage := groups[user.GroupID].UserKickMessage + //TODO Replace placeholder + + msg := tgbotapi.NewMessage(user.GroupID, kickMessage) + msg.ParseMode = "Markdown" + _, err = bot.Send(msg) + + if err != nil { + log.Println("problem while send a message in the verifier:") + log.Printf("Problem Error Message: %v", err) + } + + kickConfig := tgbotapi.KickChatMemberConfig{ + ChatMemberConfig: tgbotapi.ChatMemberConfig{ + UserID: user.UserID, + ChatID: user.GroupID, + }, + } + res, _ := bot.KickChatMember(kickConfig) + if !res.Ok { + msg := tgbotapi.NewMessage(user.GroupID, "I can't kick @"+member.User.UserName+"\nReason:\n"+res.Description) + msg.ParseMode = "Markdown" + _, err = bot.Send(msg) + + if err != nil { + log.Println("problem while send a message in the verifier:") + log.Printf("Problem Error Message: %v", err) + } + } + + _, err = bot.DeleteMessage(tgbotapi.DeleteMessageConfig{ + ChatID: user.GroupID, + MessageID: user.MessageID, + }) + + if err != nil { + log.Println("problem while deleting a message in the verifier:") + log.Println(err) + } + } + } + + for _, user := range delete { + data.RemoveUser(user.UserID, user.GroupID) + } + + case <-quitChannel: + ticker.Stop() + return + } + } + + wg.Wait() + +} diff --git a/main.go b/main.go index 88fa501..fcf6fe9 100644 --- a/main.go +++ b/main.go @@ -1,14 +1,17 @@ package main import ( + "git.dragon-labs.de/alphyron/group_helper/channels" "git.dragon-labs.de/alphyron/group_helper/cli" "git.dragon-labs.de/alphyron/group_helper/cli/commands" "git.dragon-labs.de/alphyron/group_helper/config" "git.dragon-labs.de/alphyron/group_helper/logic" + "git.dragon-labs.de/alphyron/group_helper/obj" "git.dragon-labs.de/alphyron/group_helper/repository" "git.dragon-labs.de/alphyron/group_helper/telegram" "git.dragon-labs.de/alphyron/group_helper/telegram/routines" "log" + "time" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" ) @@ -55,13 +58,22 @@ func main() { service := logic.NewGroupHelperService(groupHelperRepo) + userData := &obj.VerifyData{ + Users: make([]*obj.VerifyUser, 0), + } + cm := InitialCommandManager(bot, service) - rm := InitialRoutineManager(bot, cm, service) + rm := InitialRoutineManager(bot, cm, service, userData) + + ticker := time.NewTicker(1 * time.Minute) + quit := make(chan struct{}) + + go channels.CheckUnverifiedUser(ticker, quit, service, bot, userData) rm.StartUpdates() } -func InitialRoutineManager(bot *tgbotapi.BotAPI, commandManager *cli.CommandManager, service logic.GroupHelperService) *telegram.RoutineManager { +func InitialRoutineManager(bot *tgbotapi.BotAPI, commandManager *cli.CommandManager, service logic.GroupHelperService, userData *obj.VerifyData) *telegram.RoutineManager { rm := &telegram.RoutineManager{ Bot: bot, GroupHelperService: service, @@ -69,7 +81,7 @@ func InitialRoutineManager(bot *tgbotapi.BotAPI, commandManager *cli.CommandMana rm.RegisterRoutine(routines.NewCommandRoutine(commandManager)) rm.RegisterRoutine(routines.NewDatabaseRoutine(service)) - rm.RegisterRoutine(routines.NewJoinCheckerRoutine()) + rm.RegisterRoutine(routines.NewJoinCheckerRoutine(userData)) rm.RegisterRoutine(routines.NewVerifierRoutine(service)) return rm diff --git a/telegram/routines/joinchecker.go b/telegram/routines/joinchecker.go index de713e4..5f6a8de 100644 --- a/telegram/routines/joinchecker.go +++ b/telegram/routines/joinchecker.go @@ -2,6 +2,7 @@ package routines import ( "git.dragon-labs.de/alphyron/group_helper/models" + "git.dragon-labs.de/alphyron/group_helper/obj" "git.dragon-labs.de/alphyron/group_helper/telegram" "git.dragon-labs.de/alphyron/group_helper/util" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" @@ -12,10 +13,13 @@ import ( ) type joinCheckerRoutine struct { + verifyData *obj.VerifyData } -func NewJoinCheckerRoutine() telegram.Routine { - return joinCheckerRoutine{} +func NewJoinCheckerRoutine(verifyData *obj.VerifyData) telegram.Routine { + return joinCheckerRoutine{ + verifyData: verifyData, + } } func (j joinCheckerRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Update, group *models.Group) error { @@ -30,12 +34,7 @@ func (j joinCheckerRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Upd if update.Message.NewChatMembers != nil { for _, newUser := range *update.Message.NewChatMembers { - type check struct { - Task string - Solution bool - } - - length := 3 + length := 50 tasks := make([][]tgbotapi.InlineKeyboardButton, length) @@ -53,7 +52,7 @@ func (j joinCheckerRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Upd msg.ParseMode = "Markdown" msg.ReplyMarkup = markup - _, err := botAPI.Send(msg) + message, err := botAPI.Send(msg) if err != nil { log.Println("Problem to send verified Messages to user") @@ -61,7 +60,12 @@ func (j joinCheckerRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Upd log.Println(err) } - //TODO Add to countdown + j.verifyData.AddUser(&obj.VerifyUser{ + UserID: newUser.ID, + GroupID: group.GroupID, + LeftTime: group.KickCooldown, + MessageID: message.MessageID, + }) } } return nil diff --git a/telegram/routines/verifier.go b/telegram/routines/verifier.go index e80b53f..846bd7f 100644 --- a/telegram/routines/verifier.go +++ b/telegram/routines/verifier.go @@ -36,7 +36,7 @@ func (v verifierRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Update if success { - //TODO check if the user is realy in countdown handler + //TODO check if the user is really in countdown handler /*if isInGroup { _, err = botAPI.AnswerCallbackQuery(tgbotapi.NewCallback(update.CallbackQuery.ID, "You are already verified!")) return err