Mercurial > psgxs
diff psgxs.js @ 56:99bd1d1ac071
Migration to node-xmpp, done!
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Wed, 10 Aug 2011 15:11:22 -0700 |
parents | 0d3f18bb1d36 |
children | addbf6bbfaa8 |
line wrap: on
line diff
--- a/psgxs.js +++ b/psgxs.js @@ -19,31 +19,61 @@ * along with PSĜS. If not, see <http://www.gnu.org/licenses/>. */ -'use strict'; +//'use strict'; + +var config = require('./configuration'); + +var xmpp = require('node-xmpp'); +var conn = new xmpp.Component({ + jid: config.jid, + password: config.password, + host: 'localhost', + port: 5347 +}); + +if (config.debug) + (function() { + var send = conn.send; + conn.send = function(s) { + console.log('Sent: [1;32m' + s + '[0m'); + send.call(conn, s); + }; + })(); -var xmpp = require('xmpp'); -var sha1 = require('sha1'); +conn.on('stanza', function (stanza) { + if (config.debug) + console.log('Recv: [1;34m' + stanza + '[0m'); + + if (stanza.is('iq')) + onIq(stanza); + else if (stanza.is('message')) + onMessage(stanza); + else if (stanza.is('presence')) + onPresence(stanza); +}); + +conn._uniqueId = 42; +conn.getUniqueId = function(suffix) { + return ++this._uniqueId + (suffix?(":"+suffix):""); +}; + +var Element = xmpp.Element; +Element.prototype.getAttribute = function(name) { + return this.attrs[name]; +}; + + require('./iso8601'); var storage = require('./storage'); var errors = require('./errors'); var makeError = errors.makeError; -var fdsq = require('./fdsq'); -var toBare = fdsq.toBare; -var config = require('./configuration'); var forms = require('./forms'); -var conn = new xmpp.Connection(config.host, config.port); var notifs = require('./notifs'); notifs.setConnection(conn); var modules = require('./modules'); -var service_configuration = config.service_configuration; -var componentJID = config.jid; -var componentPassword = config.password; - -conn.log = function (_, m) { console.log(m); }; - function _(obj, color) { var str = require('sys').inspect(obj, false, null); if (color) @@ -56,17 +86,6 @@ process.addListener('uncaughtException', console.log('\x1b[41;1mUncaught exception (' + err + '), this should never happen:\x1b[0m\n' + err.stack); }); -if (typeof xmpp.StanzaBuilder.cnode != 'function' || typeof xmpp.StanzaBuilder.prototype.cnode != 'function') { - xmpp.StanzaBuilder.prototype.cnode = function (stanza) - { - var parent = this.last_node[this.last_node.length-1]; - parent.tags.push(stanza); - parent.children.push(stanza); - this.last_node.push(stanza); - return this; - }; -} - function onIq(stanza) { var type = stanza.getAttribute('type'); var from = stanza.getAttribute('to'); @@ -75,45 +94,24 @@ function onIq(stanza) { var response; if (id) - response = xmpp.iq({to: to, from: from, type: 'result', id: id}); - else - response = xmpp.iq({to: to, from: from, type: 'result'}); + response = new Element('iq', {to: to, from: from, type: 'result', id: id}); + else + response = new Element('iq', {to: to, from: from, type: 'result'}); var send = {}; - for (var i in stanza.tags) { - var child1 = stanza.tags[i]; - if (child1.name == 'pubsub') { - for (var j in child1.tags) { - var child2 = child1.tags[j]; - - for (var k in modules) { - var module = modules[k]; - - if (module.stanza && (module.stanza != 'iq')) - continue; - - if (module.type && (module.type != type)) - continue; + if (stanza.children.length != 1) + return makeError(response, errors.bad_request.n); - if (module.child2 && (module.child2 != child2.name)) - continue; - - if (module.ns && (module.ns != child2.attr.xmlns)) - continue; - - if (module.number && (module.number != j)) - continue; + var payload = stanza.children[0]; + var tag = payload.name; + var ns = payload.attrs.xmlns; + var contents = payload.children; + + if (tag == 'pubsub') { + for (var j in contents) { + var child = contents[j]; - send.response = module.func(response, stanza, child2, to, from); - if (send.response) { - response = send.response; - send.good = true; - delete send.response; - } - } - } - } else { for (var k in modules) { var module = modules[k]; @@ -123,16 +121,19 @@ function onIq(stanza) { if (module.type && (module.type != type)) continue; - if (module.child && (module.child != child1.name)) + if (module.child && (module.child != tag)) continue; - if (module.ns && (module.ns != child1.attr.xmlns)) + if (module.ns && (module.ns != child.getNS())) + continue; + + if (module.child2 && (module.child2 != child.name)) continue; if (module.number && (module.number != k)) continue; - send.response = module.func(response, stanza, child1, to, from); + send.response = module.func(response, stanza, payload, to, from); if (send.response) { response = send.response; send.good = true; @@ -140,6 +141,32 @@ function onIq(stanza) { } } } + } else { + for (var k in modules) { + var module = modules[k]; + + if (module.stanza && (module.stanza != 'iq')) + continue; + + if (module.type && (module.type != type)) + continue; + + if (module.child && (module.child != tag)) + continue; + + if (module.ns && (module.ns != ns)) + continue; + + if (module.number && (module.number != k)) + continue; + + send.response = module.func(response, stanza, payload, to, from); + if (send.response) { + response = send.response; + send.good = true; + delete send.response; + } + } } conn.send(send.good? response: makeError(response, errors.feature_not_implemented.n)); @@ -152,14 +179,14 @@ function onMessage(stanza) { var response; if (id) - response = xmpp.message({to: to, from: from, id: id}); + response = new Element('message', {to: to, from: from, id: id}); else - response = xmpp.message({to: to, from: from}); + response = new Element('message', {to: to, from: from}); var send = false; - for (var i in stanza.tags) { - var child = stanza.tags[i]; + for (var i in stanza.children) { + var child = stanza.children[i]; for (var k in modules) { var module = modules[k]; @@ -172,7 +199,7 @@ function onMessage(stanza) { if (module.child && (module.child != child.name)) continue; - if (module.ns && (module.ns != child.attr.xmlns)) + if (module.ns && (module.ns != child.attrs.xmlns)) continue; if (module.number && (module.number != k)) @@ -221,27 +248,18 @@ function onPresence(stanza) { var response; if (id) - response = xmpp.presence({to: to, from: from, id: id}); + response = new Element('presence', {to: to, from: from, id: id}); else - response = xmpp.presence({to: to, from: from}); + response = new Element('presence', {to: to, from: from}); makeError(response, errors.feature_not_implemented.n); } -conn.connect(componentJID, componentPassword, function (status, condition) { - if (status == xmpp.Status.CONNECTED) { - conn.addHandler(onMessage, null, 'message', null, null, null); - conn.addHandler(onIq, null, 'iq', null, null, null); - conn.addHandler(onPresence, null, 'presence', null, null, null); +if (process.argv.length >= 3) + storage.load(process.argv[2]); +else + storage.load(); - if (process.argv.length >= 3) - storage.load(process.argv[2]); - else - storage.load(); - - var stdin = process.openStdin(); - stdin.setEncoding('utf8'); - stdin.addListener('data', storage.debug); - } else - conn.log(xmpp.LogLevel.DEBUG, 'New connection status: ' + status + (condition? (' ('+condition+')'): '')); -}); +var stdin = process.openStdin(); +stdin.setEncoding('utf8'); +stdin.addListener('data', storage.debug);