##########################################################################################
#
# スパムフィルタープラグイン（学習機能無し版）
#（bbsプラグインのオーバーライド）
#
##########################################################################################
use strict;

use plugin::bbs::BBSHandler;
use plugin::bbs::BBS2Handler;

# plugin::bbs::BBSHandlerのdo_actionメソッドの置き換え
package plugin::bbs::BBSHandler;

sub do_action {
	my $self = shift;
	my $wiki = shift;
	my $cgi  = $wiki->get_CGI;
	
	my $name    = $cgi->param("name");
	my $subject = $cgi->param("subject");
	my $message = $cgi->param("message");
	my $count   = $cgi->param("count");
	my $page    = $cgi->param("page");
	my $option  = $cgi->param("option");
	
	if($name eq ""){
		$name = "名無しさん";
	} else {
		# post_nameというキーでクッキーをセットする
		my $path   = &Util::cookie_path($wiki);
		my $cookie = $cgi->cookie(-name=>'post_name',-value=>$name,-expires=>'+1M',-path=>$path);
		print "Set-Cookie: ",$cookie->as_string,"\n";
	}
	
	if($subject eq ""){
		$subject = "無題";
	}
	
	if($page eq "" || $count eq ""){
		return $wiki->error("パラメータが不正です");
	} elsif($message eq ""){
		return $wiki->error("本文を入力してください。");
	}
	
#--------------------------------------------------------------------------------------------------
  if($message){
    unless(&plugin::_ex_spam_filter_light::Install::judgment_text($message)){
      my $time = Util::format_date(time());
      my $file = $wiki->config('log_dir')."/spam_bbs.txt";
      if(open(SPAM_LOG, ">>$file")){
        print SPAM_LOG "-" x 70 . "\n";
        print SPAM_LOG "$page:$subject - $name ($time)\n";
        print SPAM_LOG "-" x 70 . "\n";
        print SPAM_LOG "$message\n";
        close(SPAM_LOG);
      }
      my $error_message = $wiki->error("投稿された文章はスパムメッセージと判定されました。\n");
      $subject = Util::escapeHTML($subject);
      $message = Util::escapeHTML($message);
      $message =~ s/\n/<br>/g;
      $error_message .= "<p>\n";
      $error_message .= "<div>投稿された文章：</div>\n";
      $error_message .= "<div>＜件名＞</div>\n";
      $error_message .= "<div>$subject</div>\n";
      $error_message .= "<div>＜本文＞</div>\n";
      $error_message .= "<div>$message</div>\n";
      $error_message .= "</p>\n";
      return $error_message;
    }
  }
#--------------------------------------------------------------------------------------------------

	# フォーマットプラグインへの対応
	my $format = $wiki->get_edit_format();
	$name    = $wiki->convert_to_fswiki($name   ,$format,1);
	$subject = $wiki->convert_to_fswiki($subject,$format,1);
	$message = $wiki->convert_to_fswiki($message,$format);
	
	my @lines = split(/\n/,$wiki->get_page($page));
	my $flag       = 0;
	my $form_count = 1;
	my $content    = "";
	
	foreach(@lines){
		$content = $content.$_."\n";
		if(/^{{bbs\s*.*}}$/ && $flag==0){
			if($form_count==$count){
				$content .= "!!$subject - $name (".&Util::format_date(time()).")\n".
				            "$message\n";
				
				# no_commentオプション
				if($option eq "no_comment"){
					
				# reverse_commentオプション
				} elsif($option eq "reverse_comment"){
					$content .= "{{comment reverse}}\n";
				# デフォルト
				} else {
					$content .= "{{comment}}\n";
				}
				$flag = 1;
				
			} else {
				$form_count++;
			}
		}
	}
	if($flag==1){
		$wiki->save_page($page,$content);
	}
	
	# 元のページにリダイレクトする
	$wiki->redirect($page);
}


# plugin::bbs::BBS2Handlerのdo_actionメソッドの置き換え
package plugin::bbs::BBS2Handler;

sub do_action {
	my $self = shift;
	my $wiki = shift;
	my $cgi  = $wiki->get_CGI;
	
	my $bbsname = $cgi->param("bbsname");
	my $name    = $cgi->param("name");
	my $subject = $cgi->param("subject");
	my $message = $cgi->param("message");
	my $page    = $cgi->param("page");
	my $option  = $cgi->param("option");
	
	if($name    eq ""){
		$name    = "名無しさん";
	} else {
		# post_nameというキーでクッキーをセットする
		my $path   = &Util::cookie_path($wiki);
		my $cookie = $cgi->cookie(-name=>'post_name',-value=>$name,-expires=>'+1M',-path=>$path);
		print "Set-Cookie: ",$cookie->as_string,"\n";
	}
	
	if($subject eq ""){
		$subject = "無題";
	}
	
	if($bbsname eq ""){
		return $wiki->error("パラメータが不正です。");
	}
	if($message eq ""){
		return $wiki->error("本文を入力してください。");
	}
	
#--------------------------------------------------------------------------------------------------
  if($message){
    unless(&plugin::_ex_spam_filter_light::Install::judgment_text($message)){
      my $time = Util::format_date(time());
      my $file = $wiki->config('log_dir')."/spam_bbs2.txt";
      if(open(SPAM_LOG, ">>$file")){
        print SPAM_LOG "-" x 70 . "\n";
        print SPAM_LOG "$bbsname:$subject - $name ($time)\n";
        print SPAM_LOG "-" x 70 . "\n";
        print SPAM_LOG "$message\n";
        close(SPAM_LOG);
      }
      my $error_message = $wiki->error("投稿された文章はスパムメッセージと判定されました。\n");
      $subject = Util::escapeHTML($subject);
      $message = Util::escapeHTML($message);
      $message =~ s/\n/<br>/g;
      $error_message .= "<p>\n";
      $error_message .= "<div>投稿された文章：</div>\n";
      $error_message .= "<div>＜件名＞</div>\n";
      $error_message .= "<div>$subject</div>\n";
      $error_message .= "<div>＜本文＞</div>\n";
      $error_message .= "<div>$message</div>\n";
      $error_message .= "</p>\n";
      return $error_message;
    }
  }
#--------------------------------------------------------------------------------------------------

	# フォーマットプラグインへの対応
	my $format = $wiki->get_edit_format();
	$name    = $wiki->convert_to_fswiki($name   ,$format,1);
	$subject = $wiki->convert_to_fswiki($subject,$format,1);
	$message = $wiki->convert_to_fswiki($message,$format);
	
	my $pagename = $self->get_page_name($wiki,$bbsname);
	my $content = "!![[$subject|$pagename]] - $name (".&Util::format_date(time()).")\n".
	              "$message\n";
	
	# no_commentオプション
	if($option eq "no_comment"){
		
	# reverse_commentオプション
	} elsif($option eq "reverse_comment"){
		$content .= "{{comment reverse}}\n";
	# デフォルト
	} else {
		$content .= "{{comment}}\n";
	}
	$wiki->save_page($pagename,$content);
	
	# 元のページにリダイレクト
	$wiki->redirect($pagename);
}

1;
