jsonmessage.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. )
  31. func (ext *ExtendedConn) AddHandler(handler whatsapp.Handler) {
  32. ext.Conn.AddHandler(handler)
  33. ext.handlers = append(ext.handlers, handler)
  34. }
  35. func (ext *ExtendedConn) HandleError(error) {}
  36. type UnhandledJSONMessageHandler interface {
  37. whatsapp.Handler
  38. HandleUnhandledJSONMessage(string)
  39. }
  40. type JSONParseErrorHandler interface {
  41. whatsapp.Handler
  42. HandleJSONParseError(error)
  43. }
  44. func (ext *ExtendedConn) jsonParseError(err error) {
  45. for _, handler := range ext.handlers {
  46. errorHandler, ok := handler.(JSONParseErrorHandler)
  47. if !ok {
  48. continue
  49. }
  50. errorHandler.HandleJSONParseError(err)
  51. }
  52. }
  53. func (ext *ExtendedConn) HandleJsonMessage(message string) {
  54. msg := JSONMessage{}
  55. err := json.Unmarshal([]byte(message), &msg)
  56. if err != nil || len(msg) < 2 {
  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. default:
  73. for _, handler := range ext.handlers {
  74. ujmHandler, ok := handler.(UnhandledJSONMessageHandler)
  75. if !ok {
  76. continue
  77. }
  78. ujmHandler.HandleUnhandledJSONMessage(message)
  79. }
  80. }
  81. }