################################ # Dictionary Module # ################################ # Copyright 2010 Lenny Herold # Looks up a word via either http://dictionary.reference.com # or http://www.urbandictionary.com # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see . package BotModules::Dictionary; use vars qw(@ISA); use LWP::UserAgent; use HTTP::Request; @ISA = qw(BotModules); 1; # RegisterConfig - Called when initialised, should call registerVariables sub RegisterConfig { my $self = shift; $self->SUPER::RegisterConfig(@_); $self->registerVariables( # [ name, save?, settable? ] ); } sub Help { my $self = shift; my ($event) = @_; return { '' => "This module performs dictionary lookups.", 'define, !define' => 'Look up the specified word on dictionary.reference.com', 'urban, !urban' => 'Look up the specified word on urbandictionary.com', }; } sub Told { my $self = shift; my ($event, $message) = @_; if ($message =~ /^(\s*!?define\s+)(.+)$/) { my $word = $2; my $result = define("dict", $word); if ($result) { $self->say($event, "$word: $result") } } elsif ($message =~ /^(\s*!?urban\s+)(.+)$/) { my $word = $2; my $result = define("urban", $word); if ($result) { $self->say($event, "$word: $result") } } else { return $self->SUPER::Told(@_); } return 0; } sub Heard { my $self = shift; my($event, $message) = @_; if ($message =~ /^(\s*!?define\s+)(.+)$/) { my $word = $2; my $result = define($word); if ($result) { $self->say($event, "$word: $result") } } elsif ($message =~ /^(\s*!?urban\s+)(.+)$/) { my $word = $2; my $result = define("urban", $word); if ($result) { $self->say($event, "$word: $result") } } else { return $self->SUPER::Heard(@_); } return 0; } sub define { my $type = shift; my $input = shift; my $url; my $data = LWP::UserAgent->new(); $data->timeout(10); if ($type eq "dict") { $url = "http://dictionary.reference.com/browse/$input"; } else { $input =~ s/\s/\+/g; $url = "http://www.urbandictionary.com/define.php?term=$input"; } my $query = HTTP::Request->new(GET => "$url"); my $response = $data->request($query); if ($response->is_error()) { return "%s\n", $response->status_line } my $contents = $response->content(); if ($type eq "dict") { if ($contents =~ /dndata">([\w\s\.;]+)<\/div/) { return $1; } else { return "No results found.\n"; } } else { if ($contents =~ /definition'>(.+)<\/div/) { return $1; } else { return "No results found.\n"; } } }