3 # * do we want to handle a Channel list for each User telling which
4 # Channels is the User on (of those the client is on too)?
5 # We may want this so that when a User leaves all Channels and he hasn't
6 # sent us privmsgs, we know we can remove him from the Server @users list
7 # * Maybe ChannelList and UserList should be HashesOf instead of ArrayOf?
8 # See items marked as TODO Ho.
9 # The framework to do this is now in place, thanks to the new [] method
10 # for NetmaskList, which allows retrieval by Netmask or String
16 # This module defines the fundamental building blocks for IRC
18 # Author:: Giuseppe Bilotta (giuseppe.bilotta@gmail.com)
24 # We extend the Object class with a method that
25 # checks if the receiver is nil or empty
27 return true unless self
28 return true if self.respond_to? :empty? and self.empty?
32 # We alias the to_s method to __to_s__ to make
33 # it accessible in all classes
37 # The Irc module is used to keep all IRC-related classes
38 # in the same namespace
43 # Due to its Scandinavian origins, IRC has strange case mappings, which
44 # consider the characters <tt>{}|^</tt> as the uppercase
45 # equivalents of # <tt>[]\~</tt>.
47 # This is however not the same on all IRC servers: some use standard ASCII
48 # casemapping, other do not consider <tt>^</tt> as the uppercase of
54 # Create a new casemap with name _name_, uppercase characters _upper_ and
55 # lowercase characters _lower_
57 def initialize(name, upper, lower)
59 raise "Casemap #{name.inspect} already exists!" if @@casemaps.has_key?(@key)
67 # Returns the Casemap with the given name
70 @@casemaps[name.to_sym][:casemap]
73 # Retrieve the 'uppercase characters' of this Casemap
76 @@casemaps[@key][:upper]
79 # Retrieve the 'lowercase characters' of this Casemap
82 @@casemaps[@key][:lower]
85 # Return a Casemap based on the receiver
91 # A Casemap is represented by its lower/upper mappings
94 self.__to_s__[0..-2] + " #{upper.inspect} ~(#{self})~ #{lower.inspect}>"
97 # As a String we return our name
103 # Two Casemaps are equal if they have the same upper and lower ranges
106 other = arg.to_irc_casemap
107 return self.upper == other.upper && self.lower == other.lower
110 # Give a warning if _arg_ and self are not the same Casemap
113 other = arg.to_irc_casemap
117 warn "Casemap mismatch (#{self.inspect} != #{other.inspect})"
124 # The rfc1459 casemap
126 class RfcCasemap < Casemap
130 super('rfc1459', "\x41-\x5e", "\x61-\x7e")
136 # The strict-rfc1459 Casemap
138 class StrictRfcCasemap < Casemap
142 super('strict-rfc1459', "\x41-\x5d", "\x61-\x7d")
146 StrictRfcCasemap.instance
150 class AsciiCasemap < Casemap
154 super('ascii', "\x41-\x5a", "\x61-\x7a")
158 AsciiCasemap.instance
161 # This module is included by all classes that are either bound to a server
162 # or should have a casemap.
164 module ServerOrCasemap
168 # This method initializes the instance variables @server and @casemap
169 # according to the values of the hash keys :server and :casemap in _opts_
171 def init_server_or_casemap(opts={})
172 @server = opts.fetch(:server, nil)
173 raise TypeError, "#{@server} is not a valid Irc::Server" if @server and not @server.kind_of?(Server)
175 @casemap = opts.fetch(:casemap, nil)
178 @server.casemap.must_be(@casemap)
182 @casemap = (@casemap || 'rfc1459').to_irc_casemap
186 # This is an auxiliary method: it returns true if the receiver fits the
187 # server and casemap specified in _opts_, false otherwise.
189 def fits_with_server_and_casemap?(opts={})
190 srv = opts.fetch(:server, nil)
191 cmap = opts.fetch(:casemap, nil)
192 cmap = cmap.to_irc_casemap unless cmap.nil?
195 return true if cmap.nil? or cmap == casemap
197 return true if srv == @server and (cmap.nil? or cmap == casemap)
202 # Returns the casemap of the receiver, by looking at the bound
203 # @server (if possible) or at the @casemap otherwise
206 return @server.casemap if defined?(@server) and @server
210 # Returns a hash with the current @server and @casemap as values of
211 # :server and :casemap
213 def server_and_casemap
215 h[:server] = @server if defined?(@server) and @server
216 h[:casemap] = @casemap if defined?(@casemap) and @casemap
217 h[:casemap] ||= @server.casemap if defined?(@server) and @server
221 # We allow up/downcasing with a different casemap
223 def irc_downcase(cmap=casemap)
224 self.to_s.irc_downcase(cmap)
227 # Up/downcasing something that includes this module returns its
228 # Up/downcased to_s form
234 # We allow up/downcasing with a different casemap
236 def irc_upcase(cmap=casemap)
237 self.to_s.irc_upcase(cmap)
240 # Up/downcasing something that includes this module returns its
241 # Up/downcased to_s form
252 # We start by extending the String class
253 # with some IRC-specific methods
257 # This method returns the Irc::Casemap whose name is the receiver
260 Irc::Casemap.get(self) rescue raise TypeError, "Unkown Irc::Casemap #{self.inspect}"
263 # This method returns a string which is the downcased version of the
264 # receiver, according to the given _casemap_
267 def irc_downcase(casemap='rfc1459')
268 cmap = casemap.to_irc_casemap
269 self.tr(cmap.upper, cmap.lower)
272 # This is the same as the above, except that the string is altered in place
274 # See also the discussion about irc_downcase
276 def irc_downcase!(casemap='rfc1459')
277 cmap = casemap.to_irc_casemap
278 self.tr!(cmap.upper, cmap.lower)
281 # Upcasing functions are provided too
283 # See also the discussion about irc_downcase
285 def irc_upcase(casemap='rfc1459')
286 cmap = casemap.to_irc_casemap
287 self.tr(cmap.lower, cmap.upper)
292 # See also the discussion about irc_downcase
294 def irc_upcase!(casemap='rfc1459')
295 cmap = casemap.to_irc_casemap
296 self.tr!(cmap.lower, cmap.upper)
299 # This method checks if the receiver contains IRC glob characters
301 # IRC has a very primitive concept of globs: a <tt>*</tt> stands for "any
302 # number of arbitrary characters", a <tt>?</tt> stands for "one and exactly
303 # one arbitrary character". These characters can be escaped by prefixing them
304 # with a slash (<tt>\\</tt>).
306 # A known limitation of this glob syntax is that there is no way to escape
307 # the escape character itself, so it's not possible to build a glob pattern
308 # where the escape character precedes a glob.
311 self =~ /^[*?]|[^\\][*?]/
314 # This method is used to convert the receiver into a Regular Expression
315 # that matches according to the IRC glob syntax
318 regmask = Regexp.escape(self)
319 regmask.gsub!(/(\\\\)?\\[*?]/) { |m|
328 raise "Unexpected match #{m} when converting #{self}"
331 Regexp.new("^#{regmask}$")
337 # ArrayOf is a subclass of Array whose elements are supposed to be all
338 # of the same class. This is not intended to be used directly, but rather
339 # to be subclassed as needed (see for example Irc::UserList and Irc::NetmaskList)
341 # Presently, only very few selected methods from Array are overloaded to check
342 # if the new elements are the correct class. An orthodox? method is provided
343 # to check the entire ArrayOf against the appropriate class.
345 class ArrayOf < Array
347 attr_reader :element_class
349 # Create a new ArrayOf whose elements are supposed to be all of type _kl_,
350 # optionally filling it with the elements from the Array argument.
352 def initialize(kl, ar=[])
353 raise TypeError, "#{kl.inspect} must be a class name" unless kl.kind_of?(Class)
360 raise TypeError, "#{self.class} can only be initialized from an Array"
365 self.__to_s__[0..-2].sub(/:[^:]+$/,"[#{@element_class}]\\0") + " #{super}>"
368 # Private method to check the validity of the elements passed to it
369 # and optionally raise an error
371 # TODO should it accept nils as valid?
373 def internal_will_accept?(raising, *els)
375 unless el.kind_of?(@element_class)
376 raise TypeError, "#{el.inspect} is not of class #{@element_class}" if raising
382 private :internal_will_accept?
384 # This method checks if the passed arguments are acceptable for our ArrayOf
386 def will_accept?(*els)
387 internal_will_accept?(false, *els)
390 # This method checks that all elements are of the appropriate class
396 # This method is similar to the above, except that it raises an exception
397 # if the receiver is not valid
400 raise TypeError unless valid?
403 # Overloaded from Array#<<, checks for appropriate class of argument
406 super(el) if internal_will_accept?(true, el)
409 # Overloaded from Array#&, checks for appropriate class of argument elements
413 ArrayOf.new(@element_class, r) if internal_will_accept?(true, *r)
416 # Overloaded from Array#+, checks for appropriate class of argument elements
419 ArrayOf.new(@element_class, super(ar)) if internal_will_accept?(true, *ar)
422 # Overloaded from Array#-, so that an ArrayOf is returned. There is no need
423 # to check the validity of the elements in the argument
426 ArrayOf.new(@element_class, super(ar)) # if internal_will_accept?(true, *ar)
429 # Overloaded from Array#|, checks for appropriate class of argument elements
432 ArrayOf.new(@element_class, super(ar)) if internal_will_accept?(true, *ar)
435 # Overloaded from Array#concat, checks for appropriate class of argument
439 super(ar) if internal_will_accept?(true, *ar)
442 # Overloaded from Array#insert, checks for appropriate class of argument
446 super(idx, *ar) if internal_will_accept?(true, *ar)
449 # Overloaded from Array#replace, checks for appropriate class of argument
453 super(ar) if (ar.kind_of?(ArrayOf) && ar.element_class <= @element_class) or internal_will_accept?(true, *ar)
456 # Overloaded from Array#push, checks for appropriate class of argument
460 super(*ar) if internal_will_accept?(true, *ar)
463 # Overloaded from Array#unshift, checks for appropriate class of argument(s)
467 super(el) if internal_will_accept?(true, *els)
471 # We introduce the 'downcase' method, which maps downcase() to all the Array
472 # elements, properly failing when the elements don't have a downcase method
475 self.map { |el| el.downcase }
478 # Modifying methods which we don't handle yet are made private
480 private :[]=, :collect!, :map!, :fill, :flatten!
485 # We extend the Regexp class with an Irc module which will contain some
486 # Irc-specific regexps
490 # We start with some general-purpose ones which will be used in the
491 # Irc module too, but are useful regardless
493 HEX_DIGIT = /[0-9A-Fa-f]/
494 HEX_DIGITS = /#{HEX_DIGIT}+/
495 HEX_OCTET = /#{HEX_DIGIT}#{HEX_DIGIT}?/
496 DEC_OCTET = /[01]?\d?\d|2[0-4]\d|25[0-5]/
497 DEC_IP_ADDR = /#{DEC_OCTET}.#{DEC_OCTET}.#{DEC_OCTET}.#{DEC_OCTET}/
498 HEX_IP_ADDR = /#{HEX_OCTET}.#{HEX_OCTET}.#{HEX_OCTET}.#{HEX_OCTET}/
499 IP_ADDR = /#{DEC_IP_ADDR}|#{HEX_IP_ADDR}/
501 # IPv6, from Resolv::IPv6, without the \A..\z anchors
502 HEX_16BIT = /#{HEX_DIGIT}{1,4}/
503 IP6_8Hex = /(?:#{HEX_16BIT}:){7}#{HEX_16BIT}/
504 IP6_CompressedHex = /((?:#{HEX_16BIT}(?::#{HEX_16BIT})*)?)::((?:#{HEX_16BIT}(?::#{HEX_16BIT})*)?)/
505 IP6_6Hex4Dec = /((?:#{HEX_16BIT}:){6,6})#{DEC_IP_ADDR}/
506 IP6_CompressedHex4Dec = /((?:#{HEX_16BIT}(?::#{HEX_16BIT})*)?)::((?:#{HEX_16BIT}:)*)#{DEC_IP_ADDR}/
507 IP6_ADDR = /(?:#{IP6_8Hex})|(?:#{IP6_CompressedHex})|(?:#{IP6_6Hex4Dec})|(?:#{IP6_CompressedHex4Dec})/
509 # We start with some IRC related regular expressions, used to match
510 # Irc::User nicks and users and Irc::Channel names
512 # For each of them we define two versions of the regular expression:
513 # * a generic one, which should match for any server but may turn out to
514 # match more than a specific server would accept
515 # * an RFC-compliant matcher
519 # Channel-name-matching regexps
521 CHAN_SAFE = /![A-Z0-9]{5}/
522 CHAN_ANY = /[^\x00\x07\x0A\x0D ,:]/
523 GEN_CHAN = /(?:#{CHAN_FIRST}|#{CHAN_SAFE})#{CHAN_ANY}+/
524 RFC_CHAN = /#{CHAN_FIRST}#{CHAN_ANY}{1,49}|#{CHAN_SAFE}#{CHAN_ANY}{1,44}/
526 # Nick-matching regexps
527 SPECIAL_CHAR = /[\x5b-\x60\x7b-\x7d]/
528 NICK_FIRST = /#{SPECIAL_CHAR}|[[:alpha:]]/
529 NICK_ANY = /#{SPECIAL_CHAR}|[[:alnum:]]|-/
530 GEN_NICK = /#{NICK_FIRST}#{NICK_ANY}+/
531 RFC_NICK = /#{NICK_FIRST}#{NICK_ANY}{0,8}/
533 USER_CHAR = /[^\x00\x0a\x0d @]/
534 GEN_USER = /#{USER_CHAR}+/
536 # Host-matching regexps
537 HOSTNAME_COMPONENT = /[[:alnum:]](?:[[:alnum:]]|-)*[[:alnum:]]*/
538 HOSTNAME = /#{HOSTNAME_COMPONENT}(?:\.#{HOSTNAME_COMPONENT})*/
539 HOSTADDR = /#{IP_ADDR}|#{IP6_ADDR}/
541 GEN_HOST = /#{HOSTNAME}|#{HOSTADDR}/
543 # # FreeNode network replaces the host of affiliated users with
545 # # FIXME we need the true syntax to match it properly ...
546 # PDPC_HOST_PART = /[0-9A-Za-z.-]+/
547 # PDPC_HOST = /#{PDPC_HOST_PART}(?:\/#{PDPC_HOST_PART})+/
549 # # NOTE: the final optional and non-greedy dot is needed because some
550 # # servers (e.g. FreeNode) send the hostname of the services as "services."
551 # # which is not RFC compliant, but sadly done.
552 # GEN_HOST_EXT = /#{PDPC_HOST}|#{GEN_HOST}\.??/
554 # Sadly, different networks have different, RFC-breaking ways of cloaking
555 # the actualy host address: see above for an example to handle FreeNode.
556 # Another example would be Azzurra, wich also inserts a "=" in the
557 # cloacked host. So let's just not care about this and go with the simplest
561 # User-matching Regexp
562 GEN_USER_ID = /(#{GEN_NICK})(?:(?:!(#{GEN_USER}))?@(#{GEN_HOST_EXT}))?/
564 # Things such has the BIP proxy send invalid nicks in a complete netmask,
565 # so we want to match this, rather: this matches either a compliant nick
566 # or a a string with a very generic nick, a very generic hostname after an
567 # @ sign, and an optional user after a !
568 BANG_AT = /#{GEN_NICK}|\S+?(?:!\S+?)?@\S+?/
570 # # For Netmask, we want to allow wildcards * and ? in the nick
571 # # (they are already allowed in the user and host part
572 # GEN_NICK_MASK = /(?:#{NICK_FIRST}|[?*])?(?:#{NICK_ANY}|[?*])+/
574 # # Netmask-matching Regexp
575 # GEN_MASK = /(#{GEN_NICK_MASK})(?:(?:!(#{GEN_USER}))?@(#{GEN_HOST_EXT}))?/
585 # A Netmask identifies each user by collecting its nick, username and
586 # hostname in the form <tt>nick!user@host</tt>
588 # Netmasks can also contain glob patterns in any of their components; in
589 # this form they are used to refer to more than a user or to a user
590 # appearing under different forms.
593 # * <tt>*!*@*</tt> refers to everybody
594 # * <tt>*!someuser@somehost</tt> refers to user +someuser+ on host +somehost+
595 # regardless of the nick used.
599 # Netmasks have an associated casemap unless they are bound to a server
601 include ServerOrCasemap
603 attr_reader :nick, :user, :host
606 # Create a new Netmask from string _str_, which must be in the form
607 # _nick_!_user_@_host_
609 # It is possible to specify a server or a casemap in the optional Hash:
610 # these are used to associate the Netmask with the given server and to set
611 # its casemap: if a server is specified and a casemap is not, the server's
612 # casemap is used. If both a server and a casemap are specified, the
613 # casemap must match the server's casemap or an exception will be raised.
615 # Empty +nick+, +user+ or +host+ are converted to the generic glob pattern
617 def initialize(str="", opts={})
618 # First of all, check for server/casemap option
620 init_server_or_casemap(opts)
622 # Now we can see if the given string _str_ is an actual Netmask
623 if str.respond_to?(:to_str)
625 # We match a pretty generic string, to work around non-compliant
627 when /^(?:(\S+?)(?:(?:!(\S+?))?@(\S+))?)?$/
628 # We do assignment using our internal methods
633 raise ArgumentError, "#{str.to_str.inspect} does not represent a valid #{self.class}"
636 raise TypeError, "#{str} cannot be converted to a #{self.class}"
640 # A Netmask is easily converted to a String for the usual representation.
641 # We skip the user or host parts if they are "*", unless we've been asked
646 ret << "!" << user unless user == "*"
647 ret << "@" << host unless host == "*"
652 "#{nick}!#{user}@#{host}"
655 alias :to_str :fullform
657 # This method downcases the fullform of the netmask. While this may not be
658 # significantly different from the #downcase() method provided by the
659 # ServerOrCasemap mixin, it's significantly different for Netmask
660 # subclasses such as User whose simple downcasing uses the nick only.
662 def full_irc_downcase(cmap=casemap)
663 self.fullform.irc_downcase(cmap)
666 # full_downcase() will return the fullform downcased according to the
670 self.full_irc_downcase
673 # This method returns a new Netmask which is the fully downcased version
676 return self.full_downcase.to_irc_netmask(server_and_casemap)
679 # Converts the receiver into a Netmask with the given (optional)
680 # server/casemap association. We return self unless a conversion
681 # is needed (different casemap/server)
683 # Subclasses of Netmask will return a new Netmask, using full_downcase
685 def to_irc_netmask(opts={})
686 if self.class == Netmask and not opts[:force]
687 return self if fits_with_server_and_casemap?(opts)
689 return self.full_downcase.to_irc_netmask(server_and_casemap.merge(opts))
692 # Converts the receiver into a User with the given (optional)
693 # server/casemap association. We return self unless a conversion
694 # is needed (different casemap/server)
696 def to_irc_user(opts={})
697 self.fullform.to_irc_user(server_and_casemap.merge(opts))
700 # Inspection of a Netmask reveals the server it's bound to (if there is
701 # one), its casemap and the nick, user and host part
704 str = self.__to_s__[0..-2]
705 str << " @server=#{@server}" if defined?(@server) and @server
706 str << " @nick=#{@nick.inspect} @user=#{@user.inspect}"
707 str << " @host=#{@host.inspect} casemap=#{casemap.inspect}"
711 # Equality: two Netmasks are equal if they downcase to the same thing
713 # TODO we may want it to try other.to_irc_netmask
716 return false unless other.kind_of?(self.class)
717 self.downcase == other.downcase
720 # This method changes the nick of the Netmask, defaulting to the generic
721 # glob pattern if the result is the null string.
725 @nick = "*" if @nick.empty?
728 # This method changes the user of the Netmask, defaulting to the generic
729 # glob pattern if the result is the null string.
733 @user = "*" if @user.empty?
737 # This method changes the hostname of the Netmask, defaulting to the generic
738 # glob pattern if the result is the null string.
742 @host = "*" if @host.empty?
745 # We can replace everything at once with data from another Netmask
753 @server = other.server
754 @casemap = other.casemap unless @server
756 replace(other.to_irc_netmask(server_and_casemap))
760 # This method checks if a Netmask is definite or not, by seeing if
761 # any of its components are defined by globs
764 return @nick.has_irc_glob? || @user.has_irc_glob? || @host.has_irc_glob?
769 unless u.has_irc_glob?
770 u.sub!(/^[in]=/, '=') or u.sub!(/^\W(\w+)/, '\1')
775 unless h.has_irc_glob?
777 h.sub!(/x-\w+$/, 'x-*')
779 h.match(/^[^\.]+\.[^\.]+$/) or
780 h.sub!(/azzurra[=-][0-9a-f]+/i, '*') or # hello, azzurra, you suck!
781 h.sub!(/^(\d+\.\d+\.\d+\.)\d+$/, '\1*') or
782 h.sub!(/^[^\.]+\./, '*.')
785 return Netmask.new("*!#{u}@#{h}", server_and_casemap)
788 # This method is used to match the current Netmask against another one
790 # The method returns true if each component of the receiver matches the
791 # corresponding component of the argument. By _matching_ here we mean
792 # that any netmask described by the receiver is also described by the
795 # In this sense, matching is rather simple to define in the case when the
796 # receiver has no globs: it is just necessary to check if the argument
797 # describes the receiver, which can be done by matching it against the
798 # argument converted into an IRC Regexp (see String#to_irc_regexp).
800 # The situation is also easy when the receiver has globs and the argument
801 # doesn't, since in this case the result is false.
803 # The more complex case in which both the receiver and the argument have
804 # globs is not handled yet.
807 cmp = arg.to_irc_netmask(:casemap => casemap)
808 debug "Matching #{self.fullform} against #{arg.inspect} (#{cmp.fullform})"
809 [:nick, :user, :host].each { |component|
810 us = self.send(component).irc_downcase(casemap)
811 them = cmp.send(component).irc_downcase(casemap)
812 if us.has_irc_glob? && them.has_irc_glob?
814 warn NotImplementedError
817 return false if us.has_irc_glob? && !them.has_irc_glob?
818 return false unless us =~ them.to_irc_regexp
823 # Case equality. Checks if arg matches self
826 arg.to_irc_netmask(:casemap => casemap).matches?(self)
829 # Sorting is done via the fullform
834 self.fullform.irc_downcase(casemap) <=> arg.fullform.irc_downcase(casemap)
836 self.downcase <=> arg.downcase
843 # A NetmaskList is an ArrayOf <code>Netmask</code>s
845 class NetmaskList < ArrayOf
847 # Create a new NetmaskList, optionally filling it with the elements from
848 # the Array argument fed to it.
850 def initialize(ar=[])
854 # We enhance the [] method by allowing it to pick an element that matches
855 # a given Netmask, a String or a Regexp
856 # TODO take into consideration the opportunity to use select() instead of
857 # find(), and/or a way to let the user choose which one to take (second
865 mask.matches?(args[0])
869 mask.matches?(args[0].to_irc_netmask(:casemap => mask.casemap))
873 mask.fullform =~ args[0]
890 # We keep extending String, this time adding a method that converts a
891 # String into an Irc::Netmask object
893 def to_irc_netmask(opts={})
894 Irc::Netmask.new(self, opts)
903 # An IRC User is identified by his/her Netmask (which must not have globs).
904 # In fact, User is just a subclass of Netmask.
906 # Ideally, the user and host information of an IRC User should never
907 # change, and it shouldn't contain glob patterns. However, IRC is somewhat
908 # idiosincratic and it may be possible to know the nick of a User much before
909 # its user and host are known. Moreover, some networks (namely Freenode) may
910 # change the hostname of a User when (s)he identifies with Nickserv.
912 # As a consequence, we must allow changes to a User host and user attributes.
913 # We impose a restriction, though: they may not contain glob patterns, except
914 # for the special case of an unknown user/host which is represented by a *.
916 # It is possible to create a totally unknown User (e.g. for initializations)
917 # by setting the nick to * too.
920 # * see if it's worth to add the other USER data
921 # * see if it's worth to add NICKSERV status
926 attr_accessor :real_name
928 # Create a new IRC User from a given Netmask (or anything that can be converted
929 # into a Netmask) provided that the given Netmask does not have globs.
931 def initialize(str="", opts={})
933 raise ArgumentError, "#{str.inspect} must not have globs (unescaped * or ?)" if nick.has_irc_glob? && nick != "*"
934 raise ArgumentError, "#{str.inspect} must not have globs (unescaped * or ?)" if user.has_irc_glob? && user != "*"
935 raise ArgumentError, "#{str.inspect} must not have globs (unescaped * or ?)" if host.has_irc_glob? && host != "*"
937 @real_name = String.new
940 # The nick of a User may be changed freely, but it must not contain glob patterns.
943 raise "Can't change the nick to #{newnick}" if defined?(@nick) and newnick.has_irc_glob?
947 # We have to allow changing the user of an Irc User due to some networks
948 # (e.g. Freenode) changing hostmasks on the fly. We still check if the new
949 # user data has glob patterns though.
952 raise "Can't change the username to #{newuser}" if defined?(@user) and newuser.has_irc_glob?
956 # We have to allow changing the host of an Irc User due to some networks
957 # (e.g. Freenode) changing hostmasks on the fly. We still check if the new
958 # host data has glob patterns though.
961 raise "Can't change the hostname to #{newhost}" if defined?(@host) and newhost.has_irc_glob?
965 # Checks if a User is well-known or not by looking at the hostname and user
968 return nick != "*" && user != "*" && host != "*"
977 # Set the away status of the user. Use away=(nil) or away=(false)
988 # Since to_irc_user runs the same checks on server and channel as
989 # to_irc_netmask, we just try that and return self if it works.
991 # Subclasses of User will return self if possible.
993 def to_irc_user(opts={})
994 return self if fits_with_server_and_casemap?(opts)
995 return self.full_downcase.to_irc_user(opts)
998 # We can replace everything at once with data from another User
1003 self.nick = other.nick
1004 self.user = other.user
1005 self.host = other.host
1006 @server = other.server
1007 @casemap = other.casemap unless @server
1010 self.replace(other.to_irc_user(server_and_casemap))
1014 def modes_on(channel)
1017 channel.modes_of(self)
1019 return @server.channel(channel).modes_of(self) if @server
1020 raise "Can't resolve channel #{channel}"
1027 channel.has_op?(self)
1029 return @server.channel(channel).has_op?(self) if @server
1030 raise "Can't resolve channel #{channel}"
1034 def is_voice?(channel)
1037 channel.has_voice?(self)
1039 return @server.channel(channel).has_voice?(self) if @server
1040 raise "Can't resolve channel #{channel}"
1046 # A UserList is an ArrayOf <code>User</code>s
1047 # We derive it from NetmaskList, which allows us to inherit any special
1048 # NetmaskList method
1050 class UserList < NetmaskList
1052 # Create a new UserList, optionally filling it with the elements from
1053 # the Array argument fed to it.
1055 def initialize(ar=[])
1057 @element_class = User
1060 # Convenience method: convert the UserList to a list of nicks. The indices
1064 self.map { |user| user.nick }
1073 # We keep extending String, this time adding a method that converts a
1074 # String into an Irc::User object
1076 def to_irc_user(opts={})
1077 Irc::User.new(self, opts)
1084 # An IRC Channel is identified by its name, and it has a set of properties:
1085 # * a Channel::Topic
1087 # * a set of Channel::Modes
1089 # The Channel::Topic and Channel::Mode classes are defined within the
1090 # Channel namespace because they only make sense there
1098 attr_reader :channel
1105 # Hash of modes. Subclass of Hash that defines any? and all?
1106 # to check if boolean modes (Type D) are set
1107 class ModeHash < Hash
1109 !!ar.find { |m| s = m.to_sym ; self[s] && self[s].set? }
1112 !ar.find { |m| s = m.to_sym ; !(self[s] && self[s].set?) }
1116 # Channel modes of type A manipulate lists
1118 # Example: b (banlist)
1120 class ModeTypeA < Mode
1124 @list = NetmaskList.new
1128 nm = @channel.server.new_netmask(val)
1129 @list << nm unless @list.include?(nm)
1133 nm = @channel.server.new_netmask(val)
1140 # Channel modes of type B need an argument
1144 class ModeTypeB < Mode
1153 alias :value :status
1160 @arg = nil if @arg == val
1166 # Channel modes that change the User prefixes are like
1167 # Channel modes of type B, except that they manipulate
1168 # lists of Users, so they are somewhat similar to channel
1171 class UserMode < ModeTypeB
1176 @list = UserList.new
1180 u = @channel.server.user(val)
1181 @list << u unless @list.include?(u)
1185 u = @channel.server.user(val)
1192 # Channel modes of type C need an argument when set,
1193 # but not when they get reset
1195 # Example: l (limit)
1197 class ModeTypeC < Mode
1206 alias :value :status
1219 # Channel modes of type D are basically booleans
1221 # Example: m (moderate)
1223 class ModeTypeD < Mode
1244 # A Topic represents the topic of a channel. It consists of
1245 # the topic itself, who set it and when
1248 attr_accessor :text, :set_by, :set_on
1251 # Create a new Topic setting the text, the creator and
1254 def initialize(text="", set_by="", set_on=Time.new)
1256 @set_by = set_by.to_irc_netmask
1260 # Replace a Topic with another one
1263 raise TypeError, "#{topic.inspect} is not of class #{self.class}" unless topic.kind_of?(self.class)
1264 @text = topic.text.dup
1265 @set_by = topic.set_by.dup
1266 @set_on = topic.set_on.dup
1271 def to_irc_channel_topic
1284 # Returns an Irc::Channel::Topic with self as text
1286 def to_irc_channel_topic
1287 Irc::Channel::Topic.new(self)
1296 # Here we start with the actual Channel class
1300 include ServerOrCasemap
1301 attr_reader :name, :topic, :mode, :users
1305 str = self.__to_s__[0..-2]
1306 str << " on server #{server}" if server
1307 str << " @name=#{@name.inspect} @topic=#{@topic.text.inspect}"
1308 str << " @users=[#{user_nicks.sort.join(', ')}]"
1320 @users.map { |u| u.downcase }
1323 # Checks if the receiver already has a user with the given _nick_
1326 @users.index(nick.to_irc_user(server_and_casemap))
1329 # Returns the user with nick _nick_, if available
1332 idx = has_user?(nick)
1336 # Adds a user to the channel
1338 def add_user(user, opts={})
1339 silent = opts.fetch(:silent, false)
1341 warn "Trying to add user #{user} to channel #{self} again" unless silent
1343 @users << user.to_irc_user(server_and_casemap)
1347 # Creates a new channel with the given name, optionally setting the topic
1348 # and an initial users list.
1350 # No additional info is created here, because the channel flags and userlists
1351 # allowed depend on the server.
1353 def initialize(name, topic=nil, users=[], opts={})
1354 raise ArgumentError, "Channel name cannot be empty" if name.to_s.empty?
1355 warn "Unknown channel prefix #{name[0].chr}" if name !~ /^[&#+!]/
1356 raise ArgumentError, "Invalid character in #{name.inspect}" if name =~ /[ \x07,]/
1358 init_server_or_casemap(opts)
1362 @topic = topic ? topic.to_irc_channel_topic : Channel::Topic.new
1364 @users = UserList.new
1371 @mode = ModeHash.new
1374 # Removes a user from the channel
1376 def delete_user(user)
1377 @mode.each { |sym, mode|
1378 mode.reset(user) if mode.kind_of?(UserMode)
1383 # The channel prefix
1389 # A channel is local to a server if it has the '&' prefix
1395 # A channel is modeless if it has the '+' prefix
1401 # A channel is safe if it has the '!' prefix
1407 # A channel is normal if it has the '#' prefix
1415 def create_mode(sym, kl)
1416 @mode[sym.to_sym] = kl.new(self)
1422 l << s if (m.class <= UserMode and m.list[user])
1428 @mode.has_key?(:o) and @mode[:o].list[user]
1431 def has_voice?(user)
1432 @mode.has_key?(:v) and @mode[:v].list[user]
1437 # A ChannelList is an ArrayOf <code>Channel</code>s
1439 class ChannelList < ArrayOf
1441 # Create a new ChannelList, optionally filling it with the elements from
1442 # the Array argument fed to it.
1444 def initialize(ar=[])
1448 # Convenience method: convert the ChannelList to a list of channel names.
1449 # The indices are preserved
1452 self.map { |chan| chan.name }
1462 # We keep extending String, this time adding a method that converts a
1463 # String into an Irc::Channel object
1465 def to_irc_channel(opts={})
1466 Irc::Channel.new(self, opts)
1475 # An IRC Server represents the Server the client is connected to.
1479 attr_reader :hostname, :version, :usermodes, :chanmodes
1480 alias :to_s :hostname
1481 attr_reader :supports, :capabilities
1483 attr_reader :channels, :users
1487 @channels.map { |ch| ch.downcase }
1492 @users.map { |u| u.downcase }
1496 chans, users = [@channels, @users].map {|d|
1498 a.downcase <=> b.downcase
1504 str = self.__to_s__[0..-2]
1505 str << " @hostname=#{hostname}"
1506 str << " @channels=#{chans}"
1507 str << " @users=#{users}"
1511 # Create a new Server, with all instance variables reset to nil (for
1512 # scalar variables), empty channel and user lists and @supports
1513 # initialized to the default values for all known supported features.
1516 @hostname = @version = @usermodes = @chanmodes = nil
1518 @channels = ChannelList.new
1520 @users = UserList.new
1525 # Resets the server capabilities
1527 def reset_capabilities
1529 :casemapping => 'rfc1459'.to_irc_casemap,
1532 :typea => nil, # Type A: address lists
1533 :typeb => nil, # Type B: needs a parameter
1534 :typec => nil, # Type C: needs a parameter when set
1535 :typed => nil # Type D: must not have a parameter
1538 :chantypes => "#&!+",
1549 :prefixes => [:"@", :+]
1560 # Convert a mode (o, v, h, ...) to the corresponding
1561 # prefix (@, +, %, ...). See also mode_for_prefix
1562 def prefix_for_mode(mode)
1563 return @supports[:prefix][:prefixes][
1564 @supports[:prefix][:modes].index(mode.to_sym)
1568 # Convert a prefix (@, +, %, ...) to the corresponding
1569 # mode (o, v, h, ...). See also prefix_for_mode
1570 def mode_for_prefix(pfx)
1571 return @supports[:prefix][:modes][
1572 @supports[:prefix][:prefixes].index(pfx.to_sym)
1576 # Resets the Channel and User list
1579 @users.reverse_each { |u|
1582 @channels.reverse_each { |u|
1592 @hostname = @version = @usermodes = @chanmodes = nil
1595 # This method is used to parse a 004 RPL_MY_INFO line
1597 def parse_my_info(line)
1598 ar = line.split(' ')
1605 def noval_warn(key, val, &block)
1607 yield if block_given?
1609 warn "No #{key.to_s.upcase} value"
1613 def val_warn(key, val, &block)
1614 if val == true or val == false or val.nil?
1615 yield if block_given?
1617 warn "No #{key.to_s.upcase} value must be specified, got #{val}"
1620 private :noval_warn, :val_warn
1622 # This method is used to parse a 005 RPL_ISUPPORT line
1624 # See the RPL_ISUPPORT draft[http://www.irc.org/tech_docs/draft-brocklesby-irc-isupport-03.txt]
1626 def parse_isupport(line)
1627 debug "Parsing ISUPPORT #{line.inspect}"
1628 ar = line.split(' ')
1631 prekey, val = en.split('=', 2)
1632 if prekey =~ /^-(.*)/
1633 key = $1.downcase.to_sym
1636 key = prekey.downcase.to_sym
1640 noval_warn(key, val) {
1641 @supports[key] = val.to_irc_casemap
1643 when :chanlimit, :idchan, :maxlist, :targmax
1644 noval_warn(key, val) {
1645 groups = val.split(',')
1648 @supports[key][k] = v.to_i || 0
1649 if @supports[key][k] == 0
1650 warn "Deleting #{key} limit of 0 for #{k}"
1651 @supports[key].delete(k)
1656 noval_warn(key, val) {
1657 groups = val.split(',')
1658 @supports[key][:typea] = groups[0].scan(/./).map { |x| x.to_sym}
1659 @supports[key][:typeb] = groups[1].scan(/./).map { |x| x.to_sym}
1660 @supports[key][:typec] = groups[2].scan(/./).map { |x| x.to_sym}
1661 @supports[key][:typed] = groups[3].scan(/./).map { |x| x.to_sym}
1663 when :channellen, :kicklen, :modes, :topiclen
1665 @supports[key] = val.to_i
1667 @supports[key] = nil
1670 @supports[key] = val # can also be nil
1673 @supports[key] = val
1676 @supports[key] = val
1678 noval_warn(key, val) {
1679 reparse += "CHANLIMIT=(chantypes):#{val} "
1682 noval_warn(key, val) {
1683 @supports[:targmax]['PRIVMSG'] = val.to_i
1684 @supports[:targmax]['NOTICE'] = val.to_i
1687 noval_warn(key, val) {
1688 @supports[key] = val
1691 noval_warn(key, val) {
1692 @supports[key] = val.to_i
1696 val.scan(/\((.*)\)(.*)/) { |m, p|
1697 @supports[key][:modes] = m.scan(/./).map { |x| x.to_sym}
1698 @supports[key][:prefixes] = p.scan(/./).map { |x| x.to_sym}
1701 @supports[key][:modes] = nil
1702 @supports[key][:prefixes] = nil
1705 val_warn(key, val) {
1706 @supports[key] = val.nil? ? true : val
1709 noval_warn(key, val) {
1710 @supports[key] = val.scan(/./)
1713 noval_warn(key, val) {
1714 @supports[key] = val.split(',')
1717 @supports[key] = val.nil? ? true : val
1720 reparse.gsub!("(chantypes)",@supports[:chantypes])
1721 parse_isupport(reparse) unless reparse.empty?
1724 # Returns the casemap of the server.
1727 @supports[:casemapping]
1730 # Returns User or Channel depending on what _name_ can be
1733 def user_or_channel?(name)
1734 if supports[:chantypes].include?(name[0])
1741 # Returns the actual User or Channel object matching _name_
1743 def user_or_channel(name)
1744 if supports[:chantypes].include?(name[0])
1745 return channel(name)
1751 # Checks if the receiver already has a channel with the given _name_
1753 def has_channel?(name)
1754 return false if name.nil_or_empty?
1755 channel_names.index(name.irc_downcase(casemap))
1757 alias :has_chan? :has_channel?
1759 # Returns the channel with name _name_, if available
1761 def get_channel(name)
1762 return nil if name.nil_or_empty?
1763 idx = has_channel?(name)
1764 channels[idx] if idx
1766 alias :get_chan :get_channel
1768 # Create a new Channel object bound to the receiver and add it to the
1769 # list of <code>Channel</code>s on the receiver, unless the channel was
1770 # present already. In this case, the default action is to raise an
1771 # exception, unless _fails_ is set to false. An exception can also be
1772 # raised if _str_ is nil or empty, again only if _fails_ is set to true;
1773 # otherwise, the method just returns nil
1775 def new_channel(name, topic=nil, users=[], fails=true)
1776 if name.nil_or_empty?
1777 raise "Tried to look for empty or nil channel name #{name.inspect}" if fails
1782 raise "Channel #{name} already exists on server #{self}" if fails
1786 prefix = name[0].chr
1788 # Give a warning if the new Channel goes over some server limits.
1790 # FIXME might need to raise an exception
1792 warn "#{self} doesn't support channel prefix #{prefix}" unless @supports[:chantypes].include?(prefix)
1793 warn "#{self} doesn't support channel names this long (#{name.length} > #{@supports[:channellen]})" unless name.length <= @supports[:channellen]
1795 # Next, we check if we hit the limit for channels of type +prefix+
1796 # if the server supports +chanlimit+
1798 @supports[:chanlimit].keys.each { |k|
1799 next unless k.include?(prefix)
1801 channel_names.each { |n|
1802 count += 1 if k.include?(n[0])
1804 # raise IndexError, "Already joined #{count} channels with prefix #{k}" if count == @supports[:chanlimit][k]
1805 warn "Already joined #{count}/#{@supports[:chanlimit][k]} channels with prefix #{k}, we may be going over server limits" if count >= @supports[:chanlimit][k]
1808 # So far, everything is fine. Now create the actual Channel
1810 chan = Channel.new(name, topic, users, :server => self)
1812 # We wade through +prefix+ and +chanmodes+ to create appropriate
1813 # lists and flags for this channel
1815 @supports[:prefix][:modes].each { |mode|
1816 chan.create_mode(mode, Channel::UserMode)
1817 } if @supports[:prefix][:modes]
1819 @supports[:chanmodes].each { |k, val|
1824 chan.create_mode(mode, Channel::ModeTypeA)
1828 chan.create_mode(mode, Channel::ModeTypeB)
1832 chan.create_mode(mode, Channel::ModeTypeC)
1836 chan.create_mode(mode, Channel::ModeTypeD)
1843 # debug "Created channel #{chan.inspect}"
1848 # Returns the Channel with the given _name_ on the server,
1849 # creating it if necessary. This is a short form for
1850 # new_channel(_str_, nil, [], +false+)
1853 new_channel(str,nil,[],false)
1856 # Remove Channel _name_ from the list of <code>Channel</code>s
1858 def delete_channel(name)
1859 idx = has_channel?(name)
1860 raise "Tried to remove unmanaged channel #{name}" unless idx
1861 @channels.delete_at(idx)
1864 # Checks if the receiver already has a user with the given _nick_
1867 return false if nick.nil_or_empty?
1868 user_nicks.index(nick.irc_downcase(casemap))
1871 # Returns the user with nick _nick_, if available
1874 idx = has_user?(nick)
1878 # Create a new User object bound to the receiver and add it to the list
1879 # of <code>User</code>s on the receiver, unless the User was present
1880 # already. In this case, the default action is to raise an exception,
1881 # unless _fails_ is set to false. An exception can also be raised
1882 # if _str_ is nil or empty, again only if _fails_ is set to true;
1883 # otherwise, the method just returns nil
1885 def new_user(str, fails=true)
1886 if str.nil_or_empty?
1887 raise "Tried to look for empty or nil user name #{str.inspect}" if fails
1890 tmp = str.to_irc_user(:server => self)
1891 old = get_user(tmp.nick)
1892 # debug "Tmp: #{tmp.inspect}"
1893 # debug "Old: #{old.inspect}"
1895 # debug "User already existed as #{old.inspect}"
1898 # debug "Both were known"
1899 # Do not raise an error: things like Freenode change the hostname after identification
1900 warning "User #{tmp.nick} has inconsistent Netmasks! #{self} knows #{old.inspect} but access was tried with #{tmp.inspect}" if old != tmp
1901 raise "User #{tmp} already exists on server #{self}" if fails
1903 if old.fullform.downcase != tmp.fullform.downcase
1905 # debug "Known user now #{old.inspect}"
1910 warn "#{self} doesn't support nicknames this long (#{tmp.nick.length} > #{@supports[:nicklen]})" unless tmp.nick.length <= @supports[:nicklen]
1916 # Returns the User with the given Netmask on the server,
1917 # creating it if necessary. This is a short form for
1918 # new_user(_str_, +false+)
1921 new_user(str, false)
1924 # Deletes User _user_ from Channel _channel_
1926 def delete_user_from_channel(user, channel)
1927 channel.delete_user(user)
1930 # Remove User _someuser_ from the list of <code>User</code>s.
1931 # _someuser_ must be specified with the full Netmask.
1933 def delete_user(someuser)
1934 idx = has_user?(someuser)
1935 raise "Tried to remove unmanaged user #{user}" unless idx
1936 have = self.user(someuser)
1937 @channels.each { |ch|
1938 delete_user_from_channel(have, ch)
1940 @users.delete_at(idx)
1943 # Create a new Netmask object with the appropriate casemap
1945 def new_netmask(str)
1946 str.to_irc_netmask(:server => self)
1949 # Finds all <code>User</code>s on server whose Netmask matches _mask_
1951 def find_users(mask)
1952 nm = new_netmask(mask)
1953 @users.inject(UserList.new) {
1955 if user.user == "*" or user.host == "*"
1956 list << user if user.nick.irc_downcase(casemap) =~ nm.nick.irc_downcase(casemap).to_irc_regexp
1958 list << user if user.matches?(nm)