2022-05-22 19:16:42 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"maunium.net/go/mautrix/id"
|
|
|
|
)
|
|
|
|
|
2023-08-22 15:55:09 +00:00
|
|
|
func (puppet *Puppet) SwitchCustomMXID(accessToken string, mxid id.UserID) error {
|
|
|
|
puppet.CustomMXID = mxid
|
|
|
|
puppet.AccessToken = accessToken
|
|
|
|
puppet.Update()
|
|
|
|
err := puppet.StartCustomMXID(false)
|
2022-05-22 19:16:42 +00:00
|
|
|
if err != nil {
|
2023-08-22 15:55:09 +00:00
|
|
|
return err
|
2022-05-22 19:16:42 +00:00
|
|
|
}
|
2023-08-22 15:55:09 +00:00
|
|
|
// TODO leave rooms with default puppet
|
|
|
|
return nil
|
2022-05-22 19:16:42 +00:00
|
|
|
}
|
|
|
|
|
2023-08-22 15:55:09 +00:00
|
|
|
func (puppet *Puppet) ClearCustomMXID() {
|
|
|
|
save := puppet.CustomMXID != "" || puppet.AccessToken != ""
|
|
|
|
puppet.bridge.puppetsLock.Lock()
|
|
|
|
if puppet.CustomMXID != "" && puppet.bridge.puppetsByCustomMXID[puppet.CustomMXID] == puppet {
|
|
|
|
delete(puppet.bridge.puppetsByCustomMXID, puppet.CustomMXID)
|
|
|
|
}
|
|
|
|
puppet.bridge.puppetsLock.Unlock()
|
2022-05-22 19:16:42 +00:00
|
|
|
puppet.CustomMXID = ""
|
|
|
|
puppet.AccessToken = ""
|
|
|
|
puppet.customIntent = nil
|
|
|
|
puppet.customUser = nil
|
2023-08-22 15:55:09 +00:00
|
|
|
if save {
|
|
|
|
puppet.Update()
|
2022-05-22 19:16:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (puppet *Puppet) StartCustomMXID(reloginOnFail bool) error {
|
2023-08-22 15:55:09 +00:00
|
|
|
newIntent, newAccessToken, err := puppet.bridge.DoublePuppet.Setup(puppet.CustomMXID, puppet.AccessToken, reloginOnFail)
|
2022-05-22 19:16:42 +00:00
|
|
|
if err != nil {
|
2023-08-22 15:55:09 +00:00
|
|
|
puppet.ClearCustomMXID()
|
2022-05-22 19:16:42 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-08-22 15:55:09 +00:00
|
|
|
puppet.bridge.puppetsLock.Lock()
|
|
|
|
puppet.bridge.puppetsByCustomMXID[puppet.CustomMXID] = puppet
|
|
|
|
puppet.bridge.puppetsLock.Unlock()
|
|
|
|
if puppet.AccessToken != newAccessToken {
|
|
|
|
puppet.AccessToken = newAccessToken
|
|
|
|
puppet.Update()
|
2022-05-22 19:16:42 +00:00
|
|
|
}
|
2023-08-22 15:55:09 +00:00
|
|
|
puppet.customIntent = newIntent
|
2022-05-22 19:16:42 +00:00
|
|
|
puppet.customUser = puppet.bridge.GetUserByMXID(puppet.CustomMXID)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-22 15:55:09 +00:00
|
|
|
func (user *User) tryAutomaticDoublePuppeting() {
|
|
|
|
if !user.bridge.Config.CanAutoDoublePuppet(user.MXID) {
|
|
|
|
return
|
2022-05-22 19:16:42 +00:00
|
|
|
}
|
2023-08-22 15:55:09 +00:00
|
|
|
user.log.Debug().Msg("Checking if double puppeting needs to be enabled")
|
|
|
|
puppet := user.bridge.GetPuppetByID(user.DiscordID)
|
|
|
|
if len(puppet.CustomMXID) > 0 {
|
|
|
|
user.log.Debug().Msg("User already has double-puppeting enabled")
|
|
|
|
// Custom puppet already enabled
|
|
|
|
return
|
2022-05-22 19:16:42 +00:00
|
|
|
}
|
2023-08-22 15:55:09 +00:00
|
|
|
puppet.CustomMXID = user.MXID
|
|
|
|
err := puppet.StartCustomMXID(true)
|
2022-05-22 19:16:42 +00:00
|
|
|
if err != nil {
|
2023-08-22 15:55:09 +00:00
|
|
|
user.log.Warn().Err(err).Msg("Failed to login with shared secret")
|
2022-08-16 14:13:14 +00:00
|
|
|
} else {
|
2023-08-22 15:55:09 +00:00
|
|
|
// TODO leave rooms with default puppet
|
|
|
|
user.log.Debug().Msg("Successfully automatically enabled custom puppet")
|
2022-05-22 19:16:42 +00:00
|
|
|
}
|
|
|
|
}
|