Class: Butler::IRC::Hostmask

Inherits:
Object
  • Object
show all
Defined in:
lib/butler/irc/hostmask.rb

Overview

Provides methods to see if hostmasks match

Constant Summary collapse

Filter =

List of characters to escape in hostmasks and with what to replace

[
	[/([\\\[\]{}^`.-])/, '\\\\\1'],
	[/\?/,  "."],
	[/\*/, ".*?"],
]

Instance Method Summary collapse

Constructor Details

#initialize(mask) ⇒ Hostmask

Create a hostmask from a hostmask-string, e.g. “[email protected]” Also see Butler::IRC::User#hostmask



23
24
25
26
27
28
29
30
31
32
# File 'lib/butler/irc/hostmask.rb', line 23

def initialize(mask)
	@mask    = mask.freeze

	# FIXME, not yet correct, escaped * and ?
	filtered = Filter.inject(@mask) { |s,(a,b)| s.gsub(a,b) }
	# FIXME implement later
	#string.gsub(/[\\\x00-\x1f]/) { |match| ("\\%02x" % match[0]) }
	n,u,h    = filtered.split(/[!@]/)
	@regex	= Regexp.new("\A(#{n})!(#{u})@(#{h})\z")
end

Instance Method Details

#=~(mask) ⇒ Object

Match a hostmask or anything that responds to #hostmask or #to_str Sets $1-$3 to nick, user and host if matched.



36
37
38
39
# File 'lib/butler/irc/hostmask.rb', line 36

def =~(mask)
	mask	= mask.hostmask if mask.respond_to?(:hostmask)
	!!(@regex =~ mask.to_str)
end

#inspectObject

:nodoc:



55
56
57
# File 'lib/butler/irc/hostmask.rb', line 55

def inspect # :nodoc:
	"#<Hostmask #{@mask}>"
end

#match(mask) ⇒ Object Also known as: ===

Match a hostmask or anything that responds to #hostmask or #to_str Returns a MatchData instance with 3 captures (nick, user, host)



43
44
45
46
# File 'lib/butler/irc/hostmask.rb', line 43

def match(mask)
	mask	= mask.hostmask if mask.kind_of?(User)
	@regex.match(mask.to_str)
end

#to_strObject Also known as: to_s

return the mask-string



50
51
52
# File 'lib/butler/irc/hostmask.rb', line 50

def to_str
	@mask
end