cinch 2.2.0 → 2.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cinch/#channel.rb# +407 -0
- data/lib/cinch/formatting.rb +1 -1
- data/lib/cinch/utilities/deprecation.rb +1 -1
- data/lib/cinch/version.rb +1 -1
- metadata +3 -2
@@ -0,0 +1,407 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require "set"
|
3
|
+
require "cinch/target"
|
4
|
+
|
5
|
+
module Cinch
|
6
|
+
# @attr limit
|
7
|
+
# @attr secret
|
8
|
+
# @attr moderated
|
9
|
+
# @attr invite_only
|
10
|
+
# @attr key
|
11
|
+
#
|
12
|
+
# @version 2.0.0
|
13
|
+
class Channel < Target
|
14
|
+
include Syncable
|
15
|
+
include Helpers
|
16
|
+
|
17
|
+
def initialize(name, bot)
|
18
|
+
@bot = bot
|
19
|
+
@name = name
|
20
|
+
# TODO raise if not a channel
|
21
|
+
end
|
22
|
+
|
23
|
+
def users
|
24
|
+
sync_get(:users)
|
25
|
+
end
|
26
|
+
|
27
|
+
def topic
|
28
|
+
sync_get(:topic, lambda {@bot.irc.send "TOPIC #@name"})
|
29
|
+
end
|
30
|
+
|
31
|
+
def bans
|
32
|
+
sync_get(:bans, lambda {@bot.irc.send "MODE #@name +b"})
|
33
|
+
end
|
34
|
+
|
35
|
+
def owners
|
36
|
+
sync_get(:owners, lambda {
|
37
|
+
if !@bot.irc.network.owner_list_mode
|
38
|
+
sync_set(:owners, [])
|
39
|
+
return
|
40
|
+
end
|
41
|
+
@bot.irc.send "MODE #@name +#{@bot.irc.network.owner_list_mode}"
|
42
|
+
})
|
43
|
+
end
|
44
|
+
|
45
|
+
def modes
|
46
|
+
sync_get(:modes, lambda {@bot.irc.send "MODE #@name"})
|
47
|
+
end
|
48
|
+
|
49
|
+
# @group Checks
|
50
|
+
|
51
|
+
# @param [User, String] user An {User}-object or a nickname
|
52
|
+
# @return [Boolean] Check if a user is in the channel
|
53
|
+
# @since 1.1.0
|
54
|
+
# @version 1.1.2
|
55
|
+
def has_user?(user)
|
56
|
+
users.has_key?(User(user))
|
57
|
+
end
|
58
|
+
|
59
|
+
# @return [Boolean] true if `user` is opped in the channel
|
60
|
+
# @since 1.1.0
|
61
|
+
def opped?(user)
|
62
|
+
users[User(user)].include? "o"
|
63
|
+
end
|
64
|
+
|
65
|
+
# @return [Boolean] true if `user` is half-opped in the channel
|
66
|
+
# @since 1.1.0
|
67
|
+
def half_opped?(user)
|
68
|
+
users[User(user)].include? "h"
|
69
|
+
end
|
70
|
+
|
71
|
+
# @return [Boolean] true if `user` is voiced in the channel
|
72
|
+
# @since 1.1.0
|
73
|
+
def voiced?(user)
|
74
|
+
users[User(user)].include? "v"
|
75
|
+
end
|
76
|
+
|
77
|
+
# @endgroup
|
78
|
+
|
79
|
+
# @group User groups
|
80
|
+
# @return [Array<User>] All ops in the channel
|
81
|
+
# @since 2.0.0
|
82
|
+
def ops
|
83
|
+
users.select {|user, modes| modes.include?("o")}.keys
|
84
|
+
end
|
85
|
+
|
86
|
+
# @return [Array<User>] All half-ops in the channel
|
87
|
+
# @since 2.0.0
|
88
|
+
def half_ops
|
89
|
+
users.select {|user, modes| modes.include?("h")}.keys
|
90
|
+
end
|
91
|
+
|
92
|
+
# @return [Array<User>] All voiced users in the channel
|
93
|
+
# @since 2.0.0
|
94
|
+
def voiced
|
95
|
+
users.select {|user, modes| modes.include?("v")}.keys
|
96
|
+
end
|
97
|
+
|
98
|
+
# @return [Array<User>] All admins in the channel
|
99
|
+
# @since 2.0.0
|
100
|
+
def admins
|
101
|
+
users.select {|user, modes| modes.include?("a")}.keys
|
102
|
+
end
|
103
|
+
# @endgroup
|
104
|
+
|
105
|
+
# @return [Integer] The maximum number of allowed users in the
|
106
|
+
# channel. 0 if unlimited.
|
107
|
+
def limit
|
108
|
+
modes["l"].to_i
|
109
|
+
end
|
110
|
+
|
111
|
+
def limit=(val)
|
112
|
+
if val == -1 or val.nil?
|
113
|
+
mode "-l"
|
114
|
+
else
|
115
|
+
mode "+l #{val}"
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
# @return [Boolean] true if the channel is secret (+s)
|
120
|
+
def secret?
|
121
|
+
modes["s"]
|
122
|
+
end
|
123
|
+
alias_method :secret, :secret?
|
124
|
+
|
125
|
+
def secret=(bool)
|
126
|
+
if bool
|
127
|
+
mode "+s"
|
128
|
+
else
|
129
|
+
mode "-s"
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
# @return [Boolean] true if the channel is moderated
|
134
|
+
def moderated?
|
135
|
+
modes["m"]
|
136
|
+
end
|
137
|
+
alias_method :moderated, :moderated?
|
138
|
+
|
139
|
+
def moderated=(bool)
|
140
|
+
if bool
|
141
|
+
mode "+m"
|
142
|
+
else
|
143
|
+
mode "-m"
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
# @return [Boolean] true if the channel is invite only (+i)
|
148
|
+
def invite_only?
|
149
|
+
modes["i"]
|
150
|
+
end
|
151
|
+
alias_method :invite_only, :invite_only?
|
152
|
+
|
153
|
+
def invite_only=(bool)
|
154
|
+
if bool
|
155
|
+
mode "+i"
|
156
|
+
else
|
157
|
+
mode "-i"
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
# @return [String, nil] The channel's key (aka password)
|
162
|
+
def key
|
163
|
+
modes["k"]
|
164
|
+
end
|
165
|
+
|
166
|
+
def key=(new_key)
|
167
|
+
if new_key.nil?
|
168
|
+
mode "-k #{key}"
|
169
|
+
else
|
170
|
+
mode "+k #{new_key}"
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
# @api private
|
175
|
+
# @return [void]
|
176
|
+
def sync_modes
|
177
|
+
sync_await(:users)
|
178
|
+
sync_unset(:users)
|
179
|
+
|
180
|
+
if @bot.irc.isupport["WHOX"]
|
181
|
+
@bot.irc.send "WHO #@name %acfhnru"
|
182
|
+
else
|
183
|
+
@bot.irc.send "WHO #@name"
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
# @group Channel Manipulation
|
188
|
+
|
189
|
+
# Bans someone from the channel.
|
190
|
+
#
|
191
|
+
# @param [Mask, String, #mask] target the mask, or an object having a mask, to ban
|
192
|
+
# @return [Mask] the mask used for banning
|
193
|
+
# @see #unban #unban for unbanning users
|
194
|
+
def ban(target)
|
195
|
+
mask = Mask.from(target)
|
196
|
+
|
197
|
+
@bot.irc.send "MODE #@name +b #{mask}"
|
198
|
+
mask
|
199
|
+
end
|
200
|
+
|
201
|
+
# Unbans someone from the channel.
|
202
|
+
#
|
203
|
+
# @param [Mask, String, #mask] target the mask to unban
|
204
|
+
# @return [Mask] the mask used for unbanning
|
205
|
+
# @see #ban #ban for banning users
|
206
|
+
def unban(target)
|
207
|
+
mask = Mask.from(target)
|
208
|
+
|
209
|
+
@bot.irc.send "MODE #@name -b #{mask}"
|
210
|
+
mask
|
211
|
+
end
|
212
|
+
|
213
|
+
# Ops a user.
|
214
|
+
#
|
215
|
+
# @param [String, User] user the user to op
|
216
|
+
# @return [void]
|
217
|
+
def op(user)
|
218
|
+
@bot.irc.send "MODE #@name +o #{user}"
|
219
|
+
end
|
220
|
+
|
221
|
+
# Deops a user.
|
222
|
+
#
|
223
|
+
# @param [String, User] user the user to deop
|
224
|
+
# @return [void]
|
225
|
+
def deop(user)
|
226
|
+
@bot.irc.send "MODE #@name -o #{user}"
|
227
|
+
end
|
228
|
+
|
229
|
+
# Voices a user.
|
230
|
+
#
|
231
|
+
# @param [String, User] user the user to voice
|
232
|
+
# @return [void]
|
233
|
+
def voice(user)
|
234
|
+
@bot.irc.send "MODE #@name +v #{user}"
|
235
|
+
end
|
236
|
+
|
237
|
+
# Devoices a user.
|
238
|
+
#
|
239
|
+
# @param [String, User] user the user to devoice
|
240
|
+
# @return [void]
|
241
|
+
def devoice(user)
|
242
|
+
@bot.irc.send "MODE #@name -v #{user}"
|
243
|
+
end
|
244
|
+
|
245
|
+
# Invites a user to the channel.
|
246
|
+
#
|
247
|
+
# @param [String, User] user the user to invite
|
248
|
+
# @return [void]
|
249
|
+
def invite(user)
|
250
|
+
@bot.irc.send("INVITE #{user} #@name")
|
251
|
+
end
|
252
|
+
|
253
|
+
undef_method(:topic=)
|
254
|
+
# Sets the topic.
|
255
|
+
#
|
256
|
+
# @param [String] new_topic the new topic
|
257
|
+
# @raise [Exceptions::TopicTooLong] Raised if the bot is operating
|
258
|
+
# in {Bot#strict? strict mode} and when the new topic is too long.
|
259
|
+
def topic=(new_topic)
|
260
|
+
if new_topic.size > @bot.irc.isupport["TOPICLEN"] && @bot.strict?
|
261
|
+
raise Exceptions::TopicTooLong, new_topic
|
262
|
+
end
|
263
|
+
|
264
|
+
@bot.irc.send "TOPIC #@name :#{new_topic}"
|
265
|
+
end
|
266
|
+
|
267
|
+
# Kicks a user from the channel.
|
268
|
+
#
|
269
|
+
# @param [String, User] user the user to kick
|
270
|
+
# @param [String] reason a reason for the kick
|
271
|
+
# @raise [Exceptions::KickReasonTooLong]
|
272
|
+
# @return [void]
|
273
|
+
def kick(user, reason = nil)
|
274
|
+
if reason.to_s.size > @bot.irc.isupport["KICKLEN"] && @bot.strict?
|
275
|
+
raise Exceptions::KickReasonTooLong, reason
|
276
|
+
end
|
277
|
+
|
278
|
+
@bot.irc.send("KICK #@name #{user} :#{reason}")
|
279
|
+
end
|
280
|
+
|
281
|
+
# Removes a user from the channel.
|
282
|
+
#
|
283
|
+
# This uses the REMOVE command, which is a non-standardized
|
284
|
+
# extension. Unlike a kick, it makes a user part. This prevents
|
285
|
+
# auto-rejoin scripts from firing and might also be perceived as
|
286
|
+
# less aggressive by some. Not all IRC networks support this
|
287
|
+
# command.
|
288
|
+
#
|
289
|
+
# @param [User] user the user to remove
|
290
|
+
# @param [String] reason a reason for the removal
|
291
|
+
# @return [void]
|
292
|
+
def remove(user, reason = nil)
|
293
|
+
@bot.irc.send("REMOVE #@name #{user} :#{reason}")
|
294
|
+
end
|
295
|
+
|
296
|
+
# Sets or unsets modes. Most of the time you won't need this but
|
297
|
+
# use setter methods like {Channel#invite_only=}.
|
298
|
+
#
|
299
|
+
# @param [String] s a mode string
|
300
|
+
# @return [void]
|
301
|
+
# @example
|
302
|
+
# channel.mode "+n"
|
303
|
+
def mode(s)
|
304
|
+
@bot.irc.send "MODE #@name #{s}"
|
305
|
+
end
|
306
|
+
|
307
|
+
# Causes the bot to part from the channel.
|
308
|
+
#
|
309
|
+
# @param [String] message the part message.
|
310
|
+
# @return [void]
|
311
|
+
def part(message = nil)
|
312
|
+
@bot.irc.send "PART #@name :#{message}"
|
313
|
+
end
|
314
|
+
|
315
|
+
# Joins the channel
|
316
|
+
#
|
317
|
+
# @param [String] key the channel key, if any. If none is
|
318
|
+
# specified but @key is set, @key will be used
|
319
|
+
# @return [void]
|
320
|
+
def join(key = nil)
|
321
|
+
if key.nil? and self.key != true
|
322
|
+
key = self.key
|
323
|
+
end
|
324
|
+
@bot.irc.send "JOIN #{[@name, key].compact.join(" ")}"
|
325
|
+
end
|
326
|
+
|
327
|
+
# @endgroup
|
328
|
+
|
329
|
+
# @api private
|
330
|
+
# @return [User] The added user
|
331
|
+
def add_user(user, modes = [])
|
332
|
+
# XXX
|
333
|
+
@users[user] = modes
|
334
|
+
user
|
335
|
+
end
|
336
|
+
|
337
|
+
# @api private
|
338
|
+
# @return [User, nil] The removed user
|
339
|
+
def remove_user(user)
|
340
|
+
# XXX
|
341
|
+
@users.delete(user)
|
342
|
+
end
|
343
|
+
|
344
|
+
# Removes all users
|
345
|
+
#
|
346
|
+
# @api private
|
347
|
+
# @return [void]
|
348
|
+
def clear_users
|
349
|
+
# XXX
|
350
|
+
@users.clear
|
351
|
+
end
|
352
|
+
|
353
|
+
# @note The aliases `msg` and `privmsg` are deprecated and will be
|
354
|
+
# removed in a future version.
|
355
|
+
def send(text, notice = false)
|
356
|
+
# TODO deprecate 'notice' argument
|
357
|
+
text = text.to_s
|
358
|
+
if @modes["c"]
|
359
|
+
# Remove all formatting and colors if the channel doesn't
|
360
|
+
# allow colors.
|
361
|
+
text = Cinch::Formatting.unformat(text)
|
362
|
+
end
|
363
|
+
super(text, notice)
|
364
|
+
end
|
365
|
+
alias_method :msg, :send # deprecated
|
366
|
+
alias_method :privmsg, :send # deprecated
|
367
|
+
undef_method(:msg) # yardoc hack
|
368
|
+
undef_method(:privmsg) # yardoc hack
|
369
|
+
|
370
|
+
# @deprecated
|
371
|
+
def msg(*args)
|
372
|
+
Cinch::Utilities::Deprecation.print_deprecation("2.2.0", "Channel#msg", "Channel#send")
|
373
|
+
send(*args)
|
374
|
+
end
|
375
|
+
|
376
|
+
# @deprecated
|
377
|
+
def privmsg(*args)
|
378
|
+
Cinch::Utilities::Deprecation.print_deprecation("2.2.0", "Channel#privmsg", "Channel#send")
|
379
|
+
send(*args)
|
380
|
+
end
|
381
|
+
|
382
|
+
# @return [Fixnum]
|
383
|
+
def hash
|
384
|
+
@name.hash
|
385
|
+
end
|
386
|
+
|
387
|
+
# @return [String]
|
388
|
+
# @note The alias `to_str` is deprecated and will be removed in a
|
389
|
+
# future version. Channel objects should not be treated like
|
390
|
+
# strings.
|
391
|
+
def to_s
|
392
|
+
@name
|
393
|
+
end
|
394
|
+
alias_method :to_str, :to_s # deprecated
|
395
|
+
undef_method(:to_str) # yardoc hack
|
396
|
+
|
397
|
+
def to_str
|
398
|
+
Cinch::Utilities::Deprecation.print_deprecation("2.2.0", "Channel#to_str", "Channel#to_s")
|
399
|
+
to_s
|
400
|
+
end
|
401
|
+
|
402
|
+
# @return [String]
|
403
|
+
def inspect
|
404
|
+
"#<Channel name=#{@name.inspect}>"
|
405
|
+
end
|
406
|
+
end
|
407
|
+
end
|
data/lib/cinch/formatting.rb
CHANGED
@@ -6,7 +6,7 @@ module Cinch
|
|
6
6
|
def self.print_deprecation(version, method, instead = nil)
|
7
7
|
s = "Deprecation warning: Beginning with version #{version}, #{method} should not be used anymore."
|
8
8
|
if instead != nil
|
9
|
-
s << " Use
|
9
|
+
s << " Use #{instead} instead."
|
10
10
|
end
|
11
11
|
$stderr.puts s
|
12
12
|
$stderr.puts caller
|
data/lib/cinch/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cinch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.2.
|
4
|
+
version: 2.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2015-01-01 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A simple, friendly DSL for creating IRC bots
|
15
15
|
email:
|
@@ -44,6 +44,7 @@ files:
|
|
44
44
|
- lib/cinch/cached_list.rb
|
45
45
|
- lib/cinch/bot.rb
|
46
46
|
- lib/cinch/isupport.rb
|
47
|
+
- lib/cinch/#channel.rb#
|
47
48
|
- lib/cinch/helpers.rb
|
48
49
|
- lib/cinch/handler.rb
|
49
50
|
- lib/cinch/handler_list.rb
|