All pastes #204065 Raw Edit

Fegis

public text v1 · immutable
#204065 ·published 2006-10-15 17:07 UTC
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;

# create the IRC object
my $reply;
my $irc = new Net::IRC;

# 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 $p = HTML::Parser->new(api_version => 3);
$p->handler( start => \&html_start_handler, "tagname,self");

# Setting up the search-engine
my $google = WWW::Search->new('Google', key => '4MEoDtdQFHKjCtpUhHYZ0NDRxAYxkRhE');
my $urbandictionary = WWW::Search->new('UrbanDictionary', key => 'ce3f79706283e6a097ab318a23e9703f');

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");
}

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

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, $nick) = @_;
	my $result;
	my $response = '';
	# branch off given the nature of the command
	$_ = $command->{args}[0];
	COM: 
	{
		#For google search
		if (/^g /)
		{
			my $query = substr($_, 2);
			$google->native_query($query);
			if ($result = $google->next_result())
			{
				$response = $result->title . "  " . $result->url . "  " . $result->description . "  http://www.google.com/search?q=" . uri_escape($query);
				$response = decode_entities($response);
				$response =~ s/<(([^ >]|\n)*)>//g;
				$conn->privmsg($conn->{channel}, encode("iso-8859-1", $response));
			}
			last COM;
		}
	
		#For fileext search
		if (/^f /)
		{
		}

		#For urbandictionary search
		if (/^u /)
		{
			my $query = substr($_, 2);
			$urbandictionary->native_query($query);
			if ($result = $urbandictionary->next_result())
			{
				$response = substr($result->{definition}, 0, 300) . "...  " . $result->url;
				$conn->privmsg($conn->{channel}, $response);
			}
			last COM;
		}

		if (/((http:\/\/|www\.)[^\.]{2,}\.[\d\w\.]{2,}(\/[^ ]*)?)/)
		{
			$_ = $1;
			if (!(/^http:/))
			{
				$_ = 'http://'.$_;
			}
			my $response = $ua->get($_);
			if ($response->is_success)
			{
				if ($response->headers->content_type eq 'text/html')
				{
					$reply = "No title set";
					#my $temp = encode("utf8", $response->content);
					#$p->parse($temp);
					$p->parse($response->content);
					#$p->parse($response->decoded_content(charset => 'ISO-8859-1'));
				}
				else
				{
					$reply = '';
				}
			}
			else
			{
				$reply = "the server timed out"
			}
			if ($reply ne '')
			{
				$reply =~ s/[\n\r]//g;
				$reply =~ s/^[\s]*//;
				#$reply = encode("iso-8859-1", $reply);
			}
				$conn->privmsg($conn->{channel}, $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();