Implement optional reason in room' banlist

This commit is contained in:
eleksir
2023-08-17 11:02:53 -07:00
parent 257f489172
commit 75e7a22c95
3 changed files with 42 additions and 6 deletions

41
buny.go
View File

@@ -4,6 +4,7 @@ import (
"fmt"
"regexp"
"strings"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/eleksir/go-xmpp"
@@ -82,7 +83,8 @@ func buny(v xmpp.Presence) error { //nolint:gocognit
Type: v.Type,
},
); err != nil {
gTomb.Killf("unable to send phrase to room %s: %w", room, err)
err := fmt.Errorf("unable to send phrase to room %s: %w", room, err)
gTomb.Kill(err)
// Здесь возвращаем nil, т.к. за нас ошибку залоггирует код выше
return nil
@@ -91,7 +93,22 @@ func buny(v xmpp.Presence) error { //nolint:gocognit
// https://xmpp.org/extensions/xep-0045.html#ban баним вот таким сообщением
ban := "<item affiliation='outcast' jid='" + evilJid + "'>"
ban += "<reason />"
if bEntry.ReasonEnable {
var t = time.Now()
ban += fmt.Sprintf(
"<reason>autoban at %04d.%02d.%02d %02d:%02d:%02d</reason>",
t.Year(),
t.Month(),
t.Day(),
t.Hour(),
t.Minute(),
t.Second(),
)
} else {
ban += "<reason />"
}
ban += "</item>"
if id, err := talk.RawInformationQuery(
@@ -144,7 +161,8 @@ func buny(v xmpp.Presence) error { //nolint:gocognit
Type: v.Type,
},
); err != nil {
gTomb.Killf("unable to send phrase to room %s: %w", room, err)
err := fmt.Errorf("unable to send phrase to room %s: %w", room, err)
gTomb.Kill(err)
// Здесь возвращаем nil, т.к. за нас ошибку залоггирует код выше
return nil
@@ -153,7 +171,22 @@ func buny(v xmpp.Presence) error { //nolint:gocognit
// https://xmpp.org/extensions/xep-0045.html#ban баним вот таким сообщением
ban := "<item affiliation='outcast' jid='" + evilJid + "'>"
ban += "<reason />"
if bEntry.ReasonEnable {
var t = time.Now()
ban += fmt.Sprintf(
"<reason>autoban at %04d.%02d.%02d %02d:%02d:%02d</reason>",
t.Year(),
t.Month(),
t.Day(),
t.Hour(),
t.Minute(),
t.Second(),
)
} else {
ban += "<reason />"
}
ban += "</item>"
if id, err := talk.RawInformationQuery(

View File

@@ -3,6 +3,8 @@
{
# Пустой список означает глобальные баны, которые применяются ко всем комнатам, где присутствует бот
"room_name": "",
# Записывать ли в reason в банлисте комнаты сообщение вида autoban at yyyy.MM.dd hh:mm:ss
"reason_enable": false,
# Список регулярок JID-ов, которых надо банить
"jid_re": [
"^[Mm]ary@server.tld/resource1$",

View File

@@ -50,8 +50,9 @@ type myWhiteList struct {
// myBlackList прототип структурки с чёрным списком jid-ов.
type myBlackList struct {
Blacklist []struct {
RoomName string `json:"room_name,omitempty"`
JidRe []string `json:"jid_re,omitempty"`
RoomName string `json:"room_name,omitempty"`
ReasonEnable bool `json:"reason_enable,omitempty"`
JidRe []string `json:"jid_re,omitempty"`
} `json:"blacklist,omitempty"`
}