manage users in runtime, closes #16

This commit is contained in:
Aine
2022-08-29 21:41:14 +03:00
parent 0ba951fbe6
commit f97ebb604a
7 changed files with 119 additions and 18 deletions

View File

@@ -44,8 +44,8 @@ env vars
* **POSTMOOGLE_DB_DSN** - database connection string
* **POSTMOOGLE_DB_DIALECT** - database dialect (postgres, sqlite3)
* **POSTMOOGLE_MAXSIZE** - max email size (including attachments) in megabytes
* **POSTMOOGLE_USERS** - a space-separated list of whitelisted users allowed to use the bridge. If not defined, everyone on the homeserver are allowed. Example rule: `@someone:example.com @another:example.com @bot.*:example.com @*:another.com`
* **POSTMOOGLE_ADMINS** - a space-separated list of admin users. See `POSTMOOGLE_USERS` for syntax examples
* <s>**POSTMOOGLE_USERS**</s> - deprecated and ignored, use `!pm users` instead
You can find default values in [config/defaults.go](config/defaults.go)
@@ -83,6 +83,7 @@ If you want to change them - check available options in the help message (`!pm h
---
* **!pm mailboxes** - Show the list of all mailboxes
* **!pm users** - Get or set allowed users patterns
* **!pm delete** &lt;mailbox&gt; - Delete specific mailbox
</details>

View File

@@ -39,12 +39,25 @@ func New(
users []string,
admins []string,
) (*Bot, error) {
b := &Bot{
prefix: prefix,
domain: domain,
rooms: sync.Map{},
cfg: cache.NewLRU[settings](1000),
log: log,
lp: lp,
mu: map[id.RoomID]*sync.Mutex{},
}
err := b.migrateBotSettings(users)
if err != nil {
return nil, err
}
_, homeserver, err := lp.GetClient().UserID.Parse()
if err != nil {
return nil, err
}
var allowedUsers []*regexp.Regexp
allowedUsers, uerr := parseMXIDpatterns(users, "@*:"+homeserver)
allowedUsers, uerr := parseMXIDpatterns(b.getBotSettings().Users(), "@*:"+homeserver)
if uerr != nil {
return nil, uerr
}
@@ -52,18 +65,8 @@ func New(
if aerr != nil {
return nil, aerr
}
b := &Bot{
prefix: prefix,
domain: domain,
allowedUsers: allowedUsers,
allowedAdmins: allowedAdmins,
rooms: sync.Map{},
cfg: cache.NewLRU[settings](1000),
log: log,
lp: lp,
mu: map[id.RoomID]*sync.Mutex{},
}
b.allowedUsers = allowedUsers
b.allowedAdmins = allowedAdmins
b.commands = b.buildCommandList()
return b, nil

View File

@@ -14,6 +14,7 @@ import (
const (
commandHelp = "help"
commandStop = "stop"
commandUsers = botOptionUsers
commandDelete = "delete"
commandMailboxes = "mailboxes"
)
@@ -111,6 +112,11 @@ func (b *Bot) buildCommandList() commandList {
allowed: b.allowOwner,
},
{allowed: b.allowAdmin}, // delimiter
{
key: botOptionUsers,
description: "Get or set allowed users",
allowed: b.allowAdmin,
},
{
key: commandMailboxes,
description: "Show the list of all mailboxes",
@@ -140,6 +146,8 @@ func (b *Bot) handleCommand(ctx context.Context, evt *event.Event, commandSlice
b.sendHelp(ctx)
case commandStop:
b.runStop(ctx)
case commandUsers:
b.runUsers(ctx, commandSlice)
case commandDelete:
b.runDelete(ctx, commandSlice)
case commandMailboxes:

View File

@@ -87,3 +87,41 @@ func (b *Bot) runDelete(ctx context.Context, commandSlice []string) {
b.SendNotice(ctx, evt.RoomID, "mailbox has been deleted")
}
func (b *Bot) runUsers(ctx context.Context, commandSlice []string) {
evt := eventFromContext(ctx)
cfg := b.getBotSettings()
if len(commandSlice) < 2 {
var msg strings.Builder
users := cfg.Users()
if len(users) > 0 {
msg.WriteString("Currently: `")
msg.WriteString(strings.Join(users, " "))
msg.WriteString("`\n\n")
}
msg.WriteString("Usage: `")
msg.WriteString(b.prefix)
msg.WriteString(" users PATTERN1 PATTERN2 PATTERN3...`")
msg.WriteString("where patterns like `@someone:example.com ")
msg.WriteString(" @bot.*:example.com @*:another.com @*:*`\n")
b.SendNotice(ctx, evt.RoomID, msg.String())
return
}
patterns := commandSlice[1:]
allowedUsers, err := parseMXIDpatterns(patterns, "")
if err != nil {
b.SendError(ctx, evt.RoomID, fmt.Sprintf("invalid patterns: %v", err))
return
}
cfg.Set(botOptionUsers, strings.Join(patterns, " "))
err = b.setBotSettings(cfg)
if err != nil {
b.Error(ctx, evt.RoomID, "cannot set bot config: %v", err)
}
b.allowedUsers = allowedUsers
b.SendNotice(ctx, evt.RoomID, "allowed users updated")
}

