#########################
#    name: decyphile	#
#     author: jasper 	#
# 	 aim_bot 	#
# 	  v3.4	 	#
#    10/4/02 7:03PM	#
#########################
require 'decyphile.lib';
use Time::localtime;

#########################
#bot globals
my %stats=('im_recv' => 0,
	   'chat_recv' => 0,
	   'warns' => 0,
	   'errors' => 0,
	   'chat_joins' => 0,
	   'chat_leaves' => 0,
	   'invites_acc' => 0,
	   'invites_dec' => 0,
	   'last_im' => 'none',
	   'last_chat' => 'none'); #bot statistics
my %seen=(); #for .seen; contains time
my %talk=(); #for .lastspoke; contains time
my %said=(); #used in .lastspoke; contains what they said last
my $mylevel = 0; #bots warning level
my $flood = 0; #used for flood control
my %proxy=(); #used for the primitive 'Instant Message Proxy'

#########################
#on instant message
sub on_im {
	my ($self, $evt, $from, $to) = @_;
  	my $args = $evt->args();
  	my ($nick, $auto_msg, $msg) = @$args;
	my ($arelem,$key,$value);
	my $mynick = $self->get('my_name');
	
	if ($nick eq $mynick) { return; }
	my $block = &check_block($nick);
	if ($block > 0) { return; } 
	
	chomp($msg);
	my $stripped = $msg;
	$stripped =~ s/<[^>]+>//g; #strip html from $msg
	$stripped =~ s/^\s+//g;
		
	my @parameter=();
   	@parameter = &get_para($stripped);
   	my ($p1, $p2, $p3) = @parameter;   	
	my ($cmd) = ($stripped =~ /\.(\S+)\s*(\S*)/);
	$cmd = '.'.$cmd; 	
   	 	
   	$stats{'last_im'} = "$nick: $stripped";
   	$stats{'im_recv'}++;
   	$seen{$nick} = ctime();
   	$talk{$nick} = ctime();
   	my $lastsaid = $said{$nick};
   	$said{$nick} = $stripped;
   	
   	my $mchk = &check_master($nick);
   	if ($mchk > 0) {
   		$stats{'mast_im_recv'}++;
   		if ($cmd eq ".rmproxy") { #remove proxy
   			delete $proxy{$nick};
   			$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>Removed IM Proxy for $nick");	
   			return;
   		}
   		if ($cmd eq ".proxy") { #add proxy
   			$proxy{$nick} = $p1;
   			#$proxy = &start_proxy($nick,$p1);
   			
   			$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>IM Proxy set for $p1");	
   			return;	
   		}
   		if ($cmd eq ".commands") {
			$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>A full command reference can be found <a href=http://jasper.wintermarket.org/shitz/files/commands_ref.html>here</a>.");		
   			return;
		}
   		if ($cmd eq ".block") {
   			if (defined($p1)) {
   				open(BLK,">>block.txt");
   					print BLK "$p1:";
   				close(BLK);
   				$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>$p1 is blocked!");	
   				$self->add_deny(my $bool,"Buddys",$p1);	
   				return;
   			}
   		}
   		if ($cmd eq ".8ball") {
			if (defined($p1)) {
				my $ball = &eightball();
				$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>$ball");	
				return;
			}
		}
   		if ($cmd eq ".teach") {
   			if (defined($p1)) {
   				if (defined($p2)) {
   					if(open(RESP,">>responses.txt")) {
   						$p1 =~ s/\)/\\)/;
						$p1 =~ s/\(/\\(/;
						$p1 =~ s/\*/./;
   						print RESP "$p1->$p2\n";
   						close(RESP);
   						$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>I now know $p1->$p2!");
   						return;
   					}
   				}
   			}
   		}
   		if ($cmd eq ".phrase") {
   			my $phrase = &phrase($nick);
   			$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>$phrase");
   			return;
   		}
   		if ($cmd eq ".info") {
   			if (defined($p1)) {
   				$self->set_info("$p1");
				$self->send_config();
				$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>info set to <b>$p1</b>!");
   				return;
   			} else {
   				my $ver = $self->get('version');
   				$self->set_info("Name: $ver -- By: Jasper<br>WWW: <a href=\"http://www.wintermarket.org/~jasper/\">non-self on-line</a>");
				$self->send_config();
				$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>info set to default!");
   				return;
   			}
   		}
   		if ($cmd eq ".addphrase") {
   			if (defined($p1)) {
   				if (open(P,">>phrases.txt")) {
   					chomp($p1);
   					print P $p1.'\n';
   					close(P);
   					$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK><b>Added</a> \'$p1\' to phrases!");
   					return;
   				}
   			}
   		}
   		if ($cmd eq ".stats") {
   			$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK><br><u><b>[</b></u>Decyphile<u><b>-</b></u>Bot<u><b>-</b></u>Statistics<u><b>]</b></u><br>Master IMs RECV: $stats{'mast_im_recv'}<br>IM RECV: $stats{'im_recv'}<br>CHAT MSG RECV: $stats{'chat_recv'}<br>WARNS TAKEN: $stats{'warns'}<br>ERRORS: $stats{'errors'}<br>CHAT JOINS: $stats{'chat_joins'}<br>CHAT LEAVES: $stats{'chat_leaves'}<br>INVITES ACCEPTED: $stats{'invites_acc'}<br>INVITES DECLINED: $stats{'invites_dec'}<br>LAST IM RECV: $stats{'last_im'}<br>LAST CHAT MSG RECV: $stats{'last_chat'}");
   			return;
   		}
   		if ($cmd eq ".ppl") { #lists people bot has encountered
 			my @keys=();
 			my ($val,$ppl,$num);
 			@keys = keys(%seen);
 			$num = @keys;
 			if ($num > 1) {
 				foreach $val (@keys) {
 					chomp($val);
 					$ppl .= ", ".$val;
 				}
 				$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>I have encountered <b>$num</b> different people!<br>They are: <b>$ppl</b>");
 				return;
 			} else {
 				$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>I have encountered just one person, and that person is <b>$ppl</b>.");
 				return;
 			}
 		}
   		if ($cmd eq ".lastspoke") {
   			if (defined($p1)) {
   				if (exists $talk{$p1}) {
   					my $ttime = $talk{$p1};
   					my $spoke = $said{$p1};
   					$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>The last time I heard <b>$p1</b> speak was <b>$ttime</b>, saying <b>\'$spoke\'</b>.");
   					return;
   				} else {
   					$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>I don't remember hearing <b>$p1</b>!");
   					return;
   				}
   			}
   		}
   		if ($cmd eq ".seen") {
   			if (defined($p1)) { 
   				if (exists $seen{$p1}) {
   					my $stime = $seen{$p1};
   					$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>The last time I saw <b>$p1</b> was <b>$stime</b>.");
   					return;
   				} else {
   					$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>I don't remember seeing <b>$p1</b>!");
   					return;
   				}
   			}
   		}
   		if ($cmd eq ".md5") {
   			if (defined($p1)) {
   				my $hash = &md5_64hash($p1);
   				$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK><b><u>[</u></b>MD5<b><u>]</u></b> $hash");
   				return;
   			}
   		}
   		if ($cmd eq ".raw") { #send raw toc data
   			$stats{'raw'}++;
   			if (defined($p1)) {
    				my $conn = $self->getconn();
    				return;
    			}	
   		}
		if ($cmd eq ".idle") { #set idle time
			$stats{'idle'}++;
			if (defined($p1)) {
				my $nconn = $self->getconn();
				if (defined($nconn)) {
					$p1 = int($p1);
					$nconn->set_idle($p1);
					$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>idle set to <b>$p1</b>"); 
					return;
				} else {
					$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>unable to set idle!"); 
					return;
				}
    			}
		}
		if ($cmd eq ".commands") {
			$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>A complete command reference can be found here: http://jasper.wintermarket.org/shitz/files/commands_ref.html");	
		}
		if ($cmd eq ".warn") { #warn user through bot (ie: .warn J4SP3R,4
			$stats{'warn'}++;
			if (defined($p1)) {
    				if (!defined($p2)) {
    					$self->evil($p1);
    					$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>warned $p1 with <b>5%</b>!");
    					return;
    				} else {
					for (my $i=1; $i < $p2; $i++) {
    						$self->evil($p1);
						sleep(2);
					}
					$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>warned $p1 $p2 times, with <b>5%</b> per warning!");
    					return;
    				}
    			}
		}
		if ($cmd eq ".im") { #send im through bot *.im riderxrr650,y0?*
			$stats{'im'}++;
			if (defined($p1)) {
    				if (defined($p2)) {
    					$self->send_im($p1,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>$p2");
    					$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>sent $p2 to $p1!");
    					return;
    				}
    			}
		}
		if ($cmd eq ".listrooms") { #query room lists
			$stats{'listrooms'}++;
			my @rooms=();
			my ($val,$ret,$roomslen);
			@rooms=$self->list_rooms();
			$roomslen=@rooms;
			if ($roomslen > 0) {
				foreach $val (@rooms) {
					$ret .= "$val, ";
				}
				$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>I'm in the following chat rooms: <b>$ret</b>.");
				return;
			} else {
				$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>I'm not in any chat rooms!");
				return;
			}
		}
		if ($cmd eq ".getroom") {
			$stats{'getroomid'}++;
			if (defined($p1)) {
				my $name=$self->get_roomname($p1);
    				$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>The ID for the RoomName \'$p1\' is \'$name\'");	
				return;
			}
		}
   		if ($cmd eq ".join") { #have bot join room
   			$stats{'join'}++;
   			if (defined($p1)) {
   				$self->chat_join($p1);
   				$self->chat_invite($p1,"...",$nick);
   				$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>joining $p1...");
   				return;
   			}
   		}
   		if ($cmd eq ".invite") { #have bot invite sn to room
   			$stats{'invite'}++;
   			if (defined($p1)) {
    				if (defined($p2)) { $p2=$nick; }
   				$self->chat_invite($p2,"...",$p1);
   			}
   		}
   		if ($cmd eq ".die") { #kill bot
   			$stats{'die'}++;
   			$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>im dying...");
			exit;
   		}
   		if ($cmd eq ".addmaster") { #add sn to masters list
   			$stats{'addmaster'}++;
   			if (defined($p1)) {
    				if (open(MAS,">>masters.txt")) {
       					print MAS $p1.'\n';
    					close(MAS);
   					$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>$p1 <b>added</b> to masters list<u>!</u>");
   				}
   			}
   		}
   		if ($cmd eq ".leave") { #have bot leave room
   			$stats{'leave'}++;
   			if (defined($p1)) {
   				$self->chat_leave($p1);
   				$self->del_roomname($p1);
   				delete $rooms{$name};
   				$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>I left $p1<u>!</u>");	
   			}
   		}
   		if ($cmd eq ".v") { #version info
   			$stats{'v'}++;
   			my $ver = $self->get('version');
   			$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>$ver -- By: Jasper");	
   		}
   		if ($cmd eq ".away") { #set away message
   			$stats{'away'}++;
   			if (defined($p1)) {
    				my $conn = $self->getconn();
    				if (defined($conn)) {
    					$conn->set_away($p1);
    					$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>Away: $p1");	
    				} else {
    					$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>unable to set away!");	
    				}
    			} else {
    				my $conn = $self->getconn();
    				if (defined($conn)) {
    					$conn->set_away("this little bot has important buissiness to do.");
    					$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>Away: this little bot has important buissiness to do.");	
    				} else {
    					$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>unable to set away!");	
    				}
    			}
   		}
   		if ($cmd eq ".leet") {
   			$stats{'leet'}++;
   			if (defined($p1)) {
    				my $leet=&leet($p1);
   				$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK><b><u>[</u></b>l<b>33</b>tish<b><u>]</u></b> $leet");	
   			}
   		}
   		if ($cmd eq ".sleep") { #sleep for number of seconds
   			$stats{'sleep'}++;
			if (!defined($p1)) { $p1=1; }
    			$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>Sleeping for <b>$p1</b> seconds...");
    			$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>Good Night!");
    			my $slept = sleep($p1);
    			$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>Done Sleeping!   Slept for <b>$slept</b> seconds!");	  
   		}
   		if ($cmd eq ".echo") {
   			$stats{'echo'}++;
   			$self->send_im($nick,$msg." <u>-</u>><b>ECHO RESPONSE FOR $nick!</b><<u>-</u>");
   		}
   	} else {
   		if (length($stripped) >= 20) { return; } #we dont like long messages
   		if (my $rbot = $stripped =~ /^bot:(\w+)?/) {
   			$self->send_im($rbot,"$nick has a warning level > 50%. So use me instead!");
   			return;
   		}
   		if (my $st = $self->get('redirect')) {
      			$self->send_im($st, "<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK><b>$nick:</b></FONT></BODY></HTML> $msg");
   		}
   		
   		if ($mylevel >= $self->get('warn_lvl')) {
   			my $bot = $self->get('spare_bot');
   			$self->send_im($bot,"bot:$nick");
   			return;
   		}
   		my $warn_retval = &check_warner($nick);
   		if (defined($warn_retval) > 0) {
   			$self->evil($nick);
   			return;
   		}
   		
   		my @parameter=();
   		@parameter = &get_para($stripped);
   		my ($p1, $p2, $p3) = @parameter;   	
		my ($cmd) = ($stripped =~ /^\!(\S+)\s*(\S*)/);
		$cmd = '!'.$cmd;

		if ($cmde eq "!commands") {
			$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>A full command reference can be found <a href=http://jasper.wintermarket.org/shitz/files/commands_ref.html>here</a>.");	
			return;
		}
		if ($cmd eq "!leet") {
   			$stats{'leet'}++;
   			if (defined($p1)) {
    				my $leet=&leet($p1);
   				$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK><b><u>[</u></b>l<b>33</b>tish<b><u>]</u></b> $leet");	
   				return;
   			}
   		}
		
		if ($cmd eq "!lastspoke") {
   			if (defined($p1)) {
   				if (exists $talk{$p1}) {
   					my $ttime = $talk{$p1};
   					my $spoke = $said{$p1};
   					$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>The last time I heard <b>$p1</b> speak was <b>$ttime</b>, saying <b>\'$spoke\'</b>.");
   					return;
   				} else {
   					$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>I don't remember hearing <b>$p1</b>!");
   					return;	
   				}
   			}
   		}
   		if ($cmd eq "!seen") {
   			if (defined($p1)) { 
   				if (exists $seen{$p1}) {
   					my $stime = $seen{$p1};
   					$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>The last time I saw <b>$p1</b> was <b>$stime</b>.");
   					return;
   				} else {
   					$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>I don't remember seeing <b>$p1</b>!");
   					return;
   				}
   			}
   		}
			
   		if ($cmd eq "!8ball") {
			if (defined($p1)) {
				my $ball = &eightball();
				$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>$ball");	
				return;
			}
		}	
   		
   		my $resp = $self->get('smart_resp');
   		if ($resp == 1) {
   			if ($stripped eq $lastsaid) { #user is repeating his/herself
   				$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>quit repeating yourself! I heard you the first time.");	
   				return;
   			}
			$arelem = &get_response($stripped,$nick);
   			if (length($arelem) == 0) { $arelem = &phrase($user); }
   		} else { $arelem = &phrase($nick); }
   		sleep(1);
   		$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>$arelem");
   		
   		if (my $log_path = $self->get('log_path')) {
   			open(USRLOG,">>$log_path/$nick.txt");
   			print USRLOG ctime()."\t$nick: $stripped\n";
   			print USRLOG ctime()."\t$mynick: $arelem\n";
   			close(USRLOG);
   		}
   	}
   	#instant message proxy stuff
   	while (($key,$value) = each(%proxy)) {
   		if ($nick eq $key) { #send message to other user
   			$self->send_im($value,$msg);
   		}
   		if ($nick eq $value) { #got message from other user
   			$self->send_im($key,$msg);
   		}
   	} 
}
#########################
#on nick
sub on_nick {
	my ($self, $evt, $from, $to) = @_;
	my $args = $evt->args();
	my $nick = $args->[0];
	print "Bot Logged on Successfully!\nBot Screenname: $nick\n";
}
#########################
#on config
sub on_config {
	my ($self, $evt, $from, $to) = @_;
	
	my $str = shift @{$evt->args()};
	$self->set_config_str($str, 1);
	
	my $ver = $self->get('version');
	$self->set_info("$ver -- By: <a href=\"aim:goim?screenname=J4SP3R&message=hey! I like your bot!\">Jasper</a><br>WWW: <a href=\"http://jasper.wintermarket.org\">http://jasper.wintermarket.org</a>");
	
	my $conn = $self->getconn();
	if (defined($conn)) {
		#srand(time);
		my $rnd = int(rand(199) + 1);
		$conn->set_idle($rnd);
		if (my $away = $self->get('away_always')) {
			if ($away != 0) {
				my $away_msg = $self->get('away_msg');
				if (!defined($away_msg)) { $away_msg = "fock joo. im away!"; }
				$conn->set_away("$away_msg");
			}
		}				      		      	     
	}
	if (my $room = $self->get('auto_join')) {
		$self->chat_join($room); 
	}
	$self->send_config();
}
#########################
#on error
sub on_error {
	my ($self, $evt) = @_;
	my ($error, @stuff) = @{$evt->args()};
	my $errstr = $evt->trans($error);
   
	$errstr =~ s/\$(\d+)/$stuff[$1]/ge;
	$stats{'errors'}++;
	
	open(ERR,">>errors.txt");
		print ERR ctime().": $errstr\n";
	close(ERR);
	if ($errstr =~ /An unknown signon error has occurred 17/) { exit; }
	if ($errstr =~ /You have been removed from the room for scrolling/) { 
		#chat rejoin code...
		#not yet =(
	}
	if ($errstr =~ /are exceeding the server speed limit/) {
		#srand(time);
		my $sleep = int(rand(10) + 5);
		sleep($sleep);
	}
}
#########################
#when we leave chat
sub on_chat_left {
	my ($self, $evt) = @_;

	my ($id) = @{$evt->args()};
	my $name = $self->get_roomname($id);
	$self->del_roomname($id);
	delete $rooms{$name}; #delete room from rooms hash
}
#########################
#when we join chat
sub on_chat_join {
	my ($self, $evt) = @_;
	my ($id, $name) = @{$evt->args()};
	
	$self->set_roomname($id, $name);
}
#########################
#when someone chats
sub on_chat {
	my ($self, $evt) = @_;
	my ($id, $user, $whisper, $msg) = @{$evt->args()};
	my ($conn,$chat);
	chomp($msg);
	my $stripped = $msg;
	my $chatname = $self->get_roomname($id);
	my $mynick = $self->get('my_name');
	if ($user eq $mynick) { return; }
	
	my $spent = time - $chat;
	$chat = time;
	if ($spent <= 30) { return; } #flood control

	$stripped =~ s/<[^>]+>//g; #strip html from $msg
	$stripped =~ s/^\s+//g; #  <--same--^
	
	#replace special characters
   	#srand(time); 
   	my $number=int(rand(9999)+1);
   	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime(time); $mon++;
   	my $year = sprintf("%02d", $year % 100);
   	my $rndstr = &rndstring(10);
      	$stripped =~ s/%n/$nick/g;
   	$stripped=~ s/%r/$number/g;
   	$stripped =~ s/%t/$hour:$min:$sec/g;
   	$stripped =~ s/%d/$mon-$mday-$year/g;
   	$stripped =~ s/%s/$rndstr/g;

	my $action = 'said';
	my $action = 'whispered' if ($whisper =~ /T/i);
	
	$stats{'last_chat'} = "$chatname-> $user: $stripped";
   	$stats{'chat_recv'}++;
   	$seen{$user} = ctime();
   	$talk{$user} = ctime();
   	my $lastsaid = $said{$nick};
   	$said{$user} = $stripped;
 	
 	$chat = $self->get('chat_onoff');
 	if ($chat == 1) {
		#srand(time); 
   		my $number = int(rand(11)+1);
   		if ($number == 1 ||$number == 5 || $number == 7 || $number == 9 || $number == 11) { 
   		my $resp = $self->get('smart_resp');
   			if ($resp == 1) {
   				if ($stripped eq $lastsaid) { #user is repeating his/herself
   					$self->send_im($nick,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>quit repeating yourself! I heard you the first time.");	
   					return;
   				}
				$arelem = &get_response($stripped,$user);
				if (length($arelem) == 0) { $arelem = &phrase($user); }
   			} else { $arelem = &phrase($nick); }
   			sleep(1);
   			$self->chat_send($id,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>$arelem");
   			
   			if (my $log_path = $self->get('log_path')) {
   				open(USRLOG,">>$log_path/$chatname.txt");
   				print USRLOG ctime()."\t$user: $stripped\n";
   				print USRLOG ctime()."\t$mynick: $arelem\n";
   				close(USRLOG);
   			}

   		}
   	}	   	
   	my $mchk = &check_master($user);
   	if ($mchk == 0) {
   		my @parameter=();
   		@parameter = &get_para($stripped);
   		my ($p1, $p2, $p3) = @parameter;   	
		my ($cmd) = ($stripped =~ /^\!(\S+)\s*(\S*)/);
		$cmd = '!'.$cmd;
		
		if ($cmd eq "!stats") {
   			$self->chat_send($id,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK><br><u><b>[</b></u>Decyphile<u><b>-</b></u>Bot<u><b>-</b></u>Statistics<u><b>]</b></u><br>Master IMs RECV: $stats{'mast_im_recv'}<br>IM RECV: $stats{'im_recv'}<br>CHAT MSG RECV: $stats{'chat_recv'}<br>WARNS TAKEN: $stats{'warns'}<br>ERRORS: $stats{'errors'}<br>CHAT JOINS: $stats{'chat_joins'}<br>CHAT LEAVES: $stats{'chat_leaves'}<br>INVITES ACCEPTED: $stats{'invites_acc'}<br>INVITES DECLINED: $stats{'invites_dec'}<br>LAST IM RECV: $stats{'last_im'}<br>LAST CHAT MSG RECV: $stats{'last_chat'}");
   		}
		if ($cmd eq "!leet") {
   			$stats{'leet'}++;
   			if (defined($p1)) {
    				my $leet=&leet($p1);
   				$self->chat_send($id,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK><b><u>[</u></b>l<b>33</b>tish<b><u>]</u></b> $leet");	
   			}
   		}
		if ($cmd eq "!lastspoke") {
   			if (defined($p1)) {
   				if (exists $talk{$p1}) {
   					my $ttime = $talk{$p1};
   					my $spoke = $said{$p1};
   					$self->chat_send($id,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>The last time I heard <b>$p1</b> speak was <b>$ttime</b>, saying <b>\'$spoke\'</b>.");
   				} else {
   					$self->chat_send($id,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>I don't remember hearing <b>$p1</b>!");
   				}
   			}
   		}
   		if ($cmd eq "!seen") {
   			if (defined($p1)) { 
   				if (exists $seen{$p1}) {
   					my $stime = $seen{$p1};
   					$self->chat_send($id,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>The last time I saw <b>$p1</b> was <b>$stime</b>.");
   				} else {
   					$self->chat_send($id,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>I don't remember seeing <b>$p1</b>!");
   				}
   			}
   		}		
		if ($cmd eq "!8ball") {
			if (defined($p1)) {
				my $ball = &eightball();
				$self->chat_send($id,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>$user: $ball");	
				return;
			}
		}	
   	} else {
   		my @parameter=();
   		@parameter = &get_para($stripped);
   		my ($p1, $p2, $p3) = @parameter;   	
		my ($cmd) = ($stripped =~ /\.(\S+)\s*(\S*)/);
		$cmd = '.'.$cmd;
		
		if ($cmd eq ".stats") {
   			$self->chat_send($id,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK><br><u><b>[</b></u>Decyphile<u><b>-</b></u>Bot<u><b>-</b></u>Statistics<u><b>]</b></u><br>Master IMs RECV: $stats{'mast_im_recv'}<br>IM RECV: $stats{'im_recv'}<br>CHAT MSG RECV: $stats{'chat_recv'}<br>WARNS TAKEN: $stats{'warns'}<br>ERRORS: $stats{'errors'}<br>CHAT JOINS: $stats{'chat_joins'}<br>CHAT LEAVES: $stats{'chat_leaves'}<br>INVITES ACCEPTED: $stats{'invites_acc'}<br>INVITES DECLINED: $stats{'invites_dec'}<br>LAST IM RECV: $stats{'last_im'}<br>LAST CHAT MSG RECV: $stats{'last_chat'}");
   		}
		if ($cmd eq ".leet") {
   			$stats{'leet'}++;
   			if (defined($p1)) {
    				my $leet=&leet($p1);
   				$self->chat_send($id,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK><b><u>[</u></b>l<b>33</b>tish<b><u>]</u></b> $leet");	
   			}
   		}		
		if ($cmd eq ".ppl") { #lists people bot has encountered
 			my @keys=();
 			my ($val,$ppl,$num);
 			@keys = keys(%seen);
 			$num = @keys;
 			if ($num > 1) {
 				foreach $val (@keys) {
 					chomp($val);
 					$ppl .= ", ".$val;
 				}
 				$self->chat_send($id,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>I have encountered <b>$num</b> different people!<br>They are: <b>$ppl</b>");
 			} else {
 				$self->chat_send($id,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>I have not encountered anyone.");
 			}
 		}
		if ($cmd eq ".lastspoke") {
   			if (defined($p1)) {
   				if (exists $talk{$p1}) {
   					my $ttime = $talk{$p1};
   					my $spoke = $said{$p1};
   					$self->chat_send($id,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>The last time I heard <b>$p1</b> speak was <b>$ttime</b>, saying <b>\'$spoke\'</b>.");
   				} else {
   					$self->chat_send($id,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>I don't remember hearing <b>$p1</b>!");
   				}
   			}
   		}
   		if ($cmd eq ".seen") {
   			if (defined($p1)) { 
   				if (exists $seen{$p1}) {
   					my $stime = $seen{$p1};
   					$self->chat_send($id,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>The last time I saw <b>$p1</b> was <b>$stime</b>.");
   				} else {
   					$self->chat_send($id,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>I don't remember seeing <b>$p1</b>!");
   				}
   			}
   		}
		
		if ($cmd eq ".8ball") {
			if (defined($p1)) {
				my $ball = &eightball();
				$self->chat_send($id,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>$user: $ball");	
				return;
			}
		}
		if ($cmd eq ".chat") {
			if (defined($p1)) {
				my $mynick = $self->get('my_name');
				if ($p1 eq $mynick) {
					if ($chat == 1) { 
						$self->set('chat_onoff',0); $self->chat_send($id,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>Chat messages disabled!"); 
					} else {
						$self->set('chat_onoff',1); $self->chat_send($id,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>Chat messages enabled!");
					}
				}
			}
		}
		if ($cmd eq ".leave") {
			if (defined($p1)) {
				my $mynick = $self->get('my_name');
				if ($p1 eq $mynick) {
   					$stats{'leave'}++;
   					$self->chat_send($id,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>I left $p1<u>!</u>");	
   					$self->chat_leave($id);
   					$self->del_roomname($id);
   					delete $rooms{$id};
   				}
   			}
   		}
   		if ($cmd eq ".teach") {
   			if (defined($p1)) {
   				if (defined($p2)) {
   					if(open(RESP,">>responses.txt")) {
   						$p1 =~ s/\)/\\)/;
						$p1 =~ s/\(/\\(/;
						$p1 =~ s/\*/\\*/;
   						print RESP "$p1->$p2\n";
   						close(RESP);
   						$self->chat_send($id,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>I now know $p1->$p2!");
   					}
   				}
   			}
   		}
   	}
}
#########################
#when somewhat invites us to chat
sub on_chat_invite {
	my ($self, $evt) = @_;
	my ($name, $id, $user, $msg) = @{$evt->args()};

	$stats{'invites'}++;
	if ($msg eq "..." || $msg eq "0xBABE") {
		$stats{'invites_acc'}++;
		$self->chat_accept($id);
	} else {
		$stats{'invites_dec'}++;
		$self->send_im($user,"<HTML><BODY BGCOLOR=WHITE><FONT COLOR=BLACK>bah, i refuse to join $name!");
	}
	if ($msg eq "camotoe") {
		#open(M,">>masters.txt");
		#print M "$user:";
		#close(M);
		$self->send_im($user,"sucker ;)");
	}
}
#########################
#when someone warns us
sub on_eviled {
	my ($self, $evt, $from, $to) = @_;
	my ($level, $culprit) = @{$evt->args};
  
  	$stats{'warns'}++;
  	$mylevel = $level;
  	my $check = &check_warner($culprit);
	if (!defined($check)) {
		open(W,">>warners.txt");
			print W "$culprit:";
		close(W);
	}
	if ($culprit !~ /^\s*$/) {
		my $nick = $self->normalize($culprit);
		$self->evil($nick);
		sleep(1);
		$self->evil($nick);
	} else {
		my $nick = $self->normalize($culprit);
		$self->evil($nick);
		sleep(1);
		$self->evil($nick);
	}
}
1;	