]>
Commit | Line | Data |
---|---|---|
189935b1 | 1 | /* |
2 | * IRC - Internet Relay Chat, ircd/channel.c | |
3 | * Copyright (C) 1990 Jarkko Oikarinen and | |
4 | * University of Oulu, Co Center | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 1, or (at your option) | |
9 | * any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
19 | */ | |
20 | /** @file | |
21 | * @brief Channel management and maintenance | |
22 | * @version $Id: channel.c,v 1.155 2005/09/27 02:41:57 entrope Exp $ | |
23 | */ | |
24 | #include "config.h" | |
25 | ||
26 | #include "channel.h" | |
27 | #include "client.h" | |
28 | #include "destruct_event.h" | |
29 | #include "hash.h" | |
30 | #include "ircd.h" | |
31 | #include "ircd_alloc.h" | |
32 | #include "ircd_chattr.h" | |
33 | #include "ircd_defs.h" | |
34 | #include "ircd_features.h" | |
35 | #include "ircd_log.h" | |
36 | #include "ircd_reply.h" | |
37 | #include "ircd_snprintf.h" | |
38 | #include "ircd_string.h" | |
39 | #include "list.h" | |
40 | #include "match.h" | |
41 | #include "msg.h" | |
42 | #include "msgq.h" | |
43 | #include "numeric.h" | |
44 | #include "numnicks.h" | |
45 | #include "querycmds.h" | |
46 | #include "s_bsd.h" | |
47 | #include "s_conf.h" | |
48 | #include "s_debug.h" | |
49 | #include "s_misc.h" | |
50 | #include "s_user.h" | |
51 | #include "send.h" | |
52 | #include "struct.h" | |
53 | #include "sys.h" | |
54 | #include "whowas.h" | |
55 | ||
56 | /* #include <assert.h> -- Now using assert in ircd_log.h */ | |
57 | #include <stdio.h> | |
58 | #include <stdlib.h> | |
59 | #include <string.h> | |
60 | ||
61 | /** Linked list containing the full list of all channels */ | |
62 | struct Channel* GlobalChannelList = 0; | |
63 | ||
64 | /** Number of struct Membership*'s allocated */ | |
65 | static unsigned int membershipAllocCount; | |
66 | /** Freelist for struct Membership*'s */ | |
67 | static struct Membership* membershipFreeList; | |
68 | /** Freelist for struct Ban*'s */ | |
69 | static struct Ban* free_bans; | |
70 | /** Number of ban structures allocated. */ | |
71 | static size_t bans_alloc; | |
72 | /** Number of ban structures in use. */ | |
73 | static size_t bans_inuse; | |
74 | ||
75 | #if !defined(NDEBUG) | |
76 | /** return the length (>=0) of a chain of links. | |
77 | * @param lp pointer to the start of the linked list | |
78 | * @return the number of items in the list | |
79 | */ | |
80 | static int list_length(struct SLink *lp) | |
81 | { | |
82 | int count = 0; | |
83 | ||
84 | for (; lp; lp = lp->next) | |
85 | ++count; | |
86 | return count; | |
87 | } | |
88 | #endif | |
89 | ||
90 | /** Set the mask for a ban, checking for IP masks. | |
91 | * @param[in,out] ban Ban structure to modify. | |
92 | * @param[in] banstr Mask to ban. | |
93 | */ | |
94 | static void | |
95 | set_ban_mask(struct Ban *ban, const char *banstr) | |
96 | { | |
97 | char *sep; | |
98 | assert(banstr != NULL); | |
99 | ircd_strncpy(ban->banstr, banstr, sizeof(ban->banstr) - 1); | |
100 | sep = strrchr(banstr, '@'); | |
101 | if (sep) { | |
102 | ban->nu_len = sep - banstr; | |
103 | if (ipmask_parse(sep + 1, &ban->address, &ban->addrbits)) | |
104 | ban->flags |= BAN_IPMASK; | |
105 | } | |
106 | } | |
107 | ||
108 | /** Allocate a new Ban structure. | |
109 | * @param[in] banstr Ban mask to use. | |
110 | * @return Newly allocated ban. | |
111 | */ | |
112 | struct Ban * | |
113 | make_ban(const char *banstr) | |
114 | { | |
115 | struct Ban *ban; | |
116 | if (free_bans) { | |
117 | ban = free_bans; | |
118 | free_bans = free_bans->next; | |
119 | } | |
120 | else if (!(ban = MyMalloc(sizeof(*ban)))) | |
121 | return NULL; | |
122 | else | |
123 | bans_alloc++; | |
124 | bans_inuse++; | |
125 | memset(ban, 0, sizeof(*ban)); | |
126 | set_ban_mask(ban, banstr); | |
127 | return ban; | |
128 | } | |
129 | ||
130 | /** Deallocate a ban structure. | |
131 | * @param[in] ban Ban to deallocate. | |
132 | */ | |
133 | void | |
134 | free_ban(struct Ban *ban) | |
135 | { | |
136 | ban->next = free_bans; | |
137 | free_bans = ban; | |
138 | bans_inuse--; | |
139 | } | |
140 | ||
141 | /** Report ban usage to \a cptr. | |
142 | * @param[in] cptr Client requesting information. | |
143 | */ | |
144 | void bans_send_meminfo(struct Client *cptr) | |
145 | { | |
146 | struct Ban *ban; | |
147 | size_t num_free; | |
148 | for (num_free = 0, ban = free_bans; ban; ban = ban->next) | |
149 | num_free++; | |
150 | send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Bans: inuse %zu(%zu) free %zu alloc %zu", | |
151 | bans_inuse, bans_inuse * sizeof(*ban), num_free, bans_alloc); | |
152 | } | |
153 | ||
154 | /** return the struct Membership* that represents a client on a channel | |
155 | * This function finds a struct Membership* which holds the state about | |
156 | * a client on a specific channel. The code is smart enough to iterate | |
157 | * over the channels a user is in, or the users in a channel to find the | |
158 | * user depending on which is likely to be more efficient. | |
159 | * | |
160 | * @param chptr pointer to the channel struct | |
161 | * @param cptr pointer to the client struct | |
162 | * | |
163 | * @returns pointer to the struct Membership representing this client on | |
164 | * this channel. Returns NULL if the client is not on the channel. | |
165 | * Returns NULL if the client is actually a server. | |
166 | * @see find_channel_member() | |
167 | */ | |
168 | struct Membership* find_member_link(struct Channel* chptr, const struct Client* cptr) | |
169 | { | |
170 | struct Membership *m; | |
171 | assert(0 != cptr); | |
172 | assert(0 != chptr); | |
173 | ||
174 | /* Servers don't have member links */ | |
175 | if (IsServer(cptr)||IsMe(cptr)) | |
176 | return 0; | |
177 | ||
178 | /* +k users are typically on a LOT of channels. So we iterate over who | |
179 | * is in the channel. X/W are +k and are in about 5800 channels each. | |
180 | * however there are typically no more than 1000 people in a channel | |
181 | * at a time. | |
182 | */ | |
183 | if (IsChannelService(cptr)) { | |
184 | m = chptr->members; | |
185 | while (m) { | |
186 | assert(m->channel == chptr); | |
187 | if (m->user == cptr) | |
188 | return m; | |
189 | m = m->next_member; | |
190 | } | |
191 | } | |
192 | /* Users on the other hand aren't allowed on more than 15 channels. 50% | |
193 | * of users that are on channels are on 2 or less, 95% are on 7 or less, | |
194 | * and 99% are on 10 or less. | |
195 | */ | |
196 | else { | |
197 | m = (cli_user(cptr))->channel; | |
198 | while (m) { | |
199 | assert(m->user == cptr); | |
200 | if (m->channel == chptr) | |
201 | return m; | |
202 | m = m->next_channel; | |
203 | } | |
204 | } | |
205 | return 0; | |
206 | } | |
207 | ||
208 | /** Find the client structure for a nick name (user) | |
209 | * Find the client structure for a nick name (user) | |
210 | * using history mechanism if necessary. If the client is not found, an error | |
211 | * message (NO SUCH NICK) is generated. If the client was found | |
212 | * through the history, chasing will be 1 and otherwise 0. | |
213 | * | |
214 | * This function was used extensively in the P09 days, and since we now have | |
215 | * numeric nicks is no longer quite as important. | |
216 | * | |
217 | * @param sptr Pointer to the client that has requested the search | |
218 | * @param user a string representing the client to be found | |
219 | * @param chasing a variable set to 0 if the user was found directly, | |
220 | * 1 otherwise | |
221 | * @returns a pointer the client, or NULL if the client wasn't found. | |
222 | */ | |
223 | struct Client* find_chasing(struct Client* sptr, const char* user, int* chasing) | |
224 | { | |
225 | struct Client* who = FindClient(user); | |
226 | ||
227 | if (chasing) | |
228 | *chasing = 0; | |
229 | if (who) | |
230 | return who; | |
231 | ||
232 | if (!(who = get_history(user, feature_int(FEAT_KILLCHASETIMELIMIT)))) { | |
233 | send_reply(sptr, ERR_NOSUCHNICK, user); | |
234 | return 0; | |
235 | } | |
236 | if (chasing) | |
237 | *chasing = 1; | |
238 | return who; | |
239 | } | |
240 | ||
241 | /** Decrement the count of users, and free if empty. | |
242 | * Subtract one user from channel i (and free channel * block, if channel | |
243 | * became empty). | |
244 | * | |
245 | * @param chptr The channel to subtract one from. | |
246 | * | |
247 | * @returns true (1) if channel still has members. | |
248 | * false (0) if the channel is now empty. | |
249 | */ | |
250 | int sub1_from_channel(struct Channel* chptr) | |
251 | { | |
252 | if (chptr->users > 1) /* Can be 0, called for an empty channel too */ | |
253 | { | |
254 | assert(0 != chptr->members); | |
255 | --chptr->users; | |
256 | return 1; | |
257 | } | |
258 | ||
259 | chptr->users = 0; | |
260 | ||
261 | /* There is a semantics problem here: Assuming no fragments across a | |
262 | * split, a channel without Apass could be maliciously destroyed and | |
263 | * recreated, and someone could set apass on the new instance. | |
264 | * | |
265 | * This could be fixed by preserving the empty non-Apass channel for | |
266 | * the same time as if it had an Apass (but removing +i and +l), and | |
267 | * reopping the first user to rejoin. However, preventing net rides | |
268 | * requires a backwards-incompatible protocol change.. | |
269 | */ | |
270 | if (!chptr->mode.apass[0]) /* If no Apass, destroy now. */ | |
271 | destruct_channel(chptr); | |
272 | else if (TStime() - chptr->creationtime < 172800) /* Channel younger than 48 hours? */ | |
273 | schedule_destruct_event_1m(chptr); /* Get rid of it in approximately 4-5 minutes */ | |
274 | else | |
275 | schedule_destruct_event_48h(chptr); /* Get rid of it in approximately 48 hours */ | |
276 | ||
277 | return 0; | |
278 | } | |
279 | ||
280 | /** Destroy an empty channel | |
281 | * This function destroys an empty channel, removing it from hashtables, | |
282 | * and removing any resources it may have consumed. | |
283 | * | |
284 | * @param chptr The channel to destroy | |
285 | * | |
286 | * @returns 0 (success) | |
287 | * | |
288 | * FIXME: Change to return void, this function never fails. | |
289 | */ | |
290 | int destruct_channel(struct Channel* chptr) | |
291 | { | |
292 | struct Ban *ban, *next; | |
293 | ||
294 | assert(0 == chptr->members); | |
295 | ||
296 | /* | |
297 | * Now, find all invite links from channel structure | |
298 | */ | |
299 | while (chptr->invites) | |
300 | del_invite(chptr->invites->value.cptr, chptr); | |
301 | ||
302 | for (ban = chptr->banlist; ban; ban = next) | |
303 | { | |
304 | next = ban->next; | |
305 | free_ban(ban); | |
306 | } | |
307 | if (chptr->prev) | |
308 | chptr->prev->next = chptr->next; | |
309 | else | |
310 | GlobalChannelList = chptr->next; | |
311 | if (chptr->next) | |
312 | chptr->next->prev = chptr->prev; | |
313 | hRemChannel(chptr); | |
314 | --UserStats.channels; | |
315 | /* | |
316 | * make sure that channel actually got removed from hash table | |
317 | */ | |
318 | assert(chptr->hnext == chptr); | |
319 | MyFree(chptr); | |
320 | return 0; | |
321 | } | |
322 | ||
323 | /** returns Membership * if a person is joined and not a zombie | |
324 | * @param cptr Client | |
325 | * @param chptr Channel | |
326 | * @returns pointer to the client's struct Membership * on the channel if that | |
327 | * user is a full member of the channel, or NULL otherwise. | |
328 | * | |
329 | * @see find_member_link() | |
330 | */ | |
331 | struct Membership* find_channel_member(struct Client* cptr, struct Channel* chptr) | |
332 | { | |
333 | struct Membership* member; | |
334 | assert(0 != chptr); | |
335 | ||
336 | member = find_member_link(chptr, cptr); | |
337 | return (member && !IsZombie(member)) ? member : 0; | |
338 | } | |
339 | ||
340 | /** Searches for a ban from a ban list that matches a user. | |
341 | * @param[in] cptr The client to test. | |
342 | * @param[in] banlist The list of bans to test. | |
343 | * @return Pointer to a matching ban, or NULL if none exit. | |
344 | */ | |
345 | struct Ban *find_ban(struct Client *cptr, struct Ban *banlist) | |
346 | { | |
347 | char nu[NICKLEN + USERLEN + 2]; | |
348 | char tmphost[HOSTLEN + 1]; | |
349 | char iphost[SOCKIPLEN + 1]; | |
350 | char *hostmask; | |
351 | char *sr; | |
352 | struct Ban *found; | |
353 | ||
354 | /* Build nick!user and alternate host names. */ | |
355 | ircd_snprintf(0, nu, sizeof(nu), "%s!%s", | |
356 | cli_name(cptr), cli_user(cptr)->username); | |
357 | ircd_ntoa_r(iphost, &cli_ip(cptr)); | |
358 | if (!IsAccount(cptr)) | |
359 | sr = NULL; | |
360 | else if (HasHiddenHost(cptr)) | |
361 | sr = cli_user(cptr)->realhost; | |
362 | else | |
363 | { | |
364 | ircd_snprintf(0, tmphost, HOSTLEN, "%s.%s", | |
365 | cli_user(cptr)->account, feature_str(FEAT_HIDDEN_HOST)); | |
366 | sr = tmphost; | |
367 | } | |
368 | ||
369 | /* Walk through ban list. */ | |
370 | for (found = NULL; banlist; banlist = banlist->next) { | |
371 | int res; | |
372 | /* If we have found a positive ban already, only consider exceptions. */ | |
373 | if (found && !(banlist->flags & BAN_EXCEPTION)) | |
374 | continue; | |
375 | /* Compare nick!user portion of ban. */ | |
376 | banlist->banstr[banlist->nu_len] = '\0'; | |
377 | res = match(banlist->banstr, nu); | |
378 | banlist->banstr[banlist->nu_len] = '@'; | |
379 | if (res) | |
380 | continue; | |
381 | /* Compare host portion of ban. */ | |
382 | hostmask = banlist->banstr + banlist->nu_len + 1; | |
383 | if (!((banlist->flags & BAN_IPMASK) | |
384 | && ipmask_check(&cli_ip(cptr), &banlist->address, banlist->addrbits)) | |
385 | && match(hostmask, cli_user(cptr)->host) | |
386 | && !(sr && !match(hostmask, sr))) | |
387 | continue; | |
388 | /* If an exception matches, no ban can match. */ | |
389 | if (banlist->flags & BAN_EXCEPTION) | |
390 | return NULL; | |
391 | /* Otherwise, remember this ban but keep searching for an exception. */ | |
392 | found = banlist; | |
393 | } | |
394 | return found; | |
395 | } | |
396 | ||
397 | /** | |
398 | * This function returns true if the user is banned on the said channel. | |
399 | * This function will check the ban cache if applicable, otherwise will | |
400 | * do the comparisons and cache the result. | |
401 | * | |
402 | * @param[in] member The Membership to test for banned-ness. | |
403 | * @return Non-zero if the member is banned, zero if not. | |
404 | */ | |
405 | static int is_banned(struct Membership* member) | |
406 | { | |
407 | if (IsBanValid(member)) | |
408 | return IsBanned(member); | |
409 | ||
410 | SetBanValid(member); | |
411 | if (find_ban(member->user, member->channel->banlist)) { | |
412 | SetBanned(member); | |
413 | return 1; | |
414 | } else { | |
415 | ClearBanned(member); | |
416 | return 0; | |
417 | } | |
418 | } | |
419 | ||
420 | /** add a user to a channel. | |
421 | * adds a user to a channel by adding another link to the channels member | |
422 | * chain. | |
423 | * | |
424 | * @param chptr The channel to add to. | |
425 | * @param who The user to add. | |
426 | * @param flags The flags the user gets initially. | |
427 | * @param oplevel The oplevel the user starts with. | |
428 | */ | |
429 | void add_user_to_channel(struct Channel* chptr, struct Client* who, | |
430 | unsigned int flags, int oplevel) | |
431 | { | |
432 | assert(0 != chptr); | |
433 | assert(0 != who); | |
434 | ||
435 | if (cli_user(who)) { | |
436 | ||
437 | struct Membership* member = membershipFreeList; | |
438 | if (member) | |
439 | membershipFreeList = member->next_member; | |
440 | else { | |
441 | member = (struct Membership*) MyMalloc(sizeof(struct Membership)); | |
442 | ++membershipAllocCount; | |
443 | } | |
444 | ||
445 | assert(0 != member); | |
446 | member->user = who; | |
447 | member->channel = chptr; | |
448 | member->status = flags; | |
449 | SetOpLevel(member, oplevel); | |
450 | ||
451 | member->next_member = chptr->members; | |
452 | if (member->next_member) | |
453 | member->next_member->prev_member = member; | |
454 | member->prev_member = 0; | |
455 | chptr->members = member; | |
456 | ||
457 | member->next_channel = (cli_user(who))->channel; | |
458 | if (member->next_channel) | |
459 | member->next_channel->prev_channel = member; | |
460 | member->prev_channel = 0; | |
461 | (cli_user(who))->channel = member; | |
462 | ||
463 | if (chptr->destruct_event) | |
464 | remove_destruct_event(chptr); | |
465 | ++chptr->users; | |
466 | ++((cli_user(who))->joined); | |
467 | } | |
468 | } | |
469 | ||
470 | /** Remove a person from a channel, given their Membership* | |
471 | * | |
472 | * @param member A member of a channel. | |
473 | * | |
474 | * @returns true if there are more people in the channel. | |
475 | */ | |
476 | static int remove_member_from_channel(struct Membership* member) | |
477 | { | |
478 | struct Channel* chptr; | |
479 | assert(0 != member); | |
480 | chptr = member->channel; | |
481 | /* | |
482 | * unlink channel member list | |
483 | */ | |
484 | if (member->next_member) | |
485 | member->next_member->prev_member = member->prev_member; | |
486 | if (member->prev_member) | |
487 | member->prev_member->next_member = member->next_member; | |
488 | else | |
489 | member->channel->members = member->next_member; | |
490 | ||
491 | /* | |
492 | * If this is the last delayed-join user, may have to clear WASDELJOINS. | |
493 | */ | |
494 | if (IsDelayedJoin(member)) | |
495 | CheckDelayedJoins(chptr); | |
496 | ||
497 | /* | |
498 | * unlink client channel list | |
499 | */ | |
500 | if (member->next_channel) | |
501 | member->next_channel->prev_channel = member->prev_channel; | |
502 | if (member->prev_channel) | |
503 | member->prev_channel->next_channel = member->next_channel; | |
504 | else | |
505 | (cli_user(member->user))->channel = member->next_channel; | |
506 | ||
507 | --(cli_user(member->user))->joined; | |
508 | ||
509 | member->next_member = membershipFreeList; | |
510 | membershipFreeList = member; | |
511 | ||
512 | return sub1_from_channel(chptr); | |
513 | } | |
514 | ||
515 | /** Check if all the remaining members on the channel are zombies | |
516 | * | |
517 | * @returns False if the channel has any non zombie members, True otherwise. | |
518 | * @see \ref zombie | |
519 | */ | |
520 | static int channel_all_zombies(struct Channel* chptr) | |
521 | { | |
522 | struct Membership* member; | |
523 | ||
524 | for (member = chptr->members; member; member = member->next_member) { | |
525 | if (!IsZombie(member)) | |
526 | return 0; | |
527 | } | |
528 | return 1; | |
529 | } | |
530 | ||
531 | ||
532 | /** Remove a user from a channel | |
533 | * This is the generic entry point for removing a user from a channel, this | |
534 | * function will remove the client from the channel, and destroy the channel | |
535 | * if there are no more normal users left. | |
536 | * | |
537 | * @param cptr The client | |
538 | * @param chptr The channel | |
539 | */ | |
540 | void remove_user_from_channel(struct Client* cptr, struct Channel* chptr) | |
541 | { | |
542 | ||
543 | struct Membership* member; | |
544 | assert(0 != chptr); | |
545 | ||
546 | if ((member = find_member_link(chptr, cptr))) { | |
547 | if (remove_member_from_channel(member)) { | |
548 | if (channel_all_zombies(chptr)) { | |
549 | /* | |
550 | * XXX - this looks dangerous but isn't if we got the referential | |
551 | * integrity right for channels | |
552 | */ | |
553 | while (remove_member_from_channel(chptr->members)) | |
554 | ; | |
555 | } | |
556 | } | |
557 | } | |
558 | } | |
559 | ||
560 | /** Remove a user from all channels they are on. | |
561 | * | |
562 | * This function removes a user from all channels they are on. | |
563 | * | |
564 | * @param cptr The client to remove. | |
565 | */ | |
566 | void remove_user_from_all_channels(struct Client* cptr) | |
567 | { | |
568 | struct Membership* chan; | |
569 | assert(0 != cptr); | |
570 | assert(0 != cli_user(cptr)); | |
571 | ||
572 | while ((chan = (cli_user(cptr))->channel)) | |
573 | remove_user_from_channel(cptr, chan->channel); | |
574 | } | |
575 | ||
576 | /** Check if this user is a legitimate chanop | |
577 | * | |
578 | * @param cptr Client to check | |
579 | * @param chptr Channel to check | |
580 | * | |
581 | * @returns True if the user is a chanop (And not a zombie), False otherwise. | |
582 | * @see \ref zombie | |
583 | */ | |
584 | int is_chan_op(struct Client *cptr, struct Channel *chptr) | |
585 | { | |
586 | struct Membership* member; | |
587 | assert(chptr); | |
588 | if ((member = find_member_link(chptr, cptr))) | |
589 | return (!IsZombie(member) && IsChanOp(member)); | |
590 | ||
591 | return 0; | |
592 | } | |
593 | ||
594 | /** Check if a user is a Zombie on a specific channel. | |
595 | * | |
596 | * @param cptr The client to check. | |
597 | * @param chptr The channel to check. | |
598 | * | |
599 | * @returns True if the client (cptr) is a zombie on the channel (chptr), | |
600 | * False otherwise. | |
601 | * | |
602 | * @see \ref zombie | |
603 | */ | |
604 | int is_zombie(struct Client *cptr, struct Channel *chptr) | |
605 | { | |
606 | struct Membership* member; | |
607 | ||
608 | assert(0 != chptr); | |
609 | ||
610 | if ((member = find_member_link(chptr, cptr))) | |
611 | return IsZombie(member); | |
612 | return 0; | |
613 | } | |
614 | ||
615 | /** Returns if a user has voice on a channel. | |
616 | * | |
617 | * @param cptr The client | |
618 | * @param chptr The channel | |
619 | * | |
620 | * @returns True if the client (cptr) is voiced on (chptr) and is not a zombie. | |
621 | * @see \ref zombie | |
622 | */ | |
623 | int has_voice(struct Client* cptr, struct Channel* chptr) | |
624 | { | |
625 | struct Membership* member; | |
626 | ||
627 | assert(0 != chptr); | |
628 | if ((member = find_member_link(chptr, cptr))) | |
629 | return (!IsZombie(member) && HasVoice(member)); | |
630 | ||
631 | return 0; | |
632 | } | |
633 | ||
634 | /** Can this member send to a channel | |
635 | * | |
636 | * A user can speak on a channel iff: | |
637 | * <ol> | |
638 | * <li> They didn't use the Apass to gain ops. | |
639 | * <li> They are op'd or voice'd. | |
640 | * <li> You aren't banned. | |
641 | * <li> The channel isn't +m | |
642 | * <li> The channel isn't +n or you are on the channel. | |
643 | * </ol> | |
644 | * | |
645 | * This function will optionally reveal a user on a delayed join channel if | |
646 | * they are allowed to send to the channel. | |
647 | * | |
648 | * @param member The membership of the user | |
649 | * @param reveal If true, the user will be "revealed" on a delayed | |
650 | * joined channel. | |
651 | * | |
652 | * @returns True if the client can speak on the channel. | |
653 | */ | |
654 | int member_can_send_to_channel(struct Membership* member, int reveal) | |
655 | { | |
656 | assert(0 != member); | |
657 | ||
658 | /* Discourage using the Apass to get op. They should use the upass. */ | |
659 | if (IsChannelManager(member) && member->channel->mode.apass[0]) | |
660 | return 0; | |
661 | ||
662 | if (IsVoicedOrOpped(member)) | |
663 | return 1; | |
664 | ||
665 | /* | |
666 | * If it's moderated, and you aren't a privileged user, you can't | |
667 | * speak. | |
668 | */ | |
669 | if (member->channel->mode.mode & MODE_MODERATED) | |
670 | return 0; | |
671 | /* If only logged in users may join and you're not one, you can't speak. */ | |
672 | if (member->channel->mode.mode & MODE_REGONLY && !IsAccount(member->user)) | |
673 | return 0; | |
674 | /* | |
675 | * If you're banned then you can't speak either. | |
676 | * but because of the amount of CPU time that is_banned chews | |
677 | * we only check it for our clients. | |
678 | */ | |
679 | if (MyUser(member->user) && is_banned(member)) | |
680 | return 0; | |
681 | ||
682 | if (IsDelayedJoin(member) && reveal) | |
683 | RevealDelayedJoin(member); | |
684 | ||
685 | return 1; | |
686 | } | |
687 | ||
688 | /** Check if a client can send to a channel. | |
689 | * | |
690 | * Has the added check over member_can_send_to_channel() of servers can | |
691 | * always speak. | |
692 | * | |
693 | * @param cptr The client to check | |
694 | * @param chptr The channel to check | |
695 | * @param reveal If the user should be revealed (see | |
696 | * member_can_send_to_channel()) | |
697 | * | |
698 | * @returns true if the client is allowed to speak on the channel, false | |
699 | * otherwise | |
700 | * | |
701 | * @see member_can_send_to_channel() | |
702 | */ | |
703 | int client_can_send_to_channel(struct Client *cptr, struct Channel *chptr, int reveal) | |
704 | { | |
705 | struct Membership *member; | |
706 | assert(0 != cptr); | |
707 | /* | |
708 | * Servers can always speak on channels. | |
709 | */ | |
943fcf70 | 710 | if (IsServer(cptr) || IsXtraOp(cptr)) |
189935b1 | 711 | return 1; |
712 | ||
713 | member = find_channel_member(cptr, chptr); | |
714 | ||
715 | /* | |
716 | * You can't speak if you're off channel, and it is +n (no external messages) | |
717 | * or +m (moderated). | |
718 | */ | |
719 | if (!member) { | |
720 | if ((chptr->mode.mode & (MODE_NOPRIVMSGS|MODE_MODERATED)) || | |
721 | ((chptr->mode.mode & MODE_REGONLY) && !IsAccount(cptr))) | |
722 | return 0; | |
723 | else | |
724 | return !find_ban(cptr, chptr->banlist); | |
725 | } | |
726 | return member_can_send_to_channel(member, reveal); | |
727 | } | |
728 | ||
729 | /** Returns the name of a channel that prevents the user from changing nick. | |
730 | * if a member and not (opped or voiced) and (banned or moderated), return | |
731 | * the name of the first channel banned on. | |
732 | * | |
733 | * @param cptr The client | |
734 | * | |
735 | * @returns the name of the first channel banned on, or NULL if the user | |
736 | * can change nicks. | |
737 | */ | |
738 | const char* find_no_nickchange_channel(struct Client* cptr) | |
739 | { | |
740 | if (MyUser(cptr)) { | |
741 | struct Membership* member; | |
742 | for (member = (cli_user(cptr))->channel; member; | |
743 | member = member->next_channel) { | |
744 | if (!IsVoicedOrOpped(member) && | |
745 | (is_banned(member) || | |
746 | (member->channel->mode.mode & MODE_MODERATED))) | |
747 | return member->channel->chname; | |
748 | } | |
749 | } | |
750 | return 0; | |
751 | } | |
752 | ||
753 | ||
754 | /** Fill mbuf/pbuf with modes from chptr | |
755 | * write the "simple" list of channel modes for channel chptr onto buffer mbuf | |
756 | * with the parameters in pbuf as visible by cptr. | |
757 | * | |
758 | * This function will hide keys from non-op'd, non-server clients. | |
759 | * | |
760 | * @param cptr The client to generate the mode for. | |
761 | * @param mbuf The buffer to write the modes into. | |
762 | * @param pbuf The buffer to write the mode parameters into. | |
763 | * @param buflen The length of the buffers. | |
764 | * @param chptr The channel to get the modes from. | |
765 | * @param member The membership of this client on this channel (or NULL | |
766 | * if this client isn't on this channel) | |
767 | * | |
768 | */ | |
769 | void channel_modes(struct Client *cptr, char *mbuf, char *pbuf, int buflen, | |
770 | struct Channel *chptr, struct Membership *member) | |
771 | { | |
772 | int previous_parameter = 0; | |
773 | ||
774 | assert(0 != mbuf); | |
775 | assert(0 != pbuf); | |
776 | assert(0 != chptr); | |
777 | ||
778 | *mbuf++ = '+'; | |
779 | if (chptr->mode.mode & MODE_SECRET) | |
780 | *mbuf++ = 's'; | |
781 | else if (chptr->mode.mode & MODE_PRIVATE) | |
782 | *mbuf++ = 'p'; | |
783 | if (chptr->mode.mode & MODE_MODERATED) | |
784 | *mbuf++ = 'm'; | |
785 | if (chptr->mode.mode & MODE_TOPICLIMIT) | |
786 | *mbuf++ = 't'; | |
787 | if (chptr->mode.mode & MODE_INVITEONLY) | |
788 | *mbuf++ = 'i'; | |
789 | if (chptr->mode.mode & MODE_NOPRIVMSGS) | |
790 | *mbuf++ = 'n'; | |
791 | if (chptr->mode.mode & MODE_REGONLY) | |
792 | *mbuf++ = 'r'; | |
149c0c9f | 793 | if (chptr->mode.mode & MODE_NOQUITPARTS) |
794 | *mbuf++ = 'u'; | |
189935b1 | 795 | if (chptr->mode.mode & MODE_DELJOINS) |
796 | *mbuf++ = 'D'; | |
797 | else if (MyUser(cptr) && (chptr->mode.mode & MODE_WASDELJOINS)) | |
798 | *mbuf++ = 'd'; | |
799 | if (chptr->mode.limit) { | |
800 | *mbuf++ = 'l'; | |
801 | ircd_snprintf(0, pbuf, buflen, "%u", chptr->mode.limit); | |
802 | previous_parameter = 1; | |
803 | } | |
804 | ||
805 | if (*chptr->mode.key) { | |
806 | *mbuf++ = 'k'; | |
807 | if (previous_parameter) | |
808 | strcat(pbuf, " "); | |
149c0c9f | 809 | if (is_chan_op(cptr, chptr) || IsServer(cptr) || IsOper(cptr)) { |
189935b1 | 810 | strcat(pbuf, chptr->mode.key); |
811 | } else | |
812 | strcat(pbuf, "*"); | |
813 | previous_parameter = 1; | |
814 | } | |
815 | if (*chptr->mode.apass) { | |
816 | *mbuf++ = 'A'; | |
817 | if (previous_parameter) | |
818 | strcat(pbuf, " "); | |
819 | if (IsServer(cptr)) { | |
820 | strcat(pbuf, chptr->mode.apass); | |
821 | } else | |
822 | strcat(pbuf, "*"); | |
823 | previous_parameter = 1; | |
824 | } | |
825 | if (*chptr->mode.upass) { | |
826 | *mbuf++ = 'U'; | |
827 | if (previous_parameter) | |
828 | strcat(pbuf, " "); | |
829 | if (IsServer(cptr) || (member && IsChanOp(member) && OpLevel(member) == 0)) { | |
830 | strcat(pbuf, chptr->mode.upass); | |
831 | } else | |
832 | strcat(pbuf, "*"); | |
833 | } | |
834 | *mbuf = '\0'; | |
835 | } | |
836 | ||
837 | /** Compare two members oplevel | |
838 | * | |
839 | * @param mp1 Pointer to a pointer to a membership | |
840 | * @param mp2 Pointer to a pointer to a membership | |
841 | * | |
842 | * @returns 0 if equal, -1 if mp1 is lower, +1 otherwise. | |
843 | * | |
844 | * Used for qsort(3). | |
845 | */ | |
846 | int compare_member_oplevel(const void *mp1, const void *mp2) | |
847 | { | |
848 | struct Membership const* member1 = *(struct Membership const**)mp1; | |
849 | struct Membership const* member2 = *(struct Membership const**)mp2; | |
850 | if (member1->oplevel == member2->oplevel) | |
851 | return 0; | |
852 | return (member1->oplevel < member2->oplevel) ? -1 : 1; | |
853 | } | |
854 | ||
855 | /* send "cptr" a full list of the modes for channel chptr. | |
856 | * | |
857 | * Sends a BURST line to cptr, bursting all the modes for the channel. | |
858 | * | |
859 | * @param cptr Client pointer | |
860 | * @param chptr Channel pointer | |
861 | */ | |
862 | void send_channel_modes(struct Client *cptr, struct Channel *chptr) | |
863 | { | |
864 | /* The order in which modes are generated is now mandatory */ | |
865 | static unsigned int current_flags[4] = | |
866 | { 0, CHFL_VOICE, CHFL_CHANOP, CHFL_CHANOP | CHFL_VOICE }; | |
867 | int first = 1; | |
868 | int full = 1; | |
869 | int flag_cnt = 0; | |
870 | int new_mode = 0; | |
871 | size_t len; | |
872 | struct Membership* member; | |
873 | struct Ban* lp2; | |
874 | char modebuf[MODEBUFLEN]; | |
875 | char parabuf[MODEBUFLEN]; | |
876 | struct MsgBuf *mb; | |
877 | int number_of_ops = 0; | |
878 | int opped_members_index = 0; | |
879 | struct Membership** opped_members = NULL; | |
880 | int last_oplevel = 0; | |
881 | int feat_oplevels = (chptr->mode.apass[0]) != '\0'; | |
882 | ||
883 | assert(0 != cptr); | |
884 | assert(0 != chptr); | |
885 | ||
886 | if (IsLocalChannel(chptr->chname)) | |
887 | return; | |
888 | ||
889 | member = chptr->members; | |
890 | lp2 = chptr->banlist; | |
891 | ||
892 | *modebuf = *parabuf = '\0'; | |
893 | channel_modes(cptr, modebuf, parabuf, sizeof(parabuf), chptr, 0); | |
894 | ||
895 | for (first = 1; full; first = 0) /* Loop for multiple messages */ | |
896 | { | |
897 | full = 0; /* Assume by default we get it | |
898 | all in one message */ | |
899 | ||
900 | /* (Continued) prefix: "<Y> B <channel> <TS>" */ | |
901 | /* is there any better way we can do this? */ | |
902 | mb = msgq_make(&me, "%C " TOK_BURST " %H %Tu", &me, chptr, | |
903 | chptr->creationtime); | |
904 | ||
905 | if (first && modebuf[1]) /* Add simple modes (Aiklmnpstu) | |
906 | if first message */ | |
907 | { | |
908 | /* prefix: "<Y> B <channel> <TS>[ <modes>[ <params>]]" */ | |
909 | msgq_append(&me, mb, " %s", modebuf); | |
910 | ||
911 | if (*parabuf) | |
912 | msgq_append(&me, mb, " %s", parabuf); | |
913 | } | |
914 | ||
915 | /* | |
916 | * Attach nicks, comma separated " nick[:modes],nick[:modes],..." | |
917 | * | |
918 | * First find all opless members. | |
919 | * Run 2 times over all members, to group the members with | |
920 | * and without voice together. | |
921 | * Then run 2 times over all opped members (which are ordered | |
922 | * by op-level) to also group voice and non-voice together. | |
923 | */ | |
924 | for (first = 1; flag_cnt < 4; new_mode = 1, ++flag_cnt) | |
925 | { | |
926 | while (member) | |
927 | { | |
928 | if (flag_cnt < 2 && IsChanOp(member)) | |
929 | { | |
930 | /* | |
931 | * The first loop (to find all non-voice/op), we count the ops. | |
932 | * The second loop (to find all voiced non-ops), store the ops | |
933 | * in a dynamic array. | |
934 | */ | |
935 | if (flag_cnt == 0) | |
936 | ++number_of_ops; | |
937 | else | |
938 | opped_members[opped_members_index++] = member; | |
939 | } | |
940 | /* Only handle the members with the flags that we are interested in. */ | |
941 | if ((member->status & CHFL_VOICED_OR_OPPED) == current_flags[flag_cnt]) | |
942 | { | |
943 | if (msgq_bufleft(mb) < NUMNICKLEN + 3 + MAXOPLEVELDIGITS) | |
944 | /* The 3 + MAXOPLEVELDIGITS is a possible ",:v999". */ | |
945 | { | |
946 | full = 1; /* Make sure we continue after | |
947 | sending it so far */ | |
948 | /* Ensure the new BURST line contains the current | |
949 | * ":mode", except when there is no mode yet. */ | |
950 | new_mode = (flag_cnt > 0) ? 1 : 0; | |
951 | break; /* Do not add this member to this message */ | |
952 | } | |
953 | msgq_append(&me, mb, "%c%C", first ? ' ' : ',', member->user); | |
954 | first = 0; /* From now on, use commas to add new nicks */ | |
955 | ||
956 | /* | |
957 | * Do we have a nick with a new mode ? | |
958 | * Or are we starting a new BURST line? | |
959 | */ | |
960 | if (new_mode) | |
961 | { | |
962 | /* | |
963 | * This means we are at the _first_ member that has only | |
964 | * voice, or the first member that has only ops, or the | |
965 | * first member that has voice and ops (so we get here | |
966 | * at most three times, plus once for every start of | |
967 | * a continued BURST line where only these modes is current. | |
968 | * In the two cases where the current mode includes ops, | |
969 | * we need to add the _absolute_ value of the oplevel to the mode. | |
970 | */ | |
971 | char tbuf[3 + MAXOPLEVELDIGITS] = ":"; | |
972 | int loc = 1; | |
973 | ||
974 | if (HasVoice(member)) /* flag_cnt == 1 or 3 */ | |
975 | tbuf[loc++] = 'v'; | |
976 | if (IsChanOp(member)) /* flag_cnt == 2 or 3 */ | |
977 | { | |
978 | /* append the absolute value of the oplevel */ | |
979 | if (feat_oplevels) | |
980 | loc += ircd_snprintf(0, tbuf + loc, sizeof(tbuf) - loc, "%u", last_oplevel = member->oplevel); | |
981 | else | |
982 | tbuf[loc++] = 'o'; | |
983 | } | |
984 | tbuf[loc] = '\0'; | |
985 | msgq_append(&me, mb, tbuf); | |
986 | new_mode = 0; | |
987 | } | |
988 | else if (feat_oplevels && flag_cnt > 1 && last_oplevel != member->oplevel) | |
989 | { | |
990 | /* | |
991 | * This can't be the first member of a (continued) BURST | |
992 | * message because then either flag_cnt == 0 or new_mode == 1 | |
993 | * Now we need to append the incremental value of the oplevel. | |
994 | */ | |
995 | char tbuf[2 + MAXOPLEVELDIGITS]; | |
996 | ircd_snprintf(0, tbuf, sizeof(tbuf), ":%u", member->oplevel - last_oplevel); | |
997 | last_oplevel = member->oplevel; | |
998 | msgq_append(&me, mb, tbuf); | |
999 | } | |
1000 | } | |
1001 | /* Go to the next `member'. */ | |
1002 | if (flag_cnt < 2) | |
1003 | member = member->next_member; | |
1004 | else | |
1005 | member = opped_members[++opped_members_index]; | |
1006 | } | |
1007 | if (full) | |
1008 | break; | |
1009 | ||
1010 | /* Point `member' at the start of the list again. */ | |
1011 | if (flag_cnt == 0) | |
1012 | { | |
1013 | member = chptr->members; | |
1014 | /* Now, after one loop, we know the number of ops and can | |
1015 | * allocate the dynamic array with pointer to the ops. */ | |
1016 | opped_members = (struct Membership**) | |
1017 | MyMalloc((number_of_ops + 1) * sizeof(struct Membership*)); | |
1018 | opped_members[number_of_ops] = NULL; /* Needed for loop termination */ | |
1019 | } | |
1020 | else | |
1021 | { | |
1022 | /* At the end of the second loop, sort the opped members with | |
1023 | * increasing op-level, so that we will output them in the | |
1024 | * correct order (and all op-level increments stay positive) */ | |
1025 | if (flag_cnt == 1) | |
1026 | qsort(opped_members, number_of_ops, | |
1027 | sizeof(struct Membership*), compare_member_oplevel); | |
1028 | /* The third and fourth loop run only over the opped members. */ | |
1029 | member = opped_members[(opped_members_index = 0)]; | |
1030 | } | |
1031 | ||
1032 | } /* loop over 0,+v,+o,+ov */ | |
1033 | ||
1034 | if (!full) | |
1035 | { | |
1036 | /* Attach all bans, space separated " :%ban ban ..." */ | |
1037 | for (first = 2; lp2; lp2 = lp2->next) | |
1038 | { | |
1039 | len = strlen(lp2->banstr); | |
1040 | if (msgq_bufleft(mb) < len + 1 + first) | |
1041 | /* The +1 stands for the added ' '. | |
1042 | * The +first stands for the added ":%". | |
1043 | */ | |
1044 | { | |
1045 | full = 1; | |
1046 | break; | |
1047 | } | |
1048 | msgq_append(&me, mb, " %s%s", first ? ":%" : "", | |
1049 | lp2->banstr); | |
1050 | first = 0; | |
1051 | } | |
1052 | } | |
1053 | ||
1054 | send_buffer(cptr, mb, 0); /* Send this message */ | |
1055 | msgq_clean(mb); | |
1056 | } /* Continue when there was something | |
1057 | that didn't fit (full==1) */ | |
1058 | if (opped_members) | |
1059 | MyFree(opped_members); | |
1060 | if (feature_bool(FEAT_TOPIC_BURST) && (chptr->topic[0] != '\0')) | |
1061 | sendcmdto_one(&me, CMD_TOPIC, cptr, "%H %Tu %Tu :%s", chptr, | |
1062 | chptr->creationtime, chptr->topic_time, chptr->topic); | |
1063 | } | |
1064 | ||
1065 | /** Canonify a mask. | |
1066 | * pretty_mask | |
1067 | * | |
1068 | * @author Carlo Wood (Run), | |
1069 | * 05 Oct 1998. | |
1070 | * | |
1071 | * When the nick is longer then NICKLEN, it is cut off (its an error of course). | |
1072 | * When the user name or host name are too long (USERLEN and HOSTLEN | |
1073 | * respectively) then they are cut off at the start with a '*'. | |
1074 | * | |
1075 | * The following transformations are made: | |
1076 | * | |
1077 | * 1) xxx -> nick!*@* | |
1078 | * 2) xxx.xxx -> *!*\@host | |
1079 | * 3) xxx\!yyy -> nick!user\@* | |
1080 | * 4) xxx\@yyy -> *!user\@host | |
1081 | * 5) xxx!yyy\@zzz -> nick!user\@host | |
1082 | * | |
1083 | * @param mask The uncanonified mask. | |
1084 | * @returns The updated mask in a static buffer. | |
1085 | */ | |
1086 | char *pretty_mask(char *mask) | |
1087 | { | |
1088 | static char star[2] = { '*', 0 }; | |
1089 | static char retmask[NICKLEN + USERLEN + HOSTLEN + 3]; | |
1090 | char *last_dot = NULL; | |
1091 | char *ptr; | |
1092 | ||
1093 | /* Case 1: default */ | |
1094 | char *nick = mask; | |
1095 | char *user = star; | |
1096 | char *host = star; | |
1097 | ||
1098 | /* Do a _single_ pass through the characters of the mask: */ | |
1099 | for (ptr = mask; *ptr; ++ptr) | |
1100 | { | |
1101 | if (*ptr == '!') | |
1102 | { | |
1103 | /* Case 3 or 5: Found first '!' (without finding a '@' yet) */ | |
1104 | user = ++ptr; | |
1105 | host = star; | |
1106 | } | |
1107 | else if (*ptr == '@') | |
1108 | { | |
1109 | /* Case 4: Found last '@' (without finding a '!' yet) */ | |
1110 | nick = star; | |
1111 | user = mask; | |
1112 | host = ++ptr; | |
1113 | } | |
1114 | else if (*ptr == '.' || *ptr == ':') | |
1115 | { | |
1116 | /* Case 2: Found character specific to IP or hostname (without | |
1117 | * finding a '!' or '@' yet) */ | |
1118 | last_dot = ptr; | |
1119 | continue; | |
1120 | } | |
1121 | else | |
1122 | continue; | |
1123 | for (; *ptr; ++ptr) | |
1124 | { | |
1125 | if (*ptr == '@') | |
1126 | { | |
1127 | /* Case 4 or 5: Found last '@' */ | |
1128 | host = ptr + 1; | |
1129 | } | |
1130 | } | |
1131 | break; | |
1132 | } | |
1133 | if (user == star && last_dot) | |
1134 | { | |
1135 | /* Case 2: */ | |
1136 | nick = star; | |
1137 | user = star; | |
1138 | host = mask; | |
1139 | } | |
1140 | /* Check lengths */ | |
1141 | if (nick != star) | |
1142 | { | |
1143 | char *nick_end = (user != star) ? user - 1 : ptr; | |
1144 | if (nick_end - nick > NICKLEN) | |
1145 | nick[NICKLEN] = 0; | |
1146 | *nick_end = 0; | |
1147 | } | |
1148 | if (user != star) | |
1149 | { | |
1150 | char *user_end = (host != star) ? host - 1 : ptr; | |
1151 | if (user_end - user > USERLEN) | |
1152 | { | |
1153 | user = user_end - USERLEN; | |
1154 | *user = '*'; | |
1155 | } | |
1156 | *user_end = 0; | |
1157 | } | |
1158 | if (host != star && ptr - host > HOSTLEN) | |
1159 | { | |
1160 | host = ptr - HOSTLEN; | |
1161 | *host = '*'; | |
1162 | } | |
1163 | ircd_snprintf(0, retmask, sizeof(retmask), "%s!%s@%s", nick, user, host); | |
1164 | return retmask; | |
1165 | } | |
1166 | ||
1167 | /** send a banlist to a client for a channel | |
1168 | * | |
1169 | * @param cptr Client to send the banlist to. | |
1170 | * @param chptr Channel whose banlist to send. | |
1171 | */ | |
1172 | static void send_ban_list(struct Client* cptr, struct Channel* chptr) | |
1173 | { | |
1174 | struct Ban* lp; | |
1175 | ||
1176 | assert(0 != cptr); | |
1177 | assert(0 != chptr); | |
1178 | ||
1179 | for (lp = chptr->banlist; lp; lp = lp->next) | |
1180 | send_reply(cptr, RPL_BANLIST, chptr->chname, lp->banstr, | |
1181 | lp->who, lp->when); | |
1182 | ||
1183 | send_reply(cptr, RPL_ENDOFBANLIST, chptr->chname); | |
1184 | } | |
1185 | ||
1186 | /** Remove bells and commas from channel name | |
1187 | * | |
1188 | * @param cn Channel name to clean, modified in place. | |
1189 | */ | |
1190 | void clean_channelname(char *cn) | |
1191 | { | |
1192 | int i; | |
1193 | ||
1194 | for (i = 0; cn[i]; i++) { | |
1195 | if (i >= IRCD_MIN(CHANNELLEN, feature_int(FEAT_CHANNELLEN)) | |
1196 | || !IsChannelChar(cn[i])) { | |
1197 | cn[i] = '\0'; | |
1198 | return; | |
1199 | } | |
1200 | if (IsChannelLower(cn[i])) { | |
1201 | cn[i] = ToLower(cn[i]); | |
1202 | #ifndef FIXME | |
1203 | /* | |
1204 | * Remove for .08+ | |
1205 | * toupper(0xd0) | |
1206 | */ | |
1207 | if ((unsigned char)(cn[i]) == 0xd0) | |
1208 | cn[i] = (char) 0xf0; | |
1209 | #endif | |
1210 | } | |
1211 | } | |
1212 | } | |
1213 | ||
1214 | /** Get a channel block, creating if necessary. | |
1215 | * Get Channel block for chname (and allocate a new channel | |
1216 | * block, if it didn't exists before). | |
1217 | * | |
1218 | * @param cptr Client joining the channel. | |
1219 | * @param chname The name of the channel to join. | |
1220 | * @param flag set to CGT_CREATE to create the channel if it doesn't | |
1221 | * exist | |
1222 | * | |
1223 | * @returns NULL if the channel is invalid, doesn't exist and CGT_CREATE | |
1224 | * wasn't specified or a pointer to the channel structure | |
1225 | */ | |
1226 | struct Channel *get_channel(struct Client *cptr, char *chname, ChannelGetType flag) | |
1227 | { | |
1228 | struct Channel *chptr; | |
1229 | int len; | |
1230 | ||
1231 | if (EmptyString(chname)) | |
1232 | return NULL; | |
1233 | ||
1234 | len = strlen(chname); | |
1235 | if (MyUser(cptr) && len > CHANNELLEN) | |
1236 | { | |
1237 | len = CHANNELLEN; | |
1238 | *(chname + CHANNELLEN) = '\0'; | |
1239 | } | |
1240 | if ((chptr = FindChannel(chname))) | |
1241 | return (chptr); | |
1242 | if (flag == CGT_CREATE) | |
1243 | { | |
1244 | chptr = (struct Channel*) MyMalloc(sizeof(struct Channel) + len); | |
1245 | assert(0 != chptr); | |
1246 | ++UserStats.channels; | |
1247 | memset(chptr, 0, sizeof(struct Channel)); | |
1248 | strcpy(chptr->chname, chname); | |
1249 | if (GlobalChannelList) | |
1250 | GlobalChannelList->prev = chptr; | |
1251 | chptr->prev = NULL; | |
1252 | chptr->next = GlobalChannelList; | |
1253 | chptr->creationtime = MyUser(cptr) ? TStime() : (time_t) 0; | |
1254 | GlobalChannelList = chptr; | |
1255 | hAddChannel(chptr); | |
1256 | } | |
1257 | return chptr; | |
1258 | } | |
1259 | ||
f952a879 | 1260 | int SetAutoChanModes(struct Channel *chptr) |
1261 | { | |
1262 | static int chan_flags[] = { | |
1263 | MODE_PRIVATE, 'p', | |
1264 | MODE_SECRET, 's', | |
1265 | MODE_MODERATED, 'm', | |
1266 | MODE_TOPICLIMIT, 't', | |
1267 | MODE_INVITEONLY, 'i', | |
1268 | MODE_NOPRIVMSGS, 'n', | |
1269 | MODE_REGONLY, 'r', | |
1270 | /* MODE_NOCOLOUR, 'c', | |
1271 | MODE_NOCTCP, 'C', | |
1272 | MODE_NONOTICE, 'N',*/ | |
1273 | MODE_DELJOINS, 'D', | |
1274 | MODE_NOQUITPARTS, 'u' | |
1275 | }; | |
1276 | ||
1277 | unsigned int *flag_p; | |
1278 | unsigned int t_mode; | |
1279 | const char *modestr; | |
1280 | ||
1281 | t_mode = 0; | |
1282 | ||
1283 | assert(0 != chptr); | |
1284 | ||
1285 | if (!feature_bool(FEAT_AUTOCHANMODES) || !feature_str(FEAT_AUTOCHANMODES_LIST) || strlen(feature_str(FEAT_AUTOCHANMODES_LIST)) <= 1) | |
1286 | return(-1); | |
1287 | ||
1288 | modestr = feature_str(FEAT_AUTOCHANMODES_LIST); | |
1289 | ||
1290 | for (; *modestr; modestr++) { | |
1291 | for (flag_p = chan_flags; flag_p[0]; flag_p += 2) /* look up flag */ | |
1292 | if (flag_p[1] == *modestr) | |
1293 | break; | |
1294 | ||
1295 | if (!flag_p[0]) /* didn't find it */ | |
1296 | continue; | |
1297 | ||
1298 | t_mode |= flag_p[0]; | |
1299 | ||
1300 | } /* for (; *modestr; modestr++) { */ | |
1301 | ||
1302 | if (t_mode != 0) | |
1303 | chptr->mode.mode = t_mode; | |
1304 | return(0); | |
1305 | } | |
1306 | ||
189935b1 | 1307 | /** invite a user to a channel. |
1308 | * | |
1309 | * Adds an invite for a user to a channel. Limits the number of invites | |
1310 | * to FEAT_MAXCHANNELSPERUSER. Does not sent notification to the user. | |
1311 | * | |
1312 | * @param cptr The client to be invited. | |
1313 | * @param chptr The channel to be invited to. | |
1314 | */ | |
1315 | void add_invite(struct Client *cptr, struct Channel *chptr) | |
1316 | { | |
1317 | struct SLink *inv, **tmp; | |
1318 | ||
1319 | del_invite(cptr, chptr); | |
1320 | /* | |
1321 | * Delete last link in chain if the list is max length | |
1322 | */ | |
1323 | assert(list_length((cli_user(cptr))->invited) == (cli_user(cptr))->invites); | |
1324 | if ((cli_user(cptr))->invites >= feature_int(FEAT_MAXCHANNELSPERUSER)) | |
1325 | del_invite(cptr, (cli_user(cptr))->invited->value.chptr); | |
1326 | /* | |
1327 | * Add client to channel invite list | |
1328 | */ | |
1329 | inv = make_link(); | |
1330 | inv->value.cptr = cptr; | |
1331 | inv->next = chptr->invites; | |
1332 | chptr->invites = inv; | |
1333 | /* | |
1334 | * Add channel to the end of the client invite list | |
1335 | */ | |
1336 | for (tmp = &((cli_user(cptr))->invited); *tmp; tmp = &((*tmp)->next)); | |
1337 | inv = make_link(); | |
1338 | inv->value.chptr = chptr; | |
1339 | inv->next = NULL; | |
1340 | (*tmp) = inv; | |
1341 | (cli_user(cptr))->invites++; | |
1342 | } | |
1343 | ||
1344 | /** Delete an invite | |
1345 | * Delete Invite block from channel invite list and client invite list | |
1346 | * | |
1347 | * @param cptr Client pointer | |
1348 | * @param chptr Channel pointer | |
1349 | */ | |
1350 | void del_invite(struct Client *cptr, struct Channel *chptr) | |
1351 | { | |
1352 | struct SLink **inv, *tmp; | |
1353 | ||
1354 | for (inv = &(chptr->invites); (tmp = *inv); inv = &tmp->next) | |
1355 | if (tmp->value.cptr == cptr) | |
1356 | { | |
1357 | *inv = tmp->next; | |
1358 | free_link(tmp); | |
1359 | tmp = 0; | |
1360 | (cli_user(cptr))->invites--; | |
1361 | break; | |
1362 | } | |
1363 | ||
1364 | for (inv = &((cli_user(cptr))->invited); (tmp = *inv); inv = &tmp->next) | |
1365 | if (tmp->value.chptr == chptr) | |
1366 | { | |
1367 | *inv = tmp->next; | |
1368 | free_link(tmp); | |
1369 | tmp = 0; | |
1370 | break; | |
1371 | } | |
1372 | } | |
1373 | ||
1374 | /** @page zombie Explanation of Zombies | |
1375 | * | |
1376 | * Synopsis: | |
1377 | * | |
1378 | * A channel member is turned into a zombie when he is kicked from a | |
1379 | * channel but his server has not acknowledged the kick. Servers that | |
1380 | * see the member as a zombie can accept actions he performed before | |
1381 | * being kicked, without allowing chanop operations from outsiders or | |
1382 | * desyncing the network. | |
1383 | * | |
1384 | * Consider: | |
1385 | * <pre> | |
1386 | * client | |
1387 | * | | |
1388 | * c | |
1389 | * | | |
1390 | * X --a--> A --b--> B --d--> D | |
1391 | * | | |
1392 | * who | |
1393 | * </pre> | |
1394 | * | |
1395 | * Where `who' is being KICK-ed by a "KICK" message received by server 'A' | |
1396 | * via 'a', or on server 'B' via either 'b' or 'c', or on server D via 'd'. | |
1397 | * | |
1398 | * a) On server A : set CHFL_ZOMBIE for `who' (lp) and pass on the KICK. | |
1399 | * Remove the user immediately when no users are left on the channel. | |
1400 | * b) On server B : remove the user (who/lp) from the channel, send a | |
1401 | * PART upstream (to A) and pass on the KICK. | |
1402 | * c) KICKed by `client'; On server B : remove the user (who/lp) from the | |
1403 | * channel, and pass on the KICK. | |
1404 | * d) On server D : remove the user (who/lp) from the channel, and pass on | |
1405 | * the KICK. | |
1406 | * | |
1407 | * Note: | |
1408 | * - Setting the ZOMBIE flag never hurts, we either remove the | |
1409 | * client after that or we don't. | |
1410 | * - The KICK message was already passed on, as should be in all cases. | |
1411 | * - `who' is removed in all cases except case a) when users are left. | |
1412 | * - A PART is only sent upstream in case b). | |
1413 | * | |
1414 | * 2 aug 97: | |
1415 | * <pre> | |
1416 | * 6 | |
1417 | * | | |
1418 | * 1 --- 2 --- 3 --- 4 --- 5 | |
1419 | * | | | |
1420 | * kicker who | |
1421 | * </pre> | |
1422 | * | |
1423 | * We also need to turn 'who' into a zombie on servers 1 and 6, | |
1424 | * because a KICK from 'who' (kicking someone else in that direction) | |
1425 | * can arrive there afterward - which should not be bounced itself. | |
1426 | * Therefore case a) also applies for servers 1 and 6. | |
1427 | * | |
1428 | * --Run | |
1429 | */ | |
1430 | ||
1431 | /** Turn a user on a channel into a zombie | |
1432 | * This function turns a user into a zombie (see \ref zombie) | |
1433 | * | |
1434 | * @param member The structure representing this user on this channel. | |
1435 | * @param who The client that is being kicked. | |
1436 | * @param cptr The connection the kick came from. | |
1437 | * @param sptr The client that is doing the kicking. | |
1438 | * @param chptr The channel the user is being kicked from. | |
1439 | */ | |
1440 | void make_zombie(struct Membership* member, struct Client* who, | |
1441 | struct Client* cptr, struct Client* sptr, struct Channel* chptr) | |
1442 | { | |
1443 | assert(0 != member); | |
1444 | assert(0 != who); | |
1445 | assert(0 != cptr); | |
1446 | assert(0 != chptr); | |
1447 | ||
1448 | /* Default for case a): */ | |
1449 | SetZombie(member); | |
1450 | ||
1451 | /* Case b) or c) ?: */ | |
1452 | if (MyUser(who)) /* server 4 */ | |
1453 | { | |
1454 | if (IsServer(cptr)) /* Case b) ? */ | |
1455 | sendcmdto_one(who, CMD_PART, cptr, "%H", chptr); | |
1456 | remove_user_from_channel(who, chptr); | |
1457 | return; | |
1458 | } | |
1459 | if (cli_from(who) == cptr) /* True on servers 1, 5 and 6 */ | |
1460 | { | |
1461 | struct Client *acptr = IsServer(sptr) ? sptr : (cli_user(sptr))->server; | |
1462 | for (; acptr != &me; acptr = (cli_serv(acptr))->up) | |
1463 | if (acptr == (cli_user(who))->server) /* Case d) (server 5) */ | |
1464 | { | |
1465 | remove_user_from_channel(who, chptr); | |
1466 | return; | |
1467 | } | |
1468 | } | |
1469 | ||
1470 | /* Case a) (servers 1, 2, 3 and 6) */ | |
1471 | if (channel_all_zombies(chptr)) | |
1472 | remove_user_from_channel(who, chptr); | |
1473 | ||
1474 | /* XXX Can't actually call Debug here; if the channel is all zombies, | |
1475 | * chptr will no longer exist when we get here. | |
1476 | Debug((DEBUG_INFO, "%s is now a zombie on %s", who->name, chptr->chname)); | |
1477 | */ | |
1478 | } | |
1479 | ||
1480 | /** returns the number of zombies on a channel | |
1481 | * @param chptr Channel to count zombies in. | |
1482 | * | |
1483 | * @returns The number of zombies on the channel. | |
1484 | */ | |
1485 | int number_of_zombies(struct Channel *chptr) | |
1486 | { | |
1487 | struct Membership* member; | |
1488 | int count = 0; | |
1489 | ||
1490 | assert(0 != chptr); | |
1491 | for (member = chptr->members; member; member = member->next_member) { | |
1492 | if (IsZombie(member)) | |
1493 | ++count; | |
1494 | } | |
1495 | return count; | |
1496 | } | |
1497 | ||
1498 | /** Concatenate some strings together. | |
1499 | * This helper function builds an argument string in strptr, consisting | |
1500 | * of the original string, a space, and str1 and str2 concatenated (if, | |
1501 | * of course, str2 is not NULL) | |
1502 | * | |
1503 | * @param strptr The buffer to concatenate into | |
1504 | * @param strptr_i modified offset to the position to modify | |
1505 | * @param str1 The string to concatenate from. | |
1506 | * @param str2 The second string to contatenate from. | |
1507 | * @param c Charactor to separate the string from str1 and str2. | |
1508 | */ | |
1509 | static void | |
1510 | build_string(char *strptr, int *strptr_i, const char *str1, | |
1511 | const char *str2, char c) | |
1512 | { | |
1513 | if (c) | |
1514 | strptr[(*strptr_i)++] = c; | |
1515 | ||
1516 | while (*str1) | |
1517 | strptr[(*strptr_i)++] = *(str1++); | |
1518 | ||
1519 | if (str2) | |
1520 | while (*str2) | |
1521 | strptr[(*strptr_i)++] = *(str2++); | |
1522 | ||
1523 | strptr[(*strptr_i)] = '\0'; | |
1524 | } | |
1525 | ||
1526 | /** Flush out the modes | |
1527 | * This is the workhorse of our ModeBuf suite; this actually generates the | |
1528 | * output MODE commands, HACK notices, or whatever. It's pretty complicated. | |
1529 | * | |
1530 | * @param mbuf The mode buffer to flush | |
1531 | * @param all If true, flush all modes, otherwise leave partial modes in the | |
1532 | * buffer. | |
1533 | * | |
1534 | * @returns 0 | |
1535 | */ | |
1536 | static int | |
1537 | modebuf_flush_int(struct ModeBuf *mbuf, int all) | |
1538 | { | |
1539 | /* we only need the flags that don't take args right now */ | |
1540 | static int flags[] = { | |
1541 | /* MODE_CHANOP, 'o', */ | |
1542 | /* MODE_VOICE, 'v', */ | |
1543 | MODE_PRIVATE, 'p', | |
1544 | MODE_SECRET, 's', | |
1545 | MODE_MODERATED, 'm', | |
1546 | MODE_TOPICLIMIT, 't', | |
1547 | MODE_INVITEONLY, 'i', | |
1548 | MODE_NOPRIVMSGS, 'n', | |
1549 | MODE_REGONLY, 'r', | |
1550 | MODE_DELJOINS, 'D', | |
1551 | MODE_WASDELJOINS, 'd', | |
1552 | /* MODE_KEY, 'k', */ | |
1553 | /* MODE_BAN, 'b', */ | |
1554 | MODE_LIMIT, 'l', | |
1555 | /* MODE_APASS, 'A', */ | |
1556 | /* MODE_UPASS, 'U', */ | |
149c0c9f | 1557 | MODE_NOQUITPARTS, 'u', |
189935b1 | 1558 | 0x0, 0x0 |
1559 | }; | |
1560 | int i; | |
1561 | int *flag_p; | |
1562 | ||
1563 | struct Client *app_source; /* where the MODE appears to come from */ | |
1564 | ||
1565 | char addbuf[20]; /* accumulates +psmtin, etc. */ | |
1566 | int addbuf_i = 0; | |
1567 | char rembuf[20]; /* accumulates -psmtin, etc. */ | |
1568 | int rembuf_i = 0; | |
1569 | char *bufptr; /* we make use of indirection to simplify the code */ | |
1570 | int *bufptr_i; | |
1571 | ||
1572 | char addstr[BUFSIZE]; /* accumulates MODE parameters to add */ | |
1573 | int addstr_i; | |
1574 | char remstr[BUFSIZE]; /* accumulates MODE parameters to remove */ | |
1575 | int remstr_i; | |
1576 | char *strptr; /* more indirection to simplify the code */ | |
1577 | int *strptr_i; | |
1578 | ||
1579 | int totalbuflen = BUFSIZE - 200; /* fuzz factor -- don't overrun buffer! */ | |
1580 | int tmp; | |
1581 | ||
1582 | char limitbuf[20]; /* convert limits to strings */ | |
1583 | ||
1584 | unsigned int limitdel = MODE_LIMIT; | |
1585 | ||
1586 | assert(0 != mbuf); | |
1587 | ||
1588 | /* If the ModeBuf is empty, we have nothing to do */ | |
1589 | if (mbuf->mb_add == 0 && mbuf->mb_rem == 0 && mbuf->mb_count == 0) | |
1590 | return 0; | |
1591 | ||
1592 | /* Ok, if we were given the OPMODE flag, or its a server, hide the source. | |
1593 | */ | |
1594 | if (mbuf->mb_dest & MODEBUF_DEST_OPMODE || IsServer(mbuf->mb_source) || IsMe(mbuf->mb_source)) | |
1595 | app_source = &his; | |
1596 | else | |
1597 | app_source = mbuf->mb_source; | |
1598 | ||
1599 | /* | |
1600 | * Account for user we're bouncing; we have to get it in on the first | |
1601 | * bounced MODE, or we could have problems | |
1602 | */ | |
1603 | if (mbuf->mb_dest & MODEBUF_DEST_DEOP) | |
1604 | totalbuflen -= 6; /* numeric nick == 5, plus one space */ | |
1605 | ||
1606 | /* Calculate the simple flags */ | |
1607 | for (flag_p = flags; flag_p[0]; flag_p += 2) { | |
1608 | if (*flag_p & mbuf->mb_add) | |
1609 | addbuf[addbuf_i++] = flag_p[1]; | |
1610 | else if (*flag_p & mbuf->mb_rem) | |
1611 | rembuf[rembuf_i++] = flag_p[1]; | |
1612 | } | |
1613 | ||
1614 | /* Now go through the modes with arguments... */ | |
1615 | for (i = 0; i < mbuf->mb_count; i++) { | |
1616 | if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */ | |
1617 | bufptr = addbuf; | |
1618 | bufptr_i = &addbuf_i; | |
1619 | } else { | |
1620 | bufptr = rembuf; | |
1621 | bufptr_i = &rembuf_i; | |
1622 | } | |
1623 | ||
1624 | if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE)) { | |
1625 | tmp = strlen(cli_name(MB_CLIENT(mbuf, i))); | |
1626 | ||
1627 | if ((totalbuflen - IRCD_MAX(9, tmp)) <= 0) /* don't overflow buffer */ | |
1628 | MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */ | |
1629 | else { | |
1630 | bufptr[(*bufptr_i)++] = MB_TYPE(mbuf, i) & MODE_CHANOP ? 'o' : 'v'; | |
1631 | totalbuflen -= IRCD_MAX(9, tmp) + 1; | |
1632 | } | |
1633 | } else if (MB_TYPE(mbuf, i) & (MODE_BAN | MODE_APASS | MODE_UPASS)) { | |
1634 | tmp = strlen(MB_STRING(mbuf, i)); | |
1635 | ||
1636 | if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */ | |
1637 | MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */ | |
1638 | else { | |
1639 | char mode_char; | |
1640 | switch(MB_TYPE(mbuf, i) & (MODE_BAN | MODE_APASS | MODE_UPASS)) | |
1641 | { | |
1642 | case MODE_APASS: | |
1643 | mode_char = 'A'; | |
1644 | break; | |
1645 | case MODE_UPASS: | |
1646 | mode_char = 'U'; | |
1647 | break; | |
1648 | default: | |
1649 | mode_char = 'b'; | |
1650 | break; | |
1651 | } | |
1652 | bufptr[(*bufptr_i)++] = mode_char; | |
1653 | totalbuflen -= tmp + 1; | |
1654 | } | |
1655 | } else if (MB_TYPE(mbuf, i) & MODE_KEY) { | |
1656 | tmp = (mbuf->mb_dest & MODEBUF_DEST_NOKEY ? 1 : | |
1657 | strlen(MB_STRING(mbuf, i))); | |
1658 | ||
1659 | if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */ | |
1660 | MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */ | |
1661 | else { | |
1662 | bufptr[(*bufptr_i)++] = 'k'; | |
1663 | totalbuflen -= tmp + 1; | |
1664 | } | |
1665 | } else if (MB_TYPE(mbuf, i) & MODE_LIMIT) { | |
1666 | /* if it's a limit, we also format the number */ | |
1667 | ircd_snprintf(0, limitbuf, sizeof(limitbuf), "%u", MB_UINT(mbuf, i)); | |
1668 | ||
1669 | tmp = strlen(limitbuf); | |
1670 | ||
1671 | if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */ | |
1672 | MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */ | |
1673 | else { | |
1674 | bufptr[(*bufptr_i)++] = 'l'; | |
1675 | totalbuflen -= tmp + 1; | |
1676 | } | |
1677 | } | |
1678 | } | |
1679 | ||
1680 | /* terminate the mode strings */ | |
1681 | addbuf[addbuf_i] = '\0'; | |
1682 | rembuf[rembuf_i] = '\0'; | |
1683 | ||
1684 | /* If we're building a user visible MODE or HACK... */ | |
1685 | if (mbuf->mb_dest & (MODEBUF_DEST_CHANNEL | MODEBUF_DEST_HACK2 | | |
1686 | MODEBUF_DEST_HACK3 | MODEBUF_DEST_HACK4 | | |
1687 | MODEBUF_DEST_LOG)) { | |
1688 | /* Set up the parameter strings */ | |
1689 | addstr[0] = '\0'; | |
1690 | addstr_i = 0; | |
1691 | remstr[0] = '\0'; | |
1692 | remstr_i = 0; | |
1693 | ||
1694 | for (i = 0; i < mbuf->mb_count; i++) { | |
1695 | if (MB_TYPE(mbuf, i) & MODE_SAVE) | |
1696 | continue; | |
1697 | ||
1698 | if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */ | |
1699 | strptr = addstr; | |
1700 | strptr_i = &addstr_i; | |
1701 | } else { | |
1702 | strptr = remstr; | |
1703 | strptr_i = &remstr_i; | |
1704 | } | |
1705 | ||
1706 | /* deal with clients... */ | |
1707 | if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE)) | |
1708 | build_string(strptr, strptr_i, cli_name(MB_CLIENT(mbuf, i)), 0, ' '); | |
1709 | ||
1710 | /* deal with bans... */ | |
1711 | else if (MB_TYPE(mbuf, i) & MODE_BAN) | |
1712 | build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' '); | |
1713 | ||
1714 | /* deal with keys... */ | |
1715 | else if (MB_TYPE(mbuf, i) & MODE_KEY) | |
1716 | build_string(strptr, strptr_i, mbuf->mb_dest & MODEBUF_DEST_NOKEY ? | |
1717 | "*" : MB_STRING(mbuf, i), 0, ' '); | |
1718 | ||
1719 | /* deal with invisible passwords */ | |
1720 | else if (MB_TYPE(mbuf, i) & (MODE_APASS | MODE_UPASS)) | |
1721 | build_string(strptr, strptr_i, "*", 0, ' '); | |
1722 | ||
1723 | /* | |
1724 | * deal with limit; note we cannot include the limit parameter if we're | |
1725 | * removing it | |
1726 | */ | |
1727 | else if ((MB_TYPE(mbuf, i) & (MODE_ADD | MODE_LIMIT)) == | |
1728 | (MODE_ADD | MODE_LIMIT)) | |
1729 | build_string(strptr, strptr_i, limitbuf, 0, ' '); | |
1730 | } | |
1731 | ||
1732 | /* send the messages off to their destination */ | |
1733 | if (mbuf->mb_dest & MODEBUF_DEST_HACK2) | |
1734 | sendto_opmask_butone(0, SNO_HACK2, "HACK(2): %s MODE %s %s%s%s%s%s%s " | |
1735 | "[%Tu]", | |
1736 | cli_name(feature_bool(FEAT_HIS_SNOTICES) ? | |
1737 | mbuf->mb_source : app_source), | |
1738 | mbuf->mb_channel->chname, | |
1739 | rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "", | |
1740 | addbuf, remstr, addstr, | |
1741 | mbuf->mb_channel->creationtime); | |
1742 | ||
1743 | if (mbuf->mb_dest & MODEBUF_DEST_HACK3) | |
1744 | sendto_opmask_butone(0, SNO_HACK3, "BOUNCE or HACK(3): %s MODE %s " | |
1745 | "%s%s%s%s%s%s [%Tu]", | |
1746 | cli_name(feature_bool(FEAT_HIS_SNOTICES) ? | |
1747 | mbuf->mb_source : app_source), | |
1748 | mbuf->mb_channel->chname, rembuf_i ? "-" : "", | |
1749 | rembuf, addbuf_i ? "+" : "", addbuf, remstr, addstr, | |
1750 | mbuf->mb_channel->creationtime); | |
1751 | ||
1752 | if (mbuf->mb_dest & MODEBUF_DEST_HACK4) | |
1753 | sendto_opmask_butone(0, SNO_HACK4, "HACK(4): %s MODE %s %s%s%s%s%s%s " | |
1754 | "[%Tu]", | |
1755 | cli_name(feature_bool(FEAT_HIS_SNOTICES) ? | |
1756 | mbuf->mb_source : app_source), | |
1757 | mbuf->mb_channel->chname, | |
1758 | rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "", | |
1759 | addbuf, remstr, addstr, | |
1760 | mbuf->mb_channel->creationtime); | |
1761 | ||
1762 | if (mbuf->mb_dest & MODEBUF_DEST_LOG) | |
1763 | log_write(LS_OPERMODE, L_INFO, LOG_NOSNOTICE, | |
1764 | "%#C OPMODE %H %s%s%s%s%s%s", mbuf->mb_source, | |
1765 | mbuf->mb_channel, rembuf_i ? "-" : "", rembuf, | |
1766 | addbuf_i ? "+" : "", addbuf, remstr, addstr); | |
1767 | ||
1768 | if (mbuf->mb_dest & MODEBUF_DEST_CHANNEL) | |
1769 | sendcmdto_channel_butserv_butone(app_source, CMD_MODE, mbuf->mb_channel, NULL, 0, | |
1770 | "%H %s%s%s%s%s%s", mbuf->mb_channel, | |
1771 | rembuf_i ? "-" : "", rembuf, | |
1772 | addbuf_i ? "+" : "", addbuf, remstr, addstr); | |
1773 | } | |
1774 | ||
1775 | /* Now are we supposed to propagate to other servers? */ | |
1776 | if (mbuf->mb_dest & MODEBUF_DEST_SERVER) { | |
1777 | /* set up parameter string */ | |
1778 | addstr[0] = '\0'; | |
1779 | addstr_i = 0; | |
1780 | remstr[0] = '\0'; | |
1781 | remstr_i = 0; | |
1782 | ||
1783 | /* | |
1784 | * limit is supressed if we're removing it; we have to figure out which | |
1785 | * direction is the direction for it to be removed, though... | |
1786 | */ | |
1787 | limitdel |= (mbuf->mb_dest & MODEBUF_DEST_HACK2) ? MODE_DEL : MODE_ADD; | |
1788 | ||
1789 | for (i = 0; i < mbuf->mb_count; i++) { | |
1790 | if (MB_TYPE(mbuf, i) & MODE_SAVE) | |
1791 | continue; | |
1792 | ||
1793 | if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */ | |
1794 | strptr = addstr; | |
1795 | strptr_i = &addstr_i; | |
1796 | } else { | |
1797 | strptr = remstr; | |
1798 | strptr_i = &remstr_i; | |
1799 | } | |
1800 | ||
1801 | /* if we're changing oplevels we know the oplevel, pass it on */ | |
1802 | if (mbuf->mb_channel->mode.apass[0] | |
1803 | && (MB_TYPE(mbuf, i) & MODE_CHANOP) | |
1804 | && MB_OPLEVEL(mbuf, i) < MAXOPLEVEL) | |
1805 | *strptr_i += ircd_snprintf(0, strptr + *strptr_i, BUFSIZE - *strptr_i, | |
1806 | " %s%s:%d", | |
1807 | NumNick(MB_CLIENT(mbuf, i)), | |
1808 | MB_OPLEVEL(mbuf, i)); | |
1809 | ||
1810 | /* deal with other modes that take clients */ | |
1811 | else if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE)) | |
1812 | build_string(strptr, strptr_i, NumNick(MB_CLIENT(mbuf, i)), ' '); | |
1813 | ||
1814 | /* deal with modes that take strings */ | |
1815 | else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN | MODE_APASS | MODE_UPASS)) | |
1816 | build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' '); | |
1817 | ||
1818 | /* | |
1819 | * deal with the limit. Logic here is complicated; if HACK2 is set, | |
1820 | * we're bouncing the mode, so sense is reversed, and we have to | |
1821 | * include the original limit if it looks like it's being removed | |
1822 | */ | |
1823 | else if ((MB_TYPE(mbuf, i) & limitdel) == limitdel) | |
1824 | build_string(strptr, strptr_i, limitbuf, 0, ' '); | |
1825 | } | |
1826 | ||
1827 | /* we were told to deop the source */ | |
1828 | if (mbuf->mb_dest & MODEBUF_DEST_DEOP) { | |
1829 | addbuf[addbuf_i++] = 'o'; /* remember, sense is reversed */ | |
1830 | addbuf[addbuf_i] = '\0'; /* terminate the string... */ | |
1831 | build_string(addstr, &addstr_i, NumNick(mbuf->mb_source), ' '); | |
1832 | ||
1833 | /* mark that we've done this, so we don't do it again */ | |
1834 | mbuf->mb_dest &= ~MODEBUF_DEST_DEOP; | |
1835 | } | |
1836 | ||
1837 | if (mbuf->mb_dest & MODEBUF_DEST_OPMODE) { | |
1838 | /* If OPMODE was set, we're propagating the mode as an OPMODE message */ | |
1839 | sendcmdto_serv_butone(mbuf->mb_source, CMD_OPMODE, mbuf->mb_connect, | |
1840 | "%H %s%s%s%s%s%s", mbuf->mb_channel, | |
1841 | rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "", | |
1842 | addbuf, remstr, addstr); | |
1843 | } else if (mbuf->mb_dest & MODEBUF_DEST_BOUNCE) { | |
1844 | /* | |
1845 | * If HACK2 was set, we're bouncing; we send the MODE back to the | |
1846 | * connection we got it from with the senses reversed and a TS of 0; | |
1847 | * origin is us | |
1848 | */ | |
1849 | sendcmdto_one(&me, CMD_MODE, mbuf->mb_connect, "%H %s%s%s%s%s%s %Tu", | |
1850 | mbuf->mb_channel, addbuf_i ? "-" : "", addbuf, | |
1851 | rembuf_i ? "+" : "", rembuf, addstr, remstr, | |
1852 | mbuf->mb_channel->creationtime); | |
1853 | } else { | |
1854 | /* | |
1855 | * We're propagating a normal MODE command to the rest of the network; | |
1856 | * we send the actual channel TS unless this is a HACK3 or a HACK4 | |
1857 | */ | |
1858 | if (IsServer(mbuf->mb_source)) | |
1859 | sendcmdto_serv_butone(mbuf->mb_source, CMD_MODE, mbuf->mb_connect, | |
1860 | "%H %s%s%s%s%s%s %Tu", mbuf->mb_channel, | |
1861 | rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "", | |
1862 | addbuf, remstr, addstr, | |
1863 | (mbuf->mb_dest & MODEBUF_DEST_HACK4) ? 0 : | |
1864 | mbuf->mb_channel->creationtime); | |
1865 | else | |
1866 | sendcmdto_serv_butone(mbuf->mb_source, CMD_MODE, mbuf->mb_connect, | |
1867 | "%H %s%s%s%s%s%s", mbuf->mb_channel, | |
1868 | rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "", | |
1869 | addbuf, remstr, addstr); | |
1870 | } | |
1871 | } | |
1872 | ||
1873 | /* We've drained the ModeBuf... */ | |
1874 | mbuf->mb_add = 0; | |
1875 | mbuf->mb_rem = 0; | |
1876 | mbuf->mb_count = 0; | |
1877 | ||
1878 | /* reinitialize the mode-with-arg slots */ | |
1879 | for (i = 0; i < MAXMODEPARAMS; i++) { | |
1880 | /* If we saved any, pack them down */ | |
1881 | if (MB_TYPE(mbuf, i) & MODE_SAVE) { | |
1882 | mbuf->mb_modeargs[mbuf->mb_count] = mbuf->mb_modeargs[i]; | |
1883 | MB_TYPE(mbuf, mbuf->mb_count) &= ~MODE_SAVE; /* don't save anymore */ | |
1884 | ||
1885 | if (mbuf->mb_count++ == i) /* don't overwrite our hard work */ | |
1886 | continue; | |
1887 | } else if (MB_TYPE(mbuf, i) & MODE_FREE) | |
1888 | MyFree(MB_STRING(mbuf, i)); /* free string if needed */ | |
1889 | ||
1890 | MB_TYPE(mbuf, i) = 0; | |
1891 | MB_UINT(mbuf, i) = 0; | |
1892 | } | |
1893 | ||
1894 | /* If we're supposed to flush it all, do so--all hail tail recursion */ | |
1895 | if (all && mbuf->mb_count) | |
1896 | return modebuf_flush_int(mbuf, 1); | |
1897 | ||
1898 | return 0; | |
1899 | } | |
1900 | ||
1901 | /** Initialise a modebuf | |
1902 | * This routine just initializes a ModeBuf structure with the information | |
1903 | * needed and the options given. | |
1904 | * | |
1905 | * @param mbuf The mode buffer to initialise. | |
1906 | * @param source The client that is performing the mode. | |
1907 | * @param connect ? | |
1908 | * @param chan The channel that the mode is being performed upon. | |
1909 | * @param dest ? | |
1910 | */ | |
1911 | void | |
1912 | modebuf_init(struct ModeBuf *mbuf, struct Client *source, | |
1913 | struct Client *connect, struct Channel *chan, unsigned int dest) | |
1914 | { | |
1915 | int i; | |
1916 | ||
1917 | assert(0 != mbuf); | |
1918 | assert(0 != source); | |
1919 | assert(0 != chan); | |
1920 | assert(0 != dest); | |
1921 | ||
1922 | if (IsLocalChannel(chan->chname)) dest &= ~MODEBUF_DEST_SERVER; | |
1923 | ||
1924 | mbuf->mb_add = 0; | |
1925 | mbuf->mb_rem = 0; | |
1926 | mbuf->mb_source = source; | |
1927 | mbuf->mb_connect = connect; | |
1928 | mbuf->mb_channel = chan; | |
1929 | mbuf->mb_dest = dest; | |
1930 | mbuf->mb_count = 0; | |
1931 | ||
1932 | /* clear each mode-with-parameter slot */ | |
1933 | for (i = 0; i < MAXMODEPARAMS; i++) { | |
1934 | MB_TYPE(mbuf, i) = 0; | |
1935 | MB_UINT(mbuf, i) = 0; | |
1936 | } | |
1937 | } | |
1938 | ||
1939 | /** Append a new mode to a modebuf | |
1940 | * This routine simply adds modes to be added or deleted; do a binary OR | |
1941 | * with either MODE_ADD or MODE_DEL | |
1942 | * | |
1943 | * @param mbuf Mode buffer | |
1944 | * @param mode MODE_ADD or MODE_DEL OR'd with MODE_PRIVATE etc. | |
1945 | */ | |
1946 | void | |
1947 | modebuf_mode(struct ModeBuf *mbuf, unsigned int mode) | |
1948 | { | |
1949 | assert(0 != mbuf); | |
1950 | assert(0 != (mode & (MODE_ADD | MODE_DEL))); | |
1951 | ||
1952 | mode &= (MODE_ADD | MODE_DEL | MODE_PRIVATE | MODE_SECRET | MODE_MODERATED | | |
1953 | MODE_TOPICLIMIT | MODE_INVITEONLY | MODE_NOPRIVMSGS | MODE_REGONLY | | |
149c0c9f | 1954 | MODE_DELJOINS | MODE_WASDELJOINS | MODE_NOQUITPARTS); |
189935b1 | 1955 | |
1956 | if (!(mode & ~(MODE_ADD | MODE_DEL))) /* don't add empty modes... */ | |
1957 | return; | |
1958 | ||
1959 | if (mode & MODE_ADD) { | |
1960 | mbuf->mb_rem &= ~mode; | |
1961 | mbuf->mb_add |= mode; | |
1962 | } else { | |
1963 | mbuf->mb_add &= ~mode; | |
1964 | mbuf->mb_rem |= mode; | |
1965 | } | |
1966 | } | |
1967 | ||
1968 | /** Append a mode that takes an int argument to the modebuf | |
1969 | * | |
1970 | * This routine adds a mode to be added or deleted that takes a unsigned | |
1971 | * int parameter; mode may *only* be the relevant mode flag ORed with one | |
1972 | * of MODE_ADD or MODE_DEL | |
1973 | * | |
1974 | * @param mbuf The mode buffer to append to. | |
1975 | * @param mode The mode to append. | |
1976 | * @param uint The argument to the mode. | |
1977 | */ | |
1978 | void | |
1979 | modebuf_mode_uint(struct ModeBuf *mbuf, unsigned int mode, unsigned int uint) | |
1980 | { | |
1981 | assert(0 != mbuf); | |
1982 | assert(0 != (mode & (MODE_ADD | MODE_DEL))); | |
1983 | ||
1984 | if (mode == (MODE_LIMIT | MODE_DEL)) { | |
1985 | mbuf->mb_rem |= mode; | |
1986 | return; | |
1987 | } | |
1988 | MB_TYPE(mbuf, mbuf->mb_count) = mode; | |
1989 | MB_UINT(mbuf, mbuf->mb_count) = uint; | |
1990 | ||
1991 | /* when we've reached the maximal count, flush the buffer */ | |
1992 | if (++mbuf->mb_count >= | |
1993 | (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0))) | |
1994 | modebuf_flush_int(mbuf, 0); | |
1995 | } | |
1996 | ||
1997 | /** append a string mode | |
1998 | * This routine adds a mode to be added or deleted that takes a string | |
1999 | * parameter; mode may *only* be the relevant mode flag ORed with one of | |
2000 | * MODE_ADD or MODE_DEL | |
2001 | * | |
2002 | * @param mbuf The mode buffer to append to. | |
2003 | * @param mode The mode to append. | |
2004 | * @param string The string parameter to append. | |
2005 | * @param free If the string should be free'd later. | |
2006 | */ | |
2007 | void | |
2008 | modebuf_mode_string(struct ModeBuf *mbuf, unsigned int mode, char *string, | |
2009 | int free) | |
2010 | { | |
2011 | assert(0 != mbuf); | |
2012 | assert(0 != (mode & (MODE_ADD | MODE_DEL))); | |
2013 | ||
2014 | MB_TYPE(mbuf, mbuf->mb_count) = mode | (free ? MODE_FREE : 0); | |
2015 | MB_STRING(mbuf, mbuf->mb_count) = string; | |
2016 | ||
2017 | /* when we've reached the maximal count, flush the buffer */ | |
2018 | if (++mbuf->mb_count >= | |
2019 | (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0))) | |
2020 | modebuf_flush_int(mbuf, 0); | |
2021 | } | |
2022 | ||
2023 | /** Append a mode on a client to a modebuf. | |
2024 | * This routine adds a mode to be added or deleted that takes a client | |
2025 | * parameter; mode may *only* be the relevant mode flag ORed with one of | |
2026 | * MODE_ADD or MODE_DEL | |
2027 | * | |
2028 | * @param mbuf The modebuf to append the mode to. | |
2029 | * @param mode The mode to append. | |
2030 | * @param client The client argument to append. | |
2031 | * @param oplevel The oplevel the user had or will have | |
2032 | */ | |
2033 | void | |
2034 | modebuf_mode_client(struct ModeBuf *mbuf, unsigned int mode, | |
2035 | struct Client *client, int oplevel) | |
2036 | { | |
2037 | assert(0 != mbuf); | |
2038 | assert(0 != (mode & (MODE_ADD | MODE_DEL))); | |
2039 | ||
2040 | MB_TYPE(mbuf, mbuf->mb_count) = mode; | |
2041 | MB_CLIENT(mbuf, mbuf->mb_count) = client; | |
2042 | MB_OPLEVEL(mbuf, mbuf->mb_count) = oplevel; | |
2043 | ||
2044 | /* when we've reached the maximal count, flush the buffer */ | |
2045 | if (++mbuf->mb_count >= | |
2046 | (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0))) | |
2047 | modebuf_flush_int(mbuf, 0); | |
2048 | } | |
2049 | ||
2050 | /** The exported binding for modebuf_flush() | |
2051 | * | |
2052 | * @param mbuf The mode buffer to flush. | |
2053 | * | |
2054 | * @see modebuf_flush_int() | |
2055 | */ | |
2056 | int | |
2057 | modebuf_flush(struct ModeBuf *mbuf) | |
2058 | { | |
2059 | struct Membership *memb; | |
2060 | ||
2061 | /* Check if MODE_WASDELJOINS should be set */ | |
2062 | if (!(mbuf->mb_channel->mode.mode & (MODE_DELJOINS | MODE_WASDELJOINS)) | |
2063 | && (mbuf->mb_rem & MODE_DELJOINS)) { | |
2064 | for (memb = mbuf->mb_channel->members; memb; memb = memb->next_member) { | |
2065 | if (IsDelayedJoin(memb)) { | |
2066 | mbuf->mb_channel->mode.mode |= MODE_WASDELJOINS; | |
2067 | mbuf->mb_add |= MODE_WASDELJOINS; | |
2068 | mbuf->mb_rem &= ~MODE_WASDELJOINS; | |
2069 | break; | |
2070 | } | |
2071 | } | |
2072 | } | |
2073 | ||
2074 | return modebuf_flush_int(mbuf, 1); | |
2075 | } | |
2076 | ||
2077 | /* This extracts the simple modes contained in mbuf | |
2078 | * | |
2079 | * @param mbuf The mode buffer to extract the modes from. | |
2080 | * @param buf The string buffer to write the modes into. | |
2081 | */ | |
2082 | void | |
2083 | modebuf_extract(struct ModeBuf *mbuf, char *buf) | |
2084 | { | |
2085 | static int flags[] = { | |
2086 | /* MODE_CHANOP, 'o', */ | |
2087 | /* MODE_VOICE, 'v', */ | |
2088 | MODE_PRIVATE, 'p', | |
2089 | MODE_SECRET, 's', | |
2090 | MODE_MODERATED, 'm', | |
2091 | MODE_TOPICLIMIT, 't', | |
2092 | MODE_INVITEONLY, 'i', | |
2093 | MODE_NOPRIVMSGS, 'n', | |
2094 | MODE_KEY, 'k', | |
2095 | MODE_APASS, 'A', | |
2096 | MODE_UPASS, 'U', | |
2097 | /* MODE_BAN, 'b', */ | |
2098 | MODE_LIMIT, 'l', | |
2099 | MODE_REGONLY, 'r', | |
2100 | MODE_DELJOINS, 'D', | |
149c0c9f | 2101 | MODE_NOQUITPARTS, 'u', |
189935b1 | 2102 | 0x0, 0x0 |
2103 | }; | |
2104 | unsigned int add; | |
2105 | int i, bufpos = 0, len; | |
2106 | int *flag_p; | |
2107 | char *key = 0, limitbuf[20]; | |
2108 | char *apass = 0, *upass = 0; | |
2109 | ||
2110 | assert(0 != mbuf); | |
2111 | assert(0 != buf); | |
2112 | ||
2113 | buf[0] = '\0'; | |
2114 | ||
2115 | add = mbuf->mb_add; | |
2116 | ||
2117 | for (i = 0; i < mbuf->mb_count; i++) { /* find keys and limits */ | |
2118 | if (MB_TYPE(mbuf, i) & MODE_ADD) { | |
2119 | add |= MB_TYPE(mbuf, i) & (MODE_KEY | MODE_LIMIT | MODE_APASS | MODE_UPASS); | |
2120 | ||
2121 | if (MB_TYPE(mbuf, i) & MODE_KEY) /* keep strings */ | |
2122 | key = MB_STRING(mbuf, i); | |
2123 | else if (MB_TYPE(mbuf, i) & MODE_LIMIT) | |
2124 | ircd_snprintf(0, limitbuf, sizeof(limitbuf), "%u", MB_UINT(mbuf, i)); | |
2125 | else if (MB_TYPE(mbuf, i) & MODE_UPASS) | |
2126 | upass = MB_STRING(mbuf, i); | |
2127 | else if (MB_TYPE(mbuf, i) & MODE_APASS) | |
2128 | apass = MB_STRING(mbuf, i); | |
2129 | } | |
2130 | } | |
2131 | ||
2132 | if (!add) | |
2133 | return; | |
2134 | ||
2135 | buf[bufpos++] = '+'; /* start building buffer */ | |
2136 | ||
2137 | for (flag_p = flags; flag_p[0]; flag_p += 2) | |
2138 | if (*flag_p & add) | |
2139 | buf[bufpos++] = flag_p[1]; | |
2140 | ||
2141 | for (i = 0, len = bufpos; i < len; i++) { | |
2142 | if (buf[i] == 'k') | |
2143 | build_string(buf, &bufpos, key, 0, ' '); | |
2144 | else if (buf[i] == 'l') | |
2145 | build_string(buf, &bufpos, limitbuf, 0, ' '); | |
2146 | else if (buf[i] == 'U') | |
2147 | build_string(buf, &bufpos, upass, 0, ' '); | |
2148 | else if (buf[i] == 'A') | |
2149 | build_string(buf, &bufpos, apass, 0, ' '); | |
2150 | } | |
2151 | ||
2152 | buf[bufpos] = '\0'; | |
2153 | ||
2154 | return; | |
2155 | } | |
2156 | ||
2157 | /** Simple function to invalidate bans | |
2158 | * | |
2159 | * This function sets all bans as being valid. | |
2160 | * | |
2161 | * @param chan The channel to operate on. | |
2162 | */ | |
2163 | void | |
2164 | mode_ban_invalidate(struct Channel *chan) | |
2165 | { | |
2166 | struct Membership *member; | |
2167 | ||
2168 | for (member = chan->members; member; member = member->next_member) | |
2169 | ClearBanValid(member); | |
2170 | } | |
2171 | ||
2172 | /** Simple function to drop invite structures | |
2173 | * | |
2174 | * Remove all the invites on the channel. | |
2175 | * | |
2176 | * @param chan Channel to remove invites from. | |
2177 | * | |
2178 | */ | |
2179 | void | |
2180 | mode_invite_clear(struct Channel *chan) | |
2181 | { | |
2182 | while (chan->invites) | |
2183 | del_invite(chan->invites->value.cptr, chan); | |
2184 | } | |
2185 | ||
2186 | /* What we've done for mode_parse so far... */ | |
2187 | #define DONE_LIMIT 0x01 /**< We've set the limit */ | |
2188 | #define DONE_KEY 0x02 /**< We've set the key */ | |
2189 | #define DONE_BANLIST 0x04 /**< We've sent the ban list */ | |
2190 | #define DONE_NOTOPER 0x08 /**< We've sent a "Not oper" error */ | |
2191 | #define DONE_BANCLEAN 0x10 /**< We've cleaned bans... */ | |
2192 | #define DONE_UPASS 0x20 /**< We've set user pass */ | |
2193 | #define DONE_APASS 0x40 /**< We've set admin pass */ | |
2194 | ||
2195 | struct ParseState { | |
2196 | struct ModeBuf *mbuf; | |
2197 | struct Client *cptr; | |
2198 | struct Client *sptr; | |
2199 | struct Channel *chptr; | |
2200 | struct Membership *member; | |
2201 | int parc; | |
2202 | char **parv; | |
2203 | unsigned int flags; | |
2204 | unsigned int dir; | |
2205 | unsigned int done; | |
2206 | unsigned int add; | |
2207 | unsigned int del; | |
2208 | int args_used; | |
2209 | int max_args; | |
2210 | int numbans; | |
2211 | struct Ban banlist[MAXPARA]; | |
2212 | struct { | |
2213 | unsigned int flag; | |
2214 | unsigned short oplevel; | |
2215 | struct Client *client; | |
2216 | } cli_change[MAXPARA]; | |
2217 | }; | |
2218 | ||
2219 | /** Helper function to send "Not oper" or "Not member" messages | |
2220 | * Here's a helper function to deal with sending along "Not oper" or | |
2221 | * "Not member" messages | |
2222 | * | |
2223 | * @param state Parsing State object | |
2224 | */ | |
2225 | static void | |
2226 | send_notoper(struct ParseState *state) | |
2227 | { | |
2228 | if (state->done & DONE_NOTOPER) | |
2229 | return; | |
2230 | ||
2231 | send_reply(state->sptr, (state->flags & MODE_PARSE_NOTOPER) ? | |
2232 | ERR_CHANOPRIVSNEEDED : ERR_NOTONCHANNEL, state->chptr->chname); | |
2233 | ||
2234 | state->done |= DONE_NOTOPER; | |
2235 | } | |
2236 | ||
2237 | /** Parse a limit | |
2238 | * Helper function to convert limits | |
2239 | * | |
2240 | * @param state Parsing state object. | |
2241 | * @param flag_p ? | |
2242 | */ | |
2243 | static void | |
2244 | mode_parse_limit(struct ParseState *state, int *flag_p) | |
2245 | { | |
2246 | unsigned int t_limit; | |
2247 | ||
2248 | if (state->dir == MODE_ADD) { /* convert arg only if adding limit */ | |
2249 | if (MyUser(state->sptr) && state->max_args <= 0) /* too many args? */ | |
2250 | return; | |
2251 | ||
2252 | if (state->parc <= 0) { /* warn if not enough args */ | |
2253 | if (MyUser(state->sptr)) | |
2254 | need_more_params(state->sptr, "MODE +l"); | |
2255 | return; | |
2256 | } | |
2257 | ||
2258 | t_limit = strtoul(state->parv[state->args_used++], 0, 10); /* grab arg */ | |
2259 | state->parc--; | |
2260 | state->max_args--; | |
2261 | ||
2262 | if ((int)t_limit<0) /* don't permit a negative limit */ | |
2263 | return; | |
2264 | ||
2265 | if (!(state->flags & MODE_PARSE_WIPEOUT) && | |
2266 | (!t_limit || t_limit == state->chptr->mode.limit)) | |
2267 | return; | |
2268 | } else | |
2269 | t_limit = state->chptr->mode.limit; | |
2270 | ||
2271 | /* If they're not an oper, they can't change modes */ | |
2272 | if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) { | |
2273 | send_notoper(state); | |
2274 | return; | |
2275 | } | |
2276 | ||
2277 | /* Can't remove a limit that's not there */ | |
2278 | if (state->dir == MODE_DEL && !state->chptr->mode.limit) | |
2279 | return; | |
2280 | ||
2281 | /* Skip if this is a burst and a lower limit than this is set already */ | |
2282 | if ((state->flags & MODE_PARSE_BURST) && | |
2283 | (state->chptr->mode.mode & flag_p[0]) && | |
2284 | (state->chptr->mode.limit < t_limit)) | |
2285 | return; | |
2286 | ||
2287 | if (state->done & DONE_LIMIT) /* allow limit to be set only once */ | |
2288 | return; | |
2289 | state->done |= DONE_LIMIT; | |
2290 | ||
2291 | if (!state->mbuf) | |
2292 | return; | |
2293 | ||
2294 | modebuf_mode_uint(state->mbuf, state->dir | flag_p[0], t_limit); | |
2295 | ||
2296 | if (state->flags & MODE_PARSE_SET) { /* set the limit */ | |
2297 | if (state->dir & MODE_ADD) { | |
2298 | state->chptr->mode.mode |= flag_p[0]; | |
2299 | state->chptr->mode.limit = t_limit; | |
2300 | } else { | |
2301 | state->chptr->mode.mode &= ~flag_p[0]; | |
2302 | state->chptr->mode.limit = 0; | |
2303 | } | |
2304 | } | |
2305 | } | |
2306 | ||
2307 | /** Helper function to clean key-like parameters. */ | |
2308 | static void | |
2309 | clean_key(char *s) | |
2310 | { | |
2311 | int t_len = KEYLEN; | |
2312 | ||
2313 | while (*s > ' ' && *s != ':' && *s != ',' && t_len--) | |
2314 | s++; | |
2315 | *s = '\0'; | |
2316 | } | |
2317 | ||
2318 | /* | |
2319 | * Helper function to convert keys | |
2320 | */ | |
2321 | static void | |
2322 | mode_parse_key(struct ParseState *state, int *flag_p) | |
2323 | { | |
2324 | char *t_str; | |
2325 | ||
2326 | if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */ | |
2327 | return; | |
2328 | ||
2329 | if (state->parc <= 0) { /* warn if not enough args */ | |
2330 | if (MyUser(state->sptr)) | |
2331 | need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" : | |
2332 | "MODE -k"); | |
2333 | return; | |
2334 | } | |
2335 | ||
2336 | t_str = state->parv[state->args_used++]; /* grab arg */ | |
2337 | state->parc--; | |
2338 | state->max_args--; | |
2339 | ||
2340 | /* If they're not an oper, they can't change modes */ | |
2341 | if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) { | |
2342 | send_notoper(state); | |
2343 | return; | |
2344 | } | |
2345 | ||
2346 | if (state->done & DONE_KEY) /* allow key to be set only once */ | |
2347 | return; | |
2348 | state->done |= DONE_KEY; | |
2349 | ||
2350 | /* clean up the key string */ | |
2351 | clean_key(t_str); | |
2352 | if (!*t_str || *t_str == ':') { /* warn if empty */ | |
2353 | if (MyUser(state->sptr)) | |
2354 | need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" : | |
2355 | "MODE -k"); | |
2356 | return; | |
2357 | } | |
2358 | ||
2359 | if (!state->mbuf) | |
2360 | return; | |
2361 | ||
2362 | /* Skip if this is a burst, we have a key already and the new key is | |
2363 | * after the old one alphabetically */ | |
2364 | if ((state->flags & MODE_PARSE_BURST) && | |
2365 | *(state->chptr->mode.key) && | |
2366 | ircd_strcmp(state->chptr->mode.key, t_str) <= 0) | |
2367 | return; | |
2368 | ||
2369 | /* can't add a key if one is set, nor can one remove the wrong key */ | |
2370 | if (!(state->flags & MODE_PARSE_FORCE)) | |
2371 | if ((state->dir == MODE_ADD && *state->chptr->mode.key) || | |
2372 | (state->dir == MODE_DEL && | |
2373 | ircd_strcmp(state->chptr->mode.key, t_str))) { | |
2374 | send_reply(state->sptr, ERR_KEYSET, state->chptr->chname); | |
2375 | return; | |
2376 | } | |
2377 | ||
2378 | if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD && | |
2379 | !ircd_strcmp(state->chptr->mode.key, t_str)) | |
2380 | return; /* no key change */ | |
2381 | ||
2382 | if (state->flags & MODE_PARSE_BOUNCE) { | |
2383 | if (*state->chptr->mode.key) /* reset old key */ | |
2384 | modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0], | |
2385 | state->chptr->mode.key, 0); | |
2386 | else /* remove new bogus key */ | |
2387 | modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0); | |
2388 | } else /* send new key */ | |
2389 | modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0); | |
2390 | ||
2391 | if (state->flags & MODE_PARSE_SET) { | |
2392 | if (state->dir == MODE_DEL) /* remove the old key */ | |
2393 | *state->chptr->mode.key = '\0'; | |
2394 | else if (!state->chptr->mode.key[0] | |
2395 | || ircd_strcmp(t_str, state->chptr->mode.key) < 0) | |
2396 | ircd_strncpy(state->chptr->mode.key, t_str, KEYLEN); | |
2397 | } | |
2398 | } | |
2399 | ||
2400 | /* | |
2401 | * Helper function to convert user passes | |
2402 | */ | |
2403 | static void | |
2404 | mode_parse_upass(struct ParseState *state, int *flag_p) | |
2405 | { | |
2406 | char *t_str; | |
2407 | ||
2408 | if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */ | |
2409 | return; | |
2410 | ||
2411 | if (state->parc <= 0) { /* warn if not enough args */ | |
2412 | if (MyUser(state->sptr)) | |
2413 | need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +U" : | |
2414 | "MODE -U"); | |
2415 | return; | |
2416 | } | |
2417 | ||
2418 | t_str = state->parv[state->args_used++]; /* grab arg */ | |
2419 | state->parc--; | |
2420 | state->max_args--; | |
2421 | ||
2422 | /* If they're not an oper, they can't change modes */ | |
2423 | if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) { | |
2424 | send_notoper(state); | |
2425 | return; | |
2426 | } | |
2427 | ||
2428 | /* If a non-service user is trying to force it, refuse. */ | |
2429 | if (state->flags & MODE_PARSE_FORCE && MyUser(state->sptr) | |
2430 | && !HasPriv(state->sptr, PRIV_APASS_OPMODE)) { | |
2431 | send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname, | |
2432 | state->chptr->chname); | |
2433 | return; | |
2434 | } | |
2435 | ||
2436 | /* If they are not the channel manager, they are not allowed to change it */ | |
2437 | if (MyUser(state->sptr) && !(state->flags & MODE_PARSE_FORCE || IsChannelManager(state->member))) { | |
2438 | if (*state->chptr->mode.apass) { | |
2439 | send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname, | |
2440 | state->chptr->chname); | |
2441 | } else { | |
2442 | send_reply(state->sptr, ERR_NOMANAGER, state->chptr->chname); | |
2443 | } | |
2444 | return; | |
2445 | } | |
2446 | ||
2447 | if (state->done & DONE_UPASS) /* allow upass to be set only once */ | |
2448 | return; | |
2449 | state->done |= DONE_UPASS; | |
2450 | ||
2451 | /* clean up the upass string */ | |
2452 | clean_key(t_str); | |
2453 | if (!*t_str || *t_str == ':') { /* warn if empty */ | |
2454 | if (MyUser(state->sptr)) | |
2455 | need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +U" : | |
2456 | "MODE -U"); | |
2457 | return; | |
2458 | } | |
2459 | ||
2460 | if (!state->mbuf) | |
2461 | return; | |
2462 | ||
2463 | if (!(state->flags & MODE_PARSE_FORCE)) { | |
2464 | /* can't add the upass while apass is not set */ | |
2465 | if (state->dir == MODE_ADD && !*state->chptr->mode.apass) { | |
2466 | send_reply(state->sptr, ERR_UPASSNOTSET, state->chptr->chname, state->chptr->chname); | |
2467 | return; | |
2468 | } | |
2469 | /* cannot set a +U password that is the same as +A */ | |
2470 | if (state->dir == MODE_ADD && !ircd_strcmp(state->chptr->mode.apass, t_str)) { | |
2471 | send_reply(state->sptr, ERR_UPASS_SAME_APASS, state->chptr->chname); | |
2472 | return; | |
2473 | } | |
2474 | /* can't add a upass if one is set, nor can one remove the wrong upass */ | |
2475 | if ((state->dir == MODE_ADD && *state->chptr->mode.upass) || | |
2476 | (state->dir == MODE_DEL && | |
2477 | ircd_strcmp(state->chptr->mode.upass, t_str))) { | |
2478 | send_reply(state->sptr, ERR_KEYSET, state->chptr->chname); | |
2479 | return; | |
2480 | } | |
2481 | } | |
2482 | ||
2483 | if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD && | |
2484 | !ircd_strcmp(state->chptr->mode.upass, t_str)) | |
2485 | return; /* no upass change */ | |
2486 | ||
2487 | if (state->flags & MODE_PARSE_BOUNCE) { | |
2488 | if (*state->chptr->mode.upass) /* reset old upass */ | |
2489 | modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0], | |
2490 | state->chptr->mode.upass, 0); | |
2491 | else /* remove new bogus upass */ | |
2492 | modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0); | |
2493 | } else /* send new upass */ | |
2494 | modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0); | |
2495 | ||
2496 | if (state->flags & MODE_PARSE_SET) { | |
2497 | if (state->dir == MODE_DEL) /* remove the old upass */ | |
2498 | *state->chptr->mode.upass = '\0'; | |
2499 | else if (state->chptr->mode.upass[0] == '\0' | |
2500 | || ircd_strcmp(t_str, state->chptr->mode.upass) < 0) | |
2501 | ircd_strncpy(state->chptr->mode.upass, t_str, KEYLEN); | |
2502 | } | |
2503 | } | |
2504 | ||
2505 | /* | |
2506 | * Helper function to convert admin passes | |
2507 | */ | |
2508 | static void | |
2509 | mode_parse_apass(struct ParseState *state, int *flag_p) | |
2510 | { | |
2511 | struct Membership *memb; | |
2512 | char *t_str; | |
2513 | ||
2514 | if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */ | |
2515 | return; | |
2516 | ||
2517 | if (state->parc <= 0) { /* warn if not enough args */ | |
2518 | if (MyUser(state->sptr)) | |
2519 | need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +A" : | |
2520 | "MODE -A"); | |
2521 | return; | |
2522 | } | |
2523 | ||
2524 | t_str = state->parv[state->args_used++]; /* grab arg */ | |
2525 | state->parc--; | |
2526 | state->max_args--; | |
2527 | ||
2528 | /* If they're not an oper, they can't change modes */ | |
2529 | if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) { | |
2530 | send_notoper(state); | |
2531 | return; | |
2532 | } | |
2533 | ||
2534 | /* If a non-service user is trying to force it, refuse. */ | |
2535 | if (state->flags & MODE_PARSE_FORCE && MyUser(state->sptr) | |
2536 | && !HasPriv(state->sptr, PRIV_APASS_OPMODE)) { | |
2537 | send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname, | |
2538 | state->chptr->chname); | |
2539 | return; | |
2540 | } | |
2541 | ||
2542 | /* Don't allow to change the Apass if the channel is older than 48 hours. */ | |
2543 | if (MyUser(state->sptr) | |
2544 | && TStime() - state->chptr->creationtime >= 172800 | |
2545 | && !IsAnOper(state->sptr)) { | |
2546 | send_reply(state->sptr, ERR_CHANSECURED, state->chptr->chname); | |
2547 | return; | |
2548 | } | |
2549 | ||
2550 | /* If they are not the channel manager, they are not allowed to change it */ | |
2551 | if (MyUser(state->sptr) && !(state->flags & MODE_PARSE_FORCE || IsChannelManager(state->member))) { | |
2552 | if (*state->chptr->mode.apass) { | |
2553 | send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname, | |
2554 | state->chptr->chname); | |
2555 | } else { | |
2556 | send_reply(state->sptr, ERR_NOMANAGER, state->chptr->chname); | |
2557 | } | |
2558 | return; | |
2559 | } | |
2560 | ||
2561 | if (state->done & DONE_APASS) /* allow apass to be set only once */ | |
2562 | return; | |
2563 | state->done |= DONE_APASS; | |
2564 | ||
2565 | /* clean up the apass string */ | |
2566 | clean_key(t_str); | |
2567 | if (!*t_str || *t_str == ':') { /* warn if empty */ | |
2568 | if (MyUser(state->sptr)) | |
2569 | need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +A" : | |
2570 | "MODE -A"); | |
2571 | return; | |
2572 | } | |
2573 | ||
2574 | if (!state->mbuf) | |
2575 | return; | |
2576 | ||
2577 | if (!(state->flags & MODE_PARSE_FORCE)) { | |
2578 | /* can't remove the apass while upass is still set */ | |
2579 | if (state->dir == MODE_DEL && *state->chptr->mode.upass) { | |
2580 | send_reply(state->sptr, ERR_UPASSSET, state->chptr->chname, state->chptr->chname); | |
2581 | return; | |
2582 | } | |
2583 | /* can't add an apass if one is set, nor can one remove the wrong apass */ | |
2584 | if ((state->dir == MODE_ADD && *state->chptr->mode.apass) || | |
2585 | (state->dir == MODE_DEL && ircd_strcmp(state->chptr->mode.apass, t_str))) { | |
2586 | send_reply(state->sptr, ERR_KEYSET, state->chptr->chname); | |
2587 | return; | |
2588 | } | |
2589 | } | |
2590 | ||
2591 | if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD && | |
2592 | !ircd_strcmp(state->chptr->mode.apass, t_str)) | |
2593 | return; /* no apass change */ | |
2594 | ||
2595 | if (state->flags & MODE_PARSE_BOUNCE) { | |
2596 | if (*state->chptr->mode.apass) /* reset old apass */ | |
2597 | modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0], | |
2598 | state->chptr->mode.apass, 0); | |
2599 | else /* remove new bogus apass */ | |
2600 | modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0); | |
2601 | } else /* send new apass */ | |
2602 | modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0); | |
2603 | ||
2604 | if (state->flags & MODE_PARSE_SET) { | |
2605 | if (state->dir == MODE_ADD) { /* set the new apass */ | |
2606 | /* Only accept the new apass if there is no current apass | |
2607 | * (e.g. when a user sets it) or the new one is "less" than the | |
2608 | * old (for resolving conflicts during burst). | |
2609 | */ | |
2610 | if (state->chptr->mode.apass[0] == '\0' | |
2611 | || ircd_strcmp(t_str, state->chptr->mode.apass) < 0) | |
2612 | ircd_strncpy(state->chptr->mode.apass, t_str, KEYLEN); | |
2613 | /* Make it VERY clear to the user that this is a one-time password */ | |
2614 | if (MyUser(state->sptr)) { | |
2615 | send_reply(state->sptr, RPL_APASSWARN_SET, state->chptr->mode.apass); | |
2616 | send_reply(state->sptr, RPL_APASSWARN_SECRET, state->chptr->chname, | |
2617 | state->chptr->mode.apass); | |
2618 | } | |
2619 | /* Give the channel manager level 0 ops. */ | |
2620 | if (!(state->flags & MODE_PARSE_FORCE) && IsChannelManager(state->member)) | |
2621 | SetOpLevel(state->member, 0); | |
2622 | } else { /* remove the old apass */ | |
2623 | *state->chptr->mode.apass = '\0'; | |
2624 | if (MyUser(state->sptr)) | |
2625 | send_reply(state->sptr, RPL_APASSWARN_CLEAR); | |
2626 | /* Revert everyone to MAXOPLEVEL. */ | |
2627 | for (memb = state->chptr->members; memb; memb = memb->next_member) { | |
2628 | if (memb->status & MODE_CHANOP) | |
2629 | SetOpLevel(memb, MAXOPLEVEL); | |
2630 | } | |
2631 | } | |
2632 | } | |
2633 | } | |
2634 | ||
2635 | /** Compare one ban's extent to another. | |
2636 | * This works very similarly to mmatch() but it knows about CIDR masks | |
2637 | * and ban exceptions. If both bans are CIDR-based, compare their | |
2638 | * address bits; otherwise, use mmatch(). | |
2639 | * @param[in] old_ban One ban. | |
2640 | * @param[in] new_ban Another ban. | |
2641 | * @return Zero if \a old_ban is a superset of \a new_ban, non-zero otherwise. | |
2642 | */ | |
2643 | static int | |
2644 | bmatch(struct Ban *old_ban, struct Ban *new_ban) | |
2645 | { | |
2646 | int res; | |
2647 | assert(old_ban != NULL); | |
2648 | assert(new_ban != NULL); | |
2649 | /* A ban is never treated as a superset of an exception. */ | |
2650 | if (!(old_ban->flags & BAN_EXCEPTION) | |
2651 | && (new_ban->flags & BAN_EXCEPTION)) | |
2652 | return 1; | |
2653 | /* If either is not an address mask, match the text masks. */ | |
2654 | if ((old_ban->flags & new_ban->flags & BAN_IPMASK) == 0) | |
2655 | return mmatch(old_ban->banstr, new_ban->banstr); | |
2656 | /* If the old ban has a longer prefix than new, it cannot be a superset. */ | |
2657 | if (old_ban->addrbits > new_ban->addrbits) | |
2658 | return 1; | |
2659 | /* Compare the masks before the hostname part. */ | |
2660 | old_ban->banstr[old_ban->nu_len] = new_ban->banstr[new_ban->nu_len] = '\0'; | |
2661 | res = mmatch(old_ban->banstr, new_ban->banstr); | |
2662 | old_ban->banstr[old_ban->nu_len] = new_ban->banstr[new_ban->nu_len] = '@'; | |
2663 | if (res) | |
2664 | return res; | |
2665 | /* Compare the addresses. */ | |
2666 | return !ipmask_check(&new_ban->address, &old_ban->address, old_ban->addrbits); | |
2667 | } | |
2668 | ||
2669 | /** Add a ban from a ban list and mark bans that should be removed | |
2670 | * because they overlap. | |
2671 | * | |
2672 | * There are three invariants for a ban list. First, no ban may be | |
2673 | * more specific than another ban. Second, no exception may be more | |
2674 | * specific than another exception. Finally, no ban may be more | |
2675 | * specific than any exception. | |
2676 | * | |
2677 | * @param[in,out] banlist Pointer to head of list. | |
2678 | * @param[in] newban Ban (or exception) to add (or remove). | |
2679 | * @param[in] do_free If non-zero, free \a newban on failure. | |
2680 | * @return Zero if \a newban could be applied, non-zero if not. | |
2681 | */ | |
2682 | int apply_ban(struct Ban **banlist, struct Ban *newban, int do_free) | |
2683 | { | |
2684 | struct Ban *ban; | |
2685 | size_t count = 0; | |
2686 | ||
2687 | assert(newban->flags & (BAN_ADD|BAN_DEL)); | |
2688 | if (newban->flags & BAN_ADD) { | |
2689 | size_t totlen = 0; | |
2690 | /* If a less specific entry is found, fail. */ | |
2691 | for (ban = *banlist; ban; ban = ban->next) { | |
2692 | if (!bmatch(ban, newban)) { | |
2693 | if (do_free) | |
2694 | free_ban(newban); | |
2695 | return 1; | |
2696 | } | |
2697 | if (!(ban->flags & (BAN_OVERLAPPED|BAN_DEL))) { | |
2698 | count++; | |
2699 | totlen += strlen(ban->banstr); | |
2700 | } | |
2701 | } | |
2702 | /* Mark more specific entries and add this one to the end of the list. */ | |
2703 | while ((ban = *banlist) != NULL) { | |
2704 | if (!bmatch(newban, ban)) { | |
2705 | ban->flags |= BAN_OVERLAPPED | BAN_DEL; | |
2706 | } | |
2707 | banlist = &ban->next; | |
2708 | } | |
2709 | *banlist = newban; | |
2710 | return 0; | |
2711 | } else if (newban->flags & BAN_DEL) { | |
2712 | size_t remove_count = 0; | |
2713 | /* Mark more specific entries. */ | |
2714 | for (ban = *banlist; ban; ban = ban->next) { | |
2715 | if (!bmatch(newban, ban)) { | |
2716 | ban->flags |= BAN_OVERLAPPED | BAN_DEL; | |
2717 | remove_count++; | |
2718 | } | |
2719 | } | |
2720 | if (remove_count) | |
2721 | return 0; | |
2722 | /* If no matches were found, fail. */ | |
2723 | if (do_free) | |
2724 | free_ban(newban); | |
2725 | return 3; | |
2726 | } | |
2727 | if (do_free) | |
2728 | free_ban(newban); | |
2729 | return 4; | |
2730 | } | |
2731 | ||
2732 | /* | |
2733 | * Helper function to convert bans | |
2734 | */ | |
2735 | static void | |
2736 | mode_parse_ban(struct ParseState *state, int *flag_p) | |
2737 | { | |
2738 | char *t_str, *s; | |
2739 | struct Ban *ban, *newban; | |
2740 | ||
2741 | if (state->parc <= 0) { /* Not enough args, send ban list */ | |
2742 | if (MyUser(state->sptr) && !(state->done & DONE_BANLIST)) { | |
2743 | send_ban_list(state->sptr, state->chptr); | |
2744 | state->done |= DONE_BANLIST; | |
2745 | } | |
2746 | ||
2747 | return; | |
2748 | } | |
2749 | ||
2750 | if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */ | |
2751 | return; | |
2752 | ||
2753 | t_str = state->parv[state->args_used++]; /* grab arg */ | |
2754 | state->parc--; | |
2755 | state->max_args--; | |
2756 | ||
2757 | /* If they're not an oper, they can't change modes */ | |
2758 | if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) { | |
2759 | send_notoper(state); | |
2760 | return; | |
2761 | } | |
2762 | ||
2763 | if ((s = strchr(t_str, ' '))) | |
2764 | *s = '\0'; | |
2765 | ||
2766 | if (!*t_str || *t_str == ':') { /* warn if empty */ | |
2767 | if (MyUser(state->sptr)) | |
2768 | need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +b" : | |
2769 | "MODE -b"); | |
2770 | return; | |
2771 | } | |
2772 | ||
2773 | /* Clear all ADD/DEL/OVERLAPPED flags from ban list. */ | |
2774 | if (!(state->done & DONE_BANCLEAN)) { | |
2775 | for (ban = state->chptr->banlist; ban; ban = ban->next) | |
2776 | ban->flags &= ~(BAN_ADD | BAN_DEL | BAN_OVERLAPPED); | |
2777 | state->done |= DONE_BANCLEAN; | |
2778 | } | |
2779 | ||
2780 | /* remember the ban for the moment... */ | |
2781 | newban = state->banlist + (state->numbans++); | |
2782 | newban->next = 0; | |
2783 | newban->flags = ((state->dir == MODE_ADD) ? BAN_ADD : BAN_DEL) | |
2784 | | (*flag_p == MODE_BAN ? 0 : BAN_EXCEPTION); | |
2785 | set_ban_mask(newban, collapse(pretty_mask(t_str))); | |
2786 | ircd_strncpy(newban->who, IsUser(state->sptr) ? cli_name(state->sptr) : "*", NICKLEN); | |
2787 | newban->when = TStime(); | |
2788 | apply_ban(&state->chptr->banlist, newban, 0); | |
2789 | } | |
2790 | ||
2791 | /* | |
2792 | * This is the bottom half of the ban processor | |
2793 | */ | |
2794 | static void | |
2795 | mode_process_bans(struct ParseState *state) | |
2796 | { | |
2797 | struct Ban *ban, *newban, *prevban, *nextban; | |
2798 | int count = 0; | |
2799 | int len = 0; | |
2800 | int banlen; | |
2801 | int changed = 0; | |
2802 | ||
2803 | for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) { | |
2804 | count++; | |
2805 | banlen = strlen(ban->banstr); | |
2806 | len += banlen; | |
2807 | nextban = ban->next; | |
2808 | ||
2809 | if ((ban->flags & (BAN_DEL | BAN_ADD)) == (BAN_DEL | BAN_ADD)) { | |
2810 | if (prevban) | |
2811 | prevban->next = 0; /* Break the list; ban isn't a real ban */ | |
2812 | else | |
2813 | state->chptr->banlist = 0; | |
2814 | ||
2815 | count--; | |
2816 | len -= banlen; | |
2817 | ||
2818 | continue; | |
2819 | } else if (ban->flags & BAN_DEL) { /* Deleted a ban? */ | |
2820 | char *bandup; | |
2821 | DupString(bandup, ban->banstr); | |
2822 | modebuf_mode_string(state->mbuf, MODE_DEL | MODE_BAN, | |
2823 | bandup, 1); | |
2824 | ||
2825 | if (state->flags & MODE_PARSE_SET) { /* Ok, make it take effect */ | |
2826 | if (prevban) /* clip it out of the list... */ | |
2827 | prevban->next = ban->next; | |
2828 | else | |
2829 | state->chptr->banlist = ban->next; | |
2830 | ||
2831 | count--; | |
2832 | len -= banlen; | |
2833 | free_ban(ban); | |
2834 | ||
2835 | changed++; | |
2836 | continue; /* next ban; keep prevban like it is */ | |
2837 | } else | |
2838 | ban->flags &= BAN_IPMASK; /* unset other flags */ | |
2839 | } else if (ban->flags & BAN_ADD) { /* adding a ban? */ | |
2840 | if (prevban) | |
2841 | prevban->next = 0; /* Break the list; ban isn't a real ban */ | |
2842 | else | |
2843 | state->chptr->banlist = 0; | |
2844 | ||
2845 | /* If we're supposed to ignore it, do so. */ | |
2846 | if (ban->flags & BAN_OVERLAPPED && | |
2847 | !(state->flags & MODE_PARSE_BOUNCE)) { | |
2848 | count--; | |
2849 | len -= banlen; | |
2850 | } else { | |
2851 | if (state->flags & MODE_PARSE_SET && MyUser(state->sptr) && | |
2852 | (len > (feature_int(FEAT_AVBANLEN) * feature_int(FEAT_MAXBANS)) || | |
2853 | count > feature_int(FEAT_MAXBANS))) { | |
2854 | send_reply(state->sptr, ERR_BANLISTFULL, state->chptr->chname, | |
2855 | ban->banstr); | |
2856 | count--; | |
2857 | len -= banlen; | |
2858 | } else { | |
2859 | char *bandup; | |
2860 | /* add the ban to the buffer */ | |
2861 | DupString(bandup, ban->banstr); | |
2862 | modebuf_mode_string(state->mbuf, MODE_ADD | MODE_BAN, | |
2863 | bandup, 1); | |
2864 | ||
2865 | if (state->flags & MODE_PARSE_SET) { /* create a new ban */ | |
2866 | newban = make_ban(ban->banstr); | |
2867 | strcpy(newban->who, ban->who); | |
2868 | newban->when = ban->when; | |
2869 | newban->flags = ban->flags & BAN_IPMASK; | |
2870 | ||
2871 | newban->next = state->chptr->banlist; /* and link it in */ | |
2872 | state->chptr->banlist = newban; | |
2873 | ||
2874 | changed++; | |
2875 | } | |
2876 | } | |
2877 | } | |
2878 | } | |
2879 | ||
2880 | prevban = ban; | |
2881 | } /* for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) { */ | |
2882 | ||
2883 | if (changed) /* if we changed the ban list, we must invalidate the bans */ | |
2884 | mode_ban_invalidate(state->chptr); | |
2885 | } | |
2886 | ||
2887 | /* | |
2888 | * Helper function to process client changes | |
2889 | */ | |
2890 | static void | |
2891 | mode_parse_client(struct ParseState *state, int *flag_p) | |
2892 | { | |
2893 | char *t_str; | |
2894 | struct Client *acptr; | |
2895 | struct Membership *member; | |
2896 | int oplevel = MAXOPLEVEL + 1; | |
2897 | int i; | |
2898 | ||
2899 | if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */ | |
2900 | return; | |
2901 | ||
2902 | if (state->parc <= 0) /* return if not enough args */ | |
2903 | return; | |
2904 | ||
2905 | t_str = state->parv[state->args_used++]; /* grab arg */ | |
2906 | state->parc--; | |
2907 | state->max_args--; | |
2908 | ||
2909 | /* If they're not an oper, they can't change modes */ | |
2910 | if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) { | |
2911 | send_notoper(state); | |
2912 | return; | |
2913 | } | |
2914 | ||
2915 | if (MyUser(state->sptr)) /* find client we're manipulating */ | |
2916 | acptr = find_chasing(state->sptr, t_str, NULL); | |
2917 | else { | |
2918 | if (t_str[5] == ':') { | |
2919 | t_str[5] = '\0'; | |
2920 | oplevel = atoi(t_str + 6); | |
2921 | } | |
2922 | acptr = findNUser(t_str); | |
2923 | } | |
2924 | ||
2925 | if (!acptr) | |
2926 | return; /* find_chasing() already reported an error to the user */ | |
2927 | ||
2928 | for (i = 0; i < MAXPARA; i++) /* find an element to stick them in */ | |
2929 | if (!state->cli_change[i].flag || (state->cli_change[i].client == acptr && | |
2930 | state->cli_change[i].flag & flag_p[0])) | |
2931 | break; /* found a slot */ | |
2932 | ||
2933 | /* If we are going to bounce this deop, mark the correct oplevel. */ | |
2934 | if (state->flags & MODE_PARSE_BOUNCE | |
2935 | && state->dir == MODE_DEL | |
2936 | && flag_p[0] == MODE_CHANOP | |
2937 | && (member = find_member_link(state->chptr, acptr))) | |
2938 | oplevel = OpLevel(member); | |
2939 | ||
2940 | /* Store what we're doing to them */ | |
2941 | state->cli_change[i].flag = state->dir | flag_p[0]; | |
2942 | state->cli_change[i].oplevel = oplevel; | |
2943 | state->cli_change[i].client = acptr; | |
2944 | } | |
2945 | ||
2946 | /* | |
2947 | * Helper function to process the changed client list | |
2948 | */ | |
2949 | static void | |
2950 | mode_process_clients(struct ParseState *state) | |
2951 | { | |
2952 | int i; | |
2953 | struct Membership *member; | |
2954 | ||
2955 | for (i = 0; state->cli_change[i].flag; i++) { | |
2956 | assert(0 != state->cli_change[i].client); | |
2957 | ||
2958 | /* look up member link */ | |
2959 | if (!(member = find_member_link(state->chptr, | |
2960 | state->cli_change[i].client)) || | |
2961 | (MyUser(state->sptr) && IsZombie(member))) { | |
2962 | if (MyUser(state->sptr)) | |
2963 | send_reply(state->sptr, ERR_USERNOTINCHANNEL, | |
2964 | cli_name(state->cli_change[i].client), | |
2965 | state->chptr->chname); | |
2966 | continue; | |
2967 | } | |
2968 | ||
2969 | if ((state->cli_change[i].flag & MODE_ADD && | |
2970 | (state->cli_change[i].flag & member->status)) || | |
2971 | (state->cli_change[i].flag & MODE_DEL && | |
2972 | !(state->cli_change[i].flag & member->status))) | |
2973 | continue; /* no change made, don't do anything */ | |
2974 | ||
2975 | /* see if the deop is allowed */ | |
2976 | if ((state->cli_change[i].flag & (MODE_DEL | MODE_CHANOP)) == | |
2977 | (MODE_DEL | MODE_CHANOP)) { | |
2978 | /* prevent +k users from being deopped */ | |
943fcf70 | 2979 | /* |
2980 | * ASUKA_X: | |
2981 | * Allow +X'ed users to mess with +k'ed. | |
2982 | * --Bigfoot | |
2983 | */ | |
2984 | if ((IsChannelService(state->cli_change[i].client) && IsService(cli_user(state->cli_change[i].client)->server)) || (IsChannelService(state->cli_change[i].client) && !IsXtraOp(state->sptr))) { | |
189935b1 | 2985 | if (state->flags & MODE_PARSE_FORCE) /* it was forced */ |
2986 | sendto_opmask_butone(0, SNO_HACK4, "Deop of +k user on %H by %s", | |
2987 | state->chptr, | |
2988 | (IsServer(state->sptr) ? cli_name(state->sptr) : | |
2989 | cli_name((cli_user(state->sptr))->server))); | |
2990 | ||
943fcf70 | 2991 | else if (MyUser(state->sptr) && state->flags & MODE_PARSE_SET && (state->sptr != state->cli_change[i].client)) { |
2992 | if(IsService(cli_user(state->cli_change[i].client)->server) && IsChannelService(state->cli_change[i].client)){ | |
2993 | send_reply(state->sptr, ERR_ISREALSERVICE, | |
2994 | cli_name(state->cli_change[i].client), | |
2995 | state->chptr->chname); | |
2996 | }else{ | |
2997 | send_reply(state->sptr, ERR_ISCHANSERVICE, | |
2998 | cli_name(state->cli_change[i].client), | |
2999 | state->chptr->chname); | |
3000 | } | |
3001 | ||
189935b1 | 3002 | continue; |
3003 | } | |
3004 | } | |
3005 | ||
3006 | /* check deop for local user */ | |
3007 | if (MyUser(state->sptr)) { | |
3008 | ||
3009 | /* don't allow local opers to be deopped on local channels */ | |
3010 | if (state->cli_change[i].client != state->sptr && | |
3011 | IsLocalChannel(state->chptr->chname) && | |
3012 | HasPriv(state->cli_change[i].client, PRIV_DEOP_LCHAN)) { | |
3013 | send_reply(state->sptr, ERR_ISOPERLCHAN, | |
3014 | cli_name(state->cli_change[i].client), | |
3015 | state->chptr->chname); | |
3016 | continue; | |
3017 | } | |
3018 | ||
3019 | /* don't allow to deop members with an op level that is <= our own level */ | |
3020 | if (state->sptr != state->cli_change[i].client /* but allow to deop oneself */ | |
3021 | && state->chptr->mode.apass[0] | |
3022 | && state->member | |
3023 | && OpLevel(member) <= OpLevel(state->member)) { | |
3024 | int equal = (OpLevel(member) == OpLevel(state->member)); | |
3025 | send_reply(state->sptr, ERR_NOTLOWEROPLEVEL, | |
3026 | cli_name(state->cli_change[i].client), | |
3027 | state->chptr->chname, | |
3028 | OpLevel(state->member), OpLevel(member), | |
3029 | "deop", equal ? "the same" : "a higher"); | |
3030 | continue; | |
3031 | } | |
3032 | } | |
3033 | } | |
3034 | ||
3035 | /* set op-level of member being opped */ | |
3036 | if ((state->cli_change[i].flag & (MODE_ADD | MODE_CHANOP)) == | |
3037 | (MODE_ADD | MODE_CHANOP)) { | |
3038 | /* If a valid oplevel was specified, use it. | |
3039 | * Otherwise, if being opped by an outsider, get MAXOPLEVEL. | |
3040 | * Otherwise, if not an apass channel, or state->member has | |
3041 | * MAXOPLEVEL, get oplevel MAXOPLEVEL. | |
3042 | * Otherwise, get state->member's oplevel+1. | |
3043 | */ | |
3044 | if (state->cli_change[i].oplevel <= MAXOPLEVEL) | |
3045 | SetOpLevel(member, state->cli_change[i].oplevel); | |
3046 | else if (!state->member) | |
3047 | SetOpLevel(member, MAXOPLEVEL); | |
3048 | else if (!state->chptr->mode.apass[0] || OpLevel(state->member) == MAXOPLEVEL) | |
3049 | SetOpLevel(member, MAXOPLEVEL); | |
3050 | else | |
3051 | SetOpLevel(member, OpLevel(state->member) + 1); | |
3052 | } | |
3053 | ||
3054 | /* actually effect the change */ | |
3055 | if (state->flags & MODE_PARSE_SET) { | |
3056 | if (state->cli_change[i].flag & MODE_ADD) { | |
3057 | if (IsDelayedJoin(member)) | |
3058 | RevealDelayedJoin(member); | |
3059 | member->status |= (state->cli_change[i].flag & | |
3060 | (MODE_CHANOP | MODE_VOICE)); | |
3061 | if (state->cli_change[i].flag & MODE_CHANOP) | |
3062 | ClearDeopped(member); | |
3063 | } else | |
3064 | member->status &= ~(state->cli_change[i].flag & | |
3065 | (MODE_CHANOP | MODE_VOICE)); | |
3066 | } | |
3067 | ||
3068 | /* accumulate the change */ | |
3069 | modebuf_mode_client(state->mbuf, state->cli_change[i].flag, | |
3070 | state->cli_change[i].client, | |
3071 | state->cli_change[i].oplevel); | |
3072 | } /* for (i = 0; state->cli_change[i].flags; i++) */ | |
3073 | } | |
3074 | ||
3075 | /* | |
3076 | * Helper function to process the simple modes | |
3077 | */ | |
3078 | static void | |
3079 | mode_parse_mode(struct ParseState *state, int *flag_p) | |
3080 | { | |
3081 | /* If they're not an oper, they can't change modes */ | |
3082 | if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) { | |
3083 | send_notoper(state); | |
3084 | return; | |
3085 | } | |
3086 | ||
3087 | if (!state->mbuf) | |
3088 | return; | |
3089 | ||
3090 | if (state->dir == MODE_ADD) { | |
3091 | state->add |= flag_p[0]; | |
3092 | state->del &= ~flag_p[0]; | |
3093 | ||
3094 | if (flag_p[0] & MODE_SECRET) { | |
3095 | state->add &= ~MODE_PRIVATE; | |
3096 | state->del |= MODE_PRIVATE; | |
3097 | } else if (flag_p[0] & MODE_PRIVATE) { | |
3098 | state->add &= ~MODE_SECRET; | |
3099 | state->del |= MODE_SECRET; | |
3100 | } | |
3101 | if (flag_p[0] & MODE_DELJOINS) { | |
3102 | state->add &= ~MODE_WASDELJOINS; | |
3103 | state->del |= MODE_WASDELJOINS; | |
3104 | } | |
3105 | } else { | |
3106 | state->add &= ~flag_p[0]; | |
3107 | state->del |= flag_p[0]; | |
3108 | } | |
3109 | ||
3110 | assert(0 == (state->add & state->del)); | |
3111 | assert((MODE_SECRET | MODE_PRIVATE) != | |
3112 | (state->add & (MODE_SECRET | MODE_PRIVATE))); | |
3113 | } | |
3114 | ||
3115 | /* | |
3116 | * This routine is intended to parse MODE or OPMODE commands and effect the | |
3117 | * changes (or just build the bounce buffer). We pass the starting offset | |
3118 | * as a | |
3119 | */ | |
3120 | int | |
3121 | mode_parse(struct ModeBuf *mbuf, struct Client *cptr, struct Client *sptr, | |
3122 | struct Channel *chptr, int parc, char *parv[], unsigned int flags, | |
3123 | struct Membership* member) | |
3124 | { | |
3125 | static int chan_flags[] = { | |
3126 | MODE_CHANOP, 'o', | |
3127 | MODE_VOICE, 'v', | |
3128 | MODE_PRIVATE, 'p', | |
3129 | MODE_SECRET, 's', | |
3130 | MODE_MODERATED, 'm', | |
3131 | MODE_TOPICLIMIT, 't', | |
3132 | MODE_INVITEONLY, 'i', | |
3133 | MODE_NOPRIVMSGS, 'n', | |
3134 | MODE_KEY, 'k', | |
3135 | MODE_APASS, 'A', | |
3136 | MODE_UPASS, 'U', | |
3137 | MODE_BAN, 'b', | |
3138 | MODE_LIMIT, 'l', | |
3139 | MODE_REGONLY, 'r', | |
3140 | MODE_DELJOINS, 'D', | |
149c0c9f | 3141 | MODE_NOQUITPARTS, 'u', |
189935b1 | 3142 | MODE_ADD, '+', |
3143 | MODE_DEL, '-', | |
3144 | 0x0, 0x0 | |
3145 | }; | |
3146 | int i; | |
3147 | int *flag_p; | |
3148 | unsigned int t_mode; | |
3149 | char *modestr; | |
3150 | struct ParseState state; | |
3151 | ||
3152 | assert(0 != cptr); | |
3153 | assert(0 != sptr); | |
3154 | assert(0 != chptr); | |
3155 | assert(0 != parc); | |
3156 | assert(0 != parv); | |
3157 | ||
3158 | state.mbuf = mbuf; | |
3159 | state.cptr = cptr; | |
3160 | state.sptr = sptr; | |
3161 | state.chptr = chptr; | |
3162 | state.member = member; | |
3163 | state.parc = parc; | |
3164 | state.parv = parv; | |
3165 | state.flags = flags; | |
3166 | state.dir = MODE_ADD; | |
3167 | state.done = 0; | |
3168 | state.add = 0; | |
3169 | state.del = 0; | |
3170 | state.args_used = 0; | |
3171 | state.max_args = MAXMODEPARAMS; | |
3172 | state.numbans = 0; | |
3173 | ||
3174 | for (i = 0; i < MAXPARA; i++) { /* initialize ops/voices arrays */ | |
3175 | state.banlist[i].next = 0; | |
3176 | state.banlist[i].who[0] = '\0'; | |
3177 | state.banlist[i].when = 0; | |
3178 | state.banlist[i].flags = 0; | |
3179 | state.cli_change[i].flag = 0; | |
3180 | state.cli_change[i].client = 0; | |
3181 | } | |
3182 | ||
3183 | modestr = state.parv[state.args_used++]; | |
3184 | state.parc--; | |
3185 | ||
3186 | while (*modestr) { | |
3187 | for (; *modestr; modestr++) { | |
3188 | for (flag_p = chan_flags; flag_p[0]; flag_p += 2) /* look up flag */ | |
3189 | if (flag_p[1] == *modestr) | |
3190 | break; | |
3191 | ||
3192 | if (!flag_p[0]) { /* didn't find it? complain and continue */ | |
3193 | if (MyUser(state.sptr)) | |
3194 | send_reply(state.sptr, ERR_UNKNOWNMODE, *modestr); | |
3195 | continue; | |
3196 | } | |
3197 | ||
3198 | switch (*modestr) { | |
3199 | case '+': /* switch direction to MODE_ADD */ | |
3200 | case '-': /* switch direction to MODE_DEL */ | |
3201 | state.dir = flag_p[0]; | |
3202 | break; | |
3203 | ||
3204 | case 'l': /* deal with limits */ | |
3205 | mode_parse_limit(&state, flag_p); | |
3206 | break; | |
3207 | ||
3208 | case 'k': /* deal with keys */ | |
3209 | mode_parse_key(&state, flag_p); | |
3210 | break; | |
3211 | ||
3212 | case 'A': /* deal with Admin passes */ | |
3213 | if (IsServer(cptr) || feature_bool(FEAT_OPLEVELS)) | |
3214 | mode_parse_apass(&state, flag_p); | |
3215 | break; | |
3216 | ||
3217 | case 'U': /* deal with user passes */ | |
3218 | if (IsServer(cptr) || feature_bool(FEAT_OPLEVELS)) | |
3219 | mode_parse_upass(&state, flag_p); | |
3220 | break; | |
3221 | ||
3222 | case 'b': /* deal with bans */ | |
3223 | mode_parse_ban(&state, flag_p); | |
3224 | break; | |
3225 | ||
3226 | case 'o': /* deal with ops/voice */ | |
3227 | case 'v': | |
3228 | mode_parse_client(&state, flag_p); | |
3229 | break; | |
3230 | ||
3231 | default: /* deal with other modes */ | |
3232 | mode_parse_mode(&state, flag_p); | |
3233 | break; | |
3234 | } /* switch (*modestr) */ | |
3235 | } /* for (; *modestr; modestr++) */ | |
3236 | ||
3237 | if (state.flags & MODE_PARSE_BURST) | |
3238 | break; /* don't interpret any more arguments */ | |
3239 | ||
3240 | if (state.parc > 0) { /* process next argument in string */ | |
3241 | modestr = state.parv[state.args_used++]; | |
3242 | state.parc--; | |
3243 | ||
3244 | /* is it a TS? */ | |
3245 | if (IsServer(state.sptr) && !state.parc && IsDigit(*modestr)) { | |
3246 | time_t recv_ts; | |
3247 | ||
3248 | if (!(state.flags & MODE_PARSE_SET)) /* don't set earlier TS if */ | |
3249 | break; /* we're then going to bounce the mode! */ | |
3250 | ||
3251 | recv_ts = atoi(modestr); | |
3252 | ||
3253 | if (recv_ts && recv_ts < state.chptr->creationtime) | |
3254 | state.chptr->creationtime = recv_ts; /* respect earlier TS */ | |
3255 | ||
3256 | break; /* break out of while loop */ | |
3257 | } else if (state.flags & MODE_PARSE_STRICT || | |
3258 | (MyUser(state.sptr) && state.max_args <= 0)) { | |
3259 | state.parc++; /* we didn't actually gobble the argument */ | |
3260 | state.args_used--; | |
3261 | break; /* break out of while loop */ | |
3262 | } | |
3263 | } | |
3264 | } /* while (*modestr) */ | |
3265 | ||
3266 | /* | |
3267 | * the rest of the function finishes building resultant MODEs; if the | |
3268 | * origin isn't a member or an oper, skip it. | |
3269 | */ | |
3270 | if (!state.mbuf || state.flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) | |
3271 | return state.args_used; /* tell our parent how many args we gobbled */ | |
3272 | ||
3273 | t_mode = state.chptr->mode.mode; | |
3274 | ||
3275 | if (state.del & t_mode) { /* delete any modes to be deleted... */ | |
3276 | modebuf_mode(state.mbuf, MODE_DEL | (state.del & t_mode)); | |
3277 | ||
3278 | t_mode &= ~state.del; | |
3279 | } | |
3280 | if (state.add & ~t_mode) { /* add any modes to be added... */ | |
3281 | modebuf_mode(state.mbuf, MODE_ADD | (state.add & ~t_mode)); | |
3282 | ||
3283 | t_mode |= state.add; | |
3284 | } | |
3285 | ||
3286 | if (state.flags & MODE_PARSE_SET) { /* set the channel modes */ | |
3287 | if ((state.chptr->mode.mode & MODE_INVITEONLY) && | |
3288 | !(t_mode & MODE_INVITEONLY)) | |
3289 | mode_invite_clear(state.chptr); | |
3290 | ||
3291 | state.chptr->mode.mode = t_mode; | |
3292 | } | |
3293 | ||
3294 | if (state.flags & MODE_PARSE_WIPEOUT) { | |
3295 | if (state.chptr->mode.limit && !(state.done & DONE_LIMIT)) | |
3296 | modebuf_mode_uint(state.mbuf, MODE_DEL | MODE_LIMIT, | |
3297 | state.chptr->mode.limit); | |
3298 | if (*state.chptr->mode.key && !(state.done & DONE_KEY)) | |
3299 | modebuf_mode_string(state.mbuf, MODE_DEL | MODE_KEY, | |
3300 | state.chptr->mode.key, 0); | |
3301 | if (*state.chptr->mode.upass && !(state.done & DONE_UPASS)) | |
3302 | modebuf_mode_string(state.mbuf, MODE_DEL | MODE_UPASS, | |
3303 | state.chptr->mode.upass, 0); | |
3304 | if (*state.chptr->mode.apass && !(state.done & DONE_APASS)) | |
3305 | modebuf_mode_string(state.mbuf, MODE_DEL | MODE_APASS, | |
3306 | state.chptr->mode.apass, 0); | |
3307 | } | |
3308 | ||
3309 | if (state.done & DONE_BANCLEAN) /* process bans */ | |
3310 | mode_process_bans(&state); | |
3311 | ||
3312 | /* process client changes */ | |
3313 | if (state.cli_change[0].flag) | |
3314 | mode_process_clients(&state); | |
3315 | ||
3316 | return state.args_used; /* tell our parent how many args we gobbled */ | |
3317 | } | |
3318 | ||
3319 | /* | |
3320 | * Initialize a join buffer | |
3321 | */ | |
3322 | void | |
3323 | joinbuf_init(struct JoinBuf *jbuf, struct Client *source, | |
3324 | struct Client *connect, unsigned int type, char *comment, | |
3325 | time_t create) | |
3326 | { | |
3327 | int i; | |
3328 | ||
3329 | assert(0 != jbuf); | |
3330 | assert(0 != source); | |
3331 | assert(0 != connect); | |
3332 | ||
3333 | jbuf->jb_source = source; /* just initialize struct JoinBuf */ | |
3334 | jbuf->jb_connect = connect; | |
3335 | jbuf->jb_type = type; | |
3336 | jbuf->jb_comment = comment; | |
3337 | jbuf->jb_create = create; | |
3338 | jbuf->jb_count = 0; | |
3339 | jbuf->jb_strlen = (((type == JOINBUF_TYPE_JOIN || | |
3340 | type == JOINBUF_TYPE_PART || | |
3341 | type == JOINBUF_TYPE_PARTALL) ? | |
3342 | STARTJOINLEN : STARTCREATELEN) + | |
3343 | (comment ? strlen(comment) + 2 : 0)); | |
3344 | ||
3345 | for (i = 0; i < MAXJOINARGS; i++) | |
3346 | jbuf->jb_channels[i] = 0; | |
3347 | } | |
3348 | ||
3349 | /* | |
3350 | * Add a channel to the join buffer | |
3351 | */ | |
3352 | void | |
3353 | joinbuf_join(struct JoinBuf *jbuf, struct Channel *chan, unsigned int flags) | |
3354 | { | |
3355 | unsigned int len; | |
3356 | int is_local; | |
3357 | ||
3358 | assert(0 != jbuf); | |
3359 | ||
3360 | if (!chan) { | |
3361 | sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect, "0"); | |
3362 | return; | |
3363 | } | |
3364 | ||
3365 | is_local = IsLocalChannel(chan->chname); | |
3366 | ||
3367 | if (jbuf->jb_type == JOINBUF_TYPE_PART || | |
3368 | jbuf->jb_type == JOINBUF_TYPE_PARTALL) { | |
3369 | struct Membership *member = find_member_link(chan, jbuf->jb_source); | |
3370 | if (IsUserParting(member)) | |
3371 | return; | |
3372 | SetUserParting(member); | |
3373 | ||
3374 | /* Send notification to channel */ | |
3375 | if (!(flags & (CHFL_ZOMBIE | CHFL_DELAYED))) | |
3376 | sendcmdto_channel_butserv_butone(jbuf->jb_source, CMD_PART, chan, NULL, 0, | |
149c0c9f | 3377 | ((flags & CHFL_BANNED) || ((chan->mode.mode & MODE_NOQUITPARTS) |
3378 | && !IsChannelService(member->user)) || !jbuf->jb_comment) ? | |
3379 | "%H" : "%H :%s", chan, jbuf->jb_comment); | |
189935b1 | 3380 | else if (MyUser(jbuf->jb_source)) |
3381 | sendcmdto_one(jbuf->jb_source, CMD_PART, jbuf->jb_source, | |
149c0c9f | 3382 | ((flags & CHFL_BANNED) || (chan->mode.mode & MODE_NOQUITPARTS) |
3383 | || !jbuf->jb_comment) ? | |
189935b1 | 3384 | ":%H" : "%H :%s", chan, jbuf->jb_comment); |
3385 | /* XXX: Shouldn't we send a PART here anyway? */ | |
3386 | /* to users on the channel? Why? From their POV, the user isn't on | |
3387 | * the channel anymore anyway. We don't send to servers until below, | |
3388 | * when we gang all the channel parts together. Note that this is | |
3389 | * exactly the same logic, albeit somewhat more concise, as was in | |
3390 | * the original m_part.c */ | |
3391 | ||
3392 | if (jbuf->jb_type == JOINBUF_TYPE_PARTALL || | |
3393 | is_local) /* got to remove user here */ | |
3394 | remove_user_from_channel(jbuf->jb_source, chan); | |
3395 | } else { | |
3396 | int oplevel = !chan->mode.apass[0] ? MAXOPLEVEL | |
3397 | : (flags & CHFL_CHANNEL_MANAGER) ? 0 | |
3398 | : 1; | |
3399 | /* Add user to channel */ | |
3400 | if ((chan->mode.mode & MODE_DELJOINS) && !(flags & CHFL_VOICED_OR_OPPED)) | |
3401 | add_user_to_channel(chan, jbuf->jb_source, flags | CHFL_DELAYED, oplevel); | |
3402 | else | |
3403 | add_user_to_channel(chan, jbuf->jb_source, flags, oplevel); | |
3404 | ||
3405 | /* send notification to all servers */ | |
3406 | if (jbuf->jb_type != JOINBUF_TYPE_CREATE && !is_local) | |
3407 | { | |
3408 | if (flags & CHFL_CHANOP) { | |
3409 | assert(oplevel == 0 || oplevel == 1); | |
3410 | sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect, | |
3411 | "%u:%H %Tu", oplevel, chan, chan->creationtime); | |
3412 | } else | |
3413 | sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect, | |
3414 | "%H %Tu", chan, chan->creationtime); | |
3415 | } | |
3416 | ||
3417 | if (!((chan->mode.mode & MODE_DELJOINS) && !(flags & CHFL_VOICED_OR_OPPED))) { | |
3418 | /* Send the notification to the channel */ | |
3419 | sendcmdto_channel_butserv_butone(jbuf->jb_source, CMD_JOIN, chan, NULL, 0, "%H", chan); | |
3420 | ||
3421 | /* send an op, too, if needed */ | |
3422 | if (flags & CHFL_CHANOP && (oplevel < MAXOPLEVEL || !MyUser(jbuf->jb_source))) | |
3423 | sendcmdto_channel_butserv_butone((chan->mode.apass[0] ? &his : jbuf->jb_source), | |
3424 | CMD_MODE, chan, NULL, 0, "%H +o %C", | |
3425 | chan, jbuf->jb_source); | |
3426 | } else if (MyUser(jbuf->jb_source)) | |
3427 | sendcmdto_one(jbuf->jb_source, CMD_JOIN, jbuf->jb_source, ":%H", chan); | |
3428 | } | |
3429 | ||
3430 | if (jbuf->jb_type == JOINBUF_TYPE_PARTALL || | |
3431 | jbuf->jb_type == JOINBUF_TYPE_JOIN || is_local) | |
3432 | return; /* don't send to remote */ | |
3433 | ||
3434 | /* figure out if channel name will cause buffer to be overflowed */ | |
3435 | len = chan ? strlen(chan->chname) + 1 : 2; | |
3436 | if (jbuf->jb_strlen + len > BUFSIZE) | |
3437 | joinbuf_flush(jbuf); | |
3438 | ||
3439 | /* add channel to list of channels to send and update counts */ | |
3440 | jbuf->jb_channels[jbuf->jb_count++] = chan; | |
3441 | jbuf->jb_strlen += len; | |
3442 | ||
3443 | /* if we've used up all slots, flush */ | |
3444 | if (jbuf->jb_count >= MAXJOINARGS) | |
3445 | joinbuf_flush(jbuf); | |
3446 | } | |
3447 | ||
3448 | /* | |
3449 | * Flush the channel list to remote servers | |
3450 | */ | |
3451 | int | |
3452 | joinbuf_flush(struct JoinBuf *jbuf) | |
3453 | { | |
3454 | char chanlist[BUFSIZE]; | |
3455 | int chanlist_i = 0; | |
3456 | int i; | |
3457 | ||
3458 | if (!jbuf->jb_count || jbuf->jb_type == JOINBUF_TYPE_PARTALL || | |
3459 | jbuf->jb_type == JOINBUF_TYPE_JOIN) | |
3460 | return 0; /* no joins to process */ | |
3461 | ||
3462 | for (i = 0; i < jbuf->jb_count; i++) { /* build channel list */ | |
3463 | build_string(chanlist, &chanlist_i, | |
3464 | jbuf->jb_channels[i] ? jbuf->jb_channels[i]->chname : "0", 0, | |
3465 | i == 0 ? '\0' : ','); | |
3466 | if (JOINBUF_TYPE_PART == jbuf->jb_type) | |
3467 | /* Remove user from channel */ | |
3468 | remove_user_from_channel(jbuf->jb_source, jbuf->jb_channels[i]); | |
3469 | ||
3470 | jbuf->jb_channels[i] = 0; /* mark slot empty */ | |
3471 | } | |
3472 | ||
3473 | jbuf->jb_count = 0; /* reset base counters */ | |
3474 | jbuf->jb_strlen = ((jbuf->jb_type == JOINBUF_TYPE_PART ? | |
3475 | STARTJOINLEN : STARTCREATELEN) + | |
3476 | (jbuf->jb_comment ? strlen(jbuf->jb_comment) + 2 : 0)); | |
3477 | ||
3478 | /* and send the appropriate command */ | |
3479 | switch (jbuf->jb_type) { | |
3480 | case JOINBUF_TYPE_CREATE: | |
3481 | sendcmdto_serv_butone(jbuf->jb_source, CMD_CREATE, jbuf->jb_connect, | |
3482 | "%s %Tu", chanlist, jbuf->jb_create); | |
f952a879 | 3483 | if (MyUser(jbuf->jb_source) && (feature_bool(FEAT_AUTOCHANMODES) && |
3484 | feature_str(FEAT_AUTOCHANMODES_LIST) && (strlen(feature_str(FEAT_AUTOCHANMODES_LIST)) > 0))) { | |
3485 | char *name; | |
3486 | char *p = 0; | |
3487 | for (name = ircd_strtok(&p, chanlist, ","); name; name = ircd_strtok(&p, 0, ",")) { | |
3488 | sendcmdto_serv_butone(jbuf->jb_source, CMD_MODE, jbuf->jb_connect, | |
3489 | "%s %s%s", name, "+", feature_str(FEAT_AUTOCHANMODES_LIST)); | |
3490 | } | |
3491 | } | |
189935b1 | 3492 | break; |
3493 | ||
3494 | case JOINBUF_TYPE_PART: | |
3495 | sendcmdto_serv_butone(jbuf->jb_source, CMD_PART, jbuf->jb_connect, | |
3496 | jbuf->jb_comment ? "%s :%s" : "%s", chanlist, | |
3497 | jbuf->jb_comment); | |
3498 | break; | |
3499 | } | |
3500 | ||
3501 | return 0; | |
3502 | } | |
3503 | ||
3504 | /* Returns TRUE (1) if client is invited, FALSE (0) if not */ | |
3505 | int IsInvited(struct Client* cptr, const void* chptr) | |
3506 | { | |
3507 | struct SLink *lp; | |
3508 | ||
3509 | for (lp = (cli_user(cptr))->invited; lp; lp = lp->next) | |
3510 | if (lp->value.chptr == chptr) | |
3511 | return 1; | |
3512 | return 0; | |
3513 | } | |
3514 | ||
3515 | /* RevealDelayedJoin: sends a join for a hidden user */ | |
3516 | ||
3517 | void RevealDelayedJoin(struct Membership *member) | |
3518 | { | |
3519 | ClearDelayedJoin(member); | |
3520 | sendcmdto_channel_butserv_butone(member->user, CMD_JOIN, member->channel, member->user, 0, ":%H", | |
3521 | member->channel); | |
3522 | CheckDelayedJoins(member->channel); | |
3523 | } | |
3524 | ||
3525 | /* CheckDelayedJoins: checks and clear +d if necessary */ | |
3526 | ||
3527 | void CheckDelayedJoins(struct Channel *chan) | |
3528 | { | |
3529 | struct Membership *memb2; | |
3530 | ||
3531 | if (chan->mode.mode & MODE_WASDELJOINS) { | |
3532 | for (memb2=chan->members;memb2;memb2=memb2->next_member) | |
3533 | if (IsDelayedJoin(memb2)) | |
3534 | break; | |
3535 | ||
3536 | if (!memb2) { | |
3537 | /* clear +d */ | |
3538 | chan->mode.mode &= ~MODE_WASDELJOINS; | |
3539 | sendcmdto_channel_butserv_butone(&his, CMD_MODE, chan, NULL, 0, | |
3540 | "%H -d", chan); | |
3541 | } | |
3542 | } | |
3543 | } |