5 class BabelPlugin < Plugin
6 def help(plugin, topic="")
7 "translate to <lang> <string> => translate from english to <lang>, translate from <lang> <string> => translate to english from <lang>, translate <fromlang> <tolang> <string> => translate from <fromlang> to <tolang>. Languages: en, fr, de, it, pt, es, nl"
15 if(ENV['http_proxy'] =~ /^http:\/\/(.+):(\d+)$/)
21 langs = ["en", "fr", "de", "it", "pt", "es", "nl"]
23 query = "/babelfish/tr"
24 if(m.params =~ /^to\s+(\S+)\s+(.*)/)
28 elsif(m.params =~ /^from\s+(\S+)\s+(.*)/)
32 elsif(m.params =~ /^(\S+)\s+(\S+)\s+(.*)/)
37 m.reply "incorrect usage: " + help(m.plugin)
40 lang_match = langs.join("|")
41 unless(trans_from =~ /^(#{lang_match})$/ && trans_to =~ /^(#{lang_match})$/)
42 m.reply "invalid language: valid languagess are: #{langs.join(' ')}"
46 data_text = URI.escape trans_text
47 trans_pair = "#{trans_from}_#{trans_to}"
48 data = "lp=#{trans_pair}&doit=done&intl=1&tt=urltext&urltext=#{data_text}"
50 # check cache for previous lookups
51 if @registry.has_key?("#{trans_pair}/#{data_text}")
52 m.reply @registry["#{trans_pair}/#{data_text}"]
56 http = Net::HTTP.new("babelfish.altavista.com", 80, proxy_host, proxy_port)
59 resp = http.post(query, data, {"content-type",
60 "application/x-www-form-urlencoded"})
62 if (resp.code == "200")
64 resp.body.each_line {|l|
65 if(l =~ /^\s+<td bgcolor=white class=s><div style=padding:10px;>(.*)<\/div>/)
69 @registry["#{trans_pair}/#{data_text}"] = answer
75 m.reply "couldn't parse babelfish response html :("
77 m.reply "couldn't talk to babelfish :("
82 plugin = BabelPlugin.new
83 plugin.register("translate")