allow reserved mailboxes, closes #43

This commit is contained in:
Aine
2022-11-20 20:55:41 +02:00
parent 117736dcf3
commit 6ddb894577
7 changed files with 31 additions and 8 deletions

View File

@@ -64,6 +64,7 @@ env vars
* **POSTMOOGLE_LOGLEVEL** - log level * **POSTMOOGLE_LOGLEVEL** - log level
* **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_MAILBOXES_RESERVED** - space separated list of reserved mailboxes (e.g.: `postmaster admin root`), nobody can create them
* **POSTMOOGLE_MAXSIZE** - max email size (including attachments) in megabytes * **POSTMOOGLE_MAXSIZE** - max email size (including attachments) in megabytes
* **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

View File

@@ -73,6 +73,15 @@ func (b *Bot) allowSend(actorID id.UserID, targetRoomID id.RoomID) bool {
return !cfg.NoSend() return !cfg.NoSend()
} }
func (b *Bot) isReserved(mailbox string) bool {
for _, reserved := range b.reservedMailboxes {
if mailbox == reserved {
return true
}
}
return false
}
// IsGreylisted checks if host is in greylist // IsGreylisted checks if host is in greylist
func (b *Bot) IsGreylisted(addr net.Addr) bool { func (b *Bot) IsGreylisted(addr net.Addr) bool {
if b.getBotSettings().Greylist() == 0 { if b.getBotSettings().Greylist() == 0 {

View File

@@ -20,6 +20,7 @@ type Bot struct {
domains []string domains []string
allowedUsers []*regexp.Regexp allowedUsers []*regexp.Regexp
allowedAdmins []*regexp.Regexp allowedAdmins []*regexp.Regexp
reservedMailboxes []string
commands commandList commands commandList
banlist bglist banlist bglist
rooms sync.Map rooms sync.Map
@@ -36,15 +37,17 @@ func New(
log *logger.Logger, log *logger.Logger,
prefix string, prefix string,
domains []string, domains []string,
reserved []string,
admins []string, admins []string,
) (*Bot, error) { ) (*Bot, error) {
b := &Bot{ b := &Bot{
prefix: prefix, reservedMailboxes: reserved,
domains: domains, domains: domains,
rooms: sync.Map{}, prefix: prefix,
log: log, rooms: sync.Map{},
lp: lp, log: log,
mu: map[string]*sync.Mutex{}, lp: lp,
mu: map[string]*sync.Mutex{},
} }
users, err := b.initBotUsers() users, err := b.initBotUsers()
if err != nil { if err != nil {

View File

@@ -86,7 +86,7 @@ func (b *Bot) setOption(ctx context.Context, name, value string) {
evt := eventFromContext(ctx) evt := eventFromContext(ctx)
if name == roomOptionMailbox { if name == roomOptionMailbox {
existingID, ok := b.getMapping(value) existingID, ok := b.getMapping(value)
if ok && existingID != "" && existingID != evt.RoomID { if (ok && existingID != "" && existingID != evt.RoomID) || b.isReserved(value) {
b.SendNotice(ctx, evt.RoomID, fmt.Sprintf("Mailbox `%s` (%s) already taken, kupo", value, utils.EmailsList(value, ""))) b.SendNotice(ctx, evt.RoomID, fmt.Sprintf("Mailbox `%s` (%s) already taken, kupo", value, utils.EmailsList(value, "")))
return return
} }

View File

@@ -98,7 +98,7 @@ func initBot(cfg *config.Config) {
log.Fatal("cannot initialize matrix bot: %v", err) log.Fatal("cannot initialize matrix bot: %v", err)
} }
mxb, err = bot.New(lp, mxlog, cfg.Prefix, cfg.Domains, cfg.Admins) mxb, err = bot.New(lp, mxlog, cfg.Prefix, cfg.Domains, cfg.Mailboxes.Reserved, cfg.Admins)
if err != nil { if err != nil {
// nolint // Fatal = panic, not os.Exit() // nolint // Fatal = panic, not os.Exit()
log.Fatal("cannot start matrix bot: %v", err) log.Fatal("cannot start matrix bot: %v", err)

View File

@@ -22,6 +22,9 @@ func New() *Config {
MaxSize: env.Int("maxsize", defaultConfig.MaxSize), MaxSize: env.Int("maxsize", defaultConfig.MaxSize),
StatusMsg: env.String("statusmsg", defaultConfig.StatusMsg), StatusMsg: env.String("statusmsg", defaultConfig.StatusMsg),
Admins: env.Slice("admins"), Admins: env.Slice("admins"),
Mailboxes: Mailboxes{
Reserved: env.Slice("mailboxes.reserved"),
},
TLS: TLS{ TLS: TLS{
Certs: env.Slice("tls.cert"), Certs: env.Slice("tls.cert"),
Keys: env.Slice("tls.key"), Keys: env.Slice("tls.key"),

View File

@@ -24,6 +24,8 @@ type Config struct {
MaxSize int MaxSize int
// StatusMsg of the bot // StatusMsg of the bot
StatusMsg string StatusMsg string
// Mailboxes config
Mailboxes Mailboxes
// Admins holds list of admin users (wildcards supported), e.g.: @*:example.com, @bot.*:example.com, @admin:*. Empty = no admins // Admins holds list of admin users (wildcards supported), e.g.: @*:example.com, @bot.*:example.com, @admin:*. Empty = no admins
Admins []string Admins []string
@@ -57,3 +59,8 @@ type TLS struct {
type Sentry struct { type Sentry struct {
DSN string DSN string
} }
// Mailboxes config
type Mailboxes struct {
Reserved []string
}