mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-05 16:46:26 +00:00
140 lines
4.1 KiB
Plaintext
140 lines
4.1 KiB
Plaintext
################################
|
|
# 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]
|
|
# <timeless> 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? ]
|
|
['languages', 1, 1, {
|
|
'en' => 'English',
|
|
'fr' => 'French',
|
|
'de' => 'German',
|
|
'it' => 'Italian',
|
|
'es' => 'Spanish',
|
|
}], # short code => Babelfish Language Name
|
|
['defaultLanguage', 1, 1, 'en'],
|
|
);
|
|
}
|
|
|
|
sub Help {
|
|
my $self = shift;
|
|
my ($event) = @_;
|
|
my @languages = keys(%{$self->{'languages'}});
|
|
local $";
|
|
$" = '|';
|
|
return {
|
|
'' => 'Uses babelfish.altavista.com to translate something.',
|
|
'translate' => "Syntax: \'translate [from (@languages)] [to (@languages)] sentence\'",
|
|
'x' => 'Same as translate (which see).',
|
|
};
|
|
}
|
|
|
|
sub Told {
|
|
my $self = shift;
|
|
my ($event, $message) = @_;
|
|
if ($message =~ /^\s*(?:translate|x)\s+(.*?)\s*$/osi) {
|
|
$self->Translate($event, $1);
|
|
} else {
|
|
return $self->SUPER::Told(@_);
|
|
}
|
|
return 0; # dealt with it...
|
|
}
|
|
|
|
sub translate_do {
|
|
my $self = shift;
|
|
my ($event, $lang1, $lang2, $words) = @_;
|
|
my $translate_babelfish = new WWW::Babelfish();
|
|
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 Translate {
|
|
my $self = shift;
|
|
my ($event, $rest) = @_;
|
|
my ($lang1, $lang2, $words) = (
|
|
$self->{'defaultLanguage'},
|
|
$self->{'defaultLanguage'},
|
|
);
|
|
|
|
my @languages = keys(%{$self->{'languages'}});
|
|
local $";
|
|
$" = '|';
|
|
|
|
# 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 [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, $lang1, $lang2, $words], 'babelfish', []);
|
|
}
|
|
}
|
|
|