puppet.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2018 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"
  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.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) != 3 {
  39. return "", "", false
  40. }
  41. receiver := types.MatrixUserID(match[1])
  42. receiver = strings.Replace(receiver, "=40", "@", 1)
  43. colonIndex := strings.LastIndex(receiver, "=3")
  44. receiver = receiver[:colonIndex] + ":" + receiver[colonIndex+len("=3"):]
  45. jid := types.WhatsAppID(match[2] + whatsappExt.NewUserSuffix)
  46. return receiver, jid, true
  47. }
  48. func (bridge *Bridge) GetPuppetByMXID(mxid types.MatrixUserID) *Puppet {
  49. receiver, jid, ok := bridge.ParsePuppetMXID(mxid)
  50. if !ok {
  51. return nil
  52. }
  53. user := bridge.GetUser(receiver)
  54. if user == nil {
  55. return nil
  56. }
  57. return user.GetPuppetByJID(jid)
  58. }
  59. func (user *User) GetPuppetByMXID(mxid types.MatrixUserID) *Puppet {
  60. receiver, jid, ok := user.bridge.ParsePuppetMXID(mxid)
  61. if !ok || receiver != user.ID {
  62. return nil
  63. }
  64. return user.GetPuppetByJID(jid)
  65. }
  66. func (user *User) GetPuppetByJID(jid types.WhatsAppID) *Puppet {
  67. user.puppetsLock.Lock()
  68. defer user.puppetsLock.Unlock()
  69. puppet, ok := user.puppets[jid]
  70. if !ok {
  71. dbPuppet := user.bridge.DB.Puppet.Get(jid, user.ID)
  72. if dbPuppet == nil {
  73. dbPuppet = user.bridge.DB.Puppet.New()
  74. dbPuppet.JID = jid
  75. dbPuppet.Receiver = user.ID
  76. dbPuppet.Insert()
  77. }
  78. puppet = user.NewPuppet(dbPuppet)
  79. user.puppets[puppet.JID] = puppet
  80. }
  81. return puppet
  82. }
  83. func (user *User) GetAllPuppets() []*Puppet {
  84. user.puppetsLock.Lock()
  85. defer user.puppetsLock.Unlock()
  86. dbPuppets := user.bridge.DB.Puppet.GetAll(user.ID)
  87. output := make([]*Puppet, len(dbPuppets))
  88. for index, dbPuppet := range dbPuppets {
  89. puppet, ok := user.puppets[dbPuppet.JID]
  90. if !ok {
  91. puppet = user.NewPuppet(dbPuppet)
  92. user.puppets[dbPuppet.JID] = puppet
  93. }
  94. output[index] = puppet
  95. }
  96. return output
  97. }
  98. func (user *User) NewPuppet(dbPuppet *database.Puppet) *Puppet {
  99. return &Puppet{
  100. Puppet: dbPuppet,
  101. user: user,
  102. bridge: user.bridge,
  103. log: user.log.Sub(fmt.Sprintf("Puppet/%s", dbPuppet.JID)),
  104. MXID: fmt.Sprintf("@%s:%s",
  105. user.bridge.Config.Bridge.FormatUsername(
  106. dbPuppet.Receiver,
  107. strings.Replace(
  108. dbPuppet.JID,
  109. whatsappExt.NewUserSuffix, "", 1)),
  110. user.bridge.Config.Homeserver.Domain),
  111. }
  112. }
  113. type Puppet struct {
  114. *database.Puppet
  115. user *User
  116. bridge *Bridge
  117. log log.Logger
  118. typingIn types.MatrixRoomID
  119. typingAt int64
  120. MXID types.MatrixUserID
  121. }
  122. func (puppet *Puppet) PhoneNumber() string {
  123. return strings.Replace(puppet.JID, whatsappExt.NewUserSuffix, "", 1)
  124. }
  125. func (puppet *Puppet) Intent() *appservice.IntentAPI {
  126. return puppet.bridge.AppService.Intent(puppet.MXID)
  127. }
  128. func (puppet *Puppet) UpdateAvatar(avatar *whatsappExt.ProfilePicInfo) bool {
  129. if avatar == nil {
  130. var err error
  131. avatar, err = puppet.user.Conn.GetProfilePicThumb(puppet.JID)
  132. if err != nil {
  133. puppet.log.Errorln(err)
  134. return false
  135. }
  136. }
  137. if avatar.Tag == puppet.Avatar {
  138. return false
  139. }
  140. if len(avatar.URL) == 0 {
  141. puppet.Intent().SetAvatarURL("")
  142. puppet.Avatar = avatar.Tag
  143. return true
  144. }
  145. data, err := avatar.DownloadBytes()
  146. if err != nil {
  147. puppet.log.Errorln("Failed to download avatar:", err)
  148. return false
  149. }
  150. mime := http.DetectContentType(data)
  151. resp, err := puppet.Intent().UploadBytes(data, mime)
  152. if err != nil {
  153. puppet.log.Errorln("Failed to upload avatar:", err)
  154. return false
  155. }
  156. puppet.Intent().SetAvatarURL(resp.ContentURI)
  157. puppet.Avatar = avatar.Tag
  158. return true
  159. }
  160. func (puppet *Puppet) Sync(contact whatsapp.Contact) {
  161. puppet.Intent().EnsureRegistered()
  162. newName := puppet.bridge.Config.Bridge.FormatDisplayname(contact)
  163. if puppet.Displayname != newName {
  164. err := puppet.Intent().SetDisplayName(newName)
  165. if err == nil {
  166. puppet.Displayname = newName
  167. puppet.Update()
  168. } else {
  169. puppet.log.Warnln("Failed to set display name:", err)
  170. }
  171. }
  172. if puppet.UpdateAvatar(nil) {
  173. puppet.Update()
  174. }
  175. }