X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fdict.rb;h=95d9fe2e08cd9cb7605dbc9a2e2a807a0761e5ac;hb=bfb9506b75a1cf8118364226bcb8945114e3771c;hp=a948fd06befc2bbfa2bc18f1642efb6aa680b982;hpb=bcacf025a0d2cbba181ad3e55fba30926286f9c8;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/dict.rb b/data/rbot/plugins/dict.rb index a948fd06..95d9fe2e 100644 --- a/data/rbot/plugins/dict.rb +++ b/data/rbot/plugins/dict.rb @@ -1,23 +1,34 @@ -# vim: set sw=2 et: +#-- vim:sw=2:et +#++ # -# dict plugin: provides a link to the definition of a word in one of the supported +# :title: Dictionary lookup plugin for rbot +# +# Author:: Giuseppe "Oblomov" Bilotta +# Copyright:: (C) 2006-2007 Giuseppe Bilotta +# License:: GPL v2 +# +# Provides a link to the definition of a word in one of the supported # dictionaries. Currently available are # * the Oxford dictionary for (British) English # * the De Mauro/Paravia dictionary for Italian # * the Chambers dictionary for English (accepts both US and UK) # -# other plugins can use this one to check if a given word is valid in italian +# Other plugins can use this one to check if a given word is valid in italian # or english by using the is_italian?/is_british?/is_english? methods # -# Author: Giuseppe "Oblomov" Bilotta -# # TODO: cache results and reuse them if get_cached returns a cache copy -require 'uri' - DEMAURO_LEMMA = /(.*?)(?: - (.*?))<\/anchor>/ +CHAMBERS_LEMMA = /

(.*?)<\/span> (.*?)<\/span>(.*?)<\/p>/ class DictPlugin < Plugin + BotConfig.register BotConfigIntegerValue.new('dict.hits', + :default => 3, + :desc => "Number of hits to return from a dictionary lookup") + BotConfig.register BotConfigIntegerValue.new('dict.first_par', + :default => 0, + :desc => "When set to n > 0, the bot will return the first paragraph from the first n dictionary hits") + def initialize super @dmurl = "http://www.demauroparavia.it/" @@ -44,10 +55,11 @@ class DictPlugin < Plugin justcheck = params[:justcheck] word = params[:word].downcase - url = @dmwapurl % URI.escape(word) - xml = @bot.httputil.get_cached(url) + url = @dmwapurl % CGI.escape(word) + xml = nil + info = @bot.httputil.get_response(url) rescue nil + xml = info.body if info if xml.nil? - info = @bot.httputil.last_response info = info ? " (#{info.code} - #{info.message})" : "" return false if justcheck m.reply "An error occurred while looking for #{word}#{info}" @@ -68,14 +80,20 @@ class DictPlugin < Plugin return true if justcheck text += ": " n = 0 - text += entries[0...5].map { |ar| + hits = @bot.config['dict.hits'] + text += entries[0...hits].map { |ar| n += 1 urls << @dmwaplemma % ar[2] "#{n}. #{Bold}#{ar[0]}#{Bold} - #{ar[1].gsub(/<\/?em>/,'')}: #{@dmurl}#{ar[2]}" }.join(" | ") m.reply text - Utils.get_first_pars urls, 5, :http_util => @bot.httputil, :message => m + first_pars = @bot.config['dict.first_par'] + + return unless first_pars > 0 + + Utils.get_first_pars urls, first_pars, :message => m, + :strip => /^\S+\s+-\s+/ end @@ -89,8 +107,8 @@ class DictPlugin < Plugin word = params[:word].join [word, word + "_1"].each { |check| - url = @oxurl % URI.escape(check) - h = @bot.httputil.head(url) + url = @oxurl % CGI.escape(check) + h = @bot.httputil.head(url, :max_redir => 5) if h m.reply("#{word} found: #{url}") unless justcheck return true @@ -109,11 +127,12 @@ class DictPlugin < Plugin justcheck = params[:justcheck] word = params[:word].to_s.downcase - url = @chambersurl % URI.escape(word) - xml = @bot.httputil.get_cached(url) + url = @chambersurl % CGI.escape(word) + xml = nil + info = @bot.httputil.get_response(url) rescue nil + xml = info.body if info case xml when nil - info = @bot.httputil.last_response info = info ? " (#{info.code} - #{info.message})" : "" return false if justcheck m.reply "An error occurred while looking for #{word}#{info}" @@ -125,10 +144,16 @@ class DictPlugin < Plugin when /No exact matches for .*?<\/b>, but the following may be helpful./ return false if justcheck m.reply "Nothing found for #{word}, but see #{url} for possible suggestions" - else - return false if justcheck - m.reply "#{word}: #{url}" + return end + # Else, we have a hit + return true if justcheck + m.reply "#{word}: #{url}" + entries = xml.scan(CHAMBERS_LEMMA) + hits = @bot.config['dict.hits'] + entries[0...hits].map { |ar| + m.reply(("#{Bold}%s#{Bold} #{Underline}%s#{Underline}%s" % ar).ircify_html, :overlong => :truncate) + } end def is_english?(word)