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