#!/usr/bin/perl ######################################################################### # #eglug (irc.freenode.net) bot. # # Author: Mohammed Sameer # # License: GPL # # Notes: The code is bad, but it was cooked in a few minutes ;-) # # Version: 0.0.2 # # Known Bugs: # # NickServ identification is not working. # # This whole thing needs to be rewritten. # # TODO: # # Don't cache the dict output when we can't connect to the dict server. # # Add !seen support. # # Handle all security related things. # # Changes: # - 0.0.2: # * Using google web API for to query google ######################################################################### # Patches are welcomed. # # Feature requests are welcomed. # ######################################################################### use Net::IRC; use Lingua::Ispell; require LWP::UserAgent; require URI::URL; use Chatbot::Eliza; use Net::Google; use strict; my $nick = "eglug"; my $server = "irc.freenode.net"; my $username = "bot"; my $ircname = "EGLUG Bot."; my @channels = ("#eglug"); my $pass = ""; my $google_id = ""; my $google = Net::Google->new(key=>$google_id); my $mybot = new Chatbot::Eliza; my @to; my $irc = new Net::IRC; my $conn = $irc->newconn(Nick => $nick, Server => $server, Username => $username, Ircname => $ircname ) || die("Can't connect!"); $conn->add_global_handler('376', \&on_connect); $conn->add_handler('public', \&on_public); $conn->add_handler('msg', \&on_msg); $conn->add_global_handler([ 251,252,253,254,302,255 ], \&on_init); $irc->start; sub on_connect { my $self = shift; foreach (@channels) { if ($pass) { $conn->privmsg("NickServ","IDENTIFY $pass"); } $self->join($_); } } sub on_public { my ($self, $event) = @_; @to = $event->to; my ($nick, $mynick) = ($event->nick, $self->nick); my ($arg) = ($event->args); $_ = $arg; if (/!!!/) { &return_back("a7a, please elaborate."); return; } if (/[h|7]an.f./) { &return_back("$nick: Don't talk about my mother."); return; } $arg =~ /^$mynick:/i || return; $arg = substr($arg, length($mynick)+1); if (/dict/i) { &lookup("dict -h dict.org -d jargon", $arg, "dict"); return; } if (/arabeyes/i) { &lookup("dict -h dict.arabeyes.org", $arg, "arabeyes"); return; } if (/spell/i) { &spell($arg); return; } if (/google/i) { &google($arg); return; } else { my @words = split /\W+/, $arg; shift (@words); shift (@words); $arg = ""; foreach (@words) { $arg .= "$_ "; } #my $string = "I have too many problems."; my $reply = $mybot->transform( $arg ); &return_back("$nick: $reply"); } } sub lookup { my $command = shift; my $arg = shift; my $suffix = shift; my @words = split /\W+/, $arg; shift (@words); shift (@words); $arg = ""; foreach (@words) { $arg .= "$_ "; } # print "$arg\n"; if (open (DICT,"data/$arg.$suffix")) { print "opened cache file\n"; my $result = ; close (DICT); return_back($result); } else { open (DICT, "$command $arg|") || return_back("Can't connect to dict server"); my $result = ""; my @matches = ; foreach(@matches) { $result .= $_; } $result = substr($result, 0, 300); $result .= "..."; $result =~ s/\n/ /g; return_back($result); open (DICT,">data/$arg.$suffix") || return; print DICT "$result"; close (DICT); } } sub return_back() { my $msg = shift; # print "$msg\n"; $conn->privmsg(@to, $msg); } sub on_msg { my ($self, $event) = @_; my ($nick) = $event->nick; print "*$nick* ", ($event->args), "\n"; } sub spell { my $arg = shift; my @words = split /\W+/, $arg; shift (@words); shift (@words); foreach (@words) { my $arg .= $_; } #print "$arg\n"; for my $r (Lingua::Ispell::spellcheck( $arg )) { if ($r->{'type'} eq "miss") { &return_back("$r->{'term'}: @{$r->{'misses'}}"); } elsif ($r->{'type'} eq "guess") { &return_back("$r->{'term'}: guess-> @{$r->{'guesses'}}"); } elsif ($r->{'type'} eq "none") { &return_back("$r->{'term'}: Unknown."); } else { print "$r->{'type'} $r->{'term'}\n"; } } } # Handles some messages you get when you connect sub on_init { my ($self, $event) = @_; my (@args) = ($event->args); shift (@args); print "*** @args\n"; } sub google { # &return_back("B0rked."); # return; my $arg = shift; my @words = split /\W+/, $arg; shift (@words); shift (@words); $arg = ""; foreach (@words) { $arg .= "$_ "; } my $search = $google->search(); $search->max_results(1); $search->query("$arg"); my $res = $search->results(); foreach my $result (@{$res}) { my $r = $arg; $r .= " -> "; $r .= $result->URL(); &return_back("$r"); } }