Class: Cinch::User

Inherits:
Target show all
Includes:
Syncable
Defined in:
lib/cinch/user.rb

Overview

Version:

Direct Known Subclasses

Bot

Instance Attribute Summary (collapse)

Attributes inherited from Target

#bot, #name

Instance Method Summary (collapse)

Methods included from Syncable

#mark_as_synced, #sync, #synced?, #unsync, #wait_until_synced

Methods inherited from Target

#<=>, #action, #concretize, #ctcp, #eql?, #msg, #notice, #safe_action, #safe_msg, #safe_notice

Constructor Details

- (User) initialize(*args)

A new instance of User



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/cinch/user.rb', line 91

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,
  }
  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 synced?(attr)
      @data[:unknown?] = false
      unsync :unknown?

      whois
    end
  }

  @monitored = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(m, *args)

Provides synced access to user attributes.



372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/cinch/user.rb', line 372

def method_missing(m, *args)
  if m.to_s =~ /^(.+)_unsynced$/
    m = $1.to_sym
    unsync = true
  end

  if @data.has_key?(m)
    attr(m, true, unsync)
  else
    super
  end
end

Instance Attribute Details

- (String) authname (readonly)

The current value of authname

Returns:

  • (String)

    the current value of authname



18
19
20
# File 'lib/cinch/user.rb', line 18

def authname
  @authname
end

- (String?) away (readonly)

The user’s away message, or nil if not away.

Returns:

  • (String, nil)

    the current value of away



18
19
20
# File 'lib/cinch/user.rb', line 18

def away
  @away
end

- (Array<Channel>) channels (readonly)

All channels the user is in.

Returns:

  • (Array<Channel>)

    the current value of channels



18
19
20
# File 'lib/cinch/user.rb', line 18

def channels
  @channels
end

- (Hash) data (readonly)

By default, you can use methods like User#user, User#host and alike – If you however fear that another thread might change data while you’re using it and if this means a critical issue to your code, you can store a clone of the result of this method and work with that instead.

Examples:

on :channel do |m|
  data = m.user.data.dup
  do_something_with(data.user)
  do_something_with(data.host)
end

Returns:

  • (Hash)


80
81
82
# File 'lib/cinch/user.rb', line 80

def data
  @data
end

- (String) host (readonly)

The current value of host

Returns:

  • (String)

    the current value of host



18
19
20
# File 'lib/cinch/user.rb', line 18

def host
  @host
end

- (Integer) idle (readonly)

How long this user has been idle, in seconds. This is a snapshot of the last WHOIS.

Returns:

  • (Integer)

    the current value of idle



18
19
20
# File 'lib/cinch/user.rb', line 18

def idle
  @idle
end

- (Boolean) in_whois

Returns:

  • (Boolean)


31
32
33
# File 'lib/cinch/user.rb', line 31

def in_whois
  @in_whois
end

- (String) last_nick (readonly)

Returns:

Since:

  • 1.1.0



25
26
27
# File 'lib/cinch/user.rb', line 25

def last_nick
  @last_nick
end

- (Boolean) monitored

True if the user is being monitored

Returns:

  • (Boolean)

    True if the user is being monitored

See Also:



85
86
87
# File 'lib/cinch/user.rb', line 85

def monitored
  @monitored
end

- (Boolean) online

Note:

This attribute will be updated by various events, but

unless #monitor is being used, this information cannot be ensured to be always correct.

Returns:

  • (Boolean)

    True if the user is online.



50
51
52
# File 'lib/cinch/user.rb', line 50

def online
  @online
end

- (String) realname (readonly)

The current value of realname

Returns:

  • (String)

    the current value of realname



18
19
20
# File 'lib/cinch/user.rb', line 18

def realname
  @realname
end

- (Boolean) secure (readonly)

True if the user is using a secure connection, i.e. SSL.

Returns:

  • (Boolean)

    True if the user is using a secure connection, i.e. SSL.



59
60
61
# File 'lib/cinch/user.rb', line 59

def secure
  @secure
end

- (Time) signed_on_at (readonly)

The current value of signed_on_at

Returns:

  • (Time)

    the current value of signed_on_at



18
19
20
# File 'lib/cinch/user.rb', line 18

def signed_on_at
  @signed_on_at
end

- (Boolean) synced (readonly)

Returns:

  • (Boolean)


28
29
30
# File 'lib/cinch/user.rb', line 28

def synced
  @synced
end

- (Boolean) unknown (readonly)

True if the instance references an user who cannot be found on the server.

Returns:

  • (Boolean)

    True if the instance references an user who cannot be found on the server.



38
39
40
# File 'lib/cinch/user.rb', line 38

def unknown
  @unknown
end

- (String) user (readonly)

The current value of user

Returns:

  • (String)

    the current value of user



18
19
20
# File 'lib/cinch/user.rb', line 18

def user
  @user
end

Instance Method Details

- (Object) attr(attribute, data = true, unsync = false)

See Also:



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

