custompuppet.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2021 Tulir Asokan
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "maunium.net/go/mautrix/id"
  19. )
  20. func (puppet *Puppet) SwitchCustomMXID(accessToken string, mxid id.UserID) error {
  21. puppet.CustomMXID = mxid
  22. puppet.AccessToken = accessToken
  23. puppet.EnablePresence = puppet.bridge.Config.Bridge.DefaultBridgePresence
  24. puppet.Update()
  25. err := puppet.StartCustomMXID(false)
  26. if err != nil {
  27. return err
  28. }
  29. // TODO leave rooms with default puppet
  30. return nil
  31. }
  32. func (puppet *Puppet) ClearCustomMXID() {
  33. save := puppet.CustomMXID != "" || puppet.AccessToken != ""
  34. puppet.bridge.puppetsLock.Lock()
  35. if puppet.CustomMXID != "" && puppet.bridge.puppetsByCustomMXID[puppet.CustomMXID] == puppet {
  36. delete(puppet.bridge.puppetsByCustomMXID, puppet.CustomMXID)
  37. }
  38. puppet.bridge.puppetsLock.Unlock()
  39. puppet.CustomMXID = ""
  40. puppet.AccessToken = ""
  41. puppet.customIntent = nil
  42. puppet.customUser = nil
  43. if save {
  44. puppet.Update()
  45. }
  46. }
  47. func (puppet *Puppet) StartCustomMXID(reloginOnFail bool) error {
  48. newIntent, newAccessToken, err := puppet.bridge.DoublePuppet.Setup(puppet.CustomMXID, puppet.AccessToken, reloginOnFail)
  49. if err != nil {
  50. puppet.ClearCustomMXID()
  51. return err
  52. }
  53. puppet.bridge.puppetsLock.Lock()
  54. puppet.bridge.puppetsByCustomMXID[puppet.CustomMXID] = puppet
  55. puppet.bridge.puppetsLock.Unlock()
  56. if puppet.AccessToken != newAccessToken {
  57. puppet.AccessToken = newAccessToken
  58. puppet.Update()
  59. }
  60. puppet.customIntent = newIntent
  61. puppet.customUser = puppet.bridge.GetUserByMXID(puppet.CustomMXID)
  62. return nil
  63. }
  64. func (user *User) tryAutomaticDoublePuppeting() {
  65. if !user.bridge.Config.CanAutoDoublePuppet(user.MXID) {
  66. return
  67. }
  68. user.zlog.Debug().Msg("Checking if double puppeting needs to be enabled")
  69. puppet := user.bridge.GetPuppetByJID(user.JID)
  70. if len(puppet.CustomMXID) > 0 {
  71. user.zlog.Debug().Msg("User already has double-puppeting enabled")
  72. // Custom puppet already enabled
  73. return
  74. }
  75. puppet.CustomMXID = user.MXID
  76. puppet.EnablePresence = puppet.bridge.Config.Bridge.DefaultBridgePresence
  77. err := puppet.StartCustomMXID(true)
  78. if err != nil {
  79. user.zlog.Warn().Err(err).Msg("Failed to login with shared secret")
  80. } else {
  81. // TODO leave rooms with default puppet
  82. user.zlog.Debug().Msg("Successfully automatically enabled custom puppet")
  83. }
  84. }