#!/usr/local/bin/perl
# list_mail.cgi
# List the mail messages for some user

require './sendmail-lib.pl';
require './boxes-lib.pl';
&ReadParse();
&can_read_mail($in{'user'}) || &error($text{'mail_ecannot'});

if ($config{'track_read'}) {
	dbmopen(%read, "$module_config_directory/$in{'user'}.read", 0600);
	}

$sentbox = $in{'user'} eq $access{'sent'};
&header($text{'mail_title'}, "");
@uinfo = getpwnam($in{'user'});
print "<center><font size=+2>",
      $sentbox ? $text{'mail_sent'} :
      $access{'boxname'} == 0 ?
        &text('mail_for', "<tt>".&user_mail_file($in{'user'})."</tt>") :
      $access{'boxname'} == 1 ?
	&text('mail_for2', $uinfo[6] ? $uinfo[6] : $in{'user'}) : "",
      "</font></center>\n";
print "<hr>\n";

if (!defined($in{'start'}) && $config{'order_mail'}) {
	# View mail from the oldest
	@mail = reverse(&list_mails($in{'user'}, 0, $config{'perpage'}-1));
	$in{'start'} = @mail-$config{'perpage'};
	$in{'start'} = 0 if ($in{'start'} < 0);
	}
else {
	# View mail from the most recent
	@mail = reverse(&list_mails($in{'user'},
			-$in{'start'}, -$in{'start'}-$config{'perpage'}+1));
	}
print "<center>\n";
if ($in{'start'}+$config{'perpage'} < @mail) {
	printf "<a href='list_mail.cgi?user=%s&start=%d'>%s</a>\n",
		$in{'user'}, $in{'start'}+$config{'perpage'},
		'<img src=/images/left.gif border=0 align=middle>';
	}

local $s = @mail-$in{'start'};
local $e = @mail-$in{'start'}-$config{'perpage'}+1;
if (@mail) {
	print "<font size=+1>",&text('mail_pos', $s, $e < 1 ? 1 : $e,
				     scalar(@mail)),"</font>\n";
	}

if ($in{'start'}) {
	printf "<a href='list_mail.cgi?user=%s&start=%d'>%s</a>\n",
		$in{'user'}, $in{'start'}-$config{'perpage'},
		'<img src=/images/right.gif border=0 align=middle>';
	}
print "</center>\n";

print "<form action=delete_mail.cgi>\n";
print "<input type=hidden name=user value='$in{'user'}'>\n";
if ($config{'top_buttons'}) {
	print "<table width=100%> <tr>\n";
	if (@mail) {
		print "<td>\n";
		print "<input type=submit name=delete value=\"$text{'mail_delete'}\">\n";
		print "</td>\n";
		}
	if ($config{'track_read'}) {
		print "<td>\n";
		print "<input type=submit name=mark value=\"$text{'mail_mark'}\">\n";
		print "<select name=mode>\n";
		print "<option value=1 checked>$text{'mail_mark1'}\n";
		print "<option value=0>$text{'mail_mark0'}\n";
		print "<option value=2>$text{'mail_mark2'}\n";
		print "</select></td>\n";
		}
	if (@mail) {
		print "<td><input type=submit name=forward value=\"$text{'mail_forward'}\"></td>\n";
		}
	if (!$sentbox && @mail) {
		print "<td align=right><input type=submit name=new ",
		      "value=\"$text{'mail_compose'}\"></td>\n";
		}
	print "</tr></table>\n";
	if (@mail) {
		print "<a href='' onClick='document.forms[0].d.checked = true; for(i=0; i<document.forms[0].d.length; i++) { document.forms[0].d[i].checked = true; } return false'>$text{'mail_all'}</a>&nbsp;\n";
		print "<a href='' onClick='document.forms[0].d.checked = !document.forms[0].d.checked; for(i=0; i<document.forms[0].d.length; i++) { document.forms[0].d[i].checked = !document.forms[0].d[i].checked; } return false'>$text{'mail_invert'}</a>&nbsp;\n";
		}
	}

if (!@mail) {
	print "<b>$text{'mail_none'}</b><p>\n";
	}
else {
	print "<table border width=100%>\n";
	print "<tr $tb> <td>&nbsp;</td> ",
	      $sentbox ? "<td><b>$text{'mail_to'}</b></td>"
		       : "<td><b>$text{'mail_from'}</b></td> ",
	      $config{'show_to'} ? $sentbox ? "<td><b>$text{'mail_from'}</b></td>"
					    : "<td><b>$text{'mail_to'}</b></td>"
				 : "",
	      "<td><b>$text{'mail_date'}</b></td> ",
	      "<td><b>$text{'mail_size'}</b></td> ",
	      "<td><b>$text{'mail_subject'}</b></td> </tr>\n";
	}

