jsonmessage.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. MessagePresence JSONMessageType = "Presence"
  26. MessageStream JSONMessageType = "Stream"
  27. MessageConn JSONMessageType = "Conn"
  28. MessageProps JSONMessageType = "Props"
  29. )
  30. func (ext *ExtendedConn) AddHandler(handler whatsapp.Handler) {
  31. ext.Conn.AddHandler(handler)
  32. ext.handlers = append(ext.handlers, handler)
  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. return
  57. }
  58. var msgType JSONMessageType
  59. json.Unmarshal(msg[0], &msgType)
  60. switch msgType {
  61. case MessagePresence:
  62. ext.handleMessagePresence(msg[1])
  63. case MessageStream:
  64. ext.handleMessageStream(msg[1:])
  65. case MessageConn:
  66. ext.handleMessageProps(msg[1])
  67. case MessageProps:
  68. ext.handleMessageProps(msg[1])
  69. case MessageMsgInfo:
  70. ext.handleMessageMsgInfo(msg[1])
  71. default:
  72. for _, handler := range ext.handlers {
  73. ujmHandler, ok := handler.(UnhandledJSONMessageHandler)
  74. if !ok {
  75. continue
  76. }
  77. ujmHandler.HandleUnhandledJSONMessage(message)
  78. }
  79. }
  80. }