chat.go 4.7 KB

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