for($i=$in{'start'}; $i<@mail && $i<$in{'start'}+$config{'perpage'}; $i++) {
	local $idx = $mail[$i]->{'idx'};
	print "<tr $cb>\n";
	print "<td><input type=checkbox name=d value=$idx></td>\n";
	print "<td nowrap><a href='view_mail.cgi?user=$in{'user'}&idx=$idx'>",
              &simplify_from($mail[$i]->{'header'}->{$sentbox ? 'to' : 'from'}),
	      "</td>\n";
	if ($config{'show_to'}) {
		print "<td nowrap>",&simplify_from(
		   $mail[$i]->{'header'}->{$sentbox ? 'from' : 'to'}),"</td>\n";
		}
	print "<td nowrap>",&simplify_date($mail[$i]->{'header'}->{'date'}),
	      "</td>\n";
	print "<td nowrap>",int($mail[$i]->{'size'}/1000)+1," kB","</td>\n";
	print "<td><table border=0 cellpadding=0 cellspacing=0 width=100%>",
	      "<tr><td>",&simplify_subject($mail[$i]->{'header'}->{'subject'}),
	      "</td> <td align=right>";
	if ($mail[$i]->{'header'}->{'content-type'} =~ /multipart\/\S+/i) {
		print "<img src=images/attach.gif>";
		}
	local $p = int($mail[$i]->{'header'}->{'x-priority'});
	if ($p == 1) {
		print "&nbsp;<img src=images/p1.gif>";
		}
	elsif ($p == 2) {
		print "&nbsp;<img src=images/p2.gif>";
		}
	if ($read{$mail[$i]->{'header'}->{'message-id'}} == 2) {
		print "&nbsp;<img src=images/special.gif>";
		}
	elsif ($read{$mail[$i]->{'header'}->{'message-id'}} == 1) {
		print "&nbsp;<img src=images/read.gif>";
		}
	print "</td></tr></table></td> </tr>\n";
	}
if (@mail) {
	print "</table>\n";
	print "<a href='' onClick='document.forms[0].d.checked = true; for(i=0; i<document.forms[0].d.length; i++) { document.forms[0].d[i].checked = true; } return false'>$text{'mail_all'}</a>&nbsp;\n";
	print "<a href='' onClick='document.forms[0].d.checked = !document.forms[0].d.checked; for(i=0; i<document.forms[0].d.length; i++) { document.forms[0].d[i].checked = !document.forms[0].d[i].checked; } return false'>$text{'mail_invert'}</a>&nbsp;\n";
	}

print "<table width=100%> <tr>\n";
if (@mail) {
	print "<td>\n";
	print "<input type=submit name=delete value=\"$text{'mail_delete'}\">\n";
	print "</td>\n";
	}
if ($config{'track_read'}) {
	print "<td>\n";
	print "<input type=submit name=mark value=\"$text{'mail_mark'}\">\n";
	print "<select name=mode>\n";
	print "<option value=1 checked>$text{'mail_mark1'}\n";
	print "<option value=0>$text{'mail_mark0'}\n";
	print "<option value=2>$text{'mail_mark2'}\n";
	print "</select></td>\n";
	}
if (@mail) {
	print "<td><input type=submit name=forward value=\"$text{'mail_forward'}\"></td>\n";
	}
if (!$sentbox) {
	print "</form>\n";
	print "<form action=reply_mail.cgi>\n";
	print "<input type=hidden name=user value=\"$in{'user'}\">\n";
	print "<input type=hidden name=new value=1>\n";
	print "<td align=right><input type=submit ",
	      "value=\"$text{'mail_compose'}\"></td>\n";
	}
print "</form> </tr></table>\n";

if (@mail) {
	print "<hr>\n";
	print "<form action=mail_search.cgi>\n";
	print "<input type=hidden name=user value=\"$in{'user'}\">\n";
	print "<b>$text{'mail_search'}</b>\n";
	print "<select name=field>\n";
	foreach $f ('from', 'subject', 'to', 'cc', 'date', 'body', 'size',
	    '!from', '!subject', '!to', '!cc', '!date', '!body', '!size') {
		printf "<option value=%s>%s\n", $f, $text{"match_$f"};
		}
	print "</select>\n";
	print "<input name=match size=20>\n";
	print "&nbsp;<input type=submit value='$text{'mail_ok'}'\n";
	print "</form><p>\n";
	}

print "<hr>\n";
if ($access{'mmode'} == 2 && $access{'musers'} =~ /^\S+$/ ||
    $access{'mmode'} == 4) {
	&footer("", $text{'index_return'});
	}
else {
	&footer("list_boxes.cgi", $text{'boxes_return'},
		"", $text{'index_return'});
	}

