Class Irc::Netmask
In: lib/rbot/irc.rb
Parent: Object
BasicUserMessage JoinMessage NamesMessage WhoisMessage ModeChangeMessage KickMessage MotdMessage QuitMessage BanlistMessage UserMessage NoSuchTargetMessage TopicMessage NickMessage WelcomeMessage UnknownMessage InviteMessage PartMessage NetmaskList UserList ArrayOf ChannelList Netmask User\n[lib/rbot/botuser.rb\nlib/rbot/irc.rb] Channel Singleton RfcCasemap StrictRfcCasemap AsciiCasemap Casemap PrivMessage NoticeMessage TokyoCabinet::BDB CIBDB Btree CIBtree Socket MessageQueue QueueRing Client DBHash\n[lib/rbot/registry/bdb.rb\nlib/rbot/registry/tc.rb] DBTree\n[lib/rbot/registry/bdb.rb\nlib/rbot/registry/tc.rb] Server NetmaskDb Bot\n[lib/rbot/botuser.rb\nlib/rbot/config.rb\nlib/rbot/ircbot.rb\nlib/rbot/language.rb\nlib/rbot/message.rb\nlib/rbot/messagemapper.rb\nlib/rbot/plugins.rb\nlib/rbot/rbotconfig.rb\nlib/rbot/registry/bdb.rb\nlib/rbot/registry/tc.rb] lib/rbot/ircsocket.rb lib/rbot/rfc2812.rb lib/rbot/registry/tc.rb lib/rbot/irc.rb lib/rbot/maskdb.rb lib/rbot/message.rb lib/rbot/messagemapper.rb lib/rbot/botuser.rb lib/rbot/registry/tc.rb (null) BotConfig PKGConfig ServerOrCasemap Irc dot/m_35_0.png

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:

  • *!*@* refers to everybody
  • *!someuser@somehost refers to user someuser on host somehost regardless of the nick used.

Methods

<=>   ==   ===   downcased   full_downcase   full_irc_downcase   fullform   generalize   has_irc_glob?   host=   ident=   inspect   matches?   new   nick=   replace   to_irc_netmask   to_irc_user   to_s   to_str   user=  

Included Modules

ServerOrCasemap

External Aliases

user -> ident

Attributes

host  [R] 
nick  [R] 
user  [R] 

Public Class methods

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

[Source]

     # 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

Public Instance methods

Sorting is done via the fullform

[Source]

     # File lib/rbot/irc.rb, line 853
853:     def <=>(arg)
854:       case arg
855:       when Netmask
856:         self.fullform.irc_downcase(casemap) <=> arg.fullform.irc_downcase(casemap)
857:       else
858:         self.downcase <=> arg.downcase
859:       end
860:     end

Equality: two Netmasks are equal if they downcase to the same thing

TODO we may want it to try other.to_irc_netmask

[Source]

     # 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

[Source]

     # File lib/rbot/irc.rb, line 847
847:     def ===(arg)
848:       arg.to_irc_netmask(:casemap => casemap).matches?(self)
849:     end

This method returns a new Netmask which is the fully downcased version of the receiver

[Source]

     # File lib/rbot/irc.rb, line 697
697:     def downcased
698:       return self.full_downcase.to_irc_netmask(server_and_casemap)
699:     end

full_downcase() will return the fullform downcased according to the User‘s own casemap

[Source]

     # 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.

[Source]

     # File lib/rbot/irc.rb, line 684
684:     def full_irc_downcase(cmap=casemap)
685:       self.fullform.irc_downcase(cmap)
686:     end

[Source]

     # File lib/rbot/irc.rb, line 673
673:     def fullform
674:       "#{nick}!#{user}@#{host}"
675:     end

[Source]

     # 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

This method checks if a Netmask is definite or not, by seeing if any of its components are defined by globs

[Source]

     # File lib/rbot/irc.rb, line 785
785:     def has_irc_glob?
786:       return @nick.has_irc_glob? || @user.has_irc_glob? || @host.has_irc_glob?
787:     end

This method changes the hostname of the Netmask, defaulting to the generic glob pattern if the result is the null string.

[Source]

     # File lib/rbot/irc.rb, line 762
762:     def host=(newhost)
763:       @host = newhost.to_s
764:       @host = "*" if @host.empty?
765:     end
ident=(newuser)

Alias for user=

Inspection of a Netmask reveals the server it‘s bound to (if there is one), its casemap and the nick, user and host part

[Source]

     # 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.

[Source]

     # 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

This method changes the nick of the Netmask, defaulting to the generic glob pattern if the result is the null string.

[Source]

     # File lib/rbot/irc.rb, line 745
745:     def nick=(newnick)
746:       @nick = newnick.to_s
747:       @nick = "*" if @nick.empty?
748:     end

We can replace everything at once with data from another Netmask

[Source]

     # 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

[Source]

     # 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)

[Source]

     # 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

[Source]

     # 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
to_str()

Alias for fullform

This method changes the user of the Netmask, defaulting to the generic glob pattern if the result is the null string.

[Source]

     # File lib/rbot/irc.rb, line 753
753:     def user=(newuser)
754:       @user = newuser.to_s
755:       @user = "*" if @user.empty?
756:     end

[Validate]