def attr(attribute, data = true, unsync = false)
  super
end

- (Boolean) authed?

Checks if the user is identified. Currently officially supports Quakenet and Freenode.

Returns:

  • (Boolean)

    true if the user is identified

Version:

  • 1.1.0



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

def authed?
  !attr(:authname).nil?
end

- dcc_send(io, filename = File.basename(io.path))

Note:

This method blocks.

This method returns an undefined value.

Send data via DCC SEND to a user.

Parameters:

Since:

  • 2.0.0



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/cinch/user.rb', line 310

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, not_found = false)

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.

Parameters:

  • values (Hash, nil)

    A hash of values gathered from WHOIS, or nil if no data was returned

  • not_found (Boolean) (defaults to: false)

    Has to be true if WHOIS resulted in an unknown user

Since:

  • 1.0.1

API Description:

  • private



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/cinch/user.rb', line 169

def end_of_whois(values, not_found = false)
  @in_whois = false
  if not_found
    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.nil?
    # for some reason, we did not receive user information. one
    # reason is freenode throttling WHOIS
    Thread.new do
      sleep 2
      whois
    end
    return
  end

  {
    :authname => nil,
    :idle => 0,
    :secure? => false,
    :channels => [],
  }.merge(values).each do |attr, value|
    sync(attr, value, true)
  end

  sync(:unknown?, false, true)
  self.online = true
end

- (String) inspect

Returns:



225
226
227
# File 'lib/cinch/user.rb', line 225

def inspect
  "#<User nick=#{@name.inspect}>"
end

- (Mask) mask(s = "%n!%u@%h")

Generates a mask for the user.

Parameters:

  • s (String) (defaults to: "%n!%u@%h")

    a pattern for generating the mask.

    • %n = nickname
    • %u = username
    • %h = host
    • %r = realname
    • %a = authname

Returns:



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/cinch/user.rb', line 240

def mask(s = "%n!%u@%h")
  s = s.gsub(/%(.)/) {
    case $1
    when "n"
      @name
    when "u"
      self.user
    when "h"
      self.host
    when "r"
      self.realname
    when "a"
      self.authname
    end
  }

  Mask.new(s)
end

- (Boolean) match(other) Also known as: =~

Check if the user matches a mask.

Parameters:

Returns:

  • (Boolean)


263
264
265
# File 'lib/cinch/user.rb', line 263

def match(other)
  Mask.from(other) =~ Mask.from(self)
end

- monitor

This method returns an undefined value.

Starts monitoring a user’s online state by either using MONITOR or periodically running WHOIS.

See Also:

Since:

  • 2.0.0



274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/cinch/user.rb', line 274

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

- (Boolean) online?

Note:

This attribute will be updated by various events, but

unless #monitor is being used, this information cannot be ensured to be always correct.

Returns:

  • (Boolean)

    True if the user is online.



51
52
53
# File 'lib/cinch/user.rb', line 51

def online
  @online
end

- (Boolean) respond_to?(m)

Returns:

  • (Boolean)

Since:

  • 1.1.2



386
387
388
389
390
391
392
# File 'lib/cinch/user.rb', line 386

def respond_to?(m)
  if m.to_s =~ /^(.+)_unsynced$/
    m = $1.to_sym
  end

  return @data.has_key?(m) || super
end

- (Boolean) secure?

True if the user is using a secure connection, i.e. SSL.

Returns:

  • (Boolean)

    True if the user is using a secure connection, i.e. SSL.



60
61
62
# File 'lib/cinch/user.rb', line 60

def secure
  @secure
end

- (String) to_s

Returns:



220
221
222
# File 'lib/cinch/user.rb', line 220

def to_s
  @name
end

- (Boolean) unknown?

True if the instance references an user who cannot be found on the server.

Returns:

  • (Boolean)

    True if the instance references an user who cannot be found on the server.



39
40
41
# File 'lib/cinch/user.rb', line 39

def unknown
  @unknown
end

- unmonitor

This method returns an undefined value.

Stops monitoring a user’s online state.

See Also:

Since:

  • 2.0.0



293
294
295
296
297
298
299
300
301
# File 'lib/cinch/user.rb', line 293

def unmonitor
  if @bot.irc.isupport["MONITOR"] > 0
    @bot.irc.send "MONITOR - #@name"
  else
    @monitored_timer.stop if @monitored_timer
  end

  @monitored = false
end

- unsync_all

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.

See Also:

Since:

  • 1.0.1

API Description:

  • private



215
216
217
# File 'lib/cinch/user.rb', line 215

def unsync_all
  super
end

- update_nick(new_nick)

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.

Parameters:

  • new_nick (String)

    The user’s new nick

API Description:

  • private



366
367
368
369
# File 'lib/cinch/user.rb', line 366

def update_nick(new_nick)
  @last_nick, @name = @name, new_nick
  @bot.user_list.update_nick(self)
end

- whois Also known as: refresh

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.



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/cinch/user.rb', line 147

def whois
  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