]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - rbot/plugins/fish.rb
ab84ff16fbc45e93518539f6539d960a36d29e88
[user/henk/code/ruby/rbot.git] / rbot / plugins / fish.rb
1 require 'net/http'
2 require 'uri/common'
3 Net::HTTP.version_1_2
4
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"
8   end
9   def privmsg(m)
10
11     proxy_host = nil
12     proxy_port = nil
13
14     if(ENV['http_proxy'])
15       if(ENV['http_proxy'] =~ /^http:\/\/(.+):(\d+)$/)
16         proxy_host = $1
17         proxy_port = $2
18       end
19     end
20     
21     langs = ["en", "fr", "de", "it", "pt", "es", "nl"]
22
23     query = "/babelfish/tr"
24     if(m.params =~ /^to\s+(\S+)\s+(.*)/)
25       trans_from = "en"
26       trans_to = $1
27       trans_text = $2
28     elsif(m.params =~ /^from\s+(\S+)\s+(.*)/)
29       trans_from = $1
30       trans_to = "en"
31       trans_text = $2
32     elsif(m.params =~ /^(\S+)\s+(\S+)\s+(.*)/)
33       trans_from = $1
34       trans_to = $2
35       trans_text = $3
36     else
37       m.reply "incorrect usage: " + help(m.plugin)
38       return
39     end
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(' ')}"
43       return
44     end
45
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}"
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     http = Net::HTTP.new("babelfish.altavista.com", 80, proxy_host, proxy_port)
57
58     http.start {|http|
59       resp = http.post(query, data, {"content-type",
60       "application/x-www-form-urlencoded"})
61         
62         if (resp.code == "200")
63     #puts resp.body
64         resp.body.each_line {|l|
65                 if(l =~ /^\s+<td bgcolor=white class=s><div style=padding:10px;>(.*)<\/div>/)
66                         answer = $1
67                         # cache the answer
68                         if(answer.length > 0)
69                                 @registry["#{trans_pair}/#{data_text}"] = answer
70                         end
71                         m.reply answer
72                         return
73                 end
74         }
75                 m.reply "couldn't parse babelfish response html :("
76         else
77                 m.reply "couldn't talk to babelfish :("
78         end
79         }
80   end
81 end
82 plugin = BabelPlugin.new
83 plugin.register("translate")
84