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