From 564cd82c6b7a0074ba546c6dc203a61f25e61e44 Mon Sep 17 00:00:00 2001 From: Aine Date: Mon, 22 Aug 2022 20:21:22 +0300 Subject: [PATCH] fix prefix; test attachments; add maxsize --- README.md | 3 ++- bot/command.go | 5 +++++ cmd/cmd.go | 2 +- config/config.go | 2 ++ config/defaults.go | 1 + config/types.go | 2 ++ smtp/server.go | 4 ++-- 7 files changed, 15 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ce3675b..89642bc 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ An Email to Matrix bridge - [x] Matrix bot - [x] Configuration in room's account data - [x] Receive emails to matrix rooms -- [x] Receive attachments (untested) +- [x] Receive attachments - [ ] Map email threads to matrix threads ### Send @@ -40,6 +40,7 @@ env vars * **POSTMOOGLE_LOGLEVEL** - log level * **POSTMOOGLE_DB_DSN** - database connection string * **POSTMOOGLE_DB_DIALECT** - database dialect (postgres, sqlite3) +* **POSTMOOGLE_MAXSIZE** - max email size (including attachments) in megabytes You can find default values in [config/defaults.go](config/defaults.go) diff --git a/bot/command.go b/bot/command.go index 63bc5ad..ed579f1 100644 --- a/bot/command.go +++ b/bot/command.go @@ -33,6 +33,11 @@ func (b *Bot) parseCommand(message string) []string { return nil } + index := strings.LastIndex(message, b.prefix) + if index == -1 { + return nil + } + message = strings.TrimSpace(strings.Replace(message, b.prefix, "", 1)) return strings.Split(message, " ") } diff --git a/cmd/cmd.go b/cmd/cmd.go index b4ff451..071a0bc 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -40,7 +40,7 @@ func main() { defer recovery() go startBot() - if err := smtp.Start(cfg.Domain, cfg.Port, cfg.LogLevel, mxb); err != nil { + if err := smtp.Start(cfg.Domain, cfg.Port, cfg.LogLevel, cfg.MaxSize, mxb); err != nil { //nolint:gocritic log.Fatal("SMTP server crashed: %v", err) } diff --git a/config/config.go b/config/config.go index 9c5c715..53e349d 100644 --- a/config/config.go +++ b/config/config.go @@ -13,9 +13,11 @@ func New() *Config { Homeserver: env.String("homeserver", defaultConfig.Homeserver), Login: env.String("login", defaultConfig.Login), Password: env.String("password", defaultConfig.Password), + Prefix: env.String("prefix", defaultConfig.Prefix), Domain: env.String("domain", defaultConfig.Domain), Port: env.String("port", defaultConfig.Port), NoEncryption: env.Bool("noencryption"), + MaxSize: env.Int("maxsize", defaultConfig.MaxSize), Sentry: Sentry{ DSN: env.String("sentry.dsn", defaultConfig.Sentry.DSN), SampleRate: env.Int("sentry.rate", defaultConfig.Sentry.SampleRate), diff --git a/config/defaults.go b/config/defaults.go index 1489f84..d7f2820 100644 --- a/config/defaults.go +++ b/config/defaults.go @@ -5,6 +5,7 @@ var defaultConfig = &Config{ Domain: "localhost", Port: "25", Prefix: "!pm", + MaxSize: 1024, DB: DB{ DSN: "local.db", Dialect: "sqlite3", diff --git a/config/types.go b/config/types.go index 29eaef6..764e56f 100644 --- a/config/types.go +++ b/config/types.go @@ -18,6 +18,8 @@ type Config struct { NoEncryption bool // Prefix for commands Prefix string + // MaxSize of an email (including attachments) + MaxSize int // DB config DB DB diff --git a/smtp/server.go b/smtp/server.go index 7794baa..a06ce77 100644 --- a/smtp/server.go +++ b/smtp/server.go @@ -33,7 +33,7 @@ func (b *backend) AnonymousLogin(state *smtp.ConnectionState) (smtp.Session, err return b.newSession(), nil } -func Start(domain, port, loglevel string, client Client) error { +func Start(domain, port, loglevel string, maxSize int, client Client) error { log := logger.New("smtp.", loglevel) be := &backend{ log: log, @@ -46,7 +46,7 @@ func Start(domain, port, loglevel string, client Client) error { s.AuthDisabled = true s.ReadTimeout = 10 * time.Second s.WriteTimeout = 10 * time.Second - s.MaxMessageBytes = 128 * 1024 + s.MaxMessageBytes = maxSize * 1024 * 1024 if log.GetLevel() == "DEBUG" || log.GetLevel() == "TRACE" { s.Debug = os.Stdout }