mirror of
https://github.com/weechat/weechat.git
synced 2026-07-09 19:23:13 +02:00
irc: add keys/values with tags in output of irc_message_parse_to_hashtable (issue #1654)
Key is "tag_xxx" (where "xxx" is the name of tag) and value is the unescaped tag value.
This commit is contained in:
@@ -1183,6 +1183,7 @@ irc_info_init ()
|
||||
N_("\"message\": IRC message, \"server\": server name (optional)"),
|
||||
/* TRANSLATORS: please do not translate key names (enclosed by quotes) */
|
||||
N_("\"tags\": tags, "
|
||||
"\"tag_xxx\": unescaped value of tag \"xxx\" (one key per tag), "
|
||||
"\"message_without_tags\": message without the tags, "
|
||||
"\"nick\": nick, "
|
||||
"\"user\": user, "
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
#include "irc-config.h"
|
||||
#include "irc-ignore.h"
|
||||
#include "irc-server.h"
|
||||
#include "irc-tag.h"
|
||||
|
||||
|
||||
/*
|
||||
@@ -327,6 +328,7 @@ irc_message_parse (struct t_irc_server *server, const char *message,
|
||||
/*
|
||||
* Parses an IRC message and returns hashtable with keys:
|
||||
* - tags
|
||||
* - tag_xxx (one key per tag, with unescaped value)
|
||||
* - message_without_tags
|
||||
* - nick
|
||||
* - host
|
||||
@@ -365,6 +367,7 @@ irc_message_parse_to_hashtable (struct t_irc_server *server,
|
||||
|
||||
weechat_hashtable_set (hashtable, "tags",
|
||||
(tags) ? tags : empty_str);
|
||||
irc_tag_parse (tags, hashtable, "tag_");
|
||||
weechat_hashtable_set (hashtable, "message_without_tags",
|
||||
(message_without_tags) ? message_without_tags : empty_str);
|
||||
weechat_hashtable_set (hashtable, "nick",
|
||||
|
||||
@@ -6826,7 +6826,6 @@ irc_protocol_recv_command (struct t_irc_server *server,
|
||||
message_colors_decoded = NULL;
|
||||
argv = NULL;
|
||||
argv_eol = NULL;
|
||||
hash_tags = NULL;
|
||||
date = 0;
|
||||
|
||||
ptr_msg_after_tags = irc_message;
|
||||
@@ -6841,11 +6840,16 @@ irc_protocol_recv_command (struct t_irc_server *server,
|
||||
pos_space - (irc_message + 1));
|
||||
if (tags)
|
||||
{
|
||||
hash_tags = irc_tag_parse (tags);
|
||||
hash_tags = weechat_hashtable_new (32,
|
||||
WEECHAT_HASHTABLE_STRING,
|
||||
WEECHAT_HASHTABLE_STRING,
|
||||
NULL, NULL);
|
||||
if (hash_tags)
|
||||
{
|
||||
irc_tag_parse (tags, hash_tags, NULL);
|
||||
date = irc_protocol_parse_time (
|
||||
weechat_hashtable_get (hash_tags, "time"));
|
||||
weechat_hashtable_free (hash_tags);
|
||||
}
|
||||
free (tags);
|
||||
}
|
||||
@@ -7033,6 +7037,4 @@ end:
|
||||
weechat_string_free_split (argv);
|
||||
if (argv_eol)
|
||||
weechat_string_free_split (argv_eol);
|
||||
if (hash_tags)
|
||||
weechat_hashtable_free (hash_tags);
|
||||
}
|
||||
|
||||
+34
-23
@@ -216,33 +216,34 @@ irc_tag_modifier_cb (const void *pointer, void *data,
|
||||
}
|
||||
|
||||
/*
|
||||
* Parses tags received in an IRC message.
|
||||
* Returns a hashtable with tags and their unescaped values.
|
||||
* Parses tags received in an IRC message and returns the number of tags
|
||||
* set in the hasbtable "hashtable" (values are unescaped tag values).
|
||||
*
|
||||
* If prefix_key is not NULL, it is used as prefix before the name of keys.
|
||||
*
|
||||
* Example:
|
||||
* if tags == "aaa=bbb;ccc;example.com/ddd=eee",
|
||||
* hashtable will have following keys/values:
|
||||
* "aaa" => "bbb"
|
||||
* "ccc" => NULL
|
||||
* "example.com/ddd" => "eee"
|
||||
*
|
||||
* input:
|
||||
* tags == "aaa=bbb;ccc;example.com/ddd=value\swith\sspaces"
|
||||
* prefix_key == "tag_"
|
||||
* output:
|
||||
* hashtable is completed with the following keys/values:
|
||||
* "tag_aaa" => "bbb"
|
||||
* "tag_ccc" => NULL
|
||||
* "tag_example.com/ddd" => "value with spaces"
|
||||
*/
|
||||
|
||||
struct t_hashtable *
|
||||
irc_tag_parse (const char *tags)
|
||||
int
|
||||
irc_tag_parse (const char *tags,
|
||||
struct t_hashtable *hashtable, const char *prefix_key)
|
||||
{
|
||||
struct t_hashtable *hashtable;
|
||||
char **items, *pos, *key, *unescaped;
|
||||
int num_items, i;
|
||||
char **items, *pos, *key, str_key[4096], *unescaped;
|
||||
int num_items, i, num_tags;
|
||||
|
||||
if (!tags || !tags[0])
|
||||
return NULL;
|
||||
if (!tags || !tags[0] || !hashtable)
|
||||
return 0;
|
||||
|
||||
hashtable = weechat_hashtable_new (32,
|
||||
WEECHAT_HASHTABLE_STRING,
|
||||
WEECHAT_HASHTABLE_STRING,
|
||||
NULL, NULL);
|
||||
if (!hashtable)
|
||||
return NULL;
|
||||
num_tags = 0;
|
||||
|
||||
items = weechat_string_split (tags, ";", NULL,
|
||||
WEECHAT_STRING_SPLIT_STRIP_LEFT
|
||||
@@ -260,21 +261,31 @@ irc_tag_parse (const char *tags)
|
||||
key = weechat_strndup (items[i], pos - items[i]);
|
||||
if (key)
|
||||
{
|
||||
snprintf (str_key, sizeof (str_key),
|
||||
"%s%s",
|
||||
(prefix_key) ? prefix_key : "",
|
||||
key);
|
||||
unescaped = irc_tag_unescape_value (pos + 1);
|
||||
weechat_hashtable_set (hashtable, key, unescaped);
|
||||
weechat_hashtable_set (hashtable, str_key, unescaped);
|
||||
if (unescaped)
|
||||
free (unescaped);
|
||||
free (key);
|
||||
num_tags++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* format: "tag" */
|
||||
weechat_hashtable_set (hashtable, items[i], NULL);
|
||||
snprintf (str_key, sizeof (str_key),
|
||||
"%s%s",
|
||||
(prefix_key) ? prefix_key : "",
|
||||
items[i]);
|
||||
weechat_hashtable_set (hashtable, str_key, NULL);
|
||||
num_tags++;
|
||||
}
|
||||
}
|
||||
weechat_string_free_split (items);
|
||||
}
|
||||
|
||||
return hashtable;
|
||||
return num_tags;
|
||||
}
|
||||
|
||||
@@ -27,6 +27,8 @@ extern char *irc_tag_modifier_cb (const void *pointer,
|
||||
const char *modifier,
|
||||
const char *modifier_data,
|
||||
const char *string);
|
||||
extern struct t_hashtable *irc_tag_parse (const char *tags);
|
||||
extern int irc_tag_parse (const char *tags,
|
||||
struct t_hashtable *hashtable,
|
||||
const char *prefix_key);
|
||||
|
||||
#endif /* WEECHAT_PLUGIN_IRC_TAG_H */
|
||||
|
||||
Reference in New Issue
Block a user