class Irc::Channel

An IRC Channel is identified by its name, and it has a set of properties:

The Channel::Topic and Channel::Mode classes are defined within the Channel namespace because they only make sense there

Here we start with the actual Channel class

Attributes

creation_time[RW]
mode[R]
name[R]
to_s[R]
topic[R]
url[RW]
users[R]

Public Class Methods

new(name, topic=nil, users=[], opts={}) click to toggle source

Creates a new channel with the given name, optionally setting the topic and an initial users list.

No additional info is created here, because the channel flags and userlists allowed depend on the server.

# File lib/rbot/irc.rb, line 1395
def initialize(name, topic=nil, users=[], opts={})
  raise ArgumentError, "Channel name cannot be empty" if name.to_s.empty?
  warn "Unknown channel prefix #{name[0,1]}" if name !~ /^[&#+!]/
  raise ArgumentError, "Invalid character in #{name.inspect}" if name =~ /[ \x07,]/

  init_server_or_casemap(opts)

  @name = name

  @topic = topic ? topic.to_irc_channel_topic : Channel::Topic.new

  @users = UserList.new

  users.each { |u|
    add_user(u)
  }

  # Flags
  @mode = ModeHash.new

  # creation time, only on some networks
  @creation_time = nil

  # URL, only on some networks
  @url = nil
end
npname(str) click to toggle source

Return the non-prefixed part of a channel name. Also works with ## channels found on some networks (e.g. FreeNode)

# File lib/rbot/irc.rb, line 1335
def self.npname(str)
  return str.to_s.sub(/^[&#+!]+/,'')
end

Public Instance Methods

add_user(user, opts={}) click to toggle source

Adds a user to the channel

# File lib/rbot/irc.rb, line 1380
def add_user(user, opts={})
  silent = opts.fetch(:silent, false)
  if has_user?(user)
    warn "Trying to add user #{user} to channel #{self} again" unless silent
  else
    @users << user.to_irc_user(server_and_casemap)
  end
end
create_mode(sym, kl) click to toggle source

Create a new mode

# File lib/rbot/irc.rb, line 1463
def create_mode(sym, kl)
  @mode[sym.to_sym] = kl.new(self)
end
delete_user(user) click to toggle source

Removes a user from the channel

# File lib/rbot/irc.rb, line 1424
def delete_user(user)
  @mode.each { |sym, mode|
    mode.reset(user) if mode.kind_of?(UserMode)
  }
  @users.delete(user)
end
get_user(nick) click to toggle source

Returns the user with nick nick, if available

# File lib/rbot/irc.rb, line 1373
def get_user(nick)
  idx = has_user?(nick)
  @users[idx] if idx
end
has_op?(user) click to toggle source
# File lib/rbot/irc.rb, line 1475
def has_op?(user)
  @mode.has_key?(:o) and @mode[:o].list[user]
end
has_user?(nick) click to toggle source

Checks if the receiver already has a user with the given nick

# File lib/rbot/irc.rb, line 1367
def has_user?(nick)
  @users.index(nick.to_irc_user(server_and_casemap))
end
has_voice?(user) click to toggle source
# File lib/rbot/irc.rb, line 1479
def has_voice?(user)
  @mode.has_key?(:v) and @mode[:v].list[user]
end
inspect() click to toggle source
# File lib/rbot/irc.rb, line 1344
def inspect
  str = self.__to_s__[0..-2]
  str << " on server #{server}" if server
  str << " @name=#{@name.inspect} @topic=#{@topic.text.inspect}"
  str << " @users=[#{user_nicks.sort.join(', ')}]"
  str << " (created on #{creation_time})" if creation_time
  str << " (URL #{url})" if url
  str << ">"
end
local?() click to toggle source

A channel is local to a server if it has the ‘&’ prefix

# File lib/rbot/irc.rb, line 1439
def local?
  name[0,1] == '&'
end
modeless?() click to toggle source

A channel is modeless if it has the ‘+’ prefix

# File lib/rbot/irc.rb, line 1445
def modeless?
  name[0,1] == '+'
end
modes_of(user) click to toggle source
# File lib/rbot/irc.rb, line 1467
def modes_of(user)
  l = []
  @mode.map { |s, m|
    l << s if (m.class <= UserMode and m.list[user])
  }
  l
end
normal?() click to toggle source

A channel is normal if it has the ‘#’ prefix

# File lib/rbot/irc.rb, line 1457
def normal?
  name[0,1] == '#'
end
prefix() click to toggle source

The channel prefix

# File lib/rbot/irc.rb, line 1433
def prefix
  name[0,1]
end
safe?() click to toggle source

A channel is safe if it has the ‘!’ prefix

# File lib/rbot/irc.rb, line 1451
def safe?
  name[0,1] == '!'
end
to_irc_channel() click to toggle source

Returns self

# File lib/rbot/irc.rb, line 1356
def to_irc_channel
  self
end
user_nicks() click to toggle source

TODO Ho

# File lib/rbot/irc.rb, line 1361
def user_nicks
  @users.map { |u| u.downcase }
end