From b8373f3006a02084e58979bd021e8a76b5662ff6 Mon Sep 17 00:00:00 2001 From: Tom Gilbert Date: Tue, 26 Jul 2005 22:13:46 +0000 Subject: [PATCH] switch the fish plugin to use the new map mechanism --- rbot/plugins/fish.rb | 65 ++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 39 deletions(-) diff --git a/rbot/plugins/fish.rb b/rbot/plugins/fish.rb index 35b141ef..57aaafc2 100644 --- a/rbot/plugins/fish.rb +++ b/rbot/plugins/fish.rb @@ -6,27 +6,13 @@ 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) - + def translate(m, params) langs = ["en", "fr", "de", "it", "pt", "es", "nl"] - + trans_from = params[:fromlang] ? params[:fromlang] : 'en' + trans_to = params[:tolang] ? params[:tolang] : 'en' + trans_text = params[:phrase].to_s + 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(' ')}" @@ -48,27 +34,28 @@ class BabelPlugin < Plugin 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 - } + + if (resp.code == "200") + resp.body.each_line do |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 + 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") +plugin.map 'translate to :tolang *phrase' +plugin.map 'translate from :fromlang *phrase' +plugin.map 'translate :fromlang :tolang *phrase' -- 2.32.0.93.g670b81a890