jsonmessage.go 2.6 KB

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