]>
Commit | Line | Data |
---|---|---|
212380e3 AC |
1 | /* |
2 | * ircd-ratbox: A slightly useful ircd. | |
3 | * channel.c: Controls channels. | |
4 | * | |
5 | * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center | |
6 | * Copyright (C) 1996-2002 Hybrid Development Team | |
7 | * Copyright (C) 2002-2005 ircd-ratbox development team | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
22 | * USA | |
23 | * | |
83294285 | 24 | * $Id: channel.c 3580 2007-11-07 23:45:14Z jilles $ |
212380e3 AC |
25 | */ |
26 | ||
27 | #include "stdinc.h" | |
212380e3 AC |
28 | #include "channel.h" |
29 | #include "client.h" | |
30 | #include "common.h" | |
31 | #include "hash.h" | |
32 | #include "hook.h" | |
4562c604 | 33 | #include "match.h" |
212380e3 AC |
34 | #include "ircd.h" |
35 | #include "numeric.h" | |
36 | #include "s_serv.h" /* captab */ | |
37 | #include "s_user.h" | |
38 | #include "send.h" | |
39 | #include "whowas.h" | |
40 | #include "s_conf.h" /* ConfigFileEntry, ConfigChannel */ | |
41 | #include "s_newconf.h" | |
4016731b | 42 | #include "logger.h" |
212380e3 | 43 | |
212380e3 AC |
44 | static int channel_capabs[] = { CAP_EX, CAP_IE, |
45 | CAP_SERVICE, | |
46 | CAP_TS6 | |
47 | }; | |
48 | ||
49 | #define NCHCAPS (sizeof(channel_capabs)/sizeof(int)) | |
50 | #define NCHCAP_COMBOS (1 << NCHCAPS) | |
51 | ||
52 | static struct ChCapCombo chcap_combos[NCHCAP_COMBOS]; | |
53 | ||
54 | static void free_topic(struct Channel *chptr); | |
55 | ||
56 | static int h_can_join; | |
57 | ||
58 | /* init_channels() | |
59 | * | |
60 | * input - | |
61 | * output - | |
62 | * side effects - initialises the various blockheaps | |
63 | */ | |
64 | void | |
65 | init_channels(void) | |
66 | { | |
c608a061 AC |
67 | channel_heap = rb_bh_create(sizeof(struct Channel), CHANNEL_HEAP_SIZE, "channel_heap"); |
68 | ban_heap = rb_bh_create(sizeof(struct Ban), BAN_HEAP_SIZE, "ban_heap"); | |
69 | topic_heap = rb_bh_create(TOPICLEN + 1 + USERHOST_REPLYLEN, TOPIC_HEAP_SIZE, "topic_heap"); | |
70 | member_heap = rb_bh_create(sizeof(struct membership), MEMBER_HEAP_SIZE, "member_heap"); | |
212380e3 AC |
71 | |
72 | h_can_join = register_hook("can_join"); | |
73 | } | |
74 | ||
75 | /* | |
76 | * allocate_channel - Allocates a channel | |
77 | */ | |
78 | struct Channel * | |
79 | allocate_channel(const char *chname) | |
80 | { | |
81 | struct Channel *chptr; | |
398b6a73 | 82 | chptr = rb_bh_alloc(channel_heap); |
47a03750 | 83 | chptr->chname = rb_strdup(chname); |
212380e3 AC |
84 | return (chptr); |
85 | } | |
86 | ||
87 | void | |
88 | free_channel(struct Channel *chptr) | |
89 | { | |
637c4932 | 90 | rb_free(chptr->chname); |
398b6a73 | 91 | rb_bh_free(channel_heap, chptr); |
212380e3 AC |
92 | } |
93 | ||
94 | struct Ban * | |
95 | allocate_ban(const char *banstr, const char *who) | |
96 | { | |
97 | struct Ban *bptr; | |
398b6a73 | 98 | bptr = rb_bh_alloc(ban_heap); |
47a03750 VY |
99 | bptr->banstr = rb_strdup(banstr); |
100 | bptr->who = rb_strdup(who); | |
212380e3 AC |
101 | |
102 | return (bptr); | |
103 | } | |
104 | ||
105 | void | |
106 | free_ban(struct Ban *bptr) | |
107 | { | |
637c4932 VY |
108 | rb_free(bptr->banstr); |
109 | rb_free(bptr->who); | |
398b6a73 | 110 | rb_bh_free(ban_heap, bptr); |
212380e3 AC |
111 | } |
112 | ||
113 | ||
114 | /* find_channel_membership() | |
115 | * | |
116 | * input - channel to find them in, client to find | |
117 | * output - membership of client in channel, else NULL | |
118 | * side effects - | |
119 | */ | |
120 | struct membership * | |
121 | find_channel_membership(struct Channel *chptr, struct Client *client_p) | |
122 | { | |
123 | struct membership *msptr; | |
330fc5c1 | 124 | rb_dlink_node *ptr; |
212380e3 AC |
125 | |
126 | if(!IsClient(client_p)) | |
127 | return NULL; | |
128 | ||
129 | /* Pick the most efficient list to use to be nice to things like | |
130 | * CHANSERV which could be in a large number of channels | |
131 | */ | |
330fc5c1 | 132 | if(rb_dlink_list_length(&chptr->members) < rb_dlink_list_length(&client_p->user->channel)) |
212380e3 | 133 | { |
5cefa1d6 | 134 | RB_DLINK_FOREACH(ptr, chptr->members.head) |
212380e3 AC |
135 | { |
136 | msptr = ptr->data; | |
137 | ||
138 | if(msptr->client_p == client_p) | |
139 | return msptr; | |
140 | } | |
141 | } | |
142 | else | |
143 | { | |
5cefa1d6 | 144 | RB_DLINK_FOREACH(ptr, client_p->user->channel.head) |
212380e3 AC |
145 | { |
146 | msptr = ptr->data; | |
147 | ||
148 | if(msptr->chptr == chptr) | |
149 | return msptr; | |
150 | } | |
151 | } | |
152 | ||
153 | return NULL; | |
154 | } | |
155 | ||
156 | /* find_channel_status() | |
157 | * | |
158 | * input - membership to get status for, whether we can combine flags | |
159 | * output - flags of user on channel | |
160 | * side effects - | |
161 | */ | |
162 | const char * | |
163 | find_channel_status(struct membership *msptr, int combine) | |
164 | { | |
165 | static char buffer[3]; | |
166 | char *p; | |
167 | ||
168 | p = buffer; | |
169 | ||
170 | if(is_chanop(msptr)) | |
171 | { | |
172 | if(!combine) | |
173 | return "@"; | |
174 | *p++ = '@'; | |
175 | } | |
176 | ||
177 | if(is_voiced(msptr)) | |
178 | *p++ = '+'; | |
179 | ||
180 | *p = '\0'; | |
181 | return buffer; | |
182 | } | |
183 | ||
184 | /* add_user_to_channel() | |
185 | * | |
186 | * input - channel to add client to, client to add, channel flags | |
187 | * output - | |
188 | * side effects - user is added to channel | |
189 | */ | |
190 | void | |
191 | add_user_to_channel(struct Channel *chptr, struct Client *client_p, int flags) | |
192 | { | |
193 | struct membership *msptr; | |
194 | ||
195 | s_assert(client_p->user != NULL); | |
196 | if(client_p->user == NULL) | |
197 | return; | |
198 | ||
398b6a73 | 199 | msptr = rb_bh_alloc(member_heap); |
212380e3 AC |
200 | |
201 | msptr->chptr = chptr; | |
202 | msptr->client_p = client_p; | |
203 | msptr->flags = flags; | |
204 | ||
330fc5c1 AC |
205 | rb_dlinkAdd(msptr, &msptr->usernode, &client_p->user->channel); |
206 | rb_dlinkAdd(msptr, &msptr->channode, &chptr->members); | |
212380e3 AC |
207 | |
208 | if(MyClient(client_p)) | |
330fc5c1 | 209 | rb_dlinkAdd(msptr, &msptr->locchannode, &chptr->locmembers); |
212380e3 AC |
210 | } |
211 | ||
212 | /* remove_user_from_channel() | |
213 | * | |
214 | * input - membership pointer to remove from channel | |
215 | * output - | |
216 | * side effects - membership (thus user) is removed from channel | |
217 | */ | |
218 | void | |
219 | remove_user_from_channel(struct membership *msptr) | |
220 | { | |
221 | struct Client *client_p; | |
222 | struct Channel *chptr; | |
223 | s_assert(msptr != NULL); | |
224 | if(msptr == NULL) | |
225 | return; | |
226 | ||
227 | client_p = msptr->client_p; | |
228 | chptr = msptr->chptr; | |
229 | ||
330fc5c1 AC |
230 | rb_dlinkDelete(&msptr->usernode, &client_p->user->channel); |
231 | rb_dlinkDelete(&msptr->channode, &chptr->members); | |
212380e3 AC |
232 | |
233 | if(client_p->servptr == &me) | |
330fc5c1 | 234 | rb_dlinkDelete(&msptr->locchannode, &chptr->locmembers); |
212380e3 | 235 | |
e3354945 | 236 | chptr->users_last = rb_current_time(); |
212380e3 | 237 | |
330fc5c1 | 238 | if(!(chptr->mode.mode & MODE_PERMANENT) && rb_dlink_list_length(&chptr->members) <= 0) |
212380e3 AC |
239 | destroy_channel(chptr); |
240 | ||
398b6a73 | 241 | rb_bh_free(member_heap, msptr); |
212380e3 AC |
242 | |
243 | return; | |
244 | } | |
245 | ||
246 | /* remove_user_from_channels() | |
247 | * | |
248 | * input - user to remove from all channels | |
249 | * output - | |
250 | * side effects - user is removed from all channels | |
251 | */ | |
252 | void | |
253 | remove_user_from_channels(struct Client *client_p) | |
254 | { | |
255 | struct Channel *chptr; | |
256 | struct membership *msptr; | |
330fc5c1 | 257 | rb_dlink_node *ptr; |
637c4932 | 258 | rb_dlink_node *next_ptr; |
212380e3 AC |
259 | |
260 | if(client_p == NULL) | |
261 | return; | |
262 | ||
637c4932 | 263 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->user->channel.head) |
212380e3 AC |
264 | { |
265 | msptr = ptr->data; | |
266 | chptr = msptr->chptr; | |
267 | ||
330fc5c1 | 268 | rb_dlinkDelete(&msptr->channode, &chptr->members); |
212380e3 AC |
269 | |
270 | if(client_p->servptr == &me) | |
330fc5c1 | 271 | rb_dlinkDelete(&msptr->locchannode, &chptr->locmembers); |
212380e3 | 272 | |
e3354945 | 273 | chptr->users_last = rb_current_time(); |
212380e3 | 274 | |
330fc5c1 | 275 | if(!(chptr->mode.mode & MODE_PERMANENT) && rb_dlink_list_length(&chptr->members) <= 0) |
212380e3 AC |
276 | destroy_channel(chptr); |
277 | ||
398b6a73 | 278 | rb_bh_free(member_heap, msptr); |
212380e3 AC |
279 | } |
280 | ||
281 | client_p->user->channel.head = client_p->user->channel.tail = NULL; | |
282 | client_p->user->channel.length = 0; | |
283 | } | |
284 | ||
285 | /* invalidate_bancache_user() | |
286 | * | |
287 | * input - user to invalidate ban cache for | |
288 | * output - | |
289 | * side effects - ban cache is invalidated for all memberships of that user | |
290 | * to be used after a nick change | |
291 | */ | |
292 | void | |
293 | invalidate_bancache_user(struct Client *client_p) | |
294 | { | |
295 | struct membership *msptr; | |
330fc5c1 | 296 | rb_dlink_node *ptr; |
212380e3 AC |
297 | |
298 | if(client_p == NULL) | |
299 | return; | |
300 | ||
5cefa1d6 | 301 | RB_DLINK_FOREACH(ptr, client_p->user->channel.head) |
212380e3 AC |
302 | { |
303 | msptr = ptr->data; | |
304 | msptr->bants = 0; | |
305 | msptr->flags &= ~CHFL_BANNED; | |
306 | } | |
307 | } | |
308 | ||
309 | /* check_channel_name() | |
310 | * | |
311 | * input - channel name | |
312 | * output - 1 if valid channel name, else 0 | |
313 | * side effects - | |
314 | */ | |
315 | int | |
316 | check_channel_name(const char *name) | |
317 | { | |
318 | s_assert(name != NULL); | |
319 | if(name == NULL) | |
320 | return 0; | |
321 | ||
322 | for (; *name; ++name) | |
323 | { | |
324 | if(!IsChanChar(*name)) | |
325 | return 0; | |
326 | } | |
327 | ||
328 | return 1; | |
329 | } | |
330 | ||
331 | /* free_channel_list() | |
332 | * | |
330fc5c1 | 333 | * input - rb_dlink list to free |
212380e3 AC |
334 | * output - |
335 | * side effects - list of b/e/I modes is cleared | |
336 | */ | |
337 | void | |
330fc5c1 | 338 | free_channel_list(rb_dlink_list * list) |
212380e3 | 339 | { |
330fc5c1 | 340 | rb_dlink_node *ptr; |
637c4932 | 341 | rb_dlink_node *next_ptr; |
212380e3 AC |
342 | struct Ban *actualBan; |
343 | ||
637c4932 | 344 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, list->head) |
212380e3 AC |
345 | { |
346 | actualBan = ptr->data; | |
347 | free_ban(actualBan); | |
348 | } | |
349 | ||
350 | list->head = list->tail = NULL; | |
351 | list->length = 0; | |
352 | } | |
353 | ||
354 | /* destroy_channel() | |
355 | * | |
356 | * input - channel to destroy | |
357 | * output - | |
358 | * side effects - channel is obliterated | |
359 | */ | |
360 | void | |
361 | destroy_channel(struct Channel *chptr) | |
362 | { | |
637c4932 | 363 | rb_dlink_node *ptr, *next_ptr; |
212380e3 | 364 | |
637c4932 | 365 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->invites.head) |
212380e3 AC |
366 | { |
367 | del_invite(chptr, ptr->data); | |
368 | } | |
369 | ||
370 | /* free all bans/exceptions/denies */ | |
371 | free_channel_list(&chptr->banlist); | |
372 | free_channel_list(&chptr->exceptlist); | |
373 | free_channel_list(&chptr->invexlist); | |
fea1ad52 | 374 | free_channel_list(&chptr->quietlist); |
212380e3 AC |
375 | |
376 | /* Free the topic */ | |
377 | free_topic(chptr); | |
378 | ||
330fc5c1 | 379 | rb_dlinkDelete(&chptr->node, &global_channel_list); |
212380e3 AC |
380 | del_from_channel_hash(chptr->chname, chptr); |
381 | free_channel(chptr); | |
382 | } | |
383 | ||
384 | /* channel_pub_or_secret() | |
385 | * | |
386 | * input - channel | |
387 | * output - "=" if public, "@" if secret, else "*" | |
388 | * side effects - | |
389 | */ | |
390 | static const char * | |
391 | channel_pub_or_secret(struct Channel *chptr) | |
392 | { | |
393 | if(PubChannel(chptr)) | |
394 | return ("="); | |
395 | else if(SecretChannel(chptr)) | |
396 | return ("@"); | |
397 | return ("*"); | |
398 | } | |
399 | ||
400 | /* channel_member_names() | |
401 | * | |
402 | * input - channel to list, client to list to, show endofnames | |
403 | * output - | |
404 | * side effects - client is given list of users on channel | |
405 | */ | |
406 | void | |
407 | channel_member_names(struct Channel *chptr, struct Client *client_p, int show_eon) | |
408 | { | |
409 | struct membership *msptr; | |
410 | struct Client *target_p; | |
330fc5c1 | 411 | rb_dlink_node *ptr; |
212380e3 AC |
412 | char lbuf[BUFSIZE]; |
413 | char *t; | |
414 | int mlen; | |
415 | int tlen; | |
416 | int cur_len; | |
417 | int is_member; | |
418 | int stack = IsCapable(client_p, CLICAP_MULTI_PREFIX); | |
419 | ||
420 | if(ShowChannel(client_p, chptr)) | |
421 | { | |
422 | is_member = IsMember(client_p, chptr); | |
423 | ||
b2f0da88 | 424 | cur_len = mlen = rb_sprintf(lbuf, form_str(RPL_NAMREPLY), |
212380e3 AC |
425 | me.name, client_p->name, |
426 | channel_pub_or_secret(chptr), chptr->chname); | |
427 | ||
428 | t = lbuf + cur_len; | |
429 | ||
5cefa1d6 | 430 | RB_DLINK_FOREACH(ptr, chptr->members.head) |
212380e3 AC |
431 | { |
432 | msptr = ptr->data; | |
433 | target_p = msptr->client_p; | |
434 | ||
435 | if(IsInvisible(target_p) && !is_member) | |
436 | continue; | |
437 | ||
438 | /* space, possible "@+" prefix */ | |
439 | if(cur_len + strlen(target_p->name) + 3 >= BUFSIZE - 3) | |
440 | { | |
441 | *(t - 1) = '\0'; | |
442 | sendto_one(client_p, "%s", lbuf); | |
443 | cur_len = mlen; | |
444 | t = lbuf + mlen; | |
445 | } | |
446 | ||
b2f0da88 | 447 | tlen = rb_sprintf(t, "%s%s ", find_channel_status(msptr, stack), |
212380e3 AC |
448 | target_p->name); |
449 | ||
450 | cur_len += tlen; | |
451 | t += tlen; | |
452 | } | |
453 | ||
454 | /* The old behaviour here was to always output our buffer, | |
455 | * even if there are no clients we can show. This happens | |
456 | * when a client does "NAMES" with no parameters, and all | |
457 | * the clients on a -sp channel are +i. I dont see a good | |
458 | * reason for keeping that behaviour, as it just wastes | |
459 | * bandwidth. --anfl | |
460 | */ | |
461 | if(cur_len != mlen) | |
462 | { | |
463 | *(t - 1) = '\0'; | |
464 | sendto_one(client_p, "%s", lbuf); | |
465 | } | |
466 | } | |
467 | ||
468 | if(show_eon) | |
469 | sendto_one(client_p, form_str(RPL_ENDOFNAMES), | |
470 | me.name, client_p->name, chptr->chname); | |
471 | } | |
472 | ||
473 | /* del_invite() | |
474 | * | |
475 | * input - channel to remove invite from, client to remove | |
476 | * output - | |
477 | * side effects - user is removed from invite list, if exists | |
478 | */ | |
479 | void | |
480 | del_invite(struct Channel *chptr, struct Client *who) | |
481 | { | |
330fc5c1 AC |
482 | rb_dlinkFindDestroy(who, &chptr->invites); |
483 | rb_dlinkFindDestroy(chptr, &who->user->invited); | |
212380e3 AC |
484 | } |
485 | ||
486 | /* is_banned() | |
487 | * | |
488 | * input - channel to check bans for, user to check bans against | |
489 | * optional prebuilt buffers | |
490 | * output - 1 if banned, else 0 | |
491 | * side effects - | |
492 | */ | |
493 | int | |
494 | is_banned(struct Channel *chptr, struct Client *who, struct membership *msptr, | |
495 | const char *s, const char *s2) | |
496 | { | |
497 | char src_host[NICKLEN + USERLEN + HOSTLEN + 6]; | |
498 | char src_iphost[NICKLEN + USERLEN + HOSTLEN + 6]; | |
499 | char src_althost[NICKLEN + USERLEN + HOSTLEN + 6]; | |
500 | char *s3 = NULL; | |
330fc5c1 | 501 | rb_dlink_node *ptr; |
212380e3 AC |
502 | struct Ban *actualBan = NULL; |
503 | struct Ban *actualExcept = NULL; | |
504 | ||
505 | if(!MyClient(who)) | |
506 | return 0; | |
507 | ||
508 | /* if the buffers havent been built, do it here */ | |
509 | if(s == NULL) | |
510 | { | |
b2f0da88 AC |
511 | rb_sprintf(src_host, "%s!%s@%s", who->name, who->username, who->host); |
512 | rb_sprintf(src_iphost, "%s!%s@%s", who->name, who->username, who->sockhost); | |
212380e3 AC |
513 | |
514 | s = src_host; | |
515 | s2 = src_iphost; | |
516 | } | |
517 | if(who->localClient->mangledhost != NULL) | |
518 | { | |
519 | /* if host mangling mode enabled, also check their real host */ | |
520 | if(!strcmp(who->host, who->localClient->mangledhost)) | |
521 | { | |
b2f0da88 | 522 | rb_sprintf(src_althost, "%s!%s@%s", who->name, who->username, who->orighost); |
212380e3 AC |
523 | s3 = src_althost; |
524 | } | |
525 | /* if host mangling mode not enabled and no other spoof, | |
526 | * also check the mangled form of their host */ | |
527 | else if (!IsDynSpoof(who)) | |
528 | { | |
b2f0da88 | 529 | rb_sprintf(src_althost, "%s!%s@%s", who->name, who->username, who->localClient->mangledhost); |
212380e3 AC |
530 | s3 = src_althost; |
531 | } | |
532 | } | |
533 | ||
5cefa1d6 | 534 | RB_DLINK_FOREACH(ptr, chptr->banlist.head) |
212380e3 AC |
535 | { |
536 | actualBan = ptr->data; | |
537 | if(match(actualBan->banstr, s) || | |
538 | match(actualBan->banstr, s2) || | |
539 | match_cidr(actualBan->banstr, s2) || | |
540 | match_extban(actualBan->banstr, who, chptr, CHFL_BAN) || | |
541 | (s3 != NULL && match(actualBan->banstr, s3))) | |
542 | break; | |
543 | else | |
544 | actualBan = NULL; | |
545 | } | |
546 | ||
547 | if((actualBan != NULL) && ConfigChannel.use_except) | |
548 | { | |
5cefa1d6 | 549 | RB_DLINK_FOREACH(ptr, chptr->exceptlist.head) |
212380e3 AC |
550 | { |
551 | actualExcept = ptr->data; | |
552 | ||
553 | /* theyre exempted.. */ | |
554 | if(match(actualExcept->banstr, s) || | |
555 | match(actualExcept->banstr, s2) || | |
556 | match_cidr(actualExcept->banstr, s2) || | |
557 | match_extban(actualExcept->banstr, who, chptr, CHFL_EXCEPTION) || | |
558 | (s3 != NULL && match(actualExcept->banstr, s3))) | |
559 | { | |
560 | /* cache the fact theyre not banned */ | |
561 | if(msptr != NULL) | |
562 | { | |
563 | msptr->bants = chptr->bants; | |
564 | msptr->flags &= ~CHFL_BANNED; | |
565 | } | |
566 | ||
567 | return CHFL_EXCEPTION; | |
568 | } | |
569 | } | |
570 | } | |
571 | ||
572 | /* cache the banned/not banned status */ | |
573 | if(msptr != NULL) | |
574 | { | |
575 | msptr->bants = chptr->bants; | |
576 | ||
577 | if(actualBan != NULL) | |
578 | { | |
579 | msptr->flags |= CHFL_BANNED; | |
580 | return CHFL_BAN; | |
581 | } | |
582 | else | |
583 | { | |
584 | msptr->flags &= ~CHFL_BANNED; | |
585 | return 0; | |
586 | } | |
587 | } | |
588 | ||
589 | return ((actualBan ? CHFL_BAN : 0)); | |
590 | } | |
591 | ||
592 | /* is_quieted() | |
593 | * | |
594 | * input - channel to check bans for, user to check bans against | |
595 | * optional prebuilt buffers | |
596 | * output - 1 if banned, else 0 | |
597 | * side effects - | |
598 | */ | |
599 | int | |
600 | is_quieted(struct Channel *chptr, struct Client *who, struct membership *msptr, | |
601 | const char *s, const char *s2) | |
602 | { | |
603 | char src_host[NICKLEN + USERLEN + HOSTLEN + 6]; | |
604 | char src_iphost[NICKLEN + USERLEN + HOSTLEN + 6]; | |
605 | char src_althost[NICKLEN + USERLEN + HOSTLEN + 6]; | |
606 | char *s3 = NULL; | |
330fc5c1 | 607 | rb_dlink_node *ptr; |
212380e3 AC |
608 | struct Ban *actualBan = NULL; |
609 | struct Ban *actualExcept = NULL; | |
610 | ||
611 | if(!MyClient(who)) | |
612 | return 0; | |
613 | ||
614 | /* if the buffers havent been built, do it here */ | |
615 | if(s == NULL) | |
616 | { | |
b2f0da88 AC |
617 | rb_sprintf(src_host, "%s!%s@%s", who->name, who->username, who->host); |
618 | rb_sprintf(src_iphost, "%s!%s@%s", who->name, who->username, who->sockhost); | |
212380e3 AC |
619 | |
620 | s = src_host; | |
621 | s2 = src_iphost; | |
622 | } | |
623 | if(who->localClient->mangledhost != NULL) | |
624 | { | |
625 | /* if host mangling mode enabled, also check their real host */ | |
626 | if(!strcmp(who->host, who->localClient->mangledhost)) | |
627 | { | |
b2f0da88 | 628 | rb_sprintf(src_althost, "%s!%s@%s", who->name, who->username, who->orighost); |
212380e3 AC |
629 | s3 = src_althost; |
630 | } | |
631 | /* if host mangling mode not enabled and no other spoof, | |
632 | * also check the mangled form of their host */ | |
633 | else if (!IsDynSpoof(who)) | |
634 | { | |
b2f0da88 | 635 | rb_sprintf(src_althost, "%s!%s@%s", who->name, who->username, who->localClient->mangledhost); |
212380e3 AC |
636 | s3 = src_althost; |
637 | } | |
638 | } | |
639 | ||
5cefa1d6 | 640 | RB_DLINK_FOREACH(ptr, chptr->quietlist.head) |
212380e3 AC |
641 | { |
642 | actualBan = ptr->data; | |
643 | if(match(actualBan->banstr, s) || | |
644 | match(actualBan->banstr, s2) || | |
645 | match_cidr(actualBan->banstr, s2) || | |
646 | match_extban(actualBan->banstr, who, chptr, CHFL_QUIET) || | |
647 | (s3 != NULL && match(actualBan->banstr, s3))) | |
648 | break; | |
649 | else | |
650 | actualBan = NULL; | |
651 | } | |
652 | ||
653 | if((actualBan != NULL) && ConfigChannel.use_except) | |
654 | { | |
5cefa1d6 | 655 | RB_DLINK_FOREACH(ptr, chptr->exceptlist.head) |
212380e3 AC |
656 | { |
657 | actualExcept = ptr->data; | |
658 | ||
659 | /* theyre exempted.. */ | |
660 | if(match(actualExcept->banstr, s) || | |
661 | match(actualExcept->banstr, s2) || | |
662 | match_cidr(actualExcept->banstr, s2) || | |
663 | match_extban(actualExcept->banstr, who, chptr, CHFL_EXCEPTION) || | |
664 | (s3 != NULL && match(actualExcept->banstr, s3))) | |
665 | { | |
666 | /* cache the fact theyre not banned */ | |
667 | if(msptr != NULL) | |
668 | { | |
669 | msptr->bants = chptr->bants; | |
670 | msptr->flags &= ~CHFL_BANNED; | |
671 | } | |
672 | ||
673 | return CHFL_EXCEPTION; | |
674 | } | |
675 | } | |
676 | } | |
677 | ||
678 | /* cache the banned/not banned status */ | |
679 | if(msptr != NULL) | |
680 | { | |
681 | msptr->bants = chptr->bants; | |
682 | ||
683 | if(actualBan != NULL) | |
684 | { | |
685 | msptr->flags |= CHFL_BANNED; | |
686 | return CHFL_BAN; | |
687 | } | |
688 | else | |
689 | { | |
690 | msptr->flags &= ~CHFL_BANNED; | |
691 | return 0; | |
692 | } | |
693 | } | |
694 | ||
695 | return ((actualBan ? CHFL_BAN : 0)); | |
696 | } | |
697 | ||
698 | /* can_join() | |
699 | * | |
700 | * input - client to check, channel to check for, key | |
701 | * output - reason for not being able to join, else 0 | |
702 | * side effects - | |
703 | */ | |
704 | int | |
705 | can_join(struct Client *source_p, struct Channel *chptr, char *key) | |
706 | { | |
330fc5c1 AC |
707 | rb_dlink_node *invite = NULL; |
708 | rb_dlink_node *ptr; | |
212380e3 AC |
709 | struct Ban *invex = NULL; |
710 | char src_host[NICKLEN + USERLEN + HOSTLEN + 6]; | |
711 | char src_iphost[NICKLEN + USERLEN + HOSTLEN + 6]; | |
712 | char src_althost[NICKLEN + USERLEN + HOSTLEN + 6]; | |
713 | int use_althost = 0; | |
1ebf4db4 | 714 | int i = 0; |
212380e3 AC |
715 | hook_data_channel moduledata; |
716 | ||
717 | s_assert(source_p->localClient != NULL); | |
718 | ||
b2f0da88 AC |
719 | rb_sprintf(src_host, "%s!%s@%s", source_p->name, source_p->username, source_p->host); |
720 | rb_sprintf(src_iphost, "%s!%s@%s", source_p->name, source_p->username, source_p->sockhost); | |
212380e3 AC |
721 | if(source_p->localClient->mangledhost != NULL) |
722 | { | |
723 | /* if host mangling mode enabled, also check their real host */ | |
724 | if(!strcmp(source_p->host, source_p->localClient->mangledhost)) | |
725 | { | |
b2f0da88 | 726 | rb_sprintf(src_althost, "%s!%s@%s", source_p->name, source_p->username, source_p->orighost); |
212380e3 AC |
727 | use_althost = 1; |
728 | } | |
729 | /* if host mangling mode not enabled and no other spoof, | |
730 | * also check the mangled form of their host */ | |
731 | else if (!IsDynSpoof(source_p)) | |
732 | { | |
b2f0da88 | 733 | rb_sprintf(src_althost, "%s!%s@%s", source_p->name, source_p->username, source_p->localClient->mangledhost); |
212380e3 AC |
734 | use_althost = 1; |
735 | } | |
736 | } | |
737 | ||
738 | if((is_banned(chptr, source_p, NULL, src_host, src_iphost)) == CHFL_BAN) | |
739 | return (ERR_BANNEDFROMCHAN); | |
740 | ||
741 | if(chptr->mode.mode & MODE_INVITEONLY) | |
742 | { | |
5cefa1d6 | 743 | RB_DLINK_FOREACH(invite, source_p->user->invited.head) |
212380e3 | 744 | { |
1ebf4db4 | 745 | if(invite->data == chptr) |
212380e3 AC |
746 | break; |
747 | } | |
1ebf4db4 | 748 | if(invite == NULL) |
212380e3 AC |
749 | { |
750 | if(!ConfigChannel.use_invex) | |
751 | return (ERR_INVITEONLYCHAN); | |
5cefa1d6 | 752 | RB_DLINK_FOREACH(ptr, chptr->invexlist.head) |
212380e3 AC |
753 | { |
754 | invex = ptr->data; | |
755 | if(match(invex->banstr, src_host) | |
756 | || match(invex->banstr, src_iphost) | |
757 | || match_cidr(invex->banstr, src_iphost) | |
758 | || match_extban(invex->banstr, source_p, chptr, CHFL_INVEX) | |
759 | || (use_althost && match(invex->banstr, src_althost))) | |
760 | break; | |
761 | } | |
762 | if(ptr == NULL) | |
763 | return (ERR_INVITEONLYCHAN); | |
764 | } | |
765 | } | |
766 | ||
767 | if(*chptr->mode.key && (EmptyString(key) || irccmp(chptr->mode.key, key))) | |
768 | return (ERR_BADCHANNELKEY); | |
769 | ||
770 | if(chptr->mode.limit && | |
330fc5c1 | 771 | rb_dlink_list_length(&chptr->members) >= (unsigned long) chptr->mode.limit) |
1ebf4db4 | 772 | i = ERR_CHANNELISFULL; |
212380e3 | 773 | if(chptr->mode.mode & MODE_REGONLY && EmptyString(source_p->user->suser)) |
1ebf4db4 | 774 | i = ERR_NEEDREGGEDNICK; |
212380e3 | 775 | /* join throttling stuff --nenolod */ |
1ebf4db4 | 776 | else if(chptr->mode.join_num > 0 && chptr->mode.join_time > 0) |
212380e3 | 777 | { |
e3354945 | 778 | if ((rb_current_time() - chptr->join_delta <= |
212380e3 AC |
779 | chptr->mode.join_time) && (chptr->join_count >= |
780 | chptr->mode.join_num)) | |
1ebf4db4 JT |
781 | i = ERR_THROTTLE; |
782 | } | |
783 | ||
784 | /* allow /invite to override +l/+r/+j also -- jilles */ | |
785 | if (i != 0 && invite == NULL) | |
786 | { | |
5cefa1d6 | 787 | RB_DLINK_FOREACH(invite, source_p->user->invited.head) |
1ebf4db4 JT |
788 | { |
789 | if(invite->data == chptr) | |
790 | break; | |
791 | } | |
792 | if (invite == NULL) | |
793 | return i; | |
212380e3 AC |
794 | } |
795 | ||
796 | moduledata.client = source_p; | |
797 | moduledata.chptr = chptr; | |
798 | moduledata.approved = 0; | |
799 | ||
800 | call_hook(h_can_join, &moduledata); | |
801 | ||
802 | return moduledata.approved; | |
803 | } | |
804 | ||
805 | /* can_send() | |
806 | * | |
807 | * input - user to check in channel, membership pointer | |
808 | * output - whether can explicitly send or not, else CAN_SEND_NONOP | |
809 | * side effects - | |
810 | */ | |
811 | int | |
812 | can_send(struct Channel *chptr, struct Client *source_p, struct membership *msptr) | |
813 | { | |
814 | if(IsServer(source_p) || IsService(source_p)) | |
815 | return CAN_SEND_OPV; | |
816 | ||
817 | if(MyClient(source_p) && hash_find_resv(chptr->chname) && | |
818 | !IsOper(source_p) && !IsExemptResv(source_p)) | |
819 | return CAN_SEND_NO; | |
820 | ||
821 | if(msptr == NULL) | |
822 | { | |
823 | msptr = find_channel_membership(chptr, source_p); | |
824 | ||
825 | if(msptr == NULL) | |
826 | { | |
827 | /* if its +m or +n and theyre not in the channel, | |
828 | * they cant send. we dont check bans here because | |
829 | * theres no possibility of caching them --fl | |
830 | */ | |
831 | if(chptr->mode.mode & MODE_NOPRIVMSGS || chptr->mode.mode & MODE_MODERATED) | |
832 | return CAN_SEND_NO; | |
833 | else | |
834 | return CAN_SEND_NONOP; | |
835 | } | |
836 | } | |
837 | ||
838 | if(is_chanop_voiced(msptr)) | |
839 | return CAN_SEND_OPV; | |
840 | ||
841 | if(chptr->mode.mode & MODE_MODERATED) | |
842 | return CAN_SEND_NO; | |
843 | ||
844 | if(MyClient(source_p)) | |
845 | { | |
846 | /* cached can_send */ | |
847 | if(msptr->bants == chptr->bants) | |
848 | { | |
849 | if(can_send_banned(msptr)) | |
850 | return CAN_SEND_NO; | |
851 | } | |
852 | else if(is_banned(chptr, source_p, msptr, NULL, NULL) == CHFL_BAN | |
853 | || is_quieted(chptr, source_p, msptr, NULL, NULL) == CHFL_BAN) | |
854 | return CAN_SEND_NO; | |
855 | } | |
856 | ||
857 | return CAN_SEND_NONOP; | |
858 | } | |
859 | ||
860 | /* find_bannickchange_channel() | |
861 | * Input: client to check | |
862 | * Output: channel preventing nick change | |
863 | */ | |
864 | struct Channel * | |
865 | find_bannickchange_channel(struct Client *client_p) | |
866 | { | |
867 | struct Channel *chptr; | |
868 | struct membership *msptr; | |
330fc5c1 | 869 | rb_dlink_node *ptr; |
212380e3 AC |
870 | char src_host[NICKLEN + USERLEN + HOSTLEN + 6]; |
871 | char src_iphost[NICKLEN + USERLEN + HOSTLEN + 6]; | |
872 | ||
873 | if (!MyClient(client_p)) | |
874 | return NULL; | |
875 | ||
b2f0da88 AC |
876 | rb_sprintf(src_host, "%s!%s@%s", client_p->name, client_p->username, client_p->host); |
877 | rb_sprintf(src_iphost, "%s!%s@%s", client_p->name, client_p->username, client_p->sockhost); | |
212380e3 | 878 | |
5cefa1d6 | 879 | RB_DLINK_FOREACH(ptr, client_p->user->channel.head) |
212380e3 AC |
880 | { |
881 | msptr = ptr->data; | |
882 | chptr = msptr->chptr; | |
883 | if (is_chanop_voiced(msptr)) | |
884 | continue; | |
885 | /* cached can_send */ | |
886 | if (msptr->bants == chptr->bants) | |
887 | { | |
888 | if (can_send_banned(msptr)) | |
889 | return chptr; | |
890 | } | |
891 | else if (is_banned(chptr, client_p, msptr, src_host, src_iphost) == CHFL_BAN | |
892 | || is_quieted(chptr, client_p, msptr, src_host, src_iphost) == CHFL_BAN) | |
893 | return chptr; | |
894 | } | |
895 | return NULL; | |
896 | } | |
897 | ||
898 | /* void check_spambot_warning(struct Client *source_p) | |
899 | * Input: Client to check, channel name or NULL if this is a part. | |
900 | * Output: none | |
901 | * Side-effects: Updates the client's oper_warn_count_down, warns the | |
902 | * IRC operators if necessary, and updates join_leave_countdown as | |
903 | * needed. | |
904 | */ | |
905 | void | |
906 | check_spambot_warning(struct Client *source_p, const char *name) | |
907 | { | |
908 | int t_delta; | |
909 | int decrement_count; | |
910 | if((GlobalSetOptions.spam_num && | |
911 | (source_p->localClient->join_leave_count >= GlobalSetOptions.spam_num))) | |
912 | { | |
913 | if(source_p->localClient->oper_warn_count_down > 0) | |
914 | source_p->localClient->oper_warn_count_down--; | |
915 | else | |
916 | source_p->localClient->oper_warn_count_down = 0; | |
917 | if(source_p->localClient->oper_warn_count_down == 0) | |
918 | { | |
919 | /* Its already known as a possible spambot */ | |
920 | if(name != NULL) | |
f2c1b06b | 921 | sendto_realops_snomask(SNO_BOTS, L_NETWIDE, |
212380e3 AC |
922 | "User %s (%s@%s) trying to join %s is a possible spambot", |
923 | source_p->name, | |
63aecfb9 | 924 | source_p->username, source_p->orighost, name); |
212380e3 | 925 | else |
f2c1b06b | 926 | sendto_realops_snomask(SNO_BOTS, L_NETWIDE, |
212380e3 AC |
927 | "User %s (%s@%s) is a possible spambot", |
928 | source_p->name, | |
63aecfb9 | 929 | source_p->username, source_p->orighost); |
212380e3 AC |
930 | source_p->localClient->oper_warn_count_down = OPER_SPAM_COUNTDOWN; |
931 | } | |
932 | } | |
933 | else | |
934 | { | |
935 | if((t_delta = | |
e3354945 | 936 | (rb_current_time() - source_p->localClient->last_leave_time)) > |
212380e3 AC |
937 | JOIN_LEAVE_COUNT_EXPIRE_TIME) |
938 | { | |
939 | decrement_count = (t_delta / JOIN_LEAVE_COUNT_EXPIRE_TIME); | |
940 | if(decrement_count > source_p->localClient->join_leave_count) | |
941 | source_p->localClient->join_leave_count = 0; | |
942 | else | |
943 | source_p->localClient->join_leave_count -= decrement_count; | |
944 | } | |
945 | else | |
946 | { | |
e3354945 | 947 | if((rb_current_time() - |
212380e3 AC |
948 | (source_p->localClient->last_join_time)) < GlobalSetOptions.spam_time) |
949 | { | |
950 | /* oh, its a possible spambot */ | |
951 | source_p->localClient->join_leave_count++; | |
952 | } | |
953 | } | |
954 | if(name != NULL) | |
e3354945 | 955 | source_p->localClient->last_join_time = rb_current_time(); |
212380e3 | 956 | else |
e3354945 | 957 | source_p->localClient->last_leave_time = rb_current_time(); |
212380e3 AC |
958 | } |
959 | } | |
960 | ||
961 | /* check_splitmode() | |
962 | * | |
963 | * input - | |
964 | * output - | |
965 | * side effects - compares usercount and servercount against their split | |
966 | * values and adjusts splitmode accordingly | |
967 | */ | |
968 | void | |
969 | check_splitmode(void *unused) | |
970 | { | |
971 | if(splitchecking && (ConfigChannel.no_join_on_split || ConfigChannel.no_create_on_split)) | |
972 | { | |
973 | /* not split, we're being asked to check now because someone | |
974 | * has left | |
975 | */ | |
976 | if(!splitmode) | |
977 | { | |
978 | if(eob_count < split_servers || Count.total < split_users) | |
979 | { | |
980 | splitmode = 1; | |
981 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
982 | "Network split, activating splitmode"); | |
c608a061 | 983 | check_splitmode_ev = rb_event_addish("check_splitmode", check_splitmode, NULL, 2); |
212380e3 AC |
984 | } |
985 | } | |
986 | /* in splitmode, check whether its finished */ | |
987 | else if(eob_count >= split_servers && Count.total >= split_users) | |
988 | { | |
989 | splitmode = 0; | |
990 | ||
991 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
992 | "Network rejoined, deactivating splitmode"); | |
993 | ||
c608a061 | 994 | rb_event_delete(check_splitmode_ev); |
212380e3 AC |
995 | } |
996 | } | |
997 | } | |
998 | ||
999 | ||
1000 | /* allocate_topic() | |
1001 | * | |
1002 | * input - channel to allocate topic for | |
1003 | * output - 1 on success, else 0 | |
1004 | * side effects - channel gets a topic allocated | |
1005 | */ | |
1006 | static void | |
1007 | allocate_topic(struct Channel *chptr) | |
1008 | { | |
1009 | void *ptr; | |
1010 | ||
1011 | if(chptr == NULL) | |
1012 | return; | |
1013 | ||
398b6a73 | 1014 | ptr = rb_bh_alloc(topic_heap); |
212380e3 AC |
1015 | |
1016 | /* Basically we allocate one large block for the topic and | |
1017 | * the topic info. We then split it up into two and shove it | |
1018 | * in the chptr | |
1019 | */ | |
1020 | chptr->topic = ptr; | |
1021 | chptr->topic_info = (char *) ptr + TOPICLEN + 1; | |
1022 | *chptr->topic = '\0'; | |
1023 | *chptr->topic_info = '\0'; | |
1024 | } | |
1025 | ||
1026 | /* free_topic() | |
1027 | * | |
1028 | * input - channel which has topic to free | |
1029 | * output - | |
1030 | * side effects - channels topic is free'd | |
1031 | */ | |
1032 | static void | |
1033 | free_topic(struct Channel *chptr) | |
1034 | { | |
1035 | void *ptr; | |
1036 | ||
1037 | if(chptr == NULL || chptr->topic == NULL) | |
1038 | return; | |
1039 | ||
1040 | /* This is safe for now - If you change allocate_topic you | |
1041 | * MUST change this as well | |
1042 | */ | |
1043 | ptr = chptr->topic; | |
398b6a73 | 1044 | rb_bh_free(topic_heap, ptr); |
212380e3 AC |
1045 | chptr->topic = NULL; |
1046 | chptr->topic_info = NULL; | |
1047 | } | |
1048 | ||
1049 | /* set_channel_topic() | |
1050 | * | |
1051 | * input - channel, topic to set, topic info and topic ts | |
1052 | * output - | |
1053 | * side effects - channels topic, topic info and TS are set. | |
1054 | */ | |
1055 | void | |
1056 | set_channel_topic(struct Channel *chptr, const char *topic, const char *topic_info, time_t topicts) | |
1057 | { | |
1058 | if(strlen(topic) > 0) | |
1059 | { | |
1060 | if(chptr->topic == NULL) | |
1061 | allocate_topic(chptr); | |
f427c8b0 VY |
1062 | rb_strlcpy(chptr->topic, topic, TOPICLEN + 1); |
1063 | rb_strlcpy(chptr->topic_info, topic_info, USERHOST_REPLYLEN); | |
212380e3 AC |
1064 | chptr->topic_time = topicts; |
1065 | } | |
1066 | else | |
1067 | { | |
1068 | if(chptr->topic != NULL) | |
1069 | free_topic(chptr); | |
1070 | chptr->topic_time = 0; | |
1071 | } | |
1072 | } | |
1073 | ||
7d1f9131 | 1074 | const struct mode_letter chmode_flags[] = |
212380e3 AC |
1075 | { |
1076 | {MODE_INVITEONLY, 'i'}, | |
1077 | {MODE_MODERATED, 'm'}, | |
1078 | {MODE_NOPRIVMSGS, 'n'}, | |
1079 | {MODE_PRIVATE, 'p'}, | |
1080 | {MODE_SECRET, 's'}, | |
1081 | {MODE_TOPICLIMIT, 't'}, | |
1082 | {MODE_NOCOLOR, 'c'}, | |
1083 | {MODE_FREEINVITE, 'g'}, | |
1084 | {MODE_OPMODERATE, 'z'}, | |
1085 | {MODE_EXLIMIT, 'L'}, | |
1086 | {MODE_PERMANENT, 'P'}, | |
1087 | {MODE_FREETARGET, 'F'}, | |
1088 | {MODE_DISFORWARD, 'Q'}, | |
1089 | {MODE_REGONLY, 'r'}, | |
1090 | {0, '\0'} | |
1091 | }; | |
1092 | ||
1093 | /* channel_modes() | |
1094 | * | |
1095 | * inputs - pointer to channel | |
1096 | * - pointer to client | |
f1e35c19 JT |
1097 | * output - string with simple modes |
1098 | * side effects - result from previous calls overwritten | |
212380e3 AC |
1099 | * |
1100 | * Stolen from ShadowIRCd 4 --nenolod | |
1101 | */ | |
1102 | const char * | |
1103 | channel_modes(struct Channel *chptr, struct Client *client_p) | |
1104 | { | |
1105 | int i; | |
1106 | char buf1[BUFSIZE]; | |
1107 | char buf2[BUFSIZE]; | |
1108 | static char final[BUFSIZE]; | |
1109 | char *mbuf = buf1; | |
1110 | char *pbuf = buf2; | |
1111 | ||
1112 | *mbuf++ = '+'; | |
1113 | *pbuf = '\0'; | |
1114 | ||
7d1f9131 JT |
1115 | for (i = 0; chmode_flags[i].mode; ++i) |
1116 | if(chptr->mode.mode & chmode_flags[i].mode) | |
1117 | *mbuf++ = chmode_flags[i].letter; | |
212380e3 AC |
1118 | |
1119 | if(chptr->mode.limit) | |
1120 | { | |
1121 | *mbuf++ = 'l'; | |
1122 | ||
f1e35c19 | 1123 | if(!IsClient(client_p) || IsMember(client_p, chptr)) |
b2f0da88 | 1124 | pbuf += rb_sprintf(pbuf, " %d", chptr->mode.limit); |
212380e3 AC |
1125 | } |
1126 | ||
1127 | if(*chptr->mode.key) | |
1128 | { | |
1129 | *mbuf++ = 'k'; | |
1130 | ||
f1e35c19 | 1131 | if(pbuf > buf2 || !IsClient(client_p) || IsMember(client_p, chptr)) |
b2f0da88 | 1132 | pbuf += rb_sprintf(pbuf, " %s", chptr->mode.key); |
212380e3 AC |
1133 | } |
1134 | ||
1135 | if(chptr->mode.join_num) | |
1136 | { | |
1137 | *mbuf++ = 'j'; | |
1138 | ||
f1e35c19 | 1139 | if(pbuf > buf2 || !IsClient(client_p) || IsMember(client_p, chptr)) |
b2f0da88 | 1140 | pbuf += rb_sprintf(pbuf, " %d:%d", chptr->mode.join_num, |
212380e3 AC |
1141 | chptr->mode.join_time); |
1142 | } | |
1143 | ||
f1e35c19 | 1144 | if(*chptr->mode.forward && (ConfigChannel.use_forward || !IsClient(client_p))) |
212380e3 AC |
1145 | { |
1146 | *mbuf++ = 'f'; | |
1147 | ||
f1e35c19 | 1148 | if(pbuf > buf2 || !IsClient(client_p) || IsMember(client_p, chptr)) |
b2f0da88 | 1149 | pbuf += rb_sprintf(pbuf, " %s", chptr->mode.forward); |
212380e3 AC |
1150 | } |
1151 | ||
1152 | *mbuf = '\0'; | |
1153 | ||
f427c8b0 | 1154 | rb_strlcpy(final, buf1, sizeof final); |
1f9de103 | 1155 | rb_strlcat(final, buf2, sizeof final); |
212380e3 AC |
1156 | return final; |
1157 | } | |
1158 | ||
1159 | /* Now lets do some stuff to keep track of what combinations of | |
1160 | * servers exist... | |
1161 | * Note that the number of combinations doubles each time you add | |
1162 | * something to this list. Each one is only quick if no servers use that | |
1163 | * combination, but if the numbers get too high here MODE will get too | |
1164 | * slow. I suggest if you get more than 7 here, you consider getting rid | |
1165 | * of some and merging or something. If it wasn't for irc+cs we would | |
1166 | * probably not even need to bother about most of these, but unfortunately | |
1167 | * we do. -A1kmm | |
1168 | */ | |
1169 | ||
1170 | /* void init_chcap_usage_counts(void) | |
1171 | * | |
1172 | * Inputs - none | |
1173 | * Output - none | |
1174 | * Side-effects - Initialises the usage counts to zero. Fills in the | |
1175 | * chcap_yes and chcap_no combination tables. | |
1176 | */ | |
1177 | void | |
1178 | init_chcap_usage_counts(void) | |
1179 | { | |
1180 | unsigned long m, c, y, n; | |
1181 | ||
1182 | memset(chcap_combos, 0, sizeof(chcap_combos)); | |
1183 | ||
1184 | /* For every possible combination */ | |
1185 | for (m = 0; m < NCHCAP_COMBOS; m++) | |
1186 | { | |
1187 | /* Check each capab */ | |
1188 | for (c = y = n = 0; c < NCHCAPS; c++) | |
1189 | { | |
1190 | if((m & (1 << c)) == 0) | |
1191 | n |= channel_capabs[c]; | |
1192 | else | |
1193 | y |= channel_capabs[c]; | |
1194 | } | |
1195 | chcap_combos[m].cap_yes = y; | |
1196 | chcap_combos[m].cap_no = n; | |
1197 | } | |
1198 | } | |
1199 | ||
1200 | /* void set_chcap_usage_counts(struct Client *serv_p) | |
1201 | * Input: serv_p; The client whose capabs to register. | |
1202 | * Output: none | |
1203 | * Side-effects: Increments the usage counts for the correct capab | |
1204 | * combination. | |
1205 | */ | |
1206 | void | |
1207 | set_chcap_usage_counts(struct Client *serv_p) | |
1208 | { | |
1209 | int n; | |
1210 | ||
1211 | for (n = 0; n < NCHCAP_COMBOS; n++) | |
1212 | { | |
1213 | if(IsCapable(serv_p, chcap_combos[n].cap_yes) && | |
1214 | NotCapable(serv_p, chcap_combos[n].cap_no)) | |
1215 | { | |
1216 | chcap_combos[n].count++; | |
1217 | return; | |
1218 | } | |
1219 | } | |
1220 | ||
1221 | /* This should be impossible -A1kmm. */ | |
1222 | s_assert(0); | |
1223 | } | |
1224 | ||
1225 | /* void set_chcap_usage_counts(struct Client *serv_p) | |
1226 | * | |
1227 | * Inputs - serv_p; The client whose capabs to register. | |
1228 | * Output - none | |
1229 | * Side-effects - Decrements the usage counts for the correct capab | |
1230 | * combination. | |
1231 | */ | |
1232 | void | |
1233 | unset_chcap_usage_counts(struct Client *serv_p) | |
1234 | { | |
1235 | int n; | |
1236 | ||
1237 | for (n = 0; n < NCHCAP_COMBOS; n++) | |
1238 | { | |
1239 | if(IsCapable(serv_p, chcap_combos[n].cap_yes) && | |
1240 | NotCapable(serv_p, chcap_combos[n].cap_no)) | |
1241 | { | |
1242 | /* Hopefully capabs can't change dynamically or anything... */ | |
1243 | s_assert(chcap_combos[n].count > 0); | |
1244 | ||
1245 | if(chcap_combos[n].count > 0) | |
1246 | chcap_combos[n].count--; | |
1247 | return; | |
1248 | } | |
1249 | } | |
1250 | ||
1251 | /* This should be impossible -A1kmm. */ | |
1252 | s_assert(0); | |
1253 | } | |
1254 | ||
1255 | /* void send_cap_mode_changes(struct Client *client_p, | |
1256 | * struct Client *source_p, | |
1257 | * struct Channel *chptr, int cap, int nocap) | |
1258 | * Input: The client sending(client_p), the source client(source_p), | |
1259 | * the channel to send mode changes for(chptr) | |
1260 | * Output: None. | |
1261 | * Side-effects: Sends the appropriate mode changes to capable servers. | |
1262 | * | |
1263 | * Reverted back to my original design, except that we now keep a count | |
1264 | * of the number of servers which each combination as an optimisation, so | |
1265 | * the capabs combinations which are not needed are not worked out. -A1kmm | |
1266 | */ | |
1267 | void | |
1268 | send_cap_mode_changes(struct Client *client_p, struct Client *source_p, | |
1269 | struct Channel *chptr, struct ChModeChange mode_changes[], int mode_count) | |
1270 | { | |
1271 | static char modebuf[BUFSIZE]; | |
1272 | static char parabuf[BUFSIZE]; | |
1273 | int i, mbl, pbl, nc, mc, preflen, len; | |
1274 | char *pbuf; | |
1275 | const char *arg; | |
1276 | int dir; | |
1277 | int j; | |
1278 | int cap; | |
1279 | int nocap; | |
1280 | int arglen; | |
1281 | ||
1282 | /* Now send to servers... */ | |
1283 | for (j = 0; j < NCHCAP_COMBOS; j++) | |
1284 | { | |
1285 | if(chcap_combos[j].count == 0) | |
1286 | continue; | |
1287 | ||
1288 | mc = 0; | |
1289 | nc = 0; | |
1290 | pbl = 0; | |
1291 | parabuf[0] = 0; | |
1292 | pbuf = parabuf; | |
1293 | dir = MODE_QUERY; | |
1294 | ||
1295 | cap = chcap_combos[j].cap_yes; | |
1296 | nocap = chcap_combos[j].cap_no; | |
1297 | ||
469c9689 AC |
1298 | mbl = preflen = rb_sprintf(modebuf, ":%s TMODE %ld %s ", |
1299 | use_id(source_p), (long) chptr->channelts, | |
1300 | chptr->chname); | |
212380e3 AC |
1301 | |
1302 | /* loop the list of - modes we have */ | |
1303 | for (i = 0; i < mode_count; i++) | |
1304 | { | |
1305 | /* if they dont support the cap we need, or they do support a cap they | |
1306 | * cant have, then dont add it to the modebuf.. that way they wont see | |
1307 | * the mode | |
1308 | */ | |
1309 | if((mode_changes[i].letter == 0) || | |
1310 | ((cap & mode_changes[i].caps) != mode_changes[i].caps) | |
1311 | || ((nocap & mode_changes[i].nocaps) != mode_changes[i].nocaps)) | |
1312 | continue; | |
1313 | ||
fd44b851 JT |
1314 | if(!EmptyString(mode_changes[i].id)) |
1315 | arg = mode_changes[i].id; | |
1316 | else | |
1317 | arg = mode_changes[i].arg; | |
212380e3 AC |
1318 | |
1319 | if(arg) | |
1320 | { | |
1321 | arglen = strlen(arg); | |
1322 | ||
1323 | /* dont even think about it! --fl */ | |
1324 | if(arglen > MODEBUFLEN - 5) | |
1325 | continue; | |
1326 | } | |
1327 | ||
1328 | /* if we're creeping past the buf size, we need to send it and make | |
1329 | * another line for the other modes | |
1330 | * XXX - this could give away server topology with uids being | |
1331 | * different lengths, but not much we can do, except possibly break | |
1332 | * them as if they were the longest of the nick or uid at all times, | |
1333 | * which even then won't work as we don't always know the uid -A1kmm. | |
1334 | */ | |
1335 | if(arg && ((mc == MAXMODEPARAMSSERV) || | |
1336 | ((mbl + pbl + arglen + 4) > (BUFSIZE - 3)))) | |
1337 | { | |
1338 | if(nc != 0) | |
1339 | sendto_server(client_p, chptr, cap, nocap, | |
1340 | "%s %s", modebuf, parabuf); | |
1341 | nc = 0; | |
1342 | mc = 0; | |
1343 | ||
1344 | mbl = preflen; | |
1345 | pbl = 0; | |
1346 | pbuf = parabuf; | |
1347 | parabuf[0] = 0; | |
1348 | dir = MODE_QUERY; | |
1349 | } | |
1350 | ||
1351 | if(dir != mode_changes[i].dir) | |
1352 | { | |
1353 | modebuf[mbl++] = (mode_changes[i].dir == MODE_ADD) ? '+' : '-'; | |
1354 | dir = mode_changes[i].dir; | |
1355 | } | |
1356 | ||
1357 | modebuf[mbl++] = mode_changes[i].letter; | |
1358 | modebuf[mbl] = 0; | |
1359 | nc++; | |
1360 | ||
1361 | if(arg != NULL) | |
1362 | { | |
b2f0da88 | 1363 | len = rb_sprintf(pbuf, "%s ", arg); |
212380e3 AC |
1364 | pbuf += len; |
1365 | pbl += len; | |
1366 | mc++; | |
1367 | } | |
1368 | } | |
1369 | ||
1370 | if(pbl && parabuf[pbl - 1] == ' ') | |
1371 | parabuf[pbl - 1] = 0; | |
1372 | ||
1373 | if(nc != 0) | |
1374 | sendto_server(client_p, chptr, cap, nocap, "%s %s", modebuf, parabuf); | |
1375 | } | |
1376 | } |