diff --git a/app/bot/spam.go b/app/bot/spam.go index cf1098b3..6ec470a2 100644 --- a/app/bot/spam.go +++ b/app/bot/spam.go @@ -147,15 +147,13 @@ func (s *SpamFilter) OnMessage(msg Message) (response Response) { if similaritySpam || isEmojiSpam || stopWordsSpam || s.isCasSpam(msg.From.ID) || classifiedSpam { log.Printf("[INFO] user %s detected as spammer, msg: %q", displayUsername, msg.Text) + msgPrefix := s.SpamMsg if s.Dry { - return Response{ - Text: s.SpamDryMsg + fmt.Sprintf(": %q (%d)", displayUsername, msg.From.ID), - Send: true, ReplyTo: msg.ID, - } + msgPrefix = s.SpamDryMsg } - return Response{Text: s.SpamMsg + fmt.Sprintf(": %q (%d)", displayUsername, msg.From.ID), - Send: true, ReplyTo: msg.ID, BanInterval: permanentBanDuration, DeleteReplyTo: true, - User: User{Username: msg.From.Username, ID: msg.From.ID, DisplayName: msg.From.DisplayName}, + spamRespMsg := fmt.Sprintf("%s: %q (%d)", msgPrefix, displayUsername, msg.From.ID) + return Response{Text: spamRespMsg, Send: true, ReplyTo: msg.ID, BanInterval: permanentBanDuration, + DeleteReplyTo: true, User: User{Username: msg.From.Username, ID: msg.From.ID, DisplayName: msg.From.DisplayName}, } } diff --git a/app/events/telegram.go b/app/events/telegram.go index 83d43122..3443a562 100644 --- a/app/events/telegram.go +++ b/app/events/telegram.go @@ -37,6 +37,7 @@ type TelegramListener struct { SuperUsers SuperUser StartupMsg string NoSpamReply bool + Dry bool AdminURL string AdminSecret string @@ -171,7 +172,7 @@ func (l *TelegramListener) procEvents(update tbapi.Update) error { errs := new(multierror.Error) isBanInvoked := resp.Send && resp.BanInterval > 0 - // some bots may request direct ban for given duration + // some bots may request a direct ban for given duration if isBanInvoked { log.Printf("[DEBUG] ban initiated for %+v", resp) l.SpamLogger.Save(msg, &resp) @@ -292,6 +293,12 @@ func (l *TelegramListener) banUserOrChannel(duration time.Duration, chatID, user // you do not want to accidentally get into this 30-second window of a lifetime ban. // In practice BanDuration is equal to ten minutes, // so this `if` statement is unlikely to be evaluated to true. + + if l.Dry { + log.Printf("[INFO] dry run: ban %d for %v", userID, duration) + return nil + } + if duration < 30*time.Second { duration = 1 * time.Minute } diff --git a/app/main.go b/app/main.go index 89835529..0f314d00 100644 --- a/app/main.go +++ b/app/main.go @@ -102,6 +102,10 @@ func main() { } func execute(ctx context.Context) error { + if opts.Dry { + log.Print("[WARN] dry mode, no actual bans") + } + tbAPI, err := tbapi.NewBotAPI(opts.Telegram.Token) if err != nil { return fmt.Errorf("can't make telegram bot, %w", err) @@ -142,6 +146,7 @@ func execute(ctx context.Context) error { AdminGroup: opts.Admin.Group, AdminURL: opts.Admin.URL, AdminListenAddr: opts.Admin.Address, + Dry: opts.Dry, } if err := tgListener.Do(ctx); err != nil {