Merge branch 'main' into 'user-whitelisting'

# Conflicts:
#   bot/bot.go
This commit is contained in:
Slavi Pantaleev
2022-08-28 14:50:57 +00:00
8 changed files with 131 additions and 28 deletions

View File

@@ -24,20 +24,22 @@ type Bot struct {
rooms sync.Map
log *logger.Logger
lp *linkpearl.Linkpearl
mu map[id.RoomID]*sync.Mutex
handledMembershipEvents sync.Map
}
// New creates a new matrix bot
func New(lp *linkpearl.Linkpearl, log *logger.Logger, prefix, domain string, noowner, federation bool, allowedUsers []*regexp.Regexp) *Bot {
return &Bot{
noowner: noowner,
federation: federation,
prefix: prefix,
domain: domain,
noowner: noowner,
federation: federation,
prefix: prefix,
domain: domain,
allowedUsers: allowedUsers,
rooms: sync.Map{},
log: log,
lp: lp,
rooms: sync.Map{},
log: log,
lp: lp,
mu: map[id.RoomID]*sync.Mutex{},
}
}
@@ -79,20 +81,6 @@ func (b *Bot) Start(statusMsg string) error {
return b.lp.Start(statusMsg)
}
// GetMappings returns mapping of mailbox = room
func (b *Bot) GetMapping(mailbox string) (id.RoomID, bool) {
v, ok := b.rooms.Load(mailbox)
if !ok {
return "", ok
}
roomID, ok := v.(id.RoomID)
if !ok {
return "", ok
}
return roomID, ok
}
// Stop the bot
func (b *Bot) Stop() {
err := b.lp.GetClient().SetPresence(event.PresenceOffline)

View File

@@ -256,11 +256,16 @@ func (b *Bot) setOption(ctx context.Context, name, value string) {
return
}
old := cfg.Get(name)
cfg.Set(name, value)
if name == optionMailbox {
value = fmt.Sprintf("%s@%s", value, b.domain)
cfg.Set(optionOwner, evt.Sender.String())
if old != "" {
b.rooms.Delete(old)
}
b.rooms.Store(value, evt.RoomID)
value = fmt.Sprintf("%s@%s", value, b.domain)
}
err = b.setSettings(evt.RoomID, cfg)

View File

@@ -43,12 +43,28 @@ func email2content(email *utils.Email, cfg settings, threadID id.EventID) *event
return &content
}
// GetMapping returns mapping of mailbox = room
func (b *Bot) GetMapping(mailbox string) (id.RoomID, bool) {
v, ok := b.rooms.Load(mailbox)
if !ok {
return "", ok
}
roomID, ok := v.(id.RoomID)
if !ok {
return "", ok
}
return roomID, ok
}
// Send email to matrix room
func (b *Bot) Send(ctx context.Context, email *utils.Email) error {
roomID, ok := b.GetMapping(utils.Mailbox(email.To))
if !ok {
return errors.New("room not found")
}
b.lock(roomID)
defer b.unlock(roomID)
cfg, err := b.getSettings(roomID)
if err != nil {
@@ -66,7 +82,7 @@ func (b *Bot) Send(ctx context.Context, email *utils.Email) error {
content := email2content(email, cfg, threadID)
eventID, serr := b.lp.Send(roomID, content)
if serr != nil {
return serr
return utils.UnwrapError(serr)
}
if threadID == "" && !cfg.NoThreads() {

26
bot/mutext.go Normal file
View File

@@ -0,0 +1,26 @@
package bot
import (
"sync"
"maunium.net/go/mautrix/id"
)
func (b *Bot) lock(roomID id.RoomID) {
_, ok := b.mu[roomID]
if !ok {
b.mu[roomID] = &sync.Mutex{}
}
b.mu[roomID].Lock()
}
func (b *Bot) unlock(roomID id.RoomID) {
_, ok := b.mu[roomID]
if !ok {
return
}
b.mu[roomID].Unlock()
delete(b.mu, roomID)
}

View File

@@ -117,9 +117,9 @@ func (b *Bot) getSettings(roomID id.RoomID) (settings, error) {
}
}
return config, err
return config, utils.UnwrapError(err)
}
func (b *Bot) setSettings(roomID id.RoomID, cfg settings) error {
return b.lp.GetClient().SetRoomAccountData(roomID, settingskey, cfg)
return utils.UnwrapError(b.lp.GetClient().SetRoomAccountData(roomID, settingskey, cfg))
}