comparison plugins/feature-not-implemented.js @ 36:b43ca01b9f6f

Add a plugin that respond an error to each get/set iq not handled.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 28 Jan 2012 06:01:58 +0100
parents
children ea276b47c555
comparison
equal deleted inserted replaced
35:bdfbd58b4835 36:b43ca01b9f6f
1 'use strict';
2
3 /**
4 Copyright (c) 2012, Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 Lightstring.NS.xmpp_stanzas = 'urn:ietf:params:xml:ns:xmpp-stanzas';
20
21 function register_feature_not_implemented() {
22 var conn = this;
23 conn.on('iq', function callback(stanza) {
24 var type = stanza.DOM.getAttributeNS(null, 'type');
25 if (type !== 'get' && type !== 'set')
26 return;
27
28 var handlers = conn.handlers;
29 var events = [handlers['iq']];
30
31 var payload = stanza.DOM.firstChild;
32 if (payload)
33 events.push('iq/' + payload.namespaceURI + ':' + payload.localName);
34
35 var only = events.every(function(handler) {
36 for (var i in handler)
37 if (callback !== handler[i])
38 return false;
39 return true;
40 });
41
42 if (only)
43 conn.send("<iq to='" + stanza.DOM.getAttributeNS(null, 'from') + "'" +
44 " id='" + stanza.DOM.getAttributeNS(null, 'id') + "'" +
45 " type='error'>" +
46 "<error type='cancel'>" +
47 "<feature-not-implemented xmlns='" + Lightstring.NS.xmpp_stanzas + "'/>" +
48 "</error>" +
49 "</iq>");
50 });
51 };