]> git.lizzy.rs Git - luairc.git/blobdiff - src/irc.lua
callbacks need to be able to return values (for, e.g. dcc accept callback)
[luairc.git] / src / irc.lua
index b472c49c6b769e9d45e4748ba0aa7a628c599d44..eb75721010580281a15d657039a6773513a0a2f4 100644 (file)
@@ -4,6 +4,8 @@
 -- initialization {{{
 local base =      _G
 local constants = require 'irc.constants'
+local ctcp =      require 'irc.ctcp'
+local c =         ctcp._ctcp_quote
 local irc_debug = require 'irc.debug'
 local message =   require 'irc.message'
 local misc =      require 'irc.misc'
@@ -15,7 +17,7 @@ local table =     require 'table'
 
 ---
 -- LuaIRC - IRC framework written in Lua
--- @release 0.02
+-- @release 0.2
 module 'irc'
 
 -- constants {{{
@@ -43,7 +45,9 @@ local icallbacks = {
 local requestinfo = {whois = {}}
 local handlers = {}
 local ctcp_handlers = {}
+local user_handlers = {}
 local serverinfo = {}
+local ip = nil
 -- }}}
 
 -- defaults {{{
@@ -62,13 +66,13 @@ OUTFILE = nil         -- file to send debug output to - nil is stdout
 local function main_loop_iter()
     if #rsockets == 0 and #wsockets == 0 then return false end
     local rready, wready, err = socket.select(rsockets, wsockets)
-    if err then irc_debug.err(err); return false; end
+    if err then irc_debug._err(err); return false; end
 
     for _, sock in base.ipairs(rready) do
         local cb = socket.protect(rcallbacks[sock])
         local ret, err = cb(sock)
         if not ret then
-            irc_debug.warn("socket error: " .. err)
+            irc_debug._warn("socket error: " .. err)
             _unregister_socket(sock, 'r')
         end
     end
@@ -77,7 +81,7 @@ local function main_loop_iter()
         local cb = socket.protect(wcallbacks[sock])
         local ret, err = cb(sock)
         if not ret then
-            irc_debug.warn("socket error: " .. err)
+            irc_debug._warn("socket error: " .. err)
             _unregister_socket(sock, 'w')
         end
     end
@@ -95,14 +99,20 @@ end
 -- incoming_message {{{
 local function incoming_message(sock)
     local raw_msg = socket.try(sock:receive())
-    irc_debug.message("RECV", raw_msg)
-    local msg = message.parse(raw_msg)
-    misc.try_call_warn("Unhandled server message: " .. msg.command,
-                       handlers["on_" .. msg.command:lower()],
-                       (misc.parse_user(msg.from)), base.unpack(msg.args))
+    irc_debug._message("RECV", raw_msg)
+    local msg = message._parse(raw_msg)
+    misc._try_call_warn("Unhandled server message: " .. msg.command,
+                        handlers["on_" .. msg.command:lower()],
+                        (misc.parse_user(msg.from)), base.unpack(msg.args))
     return true
 end
 -- }}}
+
+-- callback {{{
+local function callback(name, ...)
+    return misc._try_call(user_handlers[name], ...)
+end
+-- }}}
 -- }}}
 
 -- internal message handlers {{{
@@ -110,9 +120,9 @@ end
 -- on_nick {{{
 function handlers.on_nick(from, new_nick)
     for chan in channels() do
-        chan:change_nick(from, new_nick)
+        chan:_change_nick(from, new_nick)
     end
-    misc.try_call(on_nick_change, new_nick, from)
+    callback("nick_change", new_nick, from)
 end
 -- }}}
 
@@ -121,8 +131,8 @@ function handlers.on_join(from, chan)
     base.assert(serverinfo.channels[chan],
                 "Received join message for unknown channel: " .. chan)
     if serverinfo.channels[chan].join_complete then
-        serverinfo.channels[chan]:add_user(from)
-        misc.try_call(on_join, serverinfo.channels[chan], from)
+        serverinfo.channels[chan]:_add_user(from)
+        callback("join", serverinfo.channels[chan], from)
     end
 end
 -- }}}
@@ -133,8 +143,8 @@ function handlers.on_part(from, chan, part_msg)
     -- after we remove the channel from the channel list
     if not serverinfo.channels[chan] then return end
     if serverinfo.channels[chan].join_complete then
-        serverinfo.channels[chan]:remove_user(from)
-        misc.try_call(on_part, serverinfo.channels[chan], from, part_msg)
+        serverinfo.channels[chan]:_remove_user(from)
+        callback("part", serverinfo.channels[chan], from, part_msg)
     end
 end
 -- }}}
@@ -157,15 +167,15 @@ function handlers.on_mode(from, to, mode_string, ...)
             -- channel modes other than op/voice will be implemented as
             -- information request commands
             if mode == "o" then -- channel op {{{
-                chan:change_status(target, dir == "+", "o")
-                misc.try_call(({["+"] = on_op, ["-"] = on_deop})[dir],
-                              chan, from, target)
+                chan:_change_status(target, dir == "+", "o")
+                callback(({["+"] = "op", ["-"] = "deop"})[dir],
+                         chan, from, target)
                 ind = ind + 1
                 -- }}}
             elseif mode == "v" then -- voice {{{
-                chan:change_status(target, dir == "+", "v")
-                misc.try_call(({["+"] = on_voice, ["-"] = on_devoice})[dir],
-                              chan, from, target)
+                chan:_change_status(target, dir == "+", "v")
+                callback(({["+"] = "voice", ["-"] = "devoice"})[dir],
+                         chan, from, target)
                 ind = ind + 1
                 -- }}}
             end
@@ -200,14 +210,14 @@ function handlers.on_topic(from, chan, new_topic)
     serverinfo.channels[chan]._topic.user = (misc.parse_user(from))
     serverinfo.channels[chan]._topic.time = os.time()
     if serverinfo.channels[chan].join_complete then
-        misc.try_call(on_topic_change, serverinfo.channels[chan])
+        callback("topic_change", serverinfo.channels[chan])
     end
 end
 -- }}}
 
 -- on_invite {{{
 function handlers.on_invite(from, to, chan)
-    misc.try_call(on_invite, from, chan)
+    callback("invite", from, chan)
 end
 -- }}}
 
