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