#!/usr/ug/bin/perl5 # # sample use: # gsub -akf ~whuang/prog/gale/babelfish cmd/babelfish:@ugcs.caltech.edu/user/babelfish.whuang # # by Wei-Hwa Huang (whuang@ugcs.caltech.edu) # sections of code stolen from jtr@ugcs.caltech.edu use strict; use POSIX qw(strftime); # gale info use vars qw($FROM $ENC $CAT $SIGN $TIME @MESSAGE $NEWCAT $FTIME); # misc use vars qw($GALE $HOME $LOGDIR $USER %LOGGEDTO); $FROM= $ENV{'HEADER_FROM'}; $TIME= $ENV{'HEADER_TIME'}; $CAT= $ENV{'GALE_CATEGORY'}; $CAT =~ s-zephyr/message-zephyr/MESSAGE-gi; print STDERR "incoming message from category: $CAT\n"; $ENC= $ENV{'GALE_ENCRYPTED'}; $SIGN= $ENV{'GALE_SIGNED'}; $HOME= $ENV{'HOME'}; $GALE= $HOME . "/.gale/"; $LOGDIR= $HOME . "/gale/"; $USER= $ENV{'USER'}; $FTIME= $ENV{'GALE_TIME_ID_TIME'}; @MESSAGE = <>; print STDERR "$CAT\n"; my @cats = split(":",$CAT); my (@cmdcats, @botcats, @othercats) = ((),(),()); my $singlecat; foreach $singlecat (@cats) { if ($singlecat =~ /^bot\./) { push(@botcats,$singlecat); } elsif ($singlecat eq '@ugcs.caltech.edu/user/babelfish.whuang/') { push(@cmdcats,$singlecat); } elsif ($singlecat =~ /^cmd\./) { push(@cmdcats,$singlecat); } else { push(@othercats,$singlecat); } } my @legalnames = ("en_fr","en_de","en_it","en_pt","en_es", "fr_en","de_en","it_en","es_en","pt_en"); my $written = 0; # set to 1 when written, so we aren't redundant. foreach $singlecat (@cmdcats) { my $outstring; my $ext; my $outcat; if ($singlecat eq '@ugcs.caltech.edu/user/babelfish.whuang/') { $ext = shift(@MESSAGE); chomp $ext; $ext =~ s/^\s*//g; $ext =~ s/\s*$//g; $outstring = (@othercats == ()) ? "| gsend $SIGN" : "| gsend $SIGN -c ".join(':',@othercats); } else { $singlecat =~ /^cmd\.babelfish\.(.*)$/; $ext = $1; $outcat = 'bot.babelfish'; if (grep(($ext eq $_),@legalnames) == ()) { } elsif ($ext =~ /en$/) { $outcat .= '.english'; } elsif ($ext =~ /fr$/) { $outcat .= '.francais'; } elsif ($ext =~ /de$/) { $outcat .= '.deutsch'; } elsif ($ext =~ /fr$/) { $outcat .= '.francais'; } elsif ($ext =~ /it$/) { $outcat .= '.italiano'; } elsif ($ext =~ /pu$/) { $outcat .= '.portugues'; } elsif ($ext =~ /es$/) { $outcat .= '.espanol'; } $outstring = (@othercats == ()) ? "| gsend -c $outcat" : "| gsend -c $outcat:".join(':',@othercats); } if (join('',@MESSAGE) =~ /^\s*help\s*$/ or (grep(($ext eq $_),@legalnames) == ())) { @MESSAGE = (); $MESSAGE[0] = <<'End of Help Message'; babelfish version 2.0 by Wei-Hwa Huang (whuang@ugcs.caltech.edu) Uses the babelfish WWW server at http://babelfish.altavista.com to translate your puff into another language. Privately: Send a private puff to babelfish.whuang@ugcs.caltech.edu. The first line should contain and nothing else except whitespace. Publically: puff to cmd.babelfish. . The bot will output to bot.babelfish. . is one of: en_fr : English to French fr_en : French to English en_de : English to German de_en : German to English en_it : English to Italian it_en : Italian to English en_pt : English to Portuguese pt_en : Portuguese to English en_es : English to Spanish es_en : Spanish to English is one of: english : English francais : French deutsch : German italiano : Italian portugues : Portugese espanol : Spanish Known bugs: Bad things will happen if the babelfish server is down. If the output string contains "Server Error", you will probabably never see a reply as the bot will try again. End of Help Message } else { # Add your code here. if ($written==0) { open(FHAND, ">/home/whuang/prog/gale/data/temp.html"); print FHAND "
\n";
      print FHAND @MESSAGE;
      print FHAND "
\n"; close(FHAND); $written = 1; } @MESSAGE = (); $MESSAGE[0] = 'Server Error'; while (grep(/Server Error/,@MESSAGE) != ()) { system('lynx -dump \'http://babelfish.altavista.com/' .'translate.dyn?' .'url=http://www.ugcs.caltech.edu/' .'~whuang/prog/gale/data/temp.html\' ' .'>/home/whuang/prog/gale/data/out.txt'); open(FHAND, "; close(FHAND); shift @MESSAGE; } } $ENV{'GALE_FROM'}= "$FROM (translated by Cypher)"; open(FHAND, $outstring); print FHAND @MESSAGE; close(FHAND); } #unlink("/home/whuang/prog/gale/data/temp.html"); #unlink("/home/whuang/prog/gale/data/out.txt");