# requires Ruby/DICT <http://www.caliban.org/ruby/ruby-dict.shtml>
-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'
Config.register Config::StringValue.new('dictclient.match_format',
:default => '<matches>––<database>',
:desc => _('Format of match results. <matches> will be replaced with the formatted headwords, <database> 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.
end
ret
end
-
+
def format_headword(w)
@bot.config['dictclient.headword_format'].gsub '<headword>', w
end
-
+
def format_database(d)
@bot.config['dictclient.database_format'].gsub '<database>', d
end
-
+
def cmd_define(m, params)
phrase = params[:phrase].to_s
results = with_dict(m) {|d| d.define(params[:database], params[:phrase])}
# 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 <phrase> from <database> to view a definition.") %
+ _("Many definitions for %{phrase} were found in %{databases}. Use 'define <phrase> from <database> to view a definition.") %
{ :phrase => format_headword(phrase),
:databases => results.collect {|r| r.database}.uniq.
collect {|d| format_database d}.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],
)
}.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}") % {
}
end
end
-
+
def cmd_strategies(m, params)
with_dict(m) do |d|
m.reply _("Strategies: %{list}") % {
}
end
end
-
+
def help(plugin, topic='')
- _("define <phrase> [from <database>] => Show definition of a phrase; match <phrase> [using <strategy>] [from <database>] => Show matching phrases; dictclient databases => List databases; dictclient strategies => List strategies")
+ case topic
+ when 'define'
+ _('define <phrase> [from <database>] => Show definition of a phrase')
+ when 'match'
+ _('match <phrase> [using <strategy>] [from <database>] => 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
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