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"
11 langs = ["en", "fr", "de", "it", "pt", "es", "nl"]
13 query = "/babelfish/tr"
14 if(m.params =~ /^to\s+(\S+)\s+(.*)/)
18 elsif(m.params =~ /^from\s+(\S+)\s+(.*)/)
22 elsif(m.params =~ /^(\S+)\s+(\S+)\s+(.*)/)
27 m.reply "incorrect usage: " + help(m.plugin)
30 lang_match = langs.join("|")
31 unless(trans_from =~ /^(#{lang_match})$/ && trans_to =~ /^(#{lang_match})$/)
32 m.reply "invalid language: valid languagess are: #{langs.join(' ')}"
36 data_text = URI.escape trans_text
37 trans_pair = "#{trans_from}_#{trans_to}"
38 data = "lp=#{trans_pair}&doit=done&intl=1&tt=urltext&urltext=#{data_text}"
40 # check cache for previous lookups
41 if @registry.has_key?("#{trans_pair}/#{data_text}")
42 m.reply @registry["#{trans_pair}/#{data_text}"]
46 http = @bot.httputil.get_proxy(URI.parse("http://babelfish.altavista.com"))
49 resp = http.post(query, data, {"content-type",
50 "application/x-www-form-urlencoded"})
52 if (resp.code == "200")
54 resp.body.each_line {|l|
55 if(l =~ /^\s+<td bgcolor=white class=s><div style=padding:10px;>(.*)<\/div>/)
59 @registry["#{trans_pair}/#{data_text}"] = answer
65 m.reply "couldn't parse babelfish response html :("
67 m.reply "couldn't talk to babelfish :("
72 plugin = BabelPlugin.new
73 plugin.register("translate")