Mercurial > isshouni
comparison isshouni.js @ 0:156c2fd7c626
First commit.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Wed, 22 Feb 2012 19:57:56 +0100 |
parents | |
children | 617063bc96f7 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:156c2fd7c626 |
---|---|
1 #!/usr/bin/env node | |
2 'use strict'; | |
3 | |
4 /** | |
5 Copyright (c) 2012, Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> | |
6 | |
7 Permission to use, copy, modify, and/or distribute this software for any | |
8 purpose with or without fee is hereby granted, provided that the above | |
9 copyright notice and this permission notice appear in all copies. | |
10 | |
11 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
12 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
13 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
14 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
15 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
16 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
17 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
18 */ | |
19 | |
20 | |
21 var config = require('./config'); | |
22 var args = ['-slave'].concat(process.argv.slice(2)); | |
23 | |
24 var tty = require('tty'); | |
25 var xmpp = require('node-xmpp'); | |
26 var JID = require('./jid'); | |
27 var spawn = require('child_process').spawn; | |
28 | |
29 var cl = new xmpp.Client(config); | |
30 var mplayer = spawn('mplayer', args); | |
31 | |
32 var connect = function() { | |
33 cl.send(new xmpp.Element('presence', {to: config.muc + '/' + config.nick}) | |
34 .c('x', {xmlns: 'http://jabber.org/protocol/muc'}) | |
35 .c('history', {maxchars: '0'}) | |
36 .up()); | |
37 //TODO: handle join errors. | |
38 }; | |
39 cl.on('online', connect); | |
40 | |
41 var disconnect = function() { | |
42 cl.send(new xmpp.Element('presence', {to: config.muc + '/' + config.nick, type: 'unavailable'})); | |
43 process.stdin.end(); | |
44 cl.end(); | |
45 }; | |
46 | |
47 var send_message = function(name, arg) { | |
48 //console.log('SENT: ' + name + ' ' + arg); | |
49 var message = new xmpp.Element('message', {to: config.muc, type: 'groupchat'}) | |
50 .c(name, {xmlns: 'urn:linkmauve:player'}); | |
51 | |
52 if (arg) | |
53 message.t(arg); | |
54 | |
55 cl.send(message.up()); | |
56 }; | |
57 | |
58 var send_command = function(text) { | |
59 //console.log(text); | |
60 mplayer.stdin.write(text + '\n'); | |
61 }; | |
62 | |
63 cl.on('stanza', function(stanza) { | |
64 //console.log(stanza.toString()); | |
65 if (!stanza.is('message') || stanza.attrs['type'] !== 'groupchat') | |
66 return; | |
67 | |
68 var from = new JID(stanza.attrs['from']); | |
69 if (from.bare !== config.muc) | |
70 return; | |
71 if (from.resource === config.nick) | |
72 return; | |
73 | |
74 var message = stanza.getChild('body'); | |
75 if (message) | |
76 send_command('osd_show_text "' + from.resource + ': ' + message.getText() + '"'); | |
77 | |
78 var payload = stanza.getChild(null, 'urn:linkmauve:player'); | |
79 if (!payload) | |
80 return; | |
81 | |
82 //send_command('osd_show_text "' + from.resource + ' did ' + payload.name + '"'); | |
83 | |
84 if (payload.name === 'pause') | |
85 send_command('pause'); | |
86 else if (payload.name === 'seek') | |
87 send_command('seek ' + payload.getText()); | |
88 else if (payload.name === 'synchro') | |
89 send_command('seek ' + payload.getText() + ' 2'); | |
90 }); | |
91 | |
92 cl.on('error', function(e) { | |
93 console.log(e); | |
94 }); | |
95 | |
96 var commands = { | |
97 '+': 'volume 1', | |
98 '-': 'volume -1', | |
99 'm': 'mute', | |
100 ' ': function() { | |
101 send_message('pause'); | |
102 send_command('get_time_pos'); | |
103 return 'pause'; | |
104 }, | |
105 'q': function() { | |
106 tty.setRawMode(false); | |
107 disconnect(); | |
108 return 'quit'; | |
109 }, | |
110 }; | |
111 | |
112 process.stdin.resume(); | |
113 tty.setRawMode(true); | |
114 process.stdin.on('keypress', function(char, key) { | |
115 if (!char) { | |
116 if (key.name == 'right') | |
117 send_command('seek 10'); | |
118 else if (key.name == 'left') | |
119 send_command('seek -10'); | |
120 else if (key.name == 'up') | |
121 send_command('seek 60'); | |
122 else if (key.name == 'down') | |
123 send_command('seek -60'); | |
124 } else if (char in commands) { | |
125 var command = commands[char]; | |
126 if (typeof command === 'function') | |
127 command = command(); | |
128 send_command(command); | |
129 } | |
130 }); | |
131 | |
132 mplayer.stdout.on('data', function(data) { | |
133 //console.log('DATA: '+data); | |
134 data = /(ANS_TIME_POSITION)=([0-9\.]+)/.exec(data); | |
135 if (!data) | |
136 return; | |
137 | |
138 var cmd = data[1]; | |
139 var arg = data[2]; | |
140 if (cmd === 'ANS_TIME_POSITION') | |
141 send_message('synchro', arg); | |
142 }); |