2 -- Implementation of the main LuaIRC module
6 local constants = require 'irc.constants'
7 local ctcp = require 'irc.ctcp'
8 local c = ctcp._ctcp_quote
9 local irc_debug = require 'irc.debug'
10 local message = require 'irc.message'
11 local misc = require 'irc.misc'
12 local socket = require 'socket'
13 local os = require 'os'
14 local string = require 'string'
15 local table = require 'table'
19 -- LuaIRC - IRC framework written in Lua
24 _VERSION = 'LuaIRC 0.2'
28 local Channel = base.require 'irc.channel'
31 -- local variables {{{
45 local requestinfo = {whois = {}}
47 local ctcp_handlers = {}
48 local user_handlers = {}
54 TIMEOUT = 60 -- connection timeout
55 NETWORK = "localhost" -- default network
56 PORT = 6667 -- default port
57 NICK = "luabot" -- default nick
58 USERNAME = "LuaIRC" -- default username
59 REALNAME = "LuaIRC" -- default realname
60 DEBUG = false -- whether we want extra debug information
61 OUTFILE = nil -- file to send debug output to - nil is stdout
64 -- private functions {{{
66 local function main_loop_iter()
67 if #rsockets == 0 and #wsockets == 0 then return false end
68 local rready, wready, err = socket.select(rsockets, wsockets)
69 if err then irc_debug._err(err); return false; end
71 for _, sock in base.ipairs(rready) do
72 local cb = socket.protect(rcallbacks[sock])
73 local ret, err = cb(sock)
75 irc_debug._warn("socket error: " .. err)
76 _unregister_socket(sock, 'r')
80 for _, sock in base.ipairs(wready) do
81 local cb = socket.protect(wcallbacks[sock])
82 local ret, err = cb(sock)
84 irc_debug._warn("socket error: " .. err)
85 _unregister_socket(sock, 'w')
93 -- begin_main_loop {{{
94 local function begin_main_loop()
95 while main_loop_iter() do end
99 -- incoming_message {{{
100 local function incoming_message(sock)
101 local raw_msg = socket.try(sock:receive())
102 irc_debug._message("RECV", raw_msg)
103 local msg = message._parse(raw_msg)
104 misc._try_call_warn("Unhandled server message: " .. msg.command,
105 handlers["on_" .. msg.command:lower()],
106 (misc.parse_user(msg.from)), base.unpack(msg.args))
112 local function callback(name, ...)
113 misc._try_call(user_handlers[name], ...)
118 -- internal message handlers {{{
119 -- command handlers {{{
121 function handlers.on_nick(from, new_nick)
122 for chan in channels() do
123 chan:_change_nick(from, new_nick)
125 callback("nick_change", new_nick, from)
130 function handlers.on_join(from, chan)
131 base.assert(serverinfo.channels[chan],
132 "Received join message for unknown channel: " .. chan)
133 if serverinfo.channels[chan].join_complete then
134 serverinfo.channels[chan]:_add_user(from)
135 callback("join", serverinfo.channels[chan], from)
141 function handlers.on_part(from, chan, part_msg)
142 -- don't assert on chan here, since we get part messages for ourselves
143 -- after we remove the channel from the channel list
144 if not serverinfo.channels[chan] then return end
145 if serverinfo.channels[chan].join_complete then
146 serverinfo.channels[chan]:_remove_user(from)
147 callback("part", serverinfo.channels[chan], from, part_msg)
153 function handlers.on_mode(from, to, mode_string, ...)
154 local dir = mode_string:sub(1, 1)
155 mode_string = mode_string:sub(2)
158 if to:sub(1, 1) == "#" then
159 -- handle channel mode requests {{{
160 base.assert(serverinfo.channels[to],
161 "Received mode change for unknown channel: " .. to)
162 local chan = serverinfo.channels[to]
164 for i = 1, mode_string:len() do
165 local mode = mode_string:sub(i, i)
166 local target = args[ind]
167 -- channel modes other than op/voice will be implemented as
168 -- information request commands
169 if mode == "o" then -- channel op {{{
170 chan:_change_status(target, dir == "+", "o")
171 callback(({["+"] = "op", ["-"] = "deop"})[dir],
175 elseif mode == "v" then -- voice {{{
176 chan:_change_status(target, dir == "+", "v")
177 callback(({["+"] = "voice", ["-"] = "devoice"})[dir],
184 elseif from == to then
185 -- handle user mode requests {{{
186 -- TODO: make users more easily accessible so this is actually
187 -- reasonably possible
188 for i = 1, mode_string:len() do
189 local mode = mode_string:sub(i, i)
190 if mode == "i" then -- invisible {{{
192 elseif mode == "s" then -- server messages {{{
194 elseif mode == "w" then -- wallops messages {{{
196 elseif mode == "o" then -- ircop {{{
206 function handlers.on_topic(from, chan, new_topic)
207 base.assert(serverinfo.channels[chan],
208 "Received topic message for unknown channel: " .. chan)
209 serverinfo.channels[chan]._topic.text = new_topic
210 serverinfo.channels[chan]._topic.user = (misc.parse_user(from))
211 serverinfo.channels[chan]._topic.time = os.time()
212 if serverinfo.channels[chan].join_complete then
213 callback("topic_change", serverinfo.channels[chan])
219 function handlers.on_invite(from, to, chan)
220 callback("invite", from, chan)
225 function handlers.on_kick(from, chan, to)
226 base.assert(serverinfo.channels[chan],
227 "Received kick message for unknown channel: " .. chan)
228 if serverinfo.channels[chan].join_complete then
229 serverinfo.channels[chan]:_remove_user(to)
230 callback("kick", serverinfo.channels[chan], to, from)
236 function handlers.on_privmsg(from, to, msg)
237 local msgs = ctcp._ctcp_split(msg)
238 for _, v in base.ipairs(msgs) do
242 local words = misc._split(msg)
243 local received_command = words[1]
244 local cb = "on_" .. received_command:lower()
245 table.remove(words, 1)
246 -- not using try_call here because the ctcp specification requires
247 -- an error response to nonexistant commands
248 if base.type(ctcp_handlers[cb]) == "function" then
249 ctcp_handlers[cb](from, to, table.concat(words, " "))
251 notice(from, c("ERRMSG", received_command, ":Unknown query"))
255 -- normal message {{{
256 if to:sub(1, 1) == "#" then
257 base.assert(serverinfo.channels[to],
258 "Received channel msg from unknown channel: " .. to)
259 callback("channel_msg", serverinfo.channels[to], from, msg)
261 callback("private_msg", from, msg)
270 function handlers.on_notice(from, to, msg)
271 local msgs = ctcp._ctcp_split(msg)
272 for _, v in base.ipairs(msgs) do
276 local words = misc._split(msg)
277 local command = words[1]:lower()
278 table.remove(words, 1)
279 misc._try_call_warn("Unknown CTCP message: " .. command,
280 ctcp_handlers["on_rpl_"..command], from, to,
281 table.concat(words, ' '))
284 -- normal message {{{
285 if to:sub(1, 1) == "#" then
286 base.assert(serverinfo.channels[to],
287 "Received channel msg from unknown channel: " .. to)
288 callback("channel_notice", serverinfo.channels[to], from, msg)
290 callback("private_notice", from, msg)
299 function handlers.on_quit(from, quit_msg)
300 for name, chan in base.pairs(serverinfo.channels) do
301 chan:_remove_user(from)
303 callback("quit", from, quit_msg)
308 -- respond to server pings to make sure it knows we are alive
309 function handlers.on_ping(from, respond_to)
310 send("PONG", respond_to)
315 -- server replies {{{
317 -- catch topic changes
318 function handlers.on_rpl_topic(from, chan, topic)
319 base.assert(serverinfo.channels[chan],
320 "Received topic information about unknown channel: " .. chan)
321 serverinfo.channels[chan]._topic.text = topic
325 -- on_rpl_notopic {{{
326 function handlers.on_rpl_notopic(from, chan)
327 base.assert(serverinfo.channels[chan],
328 "Received topic information about unknown channel: " .. chan)
329 serverinfo.channels[chan]._topic.text = ""
333 -- on_rpl_topicdate {{{
334 -- "topic was set by <user> at <time>"
335 function handlers.on_rpl_topicdate(from, chan, user, time)
336 base.assert(serverinfo.channels[chan],
337 "Received topic information about unknown channel: " .. chan)
338 serverinfo.channels[chan]._topic.user = user
339 serverinfo.channels[chan]._topic.time = base.tonumber(time)
343 -- on_rpl_namreply {{{
344 -- handles a NAMES reply
345 function handlers.on_rpl_namreply(from, chanmode, chan, userlist)
346 base.assert(serverinfo.channels[chan],
347 "Received user information about unknown channel: " .. chan)
348 serverinfo.channels[chan]._chanmode = constants.chanmodes[chanmode]
349 local users = misc._split(userlist)
350 for k,v in base.ipairs(users) do
351 if v:sub(1, 1) == "@" or v:sub(1, 1) == "+" then
352 local nick = v:sub(2)
353 serverinfo.channels[chan]:_add_user(nick, v:sub(1, 1))
355 serverinfo.channels[chan]:_add_user(v)
361 -- on_rpl_endofnames {{{
362 -- when we get this message, the channel join has completed, so call the
364 function handlers.on_rpl_endofnames(from, chan)
365 base.assert(serverinfo.channels[chan],
366 "Received user information about unknown channel: " .. chan)
367 if not serverinfo.channels[chan].join_complete then
368 callback("me_join", serverinfo.channels[chan])
369 serverinfo.channels[chan].join_complete = true
374 -- on_rpl_welcome {{{
375 function handlers.on_rpl_welcome(from)
384 -- on_rpl_yourhost {{{
385 function handlers.on_rpl_yourhost(from, msg)
386 serverinfo.host = from
390 -- on_rpl_motdstart {{{
391 function handlers.on_rpl_motdstart(from)
397 function handlers.on_rpl_motd(from, motd)
398 serverinfo.motd = (serverinfo.motd or "") .. motd .. "\n"
402 -- on_rpl_endofmotd {{{
403 function handlers.on_rpl_endofmotd(from)
404 if not serverinfo.connected then
405 serverinfo.connected = true
406 serverinfo.connecting = false
412 -- on_rpl_whoisuser {{{
413 function handlers.on_rpl_whoisuser(from, nick, user, host, star, realname)
414 local lnick = nick:lower()
415 requestinfo.whois[lnick].nick = nick
416 requestinfo.whois[lnick].user = user
417 requestinfo.whois[lnick].host = host
418 requestinfo.whois[lnick].realname = realname
422 -- on_rpl_whoischannels {{{
423 function handlers.on_rpl_whoischannels(from, nick, channel_list)
425 if not requestinfo.whois[nick].channels then
426 requestinfo.whois[nick].channels = {}
428 for _, channel in base.ipairs(misc._split(channel_list)) do
429 table.insert(requestinfo.whois[nick].channels, channel)
434 -- on_rpl_whoisserver {{{
435 function handlers.on_rpl_whoisserver(from, nick, server, serverinfo)
437 requestinfo.whois[nick].server = server
438 requestinfo.whois[nick].serverinfo = serverinfo
443 function handlers.on_rpl_away(from, nick, away_msg)
445 if requestinfo.whois[nick] then
446 requestinfo.whois[nick].away_msg = away_msg
451 -- on_rpl_whoisoperator {{{
452 function handlers.on_rpl_whoisoperator(from, nick)
453 requestinfo.whois[nick:lower()].is_oper = true
457 -- on_rpl_whoisidle {{{
458 function handlers.on_rpl_whoisidle(from, nick, idle_seconds)
459 requestinfo.whois[nick:lower()].idle_time = idle_seconds
463 -- on_rpl_endofwhois {{{
464 function handlers.on_rpl_endofwhois(from, nick)
466 local cb = table.remove(icallbacks.whois[nick], 1)
467 cb(requestinfo.whois[nick])
468 requestinfo.whois[nick] = nil
469 if #icallbacks.whois[nick] > 0 then send("WHOIS", nick)
470 else icallbacks.whois[nick] = nil
475 -- on_rpl_version {{{
476 function handlers.on_rpl_version(from, version, server, comments)
477 local cb = table.remove(icallbacks.serverversion[server], 1)
478 cb({version = version, server = server, comments = comments})
479 if #icallbacks.serverversion[server] > 0 then send("VERSION", server)
480 else icallbacks.serverversion[server] = nil
486 function on_rpl_time(from, server, time)
487 local cb = table.remove(icallbacks.servertime[server], 1)
488 cb({time = time, server = server})
489 if #icallbacks.servertime[server] > 0 then send("TIME", server)
490 else icallbacks.servertime[server] = nil
499 function ctcp_handlers.on_action(from, to, message)
500 if to:sub(1, 1) == "#" then
501 base.assert(serverinfo.channels[to],
502 "Received channel msg from unknown channel: " .. to)
503 callback("channel_act", serverinfo.channels[to], from, message)
505 callback("private_act", from, message)
511 -- TODO: can we not have this handler be registered unless the dcc module is
513 function ctcp_handlers.on_dcc(from, to, message)
514 local type, argument, address, port, size = base.unpack(misc._split(message, " ", nil, '"', '"'))
515 if type == "SEND" then
516 if callback("dcc", from, to, argument, address, port, size) then
517 dcc._accept(argument, address, port)
519 elseif type == "CHAT" then
520 -- TODO: implement this? do people ever use this?
526 function ctcp_handlers.on_version(from, to)
527 notice(from, c("VERSION", _VERSION .. " running under " .. base._VERSION .. " with " .. socket._VERSION))
532 function ctcp_handlers.on_errmsg(from, to, message)
533 notice(from, c("ERRMSG", message, ":No error has occurred"))
538 function ctcp_handlers.on_ping(from, to, timestamp)
539 notice(from, c("PING", timestamp))
544 function ctcp_handlers.on_time(from, to)
545 notice(from, c("TIME", os.date()))
552 -- actions are handled the same, notice or not
553 ctcp_handlers.on_rpl_action = ctcp_handlers.on_action
556 -- on_rpl_version {{{
557 function ctcp_handlers.on_rpl_version(from, to, version)
558 local lfrom = from:lower()
559 local cb = table.remove(icallbacks.ctcp_version[lfrom], 1)
560 cb({version = version, nick = from})
561 if #icallbacks.ctcp_version[lfrom] > 0 then say(from, c("VERSION"))
562 else icallbacks.ctcp_version[lfrom] = nil
568 function ctcp_handlers.on_rpl_errmsg(from, to, message)
569 try_call(on_ctcp_error, from, to, message)
574 function ctcp_handlers.on_rpl_ping(from, to, timestamp)
575 local lfrom = from:lower()
576 local cb = table.remove(icallbacks.ctcp_ping[lfrom], 1)
577 cb({time = os.time() - timestamp, nick = from})
578 if #icallbacks.ctcp_ping[lfrom] > 0 then say(from, c("PING", os.time()))
579 else icallbacks.ctcp_ping[lfrom] = nil
585 function ctcp_handlers.on_rpl_time(from, to, time)
586 local lfrom = from:lower()
587 local cb = table.remove(icallbacks.ctcp_time[lfrom], 1)
588 cb({time = time, nick = from})
589 if #icallbacks.ctcp_time[lfrom] > 0 then say(from, c("TIME"))
590 else icallbacks.ctcp_time[lfrom] = nil
598 -- module functions {{{
599 -- socket handling functions {{{
600 -- _register_socket {{{
602 -- Register a socket to listen on.
603 -- @param sock LuaSocket socket object
604 -- @param mode 'r' if the socket is for reading, 'w' if for writing
605 -- @param cb Callback to call when the socket is ready for reading/writing.
606 -- It will be called with the socket as the single argument.
607 function _register_socket(sock, mode, cb)
616 base.assert(not cbs[sock], "socket already registered")
617 table.insert(socks, sock)
622 -- _unregister_socket {{{
624 -- Remove a previously registered socket.
625 -- @param sock Socket to unregister
626 -- @param mode 'r' to unregister it for reading, 'w' for writing
627 function _unregister_socket(sock, mode)
636 for i, v in base.ipairs(socks) do
637 if v == sock then table.remove(socks, i); break; end
645 -- public functions {{{
646 -- server commands {{{
649 -- Start a connection to the irc server.
650 -- @param args Table of named arguments containing connection parameters.
651 -- Defaults are the all-caps versions of these parameters given
652 -- at the top of the file, and are overridable by setting them
653 -- as well, i.e. <pre>irc.NETWORK = irc.freenode.net</pre>
654 -- Possible options are:
656 -- <li><i>network:</i> address of the irc network to connect to
657 -- (default: 'localhost')</li>
658 -- <li><i>port:</i> port to connect to
659 -- (default: '6667')</li>
660 -- <li><i>pass:</i> irc server password
661 -- (default: don't send)</li>
662 -- <li><i>nick:</i> nickname to connect as
663 -- (default: 'luabot')</li>
664 -- <li><i>username:</i> username to connect with
665 -- (default: 'LuaIRC')</li>
666 -- <li><i>realname:</i> realname to connect with
667 -- (default: 'LuaIRC')</li>
668 -- <li><i>timeout:</i> amount of time in seconds to wait before
669 -- dropping an idle connection
670 -- (default: '60')</li>
672 function connect(args)
673 local network = args.network or NETWORK
674 local port = args.port or PORT
675 local nick = args.nick or NICK
676 local username = args.username or USERNAME
677 local realname = args.realname or REALNAME
678 local timeout = args.timeout or TIMEOUT
679 serverinfo.connecting = true
680 if OUTFILE then irc_debug.set_output(OUTFILE) end
681 if DEBUG then irc_debug.enable() end
682 irc_sock = base.assert(socket.connect(network, port))
683 irc_sock:settimeout(timeout)
684 _register_socket(irc_sock, 'r', incoming_message)
685 if args.pass then send("PASS", args.pass) end
687 send("USER", username, get_ip(), network, realname)
694 -- Close the connection to the irc server.
695 -- @param message Quit message (optional, defaults to 'Leaving')
696 function quit(message)
697 message = message or "Leaving"
698 send("QUIT", message)
699 serverinfo.connected = false
706 -- @param channel Channel to join
707 function join(channel)
708 if not channel then return end
709 serverinfo.channels[channel] = Channel.new(channel)
710 send("JOIN", channel)
717 -- @param channel Channel to leave
718 function part(channel)
719 if not channel then return end
720 serverinfo.channels[channel] = nil
721 send("PART", channel)
727 -- Send a message to a user or channel.
728 -- @param name User or channel to send the message to
729 -- @param message Message to send
730 function say(name, message)
731 if not name then return end
732 message = message or ""
733 send("PRIVMSG", name, message)
739 -- Send a notice to a user or channel.
740 -- @param name User or channel to send the notice to
741 -- @param message Message to send
742 function notice(name, message)
743 if not name then return end
744 message = message or ""
745 send("NOTICE", name, message)
751 -- Perform a /me action.
752 -- @param name User or channel to send the action to
753 -- @param action Action to send
754 function act(name, action)
755 if not name then return end
756 action = action or ""
757 send("PRIVMSG", name, c("ACTION", action))
762 -- information requests {{{
763 -- server_version {{{
765 -- Request the version of the IRC server you are currently connected to.
766 -- @param cb Callback to call when the information is available. The single
767 -- table parameter to this callback will contain the fields:
769 -- <li><i>server:</i> the server which responded to the request</li>
770 -- <li><i>version:</i> the server version</li>
771 -- <li><i>comments:</i> other data provided by the server</li>
773 function server_version(cb)
774 -- apparently the optional server parameter isn't supported for servers
775 -- which you are not directly connected to (freenode specific?)
776 local server = serverinfo.host
777 if not icallbacks.serverversion[server] then
778 icallbacks.serverversion[server] = {cb}
779 send("VERSION", server)
781 table.insert(icallbacks.serverversion[server], cb)
787 -- TODO: allow server parameter (to get user idle time)
789 -- Request WHOIS information about a given user.
790 -- @param cb Callback to call when the information is available. The single
791 -- table parameter to this callback may contain any or all of the
794 -- <li><i>nick:</i> the nick that was passed to this function
795 -- (this field will always be here)</li>
796 -- <li><i>user:</i> the IRC username of the user</li>
797 -- <li><i>host:</i> the user's hostname</li>
798 -- <li><i>realname:</i> the IRC realname of the user</li>
799 -- <li><i>server:</i> the IRC server the user is connected to</li>
800 -- <li><i>serverinfo:</i> arbitrary information about the above
802 -- <li><i>awaymsg:</i> set to the user's away message if they are
804 -- <li><i>is_oper:</i> true if the user is an IRCop</li>
805 -- <li><i>idle_time:</i> amount of time the user has been idle</li>
806 -- <li><i>channels:</i> array containing the channels the user has
809 -- @param nick User to request WHOIS information about
810 function whois(cb, nick)
812 requestinfo.whois[nick] = {}
813 if not icallbacks.whois[nick] then
814 icallbacks.whois[nick] = {cb}
817 table.insert(icallbacks.whois[nick], cb)
824 -- Request the current time of the server you are connected to.
825 -- @param cb Callback to call when the information is available. The single
826 -- table parameter to this callback will contain the fields:
828 -- <li><i>server:</i> the server which responded to the request</li>
829 -- <li><i>time:</i> the time reported by the server</li>
831 function server_time(cb)
832 -- apparently the optional server parameter isn't supported for servers
833 -- which you are not directly connected to (freenode specific?)
834 local server = serverinfo.host
835 if not icallbacks.servertime[server] then
836 icallbacks.servertime[server] = {cb}
839 table.insert(icallbacks.servertime[server], cb)
848 -- Send a CTCP ping request.
849 -- @param cb Callback to call when the information is available. The single
850 -- table parameter to this callback will contain the fields:
852 -- <li><i>nick:</i> the nick which responded to the request</li>
853 -- <li><i>time:</i> the roundtrip ping time, in seconds</li>
855 -- @param nick User to ping
856 function ctcp_ping(cb, nick)
858 if not icallbacks.ctcp_ping[nick] then
859 icallbacks.ctcp_ping[nick] = {cb}
860 say(nick, c("PING", os.time()))
862 table.insert(icallbacks.ctcp_ping[nick], cb)
869 -- Send a localtime request.
870 -- @param cb Callback to call when the information is available. The single
871 -- table parameter to this callback will contain the fields:
873 -- <li><i>nick:</i> the nick which responded to the request</li>
874 -- <li><i>time:</i> the localtime reported by the remote client</li>
876 -- @param nick User to request the localtime from
877 function ctcp_time(cb, nick)
879 if not icallbacks.ctcp_time[nick] then
880 icallbacks.ctcp_time[nick] = {cb}
883 table.insert(icallbacks.ctcp_time[nick], cb)
890 -- Send a client version request.
891 -- @param cb Callback to call when the information is available. The single
892 -- table parameter to this callback will contain the fields:
894 -- <li><i>nick:</i> the nick which responded to the request</li>
895 -- <li><i>version:</i> the version reported by the remote client</li>
897 -- @param nick User to request the client version from
898 function ctcp_version(cb, nick)
900 if not icallbacks.ctcp_version[nick] then
901 icallbacks.ctcp_version[nick] = {cb}
902 say(nick, c("VERSION"))
904 table.insert(icallbacks.ctcp_version[nick], cb)
910 -- callback functions {{{
911 -- register_callback {{{
913 -- Register a user function to be called when a specific event occurs.
914 -- @param name Name of the event
915 -- @param fn Function to call when the event occurs, or nil to clear the
916 -- callback for this event
917 -- @return Value of the original callback for this event (or nil if no previous
918 -- callback had been set)
919 function register_callback(name, fn)
920 local old_handler = user_handlers[name]
921 user_handlers[name] = fn
927 -- misc functions {{{
929 -- TODO: CTCP quoting should be explicit, this table thing is quite ugly (if
932 -- Send a raw IRC command.
933 -- @param command String containing the raw IRC command
934 -- @param ... Arguments to the command. Each argument is either a string or
935 -- an array. Strings are sent literally, arrays are CTCP quoted
936 -- as a group. The last argument (if it exists) is preceded by
937 -- a : (so it may contain spaces).
938 function send(command, ...)
939 if not serverinfo.connected and not serverinfo.connecting then return end
940 local message = command
941 for i, v in base.ipairs({...}) do
945 message = message .. " " .. v
947 message = ctcp._low_quote(message)
948 -- we just truncate for now. -2 to account for the \r\n
949 message = message:sub(1, constants.IRC_MAX_MSG - 2)
950 irc_debug._message("SEND", message)
951 irc_sock:send(message .. "\r\n")
957 -- Get the local IP address for the server connection.
958 -- @return A string representation of the local IP address that the IRC server
959 -- connection is communicating on
961 return (ip or irc_sock:getsockname())
967 -- Set the local IP manually (to allow for NAT workarounds)
968 -- @param new_ip IP address to set
969 function set_ip(new_ip)
975 -- TODO: @see doesn't currently work for files/modules
977 -- Iterate over currently joined channels.
978 -- channels() is an iterator function for use in for loops.
979 -- For example, <pre>for chan in irc.channels() do print(chan:name) end</pre>
982 return function(state, arg)
983 return misc._value_iter(state, arg,
985 return v.join_complete