-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}
- BotConfig.register BotConfigEnumValue.new('translate.default_from',
+ Config.register Config::EnumValue.new('translate.default_from',
:values => LANGS, :default => 'en',
:desc => "Default language to translate from")
- BotConfig.register BotConfigEnumValue.new('translate.default_to',
+ Config.register Config::EnumValue.new('translate.default_to',
:values => LANGS, :default => 'en',
: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)
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)
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.map 'translate to :tolang *phrase'
-plugin.map 'translate from :fromlang *phrase'
-plugin.map 'translate :fromlang :tolang *phrase'
+
+plugin.default_auth('cache', false)
+
+plugin.map 'translate to :tolang *phrase', :thread => true
+plugin.map 'translate from :fromlang *phrase', :thread => true
+plugin.map 'translate cache :cmd', :action => :cache_mgmt, :auth_path => 'cache!', :requirements => { :cmd => /view|clear/ }
+plugin.map 'translate :fromlang :tolang *phrase', :thread => true