jsonmessage.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 whatsappExt
  17. import (
  18. "encoding/json"
  19. "github.com/Rhymen/go-whatsapp"
  20. )
  21. type JSONMessage []json.RawMessage
  22. type JSONMessageType string
  23. const (
  24. MessageMsgInfo JSONMessageType = "MsgInfo"
  25. MessageMsg JSONMessageType = "Msg"
  26. MessagePresence JSONMessageType = "Presence"
  27. MessageStream JSONMessageType = "Stream"
  28. MessageConn JSONMessageType = "Conn"
  29. MessageProps JSONMessageType = "Props"
  30. MessageCmd JSONMessageType = "Cmd"
  31. MessageChat JSONMessageType = "Chat"
  32. )
  33. func (ext *ExtendedConn) AddHandler(handler whatsapp.Handler) {
  34. ext.Conn.AddHandler(handler)
  35. ext.handlers = append(ext.handlers, handler)
  36. }
  37. func (ext *ExtendedConn) HandleError(error) {}
  38. type UnhandledJSONMessageHandler interface {
  39. whatsapp.Handler
  40. HandleUnhandledJSONMessage(string)
  41. }
  42. type JSONParseErrorHandler interface {
  43. whatsapp.Handler
  44. HandleJSONParseError(error)
  45. }
  46. func (ext *ExtendedConn) jsonParseError(err error) {
  47. for _, handler := range ext.handlers {
  48. errorHandler, ok := handler.(JSONParseErrorHandler)
  49. if !ok {
  50. continue
  51. }
  52. errorHandler.HandleJSONParseError(err)
  53. }
  54. }
  55. func (ext *ExtendedConn) HandleJsonMessage(message string) {
  56. msg := JSONMessage{}
  57. err := json.Unmarshal([]byte(message), &msg)
  58. if err != nil || len(msg) < 2 {
  59. return
  60. }
  61. var msgType JSONMessageType
  62. json.Unmarshal(msg[0], &msgType)
  63. switch msgType {
  64. case MessagePresence:
  65. ext.handleMessagePresence(msg[1])
  66. case MessageStream:
  67. ext.handleMessageStream(msg[1:])
  68. case MessageConn:
  69. ext.handleMessageConn(msg[1])
  70. case MessageProps:
  71. ext.handleMessageProps(msg[1])
  72. case MessageMsgInfo, MessageMsg:
  73. ext.handleMessageMsgInfo(msgType, msg[1])
  74. case MessageCmd:
  75. ext.handleMessageCommand(msg[1])
  76. case MessageChat:
  77. ext.handleMessageChatUpdate(msg[1])
  78. default:
  79. for _, handler := range ext.handlers {
  80. ujmHandler, ok := handler.(UnhandledJSONMessageHandler)
  81. if !ok {
  82. continue
  83. }
  84. ujmHandler.HandleUnhandledJSONMessage(message)
  85. }
  86. }
  87. }