@@ -216,29 +226,20 @@ function handlers.on_kick(from, chan, to)
     base.assert(serverinfo.channels[chan],
                 "Received kick message for unknown channel: " .. chan)
     if serverinfo.channels[chan].join_complete then
-        serverinfo.channels[chan]:remove_user(to)
-        misc.try_call(on_kick, serverinfo.channels[chan], to, from)
+        serverinfo.channels[chan]:_remove_user(to)
+        callback("kick", serverinfo.channels[chan], to, from)
     end
 end
 -- }}}
 
 -- on_privmsg {{{
 function handlers.on_privmsg(from, to, msg)
-    local msgs = ctcp.ctcp_split(msg, true)
+    local msgs = ctcp._ctcp_split(msg)
     for _, v in base.ipairs(msgs) do
-        if base.type(v) == "string" then
-            -- normal message {{{
-            if to:sub(1, 1) == "#" then
-                base.assert(serverinfo.channels[to],
-                            "Received channel msg from unknown channel: " .. to)
-                misc.try_call(on_channel_msg, serverinfo.channels[to], from, v)
-            else
-                misc.try_call(on_private_msg, from, v)
-            end
-            -- }}}
-        elseif base.type(v) == "table" then
+        local msg = v.str
+        if v.ctcp then
             -- ctcp message {{{
-            local words = misc.split(v[1])
+            local words = misc._split(msg)
             local received_command = words[1]
             local cb = "on_" .. received_command:lower()
             table.remove(words, 1)
@@ -247,7 +248,17 @@ function handlers.on_privmsg(from, to, msg)
             if base.type(ctcp_handlers[cb]) == "function" then
                 ctcp_handlers[cb](from, to, table.concat(words, " "))
             else
-                notice(from, {"ERRMSG Unknown query: " .. received_command})
+                notice(from, c("ERRMSG", received_command, ":Unknown query"))
+            end
+            -- }}}
+        else
+            -- normal message {{{
+            if to:sub(1, 1) == "#" then
+                base.assert(serverinfo.channels[to],
+                            "Received channel msg from unknown channel: " .. to)
+                callback("channel_msg", serverinfo.channels[to], from, msg)
+            else
+                callback("private_msg", from, msg)
             end
             -- }}}
         end
@@ -257,28 +268,28 @@ end
 
 -- on_notice {{{
 function handlers.on_notice(from, to, msg)
-    local msgs = ctcp.ctcp_split(msg, true)
+    local msgs = ctcp._ctcp_split(msg)
     for _, v in base.ipairs(msgs) do
-        if base.type(v) == "string" then
+        local msg = v.str
+        if v.ctcp then
+            -- ctcp message {{{
+            local words = misc._split(msg)
+            local command = words[1]:lower()
+            table.remove(words, 1)
+            misc._try_call_warn("Unknown CTCP message: " .. command,
+                                ctcp_handlers["on_rpl_"..command], from, to,
+                                table.concat(words, ' '))
+            -- }}}
+        else
             -- normal message {{{
             if to:sub(1, 1) == "#" then
                 base.assert(serverinfo.channels[to],
                             "Received channel msg from unknown channel: " .. to)
-                misc.try_call(on_channel_notice, serverinfo.channels[to],
-                              from, v)
+                callback("channel_notice", serverinfo.channels[to], from, msg)
             else
-                misc.try_call(on_private_notice, from, v)
+                callback("private_notice", from, msg)
             end
             -- }}}
-        elseif base.type(v) == "table" then
-            -- ctcp message {{{
-            local words = misc.split(v[1])
-            local command = words[1]:lower()
-            table.remove(words, 1)
-            misc.try_call_warn("Unknown CTCP message: " .. command,
-                               ctcp_handlers["on_rpl_"..command], from, to,
-                               table.concat(words, ' '))
-            -- }}}
         end
     end
 end
@@ -287,9 +298,9 @@ end
 -- on_quit {{{
 function handlers.on_quit(from, quit_msg)
     for name, chan in base.pairs(serverinfo.channels) do
-        chan:remove_user(from)
+        chan:_remove_user(from)
     end
-    misc.try_call(on_quit, from, quit_msg)
+    callback("quit", from, quit_msg)
 end
 -- }}}
 
@@ -335,13 +346,13 @@ function handlers.on_rpl_namreply(from, chanmode, chan, userlist)
     base.assert(serverinfo.channels[chan],
                 "Received user information about unknown channel: " .. chan)
     serverinfo.channels[chan]._chanmode = constants.chanmodes[chanmode]
-    local users = misc.split(userlist)
+    local users = misc._split(userlist)
     for k,v in base.ipairs(users) do
         if v:sub(1, 1) == "@" or v:sub(1, 1) == "+" then
             local nick = v:sub(2)
-            serverinfo.channels[chan]:add_user(nick, v:sub(1, 1))
+            serverinfo.channels[chan]:_add_user(nick, v:sub(1, 1))
         else
-            serverinfo.channels[chan]:add_user(v)
+            serverinfo.channels[chan]:_add_user(v)
         end
     end
 end
@@ -354,7 +365,7 @@ function handlers.on_rpl_endofnames(from, chan)
     base.assert(serverinfo.channels[chan],
                 "Received user information about unknown channel: " .. chan)
     if not serverinfo.channels[chan].join_complete then
-        misc.try_call(on_me_join, serverinfo.channels[chan])
+        callback("me_join", serverinfo.channels[chan])
         serverinfo.channels[chan].join_complete = true
     end
 end
@@ -393,17 +404,18 @@ function handlers.on_rpl_endofmotd(from)
     if not serverinfo.connected then
         serverinfo.connected = true
         serverinfo.connecting = false
-        misc.try_call(on_connect)
+        callback("connect")
     end
 end
 -- }}}
 
 -- on_rpl_whoisuser {{{
 function handlers.on_rpl_whoisuser(from, nick, user, host, star, realname)
-    nick = nick:lower()
-    requestinfo.whois[nick].user = user
-    requestinfo.whois[nick].host = host
-    requestinfo.whois[nick].realname = realname
+    local lnick = nick:lower()
+    requestinfo.whois[lnick].nick = nick
+    requestinfo.whois[lnick].user = user
+    requestinfo.whois[lnick].host = host
+    requestinfo.whois[lnick].realname = realname
 end
 -- }}}
 
@@ -413,7 +425,7 @@ function handlers.on_rpl_whoischannels(from, nick, channel_list)
     if not requestinfo.whois[nick].channels then
         requestinfo.whois[nick].channels = {}
     end
-    for _, channel in base.ipairs(misc.split(channel_list)) do
+    for _, channel in base.ipairs(misc._split(channel_list)) do
         table.insert(requestinfo.whois[nick].channels, channel)
     end
 end
@@ -487,20 +499,22 @@ end
 function ctcp_handlers.on_action(from, to, message)
     if to:sub(1, 1) == "#" then
         base.assert(serverinfo.channels[to],
-        "Received channel msg from unknown channel: " .. to)
-        misc.try_call(on_channel_act, serverinfo.channels[to], from, message)
+                    "Received channel msg from unknown channel: " .. to)
+        callback("channel_act", serverinfo.channels[to], from, message)
     else
-        misc.try_call(on_private_act, from, message)
+        callback("private_act", from, message)
     end
 end
 -- }}}
 
 -- on_dcc {{{
+-- TODO: can we not have this handler be registered unless the dcc module is
+-- loaded?
 function ctcp_handlers.on_dcc(from, to, message)
-    local type, argument, address, port, size = base.unpack(misc.split(message, " ", nil, '"', '"'))
+    local type, argument, address, port, size = base.unpack(misc._split(message, " ", nil, '"', '"'))
     if type == "SEND" then
-        if misc.try_call(on_dcc, from, to, argument, address, port, size) then
-            dcc.accept(argument, address, port, size)
+        if callback("dcc", from, to, argument, address, port, size) then
+            dcc._accept(argument, address, port)
         end
     elseif type == "CHAT" then
         -- TODO: implement this? do people ever use this?
@@ -510,25 +524,25 @@ end
 
 -- on_version {{{
 function ctcp_handlers.on_version(from, to)
-    notice(from, {"VERSION " .. _VERSION .. " running under " .. base._VERSION .. " with " .. socket._VERSION})
+    notice(from, c("VERSION", _VERSION .. " running under " .. base._VERSION .. " with " .. socket._VERSION))
 end
 -- }}}
 
 -- on_errmsg {{{
 function ctcp_handlers.on_errmsg(from, to, message)
-    notice(from, {"ERRMSG " .. message .. "No error has occurred"})
+    notice(from, c("ERRMSG", message, ":No error has occurred"))
 end
 -- }}}
 
 -- on_ping {{{
 function ctcp_handlers.on_ping(from, to, timestamp)
-    notice(from, {"PING " .. timestamp})
+    notice(from, c("PING", timestamp))
 end
 -- }}}
 
 -- on_time {{{
 function ctcp_handlers.on_time(from, to)
-    notice(from, {"TIME " .. os.date()})
+    notice(from, c("TIME", os.date()))
 end
 -- }}}
 -- }}}
