From 1e846b1f240c588c47e46d3d4f5f87f929fe474a Mon Sep 17 00:00:00 2001 From: Miklos Vajna Date: Fri, 5 Jun 2026 23:01:21 +0200 Subject: [PATCH] Add an initial weechat msgid script Connect to matrirc with irssi, messages are prefixed with a tag like "[abc] ", so "!r abc foo" can be sent to reply to that message, but the same doesn't work in weechat. This happens because for irssi, irssi/matrirc.pl.in provides this functionality. Start a weechat version of that in Python: if the server sends us a msgid, then expose that to the user in a similar way. Note that weechat automatically sends the message-tags capability to the server, so we don't need a connect hook, just an irc_in_privmsg hook, and that one seems to work for both channels and direct messages. --- weechat/matrirc_msgid.py | 47 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 weechat/matrirc_msgid.py diff --git a/weechat/matrirc_msgid.py b/weechat/matrirc_msgid.py new file mode 100644 index 0000000..c3c5e85 --- /dev/null +++ b/weechat/matrirc_msgid.py @@ -0,0 +1,47 @@ +import weechat +import re + +SCRIPT_NAME = "matrirc_msgid" +SCRIPT_AUTHOR = "matrirc" +SCRIPT_VERSION = "0.1.1" +SCRIPT_LICENSE = "GPL3" +SCRIPT_DESC = "Prefix incoming matrirc messages with [id] from msgid tag" + +def msgid_modifier_cb(data, modifier, modifier_data, string): + # Match tags, prefix (optional), command, target, and body + # Format: @tags rest + if not string.startswith('@'): + return string + + # Split tags and the rest of the message + parts = string.split(' ', 1) + if len(parts) < 2: + return string + + tags_str, rest = parts[0], parts[1] + + # Extract msgid tag + msgid_match = re.search(r'[;@]?msgid=([^; ]+)', tags_str) + if not msgid_match: + return string + + msgid_raw = msgid_match.group(1) + # Unescape some common IRC tag characters + msgid = msgid_raw.replace('\\:', ';').replace('\\s', ' ').replace('\\\\', '\\').replace('\\r', '\r').replace('\\n', '\n') + + # Find PRIVMSG and the body part + # Group 1: Everything up to and including the target and the following space + # Group 2: Optional colon before the body + # Group 3: The body itself + match = re.search(r'^(.*?\s+PRIVMSG\s+\S+\s+)(:?)(.*)$', rest) + if match: + prefix_and_target = match.group(1) + body = match.group(3) + # Reconstruct the message with the [id] prefix in the body. + return f"{tags_str} {prefix_and_target}:[{msgid}] {body}" + + return string + +if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, "", ""): + # Register the msgid modifier callback + weechat.hook_modifier("irc_in_privmsg", "msgid_modifier_cb", "")