| Class | Irc::Netmask |
| In: |
lib/rbot/irc.rb
|
| Parent: | Object |
A Netmask identifies each user by collecting its nick, username and hostname in the form nick!user@host
Netmasks can also contain glob patterns in any of their components; in this form they are used to refer to more than a user or to a user appearing under different forms.
Example:
| user | -> | ident |
| host | [R] | |
| nick | [R] | |
| user | [R] |
Create a new Netmask from string str, which must be in the form nick!user@host
It is possible to specify a server or a casemap in the optional Hash: these are used to associate the Netmask with the given server and to set its casemap: if a server is specified and a casemap is not, the server‘s casemap is used. If both a server and a casemap are specified, the casemap must match the server‘s casemap or an exception will be raised.
Empty nick, user or host are converted to the generic glob pattern
# File lib/rbot/irc.rb, line 639
639: def initialize(str="", opts={})
640: # First of all, check for server/casemap option
641: #
642: init_server_or_casemap(opts)
643:
644: # Now we can see if the given string _str_ is an actual Netmask
645: if str.respond_to?(:to_str)
646: case str.to_str
647: # We match a pretty generic string, to work around non-compliant
648: # servers
649: when /^(?:(\S+?)(?:(?:!(\S+?))?@(\S+))?)?$/
650: # We do assignment using our internal methods
651: self.nick = $1
652: self.user = $2
653: self.host = $3
654: else
655: raise ArgumentError, "#{str.to_str.inspect} does not represent a valid #{self.class}"
656: end
657: else
658: raise TypeError, "#{str} cannot be converted to a #{self.class}"
659: end
660: end
Equality: two Netmasks are equal if they downcase to the same thing
TODO we may want it to try other.to_irc_netmask
# File lib/rbot/irc.rb, line 737
737: def ==(other)
738: return false unless other.kind_of?(self.class)
739: self.downcase == other.downcase
740: end
Case equality. Checks if arg matches self
# File lib/rbot/irc.rb, line 847
847: def ===(arg)
848: arg.to_irc_netmask(:casemap => casemap).matches?(self)
849: end
full_downcase() will return the fullform downcased according to the User‘s own casemap
# File lib/rbot/irc.rb, line 691
691: def full_downcase
692: self.full_irc_downcase
693: end
This method downcases the fullform of the netmask. While this may not be significantly different from the downcase() method provided by the ServerOrCasemap mixin, it‘s significantly different for Netmask subclasses such as User whose simple downcasing uses the nick only.
# File lib/rbot/irc.rb, line 684
684: def full_irc_downcase(cmap=casemap)
685: self.fullform.irc_downcase(cmap)
686: end
# File lib/rbot/irc.rb, line 789
789: def generalize
790: u = user.dup
791: unless u.has_irc_glob?
792: u.sub!(/^[in]=/, '=') or u.sub!(/^\W(\w+)/, '\1')
793: u = '*' + u
794: end
795:
796: h = host.dup
797: unless h.has_irc_glob?
798: if h.include? '/'
799: h.sub!(/x-\w+$/, 'x-*')
800: else
801: h.match(/^[^\.]+\.[^\.]+$/) or
802: h.sub!(/azzurra[=-][0-9a-f]+/i, '*') or # hello, azzurra, you suck!
803: h.sub!(/^(\d+\.\d+\.\d+\.)\d+$/, '\1*') or
804: h.sub!(/^[^\.]+\./, '*.')
805: end
806: end
807: return Netmask.new("*!#{u}@#{h}", server_and_casemap)
808: end
Inspection of a Netmask reveals the server it‘s bound to (if there is one), its casemap and the nick, user and host part
# File lib/rbot/irc.rb, line 725
725: def inspect
726: str = self.__to_s__[0..-2]
727: str << " @server=#{@server}" if defined?(@server) and @server
728: str << " @nick=#{@nick.inspect} @user=#{@user.inspect}"
729: str << " @host=#{@host.inspect} casemap=#{casemap.inspect}"
730: str << ">"
731: end
This method is used to match the current Netmask against another one
The method returns true if each component of the receiver matches the corresponding component of the argument. By matching here we mean that any netmask described by the receiver is also described by the argument.
In this sense, matching is rather simple to define in the case when the receiver has no globs: it is just necessary to check if the argument describes the receiver, which can be done by matching it against the argument converted into an IRC Regexp (see String#to_irc_regexp).
The situation is also easy when the receiver has globs and the argument doesn‘t, since in this case the result is false.
The more complex case in which both the receiver and the argument have globs is not handled yet.
# File lib/rbot/irc.rb, line 828
828: def matches?(arg)
829: cmp = arg.to_irc_netmask(:casemap => casemap)
830: debug "Matching #{self.fullform} against #{arg.inspect} (#{cmp.fullform})"
831: [:nick, :user, :host].each { |component|
832: us = self.send(component).irc_downcase(casemap)
833: them = cmp.send(component).irc_downcase(casemap)
834: if us.has_irc_glob? && them.has_irc_glob?
835: next if us == them
836: warn NotImplementedError
837: return false
838: end
839: return false if us.has_irc_glob? && !them.has_irc_glob?
840: return false unless us =~ them.to_irc_regexp
841: }
842: return true
843: end
We can replace everything at once with data from another Netmask
# File lib/rbot/irc.rb, line 769
769: def replace(other)
770: case other
771: when Netmask
772: nick = other.nick
773: user = other.user
774: host = other.host
775: @server = other.server
776: @casemap = other.casemap unless @server
777: else
778: replace(other.to_irc_netmask(server_and_casemap))
779: end
780: end
Converts the receiver into a Netmask with the given (optional) server/casemap association. We return self unless a conversion is needed (different casemap/server)
Subclasses of Netmask will return a new Netmask, using full_downcase
# File lib/rbot/irc.rb, line 707
707: def to_irc_netmask(opts={})
708: if self.class == Netmask
709: return self if fits_with_server_and_casemap?(opts)
710: end
711: return self.full_downcase.to_irc_netmask(server_and_casemap.merge(opts))
712: end
Converts the receiver into a User with the given (optional) server/casemap association. We return self unless a conversion is needed (different casemap/server)
# File lib/rbot/irc.rb, line 718
718: def to_irc_user(opts={})
719: self.fullform.to_irc_user(server_and_casemap.merge(opts))
720: end
A Netmask is easily converted to a String for the usual representation. We skip the user or host parts if they are "*", unless we‘ve been asked for the full form
# File lib/rbot/irc.rb, line 666
666: def to_s
667: ret = nick.dup
668: ret << "!" << user unless user == "*"
669: ret << "@" << host unless host == "*"
670: return ret
671: end