Add POSTMOOGLE_ADMINS

This commit is contained in:
Slavi Pantaleev
2022-08-29 09:10:31 +03:00
parent 77a7e9efc6
commit a62dc0df4f
5 changed files with 47 additions and 19 deletions

View File

@@ -44,6 +44,7 @@ env vars
* **POSTMOOGLE_DB_DIALECT** - database dialect (postgres, sqlite3) * **POSTMOOGLE_DB_DIALECT** - database dialect (postgres, sqlite3)
* **POSTMOOGLE_MAXSIZE** - max email size (including attachments) in megabytes * **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 is allowed. Example rule: `@someone:example.com @another:example.com @bot.*:example.com @*:another.com` * **POSTMOOGLE_USERS** - a space-separated list of whitelisted users allowed to use the bridge. If not defined, everyone is 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
You can find default values in [config/defaults.go](config/defaults.go) You can find default values in [config/defaults.go](config/defaults.go)

View File

@@ -21,6 +21,7 @@ type Bot struct {
prefix string prefix string
domain string domain string
allowedUsers []*regexp.Regexp allowedUsers []*regexp.Regexp
allowedAdmins []*regexp.Regexp
rooms sync.Map rooms sync.Map
log *logger.Logger log *logger.Logger
lp *linkpearl.Linkpearl lp *linkpearl.Linkpearl
@@ -29,17 +30,25 @@ type Bot struct {
} }
// New creates a new matrix bot // New creates a new matrix bot
func New(lp *linkpearl.Linkpearl, log *logger.Logger, prefix, domain string, noowner, federation bool, allowedUsers []*regexp.Regexp) *Bot { func New(
lp *linkpearl.Linkpearl,
log *logger.Logger,
prefix, domain string,
noowner, federation bool,
allowedUsers []*regexp.Regexp,
allowedAdmins []*regexp.Regexp,
) *Bot {
return &Bot{ return &Bot{
noowner: noowner, noowner: noowner,
federation: federation, federation: federation,
prefix: prefix, prefix: prefix,
domain: domain, domain: domain,
allowedUsers: allowedUsers, allowedUsers: allowedUsers,
rooms: sync.Map{}, allowedAdmins: allowedAdmins,
log: log, rooms: sync.Map{},
lp: lp, log: log,
mu: map[id.RoomID]*sync.Mutex{}, lp: lp,
mu: map[id.RoomID]*sync.Mutex{},
} }
} }

View File

@@ -87,7 +87,7 @@ func initBot(cfg *config.Config) {
// nolint // Fatal = panic, not os.Exit() // nolint // Fatal = panic, not os.Exit()
log.Fatal("cannot initialize matrix bot: %v", err) log.Fatal("cannot initialize matrix bot: %v", err)
} }
mxb = bot.New(lp, mxlog, cfg.Prefix, cfg.Domain, cfg.NoOwner, cfg.Federation, cfg.Users) mxb = bot.New(lp, mxlog, cfg.Prefix, cfg.Domain, cfg.NoOwner, cfg.Federation, cfg.Users, cfg.Admins)
log.Debug("bot has been created") log.Debug("bot has been created")
} }

View File

@@ -2,6 +2,7 @@ package config
import ( import (
"fmt" "fmt"
"regexp"
"gitlab.com/etke.cc/go/env" "gitlab.com/etke.cc/go/env"
@@ -14,14 +15,14 @@ const prefix = "postmoogle"
func New() (*Config, error) { func New() (*Config, error) {
env.SetPrefix(prefix) env.SetPrefix(prefix)
mxidPatterns := env.Slice("users") userPatterns, err := getUserRegexPatterns("users")
regexPatterns, err := utils.WildcardMXIDsToRegexes(mxidPatterns)
if err != nil { if err != nil {
return nil, fmt.Errorf( return nil, err
"failed to convert wildcard user patterns (`%s`) to regular expression: %s", }
mxidPatterns,
err, adminPatterns, err := getUserRegexPatterns("admins")
) if err != nil {
return nil, err
} }
cfg := &Config{ cfg := &Config{
@@ -36,7 +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: *regexPatterns, Users: *userPatterns,
Admins: *adminPatterns,
Sentry: Sentry{ Sentry: Sentry{
DSN: env.String("sentry.dsn", defaultConfig.Sentry.DSN), DSN: env.String("sentry.dsn", defaultConfig.Sentry.DSN),
}, },
@@ -49,3 +51,17 @@ func New() (*Config, error) {
return cfg, nil return cfg, nil
} }
func getUserRegexPatterns(key string) (*[]*regexp.Regexp, error) {
mxidPatterns := env.Slice(key)
regexPatterns, err := utils.WildcardMXIDsToRegexes(mxidPatterns)
if err != nil {
return nil, fmt.Errorf(
"failed to convert wildcard %s patterns (`%s`) to regular expression: %s",
key,
mxidPatterns,
err,
)
}
return regexPatterns, nil
}

View File

@@ -30,6 +30,8 @@ type Config struct {
StatusMsg string StatusMsg string
// Users holds list of allowed users (wildcards supported), e.g.: @*:example.com, @bot.*:example.com, @admin:*. Empty = * // Users holds list of allowed users (wildcards supported), e.g.: @*:example.com, @bot.*:example.com, @admin:*. Empty = *
Users []*regexp.Regexp Users []*regexp.Regexp
// Admins holds list of admin users (wildcards supported), e.g.: @*:example.com, @bot.*:example.com, @admin:*. Empty = *
Admins []*regexp.Regexp
// DB config // DB config
DB DB DB DB