################################ # Translate Module # ################################ package BotModules::Translate; use vars qw(@ISA); use WWW::Babelfish; # Ah, the previous line looks so innocent. Yet it hides horrible # evil. Yes, this module requires the following: # # WWW::Babelfish # libwww (a bundle) # URI # MIME-Base64 # HTML::Parser # HTML-Tagset # libnet (you probably already have this) # Digest::MD5 # IO::String @ISA = qw(BotModules); 1; # -- #mozilla was here! -- # *** Signoff: techbot_Hixie (~techbot_Hixie@129.59.231.42) has left IRC [Leaving] # oops, i killed your techbot # RegisterConfig - Called when initialised, should call registerVariables sub RegisterConfig { my $self = shift; $self->SUPER::RegisterConfig(@_); $self->registerVariables( # [ name, save?, settable? ] ['supportedservices', 1, 1, { 'Babelfish' => '', #Original WWW::Babelfish Service 'Yahoo' => '', #Available since WWW::Babelfish 0.14 'Google' => '', #Available since WWW::Babelfish 0.12 }], ['languages', 1, 1, { 'en' => 'English', 'fr' => 'French', 'de' => 'German', 'it' => 'Italian', 'es' => 'Spanish', 'ar' => 'Arabic', 'zh' => 'Chinese-simp', 'zt' => 'Chinese-trad', 'zh-CN' => 'Chinese (Simp)', #Google-only 'nl' => 'Dutch', 'el' => 'Greek', 'ja' => 'Japanese', 'pt' => 'Portuguese', 'ru' => 'Russian', }], # short code => Babelfish Language Name ['defaultLanguage', 1, 1, 'en'], ['defaultservice', 1, 1, 'Babelfish'], ); } sub Help { my $self = shift; my ($event) = @_; my @languages = keys(%{$self->{'languages'}}); my @services = keys(%{$self->{'supportedservices'}}); local $"; $" = '|'; return { '' => 'Translate text between languages using Babelfish or Google.', 'translate' => "Syntax: \'translate [using (@services)] [from (@languages)] [to (@languages)] sentence\'", 'x' => 'Same as translate.', 'languages' => "Returns list of available languages to translate. Syntax: languages [(@services)]" }; } sub Told { my $self = shift; my ($event, $message) = @_; if ($message =~ /^\s*(?:translate|x)\s+(.*?)\s*$/osi) { $self->Translate($event, $1); } elsif ($message =~ /^\s*languages?(?:\s+(.*?))?\s*(?:[, ]\s*please)?\?*\s*$/osi) { $self->GetLanguages($event, $1); } else { return $self->SUPER::Told(@_); } return 0; # dealt with it... } sub translate_do { my $self = shift; my ($event, $service, $lang1, $lang2, $words) = @_; my $translate_babelfish = new WWW::Babelfish('service' => $service); my $result = $translate_babelfish->translate( 'source' => $self->{'languages'}->{$lang1}, 'destination' => $self->{'languages'}->{$lang2}, 'text' => $words, ); if ($result !~ /^ *$/os) { return "$event->{'from'}: $result"; } else { my $error = $translate_babelfish->error; if ($error =~ /^ *$/os) { return "$event->{'from'}: I'm afraid I cannot translate that from $self->{'languages'}->{$lang1} to $self->{'languages'}->{$lang2}."; } else { return "$event->{'from'}: $error"; } } } # ChildCompleted - Called when a child process has quit sub ChildCompleted { my $self = shift; my ($event, $type, $output, @data) = @_; if ($type eq 'babelfish') { $self->say($event, $output); } else { $self->SUPER::ChildCompleted($event, $type, $output, @data); } } sub GetLanguages { my $self = shift; my ($event, $rest) = @_; my @services = keys(%{$self->{'supportedservices'}}); my $service = $self->{'defaultservice'}; $service = $rest if ($rest); my $languages_babelfish = new WWW::Babelfish('service' => $service); $self->say($event,"$event->{'from'}: Available Translation Languages (for $service): " . join(", ", $languages_babelfish->languages).""); } sub Translate { my $self = shift; my ($event, $rest) = @_; my ($service, $lang1, $lang2, $words) = ( $self->{'defaultservice'}, $self->{'defaultLanguage'}, $self->{'defaultLanguage'}, ); my @services = keys(%{$self->{'supportedservices'}}); my @languages = keys(%{$self->{'languages'}}); local $"; $" = '|'; #check service syntax if ($rest =~ /^\s*using\s+(@services)\s+(.+)$/os) { $service = $1 if defined($1); $rest = $2; } # check syntax if ($rest =~ /^\s*from\s+(@languages)\s+to\s+(@languages)\s+(.+)$/os) { $lang1 = $1; $lang2 = $2; $words = $3; } elsif ($rest =~ /^\s*to\s+(@languages)\s+from\s+(@languages)\s+(.+)$/os) { $lang2 = $1; $lang1 = $2; $words = $3; } elsif ($rest =~ /^\s*(from|to)\s+(@languages)\s+(.+)$/os) { $lang1 = $2 if $1 eq 'from'; $lang2 = $2 if $1 eq 'to'; $words = $3; } else { $self->say($event, "$event->{'from'}: Noooo... That\'s not the right syntax at all! Try something like \'translate [using (@services)] [from (@languages)] [to (@languages)] sentence\'"); return; } # translate if ($lang1 eq $lang2) { $self->say($event, "$event->{'from'}: Erm, well, translating from one language to the same language... doesn't change anything!"); } else { $self->spawnChild($event, \&translate_do, [$self, $event, $service, $lang1, $lang2, $words], 'babelfish', []); } }