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