chat.go 3.7 KB

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