]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/dictclient.rb
quiz: stop quizzes and timers on cleanup
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / dictclient.rb
index 903b3f1ea50299a27b1b6e15d97cf69265fcc3fc..f55defa42cdd3ee261769696c7633394409d9062 100644 (file)
 
 
 # requires Ruby/DICT <http://www.caliban.org/ruby/ruby-dict.shtml>
-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'
@@ -73,16 +82,16 @@ class DictClientPlugin < Plugin
   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.
   # 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
@@ -100,15 +109,15 @@ class DictClientPlugin < Plugin
     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])}
@@ -118,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 <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(', ') }
@@ -135,13 +144,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],
@@ -150,20 +159,20 @@ class DictClientPlugin < Plugin
       if results
         results.collect {|database, matches|
           @bot.config['dictclient.match_format'].gsub(
-            '<matches>', matches.collect {|m| format_headword m}.join(', ')
+            '<matches>', matches.collect {|hit| format_headword hit}.join(', ')
           ).gsub(
             '<database>', 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}") % {
@@ -171,7 +180,7 @@ class DictClientPlugin < Plugin
       }
     end
   end
-  
+
   def cmd_strategies(m, params)
     with_dict(m) do |d|
       m.reply _("Strategies: %{list}") % {
@@ -179,7 +188,7 @@ class DictClientPlugin < Plugin
       }
     end
   end
-    
+
   def help(plugin, topic='')
     case topic
     when 'define'