From 65b40ab5cc49eb7827e1800cd4d42477a200c997 Mon Sep 17 00:00:00 2001 From: Sebastien Helleu Date: Thu, 13 Feb 2014 13:38:10 +0100 Subject: [PATCH] trigger: parse IRC messages received in signal/modifier hook callbacks The parsed message is added into hashtable "extra_vars". For example, signal "freenode,irc_in_PRIVMSG" received with the message "hello world!" on channel #weechat gives in the hashtable: extra_vars: tags: "" host: "tester!user@host.com" tg_signal: "freenode,irc_in_PRIVMSG" channel: "#weechat" tg_signal_data: ":tester!user@host.com PRIVMSG #weechat :hello world!" arguments: "#weechat :hello world!" nick: "tester" command: "PRIVMSG" message_without_tags: ":tester!user@host.com PRIVMSG #weechat :hello world!" --- src/plugins/trigger/trigger-callback.c | 93 +++++++++++++++++++++++++- 1 file changed, 90 insertions(+), 3 deletions(-) diff --git a/src/plugins/trigger/trigger-callback.c b/src/plugins/trigger/trigger-callback.c index 21ccd5d0f..ba181ff8f 100644 --- a/src/plugins/trigger/trigger-callback.c +++ b/src/plugins/trigger/trigger-callback.c @@ -34,6 +34,38 @@ struct t_hashtable *trigger_callback_hashtable_options = NULL; +/* + * Parses an IRC message. + * + * Returns a hashtable with the parsed message, or NULL if error. + * + * Note: the hashtable must be freed after use. + */ + +struct t_hashtable * +trigger_callback_irc_message_parse (const char *irc_message, + const char *irc_server) +{ + struct t_hashtable *hashtable_in, *hashtable_out; + + hashtable_out = NULL; + + hashtable_in = weechat_hashtable_new (32, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_STRING, + NULL, + NULL); + if (hashtable_in) + { + weechat_hashtable_set (hashtable_in, "message", irc_message); + weechat_hashtable_set (hashtable_in, "server", irc_server); + hashtable_out = weechat_info_get_hashtable ("irc_message_parse", + hashtable_in); + weechat_hashtable_free (hashtable_in); + } + + return hashtable_out; +} /* * Sets variables in "extra_vars" hashtable using tags from message. * @@ -291,11 +323,53 @@ trigger_callback_signal_cb (void *data, const char *signal, const char *type_data, void *signal_data) { const char *ptr_signal_data; - char str_data[128]; + char str_data[128], *irc_server; + const char *pos, *ptr_irc_message; TRIGGER_CALLBACK_CB_INIT(WEECHAT_RC_OK); - TRIGGER_CALLBACK_CB_NEW_EXTRA_VARS; + /* split IRC message (if signal_data is an IRC message) */ + irc_server = NULL; + ptr_irc_message = NULL; + if (strcmp (type_data, WEECHAT_HOOK_SIGNAL_STRING) == 0) + { + if (strstr (signal, ",irc_in_") + || strstr (signal, ",irc_in2_") + || strstr (signal, ",irc_raw_in_") + || strstr (signal, ",irc_raw_in2_") + || strstr (signal, ",irc_out1_") + || strstr (signal, ",irc_out_")) + { + pos = strchr (signal, ','); + if (pos) + { + irc_server = weechat_strndup (signal, pos - signal); + ptr_irc_message = (const char *)signal_data; + } + } + else + { + pos = strstr (signal, ",irc_outtags_"); + if (pos) + { + irc_server = weechat_strndup (signal, pos - signal); + pos = strchr ((const char *)signal_data, ';'); + if (pos) + ptr_irc_message = pos + 1; + } + } + } + if (irc_server && ptr_irc_message) + extra_vars = trigger_callback_irc_message_parse (ptr_irc_message, + irc_server); + if (irc_server) + free (irc_server); + + /* create hashtable (if not already created) */ + if (!extra_vars) + { + TRIGGER_CALLBACK_CB_NEW_EXTRA_VARS; + } /* add data in hashtable used for conditions/replace/command */ weechat_hashtable_set (extra_vars, "tg_signal", signal); @@ -395,8 +469,21 @@ trigger_callback_modifier_cb (void *data, const char *modifier, tags = NULL; num_tags = 0; + /* split IRC message (if string is an IRC message) */ + if (strncmp (modifier, "irc_in_", 7) + || strncmp (modifier, "irc_in2_", 8) + || strncmp (modifier, "irc_out1_", 9) + || strncmp (modifier, "irc_out_", 8)) + { + extra_vars = trigger_callback_irc_message_parse (string, + modifier_data); + } + TRIGGER_CALLBACK_CB_NEW_POINTERS; - TRIGGER_CALLBACK_CB_NEW_EXTRA_VARS; + if (!extra_vars) + { + TRIGGER_CALLBACK_CB_NEW_EXTRA_VARS; + } /* add data in hashtable used for conditions/replace/command */ weechat_hashtable_set (extra_vars, "tg_modifier", modifier);