]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/fish.rb
defc872dfebb3050f664bcc43c3bfa613fff08c1
[user/henk/code/ruby/rbot.git] / 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       return 'webpage translation is not currently supported'
40       # TODO FIXME
41       url = BASEURL+'/translate_url' +
42         "?lp=#{trans_pair}&trurl=#{data_text}"
43
44       return Utils.get_first_pars([url], 1, :message => m)
45     end
46
47     data = "lp=#{trans_pair}&doit=done&intl=1&tt=urltext&urltext=#{data_text}"
48
49     # check cache for previous lookups
50     if @registry.has_key?("#{trans_pair}/#{data_text}")
51       m.reply @registry["#{trans_pair}/#{data_text}"]
52       return
53     end
54
55     headers = {
56       "content-type" => "application/x-www-form-urlencoded; charset=utf-8"
57     }
58
59     query = "/translate_txt"
60
61     begin
62       body = @bot.httputil.get(BASEURL+query,
63                                :method => :post,
64                                :body => data,
65                                :headers => headers)
66     rescue Exception => e
67       m.reply "http error: #{e.message}"
68       return
69     end
70
71     case body
72     when nil
73       m.reply "couldn't talk to babelfish :("
74     when /^\s*<div id="result"><div style="[^"]*">(.*?)<\/div><\/div>\s*$/
75       answer = $1
76       # cache the answer
77       if(answer.length > 0)
78         @registry["#{trans_pair}/#{data_text}"] = answer
79       end
80       m.reply answer
81       return
82     when /^\s+<option value="#{trans_pair}"\s+SELECTED>/
83       m.reply "couldn't parse babelfish response html :("
84     else
85       m.reply "babelfish doesn't support translation from #{trans_from} to #{trans_to}"
86     end
87   end
88
89   def cache_mgmt(m, params)
90     cmd = params[:cmd].intern
91     case cmd
92     when :view
93       cache = []
94       @registry.each { |key, val|
95         cache << "%s => %s" % [key, val]
96       }
97       m.reply "translate cache: #{cache.inspect}"
98     when :clear
99       keys = []
100       @registry.each { |key, val|
101         keys << key
102       }
103       keys.each { |key|
104         @registry.delete(key)
105       }
106       cache_mgmt(m, :cmd => 'view')
107     end
108   end
109
110 end
111
112 plugin = BabelPlugin.new
113
114 plugin.default_auth('cache', false)
115
116 plugin.map 'translate to :tolang *phrase', :thread => true
117 plugin.map 'translate from :fromlang *phrase', :thread => true
118 plugin.map 'translate cache :cmd', :action => :cache_mgmt, :auth_path => 'cache!', :requirements => { :cmd => /view|clear/ }
119 plugin.map 'translate :fromlang :tolang *phrase', :thread => true
120