comparison 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
comparison
equal deleted inserted replaced
1:617063bc96f7 2:88eb49491a1e
27 var spawn = require('child_process').spawn; 27 var spawn = require('child_process').spawn;
28 28
29 var cl = new xmpp.Client(config); 29 var cl = new xmpp.Client(config);
30 var mplayer = spawn('mplayer', args); 30 var mplayer = spawn('mplayer', args);
31 var paused = false; 31 var paused = false;
32 var connected = false;
32 33
33 var connect = function() { 34 var connect = function() {
34 cl.send(new xmpp.Element('presence', {to: config.muc + '/' + config.nick}) 35 cl.send(new xmpp.Element('presence', {to: config.muc + '/' + config.nick})
35 .c('x', {xmlns: 'http://jabber.org/protocol/muc'}) 36 .c('x', {xmlns: 'http://jabber.org/protocol/muc'})
36 .c('history', {maxchars: '0'}) 37 .c('history', {maxchars: '0'})
43 cl.send(new xmpp.Element('presence', {to: config.muc + '/' + config.nick, type: 'unavailable'})); 44 cl.send(new xmpp.Element('presence', {to: config.muc + '/' + config.nick, type: 'unavailable'}));
44 process.stdin.end(); 45 process.stdin.end();
45 cl.end(); 46 cl.end();
46 }; 47 };
47 48
48 var send_message = function(name, arg) { 49 var send_message = function(name, arg, type) {
49 //console.log('SENT: ' + name + ' ' + arg); 50 //console.log('SENT: ' + name + ' ' + arg);
50 var message = new xmpp.Element('message', {to: config.muc, type: 'groupchat'}) 51 if (!type)
52 type = 'groupchat';
53
54 var message = new xmpp.Element('message', {to: config.muc, type: type})
51 .c(name, {xmlns: 'urn:linkmauve:player'}); 55 .c(name, {xmlns: 'urn:linkmauve:player'});
52 56
53 if (arg) 57 if (arg)
54 message.t(arg); 58 message.t(arg);
55 59
61 mplayer.stdin.write(text + '\n'); 65 mplayer.stdin.write(text + '\n');
62 }; 66 };
63 67
64 cl.on('stanza', function(stanza) { 68 cl.on('stanza', function(stanza) {
65 //console.log(stanza.toString()); 69 //console.log(stanza.toString());
66 if (!stanza.is('message') || stanza.attrs['type'] !== 'groupchat') 70
71 var type = stanza.attrs['type'];
72 var from = new JID(stanza.attrs['from']);
73 var id = stanza.attrs['id'] || '';
74
75 if (stanza.is('iq') && (type === 'get' || type === 'set'))
76 return cl.send(new xmpp.Element('iq', {to: from, id: id, type: 'error'})
77 .c('error', {type: 'cancel'})
78 .c('service-unavailable', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'})
79 .up()
80 .up());
81
82 if (from.bare !== config.muc)
67 return; 83 return;
68 84
69 var from = new JID(stanza.attrs['from']); 85 if (stanza.is('presence') && type !== 'unavailable')
70 if (from.bare !== config.muc) 86 return on_presence(stanza);
71 return; 87
88 if (stanza.is('message'))
89 return on_message(stanza);
90 });
91
92 var on_message = function(from, stanza) {
72 if (from.resource === config.nick) 93 if (from.resource === config.nick)
73 return; 94 return;
74 95
75 var message = stanza.getChild('body'); 96 var message = stanza.getChild('body');
76 if (message) 97 if (message)
87 paused = !paused; 108 paused = !paused;
88 } else if (payload.name === 'seek') 109 } else if (payload.name === 'seek')
89 send_command('seek ' + payload.getText()); 110 send_command('seek ' + payload.getText());
90 else if (payload.name === 'synchro') 111 else if (payload.name === 'synchro')
91 send_command('seek ' + payload.getText() + ' 2'); 112 send_command('seek ' + payload.getText() + ' 2');
92 }); 113 };
114
115 var on_presence = function(from, stanza) {
116 if (from.resource === config.nick) {
117 connected = true;
118 return;
119 }
120 };
93 121
94 cl.on('error', function(e) { 122 cl.on('error', function(e) {
95 console.log(e); 123 console.log(e);
96 }); 124 });
97 125