Before: > * **`!pm help`** - Show this help message > * **`!pm stop`** - Disable bridge for the room and clear all configuration > * **`!pm mailbox`** - Get or set mailbox of the room > * **`!pm owner`** - Get or set owner of the room > * **`!pm nosender`** - Get or set `nosender` of the room (`true` - hide email sender; `false` - show email sender) > * **`!pm nosubject`** - Get or set `nosubject` of the room (`true` - hide email subject; `false` - show email subject) > * **`!pm nohtml`** - Get or set `nohtml` of the room (`true` - ignore HTML in email; `false` - parse HTML in emails) > * **`!pm nothreads`** - Get or set `nothreads` of the room (`true` - ignore email threads; `false` - convert email threads into matrix threads) > * **`!pm nofiles`** - Get or set `nofiles` of the room (`true` - ignore email attachments; `false` - upload email attachments After: > * **`!pm help`** - Show this help message > * **`!pm stop`** - Disable bridge for the room and clear all configuration > * **`!pm mailbox`** (currently `something@example.com`) - Get or set mailbox of the room > * **`!pm owner`** (currently `@someone:example.com`) - Get or set owner of the room > * **`!pm nosender`** (currently `false`) - Get or set `nosender` of the room (`true` - hide email sender; `false` - show email sender) > * **`!pm nosubject`** (currently `true`) - Get or set `nosubject` of the room (`true` - hide email subject; `false` - show email subject) > * **`!pm nohtml`** (currently `false`) - Get or set `nohtml` of the room (`true` - ignore HTML in email; `false` - parse HTML in emails) > * **`!pm nothreads`** (currently `false`) - Get or set `nothreads` of the room (`true` - ignore email threads; `false` - convert email threads into matrix threads) > * **`!pm nofiles`** (currently `false`) - Get or set `nofiles` of the room (`true` - ignore email attachments; `false` - upload email attachments)
140 lines
2.8 KiB
Go
140 lines
2.8 KiB
Go
package bot
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"maunium.net/go/mautrix/id"
|
|
|
|
"gitlab.com/etke.cc/postmoogle/utils"
|
|
)
|
|
|
|
// settings of a room
|
|
type settings map[string]string
|
|
|
|
// settingsOld of a room
|
|
type settingsOld struct {
|
|
Mailbox string
|
|
Owner id.UserID
|
|
NoSender bool
|
|
}
|
|
|
|
// Allowed checks if change is allowed
|
|
func (s settings) Allowed(noowner bool, userID id.UserID) bool {
|
|
if noowner {
|
|
return true
|
|
}
|
|
|
|
owner := s.Owner()
|
|
if owner == "" {
|
|
return true
|
|
}
|
|
|
|
return owner == userID.String()
|
|
}
|
|
|
|
// Get option
|
|
func (s settings) Get(key string) string {
|
|
value := s[strings.ToLower(strings.TrimSpace(key))]
|
|
|
|
sanitizer, ok := sanitizers[key]
|
|
if ok {
|
|
return sanitizer(value)
|
|
}
|
|
return value
|
|
}
|
|
|
|
func (s settings) Mailbox() string {
|
|
return s.Get(optionMailbox)
|
|
}
|
|
|
|
func (s settings) Owner() string {
|
|
return s.Get(optionOwner)
|
|
}
|
|
|
|
func (s settings) NoSender() bool {
|
|
return utils.Bool(s.Get(optionNoSender))
|
|
}
|
|
|
|
func (s settings) NoSubject() bool {
|
|
return utils.Bool(s.Get(optionNoSubject))
|
|
}
|
|
|
|
func (s settings) NoHTML() bool {
|
|
return utils.Bool(s.Get(optionNoHTML))
|
|
}
|
|
|
|
func (s settings) NoThreads() bool {
|
|
return utils.Bool(s.Get(optionNoThreads))
|
|
}
|
|
|
|
func (s settings) NoFiles() bool {
|
|
return utils.Bool(s.Get(optionNoFiles))
|
|
}
|
|
|
|
// Set option
|
|
func (s settings) Set(key, value string) {
|
|
s[strings.ToLower(strings.TrimSpace(key))] = value
|
|
}
|
|
|
|
// TODO: remove after migration
|
|
func (b *Bot) migrateSettings(roomID id.RoomID) {
|
|
var config settingsOld
|
|
err := b.lp.GetClient().GetRoomAccountData(roomID, settingskey, &config)
|
|
if err != nil {
|
|
// any error = no need to migrate
|
|
return
|
|
}
|
|
|
|
if config.Mailbox == "" {
|
|
return
|
|
}
|
|
cfg := settings{}
|
|
cfg.Set(optionMailbox, config.Mailbox)
|
|
cfg.Set(optionOwner, config.Owner.String())
|
|
cfg.Set(optionNoSender, strconv.FormatBool(config.NoSender))
|
|
|
|
err = b.setSettings(roomID, cfg)
|
|
if err != nil {
|
|
b.log.Error("cannot migrate settings: %v", err)
|
|
}
|
|
}
|
|
|
|
func (b *Bot) getSettings(roomID id.RoomID) (settings, error) {
|
|
config := settings{}
|
|
err := b.lp.GetClient().GetRoomAccountData(roomID, settingskey, &config)
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "M_NOT_FOUND") {
|
|
// Suppress `M_NOT_FOUND (HTTP 404): Room account data not found` errors.
|
|
// Until some settings are explicitly set, we don't store any.
|
|
// In such cases, just return a default (empty) settings object.
|
|
err = nil
|
|
}
|
|
}
|
|
|
|
return config, err
|
|
}
|
|
|
|
func (b *Bot) getSettingsOption(roomID id.RoomID, name string) (any, error) {
|
|
cfg, err := b.getSettings(roomID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to retrieve settings: %v", err)
|
|
}
|
|
|
|
value := cfg.Get(name)
|
|
if value == "" {
|
|
return nil, nil
|
|
}
|
|
|
|
if name == optionMailbox {
|
|
value = fmt.Sprintf("%s@%s", value, b.domain)
|
|
}
|
|
|
|
return value, nil
|
|
}
|
|
|
|
func (b *Bot) setSettings(roomID id.RoomID, cfg settings) error {
|
|
return b.lp.GetClient().SetRoomAccountData(roomID, settingskey, cfg)
|
|
}
|