#!/usr/local/bin/perl

# ---------------------------------------------------
#
# TeaChat
$version = '1.7.2';	# 2002/2/18
#	(c) 1998-2002 Toshikazu.S All rights reserved.
#
# URL :		 http://www.teachat.org/
# E-Mail :	 webmaster@teachat.org
#
# ---------------------------------------------------

############################### ユーザー設定ここから

$script    = 'http://www.freak.ne.jp/~kuni/cgi-bin/chat/chat.cgi';
			# 【必須】chat,cgiのURL（http://からの記述を推奨）
$password  = 'eva-01';	# 【必須】管理パスワード（半角英数字）

$dir       = '.';	# chat.cgiから他のファイルへのパス。最後に「/」をつけないこと。意味がわからない人は触らないように。
$kcode     = 'sjis';	# 漢字コード。EUCで設置する場合はここを「euc」に

# --------データファイル--------
# ファイル名は変更を推奨。変更する場合は下記記述の他サーバー上のファイル名の変更も忘れずに。

$chat_file	= "$dir/chat.dat";
$ctz_file	= "$dir/ctz.dat";
$decorate_file	= "$dir/decorate.dat";
$rank_file	= "$dir/rank.dat";
$mem_file	= "$dir/mem.dat";
$count_file	= "$dir/count.dat";
$punish_file	= "$dir/punish.dat";

############################### ユーザー設定ここまで

&file_open;

# ---------------------基本設定----------------------

$method		= 'POST';
$html_pl	= "$dir/html.pl";
$tool_pl	= "$dir/tool.pl";
$ctz_pl		= "$dir/customize.pl";
$decorate_pl	= "$dir/decorate.pl";
$color_dat	= "$dir/color.dat";
$lockdir	= "$dir/lock/";
$jcode		= "$dir/jcode_se.pl";
$retry		= 4;
$background	= " background=\"$background\"" if($background);
$body		= "<body$background bgcolor=\"$bgcolor\" text=\"$text\" link=\"$link\" vlink=\"$vlink\" alink=\"$alink\"";
$background2	= " background=\"$background2\"" if($background2);
$body2		= "<body$background2 bgcolor=\"$bgcolor2\" text=\"$text2\" link=\"$link2\" vlink=\"$vlink2\" alink=\"$alink2\"";
$endhtml	= '<noembed><noscript><noscript><noscript><noscript>';
$time		= time+$stdtime*60*60;

# ---------------------分岐処理----------------------

$| = 1;
&usercheck;
require "$jcode";
&decode;
if($ktai_ua){
	&decode2;
	$high = on;
	if($view){
		if($rank){
			require "$html_pl";
			&rank_read;
			exit;
		}
		&write if($chat ne "");
		&read;
	}
	elsif($administer){
		require "$tool_pl";
		if(($pass eq $password) && $adexe eq "on"){ &ad_exe; }
		&ad_html;
	}
	else{
		require "$html_pl";
		&i_first;
	}
}
#elsif($hua =~ /UP\.Browser/i){
#	require "$ez_pl";
#	&ez;
#}
elsif($first eq "on" || $nofent eq "on"){ require "$html_pl"; &first; }
elsif($hidden eq "on"){ require "$html_pl"; &hidden; }
elsif($nof eq "on"){
	if(($chat ne "") || $enter eq "on"){ &decode2; &write if($change ne "on"); }
	&read;
}
elsif($enter){ require "$html_pl"; &decode2; &setcookie; &operation; }
elsif($view){
	&decode2;
	&write if($chat ne "");
	if($rank || $exit){ &member; require "$html_pl"; &rank_read; exit; }
	elsif($secret){ require "$html_pl"; &secret; exit; }
	elsif($secret2){ require "$html_pl"; &secret2; exit; }
	else{
		&unlock if($unlock eq "on");
		&read;
	}
}
elsif($cc eq "on"){
	require "$html_pl";
	&color_chart;
}
elsif($administer eq "on"){
	require "$tool_pl";
	if(($pass eq $password) && $adexe eq "on"){ &ad_exe; }
	&ad_html;
}
elsif($customize eq "on"){
	require "$ctz_pl";
	if(($pass eq $password) && $ctz_exe eq "on"){ &ctz_exe; }
	&ctz_html;
}
elsif($FORM{'decorate'} eq "on"){
	require "$decorate_pl";
	if(($pass eq $password) && $FORM{'decorate_exe'} eq "on"){ &decorate_exe; }
	&decorate;
}
else{
	require "$html_pl";
	&index;
}
exit;

# ------------------ユーザチェック-------------------

