jsonmessage.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. "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. MessageCall JSONMessageType = "Call"
  33. )
  34. func (ext *ExtendedConn) HandleError(error) {}
  35. type UnhandledJSONMessageHandler interface {
  36. whatsapp.Handler
  37. HandleUnhandledJSONMessage(string)
  38. }
  39. type JSONParseErrorHandler interface {
  40. whatsapp.Handler
  41. HandleJSONParseError(error)
  42. }
  43. func (ext *ExtendedConn) jsonParseError(err error) {
  44. for _, handler := range ext.handlers {
  45. errorHandler, ok := handler.(JSONParseErrorHandler)
  46. if !ok {
  47. continue
  48. }
  49. errorHandler.HandleJSONParseError(err)
  50. }
  51. }
  52. func (ext *ExtendedConn) HandleJsonMessage(message string) {
  53. msg := JSONMessage{}
  54. err := json.Unmarshal([]byte(message), &msg)
  55. if err != nil || len(msg) < 2 {
  56. ext.jsonParseError(err)
  57. return
  58. }
  59. var msgType JSONMessageType
  60. json.Unmarshal(msg[0], &msgType)
  61. switch msgType {
  62. case MessagePresence:
  63. ext.handleMessagePresence(msg[1])
  64. case MessageStream:
  65. ext.handleMessageStream(msg[1:])
  66. case MessageConn:
  67. ext.handleMessageConn(msg[1])
  68. case MessageProps:
  69. ext.handleMessageProps(msg[1])
  70. case MessageMsgInfo, MessageMsg:
  71. ext.handleMessageMsgInfo(msgType, msg[1])
  72. case MessageCmd:
  73. ext.handleMessageCommand(msg[1])
  74. case MessageChat:
  75. ext.handleMessageChatUpdate(msg[1])
  76. case MessageCall:
  77. ext.handleMessageCall(msg[1])
  78. default:
  79. for _, handler := range ext.handlers {
  80. ujmHandler, ok := handler.(UnhandledJSONMessageHandler)
  81. if !ok {
  82. continue
  83. }
  84. if ext.shouldCallSynchronously(ujmHandler) {
  85. ujmHandler.HandleUnhandledJSONMessage(message)
  86. } else {
  87. go ujmHandler.HandleUnhandledJSONMessage(message)
  88. }
  89. }
  90. }
  91. }