2000-08-26 11:39:44 -04:00
|
|
|
/*
|
|
|
|
channel.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.
|
|
|
|
|
|
|
|
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "module.h"
|
|
|
|
#include "signals.h"
|
|
|
|
#include "misc.h"
|
2000-09-30 18:49:48 -04:00
|
|
|
#include "special-vars.h"
|
2000-08-26 11:39:44 -04:00
|
|
|
|
2000-09-30 18:49:48 -04:00
|
|
|
#include "servers.h"
|
2000-08-26 11:39:44 -04:00
|
|
|
#include "channels.h"
|
2000-09-30 18:49:48 -04:00
|
|
|
#include "channels-setup.h"
|
|
|
|
#include "nicklist.h"
|
2000-08-26 11:39:44 -04:00
|
|
|
|
|
|
|
GSList *channels; /* List of all channels */
|
|
|
|
|
2001-05-11 12:08:48 -04:00
|
|
|
static char *get_join_data(CHANNEL_REC *channel)
|
|
|
|
{
|
|
|
|
return g_strdup(channel->name);
|
|
|
|
}
|
|
|
|
|
2002-05-15 20:34:37 -04:00
|
|
|
static const char *channel_get_target(WI_ITEM_REC *item)
|
|
|
|
{
|
|
|
|
return ((CHANNEL_REC *) item)->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void channel_init(CHANNEL_REC *channel, SERVER_REC *server, const char *name,
|
|
|
|
const char *visible_name, int automatic)
|
2000-08-26 11:39:44 -04:00
|
|
|
{
|
|
|
|
g_return_if_fail(channel != NULL);
|
2002-05-15 20:34:37 -04:00
|
|
|
g_return_if_fail(name != NULL);
|
|
|
|
g_return_if_fail(server != NULL);
|
2000-08-26 11:39:44 -04:00
|
|
|
|
2002-05-15 20:34:37 -04:00
|
|
|
if (visible_name == NULL)
|
|
|
|
visible_name = name;
|
2000-08-26 11:39:44 -04:00
|
|
|
|
|
|
|
MODULE_DATA_INIT(channel);
|
2000-09-02 14:53:58 -04:00
|
|
|
channel->type = module_get_uniq_id_str("WINDOW ITEM TYPE", "CHANNEL");
|
2001-07-25 21:49:08 -04:00
|
|
|
channel->destroy = (void (*) (WI_ITEM_REC *)) channel_destroy;
|
2002-05-15 20:34:37 -04:00
|
|
|
channel->get_target = channel_get_target;
|
2001-05-11 12:08:48 -04:00
|
|
|
channel->get_join_data = get_join_data;
|
2000-08-26 11:39:44 -04:00
|
|
|
|
2002-05-15 20:34:37 -04:00
|
|
|
channel->chat_type = server->chat_type;
|
|
|
|
channel->server = server;
|
|
|
|
channel->name = g_strdup(name);
|
|
|
|
channel->visible_name = g_strdup(visible_name);
|
|
|
|
channel->mode = g_strdup("");
|
|
|
|
channel->createtime = time(NULL);
|
|
|
|
|
|
|
|
channels = g_slist_append(channels, channel);
|
|
|
|
server->channels = g_slist_append(server->channels, channel);
|
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
signal_emit("channel created", 2, channel, GINT_TO_POINTER(automatic));
|
|
|
|
}
|
|
|
|
|
|
|
|
void channel_destroy(CHANNEL_REC *channel)
|
|
|
|
{
|
|
|
|
g_return_if_fail(IS_CHANNEL(channel));
|
|
|
|
|
|
|
|
if (channel->destroying) return;
|
|
|
|
channel->destroying = TRUE;
|
|
|
|
|
|
|
|
channels = g_slist_remove(channels, channel);
|
2002-05-28 16:36:42 -04:00
|
|
|
channel->server->channels =
|
|
|
|
g_slist_remove(channel->server->channels, channel);
|
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
signal_emit("channel destroyed", 1, channel);
|
|
|
|
|
|
|
|
MODULE_DATA_DEINIT(channel);
|
2001-03-16 21:32:32 -05:00
|
|
|
g_free_not_null(channel->hilight_color);
|
2000-08-26 11:39:44 -04:00
|
|
|
g_free_not_null(channel->topic);
|
2000-11-17 09:59:32 -05:00
|
|
|
g_free_not_null(channel->topic_by);
|
2000-08-26 11:39:44 -04:00
|
|
|
g_free_not_null(channel->key);
|
2000-08-31 20:26:46 -04:00
|
|
|
g_free(channel->mode);
|
2000-08-26 11:39:44 -04:00
|
|
|
g_free(channel->name);
|
2002-02-07 14:17:36 -05:00
|
|
|
|
|
|
|
channel->type = 0;
|
2000-08-26 11:39:44 -04:00
|
|
|
g_free(channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
static CHANNEL_REC *channel_find_server(SERVER_REC *server,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
GSList *tmp;
|
|
|
|
|
|
|
|
g_return_val_if_fail(IS_SERVER(server), NULL);
|
|
|
|
|
|
|
|
if (server->channel_find_func != NULL) {
|
|
|
|
/* use the server specific channel find function */
|
2000-09-30 18:49:48 -04:00
|
|
|
return server->channel_find_func(server, name);
|
2000-08-26 11:39:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (tmp = server->channels; tmp != NULL; tmp = tmp->next) {
|
|
|
|
CHANNEL_REC *rec = tmp->data;
|
|
|
|
|
2000-12-16 20:37:12 -05:00
|
|
|
if (g_strcasecmp(name, rec->name) == 0)
|
2000-08-26 11:39:44 -04:00
|
|
|
return rec;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHANNEL_REC *channel_find(SERVER_REC *server, const char *name)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail(server == NULL || IS_SERVER(server), NULL);
|
|
|
|
g_return_val_if_fail(name != NULL, NULL);
|
|
|
|
|
|
|
|
if (server != NULL)
|
|
|
|
return channel_find_server(server, name);
|
|
|
|
|
|
|
|
/* find from any server */
|
|
|
|
return gslist_foreach_find(servers,
|
|
|
|
(FOREACH_FIND_FUNC) channel_find_server,
|
|
|
|
(void *) name);
|
|
|
|
}
|
|
|
|
|
2002-05-17 11:22:05 -04:00
|
|
|
void channel_change_name(CHANNEL_REC *channel, const char *name)
|
|
|
|
{
|
|
|
|
g_return_if_fail(IS_CHANNEL(channel));
|
|
|
|
|
|
|
|
g_free(channel->name);
|
|
|
|
channel->name = g_strdup(name);
|
|
|
|
|
|
|
|
signal_emit("channel name changed", 1, channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
void channel_change_visible_name(CHANNEL_REC *channel, const char *name)
|
|
|
|
{
|
|
|
|
g_return_if_fail(IS_CHANNEL(channel));
|
|
|
|
|
|
|
|
g_free(channel->visible_name);
|
|
|
|
channel->visible_name = g_strdup(name);
|
|
|
|
|
|
|
|
signal_emit("window item name changed", 1, channel);
|
|
|
|
}
|
|
|
|
|
2001-10-21 07:08:49 -04:00
|
|
|
static CHANNEL_REC *channel_find_servers(GSList *servers, const char *name)
|
|
|
|
{
|
|
|
|
return gslist_foreach_find(servers,
|
|
|
|
(FOREACH_FIND_FUNC) channel_find_server,
|
|
|
|
(void *) name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GSList *servers_find_chatnet_except(SERVER_REC *server)
|
|
|
|
{
|
|
|
|
GSList *tmp, *list;
|
|
|
|
|
|
|
|
list = NULL;
|
|
|
|
for (tmp = servers; tmp != NULL; tmp = tmp->next) {
|
|
|
|
SERVER_REC *rec = tmp->data;
|
|
|
|
|
|
|
|
if (server != rec && rec->connrec->chatnet != NULL &&
|
|
|
|
strcmp(server->connrec->chatnet,
|
|
|
|
rec->connrec->chatnet) == 0) {
|
|
|
|
/* chatnets match */
|
|
|
|
list = g_slist_append(list, rec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2000-09-30 18:49:48 -04:00
|
|
|
/* connected to server, autojoin to channels. */
|
|
|
|
static void event_connected(SERVER_REC *server)
|
|
|
|
{
|
|
|
|
GString *chans;
|
2001-10-21 07:08:49 -04:00
|
|
|
GSList *tmp, *chatnet_servers;
|
2000-09-30 18:49:48 -04:00
|
|
|
|
|
|
|
g_return_if_fail(SERVER(server));
|
|
|
|
|
2001-11-19 09:02:27 -05:00
|
|
|
if (server->connrec->reconnection ||
|
|
|
|
server->connrec->no_autojoin_channels)
|
2000-09-30 18:49:48 -04:00
|
|
|
return;
|
|
|
|
|
2001-10-21 07:08:49 -04:00
|
|
|
/* get list of servers in same chat network */
|
|
|
|
chatnet_servers = server->connrec->chatnet == NULL ? NULL:
|
|
|
|
servers_find_chatnet_except(server);
|
|
|
|
|
2000-09-30 18:49:48 -04:00
|
|
|
/* join to the channels marked with autojoin in setup */
|
|
|
|
chans = g_string_new(NULL);
|
|
|
|
for (tmp = setupchannels; tmp != NULL; tmp = tmp->next) {
|
|
|
|
CHANNEL_SETUP_REC *rec = tmp->data;
|
|
|
|
|
|
|
|
if (!rec->autojoin ||
|
|
|
|
!channel_chatnet_match(rec->chatnet,
|
|
|
|
server->connrec->chatnet))
|
|
|
|
continue;
|
|
|
|
|
2001-10-21 07:08:49 -04:00
|
|
|
/* check that we haven't already joined this channel in
|
|
|
|
same chat network connection.. */
|
2001-10-22 05:33:32 -04:00
|
|
|
if (channel_find_servers(chatnet_servers, rec->name) == NULL)
|
2001-10-21 07:08:49 -04:00
|
|
|
g_string_sprintfa(chans, "%s,", rec->name);
|
2000-09-30 18:49:48 -04:00
|
|
|
}
|
2002-01-23 20:19:52 -05:00
|
|
|
g_slist_free(chatnet_servers);
|
2000-09-30 18:49:48 -04:00
|
|
|
|
|
|
|
if (chans->len > 0) {
|
|
|
|
g_string_truncate(chans, chans->len-1);
|
|
|
|
server->channels_join(server, chans->str, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_string_free(chans, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int match_nick_flags(SERVER_REC *server, NICK_REC *nick, char flag)
|
|
|
|
{
|
2000-10-13 21:43:05 -04:00
|
|
|
const char *flags = server->get_nick_flags();
|
2000-09-30 18:49:48 -04:00
|
|
|
|
2000-10-13 22:10:22 -04:00
|
|
|
return strchr(flags, flag) == NULL ||
|
|
|
|
(flag == flags[0] && nick->op) ||
|
2000-09-30 18:49:48 -04:00
|
|
|
(flag == flags[1] && (nick->voice || nick->halfop ||
|
|
|
|
nick->op)) ||
|
|
|
|
(flag == flags[2] && (nick->halfop || nick->op));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send the auto send command to channel */
|
|
|
|
void channel_send_autocommands(CHANNEL_REC *channel)
|
|
|
|
{
|
|
|
|
CHANNEL_SETUP_REC *rec;
|
|
|
|
NICK_REC *nick;
|
|
|
|
char **bots, **bot;
|
|
|
|
|
|
|
|
g_return_if_fail(IS_CHANNEL(channel));
|
|
|
|
|
2001-11-19 08:14:37 -05:00
|
|
|
if (channel->session_rejoin)
|
|
|
|
return;
|
|
|
|
|
2001-02-17 14:44:22 -05:00
|
|
|
rec = channel_setup_find(channel->name, channel->server->connrec->chatnet);
|
2000-09-30 18:49:48 -04:00
|
|
|
if (rec == NULL || rec->autosendcmd == NULL || !*rec->autosendcmd)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (rec->botmasks == NULL || !*rec->botmasks) {
|
|
|
|
/* just send the command. */
|
|
|
|
eval_special_string(rec->autosendcmd, "", channel->server, channel);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find first available bot.. */
|
|
|
|
bots = g_strsplit(rec->botmasks, " ", -1);
|
|
|
|
for (bot = bots; *bot != NULL; bot++) {
|
|
|
|
const char *botnick = *bot;
|
|
|
|
|
2001-11-19 18:12:00 -05:00
|
|
|
if (*botnick == '\0')
|
|
|
|
continue;
|
|
|
|
|
2001-01-27 20:45:31 -05:00
|
|
|
nick = nicklist_find_mask(channel,
|
|
|
|
channel->server->isnickflag(*botnick) ?
|
|
|
|
botnick+1 : botnick);
|
|
|
|
if (nick != NULL &&
|
|
|
|
match_nick_flags(channel->server, nick, *botnick)) {
|
|
|
|
eval_special_string(rec->autosendcmd, nick->nick,
|
|
|
|
channel->server, channel);
|
|
|
|
break;
|
|
|
|
}
|
2000-09-30 18:49:48 -04:00
|
|
|
}
|
|
|
|
g_strfreev(bots);
|
|
|
|
}
|
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
void channels_init(void)
|
|
|
|
{
|
2000-10-13 21:43:05 -04:00
|
|
|
channels_setup_init();
|
|
|
|
|
2000-09-30 18:49:48 -04:00
|
|
|
signal_add("event connected", (SIGNAL_FUNC) event_connected);
|
2000-08-26 11:39:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void channels_deinit(void)
|
|
|
|
{
|
2000-10-13 21:43:05 -04:00
|
|
|
channels_setup_deinit();
|
|
|
|
|
2000-09-30 18:49:48 -04:00
|
|
|
signal_remove("event connected", (SIGNAL_FUNC) event_connected);
|
2000-08-26 11:39:44 -04:00
|
|
|
}
|