refactored

This commit is contained in:
Aine
2022-08-29 12:30:43 +03:00
parent 79775c0c13
commit 6623251695
7 changed files with 114 additions and 112 deletions

View File

@@ -1,41 +1,40 @@
package bot package bot
import ( import (
"fmt" "context"
"maunium.net/go/mautrix/id" "maunium.net/go/mautrix/id"
"gitlab.com/etke.cc/postmoogle/utils" "gitlab.com/etke.cc/postmoogle/utils"
) )
type accessCheckerFunc func(id.UserID, id.RoomID) (bool, error) func (b *Bot) allowAnyone(actorID id.UserID, targetRoomID id.RoomID) bool {
return true
func (b *Bot) allowAnyone(actorID id.UserID, targetRoomID id.RoomID) (bool, error) {
return true, nil
} }
func (b *Bot) allowOwner(actorID id.UserID, targetRoomID id.RoomID) (bool, error) { func (b *Bot) allowOwner(actorID id.UserID, targetRoomID id.RoomID) bool {
if !utils.Match(actorID.String(), b.allowedUsers) { if !utils.Match(actorID.String(), b.allowedUsers) {
return false, nil return false
} }
if b.noowner { if b.noowner {
return true, nil return true
} }
cfg, err := b.getSettings(targetRoomID) cfg, err := b.getSettings(targetRoomID)
if err != nil { if err != nil {
return false, fmt.Errorf("failed to retrieve settings: %v", err) b.Error(context.Background(), targetRoomID, "failed to retrieve settings: %v", err)
return false
} }
owner := cfg.Owner() owner := cfg.Owner()
if owner == "" { if owner == "" {
return true, nil return true
} }
return owner == actorID.String(), nil return owner == actorID.String()
} }
func (b *Bot) allowAdmin(actorID id.UserID, targetRoomID id.RoomID) (bool, error) { func (b *Bot) allowAdmin(actorID id.UserID, targetRoomID id.RoomID) bool {
return utils.Match(actorID.String(), b.allowedAdmins), nil return utils.Match(actorID.String(), b.allowedAdmins)
} }

View File

@@ -1,39 +0,0 @@
package bot
import (
"context"
"fmt"
"strings"
"maunium.net/go/mautrix/id"
)
func (b *Bot) sendMailboxes(ctx context.Context) {
evt := eventFromContext(ctx)
mailboxes := map[string]id.RoomID{}
b.rooms.Range(func(mailbox any, roomID any) bool {
mailboxes[mailbox.(string)] = roomID.(id.RoomID)
return true
})
if len(mailboxes) == 0 {
b.Notice(ctx, evt.RoomID, "No mailboxes are managed by the bot so far, kupo!")
return
}
var msg strings.Builder
msg.WriteString("The following mailboxes are managed by the bot:\n")
for mailbox, roomID := range mailboxes {
email := fmt.Sprintf("%s@%s", mailbox, b.domain)
msg.WriteString("* `")
msg.WriteString(email)
msg.WriteString("` - `")
msg.WriteString(roomID.String())
msg.WriteString("`")
msg.WriteString("\n")
}
b.Notice(ctx, evt.RoomID, msg.String())
}

View File

