Class: Cinch::User
- Includes:
- Syncable
- Defined in:
- lib/cinch/user.rb
Overview
Direct Known Subclasses
Instance Attribute Summary collapse
-
#authname ⇒ String
readonly
The current value of authname.
-
#away ⇒ String?
readonly
The user’s away message, or ‘nil` if not away.
-
#channels ⇒ Array<Channel>
readonly
All channels the user is in.
- #data ⇒ Hash readonly
-
#host ⇒ String
readonly
The current value of host.
-
#idle ⇒ Integer
readonly
How long this user has been idle, in seconds.
- #in_whois ⇒ Boolean private
- #last_nick ⇒ String readonly
-
#monitored ⇒ Boolean
(also: #monitored?)
True if the user is being monitored.
-
#nick ⇒ String
readonly
The user’s nickname.
-
#online ⇒ Object
(also: #online?)
unless #monitor is being used, this information cannot be ensured to be always correct.
- #oper ⇒ Object (also: #oper?) readonly
-
#realname ⇒ String
readonly
The current value of realname.
-
#secure ⇒ Boolean
(also: #secure?)
readonly
True if the user is using a secure connection, i.e.
-
#signed_on_at ⇒ Time
readonly
The current value of signed_on_at.
- #synced ⇒ Boolean (also: #synced?) readonly
-
#unknown ⇒ Boolean
(also: #unknown?)
readonly
True if the instance references an user who cannot be found on the server.
-
#user ⇒ String
readonly
The current value of user.
Attributes inherited from Target
Instance Method Summary collapse
- #attr(attribute, data = true, unsync = false) ⇒ Object
-
#authed? ⇒ Boolean
Checks if the user is identified.
- #authname_unsynced ⇒ Object
- #channels_unsynced ⇒ Object
-
#dcc_send(io, filename = File.basename(io.path)) ⇒ void
Send data via DCC SEND to a user.
- #end_of_whois(values) ⇒ void private
- #host_unsynced ⇒ Object
- #idle_unsynced ⇒ Object
-
#initialize(*args) ⇒ User
constructor
A new instance of User.
- #inspect ⇒ String
-
#mask(s = "%n!%u@%h") ⇒ Mask
Generates a mask for the user.
-
#match(other) ⇒ Boolean
(also: #=~)
Check if the user matches a mask.
-
#monitor ⇒ void
Starts monitoring a user’s online state by either using MONITOR or periodically running WHOIS.
- #online_unsynced ⇒ Object (also: #online?_unsynced)
- #oper_unsynced ⇒ Object (also: #oper?_unsynced)
- #realname_unsynced ⇒ Object
-
#refresh ⇒ void
(also: #whois)
Queries the IRC server for information on the user.
- #secure_unsynced ⇒ Object (also: #secure?_unsynced)
- #signed_on_at_unsynced ⇒ Object
- #to_s ⇒ String
- #unknown_unsynced ⇒ Object (also: #unknown?_unsynced)
-
#unmonitor ⇒ void
Stops monitoring a user’s online state.
- #unsync_all ⇒ void private
-
#update_nick(new_nick) ⇒ void
private
Used to update the user’s nick on nickchange events.
- #user_unsynced ⇒ Object
Methods included from Syncable
#attribute_synced?, #mark_as_synced, #sync, #unsync, #wait_until_synced
Methods inherited from Target
#<=>, #action, #concretize, #ctcp, #eql?, #notice, #safe_action, #safe_notice, #safe_privmsg, #safe_send, #send
Constructor Details
#initialize(*args) ⇒ User
Generally, you shouldn’t initialize new instances of this class. Use Cinch::UserList#find_ensured instead.
Returns a new instance of User.
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/cinch/user.rb', line 192 def initialize(*args) @data = { user: nil, host: nil, realname: nil, authname: nil, idle: 0, signed_on_at: nil, unknown?: false, online?: false, channels: [], secure?: false, away: nil, oper?: false } case args.size when 2 @name, @bot = args when 4 @data[:user], @name, @data[:host], @bot = args else raise ArgumentError end @synced_attributes = Set.new @when_requesting_synced_attribute = lambda { |attr| unless attribute_synced?(attr) @data[:unknown?] = false unsync :unknown? refresh end } @monitored = false end |
Instance Attribute Details
#authname ⇒ String (readonly)
Returns the current value of authname.
28 29 30 |
# File 'lib/cinch/user.rb', line 28 def authname @authname end |
#away ⇒ String? (readonly)
The user’s away message, or ‘nil` if not away.
28 29 30 |
# File 'lib/cinch/user.rb', line 28 def away @away end |
#channels ⇒ Array<Channel> (readonly)
All channels the user is in.
28 29 30 |
# File 'lib/cinch/user.rb', line 28 def channels @channels end |
#data ⇒ Hash (readonly)
177 178 179 |
# File 'lib/cinch/user.rb', line 177 def data @data end |
#host ⇒ String (readonly)
Returns the current value of host.
28 29 30 |
# File 'lib/cinch/user.rb', line 28 def host @host end |
#idle ⇒ Integer (readonly)
How long this user has been idle, in seconds. This is a snapshot of the last WHOIS.
28 29 30 |
# File 'lib/cinch/user.rb', line 28 def idle @idle end |
#in_whois ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
46 47 48 |
# File 'lib/cinch/user.rb', line 46 def in_whois @in_whois end |
#last_nick ⇒ String (readonly)
37 38 39 |
# File 'lib/cinch/user.rb', line 37 def last_nick @last_nick end |
#monitored ⇒ Boolean Also known as: monitored?
The attribute writer is in fact part of the private API
Returns True if the user is being monitored.
183 184 185 |
# File 'lib/cinch/user.rb', line 183 def monitored @monitored end |
#nick ⇒ String (readonly)
The user’s nickname
28 29 30 |
# File 'lib/cinch/user.rb', line 28 def nick @nick end |
#online ⇒ Object Also known as: online?
This attribute will be updated by various events, but
unless #monitor is being used, this information cannot be ensured to be always correct.
28 29 30 |
# File 'lib/cinch/user.rb', line 28 def online @online end |
#oper ⇒ Object (readonly) Also known as: oper?
28 29 30 |
# File 'lib/cinch/user.rb', line 28 def oper @oper end |
#realname ⇒ String (readonly)
Returns the current value of realname.
28 29 30 |
# File 'lib/cinch/user.rb', line 28 def realname @realname end |
#secure ⇒ Boolean (readonly) Also known as: secure?
True if the user is using a secure connection, i.e. SSL.
28 29 30 |
# File 'lib/cinch/user.rb', line 28 def secure @secure end |
#signed_on_at ⇒ Time (readonly)
Returns the current value of signed_on_at.
28 29 30 |
# File 'lib/cinch/user.rb', line 28 def signed_on_at @signed_on_at end |
#synced ⇒ Boolean (readonly) Also known as: synced?
40 41 42 |
# File 'lib/cinch/user.rb', line 40 def synced @synced end |
#unknown ⇒ Boolean (readonly) Also known as: unknown?
True if the instance references an user who cannot be found on the server.
28 29 30 |
# File 'lib/cinch/user.rb', line 28 def unknown @unknown end |
#user ⇒ String (readonly)
Returns the current value of user.
28 29 30 |
# File 'lib/cinch/user.rb', line 28 def user @user end |
Instance Method Details
#attr(attribute, data = true, unsync = false) ⇒ Object
240 241 242 |
# File 'lib/cinch/user.rb', line 240 def attr(attribute, data = true, unsync = false) super end |
#authed? ⇒ Boolean
Checks if the user is identified. Currently officially supports Quakenet and Freenode.
235 236 237 |
# File 'lib/cinch/user.rb', line 235 def authed? !attr(:authname).nil? end |
#authname_unsynced ⇒ Object
120 121 122 |
# File 'lib/cinch/user.rb', line 120 def authname_unsynced attr(:authname, true, true) end |
#channels_unsynced ⇒ Object
147 148 149 |
# File 'lib/cinch/user.rb', line 147 def channels_unsynced attr(:channels, true, true) end |
#dcc_send(io, filename = File.basename(io.path)) ⇒ void
This method blocks.
This method returns an undefined value.
Send data via DCC SEND to a user.
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 |
# File 'lib/cinch/user.rb', line 425 def dcc_send(io, filename = File.basename(io.path)) own_ip = bot.config.dcc.own_ip || @bot.irc.socket.addr[2] dcc = DCC::Outgoing::Send.new(receiver: self, filename: filename, io: io, own_ip: own_ip) dcc.start_server handler = Handler.new(@bot, :message, Pattern.new(/^/, /\001DCC RESUME #{filename} #{dcc.port} (\d+)\001/, /$/)) do |m, position| next unless m.user == self dcc.seek(position.to_i) m.user.send "\001DCC ACCEPT #{filename} #{dcc.port} #{position}\001" handler.unregister end @bot.handlers.register(handler) @bot.loggers.info "DCC: Outgoing DCC SEND: File name: %s - Size: %dB - IP: %s - Port: %d - Status: waiting" % [filename, io.size, own_ip, dcc.port] dcc.send_handshake begin dcc.listen @bot.loggers.info "DCC: Outgoing DCC SEND: File name: %s - Size: %dB - IP: %s - Port: %d - Status: done" % [filename, io.size, own_ip, dcc.port] rescue Timeout::Error @bot.loggers.info "DCC: Outgoing DCC SEND: File name: %s - Size: %dB - IP: %s - Port: %d - Status: failed (timeout)" % [filename, io.size, own_ip, dcc.port] ensure handler.unregister end end |
#end_of_whois(values) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/cinch/user.rb', line 278 def end_of_whois(values) @in_whois = false if values.nil? # for some reason, we did not receive user information. one # reason is freenode throttling WHOIS Thread.new do sleep 2 refresh end return end if values[:unknown?] sync(:unknown?, true, true) self.online = false sync(:idle, 0, true) sync(:channels, [], true) fields = @data.keys fields.delete(:unknown?) fields.delete(:idle) fields.delete(:channels) fields.each do |field| sync(field, nil, true) end return end if values[:registered] values[:authname] ||= nick values.delete(:registered) end { authname: nil, idle: 0, secure?: false, oper?: false, away: nil, channels: [] }.merge(values).each do |attr, value| sync(attr, value, true) end sync(:unknown?, false, true) self.online = true end |
#host_unsynced ⇒ Object
110 111 112 |
# File 'lib/cinch/user.rb', line 110 def host_unsynced attr(:host, true, true) end |
#idle_unsynced ⇒ Object
125 126 127 |
# File 'lib/cinch/user.rb', line 125 def idle_unsynced attr(:idle, true, true) end |
#inspect ⇒ String
340 341 342 |
# File 'lib/cinch/user.rb', line 340 def inspect "#<User nick=#{@name.inspect}>" end |
#mask(s = "%n!%u@%h") ⇒ Mask
Generates a mask for the user.
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/cinch/user.rb', line 355 def mask(s = "%n!%u@%h") s = s.gsub(/%(.)/) { case $1 when "n" @name when "u" user when "h" host when "r" realname when "a" authname end } Mask.new(s) end |
#match(other) ⇒ Boolean Also known as: =~
Check if the user matches a mask.
378 379 380 |
# File 'lib/cinch/user.rb', line 378 def match(other) Mask.from(other) =~ Mask.from(self) end |
#monitor ⇒ void
This method returns an undefined value.
Starts monitoring a user’s online state by either using MONITOR or periodically running WHOIS.
389 390 391 392 393 394 395 396 397 398 399 400 401 |
# File 'lib/cinch/user.rb', line 389 def monitor if @bot.irc.isupport["MONITOR"] > 0 @bot.irc.send "MONITOR + #{@name}" else refresh @monitored_timer = Timer.new(@bot, interval: 30) { refresh } @monitored_timer.start end @monitored = true end |
#online_unsynced ⇒ Object Also known as: online?_unsynced
141 142 143 |
# File 'lib/cinch/user.rb', line 141 def online_unsynced attr(:online?, true, true) end |
#oper_unsynced ⇒ Object Also known as: oper?_unsynced
159 160 161 |
# File 'lib/cinch/user.rb', line 159 def oper_unsynced attr(:oper?, true, true) end |
#realname_unsynced ⇒ Object
115 116 117 |
# File 'lib/cinch/user.rb', line 115 def realname_unsynced attr(:realname, true, true) end |
#refresh ⇒ void Also known as: whois
The alias ‘whois` is deprecated and will be removed in a future version.
This method returns an undefined value.
Queries the IRC server for information on the user. This will set the User’s state to not synced. After all information are received, the object will be set back to synced.
251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/cinch/user.rb', line 251 def refresh return if @in_whois @data.keys.each do |attr| unsync attr end @in_whois = true if @bot.irc.network.whois_only_one_argument? @bot.irc.send "WHOIS #{@name}" else @bot.irc.send "WHOIS #{@name} #{@name}" end end |
#secure_unsynced ⇒ Object Also known as: secure?_unsynced
152 153 154 |
# File 'lib/cinch/user.rb', line 152 def secure_unsynced attr(:secure?, true, true) end |
#signed_on_at_unsynced ⇒ Object
130 131 132 |
# File 'lib/cinch/user.rb', line 130 def signed_on_at_unsynced attr(:signed_on_at, true, true) end |
#unknown_unsynced ⇒ Object Also known as: unknown?_unsynced
135 136 137 |
# File 'lib/cinch/user.rb', line 135 def unknown_unsynced attr(:unknown?, true, true) end |
#unmonitor ⇒ void
This method returns an undefined value.
Stops monitoring a user’s online state.
408 409 410 411 412 413 414 415 416 |
# File 'lib/cinch/user.rb', line 408 def unmonitor if @bot.irc.isupport["MONITOR"] > 0 @bot.irc.send "MONITOR - #{@name}" else @monitored_timer&.stop end @monitored = false end |
#unsync_all ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
330 331 332 |
# File 'lib/cinch/user.rb', line 330 def unsync_all super end |
#update_nick(new_nick) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Used to update the user’s nick on nickchange events.
480 481 482 483 484 485 486 487 |
# File 'lib/cinch/user.rb', line 480 def update_nick(new_nick) @last_nick, @name = @name, new_nick # Unsync authname because some networks tie authentication to # the nick, so the user might not be authenticated anymore after # changing their nick unsync(:authname) @bot.user_list.update_nick(self) end |
#user_unsynced ⇒ Object
105 106 107 |
# File 'lib/cinch/user.rb', line 105 def user_unsynced attr(:user, true, true) end |