From 4e17f85fe6fac034a212c56ceb6a54f62d947da1 Mon Sep 17 00:00:00 2001 From: Bram Matthys Date: Sun, 19 May 2019 09:24:38 +0200 Subject: [PATCH] 1) New function new_message() which should be called when a new message is sent, or at least for channel events. 2) Move adding of msgid/time/account tags to modules, which is their proper place. --- include/h.h | 7 ++- include/modules.h | 5 +- src/modules/cap/account-tag.c | 23 ++++++++- src/modules/cap/msgid.c | 39 ++++++++++++++- src/modules/cap/server-time.c | 33 ++++++++++++- src/modules/m_message.c | 4 +- src/s_misc.c | 90 ++++------------------------------- 7 files changed, 108 insertions(+), 93 deletions(-) diff --git a/include/h.h b/include/h.h index 3d385f6a3..6212da91e 100644 --- a/include/h.h +++ b/include/h.h @@ -828,8 +828,7 @@ extern long ClientCapabilityBit(const char *token); extern int user_ready_for_register(aClient *sptr); extern void SetCapability(aClient *acptr, const char *token); extern void ClearCapability(aClient *acptr, const char *token); -extern MessageTag *mtag_generate_msgid(void); +extern void new_message(aClient *sender, MessageTag *recv_mtags, MessageTag **mtag_list); +extern MessageTag *find_mtag(MessageTag *mtags, const char *token); +extern MessageTag *duplicate_mtag(MessageTag *mtag); extern void free_mtags(MessageTag *m); -extern void mtag_add_or_inherit_msgid(MessageTag *recv_mtags, MessageTag **mtag_list); -extern void mtag_add_or_inherit_account(MessageTag *recv_mtags, MessageTag **mtag_list, aClient *acptr); -extern void mtag_add_or_inherit_time(MessageTag *recv_mtags, MessageTag **mtag_list); diff --git a/include/modules.h b/include/modules.h index b9b89f90d..ec0df5365 100644 --- a/include/modules.h +++ b/include/modules.h @@ -871,6 +871,7 @@ extern char *moddata_client_get(aClient *acptr, char *varname); #define HOOKTYPE_WELCOME 97 #define HOOKTYPE_PRE_COMMAND 98 #define HOOKTYPE_POST_COMMAND 99 +#define HOOKTYPE_NEW_MESSAGE 100 /* Adding a new hook here? * 1) Add the #define HOOKTYPE_.... with a new number @@ -977,6 +978,7 @@ int hooktype_find_tkline_match(aClient *sptr, aTKline *tk); int hooktype_welcome(aClient *sptr, int after_numeric); int hooktype_pre_command(aClient *from, MessageTag *mtags, char *buf); int hooktype_post_command(aClient *from, MessageTag *mtags, char *buf); +void hooktype_new_message(aClient *sender, MessageTag *recv_mtags, MessageTag **mtag_list); #ifdef GCC_TYPECHECKING #define ValidateHook(validatefunc, func) __builtin_types_compatible_p(__typeof__(func), __typeof__(validatefunc)) @@ -1080,7 +1082,8 @@ _UNREAL_ERROR(_hook_error_incompatible, "Incompatible hook function. Check argum ((hooktype == HOOKTYPE_FIND_TKLINE_MATCH) && !ValidateHook(hooktype_find_tkline_match, func)) || \ ((hooktype == HOOKTYPE_WELCOME) && !ValidateHook(hooktype_welcome, func)) || \ ((hooktype == HOOKTYPE_PRE_COMMAND) && !ValidateHook(hooktype_pre_command, func)) || \ - ((hooktype == HOOKTYPE_POST_COMMAND) && !ValidateHook(hooktype_post_command, func)) ) \ + ((hooktype == HOOKTYPE_POST_COMMAND) && !ValidateHook(hooktype_post_command, func)) || \ + ((hooktype == HOOKTYPE_NEW_MESSAGE) && !ValidateHook(hooktype_new_message, func)) ) \ _hook_error_incompatible(); #endif /* GCC_TYPECHECKING */ diff --git a/src/modules/cap/account-tag.c b/src/modules/cap/account-tag.c index fac49ca3d..f64fba421 100644 --- a/src/modules/cap/account-tag.c +++ b/src/modules/cap/account-tag.c @@ -35,6 +35,7 @@ ModuleHeader MOD_HEADER(account-tag) long CAP_ACCOUNT_TAG = 0L; int account_tag_mtag_is_ok(aClient *acptr, char *name, char *value); +void mtag_add_or_inherit_account(aClient *acptr, MessageTag *recv_mtags, MessageTag **mtag_list); MOD_INIT(account-tag) { @@ -54,6 +55,8 @@ MOD_INIT(account-tag) mtag.clicap_handler = c; MessageTagHandlerAdd(modinfo->handle, &mtag); + HookAddVoid(modinfo->handle, HOOKTYPE_NEW_MESSAGE, 0, mtag_add_or_inherit_account); + return MOD_SUCCESS; } @@ -80,4 +83,22 @@ int account_tag_mtag_is_ok(aClient *acptr, char *name, char *value) return 0; } -// TODO: move from m_message (and elsewhere?) to here, some hook call or something +void mtag_add_or_inherit_account(aClient *acptr, MessageTag *recv_mtags, MessageTag **mtag_list) +{ + MessageTag *m = find_mtag(recv_mtags, "account"); + if (m) + { + m = duplicate_mtag(m); + } else + { + if (acptr && acptr->user && + (*acptr->user->svid != '*') && !isdigit(*acptr->user->svid)) + { + m = MyMallocEx(sizeof(MessageTag)); + m->name = strdup("account"); + m->value = strdup(acptr->user->svid); + } + } + if (m) + AddListItem(m, *mtag_list); +} diff --git a/src/modules/cap/msgid.c b/src/modules/cap/msgid.c index 2f2734cb5..859852285 100644 --- a/src/modules/cap/msgid.c +++ b/src/modules/cap/msgid.c @@ -35,6 +35,7 @@ ModuleHeader MOD_HEADER(msgid) long CAP_ACCOUNT_TAG = 0L; int msgid_mtag_is_ok(aClient *acptr, char *name, char *value); +void mtag_add_or_inherit_msgid(aClient *sender, MessageTag *recv_mtags, MessageTag **mtag_list); MOD_INIT(msgid) { @@ -50,6 +51,8 @@ MOD_INIT(msgid) mtag.flags = MTAG_HANDLER_FLAGS_NO_CAP_NEEDED; MessageTagHandlerAdd(modinfo->handle, &mtag); + HookAddVoid(modinfo->handle, HOOKTYPE_NEW_MESSAGE, 0, mtag_add_or_inherit_msgid); + return MOD_SUCCESS; } @@ -76,4 +79,38 @@ int msgid_mtag_is_ok(aClient *acptr, char *name, char *value) return 0; } -// TODO: move from m_message (and elsewhere?) to here, some hook call or something +/** Generate a msgid. + * @returns a MessageTag struct that you can use directly. + * @notes + * Apparently there has been some discussion on what method to use to + * generate msgid's. I am not going to list them here. Just saying that + * they have been considered and we chose to go for a string that contains + * 128+ bits of randomness, which has an extremely low chance of colissions. + * Or, to quote wikipedia on the birthday attack problem: + * "For comparison, 10^-18 to 10^-15 is the uncorrectable bit error rate + * of a typical hard disk. In theory, hashes or UUIDs being 128 bits, + * should stay within that range until about 820 billion outputs" + * For reference, 10^-15 is 0.000000000000001% + * The main reasons for this choice are: that it is extremely simple, + * the chance of making a mistake in an otherwise complex implementation + * is nullified and we don't risk "leaking" any details. + */ +MessageTag *mtag_generate_msgid(void) +{ + MessageTag *m = MyMallocEx(sizeof(MessageTag)); + m->name = strdup("msgid"); + m->value = MyMallocEx(MSGIDLEN+1); + gen_random_alnum(m->value, MSGIDLEN); + return m; +} + + +void mtag_add_or_inherit_msgid(aClient *sender, MessageTag *recv_mtags, MessageTag **mtag_list) +{ + MessageTag *m = find_mtag(recv_mtags, "msgid"); + if (m) + m = duplicate_mtag(m); + else + m = mtag_generate_msgid(); + AddListItem(m, *mtag_list); +} diff --git a/src/modules/cap/server-time.c b/src/modules/cap/server-time.c index 8d40bbb2d..94d88fe82 100644 --- a/src/modules/cap/server-time.c +++ b/src/modules/cap/server-time.c @@ -35,6 +35,7 @@ ModuleHeader MOD_HEADER(server-time) long CAP_SERVER_TIME = 0L; int server_time_mtag_is_ok(aClient *acptr, char *name, char *value); +void mtag_add_or_inherit_time(aClient *sender, MessageTag *recv_mtags, MessageTag **mtag_list); MOD_INIT(server-time) { @@ -54,6 +55,8 @@ MOD_INIT(server-time) mtag.clicap_handler = c; MessageTagHandlerAdd(modinfo->handle, &mtag); + HookAddVoid(modinfo->handle, HOOKTYPE_NEW_MESSAGE, 0, mtag_add_or_inherit_time); + return MOD_SUCCESS; } @@ -80,4 +83,32 @@ int server_time_mtag_is_ok(aClient *acptr, char *name, char *value) return 0; } -// TODO: move from m_message (and elsewhere?) to here, some hook call or something +void mtag_add_or_inherit_time(aClient *sender, MessageTag *recv_mtags, MessageTag **mtag_list) +{ + MessageTag *m = find_mtag(recv_mtags, "time"); + if (m) + { + m = duplicate_mtag(m); + } else + { + struct timeval t; + struct tm *tm; + char buf[64]; + + gettimeofday(&t, NULL); + tm = gmtime(&t.tv_sec); + snprintf(buf, sizeof(buf), "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", + tm->tm_year + 1900, + tm->tm_mon + 1, + tm->tm_mday, + tm->tm_hour, + tm->tm_min, + tm->tm_sec, + (int)(t.tv_usec / 1000)); + + m = MyMallocEx(sizeof(MessageTag)); + m->name = strdup("time"); + m->value = strdup(buf); + } + AddListItem(m, *mtag_list); +} diff --git a/src/modules/m_message.c b/src/modules/m_message.c index 462b6d349..f19b4a69a 100644 --- a/src/modules/m_message.c +++ b/src/modules/m_message.c @@ -370,9 +370,7 @@ int m_message(aClient *cptr, aClient *sptr, MessageTag *recv_mtags, int parc, ch if (!text) continue; - mtag_add_or_inherit_msgid(recv_mtags, &mtags); - mtag_add_or_inherit_account(recv_mtags, &mtags, sptr); - mtag_add_or_inherit_time(recv_mtags, &mtags); + new_message(sptr, recv_mtags, &mtags); sendto_channel(chptr, sptr, sptr, prefix, 0, sendflags, mtags, notice ? ":%s NOTICE %s :%s" : ":%s PRIVMSG %s :%s", diff --git a/src/s_misc.c b/src/s_misc.c index cb4602cc2..a3cad74b7 100644 --- a/src/s_misc.c +++ b/src/s_misc.c @@ -1330,31 +1330,6 @@ char *pretty_date(TS t) return buf; } -/** Generate a msgid. - * @returns a MessageTag struct that you can use directly. - * @notes - * Apparently there has been some discussion on what method to use to - * generate msgid's. I am not going to list them here. Just saying that - * they have been considered and we chose to go for a string that contains - * 128+ bits of randomness, which has an extremely low chance of colissions. - * Or, to quote wikipedia on the birthday attack problem: - * "For comparison, 10^-18 to 10^-15 is the uncorrectable bit error rate - * of a typical hard disk. In theory, hashes or UUIDs being 128 bits, - * should stay within that range until about 820 billion outputs" - * For reference, 10^-15 is 0.000000000000001% - * The main reasons for this choice are: that it is extremely simple, - * the chance of making a mistake in an otherwise complex implementation - * is nullified and we don't risk "leaking" any details. - */ -MessageTag *mtag_generate_msgid(void) -{ - MessageTag *m = MyMallocEx(sizeof(MessageTag)); - m->name = strdup("msgid"); - m->value = MyMallocEx(MSGIDLEN+1); - gen_random_alnum(m->value, MSGIDLEN); - return m; -} - /** Find a particular message-tag in the 'mtags' list */ MessageTag *find_mtag(MessageTag *mtags, const char *token) { @@ -1389,62 +1364,13 @@ MessageTag *duplicate_mtag(MessageTag *mtag) return m; } -void mtag_add_or_inherit_msgid(MessageTag *recv_mtags, MessageTag **mtag_list) +/** New message. Either really brand new, or inherited from other servers. + * This function calls modules so they can add tags tags such as: + * msgid, time and account. + */ +void new_message(aClient *sender, MessageTag *recv_mtags, MessageTag **mtag_list) { - MessageTag *m = find_mtag(recv_mtags, "msgid"); - if (m) - m = duplicate_mtag(m); - else - m = mtag_generate_msgid(); - AddListItem(m, *mtag_list); -} - -void mtag_add_or_inherit_account(MessageTag *recv_mtags, MessageTag **mtag_list, aClient *acptr) -{ - MessageTag *m = find_mtag(recv_mtags, "account"); - if (m) - { - m = duplicate_mtag(m); - } else - { - if (acptr && acptr->user && - (*acptr->user->svid != '*') && !isdigit(*acptr->user->svid)) - { - m = MyMallocEx(sizeof(MessageTag)); - m->name = strdup("account"); - m->value = strdup(acptr->user->svid); - } - } - if (m) - AddListItem(m, *mtag_list); -} - -void mtag_add_or_inherit_time(MessageTag *recv_mtags, MessageTag **mtag_list) -{ - MessageTag *m = find_mtag(recv_mtags, "time"); - if (m) - { - m = duplicate_mtag(m); - } else - { - struct timeval t; - struct tm *tm; - char buf[64]; - - gettimeofday(&t, NULL); - tm = gmtime(&t.tv_sec); - snprintf(buf, sizeof(buf), "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", - tm->tm_year + 1900, - tm->tm_mon + 1, - tm->tm_mday, - tm->tm_hour, - tm->tm_min, - tm->tm_sec, - (int)(t.tv_usec / 1000)); - - m = MyMallocEx(sizeof(MessageTag)); - m->name = strdup("time"); - m->value = strdup(buf); - } - AddListItem(m, *mtag_list); + Hook *h; + for (h = Hooks[HOOKTYPE_NEW_MESSAGE]; h; h = h->next) + (*(h->func.voidfunc))(sender, recv_mtags, mtag_list); }