X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Ffish.rb;h=dcd4a0e3928f505856d10896c5b02faccc8da637;hb=90a205831d3b120360df5dcb5f61136b0ed31dd0;hp=25816024b61876a4bfa7459a8be0b37c288e5a3f;hpb=11cf84a26f2e3f1db3a59f24625c2e2f4a1d0259;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/fish.rb b/data/rbot/plugins/fish.rb index 25816024..dcd4a0e3 100644 --- a/data/rbot/plugins/fish.rb +++ b/data/rbot/plugins/fish.rb @@ -1,7 +1,3 @@ -require 'net/http' -require 'uri/common' -Net::HTTP.version_1_2 - class BabelPlugin < Plugin LANGS = %w{en fr de it pt es nl ru zh zt el ja ko} @@ -13,9 +9,14 @@ class BabelPlugin < Plugin :desc => "Default language to translate to") def help(plugin, topic="") - from = @bot.config['translate.default_from'] - to = @bot.config['translate.default_to'] - "translate to => translate from #{from} to , translate from => translate to #{to} from , translate => translate from to . If is an http url, translates the referenced webpage and returns the 1st content paragraph. Languages: #{LANGS.join(', ')}" + case topic + when 'cache' + "translate cache [view|clear] => view or clear the translate cache contents" + else + from = @bot.config['translate.default_from'] + to = @bot.config['translate.default_to'] + "translate to => translate from #{from} to , translate from => translate to #{to} from , translate => translate from to . If is an http url, translates the referenced webpage and returns the 1st content paragraph. Languages: #{LANGS.join(', ')}. Other topics: cache" + end end def translate(m, params) @@ -30,7 +31,7 @@ class BabelPlugin < Plugin return end - data_text = URI.escape trans_text + data_text = CGI.escape trans_text trans_pair = "#{trans_from}_#{trans_to}" if (trans_text =~ /^http:\/\//) && (URI.parse(trans_text) rescue nil) @@ -55,43 +56,62 @@ class BabelPlugin < Plugin query = "/babelfish/tr" begin - resp = @bot.httputil.get_response('http://babelfish.altavista.com'+query, - :method => :post, - :body => data, - :headers => headers) + body = @bot.httputil.get('http://babelfish.altavista.com'+query, + :method => :post, + :body => data, + :headers => headers) rescue Exception => e m.reply "http error: #{e.message}" return end - if (resp.code == "200") - lines = Array.new - resp.body.each_line { |l| lines.push l } - - l = lines.join(" ") - debug "babelfish response: #{l}" - - case l - when /^\s+
(.*)<\/div><\/td>\s*<\/tr>/m - answer = $1.gsub(/\s*[\r\n]+\s*/,' ') - # cache the answer - if(answer.length > 0) - @registry["#{trans_pair}/#{data_text}"] = answer - end - m.reply answer - return - when /^\s+