-- Implementation of the Channel class
-- initialization {{{
-local base = _G
-local irc = require 'irc'
-local misc = require 'irc.misc'
-local socket = require 'socket'
-local table = require 'table'
+local irc = libs.irc
+local misc = libs.misc
+local socket = libs.socket
-- }}}
---
-- This module implements a channel object representing a single channel we
-- have joined.
-module 'irc.channel'
+local channel = {}
-- object metatable {{{
-- TODO: this <br /> shouldn't be necessary - bug in luadoc
elseif key == "chanmode" then
return self._chanmode
else
- return _M[key]
+ return channel[key]
end
end,
-- }}}
elseif key == "chanmode" then
return
else
- base.rawset(self, key, value)
+ rawset(self, key, value)
end
end,
-- }}}
__concat = function(first, second)
local first_str, second_str
- if base.type(first) == "table" then
+ if type(first) == "table" then
first_str = first._name
else
first_str = first
end
- if base.type(second) == "table" then
+ if type(second) == "table" then
second_str = second._name
else
second_str = second
-- @param self Channel object
-- @param set True to set the mode, false to unset it
-- @param letter Letter of the mode
-local function set_basic_mode(self, set, letter)
+local function set_basic_mode(set, letter)
if set then
irc.send("MODE", self.name, "+" .. letter)
else
-- @param self Channel object
-- @param user Nick of the user to add
-- @param mode Mode (op/voice) of the user, in symbolic form (@/+)
-function _add_user(self, user, mode)
+function channel:_add_user(user, mode)
mode = mode or ''
self._members[user] = mode .. user
end
-- Remove a user from the channel's internal user list.
-- @param self Channel object
-- @param user Nick of the user to remove
-function _remove_user(self, user)
+function channel:_remove_user(user)
self._members[user] = nil
end
-- }}}
-- @param user Nick of the user to affect
-- @param on True if the mode is being set, false if it's being unset
-- @param mode 'o' for op, 'v' for voice
-function _change_status(self, user, on, mode)
+function channel:_change_status(user, on, mode)
if on then
if mode == 'o' then
self._members[user] = '@' .. user
-- @param self Channel object
-- @param old_nick User's old nick
-- @param new_nick User's new nick
-function _change_nick(self, old_nick, new_nick)
+function channel:_change_nick(old_nick, new_nick)
for member in self:each_member() do
local member_nick = member:gsub('@+', '')
if member_nick == old_nick then
-- Creates a new Channel object.
-- @param chan Name of the new channel
-- @return The new channel instance
-function new(chan)
- return base.setmetatable({_name = chan, _topic = {}, _chanmode = "",
+function channel.new(chan)
+ return setmetatable({_name = chan, _topic = {}, _chanmode = "",
_members = {}}, mt)
end
-- }}}
---
-- Iterator over the ops in the channel
-- @param self Channel object
-function each_op(self)
+function channel:each_op()
return function(state, arg)
return misc._value_iter(state, arg,
function(v)
---
-- Iterator over the voiced users in the channel
-- @param self Channel object
-function each_voice(self)
+function channel:each_voice()
return function(state, arg)
return misc._value_iter(state, arg,
function(v)
---
-- Iterator over the normal users in the channel
-- @param self Channel object
-function each_user(self)
+function channel:each_user()
return function(state, arg)
return misc._value_iter(state, arg,
function(v)
---
-- Iterator over all users in the channel
-- @param self Channel object
-function each_member(self)
+function channel:each_member()
return misc._value_iter, self._members, nil
end
-- }}}
-- Gets an array of all the ops in the channel.
-- @param self Channel object
-- @return Array of channel ops
-function ops(self)
+function channel:ops()
local ret = {}
for nick in self:each_op() do
table.insert(ret, nick)
-- Gets an array of all the voiced users in the channel.
-- @param self Channel object
-- @return Array of channel voiced users
-function voices(self)
+function channel:voices()
local ret = {}
for nick in self:each_voice() do
table.insert(ret, nick)
-- Gets an array of all the normal users in the channel.
-- @param self Channel object
-- @return Array of channel normal users
-function users(self)
+function channel:users()
local ret = {}
for nick in self:each_user() do
table.insert(ret, nick)
-- Gets an array of all the users in the channel.
-- @param self Channel object
-- @return Array of channel users
-function members(self)
+function channel:members()
local ret = {}
-- not just returning self._members, since the return value shouldn't be
-- modifiable
-- Ban a user from a channel.
-- @param self Channel object
-- @param name User to ban
-function ban(self, name)
+function channel:ban(name)
irc.send("MODE", self.name, "+b", name)
end
-- }}}
-- Remove a ban on a user.
-- @param self Channel object
-- @param name User to unban
-function unban(self, name)
+function channel:unban(name)
irc.send("MODE", self.name, "-b", name)
end
-- }}}
-- Give a user voice on a channel.
-- @param self Channel object
-- @param name User to give voice to
-function voice(self, name)
+function channel:voice(name)
irc.send("MODE", self.name, "+v", name)
end
-- }}}
-- Remove voice from a user.
-- @param self Channel object
-- @param name User to remove voice from
-function devoice(self, name)
+function channel:devoice(name)
irc.send("MODE", self.name, "-v", name)
end
-- }}}
-- Give a user ops on a channel.
-- @param self Channel object
-- @param name User to op
-function op(self, name)
+function channel:op(name)
irc.send("MODE", self.name, "+o", name)
end
-- }}}
-- Remove ops from a user.
-- @param self Channel object
-- @param name User to remove ops from
-function deop(self, name)
+function channel:deop(name)
irc.send("MODE", self.name, "-o", name)
end
-- }}}
-- @param self Channel object
-- @param new_limit New value for the channel limit (optional; limit is unset
-- if this argument isn't passed)
-function set_limit(self, new_limit)
+function channel:set_limit(new_limit)
if new_limit then
irc.send("MODE", self.name, "+l", new_limit)
else
-- @param self Channel object
-- @param key New channel password (optional; password is unset if this
-- argument isn't passed)
-function set_key(self, key)
+function channel:set_key(key)
if key then
irc.send("MODE", self.name, "+k", key)
else
-- Set the private state of a channel.
-- @param self Channel object
-- @param set True to set the channel as private, false to unset it
-function set_private(self, set)
+function channel:set_private(set)
set_basic_mode(self, set, "p")
end
-- }}}
-- Set the secret state of a channel.
-- @param self Channel object
-- @param set True to set the channel as secret, false to unset it
-function set_secret(self, set)
+function channel:set_secret(set)
set_basic_mode(self, set, "s")
end
-- }}}
-- Set whether joining the channel requires an invite.
-- @param self Channel object
-- @param set True to set the channel invite only, false to unset it
-function set_invite_only(self, set)
+function channel:set_invite_only(set)
set_basic_mode(self, set, "i")
end
-- }}}
-- If true, the topic can only be changed by an op.
-- @param self Channel object
-- @param set True to lock the topic, false to unlock it
-function set_topic_lock(self, set)
+function channel:set_topic_lock(set)
set_basic_mode(self, set, "t")
end
-- }}}
-- @param self Channel object
-- @param set True to require users to be in the channel to send messages to
-- it, false to remove this restriction
-function set_no_outside_messages(self, set)
+function channel:set_no_outside_messages(set)
set_basic_mode(self, set, "n")
end
-- }}}
-- Set whether voice is required to speak.
-- @param self Channel object
-- @param set True to set the channel as moderated, false to unset it
-function set_moderated(self, set)
+function channel:set_moderated(set)
set_basic_mode(self, set, "m")
end
-- }}}
-- @param self Channel object
-- @param nick Nick to search for
-- @return True if the nick is in the channel, false otherwise
-function contains(self, nick)
+function channel:contains(nick)
for member in self:each_member() do
local member_nick = member:gsub('@+', '')
if member_nick == nick then
-- }}}
-- }}}
-- }}}
+
+return channel