custompuppet.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.CustomMXID = ""
  35. puppet.AccessToken = ""
  36. puppet.customIntent = nil
  37. puppet.customUser = nil
  38. if save {
  39. puppet.Update()
  40. }
  41. }
  42. func (puppet *Puppet) StartCustomMXID(reloginOnFail bool) error {
  43. newIntent, newAccessToken, err := puppet.bridge.DoublePuppet.Setup(puppet.CustomMXID, puppet.AccessToken, reloginOnFail)
  44. if err != nil {
  45. puppet.ClearCustomMXID()
  46. return err
  47. }
  48. if puppet.AccessToken != newAccessToken {
  49. puppet.AccessToken = newAccessToken
  50. puppet.Update()
  51. }
  52. puppet.customIntent = newIntent
  53. puppet.customUser = puppet.bridge.GetUserByMXID(puppet.CustomMXID)
  54. return nil
  55. }
  56. func (user *User) tryAutomaticDoublePuppeting() {
  57. if !user.bridge.Config.CanAutoDoublePuppet(user.MXID) {
  58. return
  59. }
  60. user.zlog.Debug().Msg("Checking if double puppeting needs to be enabled")
  61. puppet := user.bridge.GetPuppetByJID(user.JID)
  62. if len(puppet.CustomMXID) > 0 {
  63. user.zlog.Debug().Msg("User already has double-puppeting enabled")
  64. // Custom puppet already enabled
  65. return
  66. }
  67. puppet.CustomMXID = user.MXID
  68. puppet.EnablePresence = puppet.bridge.Config.Bridge.DefaultBridgePresence
  69. err := puppet.StartCustomMXID(true)
  70. if err != nil {
  71. user.zlog.Warn().Err(err).Msg("Failed to login with shared secret")
  72. } else {
  73. // TODO leave rooms with default puppet
  74. user.zlog.Debug().Msg("Successfully automatically enabled custom puppet")
  75. }
  76. }