1 ################################
3 ################################
5 package BotModules::Translate;
9 # Ah, the previous line looks so innocent. Yet it hides horrible
10 # evil. Yes, this module requires the following:
18 # libnet (you probably already have this)
22 @ISA = qw(BotModules);
25 # -- #mozilla was here! --
26 # *** Signoff: techbot_Hixie (~techbot_Hixie@129.59.231.42) has left IRC [Leaving]
27 # <timeless> oops, i killed your techbot
29 # RegisterConfig - Called when initialised, should call registerVariables
32 $self->SUPER::RegisterConfig(@_);
33 $self->registerVariables(
34 # [ name, save?, settable? ]
35 ['supportedservices', 1, 1, {
36 'Babelfish' => '', #Original WWW::Babelfish Service
37 'Yahoo' => '', #Available since WWW::Babelfish 0.14
38 'Google' => '', #Available since WWW::Babelfish 0.12
47 'zh' => 'Chinese-simp',
48 'zt' => 'Chinese-trad',
49 'zh-CN' => 'Chinese (Simp)', #Google-only
55 }], # short code => Babelfish Language Name
56 ['defaultLanguage', 1, 1, 'en'],
57 ['defaultservice', 1, 1, 'Babelfish'],
64 my @languages = keys(%{$self->{'languages'}});
65 my @services = keys(%{$self->{'supportedservices'}});
69 '' => 'Translate text between languages using Babelfish or Google.',
70 'translate' => "Syntax: \'translate [using (@services)] [from (@languages)] [to (@languages)] sentence\'",
71 'x' => 'Same as translate.',
72 'languages' => "Returns list of available languages to translate. Syntax: languages [(@services)]"
78 my ($event, $message) = @_;
79 if ($message =~ /^\s*(?:translate|x)\s+(.*?)\s*$/osi) {
80 $self->Translate($event, $1);
81 } elsif ($message =~ /^\s*languages?(?:\s+(.*?))?\s*(?:[, ]\s*please)?\?*\s*$/osi) {
82 $self->GetLanguages($event, $1);
84 return $self->SUPER::Told(@_);
86 return 0; # dealt with it...
91 my ($event, $service, $lang1, $lang2, $words) = @_;
92 my $translate_babelfish = new WWW::Babelfish('service' => $service);
93 my $result = $translate_babelfish->translate(
94 'source' => $self->{'languages'}->{$lang1},
95 'destination' => $self->{'languages'}->{$lang2},
98 if ($result !~ /^ *$/os) {
99 return "$event->{'from'}: $result";
101 my $error = $translate_babelfish->error;
102 if ($error =~ /^ *$/os) {
103 return "$event->{'from'}: I'm afraid I cannot translate that from $self->{'languages'}->{$lang1} to $self->{'languages'}->{$lang2}.";
105 return "$event->{'from'}: $error";
110 # ChildCompleted - Called when a child process has quit
113 my ($event, $type, $output, @data) = @_;
114 if ($type eq 'babelfish') {
115 $self->say($event, $output);
117 $self->SUPER::ChildCompleted($event, $type, $output, @data);
123 my ($event, $rest) = @_;
124 my @services = keys(%{$self->{'supportedservices'}});
125 my $service = $self->{'defaultservice'};
126 $service = $rest if ($rest);
128 my $languages_babelfish = new WWW::Babelfish('service' => $service);
129 $self->say($event,"$event->{'from'}: Available Translation Languages (for $service): " . join(", ", $languages_babelfish->languages)."");
135 my ($event, $rest) = @_;
136 my ($service, $lang1, $lang2, $words) = (
137 $self->{'defaultservice'},
138 $self->{'defaultLanguage'},
139 $self->{'defaultLanguage'},
142 my @services = keys(%{$self->{'supportedservices'}});
143 my @languages = keys(%{$self->{'languages'}});
148 #check service syntax
149 if ($rest =~ /^\s*using\s+(@services)\s+(.+)$/os) {
150 $service = $1 if defined($1);
155 if ($rest =~ /^\s*from\s+(@languages)\s+to\s+(@languages)\s+(.+)$/os) {
159 } elsif ($rest =~ /^\s*to\s+(@languages)\s+from\s+(@languages)\s+(.+)$/os) {
163 } elsif ($rest =~ /^\s*(from|to)\s+(@languages)\s+(.+)$/os) {
164 $lang1 = $2 if $1 eq 'from';
165 $lang2 = $2 if $1 eq 'to';
168 $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\'");
173 if ($lang1 eq $lang2) {
174 $self->say($event, "$event->{'from'}: Erm, well, translating from one language to the same language... doesn't change anything!");
176 $self->spawnChild($event, \&translate_do, [$self, $event, $service, $lang1, $lang2, $words], 'babelfish', []);