msginfo.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 whatsapp_ext
  17. import (
  18. "github.com/Rhymen/go-whatsapp"
  19. "encoding/json"
  20. "strings"
  21. )
  22. type MsgInfoCommand string
  23. const (
  24. MsgInfoCommandAcknowledge MsgInfoCommand = "ack"
  25. )
  26. type MsgInfo struct {
  27. Command MsgInfoCommand `json:"cmd"`
  28. ID string `json:"id"`
  29. Acknowledgement int `json:"ack"`
  30. MessageFromJID string `json:"from"`
  31. SenderJID string `json:"participant"`
  32. ToJID string `json:"to"`
  33. Timestamp int64 `json:"t"`
  34. }
  35. type MsgInfoHandler interface {
  36. whatsapp.Handler
  37. HandleMsgInfo(MsgInfo)
  38. }
  39. func (ext *ExtendedConn) handleMessageMsgInfo(message []byte) {
  40. var event MsgInfo
  41. err := json.Unmarshal(message, &event)
  42. if err != nil {
  43. ext.jsonParseError(err)
  44. return
  45. }
  46. event.MessageFromJID = strings.Replace(event.MessageFromJID, OldUserSuffix, NewUserSuffix, 1)
  47. event.SenderJID = strings.Replace(event.SenderJID, OldUserSuffix, NewUserSuffix, 1)
  48. event.ToJID = strings.Replace(event.ToJID, OldUserSuffix, NewUserSuffix, 1)
  49. for _, handler := range ext.handlers {
  50. msgInfoHandler, ok := handler.(MsgInfoHandler)
  51. if !ok {
  52. continue
  53. }
  54. msgInfoHandler.HandleMsgInfo(event)
  55. }
  56. }