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
19 # We start by extending the String class
\r
20 # with some IRC-specific methods
\r
24 # This method returns a string which is the downcased version of the
\r
25 # receiver, according to IRC rules: due to the Scandinavian origin of IRC,
\r
26 # the characters <tt>{}|^</tt> are considered the uppercase equivalent of
\r
29 # Since IRC is mostly case-insensitive (the Windows way: case is preserved,
\r
30 # but it's actually ignored to check equality), this method is rather
\r
31 # important when checking if two strings refer to the same entity
\r
34 # Modern server allow different casemaps, too, in which some or all
\r
35 # of the extra characters are not converted
\r
37 def irc_downcase(casemap='rfc1459')
\r
40 self.tr("\x41-\x5e", "\x61-\x7e")
\r
41 when 'strict-rfc1459'
\r
42 self.tr("\x41-\x5d", "\x61-\x7d")
\r
44 self.tr("\x41-\x5a", "\x61-\x7a")
\r
46 raise TypeError, "Unknown casemap #{casemap}"
\r
50 # This is the same as the above, except that the string is altered in place
\r
52 # See also the discussion about irc_downcase
\r
54 def irc_downcase!(casemap='rfc1459')
\r
57 self.tr!("\x41-\x5e", "\x61-\x7e")
\r
58 when 'strict-rfc1459'
\r
59 self.tr!("\x41-\x5d", "\x61-\x7d")
\r
61 self.tr!("\x41-\x5a", "\x61-\x7a")
\r
63 raise TypeError, "Unknown casemap #{casemap}"
\r
67 # Upcasing functions are provided too
\r
69 # See also the discussion about irc_downcase
\r
71 def irc_upcase(casemap='rfc1459')
\r
74 self.tr("\x61-\x7e", "\x41-\x5e")
\r
75 when 'strict-rfc1459'
\r
76 self.tr("\x61-\x7d", "\x41-\x5d")
\r
78 self.tr("\x61-\x7a", "\x41-\x5a")
\r
80 raise TypeError, "Unknown casemap #{casemap}"
\r
86 # See also the discussion about irc_downcase
\r
88 def irc_upcase!(casemap='rfc1459')
\r
91 self.tr!("\x61-\x7e", "\x41-\x5e")
\r
92 when 'strict-rfc1459'
\r
93 self.tr!("\x61-\x7d", "\x41-\x5d")
\r
95 self.tr!("\x61-\x7a", "\x41-\x5a")
\r
97 raise TypeError, "Unknown casemap #{casemap}"
\r
101 # This method checks if the receiver contains IRC glob characters
\r
103 # IRC has a very primitive concept of globs: a <tt>*</tt> stands for "any
\r
104 # number of arbitrary characters", a <tt>?</tt> stands for "one and exactly
\r
105 # one arbitrary character". These characters can be escaped by prefixing them
\r
106 # with a slash (<tt>\\</tt>).
\r
108 # A known limitation of this glob syntax is that there is no way to escape
\r
109 # the escape character itself, so it's not possible to build a glob pattern
\r
110 # where the escape character precedes a glob.
\r
113 self =~ /^[*?]|[^\\][*?]/
\r
116 # This method is used to convert the receiver into a Regular Expression
\r
117 # that matches according to the IRC glob syntax
\r
120 regmask = Regexp.escape(self)
\r
121 regmask.gsub!(/(\\\\)?\\[*?]/) { |m|
\r
130 raise "Unexpected match #{m} when converting #{self}"
\r
133 Regexp.new(regmask)
\r
138 # ArrayOf is a subclass of Array whose elements are supposed to be all
\r
139 # of the same class. This is not intended to be used directly, but rather
\r
140 # to be subclassed as needed (see for example Irc::UserList and Irc::NetmaskList)
\r
142 # Presently, only very few selected methods from Array are overloaded to check
\r
143 # if the new elements are the correct class. An orthodox? method is provided
\r
144 # to check the entire ArrayOf against the appropriate class.
\r
146 class ArrayOf < Array
\r
148 attr_reader :element_class
\r
150 # Create a new ArrayOf whose elements are supposed to be all of type _kl_,
\r
151 # optionally filling it with the elements from the Array argument.
\r
153 def initialize(kl, ar=[])
\r
154 raise TypeError, "#{kl.inspect} must be a class name" unless kl.class <= Class
\r
156 @element_class = kl
\r
161 raise TypeError, "#{self.class} can only be initialized from an Array"
\r
165 # Private method to check the validity of the elements passed to it
\r
166 # and optionally raise an error
\r
168 # TODO should it accept nils as valid?
\r
170 def internal_will_accept?(raising, *els)
\r
172 unless el.class <= @element_class
\r
173 raise TypeError if raising
\r
179 private :internal_will_accept?
\r
181 # This method checks if the passed arguments are acceptable for our ArrayOf
\r
183 def will_accept?(*els)
\r
184 internal_will_accept?(false, *els)
\r
187 # This method checks that all elements are of the appropriate class
\r
190 will_accept?(*self)
\r
193 # This method is similar to the above, except that it raises an exception
\r
194 # if the receiver is not valid
\r
196 raise TypeError unless valid?
\r
199 # Overloaded from Array#<<, checks for appropriate class of argument
\r
202 super(el) if internal_will_accept?(true, el)
\r
205 # Overloaded from Array#unshift, checks for appropriate class of argument(s)
\r
209 super(el) if internal_will_accept?(true, *els)
\r
213 # Overloaded from Array#+, checks for appropriate class of argument elements
\r
216 super(ar) if internal_will_accept?(true, *ar)
\r
220 # The Irc module is used to keep all IRC-related classes
\r
221 # in the same namespace
\r
226 # A Netmask identifies each user by collecting its nick, username and
\r
227 # hostname in the form <tt>nick!user@host</tt>
\r
229 # Netmasks can also contain glob patterns in any of their components; in this
\r
230 # form they are used to refer to more than a user or to a user appearing
\r
235 # * <tt>*!*@*</tt> refers to everybody
\r
236 # * <tt>*!someuser@somehost</tt> refers to user +someuser+ on host +somehost+
\r
237 # regardless of the nick used.
\r
240 attr_reader :nick, :user, :host
\r
241 attr_reader :casemap
\r
244 # Netmask.new(netmask) => new_netmask
\r
245 # Netmask.new(hash={}, casemap=nil) => new_netmask
\r
246 # Netmask.new("nick!user@host", casemap=nil) => new_netmask
\r
248 # Create a new Netmask in any of these forms
\r
249 # 1. from another Netmask (does a .dup)
\r
250 # 2. from a Hash with any of the keys <tt>:nick</tt>, <tt>:user</tt> and
\r
252 # 3. from a String in the form <tt>nick!user@host</tt>
\r
254 # In all but the first forms a casemap may be speficied, the default
\r
257 # The nick is downcased following IRC rules and according to the given casemap.
\r
259 # FIXME check if user and host need to be downcased too.
\r
261 # Empty +nick+, +user+ or +host+ are converted to the generic glob pattern
\r
263 def initialize(str={}, casemap=nil)
\r
266 raise ArgumentError, "Can't set casemap when initializing from other Netmask" if casemap
\r
267 @casemap = str.casemap.dup
\r
268 @nick = str.nick.dup
\r
269 @user = str.user.dup
\r
270 @host = str.host.dup
\r
272 @casemap = casemap || str[:casemap] || 'rfc1459'
\r
273 @nick = str[:nick].to_s.irc_downcase(@casemap)
\r
274 @user = str[:user].to_s
\r
275 @host = str[:host].to_s
\r
279 @casemap = casemap || 'rfc1459'
\r
283 when /(\S+)(?:!(\S+)@(?:(\S+))?)?/
\r
284 @casemap = casemap || 'rfc1459'
\r
285 @nick = $1.irc_downcase(@casemap)
\r
289 raise ArgumentError, "#{str} is not a valid netmask"
\r
292 raise ArgumentError, "#{str} is not a valid netmask"
\r
295 @nick = "*" if @nick.to_s.empty?
\r
296 @user = "*" if @user.to_s.empty?
\r
297 @host = "*" if @host.to_s.empty?
\r
300 # This method changes the nick of the Netmask, downcasing the argument
\r
301 # following IRC rules and defaulting to the generic glob pattern if
\r
302 # the result is the null string.
\r
305 @nick = newnick.to_s.irc_downcase(@casemap)
\r
306 @nick = "*" if @nick.empty?
\r
309 # This method changes the user of the Netmask, defaulting to the generic
\r
310 # glob pattern if the result is the null string.
\r
313 @user = newuser.to_s
\r
314 @user = "*" if @user.empty?
\r
317 # This method changes the hostname of the Netmask, defaulting to the generic
\r
318 # glob pattern if the result is the null string.
\r
321 @host = newhost.to_s
\r
322 @host = "*" if @host.empty?
\r
325 # This method checks if a Netmask is definite or not, by seeing if
\r
326 # any of its components are defined by globs
\r
329 return @nick.has_irc_glob? || @user.has_irc_glob? || @host.has_irc_glob?
\r
332 # A Netmask is easily converted to a String for the usual representation
\r
335 return "#{nick}@#{user}!#{host}"
\r
337 alias :to_s :fullform
\r
339 # This method is used to match the current Netmask against another one
\r
341 # The method returns true if each component of the receiver matches the
\r
342 # corresponding component of the argument. By _matching_ here we mean that
\r
343 # any netmask described by the receiver is also described by the argument.
\r
345 # In this sense, matching is rather simple to define in the case when the
\r
346 # receiver has no globs: it is just necessary to check if the argument
\r
347 # describes the receiver, which can be done by matching it against the
\r
348 # argument converted into an IRC Regexp (see String#to_irc_regexp).
\r
350 # The situation is also easy when the receiver has globs and the argument
\r
351 # doesn't, since in this case the result is false.
\r
353 # The more complex case in which both the receiver and the argument have
\r
354 # globs is not handled yet.
\r
358 raise TypeError, "#{arg} and #{self} have different casemaps" if @casemap != cmp.casemap
\r
359 raise TypeError, "#{arg} is not a valid Netmask" unless cmp.class <= Netmask
\r
360 [:nick, :user, :host].each { |component|
\r
361 us = self.send(:component)
\r
362 them = cmp.send(:component)
\r
363 raise NotImplementedError if us.has_irc_glob? && them.has_irc_glob?
\r
364 return false if us.has_irc_glob? && !them.has_irc_glob?
\r
365 return false unless us =~ them.to_irc_regexp
\r
370 # Case equality. Checks if arg matches self
\r
373 Netmask(arg).matches?(self)
\r
378 # A NetmaskList is an ArrayOf <code>Netmask</code>s
\r
380 class NetmaskList < ArrayOf
\r
382 # Create a new NetmaskList, optionally filling it with the elements from
\r
383 # the Array argument fed to it.
\r
384 def initialize(ar=[])
\r
390 # An IRC User is identified by his/her Netmask (which must not have
\r
391 # globs). In fact, User is just a subclass of Netmask. However,
\r
392 # a User will not allow one's host or user data to be changed.
\r
394 # Due to the idiosincrasies of the IRC protocol, we allow
\r
395 # the creation of a user with an unknown mask represented by the
\r
396 # glob pattern *@*. Only in this case they may be set.
\r
399 # * see if it's worth to add the other USER data
\r
400 # * see if it's worth to add NICKSERV status
\r
402 class User < Netmask
\r
405 # Create a new IRC User from a given Netmask (or anything that can be converted
\r
406 # into a Netmask) provided that the given Netmask does not have globs.
\r
408 def initialize(str="", casemap=nil)
\r
410 raise ArgumentError, "#{str.inspect} must not have globs (unescaped * or ?)" if nick.has_irc_glob? && nick != "*"
\r
411 raise ArgumentError, "#{str.inspect} must not have globs (unescaped * or ?)" if user.has_irc_glob? && user != "*"
\r
412 raise ArgumentError, "#{str.inspect} must not have globs (unescaped * or ?)" if host.has_irc_glob? && host != "*"
\r
416 # We only allow the user to be changed if it was "*". Otherwise,
\r
417 # we raise an exception if the new host is different from the old one
\r
423 raise "Can't change the username of user #{self}" if user != newuser
\r
427 # We only allow the host to be changed if it was "*". Otherwise,
\r
428 # we raise an exception if the new host is different from the old one
\r
434 raise "Can't change the hostname of user #{self}" if host != newhost
\r
438 # Checks if a User is well-known or not by looking at the hostname and user
\r
441 return user!="*" && host!="*"
\r
444 # Is the user away?
\r
450 # Set the away status of the user. Use away=(nil) or away=(false)
\r
463 # A UserList is an ArrayOf <code>User</code>s
\r
465 class UserList < ArrayOf
\r
467 # Create a new UserList, optionally filling it with the elements from
\r
468 # the Array argument fed to it.
\r
469 def initialize(ar=[])
\r
475 # A ChannelTopic represents the topic of a channel. It consists of
\r
476 # the topic itself, who set it and when
\r
478 attr_accessor :text, :set_by, :set_on
\r
481 # Create a new ChannelTopic setting the text, the creator and
\r
482 # the creation time
\r
483 def initialize(text="", set_by="", set_on=Time.new)
\r
491 # Mode on a channel
\r
496 # Channel modes of type A manipulate lists
\r
498 class ChannelModeTypeA < ChannelMode
\r
500 @list = NetmaskList.new
\r
504 @list << val unless @list.include?(val)
\r
512 # Channel modes of type B need an argument
\r
514 class ChannelModeTypeB < ChannelMode
\r
524 @arg = nil if @arg == val
\r
528 # Channel modes that change the User prefixes are like
\r
529 # Channel modes of type B, except that they manipulate
\r
530 # lists of Users, so they are somewhat similar to channel
\r
533 class ChannelUserMode < ChannelModeTypeB
\r
535 @list = UserList.new
\r
539 @list << val unless @list.include?(val)
\r
547 # Channel modes of type C need an argument when set,
\r
548 # but not when they get reset
\r
550 class ChannelModeTypeC < ChannelMode
\r
564 # Channel modes of type D are basically booleans
\r
565 class ChannelModeTypeD
\r
584 # An IRC Channel is identified by its name, and it has a set of properties:
\r
590 attr_reader :name, :topic, :casemap, :mode, :users
\r
593 # A String describing the Channel and (some of its) internals
\r
596 str = "<#{self.class}:#{'0x%08x' % self.object_id}:"
\r
597 str << " @name=#{@name.inspect} @topic=#{@topic.text.inspect}"
\r
598 str << " @users=<#{@users.join(', ')}>"
\r
602 # Creates a new channel with the given name, optionally setting the topic
\r
603 # and an initial users list.
\r
605 # No additional info is created here, because the channel flags and userlists
\r
606 # allowed depend on the server.
\r
608 # FIXME doesn't check if users have the same casemap as the channel yet
\r
610 def initialize(name, topic=nil, users=[], casemap=nil)
\r
611 @casemap = casemap || 'rfc1459'
\r
613 raise ArgumentError, "Channel name cannot be empty" if name.to_s.empty?
\r
614 raise ArgumentError, "Unknown channel prefix #{name[0].chr}" if name !~ /^[&#+!]/
\r
615 raise ArgumentError, "Invalid character in #{name.inspect}" if name =~ /[ \x07,]/
\r
617 @name = name.irc_downcase(@casemap)
\r
619 @topic = topic || ChannelTopic.new
\r
625 @users = UserList.new(users)
\r
627 raise ArgumentError, "Invalid user list #{users.inspect}"
\r
634 # Removes a user from the channel
\r
636 def delete_user(user)
\r
637 @mode.each { |sym, mode|
\r
638 mode.reset(user) if mode.class <= ChannelUserMode
\r
640 @users.delete(user)
\r
643 # The channel prefix
\r
649 # A channel is local to a server if it has the '&' prefix
\r
655 # A channel is modeless if it has the '+' prefix
\r
661 # A channel is safe if it has the '!' prefix
\r
667 # A channel is safe if it has the '#' prefix
\r
673 # Create a new mode
\r
675 def create_mode(sym, kl)
\r
676 @mode[sym.to_sym] = kl.new
\r
681 # A ChannelList is an ArrayOf <code>Channel</code>s
\r
683 class ChannelList < ArrayOf
\r
685 # Create a new ChannelList, optionally filling it with the elements from
\r
686 # the Array argument fed to it.
\r
687 def initialize(ar=[])
\r
693 # An IRC Server represents the Server the client is connected to.
\r
697 attr_reader :hostname, :version, :usermodes, :chanmodes
\r
698 alias :to_s :hostname
\r
699 attr_reader :supports, :capabilities
\r
701 attr_reader :channels, :users
\r
703 # Create a new Server, with all instance variables reset
\r
704 # to nil (for scalar variables), the channel and user lists
\r
705 # are empty, and @supports is initialized to the default values
\r
706 # for all known supported features.
\r
709 @hostname = @version = @usermodes = @chanmodes = nil
\r
711 @channels = ChannelList.new
\r
712 @channel_names = Array.new
\r
714 @users = UserList.new
\r
715 @user_nicks = Array.new
\r
720 # Resets the server capabilities
\r
722 def reset_capabilities
\r
724 :casemapping => 'rfc1459',
\r
727 :typea => nil, # Type A: address lists
\r
728 :typeb => nil, # Type B: needs a parameter
\r
729 :typec => nil, # Type C: needs a parameter when set
\r
730 :typed => nil # Type D: must not have a parameter
\r
732 :channellen => 200,
\r
733 :chantypes => "#&",
\r
743 :modes => 'ov'.scan(/./),
\r
744 :prefixes => '@+'.scan(/./)
\r
755 # Resets the Channel and User list
\r
761 @channels.each { |u|
\r
766 # Clears the server
\r
773 # This method is used to parse a 004 RPL_MY_INFO line
\r
775 def parse_my_info(line)
\r
776 ar = line.split(' ')
\r
783 def noval_warn(key, val, &block)
\r
785 yield if block_given?
\r
787 warn "No #{key.to_s.upcase} value"
\r
791 def val_warn(key, val, &block)
\r
792 if val == true or val == false or val.nil?
\r
793 yield if block_given?
\r
795 warn "No #{key.to_s.upcase} value must be specified, got #{val}"
\r
798 private :noval_warn, :val_warn
\r
800 # This method is used to parse a 005 RPL_ISUPPORT line
\r
802 # See the RPL_ISUPPORT draft[http://www.irc.org/tech_docs/draft-brocklesby-irc-isupport-03.txt]
\r
804 def parse_isupport(line)
\r
805 ar = line.split(' ')
\r
808 prekey, val = en.split('=', 2)
\r
809 if prekey =~ /^-(.*)/
\r
810 key = $1.downcase.to_sym
\r
813 key = prekey.downcase.to_sym
\r
816 when :casemapping, :network
\r
817 noval_warn(key, val) {
\r
818 @supports[key] = val
\r
820 when :chanlimit, :idchan, :maxlist, :targmax
\r
821 noval_warn(key, val) {
\r
822 groups = val.split(',')
\r
824 k, v = g.split(':')
\r
825 @supports[key][k] = v.to_i
\r
829 noval_warn(key, val) {
\r
830 reparse += "CHANLIMIT=(chantypes):#{val} "
\r
833 noval_warn(key, val) {
\r
834 @supports[key]['PRIVMSG'] = val.to_i
\r
835 @supports[key]['NOTICE'] = val.to_i
\r
838 noval_warn(key, val) {
\r
839 groups = val.split(',')
\r
840 @supports[key][:typea] = groups[0].scan(/./)
\r
841 @supports[key][:typeb] = groups[1].scan(/./)
\r
842 @supports[key][:typec] = groups[2].scan(/./)
\r
843 @supports[key][:typed] = groups[3].scan(/./)
\r
845 when :channellen, :kicklen, :modes, :topiclen
\r
847 @supports[key] = val.to_i
\r
849 @supports[key] = nil
\r
852 @supports[key] = val # can also be nil
\r
855 @supports[key] = val
\r
858 @supports[key] = val
\r
860 noval_warn(key, val) {
\r
861 @supports[key] = val.to_i
\r
865 val.scan(/\((.*)\)(.*)/) { |m, p|
\r
866 @supports[key][:modes] = m.scan(/./)
\r
867 @supports[key][:prefixes] = p.scan(/./)
\r
870 @supports[key][:modes] = nil
\r
871 @supports[key][:prefixes] = nil
\r
874 val_warn(key, val) {
\r
875 @supports[key] = val.nil? ? true : val
\r
878 noval_warn(key, val) {
\r
879 @supports[key] = val.scan(/./)
\r
882 noval_warn(key, val) {
\r
883 @supports[key] = val.split(',')
\r
886 @supports[key] = val.nil? ? true : val
\r
889 reparse.gsub!("(chantypes)",@supports[:chantypes])
\r
890 parse_isupport(reparse) unless reparse.empty?
\r
893 # Returns the casemap of the server.
\r
896 @supports[:casemapping] || 'rfc1459'
\r
899 # Returns User or Channel depending on what _name_ can be
\r
902 def user_or_channel?(name)
\r
903 if supports[:chantypes].include?(name[0].chr)
\r
910 # Returns the actual User or Channel object matching _name_
\r
912 def user_or_channel(name)
\r
913 if supports[:chantypes].include?(name[0].chr)
\r
914 return channel(name)
\r
920 # Checks if the receiver already has a channel with the given _name_
\r
922 def has_channel?(name)
\r
923 @channel_names.index(name.to_s)
\r
925 alias :has_chan? :has_channel?
\r
927 # Returns the channel with name _name_, if available
\r
929 def get_channel(name)
\r
930 idx = @channel_names.index(name.to_s)
\r
931 @channels[idx] if idx
\r
933 alias :get_chan :get_channel
\r
935 # Create a new Channel object and add it to the list of
\r
936 # <code>Channel</code>s on the receiver, unless the channel
\r
937 # was present already. In this case, the default action is
\r
938 # to raise an exception, unless _fails_ is set to false
\r
940 # The Channel is automatically created with the appropriate casemap
\r
942 def new_channel(name, topic=nil, users=[], fails=true)
\r
943 if !has_chan?(name)
\r
945 prefix = name[0].chr
\r
947 # Give a warning if the new Channel goes over some server limits.
\r
949 # FIXME might need to raise an exception
\r
951 warn "#{self} doesn't support channel prefix #{prefix}" unless @supports[:chantypes].include?(prefix)
\r
952 warn "#{self} doesn't support channel names this long (#{name.length} > #{@support[:channellen]}" unless name.length <= @supports[:channellen]
\r
954 # Next, we check if we hit the limit for channels of type +prefix+
\r
955 # if the server supports +chanlimit+
\r
957 @supports[:chanlimit].keys.each { |k|
\r
958 next unless k.include?(prefix)
\r
960 @channel_names.each { |n|
\r
961 count += 1 if k.include?(n[0].chr)
\r
963 raise IndexError, "Already joined #{count} channels with prefix #{k}" if count == @supports[:chanlimit][k]
\r
966 # So far, everything is fine. Now create the actual Channel
\r
968 chan = Channel.new(name, topic, users, self.casemap)
\r
970 # We wade through +prefix+ and +chanmodes+ to create appropriate
\r
971 # lists and flags for this channel
\r
973 @supports[:prefix][:modes].each { |mode|
\r
974 chan.create_mode(mode, ChannelUserMode)
\r
975 } if @supports[:prefix][:modes]
\r
977 @supports[:chanmodes].each { |k, val|
\r
982 chan.create_mode(mode, ChannelModeTypeA)
\r
986 chan.create_mode(mode, ChannelModeTypeB)
\r
990 chan.create_mode(mode, ChannelModeTypeC)
\r
994 chan.create_mode(mode, ChannelModeTypeD)
\r
1001 @channel_names << name
\r
1002 debug "Created channel #{chan.inspect}"
\r
1003 debug "Managing channels #{@channel_names.join(', ')}"
\r
1007 raise "Channel #{name} already exists on server #{self}" if fails
\r
1008 return get_channel(name)
\r
1011 # Returns the Channel with the given _name_ on the server,
\r
1012 # creating it if necessary. This is a short form for
\r
1013 # new_channel(_str_, nil, [], +false+)
\r
1016 new_channel(str,nil,[],false)
\r
1019 # Remove Channel _name_ from the list of <code>Channel</code>s
\r
1021 def delete_channel(name)
\r
1022 idx = has_channel?(name)
\r
1023 raise "Tried to remove unmanaged channel #{name}" unless idx
\r
1024 @channel_names.delete_at(idx)
\r
1025 @channels.delete_at(idx)
\r
1028 # Checks if the receiver already has a user with the given _nick_
\r
1030 def has_user?(nick)
\r
1031 @user_nicks.index(nick.to_s)
\r
1034 # Returns the user with nick _nick_, if available
\r
1036 def get_user(nick)
\r
1037 idx = @user_nicks.index(nick.to_s)
\r
1038 @users[idx] if idx
\r
1041 # Create a new User object and add it to the list of
\r
1042 # <code>User</code>s on the receiver, unless the User
\r
1043 # was present already. In this case, the default action is
\r
1044 # to raise an exception, unless _fails_ is set to false
\r
1046 # The User is automatically created with the appropriate casemap
\r
1048 def new_user(str, fails=true)
\r
1053 tmp = User.new(str, self.casemap)
\r
1055 if !has_user?(tmp.nick)
\r
1056 warn "#{self} doesn't support nicknames this long (#{tmp.nick.length} > #{@support[:nicklen]}" unless tmp.nick.length <= @supports[:nicklen]
\r
1058 @user_nicks << tmp.nick
\r
1059 return @users.last
\r
1061 old = get_user(tmp.nick)
\r
1063 raise "User #{tmp.nick} has inconsistent Netmasks! #{self} knows #{old} but access was tried with #{tmp}" if old != tmp
\r
1064 raise "User #{tmp} already exists on server #{self}" if fails
\r
1066 old.user = tmp.user
\r
1067 old.host = tmp.host
\r
1072 # Returns the User with the given Netmask on the server,
\r
1073 # creating it if necessary. This is a short form for
\r
1074 # new_user(_str_, +false+)
\r
1077 new_user(str, false)
\r
1080 # Remove User _someuser_ from the list of <code>User</code>s.
\r
1081 # _someuser_ must be specified with the full Netmask.
\r
1083 def delete_user(someuser)
\r
1084 idx = has_user?(someuser.nick)
\r
1085 raise "Tried to remove unmanaged user #{user}" unless idx
\r
1086 have = self.user(someuser)
\r
1087 raise "User #{someuser.nick} has inconsistent Netmasks! #{self} knows #{have} but access was tried with #{someuser}" if have != someuser && have.user != "*" && have.host != "*"
\r
1088 @channels.each { |ch|
\r
1089 delete_user_from_channel(have, ch)
\r
1091 @user_nicks.delete_at(idx)
\r
1092 @users.delete_at(idx)
\r
1095 # Create a new Netmask object with the appropriate casemap
\r
1097 def new_netmask(str)
\r
1098 if str.class <= Netmask
\r
1099 raise "Wrong casemap for Netmask #{str.inspect}" if str.casemap != self.casemap
\r
1102 Netmask.new(str, self.casemap)
\r
1105 # Finds all <code>User</code>s on server whose Netmask matches _mask_
\r
1107 def find_users(mask)
\r
1108 nm = new_netmask(mask)
\r
1109 @users.inject(UserList.new) {
\r
1111 if user.user == "*" or user.host == "*"
\r
1112 list << user if user.nick =~ nm.nick.to_irc_regexp
\r
1114 list << user if user.matches?(nm)
\r
1120 # Deletes User from Channel
\r
1122 def delete_user_from_channel(user, channel)
\r
1123 channel.delete_user(user)
\r
1135 # puts " -- irc_regexp tests"
\r
1136 # ["*", "a?b", "a*b", "a\\*b", "a\\?b", "a?\\*b", "*a*\\**b?"].each { |s|
\r
1139 # puts s.to_irc_regexp.inspect
\r
1140 # puts "aUb".match(s.to_irc_regexp)[0] if "aUb" =~ s.to_irc_regexp
\r
1143 # puts " -- Netmasks"
\r
1145 # masks << Netmask.new("start")
\r
1146 # masks << masks[0].dup
\r
1147 # masks << Netmask.new(masks[0])
\r
1148 # puts masks.join("\n")
\r
1150 # puts " -- Changing 1"
\r
1151 # masks[1].nick = "me"
\r
1152 # puts masks.join("\n")
\r
1154 # puts " -- Changing 2"
\r
1155 # masks[2].nick = "you"
\r
1156 # puts masks.join("\n")
\r
1158 # puts " -- Channel example"
\r
1159 # ch = Channel.new("#prova")
\r
1161 # puts " -- Methods"
\r
1162 # puts ch.methods.sort.join("\n")
\r
1163 # puts " -- Instance variables"
\r
1164 # puts ch.instance_variables.join("\n")
\r