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

Class Method Summary collapse

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)


1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
# 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

Instance Attribute Details

#creation_timeObject

Returns the value of attribute creation_time.



1342
1343
1344
# File 'lib/rbot/irc.rb', line 1342

def creation_time
  @creation_time
end

#modeObject (readonly)

Returns the value of attribute mode.



1340
1341
1342
# File 'lib/rbot/irc.rb', line 1340

def mode
  @mode
end

#nameObject (readonly) Also known as: to_s

Returns the value of attribute name.



1340
1341
1342
# File 'lib/rbot/irc.rb', line 1340

def name
  @name
end

#topicObject (readonly)

Returns the value of attribute topic.



1340
1341
1342
# File 'lib/rbot/irc.rb', line 1340

def topic
  @topic
end

#urlObject

Returns the value of attribute url.



1342
1343
1344
# File 'lib/rbot/irc.rb', line 1342

def url
  @url
end

#usersObject (readonly)

Returns the value of attribute users.



1340
1341
1342
# File 'lib/rbot/irc.rb', line 1340

def users
  @users
end

Class Method Details

.npname(str) ⇒ Object

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



1335
1336
1337
# File 'lib/rbot/irc.rb', line 1335

def self.npname(str)
  return str.to_s.sub(/^[&#+!]+/,'')
end

Instance Method Details

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

Adds a user to the channel



1380
1381
1382
1383
1384
1385
1386
1387
# 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) ⇒ Object

Create a new mode



1463
1464
1465
# File 'lib/rbot/irc.rb', line 1463

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

#delete_user(user) ⇒ Object

Removes a user from the channel



1424
1425
1426
1427
1428
1429
# 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) ⇒ Object

Returns the user with nick nick, if available



1373
1374
1375
1376
# File 'lib/rbot/irc.rb', line 1373

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

#has_op?(user) ⇒ Boolean

Returns:

  • (Boolean)


1475
1476
1477
# File 'lib/rbot/irc.rb', line 1475

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)


1367
1368
1369
# File 'lib/rbot/irc.rb', line 1367

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

#has_voice?(user) ⇒ Boolean

Returns:

  • (Boolean)


1479
1480
1481
# File 'lib/rbot/irc.rb', line 1479

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

#inspectObject



1344
1345
1346
1347
1348
1349
1350
1351
1352
# 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?Boolean

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

Returns:

  • (Boolean)


1439
1440
1441
# File 'lib/rbot/irc.rb', line 1439

def local?
  name[0,1] == '&'
end

#modeless?Boolean

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

Returns:

  • (Boolean)


1445
1446
1447
# File 'lib/rbot/irc.rb', line 1445

def modeless?
  name[0,1] == '+'
end

#modes_of(user) ⇒ Object



1467
1468
1469
1470
1471
1472
1473
# 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?Boolean

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

Returns:

  • (Boolean)


1457
1458
1459
# File 'lib/rbot/irc.rb', line 1457

def normal?
  name[0,1] == '#'
end

#prefixObject

The channel prefix



1433
1434
1435
# File 'lib/rbot/irc.rb', line 1433

def prefix
  name[0,1]
end

#safe?Boolean

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

Returns:

  • (Boolean)


1451
1452
1453
# File 'lib/rbot/irc.rb', line 1451

def safe?
  name[0,1] == '!'
end

#to_irc_channelObject

Returns self



1356
1357
1358
# File 'lib/rbot/irc.rb', line 1356

def to_irc_channel
  self
end

#user_nicksObject

TODO Ho



1361
1362
1363
# File 'lib/rbot/irc.rb', line 1361

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