Vous êtes sur la page 1sur 4

#!

/usr/bin/perl

# Created by Franc. This is an IRC Bot, it will message back to you as long as
you're
# privmsg it. Right now you can only do 'weather oneWord oneWord', if the city has
more
# than one word, it won't work. So yea, this is version 1 of the Bot.
# JEFF Wanted me to make it D:

# 'perl irc -h' for help menu, 'sudo chmod a+x irc' to make it executable using
'./'

system "clear";
print "Internet Relay Chat Bot\n\n";

# 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;

open (LOGFILE, ">>logfile.txt");

$SIG{INT} = \&signalint;

my $h = @ARGV[0] eq '-h'; shift @ARGV if $h;

if($h) {
print color 'bold blue';
print "Usage: perl irc <BotName> <IRCServer> <IRCChannel>\n";
print "Example: perl irc bot102928 irc.freenode.net \\#ubuntu\n";
print color 'reset';
exit;
}

my $irc = new Net::IRC;


my $date = scalar(localtime);
my $ircb = shift;
my $ircs = shift;
my $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';
print LOGFILE "\n\n";
system("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 "Checked commands.\n";


print LOGFILE "Checked commands\n";
$conn->privmsg($event->{nick}, "version ; info ; date ; weather help");

}elsif($event->{args}[0] =~ /\bversion\b/i) {

print "Checked version.\n";


print LOGFILE "Checked version\n";
$conn->privmsg($event->{nick}, "Version: 2.0A");

}elsif($event->{args}[0] =~ /\binfo\b/i) {

print "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+$/) {

print "Entered decimal \(floating-point\) 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+$/) {

print "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 "Date.\n";
print LOGFILE "Checked date\n";
$conn->privmsg($event->{nick}, "The date is $date");

}elsif($event->{args}[0] =~ /^weather(\s+)(\w+)(\s+)(\w+)/i) {

print "Weather: '$2, $4'\n";


print LOGFILE "Checked weather for '$2, $4'";

my $weather = Weather::Underground->new
(
place => "$2, $4",
debug => 0
);
my $ref = $weather->get_weather();

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}, "Location: $loc");
$conn->privmsg($event->{nick}, "\[Sky: $csc\] \[Humidity: $h\]
\[Temperature: $temp\] \[Wind: $wind\]");
$conn->privmsg($event->{nick}, "Last updated: $upd");
$conn->privmsg($event->{nick}, "-----");

}elsif($event->{args}[0] =~ /^weather(\s+)help/) {

print "Asked for weather help.\n";


print LOGFILE "Asked for weather help\n";
$conn->privmsg($event->{nick}, "Details: Gives you the the weather.");
$conn->privmsg($event->{nick}, "Syntax: weather cityname countryname");
$conn->privmsg($event->{nick}, "Example: weather toronto canada");

}elsif($event->{args}[0] =~ /\bhi\b/) {

print "Wrote hello to bot.\n";


print LOGFILE "Wrote hello to bot\n";
$conn->privmsg($event->{nick}, "Hello back, type 'commands'");

}else{
print "Typed something that does't exist.\n";
print LOGFILE "Typed something that doesn't exist\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 LOGFILE "---- File written: $date ----\n\n";


print "--------------------";
print "\nBot: $ircb\n";
print "IRC Server: $ircs\n";
print "IRC Channel: $ircc\n";
print "--------------------\n\n";
print "Ctrl+C to terminate script.\n\n";
print color 'bold blue';
print "------IRC Bot Queries------\n";

$irc->start();

Vous aimerez peut-être aussi