From 93b5e6044122e40c9d817592dcfacd95222688d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Thu, 20 Sep 2018 20:32:47 +0200 Subject: irc: display a warning when the value of option irc.server.xxx.autojoin is set to an invalid value --- ChangeLog.adoc | 1 + src/plugins/irc/irc-config.c | 83 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/ChangeLog.adoc b/ChangeLog.adoc index 81e42942f..a888cdaaf 100644 --- a/ChangeLog.adoc +++ b/ChangeLog.adoc @@ -25,6 +25,7 @@ New features:: * core: add option "-P" (or "--plugins") to customize the plugins to load at startup * core: allow partial buffer name in command /buffer close (issue #1226) * api: add function hook_line + * irc: display a warning when the value of option irc.server.xxx.autojoin is set to an invalid value * trigger: allow creation of temporary variables with the regex * trigger: add hook "line" diff --git a/src/plugins/irc/irc-config.c b/src/plugins/irc/irc-config.c index 9a7d9dc10..c3efee2eb 100644 --- a/src/plugins/irc/irc-config.c +++ b/src/plugins/irc/irc-config.c @@ -985,6 +985,72 @@ irc_config_check_gnutls_priorities (const char *priorities) #endif /* HAVE_GNUTLS */ } +/* + * Checks autojoin value, which must respect the IRC JOIN syntax: + * #chan1,#chan2 + * #chan1,#chan2,#chan3 key1,key2 + * + * Returns: + * 1: value OK + * 0: invalid value + */ + +int +irc_config_check_autojoin (const char *autojoin) +{ + char *string, **items, **channels, **keys; + int rc, num_items, num_channels, num_keys; + + rc = 0; + string = NULL; + items = NULL; + channels = NULL; + keys = NULL; + num_items = 0; + num_channels = 0; + num_keys = 0; + + /* NULL or empty value is considered as OK */ + if (!autojoin || !autojoin[0]) + return 1; + + /* ignore spaces at beginning/end of string */ + string = weechat_string_strip (autojoin, 1, 1, " "); + if (!string) + goto end; + + /* no space allowed before or after a comma */ + if (strstr (string, ", ") || strstr (string, " ,")) + goto end; + + items = weechat_string_split (string, " ", 0, 0, &num_items); + if (!items || (num_items < 1) || (num_items > 2)) + goto end; + + channels = weechat_string_split (items[0], ",", 0, 0, &num_channels); + + if (num_items == 2) + keys = weechat_string_split (items[1], ",", 0, 0, &num_keys); + + /* error if there are more keys than channels to join */ + if (num_keys > num_channels) + goto end; + + rc = 1; + +end: + if (string) + free (string); + if (items) + weechat_string_free_split (items); + if (channels) + weechat_string_free_split (channels); + if (keys) + weechat_string_free_split (keys); + + return rc; +} + /* * Callback called to check a server option when it is modified. */ @@ -1050,6 +1116,23 @@ irc_config_server_check_value_cb (const void *pointer, void *data, return 0; } break; + case IRC_SERVER_OPTION_AUTOJOIN: + if (!value || !value[0]) + break; + if (!irc_config_check_autojoin (value)) + { + weechat_printf ( + NULL, + _("%s%s: warning: invalid autojoin value \"%s\", see " + "/help %s.%s.%s"), + weechat_prefix ("error"), + IRC_PLUGIN_NAME, + value, + weechat_config_option_get_string (option, "config_name"), + weechat_config_option_get_string (option, "section_name"), + weechat_config_option_get_string (option, "name")); + } + break; case IRC_SERVER_OPTION_SPLIT_MSG_MAX_LENGTH: if (!value || !value[0]) break; -- cgit v1.2.3