Class: Cinch::Channel

Inherits:
Object
  • Object
show all
Includes:
Syncable
Defined in:
lib/cinch/channel.rb

Instance Attribute Summary collapse

Checks collapse

Channel Manipulation collapse

Sending messages collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Syncable

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

Constructor Details

#initialize(name, bot) ⇒ Channel

Returns a new instance of Channel.



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
# File 'lib/cinch/channel.rb', line 74

def initialize(name, bot)
  @bot   = bot
  @name  = name
  @users = Hash.new {|h,k| h[k] = []}
  @bans  = []

  @modes = {}
  # TODO raise if not a channel

  @topic = nil

  @in_channel = false

  @synced_attributes  = Set.new
  @when_requesting_synced_attribute = lambda {|attr|
    unless @in_channel
      unsync(attr)
      case attr
      when :users
        @bot.raw "NAMES #@name"
      when :topic
        @bot.raw "TOPIC #@name"
      when :bans
        @bot.raw "MODE #@name +b"
      when :modes
        @bot.raw "MODE #@name"
      end
    end
  }
end

Instance Attribute Details

#bansArray<Ban> (readonly)

Returns all active bans.

Returns:

  • (Array<Ban>)

    all active bans



68
69
70
# File 'lib/cinch/channel.rb', line 68

def bans
  @bans
end

#botBot (readonly)

Returns:



54
55
56
# File 'lib/cinch/channel.rb', line 54

def bot
  @bot
end

#invite_onlyBoolean Also known as: invite_only?

Returns true if the channel is invite only (+i).

Returns:

  • (Boolean)

    true if the channel is invite only (+i)



188
189
190
# File 'lib/cinch/channel.rb', line 188

def invite_only
  @invite_only
end

#keyString?

Returns The channel’s key (aka password).

Returns:

  • (String, nil)

    The channel’s key (aka password)



205
206
207
# File 'lib/cinch/channel.rb', line 205

def key
  @key
end

#limitNumber

Returns The maximum number of allowed users in the channel. 0 if unlimited.

Returns:

  • (Number)

    The maximum number of allowed users in the channel. 0 if unlimited.



137
138
139
# File 'lib/cinch/channel.rb', line 137

def limit
  @limit
end

#moderatedBoolean Also known as: moderated?

Returns true if the channel is moderated (only users with o and v are able to send messages).

Returns:

  • (Boolean)

    true if the channel is moderated (only users with o and v are able to send messages)



171
172
173
# File 'lib/cinch/channel.rb', line 171

def moderated
  @moderated
end

#modesHash<String => Object> (readonly)

Returns:



72
73
74
# File 'lib/cinch/channel.rb', line 72

def modes
  @modes
end

#nameString (readonly)

Returns the channel’s name.

Returns:

  • (String)

    the channel’s name



57
58
59
# File 'lib/cinch/channel.rb', line 57

def name
  @name
end

#secretBoolean Also known as: secret?

Returns true if the channel is secret (+s).

Returns:

  • (Boolean)

    true if the channel is secret (+s)



153
154
155
# File 'lib/cinch/channel.rb', line 153

def secret
  @secret
end

#topicString

Returns the channel’s topic.

Returns:

  • (String)

    the channel’s topic



64
65
66
# File 'lib/cinch/channel.rb', line 64

def topic
  @topic
end

#usersArray<User> (readonly)

Returns all users in the channel.

Returns:

  • (Array<User>)

    all users in the channel



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

def users
  @users
end

Class Method Details

.allArray<Channel>

Deprecated.
Note:

This method does not work properly if running more than one bot

Note:

This method will be removed in Cinch 2.0.0

Returns all channels

Returns:

  • (Array<Channel>)

    Returns all channels



45
46
47
48
49
50
# File 'lib/cinch/channel.rb', line 45

def all
  $stderr.puts "Deprecation warning: Beginning with version 1.1.0, User.all should not be used anymore."
  puts caller

  @channels.values
end

.find(name) ⇒ Channel?

Deprecated.
Note:

This method does not work properly if running more than one bot

Note:

This method will be removed in Cinch 2.0.0

Finds a channel.

Parameters:

  • name (String)

    name of a channel

Returns:



34
35
36
37
38
39
# File 'lib/cinch/channel.rb', line 34

def find(name)
  $stderr.puts "Deprecation warning: Beginning with version 1.1.0, Channel.find should not be used anymore."
  puts caller

  @channels[name]
end

.find_ensured(name, bot) ⇒ Channel

Note:

This method does not work properly if running more than one bot

Note:

This method will be removed in Cinch 2.0.0

Finds or creates a channel.

Parameters:

  • name (String)

    name of a channel

  • bot (Bot)

    a bot

