#!/usr/bin/perl # Created by Franc. This is an IRC Bot, it will message back to you as long as you're # privmsg it. # 'perl irc -h' for help menu, 'sudo chmod +x irc' to make it executable using './' # The modules you'll need to make this bot work. Download via CPAN. use strict; use Net::IRC; use Term::ANSIColor; use Weather::Underground; use HTML::Parser; use HTML::Tagset; use LWP::UserAgent; use Net::Ping; use LWP::Simple; open (LOGFILE, ">>botInteractionsHistory.txt"); $SIG{INT} = \&signalint; if(!$ARGV[0]) { print color 'bold red'; print "Type 'perl irc -h' for list of extensions.\n"; print color 'reset'; exit; } my $h = @ARGV[0] eq '-h'; shift @ARGV if $h; if($h) { print print print print exit; } my my my my my
color 'bold blue'; "Usage: perl irc \n"; "Example: perl irc bot102928 irc.freenode.net \\#ubuntu\n"; color 'reset';
$irc = new Net::IRC; $date = scalar(localtime); $ircb = shift; $ircs = shift; $ircc = shift; my $conn = $irc->newconn ( Nick => $ircb, Ircname => $ircb, Server => $ircs );
sub signalint {
}
print color 'bold red'; print STDERR "\nKilled the bot.\n"; print color 'reset'; exit;
sub on_connect { my $self = shift; $self->join($ircc); } sub on_msg { my ($conn, $event) = @_; if($event->{args}[0] =~ /\bcommands\b/i) { print "[" . scalar(localtime) . "] " . "Checked commands.\n"; print LOGFILE "Checked commands\n"; $conn->privmsg($event->{nick}, "version ; info ; date ; check_weather help ; check_ip help ; check_website help"); } elsif($event->{args}[0] =~ /\bversion\b/i) { print "[" . scalar(localtime) . "] " . "Checked version.\n"; print LOGFILE "Checked version\n"; $conn->privmsg($event->{nick}, "Version: 2.1A"); } elsif($event->{args}[0] =~ /\binfo\b/i) { print "[" . scalar(localtime) . "] " . "Checked bot information.\n"; print LOGFILE "Checked bot information\n"; $conn->privmsg($event->{nick}, "Created by Francois, Perl 5."); } elsif($event->{args}[0] =~ /^(-)?\d+\.\d+$/i) { print "[" . scalar(localtime) . "] " . "Entered decimal \(floatingpoint\) number.\n"; print LOGFILE "Entered decimal \(floating-pointing\) number\n"; $conn->privmsg($event->{nick}, "You entered a floating-point number."); } elsif($event->{args}[0] =~ /^(-)?\d+$/i) { print "[" . scalar(localtime) . "] " . "Entered whole \(integer\) number.\n"; print LOGFILE "Entered whole \(integer\) number\n"; $conn->privmsg($event->{nick}, "You entered an integer number."); }
elsif($event->{args}[0] =~ /\b(date|time)\b/i) { print "[" . scalar(localtime) . "] " . "Checked the date.\n"; print LOGFILE "Checked date\n"; $conn->privmsg($event->{nick}, "The date is $date\:EST"); } elsif($event->{args}[0] =~ /^(check_ip)(?:\s+)(help)$/i) { print "[" . scalar(localtime) . "] " . "Asked for ip_check help.\n"; print LOGFILE "Asked for ip_check help.\n"; $conn->privmsg($event->{nick}, "\[Details: Status on IP + PORT.\] \[Syntax: check_ip ipaddress portnumber\]"); } elsif($event->{args}[0] =~ /^(?:check_ip)(?:\s+)(.*)(?:\s+)(.*)/i) { print "[" . scalar(localtime) . "] " . "Checked '$1\:\:$2'.\n"; print LOGFILE "Checked '$1\:\:$2'.\n"; my $p = Net::Ping->new("tcp", 2); $p->port_number($2); if($p->ping($1)) { $conn->privmsg($event->{nick}, "IP '$1' on port '$2' can be reached."); } else { $conn->privmsg($event->{nick}, "IP '$1' on port '$2' cannot be reached.");
}
} elsif($event->{args}[0] =~ /^(?:check_website)(?:\s+)(help)$/i) { print "[" . scalar(localtime) . "] " . "Asked for website_check help.\n"; print LOGFILE "Asked for website_check help.\n"; $conn->privmsg($event->{nick}, "\[Details: Checks for website status\] \[Syntax: check_website http://websitename/\]"); } elsif($event->{args}[0] =~ /^(?:check_website)(?:\s+)(.*)/i) { print "[" . scalar(localtime) . "] " . "Website: '$1'\n"; print LOGFILE "Website: '$1'\n"; if(get($1)) { $conn->privmsg($event->{nick}, "Website ('$1') got reached.\n"); } else {
$conn->privmsg($event->{nick}, "Website ('$1') could not be reached.\n"); } } elsif($event->{args}[0] =~ /^(?:check_weather)(\s+)help/i) { print "[" . scalar(localtime) . "] " . "Asked for weather help.\n"; print LOGFILE "Asked for weather help\n"; $conn->privmsg($event->{nick}, "\[Details: Gives you the the weather\] \[Syntax: check_weather cityname countryname\]"); } elsif($event->{args}[0] =~ /^(?:check_weather)(\s+)(.*)/i) { print "[" . scalar(localtime) . "] " . "Weather: '$2'\n"; print LOGFILE "Checked weather for '$2'\n"; my $weather = Weather::Underground->new ( place => "$2", ); my $ref = $weather->get_weather(); if($ref) { my $loc = "$ref->[0]->{place}"; my $upd = "$ref->[0]->{updated}"; my $csc = "$ref->[0]->{conditions}"; my $h = "$ref->[0]->{humidity}\%"; my $temp = "$ref->[0]->{temperature_fahrenheit}F $ref->[0]>{temperature_celsius}C"; my $wind = "$ref->[0]->{wind_direction} $ref->[0]>{wind_kilometersperhour}KPH $ref->[0]->{wind_milesperhour}MPH"; $conn->privmsg($event->{nick}, $conn->privmsg($event->{nick}, $conn->privmsg($event->{nick}, \[Temperature: $temp\] \[Wind: $wind\]"); $conn->privmsg($event->{nick}, $conn->privmsg($event->{nick},
"-----"); "Location: $loc"); "\[Sky: $csc\] \[Humidity: $h\] "Last updated: $upd"); "-----");
} else { $conn->privmsg($event->{nick}, "Could not find weather for that city, sorry."); } }
elsif($event->{args}[0] =~ /\bhi\b/i) { print "[" . scalar(localtime) . "] " . "Wrote hello to bot.\n";
print LOGFILE "Wrote hello to bot\n"; $conn->privmsg($event->{nick}, "Hello back, type 'commands'"); } else { my $message = $event->{args}[0]; print "[" . scalar(localtime) . "] " . "Typed \'$message\' (Doesn't exist).\n";
print LOGFILE "Typed something that doesn't exist.. ('$message').\n"; $conn->privmsg($event->{nick}, "Command does\'t exist. type 'commands'"); } } sub on_join { my ($self, $event) = @_; my ($channel) = ($event->to)[0]; my $nick = $event->{nick}; $self->privmsg($ircc, "Hello $nick, type '/msg $ircb commands' for help."); } $conn->add_handler('join', \&on_join); $conn->add_global_handler('376', \&on_connect); $conn->add_handler('msg', \&on_msg); print color 'bold blue'; print "\nBot: \[Name: \'$ircb\'\] \[Server: \'$ircs\'\] \[Channel: \'$ircc\'\]\n\n"; print "____Bot Queries____\n\n"; $irc->start();