2000-08-26 11:39:44 -04:00
|
|
|
/*
|
|
|
|
channels-setup.c : irssi
|
|
|
|
|
|
|
|
Copyright (C) 1999-2000 Timo Sirainen
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
2007-05-08 14:41:10 -04:00
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2000-08-26 11:39:44 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "module.h"
|
|
|
|
#include "signals.h"
|
|
|
|
#include "lib-config/iconfig.h"
|
|
|
|
#include "settings.h"
|
|
|
|
|
2001-02-17 14:44:22 -05:00
|
|
|
#include "chat-protocols.h"
|
|
|
|
#include "chatnets.h"
|
2000-08-26 11:39:44 -04:00
|
|
|
#include "servers-setup.h"
|
2001-02-17 14:44:22 -05:00
|
|
|
#include "channels-setup.h"
|
2000-08-26 11:39:44 -04:00
|
|
|
|
|
|
|
GSList *setupchannels;
|
|
|
|
|
2015-12-09 09:43:31 -05:00
|
|
|
static int compare_channel_setup (CONFIG_NODE *node, CHANNEL_SETUP_REC *channel)
|
2015-12-06 16:31:49 -05:00
|
|
|
{
|
|
|
|
char *name, *chatnet;
|
|
|
|
|
|
|
|
name = config_node_get_str(node, "name", NULL);
|
|
|
|
chatnet = config_node_get_str(node, "chatnet", NULL);
|
|
|
|
|
2015-12-09 10:16:03 -05:00
|
|
|
if (g_strcmp0(name, channel->name) != 0 ||
|
|
|
|
g_strcmp0(chatnet, channel->chatnet) != 0)
|
2015-12-09 10:02:37 -05:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
2015-12-06 16:31:49 -05:00
|
|
|
}
|
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
static void channel_setup_save(CHANNEL_SETUP_REC *channel)
|
|
|
|
{
|
2015-12-09 10:02:37 -05:00
|
|
|
CONFIG_NODE *parent_node, *node;
|
2015-12-06 16:31:49 -05:00
|
|
|
GSList *config_node;
|
2000-08-26 11:39:44 -04:00
|
|
|
|
2015-12-09 10:02:37 -05:00
|
|
|
parent_node = iconfig_node_traverse("(channels", TRUE);
|
2015-12-06 16:31:49 -05:00
|
|
|
|
|
|
|
/* Try to find this channel in the configuration */
|
2015-12-09 10:02:37 -05:00
|
|
|
config_node = g_slist_find_custom(parent_node->value, channel,
|
2015-12-09 09:43:31 -05:00
|
|
|
(GCompareFunc)compare_channel_setup);
|
2015-12-06 16:31:49 -05:00
|
|
|
if (config_node != NULL)
|
|
|
|
/* Let's update this channel record */
|
|
|
|
node = config_node->data;
|
|
|
|
else
|
|
|
|
/* Create a brand-new channel record */
|
2015-12-09 10:02:37 -05:00
|
|
|
node = iconfig_node_section(parent_node, NULL, NODE_TYPE_BLOCK);
|
2000-08-26 11:39:44 -04:00
|
|
|
|
|
|
|
iconfig_node_clear(node);
|
|
|
|
iconfig_node_set_str(node, "name", channel->name);
|
|
|
|
iconfig_node_set_str(node, "chatnet", channel->chatnet);
|
|
|
|
if (channel->autojoin)
|
2000-11-26 05:24:30 -05:00
|
|
|
iconfig_node_set_bool(node, "autojoin", TRUE);
|
2000-08-26 11:39:44 -04:00
|
|
|
iconfig_node_set_str(node, "password", channel->password);
|
|
|
|
iconfig_node_set_str(node, "botmasks", channel->botmasks);
|
|
|
|
iconfig_node_set_str(node, "autosendcmd", channel->autosendcmd);
|
|
|
|
}
|
|
|
|
|
2001-02-17 14:44:22 -05:00
|
|
|
void channel_setup_create(CHANNEL_SETUP_REC *channel)
|
2000-08-26 11:39:44 -04:00
|
|
|
{
|
2002-05-17 23:26:04 -04:00
|
|
|
channel->type = module_get_uniq_id("CHANNEL SETUP", 0);
|
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
if (g_slist_find(setupchannels, channel) == NULL)
|
|
|
|
setupchannels = g_slist_append(setupchannels, channel);
|
|
|
|
channel_setup_save(channel);
|
|
|
|
|
|
|
|
signal_emit("channel setup created", 1, channel);
|
|
|
|
}
|
|
|
|
|
2001-02-17 14:44:22 -05:00
|
|
|
static void channel_config_remove(CHANNEL_SETUP_REC *channel)
|
|
|
|
{
|
2015-12-09 10:02:37 -05:00
|
|
|
CONFIG_NODE *parent_node;
|
2015-12-06 16:31:49 -05:00
|
|
|
GSList *config_node;
|
|
|
|
|
2015-12-09 10:02:37 -05:00
|
|
|
parent_node = iconfig_node_traverse("channels", FALSE);
|
2015-12-06 16:31:49 -05:00
|
|
|
|
2015-12-09 10:02:37 -05:00
|
|
|
if (parent_node == NULL)
|
2015-12-06 16:31:49 -05:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* Try to find this channel in the configuration */
|
2015-12-09 10:02:37 -05:00
|
|
|
config_node = g_slist_find_custom(parent_node->value, channel,
|
2015-12-09 09:43:31 -05:00
|
|
|
(GCompareFunc)compare_channel_setup);
|
2001-02-17 14:44:22 -05:00
|
|
|
|
2015-12-06 16:31:49 -05:00
|
|
|
if (config_node != NULL)
|
|
|
|
/* Delete the channel from the configuration */
|
2015-12-09 10:02:37 -05:00
|
|
|
iconfig_node_remove(parent_node, config_node->data);
|
2001-02-17 14:44:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void channel_setup_destroy(CHANNEL_SETUP_REC *channel)
|
2000-08-26 11:39:44 -04:00
|
|
|
{
|
|
|
|
g_return_if_fail(channel != NULL);
|
|
|
|
|
|
|
|
setupchannels = g_slist_remove(setupchannels, channel);
|
|
|
|
signal_emit("channel setup destroyed", 1, channel);
|
|
|
|
|
2000-09-26 17:39:33 -04:00
|
|
|
g_free_not_null(channel->chatnet);
|
2000-08-26 11:39:44 -04:00
|
|
|
g_free_not_null(channel->password);
|
|
|
|
g_free_not_null(channel->botmasks);
|
|
|
|
g_free_not_null(channel->autosendcmd);
|
2001-02-17 14:44:22 -05:00
|
|
|
g_free(channel->name);
|
2000-08-26 11:39:44 -04:00
|
|
|
g_free(channel);
|
|
|
|
}
|
|
|
|
|
2015-10-01 16:36:02 -04:00
|
|
|
void channel_setup_remove_chatnet(const char *chatnet)
|
|
|
|
{
|
|
|
|
GSList *tmp, *next;
|
|
|
|
|
|
|
|
g_return_if_fail(chatnet != NULL);
|
|
|
|
|
|
|
|
for (tmp = setupchannels; tmp != NULL; tmp = next) {
|
|
|
|
CHANNEL_SETUP_REC *rec = tmp->data;
|
|
|
|
|
|
|
|
next = tmp->next;
|
|
|
|
if (g_ascii_strcasecmp(rec->chatnet, chatnet) == 0)
|
|
|
|
channel_setup_remove(rec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-17 14:44:22 -05:00
|
|
|
void channel_setup_remove(CHANNEL_SETUP_REC *channel)
|
2000-08-26 11:39:44 -04:00
|
|
|
{
|
|
|
|
channel_config_remove(channel);
|
2001-02-17 14:44:22 -05:00
|
|
|
channel_setup_destroy(channel);
|
2000-08-26 11:39:44 -04:00
|
|
|
}
|
|
|
|
|
2001-02-17 14:44:22 -05:00
|
|
|
CHANNEL_SETUP_REC *channel_setup_find(const char *channel,
|
|
|
|
const char *chatnet)
|
2000-08-26 11:39:44 -04:00
|
|
|
{
|
|
|
|
GSList *tmp;
|
|
|
|
|
|
|
|
g_return_val_if_fail(channel != NULL, NULL);
|
|
|
|
|
|
|
|
for (tmp = setupchannels; tmp != NULL; tmp = tmp->next) {
|
|
|
|
CHANNEL_SETUP_REC *rec = tmp->data;
|
|
|
|
|
2014-06-10 12:06:19 -04:00
|
|
|
if (g_ascii_strcasecmp(rec->name, channel) == 0 &&
|
2000-08-26 11:39:44 -04:00
|
|
|
channel_chatnet_match(rec->chatnet, chatnet))
|
|
|
|
return rec;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-02-17 14:44:22 -05:00
|
|
|
static CHANNEL_SETUP_REC *channel_setup_read(CONFIG_NODE *node)
|
|
|
|
{
|
|
|
|
CHANNEL_SETUP_REC *rec;
|
|
|
|
CHATNET_REC *chatnetrec;
|
|
|
|
char *channel, *chatnet;
|
|
|
|
|
|
|
|
g_return_val_if_fail(node != NULL, NULL);
|
|
|
|
|
|
|
|
channel = config_node_get_str(node, "name", NULL);
|
|
|
|
chatnet = config_node_get_str(node, "chatnet", NULL);
|
|
|
|
|
|
|
|
chatnetrec = chatnet == NULL ? NULL : chatnet_find(chatnet);
|
|
|
|
if (channel == NULL || chatnetrec == NULL) {
|
|
|
|
/* missing information.. */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
rec = CHAT_PROTOCOL(chatnetrec)->create_channel_setup();
|
|
|
|
rec->type = module_get_uniq_id("CHANNEL SETUP", 0);
|
|
|
|
rec->chat_type = CHAT_PROTOCOL(chatnetrec)->id;
|
|
|
|
rec->autojoin = config_node_get_bool(node, "autojoin", FALSE);
|
|
|
|
rec->name = g_strdup(channel);
|
|
|
|
rec->chatnet = g_strdup(chatnetrec != NULL ? chatnetrec->name : chatnet);
|
|
|
|
rec->password = g_strdup(config_node_get_str(node, "password", NULL));
|
|
|
|
rec->botmasks = g_strdup(config_node_get_str(node, "botmasks", NULL));
|
|
|
|
rec->autosendcmd = g_strdup(config_node_get_str(node, "autosendcmd", NULL));
|
|
|
|
|
|
|
|
setupchannels = g_slist_append(setupchannels, rec);
|
|
|
|
signal_emit("channel setup created", 2, rec, node);
|
|
|
|
return rec;
|
|
|
|
}
|
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
static void channels_read_config(void)
|
|
|
|
{
|
|
|
|
CONFIG_NODE *node;
|
|
|
|
GSList *tmp;
|
|
|
|
|
|
|
|
while (setupchannels != NULL)
|
2001-02-17 14:44:22 -05:00
|
|
|
channel_setup_destroy(setupchannels->data);
|
2000-08-26 11:39:44 -04:00
|
|
|
|
|
|
|
/* Read channels */
|
|
|
|
node = iconfig_node_traverse("channels", FALSE);
|
|
|
|
if (node != NULL) {
|
2002-02-02 12:37:44 -05:00
|
|
|
tmp = config_node_first(node->value);
|
|
|
|
for (; tmp != NULL; tmp = config_node_next(tmp))
|
2000-08-26 11:39:44 -04:00
|
|
|
channel_setup_read(tmp->data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void channels_setup_init(void)
|
|
|
|
{
|
2001-02-17 14:44:22 -05:00
|
|
|
setupchannels = NULL;
|
2000-08-26 11:39:44 -04:00
|
|
|
source_host_ok = FALSE;
|
|
|
|
|
|
|
|
signal_add("setup reread", (SIGNAL_FUNC) channels_read_config);
|
|
|
|
signal_add("irssi init read settings", (SIGNAL_FUNC) channels_read_config);
|
|
|
|
}
|
|
|
|
|
|
|
|
void channels_setup_deinit(void)
|
|
|
|
{
|
|
|
|
while (setupchannels != NULL)
|
2001-02-17 14:44:22 -05:00
|
|
|
channel_setup_destroy(setupchannels->data);
|
2000-08-26 11:39:44 -04:00
|
|
|
|
|
|
|
signal_remove("setup reread", (SIGNAL_FUNC) channels_read_config);
|
|
|
|
signal_remove("irssi init read settings", (SIGNAL_FUNC) channels_read_config);
|
|
|
|
}
|