]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/irc.rb
Previous attempt at cleaning up the prefix matcher were too restrictive, try using...
[user/henk/code/ruby/rbot.git] / lib / rbot / irc.rb
index f3964d837695a5a68cdffcecbfc9b787d417710a..c2c1e82fdd518c30889a1eed9cad0830857ea9de 100644 (file)
@@ -461,6 +461,13 @@ class ArrayOf < Array
     }\r
   end\r
 \r
+  # We introduce the 'downcase' method, which maps downcase() to all the Array\r
+  # elements, properly failing when the elements don't have a downcase method\r
+  #\r
+  def downcase\r
+    self.map { |el| el.downcase }\r
+  end\r
+\r
   # Modifying methods which we don't handle yet are made private\r
   #\r
   private :[]=, :collect!, :map!, :fill, :flatten!\r
@@ -468,6 +475,103 @@ class ArrayOf < Array
 end\r
 \r
 \r
+# We extend the Regexp class with an Irc module which will contain some\r
+# Irc-specific regexps\r
+#\r
+class Regexp\r
+\r
+  # We start with some general-purpose ones which will be used in the\r
+  # Irc module too, but are useful regardless\r
+  DIGITS = /\d+/\r
+  HEX_DIGIT = /[0-9A-Fa-f]/\r
+  HEX_DIGITS = /#{HEX_DIGIT}+/\r
+  HEX_OCTET = /#{HEX_DIGIT}#{HEX_DIGIT}?/\r
+  DEC_OCTET = /[01]?\d?\d|2[0-4]\d|25[0-5]/\r
+  DEC_IP_ADDR = /#{DEC_OCTET}.#{DEC_OCTET}.#{DEC_OCTET}.#{DEC_OCTET}/\r
+  HEX_IP_ADDR = /#{HEX_OCTET}.#{HEX_OCTET}.#{HEX_OCTET}.#{HEX_OCTET}/\r
+  IP_ADDR = /#{DEC_IP_ADDR}|#{HEX_IP_ADDR}/\r
+\r
+  # IPv6, from Resolv::IPv6, without the \A..\z anchors\r
+  HEX_16BIT = /#{HEX_DIGIT}{1,4}/\r
+  IP6_8Hex = /(?:#{HEX_16BIT}:){7}#{HEX_16BIT}/\r
+  IP6_CompressedHex = /((?:#{HEX_16BIT}(?::#{HEX_16BIT})*)?)::((?:#{HEX_16BIT}(?::#{HEX_16BIT})*)?)/\r
+  IP6_6Hex4Dec = /((?:#{HEX_16BIT}:){6,6})#{DEC_IP_ADDR}/\r
+  IP6_CompressedHex4Dec = /((?:#{HEX_16BIT}(?::#{HEX_16BIT})*)?)::((?:#{HEX_16BIT}:)*)#{DEC_IP_ADDR}/\r
+  IP6_ADDR = /(?:#{IP6_8Hex})|(?:#{IP6_CompressedHex})|(?:#{IP6_6Hex4Dec})|(?:#{IP6_CompressedHex4Dec})/\r
+\r
+  # We start with some IRC related regular expressions, used to match\r
+  # Irc::User nicks and users and Irc::Channel names\r
+  #\r
+  # For each of them we define two versions of the regular expression:\r
+  #  * a generic one, which should match for any server but may turn out to\r
+  #    match more than a specific server would accept\r
+  #  * an RFC-compliant matcher\r
+  #\r
+  module Irc\r
+\r
+    # Channel-name-matching regexps\r
+    CHAN_FIRST = /[#&+]/\r
+    CHAN_SAFE = /![A-Z0-9]{5}/\r
+    CHAN_ANY = /[^\x00\x07\x0A\x0D ,:]/\r
+    GEN_CHAN = /(?:#{CHAN_FIRST}|#{CHAN_SAFE})#{CHAN_ANY}+/\r
+    RFC_CHAN = /#{CHAN_FIRST}#{CHAN_ANY}{1,49}|#{CHAN_SAFE}#{CHAN_ANY}{1,44}/\r
+\r
+    # Nick-matching regexps\r
+    SPECIAL_CHAR = /[\x5b-\x60\x7b-\x7d]/\r
+    NICK_FIRST = /#{SPECIAL_CHAR}|[[:alpha:]]/\r
+    NICK_ANY = /#{SPECIAL_CHAR}|[[:alnum:]]|-/\r
+    GEN_NICK = /#{NICK_FIRST}#{NICK_ANY}+/\r
+    RFC_NICK = /#{NICK_FIRST}#{NICK_ANY}{0,8}/\r
+\r
+    USER_CHAR = /[^\x00\x0a\x0d @]/\r
+    GEN_USER = /#{USER_CHAR}+/\r
+\r
+    # Host-matching regexps\r
+    HOSTNAME_COMPONENT = /[[:alnum:]](?:[[:alnum:]]|-)*[[:alnum:]]*/\r
+    HOSTNAME = /#{HOSTNAME_COMPONENT}(?:\.#{HOSTNAME_COMPONENT})*/\r
+    HOSTADDR = /#{IP_ADDR}|#{IP6_ADDR}/\r
+\r
+    GEN_HOST = /#{HOSTNAME}|#{HOSTADDR}/\r
+\r
+    # # FreeNode network replaces the host of affiliated users with\r
+    # # 'virtual hosts' \r
+    # # FIXME we need the true syntax to match it properly ...\r
+    # PDPC_HOST_PART = /[0-9A-Za-z.-]+/\r
+    # PDPC_HOST = /#{PDPC_HOST_PART}(?:\/#{PDPC_HOST_PART})+/\r
+\r
+    # # NOTE: the final optional and non-greedy dot is needed because some\r
+    # # servers (e.g. FreeNode) send the hostname of the services as "services."\r
+    # # which is not RFC compliant, but sadly done.\r
+    # GEN_HOST_EXT = /#{PDPC_HOST}|#{GEN_HOST}\.??/ \r
+\r
+    # Sadly, different networks have different, RFC-breaking ways of cloaking\r
+    # the actualy host address: see above for an example to handle FreeNode.\r
+    # Another example would be Azzurra, wich also inserts a "=" in the\r
+    # cloacked host. So let's just not care about this and go with the simplest\r
+    # thing:\r
+    GEN_HOST_EXT = /\S+/\r
+\r
+    # User-matching Regexp\r
+    GEN_USER_ID = /(#{GEN_NICK})(?:(?:!(#{GEN_USER}))?@(#{GEN_HOST_EXT}))?/\r
+\r
+    # Things such has the BIP proxy send invalid nicks in a complete netmask,\r
+    # so we want to match this, rather: this matches either a compliant nick\r
+    # or a a string with a very generic nick, a very generic hostname after an\r
+    # @ sign, and an optional user after a !\r
+    BANG_AT = /#{GEN_NICK}|\S+?(?:!\S+?)?@\S+?/\r
+\r
+    # # For Netmask, we want to allow wildcards * and ? in the nick\r
+    # # (they are already allowed in the user and host part\r
+    # GEN_NICK_MASK = /(?:#{NICK_FIRST}|[?*])?(?:#{NICK_ANY}|[?*])+/\r
+\r
+    # # Netmask-matching Regexp\r
+    # GEN_MASK = /(#{GEN_NICK_MASK})(?:(?:!(#{GEN_USER}))?@(#{GEN_HOST_EXT}))?/\r
+\r
+  end\r
+\r
+end\r
+\r
+\r
 module Irc\r
 \r
 \r
@@ -510,7 +614,9 @@ module Irc
       # Now we can see if the given string _str_ is an actual Netmask\r
       if str.respond_to?(:to_str)\r
         case str.to_str\r
-        when /^(?:(\S+?)(?:!(\S+)@(?:(\S+))?)?)?$/\r
+          # We match a pretty generic string, to work around non-compliant\r
+          # servers\r
+        when /^(?:(\S+?)(?:(?:!(\S+?))?@(\S+))?)?$/\r
           # We do assignment using our internal methods\r
           self.nick = $1\r
           self.user = $2\r
@@ -540,7 +646,7 @@ module Irc
       if self.class == Netmask\r
         return self if fits_with_server_and_casemap?(opts)\r
       end\r
-      return self.fullform.to_irc_netmask(server_and_casemap.merge(opts))\r
+      return self.downcase.to_irc_netmask(opts)\r
     end\r
 \r
     # Converts the receiver into a User with the given (optional)\r
@@ -637,7 +743,7 @@ module Irc
     #\r
     def matches?(arg)\r
       cmp = arg.to_irc_netmask(:casemap => casemap)\r
-      debug "Matching #{self.fullform} against #{arg.fullform}"\r
+      debug "Matching #{self.fullform} against #{arg.inspect} (#{cmp.fullform})"\r
       [:nick, :user, :host].each { |component|\r
         us = self.send(component).irc_downcase(casemap)\r
         them = cmp.send(component).irc_downcase(casemap)\r
@@ -684,7 +790,10 @@ module Irc
     end\r
 \r
     # We enhance the [] method by allowing it to pick an element that matches\r
-    # a given Netmask or String\r
+    # a given Netmask, a String or a Regexp\r
+    # TODO take into consideration the opportunity to use select() instead of\r
+    # find(), and/or a way to let the user choose which one to take (second\r
+    # argument?)\r
     #\r
     def [](*args)\r
       if args.length == 1\r
@@ -697,6 +806,10 @@ module Irc
           self.find { |mask|\r
             mask.matches?(args[0].to_irc_netmask(:casemap => mask.casemap))\r
           }\r
+        when Regexp\r
+          self.find { |mask|\r
+            mask.fullform =~ args[0]\r
+          }\r
         else\r
           super(*args)\r
         end\r
@@ -807,6 +920,21 @@ module Irc
       end\r
     end\r
 \r
+    # Users can be either simply downcased (their nick only)\r
+    # or fully downcased: this will return the fullform downcased\r
+    # according to the given casemap.\r
+    #\r
+    def full_irc_downcase(cmap=casemap)\r
+      self.fullform.irc_downcase(cmap)\r
+    end\r
+\r
+    # full_downcase() will return the fullform downcased according to the\r
+    # User's own casemap\r
+    #\r
+    def full_downcase\r
+      self.full_irc_downcase\r
+    end\r
+\r
     # Since to_irc_user runs the same checks on server and channel as\r
     # to_irc_netmask, we just try that and return self if it works.\r
     #\r
@@ -814,7 +942,7 @@ module Irc
     #\r
     def to_irc_user(opts={})\r
       return self if fits_with_server_and_casemap?(opts)\r
-      return self.fullform.to_irc_user(server_and_casemap(opts))\r
+      return self.full_downcase.to_irc_user(opts)\r
     end\r
 \r
     # We can replace everything at once with data from another User\r
@@ -850,6 +978,13 @@ module Irc
       @element_class = User\r
     end\r
 \r
+    # Convenience method: convert the UserList to a list of nicks. The indices\r
+    # are preserved\r
+    #\r
+    def nicks\r
+      self.map { |user| user.nick }\r
+    end\r
+\r
   end\r
 \r
 end\r
@@ -1206,6 +1341,13 @@ module Irc
       super(Channel, ar)\r
     end\r
 \r
+    # Convenience method: convert the ChannelList to a list of channel names.\r
+    # The indices are preserved\r
+    #\r
+    def names\r
+      self.map { |chan| chan.name }\r
+    end\r
+\r
   end\r
 \r
 end\r
@@ -1288,8 +1430,8 @@ module Irc
           :typec => nil, # Type C: needs a parameter when set\r
           :typed => nil  # Type D: must not have a parameter\r
         },\r
-        :channellen => 200,\r
-        :chantypes => "#&",\r
+        :channellen => 50,\r
+        :chantypes => "#&!+",\r
         :excepts => nil,\r
         :idchan => {},\r
         :invex => nil,\r
@@ -1299,8 +1441,8 @@ module Irc
         :network => nil,\r
         :nicklen => 9,\r
         :prefix => {\r
-          :modes => 'ov'.scan(/./),\r
-          :prefixes => '@+'.scan(/./)\r
+          :modes => [:o, :v],\r
+          :prefixes => [:"@", :+]\r
         },\r
         :safelist => nil,\r
         :statusmsg => nil,\r
@@ -1327,6 +1469,7 @@ module Irc
     def clear\r
       reset_lists\r
       reset_capabilities\r
+      @hostname = @version = @usermodes = @chanmodes = nil\r
     end\r
 \r
     # This method is used to parse a 004 RPL_MY_INFO line\r