# FILE: src-board-subs-13
# Message management
#-------------------------------------------------------------------------------
# DISCUS VERSION 3.10 COPYRIGHT NOTICE
#
# Discus 3.10 is copyright (c) 2000 by DiscusWare, LLC, all rights reserved.
# The use of Discus is governed by the Discus License Agreement which is
# available from the Discus WWW site at:
#    http://www.discusware.com/discus/license
#
# Pursuant to the Discus License Agreement, this copyright notice may not be
# removed or altered in any way.
#-------------------------------------------------------------------------------

#---SEPARATOR---#

sub compare_source {
	my ($source_in, $topic, $page, $postindex, $message_src) = @_;
	my ($src, $flag, @src, $line, $src_temp, @newseen, @seen, @removed, @added);
	$src = $message_src;
	$src_temp = $src;
	return ("", "") if $src eq "";
	undef @seen;
	while ($src_temp =~ m|\\image_alreadyuploaded\{(\d+)|) {
		push (@seen, "$1.gif");
		$src_temp = join("", $`, $');
	}
	while ($src_temp =~ m|\\jpeg_alreadyuploaded\{(\d+)|) {
		push (@seen, "$1.jpg");
		$src_temp = join("", $`, $');
	}
	while ($src_temp =~ m|\\attachment_alreadyuploaded\{([^,]+)|) {
		push (@seen, "$1");
		$src_temp = join("", $`, $');
	}
	$src_temp = $source_in;
	while ($src_temp =~ m|\\image_alreadyuploaded\{(\d+)|) {
		push (@newseen, "$1.gif");
		$src_temp = join("", $`, $');
	}
	while ($src_temp =~ m|\\jpeg_alreadyuploaded\{(\d+)|) {
		push (@newseen, "$1.jpg");
		$src_temp = join("", $`, $');
	}
	while ($src_temp =~ m|\\attachment_alreadyuploaded\{([^,]+)|) {
		push (@newseen, "$1");
		$src_temp = join("", $`, $');
	}
	foreach $line (@seen) {
		if (!grep(/^$line$/, @newseen)) {
			push (@removed, $line);
		}
	}
	foreach $line (@newseen) {
		if (!grep(/^$line$/, @seen)) {
			push (@added, $line);
		}
	}
	return (join("\n", @removed), join("\n", @added));
}

#---SEPARATOR---#

sub read_source {
	my ($topic, $page, $optional_post) = @_;
	if (!-e "$admin_dir/msg_index/$topic") {
		mkdir("$admin_dir/msg_index/$topic", oct($perms0777)) || &log_error("src-board-subs-11", "read_source", "Could not make directory $topic under msg_index: $!");
		chmod (oct($perms0777), "$admin_dir/msg_index/$topic");
	}
	if (open(FILE, "$admin_dir/msg_index/$topic/$page-src.txt")) {
		@file = <FILE>;
		close (FILE);
		if ($optional_post != 0) {
			@file = grep(/^$optional_post /, @file);
			$result = $file[0];
			$result =~ m|^$optional_post |; $result = $';
			chomp $result;
			return $result;
		}
		$result = join("", @file);
		return $result;
	} else {
		return "";
	}
}

#---SEPARATOR---#

sub write_source {
	my ($topic, $page, $optional_post, $string, $lock_flag, $create_flag) = @_;
	if (!-d "$admin_dir/msg_index/$topic") {
		unlink("$admin_dir/msg_index/$topic") if -f "$admin_dir/msg_index/$topic";
		mkdir("$admin_dir/msg_index/$topic", oct($perms0777)) || &log_error("src-board-subs-11", "write_source", "Could not make directory $topic under msg_index: $!");
	}
	chmod (oct($perms0777), "$admin_dir/msg_index/$topic");
	&lock("write_source", "$admin_dir/msg_index/$topic/$page-src.txt") if $lock_flag == 0;
	if (open(FILE, "$admin_dir/msg_index/$topic/$page-src.txt")) {
		@file = <FILE>;
		close (FILE);
	} else {
		&unlock("write_source", "$admin_dir/msg_index/$topic/$page-src.txt") if $lock_flag == 0;
		undef @file;
	}
	if ($optional_post != 0) {
		@file = grep(!/^$optional_post /, @file);
	} else {
		@file = split(/\n/, $string);
		foreach $line (@file) {
			$line .= "\n";
		}
	}
	push (@file, "$optional_post $string\n") if ($optional_post != 0 && $string ne "");
	if (open(FILE, ">$admin_dir/msg_index/$topic/$page-src.txt")) {
		print FILE @file;
		close (FILE);
		chmod(oct($perms0666), "$admin_dir/msg_index/$topic/$page-src.txt");
		&unlock("write_source", "$admin_dir/msg_index/$topic/$page-src.txt") if $lock_flag == 0;
		return 1;
	} else {
		chmod(oct($perms0666), "$admin_dir/msg_index/$topic/$page-src.txt");
		&unlock("write_source", "$admin_dir/msg_index/$topic/$page-src.txt") if $lock_flag == 0;
		return 0;
	}
}

#---SEPARATOR---#
#REQ:page_manager_navbar

sub viewmessage {
	my ($topic_number, $me_number, $index) = @_;
	my (@file, $line, $flag, $post, $postby, $postbyflag);
	my ($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic_number, $me_number);
	@file = split(/\n/, $message); $flag = 0;
	$prev = 0; $next = 0;
	foreach $line (@file) {
		if ($line =~ m|<!-Post: $index-!><!-Time: (\d+)-!>|) {
			$flag = 1; $time = $1;
		} elsif ($line =~ m|<!-/Post: $index-!>|) {
			$flag = 2;
		} elsif ($flag == 1 && $line =~ m|<!-Text-!>|) {
			$line =~ m|<!-Name-!>(.*)<!-/Name-!>|; $name = $1;
			$line =~ m|<!-Email-!>(.*)<!-/Email-!>|; $email = $1;
			$line =~ m|<!-Text-!>(.*)<!-/Text-!>|; $text_msg = $1;
			if ($email =~ m|<a href=['"]([^'"]+)['"]>|i) {
				$email = $1; $a = $';
				if ($email =~ m|^mailto:|) {
					$email = $';
				} else {
					if ($email =~ m|&profile=(.*)|) {
						$profile = $1;
					}
					$email = "";
				}
			} elsif ($email =~ m|<a href=mailto:([^>]+)>|i) {
				$email = $1;
			} else {
				$email = "";
			}
		} elsif ($flag == 2 && $line =~ m|<!-Post: (\d+)-!>|) {
			$next = $1; $flag = 3; last;
		} elsif ($flag == 0 && $line =~ m|<!-Post: (\d+)-!>|) {
			$prev = $1;
		}
	}
	$postby = $name;
	$PROFILE_LINK = "<A HREF=$script_url/board-profile.$cgi_extension?action=display_profile&profile=$profile>" if $profile;
	$postby = join("", $PROFILE_LINK, $name, "</A>") if $PROFILE_LINK;
	$postby = join("", "<A HREF=\"mailto:$email\">", $name, "</A>") if $email;
	$pb = $L{POSTBY};
	$datetime = &get_date_time("long", $time);
	$pb =~ s/\%name/$postby/g;
	$pb =~ s/\%date/$datetime/g;
	$postby = $pb;
	&header;
	print "<HTML><HEAD><TITLE>Message Viewer</TITLE>\n";
	print "<SCRIPT LANGUAGE=\"JavaScript\">\n";
	print "<!--\n";
	print "function setStatus(msg) {\n";
	print "  window.status=msg\n";
	print "  return true\n";
	print "}\n";
	print "// -->\n";
	print "</SCRIPT>\n";
	print "</HEAD>\n";
	print "<BODY BGCOLOR=ffffff TEXT=000000 LINK=0000ff VLINK=0000ff ALINK=ff0000 onLoad=\"window.defaultStatus = 'View Message'\">\n";
	print "$fs\n";
	print "<FONT SIZE=3><CENTER><B>View Message</B></CENTER></FONT>\n";
	&page_manager_navbar($topic_number, $me_number, "View Message");
	if (!-e "$message_dir/$topic_number") {
		while ($post =~ m|<IMG SRC="$message_url/(\d+)/(\d+)\.(\w+)"([^>]*)>|g) {
			$tn = $1; $pn = $2; $ex = $3;
			$post = join("", $`, "<IMG SRC=\"$script_url/board-admin.$cgi_extension?action=show_secure_graphic&file=/$tn/$pn.$ex&HTTP_REFERER=//$topic_number/$me_number.$ext&username=$FORM{'username'}\"$4>", $');
		}
		while ($post =~ m|<!-Attachment: ([^!]+)-!><CENTER><TABLE BORDER=1><TR><TD><IMG SRC="$html_url/icons/mime_(\w+).gif" ALIGN=LEFT ALT="([^"]+)">(.*?)<BR><A HREF="$message_url/(\d+)/[^"]+"><B>([^<]+)</B></A> \(([^>]+)\)</TD></TR></TABLE></CENTER><!-/Attachment-!>|) {
			($file, $icon, $type, $descr, $topic, $fakefile, $size) = ($1, $2, $3, $4, $5, $6, $7);
			$post = join("", $`, "<CENTER><TABLE BORDER=1><TR><TD><IMG SRC=\"$html_url/icons/mime_$icon.gif\" ALIGN=LEFT>$descr<BR><A HREF=\"$script_url/board-auth.$cgi_extension?type=$type&file=/$topic/$file\"><B>$fakefile</B></A> ($size)</TD></TR></TABLE></CENTER>", $');
		}
	}
	($bgcolor, $text, $link, $vlink, $alink, $img, $size, $face) = split(/\t/, $color);
	print "<TABLE WIDTH=100% HEIGHT=60% BORDER=1 BGCOLOR=$bgcolor BACKGROUND=\"$img\">\n";
	print "<TR><TD VALIGN=TOP ALIGN=LEFT><BASEFONT SIZE=\"$size\"><FONT COLOR=$text FACE=\"$face\" SIZE=\"$size\">";
	print "$postby<P>\n$text_msg</FONT></TD></TR></TABLE>\n";
	print "<BR></FONT>\n";
	print "<CENTER><TABLE><TR>\n";
	if ($prev != 0) {
		print "<TD WIDTH=200><FORM ACTION=$cgiurlm METHOD=POST>\n";
		print "<INPUT TYPE=HIDDEN NAME=username VALUE=$FORM{'username'}>\n";
		print "<INPUT TYPE=HIDDEN NAME=HTTP_REFERER VALUE=//$topic_number/$me_number.$ext>\n";
		print "<INPUT TYPE=HIDDEN NAME=action VALUE=viewmessage>\n";
		print "<INPUT TYPE=HIDDEN NAME=num VALUE=$prev>\n";
		print "<INPUT TYPE=SUBMIT VALUE=\"<< PREVIOUS\">\n";
		print "</FORM></TD>\n";
	}
	if ($next != 0) {
		print "<TD WIDTH=200><FORM ACTION=$cgiurlm METHOD=POST>\n";
		print "<INPUT TYPE=HIDDEN NAME=username VALUE=$FORM{'username'}>\n";
		print "<INPUT TYPE=HIDDEN NAME=HTTP_REFERER VALUE=//$topic_number/$me_number.$ext>\n";
		print "<INPUT TYPE=HIDDEN NAME=action VALUE=viewmessage>\n";
		print "<INPUT TYPE=HIDDEN NAME=num VALUE=$next>\n";
		print "<INPUT TYPE=SUBMIT VALUE=\"NEXT >>\">\n";
		print "</FORM></TD>\n";
	}
	print "</TR></TABLE></CENTER>\n";
	print "</BODY></HTML>\n";
	exit(0);
}


#---SEPARATOR---#

sub save_profile_information {
	my ($password_new, $email_new, $fullname_new, $edit_new, $notify_new, $lastcheck_new, $group_new, @modify_lines) = @_;
	return 0 if $DO_NOT_WRITE_FILES_FLAG;
	my ($line, $is_user, $is_mod, $are_users, $pass_save, $line_2, @users);
	my ($file, $user, $pass, $email, $fullname, $edit, $notify, $lastcheck, $group, $users, $passwd);
	$is_user = 0; $is_mod = 0;
	foreach $line (@modify_lines) {
		chomp $line;
		$line = "" if $line !~ m|\S|;
		($file, $user, $pass, $email, $fullname, $edit, $notify, $lastcheck, $group) = split(/:/, $line);
		if ($file eq "PASSWD") {
			$is_mod = 1;
		} elsif ($file eq "USERS") {
			$is_user = 1;
		} else {
			$line = "";
		}
	}
	@modify_lines = grep(/\S/, @modify_lines);
	$pass_save = "";
	if ($is_user) {
		undef %redefined;
		foreach $line (@modify_lines) {
			($file, $user, $pass, $email, $fullname, $edit, $notify, $lastcheck, $group) = split(/:/, $line);
			next if $file eq "PASSWD";
			if ($edit == 2 && $USER_FLAG == 1 && $GLOBAL_OPTIONS{'emchange'} == 0) {
				$email_new = "";
			}
			$pass = $password_new if $password_new ne "";
			$pass_save = $pass if $pass_save eq "";
			$pass = $pass_save;
			$pass = $PASS_SAVE if $PASS_SAVE;
			$email = $email_new if $email_new ne "";
			$fullname = $fullname_new if $fullname_new ne "";
			$fullname =~ s/\(//g;
			$fullname =~ s/\)//g;
			$edit = $edit_new if $edit_new ne "";
			$notify = $notify_new if $notify_new ne "";
			$lastcheck = $lastcheck_new if $lastcheck_new ne "";
			$group = $group_new if $group_new ne "";
			chomp $group;
			$redefined{"u:$user:$group"} = join(":", $user, $pass, $email, $fullname, $edit, $notify, $lastcheck, "$group\n");
		}
		&lock("save_profile_information", "$admin_dir/users.txt");
		open (USERS, "$admin_dir/users.txt"); @users = <USERS>; close (USERS); $are_users = 1 if scalar(@users);
		foreach $line (@users) {
			($user, $pass, $email, $fullname, $edit, $notify, $lastcheck, $group) = split(/:/, $line);
			chomp $group;
			$line = $redefined{"u:$user:$group"} if $redefined{"u:$user:$group"};
		}
		$safe = 0;
		$safe = 1 if scalar(@users) == 0;
		$safe = 0 if $is_user;
		$safe = 0 if $are_users;
		$users = \@users;
		&safe_write("$admin_dir/users.txt", $users, $safe) || &error_message("File write failure", "Could not write users.txt.  Call to safe_write failed.");
		&unlock("save_profile_information", "$admin_dir/users.txt");
	}
	if ($is_mod) {
		undef %redefined;
		foreach $line (@modify_lines) {
			($file, $user, $pass, $email, $fullname, $edit, $notify, $lastcheck) = split(/:/, $line);
			next if $file eq "USERS";
			if ($edit == 2 && $USER_FLAG == 1 && $GLOBAL_OPTIONS{'emchange'} == 0) {
				$email_new = "";
			}
			$pass = $password_new if $password_new ne "";
			$pass_save = $pass if $pass_save eq "";
			$pass = $pass_save;
			$pass = $PASS_SAVE if $PASS_SAVE;
			$email = $email_new if $email_new ne "";
			$fullname = $fullname_new if $fullname_new ne "";
			$fullname =~ s/\(//g;
			$fullname =~ s/\)//g;
			$edit = $edit_new if $edit_new ne "";
			$notify = $notify_new if $notify_new ne "";
			$lastcheck = $lastcheck_new if $lastcheck_new ne "";
			chomp $lastcheck;
			$redefined{"p:$user"} = join(":", $user, $pass, $email, $fullname, $edit, $notify, "$lastcheck\n");
		}
		&lock("save_profile_information", "$admin_dir/passwd.txt");
		open (USERS, "$admin_dir/passwd.txt") || &error_message("Save Profile Information", "Could not open[r] passwd.txt file!");
		@users = <USERS>;
		close (USERS);
		foreach $line (@users) {
			($user, $pass, $email, $fullname, $edit, $notify, $lastcheck) = split(/:/, $line);
			chomp $lastcheck;
			$line = $redefined{"p:$user"} if $redefined{"p:$user"};
		}
		$users = \@users;
		&safe_write("$admin_dir/passwd.txt", $users) || &error_message("File write failure", "Could not write passwd.txt.  Call to safe_write failed.");
		&unlock("save_profile_information", "$admin_dir/passwd.txt");
	}
}

#---SEPARATOR---#
#REQ:webtags
#REQ:add_page
#REQ:change_layout
#REQ:change_board_colors
#REQ:regenerate_subtopic_list
#REQ:regenerate_message
#REQ:inverse_webtags

sub move_message {
	my ($old_topic, $old_page, $postindex, $new_topic, $new_page, $parmin, $checkarchive, $forcetime, $archiving) = @_;
	my ($num, $dt, $url, @file, $line, $str, $strg, $ts, @pi, $ind, $secure, $XF);
	if ($old_topic == $new_topic && $old_page == $new_page) {
		return ($new_page, $new_topic);
	}
	&extract("//$old_topic/$old_page.$ext");
	return 0 if ($checkarchive && $param =~ m|Archive|);

	# Create new page if necessary

	if ($new_topic eq "Create") {
		$XF = scalar(split(/,/, $postindex));
		if ($param !~ /Sublist/) {
			&change_layout($old_topic, $old_page, $param . "Sublist");
		}
		($lint, $newsubject) = &webtags($new_page,3,1);
		&error_message("Create New Subtopic Error", "You must type a subject for the new subtopic!") if $newsubject eq "";
		&error_message("Create New Subtopic Error", "The following formatting error was returned: $newsubject") if $lint eq "!Error";
		if ($parmin == 1) {
			$parm2 = "MessagesAdd";
		} elsif ($parmin == 10402) {
			$parm2 = "MessagesArchive";
		} else {
			$parm2 = "Messages";
		}
		$parm2 .= "Archive" if $checkarchive;
		$new_page = &add_page($newsubject, $parm2, "", "", $old_topic, $old_page, $owner, time, $poster, $XF, $archiving);
		$new_topic = $old_topic;
	}

	# Lock each file needed to accomplish this task

	&lock("move_message", "$message_dir/$old_topic/$old_page.$ext");
	&lock("move_message", "$message_dir/$new_topic/$new_page.$ext");
	&lock("move_message", "$admin_dir/msg_index/$old_topic-tree.txt");
	&lock("move_message", "$admin_dir/msg_index/$new_topic-tree.txt") if $new_topic != $old_topic;
	&lock("move_message", "$admin_dir/tree.txt") if $new_topic != $old_topic;

	# Obtain message from source file

	my ($head1, $color1, $lm1, $ann1, $ann_src1, $sublist1, $about1, $about_src1, $message1, $message_src1, $description_src1) = &get_page($old_topic, $old_page);
	@pi = split(/,/, $postindex);
	foreach $line (@pi) {
		$post{$line} = 1;
	}
	$poster = "";
	@msg = split(/\n/, $message1);
	undef @new; $flag = 0; $time = 0; $msgs = 0;
	foreach $line (@msg) {
		if ($line =~ m|<!-Post: (\d+)-!>|) {
			$pn = $1;
			if (grep(/^$pn$/, @pi)) {
				$flag = 1;
				push (@new, $line);
				if ($line =~ m|<!-Time: (\d+)-!>|) {
					$time = $1 if $1 >= $time;
				}
				$line = ""; $msgs += 1;
			}
		} elsif ($line =~ m|<!-/Post: (\d+)-!>|) {
			if ($flag == 1) {
				push (@new, $line);
				$flag = 0;
				$line = "";
			}
		} elsif ($flag == 1) {
			if ($line =~ m|<!-Name-!>(.*)<!-/Name-!>|) {
				$poster = $1 if $poster eq "";
			}
			push (@new, $line);
			$line = "";
		}
	}
	@msg = grep(/\S/, @msg);
	$message1 = join("\n", @msg);
	$message1 = &regenerate_message($old_topic, $old_page, $message1);
	&set_page($old_topic, $old_page, $head1, $color1, $lm1, $ann1, $ann_src1, $sublist1, $about1, $about_src1, $message1, $message_src1, $description_src1);
	&unlock("move_message", "$message_dir/$old_topic/$old_page.$ext");

	# Write message onto destination file

	my ($head2, $color2, $lm2, $ann2, $ann_src2, $sublist2, $about2, $about_src2, $message2, $message_src2, $description_src2) = &get_page($new_topic, $new_page);
	$newinfo = join("\n", @new);
	$newinfo .= "\n" if $newinfo !~ m|\n$|;
	$message2 .= $newinfo;
	@head = split(/\n/, $head2);
	foreach $line (@head) {
		if ($line =~ m|<!--Param: (\w+)-->|) {
			$x = $1;
			if ($x !~ m|Messages|) {
				$x .= "Messages";
				$line = "<!--Param: $x-->";
			}
		}
	}
	$head2 = join("\n", @head);
	$message2 = &regenerate_message($new_topic, $new_page, $message2);
	&set_page($new_topic, $new_page, $head2, $color2, $lm2, $ann2, $ann_src2, $sublist2, $about2, $about_src2, $message2, $message_src2, $description_src2);

	# Update the tree structure file

	open (TREE, "$admin_dir/msg_index/$old_topic-tree.txt");
	@tree = <TREE>;
	close (TREE);
	undef %seenit; undef %updates;
	foreach $line (reverse(@tree)) {
		@c = split(/\t/, $line);
		if ($c[2] == $old_page) {
			$updates{$c[2]} -= 1;
			$seenit{$c[2]} = 1;
			$seenit{$c[3]} = 1;
		} elsif ($seenit{$c[2]}) {
			$updates{$c[2]} -= 1;
			$seenit{$c[3]} = 1;
		}
	}
	if ($old_topic == $new_topic) {
		undef %seenit;
		foreach $line (reverse(@tree)) {
			@c = split(/\t/, $line);
			if ($c[2] == $new_page) {
				$updates{$c[2]} += 1;
				$seenit{$c[2]} = 1;
				$seenit{$c[3]} = 1;
			} elsif ($seenit{$c[2]}) {
				$updates{$c[2]} += 1;
				$seenit{$c[3]} = 1;
			}
		}
	}
	foreach $line (@tree) {
		chomp $line;
		@c = split(/\t/, $line);
		if ($updates{$c[2]} == 1) {
			$c[7] += $msgs if !$XF;
			$line = join("\t", @c);
		} elsif ($updates{$c[2]} == -1) {
			$c[7] -= $msgs;
			$line = join("\t", @c);
		}
		if ($c[2] == $new_page) {
			@x = split(/,/, $c[11]);
			push (@x, @pi);
			$c[11] = join(",", @x);
			$c[5] .= "Messages" if $c[5] !~ m|Messages|;
			$line = join("\t", @c);
		} elsif ($c[2] == $old_page) {
			$x = $c[11]; @x = split(/,/, $x);
			foreach $x (@x) {
				if (grep(/^$x$/, @pi)) {
					$x = "";
				}
			}
			@x = grep(/\S/, @x);
			$c[11] = join(",", @x);
			$line = join("\t", @c);
		}
		$line .= "\n" if $line !~ m|\n$|;
	}
	$pwline = \@tree;
	&safe_write("$admin_dir/msg_index/$old_topic-tree.txt", $pwline);
	undef @TREE_STRUCTURE_FILE;
	&unlock("move_message", "$admin_dir/msg_index/$old_topic-tree.txt");

	if ($old_topic != $new_topic) {
		open (TREE, "$admin_dir/msg_index/$new_topic-tree.txt");
		@tree = <TREE>;
		close (TREE);
		undef %seenit;
		foreach $line (reverse(@tree)) {
			@c = split(/\t/, $line);
			if ($c[2] == $new_page) {
				$updates{$c[2]} += 1;
				$seenit{$c[2]} = 1;
				$seenit{$c[3]} = 1;
			} elsif ($seenit{$c[2]}) {
				$updates{$c[2]} += 1;
				$seenit{$c[3]} = 1;
			}
		}
		foreach $line (@tree) {
			chomp $line;
			@c = split(/\t/, $line);
			if ($updates{$c[2]} == 1) {
				$c[7] += $msgs if !$XF;
				$line = join("\t", @c);
			} elsif ($updates{$c[2]} == -1) {
				$c[7] -= $msgs;
				$line = join("\t", @c);
			}
			if ($c[2] == $new_page) {
				@x = split(/,/, $c[11]);
				push (@x, @pi);
				$c[11] = join(",", @x);
				$c[5] .= "Messages" if $c[5] !~ m|Messages|;
				$line = join("\t", @c);
			} elsif ($c[2] == $old_page) {
				$x = $c[11]; @x = split(/,/, $x);
				foreach $x (@x) {
					if (grep(/^$x$/, @pi)) {
						$x = "";
					}
				}
				@x = grep(/\S/, @x);
				$c[11] = join(",", @x);
				$line = join("\t", @c);
			}
			$line .= "\n" if $line !~ m|\n$|;
		}
		$pwline = \@tree;
		&safe_write("$admin_dir/msg_index/$new_topic-tree.txt", $pwline);
		undef @TREE_STRUCTURE_FILE;
		&unlock("move_message", "$admin_dir/msg_index/$new_topic-tree.txt");
		open (TREE, "$admin_dir/tree.txt");
		@tree = <TREE>;
		close (TREE);
		foreach $line (@tree) {
			@c = split(/\t/, $line);
			if ($c[1] == $old_topic) {
				$c[7] -= $msgs;
				$line = join("\t", @c);
			} elsif ($c[1] == $new_topic) {
				$c[7] += $msgs;
				$c[5] .= "Messages" if ($new_topic eq $new_page && $c[5] !~ m|Messages|);
				$line = join("\t", @c);
			}
		}
		$pwline = \@tree;
		&safe_write("$admin_dir/tree.txt", $pwline);
		&unlock("move_message", "$admin_dir/tree.txt");
	}

	# Copy attachments to new directory (if necessary)

	if ($new_topic != $old_topic) {
		open (ATTACH, "$admin_dir/msg_index/attach-mgr.txt");
		@attach = <ATTACH>;
		close (ATTACH);
		foreach $line (@attach) {
			chomp $line;
			($index, $file) = split(/\s+/, $line);
			if ($post{$index} == 1) {
				&copier($old_topic, $new_topic, $file);
			}
		}
		($head2, $color2, $lm2, $ann2, $ann_src2, $sublist2, $about2, $about_src2, $message2, $message_src2, $description_src2) = &get_page($new_topic, $new_page);
		@msg = split(/\n/, $message2);
		$dir = "$message_dir/$new_topic" if -e "$message_dir/$new_topic";
		$dir = "$secdir/$new_topic" if !-e "$message_dir/$new_topic";
		foreach $line (@msg) {
			if ($line =~ m|<!-Text-!>(.*)<!-/Text-!>|) {
				$text = $1;
				while ($text =~ m|<IMG SRC="$message_url/(\d+)/(\d+)\.gif" |g) {
					$file = "$2.gif";
					next if $attmoved{$file} == 1;
					next if -e "$dir/$file";
					&copier($old_topic, $new_topic, $file);
					&log_error("src-board-subs-13", "move_message", "Moved [$file] from $old_topic to $new_topic; file not in attachment list file but detected in source");
				}
				while ($text =~ m|<IMG SRC="$message_url/(\d+)/(\d+)\.jpg" |g) {
					$file = "$2.jpg";
					next if $attmoved{$file} == 1;
					next if -e "$dir/$file";
					&copier($old_topic, $new_topic, $file);
					&log_error("src-board-subs-13", "move_message", "Moved [$file] from $old_topic to $new_topic; file not in attachment list file but detected in source");
				}
				while ($text =~ m|<!-Attachment: (.*?)-!>|g) {
					$file = $1;
					next if $attmoved{$file} == 1;
					next if -e "$dir/$file";
					&copier($old_topic, $new_topic, $file);
					&log_error("src-board-subs-13", "move_message", "Moved [$file] from $old_topic to $new_topic; file not in attachment list file but detected in source");
				}
			}
		}
		foreach $file (@fm) {
			$message2 =~ s%$message_url/$old_topic/$file%$message_url/$new_topic/$file%g;
		}
		&set_page($new_topic, $new_page, $head2, $color2, $lm2, $ann2, $ann_src2, $sublist2, $about2, $about_src2, $message2, $message_src2, $description_src2);
		&unlock("move_message", "$message_dir/$new_topic/$new_page.$ext");
	} else {
		&unlock("move_message", "$message_dir/$new_topic/$new_page.$ext");
	}

	# Change the log file to indicate move

	undef %post; undef @new;
	foreach $x (@pi) {
		$post{$x} = 1;
	}
	&lock("move_message", "$admin_dir/msg_index/$old_topic-log.txt");
	$bc_old = 0; $bc_new = 0;
	open (SRC, "$admin_dir/msg_index/$old_topic-log.txt");
	open (NEW, ">$admin_dir/msg_index/$old_topic-log.txt.NEW");
	while (<SRC>) {
		@c = split(/;/);
		$n = $c[0];
		if ($post{$n} == 1) {
			$c[3] =~ m|(\d+)/(\d+)|;
			$c[3] = join("/", $new_topic, $new_page);
			$_ = join(";", @c);
			if ($old_topic == $new_topic) {
				print NEW $_;
				$bc_old += length($_);
			} else {
				push (@new, $_);
			}
		} else {
			print NEW $_;
			$bc_old += length($_);
		}
	}
	close (NEW);
	close (SRC);
	&rename_file("$admin_dir/msg_index/$old_topic-log.txt", $bc_old);
	&unlock("move_message", "$admin_dir/msg_index/$old_topic-log.txt");

	# Change topic log to indicate move

	if ($old_topic != $new_topic) {
		&lock("move_message", "$admin_dir/msg_index/$new_topic-log.txt");
		open (SRC, "$admin_dir/msg_index/$new_topic-log.txt");
		open (NEW, ">$admin_dir/msg_index/$new_topic-log.txt.NEW");
		@x = sort {$a <=> $b} @new;
		while (<SRC>) {
			$line = $_;
			@c = split(/;/);
			$u = (split(/;/, $x[0]))[0];
			while (($u < $c[0] || $c[0] == 0) && $u != 0) {
				$m = shift(@x);
				print NEW $m;
				$bc_new += length($m);
				$u = (split(/;/, $x[0]))[0];
			}
			print NEW $line;
			$bc_new += length($line);
		}
		foreach $line (@x) {
			print NEW $line;
			$bc_new += length($line);
		}
		close (NEW);
		close (SRC);
		&rename_file("$admin_dir/msg_index/$new_topic-log.txt", $bc_new);
		&unlock("move_message", "$admin_dir/msg_index/$new_topic-log.txt");
	}

	# Place message sources into correct file

	if ($GLOBAL_OPTIONS{'nosource'} == 0) {
		&lock("move_message", "$admin_dir/msg_index/$old_topic/$old_page-src.txt");
		open (OLDSRC, "$admin_dir/msg_index/$old_topic/$old_page-src.txt");
		@oldsrc = <OLDSRC>;
		close (OLDSRC);
		&lock("move_message", "$admin_dir/msg_index/$new_topic/$new_page-src.txt");
		open (NEWSRC, "$admin_dir/msg_index/$new_topic/$new_page-src.txt");
		@newsrc = <NEWSRC>;
		close (NEWSRC);
		foreach $line (@oldsrc) {
			($num, $src) = split(/\s+/, $line);
			if ($post{$num} == 1) {
				push (@newsrc, $line);
				$line = "";
			}
		}
		@oldsrc = grep(/\S/, @oldsrc);
		@newsrc = sort { $a <=> $b; } @newsrc;
		open (OLDSRC, ">$admin_dir/msg_index/$old_topic/$old_page-src.txt");
		print OLDSRC @oldsrc;
		close (OLDSRC);
		open (NEWSRC, ">$admin_dir/msg_index/$new_topic/$new_page-src.txt");
		print NEWSRC @newsrc;
		close (NEWSRC);
		&unlock("move_message", "$admin_dir/msg_index/$old_topic/$old_page-src.txt");
		&unlock("move_message", "$admin_dir/msg_index/$new_topic/$new_page-src.txt");
	}

	# Update keyword search file

	undef @new; $bc_old = 0; $bc_new = 0;
	&lock("move_message", "$admin_dir/msg_index/$old_topic-search.txt");
	&lock("move_message", "$admin_dir/msg_index/$new_topic-search.txt") if $old_topic != $new_topic;
	open (SRC, "$admin_dir/msg_index/$old_topic-search.txt");
	open (NEW, ">$admin_dir/msg_index/$old_topic-search.txt.NEW");
	while (<SRC>) {
		@c = split(/\s/, $_, 4);
		$n = $c[0];
		if ($post{$n} == 1) {
			if ($old_topic == $new_topic) {
				$c[2] = $new_page;
				$_ = join(" ", @c);
				print NEW $_;
				$bc_old += length($_);
			} else {
				$c[1] = $new_topic;
				$c[2] = $new_page;
				$_ = join(" ", @c);
				push (@new, $_);
			}
		} else {
			print NEW $_;
			$bc_old += length($_);
		}
	}
	close (NEW);
	close (SRC);
	if ($new_topic != $old_topic) {
		open (SRC, "$admin_dir/msg_index/$new_topic-search.txt");
		open (NEW, ">$admin_dir/msg_index/$new_topic-search.txt.NEW");
		@x = sort {$a <=> $b} @new;
		while (<SRC>) {
			$line = $_;
			@c = split(/\s/);
			$u = (split(/\s/, $x[0]))[0];
			while (($u < $c[0] || $c[0] == 0) && $u != 0) {
				$m = shift(@x);
				print NEW $m;
				$bc_new += length($m);
				$u = (split(/\s/, $x[0]))[0];
			}
			print NEW $line;
			$bc_new += length($line);
		}
		foreach $line (@x) {
			print NEW $line;
			$bc_new += length($line);
		}
		close (NEW);
		close (SRC);
	}
	&rename_file("$admin_dir/msg_index/$new_topic-search.txt", $bc_new) if $new_topic != $old_topic;
	&rename_file("$admin_dir/msg_index/$old_topic-search.txt", $bc_old);
	&unlock("move_message", "$admin_dir/msg_index/$old_topic-search.txt", "$admin_dir/msg_index/$new_topic-search.txt");

	# Update message counts, etc.

	foreach $key (keys(%updates)) {
		next if $key == 0;
		next if $updates{$key} == 1;
		&lock("move_message", "$message_dir/$old_topic/$key.$ext");
		my ($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($old_topic, $key);
		$sublist = &regenerate_subtopic_list($old_topic, $key, $sublist);
		&set_page($old_topic, $key, $head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src);
		&unlock("move_message", "$message_dir/$old_topic/$key.$ext");
		delete $updates{$key};
	}

	foreach $key (keys(%updates)) {
		next if $key == 0;
		next if $updates{$key} <= 0;
		&lock("move_message", "$message_dir/$new_topic/$key.$ext");
		my ($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($new_topic, $key);
		$sublist = &regenerate_subtopic_list($new_topic, $key, $sublist);
		&set_page($new_topic, $key, $head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src);
		&unlock("move_message", "$message_dir/$new_topic/$key.$ext");
	}

	&change_board_colors("*");

	return ($new_page, $new_topic);
}

sub copier {
	my ($old_topic, $new_topic, $file) = @_;
	my ($dir);
	$dir = "";
	$dir = "$message_dir/$old_topic" if -e "$message_dir/$old_topic";
	$dir = "$secdir/$old_topic" if -e "$secdir/$old_topic";
	return 0 if $dir eq "";
	if (open(SRC, "$dir/$file")) {
		binmode(SRC);
		open (DEST, ">$message_dir/$new_topic/$file") if -e "$message_dir/$new_topic";
		open (DEST, ">$secdir/$new_topic/$file") if -e "$secdir/$new_topic";
		binmode(DEST);
		while (<SRC>) {
			print DEST;
		}
		close (DEST);
		close (SRC);
		unlink("$dir/$file");
	} else {
		&log_error("src-board-subs-13", "copier", "Could not open file $file for copying: $!");
	}
	push (@fm, $file);
	$attmoved{$file} = 1;
}

#---SEPARATOR---#
#REQ:write_source
#REQ:compare_source
#REQ:format_messages
#REQ:search_stop
#REQ:read_source
#REQ:inverse_webtags

sub save_edited_message {
	my ($topic_number, $page_number, $post_index, $source, $formatted, $username) = @_;
	&flush_language($topic_number);
	&lock("save_edited_message", "$message_dir/$topic_number/$page_number.$ext");
	my ($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic_number, $page_number);
	my (@message, $line, $flag);
	@message = split(/\n/, $message);
	$flag = 0; my ($source2);
	if ($GLOBAL_OPTIONS{'nosource'} == 0) {
		$source2 = &read_source($topic_number, $page_number, $post_index);
		$source2 = &inverse_webtags($formatted) if $source2 eq "";
	} else {
		$source2 = &inverse_webtags($formatted);
	}
	my ($removed, $added) = &compare_source(&unescape($source), $topic_number, $page_number, $post_index, &unescape($source2));
	if ($added) {
		$added =~ s/\n/<BR>/g;
		&error_message("Message Edit Error", "You have attempted to add the following files:<P>$added<P>Please remove these additions from your message and then re-attempt the save.", 0, 1);
	}
	if ($removed) {
		@file = split(/\n/, $removed);
		$dir = "$message_dir/$topic_number" if -e "$message_dir/$topic_number";
		$dir = "$secdir/$topic_number" if !-e "$message_dir/$topic_number";
		foreach $file (@file) {
			unlink("$dir/$file");
		}
		&lock("save_edited_message", "$admin_dir/msg_index/attach-mgr.txt");
		open (ATTACH, "$admin_dir/msg_index/attach-mgr.txt");
		@attach = <ATTACH>;
		close (ATTACH);
		foreach $line (@attach) {
			($pn, $file) = split(/\s+/, $line);
			if ($pn == $post_index) {
				$line = "" if grep(/^$file$/, @file);
			}
		}
		open (ATTACH, ">$admin_dir/msg_index/attach-mgr.txt");
		print ATTACH @attach;
		close (ATTACH);
		&unlock("save_edited_message", "$admin_dir/msg_index/attach-mgr.txt");
	}
	my ($name, $email, $ctr, $dt, $profile);
	@mx = grep(/<!-Post: (\d+)-!>/, @message);
	$P = scalar(@mx);
	foreach $line (@message) {
		if ($line =~ m|<!-Post: $post_index-!>|) {
			$flag = 1; $line =~ m|<!-Time: (\d+)-!>|; $dt = $1; $ctr += 1;
		} elsif ($line =~ m|<!-/Post: $post_index-!>|) {
			$flag = 0;
			$msg = &format_messages($name, $post_index, $formatted, $ctr-1, $email, $dt, $profile, "$topic_number/$page_number", $P, \@mx);
			$line = join("\n", $msg, $line);
		} elsif ($flag == 1 && $line =~ m|<!-Text-!>|) {
			$line =~ m|<!-Name-!>(.*)<!-/Name-!>|; $name = $1;
			$line =~ m|<!-Email-!>(.*)<!-/Email-!>|; $email = $1;
			if ($email =~ m|action=display_profile&profile=([^"]+)|) {
				$profile = $1; $email = "";
				$profile =~ s/-/:/g;
			} elsif ($email =~ m|<A HREF="mailto:([^"]+)">|i) {
				$email = $1;
			} else {
				$email = "";
			}
			$line = "";
		} elsif ($line =~ m|<!-Post: (\d+)-!>|) {
			$ctr += 1;
		} elsif ($flag == 1) {
			$line = "";
		}
	}
	$message = join("\n", grep(/\S/, @message));
	&set_page($topic_number, $page_number, $head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, "", $description_src);
	&write_source($topic_number, $page_number, $post_index, $source, 0, 1);
	&unlock("save_edited_message", "$message_dir/$topic_number/$page_number.$ext");

	$nmtemp = &remove_html($formatted, 1);
	$nmtemp .= " ";
	$nmtemp =~ m|^([\s\S]{0,40})\s|;
	$chars = $1; $chars =~ s/^\s+//; $chars =~ s/\s+$//; $chars = &escape($chars);
	$fsize = 0;
	&lock("save_edited_message", "$admin_dir/msg_index/$topic_number-log.txt");
	open (LOG, "$admin_dir/msg_index/$topic_number-log.txt");
	open (NEWLOG, ">$admin_dir/msg_index/$topic_number-log.txt.NEW");
	while (<LOG>) {
		@c = split(/;/, $_);
		if ($c[0] == $post_index) {
			chomp;
			@c = split(/;/, $_);
			$c[6] = $chars;
			$_ = join(";", @c);
			$_ .= "\n";
			print NEWLOG $_;
			$fsize += length($_);
		} else {
			print NEWLOG $_;
			$fsize += length($_);
		}
	}
	close (LOG); close (NEWLOG);
	&rename_file("$admin_dir/msg_index/$topic_number-log.txt", $fsize);
	&unlock("save_edited_message", "$admin_dir/msg_index/$topic_number-log.txt");

	($search, $blah) = &search_stop($formatted);
	$written_flag = 0; $fsize = 0;
	&lock("save_edited_message", "$admin_dir/msg_index/$topic_number-search.txt");
	open (SEARCH, "$admin_dir/msg_index/$topic_number-search.txt");
	open (NEWSEARCH, ">$admin_dir/msg_index/$topic_number-search.txt.NEW");
	while (<SEARCH>) {
		@c = split(/\s+/, $_, 2);
		if ($c[0] == $post_index) {
			$_ = "$post_index $topic_number $page_number $search\n";
			print NEWSEARCH $_;
			$fsize += length($_);
			$written_flag = 1;
		} else {
			print NEWSEARCH $_;
			$fsize += length($_);
		}
	}
	if (!$written_flag) {
		$_ = "$post_index $topic_number $page_number $search\n";
		print NEWSEARCH $_;
		$fsize += length($_);
	}
	close (SEARCH); close (NEWSEARCH);
	&rename_file("$admin_dir/msg_index/$topic_number-search.txt", $fsize);
	&unlock("save_edited_message", "$admin_dir/msg_index/$topic_number-search.txt");
	&flush_language("*");
}

#---SEPARATOR---#
#REQ:regenerate_message

sub reorder_messages {
	my ($referer, $arraystr) = @_;
	my (@array, %message, $file, @file, $line, $flag, %printed);
	&extract($referer);
	$file = "$message_dir/$topic_number/$me_number.$ext";
	&lock("reorder_messages", $file);
	my ($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic_number,$me_number);
	@file = split(/\n/, $message);
	$flag = 0;
	foreach $line (@file) {
		if ($line =~ /<!-Post: (\d+)-!>/) {
			$flag = $1;
			$message{$flag} .= $line . "\n";
			$printed{$flag} = 0;
		} elsif ($line =~ /<!-\/Post: $flag-!>/) {
			$message{$flag} .= $line . "\n";
			$flag = 0;
		} elsif ($flag > 0) {
			$message{$flag} .= $line . "\n";
		}
	}
	@array = split(/:/, $arraystr);
	undef @file; undef @pr;
	foreach $message (@array) {
		push (@file, $message{$message});
		$printed{$message} = 1;
		push (@pr, $message);
	}
	foreach $key (keys(%printed)) {
		if ($printed{$key} == 0) {
			push (@file, $message{$key});
			push (@pr, $key);
		}
	}
	$message = join("\n", @file);
	$message = &regenerate_message($topic_number, $me_number, $message);
	&set_page($topic_number, $me_number, $head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src);
	&unlock("reorder_messages", $file);
	&lock("reorder_messages", "$admin_dir/msg_index/$topic_number-tree.txt");
	open (FILE, "$admin_dir/msg_index/$topic_number-tree.txt"); @file = <FILE>; close (FILE);
	foreach $line (@file) {
		@c = split(/\t/, $line);
		if ($c[1] == $topic_number && $c[2] == $me_number) {
			$c[11] = join(",", @pr);
			$line = join("\t", @c);
			$line .= "\n";
		}
	}
	open (FILE, ">$admin_dir/msg_index/$topic_number-tree.txt"); print FILE @file; close (FILE);
	&unlock("reorder_messages", "$admin_dir/msg_index/$topic_number-tree.txt");
}

#---SEPARATOR---#
#REQ:change_board_colors
#REQ:regenerate_message
#REQ:regenerate_subtopic_list

sub remove_message {
	my ($topic, $page, $message_number) = @_;
	my (@mn, %mn, @msg, $flag, $num);
	undef %mn;

	&lock("remove_message", "$message_dir/$topic/$page.$ext");
	my ($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic, $page);
	@mn = split(/,/, $message_number);
	foreach $num (@mn) {
		$mn{$num} = 1;
	}
	@msg = split(/\n/, $message);
	$flag = 0;
	foreach $line (@msg) {
		if ($line =~ m|<!-Post: (\d+)-!>|) {
			$num = $1;
			if ($mn{$num} == 1) {
				$flag = $num;
				$line = "";
				push (@removed, $num);
			}
		} elsif ($line =~ m|<!-/Post: $flag-!>|) {
			$line = "";
			$flag = 0;
		} elsif ($flag > 0) {
			$line = "";
		}
	}
	$message = join("\n", grep(/\S/, @msg));
	$message = &regenerate_message($topic, $page, $message);
	&set_page($topic, $page, $head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, "", $description_src);
	&unlock("remove_message", "$message_dir/$topic/$page.$ext");

	&lock("remove_message", "$admin_dir/msg_index/$topic/$page-src.txt");
	open (FILE, "$admin_dir/msg_index/$topic/$page-src.txt");
	@file = <FILE>;
	close (FILE);
	foreach $line (@file) {
		($num, $text) = split(/\s+/, $line);
		$line = "" if $mn{$num} == 1;
	}
	@file = grep(/\S/, @file);
	open (FILE, ">$admin_dir/msg_index/$topic/$page-src.txt");
	print FILE @file;
	close (FILE);
	&unlock("remove_message", "$admin_dir/msg_index/$topic/$page-src.txt");

	&lock("remove_message", "$admin_dir/msg_index/$topic-tree.txt");
	open (FILE, "$admin_dir/msg_index/$topic-tree.txt");
	@file = <FILE>;
	close (FILE);
	undef %updates;
	foreach $line (@file) {
		@c = split(/\t/, $line);
		if ($c[1] == $topic && $c[2] == $page) {
			chomp $line;
			@c = split(/\t/, $line);
			$updates{$c[2]} = 1;
			@x = split(/,/, $c[11]);
			foreach $m (@x) {
				$m = "" if $mn{$m} == 1;
			}
			@x = grep(/\S/, @x);
			$c[11] = join(",", @x);
			$line = join("\t", @c);
			$line .= "\n";
		}
	}
	foreach $line (reverse(@file)) {
		@c = split(/\t/, $line);
		if ($updates{$c[2]} == 1) {
			$updates{$c[3]} = 1;
			last if $c[3] == 0;
		}
	}
	foreach $line (@file) {
		@c = split(/\t/, $line);
		if ($updates{$c[2]} == 1) {
			$c[7] -= scalar(@mn);
			$line = join("\t", @c);
		}
	}
	open (FILE, ">$admin_dir/msg_index/$topic-tree.txt");
	print FILE @file;
	close (FILE);
	&unlock("remove_message", "$admin_dir/msg_index/$topic-tree.txt");

	&lock("remove_message", "$admin_dir/tree.txt");
	open (FILE, "$admin_dir/tree.txt");
	@file = <FILE>;
	close (FILE);
	foreach $line (@file) {
		@c = split(/\t/, $line);
		if ($c[1] == $topic) {
			$c[7] -= scalar(@mn);
			$line = join("\t", @c);
			last;
		}
	}
	open (FILE, ">$admin_dir/tree.txt");
	print FILE @file;
	close (FILE);
	&unlock("remove_message", "$admin_dir/tree.txt");

	&lock("remove_message", "$admin_dir/msg_index/attach-mgr.txt");
	open (ATTACH, "$admin_dir/msg_index/attach-mgr.txt");
	@file = <ATTACH>;
	close (ATTACH);
	foreach $line (@file) {
		($num, $file) = split(/\s+/, $line);
		if ($mn{$num} == 1) {
			unlink("$message_dir/$topic/$file");
			unlink("$secdir/$topic/$file");
			$line = "";
		}
	}
	@file = grep(/\S/, @file);
	open (ATTACH, ">$admin_dir/msg_index/attach-mgr.txt");
	print ATTACH @file;
	close (ATTACH);
	&unlock("remove_message", "$admin_dir/msg_index/attach-mgr.txt");

	$filename = "$admin_dir/msg_index/$topic-log.txt";
	$fsize = 0;
	&lock("remove_message", $filename);
	open (LOG, "$filename");
	open (LOG2, ">$filename.NEW");
	while (<LOG>) {
		@c = split(/;/, $_, 2); $c0 = $c[0];
		if ($mn{$c0} != 1) {
			print LOG2 $_;
			$fsize += length($_);
		}
	}
	close (LOG2); close (LOG);
	&rename_file($filename, $fsize);
	chmod (oct($perms0666), "$filename");
	&unlock("remove_message", $filename);

	$filename = "$admin_dir/msg_index/$topic-search.txt";
	&lock("remove_message", $filename);
	$fsize = 0;
	open (LOG, "$filename");
	open (LOG2, ">$filename.NEW");
	while (<LOG>) {
		@c = split(/\s/, $_, 2); $c0 = $c[0];
		if ($mn{$c0} != 1) {
			print LOG2 $_;
			$fsize += length($_);
		}
	}
	close (LOG2); close (LOG);
	&rename_file($filename, $fsize);
	chmod (oct($perms0666), "$filename");
	&unlock("remove_message", $filename);
	foreach $key (keys(%updates)) {
		next if $key == 0;
		&lock("remove_message", "$message_dir/$topic/$key.$ext");
		($head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src) = &get_page($topic, $key);
		$sublist = &regenerate_subtopic_list($topic, $key, $sublist);
		&set_page($topic, $key, $head, $color, $lm, $ann, $ann_src, $sublist, $about, $about_src, $message, $message_src, $description_src);
		&unlock("remove_message", "$message_dir/$topic/$key.$ext");
	}
	&change_board_colors("*");
	return 1;
}

# END - FILE IS CORRECTLY UPLOADED #
