msginfo.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 MsgInfoCommand string
  23. const (
  24. MsgInfoCommandAck MsgInfoCommand = "ack"
  25. MsgInfoCommandAcks MsgInfoCommand = "acks"
  26. )
  27. type Acknowledgement int
  28. const (
  29. AckMessageSent Acknowledgement = 1
  30. AckMessageDelivered Acknowledgement = 2
  31. AckMessageRead Acknowledgement = 3
  32. )
  33. type JSONStringOrArray []string
  34. func (jsoa *JSONStringOrArray) UnmarshalJSON(data []byte) error {
  35. var str string
  36. if json.Unmarshal(data, &str) == nil {
  37. *jsoa = []string{str}
  38. return nil
  39. }
  40. var strs []string
  41. json.Unmarshal(data, &strs)
  42. *jsoa = strs
  43. return nil
  44. }
  45. type MsgInfo struct {
  46. Command MsgInfoCommand `json:"cmd"`
  47. IDs JSONStringOrArray `json:"id"`
  48. Acknowledgement Acknowledgement `json:"ack"`
  49. MessageFromJID string `json:"from"`
  50. SenderJID string `json:"participant"`
  51. ToJID string `json:"to"`
  52. Timestamp int64 `json:"t"`
  53. }
  54. type MsgInfoHandler interface {
  55. whatsapp.Handler
  56. HandleMsgInfo(MsgInfo)
  57. }
  58. func (ext *ExtendedConn) handleMessageMsgInfo(msgType JSONMessageType, message []byte) {
  59. var event MsgInfo
  60. err := json.Unmarshal(message, &event)
  61. if err != nil {
  62. ext.jsonParseError(err)
  63. return
  64. }
  65. event.MessageFromJID = strings.Replace(event.MessageFromJID, OldUserSuffix, NewUserSuffix, 1)
  66. event.SenderJID = strings.Replace(event.SenderJID, OldUserSuffix, NewUserSuffix, 1)
  67. event.ToJID = strings.Replace(event.ToJID, OldUserSuffix, NewUserSuffix, 1)
  68. if msgType == MessageMsg {
  69. event.SenderJID = event.ToJID
  70. }
  71. for _, handler := range ext.handlers {
  72. msgInfoHandler, ok := handler.(MsgInfoHandler)
  73. if !ok {
  74. continue
  75. }
  76. if ext.shouldCallSynchronously(msgInfoHandler) {
  77. msgInfoHandler.HandleMsgInfo(event)
  78. } else {
  79. go msgInfoHandler.HandleMsgInfo(event)
  80. }
  81. }
  82. }