X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fdictclient.rb;h=d7e5091c3600d804fb3c1cf5d65e2acdb1526585;hb=16336b4a240a4265d1f2df1e30d7b68d3a924287;hp=2fcdf05a4222d39cf8ad2d877996b67f0342f97f;hpb=6f5528a63b44e610a3d25d7fe583399163d7d2da;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/dictclient.rb b/data/rbot/plugins/dictclient.rb index 2fcdf05a..d7e5091c 100644 --- a/data/rbot/plugins/dictclient.rb +++ b/data/rbot/plugins/dictclient.rb @@ -15,7 +15,11 @@ # requires Ruby/DICT -require 'dict' +begin + require 'dict' +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' @@ -73,11 +77,11 @@ class DictClientPlugin < Plugin 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. @@ -100,15 +104,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])} @@ -118,7 +122,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(', ') } @@ -135,13 +139,13 @@ class DictClientPlugin < Plugin }.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], @@ -156,14 +160,14 @@ class DictClientPlugin < Plugin ) }.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}") % { @@ -171,7 +175,7 @@ class DictClientPlugin < Plugin } end end - + def cmd_strategies(m, params) with_dict(m) do |d| m.reply _("Strategies: %{list}") % { @@ -179,9 +183,18 @@ class DictClientPlugin < Plugin } 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 @@ -189,12 +202,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