#!/usr/bin/perl use strict; use warnings; use IO::Socket; use lib 'C:/Perl/site/lib'; use Parse::IRC; use Cwd; use threads; use Switch; my $cwd = getcwd(); my $confile = $cwd . "/bot.conf"; my ($server, $port, $me, $ident, $username, $sock, $TOBOS, $autoidentify, $keyword, $npassword, $AROK, $antiChanSpam); main(); sub loadConf { open(FILE, $confile) or die "Couldn't open $confile :$\n"; foreach my $line () { chomp($line); next if $line =~ /^\#/; next if $line =~ /^\/\//; my ($syntax, $set) = split(/::/, $line); switch($syntax) { case 'botnick' { $me = $set; last; } case 'server' { $server = $set; last; } case 'port' { $port = $set; last; } case 'TOBOS' { $TOBOS = $set; last; } case 'autoidentify' { $autoidentify = $set; last; } case 'keyword' { $keyword = $set; last; } case 'npassword' { $npassword = $set; last; } case 'ident' { $ident = $set; last; } case 'username' { $username = $set; last; } case 'AROK' { $AROK = $set; last; } case 'antiChanSpam' { $antiChanSpam = $set; last; } } } close(FILE); } sub main { loadConf(); # This while will help us to reconnect on disconnect and to connect until it gets connected. while (!$sock) { createbot(); } } sub createbot { $sock = new IO::Socket::INET(PeerAddr => $server, PeerPort => $port, Proto => "tcp") or die "Can't connect: $!\n"; print $sock "NICK $me\r\n"; print $sock "USER $ident 0 0 :$username\r\n"; my %dispatch = ( 'ping' => \&irc_ping, '001' => \&irc_001, 'public' => \&irc_public, 'kick' => \&irc_kick, 'notice' => \&irc_notice, 'mode' => \&irc_mode); my $parser = Parse::IRC->new( public => 1 ); while (my $input = <$sock>) { $input =~ s/\r\n//g; my $hashref = $parser->parse( $input ); SWITCH: { my $type = lc $hashref->{command}; my @args; push @args, $hashref->{prefix} if $hashref->{prefix}; push @args, @{ $hashref->{params} }; if ( defined $dispatch{$type} ) { $dispatch{$type}->(@args); } last SWITCH; print STDOUT join( ' ', "irc_$type:", @args ), "\n"; } } } sub irc_public { my ($nick, $chan, @w) = @_; $nick = parseNick($nick); @w = map {split ' ' } @w; if ($antiChanSpam == 1 && IsSpam($chan, @w)) { send_msg($chan, "ejate de spamear weontaldo"); ban($nick, $chan, $TOBOS); } if ($w[0] eq "+uptime" && is_admin($w[1])) { send_msg($chan, `uptime`); } } sub ban { my ($nick, $chan, $type) = @_; } sub kick { my ($nick, $chan, $reason) = @_; } sub kb { my ($nick, $chan, $type, $reason) = @_; ban($nick, $chan, $type); kick($nick, $chan, $reason); } sub is_admin { my $nick = $_[0]; return 1; #respuesta temporal. } sub send_msg { my ($whom, @msg) = @_; print $sock "privmsg $whom :@msg\r\n"; return 1; } sub send_notice { my ($whom, @msg) = @_; print $sock "notice $whom :@msg\r\n"; } sub JOIN { foreach my $chan (@_) { print $sock "JOIN $chan\r\n"; } return 1; } sub PART { foreach my $chan (@_) { print $sock "PART $chan\r\n"; } } sub op { my ($chan, @nicks) = @_; my $nicksi = scalar(@nicks); my $i = 0; my $nicklist; foreach my $nick (@nicks) { if ($nicksi > 1) { $nicklist .= "$nick "; $i++; if ($i == 4) { print $sock "mode $chan +o+o+o+o $nicklist\r\n"; $i = 0; $nicklist = ""; } } else { print $sock "mode $chan +o $nick\r\n"; } } return 1; } sub irc_001 { echo("Connected to server"); JOIN("#Chile"); } sub command { print $sock ("@_\r\n"); return 1; } sub echo { print STDOUT "[echo]-> @_\n"; } sub parseNick { my $nick = $_[0]; $nick =~ s/!(.*)//g; return $nick; } sub IsSpam { my ($chan, @w) = @_; my $stats = 0; $chan = lc($chan); foreach my $line (@w) { $line = lc($line); next if $line !~ /#[a-zA-Z]/; next if $line eq $chan; $stats = 1; } return $stats; } sub irc_ping { my $Cserver = shift; print $sock "PONG :$Cserver\r\n"; return 1; } sub irc_kick { my ($nick, $chan, $kicked, $reason) = @_; if ($AROK == 1 && $kicked eq $me) { send_msg("ChanServ", "unban $chan $me"); JOIN($chan); } } sub irc_notice { my ($nick, $noticed, $w) = @_; $nick = parseNick($nick); echo("notice: $nick -> $noticed: $w"); if ($autoidentify == 1 && $nick eq "NickServ" && $w =~ /$keyword/) { echo("trying to identify with NickServ.."); send_msg("NickServ", "IDENTIFY $npassword"); } } sub irc_mode { my ($nick, $chan, $mode, $whom) = @_; $nick = parseNick($nick); if (defined($chan)) { echo("mode -> $nick $chan $mode $whom"); if ($whom eq $me && $mode eq "-o") { send_msg("ChanServ", "op $chan $me"); } } }