View File

@@ -4,12 +4,15 @@ import (
"strings"
"maunium.net/go/mautrix/id"
"gitlab.com/etke.cc/postmoogle/utils"
)
// account data keys
const (
messagekey = "cc.etke.postmoogle.message"
settingskey = "cc.etke.postmoogle.settings"
messagekey = "cc.etke.postmoogle.message"
settingskey = "cc.etke.postmoogle.settings"
botconfigkey = "cc.etke.postmoogle.config"
)
// event keys
@@ -27,6 +30,8 @@ const (
optionNoHTML = "nohtml"
optionNoThreads = "nothreads"
optionNoFiles = "nofiles"
botOptionUsers = "users"
)
var migrations = []string{}
@@ -108,3 +113,44 @@ func (b *Bot) setThreadID(roomID id.RoomID, messageID string, eventID id.EventID
}
}
}
// TODO: remove after migration
func (b *Bot) migrateBotSettings(users []string) error {
config := b.getBotSettings()
cfgUsers := config.Users()
if len(users) > 0 && len(cfgUsers) == 0 {
_, err := parseMXIDpatterns(users, "")
if err != nil {
return err
}
config.Set(botOptionUsers, strings.Join(users, " "))
return b.setBotSettings(config)
}
return nil
}
func (b *Bot) getBotSettings() settings {
cfg := b.cfg.Get(botconfigkey)
if cfg != nil {
return cfg
}
config := settings{}
err := b.lp.GetClient().GetAccountData(botconfigkey, &config)
if err != nil {
if strings.Contains(err.Error(), "M_NOT_FOUND") {
err = nil
}
b.log.Error("cannot get bot settings: %v", utils.UnwrapError(err))
} else {
b.cfg.Set(botconfigkey, config)
}
return config
}
func (b *Bot) setBotSettings(cfg settings) error {
b.cfg.Set(botconfigkey, cfg)
return utils.UnwrapError(b.lp.GetClient().SetAccountData(botconfigkey, cfg))
}

View File

@@ -52,6 +52,11 @@ func (s settings) NoFiles() bool {
return utils.Bool(s.Get(optionNoFiles))
}
// Users is bot/admin option
func (s settings) Users() []string {
return strings.Split(s.Get(botOptionUsers), " ")
}
// Set option
func (s settings) Set(key, value string) {
s[strings.ToLower(strings.TrimSpace(key))] = value

View File

@@ -22,7 +22,7 @@ type Config struct {
MaxSize int
// StatusMsg of the bot
StatusMsg string
// Users holds list of allowed users (wildcards supported), e.g.: @*:example.com, @bot.*:example.com, @admin:*. Empty = homeserver only
// Users DEPRECATED holds list of allowed users (wildcards supported), e.g.: @*:example.com, @bot.*:example.com, @admin:*. Empty = homeserver only
Users []string
// Admins holds list of admin users (wildcards supported), e.g.: @*:example.com, @bot.*:example.com, @admin:*. Empty = no admins
Admins []string