properly update mailbox, fixes #9

This commit is contained in:
Aine
2022-08-28 09:37:18 +03:00
parent 6eae1a65c4
commit 13776ad7a6
3 changed files with 25 additions and 15 deletions

View File

@@ -78,20 +78,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,6 +43,25 @@ 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) {
b.rooms.Range(func(key, value any) bool {
b.log.Debug("MAPPING %v=%v", key, value)
return true
})
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))