Skip to content

Commit

Permalink
captcha: reorganize captchaReaper()
Browse files Browse the repository at this point in the history
Additionally, do not send message indicating user did not answer the
captcha, if the user has been previously banned.
  • Loading branch information
qrwteyrutiyoup authored and marcopaganini committed Feb 10, 2022
1 parent a94fee7 commit f29a02d
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions src/captcha.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,33 +141,37 @@ func (x *opBot) captchaReaper(bot tgbotInterface, update tgbotapi.Update, user t

name := nameRef(user)

// Let's check if this user is already banned, in which case we can
// skip some of the following steps.
banned, err := isBanned(bot, update.Message.Chat.ID, user.ID)
if err != nil {
log.Printf("Warning: Unable to get information for user %s (uid=%d): %v", name, user.ID, err)
}

// At this point, we reached our captcha timeout and the user is still
// in the pending captcha list, meaning they didn't confirm the
// captcha.
_, err := sendMessage(bot, update.Message.Chat.ID, fmt.Sprintf(T("no_captcha_received"), name))
if err != nil {
log.Printf("Warning: Unable to send 'invalid captcha' message.")
}

// Let's remove the pending request.
defer x.pendingCaptcha.del(user.ID)

// Do nothing if user is already kicked (probably by an admin).
// Proced to kick+unban if that's not the case. We need to get
// the status before kickUser (below) for obvious reasons.
banned, err := isBanned(bot, update.Message.Chat.ID, user.ID)
if err != nil {
log.Printf("Warning: Unable to get information for user %s (uid=%d): %v", name, user.ID, err)
if banned {
log.Printf("User %s (uid=%d) has already been banned (by admin?) Not unbanning.", name, user.ID)
return
}

// As the user is not yet banned, proced to kick+unban.
// Kick user and remove from list.
_, err = sendMessage(bot, update.Message.Chat.ID, fmt.Sprintf(T("no_captcha_received"), name))
if err != nil {
log.Printf("Warning: Unable to send 'invalid captcha' message.")
}

if err = kickUser(bot, update.Message.Chat.ID, user.ID); err != nil {
log.Printf("Warning: Unable to kick user %s (uid=%d) out of the channel.", name, user.ID)
}

x.pendingCaptcha.del(user.ID)

if banned {
log.Printf("User %s (uid=%d) has already been banned (by admin?) Not unbanning.", name, user.ID)
return
}
if err = unBanUser(bot, update.Message.Chat.ID, user.ID); err != nil {
log.Printf("Warning: Unable to UNBAN user %s (uid=%d) (May be locked out.)", name, user.ID)
}
Expand Down

0 comments on commit f29a02d

Please sign in to comment.