Merge pull request #22 from ailin-nemui/ports

Something with server add
This commit is contained in:
ailin-nemui 2021-08-31 15:30:48 +02:00 committed by GitHub
commit b7b91ed2bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 28 deletions

View File

@ -6,7 +6,7 @@
#define IRSSI_GLOBAL_CONFIG "irssi.conf" /* config file name in /etc/ */ #define IRSSI_GLOBAL_CONFIG "irssi.conf" /* config file name in /etc/ */
#define IRSSI_HOME_CONFIG "config" /* config file name in ~/.irssi/ */ #define IRSSI_HOME_CONFIG "config" /* config file name in ~/.irssi/ */
#define IRSSI_ABI_VERSION 39 #define IRSSI_ABI_VERSION 40
#define DEFAULT_SERVER_ADD_PORT 6667 #define DEFAULT_SERVER_ADD_PORT 6667
#define DEFAULT_SERVER_ADD_TLS_PORT 6697 #define DEFAULT_SERVER_ADD_TLS_PORT 6697

View File

@ -457,8 +457,9 @@ static SERVER_SETUP_REC *server_setup_read(CONFIG_NODE *node)
port = config_node_get_int(node, "port", 0); port = config_node_get_int(node, "port", 0);
chatnet = config_node_get_str(node, "chatnet", NULL); chatnet = config_node_get_str(node, "chatnet", NULL);
if (server_setup_find(server, port, chatnet) != NULL) { if ((rec = server_setup_find(server, port, chatnet)) != NULL && rec->port == port) {
return NULL; /* duplicate server setup */
server_setup_remove(rec);
} }
rec = NULL; rec = NULL;
@ -549,7 +550,7 @@ static int compare_server_setup (CONFIG_NODE *node, SERVER_SETUP_REC *server)
return -1; return -1;
address = config_node_get_str(node, "address", NULL); address = config_node_get_str(node, "address", NULL);
chatnet = config_node_get_str(node, "chatnet", NULL); chatnet = config_node_get_str(node, "chatnet", "");
port = config_node_get_int(node, "port", 0); port = config_node_get_int(node, "port", 0);
if (address == NULL || chatnet == NULL) { if (address == NULL || chatnet == NULL) {
@ -557,7 +558,7 @@ static int compare_server_setup (CONFIG_NODE *node, SERVER_SETUP_REC *server)
} }
if (g_ascii_strcasecmp(address, server->address) != 0 || if (g_ascii_strcasecmp(address, server->address) != 0 ||
g_ascii_strcasecmp(chatnet, server->chatnet) != 0 || g_ascii_strcasecmp(chatnet, server->chatnet != NULL ? server->chatnet : "") != 0 ||
port != server->port) { port != server->port) {
return 1; return 1;
} }
@ -565,15 +566,19 @@ static int compare_server_setup (CONFIG_NODE *node, SERVER_SETUP_REC *server)
return 0; return 0;
} }
static void server_setup_save(SERVER_SETUP_REC *rec) static void server_setup_save(SERVER_SETUP_REC *rec, int old_port, const char *old_chatnet)
{ {
CONFIG_NODE *parent_node, *node; CONFIG_NODE *parent_node, *node;
SERVER_SETUP_REC search_rec = { 0 };
GSList *config_node; GSList *config_node;
parent_node = iconfig_node_traverse("(servers", TRUE); parent_node = iconfig_node_traverse("(servers", TRUE);
/* Try to find this channel in the configuration */ /* Try to find this channel in the configuration */
config_node = g_slist_find_custom(parent_node->value, rec, search_rec.address = rec->address;
search_rec.chatnet = old_chatnet != NULL ? (char *) old_chatnet : rec->chatnet;
search_rec.port = old_port;
config_node = g_slist_find_custom(parent_node->value, &search_rec,
(GCompareFunc) compare_server_setup); (GCompareFunc) compare_server_setup);
if (config_node != NULL) if (config_node != NULL)
/* Let's update this server record */ /* Let's update this server record */
@ -655,16 +660,23 @@ static void server_setup_destroy(SERVER_SETUP_REC *rec)
g_free(rec); g_free(rec);
} }
void server_setup_add(SERVER_SETUP_REC *rec) void server_setup_modify(SERVER_SETUP_REC *rec, int old_port, const char *old_chatnet)
{ {
g_return_if_fail(g_slist_find(setupservers, rec) != NULL);
rec->type = module_get_uniq_id("SERVER SETUP", 0); rec->type = module_get_uniq_id("SERVER SETUP", 0);
if (g_slist_find(setupservers, rec) == NULL) server_setup_save(rec, old_port, old_chatnet);
setupservers = g_slist_append(setupservers, rec);
server_setup_save(rec);
signal_emit("server setup updated", 1, rec); signal_emit("server setup updated", 1, rec);
} }
void server_setup_add(SERVER_SETUP_REC *rec)
{
if (g_slist_find(setupservers, rec) == NULL)
setupservers = g_slist_append(setupservers, rec);
server_setup_modify(rec, -1, NULL);
}
void server_setup_remove_chatnet(const char *chatnet) void server_setup_remove_chatnet(const char *chatnet)
{ {
GSList *tmp, *next; GSList *tmp, *next;

View File

@ -41,6 +41,7 @@ SERVER_SETUP_REC *server_setup_find(const char *address, int port,
const char *chatnet); const char *chatnet);
void server_setup_add(SERVER_SETUP_REC *rec); void server_setup_add(SERVER_SETUP_REC *rec);
void server_setup_modify(SERVER_SETUP_REC *rec, int old_port, const char *old_chatnet);
void server_setup_remove(SERVER_SETUP_REC *rec); void server_setup_remove(SERVER_SETUP_REC *rec);
/* Remove servers attached to chatne */ /* Remove servers attached to chatne */

View File

@ -110,11 +110,11 @@ static SERVER_SETUP_REC *create_server_setup(GHashTable *optlist)
static void cmd_server_add_modify(const char *data, gboolean add) static void cmd_server_add_modify(const char *data, gboolean add)
{ {
GHashTable *optlist; GHashTable *optlist;
SERVER_SETUP_REC *rec; SERVER_SETUP_REC *rec, *tmp;
char *addr, *portstr, *password, *value, *chatnet; char *addr, *portstr, *password, *value, *chatnet, *old_chatnet;
void *free_arg; void *free_arg;
gboolean newrec; gboolean newrec;
int port; int port, old_port, add_port;
if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_OPTIONS, if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_OPTIONS,
"server add", &optlist, &addr, &portstr, &password)) "server add", &optlist, &addr, &portstr, &password))
@ -122,27 +122,39 @@ static void cmd_server_add_modify(const char *data, gboolean add)
if (*addr == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS); if (*addr == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
value = g_hash_table_lookup(optlist, "port"); port = old_port = -1;
if (*portstr != '\0') if (*portstr != '\0')
port = atoi(portstr); port = add_port = atoi(portstr);
else if (value != NULL && *value != '\0')
port = atoi(value);
else if (g_hash_table_lookup(optlist, "tls") || else if (g_hash_table_lookup(optlist, "tls") ||
g_hash_table_lookup(optlist, "ssl")) g_hash_table_lookup(optlist, "ssl"))
port = DEFAULT_SERVER_ADD_TLS_PORT; add_port = DEFAULT_SERVER_ADD_TLS_PORT;
else else
port = DEFAULT_SERVER_ADD_PORT; add_port = DEFAULT_SERVER_ADD_PORT;
value = g_hash_table_lookup(optlist, "port");
if (value != NULL && *value != '\0')
old_port = atoi(value);
chatnet = g_hash_table_lookup(optlist, "network"); chatnet = g_hash_table_lookup(optlist, "network");
rec = server_setup_find(addr, port, chatnet); rec = server_setup_find(addr, old_port != -1 ? old_port : add_port, chatnet);
if (old_port == -1 && rec != NULL)
old_port = rec->port;
if (rec == NULL) { if (port == -1)
port = old_port != -1 ? old_port : add_port;
/* make sure the new port doesn't exist */
tmp = server_setup_find(addr, port, chatnet);
if (tmp != NULL && tmp->port == port)
rec = tmp;
if (rec == NULL || (rec->port != old_port && rec->port != port)) {
newrec = TRUE; newrec = TRUE;
if (add == FALSE) { if (add == FALSE) {
printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_SETUPSERVER_NOT_FOUND,
TXT_SETUPSERVER_NOT_FOUND, addr, port); addr, old_port == -1 ? port : old_port);
cmd_params_free(free_arg); cmd_params_free(free_arg);
return; return;
} }
@ -156,7 +168,8 @@ static void cmd_server_add_modify(const char *data, gboolean add)
rec->port = port; rec->port = port;
} else { } else {
newrec = FALSE; newrec = FALSE;
if (*portstr != '\0' || g_hash_table_lookup(optlist, "port")) old_chatnet = g_strdup(rec->chatnet);
old_port = rec->port;
rec->port = port; rec->port = port;
if (*password != '\0') g_free_and_null(rec->password); if (*password != '\0') g_free_and_null(rec->password);
@ -261,7 +274,13 @@ static void cmd_server_add_modify(const char *data, gboolean add)
signal_emit("server add fill", 3, rec, optlist, GINT_TO_POINTER(add)); signal_emit("server add fill", 3, rec, optlist, GINT_TO_POINTER(add));
if (newrec) {
server_setup_add(rec); server_setup_add(rec);
} else {
server_setup_modify(rec, old_port, old_chatnet);
g_free(old_chatnet);
}
printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
TXT_SETUPSERVER_ADDED, addr, port); TXT_SETUPSERVER_ADDED, addr, port);