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_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)

View File

@@ -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
}

View File

@@ -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

View File

@@ -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")
}

View File

@@ -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
}

View File

@@ -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