This repository has been archived by the owner on Apr 13, 2019. It is now read-only.
/
user.rb
344 lines (307 loc) · 8.84 KB
/
user.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# -*- coding: utf-8 -*-
require "cinch/target"
module Cinch
# @attr_reader [String] user
# @attr_reader [String] host
# @attr_reader [String] realname
# @attr_reader [String] authname
# @attr_reader [Number] idle How long this user has been idle, in seconds.
# This is a snapshot of the last WHOIS.
# @attr_reader [Time] signed_on_at
# @attr_reader [Array<Channel>] channels All channels the user is in.
class User < Target
include Syncable
@users = {} # this will be removed with version 2.0.0
class << self
# @overload find_ensured(nick, bot)
# Finds or creates a user based on his nick.
#
# @param [String] nick The user's nickname
# @param [Bot] bot An instance of Bot
# @overload find_ensured(user, nick, host, bot)
# Finds or creates a user based on his nick but already
# setting user and host.
#
# @param [String] user The username
# @param [String] nick The nickname
# @param [String] host The user's hostname
# @param [Bot] bot An instance of bot
#
# @return [User]
# @deprecated See {Bot#user_manager} and {UserManager#find_ensured} instead
# @note This method does not work properly if running more than one bot
# @note This method will be removed in Cinch 2.0.0
def find_ensured(*args)
Cinch::Utilities::Deprecation.print_deprecation("1.1.0", "User.find_ensured")
case args.size
when 2
nick = args.first
bot = args.last
bargs = [nick]
when 4
nick = args[1]
bot = args.pop
bargs = args
else
raise ArgumentError
end
downcased_nick = nick.irc_downcase(bot.irc.isupport["CASEMAPPING"])
@users[downcased_nick] = args.last.user_manager.find_ensured(*args[0..-2])
# note: the complete case statement and the assignment to
# @users is only for keeping compatibility with older
# versions, which still use User.find and User.all.
end
# Finds a user.
#
# @param [String] nick nick of a user
# @return [User, nil]
# @deprecated See {Bot#user_manager} and {UserManager#find} instead
# @note This method does not work properly if running more than one bot
# @note This method will be removed in Cinch 2.0.0
def find(nick)
Cinch::Utilities::Deprecation.print_deprecation("1.1.0", "User.find")
@users[downcased_nick]
end
# @return [Array<User>] Returns all users
# @deprecated See {Bot#user_manager} and {CacheManager#each} instead
# @note This method does not work properly if running more than one bot
# @note This method will be removed in Cinch 2.0.0
def all
Cinch::Utilities::Deprecation.print_deprecation("1.1.0", "User.all")
@users.values
end
end
alias_method :nick, :name
# @return [String]
attr_reader :last_nick
# @return [Boolean]
attr_reader :synced
# @return [Boolean]
attr_reader :in_whois
# @api private
attr_writer :in_whois
# @return [Boolean] True if the instance references an user who
# cannot be found on the server.
attr_reader :unknown
alias_method :unknown?, :unknown
undef_method "unknown?"
undef_method "unknown"
def unknown
self.unknown?
end
# @return [Boolean] True if the user is using a secure connection, i.e. SSL.
attr_reader :secure
alias_method :secure?, :secure
undef_method "secure?"
undef_method "secure"
def secure
self.secure?
end
# 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.
#
# @example
# on :channel do |m|
# data = m.user.data.dup
# do_something_with(data.user)
# do_something_with(data.host)
# end
# @return [Hash]
attr_reader :data
def initialize(*args)
@data = {
:user => nil,
:host => nil,
:realname => nil,
:authname => nil,
:idle => 0,
:signed_on_at => nil,
:unknown? => false,
:channels => [],
:secure? => 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 @synced
@data[:unknown?] = false
unsync :unknown?
unsync attr
whois
end
}
end
# Checks if the user is identified. Currently officially supports
# Quakenet and Freenode.
#
# @return [Boolean] true if the user is identified
def authed?
!attr(:authname).nil?
end
# @see Syncable#attr
def attr(attribute, data = true, unsync = false)
super
end
# 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.
#
# @return [void]
def whois
return if @in_whois
@synced = false
@data.keys.each do |attr|
unsync attr
end
@in_whois = true
if @bot.irc.network == "jtv"
# the justin tv "IRC" network does not support WHOIS with two
# arguments
@bot.raw "WHOIS #@name"
else
@bot.raw "WHOIS #@name #@name"
end
end
alias_method :refresh, :whois
# @param [Hash, nil] values A hash of values gathered from WHOIS,
# or `nil` if no data was returned
# @param [Boolean] not_found Has to be true if WHOIS resulted in
# an unknown user
# @return [void]
# @api private
# @since 1.0.1
def end_of_whois(values, not_found = false)
@in_whois = false
if not_found
sync(:unknown?, true, true)
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,
}.merge(values).each do |attr, value|
sync(attr, value, true)
end
sync(:unknown?, false, true)
@synced = true
end
# @return [void]
# @since 1.0.1
# @api private
# @see Syncable#unsync_all
def unsync_all
@synced = false
super
end
# @return [String]
def to_s
@name
end
# @return [String]
def inspect
"#<User nick=#{@name.inspect}>"
end
# Generates a mask for the user.
#
# @param [String] s a pattern for generating the mask.
#
# - %n = nickname
# - %u = username
# - %h = host
# - %r = realname
# - %a = authname
#
# @return [Mask]
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
# Check if the user matches a mask.
#
# @param [Ban, Mask, User, String] other The user or mask to match against
# @return [Boolean]
def match(other)
Mask.from(other) =~ Mask.from(self)
end
alias_method :=~, :match
# @api private
def update_nick(new_nick)
@last_nick, @name = @name, new_nick
@bot.user_manager.update_nick(self)
end
# Provides synced access to user attributes.
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 = false)
else
super
end
end
def respond_to?(m)
if m.to_s =~ /^(.+)_unsynced$/
m = $1.to_sym
end
return @data.has_key?(m) || super
end
# @return [Boolean]
def ==(other)
return case other
when self.class
@name == other.nick
when String
self.to_s == other
when Bot
self.nick == other.config.nick
else
false
end
end
alias_method :eql?, "=="
end
end