fix prefix; test attachments; add maxsize

This commit is contained in:
Aine
2022-08-22 20:21:22 +03:00
parent d5cf9a84f5
commit 564cd82c6b
7 changed files with 15 additions and 4 deletions

View File

@@ -12,7 +12,7 @@ An Email to Matrix bridge
- [x] Matrix bot - [x] Matrix bot
- [x] Configuration in room's account data - [x] Configuration in room's account data
- [x] Receive emails to matrix rooms - [x] Receive emails to matrix rooms
- [x] Receive attachments (untested) - [x] Receive attachments
- [ ] Map email threads to matrix threads - [ ] Map email threads to matrix threads
### Send ### Send
@@ -40,6 +40,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_MAXSIZE** - max email size (including attachments) in megabytes
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

@@ -33,6 +33,11 @@ func (b *Bot) parseCommand(message string) []string {
return nil return nil
} }
index := strings.LastIndex(message, b.prefix)
if index == -1 {
return nil
}
message = strings.TrimSpace(strings.Replace(message, b.prefix, "", 1)) message = strings.TrimSpace(strings.Replace(message, b.prefix, "", 1))
return strings.Split(message, " ") return strings.Split(message, " ")
} }

View File

@@ -40,7 +40,7 @@ func main() {
defer recovery() defer recovery()
go startBot() 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 //nolint:gocritic
log.Fatal("SMTP server crashed: %v", err) log.Fatal("SMTP server crashed: %v", err)
} }

View File

@@ -13,9 +13,11 @@ func New() *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),
Password: env.String("password", defaultConfig.Password), Password: env.String("password", defaultConfig.Password),
Prefix: env.String("prefix", defaultConfig.Prefix),
Domain: env.String("domain", defaultConfig.Domain), Domain: env.String("domain", defaultConfig.Domain),
Port: env.String("port", defaultConfig.Port), Port: env.String("port", defaultConfig.Port),
NoEncryption: env.Bool("noencryption"), NoEncryption: env.Bool("noencryption"),
MaxSize: env.Int("maxsize", defaultConfig.MaxSize),
Sentry: Sentry{ Sentry: Sentry{
DSN: env.String("sentry.dsn", defaultConfig.Sentry.DSN), DSN: env.String("sentry.dsn", defaultConfig.Sentry.DSN),
SampleRate: env.Int("sentry.rate", defaultConfig.Sentry.SampleRate), SampleRate: env.Int("sentry.rate", defaultConfig.Sentry.SampleRate),

View File

@@ -5,6 +5,7 @@ var defaultConfig = &Config{
Domain: "localhost", Domain: "localhost",
Port: "25", Port: "25",
Prefix: "!pm", Prefix: "!pm",
MaxSize: 1024,
DB: DB{ DB: DB{
DSN: "local.db", DSN: "local.db",
Dialect: "sqlite3", Dialect: "sqlite3",

View File

@@ -18,6 +18,8 @@ type Config struct {
NoEncryption bool NoEncryption bool
// Prefix for commands // Prefix for commands
Prefix string Prefix string
// MaxSize of an email (including attachments)
MaxSize int
// DB config // DB config
DB DB DB DB

View File

@@ -33,7 +33,7 @@ func (b *backend) AnonymousLogin(state *smtp.ConnectionState) (smtp.Session, err
return b.newSession(), nil 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) log := logger.New("smtp.", loglevel)
be := &backend{ be := &backend{
log: log, log: log,
@@ -46,7 +46,7 @@ func Start(domain, port, loglevel string, client Client) error {
s.AuthDisabled = true s.AuthDisabled = true
s.ReadTimeout = 10 * time.Second s.ReadTimeout = 10 * time.Second
s.WriteTimeout = 10 * time.Second s.WriteTimeout = 10 * time.Second
s.MaxMessageBytes = 128 * 1024 s.MaxMessageBytes = maxSize * 1024 * 1024
if log.GetLevel() == "DEBUG" || log.GetLevel() == "TRACE" { if log.GetLevel() == "DEBUG" || log.GetLevel() == "TRACE" {
s.Debug = os.Stdout s.Debug = os.Stdout
} }