Mercurial > isshouni
diff isshouni.js @ 2:88eb49491a1e
Reply to iq, and better split code.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Thu, 23 Feb 2012 20:40:37 +0100 |
parents | 617063bc96f7 |
children | 0082e861cf16 |
line wrap: on
line diff
--- a/isshouni.js +++ b/isshouni.js @@ -29,6 +29,7 @@ var spawn = require('child_process').spa var cl = new xmpp.Client(config); var mplayer = spawn('mplayer', args); var paused = false; +var connected = false; var connect = function() { cl.send(new xmpp.Element('presence', {to: config.muc + '/' + config.nick}) @@ -45,9 +46,12 @@ var disconnect = function() { cl.end(); }; -var send_message = function(name, arg) { +var send_message = function(name, arg, type) { //console.log('SENT: ' + name + ' ' + arg); - var message = new xmpp.Element('message', {to: config.muc, type: 'groupchat'}) + if (!type) + type = 'groupchat'; + + var message = new xmpp.Element('message', {to: config.muc, type: type}) .c(name, {xmlns: 'urn:linkmauve:player'}); if (arg) @@ -63,12 +67,29 @@ var send_command = function(text) { cl.on('stanza', function(stanza) { //console.log(stanza.toString()); - if (!stanza.is('message') || stanza.attrs['type'] !== 'groupchat') + + var type = stanza.attrs['type']; + var from = new JID(stanza.attrs['from']); + var id = stanza.attrs['id'] || ''; + + if (stanza.is('iq') && (type === 'get' || type === 'set')) + return cl.send(new xmpp.Element('iq', {to: from, id: id, type: 'error'}) + .c('error', {type: 'cancel'}) + .c('service-unavailable', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}) + .up() + .up()); + + if (from.bare !== config.muc) return; - var from = new JID(stanza.attrs['from']); - if (from.bare !== config.muc) - return; + if (stanza.is('presence') && type !== 'unavailable') + return on_presence(stanza); + + if (stanza.is('message')) + return on_message(stanza); +}); + +var on_message = function(from, stanza) { if (from.resource === config.nick) return; @@ -89,7 +110,14 @@ cl.on('stanza', function(stanza) { send_command('seek ' + payload.getText()); else if (payload.name === 'synchro') send_command('seek ' + payload.getText() + ' 2'); -}); +}; + +var on_presence = function(from, stanza) { + if (from.resource === config.nick) { + connected = true; + return; + } +}; cl.on('error', function(e) { console.log(e);