/
msg_pubsub_event.go
213 lines (175 loc) · 4.24 KB
/
msg_pubsub_event.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package stanza
import (
"encoding/xml"
)
// Implementation of the http://jabber.org/protocol/pubsub#event namespace
type PubSubEvent struct {
XMLName xml.Name `xml:"http://jabber.org/protocol/pubsub#event event"`
MsgExtension
EventElement EventElement
//List ItemsEvent
}
func init() {
TypeRegistry.MapExtension(PKTMessage, xml.Name{Space: "http://jabber.org/protocol/pubsub#event", Local: "event"}, PubSubEvent{})
}
type EventElement interface {
Name() string
}
// *********************
// Collection
// *********************
const PubSubCollectionEventName = "Collection"
type CollectionEvent struct {
AssocDisassoc AssocDisassoc
Node string `xml:"node,attr,omitempty"`
}
func (c CollectionEvent) Name() string {
return PubSubCollectionEventName
}
// *********************
// Associate/Disassociate
// *********************
type AssocDisassoc interface {
GetAssocDisassoc() string
}
// *********************
// Associate
// *********************
const Assoc = "Associate"
type AssociateEvent struct {
XMLName xml.Name `xml:"associate"`
Node string `xml:"node,attr"`
}
func (a *AssociateEvent) GetAssocDisassoc() string {
return Assoc
}
// *********************
// Disassociate
// *********************
const Disassoc = "Disassociate"
type DisassociateEvent struct {
XMLName xml.Name `xml:"disassociate"`
Node string `xml:"node,attr"`
}
func (e *DisassociateEvent) GetAssocDisassoc() string {
return Disassoc
}
// *********************
// Configuration
// *********************
const PubSubConfigEventName = "Configuration"
type ConfigurationEvent struct {
Node string `xml:"node,attr,omitempty"`
Form *Form
}
func (c ConfigurationEvent) Name() string {
return PubSubConfigEventName
}
// *********************
// Delete
// *********************
const PubSubDeleteEventName = "Delete"
type DeleteEvent struct {
Node string `xml:"node,attr"`
Redirect *RedirectEvent `xml:"redirect"`
}
func (c DeleteEvent) Name() string {
return PubSubConfigEventName
}
// *********************
// Redirect
// *********************
type RedirectEvent struct {
URI string `xml:"uri,attr"`
}
// *********************
// List
// *********************
const PubSubItemsEventName = "List"
type ItemsEvent struct {
XMLName xml.Name `xml:"items"`
Items []ItemEvent `xml:"item,omitempty"`
Node string `xml:"node,attr"`
Retract *RetractEvent `xml:"retract"`
}
type ItemEvent struct {
XMLName xml.Name `xml:"item"`
Id string `xml:"id,attr,omitempty"`
Publisher string `xml:"publisher,attr,omitempty"`
Any *Node `xml:",any"`
}
func (i ItemsEvent) Name() string {
return PubSubItemsEventName
}
// *********************
// List
// *********************
type RetractEvent struct {
XMLName xml.Name `xml:"retract"`
ID string `xml:"node,attr"`
}
// *********************
// Purge
// *********************
const PubSubPurgeEventName = "Purge"
type PurgeEvent struct {
XMLName xml.Name `xml:"purge"`
Node string `xml:"node,attr"`
}
func (p PurgeEvent) Name() string {
return PubSubPurgeEventName
}
// *********************
// Subscription
// *********************
const PubSubSubscriptionEventName = "Subscription"
type SubscriptionEvent struct {
SubStatus string `xml:"subscription,attr,omitempty"`
Expiry string `xml:"expiry,attr,omitempty"`
SubInfo `xml:",omitempty"`
}
func (s SubscriptionEvent) Name() string {
return PubSubSubscriptionEventName
}
func (pse *PubSubEvent) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
pse.XMLName = start.Name
// decode inner elements
for {
t, err := d.Token()
if err != nil {
return err
}
var ee EventElement
switch tt := t.(type) {
case xml.StartElement:
switch tt.Name.Local {
case "collection":
ee = &CollectionEvent{}
case "configuration":
ee = &ConfigurationEvent{}
case "delete":
ee = &DeleteEvent{}
case "items":
ee = &ItemsEvent{}
case "purge":
ee = &PurgeEvent{}
case "subscription":
ee = &SubscriptionEvent{}
default:
ee = nil
}
// known child element found, decode it
if ee != nil {
err = d.DecodeElement(ee, &tt)
if err != nil {
return err
}
pse.EventElement = ee
}
case xml.EndElement:
if tt == start.End() {
return nil
}
}
}
}