#!/usr/bin/perl
$discus_conf = '/home/cultur/www/discus_admin_24003/discus.conf';
#Discus board new messages script
#-------------------------------------------------------------------------------
# This script is copyright (c) 1998 by DiscusWare, LLC, all rights reserved.
# Its use is subject to the license agreement that can be found at the following
# URL:  http://www.chem.hope.edu/discus/license
#-------------------------------------------------------------------------------
# If you want to change the default selection on the new messages
# top page, you can do so here by commenting/uncommenting lines.
$CHECKED{'last'} = "CHECKED";  # Search last x days/hours
#$CHECKED{'user'} = "CHECKED";  # Search by username/password
#$CHECKED{'since'} = "CHECKED"; # Search since 12:01 am ...
# If you want the message tree view to be enabled by default, you
# can define this variable
$CHECKED{'treeview'} = "CHECKED";
# To enable multiple selection of topics, you can enable one of
# the following two options.  However, this makes the interface
# look not-so-good.
# $multiple =  "MULTIPLE SIZE=1";
# $multiple =  "MULTIPLE";
# If you want the "do not use 7 day limit" enabled, uncomment this:
# $nolimit = 1;
# If you want more characters in the username, change this value:
$max_name_chars = 20;
#------------------------------------------------------------------------------
if (open (FILE, "$discus_conf")) {
	@file = <FILE>;
	close (FILE);
	$evals = "";
	foreach $line (@file) {
		if ($line =~ /^(\w+)=(.*)/) {
			$varname = $1;
			$value = $2;
			$value =~ s/'/\\'/g; $value =~ s/\r//g;
			$evals .= "\$$varname='$value'; ";
		}
	}
	eval($evals);
	require "$admin_dir/source/src-board-subs-common";
} else {
	print "Content-type: text/html\n\n";
	print "<HTML><HEAD><TITLE>Script Execution Error</TITLE></HEAD>\n";
	print "<BODY BGCOLOR=#ffffff TEXT=#000000>\n";
	print "<H1>Script Execution Error</H1>\n";
	print "Discus scripts could not execute because the discus.conf file\n";
	print "could not be opened.";
	print "<P>Reason: <FONT COLOR=#ff0000><B>$!</B></FONT>" if $!;
	print "<P>This generally indicates a setup error of some kind.\n";
	print "Consult the <A HREF=\"http://www.chem.hope.edu/discus/rc\">Discus ";
	print "Resource Center</A> for troubleshooting information.</BODY></HTML>\n";
	exit(0);
}
$version_number = join(".", $release_version, $free_revision) if !$pro;
$version_number = join(".", $release_version, $pro_revision) if $pro;
$version_number .= "(W95)" if ($platform eq "win95" || $win95);
$version_number .= "(NT)" if ($platform eq "NT" || $NT);
$version_number .= "*PRO*" if $pro;
# This tries to keep long lines out of no frames interface
$maxchar = 40;
$maxchar = 25 if $interface eq "noframes";
&parse_form;
&read_cookie;
open (LOG, "$admin_dir/log.txt");
@LOG = <LOG>;
close (LOG);
@LOG = reverse @LOG;
($time) = &ex('get_date_time', "long");
#########################################################
# This is the static page that gets spit out
if ($FORM{'method'} eq "") {
	$option_string = "";
	foreach $logline (@LOG) {
		($what,$who,$when,$where,$anon) = split(/;/, $logline);
		next if $what =~ /^D/;
		last if $what !~ /^D/;
	}
	$count = scalar(@LOG);
	$when = "" if $what =~ /^D/;
	if ($when ne "") {
		($lasttime) = &ex('get_date_time', "long", $when);
	}
	$lasttime = "N/A" if $when eq "";
	$counter = -1;
	$todays_day = (localtime(time))[6];
	for($i = ($todays_day + 5); $i >= ($todays_day); $i -= 1) {
		$counter += 1;
		$option_string .= "<OPTION VALUE=\"$counter\">";
		if ($i <= 6) {
			$ll = $L{NMLASTDAY};
			$wday = $DAY[$i % 7];
			$ll =~ s/\%weekday/$wday/g;
			$option_string .= $ll;
		} else {
			$option_string .= $DAY[$i % 7];
		}
	}
	$option_topic = "";
	open (FILE, "$message_dir/board-topics.html");
	@file = <FILE>;
	close (FILE);
	if ($pro) {
		&ex('get_preferences', 1);
		if ($PREF{'favorites'} ne "") {
			$option_topic .= "<OPTION VALUE=\"$PREF{'favorites'}\" SELECTED>$L{MY_FAVORITES}\n";
		}
		$CHECKED{'treeview'} = "" if $PREF{'nm_view'};
		if ($COOKIE{'user' . $COOKIE_ID} ne "") {
			$CHECKED{'last'} = "";
			$CHECKED{'user'} = "CHECKED";
		}
	}	
	foreach $line (@file) {
		if ($line =~ /<!-Top: (\d+)-!>/) {
			$o = $1;
			&extract("//$1/$1.$ext");
			if (-e "$message_dir/$o") {
				$option_topic .= "<OPTION VALUE=\"$me_number\">$me_name\n";
			} else {
				@auth = &ex('validate_auths', $o);
				$option_topic .= "<OPTION VALUE=\"$me_number\">$me_name\n" if grep(/^$o$/, @auth);
			}				
		}
	}
	&header;
	$str = "$L{NMTITLE}";
	&ex('printuntil',1, 1, 0, "$L{NMTITLE}");
	print "$place\n";
	$ts = $L{NMALLOF};
	$ts =~ s/\%title/$titlej/g;
	$ampmstr = "<SELECT NAME=\"ampm\" onChange=\"document.forms[0].method[2].checked = true\"><OPTION VALUE=\"am\" SELECTED>$L{AM}<OPTION VALUE=\"pm\">$L{PM}</SELECT>" if $L{HRCLOCK} == 12;
	$lps = $L{NMLASTPOST};
	$lps =~ s/\%date/$lasttime/g;
	if ($nolimit == 1) {
		$nolimit = "<INPUT TYPE=CHECKBOX NAME=nolimit VALUE=1";
		$nolimit .= " CHECKED" if $PREF{'show_all_nm'} == 1;
		$nolimit .= ">$L{NM_SHOW_ALL_NM}";
	} else {
		$nolimit = "";
	}
	$ucid = "user$COOKIE_ID";
	$pcid = "rpwd$COOKIE_ID";
	if ($COOKIE{$ucid}) {
		$CHECKED{'last'} = "";
		$CHECKED{'user'} = "CHECKED";
	}
	print <<EOF;
<CENTER><FONT SIZE=+1><B>$L{NMTITLE}</B></FONT></CENTER>
<HR>
<FORM ACTION="$script_url/board-newmessages.$cgi_extension" METHOD=POST>
$L{NMSEARCHIN}&nbsp;<SELECT NAME="where" $multiple>
<OPTION VALUE="all">$ts
$option_topic
</SELECT> &nbsp;<INPUT TYPE=SUBMIT VALUE="$L{NMSEARCHBUTTON}">
<HR>
<BR>
<INPUT NAME="method" TYPE=radio VALUE="last" $CHECKED{'last'}>
$L{NMWITHINLAST}&nbsp;
<INPUT NAME="number" VALUE="1" SIZE=3 onChange="document.forms[0].method[0].checked = true">
<SELECT NAME="units" onChange="document.forms[0].method[0].checked = true">
<OPTION VALUE="1">$L{NMMINUTES}
<OPTION VALUE="60">$L{NMHOURS}
<OPTION VALUE="1440" SELECTED>$L{NMDAYS}
</SELECT>
<P>
<INPUT NAME="method" TYPE=radio VALUE="check" onClick=\"document.forms[0].username.focus()\" $CHECKED{'user'}>
$L{NMLASTCHECK}<BR>
<UL><TABLE><TR><TD><FONT FACE="$face" SIZE="$size">$L{PROFILE_USERNAME}</FONT></TD><TD><INPUT TYPE=TEXT NAME=username  VALUE="$COOKIE{$ucid}" SIZE=9 onChange=\"document.forms[0].method[1].checked = true\"></TD><TD><FONT FACE="$face" SIZE="$size">&nbsp;&nbsp;$nolimit</FONT></TD></TR>
<TR><TD><FONT FACE="$face" SIZE="$size">$L{PROFILE_PASSWORD}</FONT></TD><TD><INPUT TYPE=PASSWORD NAME=password SIZE=9 VALUE="$COOKIE{$pcid}"></TD><TD><FONT FACE="$face" SIZE="$size">&nbsp;&nbsp;<INPUT TYPE=CHECKBOX NAME=silent VALUE=on onClick="document.forms[0].method[1].checked = true">$L{NMDONTREMEMBER}</FONT></TD></TR></TABLE>
</UL>
<P>
<INPUT NAME="method" TYPE=radio VALUE="since" $CHECKED{'since'}>
$L{NMAFTER}&nbsp;
<INPUT NAME="time" TYPE=text VALUE="$L{NMDEFAULTTIME}" SIZE=5 onChange="document.forms[0].method[2].checked = true">
$ampmstr
<SELECT NAME="day" onChange="document.forms[0].method[2].checked = true">
<OPTION VALUE="-2">$L{NMTODAY}
<OPTION VALUE="-1">$L{NMYESTERDAY}
$option_string
</SELECT>
<P>
<INPUT TYPE=CHECKBOX NAME=tree VALUE=1 $CHECKED{'treeview'}>
$L{NMTREEVIEW}
<BR>
<HR>
$lps
<!-- Discus $version_number %$count% !$when!-->
</FORM>
EOF
	&ex('printuntil', 3, 17, 0, "", 0, 1);
	exit(0);
}
$cutoff = 0;
$timestore = time;
if ($FORM{'method'} eq 'redo') {
	$cutoff = $FORM{'cutoff'};
} elsif ($FORM{'method'} eq 'last') {
	$number = $FORM{'number'};
	$factor = $FORM{'units'};
	&error_message("Invalid Number", "The number you specified is invalid!") if $number <= 0;
	&error_message("Invalid Units", "What are you trying to do?") if $factor <= 0;
	$cutoff = $timestore;
	$cutoff -= $number * $factor * 60;
} elsif ($FORM{'method'} eq 'since') {
	($sec, $min, $hour, $day, $nmonth, $year, $wday, $yday, $isdst) = localtime($timestore);
	$midnighttoday = $timestore - $sec - (60 * $min) - (3600 * $hour);
	$day = $FORM{'day'} + 2;
	$cutoff = $midnighttoday - ($day * 86400);
	$cutoff += 43200 if $FORM{'ampm'} eq "pm";
	$timearg = $FORM{'time'};
	$timesep = $L{TIMESEPARATOR};
	$timearg =~ s/[^\d$timesep]//g;
	($hour, $min) = split(/$timesep/, $timearg);
	$hour =- 12 if ($L{HRCLOCK} == 12 && $FORM{'ampm'} eq "pm");
	$cutoff += ($min * 60) + ($hour * 3600);
} elsif ($FORM{'method'} eq 'check') {
	($result) = &ex('verify_user_password', $FORM{'username'}, $FORM{'password'});
	@result = split(/\n/, $result);
	@result_save = @result;
	&error_message($L{BPAUTHERROR}, $L{PROFILE_AUTHERROR_DESCR},0,1) if scalar(@result) == 0;
	if ($pro) {
		&ex('get_preferences', $FORM{'username'}, $FORM{'password'});
		$FORM{'tree'} = "" if $PREF{'nm_view'} == 1;
	}
	$lastcheck = 0;
	foreach $result (@result_save) {
		($file, $user, $pass, $foo, $foo, $foo, $foo, $lc, $therest) = split(/:/, $result);
		$lastcheck = $lc if $lc > $lastcheck;
		$password_save = $pass;
	}
	if ($lastcheck == 0) {
		$docut = "$L{NMNOCHECK}\n";
		$cutoff = $timestore;
		$cutoff -= 1440 * 60 * 7;
	} else {
		$cutoff = $lastcheck;
		if ($timestore > ($cutoff + 604800) && $PREF{'show_all_nm'} == 0 && !$FORM{'nolimit'}) {
			$docut .= "$L{NMCHECKSEVENDAYS}<P>\n";
			$cutoff = $timestore - 604800;
		} else {
			$docut .= "$L{NMYOULASTCHECKED} ";
			($dt) = &ex('get_date_time', 'long', $cutoff);
			$docut .= $dt;
			$docut .= ".<BR>\n";
		}
	}
	if (!$FORM{'silent'}) {
		$ctime = time;
		&ex('save_profile_information', "", "", "", "", "", $ctime, "", @result_save);
	} else {
		$docut .= "$L{NMNOTRECORDED}\n";
	}
}
if ($pro) {
	&ex('get_preferences', 1);
	if ($FORM{'tree'} eq "ON") {
		$FORM{'tree'} = "" if $PREF{'nm_view'} == 1;
		$FORM{'where'} = $PREF{'favorites'} if ($PREF{'last_favs'} == 1 && $PREF{'favorites'} =~ /\S/); 
	}
}
$| = 1;
undef @returned_list;
undef %file;
$counter = 0;
undef %seen;
@LOG = &ex('log_authorization', @LOG) if $pro;
&header;
foreach $line (@LOG) {
	next if $line !~ /^(\d+);([^;]*);/; # Deleted and Old style
	chop ($line) if $line =~ /\n$/;
	($what,$who,$when,$where,$how) = split(/;/, $line);
	last if $when == 0;
	last if $when < $cutoff;
	$flag = 0;
	($thistopic, $num) = split(/\//, $where);
	if ($FORM{'where'} ne "all") {
		$flag = 1 if !grep(/^$thistopic$/, split(/,/, $FORM{'where'}));
	}
	push(@returned_list, $line) if $flag == 0;
	$file{$where} .= "$line," if $flag == 0;
	$flag = 1 if $seen{$where} == 1;
	$seen{$where} = 1 if $flag == 0;
	$counter += 1 if $flag == 0;
}
#########################################################
# Build up data for the output page
undef %subdata;
undef %headstr;
undef %msgdata;
undef %topic;
undef @topic_order;
undef @result;
undef @msgid;
foreach $line (@returned_list) {
	chop $line if $line =~ m|\n$|;
	($postindex, $who, $time, $where, $addr, $host, $message2, $who2) = split(/;/, $line);
	if ($headstr{$where} eq "") {
		($topic, $page) = split(/\//, $where);
		($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src) = &get_page($topic, $page);
		push (@result, $where) if $head ne "";
		$headstr{$where} = $head;
		$msgdata{$where} = $message;
		$subdata{$where} = $sublist;
		$lm{$where} = $lm;
		@head = split(/\n/, $head);
		foreach $l (@head) {
			if ($l =~ m|<!--Topic: (\d+)/|) {
				$headdata{$where} = "$1,";
			} elsif ($l =~ m|<!--Level (\d+): (\d+)/|) {
				$headdata{$where} .= "$2,";
			}
		}
	}
	push (@msgid, $postindex);
	$msgplace{$postindex} = $where;
	$msgtext{$postindex} = &unescape($message2);
	$postedby{$postindex} = &unescape($who2); $postedby{$postindex} =~ s/\s+$//;
	$postedwhen{$postindex} = $time;
}
open (TOP, "$html_dir/messages/board-topics.html");
@top = <TOP>;
close (TOP);
foreach $line (@top) {
	push (@topic_order, $1) if $line =~ m|<!-Top: (\d+)-!>|;
}
@results = sort findorder (@result); #Sort relevant subtopics
sub findorder {
	($first, $second) = ($a, $b);
	return 0 if $first eq $second;
	($t1, $p1) = split(/\//, $first);
	($t2, $p2) = split(/\//, $second);
	if ($t1 != $t2) {
		foreach $topic (@topic_order) {
			return -1 if $topic == $t1;
			return 1 if $topic == $t2;
		}
	}
	@hd1 = split(/,/, $headdata{$first}); @hd1 = grep(/\S/, @hd1);
	@hd2 = split(/,/, $headdata{$second}); @hd2 = grep(/\S/, @hd2);
	return 1 if grep(/^$p1$/, @hd2); #2nd parent of 1st
	return -1 if grep(/^$p2$/, @hd1); #1st parent of 2nd
	$cpp = 0;
	foreach $l (@hd1) {
		last if !grep(/^$l$/, @hd2);
		$cpp = $l;
	}
	for ($i = 0; $i <= scalar(@hd1); $i++) {
		if ($hd1[$i] == $cpp) {
			$pp1 = $hd1[$i+1];
			$pp2 = $hd2[$i+1];
			last;
		}
	}
	$where = "$t1/$cpp";
	if ($subdata{$where} eq "") {
		($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src) = &get_page($t1, $cpp);
		$headstr{$where} = $head;
		$msgdata{$where} = $message;
		$subdata{$where} = $sublist;
		$lm{$where} = $lm;
		@head = split(/\n/, $head);
		foreach $l (@head) {
			if ($l =~ m|<!--Topic: (\d+)/|) {
				$headdata{$where} = "$1,";
			} elsif ($l =~ m|<!--Level (\d+): (\d+)/|) {
				$headdata{$where} .= "$2,";
			}
		}
	} else {
		$sublist = $subdata{$where};
	}
	foreach $line2 (split(/\n/, $sublist)) {
		if ($line2 =~ m|<!-Top: (\d+)-!>|) {
			return 1 if $1 == $pp2;
			return -1 if $1 == $pp1;
		}
	}
	return 0;
}
#########################################################
# Print the output page
if ($FORM{'redisplay'} ne "") {
	$FORM{'tree'} = "" if $FORM{'redisplay'} !~ /Tree/;
	$FORM{'tree'} = "ON" if $FORM{'redisplay'} =~ /Tree/;
}
&ex('printuntil', 1, 1, 0, "$L{NMRESULTSOFSEARCH}");
print "<FONT SIZE=+1><CENTER><B>$L{NMRESULTSOFSEARCH}</B></CENTER></FONT>\n<HR>\n";
$numnew = scalar(@results);
print "$docut\n";
if ($numnew == 0) {
	print "$L{NMZERONEW}<HR>\n";
	&ex('printuntil', 3, 17, 0, "", 0, 1);
	exit(0);
} elsif ($numnew == 1) {
	print "$L{NMONENEW}<HR>\n";
} else {
	$l = $L{NMPLURALNEW};
	$l =~ s/\%d/$numnew/g;
	print "$l<HR>\n";
}
&ex('printuntil', 3, 5, 0, "", 0, 1);
if ($FORM{'tree'} ne "") {
	$browser = $ENV{'HTTP_USER_AGENT'};
	if ($browser !~ /MSIE/) {
		if ($browser =~ m|Mozilla/(\d+)|) {
			$nsbug = 1 if ($1 < 4 && $interface eq "noframes");
		}
	}
	$ie3 = 0;
	$ie3 = 1  if $ENV{'HTTP_USER_AGENT'} =~ m|MSIE 3\.|;
	$nsbug = 1 if $ie3;
	undef %seen;
	if (!$nsbug) {
		print "<SCRIPT LANGUAGE=\"JavaScript\">\n";
		print "<!-- Hide from older browsers\n";
		print "var i0 = '<IMG SRC=\"';\n";
		print "var i1 = '$html_url/icons/tree_';\n";
		print "var i2 = '.gif\" HEIGHT=16 WIDTH=20 ALIGN=TOP>';\n";
		print "function crArray(leng) {\n";
		print "  this.size = leng;\n";
		print "  for(var i = 1; i <= leng; i++) {\n";
		print "    this[i] = 0;\n";
		print "  }\n";
		print "}\n";
		print "function p(ic, tl, ln, d) {\n";
		print "    var pr = '';\n";
		print "    for (var j = 0; j < ic.length; j++) {\n";
		print "      m = ic.toString();\n";
		print "      k = m.substring(j, 1+j);\n";
		print "      pr = pr + i0 + i1 + k + i2;\n";
		print "    }\n";
		print "    if (ln != '') {\n";
		print "      pr = pr + '<A ";
		print "TARGET=_blank " if $PREF{'nm_newwindow'};		
		print "HREF=\"' + ln + '\">';\n";
		print "    }\n";
		print "    pr = pr + tl;\n";
		print "    if (ln != '') {\n";
		print "      pr = pr + '</A>';\n";
		print "    }\n";
		print "    pr = pr + d;\n";
		print "    pr = pr + '<BR>';\n";
		print "    document.write(pr);\n";
		print "}\n";
	}
	undef %seen; undef %seen2; undef %pr;
	foreach $result (@results) {
		@hd = split(/,/, $headdata{$result});
		$result =~ m|^(\d+)/|; $topic = $1;
		foreach $hd (@hd) {
			$res = "$topic/$hd";
			if ($headstr{$res} eq "") {
				($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src) = &get_page($topic, $hd);
				$headstr{$res} = $head;
				$msgdata{$res} = $message;
				$subdata{$res} = $sublist;
				$lm{$res} = $lm;
			}
			$seen{"$topic/$hd"} = 1;
		}
		$seen{$result} = 1;
	}
	foreach $res (keys(%seen)) {
		$sublist = $subdata{$res};
		$head = $headstr{$res};
		@head = split(/\n/, $head);
		($hs) = grep(/^<!--Me: /, @head);
		$hs =~ m|<!--Me: (\d+)/(.*)-->|;
		$k = $2; $subname{$res} = &JavaScript_prepare($k, 1);
		@subl = split(/\n/, $sublist);
		$res =~ m|^(\d+)/|; $topic = $1;
		foreach $s (@subl) {
			if ($s =~ m|^<!-Top: (\d+)-!>|) {
				$lastnum{$res} = $1 if $seen{"$topic/$1"};
			}
		}
	}
	$last = $results[scalar(@results)-1];
	$last =~ m|^(\d+)/|; $LAST_TOPIC = $1;
	foreach $res (@results) {
		$res =~ m|^(\d+)/|; $topic = $1;
		@hs = split(/,/, $headdata{$res});
		$c = 0; $t = $topic;
		$parent = "";
		foreach $l (@hs) {
			$c += 1;
			$res2 = "$topic/$l";
			if (!$seen2{$res2}) {
				print "p('" if !$nsbug;
				$thisstr = "";
				if ($c == 1) {
					if (!$nsbug) {
						print "$thisstr", "g','<B>$subname{$res2}</B>',";
						if (-e "$message_dir/$res2.$ext") {
							print "'$message_url/$res2.$ext";
							print "?$lm{$res2}" if !$noqm;
						} else {
							print "'$script_url/board-auth.$cgi_extension?file=/$res2.$ext&lm=$lm{$res2}";
						}
						print "','');\n";
					} else {
						$lmstr = "?$lm{$res2}" if !$noqm;
						print "<IMG SRC=$html_url/icons/tree_g.gif HEIGHT=16 WIDTH=20 ALIGN=TOP>";
						print "<A HREF=$html_url/messages/$res2.$ext$lmstr" if -e "$message_dir/$res2.$ext";
						print "<A HREF=$script_url/board-auth.$cgi_extension?file=/$res2.$ext&lm=$lmstr" if !-e "$message_dir/$res2.$ext";
						print " TARGET=_blank" if $PREF{'nm_newwindow'};
						print "><B>$subname{$res2}</B></A><BR>";
					}
					$seen2{$res2} = 1;
				} else {
					for ($i = 1; $i < ($c-1); $i++) {
						$thisstr .= "i" if $on{$i};
						$thisstr .= "j" if !$on{$i};
						if ($nsbug) {
							print "<IMG SRC=$html_url/icons/tree_i.gif HEIGHT=16 WIDTH=20 ALIGN=TOP>" if $on{$i};
							print "<IMG SRC=$html_url/icons/tree_j.gif HEIGHT=16 WIDTH=20 ALIGN=TOP>" if $on{$j};
						}							
					}
					if ($l != $lastnum{$parent}) {
						$thisstr .= "e";
						if ($nsbug) {
							print "<IMG SRC=$html_url/icons/tree_e.gif HEIGHT=16 WIDTH=20 ALIGN=TOP>" if $on{$j};
						}
						$on{$i} = 1;
					} else {
						$thisstr .= "f";
						if ($nsbug) {
							print "<IMG SRC=$html_url/icons/tree_f.gif HEIGHT=16 WIDTH=20 ALIGN=TOP>" if $on{$j};
						}
						$on{$i} = 0;
					}
					if (!$nsbug) {
						print "$thisstr", "g','<B>$subname{$res2}</B>',";
						if (-e "$message_dir/$res2.$ext") {
							print "'$message_url/$res2.$ext";
							print "?$lm{$res2}" if !$noqm;
						} else {
							print "'$script_url/board-auth.$cgi_extension?file=/$res2.$ext&lm=$lm{$res2}";
						}
						print "', '');\n";
					} else {
						while ($thisstr =~ m|(\w)|g) {
							print "<IMG SRC=$html_url/icons/tree_$1.gif HEIGHT=16 WIDTH=20 ALIGN=TOP>";
						}		
						print "<IMG SRC=$html_url/icons/tree_g.gif HEIGHT=16 WIDTH=20 ALIGN=TOP>";
						$lmstr = "?$lm{$res2}" if !$noqm;
						print "<A HREF=$html_url/messages/$res2.$ext$lmstr" if -e "$message_dir/$res2.$ext";
						print "<A HREF=$script_url/board-auth.$cgi_extension?file=/$res2.$ext&lm=$lmstr" if !-e "$message_dir/$res2.$ext";
						print " TARGET=_blank" if $PREF{'nm_newwindow'};
						print "><B>$subname{$res2}</B></A><BR>";
					}
					$seen2{$res2} = 1;
				}
			}
			$thisstr =~ s/e$/i/;
			$thisstr =~ s/f$/j/;
			$parent = $res2;
			if ($pr{$res2} == 0) {
				$pr{$res2} = 1;
				if (grep(/^$res2$/, values(%msgplace))) {
					$msg = $msgdata{$res2};
					if ($subdata{$res2} =~ m|<!-Top: (\d+)-!>|) {
						while ($subdata{$res2} =~ m|<!-Top: (\d+)-!>|g) {
							$o = $1;
							$has_subtopics{$res2} = 1 if $seen{"$topic/$o"};
						}
					}
					@msg = split(/\n/, $msg);
					undef @li1; undef @li2; undef @li3; undef @li4; undef @li5; $flag = 0;
					foreach $line (@msg) {
						if ($line =~ m|<!-Post: (\d+)-!>|) {
							$pn = $1;
							if (grep(/^$pn$/, @msgid)) {
								push (@li1, $thisstr . "el");
								push (@li4, $pn);
								push (@li5, $postedwhen{$pn});
								if ($postedby{$pn} ne "") {
									$postedby{$pn} =~ s/\([^\)]*\)//g;
									$postedby{$pn} =~ s/\s+$//;
									$postedby{$pn} = &remove_html($postedby{$pn}, 1);
									$msgtext{$pn} = &remove_html($msgtext{$pn}, 1);
									$msgtext{$pn} = substr($msgtext{$pn}, 0, $maxchar);
									push (@li2, &JavaScript_prepare($postedby{$pn}, 1));
									push (@li3, &JavaScript_prepare($msgtext{$pn}, 1));
								} else {
									$flag = 1;
								}
							} else {
								$flag = 0;
							}
						} elsif ($flag > 0) {
							if ($line =~ m|<!-/Post:|) {
								$flag = 0;
								$p = &JavaScript_prepare($p, 1); $p .= " ";
								$p =~ m|^(.{0,40})\s|;
								$tempvar = $1; $tempvar = substr($tempvar, 0, $maxchar);
								push (@li3, $tempvar);
							} elsif ($line =~ m|<FONT SIZE="-1">By <B>(.*)</B> on(.*)</FONT><P>|) {
								$p = $1; $p =~ s/\([^\)]*\)//g; $p = &JavaScript_prepare($p, 1);
								$p =~ s/\s+$//;
								push (@li2, $p);
								$p = "";
								$flag = 2;
							} elsif ($flag == 2) {
								$p .= $line;
							}
						}
					}
					$p = scalar(@li1);
					if ($p > 0) {
						$li1[$p-1] =~ s/el$/fl/ if !$has_subtopics{$res2};
					}
					for ($i = 0; $i < $p; $i++) {
						if (length($li2[$i]) > $max_name_chars) {
							$l = $li2[$i];
							while ($l =~ m|&#(\d+);|) {
								$l = join("", $`, pack("c", $1), $');
							}
							$l = substr($l, 0, $max_name_chars);
							$l = &JavaScript_prepare($l, 1);
							$li2[$i] = $l;
						}
						if (!$nsbug) {
							print "p('", $li1[$i], "','<FONT SIZE=-1>", $li2[$i], "</FONT>',";
							if (-e "$message_dir/$res2.$ext") {
								print "'$message_url/$res2.$ext";
								print "?$lm{$res2}" if !$noqm;
							} else {
								print "'$script_url/board-auth.$cgi_extension?file=/$res2.$ext&lm=$lm{$res2}";
							}
							print "#POST", $li4[$i], "','<FONT SIZE=-2>-";
							$pt = $li5[$i];
							($dt) = &ex('get_date_time', 'realshort', $pt);
							print "$dt-";
							print $li3[$i], "</FONT>');\n";
						} else {
							while ($li1[$i] =~ m|(\w)|g) {
								print "<IMG SRC=$html_url/icons/tree_$1.gif HEIGHT=16 WIDTH=20 ALIGN=TOP>";
							}
							print "<A HREF=$html_url/messages/$res2.$ext#POST$li4[$i] ";
							print "<A HREF=$script_url/board-auth.$cgi_extension?file=/$res2.$ext&lm=$lmstr#POST$li4[$i]" if !-e "$message_dir/$res2.$ext";
							print " TARGET=_blank" if $PREF{'nm_newwindow'} ;
							print ">" if -e "$message_dir/$res2.$ext";
							print "<FONT SIZE=-1>$li2[$i]</FONT></A>";
							print "<FONT SIZE=-2>-";
							$pt = $li5[$i];
							($dt) = &ex('get_date_time', 'realshort', $pt);
							print "$dt-";
							print "$li3[$i]</FONT><BR>";
						}
					}
				}
			}
		}
	}
	if (!$nsbug) {
		print "// -->\n";
		print "</SCRIPT>\n";
	}
} else {
	foreach $where (@results) {
		($topic, $page) = split(/\//, $where);
		@head = split(/\n/, $headstr{$where});
		($topicstr) = grep(/<!--Topic: $topic/, @head);
		$topicstr =~ m|<!--Topic: $topic/(.*)-->|;
		$navbar = $1;
		foreach $line (@head) {
			if ($line =~ m|<!--Level \d+: \d+/(.*)-->|) {
				$navbar .= ": $1";
			}
		}
		print "<A HREF=\"$message_url/$where.$ext" if -e "$message_dir/$where.$ext";
		print "<A HREF=\"$script_url/board-auth.$cgi_extension?file=/$where.$ext&lm=$lm{$where}" if !-e "$message_dir/$where.$ext";
		print "?$lm{$where}" if (!$noqm && 	-e "$message_dir/$where.$ext");
		print " TARGET=_blank" if $PREF{'nm_newwindow'};
		print "\"><B>";
		print $navbar;
		print "</B></A>\n";
		@message = split(/\n/, $msgdata{$where});
		$flag = 0;
		print "<BLOCKQUOTE>\n";
		foreach $line (@message) {
			if ($line =~ m|<!-Post: (\d+)-!>|) {
				$post = $1; $pn = $post;
				($dateprint) = &ex('get_date_time', 'realshort', $postedwhen{$pn});
				$flag = 1 if (grep(/^$post$/, @msgid));
				if ($postedby{$pn} ne "") {
					$postedby{$pn} =~ s/\([^\)]*\)//g;
					$postedby{$pn} =~ s/\s+$//;
					$by = &JavaScript_prepare($postedby{$pn}, 1);
					$text = &JavaScript_prepare($msgtext{$pn}, 1);
					$flag = 2 if $flag == 1;
				}
			} elsif ($line =~ m|<!-/Post: (\d+)-!>|) {
				$pn = $1;
				$l = $by;
				while ($l =~ m|&#(\d+);|) {
					$l = join("", $`, pack("c", $1), $');
				}
				$l = substr($l, 0, $max_name_chars);
				$l = &JavaScript_prepare($l, 1);
				$by = $l;
				if ($flag > 0) {
					print "<A HREF=\"$message_url/$where.$ext" if -e "$message_dir/$where.$ext";
					print "<A HREF=\"$script_url/board-auth.$cgi_extension?file=/$where.$ext&lm=$lm{$where}" if !-e "$message_dir/$where.$ext";
					print "?$lm{$where}" if (!$noqm && 	-e "$message_dir/$where.$ext");
					print "#POST$pn\"";
					print " TARGET=_blank" if $PREF{'nm_newwindow'};
					print ">$by</A>: $dateprint: $text<BR>\n";
					$flag = 0;
				}
			} elsif ($flag == 1 && $line =~ m|By <B>(.*)( [\(]).*</B>.*<B>(.*)</B>:</FONT><P>|i) {
				($postby, $foo, $date) = ($1, $2, $3);
				$by = &remove_html($postby, 1);
			} elsif ($flag == 1 && $line =~ m|<B>(.*)</B>.*<B>(.*)</B>:</FONT><P>|i) {
				($postby, $date) = ($1, $2);
				$by = &remove_html($postby, 1);
			} elsif ($line =~ m|^<HR>|) {
				next;
			} elsif ($flag) {
				$line2 = &remove_html($line, 1);
				$line2 .= " ";
				$line2 =~ m|(.{0,40})\s|;
				$tempvar = $1; $tempvar = substr($tempvar, 0, $maxchar);
				$text = $tempvar;
				$flag = 3;
			}
		}
		print "</BLOCKQUOTE><P>\n";
	}
}
print "<HR>\n";
print "<FORM ACTION=\"$script_url/board-newmessages.$cgi_extension\" METHOD=POST>\n";
print "<INPUT TYPE=HIDDEN NAME=cutoff VALUE=\"$cutoff\">\n";
print "<INPUT TYPE=HIDDEN NAME=method VALUE=\"redo\">\n";
print "<INPUT TYPE=HIDDEN NAME=where VALUE=\"$FORM{'where'}\">\n";
print "<INPUT TYPE=HIDDEN NAME=tree VALUE=1>\n" if $FORM{'tree'} eq "";
print "<INPUT TYPE=SUBMIT VALUE=\"$L{NMOLDVIEWBUTTON}\">" if $FORM{'tree'} ne "";
print "<INPUT TYPE=SUBMIT VALUE=\"$L{NMTREEVIEWBUTTON}\">" if $FORM{'tree'} eq "";
print "</FORM>\n";
&ex('printuntil', 7, 17, 0, "", 0, 1);
exit(0);
#########################################################
# Miscellaneous Subroutines
sub check_password {
	local ($username, $guess) = @_;
	local ($user, $ok, @passwd_line, $line, $pass, $test_pass, @group, $groupline);
	local ($field1, $field2, @valid, @result);
	undef (@result);
	undef (@users);
	open (PASSWD, "$admin_dir/users.txt") || &error_message("Verify Password Error", "Cannot open password file (users.txt) for reading!");
	@users = <PASSWD>;
	close (PASSWD);
	@valid = @users;
	open(PASSWD,"$admin_dir/passwd.txt") || &error_message("Verify Password Error", "Cannot open password file (passwd.txt) for reading!");
	@passwd_line = <PASSWD>;
	close(PASSWD);
	$ok = "";
	foreach $line (@passwd_line) {
		($user, $pass) = split(/:/, $line, 3);
		if ($user eq $username) {
			$test_pass = crypt($guess, $pass);
			$ok = "";
			$ok = "Yes" if $test_pass eq $pass;
			$ok = "Yes" if $COOKIE{'cpwd' . $COOKIE_ID} eq crypt($pass, "cookie");
			$ok = "No" if $guess eq "";
			$ok = "No" if $pass eq "";
			push (@result, "passwd:$line") if $ok eq "Yes";
		}
	}
	open(PASSWD,"$admin_dir/users.txt") || &error_message("Verify Password Error", "Cannot open password file (users.txt) for reading!");
	@passwd_line = <PASSWD>;
	close(PASSWD);
	$ok = "";
	foreach $line (@passwd_line) {
		chop ($line) if $line =~ /\n/;
		($user, $pass, $foo, $foo, $foo, $foo, $foo, $group_test) = split(/:/, $line);
		if ($user eq $username) {
			$test_pass = crypt($guess, $pass);
			$ok = "";
			$ok = "Yes" if $test_pass eq $pass;
			$ok = "No" if $guess eq "";
			$ok = "Yes" if $COOKIE{'cpwd' . $COOKIE_ID} eq crypt($pass, "cookie");
			$ok = "No" if $pass eq "";
			push (@result, "users:$line") if $ok eq "Yes";
		}
	}
	return @result;
}