Returns:

See Also:



19
20
21
22
23
24
25
# File 'lib/cinch/channel.rb', line 19

def find_ensured(name, bot)
  $stderr.puts "Deprecation warning: Beginning with version 1.1.0, Channel.find_ensured should not be used anymore."
  puts caller

  downcased_name = name.irc_downcase(bot.irc.isupport["CASEMAPPING"])
  @channels[downcased_name] ||= bot.channel_manager.find_ensured(name)
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Returns:

  • (Boolean)


453
454
455
# File 'lib/cinch/channel.rb', line 453

def ==(other)
  @name == other.to_s
end

#action(message) ⇒ void

This method returns an undefined value.

Invoke an action (/me) in the channel.

Parameters:

  • message (String)

    the message

See Also:



431
432
433
# File 'lib/cinch/channel.rb', line 431

def action(message)
  @bot.action(@name, message)
end

#add_user(user, modes = []) ⇒ 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.



348
349
350
351
# File 'lib/cinch/channel.rb', line 348

def add_user(user, modes = [])
  @in_channel = true if user == @bot
  @users[user] = modes
end

#ban(target) ⇒ Mask

Bans someone from the channel.

Parameters:

Returns:

  • (Mask)

    the mask used for banning



237
238
239
240
241
242
# File 'lib/cinch/channel.rb', line 237

def ban(target)
  mask = Mask.from(target)

  @bot.raw "MODE #@name +b #{mask}"
  mask
end

#clear_usersvoid

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.

Removes all users



364
365
366
# File 'lib/cinch/channel.rb', line 364

def clear_users
  @users.clear
end

#ctcp(message) ⇒ void

This method returns an undefined value.

Send a CTCP to the channel.

Parameters:

  • message (String)

    the ctcp message



422
423
424
# File 'lib/cinch/channel.rb', line 422

def ctcp(message)
  send "\001#{message}\001"
end

#deop(user) ⇒ void

This method returns an undefined value.

Parameters:



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

def deop(user)
  @bot.raw "MODE #@name -o #{user}"
end

#devoice(user) ⇒ void

This method returns an undefined value.

Parameters:



275
276
277
# File 'lib/cinch/channel.rb', line 275

def devoice(user)
  @bot.raw "MODE #@name -v #{user}"
end

#half_opped?(user) ⇒ Boolean

Returns true if ‘user` is half-opped in the channel.

Returns:

  • (Boolean)

    true if ‘user` is half-opped in the channel



122
123
124
125
# File 'lib/cinch/channel.rb', line 122

def half_opped?(user)
  user = @bot.user_manager.find_ensured(user) unless user.is_a?(User)
  @users[user].include? "h"
end

#has_user?(user) ⇒ Boolean

Returns Check if a user is in the channel.

Parameters:

Returns:

  • (Boolean)

    Check if a user is in the channel



109
110
111
112
# File 'lib/cinch/channel.rb', line 109

def has_user?(user)
  user = @bot.user_manager.find_ensured(user) unless user.is_a?(User)
  @users.has_key?(user)
end

#hashFixnum

Returns:

  • (Fixnum)


459
460
461
# File 'lib/cinch/channel.rb', line 459

def hash
  @name.hash
end

#inspectString

Returns:



470
471
472
# File 'lib/cinch/channel.rb', line 470

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

#invite(user) ⇒ void

This method returns an undefined value.

Invites a user to the channel.

Parameters:



283
284
285
# File 'lib/cinch/channel.rb', line 283

def invite(user)
  @bot.raw("INVITE #{user} #@name")
end

#join(key = nil) ⇒ void

This method returns an undefined value.

Joins the channel

Parameters:

  • key (String) (defaults to: nil)

    the channel key, if any. If none is specified but @key is set, @key will be used



337
338
339
340
341
342
# File 'lib/cinch/channel.rb', line 337

def join(key = nil)
  if key.nil? and self.key != true
    key = self.key
  end
  @bot.raw "JOIN #{[@name, key].compact.join(" ")}"
end

#kick(user, reason = nil) ⇒ void

This method returns an undefined value.

Kicks a user from the channel.

Parameters:

Raises:



305
306
307
308
309
310
311
# File 'lib/cinch/channel.rb', line 305

def kick(user, reason = nil)
  if reason.to_s.size > @bot.irc.isupport["KICKLEN"] && @bot.strict?
    raise Exceptions::KickReasonTooLong, reason
  end

  @bot.raw("KICK #@name #{user} :#{reason}")
end

#mode(s) ⇒ void

This method returns an undefined value.

Sets or unsets modes. Most of the time you won’t need this but use setter methods like #invite_only=.

Examples:

channel.mode "+n"

