1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-28 13:56:37 +02:00

irc: update secure data when server autojoin option contains ${sec.data.xxx} and option autojoin_dynamic is enabled (closes #1934)

This commit is contained in:
Sébastien Helleu
2023-05-16 21:03:22 +02:00
parent e11ce668a2
commit 1f21cdc0bd
3 changed files with 158 additions and 35 deletions
+1
View File
@@ -74,6 +74,7 @@ Bug fixes::
* irc: reset all internal servers variables when disconnecting
* irc: fix join of channels in "autojoin" server option on first connection to server if auto reconnection is performed (issue #1873)
* irc: update autojoin option with redirected channels when autojoin_dynamic is enabled (issue #1898)
* irc: update secure data when server autojoin option contains `${sec.data.xxx}` and option autojoin_dynamic is enabled (issue #1934)
* irc: don't switch to buffer of joined channel if it was not manually joined nor present in server autojoin option
* irc: fix target buffer for commands 432/433 (erroneous nickname/nickname already in use) when the nickname looks like a channel
* irc: display command 437 on server buffer when nickname cannot change while banned on channel (issue #88)
+100 -35
View File
@@ -583,6 +583,54 @@ irc_join_add_channels (struct t_irc_server *server,
return new_join;
}
/*
* Sets the server autojoin option to a new value.
*
* If the autojoin contains a link to secure data (eg: "${sec.data.xxx}" with
* nothing before "${" and nothing after "}"), then the content of secure data
* is updated and the server autojoin option is kept as-is.
*/
void
irc_join_set_autojoin_option (struct t_irc_server *server,
const char *join)
{
const char *ptr_autojoin, *pos_option, *pos_closing_brace;
char *sec_data_name, **command;
sec_data_name = NULL;
ptr_autojoin = IRC_SERVER_OPTION_STRING(server, IRC_SERVER_OPTION_AUTOJOIN);
if (ptr_autojoin && ptr_autojoin[0]
&& (strncmp (ptr_autojoin, "${sec.data.", 11) == 0))
{
pos_option = ptr_autojoin + 11;
pos_closing_brace = strchr (pos_option, '}');
if (pos_closing_brace && !pos_closing_brace[1])
{
sec_data_name = weechat_strndup (pos_option,
pos_closing_brace - pos_option);
}
}
if (sec_data_name)
{
command = weechat_string_dyn_alloc (128);
weechat_string_dyn_concat (command, "/mute /secure set ", -1);
weechat_string_dyn_concat (command, sec_data_name, -1);
weechat_string_dyn_concat (command, " ", -1);
weechat_string_dyn_concat (command, join, -1);
weechat_command (weechat_buffer_search_main (), *command);
weechat_string_dyn_free (command, 1);
free (sec_data_name);
}
else
{
weechat_config_option_set (server->options[IRC_SERVER_OPTION_AUTOJOIN],
join, 1);
}
}
/*
* Adds a channel with optional key to the autojoin option of a server.
*/
@@ -591,22 +639,24 @@ void
irc_join_add_channel_to_autojoin (struct t_irc_server *server,
const char *channel_name, const char *key)
{
char *new_autojoin;
char *old_autojoin, *new_autojoin;
if (!channel_name)
return;
new_autojoin = irc_join_add_channel (
old_autojoin = irc_server_eval_expression (
server,
IRC_SERVER_OPTION_STRING(server, IRC_SERVER_OPTION_AUTOJOIN),
channel_name,
key);
IRC_SERVER_OPTION_STRING(server, IRC_SERVER_OPTION_AUTOJOIN));
new_autojoin = irc_join_add_channel (server, old_autojoin, channel_name, key);
if (new_autojoin)
{
weechat_config_option_set (server->options[IRC_SERVER_OPTION_AUTOJOIN],
new_autojoin, 1);
irc_join_set_autojoin_option (server, new_autojoin);
free (new_autojoin);
}
if (old_autojoin)
free (old_autojoin);
}
/*
@@ -617,18 +667,21 @@ void
irc_join_add_channels_to_autojoin (struct t_irc_server *server,
const char *join)
{
char *new_autojoin;
char *old_autojoin, *new_autojoin;
new_autojoin = irc_join_add_channels (
old_autojoin = irc_server_eval_expression (
server,
IRC_SERVER_OPTION_STRING(server, IRC_SERVER_OPTION_AUTOJOIN),
join);
IRC_SERVER_OPTION_STRING(server, IRC_SERVER_OPTION_AUTOJOIN));
new_autojoin = irc_join_add_channels (server, old_autojoin, join);
if (new_autojoin)
{
weechat_config_option_set (server->options[IRC_SERVER_OPTION_AUTOJOIN],
new_autojoin, 1);
irc_join_set_autojoin_option (server, new_autojoin);
free (new_autojoin);
}
if (old_autojoin)
free (old_autojoin);
}
/*
@@ -686,21 +739,24 @@ void
irc_join_remove_channel_from_autojoin (struct t_irc_server *server,
const char *channel_name)
{
char *new_autojoin;
char *old_autojoin, *new_autojoin;
if (!channel_name)
return;
new_autojoin = irc_join_remove_channel (
old_autojoin = irc_server_eval_expression (
server,
IRC_SERVER_OPTION_STRING(server, IRC_SERVER_OPTION_AUTOJOIN),
channel_name);
IRC_SERVER_OPTION_STRING(server, IRC_SERVER_OPTION_AUTOJOIN));
new_autojoin = irc_join_remove_channel (server, old_autojoin, channel_name);
if (new_autojoin)
{
weechat_config_option_set (server->options[IRC_SERVER_OPTION_AUTOJOIN],
new_autojoin, 1);
irc_join_set_autojoin_option (server, new_autojoin);
free (new_autojoin);
}
if (old_autojoin)
free (old_autojoin);
}
/*
@@ -802,22 +858,25 @@ irc_join_rename_channel_in_autojoin (struct t_irc_server *server,
const char *channel_name,
const char *new_channel_name)
{
char *new_autojoin;
char *old_autojoin, *new_autojoin;
if (!channel_name || !new_channel_name)
return;
new_autojoin = irc_join_rename_channel (
old_autojoin = irc_server_eval_expression (
server,
IRC_SERVER_OPTION_STRING(server, IRC_SERVER_OPTION_AUTOJOIN),
channel_name,
new_channel_name);
IRC_SERVER_OPTION_STRING(server, IRC_SERVER_OPTION_AUTOJOIN));
new_autojoin = irc_join_rename_channel (server, old_autojoin,
channel_name, new_channel_name);
if (new_autojoin)
{
weechat_config_option_set (server->options[IRC_SERVER_OPTION_AUTOJOIN],
new_autojoin, 1);
irc_join_set_autojoin_option (server, new_autojoin);
free (new_autojoin);
}
if (old_autojoin)
free (old_autojoin);
}
/*
@@ -860,8 +919,7 @@ irc_join_save_channels_to_autojoin (struct t_irc_server *server)
new_autojoin = irc_join_build_string (arraylist);
if (new_autojoin)
{
weechat_config_option_set (server->options[IRC_SERVER_OPTION_AUTOJOIN],
new_autojoin, 1);
irc_join_set_autojoin_option (server, new_autojoin);
free (new_autojoin);
}
@@ -897,22 +955,29 @@ irc_join_sort_channels (struct t_irc_server *server, const char *join,
void
irc_join_sort_autojoin (struct t_irc_server *server, enum t_irc_join_sort sort)
{
const char *ptr_autojoin;
char *new_autojoin;
char *old_autojoin, *new_autojoin;
if (!server)
return;
ptr_autojoin = IRC_SERVER_OPTION_STRING(server, IRC_SERVER_OPTION_AUTOJOIN);
old_autojoin = irc_server_eval_expression (
server,
IRC_SERVER_OPTION_STRING(server, IRC_SERVER_OPTION_AUTOJOIN));
if (!ptr_autojoin || !ptr_autojoin[0])
if (!old_autojoin || !old_autojoin[0])
{
if (old_autojoin)
free (old_autojoin);
return;
}
new_autojoin = irc_join_sort_channels (server, ptr_autojoin, sort);
new_autojoin = irc_join_sort_channels (server, old_autojoin, sort);
if (new_autojoin)
{
weechat_config_option_set (server->options[IRC_SERVER_OPTION_AUTOJOIN],
new_autojoin, 1);
irc_join_set_autojoin_option (server, new_autojoin);
free (new_autojoin);
}
if (old_autojoin)
free (old_autojoin);
}
+57
View File
@@ -28,6 +28,8 @@ extern "C"
#include <string.h>
#include "src/core/wee-arraylist.h"
#include "src/core/wee-config-file.h"
#include "src/core/wee-hashtable.h"
#include "src/core/wee-secure.h"
#include "src/gui/gui-buffer.h"
#include "src/plugins/irc/irc-channel.h"
#include "src/plugins/irc/irc-join.h"
@@ -596,6 +598,7 @@ TEST(IrcJoin, SortChannels)
/*
* Tests functions:
* irc_join_set_autojoin_option
* irc_join_add_channel_to_autojoin
* irc_join_add_channels_to_autojoin
* irc_join_remove_channel_from_autojoin
@@ -756,6 +759,33 @@ TEST(IrcJoin, AddRemoveChannelsAutojoin)
"#xyz,#DEF,#jkl key_xyz",
CONFIG_STRING(server->options[IRC_SERVER_OPTION_AUTOJOIN]));
/* use of secure data in autojoin option */
hashtable_set (secure_hashtable_data, "autojoin", "#abc");
config_file_option_set (server->options[IRC_SERVER_OPTION_AUTOJOIN],
"${sec.data.autojoin}", 1);
irc_join_add_channels_to_autojoin (server, "#def key_def");
STRCMP_EQUAL(
"${sec.data.autojoin}",
CONFIG_STRING(server->options[IRC_SERVER_OPTION_AUTOJOIN]));
STRCMP_EQUAL(
"#def,#abc key_def",
(const char *)hashtable_get (secure_hashtable_data, "autojoin"));
irc_join_rename_channel_in_autojoin (server, "#abc", "#zzz");
STRCMP_EQUAL(
"${sec.data.autojoin}",
CONFIG_STRING(server->options[IRC_SERVER_OPTION_AUTOJOIN]));
STRCMP_EQUAL(
"#def,#zzz key_def",
(const char *)hashtable_get (secure_hashtable_data, "autojoin"));
irc_join_remove_channel_from_autojoin (server, "#def");
STRCMP_EQUAL(
"${sec.data.autojoin}",
CONFIG_STRING(server->options[IRC_SERVER_OPTION_AUTOJOIN]));
STRCMP_EQUAL(
"#zzz",
(const char *)hashtable_get (secure_hashtable_data, "autojoin"));
hashtable_remove (secure_hashtable_data, "autojoin");
irc_server_free (server);
}
@@ -783,6 +813,19 @@ TEST(IrcJoin, SaveChannelsToAutojoin)
"#test2,#test1 key2",
CONFIG_STRING(server->options[IRC_SERVER_OPTION_AUTOJOIN]));
/* use of secure data in autojoin option */
hashtable_set (secure_hashtable_data, "autojoin", "#abc");
config_file_option_set (server->options[IRC_SERVER_OPTION_AUTOJOIN],
"${sec.data.autojoin}", 1);
irc_join_save_channels_to_autojoin (server);
STRCMP_EQUAL(
"${sec.data.autojoin}",
CONFIG_STRING(server->options[IRC_SERVER_OPTION_AUTOJOIN]));
STRCMP_EQUAL(
"#test2,#test1 key2",
(const char *)hashtable_get (secure_hashtable_data, "autojoin"));
hashtable_remove (secure_hashtable_data, "autojoin");
gui_buffer_close (channel1->buffer);
gui_buffer_close (channel2->buffer);
@@ -813,5 +856,19 @@ TEST(IrcJoin, SortAutojoinChannels)
STRCMP_EQUAL("#xyz,#zzz,#ABC,#def,#ghi key_xyz,key_zzz",
CONFIG_STRING(server->options[IRC_SERVER_OPTION_AUTOJOIN]));
/* use of secure data in autojoin option */
hashtable_set (secure_hashtable_data,
"autojoin", "#zzz,#xyz,#ghi,#def,#ABC key_zzz,key_xyz");
config_file_option_set (server->options[IRC_SERVER_OPTION_AUTOJOIN],
"${sec.data.autojoin}", 1);
irc_join_sort_autojoin (server, IRC_JOIN_SORT_ALPHA);
STRCMP_EQUAL(
"${sec.data.autojoin}",
CONFIG_STRING(server->options[IRC_SERVER_OPTION_AUTOJOIN]));
STRCMP_EQUAL(
"#xyz,#zzz,#ABC,#def,#ghi key_xyz,key_zzz",
(const char *)hashtable_get (secure_hashtable_data, "autojoin"));
hashtable_remove (secure_hashtable_data, "autojoin");
irc_server_free (server);
}