Update vmail.cgi for the new segmented voicemail system
[asterisk/asterisk.git] / vmail.cgi
1 #!/usr/bin/perl
2 #
3 # Web based Voicemail for Asterisk
4 #
5 # Copyright (C) 2002, Linux Support Services, Inc.
6 #
7 # Distributed under the terms of the GNU General Public License
8 #
9 # Written by Mark Spencer <markster@linux-support.net>
10 #
11 # (icky, I know....  if you know better perl please help!)
12 #
13 #
14 use CGI qw/:standard/;
15 use CGI::Carp qw(fatalsToBrowser);
16
17 @validfolders = ( "INBOX", "Old", "Work", "Family", "Friends", "Cust1", "Cust2", "Cust3", "Cust4", "Cust5" );
18
19 %formats = (
20         "wav" => {
21                 name => "Uncompressed WAV",
22                 mime => "audio/x-wav",
23                 pref => 1
24         },
25         "WAV" => {
26                 name => "GSM Compressed WAV",
27                 mime => "audio/x-wav",
28                 pref => 2
29         },
30         "gsm" => {
31                 name => "Raw GSM Audio",
32                 mime => "audio/x-gsm",
33                 pref => 3
34         }
35 );
36
37 $astpath = "/_asterisk";
38
39 $stdcontainerstart = "<table align=center width=600><tr><td>\n";
40 $footer = "<hr><font size=-1><a href=\"http://www.asterisk.org\">The Asterisk Open Source PBX</a> Copyright 2002, <a href=\"http://www.digium.com\">Digium, Inc.</a></a>";
41 $stdcontainerend = "</td></tr><tr><td align=right>$footer</td></tr></table>\n";
42
43 sub login_screen() {
44         print header;
45         my ($message) = @_;
46         print <<_EOH;
47
48 <TITLE>Asterisk Web-Voicemail</TITLE>
49 <BODY BGCOLOR="white">
50 $stdcontainerstart
51 <FORM METHOD="post">
52 <input type=hidden name="action" value="login">
53 <table align=center>
54 <tr><td valign=top align=center rowspan=6><img align=center src="$astpath/animlogo.gif"></td></tr>
55 <tr><td align=center colspan=2><font size=+2>Commedian Mail Login</font></td></tr>
56 <tr><td align=center colspan=2><font size=+1>$message</font></td></tr>
57 <tr><td>Mailbox:</td><td><input type=text name="mailbox"></td></tr>
58 <tr><td>Password:</td><td><input type=password name="password"></td></tr>
59 <tr><td align=right colspan=2><input value="Login" type=submit></td></tr>
60 </table>
61 </FORM>
62 $stdcontainerend
63 </BODY>\n
64 _EOH
65
66 }
67
68 sub check_login()
69 {
70         my ($mbox, $context) = split(/\@/, param('mailbox'));
71         my $pass = param('password');
72         my $category = "general";
73         my @fields;
74         if (!$context) {
75                 $context = param('context');
76         }
77         if (!$context) {
78                 $context = "default";
79         }
80         open(VMAIL, "</etc/asterisk/voicemail.conf") || die("Bleh, no voicemail.conf");
81         while(<VMAIL>) {
82                 chomp;
83                 if (/\[(.*)\]/) {
84                         $category = $1;
85                 } elsif ($category ne "general") {
86                         if (/([^\s]+)\s*\=\>?\s*(.*)/) {
87                                 @fields = split(/\,\s*/, $2);
88                                 if (($mbox eq $1) && ($pass eq $fields[0]) && ($context eq $category)) {
89                                         return $fields[1] ? $fields[1] : "Extension $mbox in $context";
90                                 }
91                         }
92                 }
93         }
94 }
95
96 sub validmailbox()
97 {
98         my ($context, $mbox) = @_;
99         my $category = "general";
100         my @fields;
101         open(VMAIL, "</etc/asterisk/voicemail.conf") || die("Bleh, no voicemail.conf");
102         while(<VMAIL>) {
103                 chomp;
104                 if (/\[(.*)\]/) {
105                         $category = $1;
106                 } elsif (($category ne "general") && ($category eq $context)) {
107                         if (/([^\s]+)\s*\=\>?\s*(.*)/) {
108                                 @fields = split(/\,\s*/, $2);
109                                 if ($mbox eq $1) {
110                                         return $fields[2] ? $fields[2] : "unknown";
111                                 }
112                         }
113                 }
114         }
115 }
116
117 sub mailbox_list()
118 {
119         my ($name, $context, $current) = @_;
120         my $tmp;
121         my $text;
122         $tmp = "<SELECT name=\"$name\">\n";
123         open(VMAIL, "</etc/asterisk/voicemail.conf") || die("Bleh, no voicemail.conf");
124         while(<VMAIL>) {
125                 chomp;
126                 s/\;.*$//;
127                 if (/\[(.*)\]/) {
128                         $category = $1;
129                 } elsif ($category ne "general") {
130                         if (/([^\s]+)\s*\=\>?\s*(.*)/) {
131                                 @fields = split(/\,\s*/, $2);
132                                 $text = "$1";
133                                 if ($fields[2]) {
134                                         $text .= " ($fields[1])";
135                                 }
136                                 if ($1 eq $current) {
137                                         $tmp .= "<OPTION SELECTED>$text</OPTION>\n";
138                                 } else {
139                                         $tmp .= "<OPTION>$text</OPTION>\n";
140                                 }
141                                 
142                                 if (($mbox eq $1) && ($pass eq $fields[0])) {
143                                         return $fields[1];
144                                 }
145                         }
146                 }
147         }
148         $tmp .= "</SELECT>\n";
149         
150 }
151
152 sub msgcount() 
153 {
154         my ($context, $mailbox, $folder) = @_;
155         my $path = "/var/spool/asterisk/voicemail/$context/$mailbox/$folder";
156         if (opendir(DIR, $path)) {
157                 my @msgs = grep(/^msg....\.txt$/, readdir(DIR));
158                 closedir(DIR);
159                 return sprintf "%d", $#msgs + 1;
160         }
161         return "0";
162 }
163
164 sub msgcountstr()
165 {
166         my ($context, $mailbox, $folder) = @_;
167         my $count = &msgcount($context, $mailbox, $folder);
168         if ($count > 1) {
169                 "$count messages";
170         } elsif ($count > 0) {
171                 "$count message";
172         } else {
173                 "no messages";
174         }
175 }
176 sub messages()
177 {
178         my ($context, $mailbox, $folder) = @_;
179         my $path = "/var/spool/asterisk/voicemail/$context/$mailbox/$folder";
180         if (opendir(DIR, $path)) {
181                 my @msgs = sort grep(/^msg....\.txt$/, readdir(DIR));
182                 closedir(DIR);
183                 return map { s/^msg(....)\.txt$/$1/; $_ } @msgs;
184         }
185         return ();
186 }
187
188 sub getcookie()
189 {
190         my ($var) = @_;
191         return cookie($var);
192 }
193
194 sub makecookie()
195 {
196         my ($format) = @_;
197         cookie(-name => "format", -value =>["$format"], -expires=>"+1y");
198 }
199
200 sub getfields()
201 {
202         my ($context, $mailbox, $folder, $msg) = @_;
203         my $fields;
204         if (open(MSG, "</var/spool/asterisk/voicemail/$context/$mailbox/$folder/msg${msg}.txt")) {
205                 while(<MSG>) {
206                         s/\#.*$//g;
207                         if (/^(\w+)\s*\=\s*(.*)$/) {
208                                 $fields->{$1} = $2;
209                         }
210                 }
211                 close(MSG);
212                 $fields->{'msgid'} = $msg;
213         } else { print "<BR>Unable to open '$msg' in '$mailbox', '$folder'\n<B>"; }
214         $fields;
215 }
216
217 sub message_prefs()
218 {
219         my ($nextaction, $msgid) = @_;
220         my $folder = param('folder');
221         my $mbox = param('mailbox');
222         my $passwd = param('password');
223         my $format = param('format');
224         if (!$format) {
225                 $format = &getcookie('format');
226         }
227         print header;
228         print <<_EOH;
229
230 <TITLE>Asterisk Web-Voicemail: Preferences</TITLE>
231 <BODY BGCOLOR="white">
232 $stdcontainerstart
233 <FORM METHOD="post">
234 <table width=100% align=center>
235 <tr><td align=right colspan=3><font size=+2>Web Voicemail Preferences</font></td></tr>
236 <tr><td align=left><font size=+1>Preferred&nbsp;Audio&nbsp;Format:</font></td><td colspan=2></td></tr>
237 _EOH
238
239 foreach $fmt (sort { $formats{$a}->{'pref'} <=> $formats{$b}->{'pref'} } keys %formats) {
240         my $clicked = "checked" if $fmt eq $format;
241         print "<tr><td></td><td align=left><input type=radio name=\"format\" $clicked value=\"$fmt\"></td><td width=100%>&nbsp;$formats{$fmt}->{name}</td></tr>\n";
242 }
243
244 print <<_EOH;
245 <tr><td align=right colspan=3><input type=submit value="save settings..."></td></tr>
246 </table>
247 <input type=hidden name="action" value="$nextaction">
248 <input type=hidden name="folder" value="$folder">
249 <input type=hidden name="mailbox" value="$mbox">
250 <input type=hidden name="context" value="$context">
251 <input type=hidden name="password" value="$passwd">
252 <input type=hidden name="msgid" value="$msgid">
253 $stdcontainerend
254 </BODY>\n
255 _EOH
256
257 }
258
259 sub message_play()
260 {
261         my ($message, $msgid) = @_;
262         my $folder = param('folder');
263         my ($mbox, $context) = split(/\@/, param('mailbox'));
264         my $passwd = param('password');
265         my $format = param('format');
266         
267         my $fields;
268         if (!$context) {
269                 $context = param('context');
270         }
271         if (!$context) {
272                 $context = "default";
273         }
274         
275         my $folders = &folder_list('newfolder', $context, $mbox, $folder);
276         my $mailboxes = &mailbox_list('forwardto', $context, $mbox);
277         if (!$format) {
278                 $format = &getcookie('format');
279         }
280         if (!$format) {
281                 &message_prefs("play", $msgid);
282         } else {
283                 print header(-cookie => &makecookie($format));
284                 $fields = &getfields($context, $mbox, $folder, $msgid);
285                 if (!$fields) {
286                         print "<BR>Bah!\n";
287                         return;
288                 }
289                 my $duration = $fields->{'duration'};
290                 if ($duration) {
291                         $duration = sprintf "%d:%02d", $duration/60, $duration % 60; 
292                 } else {
293                         $duration = "<i>Unknown</i>";
294                 }
295                 print <<_EOH;
296         
297 <TITLE>Asterisk Web-Voicemail: $folder Message $msgid</TITLE>
298 <BODY BGCOLOR="white">
299 $stdcontainerstart
300 <FORM METHOD="post">
301 <table width=100% align=center>
302 <tr><td align=right colspan=3><font size=+1>$folder Message $msgid</font></td></tr>
303 _EOH
304
305                 print <<_EOH;
306 <tr><td align=center colspan=3>
307 <table>
308         <tr><td colspan=2 align=center><font size=+1>$folder <b>$msgid</b></font></td></tr>
309         <tr><td><b>Message:</b></td><td>$msgid</td></tr>\n
310         <tr><td><b>Mailbox:</b></td><td>$mbox</td></tr>\n
311         <tr><td><b>Folder:</b></td><td>$folder</td></tr>\n
312         <tr><td><b>From:</b></td><td>$fields->{callerid}</td></tr>\n
313         <tr><td><b>Duration:</b></td><td>$duration</td></tr>\n
314         <tr><td><b>Original Date:</b></td><td>$fields->{origdate}</td></tr>\n
315         <tr><td><b>Original Mailbox:</b></td><td>$fields->{origmailbox}</td></tr>\n
316         <tr><td><b>Caller Channel:</b></td><td>$fields->{callerchan}</td></tr>\n
317         <tr><td align=center colspan=2>
318         <input name="action" type=submit value="index">&nbsp;
319         <input name="action" type=submit value="delete ">&nbsp;
320         <input name="action" type=submit value="forward to -> ">&nbsp;
321         $mailboxes&nbsp;
322         <input name="action" type=submit value="save to ->">
323         $folders&nbsp;
324         <input name="action" type=submit value="play ">
325         <input name="action" type=submit value="download">
326 </td></tr>
327 <tr><td colspan=2 align=center>
328 <embed width=400 height=40 src="vmail.cgi?action=audio&folder=$folder&mailbox=$mbox&password=$passwd&msgid=$msgid&format=$format&dontcasheme=$$.$format" autostart=yes loop=false></embed>
329 </td></tr></table>
330 </td></tr>
331 </table>
332 <input type=hidden name="folder" value="$folder">
333 <input type=hidden name="mailbox" value="$mbox">
334 <input type=hidden name="context" value="$context">
335 <input type=hidden name="password" value="$passwd">
336 <input type=hidden name="msgid" value="$msgid">
337 $stdcontainerend
338 </BODY>\n
339 _EOH
340         }
341 }
342
343 sub message_audio()
344 {
345         my ($forcedownload) = @_;
346         my $folder = param('folder');
347         my $msgid = param('msgid');
348         my $mailbox = param('mailbox');
349         my $format = param('format');
350         if (!$format) {
351                 $format = &getcookie('format');
352         }
353         my $path = "/var/spool/asterisk/vm/$mailbox/$folder/msg${msgid}.$format";
354
355         $msgid =~ /^\d\d\d\d$/ || die("Msgid Liar ($msgid)!");
356         grep(/^${format}$/, keys %formats) || die("Format Liar ($format)!");
357
358         # Mailbox and folder are already verified
359         if (open(AUDIO, "<$path")) {
360                 $size = -s $path;
361                 $|=1;
362                 if ($forcedownload) {
363                         print header(-type=>$formats{$format}->{'mime'}, -Content_length => $size, -attachment => "msg${msgid}.$format");
364                 } else {                
365                         print header(-type=>$formats{$format}->{'mime'}, -Content_length => $size);
366                 }
367                 
368                 while(($amt = sysread(AUDIO, $data, 4096)) > 0) {
369                         syswrite(STDOUT, $data, $amt);
370                 }
371                 close(AUDIO);
372         } else {
373                 die("Hrm, can't seem to open $path\n");
374         }
375 }
376
377 sub message_index() 
378 {
379         my ($folder, $message) = @_;
380         my ($mbox, $context) = split(/\@/, param('mailbox'));
381         my $passwd = param('password');
382         my $message2;
383         my $msgcount;   
384         my $hasmsg;
385         my $newmessages, $oldmessages;
386         my $format = param('format');
387         if (!$format) {
388                 $format = &getcookie('format');
389         }
390         if (!$context) {
391                 $context = param('context');
392         }
393         if (!$context) {
394                 $context = "default";
395         }
396         if ($folder) {
397                 $msgcount = &msgcountstr($context, $mbox, $folder);
398                 $message2 = "&nbsp;&nbsp;&nbsp;Folder '$folder' has " . &msgcountstr($context, $mbox, $folder);
399         } else {
400                 $newmessages = &msgcount($context, $mbox, "INBOX");
401                 $oldmessages = &msgcount($context, $mbox, "Old");
402                 if (($newmessages > 0) || ($oldmessages < 1)) {
403                         $folder = "INBOX";
404                 } else {
405                         $folder = "Old";
406                 }
407                 $message2 = "You have";
408                 if ($newmessages > 0) {
409                         $message2 .= " <b>$newmessages</b> NEW";
410                         if ($oldmessages > 0) {
411                                 $message2 .= "and <b>$oldmessages</b> OLD";
412                                 if ($oldmessages != 1) {
413                                         $message2 .= " messages.";
414                                 } else {
415                                         $message2 .= "message.";
416                                 }
417                         } else {
418                                 if ($newmessages != 1) {
419                                         $message2 .= " messages.";
420                                 } else {
421                                         $message2 .= " message.";
422                                 }
423                         }
424                 } else {
425                         if ($oldmessages > 0) {
426                                 $message2 .= " <b>$oldmessages</b> OLD";
427                                 if ($oldmessages != 1) {
428                                         $message2 .= " messages.";
429                                 } else {
430                                         $message2 .= " message.";
431                                 }
432                         } else {
433                                 $message2 .= " <b>no</b> messages.";
434                         }
435                 }
436         }
437         
438         my $folders = &folder_list('newfolder', $context, $mbox, $folder);
439         my $cfolders = &folder_list('changefolder', $context, $mbox, $folder);
440         my $mailboxes = &mailbox_list('forwardto', $context, $mbox);
441         print header(-cookie => &makecookie($format));
442         print <<_EOH;
443
444 <TITLE>Asterisk Web-Voicemail: $mbox $folder</TITLE>
445 <BODY BGCOLOR="white">
446 $stdcontainerstart
447 <FORM METHOD="post">
448 <table width=100% align=center>
449 <tr><td align=center colspan=2><font size=+2><I>$message</I></font></td></tr>
450 <tr><td align=right colspan=2><font size=+1><b>$folder</b> Messages</font> <input type=submit name="action" value="change to ->">$cfolders</td></tr>
451 <tr><td align=left colspan=2><font size=+1>$message2</font></td></tr>
452 </table>
453 <table width=100% align=center cellpadding=0 cellspacing=0>
454 _EOH
455
456 print "<tr><td>&nbsp;Msg</td><td>&nbsp;From</td><td>&nbsp;Duration</td><td>&nbsp;Date</td><td>&nbsp;</td></tr>\n";
457 print "<tr><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td></td></tr>\n";
458 foreach $msg (&messages($context, $mbox, $folder)) {
459
460         $fields = &getfields($context, $mbox, $folder, $msg);
461         $duration = $fields->{'duration'};
462         if ($duration) {
463                 $duration = sprintf "%d:%02d", $duration / 60, $duration % 60;
464         } else {
465                 $duration = "<i>Unknown</i>";
466         }
467         $hasmsg++;
468         print "<tr><td><input type=checkbox name=\"msgselect\" value=\"$msg\">&nbsp;<b>$msg</b></td><td>$fields->{'callerid'}</td><td>$duration</td><td>$fields->{'origdate'}</td><td><input name='play$msg' alt=\"Play message $msg\" border=0 type=image align=left src=\"$astpath/play.gif\"></td></tr>\n";
469
470 }
471 if (!$hasmsg) {
472         print "<tr><td colspan=4 align=center><P><b><i>No messages</i></b><P></td></tr>";
473 }
474
475 print <<_EOH;
476 </table>
477 <table width=100% align=center>
478 <tr><td align=right colspan=2>
479         <input type="submit" name="action" value="refresh">&nbsp;
480 _EOH
481
482 if ($hasmsg) {
483 print <<_EOH;
484         <input type="submit" name="action" value="delete">&nbsp;
485         <input type="submit" name="action" value="save to ->">
486         $folders&nbsp;
487         <input type="submit" name="action" value="forward to ->">
488         $mailboxes
489 _EOH
490 }
491
492 print <<_EOH;
493 </td></tr>
494 <tr><td align=right colspan=2>
495         <input type="submit" name="action" value="preferences">
496         <input type="submit" name="action" value="logout">
497 </td></tr>
498 </table>
499 <input type=hidden name="folder" value="$folder">
500 <input type=hidden name="mailbox" value="$mbox">
501 <input type=hidden name="context" value="$context">
502 <input type=hidden name="password" value="$passwd">
503 </FORM>
504 $stdcontainerend
505 </BODY>\n
506 _EOH
507 }
508
509 sub validfolder()
510 {
511         my ($folder) = @_;
512         return grep(/^$folder$/, @validfolders);
513 }
514
515 sub folder_list()
516 {
517         my ($name, $context, $mbox, $selected) = @_;
518         my $f;
519         my $count;
520         my $tmp = "<SELECT name=\"$name\">\n";
521         foreach $f (@validfolders) {
522                 $count =  &msgcount($context, $mbox, $f);
523                 if ($f eq $selected) {
524                         $tmp .= "<OPTION SELECTED>$f ($count)</OPTION>\n";
525                 } else {
526                         $tmp .= "<OPTION>$f ($count)</OPTION>\n";
527                 }
528         }
529         $tmp .= "</SELECT>";
530 }
531
532 sub message_rename()
533 {
534         my ($context, $mbox, $oldfolder, $old, $newfolder, $new) = @_;
535         my $oldfile, $newfile;
536         return if ($old eq $new) && ($oldfolder eq $newfolder);
537         
538         if ($mbox =~ /^(\w+)$/) {
539                 $mbox = $1;
540         } else {
541                 die ("Invalid mailbox<BR>\n");
542         }
543         
544         if ($oldfolder =~ /^(\w+)$/) {
545                 $oldfolder = $1;
546         } else {
547                 die("Invalid old folder<BR>\n");
548         }
549         
550         if ($newfolder =~ /^(\w+)$/) {
551                 $newfolder = $1;
552         } else {
553                 die("Invalid new folder ($newfolder)<BR>\n");
554         }
555         
556         if ($old =~ /^(\d\d\d\d)$/) {
557                 $old = $1;
558         } else {
559                 die("Invalid old Message<BR>\n");
560         }
561         
562         if ($new =~ /^(\d\d\d\d)$/) {
563                 $new = $1;
564         } else {
565                 die("Invalid old Message<BR>\n");
566         }
567         
568         my $path = "/var/spool/asterisk/voicemail/$context/$mbox/$newfolder";
569         mkdir $path, 0755;
570         my $path = "/var/spool/asterisk/voicemail/$context/$mbox/$oldfolder";
571         opendir(DIR, $path) || die("Unable to open directory\n");
572         my @files = grep /^msg${old}\.\w+$/, readdir(DIR);
573         closedir(DIR);
574         foreach $oldfile (@files) {
575                 my $tmp = $oldfile;
576                 if ($tmp =~ /^(msg${old}.\w+)$/) {
577                         $tmp = $1;
578                         $oldfile = $path . "/$tmp";
579                         $tmp =~ s/msg${old}/msg${new}/;
580                         $newfile = "/var/spool/asterisk/voicemail/$context/$mbox/$newfolder/$tmp";
581 #                       print "Renaming $oldfile to $newfile<BR>\n";
582                         rename($oldfile, $newfile);
583                 }
584         }
585 }
586
587 sub file_copy()
588 {
589         my ($orig, $new) = @_;
590         my $res;
591         my $data;
592         open(IN, "<$orig") || die("Unable to open '$orig'\n");
593         open(OUT, ">$new") || DIE("Unable to open '$new'\n");
594         while(($res = sysread(IN, $data, 4096)) > 0) {
595                 syswrite(OUT, $data, $res);
596         }
597         close(OUT);
598         close(IN);
599 }
600
601 sub message_copy()
602 {
603         my ($mbox, $oldfolder, $old, $newmbox, $new) = @_;
604         my $oldfile, $newfile;
605         return if ($mbox eq $newmbox);
606         
607         if ($mbox =~ /^(\w+)$/) {
608                 $mbox = $1;
609         } else {
610                 die ("Invalid mailbox<BR>\n");
611         }
612
613         if ($newmbox =~ /^(\w+)$/) {
614                 $newmbox = $1;
615         } else {
616                 die ("Invalid new mailbox<BR>\n");
617         }
618         
619         if ($oldfolder =~ /^(\w+)$/) {
620                 $oldfolder = $1;
621         } else {
622                 die("Invalid old folder<BR>\n");
623         }
624         
625         if ($old =~ /^(\d\d\d\d)$/) {
626                 $old = $1;
627         } else {
628                 die("Invalid old Message<BR>\n");
629         }
630         
631         if ($new =~ /^(\d\d\d\d)$/) {
632                 $new = $1;
633         } else {
634                 die("Invalid old Message<BR>\n");
635         }
636         
637         my $path = "/var/spool/asterisk/vm/$newmbox";
638         mkdir $path, 0755;
639         my $path = "/var/spool/asterisk/vm/$newmbox/INBOX";
640         mkdir $path, 0755;
641         my $path = "/var/spool/asterisk/vm/$mbox/$oldfolder";
642         opendir(DIR, $path) || die("Unable to open directory\n");
643         my @files = grep /^msg${old}\.\w+$/, readdir(DIR);
644         closedir(DIR);
645         foreach $oldfile (@files) {
646                 my $tmp = $oldfile;
647                 if ($tmp =~ /^(msg${old}.\w+)$/) {
648                         $tmp = $1;
649                         $oldfile = $path . "/$tmp";
650                         $tmp =~ s/msg${old}/msg${new}/;
651                         $newfile = "/var/spool/asterisk/vm/$newmbox/INBOX/$tmp";
652 #                       print "Copying $oldfile to $newfile<BR>\n";
653                         &file_copy($oldfile, $newfile);
654                 }
655         }
656 }
657
658 sub message_delete()
659 {
660         my ($context, $mbox, $folder, $msg) = @_;
661         if ($mbox =~ /^(\w+)$/) {
662                 $mbox = $1;
663         } else {
664                 die ("Invalid mailbox<BR>\n");
665         }
666         if ($context =~ /^(\w+)$/) {
667                 $context = $1;
668         } else {
669                 die ("Invalid context<BR>\n");
670         }
671         if ($folder =~ /^(\w+)$/) {
672                 $folder = $1;
673         } else {
674                 die("Invalid folder<BR>\n");
675         }
676         if ($msg =~ /^(\d\d\d\d)$/) {
677                 $msg = $1;
678         } else {
679                 die("Invalid Message<BR>\n");
680         }
681         my $path = "/var/spool/asterisk/voicemail/$context/$mbox/$folder";
682         opendir(DIR, $path) || die("Unable to open directory\n");
683         my @files = grep /^msg${msg}\.\w+$/, readdir(DIR);
684         closedir(DIR);
685         foreach $oldfile (@files) {
686                 if ($oldfile =~ /^(msg${msg}.\w+)$/) {
687                         $oldfile = $path . "/$1";
688 #                       print "Deleting $oldfile<BR>\n";
689                         unlink($oldfile);
690                 }
691         }
692 }
693
694 sub message_forward()
695 {
696         my ($toindex, @msgs) = @_;
697         my $folder = param('folder');
698         my ($mbox, $context) = split(/\@/, param('mailbox'));
699         my $newmbox = param('forwardto');
700         my $msg;
701         my $msgcount;
702         if (!$context) {
703                 $context = param('context');
704         }
705         if (!$context) {
706                 $context = "default";
707         }
708         $newmbox =~ s/(\w+)(\s+.*)?$/$1/;
709         if (!&validmailbox($context, $newmbox)) {
710                 die("Bah! Not a valid mailbox '$newmbox'\n");
711                 return "";
712         }
713         $msgcount = &msgcount($context, $newmbox, "INBOX");
714         my $txt;
715         if ($newmbox ne $mbox) {
716 #               print header;
717                 foreach $msg (@msgs) {
718 #                       print "Forwarding $msg from $mbox to $newmbox<BR>\n";
719                         &message_copy($context, $mbox, $folder, $msg, $newmbox, sprintf "%04d", $msgcount);
720                         $msgcount++;
721                 }
722                 $txt = "Forwarded messages " . join(', ', @msgs) . "to $newmbox";
723         } else {
724                 $txt = "Can't forward messages to yourself!\n";
725         } 
726         if ($toindex) {
727                 &message_index($folder, $txt);
728         } else {
729                 &message_play($txt, $msgs[0]);
730         }
731 }
732
733 sub message_delete_or_move()
734 {
735         my ($toindex, $del, @msgs) = @_;
736         my $txt;
737         my $path;
738         my $y, $x;
739         my $folder = param('folder');
740         my $newfolder = param('newfolder') unless $del;
741         $newfolder =~ s/^(\w+)\s+.*$/$1/;
742         my ($mbox, $context) = split(/\@/, param('mailbox'));
743         if (!$context) {
744                 $context = param('context');
745         }
746         if (!$context) {
747                 $context = "default";
748         }
749         my $passwd = param('password');
750         my $msgcount = &msgcount($context, $mbox, $folder);
751         my $omsgcount = &msgcount($context, $mbox, $newfolder) if $newfolder;
752 #       print header;
753         if ($newfolder ne $folder) {
754                 $y = 0;
755                 for ($x=0;$x<$msgcount;$x++) {
756                         my $msg = sprintf "%04d", $x;
757                         my $newmsg = sprintf "%04d", $y;
758                         if (grep(/^$msg$/, @msgs)) {
759                                 if ($newfolder) {
760                                         &message_rename($context, $mbox, $folder, $msg, $newfolder, sprintf "%04d", $omsgcount);
761                                         $omsgcount++;
762                                 } else {
763                                         &message_delete($context, $mbox, $folder, $msg);
764                                 }
765                         } else {
766                                 &message_rename($context, $mbox, $folder, $msg, $folder, $newmsg);
767                                 $y++;
768                         }
769                 }
770                 if ($del) {
771                         $txt = "Deleted messages "  . join (', ', @msgs);
772                 } else {
773                         $txt = "Moved messages "  . join (', ', @msgs) . " to $newfolder";
774                 }
775         } else {
776                 $txt = "Can't move a message to the same folder they're in already";
777         }
778         # Not as many messages now
779         $msgcount--;
780         if ($toindex || ($msgs[0] >= $msgcount)) {
781                 &message_index($folder, $txt);  
782         } else {
783                 &message_play($txt, $msgs[0]);
784         }
785 }
786
787 if (param()) {
788         my $folder = param('folder');
789         my $changefolder = param('changefolder');
790         $changefolder =~ s/(\w+)\s+.*$/$1/;
791         
792         my $newfolder = param('newfolder');
793         $newfolder =~ s/^(\w+)\s+.*$/$1/;
794         if ($newfolder && !&validfolder($newfolder)) {
795                 print header;
796                 die("Bah! new folder '$newfolder' isn't a folder.");
797         }
798         $action = param('action');
799         $msgid = param('msgid');
800         if (!$action) {
801                 my ($tmp) = grep /^play\d\d\d\d\.x$/, param;
802                 if ($tmp =~ /^play(\d\d\d\d)/) {
803                         $msgid = $1;
804                         $action = "play";
805                 } else {
806                         print header;
807                         print "No message?<BR>\n";
808                         return;
809                 }
810         }
811         @msgs = param('msgselect');
812         @msgs = ($msgid) unless @msgs;
813         {
814                 $mailbox = check_login();
815                 if ($mailbox) {
816                         if ($action eq 'login') {
817                                 &message_index($folder, "Welcome, $mailbox");
818                         } elsif (($action eq 'refresh') || ($action eq 'index')) {
819                                 &message_index($folder, "Welcome, $mailbox");
820                         } elsif ($action eq 'change to ->') {
821                                 if (&validfolder($changefolder)) {
822                                         $folder = $changefolder;
823                                         &message_index($folder, "Welcome, $mailbox");
824                                 } else {
825                                         die("Bah!  Not a valid change to folder '$changefolder'\n");
826                                 }
827                         } elsif ($action eq 'play') {
828                                 &message_play("$mailbox $folder $msgid", $msgid);
829                         } elsif ($action eq 'preferences') {
830                                 &message_prefs("refresh", $msgid);
831                         } elsif ($action eq 'download') {
832                                 &message_audio(1);
833                         } elsif ($action eq 'play ') {
834                                 &message_audio(0);
835                         } elsif ($action eq 'audio') {
836                                 &message_audio(0);
837                         } elsif ($action eq 'delete') {
838                                 &message_delete_or_move(1, 1, @msgs);
839                         } elsif ($action eq 'delete ') {
840                                 &message_delete_or_move(0, 1, @msgs);
841                         } elsif ($action eq 'forward to ->') {
842                                 &message_forward(1, @msgs);
843                         } elsif ($action eq 'forward to -> ') {
844                                 &message_forward(0, @msgs);
845                         } elsif ($action eq 'save to ->') {
846                                 &message_delete_or_move(1, 0, @msgs);
847                         } elsif ($action eq 'save to -> ') {
848                                 &message_delete_or_move(0, 0, @msgs);
849                         } elsif ($action eq 'logout') {
850                                 &login_screen("Logged out!\n");
851                         }
852                 } else {
853                         sleep(1);
854                         &login_screen("Login Incorrect!\n");
855                 }
856         }
857 } else {
858         &login_screen("\&nbsp;");
859 }