comparison plugins/disco.js @ 70:fdd1ae375067

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