lint; rearrange code
This commit is contained in:
@@ -53,7 +53,7 @@ linters-settings:
|
|||||||
sections:
|
sections:
|
||||||
- standard
|
- standard
|
||||||
- default
|
- default
|
||||||
- prefix(gitlab.com/etke.cc/int/scheduler)
|
- prefix(gitlab.com/etke.cc/postmoogle)
|
||||||
section-separators:
|
section-separators:
|
||||||
- newLine
|
- newLine
|
||||||
linters:
|
linters:
|
||||||
|
|||||||
@@ -10,10 +10,11 @@ import (
|
|||||||
"github.com/getsentry/sentry-go"
|
"github.com/getsentry/sentry-go"
|
||||||
"gitlab.com/etke.cc/go/logger"
|
"gitlab.com/etke.cc/go/logger"
|
||||||
"gitlab.com/etke.cc/linkpearl"
|
"gitlab.com/etke.cc/linkpearl"
|
||||||
"gitlab.com/etke.cc/postmoogle/utils"
|
|
||||||
"maunium.net/go/mautrix/event"
|
"maunium.net/go/mautrix/event"
|
||||||
"maunium.net/go/mautrix/format"
|
"maunium.net/go/mautrix/format"
|
||||||
"maunium.net/go/mautrix/id"
|
"maunium.net/go/mautrix/id"
|
||||||
|
|
||||||
|
"gitlab.com/etke.cc/postmoogle/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Bot represents matrix bot
|
// Bot represents matrix bot
|
||||||
|
|||||||
112
bot/command.go
112
bot/command.go
@@ -8,14 +8,29 @@ import (
|
|||||||
"maunium.net/go/mautrix/event"
|
"maunium.net/go/mautrix/event"
|
||||||
"maunium.net/go/mautrix/format"
|
"maunium.net/go/mautrix/format"
|
||||||
"maunium.net/go/mautrix/id"
|
"maunium.net/go/mautrix/id"
|
||||||
|
|
||||||
|
"gitlab.com/etke.cc/postmoogle/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
var commands = map[string]string{
|
type sanitizerFunc func(string) string
|
||||||
"mailbox": "Get or set mailbox of that room",
|
|
||||||
"owner": "Get or set owner of that room",
|
var (
|
||||||
"nosender": "Get or set `nosender` of that room (`true` - hide email sender; `false` - show email sender)",
|
commands = map[string]string{
|
||||||
"help": "Get help",
|
// special commands
|
||||||
}
|
"help": "Get help",
|
||||||
|
|
||||||
|
// options commands
|
||||||
|
"mailbox": "Get or set mailbox of that room",
|
||||||
|
"owner": "Get or set owner of that room",
|
||||||
|
"nosender": "Get or set `nosender` of that room (`true` - hide email sender; `false` - show email sender)",
|
||||||
|
}
|
||||||
|
|
||||||
|
// sanitizers is map of option name => sanitizer function
|
||||||
|
sanitizers = map[string]sanitizerFunc{
|
||||||
|
"mailbox": utils.Mailbox,
|
||||||
|
"nosender": utils.SanitizeBoolString,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
func (b *Bot) handleCommand(ctx context.Context, evt *event.Event, command []string) {
|
func (b *Bot) handleCommand(ctx context.Context, evt *event.Event, command []string) {
|
||||||
if _, ok := commands[command[0]]; !ok {
|
if _, ok := commands[command[0]]; !ok {
|
||||||
@@ -30,11 +45,7 @@ func (b *Bot) handleCommand(ctx context.Context, evt *event.Event, command []str
|
|||||||
switch command[0] {
|
switch command[0] {
|
||||||
case "help":
|
case "help":
|
||||||
b.sendHelp(ctx, evt.RoomID)
|
b.sendHelp(ctx, evt.RoomID)
|
||||||
case "owner":
|
default:
|
||||||
b.handleOption(ctx, evt, command)
|
|
||||||
case "mailbox":
|
|
||||||
b.handleOption(ctx, evt, command)
|
|
||||||
case "nosender":
|
|
||||||
b.handleOption(ctx, evt, command)
|
b.handleOption(ctx, evt, command)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -74,3 +85,82 @@ func (b *Bot) sendHelp(ctx context.Context, roomID id.RoomID) {
|
|||||||
b.Error(span.Context(), roomID, "cannot send message: %v", err)
|
b.Error(span.Context(), roomID, "cannot send message: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Bot) handleOption(ctx context.Context, evt *event.Event, command []string) {
|
||||||
|
if len(command) == 1 {
|
||||||
|
b.getOption(ctx, evt, command[0])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
b.setOption(ctx, evt, command[0], command[1])
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bot) getOption(ctx context.Context, evt *event.Event, name string) {
|
||||||
|
msg := "`%s` of this room is %s"
|
||||||
|
span := sentry.StartSpan(ctx, "http.server", sentry.TransactionName("getOption"))
|
||||||
|
defer span.Finish()
|
||||||
|
|
||||||
|
cfg, err := b.getSettings(span.Context(), evt.RoomID)
|
||||||
|
if err != nil {
|
||||||
|
b.Error(span.Context(), evt.RoomID, "failed to retrieve settings: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
value := cfg.Get(name)
|
||||||
|
if value == "" {
|
||||||
|
b.Notice(span.Context(), evt.RoomID, "`%s` is not set", name)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if name == "mailbox" {
|
||||||
|
msg = msg + "@" + b.domain
|
||||||
|
}
|
||||||
|
|
||||||
|
b.Notice(span.Context(), evt.RoomID, msg, name, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bot) setOption(ctx context.Context, evt *event.Event, name, value string) {
|
||||||
|
span := sentry.StartSpan(ctx, "http.server", sentry.TransactionName("setOption"))
|
||||||
|
defer span.Finish()
|
||||||
|
msg := "`%s` of this room set to %s"
|
||||||
|
|
||||||
|
sanitizer, ok := sanitizers[name]
|
||||||
|
if ok {
|
||||||
|
value = sanitizer(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
if name == "mailbox" {
|
||||||
|
existingID, ok := b.GetMapping(ctx, value)
|
||||||
|
if ok && existingID != "" && existingID != evt.RoomID {
|
||||||
|
b.Notice(span.Context(), evt.RoomID, "Mailbox %s@%s already taken", value, b.domain)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg, err := b.getSettings(span.Context(), evt.RoomID)
|
||||||
|
if err != nil {
|
||||||
|
b.Error(span.Context(), evt.RoomID, "failed to retrieve settings: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !cfg.Allowed(b.noowner, evt.Sender) {
|
||||||
|
b.Notice(span.Context(), evt.RoomID, "you don't have permission to do that")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg.Set(name, value)
|
||||||
|
if name == "mailbox" {
|
||||||
|
msg = msg + "@" + b.domain
|
||||||
|
cfg.Set("owner", evt.Sender.String())
|
||||||
|
b.roomsmu.Lock()
|
||||||
|
b.rooms[value] = evt.RoomID
|
||||||
|
b.roomsmu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
err = b.setSettings(span.Context(), evt.RoomID, cfg)
|
||||||
|
if err != nil {
|
||||||
|
b.Error(span.Context(), evt.RoomID, "cannot update settings: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
b.Notice(span.Context(), evt.RoomID, msg, name, value)
|
||||||
|
}
|
||||||
|
|||||||
@@ -13,9 +13,10 @@ const settingskey = "cc.etke.postmoogle.settings"
|
|||||||
|
|
||||||
var migrations = []string{}
|
var migrations = []string{}
|
||||||
|
|
||||||
|
// settings of a room
|
||||||
type settings map[string]string
|
type settings map[string]string
|
||||||
|
|
||||||
// settingsStruct of a room
|
// settingsOld of a room
|
||||||
type settingsOld struct {
|
type settingsOld struct {
|
||||||
Mailbox string
|
Mailbox string
|
||||||
Owner id.UserID
|
Owner id.UserID
|
||||||
|
|||||||
@@ -1,96 +0,0 @@
|
|||||||
package bot
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"github.com/getsentry/sentry-go"
|
|
||||||
"gitlab.com/etke.cc/postmoogle/utils"
|
|
||||||
"maunium.net/go/mautrix/event"
|
|
||||||
)
|
|
||||||
|
|
||||||
type sanitizerFunc func(string) string
|
|
||||||
|
|
||||||
// sanitizers is map of option name => sanitizer function
|
|
||||||
var sanitizers = map[string]sanitizerFunc{
|
|
||||||
"mailbox": utils.Mailbox,
|
|
||||||
"nosender": utils.SanitizeBoolString,
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Bot) handleOption(ctx context.Context, evt *event.Event, command []string) {
|
|
||||||
if len(command) == 1 {
|
|
||||||
b.getOption(ctx, evt, command[0])
|
|
||||||
return
|
|
||||||
}
|
|
||||||
b.setOption(ctx, evt, command[0], command[1])
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Bot) getOption(ctx context.Context, evt *event.Event, name string) {
|
|
||||||
msg := "`%s` of this room is %s"
|
|
||||||
span := sentry.StartSpan(ctx, "http.server", sentry.TransactionName("getOption"))
|
|
||||||
defer span.Finish()
|
|
||||||
|
|
||||||
cfg, err := b.getSettings(span.Context(), evt.RoomID)
|
|
||||||
if err != nil {
|
|
||||||
b.Error(span.Context(), evt.RoomID, "failed to retrieve settings: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
value := cfg.Get(name)
|
|
||||||
if value == "" {
|
|
||||||
b.Notice(span.Context(), evt.RoomID, "`%s` is not set", name)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if name == "mailbox" {
|
|
||||||
msg = msg + "@" + b.domain
|
|
||||||
}
|
|
||||||
|
|
||||||
b.Notice(span.Context(), evt.RoomID, msg, name, value)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Bot) setOption(ctx context.Context, evt *event.Event, name, value string) {
|
|
||||||
span := sentry.StartSpan(ctx, "http.server", sentry.TransactionName("setOption"))
|
|
||||||
defer span.Finish()
|
|
||||||
msg := "`%s` of this room set to %s"
|
|
||||||
|
|
||||||
sanitizer, ok := sanitizers[name]
|
|
||||||
if ok {
|
|
||||||
value = sanitizer(value)
|
|
||||||
}
|
|
||||||
|
|
||||||
if name == "mailbox" {
|
|
||||||
existingID, ok := b.GetMapping(ctx, value)
|
|
||||||
if ok && existingID != "" && existingID != evt.RoomID {
|
|
||||||
b.Notice(span.Context(), evt.RoomID, "Mailbox %s@%s already taken", value, b.domain)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cfg, err := b.getSettings(span.Context(), evt.RoomID)
|
|
||||||
if err != nil {
|
|
||||||
b.Error(span.Context(), evt.RoomID, "failed to retrieve settings: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if !cfg.Allowed(b.noowner, evt.Sender) {
|
|
||||||
b.Notice(span.Context(), evt.RoomID, "you don't have permission to do that")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
cfg.Set(name, value)
|
|
||||||
if name == "mailbox" {
|
|
||||||
msg = msg + "@" + b.domain
|
|
||||||
cfg.Set("owner", evt.Sender.String())
|
|
||||||
b.roomsmu.Lock()
|
|
||||||
b.rooms[value] = evt.RoomID
|
|
||||||
b.roomsmu.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
err = b.setSettings(span.Context(), evt.RoomID, cfg)
|
|
||||||
if err != nil {
|
|
||||||
b.Error(span.Context(), evt.RoomID, "cannot update settings: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
b.Notice(span.Context(), evt.RoomID, msg, name, value)
|
|
||||||
}
|
|
||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
"gitlab.com/etke.cc/go/logger"
|
"gitlab.com/etke.cc/go/logger"
|
||||||
"gitlab.com/etke.cc/linkpearl"
|
"gitlab.com/etke.cc/linkpearl"
|
||||||
lpcfg "gitlab.com/etke.cc/linkpearl/config"
|
lpcfg "gitlab.com/etke.cc/linkpearl/config"
|
||||||
|
|
||||||
"gitlab.com/etke.cc/postmoogle/bot"
|
"gitlab.com/etke.cc/postmoogle/bot"
|
||||||
"gitlab.com/etke.cc/postmoogle/config"
|
"gitlab.com/etke.cc/postmoogle/config"
|
||||||
"gitlab.com/etke.cc/postmoogle/smtp"
|
"gitlab.com/etke.cc/postmoogle/smtp"
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/getsentry/sentry-go"
|
"github.com/getsentry/sentry-go"
|
||||||
"github.com/jhillyerd/enmime"
|
"github.com/jhillyerd/enmime"
|
||||||
"gitlab.com/etke.cc/go/logger"
|
"gitlab.com/etke.cc/go/logger"
|
||||||
|
|
||||||
"gitlab.com/etke.cc/postmoogle/utils"
|
"gitlab.com/etke.cc/postmoogle/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -3,8 +3,9 @@ package smtp
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"gitlab.com/etke.cc/postmoogle/utils"
|
|
||||||
"maunium.net/go/mautrix/id"
|
"maunium.net/go/mautrix/id"
|
||||||
|
|
||||||
|
"gitlab.com/etke.cc/postmoogle/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Client interface to send emails
|
// Client interface to send emails
|
||||||
|
|||||||
Reference in New Issue
Block a user