#! @PERL@ -w

# List the messages in a user's log directory

# Copyright (c) 2005 by Rhyolite Software
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND RHYOLITE SOFTWARE DISCLAIMS ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RHYOLITE SOFTWARE
# BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
# OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
# ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
# SOFTWARE.
#	Rhyolite Software DCC 1.2.74-1.6 $Revision$
#	@configure_input@

# This file must protected with an equivalent to httpd.conf lines
#   in the README file.


use strict 'subs';

my($page_size) = 60;

my($total_msgs);

# get DCC parameters
local($list_msg_link, $user_dir);		# quiet -w warnings
do('@cgibin@/common') || die("could not get DCC configuration: $!\n");

get_query();

local(%msgs_date, %msgs_from, %msgs_subject, $msg_first, $msg_last, @msgs_num);
get_log_msgs($query{msg} ? $query{msg} : "", $page_size);

html_head("Messages Logged for $user at $hostname");

$total_msgs = @msgs_num;
if ($total_msgs) {
    print "<H3>$total_msgs messages logged for <EM>$user</EM> at $hostname</H3>\n<P>\n";
} else {
    print "<H3>No messages logged for <EM>$user</EM> at $hostname</H3>\n<P>\n";
}
print "<P class=warn><B>\n";
print $query{result} ? html_str_encode($query{result}) : "&nbsp;";
print "</B>\n<P>\n";
common_buttons();
print "</TABLE>\n";

if ($total_msgs) {
    my($msg_num, $msg, $next_date, $last_date);

    print <<EOF;
<P>
<TABLE   border=1 cellpadding="2%"
    summary="messages logged for $user at $hostname">
EOF

    print_links();

    for ($msg_num = $msg_first; $msg_num <= $msg_last; ++$msg_num) {
	my($msg) = $msgs_num[$msg_num];

	next if (!$msg);
	$next_date = $msgs_date{$msg};
	if ($next_date =~ s/(.*) .*/$1/) {
	    if (! $last_date) {
		print "<TR><TH>Message<TH>$next_date<TH>Author<TH>Subject\n";
	    } elsif ($last_date ne $next_date) {
		print "<TR><TH>&nbsp;<TH>$next_date<TH>&nbsp;<TH>&nbsp;\n";
	    }
	    $last_date = $next_date;
	}

	print <<EOF;
<TR><TD class=mono>$list_msg_link?msg=$msg">$msg</A>
    <TD class=mono>$msgs_date{$msg}
    <TD>$msgs_from{$msg}
    <TD>$msgs_subject{$msg}
EOF
    }
    print_links();
    print "</TABLE>\n";
}

print <<EOF;
</BODY>
</HTML>
EOF


# re-enable mail notifications
unlink("$user_dir/notify.marker", "$user_dir/notify.pending",
       "$user_dir/notify.block");
close(MARK)
    if (open(MARK, "> $user_dir/notify.marker"));



sub print_links {
    my($msg_num);

    print "<TR><TD colspan=4>\n";
    if ($msg_first > 0) {
	$msg_num = $msg_first - $page_size;
	$msg_num = 0 if ($msg_num < 0);
	print "    $list_log_link?msg=$msgs_num[0]\">Newest</A>\n";
	print "    $list_log_link?msg=$msgs_num[$msg_num]\">Newer</A>\n";
    } else {
	print "    Newest\n";
	print "    Newer\n";
    }

    if ($msg_last < $total_msgs-1) {
	print "    $list_log_link?msg=$msgs_num[$msg_last+1]\">Older</A>\n";
	$msg_num = $total_msgs - $page_size;
	$msg_num = $msg_last if ($msg_num < $msg_last);
	print "    $list_log_link?msg=$msgs_num[$msg_num]\">Oldest</A>\n";
    } else {
	print "    Older\n";
	print "    Oldest\n";
    }
}
