From 848e4dd784555924625e6a4bce90bd7d7c22a14f Mon Sep 17 00:00:00 2001 From: Egor Date: Tue, 23 Jan 2024 13:51:13 +0200 Subject: [PATCH] move all of ctcp probing into ctcp.lua --- lua/ctcp.lua | 37 ++++++++++++++++++++++++++++++------- lua/init.lua | 3 +++ src/admin.c | 11 +++++++++-- src/common.h | 2 -- src/irc.c | 37 +++++++------------------------------ src/net.c | 2 -- 6 files changed, 49 insertions(+), 43 deletions(-) diff --git a/lua/ctcp.lua b/lua/ctcp.lua index df81634..b033d3c 100644 --- a/lua/ctcp.lua +++ b/lua/ctcp.lua @@ -1,18 +1,41 @@ -- ctcp probe module +local servicename = "cia!cia@cia.gov" function commands.refreshctcp(client, source, command, args) local flags = args[2] and tonumber(args[2]) or 3 if args[1] and args[1] ~= "*" then - if flags&1 ~= 0 then client_privmsg(client_map[args[1]], "\1VERSION\1", "cia!cia@cia.gov") end - if flags&2 ~= 0 then client_privmsg(client_map[args[1]], "\1TIME\1", "cia!cia@cia.gov") end + if flags&1 ~= 0 then client_privmsg(client_map[args[1]], "\1VERSION\1", servicename) end + if flags&2 ~= 0 then client_privmsg(client_map[args[1]], "\1TIME\1", servicename) end else for k, v in pairs(client_map) do - if flags&1 ~= 0 then client_privmsg(v, "\1VERSION\1", "cia!cia@cia.gov") end - if flags&2 ~= 0 then client_privmsg(v, "\1TIME\1", "cia!cia@cia.gov") end + if flags&1 ~= 0 then client_privmsg(v, "\1VERSION\1", servicename) end + if flags&2 ~= 0 then client_privmsg(v, "\1TIME\1", servicename) end end end end hook_add("newuser", "ctcp_probe", function(hook, cl1, cl2, chan, txt) - client_privmsg(cl1, "\1VERSION\1", "cia!cia@cia.gov") - -- TODO: a delay mechanism to avoid getting ratelimited - client_privmsg(cl1, "\1TIME\1", "cia!cia@cia.gov") + client_privmsg(cl1, "\1VERSION\1", servicename) + client_data[cl1.key].ctcp_probe_wait = true +end) +hook_add("noticetoservice", "ctcp_logger", function(hook, cl1, cl2, chan, txt) + if txt:sub(1, 9) == "\1VERSION " then + client_data[cl1.key].ctcp_version = txt:sub(10, (txt:find("\1", 10, true) or 0)-1) + if client_data[cl1.key].ctcp_probe_wait then + client_data[cl1.key].ctcp_probe_wait = nil + client_privmsg(cl1, "\1TIME\1", servicename) + end + elseif txt:sub(1, 6) == "\1TIME " then + client_data[cl1.key].ctcp_time = string.format("%s (received on %s UTC)", + txt:sub(7, (txt:find("\1", 7, true) or 0)-1), + os.date("!%Y-%m-%d %H:%M:%S")) + end +end) +hook_add("whois", "ctcp_whois", function(hook, cl1, cl2, chan, txt) + if cl1.umode & 2 ~= 0 then -- UMODE_OPER + if client_data[cl2.key].ctcp_version then + rpl_whoisspecial(cl1, cl2, "CTCP VERSION: ".. client_data[cl2.key].ctcp_version) + end + if client_data[cl2.key].ctcp_time then + rpl_whoisspecial(cl1, cl2, "CTCP TIME: ".. client_data[cl2.key].ctcp_time) + end + end end) diff --git a/lua/init.lua b/lua/init.lua index 053f6b6..fdb3289 100644 --- a/lua/init.lua +++ b/lua/init.lua @@ -47,6 +47,9 @@ hook_setfold("newuser", fold_sendtox) hook_setfold("nickadd", fold_sendtox) hook_setfold("nickdel", fold_sendtox) hook_setfold("nickchange", fold_sendtox) +hook_setfold("privmsgtoservice", fold_sendtox) +hook_setfold("noticetoservice", fold_sendtox) +hook_setfold("whois", fold_sendtox) if not client_data then client_data = {} end diff --git a/src/admin.c b/src/admin.c index cd6da9e..77dcd19 100644 --- a/src/admin.c +++ b/src/admin.c @@ -139,8 +139,6 @@ static int client_index(lua_State *L) string_getter(cl, away); string_getter(cl, nick_reg); /* string_getter(cl, pass_reg); */ - string_getter(cl, ctcp_version); - string_getter(cl, ctcp_time); string_getter(cl, peername); integer_getter(cl, peerport); /* buf/bufLen/wbuf/wbufLen */ @@ -271,6 +269,13 @@ static int fn_client_chghost(lua_State *L) client_chghost(cl, newusername, newhostname); return 0; } +static int fn_rpl_whoisspecial(lua_State *L) { + struct client_data *cl1 = *(struct client_data **)luaL_checkudata(L, 1, "client"); + struct client_data *cl2 = *(struct client_data **)luaL_checkudata(L, 2, "client"); + const char *text = luaL_checkstring(L, 3); + rpl_whoisspecial(cl1, cl2, "", text), sendbuf(cl1); + return 0; +} void admin_init(void) { @@ -332,6 +337,8 @@ void admin_init(void) lua_setglobal(L, "channel_part"); lua_pushcfunction(L, fn_client_chghost); lua_setglobal(L, "client_chghost"); + lua_pushcfunction(L, fn_rpl_whoisspecial); + lua_setglobal(L, "rpl_whoisspecial"); } void admin_fini(void) { diff --git a/src/common.h b/src/common.h index f2c73c9..1c59cba 100644 --- a/src/common.h +++ b/src/common.h @@ -46,8 +46,6 @@ struct client_data { char *away; char *nick_reg; char *pass_reg; - char *ctcp_version; - char *ctcp_time; char *peername; unsigned short peerport; char buf[2048]; diff --git a/src/irc.c b/src/irc.c index a6d10f7..66f296d 100644 --- a/src/irc.c +++ b/src/irc.c @@ -537,32 +537,10 @@ int processPacket(struct client_data *client, char *source, char *command, int a if (argc < 2) return err_needmoreparams(client, command), sendbuf(client), 0; client->lastactivity = get_time(); - if (command[0] == 'N' && !strcmp(args[0], "cia")) { - // CTCP response logger - if (!strncmp(args[1], "\1VERSION ", 9)) { - args[1] += 9; - char *p = strchr(args[1], '\1'); - if (p) - *p = '\0'; - free(client->ctcp_version); - client->ctcp_version = mystrdup(args[1]); - if (!client->ctcp_version) - abort(); - } - if (!strncmp(args[1], "\1TIME ", 6)) { - args[1] += 6; - char *p = strchr(args[1], '\1'); - if (p) - *p = '\0'; - free(client->ctcp_time); - time_t current = get_time(); - char buf[20]; - strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", gmtime(¤t)); - client->ctcp_time = malloc(strlen(args[1])+sizeof(" (received on UTC)")+sizeof(buf)); - if (!client->ctcp_time) - abort(); - sprintf(client->ctcp_time, "%s (received on %s UTC)", args[1], buf); - } + if (!strcmp(args[0], "cia")) { +#ifdef HAS_LUA + admin_hook(command[0] == 'P' ? "privmsgtoservice" : "noticetoservice", client, 0, 0, args[1]); +#endif return 0; } (command[0] == 'P' ? msg_privmsg : msg_notice)(client, args[0], args[1]); @@ -1084,13 +1062,12 @@ int processPacket(struct client_data *client, char *source, char *command, int a rpl_whoisaccount(client, cl), sendbuf(client); if (client == cl || client->umode & UMODE_OPER) rpl_whoisactually(client, cl), sendbuf(client); - if (cl->ctcp_version && client->umode & UMODE_OPER) - rpl_whoisspecial(client, cl, "CTCP VERSION: ", cl->ctcp_version), sendbuf(client); - if (cl->ctcp_time && client->umode & UMODE_OPER) - rpl_whoisspecial(client, cl, "CTCP TIME: ", cl->ctcp_time), sendbuf(client); rpl_whoismodes(client, cl), sendbuf(client); if (cl->umode & UMODE_BOT) rpl_whoisbot(client, cl), sendbuf(client); +#ifdef HAS_LUA + admin_hook("whois", client, cl, 0, 0); +#endif rpl_endofwhois(client, cl), sendbuf(client); } else { err_nosuchnick(client, nick); diff --git a/src/net.c b/src/net.c index f8311ba..1556060 100644 --- a/src/net.c +++ b/src/net.c @@ -135,8 +135,6 @@ void killConnectionPart2(struct client_data *cl) if (cl->pass_reg) memset(cl->pass_reg, 0, strlen(cl->pass_reg)); free(cl->pass_reg); - free(cl->ctcp_version); - free(cl->ctcp_time); free(cl->peername); free(cl); } -- 2.56.0