Mercurial > isshouni
annotate isshouni.js @ 3:0082e861cf16
Fix text message handling and new participant.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Thu, 23 Feb 2012 20:44:12 +0100 |
parents | 88eb49491a1e |
children | e6f5077c2ea9 |
rev | line source |
---|---|
0 | 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); | |
1
617063bc96f7
Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
31 var paused = false; |
2
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
32 var connected = false; |
0 | 33 |
34 var connect = function() { | |
35 cl.send(new xmpp.Element('presence', {to: config.muc + '/' + config.nick}) | |
36 .c('x', {xmlns: 'http://jabber.org/protocol/muc'}) | |
37 .c('history', {maxchars: '0'}) | |
38 .up()); | |
39 //TODO: handle join errors. | |
40 }; | |
41 cl.on('online', connect); | |
42 | |
43 var disconnect = function() { | |
44 cl.send(new xmpp.Element('presence', {to: config.muc + '/' + config.nick, type: 'unavailable'})); | |
45 process.stdin.end(); | |
46 cl.end(); | |
47 }; | |
48 | |
2
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
49 var send_message = function(name, arg, type) { |
0 | 50 //console.log('SENT: ' + name + ' ' + arg); |
2
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
51 if (!type) |
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
52 type = 'groupchat'; |
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
53 |
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
54 var message = new xmpp.Element('message', {to: config.muc, type: type}) |
0 | 55 .c(name, {xmlns: 'urn:linkmauve:player'}); |
56 | |
57 if (arg) | |
58 message.t(arg); | |
59 | |
60 cl.send(message.up()); | |
61 }; | |
62 | |
63 var send_command = function(text) { | |
64 //console.log(text); | |
65 mplayer.stdin.write(text + '\n'); | |
66 }; | |
67 | |
68 cl.on('stanza', function(stanza) { | |
69 //console.log(stanza.toString()); | |
2
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
70 |
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
71 var type = stanza.attrs['type']; |
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
72 var from = new JID(stanza.attrs['from']); |
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
73 var id = stanza.attrs['id'] || ''; |
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
74 |
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
75 if (stanza.is('iq') && (type === 'get' || type === 'set')) |
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
76 return cl.send(new xmpp.Element('iq', {to: from, id: id, type: 'error'}) |
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
77 .c('error', {type: 'cancel'}) |
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
78 .c('service-unavailable', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}) |
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
79 .up() |
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
80 .up()); |
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
81 |
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
82 if (from.bare !== config.muc) |
0 | 83 return; |
84 | |
2
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
85 if (stanza.is('presence') && type !== 'unavailable') |
3
0082e861cf16
Fix text message handling and new participant.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
86 return on_presence(from, stanza); |
2
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
87 |
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
88 if (stanza.is('message')) |
3
0082e861cf16
Fix text message handling and new participant.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
89 return on_message(from, stanza); |
2
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
90 }); |
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
91 |
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
92 var on_message = function(from, stanza) { |
0 | 93 if (from.resource === config.nick) |
94 return; | |
95 | |
96 var message = stanza.getChild('body'); | |
97 if (message) | |
3
0082e861cf16
Fix text message handling and new participant.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
98 message = message.getText(); |
0082e861cf16
Fix text message handling and new participant.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
99 if (message) |
0082e861cf16
Fix text message handling and new participant.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
100 send_command('osd_show_text "' + from.resource + ': ' + message + '"'); |
0 | 101 |
102 var payload = stanza.getChild(null, 'urn:linkmauve:player'); | |
103 if (!payload) | |
104 return; | |
105 | |
106 //send_command('osd_show_text "' + from.resource + ' did ' + payload.name + '"'); | |
107 | |
1
617063bc96f7
Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
108 if (payload.name === 'pause') { |
0 | 109 send_command('pause'); |
1
617063bc96f7
Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
110 paused = !paused; |
617063bc96f7
Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
111 } else if (payload.name === 'seek') |
0 | 112 send_command('seek ' + payload.getText()); |
113 else if (payload.name === 'synchro') | |
114 send_command('seek ' + payload.getText() + ' 2'); | |
2
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
115 }; |
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
116 |
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
117 var on_presence = function(from, stanza) { |
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
118 if (from.resource === config.nick) { |
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
119 connected = true; |
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
120 return; |
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
121 } |
3
0082e861cf16
Fix text message handling and new participant.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
122 if (!connected) |
0082e861cf16
Fix text message handling and new participant.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
123 return; |
0082e861cf16
Fix text message handling and new participant.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
124 |
0082e861cf16
Fix text message handling and new participant.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
125 console.log('New participant: ' + from.resource); |
0082e861cf16
Fix text message handling and new participant.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
126 |
0082e861cf16
Fix text message handling and new participant.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
127 if (paused) |
0082e861cf16
Fix text message handling and new participant.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
128 send_message('paused', undefined, 'chat'); |
2
88eb49491a1e
Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
129 }; |
0 | 130 |
131 cl.on('error', function(e) { | |
132 console.log(e); | |
133 }); | |
134 | |
135 var commands = { | |
136 '+': 'volume 1', | |
137 '-': 'volume -1', | |
138 'm': 'mute', | |
139 ' ': function() { | |
140 send_message('pause'); | |
1
617063bc96f7
Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
141 paused = !paused; |
617063bc96f7
Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
142 console.log(paused); |
617063bc96f7
Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
143 if (paused) |
617063bc96f7
Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
144 send_command('get_time_pos'); |
0 | 145 return 'pause'; |
146 }, | |
147 'q': function() { | |
148 tty.setRawMode(false); | |
149 disconnect(); | |
150 return 'quit'; | |
151 }, | |
152 }; | |
153 | |
1
617063bc96f7
Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
154 var seek = function(time) { |
617063bc96f7
Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
155 send_message('seek', time); |
617063bc96f7
Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
156 send_command('seek ' + time); |
617063bc96f7
Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
157 }; |
617063bc96f7
Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
158 |
0 | 159 process.stdin.resume(); |
160 tty.setRawMode(true); | |
161 process.stdin.on('keypress', function(char, key) { | |
162 if (!char) { | |
163 if (key.name == 'right') | |
1
617063bc96f7
Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
164 seek(10); |
0 | 165 else if (key.name == 'left') |
1
617063bc96f7
Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
166 seek(-10); |
0 | 167 else if (key.name == 'up') |
1
617063bc96f7
Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
168 seek(60); |
0 | 169 else if (key.name == 'down') |
1
617063bc96f7
Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
170 seek(-60); |
0 | 171 } else if (char in commands) { |
172 var command = commands[char]; | |
173 if (typeof command === 'function') | |
174 command = command(); | |
175 send_command(command); | |
176 } | |
177 }); | |
178 | |
179 mplayer.stdout.on('data', function(data) { | |
180 //console.log('DATA: '+data); | |
181 data = /(ANS_TIME_POSITION)=([0-9\.]+)/.exec(data); | |
182 if (!data) | |
183 return; | |
184 | |
185 var cmd = data[1]; | |
186 var arg = data[2]; | |
187 if (cmd === 'ANS_TIME_POSITION') | |
188 send_message('synchro', arg); | |
189 }); |