Debian Codegraph @

agi/jukebox.agi

raw
1 #!/usr/bin/perl
2 #
3 # Jukebox 0.2
4 #
5 # A music manager for Asterisk.
6 #
7 # Copyright (C) 2005-2006, Justin Tunney
8 #
9 # Justin Tunney <[email protected]>
10 #
11 # This program is free software, distributed under the terms of the
12 # GNU General Public License v2.
13 #
14 # Keep it open source pigs
15 #
16 # --------------------------------------------------------------------
17 #
18 # Uses festival to list off all your MP3 music files over a channel in
19 # a hierarchical fashion. Put this file in your agi-bin folder which
20 # is located at: /var/lib/asterisk/agi-bin Be sure to chmod +x it!
21 #
22 # Invocation Example:
23 # exten => 68742,1,Answer()
24 # exten => 68742,2,agi,jukebox.agi|/home/justin/Music
25 # exten => 68742,3,Hangup()
26 #
27 # exten => 68742,1,Answer()
28 # exten => 68742,2,agi,jukebox.agi|/home/justin/Music|pm
29 # exten => 68742,3,Hangup()
30 #
31 # Options:
32 # p - Precache text2wave outputs for every possible filename.
33 # It is much better to set this option because if a caller
34 # presses a key during a cache operation, it will be ignored.
35 # m - Go back to menu after playing song
36 # g - Do not play the greeting message
37 #
38 # Usage Instructions:
39 # - Press '*' to go up a directory. If you are in the root music
40 # folder you will be exitted from the script.
41 # - If you have a really long list of files, you can filter the list
42 # at any time by pressing '#' and spelling out a few letters you
43 # expect the files to start with. For example, if you wanted to
44 # know what extension 'Requiem For A Dream' was, you'd type:
45 # '#737'. Note, phone keypads don't include Q and Z. Q is 7 and
46 # Z is 9.
47 #
48 # Notes:
49 # - This AGI script uses the MP3Player command which uses the
50 # mpg123 Program. Grab yourself a copy of this program by
51 # going to http://www.mpg123.de/cgi-bin/sitexplorer.cgi?/mpg123/
52 # Be sure to download mpg123-0.59r.tar.gz because it is known to
53 # work with Asterisk and hopefully isn't the release with that
54 # awful security problem. If you're using Fedora Core 3 do make
55 # linux-devel and you're peachy keen.
56 #
57 # - You won't get nifty STDERR debug messages if you're using a
58 # remote asterisk shell.
59 #
60 # - For some reason, caching certain files will generate the
61 # error: 'using default diphone ax-ax for y-pau'. Example:
62 # # echo "Depeche Mode - CUW - 05 - The Meaning of Love" | text2wave -o /var/jukeboxcache/jukeboxcache/Depeche_Mode/Depeche_Mode_-_CUW_-_05_-_The_Meaning_of_Love.mp3.ul -otype ulaw -
63 # The temporary work around is to just touch these files.
64 #
65 # - The background app doesn't like to get more than 2031 chars
66 # of input.
67 #
68
69 use strict;
70
71 $|=1;
72
73 # Setup some variables
74 my %AGI; my $tests = 0; my $fail = 0; my $pass = 0;
75 my @masterCacheList = ();
76 my $maxNumber = 10;
77
78 while (<STDIN>) {
79 chomp;
80 last unless length($_);
81 if (/^agi_(\w+)\:\s+(.*)$/) {
82 $AGI{$1} = $2;
83 }
84 }
85
86 # setup options
87 my $SHOWGREET = 1;
88 my $PRECACHE = 0;
89 my $MENUAFTERSONG = 0;
90
91 $PRECACHE = 1 if $ARGV[1] =~ /p/;
92 $MENUAFTERSONG = 1 if $ARGV[1] =~ /m/;
93 $SHOWGREET = 0 if $ARGV[1] =~ /g/;
94
95 # setup folders
96 my $MUSIC = $ARGV[0];
97 $MUSIC = &rmts($MUSIC);
98 my $FESTIVALCACHE = "/var/jukeboxcache";
99 if (! -e $FESTIVALCACHE) {
100 `mkdir -p -m0776 $FESTIVALCACHE`;
101 }
102
103 # make sure we have some essential files
104 if (! -e "$FESTIVALCACHE/jukebox_greet.ul") {
105 `echo "Welcome to the Asterisk Jukebox" | text2wave -o $FESTIVALCACHE/jukebox_greet.ul -otype ulaw -`;
106 }
107 if (! -e "$FESTIVALCACHE/jukebox_press.ul") {
108 `echo "Press" | text2wave -o $FESTIVALCACHE/jukebox_press.ul -otype ulaw -`;
109 }
110 if (! -e "$FESTIVALCACHE/jukebox_for.ul") {
111 `echo "For" | text2wave -o $FESTIVALCACHE/jukebox_for.ul -otype ulaw -`;
112 }
113 if (! -e "$FESTIVALCACHE/jukebox_toplay.ul") {
114 `echo "To play" | text2wave -o $FESTIVALCACHE/jukebox_toplay.ul -otype ulaw -`;
115 }
116 if (! -e "$FESTIVALCACHE/jukebox_nonefound.ul") {
117 `echo "There were no music files found in this folder" | text2wave -o $FESTIVALCACHE/jukebox_nonefound.ul -otype ulaw -`;
118 }
119 if (! -e "$FESTIVALCACHE/jukebox_percent.ul") {
120 `echo "Percent" | text2wave -o $FESTIVALCACHE/jukebox_percent.ul -otype ulaw -`;
121 }
122 if (! -e "$FESTIVALCACHE/jukebox_generate.ul") {
123 `echo "Please wait while Astrisk Jukebox cashes the files of your music collection" | text2wave -o $FESTIVALCACHE/jukebox_generate.ul -otype ulaw -`;
124 }
125 if (! -e "$FESTIVALCACHE/jukebox_invalid.ul") {
126 `echo "You have entered an invalid selection" | text2wave -o $FESTIVALCACHE/jukebox_invalid.ul -otype ulaw -`;
127 }
128 if (! -e "$FESTIVALCACHE/jukebox_thankyou.ul") {
129 `echo "Thank you for using Astrisk Jukebox, Goodbye" | text2wave -o $FESTIVALCACHE/jukebox_thankyou.ul -otype ulaw -`;
130 }
131
132 # greet the user
133 if ($SHOWGREET) {
134 print "EXEC Playback \"$FESTIVALCACHE/jukebox_greet\"\n";
135 my $result = <STDIN>; &check_result($result);
136 }
137
138 # go through the directories
139 music_dir_cache() if $PRECACHE;
140 music_dir_menu('/');
141
142 exit 0;
143
144 ##########################################################################
145
146 sub music_dir_menu {
147 my $dir = shift;
148
149 # generate a list of mp3's and directories and assign each one it's
150 # own selection number. Then make sure that we've got a sound clip
151 # for the file name
152 if (!opendir(THEDIR, rmts($MUSIC.$dir))) {
153 print STDERR "Failed to open music directory: $dir\n";
154 exit 1;
155 }
156 my @files = sort readdir THEDIR;
157 my $cnt = 1;
158 my @masterBgList = ();
159
160 foreach my $file (@files) {
161 chomp($file);
162 if ($file ne '.' && $file ne '..' && $file ne 'festivalcache') { # ignore special files
163 my $real_version = &rmts($MUSIC.$dir).'/'.$file;
164 my $cache_version = &rmts($FESTIVALCACHE.$dir).'/'.$file.'.ul';
165 my $cache_version2 = &rmts($FESTIVALCACHE.$dir).'/'.$file;
166 my $cache_version_esc = &clean_file($cache_version);
167 my $cache_version2_esc = &clean_file($cache_version2);
168
169 if (-d $real_version) {
170 # 0:id 1:type 2:text2wav-file 3:for-filtering 4:the-directory 5:text2wav echo
171 push(@masterBgList, [$cnt++, 1, $cache_version2_esc, &remove_special_chars($file), $file, "for the $file folder"]);
172 } elsif ($real_version =~ /\.mp3$/) {
173 # 0:id 1:type 2:text2wav-file 3:for-filtering 4:the-mp3
174 push(@masterBgList, [$cnt++, 2, $cache_version2_esc, &remove_special_chars($file), $real_version, "to play $file"]);
175 }
176 }
177 }
178 close(THEDIR);
179
180 my @filterList = @masterBgList;
181
182 if (@filterList == 0) {
183 print "EXEC Playback \"$FESTIVALCACHE/jukebox_nonefound\"\n";
184 my $result = <STDIN>; &check_result($result);
185 return 0;
186 }
187
188 for (;;) {
189 MYCONTINUE:
190
191 # play bg selections and figure out their selection
192 my $digit = '';
193 my $digitstr = '';
194 for (my $n=0; $n<@filterList; $n++) {
195 &cache_speech(&remove_file_extension($filterList[$n][5]), "$filterList[$n][2].ul") if ! -e "$filterList[$n][2].ul";
196 &cache_speech("Press $filterList[$n][0]", "$FESTIVALCACHE/jukebox_$filterList[$n][0].ul") if ! -e "$FESTIVALCACHE/jukebox_$filterList[$n][0].ul";
197 print "EXEC Background \"$filterList[$n][2]&$FESTIVALCACHE/jukebox_$filterList[$n][0]\"\n";
198 my $result = <STDIN>;
199 $digit = &check_result($result);
200 if ($digit > 0) {
201 $digitstr .= chr($digit);
202 last;
203 }
204 }
205 for (;;) {
206 print "WAIT FOR DIGIT 3000\n";
207 my $result = <STDIN>;
208 $digit = &check_result($result);
209 last if $digit <= 0;
210 $digitstr .= chr($digit);
211 }
212
213 # see if it's a valid selection
214 print STDERR "Digits Entered: '$digitstr'\n";
215 exit 0 if $digitstr eq '';
216 my $found = 0;
217 goto EXITSUB if $digitstr =~ /\*/;
218
219 # filter the list
220 if ($digitstr =~ /^\#\d+/) {
221 my $regexp = '';
222 for (my $n=1; $n<length($digitstr); $n++) {
223 my $d = substr($digitstr, $n, 1);
224 if ($d == 2) {
225 $regexp .= '[abc]';
226 } elsif ($d == 3) {
227 $regexp .= '[def]';
228 } elsif ($d == 4) {
229 $regexp .= '[ghi]';
230 } elsif ($d == 5) {
231 $regexp .= '[jkl]';
232 } elsif ($d == 6) {
233 $regexp .= '[mno]';
234 } elsif ($d == 7) {
235 $regexp .= '[pqrs]';
236 } elsif ($d == 8) {
237 $regexp .= '[tuv]';
238 } elsif ($d == 9) {
239 $regexp .= '[wxyz]';
240 }
241 }
242 @filterList = ();
243 for (my $n=1; $n<@masterBgList; $n++) {
244 push(@filterList, $masterBgList[$n]) if $masterBgList[$n][3] =~ /^$regexp/i;
245 }
246 goto MYCONTINUE;
247 }
248
249 for (my $n=0; $n<@masterBgList; $n++) {
250 if ($digitstr == $masterBgList[$n][0]) {
251 if ($masterBgList[$n][1] == 1) { # a folder
252 &music_dir_menu(rmts($dir).'/'.$masterBgList[$n][4]);
253 @filterList = @masterBgList;
254 goto MYCONTINUE;
255 } elsif ($masterBgList[$n][1] == 2) { # a file
256 # because *'s scripting language is crunk and won't allow us to escape
257 # funny filenames, we need to create a temporary symlink to the mp3
258 # file
259 my $mp3 = &escape_file($masterBgList[$n][4]);
260 my $link = `mktemp`;
261 chomp($link);
262 $link .= '.mp3';
263 print STDERR "ln -s $mp3 $link\n";
264 my $cmdr = `ln -s $mp3 $link`;
265 chomp($cmdr);
266 print "Failed to create symlink to mp3: $cmdr\n" if $cmdr ne '';
267
268 print "EXEC MP3Player \"$link\"\n";
269 my $result = <STDIN>; &check_result($result);
270
271 `rm $link`;
272
273 if (!$MENUAFTERSONG) {
274 print "EXEC Playback \"$FESTIVALCACHE/jukebox_thankyou\"\n";
275 my $result = <STDIN>; &check_result($result);
276 exit 0;
277 } else {
278 goto MYCONTINUE;
279 }
280 }
281 }
282 }
283 print "EXEC Playback \"$FESTIVALCACHE/jukebox_invalid\"\n";
284 my $result = <STDIN>; &check_result($result);
285 }
286 EXITSUB:
287 }
288
289 sub cache_speech {
290 my $speech = shift;
291 my $file = shift;
292
293 my $theDir = extract_file_dir($file);
294 `mkdir -p -m0776 $theDir`;
295
296 print STDERR "echo \"$speech\" | text2wave -o $file -otype ulaw -\n";
297 my $cmdr = `echo "$speech" | text2wave -o $file -otype ulaw -`;
298 chomp($cmdr);
299 if ($cmdr =~ /using default diphone/) {
300 # temporary bug work around....
301 `touch $file`;
302 } elsif ($cmdr ne '') {
303 print STDERR "Command Failed\n";
304 exit 1;
305 }
306 }
307
308 sub music_dir_cache {
309 # generate list of text2speech files to generate
310 if (!music_dir_cache_genlist('/')) {
311 print STDERR "Horrible Dreadful Error: No Music Found in $MUSIC!";
312 exit 1;
313 }
314
315 # add to list how many 'number' files we have to generate. We can't
316 # use the SayNumber app in Asterisk because we want to chain all
317 # talking in one Background command. We also want a consistent
318 # voice...
319 for (my $n=1; $n<=$maxNumber; $n++) {
320 push(@masterCacheList, [3, "Press $n", "$FESTIVALCACHE/jukebox_$n.ul"]) if ! -e "$FESTIVALCACHE/jukebox_$n.ul";
321 }
322
323 # now generate all these darn text2speech files
324 if (@masterCacheList > 5) {
325 print "EXEC Playback \"$FESTIVALCACHE/jukebox_generate\"\n";
326 my $result = <STDIN>; &check_result($result);
327 }
328 my $theTime = time();
329 for (my $n=0; $n < @masterCacheList; $n++) {
330 my $cmdr = '';
331 if ($masterCacheList[$n][0] == 1) { # directory
332 &cache_speech("for folder $masterCacheList[$n][1]", $masterCacheList[$n][2]);
333 } elsif ($masterCacheList[$n][0] == 2) { # file
334 &cache_speech("to play $masterCacheList[$n][1]", $masterCacheList[$n][2]);
335 } elsif ($masterCacheList[$n][0] == 3) { # number
336 &cache_speech($masterCacheList[$n][1], $masterCacheList[$n][2]);
337 }
338 if (time() >= $theTime + 30) {
339 my $percent = int($n / @masterCacheList * 100);
340 print "SAY NUMBER $percent \"\"\n";
341 my $result = <STDIN>; &check_result($result);
342 print "EXEC Playback \"$FESTIVALCACHE/jukebox_percent\"\n";
343 my $result = <STDIN>; &check_result($result);
344 $theTime = time();
345 }
346 }
347 }
348
349 # this function will fill the @masterCacheList of all the files that
350 # need to have text2speech ulaw files of their names generated
351 sub music_dir_cache_genlist {
352 my $dir = shift;
353 if (!opendir(THEDIR, rmts($MUSIC.$dir))) {
354 print STDERR "Failed to open music directory: $dir\n";
355 exit 1;
356 }
357 my @files = sort readdir THEDIR;
358 my $foundFiles = 0;
359 my $tmpMaxNum = 0;
360 foreach my $file (@files) {
361 chomp;
362 if ($file ne '.' && $file ne '..' && $file ne 'festivalcache') { # ignore special files
363 my $real_version = &rmts($MUSIC.$dir).'/'.$file;
364 my $cache_version = &rmts($FESTIVALCACHE.$dir).'/'.$file.'.ul';
365 my $cache_version2 = &rmts($FESTIVALCACHE.$dir).'/'.$file;
366 my $cache_version_esc = &clean_file($cache_version);
367 my $cache_version2_esc = &clean_file($cache_version2);
368
369 if (-d $real_version) {
370 if (music_dir_cache_genlist(rmts($dir).'/'.$file)) {
371 $tmpMaxNum++;
372 $maxNumber = $tmpMaxNum if $tmpMaxNum > $maxNumber;
373 push(@masterCacheList, [1, $file, $cache_version_esc]) if ! -e $cache_version_esc;
374 $foundFiles = 1;
375 }
376 } elsif ($real_version =~ /\.mp3$/) {
377 $tmpMaxNum++;
378 $maxNumber = $tmpMaxNum if $tmpMaxNum > $maxNumber;
379 push(@masterCacheList, [2, &remove_file_extension($file), $cache_version_esc]) if ! -e $cache_version_esc;
380 $foundFiles = 1;
381 }
382 }
383 }
384 close(THEDIR);
385 return $foundFiles;
386 }
387
388 sub rmts { # remove trailing slash
389 my $hog = shift;
390 $hog =~ s/\/$//;
391 return $hog;
392 }
393
394 sub extract_file_name {
395 my $hog = shift;
396 $hog =~ /\/?([^\/]+)$/;
397 return $1;
398 }
399
400 sub extract_file_dir {
401 my $hog = shift;
402 return $hog if ! ($hog =~ /\//);
403 $hog =~ /(.*)\/[^\/]*$/;
404 return $1;
405 }
406
407 sub remove_file_extension {
408 my $hog = shift;
409 return $hog if ! ($hog =~ /\./);
410 $hog =~ /(.*)\.[^.]*$/;
411 return $1;
412 }
413
414 sub clean_file {
415 my $hog = shift;
416 $hog =~ s/\\/_/g;
417 $hog =~ s/ /_/g;
418 $hog =~ s/\t/_/g;
419 $hog =~ s/\'/_/g;
420 $hog =~ s/\"/_/g;
421 $hog =~ s/\(/_/g;
422 $hog =~ s/\)/_/g;
423 $hog =~ s/&/_/g;
424 $hog =~ s/\[/_/g;
425 $hog =~ s/\]/_/g;
426 $hog =~ s/\$/_/g;
427 $hog =~ s/\|/_/g;
428 $hog =~ s/\^/_/g;
429 return $hog;
430 }
431
432 sub remove_special_chars {
433 my $hog = shift;
434 $hog =~ s/\\//g;
435 $hog =~ s/ //g;
436 $hog =~ s/\t//g;
437 $hog =~ s/\'//g;
438 $hog =~ s/\"//g;
439 $hog =~ s/\(//g;
440 $hog =~ s/\)//g;
441 $hog =~ s/&//g;
442 $hog =~ s/\[//g;
443 $hog =~ s/\]//g;
444 $hog =~ s/\$//g;
445 $hog =~ s/\|//g;
446 $hog =~ s/\^//g;
447 return $hog;
448 }
449
450 sub escape_file {
451 my $hog = shift;
452 $hog =~ s/\\/\\\\/g;
453 $hog =~ s/ /\\ /g;
454 $hog =~ s/\t/\\\t/g;
455 $hog =~ s/\'/\\\'/g;
456 $hog =~ s/\"/\\\"/g;
457 $hog =~ s/\(/\\\(/g;
458 $hog =~ s/\)/\\\)/g;
459 $hog =~ s/&/\\&/g;
460 $hog =~ s/\[/\\\[/g;
461 $hog =~ s/\]/\\\]/g;
462 $hog =~ s/\$/\\\$/g;
463 $hog =~ s/\|/\\\|/g;
464 $hog =~ s/\^/\\\^/g;
465 return $hog;
466 }
467
468 sub check_result {
469 my ($res) = @_;
470 my $retval;
471 $tests++;
472 chomp $res;
473 if ($res =~ /^200/) {
474 $res =~ /result=(-?\d+)/;
475 if (!length($1)) {
476 print STDERR "FAIL ($res)\n";
477 $fail++;
478 exit 1;
479 } else {
480 print STDERR "PASS ($1)\n";
481 return $1;
482 }
483 } else {
484 print STDERR "FAIL (unexpected result '$res')\n";
485 exit 1;
486 }
487 }