/
Roster.js
81 lines (73 loc) · 2.88 KB
/
Roster.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
var xmpp = require('node-xmpp');
var r = require('../lib/Roster.js');
var ltx = require('ltx');
var Roster = r.Roster;
var Contact = r.Contact;
var logger = PROJECTX.logger;
// http://xmpp.org/rfcs/rfc3921.html#roster
/* Contacts have :
- key(jid)
- state
- name
- groups [Not supported here for now TODO]
*/
//TODO Kartik: No authentication for any of roster related operation
exports.setRoster = function(jid, client, authCb) {
Roster.find(jid, function(roster) {
client.roster = roster;
authCb();
});
};
exports.configure = function(server, config) {
server.on("connect", function(client) {
client.on('stanza', function(stz) {
var self = this;
var stanza = ltx.parse(stz.toString());
var query = null;
if (stanza.is('iq') && (query = stanza.getChild('query', "jabber:iq:roster"))) {
if(stanza.attrs.type === "get") {
logger.info("Roster get request from : "+stanza.attrs.from);
stanza.attrs.type = "result";
var contactJidBStr = null;
for(contactJidBStr in client.roster.contacts) {
if(client.roster.contacts.hasOwnProperty(contactJidBStr)) {
var contact = client.roster.contacts[contactJidBStr];
query.c("item", {jid: contact.contactJid, name: contact.name, subscription: contact.subscription});
}
}
stanza.attrs.to = stanza.attrs.from;
client.send(stanza);
} else if(stanza.attrs.type === "set") {
stanza.attrs.type = "result";
var i = query.getChild('item', "jabber:iq:roster");
if(i.attrs.subscription === "remove") {
client.roster.deleteContact(new xmpp.JID(i.attrs.jid), function(err) {
var resultStanza = null;
if(err == null) {
resultStanza = new xmpp.Element('iq', {'to' : stanza.attrs.from, 'type' : 'result', 'id' : stanza.attrs.id});
}else {
resultStanza = new xmpp.Element('iq', {'to' : stanza.attrs.from, 'type' : 'error', 'id' : stanza.attrs.id});
resultStanza.c("error").c('text', { xmlns: "urn:ietf:params:xml:ns:xmpp-stanzas" }).t(err.message);
}
logger.info("Post deleteContact roster");
client.send(resultStanza);
});
} else {
// update or add contact request
client.roster.addContact(new xmpp.JID(i.attrs.jid), i.attrs.name, function(err) {
var resultStanza = null;
if(err == null) {
resultStanza = new xmpp.Element('iq', {'to' : stanza.attrs.from, 'type' : 'result', 'id' : stanza.attrs.id});
}else {
resultStanza = new xmpp.Element('iq', {'to' : stanza.attrs.from, 'type' : 'error', 'id' : stanza.attrs.id});
resultStanza.c("error").c('text', { xmlns: "urn:ietf:params:xml:ns:xmpp-stanzas" }).t(err.message);
}
logger.info("Post addContact Roster");
client.send(resultStanza);
});
}
}
}
});
});
}