@@ -60,19 +60,21 @@ func New(
// Error message to the log and matrix room // Error message to the log and matrix room
func (b *Bot) Error(ctx context.Context, roomID id.RoomID, message string, args ...interface{}) { func (b *Bot) Error(ctx context.Context, roomID id.RoomID, message string, args ...interface{}) {
b.log.Error(message, args...) b.log.Error(message, args...)
err := fmt.Errorf(message, args...)
sentry.GetHubFromContext(ctx).CaptureException(fmt.Errorf(message, args...)) sentry.GetHubFromContext(ctx).CaptureException(err)
if roomID != "" { if roomID != "" {
// nolint // if something goes wrong here nobody can help... b.SendError(ctx, roomID, message)
b.lp.Send(roomID, &event.MessageEventContent{
MsgType: event.MsgNotice,
Body: "ERROR: " + fmt.Sprintf(message, args...),
})
} }
} }
// Notice sends a notice message to the matrix room // SendError sends an error message to the matrix room
func (b *Bot) Notice(ctx context.Context, roomID id.RoomID, message string) { func (b *Bot) SendError(ctx context.Context, roomID id.RoomID, message string) {
b.SendNotice(ctx, roomID, "ERROR: "+message)
}
// SendNotice sends a notice message to the matrix room
func (b *Bot) SendNotice(ctx context.Context, roomID id.RoomID, message string) {
content := format.RenderMarkdown(message, true, true) content := format.RenderMarkdown(message, true, true)
content.MsgType = event.MsgNotice content.MsgType = event.MsgNotice
_, err := b.lp.Send(roomID, &content) _, err := b.lp.Send(roomID, &content)

View File

@@ -22,7 +22,7 @@ type (
key string key string
description string description string
sanitizer func(string) string sanitizer func(string) string
accessChecker accessCheckerFunc allowed func(id.UserID, id.RoomID) bool
} }
commandList []command commandList []command
) )
@@ -42,12 +42,12 @@ func (b *Bot) buildCommandList() commandList {
{ {
key: commandHelp, key: commandHelp,
description: "Show this help message", description: "Show this help message",
accessChecker: b.allowAnyone, allowed: b.allowAnyone,
}, },
{ {
key: commandStop, key: commandStop,
description: "Disable bridge for the room and clear all configuration", description: "Disable bridge for the room and clear all configuration",
accessChecker: b.allowOwner, allowed: b.allowOwner,
}, },
{}, // delimiter {}, // delimiter
// options commands // options commands
@@ -55,13 +55,13 @@ func (b *Bot) buildCommandList() commandList {
key: optionMailbox, key: optionMailbox,
description: "Get or set mailbox of the room", description: "Get or set mailbox of the room",
sanitizer: utils.Mailbox, sanitizer: utils.Mailbox,
accessChecker: b.allowOwner, allowed: b.allowOwner,
}, },
{ {
key: optionOwner, key: optionOwner,
description: "Get or set owner of the room", description: "Get or set owner of the room",
sanitizer: func(s string) string { return s }, sanitizer: func(s string) string { return s },
accessChecker: b.allowOwner, allowed: b.allowOwner,
}, },
{}, // delimiter {}, // delimiter
{ {
@@ -71,7 +71,7 @@ func (b *Bot) buildCommandList() commandList {
optionNoSender, optionNoSender,
), ),
sanitizer: utils.SanitizeBoolString, sanitizer: utils.SanitizeBoolString,
accessChecker: b.allowOwner, allowed: b.allowOwner,
}, },
{ {
key: optionNoSubject, key: optionNoSubject,
@@ -80,7 +80,7 @@ func (b *Bot) buildCommandList() commandList {
optionNoSubject, optionNoSubject,
), ),
sanitizer: utils.SanitizeBoolString, sanitizer: utils.SanitizeBoolString,
accessChecker: b.allowOwner, allowed: b.allowOwner,
}, },
{ {
key: optionNoHTML, key: optionNoHTML,
@@ -89,7 +89,7 @@ func (b *Bot) buildCommandList() commandList {
optionNoHTML, optionNoHTML,
), ),
sanitizer: utils.SanitizeBoolString, sanitizer: utils.SanitizeBoolString,
accessChecker: b.allowOwner, allowed: b.allowOwner,
}, },
{ {
key: optionNoThreads, key: optionNoThreads,
@@ -98,7 +98,7 @@ func (b *Bot) buildCommandList() commandList {
optionNoThreads, optionNoThreads,
), ),
sanitizer: utils.SanitizeBoolString, sanitizer: utils.SanitizeBoolString,
accessChecker: b.allowOwner, allowed: b.allowOwner,
}, },
{ {
key: optionNoFiles, key: optionNoFiles,
@@ -107,13 +107,13 @@ func (b *Bot) buildCommandList() commandList {
optionNoFiles, optionNoFiles,
), ),
sanitizer: utils.SanitizeBoolString, sanitizer: utils.SanitizeBoolString,
accessChecker: b.allowOwner, allowed: b.allowOwner,
}, },
{}, // delimiter {}, // delimiter
{ {
key: commandMailboxes, key: commandMailboxes,
description: "Show the list of all mailboxes", description: "Show the list of all mailboxes",
accessChecker: b.allowAdmin, allowed: b.allowAdmin,
}, },
} }
} }
@@ -129,13 +129,8 @@ func (b *Bot) handleCommand(ctx context.Context, evt *event.Event, commandSlice
return return
} }
allowed, err := cmd.accessChecker(evt.Sender, evt.RoomID) if !cmd.allowed(evt.Sender, evt.RoomID) {
if err != nil { b.SendNotice(ctx, evt.RoomID, "not allowed to do that, kupo")
b.Error(ctx, evt.RoomID, err.Error())
return
}
if !allowed {
b.Notice(ctx, evt.RoomID, "not allowed to do that, kupo")
return return
} }
@@ -181,7 +176,7 @@ func (b *Bot) sendIntroduction(ctx context.Context, roomID id.RoomID) {
msg.WriteString(b.domain) msg.WriteString(b.domain)
msg.WriteString("` and have them appear in this room.") msg.WriteString("` and have them appear in this room.")
b.Notice(ctx, roomID, msg.String()) b.SendNotice(ctx, roomID, msg.String())
} }
func (b *Bot) sendHelp(ctx context.Context, roomID id.RoomID) { func (b *Bot) sendHelp(ctx context.Context, roomID id.RoomID) {
@@ -226,7 +221,52 @@ func (b *Bot) sendHelp(ctx context.Context, roomID id.RoomID) {
msg.WriteString("\n") msg.WriteString("\n")
} }
b.Notice(ctx, roomID, msg.String()) b.SendNotice(ctx, roomID, msg.String())
}
func (b *Bot) sendMailboxes(ctx context.Context) {
evt := eventFromContext(ctx)
mailboxes := map[string]id.RoomID{}
b.rooms.Range(func(key any, value any) bool {
if key == nil {
return true
}
if value == nil {
return true
}
mailbox, ok := key.(string)
if !ok {
return true
}
roomID, ok := value.(id.RoomID)
if !ok {
return true
}
mailboxes[mailbox] = roomID
return true
})
if len(mailboxes) == 0 {
b.SendNotice(ctx, evt.RoomID, "No mailboxes are managed by the bot so far, kupo!")
return
}
var msg strings.Builder
msg.WriteString("The following mailboxes are managed by the bot:\n")
for mailbox, roomID := range mailboxes {
msg.WriteString("* `")
msg.WriteString(mailbox)
msg.WriteString("@")
msg.WriteString(b.domain)
msg.WriteString("` - `")
msg.WriteString(roomID.String())
msg.WriteString("`")
msg.WriteString("\n")
}
b.SendNotice(ctx, evt.RoomID, msg.String())
} }
func (b *Bot) runStop(ctx context.Context) { func (b *Bot) runStop(ctx context.Context) {
@@ -239,7 +279,7 @@ func (b *Bot) runStop(ctx context.Context) {
mailbox := cfg.Get(optionMailbox) mailbox := cfg.Get(optionMailbox)
if mailbox == "" { if mailbox == "" {
b.Notice(ctx, evt.RoomID, "that room is not configured yet") b.SendNotice(ctx, evt.RoomID, "that room is not configured yet")
return return
} }
@@ -251,7 +291,7 @@ func (b *Bot) runStop(ctx context.Context) {
return return
} }
b.Notice(ctx, evt.RoomID, "mailbox has been disabled") b.SendNotice(ctx, evt.RoomID, "mailbox has been disabled")
} }
func (b *Bot) handleOption(ctx context.Context, cmd []string) { func (b *Bot) handleOption(ctx context.Context, cmd []string) {
@@ -272,7 +312,7 @@ func (b *Bot) getOption(ctx context.Context, name string) {
value := cfg.Get(name) value := cfg.Get(name)
if value == "" { if value == "" {
b.Notice(ctx, evt.RoomID, fmt.Sprintf("`%s` is not set, kupo.", name)) b.SendNotice(ctx, evt.RoomID, fmt.Sprintf("`%s` is not set, kupo.", name))
return return
} }
@@ -280,7 +320,7 @@ func (b *Bot) getOption(ctx context.Context, name string) {
value = value + "@" + b.domain value = value + "@" + b.domain
} }
b.Notice(ctx, evt.RoomID, fmt.Sprintf("`%s` of this room is `%s`", name, value)) b.SendNotice(ctx, evt.RoomID, fmt.Sprintf("`%s` of this room is `%s`", name, value))
} }
func (b *Bot) setOption(ctx context.Context, name, value string) { func (b *Bot) setOption(ctx context.Context, name, value string) {
@@ -293,7 +333,7 @@ func (b *Bot) setOption(ctx context.Context, name, value string) {
if name == optionMailbox { if name == optionMailbox {
existingID, ok := b.GetMapping(value) existingID, ok := b.GetMapping(value)
if ok && existingID != "" && existingID != evt.RoomID { if ok && existingID != "" && existingID != evt.RoomID {
b.Notice(ctx, evt.RoomID, fmt.Sprintf("Mailbox `%s@%s` already taken, kupo", value, b.domain)) b.SendNotice(ctx, evt.RoomID, fmt.Sprintf("Mailbox `%s@%s` already taken, kupo", value, b.domain))
return return
} }
} }
@@ -326,5 +366,5 @@ func (b *Bot) setOption(ctx context.Context, name, value string) {
value = value + "@" + b.domain value = value + "@" + b.domain
} }
b.Notice(ctx, evt.RoomID, fmt.Sprintf("`%s` of this room set to `%s`", name, value)) b.SendNotice(ctx, evt.RoomID, fmt.Sprintf("`%s` of this room set to `%s`", name, value))
} }

