portal.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. "maunium.net/go/mautrix-whatsapp/database"
  19. log "maunium.net/go/maulogger"
  20. "fmt"
  21. "maunium.net/go/mautrix-whatsapp/types"
  22. "maunium.net/go/gomatrix"
  23. "strings"
  24. "maunium.net/go/mautrix-appservice"
  25. "github.com/Rhymen/go-whatsapp"
  26. )
  27. func (user *User) GetPortalByMXID(mxid types.MatrixRoomID) *Portal {
  28. portal, ok := user.portalsByMXID[mxid]
  29. if !ok {
  30. dbPortal := user.bridge.DB.Portal.GetByMXID(mxid)
  31. if dbPortal == nil || dbPortal.Owner != user.ID {
  32. return nil
  33. }
  34. portal = user.NewPortal(dbPortal)
  35. user.portalsByJID[portal.JID] = portal
  36. if len(portal.MXID) > 0 {
  37. user.portalsByMXID[portal.MXID] = portal
  38. }
  39. }
  40. return portal
  41. }
  42. func (user *User) GetPortalByJID(jid types.WhatsAppID) *Portal {
  43. portal, ok := user.portalsByJID[jid]
  44. if !ok {
  45. dbPortal := user.bridge.DB.Portal.GetByJID(user.ID, jid)
  46. if dbPortal == nil {
  47. dbPortal = user.bridge.DB.Portal.New()
  48. dbPortal.JID = jid
  49. dbPortal.Owner = user.ID
  50. dbPortal.Insert()
  51. }
  52. portal = user.NewPortal(dbPortal)
  53. user.portalsByJID[portal.JID] = portal
  54. if len(portal.MXID) > 0 {
  55. user.portalsByMXID[portal.MXID] = portal
  56. }
  57. }
  58. return portal
  59. }
  60. func (user *User) GetAllPortals() []*Portal {
  61. dbPortals := user.bridge.DB.Portal.GetAll(user.ID)
  62. output := make([]*Portal, len(dbPortals))
  63. for index, dbPortal := range dbPortals {
  64. portal, ok := user.portalsByJID[dbPortal.JID]
  65. if !ok {
  66. portal = user.NewPortal(dbPortal)
  67. user.portalsByJID[dbPortal.JID] = portal
  68. if len(dbPortal.MXID) > 0 {
  69. user.portalsByMXID[dbPortal.MXID] = portal
  70. }
  71. }
  72. output[index] = portal
  73. }
  74. return output
  75. }
  76. func (user *User) NewPortal(dbPortal *database.Portal) *Portal {
  77. return &Portal{
  78. Portal: dbPortal,
  79. user: user,
  80. bridge: user.bridge,
  81. log: user.log.Sub(fmt.Sprintf("Portal/%s", dbPortal.JID)),
  82. }
  83. }
  84. type Portal struct {
  85. *database.Portal
  86. user *User
  87. bridge *Bridge
  88. log log.Logger
  89. }
  90. func (portal *Portal) Sync(contact whatsapp.Contact) {
  91. if len(portal.MXID) == 0 {
  92. if !portal.IsPrivateChat() {
  93. portal.Name = contact.Name
  94. }
  95. err := portal.CreateMatrixRoom()
  96. if err != nil {
  97. portal.log.Errorln("Failed to create portal room:", err)
  98. return
  99. }
  100. }
  101. if !portal.IsPrivateChat() && portal.Name != contact.Name {
  102. portal.Name = contact.Name
  103. portal.Update()
  104. // TODO add SetRoomName function to intent API
  105. portal.MainIntent().SendStateEvent(portal.MXID, "m.room.name", "", map[string]interface{}{
  106. "name": portal.Name,
  107. })
  108. }
  109. }
  110. func (portal *Portal) CreateMatrixRoom() error {
  111. if len(portal.MXID) > 0 {
  112. return nil
  113. }
  114. name := portal.Name
  115. topic := ""
  116. isPrivateChat := false
  117. if strings.HasSuffix(portal.JID, "s.whatsapp.net") {
  118. puppet := portal.user.GetPuppetByJID(portal.JID)
  119. name = puppet.Displayname
  120. topic = "WhatsApp private chat"
  121. isPrivateChat = true
  122. }
  123. resp, err := portal.MainIntent().CreateRoom(&gomatrix.ReqCreateRoom{
  124. Visibility: "private",
  125. Name: name,
  126. Topic: topic,
  127. Invite: []string{portal.user.ID},
  128. Preset: "private_chat",
  129. IsDirect: isPrivateChat,
  130. })
  131. if err != nil {
  132. return err
  133. }
  134. portal.MXID = resp.RoomID
  135. portal.Update()
  136. return nil
  137. }
  138. func (portal *Portal) IsPrivateChat() bool {
  139. return strings.HasSuffix(portal.JID, puppetJIDStrippedSuffix)
  140. }
  141. func (portal *Portal) MainIntent() *appservice.IntentAPI {
  142. if portal.IsPrivateChat() {
  143. return portal.user.GetPuppetByJID(portal.JID).Intent()
  144. }
  145. return portal.bridge.AppService.BotIntent()
  146. }
  147. func (portal *Portal) HandleTextMessage(message whatsapp.TextMessage) {
  148. portal.CreateMatrixRoom()
  149. var intent *appservice.IntentAPI
  150. if portal.IsPrivateChat() {
  151. intent = portal.MainIntent()
  152. } else {
  153. portal.log.Debugln("Received group text message:", message)
  154. return
  155. }
  156. resp, err := intent.SendText(portal.MXID, message.Text)
  157. portal.log.Debugln("Handled message ", message, "->", resp, err)
  158. }
  159. func (portal *Portal) HandleMediaMessage(download func() ([]byte, error), msgID, mime, caption string) {
  160. portal.CreateMatrixRoom()
  161. var intent *appservice.IntentAPI
  162. if portal.IsPrivateChat() {
  163. intent = portal.MainIntent()
  164. } else {
  165. portal.log.Debugln("Received group media message:", msgID)
  166. return
  167. }
  168. img, err := download()
  169. if err != nil {
  170. portal.log.Errorln("Failed to download media:", err)
  171. return
  172. }
  173. uploaded, err := intent.UploadBytes(img, mime)
  174. if err != nil {
  175. portal.log.Errorln("Failed to upload media:", err)
  176. return
  177. }
  178. resp, err := intent.SendImage(portal.MXID, caption, uploaded.ContentURI)
  179. portal.log.Debugln("Handled message ", msgID, "->", resp, err)
  180. }
  181. func (portal *Portal) HandleMatrixMessage(evt *gomatrix.Event) {
  182. var err error
  183. switch evt.Content.MsgType {
  184. case gomatrix.MsgText:
  185. err = portal.user.Conn.Send(whatsapp.TextMessage{
  186. Text: evt.Content.Body,
  187. Info: whatsapp.MessageInfo{
  188. RemoteJid: portal.JID,
  189. },
  190. })
  191. default:
  192. portal.log.Debugln("Unhandled Matrix event:", evt)
  193. return
  194. }
  195. if err != nil {
  196. portal.log.Errorln("Error handling Matrix event %s: %v", evt.ID, err)
  197. } else {
  198. portal.log.Debugln("Handled Matrix event:", evt)
  199. }
  200. }