-
Notifications
You must be signed in to change notification settings - Fork 3
/
pep.js
234 lines (184 loc) · 8.01 KB
/
pep.js
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
var EXPORTED_SYMBOLS = ["pepService"];
function PEPNodeHandler(node, callback) {
this._node = node;
this._callback = callback;
if (callback)
servicesManager.publishDiscoInfo(node+"+notify");
}
_DECL_(PEPNodeHandler).prototype = {
/**
* Sends update node configuration packet
*
* @param delta Object - Hash with attribute name as property, and new
* value as hash value
*/
getConfiguration: function(callback, force) {
if (this._configuration && !force) {
if (callback)
callback(this._configuration);
return this._configuration;
}
servicesManager.sendIqWithGenerator(
(function (callback) {
var [pkt, query, queryDOM] = yield {
type: "get",
domBuilder: ["pubsub", {xmlns: "http://jabber.org/protocol/pubsub#owner"},
[["configure", {node: this._node}]]]
};
if (pkt.getType() == "result") {
var ns1 = new Namespace("http://jabber.org/protocol/pubsub#owner");
var ns2 = new Namespace("jabber:x:data");
this._configuration = {};
for each (var field in query.ns1::configure.ns2::x.ns2::field)
this._configuration[filed.@var] = field.ns2::value.text().toString();
} else
this._configuration = null;
if (callback)
callback(this._configuration);
yield null;
}).call(this, callback));
return this._configuration;
},
reconfigureNode: function(delta, callback) {
servicesManager.sendIqWithGenerator(
(function (delta, callback) {
var fields = [];
delta["FORM_TYPE"] = "http://jabber.org/protocol/pubsub#node_config";
for (var v in delta)
fields.push(["field", {"var": v}, [["value", {}, [delta[v]]]]]);
var [pkt, query, queryDOM] = yield {
type: "set",
domBuilder: ["pubsub", {xmlns: "http://jabber.org/protocol/pubsub#owner"},
[["configure", {node: this._node},
[["x", {xmlns: "jabber:x:data", type: "submit"}, fields]]]]]
};
if (callback) {
var error = pkt.getChild(null, "urn:ietf:params:xml:ns:xmpp-stanzas");
var type = pkt.getType() == "result" ? null : error ? error.nodeName : "unknown";
callback(type, pkt);
}
yield null;
}).apply(this, arguments));
},
deleteNode: function(callback) {
servicesManager.sendIqWithGenerator(
(function (callback) {
var fields = [];
var [pkt, query, queryDOM] = yield {
type: "set",
domBuilder: ["pubsub", {xmlns: "http://jabber.org/protocol/pubsub#owner"},
[["delete", {node: this._node},[]]]]
};
if (callback) {
var error = pkt.getChild(null, "urn:ietf:params:xml:ns:xmpp-stanzas");
var type = pkt.getType() == "result" ? null : error ? error.nodeName : "unknown";
callback(type);
}
yield null;
}).apply(this, arguments));
},
destroy: function() {
if (!this._callback)
return;
servicesManager.unpublishDiscoInfo(this._node+"+notify");
delete pepService._observers[this._node];
},
createNode: function(callback, configuration) {
servicesManager.sendIqWithGenerator(
(function (callback, configuration) {
var fields = [];
if (configuration) {
configuration["FORM_TYPE"] = "http://jabber.org/protocol/pubsub#node_config";
for (var v in configuration)
fields.push(["field", {"var": v}, [["value", {}, [configuration[v]]]]]);
}
var create = [["create", {node: this._node}, []]];
if (fields.length > 1)
create.push(["x", {xmlns: "jabber:x:data", type: "submit"}, fields]);
var [pkt, query, queryDOM] = yield {
type: "set",
domBuilder: ["pubsub", {xmlns: "http://jabber.org/protocol/pubsub#owner"},
create]
};
if (callback) {
var error = pkt.getChild(null, "urn:ietf:params:xml:ns:xmpp-stanzas");
var type = pkt.getType() == "result" ? null : error ? error.nodeName : "unknown";
callback(type);
}
yield null;
}).apply(this, arguments));
return null;
},
publishItem: function(id, data, dontSend, callback) {
var pkt = new JSJaCIQ();
var ns = "http://jabber.org/protocol/pubsub"
pkt.setType("set");
pkt.appendNode("pubsub", {xmlns: ns},
[["publish", {node: this._node},
[["item", id ? {id: id} : {}, data]]]]);
if (!dontSend)
account.connection.send(pkt, callback);
return pkt;
},
retractItem: function(id, dontSend, callback) {
var pkt = new JSJaCIQ();
var ns = "http://jabber.org/protocol/pubsub"
pkt.setType("set");
pkt.appendNode("pubsub", {xmlns: ns},
[["retract", {node: this._node},
[["item", {id: id}, []]]]]);
if (!dontSend)
account.connection.send(pkt, callback);
return pkt;
},
getItems: function(to, callback, maxItems) {
servicesManager.sendIqWithGenerator(
(function (to, callback, maxItems) {
[pkt, query, queryDOM] = yield {
type: "get",
to: to,
domBuilder: ["pubsub", {xmlns: "http://jabber.org/protocol/pubsub"},
[["items", maxItems ? {node: this._node, max_items: maxItems} :
{node: this._node}]]]
};
var res = [], error;
if (pkt.getType() == "result") {
var ns = new Namespace("http://jabber.org/protocol/pubsub");
dump ("GOT getItems: "+query+"\n");
for each (var item in query.ns::items.ns::item)
res.push(item);
} else {
error = pkt.getChild(null, "urn:ietf:params:xml:ns:xmpp-stanzas");
error = error ? error.nodeName : "unknown";
}
if (callback)
callback(to, res, error);
yield null;
}).apply(this, arguments));
}
}
var pepService = {
_observers: {},
handlePEPNode: function(node, callback) {
var handler = new PEPNodeHandler(node, callback)
if (callback)
this._observers[node] = handler;
return handler;
},
_onEvent: function(pkt, event, jid, eventE4X) {
if (event.nodeName != "event")
return;
var pepNS = new Namespace("http://jabber.org/protocol/pubsub#event");
var items = eventE4X.pepNS::items;
if (!this._observers[items.@node])
return;
var data = {added: [], removed: []};
for each (var item in items.pepNS::item)
data.added.push(item)
for each (var item in items.pepNS::retract)
data.removed.push(item.@id.toString())
this._observers[items.@node]._callback.call(null, jid, items.@node.toString(), data, pkt);
}
};
servicesManager.addMessageService("http://jabber.org/protocol/pubsub#event",
new Callback(pepService._onEvent, pepService));