Parameters:

  • s (String)

    a mode string



320
321
322
# File 'lib/cinch/channel.rb', line 320

def mode(s)
  @bot.raw "MODE #@name #{s}"
end

#notice(message) ⇒ void

This method returns an undefined value.

Send a notice to the channel.

Parameters:

  • message (String)

    the message



385
386
387
# File 'lib/cinch/channel.rb', line 385

def notice(message)
  @bot.notice(@name, message)
end

#op(user) ⇒ void

This method returns an undefined value.

Parameters:



257
258
259
# File 'lib/cinch/channel.rb', line 257

def op(user)
  @bot.raw "MODE #@name +o #{user}"
end

#opped?(user) ⇒ Boolean

Returns true if ‘user` is opped in the channel.

Returns:

  • (Boolean)

    true if ‘user` is opped in the channel



116
117
118
119
# File 'lib/cinch/channel.rb', line 116

def opped?(user)
  user = @bot.user_manager.find_ensured(user) unless user.is_a?(User)
  @users[user].include? "o"
end

#part(message = nil) ⇒ void

This method returns an undefined value.

Causes the bot to part from the channel.

Parameters:

  • message (String) (defaults to: nil)

    the part message.



328
329
330
# File 'lib/cinch/channel.rb', line 328

def part(message = nil)
  @bot.raw "PART #@name :#{message}"
end

#remove_user(user) ⇒ 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.



355
356
357
358
# File 'lib/cinch/channel.rb', line 355

def remove_user(user)
  @in_channel = false if user == @bot
  @users.delete(user)
end

#safe_action(message) ⇒ void

TODO:

Handle mIRC color codes more gracefully.

This method returns an undefined value.

Invoke an action (/me) in the channel but remove any non-printable characters. The purpose of this method is to send text from untrusted sources, like other users or feeds.

Note: this will break any mIRC color codes embedded in the string.

Parameters:

  • message (String)

    the message

See Also:



446
447
448
# File 'lib/cinch/channel.rb', line 446

def safe_action(message)
  @bot.safe_action(@name, message)
end

#safe_notice(message) ⇒ void

TODO:

Handle mIRC color codes more gracefully.

This method returns an undefined value.

Like #safe_send but for notices.

Parameters:

  • message (String)

    the message

See Also:



395
396
397
# File 'lib/cinch/channel.rb', line 395

def safe_notice(message)
  @bot.safe_notice(@name, message)
end

#safe_send(message) ⇒ void Also known as: safe_privmsg, safe_msg

TODO:

Handle mIRC color codes more gracefully.

This method returns an undefined value.

Send a message to the channel, but remove any non-printable characters. The purpose of this method is to send text from untrusted sources, like other users or feeds.

Note: this will break any mIRC color codes embedded in the string.

Parameters:

  • message (String)

    the message

See Also:



410
411
412
# File 'lib/cinch/channel.rb', line 410

def safe_send(message)
  @bot.safe_msg(@name, message)
end

#send(message) ⇒ void Also known as: privmsg, msg

This method returns an undefined value.

Send a message to the channel.

Parameters:

  • message (String)

    the message

See Also:



375
376
377
# File 'lib/cinch/channel.rb', line 375

def send(message)
  @bot.msg(@name, message)
end

#sync_modes(all = true) ⇒ 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.



222
223
224
225
226
227
228
229
# File 'lib/cinch/channel.rb', line 222

def sync_modes(all = true)
  unsync :users
  unsync :bans
  unsync :modes
  @bot.raw "NAMES #@name" if all
  @bot.raw "MODE #@name +b" # bans
  @bot.raw "MODE #@name"
end

#to_sString Also known as: to_str

Returns:



464
465
466
# File 'lib/cinch/channel.rb', line 464

def to_s
  @name
end

#unban(target) ⇒ Mask

Unbans someone from the channel.

Parameters:

Returns:

  • (Mask)

    the mask used for unbanning



248
249
250
251
252
253
# File 'lib/cinch/channel.rb', line 248

def unban(target)
  mask = Mask.from(target)

  @bot.raw "MODE #@name -b #{mask}"
  mask
end

#voice(user) ⇒ void

This method returns an undefined value.

Parameters:



269
270
271
# File 'lib/cinch/channel.rb', line 269

def voice(user)
  @bot.raw "MODE #@name +v #{user}"
end

#voiced?(user) ⇒ Boolean

Returns true if ‘user` is voiced in the channel.

Returns:

  • (Boolean)

    true if ‘user` is voiced in the channel



128
129
130
131
# File 'lib/cinch/channel.rb', line 128

def voiced?(user)
  user = @bot.user_manager.find_ensured(user) unless user.is_a?(User)
  @users[user].include? "v"
end