-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathxmpp_library.js
115 lines (103 loc) · 4.15 KB
/
xmpp_library.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
"use strict";
var xmpp = require ('node-xmpp-client');
var dict = require ('dict');
var _ = require ('underscore');
var WYLIODRIN_NAMESPACE = "wyliodrin";
var stanzas = [];
var bufferSize = null;
xmpp.Client.prototype.load = function (t, wother,buffer)
{
// t = this;
// console.log (this);
t.other = wother;
if(!bufferSize)
{
bufferSize = buffer;
}
// this.on ('iq', function (stanza)
// {
// var p = stanza.getChild ('ping', 'urn:xmpp:ping');
// console.log (stanza.root().toString());
// console.log ('stanza');
// if (p && p.type == 'get')
// {
// console.log ('ping');
// t.send (new xmpp.Element ('iq', {to:p.attrs.from, type:'result', id:p.attrs.id}).c('ping', {xmlns:'urn:xmpp:ping'}));
// }
// });
this.on ('stanza', function (stanza)
{
// console.log ('received = '+stanza.root().toString());
if (stanza.is('iq'))
{
var p = stanza.getChild ('ping');
// console.log (stanza);
if (p && stanza.attrs.type == 'get')
{
// console.log ('ping');
t.send (new xmpp.Element ('iq', {to:p.attrs.from, type:'result', id:p.attrs.id}).c('ping', {xmlns:'urn:xmpp:ping'}));
}
}
else
if (stanza.is('message'))
{
_.each (stanza.children, function (es)
{
// console.log (es);
if (es.getNS() == WYLIODRIN_NAMESPACE)
{
var name = es.getName ();
// console.log (name);
var error = stanza.attrs.type == 'error';
if (t.tags().has(name)) t.tags().get(name)(t, new xmpp.JID(stanza.attrs.from).bare().toString().toLowerCase(), new xmpp.JID(stanza.attrs.to).bare().toString().toLowerCase(), es, error);
else if (t.other) t.other (new xmpp.JID (stanza.attrs.from).bare().toString().toLowerCase(), new xmpp.JID(stanza.attrs.to).bare().toString().toLowerCase(), es, error);
}
});
}
else
if (stanza.is('presence'))
{
var name = stanza.getName ();
var error = stanza.attrs.type == 'error';
if (t.tags().has(name)) t.tags().get(name)(t, new xmpp.JID(stanza.attrs.from).bare().toString().toLowerCase(), new xmpp.JID(stanza.attrs.to).bare().toString().toLowerCase(), stanza, error);
else if (t.other) t.other (new xmpp.JID(stanza.attrs.from).bare().toString().toLowerCase(), new xmpp.JID(stanza.attrs.to).bare().toString().toLowerCase(), stanza, error);
}
});
}
xmpp.Client.prototype.tags = function ()
{
if (_.isUndefined (this.tagslist)) this.tagslist = dict ();
return this.tagslist;
}
xmpp.Client.prototype.tag = function (name, namespace, activity)
{
this.tags().set (name, activity);
}
xmpp.Client.prototype.sendWyliodrin = function (to, stanza, store)
{
// console.log('send wyliodrin');
stanza.attrs.xmlns = WYLIODRIN_NAMESPACE;
var s=new xmpp.Element ('message', {to: to}).cnode(stanza);
// console.log ('sent = '+s.root().toString());
if(!store)
{
// console.log('send');
this.send (s);
}
else
{
if(stanzas.length<bufferSize)
{
stanzas.push(s);
}
}
}
xmpp.Client.prototype.emptyStanzaBuffer = function()
{
for (var i = 0; i<stanzas.length; i++)
{
this.send(stanzas[i]);
}
}
exports.xmpp = xmpp;
exports.WYLIODRIN_NAMESPACE = WYLIODRIN_NAMESPACE;