#!/usr/local/bin/perl
# mail_search.cgi
# Find mail messages matching some pattern

require './qmail-lib.pl';
require './boxes-lib.pl';
&ReadParse();
$in{'match'} || &error($text{'search_ematch'});

$umf = &user_mail_dir($in{'user'});
&header($text{'search_title'}, "");
print "<center><font size=+2>",&text('mail_for', "<tt>$umf</tt>"),
      "</font></center>\n";
print "<hr>\n";

@rv = $config{'mail_system'} == 1 ?
	&search_maildir($umf, $in{'field'}, $in{'match'}) :
	&search_mail($umf, $in{'field'}, $in{'match'});
print "<p><b>",&text($in{'field'} =~ /^\!/ ? 'search_results3' :
          'search_results2', scalar(@rv), "<tt>$in{'match'}</tt>"),"</b><p>\n";
if (@rv) {
	print "<form action=delete_mail.cgi>\n";
	print "<input type=hidden name=user value='$in{'user'}'>\n";
	if ($config{'top_buttons'}) {
		print "<input type=submit name=delete ",
		      "value=\"$text{'mail_delete'}\">&nbsp;&nbsp;\n";
		print "<input type=submit name=forward ",
		      "value=\"$text{'mail_forward'}\"><p>\n";
		print "<a href='' onClick='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='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;<br>\n";
		}
	print "<table border width=100%>\n";
	print "<tr $tb> <td>&nbsp;</td> ",
	      "<td><b>$text{'mail_from'}</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";
	}
foreach $m (@rv) {
	local $idx = $m->{'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($m->{'header'}->{'from'}),"</td>\n";
	print "<td nowrap>",&simplify_date($m->{'header'}->{'date'}),"</td>\n";
	print "<td nowrap>",int($m->{'size'}/1000)+1," kB","</td>\n";
	print "<td><table border=0 cellpadding=0 cellspacing=0 width=100%>",
	      "<tr><td>",&simplify_subject($m->{'header'}->{'subject'}),
	      "</td> <td align=right>";
	if ($m->{'header'}->{'content-type'} =~ /multipart\/\S+/i) {
		print "<img src=images/attach.gif>";
		}
	local $p = int($m->{'header'}->{'x-priority'});
	if ($p == 1) {
		print "&nbsp;<img src=images/p1.gif>";
		}
	elsif ($p == 2) {
		print "&nbsp;<img src=images/p2.gif>";
		}
	print "</td></tr></table></td> </tr>\n";
	}
if (@rv) {
	print "</table>\n";
	print "<a href='' onClick='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='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;<p>\n";
	print "<input type=submit name=delete ",
	      "value=\"$text{'mail_delete'}\">&nbsp;&nbsp;\n";
	print "<input type=submit name=forward value=\"$text{'mail_forward'}\">\n";
	print "</form><p>\n";
	}
else {
	print "<b>$text{'search_none'}</b> <p>\n";
	}

print "<hr>\n";
&footer("list_mail.cgi?user=$in{'user'}", $text{'mail_return'},
	"list_boxes.cgi", $text{'boxes_return'});

