rendered paste body#!/usr/bin/perl -w
use Net::IRC;
use strict;
use LWP::UserAgent;
use HTML::Entities;
use HTML::Parser;
use WWW::Search;
use Encode;
use URI::Escape;
use IMDB::Film;
# create the IRC object
my $reply;
my $irc = new Net::IRC;
# Create a connection object. You can have more than one "connection" per
# IRC object, but we'll just be working with one.
my $conn = $irc->newconn(
Server => shift || 'se.quakenet.org',
Port => shift || '6667',
Nick => 'OPFS-Helper',
Ircname => 'Stupid Bot',
Username => 'OPFSBot'
);
$conn->{channel} = shift || '#opfs';
# Setting up the browser
my $ua = LWP::UserAgent->new;
$ua->agent('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR
2.0.50727)');
$ua->timeout(10);
$ua->env_proxy;
# setting up the parser
my $parser = HTML::Parser->new(api_version => 3);
$parser->handler( start => \&html_start_handler, "tagname,self");
# Setting up the search-engine
my $google = WWW::Search->new('Google', key => '4MEoDtdQFHKjCtpUhHYZ0NDRxAYxkRhE');
sub html_start_handler
{
return if shift ne "title";
my $self = shift;
$self->handler(text => sub { $reply = shift; }, "dtext");
$self->handler(end => sub { shift->eof if shift eq "title"; }, "tagname,self");
}
sub join {
# shift in our connection object that is passed automatically
my $conn = shift;
# when we connect, join our channel and greet it
$conn->join($conn->{channel});
}
sub on_public
{
# here, we get the connection object, the text of the command, and
# the nick of the person who issued the command
my ($conn, $command) = @_;
my $nick = $command->{nick};
my $result;
my $query;
my $response;
$reply = '';
# branch off given the nature of the command
$_ = $command->{args}[0];
#For google search
if (/^g /)
{
$query = substr($_, 2);
$google->native_query($query);
if ($result = $google->next_result())
{
$reply = $result->title . " " . $result->url . " " . $result->description . " http://www.google.com/search?q=" . uri_escape($query);
}
}
#For fileext search
elsif (/^f /)
{
$query = substr($_, 2);
$reply = 'http://filext.com/detaillist.php?extdetail=' . uri_escape($query);
}
#For urbandictionary search
elsif (/^u /)
{
# it works better creating it everytime, strange but that's the truth
my $urbandictionary = WWW::Search->new('UrbanDictionary', key => 'ce3f79706283e6a097ab318a23e9703f');
$query = substr($_, 2);
$urbandictionary->native_query($query);
if ($result = $urbandictionary->next_result())
{
$reply = substr($result->{definition}, 0, 170) . "... " . $result->url;
}
}
#For imdb search
if (/^imdb /)
{
$query = substr($_, 5);
my $imdb = new IMDB::Film(crit => $query);
if($imdb->status)
{
$reply = $imdb->title()." " . $imdb->rating() . " (".$imdb->year().") ".$imdb->duration()." ".substr($imdb->plot(), 0, 100)."... http://www.imdb.com/title/tt". $imdb->id();
}
}
#For search on hitta.se
if (/^hitta /)
{
$query = substr($_, 5);
#http://hitta.se/SearchMixed.aspx?UCSB%3ATextBoxWho=$person&UCSB%3ATextBoxWhere=$place
}
elsif (/^välj (.*) eller (.*)/i)
{
my $chance = 47;
my $text;
my $rand = rand(100);
my @choose = ('Du skall givetvis ', 'Välj ');
my @unsure = ('Jag har ingen aning');
$text = $choose[rand(@choose)];
$reply = $nick . ': ';
if ($rand < $chance)
{
$reply .= $text . $1;
}
elsif ($rand < $chance * 2)
{
$reply .= $text . $2;
}
else
{
$reply .= $unsure[rand(@unsure)];
}
}
elsif (/^fråga (.*)?/i)
{
my $chance = 47;
my @positive = ('ja', 'JAAAAAAAAA!!!', 'Givetvis');
my @negative = ('nope', 'Neeeej', 'Absolut inte');
my @unsure = ('Vet inte', 'Ingen aning faktiskt');
my $rand = rand(100);
$reply = $nick . ': ';
if ($rand < $chance)
{
$reply .= $positive[rand(@positive)];
}
elsif ($rand < $chance * 2)
{
$reply .= $negative[rand(@negative)];
}
else
{
$reply .= $unsure[rand(@unsure)];
}
}
elsif (/((http:\/\/|www\.)[^\.]{2,}\.[\d\w\.]{2,}(\/[^ ]*)?)/i)
{
$_ = $1;
if (!(/^http:/))
{
$_ = 'http://'.$_;
}
$response = $ua->get($_);
if ($response->is_success)
{
if ($response->headers->content_type eq 'text/html')
{
$parser->parse($response->content); #get <title> into $reply
}
}
else
{
$reply = "Could not parse/timed out";
}
}
$reply = decode_entities($reply);
$reply =~ s/^\s//g;
$reply =~ s/<(([^ >]|\n)*)>//g;
$reply =~ s/[\r\n]//g;
$conn->privmsg($conn->{channel}, encode("iso-8859-1", $reply));
}
# The end of MOTD (message of the day), numbered 376 signifies we've connect
$conn->add_handler('376', \&join);
$conn->add_handler('422', \&join);
$conn->add_handler('kick', \&join);
$conn->add_handler('public', \&on_public);
# start IRC
$irc->start();