sub usercheck{
	$host = &getip;
	&punish if($punish_check);

	$hua		= $ENV{'HTTP_USER_AGENT'};
	$jxn		= $ENV{'X_JPHONE_MSNAME'};
	$jxn		= $ENV{'HTTP_X_JPHONE_MSNAME'};
	$jphone		= 1 if($jxn || ($hua =~ /J-PHONE\//i));
	$jphone_old	= 1 if($jxn || ($hua =~ /J-PHONE\/1/i) || ($hua =~ /J-PHONE\/2/i));
	$hua		= $jxn if($jxn && !$hua);
	$method		= 'GET' if($jphone_old);
	$ktai_ua	= 1 if(($hua =~ /DoCoMo|ASTEL|pda|UP\.Browser|Palmscape|Xiino/i) || $jphone);
	$nonrefer_ua	= 1 if($ktai_ua || ($hua =~ /Lynx|Windows CE/i));

	$charset	= 'Shift_JIS';
	$charset	= 'x-sjis' if($hua =~ /MSIE 3|Mozilla\/1|Mozilla\/2|Mozilla\/3/i);
	$charset	= 'EUC-JP' if($kcode eq 'euc');
	$firewall	= 0 if($nonrefer_ua);

	$accesskey = 'accesskey';
	$accesskey = 'directkey' if($jphone);
}

# --------------------REFERER判定--------------------

sub getreferer{
	$refer = $ENV{'HTTP_REFERER'};
	$ident = $script;
	$ident =~ s/\~/.*/g;
	if(!$nonrefer_ua && ($refer !~ /$ident/i)){ &error(0); }
}

# ----------------------ヘッダ-----------------------

sub http_header{
	$doctype = $doctype2;
	$doctype = 'Transitional' unless($doctype2);
	print <<"EOF";
Pragma: no-cache
Expires: -1
P3P: CP="NOI ADMa"
Cache-Control: no-cache
robots: noindex,nofollow
Content-Style-Type: text/css
Content-Script-Type: text/javascript
Content-Language: ja
Content-Type: text/html; charset=$charset

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 $doctype//EN">
<html><head><title>$title</title>
EOF
}

# ---------------元IP取得 ホスト変換-----------------

sub getip{
	$ip = $ENV{'REMOTE_HOST'};
	$ip = $ENV{'REMOTE_ADDR'} if(!$ip);

	if($trueip){
		if($ENV{'HTTP_X_FORWARDED_FOR'} =~ m/^(\d+)\.(\d+)\.(\d+)\.(\d+)(\D*).*/){
			$ip = "$1.$2.$3.$4";
		}
		elsif($ENV{'HTTP_CACHE_INFO'} =~ m/(\d+)\.(\d+)\.(\d+)\.(\d+)/){
			$ip = "$1.$2.$3.$4";
		}
		elsif($ENV{'HTTP_FROM'} =~ m/(\d+)\.(\d+)\.(\d+)\.(\d+)/){
			$ip = "$1.$2.$3.$4";
		}
		elsif($ENV{'HTTP_SP_HOST'} =~ m/(\d+)\.(\d+)\.(\d+)\.(\d+)/){
			$ip = "$1.$2.$3.$4";
		}
		elsif($ENV{'HTTP_VIA'} =~ m/.*\s(\d+)\.(\d+)\.(\d+)\.(\d+)/){
			$ip = "$1.$2.$3.$4";
		}
		elsif($ENV{'HTTP_FORWARDED'} =~ m/.*\s(\d+)\.(\d+)\.(\d+)\.(\d+)/){
			$ip = "$1.$2.$3.$4";
		}
		elsif($ENV{'HTTP_CLIENT_IP'} =~ m/.*\s(\d+)\.(\d+)\.(\d+)\.(\d+)/){
			$ip = "$1.$2.$3.$4";
		}
	}
	
	if($ip =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/){
		($i1,$i2,$i3,$i4) = split(/\./,$ip);
		$p_name = pack("CCCC",$i1,$i2,$i3,$i4);
		($iname, @addr) = gethostbyaddr($p_name, 2);
	}

	$iname = $ip if(!$iname);
	return $iname;
}

# -------------------アクセス制限--------------------

sub punish{
	open(DB,"$punish_file") || &error(8);
	@p_line = <DB>;
	close(DB);
	$p_mode_ch = shift(@p_line);
	chop($p_mode_ch);
	$p_flag = 0;

	foreach (@p_line) {
		($pmv, $phv) = split(/\t/, $_);
		chop($phv) if($phv =~ /\n/);
		if(($p_mode_ch eq "1") && ($pmv eq "1")){
			if(($host =~ /$phv/) || ($xfor =~ /$phv/)){ &error(0); }
		}
		elsif(($p_mode_ch eq "2") && ($pmv eq "2")){
			if($host =~ /$phv/){ $p_flag = 1; }
		}
	}

	if(($p_mode_ch eq "2") && !$p_flag){ &error(12); }
	if($hua =~ /Iria/i){ &error(0); }
}

# ---------------------表示処理----------------------

sub read{
	$x_namez_cnt = @x_namez;
	if($x_namez_cnt){
		foreach (@x_namez){
		#	$eucname = $name;
		#	&jcode'convert(*eucname,'euc');
		#	&jcode'convert(*line,'euc');
			&error(14) if($name eq $_);
		}
	}
#	&getreferer if($kickrom);

	&lock(3) if($lock_check);
	open(IN, "$chat_file") || &error(1);
	@lines = <IN>;
	close(IN);
	&member if(!($ktai_ua && $FORM{'peephole'} && ($pass eq $password)));
	rmdir($lockfile) if($lock_check);

	$n_color =~ s/#//g;
	$s_color =~ s/#//g;
	$script =~ s/~/%7E/g;
	$realname = $name;
	$rom_name = $name;
	&encode($rom_name);
	$rom_name = '' if(!$romer && ($name eq $host));
	$relper = "$script?name=$rom_name&reload=$reload&window=$window&view=on&space=$space&line_ch=$line_ch&high=$high&sechide=$sechide&secret3=$secret3&n_color=$n_color&addalert=$FORM{'addalert'}&filecheck=$FORM{'filecheck'}";
	&http_header;
	$relper_nof = "&email=$email&s_color=$s_color&nof=on" if($nof);
	print qq(<META HTTP-EQUIV="Refresh" CONTENT="$reload;URL=$relper$relper_nof">\n) if($reload && !$ktai_ua);
	&css if(!$high || !$ktai_ua);
	$rb = " <span class=\"fface\">[<a href=\"$relper&relb=on\">Reload</a>]</span><br>\n" if($relb && !$nof);
	if($reloadcnt && !$high && $reload){
		print qq(<SCRIPT Language="JavaScript">\n<!--\nfunction timer\(\) {\n    cnt--;\n    var bar = cnt / 2;\n    block = "";\n);
		print qq(    while \(bar > 0\) { block += "■"; bar--; }\n    status = "リロードまで約" + cnt + "秒 " + block;\n);
		print qq(    if \(cnt > 4\) { Timecount = setTimeout\("timer\(\)",900\); }\n    else { clearTimeout\(Timecount\); status = "間もなくリロードします"; }\n}\n// -->\n</SCRIPT>);
		$reljs = " onLoad=\"cnt=$reload\;timer\(\)\"";
	}
	print qq(</head>$body2$reljs>\n);
	print qq($title) if(!$ktai_ua);
	print qq($rb);
	if(!$filesize_flag && $ktai_ua && $FORM{'filecheck'} && !$chat){
		print "<center>新規の発言はありません。新規の発言がある場合のみログを表\示します。<br>[<a href=\"$relper&ktaireload=on\" $accesskey=\"1\">1.Reload</a>]</center>\n";
		exit;
	}
	print qq(<SCRIPT LANGUAGE="JavaScript"><!--\nalert\("参加人数が変化しました。"\);\n// --></SCRIPT>\n) if($alert_flag && $FORM{'addalert'});

	if(!$high){
		$mem_num = @mem_num;
		if($mem_num eq "0"){ $mem_num = 'なし'; }
		else{ $mem_num = "$mem_num人 : @mem_num"; }
		if(!$romer){ $memt = '参加者'; }
		else{ $memt = 'Access'; }
		if($romcount && !$romer){
			$rom_num = @rom_num;
			if($rom_num eq "0"){ $rom_num = ' ROM なし'; }
			else{ $rom_num = " ROM $rom_num人"; }
		}
		else{ $rom_num = ''; }
		print qq( <font size="2">$memt $mem_num$rom_num</font>);
	}
	print qq(<hr size="1" class="line">) if(!$ktai_ua);

	if($nof eq "on"){
		require "$html_pl";
		&operation;
		print qq(<hr size="1" class="line">\n);
	}
	elsif($inlog_sec_send eq "on"){
		require "$html_pl";
		&inlog_sec_send;
		print qq(<hr size="1" class="line">\n);
	}
	if($ktai_ua && ($k_tairom ne "on")){
		if(!$exit){
			require "$html_pl";
			&i_operation;
			print qq(<hr size="1" class="line">\n);
		}
		else{
			print qq(<a href="$script" $accesskey="1">1.再入室</a>);
			print qq(/<a href="$home_k" $accesskey="2">2.HOME</a>) if($home_k);
			print qq(<br>\n);
		}
	}

	$i = 0;
	foreach $line (@lines){
		if(++$i > $window){ last; }
		($time_v, $host3_v, $hua2, $name, $email, $chat, $n_color, $s_color, $greet, $s_host_v, $s_host_n, $realhost) = split(/\t/, $line);
		chop($realhost) if($realhost =~ /\n/);
		&time;
		$secflag = '';
		if($email && ($email =~ /(.*)\@(.*)\.(.*)/)){
			$mb = "<a href=\"mailto:$email\"><font color=\"$mailb\" class=\"mb\">$mailb_mark</font></a>";
		}
		elsif($email && ($email =~ /http:\/\//)){
			$mb = "<a href=\"$email\" target=\"_blank\"><font color=\"$mailb\" class=\"mb\">$mailb_mark</font></a>";
		}
		else{
			$mb = "<font color=\"$mailb\" class=\"mb\">$mailb_mark</font>";
		}
		$flag_e = '';
		if($s_host_v eq "enter" || $s_host_v eq "exit"){
			$name = $notice;
			$n_color_ch = $notice_color;
			$mb = '';
			if($greet){
				$greet = "<font color=\"$n_color\"><b>『$greet』</b></font> ";
			}
			if($secret3){ $line = ''; --$i; next; }
			$flag_e = 1;

		}
		elsif($s_host_v){
			if($sechide){ $line = ''; --$i; next; }
			if(($s_host_v eq $host) && ($s_host_n eq $realname)){ $secflag = "さんから$s_host_nさんへの電報"; }
			elsif(($realhost eq $host) && ($s_host_v eq "ALL")){ $secflag = "：参加者全員への電報"; }
			elsif(($realhost eq $host) && ($realname eq $name)){ $secflag = "：$s_host_nさんへの電報"; }
			elsif(($s_host_v eq "ALL") && ($realname ne $host)){ $secflag = "さんから参加者全員への電報"; }
			else{ $line = ''; --$i; next; }
			$chat =~ tr/a-fA-F0-9/fedcbaFEDCBA9876543210/;
			$chat = &decode3($chat);
			$chat =~ tr/+/ /;
		}
		elsif($secret3){ $line = ''; --$i; next; }
		else{
			$greet = '';
		}
		if(!$flag_e){
			$n_color_ch = $n_color;
			$chat = "<font color=\"$s_color\">$chat</font>" if($saycolor_check);
		}
		# ログのホストアドレス表示
		if($ipcomout eq "1"){		# 入室・退室時のみ表示する
			if($hua2){
				$host3_v = " $host3_v";
			}else{
				$host3_v = '';
			}
		}
		elsif($ipcomout eq "2"){	# 常に表示する
			$host3_v = " $host3_v";
		}
		elsif($ipcomout eq "3"){	# コメントアウトしてソースに表示
			$host3_v = "<!-- $host3_v -->";
		}
		else{
			$host3_v = '';		# 条件にマッチしない場合は表示しない
		}
		$hua2 = " $hua2" if($hua2);
		$user = "$date_v$hua2$host3_v";
		$chat = "<b>$chat</b>" if($cs_markbold);
		if(!$high){
			$br = "<br><br>" if($space eq "on");
			print qq($mb<font color="$n_color_ch"><b>$name</b></font>$secflag &gt; $chat $greet<span class="fface">[$user]</span>$br);
			if($line_ch eq "on"){ print qq(<hr size="1" class="line">); }
			else{ print "<br>"; }
			print "\n";
		}
		else{
			$chat=~s/<[^>]*>//ig;
			$greet=~s/<[^>]*>//ig;
			print qq(<font color="$n_color_ch">$name</font>$secflag &gt; $chat $greet\[$date_i\]);
		#	print qq($name$secflag &gt; $chat $greet\[$date_i\]);
		#	print qq(<p>) if($space eq "on");
			if($line_ch eq "on"){ print qq(<hr size="1" class="line">); }
			else{ print "<br>"; }
			print "\n";
		}
	}
	print qq(<span class="fface">);
	if($reload){ print qq(Reload : $reload  ); }
	else{ print qq(Reload : off  )  }
	print qq(Line : $window</span>\n);
	# 下記の改変は禁止されています。
	print qq(<div align="right"><a href="http://www.teachat.org/" target="_blank"><span class="fface">TeaChat $version</span></a></div>);
	print qq(</body></html>);
	print qq($endhtml) if(!$ktai_ua);
}

# ---------------------記録処理----------------------

sub write{
	&getreferer if($firewall);
	$failure = 0;

	$chat2 = $chat;
	$host3 = $host;
	$host4 = $host3;
	&lieip if($lieip_check);
	if($enter eq "on" || $exit eq "on"){
		if($cs_notice_style eq "off"){
			if($greet){
				$chat = $greet;
				$time_v = $time;
				&time;
				&rank_write;
				&tag;
			}
			else{
				$failure = 1;
			}
		}
		else{
			if($name eq $host4){
				if($enter && $cs_nameconfirm){ &error(13); }
				else{ $name = "\[$host4\]"; }
			}

			foreach(@x_namez){
				&error(14) if($name eq $_);
			}

			srand($time);
			@enter_mes2 = @enter_mes;
			@leave_mes2 = @leave_mes;
			$em_num = @enter_mes2;
				$lm_num = @leave_mes2;
			$em_num2 = int(rand $em_num);
			$lm_num2 = int(rand $lm_num);

			if($enter eq "on"){
				$e1 = $enter_mes[$em_num2];
			}
			elsif($exit eq "on"){
				$e1 = $leave_mes[$lm_num2];
			}
			$e1 =~ s/&quot;/"/g;
			$e1 =~ s/&gt;/>/g;
			$e1 =~ s/&lt;/</g;
			$e1 =~ s/\[NAME\]/$name/g;
			$e1 =~ s/\[NAME_COLOR\]/$n_color/g;
			chop($e1) if($e1 =~ /\n/);

			$chat     = $e1;
			$s_host_v = 'exit';
			$hua2     = $hua;
		}
	}
	else{
		$time_v = $time;
		&time;
		&rank_write;
		&tag;
		if($face){ $chat = "$chat $face"; }
		if($s_host){
			($s_host_v, $s_host_n) = split(/:/, $s_host);
		}
		
		$echat = $chat;
		&jcode'convert(*echat ,'euc');
		&jcode'convert(*xwords ,'euc');
		$chat = "<font color=\"$err_color\">［発言の中に不正なキーワードが含まれています。削除されました。］</font>" if($cs_xwords && ($echat =~ /$xwords/i));

		if($s_host){
			$chat = &encode($chat);
			$chat =~ tr/a-fA-F0-9/fedcbaFEDCBA9876543210/;
		}
	}

	&lock(1) if($lock_check);

	open(IN, "$chat_file") || &error(1);
	@lines = <IN>;
	close(IN);

	@continuity_confirm = @lines;
	$lastline = shift(@continuity_confirm);
	($d1, $d2, $d3, $last_name, $d5, $last_comment, $d7, $d8, $d9, $d10, $d11, $last_realhost) = split(/\t/, $lastline);
	chop($last_realhost) if($last_realhost =~ /\n/);
	if($cs_chatconfirm && ($last_name eq $name) && ($last_comment eq $chat) && ($last_realhost eq $host)){
		$failure = 1;
	}

	if($clear_on && ($chat2 eq "clear")){
		foreach $line (@lines){
			($d1, $d2, $d3, $last_name2, $d5, $d6, $d7, $d8, $d9, $shv, $d10, $realhost) = split(/\t/, $line);
			chop($realhost) if($realhost =~ /\n/);
			if(($host eq $realhost) && ($shv ne 'exit') && ($last_name2 eq $name)){
				$line = '';
			}
		}
		$chat = "<font color=\"$err_color\">［発言者によって$nameさんの発言ログは消去されました。］</font>";
	}
	$allclear_flag = 0;
	$allclear_flag = 1 if($chat2 eq $cs_allclear);
	
	$chatstyle = "";
	
	if($FORM{'fontsize'} && ($FORM{'fontsize'} ne "3")){
		$chatstyle = " size=\"$FORM{'fontsize'}\"";
	}
	if($FORM{'fontfamily'} && ($FORM{'fontfamily'} ne "-")){
		$chatstyle .= " style=\"font-family: \'$FORM{'fontfamily'}\';\"";
	}
	if($FORM{'fontstyle'} && ($FORM{'fontstyle'} ne "-")){
		if($FORM{'fontstyle'} eq "iv"){
			$chatstyle .= " color=\"$bgcolor2\"";
		}
		elsif($FORM{'fontstyle'} eq "b"){
			$chat = "<b>$chat</b>";
		}
		elsif($FORM{'fontstyle'} eq "i"){
			$chat = "<i>$chat</i>";
		}
		elsif($FORM{'fontstyle'} eq "s"){
			$chat = "<strike>$chat</strike>";
		}
		elsif($FORM{'fontstyle'} eq "u"){
			$chat = "<u>$chat</u>";
		}
	}
	$chat = "<font$chatstyle>$chat</font>" if($chatstyle);
	
	$w_val = "$time\t$host3\t$hua2\t$name\t$email\t$chat\t$n_color\t$s_color\t$greet\t$s_host_v\t$s_host_n\t$host\n";

	if(!$failure){
		$i = 0;
		foreach $line (@lines){
			if(++$i > $log_max){ last; }
			push(@array, $line);
		}
		unshift(@array, $w_val);

		open(OUT, ">$chat_file") || &error(1);
		if(!$allclear_flag){
			print OUT @array;
		}
		close(OUT);
	}

	rmdir($lockfile) if($lock_check);
}

# -------------------タグチェック--------------------

sub tag{
	&tag_error if($chat =~ /<($x_tag)|($x_tag)>|<.*style=|<.*mailbox:|<.*width|<.*height/i);
	$chat =~ s/<a href=/<a target="_blank" class="userlink" href=/ig;
	$chat =~ s/((http[s]?:\/\/|ftp:\/\/)[\w\$\#\~\.\/\-\?\=\&\:\%\+\,]+)/<a href\=\"$1\" target\=\"\_blank\" class=\"userlink\">$1<\/a>/g unless $chat =~ /</i;
	$chat =~ s/([\w\.\-\_]+\@[\w\.\-\_]+\.[\w\.\-\_]+)/<a href\=\"mailto:$1\" class=\"userlink\">$1<\/a>/g unless $chat =~ /</i;

	@tags = split( /</ ,  $chat ); $dmy = shift(@tags);
	foreach $tag (@tags){
		$tag =~s/([^>]*)>(.*)/$1/;
		$tag =~s/^\/(.*)//;
		$tag =~s/^([^\s]*).*/$1/;
	}
	$chat.='>' if($chat=~/<\/$/);
	@tags = reverse(@tags);
	foreach $tag (@tags){
		next if($tag =~/(img|^hr$|^br$)/i);
		$chat.="</$tag>" if($tag);
	}
}

sub tag_error{
	$chat =~ s/</&lt;/g;
	$chat =~ s/>/&gt;/g;
	$chat = $chat . "<font color=\"$err_color\">［注意：そのタグは使用を禁止しております。ご了承下さい］</font>";
}

# --------------------時刻変換-----------------------

sub time{
	($sec, $min, $hour, $mday, $mon, $year, $wday) = localtime($time_v);
	$mon2 = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec') [$mon];
	$wday2 = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat') [$wday];
	$wday3 = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday') [$wday];
	$youbi = ('日','月','火','水','木','金','土') [$wday];
	$year2 = $year;
	$year2 = $year2 - 100;
	$year2  = sprintf("%.2d",$year2);
	if($year < 90){ $year = $year+2000; }
	else{ $year = $year+1900; }
	++$mon;
	$hour = sprintf("%.2d",$hour);
	if($hour > 11){
		$hour2 = $hour - 12;
		$ampm = "PM";
	}
	else{
		$hour2 = $hour;
		$ampm = "AM";
	}
	$min  = sprintf("%.2d",$min);
	$sec  = sprintf("%.2d",$sec);
	if($cs_timestyle eq "1"){
		$date_v = "$wday2 $mday $mon2 $year $hour:$min:$sec";
	}
	elsif($cs_timestyle eq "2"){
		$date_v = "$wday3, $mday-$mon2-$year $hour:$min:$sec";
	}
	elsif($cs_timestyle eq "3"){
		$date_v = "$year/$mon/$mday $hour:$min";
	}
	elsif($cs_timestyle eq "4"){
		$date_v = "$year/$mon/$mday $hour:$min:$sec";
	}
	elsif($cs_timestyle eq "5"){
		$date_v = "$year/$mon/$mday $hour2:$min:$sec $ampm";
	}
	elsif($cs_timestyle eq "6"){
		$date_v = "$year/$mon/$mday($youbi) $hour:$min";
	}
	elsif($cs_timestyle eq "7"){
		$date_v = "$year/$mon/$mday($youbi) $hour:$min:$sec";
	}
	elsif($cs_timestyle eq "8"){
		$date_v = "$year/$mon/$mday($youbi) $hour2:$min:$sec $ampm";
	}
	elsif($cs_timestyle eq "9"){
		$date_v = "$year年$mon月$mday日($youbi) $hour:$min";
	}
	elsif($cs_timestyle eq "10"){
		$date_v = "$year年$mon月$mday日($youbi) $hour:$min:$sec";
	}
	elsif($cs_timestyle eq "11"){
		$date_v = "$year年$mon月$mday日($youbi) $hour2:$min:$sec $ampm";
	}
	elsif($cs_timestyle eq "12"){
		$date_v = "$year2/$mon/$mday($wday2) $hour:$min:$sec";
	}
	$date_i = "$mon/$mday $hour:$min";
}

# -------------------ホスト名置換--------------------

sub lieip{
	$dmy = shift(@ctzline2);
	$dmy = shift(@ctzline2);
	$dmy = shift(@ctzline2);
	$dmy = shift(@ctzline2);
	$dmy = shift(@ctzline2);
	$dmy = shift(@ctzline2);
	$dmy = shift(@ctzline2);

	foreach $l_line (@ctzline2){
		($ip_email, $ip_host, $ip_new) = split(/\t/, $l_line);
		chop($ip_new) if($ip_new =~ /\n|\cM/);
		if($email =~ /$ip_email/ && $host3 =~ /$ip_host/){ $host3 = $ip_new; }
		chop($host3) if($host3 =~ /\n|\cM/);
	}
}
# ---------------設定ファイルオープン----------------

sub file_open{
	open(IN, "$ctz_file") || &error(7);
	@ctzline = <IN>;
	close(IN);
	@ctzline2 = @ctzline;

	$line_base	= shift(@ctzline);
	$line_frame1	= shift(@ctzline);
	$line_frame2	= shift(@ctzline);
	$line_function	= shift(@ctzline);
	$line_xwords	= shift(@ctzline);
	$enter_mes	= shift(@ctzline);
	$leave_mes	= shift(@ctzline);
	chop($line_base)	if($line_base =~ /\n/);
	chop($line_frame1)	if($line_frame1 =~ /\n/);
	chop($line_frame2)	if($line_frame2 =~ /\n/);
	chop($line_function)	if($line_function =~ /\n/);
	chop($line_xwords)	if($line_xwords =~ /\n/);
	chop($enter_mes)	if($enter_mes =~ /\n/);
	chop($leave_mes)	if($leave_mes =~ /\n/);

	($cs_title, $cs_home, $cs_bbs, $cs_log_max, $cs_rank_max, $cs_rank_mini, $cs_stdtime, $cs_home_k, $cs_allclear, $cs_chatmaxlength) = split(/\t/, $line_base);
	chop($cs_chatmaxlength) if($cs_chatmaxlength =~ /\n|\cM/);
	($cs_bgcolor, $cs_text, $cs_link, $cs_vlink, $cs_alink, $cs_hlink, $cs_background, $cs_logo, $cs_b_text, $cs_b_bgcolor, $cs_b_border, $cs_form_bg, $cs_form_tx, $cs_form_bd, $cs_icq_uin, $cs_icq_img, $cs_f_size, $cs_font) = split(/\t/, $line_frame1);
	chop($cs_font) if($cs_font =~ /\n|\cM/);
	($cs_notice, $cs_notice_color, $cs_bgcolor2, $cs_text2, $cs_link2, $cs_vlink2, $cs_alink2, $cs_background2, $cs_hlink2, $cs_mailb, $cs_mailb_mark, $cs_ipcolor, $cs_defa_space, $cs_err_color, $cs_markbold, $cs_ipsize, $cs_ipfont, $cs_ipcomout, $cs_timestyle, $cs_notice_style, $cs_hr_b_style, $cs_hr_b_color) = split(/\t/, $line_frame2);
	chop($cs_hr_b_color) if($cs_hr_b_color =~ /\n|\cM/);
	($cs_ctch, $cs_saycolor_check, $cs_secret_check, $cs_lieip_check, $cs_punish_check, $cs_clear_on, $cs_lock_check, $cs_reloadcnt, $cs_romer, $cs_romcount, $cs_xrom, $cs_firewall, $cs_kickrom, $cs_nameconfirm, $cs_chatconfirm, $cs_trueip) = split(/\t/, $line_function);
	chop($cs_trueip) if($cs_trueip =~ /\n|\cM/);
	($cs_xwords, $cs_x_tag, $cs_x_namez) = split(/\t/, $line_xwords);
	chop($cs_x_namez) if($cs_x_namez =~ /\n|\cM/);

	$title		= $cs_title;		$background2 	= $cs_background2;
	$home		= $cs_home;		$hlink2		= $cs_hlink2;
	$bbs		= $cs_bbs;		$mailb		= $cs_mailb;
	$log_max	= $cs_log_max;		$mailb_mark	= $cs_mailb_mark;
	$rank_max	= $cs_rank_max;		$ipcolor	= $cs_ipcolor;
	$rank_mini	= $cs_rank_mini;	$defa_space	= $cs_defa_space;
	$stdtime	= $cs_stdtime;		$err_color	= $cs_err_color;
	$bgcolor	= $cs_bgcolor;		$ctch		= $cs_ctch;
	$text		= $cs_text;		$saycolor_check = $cs_saycolor_check;
	$link		= $cs_link;		$secret_check	= $cs_secret_check;
	$vlink		= $cs_vlink;		$lieip_check	= $cs_lieip_check;
	$alink		= $cs_alink;		$punish_check	= $cs_punish_check;
	$hlink		= $cs_hlink;		$clear_on	= $cs_clear_on;
	$background	= $cs_background;	$lock_check	= $cs_lock_check;
	$logo		= $cs_logo;		$reloadcnt	= $cs_reloadcnt;
	$b_text		= $cs_b_text;		$ipcomout	= $cs_ipcomout;
	$b_bgcolor	= $cs_b_bgcolor;	$romer		= $cs_romer;
	$b_border	= $cs_b_border;		$romcount	= $cs_romcount;
	$form_bg	= $cs_form_bg;		$xrom		= $cs_xrom;
	$form_tx	= $cs_form_tx;		$firewall	= $cs_firewall;
	$home_k		= $cs_home_k;		$kickrom	= $cs_kickrom;
	$form_bd	= $cs_form_bd;		$trueip		= $cs_trueip;
	$notice		= $cs_notice;		$icq_uin	= $cs_icq_uin;
	$notice_color	= $cs_notice_color;	$icq_img	= $cs_icq_img;
	$bgcolor2	= $cs_bgcolor2;		$xwords		= $cs_xwords;
	$text2		= $cs_text2;		$x_tag		= $cs_x_tag;
						$x_namez	= $cs_x_namez;
	$link2		= $cs_link2;		$logo		=~ s/&quot;/"/g;
	$vlink2		= $cs_vlink2;		$logo		=~ s/&gt;/>/g;
	$alink2		= $cs_alink2;		$logo		=~ s/&lt;/</g;
	$icq_uin	=~ s/ //g;		$icq_img	=~ s/ //g;

	$title		= 'TeaChat'	if(!$title);		$vlink2		= '#663333'		if(!$vlink2);
	$log_max	= '1000'	if(!$log_max);		$alink2		= '#660066'		if(!$alink2);
	$rank_max	= '1000'	if(!$rank_max);		$hlink2		= '#660066'		if(!$hlink2);
	$rank_mini	= '5'		if(!$rank_mini);	$mailb		= '#663333'		if(!$mailb);
	$stdtime	= '0'		if(!$stdtime);		$mailb_mark	= '◆'			if(!$mailb_mark);
	$mailb_mark	=~ s/&quot;/"/g;
	$mailb_mark	=~ s/&gt;/>/g;
	$mailb_mark	=~ s/&lt;/</g;

	$bgcolor	= '#663333'	if(!$bgcolor);		$ipcolor	= '#663333'		if(!$ipcolor);
	$text		= '#fce9c3'	if(!$text);		$defa_space	= '0'			if($defa_space eq "");
	$link		= '#deb887'	if(!$link);		$err_color	= '#dc143c'		if(!$err_color);
	$vlink		= '#deb887'	if(!$vlink);		$cs_notice_style= 'on'			if(!$cs_notice_style);
	$alink		= '#deb887'	if(!$alink);		$ipcomout	= 2			if($ipcomout eq "");
	$hlink		= '#deb887'	if(!$hlink);		$secret_check	= '1'			if($secret_check eq "");
	$b_text		= '#fce9c3'	if(!$b_text);		$notice		= 'お知らせ'		if(!$notice);
	$b_bgcolor	= '#663333'	if(!$b_bgcolor);	$notice_color	= '#804040'		if(!$notice_color);
	$b_border	= '#fce9c3'	if(!$b_border);		$bgcolor2	= '#fce9c3'		if(!$bgcolor2);
	$form_bg	= '#ffffff'	if(!$form_bg);		$text2		= '#003366'		if(!$text2);
	$form_tx	= '#663333'	if(!$form_tx);		$link2		= '#663333'		if(!$link2);
	$form_bd	= '#fce9c3'	if(!$form_bd);		$cs_timestyle   = 1			if(!$cs_timestyle);
	$cs_ipsize	= '0.8'		if(!$cs_ipsize);	$cs_ipfont	= 'Osaka,Verdana'	if(!$cs_ipfont);
	$cs_hr_b_style	= 'solid'	if(!$cs_hr_b_style);	$cs_hr_b_color	= '#808080'		if(!$cs_hr_b_color);
	$cs_allclear	= 'allclear'	if(!$cs_allclear);	$cs_chatmaxlength = 1000		if(!$cs_chatmaxlength);
	$cs_font	= 'MS UI Gothic,Osaka'	if(!$cs_font);
	$cs_f_size	= '1.0'		if(!$cs_f_size);
	$logo		= '<font face="arial, helvetica"><b>TeaChat</b></font>' if(!$logo);
	$x_tag		= 'img|--|!--|applet|basefont|bgsound|body|em|embed|form|frame|head|html|iframe|input|isindex|listing|meta|nobr|noscript|object|plaintext|pre|script|span|table|xmp' if(!$x_tag);
	@x_namez	= split(/\|/, $x_namez);
	$enter_mes	= '<b><font size="4" color="[NAME_COLOR]">[NAME]</font><font color="#804040">さんが入室されました。</font></b>' if(!$enter_mes);
	$leave_mes	= '<b><font size="4" color="[NAME_COLOR]">[NAME]</font><font color="#804040">さんが退室されました。</font></b>' if(!$leave_mes);
	@enter_mes	= split(/\t/, $enter_mes);
	@leave_mes	= split(/\t/, $leave_mes);
}

# -------------------デコード処理--------------------

sub decode{
	if($ENV{'REQUEST_METHOD'} eq "POST"){ read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); }
	else{ $buffer = $ENV{'QUERY_STRING'}; }
	@pairs = split(/&/,$buffer);
	foreach $pair (@pairs) {
		($vn, $value) = split(/=/, $pair);
		$value =~ tr/+/ /;
		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
		$value =~ s/\t/ /g;
#		$value =~ s/\n//g;
#		$value =~ tr/\x0D\x0A//d;
		$value =~ s/\cM//g;
		$value =~ s/</&lt;/g;
		$value =~ s/>/&gt;/g;
		$value =~ s/"/&quot;/g;
		&jcode'convert(*value,$kcode);
		$FORM{$vn} = $value;
	}

	$cookie		= $FORM{'cookie'};	$name		= $FORM{'name'};
	$first		= $FORM{'first'};	$name		=~ s/ /_/g;
						$name		=~ s/:/：/g;
	$enter		= $FORM{'enter'};	$name		= $host if(!$name);
	$email		= $FORM{'email'};	$exit		= $FORM{'exit'};
	$email		= '' if(!(($email =~ /(.*)\@(.*)\.(.*)/) || ($email =~ /http:\/\//)));
	$email		=~ s/ /_/g;		$rank		= $FORM{'rank'};
	$view		= $FORM{'view'};	$cc		= $FORM{'cc'};
	$relb		= $FORM{'relb'};	$window		= $FORM{'window'};
	$change		= $FORM{'change'};	$window		= 20 if(!$window);
	$nof		= $FORM{'nof'};		$reload		= $FORM{'reload'};
	$nofent		= $FORM{'nofent'};	$reload		= 0 if(!$reload);
	$administer	= $FORM{'administer'};	$n_color	= $FORM{'n_color'};
	$pass		= $FORM{'pass'};	$n_color2	= $FORM{'n_color2'};
	$adexe		= $FORM{'adexe'};	$s_color	= $FORM{'s_color'};
	$n_color	=~ s/ //g;		$n_color2	=~ s/ //g;
	$s_color	=~ s/ //g;		$s_color	=~ s/\n//g;
	$n_color	= $text2 if($n_color  =~ /[^\w#]/);
	$n_color2	= $text2 if($n_color2 =~ /[^\w#]/);
	$s_color	= $text2 if($s_color  =~ /[^\w#]/);
	$unlock		= $FORM{'unlock'};	$greet		= $FORM{'greet'};
	$space		= $FORM{'space'};	$chat		= $FORM{'chat'};
	$chat		=~ s/\n//g;		$greet		=~ s/\n//g;
	$email		=~ s/\n//g;		$n_color2	=~ s/\n//g;
	$name		=~ s/\n//g;
	if(!$space){
		if($defa_space){ $space = "on" }
		else{ $space = "off" }
	}
	$s_host		= $FORM{'s_host'};
	$line_ch	= $FORM{'line_ch'};
	$line_ch	= "on" if(!$line_ch);
	if($FORM{'usetag'} eq "on"){
		$chat	=~ s/&lt;/</g;
		$chat	=~ s/&gt;/>/g;
		$chat	=~ s/&quot;/"/g;
	}
	$face		= $FORM{'face'};	$auto		= $FORM{'auto'};
	$face		=~ s/&lt;/</g;		$secret		= $FORM{'secret'};
	$face		=~ s/&gt;/>/g;		$e_host		= $FORM{'e_host'};
	$face		=~ s/&quot;/"/g;	$secret2	= $FORM{'secret2'};
	$face		= '' if($face eq '-');	$ncsame		= $FORM{'ncsame'};
	$hidden		= $FORM{'hidden'};	$high		= $FORM{'high'};
	$secret3	= $FORM{'secret3'};	$sechide	= $FORM{'sechide'};
	$customize	= $FORM{'customize'};	$ctz_exe	= $FORM{'ctz_exe'};
	$k_tairom	= $FORM{'k_tairom'};	$ktairank	= $FORM{'ktairank'};
	$xrom_flag	= $FORM{'xrom_flag'};	$inlog_sec_send	= $FORM{'inlog_sec_send'};
}
sub decode2{
	$n_color = $n_color2 if($n_color2);
	$n_color = "$text2" if(!$n_color);
	if(!$s_color){
		if($ncsame){ $s_color = $n_color; }
		else{ $s_color = $text2; }
	}
	elsif($s_color && $ncsame){ $s_color = $n_color; }
}

sub decode3{
	$_[0] =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	$_[0];
}

sub encode{
	$_[0] =~ s/([^0-9A-Za-z_ ])/'%'.unpack('H2',$1)/ge;
	$_[0] =~ s/\s/+/g;
	$_[0];
}

# --------------------参加者表示---------------------

sub member{
	open(LOG,"$mem_file") || &error(5);
	@lastmember=<LOG>;
	close(LOG);
	$filesize = -s $chat_file;
	$flag = 1;
	$alert_flag = 0;
	$now_mem = 0;

	foreach (@lastmember){ # mem.datのフラッシュ
		($mtime, $name2, $host2, $n_color3, $last_mem, $lastsize) = split(/\t/, $_);
		$lastsize =~ tr/\x0D\x0A//d;

		if($exit && ($name2 eq $name)){
			$flag = 0;
			next;
		# 退室する場合は自分のデータを削除。
		}
		if($time-300 > $mtime){
			next;
		# 5分以上前の参加者は削除
		}
		elsif(($host2 eq $host) || ($name2 eq $name)){
			push(@member,$_) if($flag);
			$flag = 0;
		# 5分以内の参加者で本人なら追加。ホスト、又は名前で本人判定
		}
		else{
			push(@member,$_);
		}
	}
	push(@member,"$time\t$name\t$host\t$n_color\t$now_mem\t$filesize\n") if($flag);
	# 参加者ファイルの中に自分がいない場合は追加

#	@member = grep($_, @member);

	foreach (@member){
		($mtime, $name2, $host2, $n_color3, $last_mem, $lastsize) = split(/\t/, $_);
		$lastsize =~ tr/\x0D\x0A//d;
		if(!$romer){	# 通常参加者表示時
			push(@mem_num,"<font color=\"$n_color3\">$name2</font> ") if($name2 ne $host2);
			# ホスト名と名前が異なる場合参加者配列にいれる
			push(@rom_num, "<font color=\"$n_color3\">$name2</font> ") if($name2 eq $host2);
			# ROMはROM表示用配列にいれる
		}
		else{	# Access表示時
			push(@mem_num,"<font color=\"$n_color3\">$name2</font> ");
			# $name2がホスト名(ROM)でも参加者配列にいれる（ROM表示モード）
		}
		if($secret || $inlog_sec_send){
			push(@sec_mem,"<option value=\"$host2\:$name2\">$name2\n") if(($name2 ne $host2) && ($name2 ne $name));
			# 自分を除く参加者を電報用参加者配列に加える
		}

		$now_mem++ if($name2 ne $host2);
		# 参加人数変化アラート用、人数カウント
	}

	foreach (@member){ # 現在人数とファイルサイズをフラッシュ
		($mtime, $name2, $host2, $n_color3, $last_mem, $lastsize) = split(/\t/, $_);
		$lastsize =~ tr/\x0D\x0A//d;
		if(($host2 eq $host) || ($name2 eq $name)){
			$alert_flag = 1 if($now_mem > $last_mem);
			# 人数の増加が確認できればアラート

			$filesize_flag = 1 if($filesize ne $lastsize);
			#ファイルサイズが異なっていたらフラグを立てる

			$_ = "$time\t$name\t$host\t$n_color\t$now_mem\t$filesize\n";
			# 現在人数を記録
		}
	}

	open(LOG,">$mem_file") || &error(5);
	print LOG @member;
	close(LOG);
}

# --------------発言ランキング書き込み---------------

sub rank_write{
	&lock(4) if($lock_check);

	open(RANK,"$rank_file") || &error(2);
	@rank=<RANK>;
	close(RANK);

	$rank2 = @rank;
	if($rank2 >= $rank_max){ shift(@rank); }

	$flag = 1;
	foreach $rank (@rank){
		($handle,$rvalue,$rtime) = split(/\t/, $rank);
		next if($handle ne $name);
		$rvalue++;
		$flag = 0;
		$rank = "$name\t$rvalue\t$time\n";
		last;
	}
	push(@rank,"$name\t1\t$date_v\n") if($flag);

	open(RANK, ">$rank_file") || &error(2);
	print RANK @rank;
	close(RANK);

	rmdir($lockfile) if($lock_check);
}

# ------------------スタイルシート-------------------

sub css{
	if($view eq "on" && !$exit && !$rank && !$secret){
		$link = $link2;
		$vlink = $vlink2;
		$alink = $alink2;
		$hlink = $hlink2;
		if($hua !~ /MSIE 3/i){
			$mbstyle = "a\:hover \{ color\: $hlink\; text-decoration\: underline\; \}\na\:hover \.mb \{ color\: $hlink\; text\-decoration\: underline\; \}";
		}
		else{
			$mbstyle = "a \.mb \{ color\: $mailb\; text\-decoration\: none\; \}";
		}
	}
	else{
		$mbstyle = "a\:hover \{ color\: $hlink\; text-decoration\: underline\; \}" if($hua !~ /MSIE 3/i);
	}

	$css_ie = "\n.i_text \{ color\: $form_tx\; background-color\: $form_bg\; border\: 1px solid $form_bd\; \}\n\.bttn \{ color: $b_text\; background\-color\:$b_bgcolor\; border\: 1px solid $b_border\; cursor: hand; \}";
	$css_ie = '' if($hua !~ /MSIE|Cuam|Opera/i);
	$cs_ipsize2 = $cs_ipsize."em";
	$cs_f_size2 = $cs_f_size."em";
	if($hua =~ /Opera/i){
		$linecss = "\n.line { border: 0px $cs_hr_b_style $cs_hr_b_color; margin: 7px; height: 1px; width: 100%; }";
	}
	elsif($hua =~ /MSIE/i){
		$linecss = "\n.line { border: 1px $cs_hr_b_style $cs_hr_b_color; margin: 1px; height: 0px; width: 100%; }";
	}
	elsif($hua =~ /Mac_PowerPC/i){
		$linecss = "";
	}

	print <<"EOF";
<style type="text/css"><!--
body { font-family: $cs_font; font-size: $cs_f_size2; }$linecss
.fface { color: $ipcolor; font-family: $cs_ipfont; font-size: $cs_ipsize2; }
a:link { color: $link; text-decoration: none; }
a:visited { color: $vlink; text-decoration: none; }
a:active { color: $alink; text-decoration: underline; }
$mbstyle
a.userlink:link { color: $link; text-decoration: underline; }
a.userlink:visited { color: $vlink; text-decoration: none; }
a.userlink:active { color: $alink; text-decoration: underline; }
a.userlink:hover { color: $hlink; text-decoration: none; }$css_ie
--></style>
EOF
}

# ----------------ファイルロック処理-----------------

sub lock{
	$lock = $_[0];
	$lockfile = $lockdir.$lock;

	while (!mkdir($lockfile, 0755)){
		if(--$retry <= 0){
			rmdir($lockfile); print "Status: 204\n\n";
		}
		sleep(2);
	}
}

sub unlock{
	rmdir($lockfile1);
	rmdir($lockfile2);
	rmdir($lockfile3);
	rmdir($lockfile4);
	rmdir($lockfile5);
}

# --------------------エラー処理---------------------

sub error{
	$msg = (
		'0',
		'チャットファイルが開けません',
		'ランキングファイルが開けません',
		'名前が入力されていません',
		'他ページからの書き込みは出来ません',
		'参加者ファイルが開けません',
		'カウンタファイルが開けません',
		'初期設定・ホスト置換ファイルが開けません',
		'アクセス制限ファイルが開けません',
		'参加者ファイルが開けません',
		'色データファイルが開けません',
		'顔文字・文字色ファイルが開けません',
		'現在、指定されたホスト以外のアクセスは許可されておりません',
		'名前が未入力です',
		'名前が不正です。入力された名前は現在禁止されています',
	)[$_[0]];

	if($msg eq "0"){ # アクセス制限の時
		print "Content-type: text/html\n\n";
		print qq(<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">\n<HTML><HEAD>\n<TITLE>403 Forbidden</TITLE>\n);
		print qq(</HEAD><BODY>\n<H1>Forbidden</H1>\nYou don't have permission to access /chat.cgi\n);
		print qq(on this server.<P>\n<HR>\n<ADDRESS>Apache/1.3.14 Server Port 80</ADDRESS>\n</BODY></HTML>);
		exit;
	}
	&http_header;
	&css;
	print qq(</head>\n);
	print qq($body>\n);
	print qq(<center><font size="4"><b>ERROR!</b></font><br><br>$msg</center>\n);
	print qq(</body></html>\n);
	exit;
}

