Class: Butler::IRC::User

Inherits:
Object
  • Object
show all
Includes:
Flags, Comparable, Enumerable
Defined in:
lib/butler/irc/user.rb,
lib/butler/irc/hostmask.rb,
lib/butler/bot.rb

Overview

Hostmask

Defined Under Namespace

Modules: Flags

Constant Summary collapse

USER_STATUS =
[:unknown, :myself, :online, :out_of_sight, :offline, :mutated]
PREFIXES =
Hash.new(0)

Constants included from Flags

Flags::ADMIN, Flags::AOP, Flags::IRCOP, Flags::OP, Flags::UOP, Flags::VOICE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#join

Constructor Details

#initialize(users, *args, &block) ⇒ User

Returns a new instance of User.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/butler/irc/user.rb', line 51

def initialize(users, channels, nick=nil, user=nil, host=nil, real=nil)
	@nick, @user, @host, @real = nil
	@users        = users
	@nick         = nick.to_str.strip_user_prefixes.freeze if nick
	@user         = user.freeze if user
	@host         = host.freeze if host
	@real         = real.freeze if real
	
	@compare      = @nick ? @nick.downcase : "\xff"
	@all_channels = channels
	@channels     = {} # downcased channelname => flags
	@status       = :unknown
	@is_away      = false
	@away_message = ""
end

Instance Attribute Details

#accessObject

Returns the value of attribute access.



225
226
227
# File 'lib/butler/bot.rb', line 225

def access
  @access
end

#compareObject (readonly)

lowercase nickname



49
50
51
# File 'lib/butler/irc/user.rb', line 49

def compare
  @compare
end

#hostObject (readonly)

hostpart of a user



40
41
42
# File 'lib/butler/irc/user.rb', line 40

def host
  @host
end

#nickObject

nickname of a user



34
35
36
# File 'lib/butler/irc/user.rb', line 34

def nick
  @nick
end

#realObject (readonly)

realname of a user



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

def real
  @real
end

#sessionObject (readonly)

Returns the value of attribute session.



226
227
228
# File 'lib/butler/bot.rb', line 226

def session
  @session
end

#statusObject

users last known status (:unknown, :online, :out_of_sight, :offline)



46
47
48
# File 'lib/butler/irc/user.rb', line 46

def status
  @status
end

#userObject (readonly)

username of a user



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

def user
  @user
end

Instance Method Details

#<=>(other) ⇒ Object

Compares nicknames



248
249
250
# File 'lib/butler/irc/user.rb', line 248

def <=>(other)
	@compare <=> other.to_str.downcase
end

#==(other) ⇒ Object

same Butler::IRC::User? FIXME, might require stricter comparison for IRC::User objects



222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/butler/irc/user.rb', line 222

def ==(other)
	if other.kind_of?(User) then
		(
			(!(@nick && other.nick) || @compare == other.compare) &&
			(!(@user && other.user) || @user == other.user) &&
			(!(@host && other.host) || @host == other.host) &&
			(!(@real && other.real) || @real == other.real)
		)
	else
		@compare == other.to_str.downcase
	end
end

#===(other) ⇒ Object

Lowercase String-comparison of nicknames. accepts everything that responds to :to_str



243
244
245
# File 'lib/butler/irc/user.rb', line 243

def ===(other)
	@nick && @compare === other
end

#=~(regex) ⇒ Object

Matching nickname to a regular expression. Same as User#to_s =~ /regular expression/



237
238
239
# File 'lib/butler/irc/user.rb', line 237

def =~(regex)
	@nick =~ regex
end

#add_channel(channel, reason) ⇒ Object

add a channel to the user (should only be used by Butler::IRC::Parser)



170
171
172
173
174
175
176
# File 'lib/butler/irc/user.rb', line 170

def add_channel(channel, reason)
	@channels[channel.to_str.downcase] ||= 0
	if @status != :myself && [:out_of_sight, :unknown, :offline].include?(@status) && common_channels?(@users.myself) then
		self.status	= :online
	end
	self
end

#add_flags(channel, flags) ⇒ Object

Raises:

  • (ArgumentError)


186
187
188
189
190
# File 'lib/butler/irc/user.rb', line 186

def add_flags(channel, flags)
	channel = channel.to_str.downcase
	raise ArgumentError, "User #{self} is not listed in #{channel}" unless @channels.include?(channel)
	@channels[channel] |= flags
end

#authorized?(*args) ⇒ Boolean

Returns:

  • (Boolean)


228
229
230
# File 'lib/butler/bot.rb', line 228

def authorized?(*args)
	@access.authorized?(*args)
end

#away=(is_away) ⇒ Object

set away status (should only be used by Butler::IRC::Parser)



123
124
125
# File 'lib/butler/irc/user.rb', line 123

def away=(is_away) #:nodoc:
	@is_away = is_away
end

#away_messageObject

retrieve away message



152
153
154
# File 'lib/butler/irc/user.rb', line 152

def away_message
	@away_message
end

#away_message=(message) ⇒ Object

set away message (should only be used by Butler::IRC::Parser)



157
158
159
# File 'lib/butler/irc/user.rb', line 157

def away_message=(message) #:nodoc:
	@away_message	= message.freeze
end

#butler_initializeUser

Returns a new instance of User.

Returns:

  • (User)

    a new instance of User



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/butler/bot.rb', line 232

def initialize(users, channels, nick=nil, user=nil, host=nil, real=nil)
	@nick, @user, @host, @real = nil
	@users        = users
	@nick         = nick.to_str.strip_user_prefixes.freeze if nick
	@user         = user.freeze if user
	@host         = host.freeze if host
	@real         = real.freeze if real
	
	@compare      = @nick ? @nick.downcase : "\xff"
	@all_channels = channels
	@channels     = {} # downcased channelname => flags
	@status       = :unknown
	@is_away      = false
	@away_message = ""
