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