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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
/*
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
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "module.h"
#include "signals.h"
#include "settings.h"
#include "misc.h"
#include "irc-servers.h"
#include "irc-channels.h"
#include "irc-commands.h"
#include "channel-rejoin.h"
#define REJOIN_TIMEOUT (1000*60*5) /* try to rejoin every 5 minutes */
static int rejoin_tag;
static void rejoin_destroy(IRC_SERVER_REC *server, REJOIN_REC *rec)
{
g_return_if_fail(IS_IRC_SERVER(server));
g_return_if_fail(rec != NULL);
server->rejoin_channels =
g_slist_remove(server->rejoin_channels, rec);
signal_emit("channel rejoin remove", 2, server, rec);
g_free(rec->channel);
g_free_not_null(rec->key);
g_free(rec);
}
static REJOIN_REC *rejoin_find(IRC_SERVER_REC *server, const char *channel)
{
GSList *tmp;
g_return_val_if_fail(IS_IRC_SERVER(server), NULL);
g_return_val_if_fail(channel != NULL, NULL);
for (tmp = server->rejoin_channels; tmp != NULL; tmp = tmp->next) {
REJOIN_REC *rec = tmp->data;
if (g_strcasecmp(rec->channel, channel) == 0)
return rec;
}
return NULL;
}
#define channel_have_key(chan) \
((chan) != NULL && (chan)->key != NULL && (chan)->key[0] != '\0')
static void channel_rejoin(IRC_SERVER_REC *server, const char *channel)
{
IRC_CHANNEL_REC *chanrec;
REJOIN_REC *rec;
g_return_if_fail(IS_IRC_SERVER(server));
g_return_if_fail(channel != NULL);
if (!settings_get_bool("channels_rejoin_unavailable"))
return;
chanrec = irc_channel_find(server, channel);
if (chanrec == NULL || chanrec->joined) return;
rec = rejoin_find(server, channel);
if (rec != NULL) {
/* already exists */
rec->joining = FALSE;
/* update channel key */
g_free_and_null(rec->key);
if (channel_have_key(chanrec))
rec->key = g_strdup(chanrec->key);
} else {
/* new rejoin */
rec = g_new0(REJOIN_REC, 1);
rec->channel = g_strdup(channel);
if (channel_have_key(chanrec))
rec->key = g_strdup(chanrec->key);
server->rejoin_channels =
g_slist_append(server->rejoin_channels, rec);
signal_emit("channel rejoin new", 2, server, rec);
}
chanrec->left = TRUE;
channel_destroy(CHANNEL(chanrec));
}
static void event_duplicate_channel(IRC_SERVER_REC *server, const char *data)
{
CHANNEL_REC *chanrec;
char *params, *channel, *p;
g_return_if_fail(data != NULL);
params = event_get_params(data, 3, NULL, NULL, &channel);
p = strchr(channel, ' ');
if (p != NULL) *p = '\0';
if (channel[0] == '!' && channel[1] != '!') {
chanrec = channel_find(SERVER(server), channel);
if (chanrec != NULL && !chanrec->names_got) {
/* duplicate channel - this should only happen when
there's some sync problem with servers, rejoining
after a while should help.
note that this same 407 is sent when trying to
create !!channel that already exists so we don't
want to try rejoining then. */
channel_rejoin(server, channel);
signal_stop();
}
}
g_free(params);
}
static void event_target_unavailable(IRC_SERVER_REC *server, const char *data)
{
char *params, *channel;
IRC_CHANNEL_REC *chanrec;
g_return_if_fail(data != NULL);
params = event_get_params(data, 2, NULL, &channel);
if (ischannel(*channel)) {
chanrec = irc_channel_find(server, channel);
if (chanrec != NULL && chanrec->joined) {
/* dalnet event - can't change nick while
banned in channel */
} else {
/* channel is unavailable - try to join again
a bit later */
channel_rejoin(server, channel);
signal_stop();
}
}
g_free(params);
}
/* join ok/failed - remove from rejoins list. this happens always after join
except if the "target unavailable" error happens again */
static void sig_remove_rejoin(IRC_CHANNEL_REC *channel)
{
REJOIN_REC *rec;
if (!IS_IRC_CHANNEL(channel))
return;
rec = rejoin_find(channel->server, channel->name);
if (rec != NULL && rec->joining) {
/* join failed, remove the rejoin */
rejoin_destroy(channel->server, rec);
}
}
static void sig_disconnected(IRC_SERVER_REC *server)
{
if (!IS_IRC_SERVER(server))
return;
while (server->rejoin_channels != NULL)
rejoin_destroy(server, server->rejoin_channels->data);
}
static void server_rejoin_channels(IRC_SERVER_REC *server)
{
GSList *tmp, *next;
GString *channels, *keys;
int use_keys;
g_return_if_fail(IS_IRC_SERVER(server));
channels = g_string_new(NULL);
keys = g_string_new(NULL);
use_keys = FALSE;
for (tmp = server->rejoin_channels; tmp != NULL; tmp = next) {
REJOIN_REC *rec = tmp->data;
next = tmp->next;
if (rec->joining) {
/* we missed the join (failed) message,
remove from rejoins.. */
rejoin_destroy(server, rec);
continue;
}
rec->joining = TRUE;
g_string_sprintfa(channels, "%s,", rec->channel);
if (rec->key == NULL)
g_string_append(keys, "x,");
else {
g_string_sprintfa(keys, "%s,", rec->key);
use_keys = TRUE;
}
}
if (channels->len > 0) {
g_string_truncate(channels, channels->len-1);
g_string_truncate(keys, keys->len-1);
if (use_keys) g_string_sprintfa(channels, " %s", keys->str);
server->channels_join(SERVER(server), channels->str, TRUE);
}
g_string_free(channels, TRUE);
g_string_free(keys, TRUE);
}
static int sig_rejoin(void)
{
GSList *tmp;
for (tmp = servers; tmp != NULL; tmp = tmp->next) {
IRC_SERVER_REC *rec = tmp->data;
if (IS_IRC_SERVER(rec) && rec->rejoin_channels != NULL)
server_rejoin_channels(rec);
}
return TRUE;
}
static void cmd_rmrejoins(const char *data, IRC_SERVER_REC *server)
{
CMD_IRC_SERVER(server);
while (server->rejoin_channels != NULL)
rejoin_destroy(server, server->rejoin_channels->data);
}
void channel_rejoin_init(void)
{
settings_add_bool("servers", "channels_rejoin_unavailable", TRUE);
rejoin_tag = g_timeout_add(REJOIN_TIMEOUT,
(GSourceFunc) sig_rejoin, NULL);
command_bind_irc("rmrejoins", NULL, (SIGNAL_FUNC) cmd_rmrejoins);
signal_add_first("event 407", (SIGNAL_FUNC) event_duplicate_channel);
signal_add_first("event 437", (SIGNAL_FUNC) event_target_unavailable);
signal_add_first("channel joined", (SIGNAL_FUNC) sig_remove_rejoin);
signal_add_first("channel destroyed", (SIGNAL_FUNC) sig_remove_rejoin);
signal_add("server disconnected", (SIGNAL_FUNC) sig_disconnected);
}
void channel_rejoin_deinit(void)
{
g_source_remove(rejoin_tag);
command_unbind("rmrejoins", (SIGNAL_FUNC) cmd_rmrejoins);
signal_remove("event 407", (SIGNAL_FUNC) event_duplicate_channel);
signal_remove("event 437", (SIGNAL_FUNC) event_target_unavailable);
signal_remove("channel joined", (SIGNAL_FUNC) sig_remove_rejoin);
signal_remove("channel destroyed", (SIGNAL_FUNC) sig_remove_rejoin);
signal_remove("server disconnected", (SIGNAL_FUNC) sig_disconnected);
}
|