]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/irc.rb
58e9f90ad12eefb271548f411f99edb75fc4888d
[user/henk/code/ruby/rbot.git] / lib / rbot / irc.rb
1 #-- vim:sw=2:et
2 # General TODO list
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
11 #++
12 # :title: IRC module
13 #
14 # Basic IRC stuff
15 #
16 # This module defines the fundamental building blocks for IRC
17 #
18 # Author:: Giuseppe Bilotta (giuseppe.bilotta@gmail.com)
19
20 require 'singleton'
21
22 class Object
23
24   # We extend the Object class with a method that
25   # checks if the receiver is nil or empty
26   def nil_or_empty?
27     return true unless self
28     return true if self.respond_to? :empty? and self.empty?
29     return false
30   end
31
32   # We alias the to_s method to __to_s__ to make
33   # it accessible in all classes
34   alias :__to_s__ :to_s 
35 end
36
37 # The Irc module is used to keep all IRC-related classes
38 # in the same namespace
39 #
40 module Irc
41
42
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>.
46   #
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
49   # <tt>~</tt>
50   #
51   class Casemap
52     @@casemaps = {}
53
54     # Create a new casemap with name _name_, uppercase characters _upper_ and
55     # lowercase characters _lower_
56     #
57     def initialize(name, upper, lower)
58       @key = name.to_sym
59       raise "Casemap #{name.inspect} already exists!" if @@casemaps.has_key?(@key)
60       @@casemaps[@key] = {
61         :upper => upper,
62         :lower => lower,
63         :casemap => self
64       }
65     end
66
67     # Returns the Casemap with the given name
68     #
69     def Casemap.get(name)
70       @@casemaps[name.to_sym][:casemap]
71     end
72
73     # Retrieve the 'uppercase characters' of this Casemap
74     #
75     def upper
76       @@casemaps[@key][:upper]
77     end
78
79     # Retrieve the 'lowercase characters' of this Casemap
80     #
81     def lower
82       @@casemaps[@key][:lower]
83     end
84
85     # Return a Casemap based on the receiver
86     #
87     def to_irc_casemap
88       self
89     end
90
91     # A Casemap is represented by its lower/upper mappings
92     #
93     def inspect
94       self.__to_s__[0..-2] + " #{upper.inspect} ~(#{self})~ #{lower.inspect}>"
95     end
96
97     # As a String we return our name
98     #
99     def to_s
100       @key.to_s
101     end
102
103     # Two Casemaps are equal if they have the same upper and lower ranges
104     #
105     def ==(arg)
106       other = arg.to_irc_casemap
107       return self.upper == other.upper && self.lower == other.lower
108     end
109
110     # Give a warning if _arg_ and self are not the same Casemap
111     #
112     def must_be(arg)
113       other = arg.to_irc_casemap
114       if self == other
115         return true
116       else
117         warn "Casemap mismatch (#{self.inspect} != #{other.inspect})"
118         return false
119       end
120     end
121
122   end
123
124   # The rfc1459 casemap
125   #
126   class RfcCasemap < Casemap
127     include Singleton
128
129     def initialize
130       super('rfc1459', "\x41-\x5e", "\x61-\x7e")
131     end
132
133   end
134   RfcCasemap.instance
135
136   # The strict-rfc1459 Casemap
137   #
138   class StrictRfcCasemap < Casemap
139     include Singleton
140
141     def initialize
142       super('strict-rfc1459', "\x41-\x5d", "\x61-\x7d")
143     end
144
145   end
146   StrictRfcCasemap.instance
147
148   # The ascii Casemap
149   #
150   class AsciiCasemap < Casemap
151     include Singleton
152
153     def initialize
154       super('ascii', "\x41-\x5a", "\x61-\x7a")
155     end
156
157   end
158   AsciiCasemap.instance
159
160
161   # This module is included by all classes that are either bound to a server
162   # or should have a casemap.
163   #
164   module ServerOrCasemap
165
166     attr_reader :server
167
168     # This method initializes the instance variables @server and @casemap
169     # according to the values of the hash keys :server and :casemap in _opts_
170     #
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)
174
175       @casemap = opts.fetch(:casemap, nil)
176       if @server
177         if @casemap
178           @server.casemap.must_be(@casemap)
179           @casemap = nil
180         end
181       else
182         @casemap = (@casemap || 'rfc1459').to_irc_casemap
183       end
184     end
185
186     # This is an auxiliary method: it returns true if the receiver fits the
187     # server and casemap specified in _opts_, false otherwise.
188     #
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?
193
194       if srv.nil?
195         return true if cmap.nil? or cmap == casemap
196       else
197         return true if srv == @server and (cmap.nil? or cmap == casemap)
198       end
199       return false
200     end
201
202     # Returns the casemap of the receiver, by looking at the bound
203     # @server (if possible) or at the @casemap otherwise
204     #
205     def casemap
206       return @server.casemap if defined?(@server) and @server
207       return @casemap
208     end
209
210     # Returns a hash with the current @server and @casemap as values of
211     # :server and :casemap
212     #
213     def server_and_casemap
214       h = {}
215       h[:server] = @server if defined?(@server) and @server
216       h[:casemap] = @casemap if defined?(@casemap) and @casemap
217       return h
218     end
219
220     # We allow up/downcasing with a different casemap
221     #
222     def irc_downcase(cmap=casemap)
223       self.to_s.irc_downcase(cmap)
224     end
225
226     # Up/downcasing something that includes this module returns its
227     # Up/downcased to_s form
228     #
229     def downcase
230       self.irc_downcase
231     end
232
233     # We allow up/downcasing with a different casemap
234     #
235     def irc_upcase(cmap=casemap)
236       self.to_s.irc_upcase(cmap)
237     end
238
239     # Up/downcasing something that includes this module returns its
240     # Up/downcased to_s form
241     #
242     def upcase
243       self.irc_upcase
244     end
245
246   end
247
248 end
249
250
251 # We start by extending the String class
252 # with some IRC-specific methods
253 #
254 class String
255
256   # This method returns the Irc::Casemap whose name is the receiver
257   #
258   def to_irc_casemap
259     Irc::Casemap.get(self) rescue raise TypeError, "Unkown Irc::Casemap #{self.inspect}"
260   end
261
262   # This method returns a string which is the downcased version of the
263   # receiver, according to the given _casemap_
264   #
265   #
266   def irc_downcase(casemap='rfc1459')
267     cmap = casemap.to_irc_casemap
268     self.tr(cmap.upper, cmap.lower)
269   end
270
271   # This is the same as the above, except that the string is altered in place
272   #
273   # See also the discussion about irc_downcase
274   #
275   def irc_downcase!(casemap='rfc1459')
276     cmap = casemap.to_irc_casemap
277     self.tr!(cmap.upper, cmap.lower)
278   end
279
280   # Upcasing functions are provided too
281   #
282   # See also the discussion about irc_downcase
283   #
284   def irc_upcase(casemap='rfc1459')
285     cmap = casemap.to_irc_casemap
286     self.tr(cmap.lower, cmap.upper)
287   end
288
289   # In-place upcasing
290   #
291   # See also the discussion about irc_downcase
292   #
293   def irc_upcase!(casemap='rfc1459')
294     cmap = casemap.to_irc_casemap
295     self.tr!(cmap.lower, cmap.upper)
296   end
297
298   # This method checks if the receiver contains IRC glob characters
299   #
300   # IRC has a very primitive concept of globs: a <tt>*</tt> stands for "any
301   # number of arbitrary characters", a <tt>?</tt> stands for "one and exactly
302   # one arbitrary character". These characters can be escaped by prefixing them
303   # with a slash (<tt>\\</tt>).
304   #
305   # A known limitation of this glob syntax is that there is no way to escape
306   # the escape character itself, so it's not possible to build a glob pattern
307   # where the escape character precedes a glob.
308   #
309   def has_irc_glob?
310     self =~ /^[*?]|[^\\][*?]/
311   end
312
313   # This method is used to convert the receiver into a Regular Expression
314   # that matches according to the IRC glob syntax
315   #
316   def to_irc_regexp
317     regmask = Regexp.escape(self)
318     regmask.gsub!(/(\\\\)?\\[*?]/) { |m|
319       case m
320       when /\\(\\[*?])/
321         $1
322       when /\\\*/
323         '.*'
324       when /\\\?/
325         '.'
326       else
327         raise "Unexpected match #{m} when converting #{self}"
328       end
329     }
330     Regexp.new("^#{regmask}$")
331   end
332
333 end
334
335
336 # ArrayOf is a subclass of Array whose elements are supposed to be all
337 # of the same class. This is not intended to be used directly, but rather
338 # to be subclassed as needed (see for example Irc::UserList and Irc::NetmaskList)
339 #
340 # Presently, only very few selected methods from Array are overloaded to check
341 # if the new elements are the correct class. An orthodox? method is provided
342 # to check the entire ArrayOf against the appropriate class.
343 #
344 class ArrayOf < Array
345
346   attr_reader :element_class
347
348   # Create a new ArrayOf whose elements are supposed to be all of type _kl_,
349   # optionally filling it with the elements from the Array argument.
350   #
351   def initialize(kl, ar=[])
352     raise TypeError, "#{kl.inspect} must be a class name" unless kl.kind_of?(Class)
353     super()
354     @element_class = kl
355     case ar
356     when Array
357       insert(0, *ar)
358     else
359       raise TypeError, "#{self.class} can only be initialized from an Array"
360     end
361   end
362
363   def inspect
364     self.__to_s__[0..-2].sub(/:[^:]+$/,"[#{@element_class}]\\0") + " #{super}>"
365   end
366
367   # Private method to check the validity of the elements passed to it
368   # and optionally raise an error
369   #
370   # TODO should it accept nils as valid?
371   #
372   def internal_will_accept?(raising, *els)
373     els.each { |el|
374       unless el.kind_of?(@element_class)
375         raise TypeError, "#{el.inspect} is not of class #{@element_class}" if raising
376         return false
377       end
378     }
379     return true
380   end
381   private :internal_will_accept?
382
383   # This method checks if the passed arguments are acceptable for our ArrayOf
384   #
385   def will_accept?(*els)
386     internal_will_accept?(false, *els)
387   end
388
389   # This method checks that all elements are of the appropriate class
390   #
391   def valid?
392     will_accept?(*self)
393   end
394
395   # This method is similar to the above, except that it raises an exception
396   # if the receiver is not valid
397   #
398   def validate
399     raise TypeError unless valid?
400   end
401
402   # Overloaded from Array#<<, checks for appropriate class of argument
403   #
404   def <<(el)
405     super(el) if internal_will_accept?(true, el)
406   end
407
408   # Overloaded from Array#&, checks for appropriate class of argument elements
409   #
410   def &(ar)
411     r = super(ar)
412     ArrayOf.new(@element_class, r) if internal_will_accept?(true, *r)
413   end
414
415   # Overloaded from Array#+, checks for appropriate class of argument elements
416   #
417   def +(ar)
418     ArrayOf.new(@element_class, super(ar)) if internal_will_accept?(true, *ar)
419   end
420
421   # Overloaded from Array#-, so that an ArrayOf is returned. There is no need
422   # to check the validity of the elements in the argument
423   #
424   def -(ar)
425     ArrayOf.new(@element_class, super(ar)) # if internal_will_accept?(true, *ar)
426   end
427
428   # Overloaded from Array#|, checks for appropriate class of argument elements
429   #
430   def |(ar)
431     ArrayOf.new(@element_class, super(ar)) if internal_will_accept?(true, *ar)
432   end
433
434   # Overloaded from Array#concat, checks for appropriate class of argument
435   # elements
436   #
437   def concat(ar)
438     super(ar) if internal_will_accept?(true, *ar)
439   end
440
441   # Overloaded from Array#insert, checks for appropriate class of argument
442   # elements
443   #
444   def insert(idx, *ar)
445     super(idx, *ar) if internal_will_accept?(true, *ar)
446   end
447
448   # Overloaded from Array#replace, checks for appropriate class of argument
449   # elements
450   #
451   def replace(ar)
452     super(ar) if (ar.kind_of?(ArrayOf) && ar.element_class <= @element_class) or internal_will_accept?(true, *ar)
453   end
454
455   # Overloaded from Array#push, checks for appropriate class of argument
456   # elements
457   #
458   def push(*ar)
459     super(*ar) if internal_will_accept?(true, *ar)
460   end
461
462   # Overloaded from Array#unshift, checks for appropriate class of argument(s)
463   #
464   def unshift(*els)
465     els.each { |el|
466       super(el) if internal_will_accept?(true, *els)
467     }
468   end
469
470   # We introduce the 'downcase' method, which maps downcase() to all the Array
471   # elements, properly failing when the elements don't have a downcase method
472   #
473   def downcase
474     self.map { |el| el.downcase }
475   end
476
477   # Modifying methods which we don't handle yet are made private
478   #
479   private :[]=, :collect!, :map!, :fill, :flatten!
480
481 end
482
483
484 # We extend the Regexp class with an Irc module which will contain some
485 # Irc-specific regexps
486 #
487 class Regexp
488
489   # We start with some general-purpose ones which will be used in the
490   # Irc module too, but are useful regardless
491   DIGITS = /\d+/
492   HEX_DIGIT = /[0-9A-Fa-f]/
493   HEX_DIGITS = /#{HEX_DIGIT}+/
494   HEX_OCTET = /#{HEX_DIGIT}#{HEX_DIGIT}?/
495   DEC_OCTET = /[01]?\d?\d|2[0-4]\d|25[0-5]/
496   DEC_IP_ADDR = /#{DEC_OCTET}.#{DEC_OCTET}.#{DEC_OCTET}.#{DEC_OCTET}/
497   HEX_IP_ADDR = /#{HEX_OCTET}.#{HEX_OCTET}.#{HEX_OCTET}.#{HEX_OCTET}/
498   IP_ADDR = /#{DEC_IP_ADDR}|#{HEX_IP_ADDR}/
499
500   # IPv6, from Resolv::IPv6, without the \A..\z anchors
501   HEX_16BIT = /#{HEX_DIGIT}{1,4}/
502   IP6_8Hex = /(?:#{HEX_16BIT}:){7}#{HEX_16BIT}/
503   IP6_CompressedHex = /((?:#{HEX_16BIT}(?::#{HEX_16BIT})*)?)::((?:#{HEX_16BIT}(?::#{HEX_16BIT})*)?)/
504   IP6_6Hex4Dec = /((?:#{HEX_16BIT}:){6,6})#{DEC_IP_ADDR}/
505   IP6_CompressedHex4Dec = /((?:#{HEX_16BIT}(?::#{HEX_16BIT})*)?)::((?:#{HEX_16BIT}:)*)#{DEC_IP_ADDR}/
506   IP6_ADDR = /(?:#{IP6_8Hex})|(?:#{IP6_CompressedHex})|(?:#{IP6_6Hex4Dec})|(?:#{IP6_CompressedHex4Dec})/
507
508   # We start with some IRC related regular expressions, used to match
509   # Irc::User nicks and users and Irc::Channel names
510   #
511   # For each of them we define two versions of the regular expression:
512   # * a generic one, which should match for any server but may turn out to
513   #   match more than a specific server would accept
514   # * an RFC-compliant matcher
515   #
516   module Irc
517
518     # Channel-name-matching regexps
519     CHAN_FIRST = /[#&+]/
520     CHAN_SAFE = /![A-Z0-9]{5}/
521     CHAN_ANY = /[^\x00\x07\x0A\x0D ,:]/
522     GEN_CHAN = /(?:#{CHAN_FIRST}|#{CHAN_SAFE})#{CHAN_ANY}+/
523     RFC_CHAN = /#{CHAN_FIRST}#{CHAN_ANY}{1,49}|#{CHAN_SAFE}#{CHAN_ANY}{1,44}/
524
525     # Nick-matching regexps
526     SPECIAL_CHAR = /[\x5b-\x60\x7b-\x7d]/
527     NICK_FIRST = /#{SPECIAL_CHAR}|[[:alpha:]]/
528     NICK_ANY = /#{SPECIAL_CHAR}|[[:alnum:]]|-/
529     GEN_NICK = /#{NICK_FIRST}#{NICK_ANY}+/
530     RFC_NICK = /#{NICK_FIRST}#{NICK_ANY}{0,8}/
531
532     USER_CHAR = /[^\x00\x0a\x0d @]/
533     GEN_USER = /#{USER_CHAR}+/
534
535     # Host-matching regexps
536     HOSTNAME_COMPONENT = /[[:alnum:]](?:[[:alnum:]]|-)*[[:alnum:]]*/
537     HOSTNAME = /#{HOSTNAME_COMPONENT}(?:\.#{HOSTNAME_COMPONENT})*/
538     HOSTADDR = /#{IP_ADDR}|#{IP6_ADDR}/
539
540     GEN_HOST = /#{HOSTNAME}|#{HOSTADDR}/
541
542     # # FreeNode network replaces the host of affiliated users with
543     # # 'virtual hosts' 
544     # # FIXME we need the true syntax to match it properly ...
545     # PDPC_HOST_PART = /[0-9A-Za-z.-]+/
546     # PDPC_HOST = /#{PDPC_HOST_PART}(?:\/#{PDPC_HOST_PART})+/
547
548     # # NOTE: the final optional and non-greedy dot is needed because some
549     # # servers (e.g. FreeNode) send the hostname of the services as "services."
550     # # which is not RFC compliant, but sadly done.
551     # GEN_HOST_EXT = /#{PDPC_HOST}|#{GEN_HOST}\.??/ 
552
553     # Sadly, different networks have different, RFC-breaking ways of cloaking
554     # the actualy host address: see above for an example to handle FreeNode.
555     # Another example would be Azzurra, wich also inserts a "=" in the
556     # cloacked host. So let's just not care about this and go with the simplest
557     # thing:
558     GEN_HOST_EXT = /\S+/
559
560     # User-matching Regexp
561     GEN_USER_ID = /(#{GEN_NICK})(?:(?:!(#{GEN_USER}))?@(#{GEN_HOST_EXT}))?/
562
563     # Things such has the BIP proxy send invalid nicks in a complete netmask,
564     # so we want to match this, rather: this matches either a compliant nick
565     # or a a string with a very generic nick, a very generic hostname after an
566     # @ sign, and an optional user after a !
567     BANG_AT = /#{GEN_NICK}|\S+?(?:!\S+?)?@\S+?/
568
569     # # For Netmask, we want to allow wildcards * and ? in the nick
570     # # (they are already allowed in the user and host part
571     # GEN_NICK_MASK = /(?:#{NICK_FIRST}|[?*])?(?:#{NICK_ANY}|[?*])+/
572
573     # # Netmask-matching Regexp
574     # GEN_MASK = /(#{GEN_NICK_MASK})(?:(?:!(#{GEN_USER}))?@(#{GEN_HOST_EXT}))?/
575
576   end
577
578 end
579
580
581 module Irc
582
583
584   # A Netmask identifies each user by collecting its nick, username and
585   # hostname in the form <tt>nick!user@host</tt>
586   #
587   # Netmasks can also contain glob patterns in any of their components; in
588   # this form they are used to refer to more than a user or to a user
589   # appearing under different forms.
590   #
591   # Example:
592   # * <tt>*!*@*</tt> refers to everybody
593   # * <tt>*!someuser@somehost</tt> refers to user +someuser+ on host +somehost+
594   #   regardless of the nick used.
595   #
596   class Netmask
597
598     # Netmasks have an associated casemap unless they are bound to a server
599     #
600     include ServerOrCasemap
601
602     attr_reader :nick, :user, :host
603     alias :ident :user
604
605     # Create a new Netmask from string _str_, which must be in the form
606     # _nick_!_user_@_host_
607     #
608     # It is possible to specify a server or a casemap in the optional Hash:
609     # these are used to associate the Netmask with the given server and to set
610     # its casemap: if a server is specified and a casemap is not, the server's
611     # casemap is used. If both a server and a casemap are specified, the
612     # casemap must match the server's casemap or an exception will be raised.
613     #
614     # Empty +nick+, +user+ or +host+ are converted to the generic glob pattern
615     #
616     def initialize(str="", opts={})
617       # First of all, check for server/casemap option
618       #
619       init_server_or_casemap(opts)
620
621       # Now we can see if the given string _str_ is an actual Netmask
622       if str.respond_to?(:to_str)
623         case str.to_str
624           # We match a pretty generic string, to work around non-compliant
625           # servers
626         when /^(?:(\S+?)(?:(?:!(\S+?))?@(\S+))?)?$/
627           # We do assignment using our internal methods
628           self.nick = $1
629           self.user = $2
630           self.host = $3
631         else
632           raise ArgumentError, "#{str.to_str.inspect} does not represent a valid #{self.class}"
633         end
634       else
635         raise TypeError, "#{str} cannot be converted to a #{self.class}"
636       end
637     end
638
639     # A Netmask is easily converted to a String for the usual representation.
640     # We skip the user or host parts if they are "*", unless we've been asked
641     # for the full form
642     #
643     def to_s
644       ret = nick.dup
645       ret << "!" << user unless user == "*"
646       ret << "@" << host unless host == "*"
647       return ret
648     end
649
650     def fullform
651       "#{nick}!#{user}@#{host}"
652     end
653
654     alias :to_str :fullform
655
656     # This method downcases the fullform of the netmask. While this may not be
657     # significantly different from the #downcase() method provided by the
658     # ServerOrCasemap mixin, it's significantly different for Netmask
659     # subclasses such as User whose simple downcasing uses the nick only.
660     #
661     def full_irc_downcase(cmap=casemap)
662       self.fullform.irc_downcase(cmap)
663     end
664
665     # full_downcase() will return the fullform downcased according to the
666     # User's own casemap
667     #
668     def full_downcase
669       self.full_irc_downcase
670     end
671
672     # This method returns a new Netmask which is the fully downcased version
673     # of the receiver
674     def downcased
675       return self.full_downcase.to_irc_netmask(server_and_casemap)
676     end
677
678     # Converts the receiver into a Netmask with the given (optional)
679     # server/casemap association. We return self unless a conversion
680     # is needed (different casemap/server)
681     #
682     # Subclasses of Netmask will return a new Netmask, using full_downcase
683     #
684     def to_irc_netmask(opts={})
685       if self.class == Netmask
686         return self if fits_with_server_and_casemap?(opts)
687       end
688       return self.full_downcase.to_irc_netmask(server_and_casemap.merge(opts))
689     end
690
691     # Converts the receiver into a User with the given (optional)
692     # server/casemap association. We return self unless a conversion
693     # is needed (different casemap/server)
694     #
695     def to_irc_user(opts={})
696       self.fullform.to_irc_user(server_and_casemap.merge(opts))
697     end
698
699     # Inspection of a Netmask reveals the server it's bound to (if there is
700     # one), its casemap and the nick, user and host part
701     #
702     def inspect
703       str = self.__to_s__[0..-2]
704       str << " @server=#{@server}" if defined?(@server) and @server
705       str << " @nick=#{@nick.inspect} @user=#{@user.inspect}"
706       str << " @host=#{@host.inspect} casemap=#{casemap.inspect}"
707       str << ">"
708     end
709
710     # Equality: two Netmasks are equal if they downcase to the same thing
711     #
712     # TODO we may want it to try other.to_irc_netmask
713     #
714     def ==(other)
715       return false unless other.kind_of?(self.class)
716       self.downcase == other.downcase
717     end
718
719     # This method changes the nick of the Netmask, defaulting to the generic
720     # glob pattern if the result is the null string.
721     #
722     def nick=(newnick)
723       @nick = newnick.to_s
724       @nick = "*" if @nick.empty?
725     end
726
727     # This method changes the user of the Netmask, defaulting to the generic
728     # glob pattern if the result is the null string.
729     #
730     def user=(newuser)
731       @user = newuser.to_s
732       @user = "*" if @user.empty?
733     end
734     alias :ident= :user=
735
736     # This method changes the hostname of the Netmask, defaulting to the generic
737     # glob pattern if the result is the null string.
738     #
739     def host=(newhost)
740       @host = newhost.to_s
741       @host = "*" if @host.empty?
742     end
743
744     # We can replace everything at once with data from another Netmask
745     #
746     def replace(other)
747       case other
748       when Netmask
749         nick = other.nick
750         user = other.user
751         host = other.host
752         @server = other.server
753         @casemap = other.casemap unless @server
754       else
755         replace(other.to_irc_netmask(server_and_casemap))
756       end
757     end
758
759     # This method checks if a Netmask is definite or not, by seeing if
760     # any of its components are defined by globs
761     #
762     def has_irc_glob?
763       return @nick.has_irc_glob? || @user.has_irc_glob? || @host.has_irc_glob?
764     end
765
766     def generalize
767       u = user.dup
768       unless u.has_irc_glob?
769         u.sub!(/^[in]=/, '=') or u.sub!(/^\W(\w+)/, '\1')
770         u = '*' + u
771       end
772
773       h = host.dup
774       unless h.has_irc_glob?
775         if h.include? '/'
776           h.sub!(/x-\w+$/, 'x-*')
777         else
778           h.match(/^[^\.]+\.[^\.]+$/) or
779           h.sub!(/azzurra[=-][0-9a-f]+/i, '*') or # hello, azzurra, you suck!
780           h.sub!(/^(\d+\.\d+\.\d+\.)\d+$/, '\1*') or
781           h.sub!(/^[^\.]+\./, '*.')
782         end
783       end
784       return Netmask.new("*!#{u}@#{h}", server_and_casemap)
785     end
786
787     # This method is used to match the current Netmask against another one
788     #
789     # The method returns true if each component of the receiver matches the
790     # corresponding component of the argument. By _matching_ here we mean
791     # that any netmask described by the receiver is also described by the
792     # argument.
793     #
794     # In this sense, matching is rather simple to define in the case when the
795     # receiver has no globs: it is just necessary to check if the argument
796     # describes the receiver, which can be done by matching it against the
797     # argument converted into an IRC Regexp (see String#to_irc_regexp).
798     #
799     # The situation is also easy when the receiver has globs and the argument
800     # doesn't, since in this case the result is false.
801     #
802     # The more complex case in which both the receiver and the argument have
803     # globs is not handled yet.
804     #
805     def matches?(arg)
806       cmp = arg.to_irc_netmask(:casemap => casemap)
807       debug "Matching #{self.fullform} against #{arg.inspect} (#{cmp.fullform})"
808       [:nick, :user, :host].each { |component|
809         us = self.send(component).irc_downcase(casemap)
810         them = cmp.send(component).irc_downcase(casemap)
811         if us.has_irc_glob? && them.has_irc_glob?
812           next if us == them
813           warn NotImplementedError
814           return false
815         end
816         return false if us.has_irc_glob? && !them.has_irc_glob?
817         return false unless us =~ them.to_irc_regexp
818       }
819       return true
820     end
821
822     # Case equality. Checks if arg matches self
823     #
824     def ===(arg)
825       arg.to_irc_netmask(:casemap => casemap).matches?(self)
826     end
827
828     # Sorting is done via the fullform
829     #
830     def <=>(arg)
831       case arg
832       when Netmask
833         self.fullform.irc_downcase(casemap) <=> arg.fullform.irc_downcase(casemap)
834       else
835         self.downcase <=> arg.downcase
836       end
837     end
838
839   end
840
841
842   # A NetmaskList is an ArrayOf <code>Netmask</code>s
843   #
844   class NetmaskList < ArrayOf
845
846     # Create a new NetmaskList, optionally filling it with the elements from
847     # the Array argument fed to it.
848     #
849     def initialize(ar=[])
850       super(Netmask, ar)
851     end
852
853     # We enhance the [] method by allowing it to pick an element that matches
854     # a given Netmask, a String or a Regexp
855     # TODO take into consideration the opportunity to use select() instead of
856     # find(), and/or a way to let the user choose which one to take (second
857     # argument?)
858     #
859     def [](*args)
860       if args.length == 1
861         case args[0]
862         when Netmask
863           self.find { |mask|
864             mask.matches?(args[0])
865           }
866         when String
867           self.find { |mask|
868             mask.matches?(args[0].to_irc_netmask(:casemap => mask.casemap))
869           }
870         when Regexp
871           self.find { |mask|
872             mask.fullform =~ args[0]
873           }
874         else
875           super(*args)
876         end
877       else
878         super(*args)
879       end
880     end
881
882   end
883
884 end
885
886
887 class String
888
889   # We keep extending String, this time adding a method that converts a
890   # String into an Irc::Netmask object
891   #
892   def to_irc_netmask(opts={})
893     Irc::Netmask.new(self, opts)
894   end
895
896 end
897
898
899 module Irc
900
901
902   # An IRC User is identified by his/her Netmask (which must not have globs).
903   # In fact, User is just a subclass of Netmask.
904   #
905   # Ideally, the user and host information of an IRC User should never
906   # change, and it shouldn't contain glob patterns. However, IRC is somewhat
907   # idiosincratic and it may be possible to know the nick of a User much before
908   # its user and host are known. Moreover, some networks (namely Freenode) may
909   # change the hostname of a User when (s)he identifies with Nickserv.
910   #
911   # As a consequence, we must allow changes to a User host and user attributes.
912   # We impose a restriction, though: they may not contain glob patterns, except
913   # for the special case of an unknown user/host which is represented by a *.
914   #
915   # It is possible to create a totally unknown User (e.g. for initializations)
916   # by setting the nick to * too.
917   #
918   # TODO list:
919   # * see if it's worth to add the other USER data
920   # * see if it's worth to add NICKSERV status
921   #
922   class User < Netmask
923     alias :to_s :nick
924
925     attr_accessor :real_name
926
927     # Create a new IRC User from a given Netmask (or anything that can be converted
928     # into a Netmask) provided that the given Netmask does not have globs.
929     #
930     def initialize(str="", opts={})
931       super
932       raise ArgumentError, "#{str.inspect} must not have globs (unescaped * or ?)" if nick.has_irc_glob? && nick != "*"
933       raise ArgumentError, "#{str.inspect} must not have globs (unescaped * or ?)" if user.has_irc_glob? && user != "*"
934       raise ArgumentError, "#{str.inspect} must not have globs (unescaped * or ?)" if host.has_irc_glob? && host != "*"
935       @away = false
936       @real_name = String.new
937     end
938
939     # The nick of a User may be changed freely, but it must not contain glob patterns.
940     #
941     def nick=(newnick)
942       raise "Can't change the nick to #{newnick}" if defined?(@nick) and newnick.has_irc_glob?
943       super
944     end
945
946     # We have to allow changing the user of an Irc User due to some networks
947     # (e.g. Freenode) changing hostmasks on the fly. We still check if the new
948     # user data has glob patterns though.
949     #
950     def user=(newuser)
951       raise "Can't change the username to #{newuser}" if defined?(@user) and newuser.has_irc_glob?
952       super
953     end
954
955     # We have to allow changing the host of an Irc User due to some networks
956     # (e.g. Freenode) changing hostmasks on the fly. We still check if the new
957     # host data has glob patterns though.
958     #
959     def host=(newhost)
960       raise "Can't change the hostname to #{newhost}" if defined?(@host) and newhost.has_irc_glob?
961       super
962     end
963
964     # Checks if a User is well-known or not by looking at the hostname and user
965     #
966     def known?
967       return nick != "*" && user != "*" && host != "*"
968     end
969
970     # Is the user away?
971     #
972     def away?
973       return @away
974     end
975
976     # Set the away status of the user. Use away=(nil) or away=(false)
977     # to unset away
978     #
979     def away=(msg="")
980       if msg
981         @away = msg
982       else
983         @away = false
984       end
985     end
986
987     # Since to_irc_user runs the same checks on server and channel as
988     # to_irc_netmask, we just try that and return self if it works.
989     #
990     # Subclasses of User will return self if possible.
991     #
992     def to_irc_user(opts={})
993       return self if fits_with_server_and_casemap?(opts)
994       return self.full_downcase.to_irc_user(opts)
995     end
996
997     # We can replace everything at once with data from another User
998     #
999     def replace(other)
1000       case other
1001       when User
1002         self.nick = other.nick
1003         self.user = other.user
1004         self.host = other.host
1005         @server = other.server
1006         @casemap = other.casemap unless @server
1007         @away = other.away?
1008       else
1009         self.replace(other.to_irc_user(server_and_casemap))
1010       end
1011     end
1012
1013     def modes_on(channel)
1014       case channel
1015       when Channel
1016         channel.modes_of(self)
1017       else
1018         return @server.channel(channel).modes_of(self) if @server
1019         raise "Can't resolve channel #{channel}"
1020       end
1021     end
1022
1023     def is_op?(channel)
1024       case channel
1025       when Channel
1026         channel.has_op?(self)
1027       else
1028         return @server.channel(channel).has_op?(self) if @server
1029         raise "Can't resolve channel #{channel}"
1030       end
1031     end
1032
1033     def is_voice?(channel)
1034       case channel
1035       when Channel
1036         channel.has_voice?(self)
1037       else
1038         return @server.channel(channel).has_voice?(self) if @server
1039         raise "Can't resolve channel #{channel}"
1040       end
1041     end
1042   end
1043
1044
1045   # A UserList is an ArrayOf <code>User</code>s
1046   # We derive it from NetmaskList, which allows us to inherit any special
1047   # NetmaskList method
1048   #
1049   class UserList < NetmaskList
1050
1051     # Create a new UserList, optionally filling it with the elements from
1052     # the Array argument fed to it.
1053     #
1054     def initialize(ar=[])
1055       super(ar)
1056       @element_class = User
1057     end
1058
1059     # Convenience method: convert the UserList to a list of nicks. The indices
1060     # are preserved
1061     #
1062     def nicks
1063       self.map { |user| user.nick }
1064     end
1065
1066   end
1067
1068 end
1069
1070 class String
1071
1072   # We keep extending String, this time adding a method that converts a
1073   # String into an Irc::User object
1074   #
1075   def to_irc_user(opts={})
1076     Irc::User.new(self, opts)
1077   end
1078
1079 end
1080
1081 module Irc
1082
1083   # An IRC Channel is identified by its name, and it has a set of properties:
1084   # * a Channel::Topic
1085   # * a UserList
1086   # * a set of Channel::Modes
1087   #
1088   # The Channel::Topic and Channel::Mode classes are defined within the
1089   # Channel namespace because they only make sense there
1090   #
1091   class Channel
1092
1093
1094     # Mode on a Channel
1095     #
1096     class Mode
1097       attr_reader :channel
1098       def initialize(ch)
1099         @channel = ch
1100       end
1101
1102     end
1103
1104     # Hash of modes. Subclass of Hash that defines any? and all?
1105     # to check if boolean modes (Type D) are set
1106     class ModeHash < Hash
1107       def any?(*ar)
1108         !!ar.find { |m| s = m.to_sym ; self[s] && self[s].set? }
1109       end
1110       def all?(*ar)
1111         !ar.find { |m| s = m.to_sym ; !(self[s] && self[s].set?) }
1112       end
1113     end
1114
1115     # Channel modes of type A manipulate lists
1116     #
1117     # Example: b (banlist)
1118     #
1119     class ModeTypeA < Mode
1120       attr_reader :list
1121       def initialize(ch)
1122         super
1123         @list = NetmaskList.new
1124       end
1125
1126       def set(val)
1127         nm = @channel.server.new_netmask(val)
1128         @list << nm unless @list.include?(nm)
1129       end
1130
1131       def reset(val)
1132         nm = @channel.server.new_netmask(val)
1133         @list.delete(nm)
1134       end
1135
1136     end
1137
1138
1139     # Channel modes of type B need an argument
1140     #
1141     # Example: k (key)
1142     #
1143     class ModeTypeB < Mode
1144       def initialize(ch)
1145         super
1146         @arg = nil
1147       end
1148
1149       def status
1150         @arg
1151       end
1152       alias :value :status
1153
1154       def set(val)
1155         @arg = val
1156       end
1157
1158       def reset(val)
1159         @arg = nil if @arg == val
1160       end
1161
1162     end
1163
1164
1165     # Channel modes that change the User prefixes are like
1166     # Channel modes of type B, except that they manipulate
1167     # lists of Users, so they are somewhat similar to channel
1168     # modes of type A
1169     #
1170     class UserMode < ModeTypeB
1171       attr_reader :list
1172       alias :users :list
1173       def initialize(ch)
1174         super
1175         @list = UserList.new
1176       end
1177
1178       def set(val)
1179         u = @channel.server.user(val)
1180         @list << u unless @list.include?(u)
1181       end
1182
1183       def reset(val)
1184         u = @channel.server.user(val)
1185         @list.delete(u)
1186       end
1187
1188     end
1189
1190
1191     # Channel modes of type C need an argument when set,
1192     # but not when they get reset
1193     #
1194     # Example: l (limit)
1195     #
1196     class ModeTypeC < Mode
1197       def initialize(ch)
1198         super
1199         @arg = nil
1200       end
1201
1202       def status
1203         @arg
1204       end
1205       alias :value :status
1206
1207       def set(val)
1208         @arg = val
1209       end
1210
1211       def reset
1212         @arg = nil
1213       end
1214
1215     end
1216
1217
1218     # Channel modes of type D are basically booleans
1219     #
1220     # Example: m (moderate)
1221     #
1222     class ModeTypeD < Mode
1223       def initialize(ch)
1224         super
1225         @set = false
1226       end
1227
1228       def set?
1229         return @set
1230       end
1231
1232       def set
1233         @set = true
1234       end
1235
1236       def reset
1237         @set = false
1238       end
1239
1240     end
1241
1242
1243     # A Topic represents the topic of a channel. It consists of
1244     # the topic itself, who set it and when
1245     #
1246     class Topic
1247       attr_accessor :text, :set_by, :set_on
1248       alias :to_s :text
1249
1250       # Create a new Topic setting the text, the creator and
1251       # the creation time
1252       #
1253       def initialize(text="", set_by="", set_on=Time.new)
1254         @text = text
1255         @set_by = set_by.to_irc_netmask
1256         @set_on = set_on
1257       end
1258
1259       # Replace a Topic with another one
1260       #
1261       def replace(topic)
1262         raise TypeError, "#{topic.inspect} is not of class #{self.class}" unless topic.kind_of?(self.class)
1263         @text = topic.text.dup
1264         @set_by = topic.set_by.dup
1265         @set_on = topic.set_on.dup
1266       end
1267
1268       # Returns self
1269       #
1270       def to_irc_channel_topic
1271         self
1272       end
1273
1274     end
1275
1276   end
1277
1278 end
1279
1280
1281 class String
1282
1283   # Returns an Irc::Channel::Topic with self as text
1284   #
1285   def to_irc_channel_topic
1286     Irc::Channel::Topic.new(self)
1287   end
1288
1289 end
1290
1291
1292 module Irc
1293
1294
1295   # Here we start with the actual Channel class
1296   #
1297   class Channel
1298
1299     include ServerOrCasemap
1300     attr_reader :name, :topic, :mode, :users
1301     alias :to_s :name
1302
1303     def inspect
1304       str = self.__to_s__[0..-2]
1305       str << " on server #{server}" if server
1306       str << " @name=#{@name.inspect} @topic=#{@topic.text.inspect}"
1307       str << " @users=[#{user_nicks.sort.join(', ')}]"
1308       str << ">"
1309     end
1310
1311     # Returns self
1312     #
1313     def to_irc_channel
1314       self
1315     end
1316
1317     # TODO Ho
1318     def user_nicks
1319       @users.map { |u| u.downcase }
1320     end
1321
1322     # Checks if the receiver already has a user with the given _nick_
1323     #
1324     def has_user?(nick)
1325       @users.index(nick.to_irc_user(server_and_casemap))
1326     end
1327
1328     # Returns the user with nick _nick_, if available
1329     #
1330     def get_user(nick)
1331       idx = has_user?(nick)
1332       @users[idx] if idx
1333     end
1334
1335     # Adds a user to the channel
1336     #
1337     def add_user(user, opts={})
1338       silent = opts.fetch(:silent, false) 
1339       if has_user?(user)
1340         warn "Trying to add user #{user} to channel #{self} again" unless silent
1341       else
1342         @users << user.to_irc_user(server_and_casemap)
1343       end
1344     end
1345
1346     # Creates a new channel with the given name, optionally setting the topic
1347     # and an initial users list.
1348     #
1349     # No additional info is created here, because the channel flags and userlists
1350     # allowed depend on the server.
1351     #
1352     def initialize(name, topic=nil, users=[], opts={})
1353       raise ArgumentError, "Channel name cannot be empty" if name.to_s.empty?
1354       warn "Unknown channel prefix #{name[0].chr}" if name !~ /^[&#+!]/
1355       raise ArgumentError, "Invalid character in #{name.inspect}" if name =~ /[ \x07,]/
1356
1357       init_server_or_casemap(opts)
1358
1359       @name = name
1360
1361       @topic = topic ? topic.to_irc_channel_topic : Channel::Topic.new
1362
1363       @users = UserList.new
1364
1365       users.each { |u|
1366         add_user(u)
1367       }
1368
1369       # Flags
1370       @mode = ModeHash.new
1371     end
1372
1373     # Removes a user from the channel
1374     #
1375     def delete_user(user)
1376       @mode.each { |sym, mode|
1377         mode.reset(user) if mode.kind_of?(UserMode)
1378       }
1379       @users.delete(user)
1380     end
1381
1382     # The channel prefix
1383     #
1384     def prefix
1385       name[0].chr
1386     end
1387
1388     # A channel is local to a server if it has the '&' prefix
1389     #
1390     def local?
1391       name[0] == 0x26
1392     end
1393
1394     # A channel is modeless if it has the '+' prefix
1395     #
1396     def modeless?
1397       name[0] == 0x2b
1398     end
1399
1400     # A channel is safe if it has the '!' prefix
1401     #
1402     def safe?
1403       name[0] == 0x21
1404     end
1405
1406     # A channel is normal if it has the '#' prefix
1407     #
1408     def normal?
1409       name[0] == 0x23
1410     end
1411
1412     # Create a new mode
1413     #
1414     def create_mode(sym, kl)
1415       @mode[sym.to_sym] = kl.new(self)
1416     end
1417
1418     def modes_of(user)
1419       l = []
1420       @mode.map { |s, m|
1421         l << s if (m.class <= UserMode and m.list[user])
1422       }
1423       l
1424     end
1425
1426     def has_op?(user)
1427       @mode.has_key?(:o) and @mode[:o].list[user]
1428     end
1429
1430     def has_voice?(user)
1431       @mode.has_key?(:v) and @mode[:v].list[user]
1432     end
1433   end
1434
1435
1436   # A ChannelList is an ArrayOf <code>Channel</code>s
1437   #
1438   class ChannelList < ArrayOf
1439
1440     # Create a new ChannelList, optionally filling it with the elements from
1441     # the Array argument fed to it.
1442     #
1443     def initialize(ar=[])
1444       super(Channel, ar)
1445     end
1446
1447     # Convenience method: convert the ChannelList to a list of channel names.
1448     # The indices are preserved
1449     #
1450     def names
1451       self.map { |chan| chan.name }
1452     end
1453
1454   end
1455
1456 end
1457
1458
1459 class String
1460
1461   # We keep extending String, this time adding a method that converts a
1462   # String into an Irc::Channel object
1463   #
1464   def to_irc_channel(opts={})
1465     Irc::Channel.new(self, opts)
1466   end
1467
1468 end
1469
1470
1471 module Irc
1472
1473
1474   # An IRC Server represents the Server the client is connected to.
1475   #
1476   class Server
1477
1478     attr_reader :hostname, :version, :usermodes, :chanmodes
1479     alias :to_s :hostname
1480     attr_reader :supports, :capabilities
1481
1482     attr_reader :channels, :users
1483
1484     # TODO Ho
1485     def channel_names
1486       @channels.map { |ch| ch.downcase }
1487     end
1488
1489     # TODO Ho
1490     def user_nicks
1491       @users.map { |u| u.downcase }
1492     end
1493
1494     def inspect
1495       chans, users = [@channels, @users].map {|d|
1496         d.sort { |a, b|
1497           a.downcase <=> b.downcase
1498         }.map { |x|
1499           x.inspect
1500         }
1501       }
1502
1503       str = self.__to_s__[0..-2]
1504       str << " @hostname=#{hostname}"
1505       str << " @channels=#{chans}"
1506       str << " @users=#{users}"
1507       str << ">"
1508     end
1509
1510     # Create a new Server, with all instance variables reset to nil (for
1511     # scalar variables), empty channel and user lists and @supports
1512     # initialized to the default values for all known supported features.
1513     #
1514     def initialize
1515       @hostname = @version = @usermodes = @chanmodes = nil
1516
1517       @channels = ChannelList.new
1518
1519       @users = UserList.new
1520
1521       reset_capabilities
1522     end
1523
1524     # Resets the server capabilities
1525     #
1526     def reset_capabilities
1527       @supports = {
1528         :casemapping => 'rfc1459'.to_irc_casemap,
1529         :chanlimit => {},
1530         :chanmodes => {
1531           :typea => nil, # Type A: address lists
1532           :typeb => nil, # Type B: needs a parameter
1533           :typec => nil, # Type C: needs a parameter when set
1534           :typed => nil  # Type D: must not have a parameter
1535         },
1536         :channellen => 50,
1537         :chantypes => "#&!+",
1538         :excepts => nil,
1539         :idchan => {},
1540         :invex => nil,
1541         :kicklen => nil,
1542         :maxlist => {},
1543         :modes => 3,
1544         :network => nil,
1545         :nicklen => 9,
1546         :prefix => {
1547           :modes => [:o, :v],
1548           :prefixes => [:"@", :+]
1549         },
1550         :safelist => nil,
1551         :statusmsg => nil,
1552         :std => nil,
1553         :targmax => {},
1554         :topiclen => nil
1555       }
1556       @capabilities = {}
1557     end
1558
1559     # Convert a mode (o, v, h, ...) to the corresponding
1560     # prefix (@, +, %, ...). See also mode_for_prefix
1561     def prefix_for_mode(mode)
1562       return @supports[:prefix][:prefixes][
1563         @supports[:prefix][:modes].index(mode.to_sym)
1564       ]
1565     end
1566
1567     # Convert a prefix (@, +, %, ...) to the corresponding
1568     # mode (o, v, h, ...). See also prefix_for_mode
1569     def mode_for_prefix(pfx)
1570       return @supports[:prefix][:modes][
1571         @supports[:prefix][:prefixes].index(pfx.to_sym)
1572       ]
1573     end
1574
1575     # Resets the Channel and User list
1576     #
1577     def reset_lists
1578       @users.reverse_each { |u|
1579         delete_user(u)
1580       }
1581       @channels.reverse_each { |u|
1582         delete_channel(u)
1583       }
1584     end
1585
1586     # Clears the server
1587     #
1588     def clear
1589       reset_lists
1590       reset_capabilities
1591       @hostname = @version = @usermodes = @chanmodes = nil
1592     end
1593
1594     # This method is used to parse a 004 RPL_MY_INFO line
1595     #
1596     def parse_my_info(line)
1597       ar = line.split(' ')
1598       @hostname = ar[0]
1599       @version = ar[1]
1600       @usermodes = ar[2]
1601       @chanmodes = ar[3]
1602     end
1603
1604     def noval_warn(key, val, &block)
1605       if val
1606         yield if block_given?
1607       else
1608         warn "No #{key.to_s.upcase} value"
1609       end
1610     end
1611
1612     def val_warn(key, val, &block)
1613       if val == true or val == false or val.nil?
1614         yield if block_given?
1615       else
1616         warn "No #{key.to_s.upcase} value must be specified, got #{val}"
1617       end
1618     end
1619     private :noval_warn, :val_warn
1620
1621     # This method is used to parse a 005 RPL_ISUPPORT line
1622     #
1623     # See the RPL_ISUPPORT draft[http://www.irc.org/tech_docs/draft-brocklesby-irc-isupport-03.txt]
1624     #
1625     def parse_isupport(line)
1626       debug "Parsing ISUPPORT #{line.inspect}"
1627       ar = line.split(' ')
1628       reparse = ""
1629       ar.each { |en|
1630         prekey, val = en.split('=', 2)
1631         if prekey =~ /^-(.*)/
1632           key = $1.downcase.to_sym
1633           val = false
1634         else
1635           key = prekey.downcase.to_sym
1636         end
1637         case key
1638         when :casemapping
1639           noval_warn(key, val) {
1640             @supports[key] = val.to_irc_casemap
1641           }
1642         when :chanlimit, :idchan, :maxlist, :targmax
1643           noval_warn(key, val) {
1644             groups = val.split(',')
1645             groups.each { |g|
1646               k, v = g.split(':')
1647               @supports[key][k] = v.to_i || 0
1648               if @supports[key][k] == 0
1649                 warn "Deleting #{key} limit of 0 for #{k}"
1650                 @supports[key].delete(k)
1651               end
1652             }
1653           }
1654         when :chanmodes
1655           noval_warn(key, val) {
1656             groups = val.split(',')
1657             @supports[key][:typea] = groups[0].scan(/./).map { |x| x.to_sym}
1658             @supports[key][:typeb] = groups[1].scan(/./).map { |x| x.to_sym}
1659             @supports[key][:typec] = groups[2].scan(/./).map { |x| x.to_sym}
1660             @supports[key][:typed] = groups[3].scan(/./).map { |x| x.to_sym}
1661           }
1662         when :channellen, :kicklen, :modes, :topiclen
1663           if val
1664             @supports[key] = val.to_i
1665           else
1666             @supports[key] = nil
1667           end
1668         when :chantypes
1669           @supports[key] = val # can also be nil
1670         when :excepts
1671           val ||= 'e'
1672           @supports[key] = val
1673         when :invex
1674           val ||= 'I'
1675           @supports[key] = val
1676         when :maxchannels
1677           noval_warn(key, val) {
1678             reparse += "CHANLIMIT=(chantypes):#{val} "
1679           }
1680         when :maxtargets
1681           noval_warn(key, val) {
1682             @supports[:targmax]['PRIVMSG'] = val.to_i
1683             @supports[:targmax]['NOTICE'] = val.to_i
1684           }
1685         when :network
1686           noval_warn(key, val) {
1687             @supports[key] = val
1688           }
1689         when :nicklen
1690           noval_warn(key, val) {
1691             @supports[key] = val.to_i
1692           }
1693         when :prefix
1694           if val
1695             val.scan(/\((.*)\)(.*)/) { |m, p|
1696               @supports[key][:modes] = m.scan(/./).map { |x| x.to_sym}
1697               @supports[key][:prefixes] = p.scan(/./).map { |x| x.to_sym}
1698             }
1699           else
1700             @supports[key][:modes] = nil
1701             @supports[key][:prefixes] = nil
1702           end
1703         when :safelist
1704           val_warn(key, val) {
1705             @supports[key] = val.nil? ? true : val
1706           }
1707         when :statusmsg
1708           noval_warn(key, val) {
1709             @supports[key] = val.scan(/./)
1710           }
1711         when :std
1712           noval_warn(key, val) {
1713             @supports[key] = val.split(',')
1714           }
1715         else
1716           @supports[key] =  val.nil? ? true : val
1717         end
1718       }
1719       reparse.gsub!("(chantypes)",@supports[:chantypes])
1720       parse_isupport(reparse) unless reparse.empty?
1721     end
1722
1723     # Returns the casemap of the server.
1724     #
1725     def casemap
1726       @supports[:casemapping]
1727     end
1728
1729     # Returns User or Channel depending on what _name_ can be
1730     # a name of
1731     #
1732     def user_or_channel?(name)
1733       if supports[:chantypes].include?(name[0])
1734         return Channel
1735       else
1736         return User
1737       end
1738     end
1739
1740     # Returns the actual User or Channel object matching _name_
1741     #
1742     def user_or_channel(name)
1743       if supports[:chantypes].include?(name[0])
1744         return channel(name)
1745       else
1746         return user(name)
1747       end
1748     end
1749
1750     # Checks if the receiver already has a channel with the given _name_
1751     #
1752     def has_channel?(name)
1753       return false if name.nil_or_empty?
1754       channel_names.index(name.irc_downcase(casemap))
1755     end
1756     alias :has_chan? :has_channel?
1757
1758     # Returns the channel with name _name_, if available
1759     #
1760     def get_channel(name)
1761       return nil if name.nil_or_empty?
1762       idx = has_channel?(name)
1763       channels[idx] if idx
1764     end
1765     alias :get_chan :get_channel
1766
1767     # Create a new Channel object bound to the receiver and add it to the
1768     # list of <code>Channel</code>s on the receiver, unless the channel was
1769     # present already. In this case, the default action is to raise an
1770     # exception, unless _fails_ is set to false.  An exception can also be
1771     # raised if _str_ is nil or empty, again only if _fails_ is set to true;
1772     # otherwise, the method just returns nil
1773     #
1774     def new_channel(name, topic=nil, users=[], fails=true)
1775       if name.nil_or_empty?
1776         raise "Tried to look for empty or nil channel name #{name.inspect}" if fails
1777         return nil
1778       end
1779       ex = get_chan(name)
1780       if ex
1781         raise "Channel #{name} already exists on server #{self}" if fails
1782         return ex
1783       else
1784
1785         prefix = name[0].chr
1786
1787         # Give a warning if the new Channel goes over some server limits.
1788         #
1789         # FIXME might need to raise an exception
1790         #
1791         warn "#{self} doesn't support channel prefix #{prefix}" unless @supports[:chantypes].include?(prefix)
1792         warn "#{self} doesn't support channel names this long (#{name.length} > #{@supports[:channellen]})" unless name.length <= @supports[:channellen]
1793
1794         # Next, we check if we hit the limit for channels of type +prefix+
1795         # if the server supports +chanlimit+
1796         #
1797         @supports[:chanlimit].keys.each { |k|
1798           next unless k.include?(prefix)
1799           count = 0
1800           channel_names.each { |n|
1801             count += 1 if k.include?(n[0])
1802           }
1803           # raise IndexError, "Already joined #{count} channels with prefix #{k}" if count == @supports[:chanlimit][k]
1804           warn "Already joined #{count}/#{@supports[:chanlimit][k]} channels with prefix #{k}, we may be going over server limits" if count >= @supports[:chanlimit][k]
1805         }
1806
1807         # So far, everything is fine. Now create the actual Channel
1808         #
1809         chan = Channel.new(name, topic, users, :server => self)
1810
1811         # We wade through +prefix+ and +chanmodes+ to create appropriate
1812         # lists and flags for this channel
1813
1814         @supports[:prefix][:modes].each { |mode|
1815           chan.create_mode(mode, Channel::UserMode)
1816         } if @supports[:prefix][:modes]
1817
1818         @supports[:chanmodes].each { |k, val|
1819           if val
1820             case k
1821             when :typea
1822               val.each { |mode|
1823                 chan.create_mode(mode, Channel::ModeTypeA)
1824               }
1825             when :typeb
1826               val.each { |mode|
1827                 chan.create_mode(mode, Channel::ModeTypeB)
1828               }
1829             when :typec
1830               val.each { |mode|
1831                 chan.create_mode(mode, Channel::ModeTypeC)
1832               }
1833             when :typed
1834               val.each { |mode|
1835                 chan.create_mode(mode, Channel::ModeTypeD)
1836               }
1837             end
1838           end
1839         }
1840
1841         @channels << chan
1842         # debug "Created channel #{chan.inspect}"
1843         return chan
1844       end
1845     end
1846
1847     # Returns the Channel with the given _name_ on the server,
1848     # creating it if necessary. This is a short form for
1849     # new_channel(_str_, nil, [], +false+)
1850     #
1851     def channel(str)
1852       new_channel(str,nil,[],false)
1853     end
1854
1855     # Remove Channel _name_ from the list of <code>Channel</code>s
1856     #
1857     def delete_channel(name)
1858       idx = has_channel?(name)
1859       raise "Tried to remove unmanaged channel #{name}" unless idx
1860       @channels.delete_at(idx)
1861     end
1862
1863     # Checks if the receiver already has a user with the given _nick_
1864     #
1865     def has_user?(nick)
1866       return false if nick.nil_or_empty?
1867       user_nicks.index(nick.irc_downcase(casemap))
1868     end
1869
1870     # Returns the user with nick _nick_, if available
1871     #
1872     def get_user(nick)
1873       idx = has_user?(nick)
1874       @users[idx] if idx
1875     end
1876
1877     # Create a new User object bound to the receiver and add it to the list
1878     # of <code>User</code>s on the receiver, unless the User was present
1879     # already. In this case, the default action is to raise an exception,
1880     # unless _fails_ is set to false. An exception can also be raised
1881     # if _str_ is nil or empty, again only if _fails_ is set to true;
1882     # otherwise, the method just returns nil
1883     #
1884     def new_user(str, fails=true)
1885       if str.nil_or_empty?
1886         raise "Tried to look for empty or nil user name #{str.inspect}" if fails
1887         return nil
1888       end
1889       tmp = str.to_irc_user(:server => self)
1890       old = get_user(tmp.nick)
1891       # debug "Tmp: #{tmp.inspect}"
1892       # debug "Old: #{old.inspect}"
1893       if old
1894         # debug "User already existed as #{old.inspect}"
1895         if tmp.known?
1896           if old.known?
1897             # debug "Both were known"
1898             # Do not raise an error: things like Freenode change the hostname after identification
1899             warning "User #{tmp.nick} has inconsistent Netmasks! #{self} knows #{old.inspect} but access was tried with #{tmp.inspect}" if old != tmp
1900             raise "User #{tmp} already exists on server #{self}" if fails
1901           end
1902           if old.fullform.downcase != tmp.fullform.downcase
1903             old.replace(tmp)
1904             # debug "Known user now #{old.inspect}"
1905           end
1906         end
1907         return old
1908       else
1909         warn "#{self} doesn't support nicknames this long (#{tmp.nick.length} > #{@supports[:nicklen]})" unless tmp.nick.length <= @supports[:nicklen]
1910         @users << tmp
1911         return @users.last
1912       end
1913     end
1914
1915     # Returns the User with the given Netmask on the server,
1916     # creating it if necessary. This is a short form for
1917     # new_user(_str_, +false+)
1918     #
1919     def user(str)
1920       new_user(str, false)
1921     end
1922
1923     # Deletes User _user_ from Channel _channel_
1924     #
1925     def delete_user_from_channel(user, channel)
1926       channel.delete_user(user)
1927     end
1928
1929     # Remove User _someuser_ from the list of <code>User</code>s.
1930     # _someuser_ must be specified with the full Netmask.
1931     #
1932     def delete_user(someuser)
1933       idx = has_user?(someuser)
1934       raise "Tried to remove unmanaged user #{user}" unless idx
1935       have = self.user(someuser)
1936       @channels.each { |ch|
1937         delete_user_from_channel(have, ch)
1938       }
1939       @users.delete_at(idx)
1940     end
1941
1942     # Create a new Netmask object with the appropriate casemap
1943     #
1944     def new_netmask(str)
1945       str.to_irc_netmask(:server => self)
1946     end
1947
1948     # Finds all <code>User</code>s on server whose Netmask matches _mask_
1949     #
1950     def find_users(mask)
1951       nm = new_netmask(mask)
1952       @users.inject(UserList.new) {
1953         |list, user|
1954         if user.user == "*" or user.host == "*"
1955           list << user if user.nick.irc_downcase(casemap) =~ nm.nick.irc_downcase(casemap).to_irc_regexp
1956         else
1957           list << user if user.matches?(nm)
1958         end
1959         list
1960       }
1961     end
1962
1963   end
1964
1965 end
1966