View File

@@ -37,8 +37,8 @@ func New() (*Config, error) {
Federation: env.Bool("federation"), Federation: env.Bool("federation"),
MaxSize: env.Int("maxsize", defaultConfig.MaxSize), MaxSize: env.Int("maxsize", defaultConfig.MaxSize),
StatusMsg: env.String("statusmsg", defaultConfig.StatusMsg), StatusMsg: env.String("statusmsg", defaultConfig.StatusMsg),
Users: *userPatterns, Users: userPatterns,
Admins: *adminPatterns, Admins: adminPatterns,
Sentry: Sentry{ Sentry: Sentry{
DSN: env.String("sentry.dsn", defaultConfig.Sentry.DSN), DSN: env.String("sentry.dsn", defaultConfig.Sentry.DSN),
}, },
@@ -52,7 +52,7 @@ func New() (*Config, error) {
return cfg, nil return cfg, nil
} }
func getUserRegexPatterns(key string) (*[]*regexp.Regexp, error) { func getUserRegexPatterns(key string) ([]*regexp.Regexp, error) {
mxidPatterns := env.Slice(key) mxidPatterns := env.Slice(key)
regexPatterns, err := utils.WildcardMXIDsToRegexes(mxidPatterns) regexPatterns, err := utils.WildcardMXIDsToRegexes(mxidPatterns)
if err != nil { if err != nil {

View File

@@ -7,7 +7,7 @@ import (
) )
// WildcardMXIDsToRegexes converts a list of wildcard patterns to a list of regular expressions // WildcardMXIDsToRegexes converts a list of wildcard patterns to a list of regular expressions
func WildcardMXIDsToRegexes(wildCardPatterns []string) (*[]*regexp.Regexp, error) { func WildcardMXIDsToRegexes(wildCardPatterns []string) ([]*regexp.Regexp, error) {
regexPatterns := make([]*regexp.Regexp, len(wildCardPatterns)) regexPatterns := make([]*regexp.Regexp, len(wildCardPatterns))
for idx, wildCardPattern := range wildCardPatterns { for idx, wildCardPattern := range wildCardPatterns {
@@ -18,7 +18,7 @@ func WildcardMXIDsToRegexes(wildCardPatterns []string) (*[]*regexp.Regexp, error
regexPatterns[idx] = regex regexPatterns[idx] = regex
} }
return &regexPatterns, nil return regexPatterns, nil
} }
// Match tells if the given user id is allowed to use the bot, according to the given whitelist // Match tells if the given user id is allowed to use the bot, according to the given whitelist

View File

@@ -202,7 +202,7 @@ func TestMatch(t *testing.T) {
t.Error(err) t.Error(err)
} }
actualResult := Match(testData.checkedValue, *allowedUserRegexes) actualResult := Match(testData.checkedValue, allowedUserRegexes)
if actualResult == testData.expectedResult { if actualResult == testData.expectedResult {
return return