Skip to content

Commit

Permalink
adjust logging for soft bans
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Apr 24, 2024
1 parent a320599 commit 197116a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
10 changes: 4 additions & 6 deletions app/events/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,12 @@ func (a *admin) MsgHandler(update tbapi.Update) error {

// ban user
banReq := banRequest{duration: bot.PermanentBanDuration, userID: info.UserID, chatID: a.primChatID,
tbAPI: a.tbAPI, dry: a.dry, training: a.trainingMode}
tbAPI: a.tbAPI, dry: a.dry, training: a.trainingMode, userName: update.Message.ForwardSenderName}

if err := banUserOrChannel(banReq); err != nil {
errs = multierror.Append(errs, fmt.Errorf("failed to ban user %d: %w", info.UserID, err))
}

log.Printf("[INFO] user %q (%d) banned", update.Message.ForwardSenderName, info.UserID)
return errs.ErrorOrNil()
}

Expand Down Expand Up @@ -261,18 +260,16 @@ func (a *admin) directReport(update tbapi.Update, updateSamples bool) error {
errs = multierror.Append(errs, fmt.Errorf("failed to delete message %d: %w", update.Message.MessageID, err))
} else {
log.Printf("[INFO] admin spam reprot message %d deleted", update.Message.MessageID)

}

// ban user
banReq := banRequest{duration: bot.PermanentBanDuration, userID: origMsg.From.ID, chatID: a.primChatID,
tbAPI: a.tbAPI, dry: a.dry, training: a.trainingMode}
tbAPI: a.tbAPI, dry: a.dry, training: a.trainingMode, userName: update.Message.ForwardSenderName}

if err := banUserOrChannel(banReq); err != nil {
errs = multierror.Append(errs, fmt.Errorf("failed to ban user %d: %w", origMsg.From.ID, err))
}

log.Printf("[INFO] user %q (%d) banned", update.Message.ForwardSenderName, origMsg.From.ID)
return errs.ErrorOrNil()
}

Expand Down Expand Up @@ -506,17 +503,18 @@ func (a *admin) callbackShowInfo(query *tbapi.CallbackQuery) error {
// deleteAndBan deletes the message and bans the user
func (a *admin) deleteAndBan(query *tbapi.CallbackQuery, userID int64, msgID int) error {
errs := new(multierror.Error)
userName := a.locator.UserNameByID(userID)
banReq := banRequest{
duration: bot.PermanentBanDuration,
userID: userID,
chatID: a.primChatID,
tbAPI: a.tbAPI,
dry: a.dry,
training: false, // reset training flag, ban for real
userName: userName,
}

// check if user is super and don't ban if so
userName := a.locator.UserNameByID(userID)
msgFromSuper := userName != "" && a.superUsers.IsSuper(userName)
if !msgFromSuper {
if err := banUserOrChannel(banReq); err != nil {
Expand Down
8 changes: 6 additions & 2 deletions app/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type Bot interface {
func escapeMarkDownV1Text(text string) string {
escSymbols := []string{"_", "*", "`", "["}
for _, esc := range escSymbols {
text = strings.Replace(text, esc, "\\"+esc, -1)
text = strings.ReplaceAll(text, esc, "\\"+esc)
}
return text
}
Expand Down Expand Up @@ -103,6 +103,7 @@ type banRequest struct {
channelID int64
chatID int64
duration time.Duration
userName string

dry bool
training bool // training mode, do not do the actual ban
Expand Down Expand Up @@ -135,7 +136,7 @@ func banUserOrChannel(r banRequest) error {
r.duration = 1 * time.Minute
}

if r.restrict {
if r.restrict { // soft ban mode
resp, err := r.tbAPI.Request(tbapi.RestrictChatMemberConfig{
ChatMemberConfig: tbapi.ChatMemberConfig{
ChatID: r.chatID,
Expand All @@ -157,6 +158,7 @@ func banUserOrChannel(r banRequest) error {
if !resp.Ok {
return fmt.Errorf("response is not Ok: %v", string(resp.Result))
}
log.Printf("[INFO] %s restricted by bot for %v", r.userName, r.duration)
return nil
}

Expand All @@ -172,6 +174,7 @@ func banUserOrChannel(r banRequest) error {
if !resp.Ok {
return fmt.Errorf("response is not Ok: %v", string(resp.Result))
}
log.Printf("[INFO] channel %s banned by bot for %v", r.userName, r.duration)
return nil
}

Expand All @@ -189,6 +192,7 @@ func banUserOrChannel(r banRequest) error {
return fmt.Errorf("response is not Ok: %v", string(resp.Result))
}

log.Printf("[INFO] user %s banned by bot for %v", r.userName, r.duration)
return nil
}

Expand Down
11 changes: 4 additions & 7 deletions app/events/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,12 @@ func (l *TelegramListener) procEvents(update tbapi.Update) error {
return nil
}

banReq := banRequest{duration: resp.BanInterval, userID: resp.User.ID, channelID: resp.ChannelID,
banReq := banRequest{duration: resp.BanInterval, userID: resp.User.ID, channelID: resp.ChannelID, userName: banUserStr,
chatID: fromChat, dry: l.Dry, training: l.TrainingMode, tbAPI: l.TbAPI, restrict: l.SoftBanMode}
if err := banUserOrChannel(banReq); err == nil {
log.Printf("[INFO] %s banned by bot for %v", banUserStr, resp.BanInterval)
if l.adminChatID != 0 && msg.From.ID != 0 {
l.adminHandler.ReportBan(banUserStr, msg)
}
} else {
if err := banUserOrChannel(banReq); err != nil {
errs = multierror.Append(errs, fmt.Errorf("failed to ban %s: %w", banUserStr, err))
} else if l.adminChatID != 0 && msg.From.ID != 0 {
l.adminHandler.ReportBan(banUserStr, msg)
}
}

Expand Down

0 comments on commit 197116a

Please sign in to comment.