#Copyrighy 2006 kisuku(nanato)

#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


######################
##葵CORE
package aoicore;
use Encode;

use utf8;

###### 葵CORE AKOSM2
# 文字列処理をするので、UTF8


#############設定項目#################

$munouname='葵上';	#人工無能の名前

$munouself='私';	#一人称

$haziki='消えろ、ボケナス';#禁止用語を検出した場合の返答。（できればもっと穏便な言葉に変更しましょう）

#禁止用語（カタカナは全角、アルファベット数字は半角)
@prohibitword = ('生挿入','自殺','身障','しこしこ','Ｈしたい','Ｈしよ','資料請求','副収入','アソコ','処女','童貞','エッチ','SEX','sex','セッ○ス','セックス','性交','なめたい','下半身','キス','乳首','乳輪','爆乳','貧乳','ちくび','おっぱぃ','生理','性器','クリト','ワレメ','ヴァギ','オ○ン','お○ん','オ○コ','お○こ','マン○','まん○','マ○コ','ま○こ','オマ○','おま○','オメコ','おめこ','マンコ','オマンコ','おまんこ','まんこ','膣','陰唇','おなにー','せんずり','オナニ','自慰','舐め','穴あき','しみパン','生脱ぎ','Tバック','パンティー','コスプレ','淫汁','マン汁','モミモミ','濡れ','ヌレヌレ','コ汁','こ汁','愛液',
'パイパン','カズノコ天井','ミミズ千匹','Gスポット','快感','オーガズム','エクスタシー','ヤリマン','やりまん','淫乱','乱交','尺八','ヤリ友','体位','騎乗位','クンニ','フェラチオ','フェラ','SM',
'パイズリ','もろだし','もろ出し','モロ出し','無修正','無修正動画','レイプ','熟女','人妻','亀頭','ペ○ス','ペニス','包茎','チ○コ','ち○こ','チ○ポ','ち○ぽ','チンチン','ちんちん','ちんこ','チンポ','ちんぽ',
'金玉','きんたま','勃起','ガマン汁','精液','射精','顔射','ざーめん','ザーメン',
'中出し','妊娠','コンドーム','避妊','ウンコ','うんこ','○んこ','う○こ','アヌス',
'ア○ス','アナル','ア○ル','盗聴','交際','援交','風俗','援助交際','情事','盗撮',
'浮気','不倫','出会い','子宮','うざい','ウザイ','うぜぇ','うぜえ','呪う','呪って',
'血を見る','死ね','ばーか','バーカ','バカ','馬鹿','殺す');




############ここまで#################



####
# _aoi_status.cgi
# [BEGIN]
# 最終アクセス時刻time(s)
# 前回発言のキーワード（topword）
# [END]



####
# _aoidata.cgi
# [BEGIN]
# 文字列　ex:単語,反応文,反応文
# ...
# [END]


#####　一口メモ
# @file1 キーワードファイル
# $key	ファイルに記録されている前回の文の重要語
# $topword 今回入力された文の重要語
# $topword2　葵の発言の重要語







