puppet.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. "fmt"
  19. "net/http"
  20. "regexp"
  21. "strings"
  22. "github.com/Rhymen/go-whatsapp"
  23. log "maunium.net/go/maulogger/v2"
  24. "maunium.net/go/mautrix-appservice"
  25. "maunium.net/go/mautrix-whatsapp/database"
  26. "maunium.net/go/mautrix-whatsapp/types"
  27. "maunium.net/go/mautrix-whatsapp/whatsapp-ext"
  28. )
  29. func (bridge *Bridge) ParsePuppetMXID(mxid types.MatrixUserID) (types.WhatsAppID, bool) {
  30. userIDRegex, err := regexp.Compile(fmt.Sprintf("^@%s:%s$",
  31. bridge.Config.Bridge.FormatUsername("([0-9]+)"),
  32. bridge.Config.Homeserver.Domain))
  33. if err != nil {
  34. bridge.Log.Warnln("Failed to compile puppet user ID regex:", err)
  35. return "", false
  36. }
  37. match := userIDRegex.FindStringSubmatch(string(mxid))
  38. if match == nil || len(match) != 2 {
  39. return "", false
  40. }
  41. jid := types.WhatsAppID(match[1] + whatsappExt.NewUserSuffix)
  42. return jid, true
  43. }
  44. func (bridge *Bridge) GetPuppetByMXID(mxid types.MatrixUserID) *Puppet {
  45. jid, ok := bridge.ParsePuppetMXID(mxid)
  46. if !ok {
  47. return nil
  48. }
  49. return bridge.GetPuppetByJID(jid)
  50. }
  51. func (bridge *Bridge) GetPuppetByJID(jid types.WhatsAppID) *Puppet {
  52. bridge.puppetsLock.Lock()
  53. defer bridge.puppetsLock.Unlock()
  54. puppet, ok := bridge.puppets[jid]
  55. if !ok {
  56. dbPuppet := bridge.DB.Puppet.Get(jid)
  57. if dbPuppet == nil {
  58. dbPuppet = bridge.DB.Puppet.New()
  59. dbPuppet.JID = jid
  60. dbPuppet.Insert()
  61. }
  62. puppet = bridge.NewPuppet(dbPuppet)
  63. bridge.puppets[puppet.JID] = puppet
  64. if len(puppet.CustomMXID) > 0 {
  65. bridge.puppetsByCustomMXID[puppet.CustomMXID] = puppet
  66. }
  67. }
  68. return puppet
  69. }
  70. func (bridge *Bridge) GetPuppetByCustomMXID(mxid types.MatrixUserID) *Puppet {
  71. bridge.puppetsLock.Lock()
  72. defer bridge.puppetsLock.Unlock()
  73. puppet, ok := bridge.puppetsByCustomMXID[mxid]
  74. if !ok {
  75. dbPuppet := bridge.DB.Puppet.GetByCustomMXID(mxid)
  76. if dbPuppet == nil {
  77. return nil
  78. }
  79. puppet = bridge.NewPuppet(dbPuppet)
  80. bridge.puppets[puppet.JID] = puppet
  81. bridge.puppetsByCustomMXID[puppet.CustomMXID] = puppet
  82. }
  83. return puppet
  84. }
  85. func (bridge *Bridge) GetAllPuppetsWithCustomMXID() []*Puppet {
  86. return bridge.dbPuppetsToPuppets(bridge.DB.Puppet.GetAllWithCustomMXID())
  87. }
  88. func (bridge *Bridge) GetAllPuppets() []*Puppet {
  89. return bridge.dbPuppetsToPuppets(bridge.DB.Puppet.GetAll())
  90. }
  91. func (bridge *Bridge) dbPuppetsToPuppets(dbPuppets []*database.Puppet) []*Puppet {
  92. bridge.puppetsLock.Lock()
  93. defer bridge.puppetsLock.Unlock()
  94. output := make([]*Puppet, len(dbPuppets))
  95. for index, dbPuppet := range dbPuppets {
  96. puppet, ok := bridge.puppets[dbPuppet.JID]
  97. if !ok {
  98. puppet = bridge.NewPuppet(dbPuppet)
  99. bridge.puppets[dbPuppet.JID] = puppet
  100. if len(dbPuppet.CustomMXID) > 0 {
  101. bridge.puppetsByCustomMXID[dbPuppet.CustomMXID] = puppet
  102. }
  103. }
  104. output[index] = puppet
  105. }
  106. return output
  107. }
  108. func (bridge *Bridge) NewPuppet(dbPuppet *database.Puppet) *Puppet {
  109. return &Puppet{
  110. Puppet: dbPuppet,
  111. bridge: bridge,
  112. log: bridge.Log.Sub(fmt.Sprintf("Puppet/%s", dbPuppet.JID)),
  113. MXID: fmt.Sprintf("@%s:%s",
  114. bridge.Config.Bridge.FormatUsername(
  115. strings.Replace(
  116. dbPuppet.JID,
  117. whatsappExt.NewUserSuffix, "", 1)),
  118. bridge.Config.Homeserver.Domain),
  119. }
  120. }
  121. type Puppet struct {
  122. *database.Puppet
  123. bridge *Bridge
  124. log log.Logger
  125. typingIn types.MatrixRoomID
  126. typingAt int64
  127. MXID types.MatrixUserID
  128. customIntent *appservice.IntentAPI
  129. customTypingIn map[string]bool
  130. customUser *User
  131. }
  132. func (puppet *Puppet) PhoneNumber() string {
  133. return strings.Replace(puppet.JID, whatsappExt.NewUserSuffix, "", 1)
  134. }
  135. func (puppet *Puppet) IntentFor(portal *Portal) *appservice.IntentAPI {
  136. if puppet.customIntent == nil || portal.Key.JID == puppet.JID{
  137. return puppet.DefaultIntent()
  138. }
  139. return puppet.customIntent
  140. }
  141. func (puppet *Puppet) CustomIntent() *appservice.IntentAPI {
  142. return puppet.customIntent
  143. }
  144. func (puppet *Puppet) DefaultIntent() *appservice.IntentAPI {
  145. return puppet.bridge.AS.Intent(puppet.MXID)
  146. }
  147. func (puppet *Puppet) UpdateAvatar(source *User, avatar *whatsappExt.ProfilePicInfo) bool {
  148. if avatar == nil {
  149. var err error
  150. avatar, err = source.Conn.GetProfilePicThumb(puppet.JID)
  151. if err != nil {
  152. puppet.log.Warnln("Failed to get avatar:", err)
  153. return false
  154. }
  155. }
  156. if avatar.Status != 0 {
  157. return false
  158. }
  159. if avatar.Tag == puppet.Avatar {
  160. return false
  161. }
  162. if len(avatar.URL) == 0 {
  163. err := puppet.DefaultIntent().SetAvatarURL("")
  164. if err != nil {
  165. puppet.log.Warnln("Failed to remove avatar:", err)
  166. }
  167. puppet.Avatar = avatar.Tag
  168. return true
  169. }
  170. data, err := avatar.DownloadBytes()
  171. if err != nil {
  172. puppet.log.Warnln("Failed to download avatar:", err)
  173. return false
  174. }
  175. mime := http.DetectContentType(data)
  176. resp, err := puppet.DefaultIntent().UploadBytes(data, mime)
  177. if err != nil {
  178. puppet.log.Warnln("Failed to upload avatar:", err)
  179. return false
  180. }
  181. err = puppet.DefaultIntent().SetAvatarURL(resp.ContentURI)
  182. if err != nil {
  183. puppet.log.Warnln("Failed to set avatar:", err)
  184. }
  185. puppet.Avatar = avatar.Tag
  186. return true
  187. }
  188. func (puppet *Puppet) Sync(source *User, contact whatsapp.Contact) {
  189. err := puppet.DefaultIntent().EnsureRegistered()
  190. if err != nil {
  191. puppet.log.Errorln("Failed to ensure registered:", err)
  192. }
  193. if contact.Jid == source.JID {
  194. contact.Notify = source.Conn.Info.Pushname
  195. }
  196. newName, quality := puppet.bridge.Config.Bridge.FormatDisplayname(contact)
  197. if puppet.Displayname != newName && quality >= puppet.NameQuality {
  198. err := puppet.DefaultIntent().SetDisplayName(newName)
  199. if err == nil {
  200. puppet.Displayname = newName
  201. puppet.NameQuality = quality
  202. puppet.Update()
  203. } else {
  204. puppet.log.Warnln("Failed to set display name:", err)
  205. }
  206. }
  207. if puppet.UpdateAvatar(source, nil) {
  208. puppet.Update()
  209. }
  210. }