From 20652e5e3101808a2451f4e2d936e38ae0dc184d Mon Sep 17 00:00:00 2001 From: mio Date: Thu, 28 Apr 2022 03:20:09 +0000 Subject: [PATCH] Add function to get a list of users in a channel --- itte.lua | 68 ++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 59 insertions(+), 9 deletions(-) diff --git a/itte.lua b/itte.lua index 464f89f..113f987 100644 --- a/itte.lua +++ b/itte.lua @@ -51,9 +51,10 @@ itte.config = { dc_svr = { "conn", "Connection to {{server}} closed." }, exit = { "conn", "Exiting ..." }, listen = { "listen" }, + names = { "names", "channel", "users" }, nickserv = { "auth_nickserv" }, - privmsg = { "sender", "recipient", "reply_to", "code", "code_params", - "body" }, + privmsg = { "privmsg", "sender", "recipient", "reply_to", "code", + "code_params", "body" }, redact = { "********" }, send = { "send" }, svrs_not_found = { "config", "Error: servers not found." }, @@ -217,6 +218,11 @@ function itte.get_commands(svr) check = "MODE", resp = "JOIN ", }, + names = { + -- Check and response are reversed + check = "353", + resp = "NAMES", + }, nick = { check = "NOTICE", resp = "NICK " .. svr.nick, @@ -551,6 +557,7 @@ itte.docs.traverse_channels = [[ (context_table, mode_str, channels_table) ]] function itte.traverse_channels(cxt, mode, chans) local channels = cxt.channels + cxt.chan_meta = {} if chans ~= nil then channels = chans end if mode == "join" then @@ -560,6 +567,7 @@ function itte.traverse_channels(cxt, mode, chans) for c = 1, #channels do if not util.has_key(cxt.channels, channels[c]) then table.insert(cxt.channels, channels[c]) + cxt.chan_meta[channels[c]:sub(2)] = {} end end @@ -570,6 +578,7 @@ function itte.traverse_channels(cxt, mode, chans) for c = 1, #channels do if util.has_key(cxt.channels, channels[c]) then table.remove(cxt.channels, util.find_key(cxt.channels, channels[c])) + cxt.chan_meta[channels[c]:sub(2)] = nil end end end @@ -620,18 +629,52 @@ function itte.parse_privmsg(cxt, str) body = itte.config.debugs.redact[1] code_params = itte.config.debugs.redact[1] end - util.debug("privmsg", "{ " .. - itte.config.debugs.privmsg[1] .. ": " .. msg.sender .. - ", " .. itte.config.debugs.privmsg[2] .. ": " .. msg.recipient .. - ", " .. itte.config.debugs.privmsg[3] .. ": " .. msg.reply_to .. - ", " .. itte.config.debugs.privmsg[4] .. ": " .. msg.code .. - ", " .. itte.config.debugs.privmsg[5] .. ": " .. code_params .. - ", " .. itte.config.debugs.privmsg[6] .. ": " .. body .. + util.debug(itte.config.debugs.privmsg[1], "{ " .. + itte.config.debugs.privmsg[2] .. ": " .. msg.sender .. + ", " .. itte.config.debugs.privmsg[3] .. ": " .. msg.recipient .. + ", " .. itte.config.debugs.privmsg[4] .. ": " .. msg.reply_to .. + ", " .. itte.config.debugs.privmsg[5] .. ": " .. msg.code .. + ", " .. itte.config.debugs.privmsg[6] .. ": " .. code_params .. + ", " .. itte.config.debugs.privmsg[7] .. ": " .. body .. " }", itte.config.debug) return msg end +itte.docs.parse_names = [[ (context_table, data_str) + Given the context table and a NAMES data string, parse the string and + return the channel name and a table of users. + ]] +function itte.parse_names(cxt, str) + -- Separator marks the start of the message body + body_sep, _ = string.find(str, ":", 2) + local channel = string.sub(str, string.find(str, "#"), + string.find(str, ":", 2) - 2) + local names = util.split_str(string.sub(str, body_sep + 1)) + util.debug(itte.config.debugs.names[1], + itte.config.debugs.names[2] .. ": " .. channel .. + ", " .. itte.config.debugs.names[3] .. ": { " .. table.concat(names, ", ") + .. " }", itte.config.debug) + return channel, names +end + + +itte.docs.get_users = [[ (context_table, channel_str) + Given the context table and a channel, return a table of users in the + channel. + ]] +function itte.get_users(cxt, str) + local chan = str + if string.find(chan, "#") == 1 then chan = str:sub(2) end + if not util.has_key(cxt.chan_meta, chan) then + do return end + end + -- Query server for the latest list of names + itte.send_command(cxt.con, cxt.cmds.names.resp .. " #" .. chan) + return cxt.chan_meta[chan].users +end + + -- --------------------------------------------------------------------------- -- Service code handlers -- --------------------------------------------------------------------------- @@ -840,6 +883,13 @@ function itte.listen(name, str) itte.send_command(cxt.con, string.gsub(str, cxt.cmds.ping.check, cxt.cmds.ping.resp)) + -- Update channel names list + elseif string.find(str, ":" .. cxt.host .. " " .. cxt.cmds.names.check) == 1 + then + util.debug(itte.config.debugs.listen[1], str, itte.config.debug) + local channel, names = itte.parse_names(cxt, str) + cxt.chan_meta[channel:sub(2)] = { users = names } + -- Respond to service code elseif util.is_substr(str, cxt.cmds.privmsg.check) then local msg = itte.parse_privmsg(cxt, str)