presence.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 whatsappExt
  17. import (
  18. "encoding/json"
  19. "strings"
  20. "github.com/Rhymen/go-whatsapp"
  21. )
  22. type PresenceType string
  23. const (
  24. PresenceUnavailable PresenceType = "unavailable"
  25. PresenceAvailable PresenceType = "available"
  26. PresenceComposing PresenceType = "composing"
  27. )
  28. type Presence struct {
  29. JID string `json:"id"`
  30. SenderJID string `json:"participant"`
  31. Status PresenceType `json:"type"`
  32. Timestamp int64 `json:"t"`
  33. Deny bool `json:"deny"`
  34. }
  35. type PresenceHandler interface {
  36. whatsapp.Handler
  37. HandlePresence(Presence)
  38. }
  39. func (ext *ExtendedConn) handleMessagePresence(message []byte) {
  40. var event Presence
  41. err := json.Unmarshal(message, &event)
  42. if err != nil {
  43. ext.jsonParseError(err)
  44. return
  45. }
  46. event.JID = strings.Replace(event.JID, OldUserSuffix, NewUserSuffix, 1)
  47. if len(event.SenderJID) == 0 {
  48. event.SenderJID = event.JID
  49. } else {
  50. event.SenderJID = strings.Replace(event.SenderJID, OldUserSuffix, NewUserSuffix, 1)
  51. }
  52. for _, handler := range ext.handlers {
  53. presenceHandler, ok := handler.(PresenceHandler)
  54. if !ok {
  55. continue
  56. }
  57. go presenceHandler.HandlePresence(event)
  58. }
  59. }