From: dms Date: Fri, 2 Feb 2001 13:10:01 +0000 (+0000) Subject: - fixes for babelfish X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=cbf84c0bb18fbc963b8dcd9f0a892d05b5b40c31;p=infobot.git - fixes for babelfish - typo of IsChanConf for wwwsearch. - parseCmdHooks return vals fixed. - babel.pl: regex fixed -- works!!! git-svn-id: https://svn.code.sf.net/p/infobot/code/trunk@299 c11ca15a-4712-0410-83d8-924469b57eb5 --- diff --git a/blootbot/src/CommandStubs.pl b/blootbot/src/CommandStubs.pl index c420031..705223a 100644 --- a/blootbot/src/CommandStubs.pl +++ b/blootbot/src/CommandStubs.pl @@ -4,9 +4,9 @@ if (&IsParam("useStrict")) { use strict; } -$babel::lang_regex = ""; # lame fix. +$babel_lang_regex = "fr|sp|po|pt|it|ge|de|gr|en"; -### PROPOSED COMMAND HOOK IMPLEMENTATION. +### COMMAND HOOK IMPLEMENTATION. # addCmdHook("SECTION", 'TEXT_HOOK', # (CODEREF => 'Blah', # Forker => 1, @@ -83,12 +83,12 @@ sub parseCmdHook { ### IDENTIFIER. if (exists $hash{'Identifier'}) { - return unless (&hasParam($hash{'Identifier'})); + return 1 unless (&hasParam($hash{'Identifier'})); } ### USER FLAGS. if (exists $hash{'UserFlag'}) { - return unless (&hasFlag($hash{'UserFlag'})); + return 1 unless (&hasFlag($hash{'UserFlag'})); } ### FORKER,IDENTIFIER,CODEREF. @@ -207,13 +207,13 @@ sub Modules { } # babel bot: Jonathan Feinberg++ - if (&IsParam("babelfish") and $message =~ m{ + if (&IsChanConf("babelfish") and $message =~ m{ ^\s* (?:babel(?:fish)?|x|xlate|translate) \s+ (to|from) # direction of translation (through) \s+ - ($babel::lang_regex)\w* # which language? + ($babel_lang_regex)\w* # which language? \s* (.+) # The phrase to be translated }xoi) { @@ -224,7 +224,7 @@ sub Modules { return; } - if (&IsParam("debian")) { + if (&IsChanConf("debian")) { my $debiancmd = 'conflicts?|depends?|desc|file|info|provides?'; $debiancmd .= '|recommends?|suggests?|maint|maintainer'; if ($message =~ /^($debiancmd)(\s+(.*))?$/i) { @@ -241,7 +241,7 @@ sub Modules { } # google searching. Simon++ - if (&IsParam("wwwsearch") and $message =~ /^(?:search\s+)?(\S+)\s+for\s+['"]?(.*?)['"]?\s*\?*$/i) { + if (&IsChanConf("wwwsearch") and $message =~ /^(?:search\s+)?(\S+)\s+for\s+['"]?(.*?)['"]?\s*\?*$/i) { return unless (&hasParam("wwwsearch")); &Forker("wwwsearch", sub { &W3Search::W3Search($1,$2); } ); diff --git a/blootbot/src/Modules/babel.pl b/blootbot/src/Modules/babel.pl index a75a19a..c2c7de9 100644 --- a/blootbot/src/Modules/babel.pl +++ b/blootbot/src/Modules/babel.pl @@ -35,47 +35,52 @@ BEGIN { sub babelfish { my ($direction, $lang, $phrase) = @_; - return unless &loadPerlModule("URI::Escape"); + return unless &::loadPerlModule("URI::Escape"); + return unless &::loadPerlModule("LWP::UserAgent"); $lang = $lang_code{$lang}; my $ua = new LWP::UserAgent; $ua->timeout(10); + $ua->proxy('http', $::param{'httpProxy'}) if &::IsParam("httpProxy"); - my $url = 'http://babelfish.altavista.digital.com/cgi-bin/translate'; + my $url = 'http://babelfish.altavista.com/raging/translate.dyn'; my $req = HTTP::Request->new('POST',$url); + $req->content_type('application/x-www-form-urlencoded'); my $tolang = "en_$lang"; my $toenglish = "${lang}_en"; if ($direction eq 'to') { - &::performStrictReply( translate($phrase, $tolang, $req, $ua) ); + my $xlate = translate($phrase, $tolang, $req, $ua); + &::pSReply($xlate) if ($xlate); return; } elsif ($direction eq 'from') { - &::performStrictReply( translate($phrase, $toenglish, $req, $ua) ); + my $xlate = translate($phrase, $toenglish, $req, $ua); + &::pStReply($xlate) if ($xlate); return; } + &DEBUG("what's this junk?"); my $last_english = $phrase; my $last_lang; my %results = (); my $i = 0; while ($i++ < 7) { - last if $results{$phrase}++; + last if $results{$phrase}++; # REMOVE! $last_lang = $phrase = translate($phrase, $tolang, $req, $ua); - last if $results{$phrase}++; + last if $results{$phrase}++; # REMOVE! $last_english = $phrase = translate($phrase, $toenglish, $req, $ua); } - &::performStrictReply($last_english); + &::pSReply($last_english); } sub translate { - return '' if $no_babel; my ($phrase, $languagepair, $req, $ua) = @_; - my $urltext = uri_escape($phrase); + my $urltext = URI::Escape::uri_escape($phrase); $req->content("urltext=$urltext&lp=$languagepair&doit=done"); my $res = $ua->request($req); @@ -83,25 +88,23 @@ sub translate { my $translated; if ($res->is_success) { # success. my $html = $res->content; - # This method subject to change with the whims of Altavista's design - # staff. - - $translated = - ($html =~ m{
- \s+ - - \s* - (?:\*\*\s+time\ out\s+\*\*)? - \s* - ([^<]*) - }sx); - - $translated =~ s/\n/ /g; - $translated =~ s/\s*$//; + $html =~ s/\cM//g; + $html =~ s/\n\s*\n/\n/g; + $html =~ s/\n/ /g; # ... + + if ($html =~ /(.*?)<\/textarea/si) { + $translated = $1; + $translated =~ s/^[\n ]|[\n ]$//g; + } else { + &::WARN("failed regex for babelfish."); + } + } else { # failure - $translated = ":("; + $translated = "FAILURE w/ babelfish"; } + $translated ||= "NULL reply from babelfish."; + return $translated; }