-
Notifications
You must be signed in to change notification settings - Fork 10
/
presence.go
70 lines (52 loc) · 1.77 KB
/
presence.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package xco
import (
"github.com/pkg/errors"
)
const (
// SUBSCRIBE represents the subscribe Presence message type
SUBSCRIBE = "subscribe"
// SUBSCRIBED represents the subscribed Presence message type
SUBSCRIBED = "subscribed"
// UNSUBSCRIBE represents the unsubsribe Presence message type
UNSUBSCRIBE = "unsubscribe"
// UNSUBSCRIBED represents the unsubsribed Presence message type
UNSUBSCRIBED = "unsubscribed"
// UNAVAILABLE represents the unavailable Presence message type
UNAVAILABLE = "unavailable"
// PROBE represents the probe Presence message type
PROBE = "probe"
)
// Presence represents a message identifying whether an entity is available and the subscription requests/responses for the entity
type Presence struct {
Header
Show string `xml:"show,omitempty"`
Status string `xml:"status,omitempty"`
Priority byte `xml:"priority,omitempty"`
Type string `xml:"type,attr,omitempty"`
// XEP-0172 User nicknames
Nick string `xml:"http://jabber.org/protocol/nick nick,omitempty"`
XMLName string `xml:"presence"`
}
// PresenceHandler handles incoming presence requests
type PresenceHandler func(c *Component, p *Presence) error
func noOpPresenceHandler(c *Component, p *Presence) error {
return nil
}
// AlwaysOnlinePresenceHandler always returns "subscribed" to any presence requests
func AlwaysOnlinePresenceHandler(c *Component, p *Presence) error {
resp := &Presence{
Header: Header{
From: p.To,
To: p.From,
ID: p.ID,
},
Type: "subscribed",
}
return errors.Wrap(c.Send(resp), "Error sending always online presence")
}
// ToAddressPresenceHandler calls the function with the To address
func ToAddressPresenceHandler(fn func(subject Address) error) PresenceHandler {
return func(c *Component, p *Presence) error {
return fn(*p.To)
}
}