custompuppet.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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.Store = puppet
  60. ia := puppet.bridge.AS.NewIntentAPI("custom")
  61. ia.Client = client
  62. ia.Localpart = puppet.CustomMXID[1:strings.IndexRune(puppet.CustomMXID, ':')]
  63. ia.UserID = puppet.CustomMXID
  64. ia.IsCustomPuppet = true
  65. return ia, nil
  66. }
  67. func (puppet *Puppet) StartCustomMXID() error {
  68. if len(puppet.CustomMXID) == 0 {
  69. return nil
  70. }
  71. intent, err := puppet.newCustomIntent()
  72. if err != nil {
  73. puppet.CustomMXID = ""
  74. puppet.AccessToken = ""
  75. return err
  76. }
  77. urlPath := intent.BuildURL("account", "whoami")
  78. var resp struct{ UserID string `json:"user_id"` }
  79. _, err = intent.MakeRequest("GET", urlPath, nil, &resp)
  80. if err != nil {
  81. puppet.CustomMXID = ""
  82. puppet.AccessToken = ""
  83. return err
  84. }
  85. if resp.UserID != puppet.CustomMXID {
  86. puppet.CustomMXID = ""
  87. puppet.AccessToken = ""
  88. return ErrMismatchingMXID
  89. }
  90. puppet.customIntent = intent
  91. puppet.startSyncing()
  92. return nil
  93. }
  94. func (puppet *Puppet) startSyncing() {
  95. if !puppet.bridge.Config.Bridge.SyncWithCustomPuppets {
  96. return
  97. }
  98. go func() {
  99. puppet.log.Debugln("Starting syncing...")
  100. err := puppet.customIntent.Sync()
  101. if err != nil {
  102. puppet.log.Errorln("Fatal error syncing:", err)
  103. }
  104. }()
  105. }
  106. func (puppet *Puppet) stopSyncing() {
  107. if !puppet.bridge.Config.Bridge.SyncWithCustomPuppets {
  108. return
  109. }
  110. puppet.customIntent.StopSync()
  111. }
  112. func (puppet *Puppet) ProcessResponse(resp *mautrix.RespSync, since string) error {
  113. puppet.log.Debugln("Sync data:", resp, since)
  114. // TODO handle sync data
  115. return nil
  116. }
  117. func (puppet *Puppet) OnFailedSync(res *mautrix.RespSync, err error) (time.Duration, error) {
  118. puppet.log.Warnln("Sync error:", err)
  119. return 10 * time.Second, nil
  120. }
  121. func (puppet *Puppet) GetFilterJSON(_ string) json.RawMessage {
  122. mxid, _ := json.Marshal(puppet.CustomMXID)
  123. return json.RawMessage(fmt.Sprintf(`{
  124. "account_data": { "types": [] },
  125. "presence": {
  126. "senders": [
  127. %s
  128. ],
  129. "types": [
  130. "m.presence"
  131. ]
  132. },
  133. "room": {
  134. "ephemeral": {
  135. "types": [
  136. "m.typing",
  137. "m.receipt"
  138. ]
  139. },
  140. "include_leave": false,
  141. "account_data": { "types": [] },
  142. "state": { "types": [] },
  143. "timeline": { "types": [] }
  144. }
  145. }`, mxid))
  146. }
  147. func (puppet *Puppet) SaveFilterID(_, _ string) {}
  148. func (puppet *Puppet) SaveNextBatch(_, nbt string) { puppet.NextBatch = nbt }
  149. func (puppet *Puppet) SaveRoom(room *mautrix.Room) {}
  150. func (puppet *Puppet) LoadFilterID(_ string) string { return "" }
  151. func (puppet *Puppet) LoadNextBatch(_ string) string { return puppet.NextBatch }
  152. func (puppet *Puppet) LoadRoom(roomID string) *mautrix.Room { return nil }