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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
/*
massjoin.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.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "module.h"
#include <irssi/src/core/signals.h>
#include <irssi/src/core/settings.h>
#include <irssi/src/irc/core/irc-servers.h>
#include <irssi/src/irc/core/irc-channels.h>
#include <irssi/src/irc/core/irc-nicklist.h>
static int massjoin_tag;
static int massjoin_max_joins;
/* Massjoin support - really useful when trying to do things (like op/deop)
to people after netjoins. It sends
"massjoin #channel nick!user@host nick2!user@host ..." signals */
static void event_join(IRC_SERVER_REC *server, const char *data,
const char *nick, const char *address)
{
char *params, *channel, *account, *realname, *ptr;
IRC_CHANNEL_REC *chanrec;
NICK_REC *nickrec;
GSList *nicks, *tmp;
gboolean send_massjoin;
g_return_if_fail(data != NULL);
params = event_get_params(data, 3, &channel, &account, &realname);
ptr = strchr(channel, 7); /* ^G does something weird.. */
if (ptr != NULL) *ptr = '\0';
/* find channel */
chanrec = irc_channel_find(server, channel);
if (chanrec == NULL) {
g_free(params);
return;
}
if (g_ascii_strcasecmp(nick, server->nick) == 0) {
/* do not overwrite our /UPGRADEd ownnick */
if (chanrec->session_rejoin) {
g_free(params);
return;
} else {
/* You joined, do not massjoin */
send_massjoin = FALSE;
}
} else {
send_massjoin = TRUE;
}
/* check that the nick isn't already in nicklist. seems to happen
sometimes (server desyncs or something?) */
nickrec = nicklist_find(CHANNEL(chanrec), nick);
if (nickrec != NULL) {
/* destroy the old record */
nicklist_remove(CHANNEL(chanrec), nickrec);
}
/* add user to nicklist */
nickrec = irc_nicklist_insert(chanrec, nick, FALSE, FALSE, FALSE, send_massjoin, NULL);
if (nickrec == NULL) {
/* invalid nick? */
g_free(params);
return;
}
if (*account != '\0' && g_strcmp0(nickrec->account, account) != 0) {
nicklist_set_account(CHANNEL(chanrec), nickrec, account);
}
nicklist_set_host(CHANNEL(chanrec), nickrec, address);
if (send_massjoin && chanrec->massjoins == 0) {
/* no nicks waiting in massjoin queue */
chanrec->massjoin_start = time(NULL);
chanrec->last_massjoins = 0;
}
if (nickrec->realname == NULL) {
/* Check if user is already in some other channel,
get the realname and other stuff from there */
nicks = nicklist_get_same(SERVER(server), nick);
for (tmp = nicks; tmp != NULL; tmp = tmp->next->next) {
NICK_REC *rec = tmp->next->data;
if (rec->realname != NULL) {
nickrec->last_check = rec->last_check;
nickrec->realname = g_strdup(rec->realname);
nickrec->gone = rec->gone;
nickrec->serverop = rec->serverop;
break;
}
}
g_slist_free(nicks);
}
if (*realname != '\0' && g_strcmp0(nickrec->realname, realname) != 0) {
g_free(nickrec->realname);
nickrec->realname = g_strdup(realname);
}
if (send_massjoin) {
chanrec->massjoins++;
}
g_free(params);
}
static void event_chghost(IRC_SERVER_REC *server, const char *data,
const char *nick, const char *old_address)
{
char *params, *user, *host, *address;
GSList *nicks, *tmp;
g_return_if_fail(nick != NULL);
g_return_if_fail(data != NULL);
params = event_get_params(data, 2, &user, &host);
/* check that the nick isn't already in nicklist. seems to happen
sometimes (server desyncs or something?) */
nicks = nicklist_get_same(SERVER(server), nick);
address = nicks != NULL ? g_strconcat(user, "@", host, NULL) : NULL;
for (tmp = nicks; tmp != NULL; tmp = tmp->next->next) {
NICK_REC *rec = tmp->next->data;
nicklist_set_host(CHANNEL(tmp->data), rec, address);
}
g_free(address);
g_slist_free(nicks);
g_free(params);
}
static void event_account(IRC_SERVER_REC *server, const char *data,
const char *nick, const char *address)
{
char *params, *account;
GSList *nicks, *tmp;
g_return_if_fail(nick != NULL);
g_return_if_fail(data != NULL);
params = event_get_params(data, 1, &account);
nicks = nicklist_get_same(SERVER(server), nick);
for (tmp = nicks; tmp != NULL; tmp = tmp->next->next) {
NICK_REC *rec = tmp->next->data;
nicklist_set_account(CHANNEL(tmp->data), rec, account);
}
g_slist_free(nicks);
g_free(params);
}
static void event_part(IRC_SERVER_REC *server, const char *data,
const char *nick, const char *addr)
{
char *params, *channel, *reason;
IRC_CHANNEL_REC *chanrec;
NICK_REC *nickrec;
g_return_if_fail(data != NULL);
if (g_ascii_strcasecmp(nick, server->nick) == 0) {
/* you left channel, no need to do anything here */
return;
}
params = event_get_params(data, 2, &channel, &reason);
/* find channel */
chanrec = irc_channel_find(server, channel);
if (chanrec == NULL) {
g_free(params);
return;
}
/* remove user from nicklist */
nickrec = nicklist_find(CHANNEL(chanrec), nick);
if (nickrec != NULL) {
if (nickrec->send_massjoin) {
/* quick join/part after which it's useless to send
nick in massjoin */
chanrec->massjoins--;
}
nicklist_remove(CHANNEL(chanrec), nickrec);
}
g_free(params);
}
static void event_quit(IRC_SERVER_REC *server, const char *data,
const char *nick)
{
IRC_CHANNEL_REC *channel;
NICK_REC *nickrec;
GSList *nicks, *tmp;
g_return_if_fail(data != NULL);
if (g_ascii_strcasecmp(nick, server->nick) == 0) {
/* you quit, don't do anything here */
return;
}
/* Remove nick from all channels */
nicks = nicklist_get_same(SERVER(server), nick);
for (tmp = nicks; tmp != NULL; tmp = tmp->next->next) {
channel = tmp->data;
nickrec = tmp->next->data;
if (nickrec->send_massjoin) {
/* quick join/quit after which it's useless to
send nick in massjoin */
channel->massjoins--;
}
nicklist_remove(CHANNEL(channel), nickrec);
}
g_slist_free(nicks);
/* invalidate any outstanding accountqueries for the nick */
irc_channels_query_purge_accountquery(server, nick);
}
static void event_kick(IRC_SERVER_REC *server, const char *data)
{
char *params, *channel, *nick, *reason;
IRC_CHANNEL_REC *chanrec;
NICK_REC *nickrec;
g_return_if_fail(data != NULL);
params = event_get_params(data, 3, &channel, &nick, &reason);
if (g_ascii_strcasecmp(nick, server->nick) == 0) {
/* you were kicked, no need to do anything */
g_free(params);
return;
}
/* Remove user from nicklist */
chanrec = irc_channel_find(server, channel);
nickrec = chanrec == NULL ? NULL :
nicklist_find(CHANNEL(chanrec), nick);
if (chanrec != NULL && nickrec != NULL) {
if (nickrec->send_massjoin) {
/* quick join/kick after which it's useless to
send nick in massjoin */
chanrec->massjoins--;
}
nicklist_remove(CHANNEL(chanrec), nickrec);
}
g_free(params);
}
static void massjoin_send_hash(gpointer key, NICK_REC *nick, GSList **list)
{
if (nick->send_massjoin) {
nick->send_massjoin = FALSE;
*list = g_slist_append(*list, nick);
}
}
/* Send channel's massjoin list signal */
static void massjoin_send(IRC_CHANNEL_REC *channel)
{
GSList *list;
list = NULL;
g_hash_table_foreach(channel->nicks, (GHFunc) massjoin_send_hash, &list);
channel->massjoins = 0;
signal_emit("massjoin", 2, channel, list);
g_slist_free(list);
}
static void server_check_massjoins(IRC_SERVER_REC *server, time_t max)
{
GSList *tmp;
/*
1) First time always save massjoin count to last_massjoins
2) Next time check if there's been less than massjoin_max_joins
(yes, the name is misleading..) joins since previous check.
yes) send a massjoin signal and reset last_massjoin count
no) unless we've waited for massjoin_max_wait seconds already,
goto 2.
So, with single joins the massjoin signal is sent 1-2 seconds after
the join.
*/
/* Scan all channels through for massjoins */
for (tmp = server->channels; tmp != NULL; tmp = tmp->next) {
IRC_CHANNEL_REC *rec = tmp->data;
if (!IS_IRC_CHANNEL(rec) || rec->massjoins <= 0)
continue;
if (rec->massjoin_start < max || /* We've waited long enough */
(rec->last_massjoins > 0 &&
rec->massjoins-massjoin_max_joins < rec->last_massjoins)) { /* Less than x joins since last check */
/* send them */
massjoin_send(rec);
} else {
/* Wait for some more.. */
rec->last_massjoins = rec->massjoins;
}
}
}
static int sig_massjoin_timeout(void)
{
GSList *tmp;
time_t max;
max = time(NULL)-settings_get_int("massjoin_max_wait");
for (tmp = servers; tmp != NULL; tmp = tmp->next) {
IRC_SERVER_REC *server = tmp->data;
if (IS_IRC_SERVER(server))
server_check_massjoins(server, max);
}
return 1;
}
static void read_settings(void)
{
massjoin_max_joins = settings_get_int("massjoin_max_joins");
}
void massjoin_init(void)
{
settings_add_int("misc", "massjoin_max_wait", 5000);
settings_add_int("misc", "massjoin_max_joins", 3);
massjoin_tag = g_timeout_add(1000, (GSourceFunc) sig_massjoin_timeout, NULL);
read_settings();
signal_add_first("event join", (SIGNAL_FUNC) event_join);
signal_add("event chghost", (SIGNAL_FUNC) event_chghost);
signal_add("event account", (SIGNAL_FUNC) event_account);
signal_add("event part", (SIGNAL_FUNC) event_part);
signal_add("event kick", (SIGNAL_FUNC) event_kick);
signal_add("event quit", (SIGNAL_FUNC) event_quit);
signal_add("setup changed", (SIGNAL_FUNC) read_settings);
}
void massjoin_deinit(void)
{
g_source_remove(massjoin_tag);
signal_remove("event join", (SIGNAL_FUNC) event_join);
signal_remove("event chghost", (SIGNAL_FUNC) event_chghost);
signal_remove("event account", (SIGNAL_FUNC) event_account);
signal_remove("event part", (SIGNAL_FUNC) event_part);
signal_remove("event kick", (SIGNAL_FUNC) event_kick);
signal_remove("event quit", (SIGNAL_FUNC) event_quit);
signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
}
|