set default POSTMOOGLE_USERS pattern

This commit is contained in:
Aine
2022-08-29 20:56:28 +03:00
parent 74e7fa5f3b
commit 0ba951fbe6
6 changed files with 43 additions and 52 deletions

View File

@@ -44,7 +44,7 @@ env vars
* **POSTMOOGLE_DB_DSN** - database connection string * **POSTMOOGLE_DB_DSN** - database connection string
* **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 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 * **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

@@ -2,12 +2,21 @@ package bot
import ( import (
"context" "context"
"regexp"
"maunium.net/go/mautrix/id" "maunium.net/go/mautrix/id"
"gitlab.com/etke.cc/postmoogle/utils" "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 { func (b *Bot) allowAnyone(actorID id.UserID, targetRoomID id.RoomID) bool {
return true return true
} }

View File

@@ -36,9 +36,23 @@ func New(
log *logger.Logger, log *logger.Logger,
prefix string, prefix string,
domain string, domain string,
allowedUsers []*regexp.Regexp, users []string,
allowedAdmins []*regexp.Regexp, admins []string,
) *Bot { ) (*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{ b := &Bot{
prefix: prefix, prefix: prefix,
domain: domain, domain: domain,
@@ -50,10 +64,9 @@ func New(
lp: lp, lp: lp,
mu: map[id.RoomID]*sync.Mutex{}, mu: map[id.RoomID]*sync.Mutex{},
} }
b.commands = b.buildCommandList() b.commands = b.buildCommandList()
return b return b, nil
} }
// Error message to the log and matrix room // Error message to the log and matrix room

View File

@@ -27,12 +27,7 @@ var (
func main() { func main() {
quit := make(chan struct{}) quit := make(chan struct{})
cfg, err := config.New() cfg := config.New()
if err != nil {
log = logger.New("postmoogle.", "info")
log.Fatal("cannot read config: %v", err)
}
log = logger.New("postmoogle.", cfg.LogLevel) log = logger.New("postmoogle.", cfg.LogLevel)
log.Info("#############################") log.Info("#############################")
@@ -87,7 +82,12 @@ 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.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") log.Debug("bot has been created")
} }

View File

@@ -1,30 +1,15 @@
package config package config
import ( import (
"fmt"
"regexp"
"gitlab.com/etke.cc/go/env" "gitlab.com/etke.cc/go/env"
"gitlab.com/etke.cc/postmoogle/utils"
) )
const prefix = "postmoogle" const prefix = "postmoogle"
// New config // New config
func New() (*Config, error) { func New() *Config {
env.SetPrefix(prefix) 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{ cfg := &Config{
Homeserver: env.String("homeserver", defaultConfig.Homeserver), Homeserver: env.String("homeserver", defaultConfig.Homeserver),
Login: env.String("login", defaultConfig.Login), Login: env.String("login", defaultConfig.Login),
@@ -35,8 +20,8 @@ func New() (*Config, error) {
NoEncryption: env.Bool("noencryption"), NoEncryption: env.Bool("noencryption"),
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: env.Slice("users"),
Admins: adminPatterns, Admins: env.Slice("admins"),
Sentry: Sentry{ Sentry: Sentry{
DSN: env.String("sentry.dsn", defaultConfig.Sentry.DSN), DSN: env.String("sentry.dsn", defaultConfig.Sentry.DSN),
}, },
@@ -47,19 +32,5 @@ func New() (*Config, error) {
}, },
} }
return cfg, nil return cfg
}
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

@@ -1,7 +1,5 @@
package config package config
import "regexp"
// Config of Postmoogle // Config of Postmoogle
type Config struct { type Config struct {
// Homeserver url // Homeserver url
@@ -24,10 +22,10 @@ type Config struct {
MaxSize int MaxSize int
// StatusMsg of the bot // StatusMsg of the bot
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 = homeserver only
Users []*regexp.Regexp Users []string
// Admins holds list of admin users (wildcards supported), e.g.: @*:example.com, @bot.*:example.com, @admin:*. Empty = * // Admins holds list of admin users (wildcards supported), e.g.: @*:example.com, @bot.*:example.com, @admin:*. Empty = no admins
Admins []*regexp.Regexp Admins []string
// DB config // DB config
DB DB DB DB