#!perl -T use strict; use Irssi; use LWP::Simple; use LWP::UserAgent; use CGI qw/unescapeHTML/; use vars qw($VERSION %IRSSI); $VERSION = '0.0.1'; %IRSSI = ( 'authors' => 'Craig Andrews', 'contact' => 'craig@simplyspiffing.com', 'name' => 'lookupbot', 'description' => 'Some kind of magical internet searcher', 'license' => 'Craig\'s Magical Freebie License'); # Get the count and cleaned up data sub get_data { my $data = shift; my @params = split / +/, $data; my $trigger = shift @params; my $count = 1; if (defined $params[0] && int($params[0]) > 0) { $count = shift @params; $count = 10 if $count > 10; } $data = join ' ', @params; $data =~ s/[^[:print:]]/ /g; $data =~ s/ */ /g; return ($trigger, $count, $data); } # Retrieve the content froma url sub get_content { my ($url, $data) = @_; $data = CGI::escape($data); $url = sprintf($url, $data) if defined $data; my $ua = LWP::UserAgent->new(agent => "google suck"); my $result = $ua->get($url); my $content; $content = $result->content if $result->is_success; return $content; } sub image_search { my $content = shift; my @definitions = $content =~ /imgurl=(.+?)\&/g; return @definitions; } sub define_search { my $content = shift; $content =~ s/[\n\r]+/ /g; my @definitions = $content =~ /(?<=
  • )(.+?)(?=
    |
  • )/ig; return @definitions; } sub dict_search { my $content = shift; $content =~ s/[\n\r]+/ /g; $content = '' if $content =~ /No entry found for/; $content =~ s/[^[:print:]]//g; $content =~ s/^(.*?)(?=entr(y|ies) found for)//; $content =~ s/(.*?)<\/table>/$1/ig; $content =~ s/<\/?CITE>/"/g; my @definitions; if ($content =~ /^entry/) { my @tmp = $content =~ /

    (.+?)<\/p>/ig; @definitions[0] = @tmp[1]; } else { @definitions = $content =~ /(?<=

  • |
    )(.+?)(?=
    |
  • |<\/li>|<\/dd>)/ig; } return @definitions; } sub thes_search { my $content = shift; my @lines = split /\n/, $content; @lines = grep { /(Main Entry|Synonyms|Antonyms):/ } @lines; my @definitions; foreach (@lines) { s/^(.*?)/ /; s/<\/b>(.*?)/ /g; s/<\/td>.*$/ /; if (/Main Entry:/ || scalar(@definitions) == 0) { push @definitions, $_; } else { $definitions[@definitions - 1] .= "\n$_"; } } return @definitions; } sub urban_search { my $content = shift; $content =~ s/[\n\r]+/ /g; $content = '' if $content =~ /is undefined/; $content =~ s/[^[:print:]]//g; my @lines = $content =~ /
    (.+?)
    /ig; my @definitions; my $ix; for ($ix = 0; $ix < @lines; $ix++) { my $line = $lines[$ix]; unless ($line =~ /def_p/) { $line =~ s/<\/p>/\n/g; push @definitions, $line; } } return @definitions; } sub profan_search { my $content = shift; my @matches = $content =~ /(.+?)(.+)/; return join "\n", @matches; } ### # Many different lookerupperers ### my %ENGINES = ('!image' => {'url' => 'http://images.google.co.uk/images?hl=en&q=%s', 'sub' => \&image_search}, '!define' => {'url' => 'http://www.google.co.uk/search?hl=en&q=define%%3A%%20%s', 'sub' => \&define_search}, '!dict' => {'url' => 'http://www.dictionary.com/search?q=%s', 'sub' => \&dict_search}, '!thes' => {'url' => 'http://www.thesaurus.com/search?q=%s', 'sub' => \&thes_search}, '!urban' => {'url' => 'http://www.urbandictionary.com/define.php?term=%s', 'sub' => \&urban_search}, '!profan' => {'url' => 'http://www.viz.co.uk/profanisaurus/profan_results.php?profan=search&prof_search=%s', 'sub' => \&profan_search}); # !stuff sub public_responder { my ($server, $data, $nick, $mask, $target) = @_; $data =~ s/`//gosm; my ($trigger, $count, $term) = get_data($data); my @definitions; my $found = 0; my $func; for $func (keys %ENGINES) { next unless $trigger eq $func; $found = 1; my $url = $ENGINES{$func}->{'url'}; my $sub = $ENGINES{$func}->{'sub'}; my $content = get_content($url, $term); @definitions = &{$sub}($content) if defined $content; } # Quit if this isn't for us return unless $found == 1; # Display if necessary if (defined @definitions && scalar(@definitions) > 0) { for (my $ix = 0; $ix < @definitions && $ix < $count; $ix++) { my @lines = split /\n/, $definitions[$ix]; my $text; foreach $text (@lines) { next if $text =~ /^\s*$/; # Strip HTML $text =~ s/<(.*?)>/ /g; $text = unescapeHTML($text); # Strip non-printable characters $text =~ s/[^[:print:]]/ /g; # Sort out whitespace $text =~ s/ +/ /g; $text =~ s/^ *//; $text =~ s/ *$//; # And display $server->command("msg $target -!- $text"); } } } else { $server->command("msg $target -!- No results found"); } } sub kick_handler { my ($server, $channel, $nick, $kicker, $address, $reason) = @_; if ($nick eq $server->{nick}) { my $chanrec = $server->channel_find( $channel ); my $password = $chanrec->{ key } if ( $chanrec ); my $rejoinchan = $chanrec->{ name } if ( $chanrec ); $server->send_raw("join $rejoinchan $password"); } } Irssi::signal_add("message public", \&public_responder); Irssi::signal_add("message kick", \&kick_handler);