translator: make source language optional
[rbot] / data / rbot / plugins / fish.rb
1 class BabelPlugin < Plugin
2   LANGS = %w{en fr de it pt es nl ru zh zt el ja ko}
3   BASEURL = 'http://babelfish.yahoo.com'
4
5   Config.register Config::EnumValue.new('translate.default_from',
6     :values => LANGS, :default => 'en',
7     :desc => "Default language to translate from")
8   Config.register Config::EnumValue.new('translate.default_to',
9     :values => LANGS, :default => 'en',
10     :desc => "Default language to translate to")
11
12   def help(plugin, topic="")
13     case topic
14     when 'cache'
15       "translate cache [view|clear] => view or clear the translate cache contents"
16     else
17       from = @bot.config['translate.default_from']
18       to = @bot.config['translate.default_to']
19       "translate to <lang> <string> => translate from #{from} to <lang>, translate from <lang> <string> => translate to #{to} from <lang>, translate <fromlang> <tolang> <string> => translate from <fromlang> to <tolang>. If <string> is an http url, translates the referenced webpage and returns the 1st content paragraph. Languages: #{LANGS.join(', ')}. Other topics: cache"
20     end
21   end
22
23   def translate(m, params)
24     langs = LANGS
25     trans_from = params[:fromlang] ? params[:fromlang] : @bot.config['translate.default_from']
26     trans_to = params[:tolang] ? params[:tolang] : @bot.config['translate.default_to']
27     trans_text = params[:phrase].to_s
28
29     lang_match = langs.join("|")
30     unless(trans_from =~ /^(#{lang_match})$/ && trans_to =~ /^(#{lang_match})$/)
31       m.reply "invalid language: valid languagess are: #{langs.join(' ')}"
32       return
33     end
34
35     data_text = CGI.escape trans_text
36     trans_pair = "#{trans_from}_#{trans_to}"
37
38     if (trans_text =~ /^http:\/\//) && (URI.parse(trans_text) rescue nil)
39       m.reply 'webpage translation is not currently supported'
40       return
41       # TODO FIXME
42       # url = BASEURL+'/translate_url' +
43       #   "?lp=#{trans_pair}&trurl=#{data_text}"
44       #
45       # return Utils.get_first_pars([url], 1, :message => m)
46     end
47
48     data = "lp=#{trans_pair}&doit=done&intl=1&tt=urltext&urltext=#{data_text}"
49
50     # check cache for previous lookups
51     if @registry.has_key?("#{trans_pair}/#{data_text}")
52       m.reply @registry["#{trans_pair}/#{data_text}"]
53       return
54     end
55
56     headers = {
57       "content-type" => "application/x-www-form-urlencoded; charset=utf-8"
58     }
59
60     query = "/translate_txt"
61
62     begin
63       body = @bot.httputil.get(BASEURL+query,
64                                :method => :post,
65                                :body => data,
66                                :headers => headers)
67     rescue Exception => e
68       m.reply "http error: #{e.message}"
69       return
70     end
71
72     case body
73     when nil
74       m.reply "couldn't talk to babelfish :("
75     when /^\s*<div id="result"><div style="[^"]*">(.*?)<\/div><\/div>\s*$/
76       answer = $1
77       # cache the answer
78       if(answer.length > 0)
79         @registry["#{trans_pair}/#{data_text}"] = answer
80       end
81       m.reply answer
82       return
83     when /^\s+<option value="#{trans_pair}"\s+SELECTED>/
84       m.reply "couldn't parse babelfish response html :("
85     else
86       m.reply "babelfish doesn't support translation from #{trans_from} to #{trans_to}"
87     end
88   end
89
90   def cache_mgmt(m, params)
91     cmd = params[:cmd].intern
92     case cmd
93     when :view
94       cache = []
95       @registry.each { |key, val|
96         cache << "%s => %s" % [key, val]
97       }
98       m.reply "translate cache: #{cache.inspect}"
99     when :clear
100       keys = []
101       @registry.each { |key, val|
102         keys << key
103       }
104       keys.each { |key|
105         @registry.delete(key)
106       }
107       cache_mgmt(m, :cmd => 'view')
108     end
109   end
110
111 end
112
113 plugin = BabelPlugin.new
114
115 plugin.default_auth('cache', false)
116
117 plugin.map 'translate to :tolang *phrase', :thread => true
118 plugin.map 'translate from :fromlang *phrase', :thread => true
119 plugin.map 'translate cache :cmd', :action => :cache_mgmt, :auth_path => 'cache!', :requirements => { :cmd => /view|clear/ }
120 plugin.map 'translate :fromlang :tolang *phrase', :thread => true
121