Class: Irc::Channel

Inherits:
Object show all
Includes:
ServerOrCasemap
Defined in:
lib/rbot/irc.rb,
lib/rbot/irc.rb

Overview

Here we start with the actual Channel class

Defined Under Namespace

Classes: Mode, ModeHash, ModeTypeA, ModeTypeB, ModeTypeC, ModeTypeD, Topic, UserMode

Instance Attribute Summary collapse

Attributes included from ServerOrCasemap

#server

Instance Method Summary collapse

Methods included from ServerOrCasemap

#casemap, #downcase, #fits_with_server_and_casemap?, #init_server_or_casemap, #irc_downcase, #irc_upcase, #server_and_casemap, #upcase

Constructor Details

#initialize(name, topic = nil, users = [], opts = {}) ⇒ Channel

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.

Raises:

  • (ArgumentError)


1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
# File 'lib/rbot/irc.rb', line 1352

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].chr}" 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
end

Instance Attribute Details

#modeObject (readonly)

Returns the value of attribute mode.



1300
1301
1302
# File 'lib/rbot/irc.rb', line 1300

def mode
  @mode
end

#nameObject (readonly) Also known as: to_s

Returns the value of attribute name.



1300
1301
1302
# File 'lib/rbot/irc.rb', line 1300

def name
  @name
end

#topicObject (readonly)

Returns the value of attribute topic.



1300
1301
1302
# File 'lib/rbot/irc.rb', line 1300

def topic
  @topic
end

#usersObject (readonly)

Returns the value of attribute users.



1300
1301
1302
# File 'lib/rbot/irc.rb', line 1300

def users
  @users
end

Instance Method Details

#add_user(user, opts = {}) ⇒ Object

Adds a user to the channel



1337
1338
1339
1340
1341
1342
1343
1344
# File 'lib/rbot/irc.rb', line 1337

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) ⇒ Object

Create a new mode



1414
1415
1416
# File 'lib/rbot/irc.rb', line 1414

def create_mode(sym, kl)
  @mode[sym.to_sym] = kl.new(self)
end

#delete_user(user) ⇒ Object

Removes a user from the channel



1375
1376
1377
1378
1379
1380
# File 'lib/rbot/irc.rb', line 1375

def delete_user(user)
  @mode.each { |sym, mode|
    mode.reset(user) if mode.kind_of?(UserMode)
  }
  @users.delete(user)
end

#get_user(nick) ⇒ Object

Returns the user with nick nick, if available



1330
1331
1332
1333
# File 'lib/rbot/irc.rb', line 1330

def get_user(nick)
  idx = has_user?(nick)
  @users[idx] if idx
end

#has_op?(user) ⇒ Boolean

Returns:

  • (Boolean)


1426
1427
1428
# File 'lib/rbot/irc.rb', line 1426

def has_op?(user)
  @mode.has_key?(:o) and @mode[:o].list[user]
end

#has_user?(nick) ⇒ Boolean

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

Returns:

  • (Boolean)


1324
1325
1326
# File 'lib/rbot/irc.rb', line 1324

def has_user?(nick)
  @users.index(nick.to_irc_user(server_and_casemap))
end

#has_voice?(user) ⇒ Boolean

Returns:

  • (Boolean)


1430
1431
1432
# File 'lib/rbot/irc.rb', line 1430

def has_voice?(user)
  @mode.has_key?(:v) and @mode[:v].list[user]
end

#inspectObject



1303
1304
1305
1306
1307
1308
1309
# File 'lib/rbot/irc.rb', line 1303

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 << ">"
end

#local?Boolean

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

Returns:

  • (Boolean)


1390
1391
1392
# File 'lib/rbot/irc.rb', line 1390

def local?
  name[0] == 0x26
end

#modeless?Boolean

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

Returns:

  • (Boolean)


1396
1397
1398
# File 'lib/rbot/irc.rb', line 1396

def modeless?
  name[0] == 0x2b
end

#modes_of(user) ⇒ Object



1418
1419
1420
1421
1422
1423
1424
# File 'lib/rbot/irc.rb', line 1418

def modes_of(user)
  l = []
  @mode.map { |s, m|
    l << s if (m.class <= UserMode and m.list[user])
  }
  l
end

#normal?Boolean

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

Returns:

  • (Boolean)


1408
1409
1410
# File 'lib/rbot/irc.rb', line 1408

def normal?
  name[0] == 0x23
end

#prefixObject

The channel prefix



1384
1385
1386
# File 'lib/rbot/irc.rb', line 1384

def prefix
  name[0].chr
end

#safe?Boolean

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

Returns:

  • (Boolean)


1402
1403
1404
# File 'lib/rbot/irc.rb', line 1402

def safe?
  name[0] == 0x21
end

#to_irc_channelObject

Returns self



1313
1314
1315
# File 'lib/rbot/irc.rb', line 1313

def to_irc_channel
  self
end

#user_nicksObject

TODO Ho



1318
1319
1320
# File 'lib/rbot/irc.rb', line 1318

def user_nicks
  @users.map { |u| u.downcase }
end