conn.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 ConnInfo struct {
  22. ProtocolVersion []int `json:"protoVersion"`
  23. BinaryVersion int `json:"binVersion"`
  24. Phone struct {
  25. WhatsAppVersion string `json:"wa_version"`
  26. MCC string `json:"mcc"`
  27. MNC string `json:"mnc"`
  28. OSVersion string `json:"os_version"`
  29. DeviceManufacturer string `json:"device_manufacturer"`
  30. DeviceModel string `json:"device_model"`
  31. OSBuildNumber string `json:"os_build_number"`
  32. } `json:"phone"`
  33. Features map[string]interface{} `json:"features"`
  34. PushName string `json:"pushname"`
  35. }
  36. type ConnInfoHandler interface {
  37. whatsapp.Handler
  38. HandleConnInfo(ConnInfo)
  39. }
  40. func (ext *ExtendedConn) handleMessageConn(message []byte) {
  41. var event ConnInfo
  42. err := json.Unmarshal(message, &event)
  43. if err != nil {
  44. ext.jsonParseError(err)
  45. return
  46. }
  47. for _, handler := range ext.handlers {
  48. connInfoHandler, ok := handler.(ConnInfoHandler)
  49. if !ok {
  50. continue
  51. }
  52. if ext.shouldCallSynchronously(connInfoHandler) {
  53. connInfoHandler.HandleConnInfo(event)
  54. } else {
  55. go connInfoHandler.HandleConnInfo(event)
  56. }
  57. }
  58. }