jsonmessage.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. ext.jsonParseError(err)
  60. return
  61. }
  62. var msgType JSONMessageType
  63. json.Unmarshal(msg[0], &msgType)
  64. switch msgType {
  65. case MessagePresence:
  66. ext.handleMessagePresence(msg[1])
  67. case MessageStream:
  68. ext.handleMessageStream(msg[1:])
  69. case MessageConn:
  70. ext.handleMessageConn(msg[1])
  71. case MessageProps:
  72. ext.handleMessageProps(msg[1])
  73. case MessageMsgInfo, MessageMsg:
  74. ext.handleMessageMsgInfo(msgType, msg[1])
  75. case MessageCmd:
  76. ext.handleMessageCommand(msg[1])
  77. case MessageChat:
  78. ext.handleMessageChatUpdate(msg[1])
  79. default:
  80. for _, handler := range ext.handlers {
  81. ujmHandler, ok := handler.(UnhandledJSONMessageHandler)
  82. if !ok {
  83. continue
  84. }
  85. ujmHandler.HandleUnhandledJSONMessage(message)
  86. }
  87. }
  88. }