sub engine{#$_[0]・・・名前 $_[1]・・・入力文字列 $_[2]・・・分解文字列 $_[3]・・・分解文字列 utf8フラグあり

$name=$_[0];
$msg=$_[2];#アルファベット英語の半角性を保証するため
$msg=~s/\&//g;
$wd=$_[2];
$wd_=$_[3];
utf8::decode($name);
utf8::decode($msg);
utf8::decode($wd);
utf8::decode($wd_);
$name=~s/\n//;
$msg=~s/\n//;
$wd=~s/\n//;
$wd_=~s/\n//;

#---end


#禁止用語チェック
if(&chkword($msg) eq 'bad'){&clearkey();utf8::encode($haziki);return $haziki;}

#大鎌君未動作
if($wd eq ''){&clearkey();return 'ERROR:JavaScript not running';}


#############ここよりファイル読み書き　ロック
&lock;


#--900 キーワードをロード
open(FDAT,'< _aoi_status.dat');
$prevtime=<FDAT>;
$key=<FDAT>;
close FDAT;
$key=~s/\n//;
utf8::decode($key);
if($prevtime+600<time){
	#10分以上たっているばあい、前のキーは無効。
	$key='';
}
#--900 end



#--901 データをロード
if(open(FDAT,'< _aoidata.cgi')==0){#※ファイルが無い時新規作成
	open(FDAT,'> _aoidata.cgi');
	close FDAT;
	chmod(0755,'_aoidata.cgi');
	open(FDAT,'< _aoidata.cgi');
	}
@file1=<FDAT>;
close FDAT;

	#--902 utf8::decode
	$a=0;
	foreach(@file1){
		utf8::decode($file1[$a]);$a++;
	}
	#--902 end

#--901 end


#--903 今回の入力された文を記録
	
	$topword=&topword($wd,$wd_);#今回入力された文の重要語
	
	$sentence=$wd;
	$sentence=~s/\&//g;
	$sentence=~s/$topword/\&$topword\&/;
	#置換 例：私は&アイヤイヤ&です

	#--904 前回発言の重要語が存在すれば、それをキーとして辞書登録
	if($key ne ''){
		
		$a=0;
		$flag=0;
		
		foreach(@file1){
			if($_ =~/^$key\,/ && $_ !~/$sentence/){ #重要語があり、同じ文が登録されていなければ。
				$file1[$a]=~s/\n//;
				$file1[$a].=','.$sentence."\n";
				$flag=1;
			}
			$a++;	
		}
		
		if($flag==0){	#重要語が登録されていなければ、新規登録
			$file1[$a]=$key.','.$sentence."\n";
		}	
	
	}else{
	
	#$keyが空なら登録作業を行わない
		
	}
	#--904 end

#--903 end


#--906 発言設定 $ret[0]設定 自分の$topword2設定
$topword2='';
@ret='';
$ret_=0;
	#--907 本文に含まれているキーワードに対応するメッセージ群からひとつ選択
	foreach(@file1){
	$_=~/^(.+?)\,/;
	$tmp1=$1;
	#$tmp1=~s/\&//g;
	if($msg =~/$tmp1/){
		@tmp=split(/\,/,$_);
		$tmp2=@tmp;
		$ret[$ret_]=$tmp[int(($tmp2-1)*rand()+1)];
	#open(FDBG,'>>debug.txt');print FDBG $ret[$ret_]."\n";close FDBG;#デバッグモード
			$ret_++;

	}		
	}
	#907 end
	

$ret_n=@ret;
$ret[0]=$ret[int($ret_n*rand())];#さらにメッセージをひとつにしぼる
$ret[0]=~/\&(.+?)\&/;
$topword2=$1;
$ret[0]=~s/\&//g;
$ret[0]=~s/\n//g;	#ver 1.02


#--906 end


#--907 データからUTF8フラグを削除
$a=0;
while($file1[$a] ne ''){
	utf8::encode($file1[$a]);
	$a++;
}
#--907 end


#--905 データをセーブ

open(FDAT,'> _aoidata.cgi');
print FDAT @file1;
close FDAT;

open(FKEY,'> _aoi_status.dat');
print FKEY time."\n";
if($topword2 eq ''){$q=$topword."\n";}else{$q=$topword2."\n";}
utf8::encode($q);
print FKEY $q;
close FKEY;
#--905 end

#ファイル操作終わり　unlock
&unlock;


	

#名前を置換
	$ret[0]=~s/$munouname/$munouself/g;
	$ret[0]=~s/俺/$munouself/g;	#ver 1.02
	$ret[0]=~s/私/$munouself/g;
	$ret[0]=~s/僕/$munouself/g;





utf8::encode($ret[0]);#返答文encode ver1.03で出力にフラグあり ver1.04フラグ無し

 return $ret[0];
}##ver02ここまで


#############################################
###################　ひとつのキーワードを探す topword
#############################################

sub topword{
# $_[0]・・・区切り文字列
# $_[1]・・・区切り数列
#return $topword

#入力文字列整理、$topword選出、時間確認
my @res=split(/\&/,$wd);
my @res_=split(/\&/,$wd_);
my @keyword,$topword;

	#--001 キーワードっぽいのを抜き出し（複数）
	my $a=0,$b=0;
	foreach(@res_){
		if(($_==2 && length($res[$a])>=3 )||
			($_==4 && length($res[$a])>=3) ||
			($_==99 && $res[$a] ne '私')){#カタカナ、漢字、英語
		
		 $keyword[$b]=$res[$a];$b++;
		}
		$a++;		
	
	}
	#--001 end

	#--002　最有力をひとつ
	my $length=0;#文字長
	foreach(@keyword){	#topword設定（一番長いワード）
		if(length($_) > $length){$length=length($_);$topword=$_;}
	}
	#--002 end
	
	
	#--003 漢字、カタカナ、英語のキーワードがみつからない時、ひらがな検索	
	if($topword eq ''){
		
		@keyword='';#初期化
		
		#--004 複数検索
		$a=0;$b=0;
		foreach(@res_){
			if($_==3 && length($res[$a])>=3 && length($res[$a])<=8){#ひらがな
				$keyword[$b]=$res[$a];$b++;
			}
			$a++;
		}
		#--004 end
		
		#--005 ひとつに絞込み
		$length=0;
		foreach(@keyword){	#topword設定（一番長いワード）
		if(length($_) > $length){$length=length($_);$topword=$_;}
		}
		#--005 end		
	    	
	}#--end 003
			
	return $topword;

}

#########################################
###################設定キーワードを空にする
########################################
sub clearkey{

	open(FKEY,'> _aoi_status.dat');
	binmode(FKEY,':utf8');
	print FKEY time."\n";
	print FKEY "\n";
	close FKEY;

}


############################################
sub lock{


	for($ii=0; $ii<=7; $ii++,sleep 1){
	if(rename('lockfile_aoi','lockfile_aoi_')){return;}
	}
	#１０秒間リトライして反応がないと切れて強制執行
	
	
	
	}

sub unlock{
	rename('lockfile_aoi_','lockfile_aoi');
	}

############################################

############################################
###############　禁止用語　#################
############################################
############################################

sub chkword{
# $_[0]　入力文                     
# return 'ok' or 'bad'

my $w=$_[0];


foreach(@prohibitword){
	if($w =~/$_/){return 'bad';}

}

return 'ok';



}


1;