@@ -541,10 +555,11 @@ ctcp_handlers.on_rpl_action = ctcp_handlers.on_action
 
 -- on_rpl_version {{{
 function ctcp_handlers.on_rpl_version(from, to, version)
-    local cb = table.remove(icallbacks.ctcp_version[from], 1)
+    local lfrom = from:lower()
+    local cb = table.remove(icallbacks.ctcp_version[lfrom], 1)
     cb({version = version, nick = from})
-    if #icallbacks.ctcp_version[from] > 0 then say(from, {"VERSION"})
-    else icallbacks.ctcp_version[from] = nil
+    if #icallbacks.ctcp_version[lfrom] > 0 then say(from, c("VERSION"))
+    else icallbacks.ctcp_version[lfrom] = nil
     end
 end
 -- }}}
@@ -557,20 +572,22 @@ end
 
 -- on_rpl_ping {{{
 function ctcp_handlers.on_rpl_ping(from, to, timestamp)
-    local cb = table.remove(icallbacks.ctcp_ping[from], 1)
+    local lfrom = from:lower()
+    local cb = table.remove(icallbacks.ctcp_ping[lfrom], 1)
     cb({time = os.time() - timestamp, nick = from})
-    if #icallbacks.ctcp_ping[from] > 0 then say(from, {"PING " .. os.time()})
-    else icallbacks.ctcp_ping[from] = nil
+    if #icallbacks.ctcp_ping[lfrom] > 0 then say(from, c("PING", os.time()))
+    else icallbacks.ctcp_ping[lfrom] = nil
     end
 end
 -- }}}
 
 -- on_rpl_time {{{
 function ctcp_handlers.on_rpl_time(from, to, time)
-    local cb = table.remove(icallbacks.ctcp_time[from], 1)
+    local lfrom = from:lower()
+    local cb = table.remove(icallbacks.ctcp_time[lfrom], 1)
     cb({time = time, nick = from})
-    if #icallbacks.ctcp_time[from] > 0 then say(from, {"TIME"})
-    else icallbacks.ctcp_time[from] = nil
+    if #icallbacks.ctcp_time[lfrom] > 0 then say(from, c("TIME"))
+    else icallbacks.ctcp_time[lfrom] = nil
     end
 end
 -- }}}
