puppet.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. if dbPuppet == nil {
  97. continue
  98. }
  99. puppet, ok := bridge.puppets[dbPuppet.JID]
  100. if !ok {
  101. puppet = bridge.NewPuppet(dbPuppet)
  102. bridge.puppets[dbPuppet.JID] = puppet
  103. if len(dbPuppet.CustomMXID) > 0 {
  104. bridge.puppetsByCustomMXID[dbPuppet.CustomMXID] = puppet
  105. }
  106. }
  107. output[index] = puppet
  108. }
  109. return output
  110. }
  111. func (bridge *Bridge) NewPuppet(dbPuppet *database.Puppet) *Puppet {
  112. return &Puppet{
  113. Puppet: dbPuppet,
  114. bridge: bridge,
  115. log: bridge.Log.Sub(fmt.Sprintf("Puppet/%s", dbPuppet.JID)),
  116. MXID: fmt.Sprintf("@%s:%s",
  117. bridge.Config.Bridge.FormatUsername(
  118. strings.Replace(
  119. dbPuppet.JID,
  120. whatsappExt.NewUserSuffix, "", 1)),
  121. bridge.Config.Homeserver.Domain),
  122. }
  123. }
  124. type Puppet struct {
  125. *database.Puppet
  126. bridge *Bridge
  127. log log.Logger
  128. typingIn types.MatrixRoomID
  129. typingAt int64
  130. MXID types.MatrixUserID
  131. customIntent *appservice.IntentAPI
  132. customTypingIn map[string]bool
  133. customUser *User
  134. }
  135. func (puppet *Puppet) PhoneNumber() string {
  136. return strings.Replace(puppet.JID, whatsappExt.NewUserSuffix, "", 1)
  137. }
  138. func (puppet *Puppet) IntentFor(portal *Portal) *appservice.IntentAPI {
  139. if (!portal.IsPrivateChat() && puppet.customIntent == nil) || portal.backfilling || portal.Key.JID == puppet.JID {
  140. return puppet.DefaultIntent()
  141. }
  142. return puppet.customIntent
  143. }
  144. func (puppet *Puppet) CustomIntent() *appservice.IntentAPI {
  145. return puppet.customIntent
  146. }
  147. func (puppet *Puppet) DefaultIntent() *appservice.IntentAPI {
  148. return puppet.bridge.AS.Intent(puppet.MXID)
  149. }
  150. func (puppet *Puppet) UpdateAvatar(source *User, avatar *whatsappExt.ProfilePicInfo) bool {
  151. if avatar == nil {
  152. var err error
  153. avatar, err = source.Conn.GetProfilePicThumb(puppet.JID)
  154. if err != nil {
  155. puppet.log.Warnln("Failed to get avatar:", err)
  156. return false
  157. }
  158. }
  159. if avatar.Status != 0 {
  160. return false
  161. }
  162. if avatar.Tag == puppet.Avatar {
  163. return false
  164. }
  165. if len(avatar.URL) == 0 {
  166. err := puppet.DefaultIntent().SetAvatarURL("")
  167. if err != nil {
  168. puppet.log.Warnln("Failed to remove avatar:", err)
  169. }
  170. puppet.AvatarURL = ""
  171. puppet.Avatar = avatar.Tag
  172. go puppet.updatePortalAvatar()
  173. return true
  174. }
  175. data, err := avatar.DownloadBytes()
  176. if err != nil {
  177. puppet.log.Warnln("Failed to download avatar:", err)
  178. return false
  179. }
  180. mime := http.DetectContentType(data)
  181. resp, err := puppet.DefaultIntent().UploadBytes(data, mime)
  182. if err != nil {
  183. puppet.log.Warnln("Failed to upload avatar:", err)
  184. return false
  185. }
  186. puppet.AvatarURL = resp.ContentURI
  187. err = puppet.DefaultIntent().SetAvatarURL(puppet.AvatarURL)
  188. if err != nil {
  189. puppet.log.Warnln("Failed to set avatar:", err)
  190. }
  191. puppet.Avatar = avatar.Tag
  192. go puppet.updatePortalAvatar()
  193. return true
  194. }
  195. func (puppet *Puppet) UpdateName(source *User, contact whatsapp.Contact) bool {
  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. go puppet.updatePortalName()
  203. puppet.Update()
  204. } else {
  205. puppet.log.Warnln("Failed to set display name:", err)
  206. }
  207. return true
  208. }
  209. return false
  210. }
  211. func (puppet *Puppet) updatePortalMeta(meta func(portal *Portal)) {
  212. if puppet.bridge.Config.Bridge.PrivateChatPortalMeta {
  213. for _, portal := range puppet.bridge.GetAllPortalsByJID(puppet.JID) {
  214. meta(portal)
  215. }
  216. }
  217. }
  218. func (puppet *Puppet) updatePortalAvatar() {
  219. puppet.updatePortalMeta(func(portal *Portal) {
  220. if len(portal.MXID) > 0 {
  221. _, err := portal.MainIntent().SetRoomAvatar(portal.MXID, puppet.AvatarURL)
  222. if err != nil {
  223. portal.log.Warnln("Failed to set avatar:", err)
  224. }
  225. }
  226. portal.AvatarURL = puppet.AvatarURL
  227. portal.Avatar = puppet.Avatar
  228. portal.Update()
  229. })
  230. }
  231. func (puppet *Puppet) updatePortalName() {
  232. puppet.updatePortalMeta(func(portal *Portal) {
  233. if len(portal.MXID) > 0 {
  234. _, err := portal.MainIntent().SetRoomName(portal.MXID, puppet.Displayname)
  235. if err != nil {
  236. portal.log.Warnln("Failed to set name:", err)
  237. }
  238. }
  239. portal.Name = puppet.Displayname
  240. portal.Update()
  241. })
  242. }
  243. func (puppet *Puppet) Sync(source *User, contact whatsapp.Contact) {
  244. err := puppet.DefaultIntent().EnsureRegistered()
  245. if err != nil {
  246. puppet.log.Errorln("Failed to ensure registered:", err)
  247. }
  248. if contact.Jid == source.JID {
  249. contact.Notify = source.Conn.Info.Pushname
  250. }
  251. update := false
  252. update = puppet.UpdateName(source, contact) || update
  253. update = puppet.UpdateAvatar(source, nil) || update
  254. if update {
  255. puppet.Update()
  256. }
  257. }