summaryrefslogtreecommitdiff
path: root/src/irc/core/channel-rejoin.c
blob: 89abae0c6aaa74f86bfa536268c76f2ba1f12042 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 channel-rejoin.c : rejoin to channel if it's "temporarily unavailable"
                    this has nothing to do with autorejoin if kicked

    Copyright (C) 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"
#include "channels.h"
#include "irc.h"

#define REJOIN_TIMEOUT (1000*60*5) /* try to rejoin every 5 minutes */

static int rejoin_tag;

static void channel_rejoin(IRC_SERVER_REC *server, const char *channel)
{
	CHANNEL_REC *chanrec;
	char *str;

	chanrec = channel_find(server, channel);
	str = chanrec == NULL || chanrec->key == NULL || *chanrec->key == '\0' ?
		g_strdup(channel) : g_strdup_printf("%s %s", channel, chanrec->key);

	server->rejoin_channels = g_slist_append(server->rejoin_channels, str);
}

static void event_target_unavailable(const char *data, IRC_SERVER_REC *server)
{
	char *params, *channel;

	g_return_if_fail(data != NULL);

	params = event_get_params(data, 2, NULL, &channel);
	if (ischannel(*channel)) {
		/* channel is unavailable - try to join again a bit later */
		channel_rejoin(server, channel);
	}

	g_free(params);
}

static void sig_disconnected(IRC_SERVER_REC *server)
{
	g_slist_foreach(server->rejoin_channels, (GFunc) g_free, NULL);
	g_slist_free(server->rejoin_channels);
}

static void server_rejoin_channels(IRC_SERVER_REC *server)
{
	while (server->rejoin_channels != NULL) {
		char *channel = server->rejoin_channels->data;

                channels_join(server, channel, TRUE);
		server->rejoin_channels = g_slist_remove(server->rejoin_channels, channel);
	}
}

static int sig_rejoin(void)
{
	GSList *tmp;

	for (tmp = servers; tmp != NULL; tmp = tmp->next) {
		IRC_SERVER_REC *rec = tmp->data;

		if (irc_server_check(rec))
			server_rejoin_channels(rec);
	}

	return TRUE;
}

void channel_rejoin_init(void)
{
	rejoin_tag = g_timeout_add(REJOIN_TIMEOUT, (GSourceFunc) sig_rejoin, NULL);

	signal_add_first("event 437", (SIGNAL_FUNC) event_target_unavailable);
	signal_add("server disconnected", (SIGNAL_FUNC) sig_disconnected);
}

void channel_rejoin_deinit(void)
{
	g_source_remove(rejoin_tag);

	signal_remove("event 437", (SIGNAL_FUNC) event_target_unavailable);
	signal_remove("server disconnected", (SIGNAL_FUNC) sig_disconnected);
}