Page 1 of 1

[SOLVED] Module that adds a message tag to channel messages, how to?

Posted: Wed Nov 13, 2024 6:54 am
by Axle1975
I want to write a module that tags all channel messages with a custom message tag. I tried a few things, but can't get it working - or at least the IRC client (KittehChat java based client) doesn't see the new messages.

Does the below look right, or am I way off? The log message appears no problem. Its just that the clients don't see any sign of the taforever.com/name=value tag in the raw message.

Is it the right hook and is it the right method of adding tags to messages??

Does my module need to register the new message tag before its recognised??

Or is it likely a client-side problem, does the client need to send additional CAP REQ arguments on connect?? It currently CAP REQ's the following "userhost-in-names message-tags batch multi-prefix extended-join away-notify setname account-tag chghost account-notify labeled-response server-time"

Any pointers would be greatly appreciated!

Code: Select all

#include "unrealircd.h"

ModuleHeader MOD_HEADER = {
	"third/my_module",
	"1.0",
	"My module that adds message tags",
	"TA Forever",
	"unrealircd-6",
	};

int channel_message_hook(Client *client, Channel *channel, MessageTag **mtags, const char *text, SendType sendtype)
{
	unreal_log(ULOG_INFO, "taforever.com", "channel_message_hook", NULL, "entry");
	MessageTag *m = safe_alloc(sizeof(MessageTag));
	safe_strdup(m->name, "taforever.com/name");
	safe_strdup(m->value, "value");
	AppendListItem(m, *mtags);
	return 0;
}

MOD_INIT() {
	HookAdd(modinfo->handle, HOOKTYPE_PRE_CHANMSG, 0, channel_message_hook);
	return MOD_SUCCESS;
}

MOD_LOAD()
{
	return MOD_SUCCESS;
}

MOD_UNLOAD()
{
	return MOD_SUCCESS;
}

Re: Module that adds a message tag to channel messages, how to?

Posted: Thu Nov 14, 2024 6:35 am
by Axle1975
ok I got it sussed, thanks for your help all! It's necessary to register a message tag handler:

Code: Select all

MOD_INIT() {
    MessageTagHandlerInfo tag;
    
    MARK_AS_GLOBAL_MODULE(modinfo);
    
    memset(&mtag, 0, sizeof(mtag));
    mtag.name = "taforever.com/name";
    mtag.flags = MTAG_HANDLER_FLAGS_NO_CAP_NEEDED;
    MessageTagHandlerAdd(modinfo->handle, &mtag);
    
    HookAdd(modinfo->handle, HOOKTYPE_PRE_CHANMSG, 0, channel_message_hook);
    return MOD_SUCCESS;
 }

Re: Module that adds a message tag to channel messages, how to?

Posted: Thu Nov 14, 2024 11:27 am
by Axle1975
Now I want to do the same for User Messages. HOOKTYPE_PRE_CHANMSG works well for channel messages, but there doesn't seem to be any User Message equivalent. HOOKTYPE_USERMSG is no good because its only called after the message is actually forwarded to the user so there's no opportunity to modify mtags. I worked around it for now by hacking in an extra "RunHook(HOOKTYPE_PRE_CHANMSG, client, target, &mtags, text, sendtype)" in src/modules/message.c, just before forwarding the user message but obviously that's just a nasty hack.

Maybe unrealircd developers would like to add a HOOKTYPE_PRE_USERMSG hook in a future version?

Re: Module that adds a message tag to channel messages, how to?

Posted: Thu Nov 14, 2024 12:25 pm
by Valware
You should look at existing modules and see how they do it. Tip: You're using the wrong hook.
You don't need to add this to both channel and user messages, you can just add them to "new messages" and this does both.
Example: https://github.com/unrealircd/unrealirc ... name.c#L51

Re: Module that adds a message tag to channel messages, how to?

Posted: Fri Nov 15, 2024 6:58 am
by Axle1975
Hi Val, thanks for your reply. Yeah I found the new message hook but unfortunately its not suitable for my use case. You see I need to inspect the message text in order to determine the value for the new tag. The new message hook doesn't provide the message text AFAIK

Re: Module that adds a message tag to channel messages, how to?

Posted: Fri Nov 15, 2024 8:05 am
by Syzop
The HOOKTYPE_PRE_USERMSG hook existed in the past. It was removed in 2019 because people were blocking or filtering user messages there, which was not correct because we moved to HOOKTYPE_CAN_SEND_TO_USER for that, which uses generic error reporting. For the HOOKTYPE_PRE_CHANMSG it was clear there were other use cases, it is used by delayjoin, and it was later improved (on request of Valware) in commit 89b2d91084501785a975c58f4bd48413294e3a4f to allow fiddling with message tags.

Sure, we can re-add HOOKTYPE_PRE_USERMSG for the same reason as we allow the CHANMSG variant.

Re: Module that adds a message tag to channel messages, how to?

Posted: Fri Nov 15, 2024 8:45 am
by Axle1975
I guess another solution that would suit my use case would be to pass the message text as an argument to the new message hook