custompuppet.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2019 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. "encoding/json"
  19. "fmt"
  20. "strings"
  21. "time"
  22. "github.com/pkg/errors"
  23. "maunium.net/go/mautrix"
  24. "maunium.net/go/mautrix-appservice"
  25. )
  26. var (
  27. ErrNoCustomMXID = errors.New("no custom mxid set")
  28. ErrMismatchingMXID = errors.New("whoami result does not match custom mxid")
  29. )
  30. func (puppet *Puppet) SwitchCustomMXID(accessToken string, mxid string) error {
  31. prevCustomMXID := puppet.CustomMXID
  32. if puppet.customIntent != nil {
  33. puppet.stopSyncing()
  34. }
  35. puppet.CustomMXID = mxid
  36. puppet.AccessToken = accessToken
  37. err := puppet.StartCustomMXID()
  38. if err != nil {
  39. return err
  40. }
  41. if len(prevCustomMXID) > 0 {
  42. delete(puppet.bridge.puppetsByCustomMXID, prevCustomMXID)
  43. }
  44. if len(puppet.CustomMXID) > 0 {
  45. puppet.bridge.puppetsByCustomMXID[puppet.CustomMXID] = puppet
  46. }
  47. puppet.Update()
  48. // TODO leave rooms with default puppet
  49. return nil
  50. }
  51. func (puppet *Puppet) newCustomIntent() (*appservice.IntentAPI, error) {
  52. if len(puppet.CustomMXID) == 0 {
  53. return nil, ErrNoCustomMXID
  54. }
  55. client, err := mautrix.NewClient(puppet.bridge.AS.HomeserverURL, puppet.CustomMXID, puppet.AccessToken)
  56. if err != nil {
  57. return nil, err
  58. }
  59. client.Logger = puppet.bridge.AS.Log.Sub(puppet.CustomMXID)
  60. client.Syncer = puppet
  61. client.Store = puppet
  62. ia := puppet.bridge.AS.NewIntentAPI("custom")
  63. ia.Client = client
  64. ia.Localpart = puppet.CustomMXID[1:strings.IndexRune(puppet.CustomMXID, ':')]
  65. ia.UserID = puppet.CustomMXID
  66. ia.IsCustomPuppet = true
  67. return ia, nil
  68. }
  69. func (puppet *Puppet) StartCustomMXID() error {
  70. if len(puppet.CustomMXID) == 0 {
  71. return nil
  72. }
  73. intent, err := puppet.newCustomIntent()
  74. if err != nil {
  75. puppet.CustomMXID = ""
  76. puppet.AccessToken = ""
  77. return err
  78. }
  79. urlPath := intent.BuildURL("account", "whoami")
  80. var resp struct{ UserID string `json:"user_id"` }
  81. _, err = intent.MakeRequest("GET", urlPath, nil, &resp)
  82. if err != nil {
  83. puppet.CustomMXID = ""
  84. puppet.AccessToken = ""
  85. return err
  86. }
  87. if resp.UserID != puppet.CustomMXID {
  88. puppet.CustomMXID = ""
  89. puppet.AccessToken = ""
  90. return ErrMismatchingMXID
  91. }
  92. puppet.customIntent = intent
  93. puppet.startSyncing()
  94. return nil
  95. }
  96. func (puppet *Puppet) startSyncing() {
  97. if !puppet.bridge.Config.Bridge.SyncWithCustomPuppets {
  98. return
  99. }
  100. go func() {
  101. puppet.log.Debugln("Starting syncing...")
  102. err := puppet.customIntent.Sync()
  103. if err != nil {
  104. puppet.log.Errorln("Fatal error syncing:", err)
  105. }
  106. }()
  107. }
  108. func (puppet *Puppet) stopSyncing() {
  109. if !puppet.bridge.Config.Bridge.SyncWithCustomPuppets {
  110. return
  111. }
  112. puppet.customIntent.StopSync()
  113. }
  114. func (puppet *Puppet) ProcessResponse(resp *mautrix.RespSync, since string) error {
  115. d, _ := json.Marshal(resp)
  116. puppet.log.Debugln("Sync data:", string(d), since)
  117. // TODO handle sync data
  118. return nil
  119. }
  120. func (puppet *Puppet) OnFailedSync(res *mautrix.RespSync, err error) (time.Duration, error) {
  121. puppet.log.Warnln("Sync error:", err)
  122. return 10 * time.Second, nil
  123. }
  124. func (puppet *Puppet) GetFilterJSON(_ string) json.RawMessage {
  125. mxid, _ := json.Marshal(puppet.CustomMXID)
  126. return json.RawMessage(fmt.Sprintf(`{
  127. "account_data": { "types": [] },
  128. "presence": {
  129. "senders": [
  130. %s
  131. ],
  132. "types": [
  133. "m.presence"
  134. ]
  135. },
  136. "room": {
  137. "ephemeral": {
  138. "types": [
  139. "m.typing",
  140. "m.receipt"
  141. ]
  142. },
  143. "include_leave": false,
  144. "account_data": { "types": [] },
  145. "state": { "types": [] },
  146. "timeline": { "types": [] }
  147. }
  148. }`, mxid))
  149. }
  150. func (puppet *Puppet) SaveFilterID(_, _ string) {}
  151. func (puppet *Puppet) SaveNextBatch(_, nbt string) { puppet.NextBatch = nbt; puppet.Update() }
  152. func (puppet *Puppet) SaveRoom(room *mautrix.Room) {}
  153. func (puppet *Puppet) LoadFilterID(_ string) string { return "" }
  154. func (puppet *Puppet) LoadNextBatch(_ string) string { return puppet.NextBatch }
  155. func (puppet *Puppet) LoadRoom(roomID string) *mautrix.Room { return nil }