Skip to content

Commit

Permalink
Only skip sending captcha when user is kick/banned
Browse files Browse the repository at this point in the history
  • Loading branch information
qrwteyrutiyoup authored and marcopaganini committed Jun 1, 2024
1 parent 5e096c2 commit 3323054
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
30 changes: 19 additions & 11 deletions src/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,13 @@ func (x *opBot) Run(bot *tgbotapi.BotAPI) {
x.notifications.manageNotifications(bot, update)

// Let's now handle some ban patterns before proceeding.
if x.handledPatternMatching(bot, update) {
continue
if match, err := x.handledPatternMatching(bot, update); err == nil {
switch match {
case opBan, opKick:
// For these cases, there is no need to send a captcha.
log.Printf("Not sending captcha to user %d because pattern matched with action %q\n", update.Message.From.ID, match.String())
continue
}
}

// Handle messages from users who are yet to validate the captcha.
Expand Down Expand Up @@ -185,7 +190,7 @@ func (x *opBot) Run(bot *tgbotapi.BotAPI) {
// Block many types of rich media from new users (but always allows admins).
admin, err := isAdmin(bot, update.Message.Chat.ID, update.Message.From.ID)
if err != nil {
log.Printf("Unable to determine if user is an admin. Assuming not.")
log.Printf("Unable to determine if user (id: %d) is an admin in chat (id: %d). Assuming not.", update.Message.From.ID, update.Message.Chat.ID)
}

// Block undesirable rich media messages from regular users.
Expand Down Expand Up @@ -394,34 +399,37 @@ func (x *opBot) processUserCommands(bot *tgbotapi.BotAPI, update tgbotapi.Update
}
}

func (x *opBot) handledPatternMatching(bot *tgbotapi.BotAPI, update tgbotapi.Update) bool {
func (x *opBot) handledPatternMatching(bot *tgbotapi.BotAPI, update tgbotapi.Update) (opMatchAction, error) {
ok, action := x.patterns.MatchFromUpdate(bot, update)

if !ok {
// No matches, so we can return.
return false
return action, nil
}

// Message that matched the patterns is always deleted, then a specific
// action follows.
deleteMessage(bot, update.Message.Chat.ID, update.Message.MessageID)
log.Printf("Removed message that matched the ban patterns. ChatID: %v, MessageID: %v", update.Message.Chat.ID, update.Message.MessageID)
promPatternMessageDeletedCount.Inc()
if deleteMessage(bot, update.Message.Chat.ID, update.Message.MessageID) == nil {
log.Printf("Removed message that matched the ban patterns. ChatID: %v, MessageID: %v", update.Message.Chat.ID, update.Message.MessageID)
promPatternMessageDeletedCount.Inc()
}

// For now we only have two actions: ban and kick.
var err error
if action == opBan || action == opKick {
f := map[opMatchAction]func(kickChatMemberer, int64, int) error{
opBan: banUser,
opKick: kickUser,
}
if err := f[action](bot, update.Message.Chat.ID, update.Message.From.ID); err != nil {
err = f[action](bot, update.Message.Chat.ID, update.Message.From.ID)
if err != nil {
log.Printf("Error performing action %q with username %q (%s %s): %v", action.String(), update.Message.From.UserName, update.Message.From.FirstName, update.Message.From.LastName, err)
} else {
log.Printf("Action %q performed for user %q (%s %s). Hasta la vista, baby...", action.String(), update.Message.From.UserName, update.Message.From.FirstName, update.Message.From.LastName)
promPatternKickBannedCount.Inc()
}
}
promPatternKickBannedCount.Inc()
return true
return action, err
}

// removeBadRichMessages removes undesirable rich messages (Voice, VideoNotes, etc).
Expand Down
3 changes: 2 additions & 1 deletion src/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,13 @@ func sendMessage(bot sender, chatid int64, text string) (tgbotapi.Message, error

// deleteMessage deletes a given message id in a chat id. It returns nothing as there's
// not much to do other than log if the message deletion fails.
func deleteMessage(bot deleteMessager, chatid int64, msgid int) {
func deleteMessage(bot deleteMessager, chatid int64, msgid int) error {
_, err := bot.DeleteMessage(tgbotapi.DeleteMessageConfig{ChatID: chatid, MessageID: msgid})
if err != nil {
log.Printf("Unable to delete message (chat_id=%d, message_id=%d). Something is wrong.", chatid, msgid)
}
log.Printf("Removed message_id %d from chat_id %d", chatid, msgid)
return err
}

// sendReplyWithMarkup sends a reply to a specific MessageID with markup.
Expand Down

0 comments on commit 3323054

Please sign in to comment.