comparison plugins/disco.js @ 45:063e31247e71

Fix disco plugin.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 31 Jan 2012 21:09:07 +0100
parents 6773e5bc2ca0
children aaad98b33f86
comparison
equal deleted inserted replaced
44:3dfb596cf669 45:063e31247e71
17 */ 17 */
18 18
19 ///////// 19 /////////
20 //Disco// 20 //Disco//
21 ///////// 21 /////////
22 Lightstring.NS['disco#info'] = "http://jabber.org/protocol/disco#info"; 22 Lightstring.plugins['disco'] = {
23 Lightstring.NS['disco#items'] = "http://jabber.org/protocol/disco#items"; 23 namespaces: {
24 Lightstring.stanza.disco = { 24 'disco#info': "http://jabber.org/protocol/disco#info",
25 items: function(aTo, aNode) { 25 'disco#items': "http://jabber.org/protocol/disco#items"
26 if(aTo)
27 var iq = "<iq type='get' to='"+aTo+"'>";
28 else
29 var iq = "<iq type='get'>";
30
31 if(aNode)
32 var query = "<query xmlns='"+Lightstring.NS['disco#items']+"' node='"+aNode+"'/>";
33 else
34 var query = "<query xmlns='"+Lightstring.NS['disco#items']+"'/>";
35
36 return iq+query+"</iq>";
37 }, 26 },
38 info: function(aTo, aNode) { 27 stanzas: {
39 if(aTo) 28 'disco#info': function(aTo, aNode) {
40 var iq = "<iq type='get' to='"+aTo+"'>"; 29 if(aTo)
41 else 30 var iq = "<iq type='get' to='"+aTo+"'>";
42 var iq = "<iq type='get'>"; 31 else
43 if(aNode) 32 var iq = "<iq type='get'>";
44 var query = "<query xmlns='"+Lightstring.NS['disco#info']+"' node='"+aNode+"'/>"; 33
45 else 34 if(aNode)
46 var query = "<query xmlns='"+Lightstring.NS['disco#info']+"'/>"; 35 var query = "<query xmlns='"+Lightstring.NS['disco#items']+"' node='"+aNode+"'/>";
47 36 else
48 return iq+query+"</iq>"; 37 var query = "<query xmlns='"+Lightstring.NS['disco#items']+"'/>";
38
39 return iq+query+"</iq>";
40 },
41 'disco#info': function(aTo, aNode) {
42 if(aTo)
43 var iq = "<iq type='get' to='"+aTo+"'>";
44 else
45 var iq = "<iq type='get'>";
46 if(aNode)
47 var query = "<query xmlns='"+Lightstring.NS['disco#info']+"' node='"+aNode+"'/>";
48 else
49 var query = "<query xmlns='"+Lightstring.NS['disco#info']+"'/>";
50
51 return iq+query+"</iq>";
52 }
53 },
54 handlers: {
55 //TODO: fix that handler.
56 /*conn.on('iq/' + Lightstring.NS['disco#info'] + ':query', function(stanza) {
57 if (stanza.DOM.getAttributeNS(null, 'type') !== 'get')
58 return;
59
60 var query = stanza.DOM.firstChild;
61 if (query.getAttributeNS(null, 'node')) {
62 var response = "<iq to='" + stanza.DOM.getAttributeNS(null, 'from') + "'" +
63 " id='" + stanza.DOM.getAttributeNS(null, 'id') + "'" +
64 " type='error'/>"; //TODO: precise the error.
65 conn.send(response);
66 return;
67 }
68
69 var features = [Lightstring.NS.sxe, Lightstring.NS.jingle.transports.sxe]; //TODO: put that elsewhere.
70
71 var response = "<iq to='" + stanza.DOM.getAttributeNS(null, 'from') + "'" +
72 " id='" + stanza.DOM.getAttributeNS(null, 'id') + "'" +
73 " type='result'>" +
74 "<query xmlns='" + Lightstring.NS['disco#info'] + "'>" +
75 "<identity category='client' type='browser'/>";
76 features.forEach(function(f) {
77 response += "<feature var='" + f + "'/>";
78 });
79 response += "</query>" +
80 "</iq>";
81
82 conn.send(response);
83 });*/
84 },
85 methods: {
86 discoItems: function(aTo, aResult, aError) {
87 this.send(Lightstring.stanzas.disco.items(aTo), function (stanza) {
88 var items = [];
89
90 var children = stanza.DOM.firstChild.childNodes;
91 var length = children.length;
92
93 for (var i = 0; i < length; i++) {
94 var node = children[i];
95 if (node.localName !== 'item')
96 continue;
97
98 var item = {
99 jid: node.getAttributeNS(null, 'jid'),
100 name: node.getAttributeNS(null, 'name'),
101 node: node.getAttributeNS(null, 'node')
102 };
103 items.push(item);
104 }
105
106 stanza.items = items;
107
108 if (aResult)
109 aResult(stanza);
110 }, aError);
111 },
112 discoInfo: function(aTo, aNode, aResult, aError) {
113 this.send(Lightstring.stanzas.disco.info(aTo, aNode), function(stanza){
114 var identities = [];
115 var features = [];
116 var fields = {};
117
118 var children = stanza.DOM.firstChild.childNodes;
119 var length = children.length;
120
121 for (var i = 0; i < length; i++) {
122
123 if (children[i].localName === 'feature')
124 features.push(children[i].getAttributeNS(null, 'var'));
125
126 else if (children[i].localName === 'identity') {
127 var identity = {
128 category: children[i].getAttributeNS(null, 'category'),
129 type: children[i].getAttributeNS(null, 'type')
130 };
131 var name = children[i].getAttributeNS(null, 'name');
132 if (name)
133 identity.name = name;
134 identities.push(identity);
135 }
136
137 else if (children[i].localName === 'x') {
138 for (var j = 0; j < children[i].childNodes.length; j++) {
139 var child = children[i].childNodes[j];
140 var field = {
141 type: child.getAttribute('type')
142 };
143
144 var _var = child.getAttribute('var');
145
146 var label = child.getAttribute('label');
147 if (label)
148 field.label = label;
149
150 for (var y = 0; y < child.childNodes.length; y++) {
151 if(child.childNodes[y].localName === 'desc')
152 field.desc = child.childNodes[y].textContent;
153 else if(child.childNodes[y].localName === 'required')
154 field.required = true;
155 else if(child.childNodes[y].localName === 'value')
156 field.value = child.childNodes[y].textContent;
157 }
158
159 fields[_var] = field;
160 }
161 }
162 }
163
164 stanza.identities = identities;
165 stanza.features = features;
166 stanza.fields = fields;
167
168 if (aResult)
169 aResult(stanza);
170 }, aError);
171 }
49 } 172 }
50 }; 173 };
51 Lightstring.discoItems = function(aConnection, aTo, aCallback) {
52 aConnection.send(Lightstring.stanza.disco.items(aTo), function(stanza){
53 var items = [];
54 var elms = stanza.DOM.querySelectorAll('item');
55 for(var i = 0; i < elms.length; i++) {
56 var node = elms[i];
57 var item = {
58 jid: node.getAttribute('jid'),
59 name: node.getAttribute('name'),
60 node: node.getAttribute('node')
61 }
62 items.push(item);
63 };
64 if(aCallback)
65 aCallback(items);
66 });
67 };
68 Lightstring.discoInfo = function(aConnection, aTo, aNode, aCallback) {
69 aConnection.send(Lightstring.stanza.disco.info(aTo, aNode), function(stanza){
70 var identities = [];
71 var features = [];
72 var fields = {};
73
74 var children = stanza.DOM.firstChild.childNodes;
75 var length = children.length;
76
77 for (var i = 0; i < length; i++) {
78
79 if (children[i].localName === 'feature')
80 features.push(children[i].getAttributeNS(null, 'var'));
81
82 else if (children[i].localName === 'identity') {
83 var identity = {
84 category: children[i].getAttributeNS(null, 'category'),
85 type: children[i].getAttributeNS(null, 'type')
86 };
87 var name = children[i].getAttributeNS(null, 'name');
88 if (name)
89 identity.name = name;
90 identities.push(identity);
91 }
92
93 else if (children[i].localName === 'x') {
94 for (var j = 0; j < children[i].childNodes.length; j++) {
95 var child = children[i].childNodes[j];
96 var field = {
97 type: child.getAttribute('type')
98 };
99
100 var _var = child.getAttribute('var');
101
102 var label = child.getAttribute('label');
103 if(label) field.label = label;
104
105
106 for (var y = 0; y < child.childNodes.length; y++) {
107 if(child.childNodes[y].localName === 'desc')
108 field.desc = child.childNodes[y].textContent;
109 else if(child.childNodes[y].localName === 'required')
110 field.required = true;
111 else if(child.childNodes[y].localName === 'value')
112 field.value = child.childNodes[y].textContent;
113 }
114
115
116 fields[_var] = field;
117 }
118 }
119 }
120
121 aCallback({'identities': identities, 'features': features, 'fields': fields}, stanza);
122 });
123 };