call.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. "strings"
  20. "github.com/Rhymen/go-whatsapp"
  21. )
  22. type CallInfoType string
  23. const (
  24. CallOffer CallInfoType = "offer"
  25. CallOfferVideo CallInfoType = "offer_video"
  26. CallTransport CallInfoType = "transport"
  27. CallRelayLatency CallInfoType = "relaylatency"
  28. CallTerminate CallInfoType = "terminate"
  29. )
  30. type CallInfo struct {
  31. ID string `json:"id"`
  32. Type CallInfoType `json:"type"`
  33. From string `json:"from"`
  34. Platform string `json:"platform"`
  35. Version []int `json:"version"`
  36. Data [][]interface{} `json:"data"`
  37. }
  38. type CallInfoHandler interface {
  39. whatsapp.Handler
  40. HandleCallInfo(CallInfo)
  41. }
  42. func (ext *ExtendedConn) handleMessageCall(message []byte) {
  43. var event CallInfo
  44. err := json.Unmarshal(message, &event)
  45. if err != nil {
  46. ext.jsonParseError(err)
  47. return
  48. }
  49. event.From = strings.Replace(event.From, OldUserSuffix, NewUserSuffix, 1)
  50. for _, handler := range ext.handlers {
  51. callInfoHandler, ok := handler.(CallInfoHandler)
  52. if !ok {
  53. continue
  54. }
  55. if ext.shouldCallSynchronously(callInfoHandler) {
  56. callInfoHandler.HandleCallInfo(event)
  57. } else {
  58. go callInfoHandler.HandleCallInfo(event)
  59. }
  60. }
  61. }