]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/messagemapper.rb
message: add #thanks method, similar to okay
[user/henk/code/ruby/rbot.git] / lib / rbot / messagemapper.rb
index c272ab77737efc442ef6f4b2fe688de785756f62..3966bc17f7360ec3853cda6995c479d3ab9e7590 100644 (file)
@@ -1,6 +1,41 @@
+# First of all we add a method to the Regexp class
+class Regexp
+
+  # a Regexp has captures when its source has open parenthesis which are
+  # preceded by an even number of slashes and not followed by a question mark
+  #
+  def has_captures?
+    self.source.match(/(?:^|[^\\])(?:\\\\)*\([^?]/)
+  end
+
+  # We may want to remove captures
+  def remove_captures
+    new = self.source.gsub(/(^|[^\\])((?:\\\\)*)\(([^?])/) {
+      "%s%s(?:%s" % [$1, $2, $3]
+    }
+    Regexp.new(new, self.options)
+  end
+
+  # We may want to remove head and tail anchors
+  def remove_head_tail
+    new = self.source.sub(/^\^/,'').sub(/\$$/,'')
+    Regexp.new(new, self.options)
+  end
+
+  # The MessageMapper cleanup method: does both remove_capture
+  # and remove_head_tail
+  def mm_cleanup
+    new = self.source.gsub(/(^|[^\\])((?:\\\\)*)\(([^?])/) {
+      "%s%s(?:%s" % [$1, $2, $3]
+    }.sub(/^\^/,'').sub(/\$$/,'')
+    Regexp.new(new, self.options)
+  end
+end
+
 module Irc
+class Bot
 
-  # +MessageMapper+ is a class designed to reduce the amount of regexps and
+  # MessageMapper is a class designed to reduce the amount of regexps and
   # string parsing plugins and bot modules need to do, in order to process
   # and respond to messages.
   #
@@ -13,124 +48,239 @@ module Irc
   # A template such as "foo :option :otheroption" will match the string "foo
   # bar baz" and, by default, result in method +foo+ being called, if
   # present, in the parent class. It will receive two parameters, the
-  # Message (derived from BasicUserMessage) and a Hash containing
+  # message (derived from BasicUserMessage) and a Hash containing
   #   {:option => "bar", :otheroption => "baz"}
   # See the #map method for more details.
   class MessageMapper
+
+    class Failure
+      STRING   = "template %{template} failed to recognize message %{message}"
+      FRIENDLY = "I failed to understand the command"
+      attr_reader :template
+      attr_reader :message
+      def initialize(tmpl, msg)
+        @template = tmpl
+        @message = msg
+      end
+
+      def to_s
+        STRING % {
+          :template => template.template,
+          :regexp => template.regexp,
+          :message => message.message,
+          :action => template.options[:action]
+        }
+      end
+    end
+
+    # failures with a friendly message
+    class FriendlyFailure < Failure
+      def friendly
+        self.class::FRIENDLY rescue FRIENDLY
+      end
+    end
+
+    class NotPrivateFailure < FriendlyFailure
+      STRING   = "template %{template} is not configured for private messages"
+      FRIENDLY = "the command must not be given in private"
+    end
+
+    class NotPublicFailure < FriendlyFailure
+      STRING   = "template %{template} is not configured for public messages"
+      FRIENDLY = "the command must not be given in public"
+    end
+
+    class NoMatchFailure < Failure
+      STRING = "%{message} does not match %{template} (%{regex})"
+    end
+
+    class PartialMatchFailure < Failure
+      STRING = "%{message} only matches %{template} (%{regex}) partially"
+    end
+
+    class NoActionFailure < FriendlyFailure
+      STRING   = "%{template} calls undefined action %{action}"
+      FRIENDLY = "uh-ho, somebody forgot to tell me how to do that ..."
+    end
+
     # used to set the method name used as a fallback for unmatched messages.
     # The default fallback is a method called "usage".
     attr_writer :fallback
 
-    # parent::   parent class which will receive mapped messages
+    # _parent_::   parent class which will receive mapped messages
     #
-    # create a new MessageMapper with parent class +parent+. This class will
+    # Create a new MessageMapper with parent class _parent_. This class will
     # receive messages from the mapper via the handle() method.
     def initialize(parent)
       @parent = parent
       @templates = Array.new
-      @fallback = 'usage'
+      @fallback = :usage
     end
 
-    # args:: hash format containing arguments for this template
+    # call-seq: map(botmodule, template, options)
+    #
+    # _botmodule_:: the BotModule which will handle this map
+    # _template_::  a String describing the messages to be matched
+    # _options_::   a Hash holding variouns options
     #
-    # map a template string to an action. example:
-    #   map 'myplugin :parameter1 :parameter2'
-    # (other examples follow). By default, maps a matched string to an
-    # action with the name of the first word in the template. The action is
-    # a method which takes a message and a parameter hash for arguments.
+    # This method is used to register a new MessageTemplate that will map any
+    # BasicUserMessage matching the given _template_ to a corresponding action.
+    # A simple example:
+    #   plugin.map 'myplugin :parameter'
+    # (other examples follow).
     #
-    # The :action => 'method_name' option can be used to override this
-    # default behaviour. Example:
-    #   map 'myplugin :parameter1 :parameter2', :action => 'mymethod'
+    # By default, the action to which the messages are mapped is a method named
+    # like the first word of the template. The
+    #   :action => 'method_name'
+    # option can be used to override this default behaviour. Example:
+    #   plugin.map 'myplugin :parameter', :action => 'mymethod'
     #
-    # By default whether a handler is fired depends on an auth check. The
-    # first component of the string is used for the auth check, unless
-    # overridden via the :auth => 'auth_name' option.
+    # By default whether a handler is fired depends on an auth check. In rbot
+    # versions up to 0.9.10, the first component of the string was used for the
+    # auth check, unless overridden via the :auth => 'auth_name' option. Since
+    # version 0.9.11, a new auth method has been implemented. TODO document.
     #
     # Static parameters (not prefixed with ':' or '*') must match the
     # respective component of the message exactly. Example:
-    #   map 'myplugin :foo is :bar'
+    #   plugin.map 'myplugin :foo is :bar'
     # will only match messages of the form "myplugin something is
     # somethingelse"
     #
     # Dynamic parameters can be specified by a colon ':' to match a single
-    # component (whitespace seperated), or a * to suck up all following
+    # component (whitespace separated), or a * to suck up all following
     # parameters into an array. Example:
-    #   map 'myplugin :parameter1 *rest'
+    #   plugin.map 'myplugin :parameter1 *rest'
     #
     # You can provide defaults for dynamic components using the :defaults
     # parameter. If a component has a default, then it is optional. e.g:
-    #   map 'myplugin :foo :bar', :defaults => {:bar => 'qux'}
+    #   plugin.map 'myplugin :foo :bar', :defaults => {:bar => 'qux'}
     # would match 'myplugin param param2' and also 'myplugin param'. In the
     # latter case, :bar would be provided from the default.
     #
+    # Static and dynamic parameters can also be made optional by wrapping them
+    # in square brackets []. For example
+    #   plugin.map 'myplugin :foo [is] :bar'
+    # will match both 'myplugin something is somethingelse' and 'myplugin
+    # something somethingelse'.
+    #
     # Components can be validated before being allowed to match, for
     # example if you need a component to be a number:
-    #   map 'myplugin :param', :requirements => {:param => /^\d+$/}
+    #   plugin.map 'myplugin :param', :requirements => {:param => /^\d+$/}
     # will only match strings of the form 'myplugin 1234' or some other
     # number.
     #
     # Templates can be set not to match public or private messages using the
     # :public or :private boolean options.
     #
+    # Summary of recognized options:
+    #
+    # action::
+    #   method to call when the template is matched
+    # auth_path::
+    #   TODO document
+    # requirements::
+    #   a Hash whose keys are names of dynamic parameters and whose values are
+    #   regular expressions that the parameters must match
+    # defaults::
+    #   a Hash whose keys are names of dynamic parameters and whose values are
+    #   the values to be assigned to those parameters when they are missing from
+    #   the message. Any dynamic parameter appearing in the :defaults Hash is
+    #   therefore optional
+    # public::
+    #   a boolean (defaults to true) that determines whether the template should
+    #   match public (in channel) messages.
+    # private::
+    #   a boolean (defaults to true) that determines whether the template should
+    #   match private (not in channel) messages.
+    # threaded::
+    #   a boolean (defaults to false) that determines whether the action should be
+    #   called in a separate thread.
+    #
+    #
     # Further examples:
     #
-    #   # match 'karmastats' and call my stats() method
-    #   map 'karmastats', :action => 'stats'
-    #   # match 'karma' with an optional 'key' and call my karma() method
-    #   map 'karma :key', :defaults => {:key => false}
-    #   # match 'karma for something' and call my karma() method
-    #   map 'karma for :key'
+    #   # match 'pointstats' and call my stats() method
+    #   plugin.map 'pointstats', :action => 'stats'
+    #   # match 'points' with an optional 'key' and call my points() method
+    #   plugin.map 'points :key', :defaults => {:key => false}
+    #   # match 'points for something' and call my points() method
+    #   plugin.map 'points for :key'
     #
     #   # two matches, one for public messages in a channel, one for
     #   # private messages which therefore require a channel argument
-    #   map 'urls search :channel :limit :string', :action => 'search',
+    #   plugin.map 'urls search :channel :limit :string',
+    #             :action => 'search',
     #             :defaults => {:limit => 4},
     #             :requirements => {:limit => /^\d+$/},
     #             :public => false
-    #   plugin.map 'urls search :limit :string', :action => 'search',
+    #   plugin.map 'urls search :limit :string',
+    #             :action => 'search',
     #             :defaults => {:limit => 4},
     #             :requirements => {:limit => /^\d+$/},
     #             :private => false
     #
     def map(botmodule, *args)
-      @templates << Template.new(botmodule, *args)
+      @templates << MessageTemplate.new(botmodule, *args)
     end
 
+    # Iterate over each MessageTemplate handled.
     def each
       @templates.each {|tmpl| yield tmpl}
     end
 
+    # Return the last added MessageTemplate
     def last
       @templates.last
     end
 
-    # m::  derived from BasicUserMessage
+    # _m_::  derived from BasicUserMessage
     #
-    # examine the message +m+, comparing it with each map()'d template to
+    # Examine the message _m_, comparing it with each map()'d template to
     # find and process a match. Templates are examined in the order they
     # were map()'d - first match wins.
     #
-    # returns +true+ if a match is found including fallbacks, +false+
+    # Returns +true+ if a match is found including fallbacks, +false+
     # otherwise.
     def handle(m)
       return false if @templates.empty?
       failures = []
       @templates.each do |tmpl|
-        options, failure = tmpl.recognize(m)
-        if options.nil?
-          failures << [tmpl, failure]
+        params = tmpl.recognize(m)
+        if params.kind_of? Failure
+          failures << params
         else
-          action = tmpl.options[:action] ? tmpl.options[:action] : tmpl.items[0]
+          action = tmpl.options[:action]
           unless @parent.respond_to?(action)
-            failures << [tmpl, "class does not respond to action #{action}"]
+            failures << NoActionFailure.new(tmpl, m)
             next
           end
           auth = tmpl.options[:full_auth_path]
           debug "checking auth for #{auth}"
           if m.bot.auth.allow?(auth, m.source, m.replyto)
-            debug "template match found and auth'd: #{action.inspect} #{options.inspect}"
-            @parent.send(action, m, options)
+            debug "template match found and auth'd: #{action.inspect} #{params.inspect}"
+            if !m.in_thread and (tmpl.options[:thread] or tmpl.options[:threaded]) and
+                (defined? WebServiceUser and not m.source.instance_of? WebServiceUser)
+              # Web service: requests are handled threaded anyway and we want to 
+              # wait for the responses.
+
+              # since the message action is in a separate thread, the message may be
+              # delegated to unreplied() before the thread has a chance to actually
+              # mark it as replied. since threading is used mostly for commands that
+              # are known to take some processing time (e.g. download a web page) before
+              # giving an output, we just mark these as 'replied' here
+              m.replied = true
+              Thread.new do
+                begin
+                  @parent.send(action, m, params)
+                rescue Exception => e
+                  error "In threaded action: #{e.message}"
+                  debug e.backtrace.join("\n")
+                end
+              end
+            else
+              @parent.send(action, m, params)
+            end
+
             return true
           end
           debug "auth failed for #{auth}"
@@ -139,13 +289,13 @@ module Irc
           return false
         end
       end
-      failures.each {|f, r|
-        debug "#{f.inspect} => #{r}"
+      failures.each {|r|
+        debug "#{r.template.inspect} => #{r}"
       }
       debug "no handler found, trying fallback"
-      if @fallback != nil && @parent.respond_to?(@fallback)
+      if @fallback && @parent.respond_to?(@fallback)
         if m.bot.auth.allow?(@fallback, m.source, m.replyto)
-          @parent.send(@fallback, m, {})
+          @parent.send(@fallback, m, {:failures => failures})
           return true
         end
       end
@@ -154,35 +304,177 @@ module Irc
 
   end
 
-  class Template
-    attr_reader :defaults # The defaults hash
-    attr_reader :options  # The options hash
-    attr_reader :items
-    attr_reader :regexp
+  # MessageParameter is a class that collects all the necessary information
+  # about a message (dynamic) parameter (the :param or *param that can be found
+  # in a #map).
+  #
+  # It has a +name+ attribute, +multi+ and +optional+ booleans that tell if the
+  # parameter collects more than one word, and if it's optional (respectively).
+  # In the latter case, it can also have a default value.
+  #
+  # It is possible to assign a collector to a MessageParameter. This can be either
+  # a Regexp with captures or an Array or a Hash. The collector defines what the
+  # collect() method is supposed to return.
+  class MessageParameter
+    attr_reader :name
+    attr_writer :multi
+    attr_writer :optional
+    attr_accessor :default
+
+    def initialize(name)
+      self.name = name
+      @multi = false
+      @optional = false
+      @default = nil
+      @regexp = nil
+      @index = nil
+    end
+
+    def name=(val)
+      @name = val.to_sym
+    end
+
+    def multi?
+      @multi
+    end
+
+    def optional?
+      @optional
+    end
+
+    # This method is used to turn a matched item into the actual parameter value.
+    # It only does something when collector= set the @regexp to something. In
+    # this case, _val_ is matched against @regexp and then the match result
+    # specified in @index is selected. As a special case, when @index is nil
+    # the first non-nil captured group is returned.
+    def collect(val)
+      return val unless @regexp
+      mdata = @regexp.match(val)
+      if @index
+        return mdata[@index]
+      else
+        return mdata[1..-1].compact.first
+      end
+    end
+
+    # This method allow the plugin programmer to choose to only pick a subset of the
+    # string matched by a parameter. This is done by passing the collector=()
+    # method either a Regexp with captures or an Array or a Hash.
+    #
+    # When the method is passed a Regexp with captures, the collect() method will
+    # return the first non-nil captured group.
+    #
+    # When the method is passed an Array, it will grab a regexp from the first
+    # element, and possibly an index from the second element. The index can
+    # also be nil.
+    #
+    # When the method is passed a Hash, it will grab a regexp from the :regexp
+    # element, and possibly an index from the :index element. The index can
+    # also be nil.
+    def collector=(val)
+      return unless val
+      case val
+      when Regexp
+        return unless val.has_captures?
+        @regexp = val
+      when Array
+        warning "Collector #{val.inspect} is too long, ignoring extra entries" unless val.length <= 2
+        @regexp = val[0]
+        @index = val[1] rescue nil
+      when Hash
+        raise "Collector #{val.inspect} doesn't have a :regexp key" unless val.has_key?(:regexp)
+        @regexp = val[:regexp]
+        @index = val.fetch(:regexp, nil)
+      end
+      raise "The regexp of collector #{val.inspect} isn't a Regexp" unless @regexp.kind_of?(Regexp)
+      raise "The index of collector #{val.inspect} is present but not an integer " if @index and not @index.kind_of?(Fixnum)
+    end
+
+    def inspect
+      mul = multi? ? " multi" : " single"
+      opt = optional? ? " optional" : " needed"
+      if @regexp
+        reg = " regexp=%s index=%s" % [@regexp, @index]
+      else
+        reg = nil
+      end
+      "<%s %s%s%s%s>" % [self.class, name, mul, opt, reg]
+    end
+  end
+
+  # MessageTemplate is the class that holds the actual message template map()'d
+  # by a BotModule and handled by a MessageMapper
+  #
+  class MessageTemplate
+    attr_reader :defaults  # the defaults hash
+    attr_reader :options   # the options hash
+    attr_reader :template  # the actual template string
+    attr_reader :items     # the collection of dynamic and static items in the template
+    attr_reader :regexp    # the Regexp corresponding to the template
+    attr_reader :botmodule # the BotModule that map()'d this MessageTemplate
 
+    # call-seq: initialize(botmodule, template, opts={})
+    #
+    # Create a new MessageTemplate associated to BotModule _botmodule_, with
+    # template _template_ and options _opts_
+    #
     def initialize(botmodule, template, hash={})
       raise ArgumentError, "Third argument must be a hash!" unless hash.kind_of?(Hash)
       @defaults = hash[:defaults].kind_of?(Hash) ? hash.delete(:defaults) : {}
       @requirements = hash[:requirements].kind_of?(Hash) ? hash.delete(:requirements) : {}
-      # The old way matching was done, this prepared the match items.
-      # Now we use for some preliminary syntax checking and to get the words used in the auth_path
+      @template = template
+      case botmodule
+      when String
+        @botmodule = botmodule
+      when Plugins::BotModule
+        @botmodule = botmodule.name
+      else
+        raise ArgumentError, "#{botmodule.inspect} is not a botmodule nor a botmodule name"
+      end
+
       self.items = template
+      # @dyn_items is an array of MessageParameters, except for the first entry
+      # which is the template
+      @dyn_items = @items.collect { |it|
+        if it.kind_of?(Symbol)
+          i = it.to_s
+          opt = MessageParameter.new(i)
+          if i.sub!(/^\*/,"")
+            opt.name = i
+            opt.multi = true
+          end
+          opt.default = @defaults[opt.name]
+          opt.collector = @requirements[opt.name]
+          opt
+        else
+          nil
+        end
+      }
+      @dyn_items.unshift(template).compact!
+      debug "Items: #{@items.inspect}; dyn items: #{@dyn_items.inspect}"
+
       self.regexp = template
-      debug "Command #{template.inspect} in #{botmodule} will match using #{@regexp}"
+      debug "Command #{template.inspect} in #{@botmodule} will match using #{@regexp}"
+
+      set_auth_path(hash)
+
+      unless hash.has_key?(:action)
+        hash[:action] = items[0]
+      end
+
+      @options = hash
+
+      # debug "Create template #{self.inspect}"
+    end
+
+    def set_auth_path(hash)
       if hash.has_key?(:auth)
-        warning "Command #{template.inspect} in #{botmodule} uses old :auth syntax, please upgrade"
+        warning "Command #{@template.inspect} in #{@botmodule} uses old :auth syntax, please upgrade"
       end
       if hash.has_key?(:full_auth_path)
-        warning "Command #{template.inspect} in #{botmodule} sets :full_auth_path, please don't do this"
+        warning "Command #{@template.inspect} in #{@botmodule} sets :full_auth_path, please don't do this"
       else
-        case botmodule
-        when String
-          pre = botmodule
-        when Plugins::BotModule
-          pre = botmodule.name
-        else
-          raise ArgumentError, "Can't find auth base in #{botmodule.inspect}"
-        end
+        pre = @botmodule
         words = items.reject{ |x|
           x == pre || x.kind_of?(Symbol) || x =~ /\[|\]/
         }
@@ -204,43 +496,21 @@ module Irc
           end
           pre = nil if extra.sub!(/^!/, "")
           post = nil if extra.sub!(/!$/, "")
+          extra = nil if extra.empty?
         else
           extra = nil
         end
         hash[:full_auth_path] = [pre,extra,post].compact.join("::")
-        debug "Command #{template} in #{botmodule} will use authPath #{hash[:full_auth_path]}"
+        debug "Command #{@template} in #{botmodule} will use authPath #{hash[:full_auth_path]}"
         # TODO check if the full_auth_path is sane
       end
-
-      @options = hash
-
-      # @dyn_items is an array of arrays whose first entry is the Symbol
-      # (without the *, if any) of a dynamic item, and whose second entry is
-      # false if the Symbol refers to a single-word item, or true if it's
-      # multiword. @dyn_items.first will be the template.
-      @dyn_items = @items.collect { |it|
-        if it.kind_of?(Symbol)
-          i = it.to_s
-          if i.sub!(/^\*/,"")
-            [i.intern, true]
-          else
-            [i.intern, false]
-          end
-        else
-          nil
-        end
-      }
-      @dyn_items.unshift(template).compact!
-      debug "Items: #{@items.inspect}; dyn items: #{@dyn_items.inspect}"
-
-      # debug "Create template #{self.inspect}"
     end
 
     def items=(str)
       raise ArgumentError, "template #{str.inspect} should be a String" unless str.kind_of?(String)
 
       # split and convert ':xyz' to symbols
-      items = str.strip.split(/\]?\s+\[?/).collect { |c|
+      items = str.strip.split(/\]?\s+\[?|\]?$/).collect { |c|
         # there might be extra (non-alphanumeric) stuff (e.g. punctuation) after the symbol name
         if /^(:|\*)(\w+)(.*)/ =~ c
           sym = ($1 == ':' ) ? $2.intern : "*#{$2}".intern
@@ -287,13 +557,16 @@ module Irc
         debug "Requirements for #{name}: #{has_req.inspect}"
         case has_req
         when nil
-          sub = is_single ? "\\S+" : ".*"
+          sub = is_single ? "\\S+" : ".*?"
         when Regexp
-          # Remove the ^ and $ placed around requirement regexp at times
-          # They were unnecessary first, and are dangerous now
-          sub = has_req.source.sub(/^\^/,'').sub(/\$$/,'')
+          # Remove captures and the ^ and $ that are sometimes placed in requirement regexps
+          sub = has_req.mm_cleanup
         when String
           sub = Regexp.escape(has_req)
+        when Array
+          sub = has_req[0].mm_cleanup
+        when Hash
+          sub = has_req[:regexp].mm_cleanup
         else
           warning "Odd requirement #{has_req.inspect} of class #{has_req.class} for parameter '#{name}'"
           sub = Regexp.escape(has_req.to_s) rescue "\\S+"
@@ -302,12 +575,15 @@ module Irc
         s = "#{not_needed ? "(?:" : ""}#{whites}(#{sub})#{ not_needed ? ")?" : ""}"
       }
       # debug "Replaced dyns: #{rx.inspect}"
-      rx.gsub!(/((?:\\ )*)\\\[/, "(?:\\1")
+      rx.gsub!(/((?:\\ )*)((?:\\\[)+)/, '\2\1')
+      # debug "Corrected optionals spacing: #{rx.inspect}"
+      rx.gsub!(/\\\[/, "(?:")
       rx.gsub!(/\\\]/, ")?")
       # debug "Delimited optionals: #{rx.inspect}"
       rx.gsub!(/(?:\\ )+/, "\\s+")
       # debug "Corrected spaces: #{rx.inspect}"
-      @regexp = Regexp.new(rx)
+      # Created message (such as by fake_message) can contain multiple lines
+      @regexp = /\A#{rx}\z/m
     end
 
     # Recognize the provided string components, returning a hash of
@@ -316,27 +592,26 @@ module Irc
 
       debug "Testing #{m.message.inspect} against #{self.inspect}"
 
-      # Early out
-      return nil, "template #{@dyn_items.first.inspect} is not configured for private messages" if @options.has_key?(:private) && !@options[:private] && m.private?
-      return nil, "template #{@dyn_items.first.inspect} is not configured for public messages" if @options.has_key?(:public) && !@options[:public] && !m.private?
-
-      options = {}
-
       matching = @regexp.match(m.message)
-      return nil, "#{m.message.inspect} doesn't match #{@dyn_items.first.inspect} (#{@regexp})" unless matching
-      return nil, "#{m.message.inspect} only matches #{@dyn_items.first.inspect} (#{@regexp}) partially" unless matching[0] == m.message
+      return MessageMapper::NoMatchFailure.new(self, m) unless matching
+      return MessageMapper::PartialMatchFailure.new(self, m) unless matching[0] == m.message
+
+      return MessageMapper::NotPrivateFailure.new(self, m) if @options.has_key?(:private) && !@options[:private] && m.private?
+      return MessageMapper::NotPublicFailure.new(self, m) if @options.has_key?(:public) && !@options[:public] && !m.private?
 
       debug_match = matching[1..-1].collect{ |d| d.inspect}.join(', ')
       debug "#{m.message.inspect} matched #{@regexp} with #{debug_match}"
-      debug "Associating #{debug_match} with dyn items #{@dyn_items[1..-1].join(', ')}"
+      debug "Associating #{debug_match} with dyn items #{@dyn_items.join(', ')}"
 
-      (@dyn_items.length - 1).downto 1 do |i|
-        it = @dyn_items[i]
-        item = it[0]
-        debug "dyn item #{item} (multi-word: #{it[1].inspect})"
-        if it[1]
+      options = @defaults.dup
+
+      @dyn_items.each_with_index { |it, i|
+        next if i == 0
+        item = it.name
+        debug "dyn item #{item} (multi-word: #{it.multi?.inspect})"
+        if it.multi?
           if matching[i].nil?
-            default = @defaults[item]
+            default = it.default
             case default
             when Array
               value = default.clone
@@ -345,8 +620,8 @@ module Irc
             when nil, false, []
               value = []
             else
-              value = []
               warning "Unmanageable default #{default} detected for :*#{item.to_s}, using []"
+              value = []
             end
             case default
             when String
@@ -361,18 +636,20 @@ module Irc
           def value.to_s
             @string_value
           end
-          options[item] = value
-          debug "set #{item} to #{value.inspect}"
         else
-          value = matching[i] || value = @defaults[item]
-          warning "No default value for option #{item.inspect} specified" unless @defaults.has_key?(item)
-          options[item] = value
-          debug "set #{item} to #{options[item].inspect}"
+          if matching[i].nil?
+            warning "No default value for option #{item.inspect} specified" unless @defaults.has_key?(item)
+            value = it.default
+          else
+            value = it.collect(matching[i])
+          end
         end
-      end
+        options[item] = value
+        debug "set #{item} to #{options[item].inspect}"
+      }
 
       options.delete_if {|k, v| v.nil?} # Remove nil values.
-      return options, nil
+      return options
     end
 
     def inspect
@@ -396,3 +673,4 @@ module Irc
     end
   end
 end
+end