diff --git a/README.md b/README.md index 5a1ac15..b5913e7 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ 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 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 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 You can find default values in [config/defaults.go](config/defaults.go) diff --git a/bot/access.go b/bot/access.go index 08b2967..31cd5cc 100644 --- a/bot/access.go +++ b/bot/access.go @@ -2,12 +2,21 @@ package bot import ( "context" + "regexp" "maunium.net/go/mautrix/id" "gitlab.com/etke.cc/postmoogle/utils" ) +func parseMXIDpatterns(patterns []string, defaultPattern string) ([]*regexp.Regexp, error) { + if len(patterns) == 0 && defaultPattern != "" { + patterns = []string{defaultPattern} + } + + return utils.WildcardMXIDsToRegexes(patterns) +} + func (b *Bot) allowAnyone(actorID id.UserID, targetRoomID id.RoomID) bool { return true } diff --git a/bot/bot.go b/bot/bot.go index cd5f7e4..de25ddb 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -36,9 +36,23 @@ func New( log *logger.Logger, prefix string, domain string, - allowedUsers []*regexp.Regexp, - allowedAdmins []*regexp.Regexp, -) *Bot { + users []string, + admins []string, +) (*Bot, error) { + _, homeserver, err := lp.GetClient().UserID.Parse() + if err != nil { + return nil, err + } + var allowedUsers []*regexp.Regexp + allowedUsers, uerr := parseMXIDpatterns(users, "@*:"+homeserver) + if uerr != nil { + return nil, uerr + } + allowedAdmins, aerr := parseMXIDpatterns(admins, "") + if aerr != nil { + return nil, aerr + } + b := &Bot{ prefix: prefix, domain: domain, @@ -50,10 +64,9 @@ func New( lp: lp, mu: map[id.RoomID]*sync.Mutex{}, } - b.commands = b.buildCommandList() - return b + return b, nil } // Error message to the log and matrix room diff --git a/cmd/cmd.go b/cmd/cmd.go index 1ce08ba..a76978c 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -27,12 +27,7 @@ var ( func main() { quit := make(chan struct{}) - cfg, err := config.New() - if err != nil { - log = logger.New("postmoogle.", "info") - log.Fatal("cannot read config: %v", err) - } - + cfg := config.New() log = logger.New("postmoogle.", cfg.LogLevel) log.Info("#############################") @@ -87,7 +82,12 @@ func initBot(cfg *config.Config) { // nolint // Fatal = panic, not os.Exit() log.Fatal("cannot initialize matrix bot: %v", err) } - mxb = bot.New(lp, mxlog, cfg.Prefix, cfg.Domain, cfg.Users, cfg.Admins) + + mxb, err = bot.New(lp, mxlog, cfg.Prefix, cfg.Domain, cfg.Users, cfg.Admins) + if err != nil { + // nolint // Fatal = panic, not os.Exit() + log.Fatal("cannot start matrix bot: %v", err) + } log.Debug("bot has been created") } diff --git a/config/config.go b/config/config.go index a4b11db..9d3cec2 100644 --- a/config/config.go +++ b/config/config.go @@ -1,30 +1,15 @@ package config import ( - "fmt" - "regexp" - "gitlab.com/etke.cc/go/env" - - "gitlab.com/etke.cc/postmoogle/utils" ) const prefix = "postmoogle" // New config -func New() (*Config, error) { +func New() *Config { env.SetPrefix(prefix) - userPatterns, err := getUserRegexPatterns("users") - if err != nil { - return nil, err - } - - adminPatterns, err := getUserRegexPatterns("admins") - if err != nil { - return nil, err - } - cfg := &Config{ Homeserver: env.String("homeserver", defaultConfig.Homeserver), Login: env.String("login", defaultConfig.Login), @@ -35,8 +20,8 @@ func New() (*Config, error) { NoEncryption: env.Bool("noencryption"), MaxSize: env.Int("maxsize", defaultConfig.MaxSize), StatusMsg: env.String("statusmsg", defaultConfig.StatusMsg), - Users: userPatterns, - Admins: adminPatterns, + Users: env.Slice("users"), + Admins: env.Slice("admins"), Sentry: Sentry{ DSN: env.String("sentry.dsn", defaultConfig.Sentry.DSN), }, @@ -47,19 +32,5 @@ func New() (*Config, error) { }, } - 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 + return cfg } diff --git a/config/types.go b/config/types.go index 94566b6..531ebfb 100644 --- a/config/types.go +++ b/config/types.go @@ -1,7 +1,5 @@ package config -import "regexp" - // Config of Postmoogle type Config struct { // Homeserver url @@ -24,10 +22,10 @@ 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 = * - Users []*regexp.Regexp - // Admins holds list of admin users (wildcards supported), e.g.: @*:example.com, @bot.*:example.com, @admin:*. Empty = * - Admins []*regexp.Regexp + // Users 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 // DB config DB DB