]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/fish.rb
plugins: use CGI.escape instead of URI.escape where appropriate, remove some checks...
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / fish.rb
index 25816024b61876a4bfa7459a8be0b37c288e5a3f..dcd4a0e3928f505856d10896c5b02faccc8da637 100644 (file)
@@ -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 <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(', ')}"
+    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 <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"
+    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+<td bgcolor=white class=s><div style=padding:10px;>(.*)<\/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+<option value="#{trans_pair}"\s+SELECTED>/
-        m.reply "couldn't parse babelfish response html :("
-      else
-        m.reply "babelfish doesn't support translation from #{trans_from} to #{trans_to}"
+    case body
+    when nil
+      m.reply "couldn't talk to babelfish :("
+    when /^\s+<td bgcolor=white class=s><div style=padding:10px;>(.*)<\/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+<option value="#{trans_pair}"\s+SELECTED>/
+      m.reply "couldn't parse babelfish response html :("
     else
-      m.reply "couldn't talk to babelfish :("
+      m.reply "babelfish doesn't support translation from #{trans_from} to #{trans_to}"
     end
   end
+
+  def cache_mgmt(m, params)
+    cmd = params[:cmd].intern
+    case cmd
+    when :view
+      cache = []
+      @registry.each { |key, val|
+        cache << "%s => %s" % [key, val]
+      }
+      m.reply "translate cache: #{cache.inspect}"
+    when :clear
+      keys = []
+      @registry.each { |key, val|
+        keys << key
+      }
+      keys.each { |key|
+        @registry.delete(key)
+      }
+      cache_mgmt(m, :cmd => 'view')
+    end
+  end
+
 end
+
 plugin = BabelPlugin.new
+
+plugin.default_auth('cache', false)
+
 plugin.map 'translate to :tolang *phrase'
 plugin.map 'translate from :fromlang *phrase'
+plugin.map 'translate cache :cmd', :action => :cache_mgmt, :auth_path => 'cache!', :requirements => { :cmd => /view|clear/ }
 plugin.map 'translate :fromlang :tolang *phrase'