3 # * do we want to handle a Channel list for each User telling which
\r
4 # Channels is the User on (of those the client is on too)?
\r
5 # We may want this so that when a User leaves all Channels and he hasn't
\r
6 # sent us privmsgs, we know remove him from the Server @users list
\r
12 # This module defines the fundamental building blocks for IRC
\r
14 # Author:: Giuseppe Bilotta (giuseppe.bilotta@gmail.com)
\r
15 # Copyright:: Copyright (c) 2006 Giuseppe Bilotta
\r
18 # TODO User should have associated Server too
\r
20 # TODO rather than the complex init methods, we should provide a single one (having a String parameter)
\r
21 # and then provide to_irc_netmask(casemap), to_irc_user(server), to_irc_channel(server) etc
\r
24 # We start by extending the String class
\r
25 # with some IRC-specific methods
\r
29 # This method returns a string which is the downcased version of the
\r
30 # receiver, according to IRC rules: due to the Scandinavian origin of IRC,
\r
31 # the characters <tt>{}|^</tt> are considered the uppercase equivalent of
\r
34 # Since IRC is mostly case-insensitive (the Windows way: case is preserved,
\r
35 # but it's actually ignored to check equality), this method is rather
\r
36 # important when checking if two strings refer to the same entity
\r
39 # Modern server allow different casemaps, too, in which some or all
\r
40 # of the extra characters are not converted
\r
42 def irc_downcase(casemap='rfc1459')
\r
45 self.tr("\x41-\x5e", "\x61-\x7e")
\r
46 when 'strict-rfc1459'
\r
47 self.tr("\x41-\x5d", "\x61-\x7d")
\r
49 self.tr("\x41-\x5a", "\x61-\x7a")
\r
51 raise TypeError, "Unknown casemap #{casemap}"
\r
55 # This is the same as the above, except that the string is altered in place
\r
57 # See also the discussion about irc_downcase
\r
59 def irc_downcase!(casemap='rfc1459')
\r
62 self.tr!("\x41-\x5e", "\x61-\x7e")
\r
63 when 'strict-rfc1459'
\r
64 self.tr!("\x41-\x5d", "\x61-\x7d")
\r
66 self.tr!("\x41-\x5a", "\x61-\x7a")
\r
68 raise TypeError, "Unknown casemap #{casemap}"
\r
72 # Upcasing functions are provided too
\r
74 # See also the discussion about irc_downcase
\r
76 def irc_upcase(casemap='rfc1459')
\r
79 self.tr("\x61-\x7e", "\x41-\x5e")
\r
80 when 'strict-rfc1459'
\r
81 self.tr("\x61-\x7d", "\x41-\x5d")
\r
83 self.tr("\x61-\x7a", "\x41-\x5a")
\r
85 raise TypeError, "Unknown casemap #{casemap}"
\r
91 # See also the discussion about irc_downcase
\r
93 def irc_upcase!(casemap='rfc1459')
\r
96 self.tr!("\x61-\x7e", "\x41-\x5e")
\r
97 when 'strict-rfc1459'
\r
98 self.tr!("\x61-\x7d", "\x41-\x5d")
\r
100 self.tr!("\x61-\x7a", "\x41-\x5a")
\r
102 raise TypeError, "Unknown casemap #{casemap}"
\r
106 # This method checks if the receiver contains IRC glob characters
\r
108 # IRC has a very primitive concept of globs: a <tt>*</tt> stands for "any
\r
109 # number of arbitrary characters", a <tt>?</tt> stands for "one and exactly
\r
110 # one arbitrary character". These characters can be escaped by prefixing them
\r
111 # with a slash (<tt>\\</tt>).
\r
113 # A known limitation of this glob syntax is that there is no way to escape
\r
114 # the escape character itself, so it's not possible to build a glob pattern
\r
115 # where the escape character precedes a glob.
\r
118 self =~ /^[*?]|[^\\][*?]/
\r
121 # This method is used to convert the receiver into a Regular Expression
\r
122 # that matches according to the IRC glob syntax
\r
125 regmask = Regexp.escape(self)
\r
126 regmask.gsub!(/(\\\\)?\\[*?]/) { |m|
\r
135 raise "Unexpected match #{m} when converting #{self}"
\r
138 Regexp.new(regmask)
\r
143 # ArrayOf is a subclass of Array whose elements are supposed to be all
\r
144 # of the same class. This is not intended to be used directly, but rather
\r
145 # to be subclassed as needed (see for example Irc::UserList and Irc::NetmaskList)
\r
147 # Presently, only very few selected methods from Array are overloaded to check
\r
148 # if the new elements are the correct class. An orthodox? method is provided
\r
149 # to check the entire ArrayOf against the appropriate class.
\r
151 class ArrayOf < Array
\r
153 attr_reader :element_class
\r
155 # Create a new ArrayOf whose elements are supposed to be all of type _kl_,
\r
156 # optionally filling it with the elements from the Array argument.
\r
158 def initialize(kl, ar=[])
\r
159 raise TypeError, "#{kl.inspect} must be a class name" unless kl.class <= Class
\r
161 @element_class = kl
\r
166 raise TypeError, "#{self.class} can only be initialized from an Array"
\r
170 # Private method to check the validity of the elements passed to it
\r
171 # and optionally raise an error
\r
173 # TODO should it accept nils as valid?
\r
175 def internal_will_accept?(raising, *els)
\r
177 unless el.class <= @element_class
\r
178 raise TypeError, "#{el.inspect} is not of class #{@element_class}" if raising
\r
184 private :internal_will_accept?
\r
186 # This method checks if the passed arguments are acceptable for our ArrayOf
\r
188 def will_accept?(*els)
\r
189 internal_will_accept?(false, *els)
\r
192 # This method checks that all elements are of the appropriate class
\r
195 will_accept?(*self)
\r
198 # This method is similar to the above, except that it raises an exception
\r
199 # if the receiver is not valid
\r
201 raise TypeError unless valid?
\r
204 # Overloaded from Array#<<, checks for appropriate class of argument
\r
207 super(el) if internal_will_accept?(true, el)
\r
210 # Overloaded from Array#unshift, checks for appropriate class of argument(s)
\r
214 super(el) if internal_will_accept?(true, *els)
\r
218 # Overloaded from Array#+, checks for appropriate class of argument elements
\r
221 super(ar) if internal_will_accept?(true, *ar)
\r
226 # The Irc module is used to keep all IRC-related classes
\r
227 # in the same namespace
\r
232 # A Netmask identifies each user by collecting its nick, username and
\r
233 # hostname in the form <tt>nick!user@host</tt>
\r
235 # Netmasks can also contain glob patterns in any of their components; in this
\r
236 # form they are used to refer to more than a user or to a user appearing
\r
241 # * <tt>*!*@*</tt> refers to everybody
\r
242 # * <tt>*!someuser@somehost</tt> refers to user +someuser+ on host +somehost+
\r
243 # regardless of the nick used.
\r
246 attr_reader :nick, :user, :host
\r
247 attr_reader :casemap
\r
250 # Netmask.new(netmask) => new_netmask
\r
251 # Netmask.new(hash={}, casemap=nil) => new_netmask
\r
252 # Netmask.new("nick!user@host", casemap=nil) => new_netmask
\r
254 # Create a new Netmask in any of these forms
\r
255 # 1. from another Netmask (does a .dup)
\r
256 # 2. from a Hash with any of the keys <tt>:nick</tt>, <tt>:user</tt> and
\r
258 # 3. from a String in the form <tt>nick!user@host</tt>
\r
260 # In all but the first forms a casemap may be speficied, the default
\r
263 # The nick is downcased following IRC rules and according to the given casemap.
\r
265 # FIXME check if user and host need to be downcased too.
\r
267 # Empty +nick+, +user+ or +host+ are converted to the generic glob pattern
\r
269 def initialize(str={}, casemap=nil)
\r
272 raise ArgumentError, "Can't set casemap when initializing from other Netmask" if casemap
\r
273 @casemap = str.casemap.dup
\r
274 @nick = str.nick.dup
\r
275 @user = str.user.dup
\r
276 @host = str.host.dup
\r
278 @casemap = casemap || str[:casemap] || 'rfc1459'
\r
279 @nick = str[:nick].to_s.irc_downcase(@casemap)
\r
280 @user = str[:user].to_s
\r
281 @host = str[:host].to_s
\r
285 @casemap = casemap || 'rfc1459'
\r
289 when /^(\S+?)(?:!(\S+)@(?:(\S+))?)?$/
\r
290 @casemap = casemap || 'rfc1459'
\r
291 @nick = $1.irc_downcase(@casemap)
\r
295 raise ArgumentError, "#{str} is not a valid netmask"
\r
298 raise ArgumentError, "#{str} is not a valid netmask"
\r
301 @nick = "*" if @nick.to_s.empty?
\r
302 @user = "*" if @user.to_s.empty?
\r
303 @host = "*" if @host.to_s.empty?
\r
306 # Equality: two Netmasks are equal if they have the same @nick, @user, @host and @casemap
\r
309 self.class == other.class && @nick == other.nick && @user == other.user && @host == other.host && @casemap == other.casemap
\r
312 # This method changes the nick of the Netmask, downcasing the argument
\r
313 # following IRC rules and defaulting to the generic glob pattern if
\r
314 # the result is the null string.
\r
317 @nick = newnick.to_s.irc_downcase(@casemap)
\r
318 @nick = "*" if @nick.empty?
\r
321 # This method changes the user of the Netmask, defaulting to the generic
\r
322 # glob pattern if the result is the null string.
\r
325 @user = newuser.to_s
\r
326 @user = "*" if @user.empty?
\r
329 # This method changes the hostname of the Netmask, defaulting to the generic
\r
330 # glob pattern if the result is the null string.
\r
333 @host = newhost.to_s
\r
334 @host = "*" if @host.empty?
\r
337 # This method changes the casemap of a Netmask, which is needed in some
\r
338 # extreme circumstances. Please use sparingly
\r
340 def casemap=(newcmap)
\r
341 @casemap = newcmap.to_s
\r
342 @casemap = "rfc1459" if @casemap.empty?
\r
345 # This method checks if a Netmask is definite or not, by seeing if
\r
346 # any of its components are defined by globs
\r
349 return @nick.has_irc_glob? || @user.has_irc_glob? || @host.has_irc_glob?
\r
352 # A Netmask is easily converted to a String for the usual representation
\r
355 return "#{nick}!#{user}@#{host}"
\r
357 alias :to_s :fullform
\r
359 # This method is used to match the current Netmask against another one
\r
361 # The method returns true if each component of the receiver matches the
\r
362 # corresponding component of the argument. By _matching_ here we mean that
\r
363 # any netmask described by the receiver is also described by the argument.
\r
365 # In this sense, matching is rather simple to define in the case when the
\r
366 # receiver has no globs: it is just necessary to check if the argument
\r
367 # describes the receiver, which can be done by matching it against the
\r
368 # argument converted into an IRC Regexp (see String#to_irc_regexp).
\r
370 # The situation is also easy when the receiver has globs and the argument
\r
371 # doesn't, since in this case the result is false.
\r
373 # The more complex case in which both the receiver and the argument have
\r
374 # globs is not handled yet.
\r
377 cmp = Netmask.new(arg)
\r
378 raise TypeError, "#{arg} and #{self} have different casemaps" if @casemap != cmp.casemap
\r
379 raise TypeError, "#{arg} is not a valid Netmask" unless cmp.class <= Netmask
\r
380 [:nick, :user, :host].each { |component|
\r
381 us = self.send(component)
\r
382 them = cmp.send(component)
\r
383 raise NotImplementedError if us.has_irc_glob? && them.has_irc_glob?
\r
384 return false if us.has_irc_glob? && !them.has_irc_glob?
\r
385 return false unless us =~ them.to_irc_regexp
\r
390 # Case equality. Checks if arg matches self
\r
393 Netmask.new(arg).matches?(self)
\r
399 self.fullform <=> arg.fullform
\r
401 self.to_s <=> arg.to_s
\r
408 # A NetmaskList is an ArrayOf <code>Netmask</code>s
\r
410 class NetmaskList < ArrayOf
\r
412 # Create a new NetmaskList, optionally filling it with the elements from
\r
413 # the Array argument fed to it.
\r
414 def initialize(ar=[])
\r
420 # An IRC User is identified by his/her Netmask (which must not have
\r
421 # globs). In fact, User is just a subclass of Netmask. However,
\r
422 # a User will not allow one's host or user data to be changed.
\r
424 # Due to the idiosincrasies of the IRC protocol, we allow
\r
425 # the creation of a user with an unknown mask represented by the
\r
426 # glob pattern *@*. Only in this case they may be set.
\r
429 # * see if it's worth to add the other USER data
\r
430 # * see if it's worth to add NICKSERV status
\r
432 class User < Netmask
\r
435 # Create a new IRC User from a given Netmask (or anything that can be converted
\r
436 # into a Netmask) provided that the given Netmask does not have globs.
\r
438 def initialize(str="", casemap=nil)
\r
440 raise ArgumentError, "#{str.inspect} must not have globs (unescaped * or ?)" if nick.has_irc_glob? && nick != "*"
\r
441 raise ArgumentError, "#{str.inspect} must not have globs (unescaped * or ?)" if user.has_irc_glob? && user != "*"
\r
442 raise ArgumentError, "#{str.inspect} must not have globs (unescaped * or ?)" if host.has_irc_glob? && host != "*"
\r
446 # We only allow the user to be changed if it was "*". Otherwise,
\r
447 # we raise an exception if the new host is different from the old one
\r
453 raise "Can't change the username of user #{self}" if user != newuser
\r
457 # We only allow the host to be changed if it was "*". Otherwise,
\r
458 # we raise an exception if the new host is different from the old one
\r
464 raise "Can't change the hostname of user #{self}" if host != newhost
\r
468 # Checks if a User is well-known or not by looking at the hostname and user
\r
471 return user!="*" && host!="*"
\r
474 # Is the user away?
\r
480 # Set the away status of the user. Use away=(nil) or away=(false)
\r
493 # A UserList is an ArrayOf <code>User</code>s
\r
495 class UserList < ArrayOf
\r
497 # Create a new UserList, optionally filling it with the elements from
\r
498 # the Array argument fed to it.
\r
499 def initialize(ar=[])
\r
505 # A ChannelTopic represents the topic of a channel. It consists of
\r
506 # the topic itself, who set it and when
\r
508 attr_accessor :text, :set_by, :set_on
\r
511 # Create a new ChannelTopic setting the text, the creator and
\r
512 # the creation time
\r
513 def initialize(text="", set_by="", set_on=Time.new)
\r
519 # Replace a ChannelTopic with another one
\r
521 raise TypeError, "#{topic.inspect} is not an Irc::ChannelTopic" unless topic.class <= ChannelTopic
\r
522 @text = topic.text.dup
\r
523 @set_by = topic.set_by.dup
\r
524 @set_on = topic.set_on.dup
\r
529 # Mode on a channel
\r
537 # Channel modes of type A manipulate lists
\r
539 class ChannelModeTypeA < ChannelMode
\r
542 @list = NetmaskList.new
\r
546 nm = @channel.server.new_netmask(val)
\r
547 @list << nm unless @list.include?(nm)
\r
551 nm = @channel.server.new_netmask(val)
\r
556 # Channel modes of type B need an argument
\r
558 class ChannelModeTypeB < ChannelMode
\r
569 @arg = nil if @arg == val
\r
573 # Channel modes that change the User prefixes are like
\r
574 # Channel modes of type B, except that they manipulate
\r
575 # lists of Users, so they are somewhat similar to channel
\r
578 class ChannelUserMode < ChannelModeTypeB
\r
581 @list = UserList.new
\r
585 u = @channel.server.user(val)
\r
586 @list << u unless @list.include?(u)
\r
590 u = @channel.server.user(val)
\r
595 # Channel modes of type C need an argument when set,
\r
596 # but not when they get reset
\r
598 class ChannelModeTypeC < ChannelMode
\r
613 # Channel modes of type D are basically booleans
\r
614 class ChannelModeTypeD < ChannelMode
\r
634 # An IRC Channel is identified by its name, and it has a set of properties:
\r
640 attr_reader :name, :topic, :mode, :users, :server
\r
643 # A String describing the Channel and (some of its) internals
\r
646 str = "<#{self.class}:#{'0x%08x' % self.object_id}:"
\r
647 str << " on server #{server}"
\r
648 str << " @name=#{@name.inspect} @topic=#{@topic.text.inspect}"
\r
649 str << " @users=<#{@users.sort.join(', ')}>"
\r
653 # Creates a new channel with the given name, optionally setting the topic
\r
654 # and an initial users list.
\r
656 # No additional info is created here, because the channel flags and userlists
\r
657 # allowed depend on the server.
\r
659 # FIXME doesn't check if users have the same casemap as the channel yet
\r
661 def initialize(server, name, topic=nil, users=[])
\r
662 raise TypeError, "First parameter must be an Irc::Server" unless server.class <= Server
\r
663 raise ArgumentError, "Channel name cannot be empty" if name.to_s.empty?
\r
664 raise ArgumentError, "Unknown channel prefix #{name[0].chr}" if name !~ /^[&#+!]/
\r
665 raise ArgumentError, "Invalid character in #{name.inspect}" if name =~ /[ \x07,]/
\r
669 @name = name.irc_downcase(casemap)
\r
671 @topic = topic || ChannelTopic.new
\r
677 @users = UserList.new(users)
\r
679 raise ArgumentError, "Invalid user list #{users.inspect}"
\r
686 # Returns the casemap of the originating server
\r
688 return @server.casemap
\r
691 # Removes a user from the channel
\r
693 def delete_user(user)
\r
694 @mode.each { |sym, mode|
\r
695 mode.reset(user) if mode.class <= ChannelUserMode
\r
697 @users.delete(user)
\r
700 # The channel prefix
\r
706 # A channel is local to a server if it has the '&' prefix
\r
712 # A channel is modeless if it has the '+' prefix
\r
718 # A channel is safe if it has the '!' prefix
\r
724 # A channel is safe if it has the '#' prefix
\r
730 # Create a new mode
\r
732 def create_mode(sym, kl)
\r
733 @mode[sym.to_sym] = kl.new(self)
\r
738 # A ChannelList is an ArrayOf <code>Channel</code>s
\r
740 class ChannelList < ArrayOf
\r
742 # Create a new ChannelList, optionally filling it with the elements from
\r
743 # the Array argument fed to it.
\r
744 def initialize(ar=[])
\r
750 # An IRC Server represents the Server the client is connected to.
\r
754 attr_reader :hostname, :version, :usermodes, :chanmodes
\r
755 alias :to_s :hostname
\r
756 attr_reader :supports, :capabilities
\r
758 attr_reader :channels, :users
\r
761 @channels.map { |ch| ch.name }
\r
765 @users.map { |u| u.nick }
\r
769 chans = @channels.map { |ch|
\r
772 users = @users.map { |u|
\r
776 str = "<#{self.class}:#{'0x%08x' % self.object_id}:"
\r
777 str << " @channels=#{chans}"
\r
778 str << " @users=#{users}>"
\r
782 # Create a new Server, with all instance variables reset
\r
783 # to nil (for scalar variables), the channel and user lists
\r
784 # are empty, and @supports is initialized to the default values
\r
785 # for all known supported features.
\r
788 @hostname = @version = @usermodes = @chanmodes = nil
\r
790 @channels = ChannelList.new
\r
792 @users = UserList.new
\r
797 # Resets the server capabilities
\r
799 def reset_capabilities
\r
801 :casemapping => 'rfc1459',
\r
804 :typea => nil, # Type A: address lists
\r
805 :typeb => nil, # Type B: needs a parameter
\r
806 :typec => nil, # Type C: needs a parameter when set
\r
807 :typed => nil # Type D: must not have a parameter
\r
809 :channellen => 200,
\r
810 :chantypes => "#&",
\r
820 :modes => 'ov'.scan(/./),
\r
821 :prefixes => '@+'.scan(/./)
\r
832 # Resets the Channel and User list
\r
838 @channels.each { |u|
\r
843 # Clears the server
\r
850 # This method is used to parse a 004 RPL_MY_INFO line
\r
852 def parse_my_info(line)
\r
853 ar = line.split(' ')
\r
860 def noval_warn(key, val, &block)
\r
862 yield if block_given?
\r
864 warn "No #{key.to_s.upcase} value"
\r
868 def val_warn(key, val, &block)
\r
869 if val == true or val == false or val.nil?
\r
870 yield if block_given?
\r
872 warn "No #{key.to_s.upcase} value must be specified, got #{val}"
\r
875 private :noval_warn, :val_warn
\r
877 # This method is used to parse a 005 RPL_ISUPPORT line
\r
879 # See the RPL_ISUPPORT draft[http://www.irc.org/tech_docs/draft-brocklesby-irc-isupport-03.txt]
\r
881 def parse_isupport(line)
\r
882 debug "Parsing ISUPPORT #{line.inspect}"
\r
883 ar = line.split(' ')
\r
886 prekey, val = en.split('=', 2)
\r
887 if prekey =~ /^-(.*)/
\r
888 key = $1.downcase.to_sym
\r
891 key = prekey.downcase.to_sym
\r
894 when :casemapping, :network
\r
895 noval_warn(key, val) {
\r
896 @supports[key] = val
\r
898 debug "Resetting casemap of #{u} from #{u.casemap} to #{val}"
\r
902 when :chanlimit, :idchan, :maxlist, :targmax
\r
903 noval_warn(key, val) {
\r
904 groups = val.split(',')
\r
906 k, v = g.split(':')
\r
907 @supports[key][k] = v.to_i
\r
911 noval_warn(key, val) {
\r
912 reparse += "CHANLIMIT=(chantypes):#{val} "
\r
915 noval_warn(key, val) {
\r
916 @supports[key]['PRIVMSG'] = val.to_i
\r
917 @supports[key]['NOTICE'] = val.to_i
\r
920 noval_warn(key, val) {
\r
921 groups = val.split(',')
\r
922 @supports[key][:typea] = groups[0].scan(/./).map { |x| x.to_sym}
\r
923 @supports[key][:typeb] = groups[1].scan(/./).map { |x| x.to_sym}
\r
924 @supports[key][:typec] = groups[2].scan(/./).map { |x| x.to_sym}
\r
925 @supports[key][:typed] = groups[3].scan(/./).map { |x| x.to_sym}
\r
927 when :channellen, :kicklen, :modes, :topiclen
\r
929 @supports[key] = val.to_i
\r
931 @supports[key] = nil
\r
934 @supports[key] = val # can also be nil
\r
937 @supports[key] = val
\r
940 @supports[key] = val
\r
942 noval_warn(key, val) {
\r
943 @supports[key] = val.to_i
\r
947 val.scan(/\((.*)\)(.*)/) { |m, p|
\r
948 @supports[key][:modes] = m.scan(/./).map { |x| x.to_sym}
\r
949 @supports[key][:prefixes] = p.scan(/./).map { |x| x.to_sym}
\r
952 @supports[key][:modes] = nil
\r
953 @supports[key][:prefixes] = nil
\r
956 val_warn(key, val) {
\r
957 @supports[key] = val.nil? ? true : val
\r
960 noval_warn(key, val) {
\r
961 @supports[key] = val.scan(/./)
\r
964 noval_warn(key, val) {
\r
965 @supports[key] = val.split(',')
\r
968 @supports[key] = val.nil? ? true : val
\r
971 reparse.gsub!("(chantypes)",@supports[:chantypes])
\r
972 parse_isupport(reparse) unless reparse.empty?
\r
975 # Returns the casemap of the server.
\r
978 @supports[:casemapping] || 'rfc1459'
\r
981 # Returns User or Channel depending on what _name_ can be
\r
984 def user_or_channel?(name)
\r
985 if supports[:chantypes].include?(name[0])
\r
992 # Returns the actual User or Channel object matching _name_
\r
994 def user_or_channel(name)
\r
995 if supports[:chantypes].include?(name[0])
\r
996 return channel(name)
\r
1002 # Checks if the receiver already has a channel with the given _name_
\r
1004 def has_channel?(name)
\r
1005 channel_names.index(name.to_s)
\r
1007 alias :has_chan? :has_channel?
\r
1009 # Returns the channel with name _name_, if available
\r
1011 def get_channel(name)
\r
1012 idx = channel_names.index(name.to_s)
\r
1013 channels[idx] if idx
\r
1015 alias :get_chan :get_channel
\r
1017 # Create a new Channel object and add it to the list of
\r
1018 # <code>Channel</code>s on the receiver, unless the channel
\r
1019 # was present already. In this case, the default action is
\r
1020 # to raise an exception, unless _fails_ is set to false
\r
1022 # The Channel is automatically created with the appropriate casemap
\r
1024 def new_channel(name, topic=nil, users=[], fails=true)
\r
1025 ex = get_chan(name)
\r
1027 raise "Channel #{name} already exists on server #{self}" if fails
\r
1031 prefix = name[0].chr
\r
1033 # Give a warning if the new Channel goes over some server limits.
\r
1035 # FIXME might need to raise an exception
\r
1037 warn "#{self} doesn't support channel prefix #{prefix}" unless @supports[:chantypes].include?(prefix)
\r
1038 warn "#{self} doesn't support channel names this long (#{name.length} > #{@supports[:channellen]})" unless name.length <= @supports[:channellen]
\r
1040 # Next, we check if we hit the limit for channels of type +prefix+
\r
1041 # if the server supports +chanlimit+
\r
1043 @supports[:chanlimit].keys.each { |k|
\r
1044 next unless k.include?(prefix)
\r
1046 channel_names.each { |n|
\r
1047 count += 1 if k.include?(n[0])
\r
1049 raise IndexError, "Already joined #{count} channels with prefix #{k}" if count == @supports[:chanlimit][k]
\r
1052 # So far, everything is fine. Now create the actual Channel
\r
1054 chan = Channel.new(self, name, topic, users)
\r
1056 # We wade through +prefix+ and +chanmodes+ to create appropriate
\r
1057 # lists and flags for this channel
\r
1059 @supports[:prefix][:modes].each { |mode|
\r
1060 chan.create_mode(mode, ChannelUserMode)
\r
1061 } if @supports[:prefix][:modes]
\r
1063 @supports[:chanmodes].each { |k, val|
\r
1068 chan.create_mode(mode, ChannelModeTypeA)
\r
1072 chan.create_mode(mode, ChannelModeTypeB)
\r
1076 chan.create_mode(mode, ChannelModeTypeC)
\r
1080 chan.create_mode(mode, ChannelModeTypeD)
\r
1087 # debug "Created channel #{chan.inspect}"
\r
1092 # Returns the Channel with the given _name_ on the server,
\r
1093 # creating it if necessary. This is a short form for
\r
1094 # new_channel(_str_, nil, [], +false+)
\r
1097 new_channel(str,nil,[],false)
\r
1100 # Remove Channel _name_ from the list of <code>Channel</code>s
\r
1102 def delete_channel(name)
\r
1103 idx = has_channel?(name)
\r
1104 raise "Tried to remove unmanaged channel #{name}" unless idx
\r
1105 @channels.delete_at(idx)
\r
1108 # Checks if the receiver already has a user with the given _nick_
\r
1110 def has_user?(nick)
\r
1111 user_nicks.index(nick.to_s)
\r
1114 # Returns the user with nick _nick_, if available
\r
1116 def get_user(nick)
\r
1117 idx = user_nicks.index(nick.to_s)
\r
1118 @users[idx] if idx
\r
1121 # Create a new User object and add it to the list of
\r
1122 # <code>User</code>s on the receiver, unless the User
\r
1123 # was present already. In this case, the default action is
\r
1124 # to raise an exception, unless _fails_ is set to false
\r
1126 # The User is automatically created with the appropriate casemap
\r
1128 def new_user(str, fails=true)
\r
1133 tmp = User.new(str, self.casemap)
\r
1135 # debug "Creating or selecting user #{tmp.inspect} from #{str.inspect}"
\r
1136 old = get_user(tmp.nick)
\r
1138 # debug "User already existed as #{old.inspect}"
\r
1141 raise "User #{tmp.nick} has inconsistent Netmasks! #{self} knows #{old.inspect} but access was tried with #{tmp.inspect}" if old != tmp
\r
1142 raise "User #{tmp} already exists on server #{self}" if fails
\r
1144 old.user = tmp.user
\r
1145 old.host = tmp.host
\r
1146 # debug "User improved to #{old.inspect}"
\r
1151 warn "#{self} doesn't support nicknames this long (#{tmp.nick.length} > #{@supports[:nicklen]})" unless tmp.nick.length <= @supports[:nicklen]
\r
1153 return @users.last
\r
1157 # Returns the User with the given Netmask on the server,
\r
1158 # creating it if necessary. This is a short form for
\r
1159 # new_user(_str_, +false+)
\r
1162 new_user(str, false)
\r
1165 # Remove User _someuser_ from the list of <code>User</code>s.
\r
1166 # _someuser_ must be specified with the full Netmask.
\r
1168 def delete_user(someuser)
\r
1169 idx = has_user?(someuser.nick)
\r
1170 raise "Tried to remove unmanaged user #{user}" unless idx
\r
1171 have = self.user(someuser)
\r
1172 raise "User #{someuser.nick} has inconsistent Netmasks! #{self} knows #{have} but access was tried with #{someuser}" if have != someuser && have.user != "*" && have.host != "*"
\r
1173 @channels.each { |ch|
\r
1174 delete_user_from_channel(have, ch)
\r
1176 @users.delete_at(idx)
\r
1179 # Create a new Netmask object with the appropriate casemap
\r
1181 def new_netmask(str)
\r
1182 if str.class <= Netmask
\r
1183 raise "Wrong casemap for Netmask #{str.inspect}" if str.casemap != self.casemap
\r
1186 Netmask.new(str, self.casemap)
\r
1189 # Finds all <code>User</code>s on server whose Netmask matches _mask_
\r
1191 def find_users(mask)
\r
1192 nm = new_netmask(mask)
\r
1193 @users.inject(UserList.new) {
\r
1195 if user.user == "*" or user.host == "*"
\r
1196 list << user if user.nick =~ nm.nick.to_irc_regexp
\r
1198 list << user if user.matches?(nm)
\r
1204 # Deletes User from Channel
\r
1206 def delete_user_from_channel(user, channel)
\r
1207 channel.delete_user(user)
\r