presence.go 1.6 KB

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