From 0f3e302547363ea237454dda891ddb5de1be4476 Mon Sep 17 00:00:00 2001 From: Tom Gilbert Date: Sat, 9 Oct 2004 01:51:05 +0000 Subject: initial import of rbot --- rbot/plugins/fish.rb | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 rbot/plugins/fish.rb (limited to 'rbot/plugins/fish.rb') diff --git a/rbot/plugins/fish.rb b/rbot/plugins/fish.rb new file mode 100644 index 00000000..ab84ff16 --- /dev/null +++ b/rbot/plugins/fish.rb @@ -0,0 +1,84 @@ +require 'net/http' +require 'uri/common' +Net::HTTP.version_1_2 + +class BabelPlugin < Plugin + def help(plugin, topic="") + "translate to => translate from english to , translate from => translate to english from , translate => translate from to . Languages: en, fr, de, it, pt, es, nl" + end + def privmsg(m) + + proxy_host = nil + proxy_port = nil + + if(ENV['http_proxy']) + if(ENV['http_proxy'] =~ /^http:\/\/(.+):(\d+)$/) + proxy_host = $1 + proxy_port = $2 + end + end + + langs = ["en", "fr", "de", "it", "pt", "es", "nl"] + + query = "/babelfish/tr" + if(m.params =~ /^to\s+(\S+)\s+(.*)/) + trans_from = "en" + trans_to = $1 + trans_text = $2 + elsif(m.params =~ /^from\s+(\S+)\s+(.*)/) + trans_from = $1 + trans_to = "en" + trans_text = $2 + elsif(m.params =~ /^(\S+)\s+(\S+)\s+(.*)/) + trans_from = $1 + trans_to = $2 + trans_text = $3 + else + m.reply "incorrect usage: " + help(m.plugin) + return + end + lang_match = langs.join("|") + unless(trans_from =~ /^(#{lang_match})$/ && trans_to =~ /^(#{lang_match})$/) + m.reply "invalid language: valid languagess are: #{langs.join(' ')}" + return + end + + data_text = URI.escape trans_text + trans_pair = "#{trans_from}_#{trans_to}" + data = "lp=#{trans_pair}&doit=done&intl=1&tt=urltext&urltext=#{data_text}" + + # check cache for previous lookups + if @registry.has_key?("#{trans_pair}/#{data_text}") + m.reply @registry["#{trans_pair}/#{data_text}"] + return + end + + http = Net::HTTP.new("babelfish.altavista.com", 80, proxy_host, proxy_port) + + http.start {|http| + resp = http.post(query, data, {"content-type", + "application/x-www-form-urlencoded"}) + + if (resp.code == "200") + #puts resp.body + resp.body.each_line {|l| + if(l =~ /^\s+
(.*)<\/div>/) + answer = $1 + # cache the answer + if(answer.length > 0) + @registry["#{trans_pair}/#{data_text}"] = answer + end + m.reply answer + return + end + } + m.reply "couldn't parse babelfish response html :(" + else + m.reply "couldn't talk to babelfish :(" + end + } + end +end +plugin = BabelPlugin.new +plugin.register("translate") + -- cgit v1.2.3