Mercurial > isshouni
view isshouni.js @ 6:377a9b42226a default tip
Add logging fonctions and don’t listen to messages from the room itself.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Thu, 23 Feb 2012 23:51:31 +0100 |
parents | 4a063df30be9 |
children |
line wrap: on
line source
#!/usr/bin/env node 'use strict'; /** Copyright (c) 2012, Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ if (process.argv.length < 3) { console.log('You should put at least a video on your command line.'); process.exit(); } var config = require('./config'); var args = ['-slave'].concat(process.argv.slice(2)); var tty = require('tty'); var xmpp = require('node-xmpp'); var JID = require('./jid'); var spawn = require('child_process').spawn; 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}) .c('x', {xmlns: 'http://jabber.org/protocol/muc'}) .c('history', {maxchars: '0'}) .up()); //TODO: handle join errors. }; cl.on('online', connect); var disconnect = function() { cl.send(new xmpp.Element('presence', {to: config.muc + '/' + config.nick, type: 'unavailable'})); process.stdin.end(); cl.end(); tty.setRawMode(false); setTimeout(process.exit, 1000); }; var send_message = function(name, arg, type) { //console.log('SENT: ' + name + ' ' + arg); if (!type) type = 'groupchat'; var message = new xmpp.Element('message', {to: config.muc, type: type}) .c(name, {xmlns: 'urn:linkmauve:player'}); if (arg) message.t(arg); cl.send(message.up()); }; var send_command = function(text) { //console.log(text); mplayer.stdin.write(text + '\n'); }; cl.on('stanza', function(stanza) { console.log(stanza.toString()); 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; if (stanza.is('presence') && type !== 'unavailable') return on_presence(from, stanza); if (stanza.is('message')) return on_message(from, stanza); }); var on_message = function(from, stanza) { if (from.resource && from.resource === config.nick) return; var message = stanza.getChild('body'); if (message) message = message.getText(); if (message) send_command('osd_show_text "' + from.resource + ': ' + message + '"'); var payload = stanza.getChild(null, 'urn:linkmauve:player'); if (!payload) return; send_command('osd_show_text "' + from.resource + ' did ' + payload.name + '"'); if (payload.name === 'pause') { send_command('pause'); paused = !paused; } else if (payload.name === 'seek') 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; } if (!connected) return; /*console.log('New participant: ' + from.resource); if (paused) send_message('paused', undefined, 'chat');*/ }; cl.on('error', function(e) { console.log(e); }); var commands = { '+': 'volume 1', '-': 'volume -1', 'm': 'mute', ' ': function() { send_message('pause'); paused = !paused; console.log(paused); if (paused) send_command('get_time_pos'); return 'pause'; }, 'q': function() { disconnect(); return 'quit'; }, }; var seek = function(time) { send_message('seek', time); send_command('seek ' + time); }; process.stdin.resume(); tty.setRawMode(true); process.stdin.on('keypress', function(char, key) { if (!char) { if (key.name == 'right') seek(10); else if (key.name == 'left') seek(-10); else if (key.name == 'up') seek(60); else if (key.name == 'down') seek(-60); } else if (char in commands) { var command = commands[char]; if (typeof command === 'function') command = command(); send_command(command); } }); mplayer.stdout.on('data', function(data) { //console.log('DATA: '+data); data = /(ANS_TIME_POSITION)=([0-9\.]+)/.exec(data); if (!data) return; var cmd = data[1]; var arg = data[2]; if (cmd === 'ANS_TIME_POSITION') send_message('synchro', arg); });