chat.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 whatsappExt
  17. import (
  18. "encoding/json"
  19. "strings"
  20. "github.com/Rhymen/go-whatsapp"
  21. )
  22. type ChatUpdateCommand string
  23. const (
  24. ChatUpdateCommandAction ChatUpdateCommand = "action"
  25. )
  26. type ChatUpdate struct {
  27. JID string `json:"id"`
  28. Command ChatUpdateCommand `json:"cmd"`
  29. Data ChatUpdateData `json:"data"`
  30. }
  31. type ChatActionType string
  32. const (
  33. ChatActionNameChange ChatActionType = "subject"
  34. ChatActionAddTopic ChatActionType = "desc_add"
  35. ChatActionRemoveTopic ChatActionType = "desc_remove"
  36. ChatActionRestrict ChatActionType = "restrict"
  37. ChatActionAnnounce ChatActionType = "announce"
  38. ChatActionPromote ChatActionType = "promote"
  39. ChatActionDemote ChatActionType = "demote"
  40. ChatActionIntroduce ChatActionType = "introduce"
  41. )
  42. type ChatUpdateData struct {
  43. Action ChatActionType
  44. SenderJID string
  45. NameChange struct {
  46. Name string `json:"subject"`
  47. SetAt int64 `json:"s_t"`
  48. SetBy string `json:"s_o"`
  49. }
  50. AddTopic struct {
  51. Topic string `json:"desc"`
  52. ID string `json:"descId"`
  53. SetAt int64 `json:"descTime"`
  54. SetBy string `json:"descOwner"`
  55. }
  56. RemoveTopic struct {
  57. ID string `json:"descId"`
  58. }
  59. Introduce struct {
  60. CreationTime int64 `json:"creation"`
  61. Admins []string `json:"admins"`
  62. SuperAdmins []string `json:"superadmins"`
  63. Regulars []string `json:"regulars"`
  64. }
  65. Restrict bool
  66. Announce bool
  67. PermissionChange struct {
  68. JIDs []string `json:"participants"`
  69. }
  70. }
  71. func (cud *ChatUpdateData) UnmarshalJSON(data []byte) error {
  72. var arr []json.RawMessage
  73. err := json.Unmarshal(data, &arr)
  74. if err != nil {
  75. return err
  76. } else if len(arr) < 3 {
  77. return nil
  78. }
  79. err = json.Unmarshal(arr[0], &cud.Action)
  80. if err != nil {
  81. return err
  82. }
  83. err = json.Unmarshal(arr[1], &cud.SenderJID)
  84. if err != nil {
  85. return err
  86. }
  87. cud.SenderJID = strings.Replace(cud.SenderJID, OldUserSuffix, NewUserSuffix, 1)
  88. var unmarshalTo interface{}
  89. switch cud.Action {
  90. case ChatActionIntroduce:
  91. err = json.Unmarshal(arr[2], &cud.NameChange)
  92. if err != nil {
  93. return err
  94. }
  95. err = json.Unmarshal(arr[2], &cud.AddTopic)
  96. if err != nil {
  97. return err
  98. }
  99. unmarshalTo = &cud.Introduce
  100. case ChatActionNameChange:
  101. unmarshalTo = &cud.NameChange
  102. case ChatActionAddTopic:
  103. unmarshalTo = &cud.AddTopic
  104. case ChatActionRemoveTopic:
  105. unmarshalTo = &cud.RemoveTopic
  106. case ChatActionRestrict:
  107. unmarshalTo = &cud.Restrict
  108. case ChatActionAnnounce:
  109. unmarshalTo = &cud.Announce
  110. case ChatActionPromote, ChatActionDemote:
  111. unmarshalTo = &cud.PermissionChange
  112. default:
  113. return nil
  114. }
  115. err = json.Unmarshal(arr[2], unmarshalTo)
  116. if err != nil {
  117. return err
  118. }
  119. cud.NameChange.SetBy = strings.Replace(cud.NameChange.SetBy, OldUserSuffix, NewUserSuffix, 1)
  120. for index, jid := range cud.PermissionChange.JIDs {
  121. cud.PermissionChange.JIDs[index] = strings.Replace(jid, OldUserSuffix, NewUserSuffix, 1)
  122. }
  123. for index, jid := range cud.Introduce.SuperAdmins {
  124. cud.Introduce.SuperAdmins[index] = strings.Replace(jid, OldUserSuffix, NewUserSuffix, 1)
  125. }
  126. for index, jid := range cud.Introduce.Admins {
  127. cud.Introduce.Admins[index] = strings.Replace(jid, OldUserSuffix, NewUserSuffix, 1)
  128. }
  129. for index, jid := range cud.Introduce.Regulars {
  130. cud.Introduce.Regulars[index] = strings.Replace(jid, OldUserSuffix, NewUserSuffix, 1)
  131. }
  132. return nil
  133. }
  134. type ChatUpdateHandler interface {
  135. whatsapp.Handler
  136. HandleChatUpdate(ChatUpdate)
  137. }
  138. func (ext *ExtendedConn) handleMessageChatUpdate(message []byte) {
  139. var event ChatUpdate
  140. err := json.Unmarshal(message, &event)
  141. if err != nil {
  142. ext.jsonParseError(err)
  143. return
  144. }
  145. event.JID = strings.Replace(event.JID, OldUserSuffix, NewUserSuffix, 1)
  146. for _, handler := range ext.handlers {
  147. chatUpdateHandler, ok := handler.(ChatUpdateHandler)
  148. if !ok {
  149. continue
  150. }
  151. if ext.shouldCallSynchronously(chatUpdateHandler) {
  152. chatUpdateHandler.HandleChatUpdate(event)
  153. } else {
  154. go chatUpdateHandler.HandleChatUpdate(event)
  155. }
  156. }
  157. }