Debian Codegraph @

xmpp_pubsub.go

raw
1 package xmpp
2
3 import (
4 "encoding/xml"
5 "fmt"
6 )
7
8 type clientPubsubItem struct {
9 XMLName xml.Name `xml:"item"`
10 ID string `xml:"id,attr"`
11 Body []byte `xml:",innerxml"`
12 }
13
14 type clientPubsubItems struct {
15 XMLName xml.Name `xml:"items"`
16 Node string `xml:"node,attr"`
17 Items []clientPubsubItem `xml:"item"`
18 }
19
20 type clientPubsubEvent struct {
21 XMLName xml.Name `xml:"event"`
22 XMLNS string `xml:"xmlns,attr"`
23 Items clientPubsubItems `xml:"items"`
24 }
25
26 type clientPubsubError struct {
27 XMLName xml.Name
28 }
29
30 type clientPubsubSubscription struct {
31 XMLName xml.Name `xml:"subscription"`
32 Node string `xml:"node,attr"`
33 JID string `xml:"jid,attr"`
34 SubID string `xml:"subid,attr"`
35 }
36
37 type PubsubEvent struct {
38 Node string
39 Items []PubsubItem
40 }
41
42 type PubsubSubscription struct {
43 SubID string
44 JID string
45 Node string
46 Error string
47 }
48 type PubsubUnsubscription PubsubSubscription
49
50 type PubsubItem struct {
51 ID string
52 InnerXML []byte
53 }
54
55 type PubsubItems struct {
56 Node string
57 Items []PubsubItem
58 }
59
60 // Converts []clientPubsubItem to []PubsubItem
61 func pubsubItemsToReturn(items []clientPubsubItem) []PubsubItem {
62 var tmp []PubsubItem
63 for _, i := range items {
64 tmp = append(tmp, PubsubItem{
65 ID: i.ID,
66 InnerXML: i.Body,
67 })
68 }
69
70 return tmp
71 }
72
73 func pubsubClientToReturn(event clientPubsubEvent) PubsubEvent {
74 return PubsubEvent{
75 Node: event.Items.Node,
76 Items: pubsubItemsToReturn(event.Items.Items),
77 }
78 }
79
80 func pubsubStanza(body string) string {
81 return fmt.Sprintf("<pubsub xmlns='%s'>%s</pubsub>",
82 XMPPNS_PUBSUB, body)
83 }
84
85 func pubsubSubscriptionStanza(node, jid string) string {
86 body := fmt.Sprintf("<subscribe node='%s' jid='%s'/>",
87 xmlEscape(node),
88 xmlEscape(jid))
89 return pubsubStanza(body)
90 }
91
92 func pubsubUnsubscriptionStanza(node, jid string) string {
93 body := fmt.Sprintf("<unsubscribe node='%s' jid='%s'/>",
94 xmlEscape(node),
95 xmlEscape(jid))
96 return pubsubStanza(body)
97 }
98
99 func (c *Client) PubsubSubscribeNode(node, jid string) error {
100 id := getUUID()
101 c.subIDs = append(c.subIDs, id)
102 _, err := c.RawInformation(c.jid,
103 jid,
104 id,
105 "set",
106 pubsubSubscriptionStanza(node, c.jid))
107 return err
108 }
109
110 func (c *Client) PubsubUnsubscribeNode(node, jid string) error {
111 id := getUUID()
112 c.unsubIDs = append(c.unsubIDs, id)
113 _, err := c.RawInformation(c.jid,
114 jid,
115 id,
116 "set",
117 pubsubUnsubscriptionStanza(node, c.jid))
118 return err
119 }
120
121 func (c *Client) PubsubRequestLastItems(node, jid string) error {
122 id := getUUID()
123 c.itemsIDs = append(c.itemsIDs, id)
124 body := fmt.Sprintf("<items node='%s'/>", node)
125 _, err := c.RawInformation(c.jid, jid, id, "get", pubsubStanza(body))
126 return err
127 }
128
129 func (c *Client) PubsubRequestItem(node, jid, id string) error {
130 stanzaID := getUUID()
131 c.itemsIDs = append(c.itemsIDs, stanzaID)
132 body := fmt.Sprintf("<items node='%s'><item id='%s'/></items>", node, id)
133 _, err := c.RawInformation(c.jid, jid, stanzaID, "get", pubsubStanza(body))
134 return err
135 }