Class: Butler::IRC::User
- Inherits:
-
Object
- Object
- Butler::IRC::User
- Includes:
- Flags, Comparable, Enumerable
- Defined in:
- lib/butler/irc/user.rb,
lib/butler/irc/hostmask.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
-
#compare ⇒ Object
readonly
lowercase nickname.
-
#host ⇒ Object
readonly
hostpart of a user.
-
#nick ⇒ Object
nickname of a user.
-
#real ⇒ Object
readonly
realname of a user.
-
#status ⇒ Object
users last known status (:unknown, :online, :out_of_sight, :offline).
-
#user ⇒ Object
readonly
username of a user.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compares nicknames.
-
#==(other) ⇒ Object
same Butler::IRC::User? FIXME, might require stricter comparison for IRC::User objects.
-
#===(other) ⇒ Object
Lowercase String-comparison of nicknames.
-
#=~(regex) ⇒ Object
Matching nickname to a regular expression.
-
#add_channel(channel, reason) ⇒ Object
add a channel to the user (should only be used by Butler::IRC::Parser).
- #add_flags(channel, flags) ⇒ Object
-
#away=(is_away) ⇒ Object
set away status (should only be used by Butler::IRC::Parser).
-
#away_message ⇒ Object
retrieve away message.
-
#away_message=(message) ⇒ Object
set away message (should only be used by Butler::IRC::Parser).
- #channel_names ⇒ Object
- #channels ⇒ Object
- #common_channels(with_other_user) ⇒ Object
- #common_channels?(with_other_user) ⇒ Boolean
-
#delete_channel(channel, reason) ⇒ Object
remove a channel from the user (should only be used by Butler::IRC::Parser).
- #delete_flags(channel, flags) ⇒ Object
-
#each ⇒ Object
Iterate over all users in this channel.
-
#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, …
-
#hostmask ⇒ Object
Return the users hostmask, uses wildcards for unknown parts FIXME enable wildcard enforcement.
- #in_channel?(channel) ⇒ Boolean
-
#initialize(users, channels, nick = nil, user = nil, host = nil, real = nil) ⇒ User
constructor
A new instance of User.
- #inspect ⇒ Object
-
#is_away? ⇒ Boolean
check if user is away.
- #kill ⇒ Object
-
#op?(in_channel) ⇒ Boolean
check if user has op (+o) in given channel (String or Butler::IRC::Channel).
- #quit ⇒ Object
-
#to_s ⇒ Object
Returns the (frozen!) nickname of the user.
-
#to_str ⇒ Object
:nodoc: string representation of Butler::IRC::User (nickname).
-
#uop?(in_channel) ⇒ Boolean
check if user has uop (+u) in given channel (String or Butler::IRC::Channel).
-
#update(user = nil, host = nil, real = nil) ⇒ Object
update userdata (set user, host, real if they are nil).
-
#voice?(in_channel) ⇒ Boolean
check if user has voice (+v) in given channel (String or Butler::IRC::Channel).
Methods included from Enumerable
Constructor Details
#initialize(users, channels, nick = nil, user = nil, host = nil, real = nil) ⇒ 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
#compare ⇒ Object (readonly)
lowercase nickname
49 50 51 |
# File 'lib/butler/irc/user.rb', line 49 def compare @compare end |
#host ⇒ Object (readonly)
hostpart of a user
40 41 42 |
# File 'lib/butler/irc/user.rb', line 40 def host @host end |
#nick ⇒ Object
nickname of a user
34 35 36 |
# File 'lib/butler/irc/user.rb', line 34 def nick @nick end |
#real ⇒ Object (readonly)
realname of a user
43 44 45 |
# File 'lib/butler/irc/user.rb', line 43 def real @real end |
#status ⇒ Object
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 |
#user ⇒ Object (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
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 |
#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_message ⇒ Object
retrieve away message
152 153 154 |
# File 'lib/butler/irc/user.rb', line 152 def @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 () #:nodoc: @away_message = .freeze end |
#channel_names ⇒ Object
76 77 78 |
# File 'lib/butler/irc/user.rb', line 76 def channel_names @channels.keys end |
#channels ⇒ Object
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
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
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 |
#each ⇒ Object
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 |
#hostmask ⇒ Object
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
142 143 144 |
# File 'lib/butler/irc/user.rb', line 142 def in_channel?(channel) @channels.has_key?(channel.to_str.downcase) end |
#inspect ⇒ Object
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
147 148 149 |
# File 'lib/butler/irc/user.rb', line 147 def is_away? @is_away end |
#kill ⇒ Object
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)
128 129 130 |
# File 'lib/butler/irc/user.rb', line 128 def op?(in_channel) !(@channels[in_channel.to_str.downcase] & OP).zero? end |
#quit ⇒ Object
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_s ⇒ Object
Returns the (frozen!) nickname of the user
211 212 213 |
# File 'lib/butler/irc/user.rb', line 211 def to_s @nick end |
#to_str ⇒ Object
: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)
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)
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)
133 134 135 |
# File 'lib/butler/irc/user.rb', line 133 def voice?(in_channel) !(@channels[in_channel.to_str.downcase] & VOICE).zero? end |