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

Enumerable: channels (IRC::Channel instances) the user is known to be in

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.



194
195
196
# File 'lib/butler/bot.rb', line 194

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

#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



241
242
243
# File 'lib/butler/irc/user.rb', line 241

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

#==(other) ⇒ Object

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



215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/butler/irc/user.rb', line 215

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



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

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

#=~(regex) ⇒ Object

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



230
231
232
# File 'lib/butler/irc/user.rb', line 230

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

#add_channel(channel, reason) ⇒ Object

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



158
159
160
161
162
163
164
# File 'lib/butler/irc/user.rb', line 158

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

FIXME

Raises:

  • (ArgumentError)


176
177
178
179
180
# File 'lib/butler/irc/user.rb', line 176

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)


196
197
198
# File 'lib/butler/bot.rb', line 196

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

#away=(is_away) ⇒ Object

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



110
111
112
# File 'lib/butler/irc/user.rb', line 110

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

#away_messageObject

retrieve away message



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

def away_message
	@away_message
end

#away_message=(message) ⇒ Object

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



144
145
146
# File 'lib/butler/irc/user.rb', line 144

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



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/butler/bot.rb', line 200

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



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

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

#common_channels?(with_other_user) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#delete_channel(channel, reason) ⇒ Object

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



168
169
170
171
172
173
# File 'lib/butler/irc/user.rb', line 168

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

FIXME

Raises:

  • (ArgumentError)


183
184
185
186
187
# File 'lib/butler/irc/user.rb', line 183

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

#hostmaskObject



48
49
50
# File 'lib/butler/irc/hostmask.rb', line 48

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

#in_channel?(channel) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#inspectObject



245
246
247
# File 'lib/butler/irc/user.rb', line 245

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)


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

def is_away?
	@is_away
end

#killObject

FIXME



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

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)


115
116
117
# File 'lib/butler/irc/user.rb', line 115

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

#quitObject

FIXME



190
191
192
193
194
# File 'lib/butler/irc/user.rb', line 190

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



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

def to_s
	@nick
end

#to_strObject

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



209
210
211
# File 'lib/butler/irc/user.rb', line 209

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)


125
126
127
# File 'lib/butler/irc/user.rb', line 125

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)


120
121
122
# File 'lib/butler/irc/user.rb', line 120

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