X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fdictclient.rb;h=f55defa42cdd3ee261769696c7633394409d9062;hb=052217de30c59206d7025b582d4604557a747470;hp=2cd782cefae88cc232a6b5a719354fbd0c3bc170;hpb=ab7fc621d6b073d3cb5be4a7db3d4f7caf14b18b;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/dictclient.rb b/data/rbot/plugins/dictclient.rb index 2cd782ce..f55defa4 100644 --- a/data/rbot/plugins/dictclient.rb +++ b/data/rbot/plugins/dictclient.rb @@ -15,7 +15,16 @@ # requires Ruby/DICT -require 'dict' +begin + require 'dict' + class ::DICTError + def initialize(msg, code = 1) + super(msg) + end + end +rescue LoadError + raise LoadError, "Ruby/DICT not found, grab it from http://www.caliban.org/ruby/ruby-dict.shtml" +end class ::String # Returns a new string truncated to length 'to' @@ -42,42 +51,47 @@ class ::Definition end def body - definition[1..-1].join.gsub(/\s+/, ' ').strip + # two or more consecutive newlines are replaced with double spaces, while single + # newlines are replaced with single spaces + lb = /\r?\n/ + definition[1..-1].join. + gsub(/\s*(:#{lb}){2,}\s*/, ' '). + gsub(/\s*#{lb}\s*/, ' ').strip end end class DictClientPlugin < Plugin - BotConfig.register BotConfigStringValue.new('dictclient.server', + Config.register Config::StringValue.new('dictclient.server', :default => 'dict.org', :desc => _('Hostname or hostname:port of the DICT server used to lookup words')) - BotConfig.register BotConfigIntegerValue.new('dictclient.max_defs_before_collapse', + Config.register Config::IntegerValue.new('dictclient.max_defs_before_collapse', :default => 4, :desc => _('When multiple databases reply a number of definitions that above this limit, only the database names will be listed. Otherwise, the full definitions from each database are replied')) - BotConfig.register BotConfigIntegerValue.new('dictclient.max_length_per_def', + Config.register Config::IntegerValue.new('dictclient.max_length_per_def', :default => 200, :desc => _('Each definition is truncated to this length')) - BotConfig.register BotConfigStringValue.new('dictclient.headword_format', + Config.register Config::StringValue.new('dictclient.headword_format', :default => "#{Bold}#{Bold}", :desc => _('Format of headwords; will be replaced with the actual word')) - BotConfig.register BotConfigStringValue.new('dictclient.database_format', + Config.register Config::StringValue.new('dictclient.database_format', :default => "#{Underline}#{Underline}", :desc => _('Format of database names; will be replaced with the database name')) - BotConfig.register BotConfigStringValue.new('dictclient.definition_format', + Config.register Config::StringValue.new('dictclient.definition_format', :default => ': -', :desc => _('Format of definitions. will be replaced with the formatted headword, will be replaced with the truncated definition, and with the formatted database name')) - BotConfig.register BotConfigStringValue.new('dictclient.match_format', + Config.register Config::StringValue.new('dictclient.match_format', :default => '––', :desc => _('Format of match results. will be replaced with the formatted headwords, with the formatted database name')) - + def initialize super end - + # create a DICT object, which is passed to the block. after the block finishes, # the DICT object is automatically disconnected. the return value of the block # is returned from this method. # if an IRC message argument is passed, the error message will be replied - def with_dict(m=nil &block) + def with_dict(m=nil, &block) server, port = @bot.config['dictclient.server'].split ':' if @bot.config['dictclient.server'] server ||= 'dict.org' port ||= DICT::DEFAULT_PORT @@ -95,15 +109,15 @@ class DictClientPlugin < Plugin end ret end - + def format_headword(w) @bot.config['dictclient.headword_format'].gsub '', w end - + def format_database(d) @bot.config['dictclient.database_format'].gsub '', d end - + def cmd_define(m, params) phrase = params[:phrase].to_s results = with_dict(m) {|d| d.define(params[:database], params[:phrase])} @@ -113,7 +127,7 @@ class DictClientPlugin < Plugin # the number of definitions is above dictclient.max_defs_before_collapse if results.any? {|r| r.database != results[0].database} && results.length > @bot.config['dictclient.max_defs_before_collapse'] - _("Many definitions for %{phrase} were found in %{databases}. Use 'define from to view a definition.") % + _("Many definitions for %{phrase} were found in %{databases}. Use 'define from to view a definition.") % { :phrase => format_headword(phrase), :databases => results.collect {|r| r.database}.uniq. collect {|d| format_database d}.join(', ') } @@ -127,16 +141,16 @@ class DictClientPlugin < Plugin ).gsub( '', r.body.truncate(@bot.config['dictclient.max_length_per_def']) ) - }.join ' ' + }.join ' | ' end else - _("No definition for %{phrase} found from %{database}.") % + _("No definition for %{phrase} found from %{database}.") % { :phrase => format_headword(phrase), :database => format_database(params[:database]) } end ) end - + def cmd_match(m, params) phrase = params[:phrase].to_s results = with_dict(m) {|d| d.match(params[:database], @@ -145,38 +159,47 @@ class DictClientPlugin < Plugin if results results.collect {|database, matches| @bot.config['dictclient.match_format'].gsub( - '', matches.collect {|m| format_headword m}.join(', ') + '', matches.collect {|hit| format_headword hit}.join(', ') ).gsub( '', format_database(database) ) }.join ' ' else - _("Nothing matched %{query} from %{database} using %{strategy}") % + _("Nothing matched %{query} from %{database} using %{strategy}") % { :query => format_headword(phrase), :database => format_database(params[:database]), :strategy => params[:strategy] } end ) end - + def cmd_databases(m, params) with_dict(m) do |d| m.reply _("Databases: %{list}") % { - :list => d.show_db.collect {|db, des| "#{format_database db}: #{des}"}.join('; ') + :list => d.show_db.collect {|db, des| "#{format_database db}: #{des}"}.join(' | ') } end end - + def cmd_strategies(m, params) with_dict(m) do |d| m.reply _("Strategies: %{list}") % { - :list => d.show_strat.collect {|s, des| "#{s}: #{des}"}.join('; ') + :list => d.show_strat.collect {|s, des| "#{s}: #{des}"}.join(' | ') } end end - + def help(plugin, topic='') - _("define [from ] => Show definition of a phrase; match [using ] [from ] => Show matching phrases; dictclient databases => List databases; dictclient strategies => List strategies") + case topic + when 'define' + _('define [from ] => Show definition of a phrase') + when 'match' + _('match [using ] [from ] => Show phrases matching the given pattern') + when 'server information' + _('dictclient databases => List databases; dictclient strategies => List strategies') + else + _('look up phrases on the configured DICT server. topics: define, match, server information') + end end end @@ -184,12 +207,14 @@ plugin = DictClientPlugin.new plugin.map 'define *phrase [from :database]', :action => 'cmd_define', - :defaults => {:database => DICT::ALL_DATABASES} + :defaults => {:database => DICT::ALL_DATABASES}, + :threaded => true plugin.map 'match *phrase [using :strategy] [from :database]', :action => 'cmd_match', :defaults => {:database => DICT::ALL_DATABASES, - :strategy => DICT::DEFAULT_MATCH_STRATEGY } + :strategy => DICT::DEFAULT_MATCH_STRATEGY }, + :threaded => true -plugin.map 'dictclient databases', :action => 'cmd_databases' -plugin.map 'dictclient strategies', :action => 'cmd_strategies' +plugin.map 'dictclient databases', :action => 'cmd_databases', :thread => true +plugin.map 'dictclient strategies', :action => 'cmd_strategies', :thread => true