-- -- $Id$ -- $Revision$ -- $Date$ -- local name = "whois.lua" local desc = "Vetinari's whois.lua" -- AKA: print the whois info to the tab/window where it was called and not -- to the server tab/window... local version = "0.2" pending = {} request = {} function xchat_register() return name, desc, version end function xchat_init() xchat.hook_command("WHOIS", xchat.PRI_NORM, "handle_whois", nil, nil) xchat.hook_server("301", xchat.PRI_NORM, "handle_reply", nil) xchat.hook_server("311", xchat.PRI_NORM, "handle_reply", nil) xchat.hook_server("312", xchat.PRI_NORM, "handle_reply", nil) xchat.hook_server("313", xchat.PRI_NORM, "handle_reply", nil) xchat.hook_server("317", xchat.PRI_NORM, "handle_reply", nil) xchat.hook_server("318", xchat.PRI_NORM, "handle_reply", nil) xchat.hook_server("319", xchat.PRI_NORM, "handle_reply", nil) -- freenode: identified to services: xchat.hook_server("320", xchat.PRI_NORM, "handle_reply", nil) -- QNet: Authed As: xchat.hook_server("330", xchat.PRI_NORM, "handle_reply", nil) -- QNet: Real Host / IP xchat.hook_server("338", xchat.PRI_NORM, "handle_reply", nil) -- NOSUCHNICK xchat.hook_server("401", xchat.PRI_NORM, "handle_reply", nil) end function handle_whois(word, word_eol, data) local nick = word[2] local target = word[3] if target == nil then target = nick end local server = xchat.get_info("server") local ctx = xchat.get_context() table.insert(pending, { ["server"] = server, ["nick"] = nick, ["ctx"] = ctx, } ) xchat.command(string.format("QUOTE WHOIS %s %s", nick, target)) return xchat.EAT_ALL end function pending_ctx(nick, server) local ctx = table.foreach(pending, function(i, list) if xchat.nickcmp(list.nick, nick) == 0 and xchat.nickcmp(list.server, server) == 0 then return list.ctx end end ) return ctx end function idle_format(secs) local hrs = math.floor(secs/3600) secs = secs - 3600 * hrs local mins = math.floor(secs/60) secs = secs - 60 * mins return string.format("%02d:%02d:%02d", hrs, mins, secs) end numerics = { n301 = function(ctx, word, eol) xchat.emit_print("WhoIs Away Line", word[4], string.sub(eol[5], 2)) return xchat.EAT_XCHAT end, n307 = function(ctx, word, eol) xchat.emit_print("WhoIs Identified", word[4], string.sub(eol[5], 2)) return xchat.EAT_XCHAT end, n311 = function(ctx, word, eol) xchat.emit_print("WhoIs Name Line", word[4], word[5], word[6], string.sub(eol[8], 2)) return xchat.EAT_XCHAT end, n312 = function(ctx, word, eol) xchat.emit_print("WhoIs Server Line", word[4], eol[5]) return xchat.EAT_XCHAT end, n313 = function(ctx, word, eol) xchat.emit_print("WhoIs Channel/Oper Line", word[4], string.sub(eol[5], 2)) return xchat.EAT_XCHAT end, n317 = function(ctx, word, eol) if string.find(word[6], "^(%d+)$") then xchat.emit_print("WhoIs Idle Line with Signon", word[4], idle_format(tonumber(word[5])), os.date("%a %b %d %H:%M:%S", tonumber(word[6])) ) else xchat.emit_print("WhoIs Idle Line", word[4], idle_format(tonumber(word[5])) ) end return xchat.EAT_XCHAT end, n318 = function(ctx, word, eol) xchat.emit_print("WhoIs End", word[4]) local i = table.foreach(pending, function(n, list) if list.ctx == ctx then return n end end ) table.remove(pending, i) return xchat.EAT_XCHAT end, n319 = function(ctx, word, eol) xchat.emit_print("WhoIs Channel/Oper Line", word[4], string.sub(eol[5], 2)) return xchat.EAT_XCHAT end, n320 = function(ctx, word, eol) xchat.emit_print("WhoIs Identified", word[4], string.sub(eol[5], 2)) return xchat.EAT_XCHAT end, n330 = function(ctx, word, eol) xchat.emit_print("WhoIs Authenticated", word[4], string.sub(eol[6], 2), word[5]) return xchat.EAT_XCHAT end, n338 = function(ctx, word, eol) xchat.emit_print("WhoIs Real Host", word[4], word[5], word[6], string.sub(eol[7], 2)) return xchat.EAT_XCHAT end, n401 = function(ctx, word, eol) xchat.emit_print("WhoIs Special", word[4], string.sub(eol[5], 2)) return xchat.EAT_XCHAT end, } function handle_reply(word, eol, data) local func = string.format("n%s", word[2]) if type(numerics[func]) == "function" then local server = xchat.get_info("server") local ctx = pending_ctx(word[4], server) if ctx ~= nil then xchat.set_context(ctx) return numerics[func](ctx, word, eol) end end return xchat.EAT_NONE end -- END