end

#channel_namesObject



76
77
78
# File 'lib/butler/irc/user.rb', line 76

def channel_names
	@channels.keys
end

#channelsObject



72
73
74
# File 'lib/butler/irc/user.rb', line 72

def channels
	@all_channels.map_names(*@channels.keys)
end

#common_channels(with_other_user) ⇒ Object



161
162
163
# File 'lib/butler/irc/user.rb', line 161

def common_channels(with_other_user)
	@channels.keys & with_other_user.channel_names
end

#common_channels?(with_other_user) ⇒ Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/butler/irc/user.rb', line 165

def common_channels?(with_other_user)
	!common_channels(with_other_user).empty?
end

#delete_channel(channel, reason) ⇒ Object

remove a channel from the user (should only be used by Butler::IRC::Parser)



179
180
181
182
183
184
# File 'lib/butler/irc/user.rb', line 179

def delete_channel(channel, reason)
	@channels.delete(channel.to_str.downcase)
	if @channels.empty? && ![:out_of_sight, :unknown, :myself].include?(@status) then
		self.status	= :out_of_sight
	end
end

#delete_flags(channel, flags) ⇒ Object

Raises:

  • (ArgumentError)


192
193
194
195
196
# File 'lib/butler/irc/user.rb', line 192

def delete_flags(channel, flags)
	channel = channel.to_str.downcase
	raise ArgumentError, "User #{self} is not listed in #{channel}" unless @channels.include?(channel)
	@channels[channel] &= ~flags
end

#eachObject

Iterate over all users in this channel



68
69
70
# File 'lib/butler/irc/user.rb', line 68

def each
	@channels.each_value { |channel| yield @all_channels[channel] }
end

#force_update(user = nil, host = nil, real = nil) ⇒ Object

:nodoc: DO NOT USE THIS METHOD This method is intended to be used by IRC::Parser or IRC::Client in case the server alters parts about ‘myself’ examples: some ircd’s change the ‘user’ part (prefix it), some ircd’s allow hiding the host, …



97
98
99
100
101
102
# File 'lib/butler/irc/user.rb', line 97

def force_update(user=nil, host=nil, real=nil)
	@user = user.freeze if user
	@host = host.freeze if host
	@real = real.freeze if real
	self
end

#hostmaskObject

Return the users hostmask, uses wildcards for unknown parts FIXME enable wildcard enforcement



63
64
65
# File 'lib/butler/irc/hostmask.rb', line 63

def hostmask
	return Hostmask.new("#{@nick||'*'}!#{@user||'*'}@#{@host||'*'}")
end

#in_channel?(channel) ⇒ Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/butler/irc/user.rb', line 142

def in_channel?(channel)
	@channels.has_key?(channel.to_str.downcase)
end

#inspectObject



252
253
254
# File 'lib/butler/irc/user.rb', line 252

def inspect
	"#<%s:0x%x %s!%s@%s (%s)>" %  [self.class, object_id, @nick || "?", @user || "?", @host || "?", @real || "?"]
end

#is_away?Boolean

check if user is away

Returns:

  • (Boolean)


147
148
149
# File 'lib/butler/irc/user.rb', line 147

def is_away?
	@is_away
end

#killObject



204
205
206
207
208
# File 'lib/butler/irc/user.rb', line 204

def kill
	@channels.each { |channel, _| delete_channel(channel, :kill) }
	self.status	= :offline
	@users.delete(self, :kill)
end

#op?(in_channel) ⇒ Boolean

check if user has op (+o) in given channel (String or Butler::IRC::Channel)

Returns:

  • (Boolean)


128
129
130
# File 'lib/butler/irc/user.rb', line 128

def op?(in_channel)
	!(@channels[in_channel.to_str.downcase] & OP).zero?
end

#quitObject



198
199
200
201
202
# File 'lib/butler/irc/user.rb', line 198

def quit
	@channels.each { |channel, _| delete_channel(channel, :quit) }
	self.status	= :offline
	@users.delete(self, :quit)
end

#to_sObject

Returns the (frozen!) nickname of the user



211
212
213
# File 'lib/butler/irc/user.rb', line 211

def to_s
	@nick
end

#to_strObject

:nodoc: string representation of Butler::IRC::User (nickname)



216
217
218
# File 'lib/butler/irc/user.rb', line 216

def to_str
	@compare
end

#uop?(in_channel) ⇒ Boolean

check if user has uop (+u) in given channel (String or Butler::IRC::Channel)

Returns:

  • (Boolean)


138
139
140
# File 'lib/butler/irc/user.rb', line 138

def uop?(in_channel)
	!(@channels[in_channel.to_str.downcase] & UOP).zero?
end

#update(user = nil, host = nil, real = nil) ⇒ Object

update userdata (set user, host, real if they are nil)

Raises:

  • (ArgumentError)


81
82
83
84
85
86
87
88
89
# File 'lib/butler/irc/user.rb', line 81

def update(user=nil, host=nil, real=nil) #:nodoc:
	raise ArgumentError, "Non assignable new value for user" if @user and user and @user != user
	raise ArgumentError, "Non assignable new value for host" if @host and host and @host != host
	raise ArgumentError, "Non assignable new value for real" if @real and real and @real != real
	@user = user.freeze if user
	@host = host.freeze if host
	@real = real.freeze if real
	self
end

#voice?(in_channel) ⇒ Boolean

check if user has voice (+v) in given channel (String or Butler::IRC::Channel)

Returns:

  • (Boolean)


133
134
135
# File 'lib/butler/irc/user.rb', line 133

def voice?(in_channel)
	!(@channels[in_channel.to_str.downcase] & VOICE).zero?
end