@@ -580,7 +597,13 @@ end
 
 -- module functions {{{
 -- socket handling functions {{{
--- _register_socket() - register a socket to listen on {{{
+-- _register_socket {{{
+--
+-- Register a socket to listen on.
+-- @param sock LuaSocket socket object
+-- @param mode 'r' if the socket is for reading, 'w' if for writing
+-- @param cb   Callback to call when the socket is ready for reading/writing.
+--             It will be called with the socket as the single argument.
 function _register_socket(sock, mode, cb)
     local socks, cbs
     if mode == 'r' then
@@ -596,7 +619,11 @@ function _register_socket(sock, mode, cb)
 end
 -- }}}
 
--- _unregister_socket() - remove a previously registered socket {{{
+-- _unregister_socket {{{
+--
+-- Remove a previously registered socket.
+-- @param sock Socket to unregister
+-- @param mode 'r' to unregister it for reading, 'w' for writing
 function _unregister_socket(sock, mode)
     local socks, cbs
     if mode == 'r' then
@@ -657,7 +684,7 @@ function connect(args)
     _register_socket(irc_sock, 'r', incoming_message)
     if args.pass then send("PASS", args.pass) end
     send("NICK", nick)
-    send("USER", username, (irc_sock:getsockname()), network, realname)
+    send("USER", username, get_ip(), network, realname)
     begin_main_loop()
 end
 -- }}}
@@ -727,13 +754,22 @@ end
 function act(name, action)
     if not name then return end
     action = action or ""
-    send("PRIVMSG", name, {"ACTION", action})
+    send("PRIVMSG", name, c("ACTION", action))
 end
 -- }}}
 -- }}}
 
 -- information requests {{{
 -- server_version {{{
+---
+-- Request the version of the IRC server you are currently connected to.
+-- @param cb Callback to call when the information is available. The single
+--           table parameter to this callback will contain the fields:
+--           <ul>
+--           <li><i>server:</i>   the server which responded to the request</li>
+--           <li><i>version:</i>  the server version</li>
+--           <li><i>comments:</i> other data provided by the server</li>
+--           </ul>
 function server_version(cb)
     -- apparently the optional server parameter isn't supported for servers
     -- which you are not directly connected to (freenode specific?)
@@ -749,9 +785,31 @@ end
 
 -- whois {{{
 -- TODO: allow server parameter (to get user idle time)
+---
+-- Request WHOIS information about a given user.
+-- @param cb Callback to call when the information is available. The single
+--           table parameter to this callback may contain any or all of the
+--           fields:
+--           <ul>
+--           <li><i>nick:</i>       the nick that was passed to this function
+--                                  (this field will always be here)</li>
+--           <li><i>user:</i>       the IRC username of the user</li>
+--           <li><i>host:</i>       the user's hostname</li>
+--           <li><i>realname:</i>   the IRC realname of the user</li>
+--           <li><i>server:</i>     the IRC server the user is connected to</li>
+--           <li><i>serverinfo:</i> arbitrary information about the above
+--                                  server</li>
+--           <li><i>awaymsg:</i>    set to the user's away message if they are
+--                                  away</li>
+--           <li><i>is_oper:</i>    true if the user is an IRCop</li>
+--           <li><i>idle_time:</i>  amount of time the user has been idle</li>
+--           <li><i>channels:</i>   array containing the channels the user has
+--                                  joined</li>
+--           </ul>
+-- @param nick User to request WHOIS information about
 function whois(cb, nick)
     nick = nick:lower()
-    requestinfo.whois[nick] = {nick = nick}
+    requestinfo.whois[nick] = {}
     if not icallbacks.whois[nick] then
         icallbacks.whois[nick] = {cb}
         send("WHOIS", nick)
@@ -762,6 +820,14 @@ end
 -- }}}
 
 -- server_time {{{
+---
+-- Request the current time of the server you are connected to.
+-- @param cb Callback to call when the information is available. The single
+--           table parameter to this callback will contain the fields:
+--           <ul>
+--           <li><i>server:</i> the server which responded to the request</li>
+--           <li><i>time:</i>   the time reported by the server</li>
+--           </ul>
 function server_time(cb)
     -- apparently the optional server parameter isn't supported for servers
     -- which you are not directly connected to (freenode specific?)
@@ -777,36 +843,63 @@ end
 -- }}}
 
 -- ctcp commands {{{
--- ctcp_ping() - send a CTCP ping request {{{
+-- ctcp_ping {{{
+---
+-- Send a CTCP ping request.
+-- @param cb Callback to call when the information is available. The single
+--           table parameter to this callback will contain the fields:
+--           <ul>
+--           <li><i>nick:</i> the nick which responded to the request</li>
+--           <li><i>time:</i> the roundtrip ping time, in seconds</li>
+--           </ul>
+-- @param nick User to ping
 function ctcp_ping(cb, nick)
     nick = nick:lower()
     if not icallbacks.ctcp_ping[nick] then
         icallbacks.ctcp_ping[nick] = {cb}
-        say(nick, {"PING " .. os.time()})
+        say(nick, c("PING", os.time()))
     else
         table.insert(icallbacks.ctcp_ping[nick], cb)
     end
 end
 -- }}}
 
--- ctcp_time() - send a localtime request {{{
+-- ctcp_time {{{
+---
+-- Send a localtime request.
+-- @param cb Callback to call when the information is available. The single
+--           table parameter to this callback will contain the fields:
+--           <ul>
+--           <li><i>nick:</i> the nick which responded to the request</li>
+--           <li><i>time:</i> the localtime reported by the remote client</li>
+--           </ul>
+-- @param nick User to request the localtime from
 function ctcp_time(cb, nick)
     nick = nick:lower()
     if not icallbacks.ctcp_time[nick] then
         icallbacks.ctcp_time[nick] = {cb}
-        say(nick, {"TIME"})
+        say(nick, c("TIME"))
     else
         table.insert(icallbacks.ctcp_time[nick], cb)
     end
 end
 -- }}}
 
--- ctcp_version() - send a client version request {{{
+-- ctcp_version {{{
+---
+-- Send a client version request.
+-- @param cb Callback to call when the information is available. The single
+--           table parameter to this callback will contain the fields:
+--           <ul>
+--           <li><i>nick:</i>    the nick which responded to the request</li>
+--           <li><i>version:</i> the version reported by the remote client</li>
+--           </ul>
+-- @param nick User to request the client version from
 function ctcp_version(cb, nick)
     nick = nick:lower()
     if not icallbacks.ctcp_version[nick] then
         icallbacks.ctcp_version[nick] = {cb}
-        say(nick, {"VERSION"})
+        say(nick, c("VERSION"))
     else
         table.insert(icallbacks.ctcp_version[nick], cb)
     end
@@ -814,50 +907,83 @@ end
 -- }}}
 -- }}}
 
+-- callback functions {{{
+-- register_callback {{{
+---
+-- Register a user function to be called when a specific event occurs.
+-- @param name Name of the event
+-- @param fn   Function to call when the event occurs, or nil to clear the
+--             callback for this event
+-- @return Value of the original callback for this event (or nil if no previous
+--         callback had been set)
+function register_callback(name, fn)
+    local old_handler = user_handlers[name]
+    user_handlers[name] = fn
+    return old_handler
+end
+-- }}}
+-- }}}
+
 -- misc functions {{{
--- send() - send a raw irc command {{{
--- send takes a command and a variable number of arguments
--- if the argument is a string, it is sent literally
--- if the argument is a table, it is CTCP quoted
--- the last argument is preceded by a :
+-- send {{{
+-- TODO: CTCP quoting should be explicit, this table thing is quite ugly (if
+-- convenient)
+---
+-- Send a raw IRC command.
+-- @param command String containing the raw IRC command
+-- @param ...     Arguments to the command. Each argument is either a string or
+--                an array. Strings are sent literally, arrays are CTCP quoted
+--                as a group. The last argument (if it exists) is preceded by
+--                a : (so it may contain spaces).
 function send(command, ...)
     if not serverinfo.connected and not serverinfo.connecting then return end
     local message = command
     for i, v in base.ipairs({...}) do
-        local arg
-        -- passing a table in as an argument means to treat that table as a
-        -- CTCP command, so quote it appropriately
-        if base.type(v) == "string" then
-            arg = v
-        elseif base.type(v) == "table" then
-            arg = ctcp.ctcp_quote(table.concat(v, " "))
-        end
         if i == #{...} then
-            arg = ":" .. arg
+            v = ":" .. v
         end
-        message = message .. " " .. arg
+        message = message .. " " .. v
     end
-    message = ctcp.low_quote(message)
+    message = ctcp._low_quote(message)
     -- we just truncate for now. -2 to account for the \r\n
     message = message:sub(1, constants.IRC_MAX_MSG - 2)
-    irc_debug.message("SEND", message)
+    irc_debug._message("SEND", message)
     irc_sock:send(message .. "\r\n")
 end
 -- }}}
 
--- get_ip() - get the local ip address for the server connection {{{
+-- get_ip {{{
+---
+-- Get the local IP address for the server connection.
+-- @return A string representation of the local IP address that the IRC server
+--         connection is communicating on
 function get_ip()
-    return (irc_sock:getsockname())
+    return (ip or irc_sock:getsockname())
 end
 -- }}}
 
--- channels() - iterate over currently joined channels {{{
+-- set_ip {{{
+---
+-- Set the local IP manually (to allow for NAT workarounds)
+-- @param new_ip IP address to set
+function set_ip(new_ip)
+    ip = new_ip
+end
+-- }}}
+
+-- channels {{{
+-- TODO: @see doesn't currently work for files/modules
+---
+-- Iterate over currently joined channels.
+-- channels() is an iterator function for use in for loops.
+-- For example, <pre>for chan in irc.channels() do print(chan:name) end</pre>
+-- @see irc.channel
 function channels()
     return function(state, arg)
-               return misc.value_iter(state, arg,
-                                      function(v)
-                                          return v.join_complete
-                                      end)
+               return misc._value_iter(state, arg,
+                                       function(v)
+                                           return v.join_complete
+                                       end)
            end,
            serverinfo.channels,
            nil