3 # * do we want to handle a Channel list for each User telling which
\r
4 # Channels is the User on (of those the client is on too)?
\r
5 # We may want this so that when a User leaves all Channels and he hasn't
\r
6 # sent us privmsgs, we know we can remove him from the Server @users list
\r
7 # * Maybe ChannelList and UserList should be HashesOf instead of ArrayOf?
\r
8 # See items marked as TODO Ho.
\r
9 # The framework to do this is now in place, thanks to the new [] method
\r
10 # for NetmaskList, which allows retrieval by Netmask or String
\r
12 # :title: IRC module
\r
16 # This module defines the fundamental building blocks for IRC
\r
18 # Author:: Giuseppe Bilotta (giuseppe.bilotta@gmail.com)
\r
19 # Copyright:: Copyright (c) 2006 Giuseppe Bilotta
\r
26 # We extend the Object class with a method that
\r
27 # checks if the receiver is nil or empty
\r
29 return true unless self
\r
30 return true if self.respond_to? :empty and self.empty?
\r
34 # We alias the to_s method to __to_s__ to make
\r
35 # it accessible in all classes
\r
36 alias :__to_s__ :to_s
\r
39 # The Irc module is used to keep all IRC-related classes
\r
40 # in the same namespace
\r
45 # Due to its Scandinavian origins, IRC has strange case mappings, which
\r
46 # consider the characters <tt>{}|^</tt> as the uppercase
\r
47 # equivalents of # <tt>[]\~</tt>.
\r
49 # This is however not the same on all IRC servers: some use standard ASCII
\r
50 # casemapping, other do not consider <tt>^</tt> as the uppercase of
\r
56 # Create a new casemap with name _name_, uppercase characters _upper_ and
\r
57 # lowercase characters _lower_
\r
59 def initialize(name, upper, lower)
\r
61 raise "Casemap #{name.inspect} already exists!" if @@casemaps.has_key?(@key)
\r
62 @@casemaps[@key] = {
\r
69 # Returns the Casemap with the given name
\r
71 def Casemap.get(name)
\r
72 @@casemaps[name.to_sym][:casemap]
\r
75 # Retrieve the 'uppercase characters' of this Casemap
\r
78 @@casemaps[@key][:upper]
\r
81 # Retrieve the 'lowercase characters' of this Casemap
\r
84 @@casemaps[@key][:lower]
\r
87 # Return a Casemap based on the receiver
\r
93 # A Casemap is represented by its lower/upper mappings
\r
96 self.__to_s__[0..-2] + " #{upper.inspect} ~(#{self})~ #{lower.inspect}>"
\r
99 # As a String we return our name
\r
105 # Two Casemaps are equal if they have the same upper and lower ranges
\r
108 other = arg.to_irc_casemap
\r
109 return self.upper == other.upper && self.lower == other.lower
\r
112 # Give a warning if _arg_ and self are not the same Casemap
\r
115 other = arg.to_irc_casemap
\r
119 warn "Casemap mismatch (#{self.inspect} != #{other.inspect})"
\r
126 # The rfc1459 casemap
\r
128 class RfcCasemap < Casemap
\r
132 super('rfc1459', "\x41-\x5e", "\x61-\x7e")
\r
136 RfcCasemap.instance
\r
138 # The strict-rfc1459 Casemap
\r
140 class StrictRfcCasemap < Casemap
\r
144 super('strict-rfc1459', "\x41-\x5d", "\x61-\x7d")
\r
148 StrictRfcCasemap.instance
\r
150 # The ascii Casemap
\r
152 class AsciiCasemap < Casemap
\r
156 super('ascii', "\x41-\x5a", "\x61-\x7a")
\r
160 AsciiCasemap.instance
\r
163 # This module is included by all classes that are either bound to a server
\r
164 # or should have a casemap.
\r
166 module ServerOrCasemap
\r
168 attr_reader :server
\r
170 # This method initializes the instance variables @server and @casemap
\r
171 # according to the values of the hash keys :server and :casemap in _opts_
\r
173 def init_server_or_casemap(opts={})
\r
174 @server = opts.fetch(:server, nil)
\r
175 raise TypeError, "#{@server} is not a valid Irc::Server" if @server and not @server.kind_of?(Server)
\r
177 @casemap = opts.fetch(:casemap, nil)
\r
180 @server.casemap.must_be(@casemap)
\r
184 @casemap = (@casemap || 'rfc1459').to_irc_casemap
\r
188 # This is an auxiliary method: it returns true if the receiver fits the
\r
189 # server and casemap specified in _opts_, false otherwise.
\r
191 def fits_with_server_and_casemap?(opts={})
\r
192 srv = opts.fetch(:server, nil)
\r
193 cmap = opts.fetch(:casemap, nil)
\r
194 cmap = cmap.to_irc_casemap unless cmap.nil?
\r
197 return true if cmap.nil? or cmap == casemap
\r
199 return true if srv == @server and (cmap.nil? or cmap == casemap)
\r
204 # Returns the casemap of the receiver, by looking at the bound
\r
205 # @server (if possible) or at the @casemap otherwise
\r
208 return @server.casemap if defined?(@server) and @server
\r
212 # Returns a hash with the current @server and @casemap as values of
\r
213 # :server and :casemap
\r
215 def server_and_casemap
\r
217 h[:server] = @server if defined?(@server) and @server
\r
218 h[:casemap] = @casemap if defined?(@casemap) and @casemap
\r
222 # We allow up/downcasing with a different casemap
\r
224 def irc_downcase(cmap=casemap)
\r
225 self.to_s.irc_downcase(cmap)
\r
228 # Up/downcasing something that includes this module returns its
\r
229 # Up/downcased to_s form
\r
235 # We allow up/downcasing with a different casemap
\r
237 def irc_upcase(cmap=casemap)
\r
238 self.to_s.irc_upcase(cmap)
\r
241 # Up/downcasing something that includes this module returns its
\r
242 # Up/downcased to_s form
\r
253 # We start by extending the String class
\r
254 # with some IRC-specific methods
\r
258 # This method returns the Irc::Casemap whose name is the receiver
\r
261 Irc::Casemap.get(self) rescue raise TypeError, "Unkown Irc::Casemap #{self.inspect}"
\r
264 # This method returns a string which is the downcased version of the
\r
265 # receiver, according to the given _casemap_
\r
268 def irc_downcase(casemap='rfc1459')
\r
269 cmap = casemap.to_irc_casemap
\r
270 self.tr(cmap.upper, cmap.lower)
\r
273 # This is the same as the above, except that the string is altered in place
\r
275 # See also the discussion about irc_downcase
\r
277 def irc_downcase!(casemap='rfc1459')
\r
278 cmap = casemap.to_irc_casemap
\r
279 self.tr!(cmap.upper, cmap.lower)
\r
282 # Upcasing functions are provided too
\r
284 # See also the discussion about irc_downcase
\r
286 def irc_upcase(casemap='rfc1459')
\r
287 cmap = casemap.to_irc_casemap
\r
288 self.tr(cmap.lower, cmap.upper)
\r
291 # In-place upcasing
\r
293 # See also the discussion about irc_downcase
\r
295 def irc_upcase!(casemap='rfc1459')
\r
296 cmap = casemap.to_irc_casemap
\r
297 self.tr!(cmap.lower, cmap.upper)
\r
300 # This method checks if the receiver contains IRC glob characters
\r
302 # IRC has a very primitive concept of globs: a <tt>*</tt> stands for "any
\r
303 # number of arbitrary characters", a <tt>?</tt> stands for "one and exactly
\r
304 # one arbitrary character". These characters can be escaped by prefixing them
\r
305 # with a slash (<tt>\\</tt>).
\r
307 # A known limitation of this glob syntax is that there is no way to escape
\r
308 # the escape character itself, so it's not possible to build a glob pattern
\r
309 # where the escape character precedes a glob.
\r
312 self =~ /^[*?]|[^\\][*?]/
\r
315 # This method is used to convert the receiver into a Regular Expression
\r
316 # that matches according to the IRC glob syntax
\r
319 regmask = Regexp.escape(self)
\r
320 regmask.gsub!(/(\\\\)?\\[*?]/) { |m|
\r
329 raise "Unexpected match #{m} when converting #{self}"
\r
332 Regexp.new("^#{regmask}$")
\r
338 # ArrayOf is a subclass of Array whose elements are supposed to be all
\r
339 # of the same class. This is not intended to be used directly, but rather
\r
340 # to be subclassed as needed (see for example Irc::UserList and Irc::NetmaskList)
\r
342 # Presently, only very few selected methods from Array are overloaded to check
\r
343 # if the new elements are the correct class. An orthodox? method is provided
\r
344 # to check the entire ArrayOf against the appropriate class.
\r
346 class ArrayOf < Array
\r
348 attr_reader :element_class
\r
350 # Create a new ArrayOf whose elements are supposed to be all of type _kl_,
\r
351 # optionally filling it with the elements from the Array argument.
\r
353 def initialize(kl, ar=[])
\r
354 raise TypeError, "#{kl.inspect} must be a class name" unless kl.kind_of?(Class)
\r
356 @element_class = kl
\r
361 raise TypeError, "#{self.class} can only be initialized from an Array"
\r
366 self.__to_s__[0..-2].sub(/:[^:]+$/,"[#{@element_class}]\\0") + " #{super}>"
\r
369 # Private method to check the validity of the elements passed to it
\r
370 # and optionally raise an error
\r
372 # TODO should it accept nils as valid?
\r
374 def internal_will_accept?(raising, *els)
\r
376 unless el.kind_of?(@element_class)
\r
377 raise TypeError, "#{el.inspect} is not of class #{@element_class}" if raising
\r
383 private :internal_will_accept?
\r
385 # This method checks if the passed arguments are acceptable for our ArrayOf
\r
387 def will_accept?(*els)
\r
388 internal_will_accept?(false, *els)
\r
391 # This method checks that all elements are of the appropriate class
\r
394 will_accept?(*self)
\r
397 # This method is similar to the above, except that it raises an exception
\r
398 # if the receiver is not valid
\r
401 raise TypeError unless valid?
\r
404 # Overloaded from Array#<<, checks for appropriate class of argument
\r
407 super(el) if internal_will_accept?(true, el)
\r
410 # Overloaded from Array#&, checks for appropriate class of argument elements
\r
414 ArrayOf.new(@element_class, r) if internal_will_accept?(true, *r)
\r
417 # Overloaded from Array#+, checks for appropriate class of argument elements
\r
420 ArrayOf.new(@element_class, super(ar)) if internal_will_accept?(true, *ar)
\r
423 # Overloaded from Array#-, so that an ArrayOf is returned. There is no need
\r
424 # to check the validity of the elements in the argument
\r
427 ArrayOf.new(@element_class, super(ar)) # if internal_will_accept?(true, *ar)
\r
430 # Overloaded from Array#|, checks for appropriate class of argument elements
\r
433 ArrayOf.new(@element_class, super(ar)) if internal_will_accept?(true, *ar)
\r
436 # Overloaded from Array#concat, checks for appropriate class of argument
\r
440 super(ar) if internal_will_accept?(true, *ar)
\r
443 # Overloaded from Array#insert, checks for appropriate class of argument
\r
446 def insert(idx, *ar)
\r
447 super(idx, *ar) if internal_will_accept?(true, *ar)
\r
450 # Overloaded from Array#replace, checks for appropriate class of argument
\r
454 super(ar) if (ar.kind_of?(ArrayOf) && ar.element_class <= @element_class) or internal_will_accept?(true, *ar)
\r
457 # Overloaded from Array#push, checks for appropriate class of argument
\r
461 super(*ar) if internal_will_accept?(true, *ar)
\r
464 # Overloaded from Array#unshift, checks for appropriate class of argument(s)
\r
468 super(el) if internal_will_accept?(true, *els)
\r
472 # We introduce the 'downcase' method, which maps downcase() to all the Array
\r
473 # elements, properly failing when the elements don't have a downcase method
\r
476 self.map { |el| el.downcase }
\r
479 # Modifying methods which we don't handle yet are made private
\r
481 private :[]=, :collect!, :map!, :fill, :flatten!
\r
486 # We extend the Regexp class with an Irc module which will contain some
\r
487 # Irc-specific regexps
\r
491 # We start with some general-purpose ones which will be used in the
\r
492 # Irc module too, but are useful regardless
\r
494 HEX_DIGIT = /[0-9A-Fa-f]/
\r
495 HEX_DIGITS = /#{HEX_DIGIT}+/
\r
496 HEX_OCTET = /#{HEX_DIGIT}#{HEX_DIGIT}?/
\r
497 DEC_OCTET = /[01]?\d?\d|2[0-4]\d|25[0-5]/
\r
498 DEC_IP_ADDR = /#{DEC_OCTET}.#{DEC_OCTET}.#{DEC_OCTET}.#{DEC_OCTET}/
\r
499 HEX_IP_ADDR = /#{HEX_OCTET}.#{HEX_OCTET}.#{HEX_OCTET}.#{HEX_OCTET}/
\r
500 IP_ADDR = /#{DEC_IP_ADDR}|#{HEX_IP_ADDR}/
\r
502 # IPv6, from Resolv::IPv6, without the \A..\z anchors
\r
503 HEX_16BIT = /#{HEX_DIGIT}{1,4}/
\r
504 IP6_8Hex = /(?:#{HEX_16BIT}:){7}#{HEX_16BIT}/
\r
505 IP6_CompressedHex = /((?:#{HEX_16BIT}(?::#{HEX_16BIT})*)?)::((?:#{HEX_16BIT}(?::#{HEX_16BIT})*)?)/
\r
506 IP6_6Hex4Dec = /((?:#{HEX_16BIT}:){6,6})#{DEC_IP_ADDR}/
\r
507 IP6_CompressedHex4Dec = /((?:#{HEX_16BIT}(?::#{HEX_16BIT})*)?)::((?:#{HEX_16BIT}:)*)#{DEC_IP_ADDR}/
\r
508 IP6_ADDR = /(?:#{IP6_8Hex})|(?:#{IP6_CompressedHex})|(?:#{IP6_6Hex4Dec})|(?:#{IP6_CompressedHex4Dec})/
\r
510 # We start with some IRC related regular expressions, used to match
\r
511 # Irc::User nicks and users and Irc::Channel names
\r
513 # For each of them we define two versions of the regular expression:
\r
514 # * a generic one, which should match for any server but may turn out to
\r
515 # match more than a specific server would accept
\r
516 # * an RFC-compliant matcher
\r
520 # Channel-name-matching regexps
\r
521 CHAN_FIRST = /[#&+]/
\r
522 CHAN_SAFE = /![A-Z0-9]{5}/
\r
523 CHAN_ANY = /[^\x00\x07\x0A\x0D ,:]/
\r
524 GEN_CHAN = /(?:#{CHAN_FIRST}|#{CHAN_SAFE})#{CHAN_ANY}+/
\r
525 RFC_CHAN = /#{CHAN_FIRST}#{CHAN_ANY}{1,49}|#{CHAN_SAFE}#{CHAN_ANY}{1,44}/
\r
527 # Nick-matching regexps
\r
528 SPECIAL_CHAR = /[\x5b-\x60\x7b-\x7d]/
\r
529 NICK_FIRST = /#{SPECIAL_CHAR}|[[:alpha:]]/
\r
530 NICK_ANY = /#{SPECIAL_CHAR}|[[:alnum:]]|-/
\r
531 GEN_NICK = /#{NICK_FIRST}#{NICK_ANY}+/
\r
532 RFC_NICK = /#{NICK_FIRST}#{NICK_ANY}{0,8}/
\r
534 USER_CHAR = /[^\x00\x0a\x0d @]/
\r
535 GEN_USER = /#{USER_CHAR}+/
\r
537 # Host-matching regexps
\r
538 HOSTNAME_COMPONENT = /[[:alnum:]](?:[[:alnum:]]|-)*[[:alnum:]]*/
\r
539 HOSTNAME = /#{HOSTNAME_COMPONENT}(?:\.#{HOSTNAME_COMPONENT})*/
\r
540 HOSTADDR = /#{IP_ADDR}|#{IP6_ADDR}/
\r
542 GEN_HOST = /#{HOSTNAME}|#{HOSTADDR}/
\r
544 # # FreeNode network replaces the host of affiliated users with
\r
545 # # 'virtual hosts'
\r
546 # # FIXME we need the true syntax to match it properly ...
\r
547 # PDPC_HOST_PART = /[0-9A-Za-z.-]+/
\r
548 # PDPC_HOST = /#{PDPC_HOST_PART}(?:\/#{PDPC_HOST_PART})+/
\r
550 # # NOTE: the final optional and non-greedy dot is needed because some
\r
551 # # servers (e.g. FreeNode) send the hostname of the services as "services."
\r
552 # # which is not RFC compliant, but sadly done.
\r
553 # GEN_HOST_EXT = /#{PDPC_HOST}|#{GEN_HOST}\.??/
\r
555 # Sadly, different networks have different, RFC-breaking ways of cloaking
\r
556 # the actualy host address: see above for an example to handle FreeNode.
\r
557 # Another example would be Azzurra, wich also inserts a "=" in the
\r
558 # cloacked host. So let's just not care about this and go with the simplest
\r
560 GEN_HOST_EXT = /\S+/
\r
562 # User-matching Regexp
\r
563 GEN_USER_ID = /(#{GEN_NICK})(?:(?:!(#{GEN_USER}))?@(#{GEN_HOST_EXT}))?/
\r
565 # Things such has the BIP proxy send invalid nicks in a complete netmask,
\r
566 # so we want to match this, rather: this matches either a compliant nick
\r
567 # or a a string with a very generic nick, a very generic hostname after an
\r
568 # @ sign, and an optional user after a !
\r
569 BANG_AT = /#{GEN_NICK}|\S+?(?:!\S+?)?@\S+?/
\r
571 # # For Netmask, we want to allow wildcards * and ? in the nick
\r
572 # # (they are already allowed in the user and host part
\r
573 # GEN_NICK_MASK = /(?:#{NICK_FIRST}|[?*])?(?:#{NICK_ANY}|[?*])+/
\r
575 # # Netmask-matching Regexp
\r
576 # GEN_MASK = /(#{GEN_NICK_MASK})(?:(?:!(#{GEN_USER}))?@(#{GEN_HOST_EXT}))?/
\r
586 # A Netmask identifies each user by collecting its nick, username and
\r
587 # hostname in the form <tt>nick!user@host</tt>
\r
589 # Netmasks can also contain glob patterns in any of their components; in
\r
590 # this form they are used to refer to more than a user or to a user
\r
591 # appearing under different forms.
\r
594 # * <tt>*!*@*</tt> refers to everybody
\r
595 # * <tt>*!someuser@somehost</tt> refers to user +someuser+ on host +somehost+
\r
596 # regardless of the nick used.
\r
600 # Netmasks have an associated casemap unless they are bound to a server
\r
602 include ServerOrCasemap
\r
604 attr_reader :nick, :user, :host
\r
607 # Create a new Netmask from string _str_, which must be in the form
\r
608 # _nick_!_user_@_host_
\r
610 # It is possible to specify a server or a casemap in the optional Hash:
\r
611 # these are used to associate the Netmask with the given server and to set
\r
612 # its casemap: if a server is specified and a casemap is not, the server's
\r
613 # casemap is used. If both a server and a casemap are specified, the
\r
614 # casemap must match the server's casemap or an exception will be raised.
\r
616 # Empty +nick+, +user+ or +host+ are converted to the generic glob pattern
\r
618 def initialize(str="", opts={})
\r
619 # First of all, check for server/casemap option
\r
621 init_server_or_casemap(opts)
\r
623 # Now we can see if the given string _str_ is an actual Netmask
\r
624 if str.respond_to?(:to_str)
\r
626 # We match a pretty generic string, to work around non-compliant
\r
628 when /^(?:(\S+?)(?:(?:!(\S+?))?@(\S+))?)?$/
\r
629 # We do assignment using our internal methods
\r
634 raise ArgumentError, "#{str.to_str.inspect} does not represent a valid #{self.class}"
\r
637 raise TypeError, "#{str} cannot be converted to a #{self.class}"
\r
641 # A Netmask is easily converted to a String for the usual representation.
\r
642 # We skip the user or host parts if they are "*", unless we've been asked
\r
643 # for the full form
\r
647 ret << "!" << user unless user == "*"
\r
648 ret << "@" << host unless host == "*"
\r
653 "#{nick}!#{user}@#{host}"
\r
656 alias :to_str :fullform
\r
658 # This method downcases the fullform of the netmask. While this may not be
\r
659 # significantly different from the #downcase() method provided by the
\r
660 # ServerOrCasemap mixin, it's significantly different for Netmask
\r
661 # subclasses such as User whose simple downcasing uses the nick only.
\r
663 def full_irc_downcase(cmap=casemap)
\r
664 self.fullform.irc_downcase(cmap)
\r
667 # full_downcase() will return the fullform downcased according to the
\r
668 # User's own casemap
\r
671 self.full_irc_downcase
\r
674 # Converts the receiver into a Netmask with the given (optional)
\r
675 # server/casemap association. We return self unless a conversion
\r
676 # is needed (different casemap/server)
\r
678 # Subclasses of Netmask will return a new Netmask, using full_downcase
\r
680 def to_irc_netmask(opts={})
\r
681 if self.class == Netmask
\r
682 return self if fits_with_server_and_casemap?(opts)
\r
684 return self.full_downcase.to_irc_netmask(server_and_casemap.merge(opts))
\r
687 # Converts the receiver into a User with the given (optional)
\r
688 # server/casemap association. We return self unless a conversion
\r
689 # is needed (different casemap/server)
\r
691 def to_irc_user(opts={})
\r
692 self.fullform.to_irc_user(server_and_casemap.merge(opts))
\r
695 # Inspection of a Netmask reveals the server it's bound to (if there is
\r
696 # one), its casemap and the nick, user and host part
\r
699 str = self.__to_s__[0..-2]
\r
700 str << " @server=#{@server}" if defined?(@server) and @server
\r
701 str << " @nick=#{@nick.inspect} @user=#{@user.inspect}"
\r
702 str << " @host=#{@host.inspect} casemap=#{casemap.inspect}"
\r
706 # Equality: two Netmasks are equal if they downcase to the same thing
\r
708 # TODO we may want it to try other.to_irc_netmask
\r
711 return false unless other.kind_of?(self.class)
\r
712 self.downcase == other.downcase
\r
715 # This method changes the nick of the Netmask, defaulting to the generic
\r
716 # glob pattern if the result is the null string.
\r
719 @nick = newnick.to_s
\r
720 @nick = "*" if @nick.empty?
\r
723 # This method changes the user of the Netmask, defaulting to the generic
\r
724 # glob pattern if the result is the null string.
\r
727 @user = newuser.to_s
\r
728 @user = "*" if @user.empty?
\r
730 alias :ident= :user=
\r
732 # This method changes the hostname of the Netmask, defaulting to the generic
\r
733 # glob pattern if the result is the null string.
\r
736 @host = newhost.to_s
\r
737 @host = "*" if @host.empty?
\r
740 # We can replace everything at once with data from another Netmask
\r
748 @server = other.server
\r
749 @casemap = other.casemap unless @server
\r
751 replace(other.to_irc_netmask(server_and_casemap))
\r
755 # This method checks if a Netmask is definite or not, by seeing if
\r
756 # any of its components are defined by globs
\r
759 return @nick.has_irc_glob? || @user.has_irc_glob? || @host.has_irc_glob?
\r
764 unless u.has_irc_glob?
\r
765 u.sub!(/^[in]=/, '=') or u.sub!(/^\W(\w+)/, '\1')
\r
770 unless h.has_irc_glob?
\r
772 h.sub!(/x-\w+$/, 'x-*')
\r
774 h.match(/^[^\.]+\.[^\.]+$/) or
\r
775 h.sub!(/^(\d+\.\d+\.\d+\.)\d+$/, '\1*') or
\r
776 h.sub!(/^[^\.]+\./, '*.')
\r
779 return Netmask.new("*!#{u}@#{h}", server_and_casemap)
\r
782 # This method is used to match the current Netmask against another one
\r
784 # The method returns true if each component of the receiver matches the
\r
785 # corresponding component of the argument. By _matching_ here we mean
\r
786 # that any netmask described by the receiver is also described by the
\r
789 # In this sense, matching is rather simple to define in the case when the
\r
790 # receiver has no globs: it is just necessary to check if the argument
\r
791 # describes the receiver, which can be done by matching it against the
\r
792 # argument converted into an IRC Regexp (see String#to_irc_regexp).
\r
794 # The situation is also easy when the receiver has globs and the argument
\r
795 # doesn't, since in this case the result is false.
\r
797 # The more complex case in which both the receiver and the argument have
\r
798 # globs is not handled yet.
\r
801 cmp = arg.to_irc_netmask(:casemap => casemap)
\r
802 debug "Matching #{self.fullform} against #{arg.inspect} (#{cmp.fullform})"
\r
803 [:nick, :user, :host].each { |component|
\r
804 us = self.send(component).irc_downcase(casemap)
\r
805 them = cmp.send(component).irc_downcase(casemap)
\r
806 if us.has_irc_glob? && them.has_irc_glob?
\r
808 warn NotImplementedError
\r
811 return false if us.has_irc_glob? && !them.has_irc_glob?
\r
812 return false unless us =~ them.to_irc_regexp
\r
817 # Case equality. Checks if arg matches self
\r
820 arg.to_irc_netmask(:casemap => casemap).matches?(self)
\r
823 # Sorting is done via the fullform
\r
828 self.fullform.irc_downcase(casemap) <=> arg.fullform.irc_downcase(casemap)
\r
830 self.downcase <=> arg.downcase
\r
837 # A NetmaskList is an ArrayOf <code>Netmask</code>s
\r
839 class NetmaskList < ArrayOf
\r
841 # Create a new NetmaskList, optionally filling it with the elements from
\r
842 # the Array argument fed to it.
\r
844 def initialize(ar=[])
\r
848 # We enhance the [] method by allowing it to pick an element that matches
\r
849 # a given Netmask, a String or a Regexp
\r
850 # TODO take into consideration the opportunity to use select() instead of
\r
851 # find(), and/or a way to let the user choose which one to take (second
\r
855 if args.length == 1
\r
859 mask.matches?(args[0])
\r
863 mask.matches?(args[0].to_irc_netmask(:casemap => mask.casemap))
\r
867 mask.fullform =~ args[0]
\r
884 # We keep extending String, this time adding a method that converts a
\r
885 # String into an Irc::Netmask object
\r
887 def to_irc_netmask(opts={})
\r
888 Irc::Netmask.new(self, opts)
\r
897 # An IRC User is identified by his/her Netmask (which must not have globs).
\r
898 # In fact, User is just a subclass of Netmask.
\r
900 # Ideally, the user and host information of an IRC User should never
\r
901 # change, and it shouldn't contain glob patterns. However, IRC is somewhat
\r
902 # idiosincratic and it may be possible to know the nick of a User much before
\r
903 # its user and host are known. Moreover, some networks (namely Freenode) may
\r
904 # change the hostname of a User when (s)he identifies with Nickserv.
\r
906 # As a consequence, we must allow changes to a User host and user attributes.
\r
907 # We impose a restriction, though: they may not contain glob patterns, except
\r
908 # for the special case of an unknown user/host which is represented by a *.
\r
910 # It is possible to create a totally unknown User (e.g. for initializations)
\r
911 # by setting the nick to * too.
\r
914 # * see if it's worth to add the other USER data
\r
915 # * see if it's worth to add NICKSERV status
\r
917 class User < Netmask
\r
920 attr_accessor :real_name
\r
922 # Create a new IRC User from a given Netmask (or anything that can be converted
\r
923 # into a Netmask) provided that the given Netmask does not have globs.
\r
925 def initialize(str="", opts={})
\r
927 raise ArgumentError, "#{str.inspect} must not have globs (unescaped * or ?)" if nick.has_irc_glob? && nick != "*"
\r
928 raise ArgumentError, "#{str.inspect} must not have globs (unescaped * or ?)" if user.has_irc_glob? && user != "*"
\r
929 raise ArgumentError, "#{str.inspect} must not have globs (unescaped * or ?)" if host.has_irc_glob? && host != "*"
\r
931 @real_name = String.new
\r
934 # The nick of a User may be changed freely, but it must not contain glob patterns.
\r
937 raise "Can't change the nick to #{newnick}" if defined?(@nick) and newnick.has_irc_glob?
\r
941 # We have to allow changing the user of an Irc User due to some networks
\r
942 # (e.g. Freenode) changing hostmasks on the fly. We still check if the new
\r
943 # user data has glob patterns though.
\r
946 raise "Can't change the username to #{newuser}" if defined?(@user) and newuser.has_irc_glob?
\r
950 # We have to allow changing the host of an Irc User due to some networks
\r
951 # (e.g. Freenode) changing hostmasks on the fly. We still check if the new
\r
952 # host data has glob patterns though.
\r
955 raise "Can't change the hostname to #{newhost}" if defined?(@host) and newhost.has_irc_glob?
\r
959 # Checks if a User is well-known or not by looking at the hostname and user
\r
962 return nick != "*" && user != "*" && host != "*"
\r
965 # Is the user away?
\r
971 # Set the away status of the user. Use away=(nil) or away=(false)
\r
982 # Since to_irc_user runs the same checks on server and channel as
\r
983 # to_irc_netmask, we just try that and return self if it works.
\r
985 # Subclasses of User will return self if possible.
\r
987 def to_irc_user(opts={})
\r
988 return self if fits_with_server_and_casemap?(opts)
\r
989 return self.full_downcase.to_irc_user(opts)
\r
992 # We can replace everything at once with data from another User
\r
997 self.nick = other.nick
\r
998 self.user = other.user
\r
999 self.host = other.host
\r
1000 @server = other.server
\r
1001 @casemap = other.casemap unless @server
\r
1002 @away = other.away?
\r
1004 self.replace(other.to_irc_user(server_and_casemap))
\r
1008 def modes_on(channel)
\r
1011 channel.modes_of(self)
\r
1013 return @server.channel(channel).modes_of(self) if @server
\r
1014 raise "Can't resolve channel #{channel}"
\r
1018 def is_op?(channel)
\r
1021 channel.has_op?(self)
\r
1023 return @server.channel(channel).has_op?(self) if @server
\r
1024 raise "Can't resolve channel #{channel}"
\r
1028 def is_voice?(channel)
\r
1031 channel.has_voice?(self)
\r
1033 return @server.channel(channel).has_voice?(self) if @server
\r
1034 raise "Can't resolve channel #{channel}"
\r
1040 # A UserList is an ArrayOf <code>User</code>s
\r
1041 # We derive it from NetmaskList, which allows us to inherit any special
\r
1042 # NetmaskList method
\r
1044 class UserList < NetmaskList
\r
1046 # Create a new UserList, optionally filling it with the elements from
\r
1047 # the Array argument fed to it.
\r
1049 def initialize(ar=[])
\r
1051 @element_class = User
\r
1054 # Convenience method: convert the UserList to a list of nicks. The indices
\r
1058 self.map { |user| user.nick }
\r
1067 # We keep extending String, this time adding a method that converts a
\r
1068 # String into an Irc::User object
\r
1070 def to_irc_user(opts={})
\r
1071 Irc::User.new(self, opts)
\r
1078 # An IRC Channel is identified by its name, and it has a set of properties:
\r
1079 # * a Channel::Topic
\r
1081 # * a set of Channel::Modes
\r
1083 # The Channel::Topic and Channel::Mode classes are defined within the
\r
1084 # Channel namespace because they only make sense there
\r
1089 # Mode on a Channel
\r
1092 attr_reader :channel
\r
1093 def initialize(ch)
\r
1100 # Channel modes of type A manipulate lists
\r
1102 # Example: b (banlist)
\r
1104 class ModeTypeA < Mode
\r
1106 def initialize(ch)
\r
1108 @list = NetmaskList.new
\r
1112 nm = @channel.server.new_netmask(val)
\r
1113 @list << nm unless @list.include?(nm)
\r
1117 nm = @channel.server.new_netmask(val)
\r
1124 # Channel modes of type B need an argument
\r
1126 # Example: k (key)
\r
1128 class ModeTypeB < Mode
\r
1129 def initialize(ch)
\r
1137 alias :value :status
\r
1144 @arg = nil if @arg == val
\r
1150 # Channel modes that change the User prefixes are like
\r
1151 # Channel modes of type B, except that they manipulate
\r
1152 # lists of Users, so they are somewhat similar to channel
\r
1155 class UserMode < ModeTypeB
\r
1157 alias :users :list
\r
1158 def initialize(ch)
\r
1160 @list = UserList.new
\r
1164 u = @channel.server.user(val)
\r
1165 @list << u unless @list.include?(u)
\r
1169 u = @channel.server.user(val)
\r
1176 # Channel modes of type C need an argument when set,
\r
1177 # but not when they get reset
\r
1179 # Example: l (limit)
\r
1181 class ModeTypeC < Mode
\r
1182 def initialize(ch)
\r
1190 alias :value :status
\r
1203 # Channel modes of type D are basically booleans
\r
1205 # Example: m (moderate)
\r
1207 class ModeTypeD < Mode
\r
1208 def initialize(ch)
\r
1228 # A Topic represents the topic of a channel. It consists of
\r
1229 # the topic itself, who set it and when
\r
1232 attr_accessor :text, :set_by, :set_on
\r
1235 # Create a new Topic setting the text, the creator and
\r
1236 # the creation time
\r
1238 def initialize(text="", set_by="", set_on=Time.new)
\r
1240 @set_by = set_by.to_irc_netmask
\r
1244 # Replace a Topic with another one
\r
1246 def replace(topic)
\r
1247 raise TypeError, "#{topic.inspect} is not of class #{self.class}" unless topic.kind_of?(self.class)
\r
1248 @text = topic.text.dup
\r
1249 @set_by = topic.set_by.dup
\r
1250 @set_on = topic.set_on.dup
\r
1255 def to_irc_channel_topic
\r
1268 # Returns an Irc::Channel::Topic with self as text
\r
1270 def to_irc_channel_topic
\r
1271 Irc::Channel::Topic.new(self)
\r
1280 # Here we start with the actual Channel class
\r
1284 include ServerOrCasemap
\r
1285 attr_reader :name, :topic, :mode, :users
\r
1289 str = self.__to_s__[0..-2]
\r
1290 str << " on server #{server}" if server
\r
1291 str << " @name=#{@name.inspect} @topic=#{@topic.text.inspect}"
\r
1292 str << " @users=[#{user_nicks.sort.join(', ')}]"
\r
1298 def to_irc_channel
\r
1304 @users.map { |u| u.downcase }
\r
1307 # Checks if the receiver already has a user with the given _nick_
\r
1309 def has_user?(nick)
\r
1310 @users.index(nick.to_irc_user(server_and_casemap))
\r
1313 # Returns the user with nick _nick_, if available
\r
1315 def get_user(nick)
\r
1316 idx = has_user?(nick)
\r
1317 @users[idx] if idx
\r
1320 # Adds a user to the channel
\r
1322 def add_user(user, opts={})
\r
1323 silent = opts.fetch(:silent, false)
\r
1324 if has_user?(user)
\r
1325 warn "Trying to add user #{user} to channel #{self} again" unless silent
\r
1327 @users << user.to_irc_user(server_and_casemap)
\r
1331 # Creates a new channel with the given name, optionally setting the topic
\r
1332 # and an initial users list.
\r
1334 # No additional info is created here, because the channel flags and userlists
\r
1335 # allowed depend on the server.
\r
1337 def initialize(name, topic=nil, users=[], opts={})
\r
1338 raise ArgumentError, "Channel name cannot be empty" if name.to_s.empty?
\r
1339 warn "Unknown channel prefix #{name[0].chr}" if name !~ /^[&#+!]/
\r
1340 raise ArgumentError, "Invalid character in #{name.inspect}" if name =~ /[ \x07,]/
\r
1342 init_server_or_casemap(opts)
\r
1346 @topic = topic ? topic.to_irc_channel_topic : Channel::Topic.new
\r
1348 @users = UserList.new
\r
1358 # Removes a user from the channel
\r
1360 def delete_user(user)
\r
1361 @mode.each { |sym, mode|
\r
1362 mode.reset(user) if mode.kind_of?(UserMode)
\r
1364 @users.delete(user)
\r
1367 # The channel prefix
\r
1373 # A channel is local to a server if it has the '&' prefix
\r
1379 # A channel is modeless if it has the '+' prefix
\r
1385 # A channel is safe if it has the '!' prefix
\r
1391 # A channel is normal if it has the '#' prefix
\r
1397 # Create a new mode
\r
1399 def create_mode(sym, kl)
\r
1400 @mode[sym.to_sym] = kl.new(self)
\r
1403 def modes_of(user)
\r
1405 @mode.map { |s, m|
\r
1406 l << s if (m.class <= UserMode and m.list[user])
\r
1412 @mode.has_key?(:o) and @mode[:o].list[user]
\r
1415 def has_voice?(user)
\r
1416 @mode.has_key?(:v) and @mode[:v].list[user]
\r
1421 # A ChannelList is an ArrayOf <code>Channel</code>s
\r
1423 class ChannelList < ArrayOf
\r
1425 # Create a new ChannelList, optionally filling it with the elements from
\r
1426 # the Array argument fed to it.
\r
1428 def initialize(ar=[])
\r
1429 super(Channel, ar)
\r
1432 # Convenience method: convert the ChannelList to a list of channel names.
\r
1433 # The indices are preserved
\r
1436 self.map { |chan| chan.name }
\r
1446 # We keep extending String, this time adding a method that converts a
\r
1447 # String into an Irc::Channel object
\r
1449 def to_irc_channel(opts={})
\r
1450 Irc::Channel.new(self, opts)
\r
1459 # An IRC Server represents the Server the client is connected to.
\r
1463 attr_reader :hostname, :version, :usermodes, :chanmodes
\r
1464 alias :to_s :hostname
\r
1465 attr_reader :supports, :capabilities
\r
1467 attr_reader :channels, :users
\r
1471 @channels.map { |ch| ch.downcase }
\r
1476 @users.map { |u| u.downcase }
\r
1480 chans, users = [@channels, @users].map {|d|
\r
1482 a.downcase <=> b.downcase
\r
1488 str = self.__to_s__[0..-2]
\r
1489 str << " @hostname=#{hostname}"
\r
1490 str << " @channels=#{chans}"
\r
1491 str << " @users=#{users}"
\r
1495 # Create a new Server, with all instance variables reset to nil (for
\r
1496 # scalar variables), empty channel and user lists and @supports
\r
1497 # initialized to the default values for all known supported features.
\r
1500 @hostname = @version = @usermodes = @chanmodes = nil
\r
1502 @channels = ChannelList.new
\r
1504 @users = UserList.new
\r
1506 reset_capabilities
\r
1509 # Resets the server capabilities
\r
1511 def reset_capabilities
\r
1513 :casemapping => 'rfc1459'.to_irc_casemap,
\r
1516 :typea => nil, # Type A: address lists
\r
1517 :typeb => nil, # Type B: needs a parameter
\r
1518 :typec => nil, # Type C: needs a parameter when set
\r
1519 :typed => nil # Type D: must not have a parameter
\r
1521 :channellen => 50,
\r
1522 :chantypes => "#&!+",
\r
1532 :modes => [:o, :v],
\r
1533 :prefixes => [:"@", :+]
\r
1536 :statusmsg => nil,
\r
1541 @capabilities = {}
\r
1544 # Convert a mode (o, v, h, ...) to the corresponding
\r
1545 # prefix (@, +, %, ...). See also mode_for_prefix
\r
1546 def prefix_for_mode(mode)
\r
1547 return @supports[:prefix][:prefixes][
\r
1548 @supports[:prefix][:modes].index(mode.to_sym)
\r
1552 # Convert a prefix (@, +, %, ...) to the corresponding
\r
1553 # mode (o, v, h, ...). See also prefix_for_mode
\r
1554 def mode_for_prefix(pfx)
\r
1555 return @supports[:prefix][:modes][
\r
1556 @supports[:prefix][:prefixes].index(pfx.to_sym)
\r
1560 # Resets the Channel and User list
\r
1563 @users.reverse_each { |u|
\r
1566 @channels.reverse_each { |u|
\r
1571 # Clears the server
\r
1575 reset_capabilities
\r
1576 @hostname = @version = @usermodes = @chanmodes = nil
\r
1579 # This method is used to parse a 004 RPL_MY_INFO line
\r
1581 def parse_my_info(line)
\r
1582 ar = line.split(' ')
\r
1585 @usermodes = ar[2]
\r
1586 @chanmodes = ar[3]
\r
1589 def noval_warn(key, val, &block)
\r
1591 yield if block_given?
\r
1593 warn "No #{key.to_s.upcase} value"
\r
1597 def val_warn(key, val, &block)
\r
1598 if val == true or val == false or val.nil?
\r
1599 yield if block_given?
\r
1601 warn "No #{key.to_s.upcase} value must be specified, got #{val}"
\r
1604 private :noval_warn, :val_warn
\r
1606 # This method is used to parse a 005 RPL_ISUPPORT line
\r
1608 # See the RPL_ISUPPORT draft[http://www.irc.org/tech_docs/draft-brocklesby-irc-isupport-03.txt]
\r
1610 def parse_isupport(line)
\r
1611 debug "Parsing ISUPPORT #{line.inspect}"
\r
1612 ar = line.split(' ')
\r
1615 prekey, val = en.split('=', 2)
\r
1616 if prekey =~ /^-(.*)/
\r
1617 key = $1.downcase.to_sym
\r
1620 key = prekey.downcase.to_sym
\r
1624 noval_warn(key, val) {
\r
1625 @supports[key] = val.to_irc_casemap
\r
1627 when :chanlimit, :idchan, :maxlist, :targmax
\r
1628 noval_warn(key, val) {
\r
1629 groups = val.split(',')
\r
1631 k, v = g.split(':')
\r
1632 @supports[key][k] = v.to_i || 0
\r
1633 if @supports[key][k] == 0
\r
1634 warn "Deleting #{key} limit of 0 for #{k}"
\r
1635 @supports[key].delete(k)
\r
1640 noval_warn(key, val) {
\r
1641 groups = val.split(',')
\r
1642 @supports[key][:typea] = groups[0].scan(/./).map { |x| x.to_sym}
\r
1643 @supports[key][:typeb] = groups[1].scan(/./).map { |x| x.to_sym}
\r
1644 @supports[key][:typec] = groups[2].scan(/./).map { |x| x.to_sym}
\r
1645 @supports[key][:typed] = groups[3].scan(/./).map { |x| x.to_sym}
\r
1647 when :channellen, :kicklen, :modes, :topiclen
\r
1649 @supports[key] = val.to_i
\r
1651 @supports[key] = nil
\r
1654 @supports[key] = val # can also be nil
\r
1657 @supports[key] = val
\r
1660 @supports[key] = val
\r
1662 noval_warn(key, val) {
\r
1663 reparse += "CHANLIMIT=(chantypes):#{val} "
\r
1666 noval_warn(key, val) {
\r
1667 @supports[:targmax]['PRIVMSG'] = val.to_i
\r
1668 @supports[:targmax]['NOTICE'] = val.to_i
\r
1671 noval_warn(key, val) {
\r
1672 @supports[key] = val
\r
1675 noval_warn(key, val) {
\r
1676 @supports[key] = val.to_i
\r
1680 val.scan(/\((.*)\)(.*)/) { |m, p|
\r
1681 @supports[key][:modes] = m.scan(/./).map { |x| x.to_sym}
\r
1682 @supports[key][:prefixes] = p.scan(/./).map { |x| x.to_sym}
\r
1685 @supports[key][:modes] = nil
\r
1686 @supports[key][:prefixes] = nil
\r
1689 val_warn(key, val) {
\r
1690 @supports[key] = val.nil? ? true : val
\r
1693 noval_warn(key, val) {
\r
1694 @supports[key] = val.scan(/./)
\r
1697 noval_warn(key, val) {
\r
1698 @supports[key] = val.split(',')
\r
1701 @supports[key] = val.nil? ? true : val
\r
1704 reparse.gsub!("(chantypes)",@supports[:chantypes])
\r
1705 parse_isupport(reparse) unless reparse.empty?
\r
1708 # Returns the casemap of the server.
\r
1711 @supports[:casemapping]
\r
1714 # Returns User or Channel depending on what _name_ can be
\r
1717 def user_or_channel?(name)
\r
1718 if supports[:chantypes].include?(name[0])
\r
1725 # Returns the actual User or Channel object matching _name_
\r
1727 def user_or_channel(name)
\r
1728 if supports[:chantypes].include?(name[0])
\r
1729 return channel(name)
\r
1735 # Checks if the receiver already has a channel with the given _name_
\r
1737 def has_channel?(name)
\r
1738 return false if name.nil_or_empty?
\r
1739 channel_names.index(name.irc_downcase(casemap))
\r
1741 alias :has_chan? :has_channel?
\r
1743 # Returns the channel with name _name_, if available
\r
1745 def get_channel(name)
\r
1746 return nil if name.nil_or_empty?
\r
1747 idx = has_channel?(name)
\r
1748 channels[idx] if idx
\r
1750 alias :get_chan :get_channel
\r
1752 # Create a new Channel object bound to the receiver and add it to the
\r
1753 # list of <code>Channel</code>s on the receiver, unless the channel was
\r
1754 # present already. In this case, the default action is to raise an
\r
1755 # exception, unless _fails_ is set to false. An exception can also be
\r
1756 # raised if _str_ is nil or empty, again only if _fails_ is set to true;
\r
1757 # otherwise, the method just returns nil
\r
1759 def new_channel(name, topic=nil, users=[], fails=true)
\r
1760 if name.nil_or_empty?
\r
1761 raise "Tried to look for empty or nil channel name #{name.inspect}" if fails
\r
1764 ex = get_chan(name)
\r
1766 raise "Channel #{name} already exists on server #{self}" if fails
\r
1770 prefix = name[0].chr
\r
1772 # Give a warning if the new Channel goes over some server limits.
\r
1774 # FIXME might need to raise an exception
\r
1776 warn "#{self} doesn't support channel prefix #{prefix}" unless @supports[:chantypes].include?(prefix)
\r
1777 warn "#{self} doesn't support channel names this long (#{name.length} > #{@supports[:channellen]})" unless name.length <= @supports[:channellen]
\r
1779 # Next, we check if we hit the limit for channels of type +prefix+
\r
1780 # if the server supports +chanlimit+
\r
1782 @supports[:chanlimit].keys.each { |k|
\r
1783 next unless k.include?(prefix)
\r
1785 channel_names.each { |n|
\r
1786 count += 1 if k.include?(n[0])
\r
1788 # raise IndexError, "Already joined #{count} channels with prefix #{k}" if count == @supports[:chanlimit][k]
\r
1789 warn "Already joined #{count}/#{@supports[:chanlimit][k]} channels with prefix #{k}, we may be going over server limits" if count >= @supports[:chanlimit][k]
\r
1792 # So far, everything is fine. Now create the actual Channel
\r
1794 chan = Channel.new(name, topic, users, :server => self)
\r
1796 # We wade through +prefix+ and +chanmodes+ to create appropriate
\r
1797 # lists and flags for this channel
\r
1799 @supports[:prefix][:modes].each { |mode|
\r
1800 chan.create_mode(mode, Channel::UserMode)
\r
1801 } if @supports[:prefix][:modes]
\r
1803 @supports[:chanmodes].each { |k, val|
\r
1808 chan.create_mode(mode, Channel::ModeTypeA)
\r
1812 chan.create_mode(mode, Channel::ModeTypeB)
\r
1816 chan.create_mode(mode, Channel::ModeTypeC)
\r
1820 chan.create_mode(mode, Channel::ModeTypeD)
\r
1827 # debug "Created channel #{chan.inspect}"
\r
1832 # Returns the Channel with the given _name_ on the server,
\r
1833 # creating it if necessary. This is a short form for
\r
1834 # new_channel(_str_, nil, [], +false+)
\r
1837 new_channel(str,nil,[],false)
\r
1840 # Remove Channel _name_ from the list of <code>Channel</code>s
\r
1842 def delete_channel(name)
\r
1843 idx = has_channel?(name)
\r
1844 raise "Tried to remove unmanaged channel #{name}" unless idx
\r
1845 @channels.delete_at(idx)
\r
1848 # Checks if the receiver already has a user with the given _nick_
\r
1850 def has_user?(nick)
\r
1851 return false if nick.nil_or_empty?
\r
1852 user_nicks.index(nick.irc_downcase(casemap))
\r
1855 # Returns the user with nick _nick_, if available
\r
1857 def get_user(nick)
\r
1858 idx = has_user?(nick)
\r
1859 @users[idx] if idx
\r
1862 # Create a new User object bound to the receiver and add it to the list
\r
1863 # of <code>User</code>s on the receiver, unless the User was present
\r
1864 # already. In this case, the default action is to raise an exception,
\r
1865 # unless _fails_ is set to false. An exception can also be raised
\r
1866 # if _str_ is nil or empty, again only if _fails_ is set to true;
\r
1867 # otherwise, the method just returns nil
\r
1869 def new_user(str, fails=true)
\r
1870 if str.nil_or_empty?
\r
1871 raise "Tried to look for empty or nil user name #{str.inspect}" if fails
\r
1874 tmp = str.to_irc_user(:server => self)
\r
1875 old = get_user(tmp.nick)
\r
1876 # debug "Tmp: #{tmp.inspect}"
\r
1877 # debug "Old: #{old.inspect}"
\r
1879 # debug "User already existed as #{old.inspect}"
\r
1882 # debug "Both were known"
\r
1883 # Do not raise an error: things like Freenode change the hostname after identification
\r
1884 warning "User #{tmp.nick} has inconsistent Netmasks! #{self} knows #{old.inspect} but access was tried with #{tmp.inspect}" if old != tmp
\r
1885 raise "User #{tmp} already exists on server #{self}" if fails
\r
1887 if old.fullform.downcase != tmp.fullform.downcase
\r
1889 # debug "Known user now #{old.inspect}"
\r
1894 warn "#{self} doesn't support nicknames this long (#{tmp.nick.length} > #{@supports[:nicklen]})" unless tmp.nick.length <= @supports[:nicklen]
\r
1896 return @users.last
\r
1900 # Returns the User with the given Netmask on the server,
\r
1901 # creating it if necessary. This is a short form for
\r
1902 # new_user(_str_, +false+)
\r
1905 new_user(str, false)
\r
1908 # Deletes User _user_ from Channel _channel_
\r
1910 def delete_user_from_channel(user, channel)
\r
1911 channel.delete_user(user)
\r
1914 # Remove User _someuser_ from the list of <code>User</code>s.
\r
1915 # _someuser_ must be specified with the full Netmask.
\r
1917 def delete_user(someuser)
\r
1918 idx = has_user?(someuser)
\r
1919 raise "Tried to remove unmanaged user #{user}" unless idx
\r
1920 have = self.user(someuser)
\r
1921 @channels.each { |ch|
\r
1922 delete_user_from_channel(have, ch)
\r
1924 @users.delete_at(idx)
\r
1927 # Create a new Netmask object with the appropriate casemap
\r
1929 def new_netmask(str)
\r
1930 str.to_irc_netmask(:server => self)
\r
1933 # Finds all <code>User</code>s on server whose Netmask matches _mask_
\r
1935 def find_users(mask)
\r
1936 nm = new_netmask(mask)
\r
1937 @users.inject(UserList.new) {
\r
1939 if user.user == "*" or user.host == "*"
\r
1940 list << user if user.nick.irc_downcase(casemap) =~ nm.nick.irc_downcase(casemap).to_irc_regexp
\r
1942 list << user if user.matches?(nm)
\r