ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.2.x/src/channel_mode.c
Revision: 10040
Committed: Sat May 28 12:06:04 2022 UTC (4 years, 1 month ago) by michael
Content type: text/x-csrc
File size: 29855 byte(s)
Log Message:
- Rip out channel mode 'u' for now

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 1997-2022 ircd-hybrid development team
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 2 of the License, or
9 * (at your option) 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 * USA
20 */
21
22 /*! \file channel_mode.c
23 * \brief Controls modes on channels.
24 * \version $Id$
25 */
26
27 #include "stdinc.h"
28 #include "list.h"
29 #include "channel.h"
30 #include "channel_mode.h"
31 #include "client.h"
32 #include "conf.h"
33 #include "hostmask.h"
34 #include "irc_string.h"
35 #include "ircd.h"
36 #include "numeric.h"
37 #include "server.h"
38 #include "send.h"
39 #include "memory.h"
40 #include "parse.h"
41 #include "extban.h"
42
43
44 /** Buffer holding as list of channel modes to be used for RPL_MYINFO */
45 char cmode_rpl04[2][256];
46 /** Buffer holding as list of channel modes to be used for RPL_ISUPPORT */
47 char cmode_class[4][256];
48
49 static struct ChModeChange mode_changes[IRCD_BUFSIZE];
50 static unsigned int mode_count;
51 static unsigned int mode_limit; /* number of modes set other than simple */
52 static unsigned int simple_modes_mask; /* bit mask of simple modes already set */
53
54
55 /* check_string()
56 *
57 * inputs - string to check
58 * output - pointer to modified string
59 * side effects - Fixes a string so that the first white space found
60 * becomes an end of string marker (`\0`).
61 * returns the 'fixed' string or "*" if the string
62 * was NULL length or a NULL pointer.
63 */
64 static void
65 check_string(char *s)
66 {
67 char *str = s;
68
69 assert(s);
70
71 for (; *s; ++s)
72 {
73 if (IsSpace(*s))
74 {
75 *s = '\0';
76 break;
77 }
78 }
79
80 if (EmptyString(str))
81 strcpy(str, "*");
82 }
83
84 static const char *
85 get_mask(const struct Ban *ban)
86 {
87 static char buf[MODEBUFLEN];
88 const unsigned int i = extban_format(ban->extban, buf);
89
90 assert(i <= sizeof(buf));
91
92 /* Matching extbans only use ban->host */
93 if (ban->extban & extban_matching_mask())
94 strlcpy(buf + i, ban->host, sizeof(buf) - i);
95 else
96 snprintf(buf + i, sizeof(buf) - i, "%s!%s@%s", ban->name, ban->user, ban->host);
97
98 return buf;
99 }
100
101 /*
102 * Ban functions to work with mode +b/e/I
103 */
104 /* add the specified ID to the channel.
105 * -is 8/9/00
106 */
107
108 const char *
109 add_id(struct Client *client, struct Channel *channel, const char *banid, dlink_list *list, unsigned int type)
110 {
111 dlink_node *node;
112 char mask[MODEBUFLEN];
113 char *maskptr = mask;
114 unsigned int extbans, offset;
115
116 strlcpy(mask, banid, sizeof(mask));
117
118 if (MyClient(client))
119 {
120 unsigned int num_mask = dlink_list_length(&channel->banlist) +
121 dlink_list_length(&channel->exceptlist) +
122 dlink_list_length(&channel->invexlist);
123
124 /* Don't let local clients overflow the b/e/I lists */
125 if (num_mask >= ((HasCMode(channel, MODE_EXTLIMIT)) ? ConfigChannel.max_bans_large :
126 ConfigChannel.max_bans))
127 {
128 sendto_one_numeric(client, &me, ERR_BANLISTFULL, channel->name, banid);
129 return NULL;
130 }
131
132 collapse(mask);
133 }
134
135 enum extban_type etype = extban_parse(mask, &extbans, &offset);
136 maskptr += offset;
137
138 if (MyClient(client))
139 {
140 if (etype == EXTBAN_INVALID)
141 {
142 sendto_one_numeric(client, &me, ERR_INVALIDBAN, channel->name, mask);
143 return NULL;
144 }
145
146 if (etype != EXTBAN_NONE && ConfigChannel.enable_extbans == 0)
147 {
148 sendto_one_numeric(client, &me, ERR_INVALIDBAN, channel->name, mask);
149 return NULL;
150 }
151
152 unsigned int extban_acting = extbans & extban_acting_mask();
153 if (extban_acting)
154 {
155 const struct Extban *extban = extban_find_flag(extban_acting);
156
157 if (extban == NULL || !(extban->types & type))
158 {
159 sendto_one_numeric(client, &me, ERR_INVALIDBAN, channel->name, mask);
160 return NULL;
161 }
162 }
163
164 unsigned extban_matching = extbans & extban_matching_mask();
165 if (extban_matching)
166 {
167 const struct Extban *extban = extban_find_flag(extban_matching);
168
169 if (extban == NULL || !(extban->types & type))
170 {
171 sendto_one_numeric(client, &me, ERR_INVALIDBAN, channel->name, mask);
172 return NULL;
173 }
174 }
175 }
176
177 /* Don't allow empty bans */
178 if (EmptyString(maskptr))
179 return NULL;
180
181 struct Ban *ban = xcalloc(sizeof(*ban));
182 ban->extban = extbans;
183 ban->when = event_base->time.sec_real;
184
185 check_string(maskptr);
186
187 if (etype == EXTBAN_MATCHING)
188 /* Matching extbans have their own format, don't try to parse it */
189 strlcpy(ban->host, maskptr, sizeof(ban->host));
190 else
191 {
192 struct split_nuh_item nuh;
193
194 nuh.nuhmask = maskptr;
195 nuh.nickptr = ban->name;
196 nuh.userptr = ban->user;
197 nuh.hostptr = ban->host;
198
199 nuh.nicksize = sizeof(ban->name);
200 nuh.usersize = sizeof(ban->user);
201 nuh.hostsize = sizeof(ban->host);
202
203 split_nuh(&nuh);
204
205 ban->type = parse_netmask(ban->host, &ban->addr, &ban->bits);
206 }
207
208 if (MyClient(client))
209 ban->banstr_len = strlcpy(ban->banstr, get_mask(ban), sizeof(ban->banstr));
210 else
211 ban->banstr_len = strlcpy(ban->banstr, banid, sizeof(ban->banstr));
212
213 DLINK_FOREACH(node, list->head)
214 {
215 const struct Ban *tmp = node->data;
216
217 if (irccmp(tmp->banstr, ban->banstr) == 0)
218 {
219 xfree(ban);
220 return NULL;
221 }
222 }
223
224 clear_ban_cache_list(&channel->members_local);
225
226 if (IsClient(client))
227 snprintf(ban->who, sizeof(ban->who), "%s!%s@%s", client->name,
228 client->username, client->host);
229 else if (IsHidden(client) || ConfigServerHide.hide_servers)
230 strlcpy(ban->who, me.name, sizeof(ban->who));
231 else
232 strlcpy(ban->who, client->name, sizeof(ban->who));
233
234 dlinkAdd(ban, &ban->node, list);
235
236 return ban->banstr;
237 }
238
239 /*
240 * inputs - pointer to channel
241 * - pointer to ban id
242 * - type of ban, i.e. ban, exception, invex
243 * output - 0 for failure, 1 for success
244 * side effects -
245 */
246 static const char *
247 del_id(struct Client *client, struct Channel *channel, const char *banid, dlink_list *list, unsigned int type)
248 {
249 static char mask[MODEBUFLEN];
250 dlink_node *node;
251
252 assert(banid);
253
254 /* TBD: n!u@h formatting fo local clients */
255
256 DLINK_FOREACH(node, list->head)
257 {
258 struct Ban *ban = node->data;
259
260 if (irccmp(banid, ban->banstr) == 0)
261 {
262 strlcpy(mask, ban->banstr, sizeof(mask)); /* caSe might be different in 'banid' */
263 clear_ban_cache_list(&channel->members_local);
264 remove_ban(ban, list);
265
266 return mask;
267 }
268 }
269
270 return NULL;
271 }
272
273 /* channel_modes()
274 *
275 * inputs - pointer to channel
276 * - pointer to client
277 * - pointer to mode buf
278 * - pointer to parameter buf
279 * output - NONE
280 * side effects - write the "simple" list of channel modes for channel
281 * channel onto buffer mbuf with the parameters in pbuf.
282 */
283 void
284 channel_modes(const struct Channel *channel, const struct Client *client,
285 const struct ChannelMember *member, char *mbuf, char *pbuf)
286 {
287 *mbuf++ = '+';
288 *pbuf = '\0';
289
290 for (const struct chan_mode *tab = cmode_tab; tab->letter; ++tab)
291 if (tab->mode && HasCMode(channel, tab->mode))
292 *mbuf++ = tab->letter;
293
294 if (channel->mode.limit)
295 {
296 *mbuf++ = 'l';
297
298 if (IsServer(client) || member || (member = member_find_link(client, channel)))
299 pbuf += sprintf(pbuf, "%u ", channel->mode.limit);
300 }
301
302 if (channel->mode.key[0])
303 {
304 *mbuf++ = 'k';
305
306 if (IsServer(client) || member || (member = member_find_link(client, channel)))
307 sprintf(pbuf, "%s ", channel->mode.key);
308 }
309
310 *mbuf = '\0';
311 }
312
313 /* fix_key()
314 *
315 * inputs - pointer to key to clean up
316 * output - pointer to cleaned up key
317 * side effects - input string is modified
318 *
319 * stolen from Undernet's ircd -orabidoo
320 */
321 static char *
322 fix_key(char *arg)
323 {
324 unsigned char *s = (unsigned char *)arg;
325 unsigned char *t = (unsigned char *)arg;
326
327 for (unsigned char c; (c = *s) && s - (unsigned char *)arg < KEYLEN; ++s)
328 {
329 c &= 0x7f;
330
331 if (c != ':' && c > ' ' && c != ',')
332 *t++ = c;
333 }
334
335 *t = '\0';
336 return arg;
337 }
338
339 /*
340 * inputs - pointer to channel
341 * output - none
342 * side effects - clear ban cache
343 */
344 void
345 clear_ban_cache_list(dlink_list *list)
346 {
347 dlink_node *node;
348
349 DLINK_FOREACH(node, list->head)
350 {
351 struct ChannelMember *member = node->data;
352 member->flags &= ~(CHFL_BAN_SILENCED | CHFL_BAN_CHECKED | CHFL_MUTE_CHECKED);
353 }
354 }
355
356 /*
357 * Bitmasks for various error returns that channel_mode_set should only return
358 * once per call -orabidoo
359 */
360 enum
361 {
362 SM_ERR_NOOPS = 1 << 0, /* No chan ops */
363 SM_ERR_UNKNOWN = 1 << 1,
364 SM_ERR_RPL_B = 1 << 2,
365 SM_ERR_RPL_E = 1 << 3,
366 SM_ERR_RPL_I = 1 << 4,
367 SM_ERR_NOTONCHANNEL = 1 << 5, /* Client is not on channel */
368 SM_ERR_NOTOPER = 1 << 6, /* Only irc-operators can change that mode */
369 SM_ERR_ONLYSERVER = 1 << 7 /* Only servers or services can change that mode */
370 };
371
372 /* Mode functions handle mode changes for a particular mode... */
373 static void
374 chm_nosuch(struct Client *client, struct Channel *channel, int parc, int *parn, char **parv,
375 int *errors, int alev, int dir, const char c, const struct chan_mode *mode)
376 {
377 if (*errors & SM_ERR_UNKNOWN)
378 return;
379
380 *errors |= SM_ERR_UNKNOWN;
381 sendto_one_numeric(client, &me, ERR_UNKNOWNMODE, c);
382 }
383
384 static void
385 chm_simple(struct Client *client, struct Channel *channel, int parc, int *parn, char **parv,
386 int *errors, int alev, int dir, const char c, const struct chan_mode *mode)
387 {
388 if (mode->only_opers == true)
389 {
390 if (MyClient(client) && !HasUMode(client, UMODE_OPER))
391 {
392 if (!(*errors & SM_ERR_NOTOPER))
393 sendto_one_numeric(client, &me, ERR_NOPRIVILEGES);
394
395 *errors |= SM_ERR_NOTOPER;
396 return;
397 }
398 }
399
400 if (mode->only_servers == true)
401 {
402 if (!IsServer(client) && !HasFlag(client, FLAGS_SERVICE))
403 {
404 if (!(*errors & SM_ERR_ONLYSERVER))
405 sendto_one_numeric(client, &me,
406 alev == CHACCESS_NOTONCHAN ? ERR_NOTONCHANNEL :
407 ERR_ONLYSERVERSCANCHANGE, channel->name);
408
409 *errors |= SM_ERR_ONLYSERVER;
410 return;
411 }
412 }
413
414 if (alev < mode->required_oplevel)
415 {
416 if (!(*errors & SM_ERR_NOOPS))
417 sendto_one_numeric(client, &me,
418 alev == CHACCESS_NOTONCHAN ? ERR_NOTONCHANNEL :
419 ERR_CHANOPRIVSNEEDED, channel->name);
420
421 *errors |= SM_ERR_NOOPS;
422 return;
423 }
424
425 /* If have already dealt with this simple mode, ignore it */
426 if (simple_modes_mask & mode->mode)
427 return;
428
429 simple_modes_mask |= mode->mode;
430
431 if (dir == MODE_ADD) /* setting + */
432 {
433 if (MyClient(client) && HasCMode(channel, mode->mode))
434 return;
435
436 AddCMode(channel, mode->mode);
437 }
438 else if (dir == MODE_DEL) /* setting - */
439 {
440 if (MyClient(client) && !HasCMode(channel, mode->mode))
441 return;
442
443 DelCMode(channel, mode->mode);
444 }
445
446 mode_changes[mode_count].letter = mode->letter;
447 mode_changes[mode_count].arg = NULL;
448 mode_changes[mode_count].id = NULL;
449 mode_changes[mode_count++].dir = dir;
450 }
451
452 static void
453 chm_mask(struct Client *client, struct Channel *channel, int parc, int *parn, char **parv,
454 int *errors, int alev, int dir, const char c, const struct chan_mode *mode)
455 {
456 const char *ret = NULL;
457 dlink_list *list;
458 enum irc_numerics rpl_list = 0, rpl_endlist = 0;
459 int errtype = 0;
460
461 switch (mode->flag)
462 {
463 case CHFL_BAN:
464 errtype = SM_ERR_RPL_B;
465 list = &channel->banlist;
466 rpl_list = RPL_BANLIST;
467 rpl_endlist = RPL_ENDOFBANLIST;
468 break;
469 case CHFL_EXCEPTION:
470 errtype = SM_ERR_RPL_E;
471 list = &channel->exceptlist;
472 rpl_list = RPL_EXCEPTLIST;
473 rpl_endlist = RPL_ENDOFEXCEPTLIST;
474 break;
475 case CHFL_INVEX:
476 errtype = SM_ERR_RPL_I;
477 list = &channel->invexlist;
478 rpl_list = RPL_INVEXLIST;
479 rpl_endlist = RPL_ENDOFINVEXLIST;
480 break;
481 default:
482 list = NULL; /* Let it crash */
483 }
484
485 if (dir == MODE_QUERY || parc <= *parn)
486 {
487 dlink_node *node;
488
489 if (*errors & errtype)
490 return;
491
492 *errors |= errtype;
493
494 DLINK_FOREACH(node, list->head)
495 {
496 const struct Ban *ban = node->data;
497 sendto_one_numeric(client, &me, rpl_list, channel->name,
498 ban->banstr, ban->who, ban->when);
499 }
500
501 sendto_one_numeric(client, &me, rpl_endlist, channel->name);
502 return;
503 }
504
505 if (alev < mode->required_oplevel)
506 {
507 if (!(*errors & SM_ERR_NOOPS))
508 sendto_one_numeric(client, &me,
509 alev == CHACCESS_NOTONCHAN ? ERR_NOTONCHANNEL :
510 ERR_CHANOPRIVSNEEDED, channel->name);
511
512 *errors |= SM_ERR_NOOPS;
513 return;
514 }
515
516 if (MyClient(client) && (++mode_limit > MAXMODEPARAMS))
517 return;
518
519 char *mask = parv[*parn];
520 ++(*parn);
521
522 if (*mask == ':' || (!MyConnect(client) && strchr(mask, ' ')))
523 return;
524
525 if (dir == MODE_ADD) /* setting + */
526 {
527 ret = add_id(client, channel, mask, list, mode->flag);
528 if (ret == NULL)
529 return;
530 }
531 else if (dir == MODE_DEL) /* setting - */
532 {
533 ret = del_id(client, channel, mask, list, mode->flag);
534 if (ret == NULL)
535 return;
536 }
537
538 static char buf[MAXPARA][MODEBUFLEN];
539 mask = buf[(*parn) - 1];
540 strlcpy(mask, ret, sizeof(buf[(*parn) - 1]));
541
542 mode_changes[mode_count].letter = mode->letter;
543 mode_changes[mode_count].arg = mask; /* At this point 'mask' is no longer than MODEBUFLEN */
544 mode_changes[mode_count].id = NULL;
545 mode_changes[mode_count++].dir = dir;
546 }
547
548 static void
549 chm_flag(struct Client *client, struct Channel *channel, int parc, int *parn, char **parv,
550 int *errors, int alev, int dir, const char c, const struct chan_mode *mode)
551 {
552 if (alev < mode->required_oplevel)
553 {
554 if (!(*errors & SM_ERR_NOOPS))
555 sendto_one_numeric(client, &me,
556 alev == CHACCESS_NOTONCHAN ? ERR_NOTONCHANNEL :
557 ERR_CHANOPRIVSNEEDED, channel->name);
558
559 *errors |= SM_ERR_NOOPS;
560 return;
561 }
562
563 if (dir == MODE_QUERY || parc <= *parn)
564 return;
565
566 struct Client *client_target = find_chasing(client, parv[(*parn)++]);
567 if (client_target == NULL)
568 return; /* find_chasing sends ERR_NOSUCHNICK */
569
570 struct ChannelMember *member = member_find_link(client_target, channel);
571 if (member == NULL)
572 {
573 if (!(*errors & SM_ERR_NOTONCHANNEL))
574 sendto_one_numeric(client, &me, ERR_USERNOTINCHANNEL, client_target->name, channel->name);
575
576 *errors |= SM_ERR_NOTONCHANNEL;
577 return;
578 }
579
580 if (MyClient(client) && (++mode_limit > MAXMODEPARAMS))
581 return;
582
583 if (dir == MODE_ADD) /* setting + */
584 {
585 if (member_has_flags(member, mode->flag) == true)
586 return; /* No redundant mode changes */
587
588 AddMemberFlag(member, mode->flag);
589 }
590 else if (dir == MODE_DEL) /* setting - */
591 {
592 if (member_has_flags(member, mode->flag) == false)
593 return; /* No redundant mode changes */
594
595 DelMemberFlag(member, mode->flag);
596 }
597
598 mode_changes[mode_count].letter = mode->letter;
599 mode_changes[mode_count].arg = client_target->name;
600 mode_changes[mode_count].id = client_target->id;
601 mode_changes[mode_count++].dir = dir;
602 }
603
604 static void
605 chm_limit(struct Client *client, struct Channel *channel, int parc, int *parn, char **parv,
606 int *errors, int alev, int dir, const char c, const struct chan_mode *mode)
607 {
608 if (alev < mode->required_oplevel)
609 {
610 if (!(*errors & SM_ERR_NOOPS))
611 sendto_one_numeric(client, &me,
612 alev == CHACCESS_NOTONCHAN ? ERR_NOTONCHANNEL :
613 ERR_CHANOPRIVSNEEDED, channel->name);
614 *errors |= SM_ERR_NOOPS;
615 return;
616 }
617
618 if (dir == MODE_QUERY)
619 return;
620
621 if (dir == MODE_ADD && parc > *parn)
622 {
623 char *const lstr = parv[(*parn)++];
624 int limit = 0;
625
626 if (EmptyString(lstr) || (limit = atoi(lstr)) <= 0)
627 return;
628
629 sprintf(lstr, "%d", limit);
630
631 /* If somebody sets MODE #channel +ll 1 2, accept latter --fl */
632 for (unsigned int i = 0; i < mode_count; ++i)
633 if (mode_changes[i].letter == mode->letter && mode_changes[i].dir == MODE_ADD)
634 mode_changes[i].letter = 0;
635
636 mode_changes[mode_count].letter = mode->letter;
637 mode_changes[mode_count].arg = lstr;
638 mode_changes[mode_count].id = NULL;
639 mode_changes[mode_count++].dir = dir;
640
641 channel->mode.limit = limit;
642 }
643 else if (dir == MODE_DEL)
644 {
645 if (channel->mode.limit == 0)
646 return;
647
648 channel->mode.limit = 0;
649
650 mode_changes[mode_count].letter = mode->letter;
651 mode_changes[mode_count].arg = NULL;
652 mode_changes[mode_count].id = NULL;
653 mode_changes[mode_count++].dir = dir;
654 }
655 }
656
657 static void
658 chm_key(struct Client *client, struct Channel *channel, int parc, int *parn, char **parv,
659 int *errors, int alev, int dir, const char c, const struct chan_mode *mode)
660 {
661 if (alev < mode->required_oplevel)
662 {
663 if (!(*errors & SM_ERR_NOOPS))
664 sendto_one_numeric(client, &me,
665 alev == CHACCESS_NOTONCHAN ? ERR_NOTONCHANNEL :
666 ERR_CHANOPRIVSNEEDED, channel->name);
667 *errors |= SM_ERR_NOOPS;
668 return;
669 }
670
671 if (dir == MODE_QUERY)
672 return;
673
674 if (dir == MODE_ADD && parc > *parn)
675 {
676 char *const key = fix_key(parv[(*parn)++]);
677
678 if (EmptyString(key))
679 return;
680
681 assert(key[0] != ' ');
682 strlcpy(channel->mode.key, key, sizeof(channel->mode.key));
683
684 /* If somebody does MODE #channel +kk a b, accept latter --fl */
685 for (unsigned int i = 0; i < mode_count; ++i)
686 if (mode_changes[i].letter == mode->letter && mode_changes[i].dir == MODE_ADD)
687 mode_changes[i].letter = 0;
688
689 mode_changes[mode_count].letter = mode->letter;
690 mode_changes[mode_count].arg = key;
691 mode_changes[mode_count].id = NULL;
692 mode_changes[mode_count++].dir = dir;
693 }
694 else if (dir == MODE_DEL)
695 {
696 if (parc > *parn)
697 ++(*parn);
698
699 if (channel->mode.key[0] == '\0')
700 return;
701
702 channel->mode.key[0] = '\0';
703
704 mode_changes[mode_count].letter = mode->letter;
705 mode_changes[mode_count].arg = "*";
706 mode_changes[mode_count].id = NULL;
707 mode_changes[mode_count++].dir = dir;
708 }
709 }
710
711 /* get_channel_access()
712 *
713 * inputs - pointer to Client struct
714 * - pointer to Membership struct
715 * output - CHACCESS_CHANOP if we should let them have
716 * chanop level access, 0 for peon level access.
717 * side effects - NONE
718 */
719 static int
720 get_channel_access(const struct Client *client,
721 const struct ChannelMember *member)
722 {
723 /* Let hacked servers in for now... */
724 if (!MyClient(client))
725 return CHACCESS_REMOTE;
726
727 if (member == NULL)
728 return CHACCESS_NOTONCHAN;
729
730 /* Just to be sure.. */
731 assert(client == member->client);
732
733 if (member_has_flags(member, CHFL_CHANOP) == true)
734 return CHACCESS_CHANOP;
735
736 if (member_has_flags(member, CHFL_HALFOP) == true)
737 return CHACCESS_HALFOP;
738
739 return CHACCESS_PEON;
740 }
741
742 /* send_mode_changes_server()
743 * Input: the source client(client),
744 * the channel to send mode changes for(channel)
745 * Output: None.
746 * Side-effects: Sends the appropriate mode changes to servers.
747 *
748 */
749 static void
750 send_mode_changes_server(struct Client *client, struct Channel *channel)
751 {
752 char modebuf[IRCD_BUFSIZE] = "";
753 char parabuf[IRCD_BUFSIZE] = ""; /* Essential that parabuf[0] = '\0' */
754 char *parptr = parabuf;
755 unsigned int mbl = 0, pbl = 0, arglen = 0, modecount = 0, paracount = 0;
756 unsigned int dir = MODE_QUERY;
757
758 mbl = snprintf(modebuf, sizeof(modebuf), ":%s TMODE %ju %s ",
759 client->id, channel->creation_time, channel->name);
760
761 /* Loop the list of modes we have */
762 for (unsigned int i = 0; i < mode_count; ++i)
763 {
764 if (mode_changes[i].letter == 0)
765 continue;
766
767 const char *arg;
768 if (mode_changes[i].id)
769 arg = mode_changes[i].id;
770 else
771 arg = mode_changes[i].arg;
772
773 if (arg)
774 arglen = strlen(arg);
775 else
776 arglen = 0;
777
778 /*
779 * If we're creeping past the buf size, we need to send it and make
780 * another line for the other modes
781 */
782 if ((paracount == MAXMODEPARAMS) ||
783 ((arglen + mbl + pbl + 2 /* +2 for /r/n */ ) > IRCD_BUFSIZE))
784 {
785 if (modecount)
786 sendto_server(client, 0, 0, paracount == 0 ? "%s" : "%s %s", modebuf, parabuf);
787
788 modecount = 0;
789 paracount = 0;
790
791 mbl = snprintf(modebuf, sizeof(modebuf), ":%s TMODE %ju %s ",
792 client->id, channel->creation_time, channel->name);
793
794 pbl = 0;
795 parabuf[0] = '\0';
796 parptr = parabuf;
797 dir = MODE_QUERY;
798 }
799
800 if (dir != mode_changes[i].dir)
801 {
802 modebuf[mbl++] = (mode_changes[i].dir == MODE_ADD) ? '+' : '-';
803 dir = mode_changes[i].dir;
804 }
805
806 modebuf[mbl++] = mode_changes[i].letter;
807 modebuf[mbl] = '\0';
808 ++modecount;
809
810 if (arg)
811 {
812 int len = sprintf(parptr, (pbl == 0) ? "%s" : " %s", arg);
813 pbl += len;
814 parptr += len;
815 ++paracount;
816 }
817 }
818
819 if (modecount)
820 sendto_server(client, 0, 0, paracount == 0 ? "%s" : "%s %s", modebuf, parabuf);
821 }
822
823 /* void send_mode_changes(struct Client *client,
824 * struct Client *client,
825 * struct Channel *channel)
826 * Input: The client sending(client), the source client(client),
827 * the channel to send mode changes for(channel),
828 * mode change globals.
829 * Output: None.
830 * Side-effects: Sends the appropriate mode changes to other clients
831 * and propagates to servers.
832 */
833 static void
834 send_mode_changes_client(struct Client *client, struct Channel *channel)
835 {
836 char modebuf[IRCD_BUFSIZE] = "";
837 char parabuf[IRCD_BUFSIZE] = ""; /* Essential that parabuf[0] = '\0' */
838 char *parptr = parabuf;
839 unsigned int mbl = 0, pbl = 0, arglen = 0, modecount = 0, paracount = 0;
840 unsigned int dir = MODE_QUERY;
841
842 if (IsClient(client))
843 mbl = snprintf(modebuf, sizeof(modebuf), ":%s!%s@%s MODE %s ", client->name,
844 client->username, client->host, channel->name);
845 else
846 mbl = snprintf(modebuf, sizeof(modebuf), ":%s MODE %s ", (IsHidden(client) ||
847 ConfigServerHide.hide_servers) ?
848 me.name : client->name, channel->name);
849
850 for (unsigned int i = 0; i < mode_count; ++i)
851 {
852 if (mode_changes[i].letter == 0)
853 continue;
854
855 const char *arg = mode_changes[i].arg;
856 if (arg)
857 arglen = strlen(arg);
858 else
859 arglen = 0;
860
861 if ((paracount == MAXMODEPARAMS) ||
862 ((arglen + mbl + pbl + 2 /* +2 for /r/n */ ) > IRCD_BUFSIZE))
863 {
864 if (modecount)
865 sendto_channel_local(NULL, channel, 0, 0, 0, paracount == 0 ? "%s" : "%s %s", modebuf, parabuf);
866
867 modecount = 0;
868 paracount = 0;
869
870 if (IsClient(client))
871 mbl = snprintf(modebuf, sizeof(modebuf), ":%s!%s@%s MODE %s ", client->name,
872 client->username, client->host, channel->name);
873 else
874 mbl = snprintf(modebuf, sizeof(modebuf), ":%s MODE %s ", (IsHidden(client) ||
875 ConfigServerHide.hide_servers) ?
876 me.name : client->name, channel->name);
877
878 pbl = 0;
879 parabuf[0] = '\0';
880 parptr = parabuf;
881 dir = MODE_QUERY;
882 }
883
884 if (dir != mode_changes[i].dir)
885 {
886 modebuf[mbl++] = (mode_changes[i].dir == MODE_ADD) ? '+' : '-';
887 dir = mode_changes[i].dir;
888 }
889
890 modebuf[mbl++] = mode_changes[i].letter;
891 modebuf[mbl] = '\0';
892 ++modecount;
893
894 if (arg)
895 {
896 int len = sprintf(parptr, (pbl == 0) ? "%s" : " %s", arg);
897 pbl += len;
898 parptr += len;
899 ++paracount;
900 }
901 }
902
903 if (modecount)
904 sendto_channel_local(NULL, channel, 0, 0, 0, paracount == 0 ? "%s" : "%s %s", modebuf, parabuf);
905 }
906
907 const struct chan_mode *cmode_map[256];
908 const struct chan_mode cmode_tab[] =
909 {
910 { .letter = 'b', .flag = CHFL_BAN, .required_oplevel = CHACCESS_HALFOP, .func = chm_mask, .class = MODE_CLASS_A },
911 { .letter = 'c', .mode = MODE_NOCTRL, .required_oplevel = CHACCESS_HALFOP, .func = chm_simple, .class = MODE_CLASS_D },
912 { .letter = 'e', .flag = CHFL_EXCEPTION, .required_oplevel = CHACCESS_HALFOP, .func = chm_mask, .class = MODE_CLASS_A },
913 { .letter = 'h', .flag = CHFL_HALFOP, .required_oplevel = CHACCESS_CHANOP, .func = chm_flag, .class = MODE_CLASS_B },
914 { .letter = 'i', .mode = MODE_INVITEONLY, .required_oplevel = CHACCESS_HALFOP, .func = chm_simple, .class = MODE_CLASS_D },
915 { .letter = 'k', .func = chm_key, .required_oplevel = CHACCESS_HALFOP, .class = MODE_CLASS_B },
916 { .letter = 'l', .func = chm_limit, .required_oplevel = CHACCESS_HALFOP, .class = MODE_CLASS_C },
917 { .letter = 'm', .mode = MODE_MODERATED, .required_oplevel = CHACCESS_HALFOP, .func = chm_simple, .class = MODE_CLASS_D },
918 { .letter = 'n', .mode = MODE_NOPRIVMSGS, .required_oplevel = CHACCESS_HALFOP, .func = chm_simple, .class = MODE_CLASS_D },
919 { .letter = 'o', .flag = CHFL_CHANOP, .required_oplevel = CHACCESS_CHANOP, .func = chm_flag, .class = MODE_CLASS_B },
920 { .letter = 'p', .mode = MODE_PRIVATE, .required_oplevel = CHACCESS_HALFOP, .func = chm_simple, .class = MODE_CLASS_D },
921 { .letter = 'r', .mode = MODE_REGISTERED, .required_oplevel = CHACCESS_REMOTE, .only_servers = true, .func = chm_simple, .class = MODE_CLASS_D },
922 { .letter = 's', .mode = MODE_SECRET, .required_oplevel = CHACCESS_HALFOP, .func = chm_simple, .class = MODE_CLASS_D },
923 { .letter = 't', .mode = MODE_TOPICLIMIT, .required_oplevel = CHACCESS_HALFOP, .func = chm_simple, .class = MODE_CLASS_D },
924 { .letter = 'v', .flag = CHFL_VOICE, .required_oplevel = CHACCESS_HALFOP, .func = chm_flag, .class = MODE_CLASS_B },
925 { .letter = 'C', .mode = MODE_NOCTCP, .required_oplevel = CHACCESS_HALFOP, .func = chm_simple, .class = MODE_CLASS_D },
926 { .letter = 'I', .flag = CHFL_INVEX, .required_oplevel = CHACCESS_HALFOP, .func = chm_mask, .class = MODE_CLASS_A },
927 { .letter = 'K', .mode = MODE_NOKNOCK, .required_oplevel = CHACCESS_HALFOP, .func = chm_simple, .class = MODE_CLASS_D },
928 { .letter = 'L', .mode = MODE_EXTLIMIT, .required_oplevel = CHACCESS_HALFOP, .only_opers = true, .func = chm_simple, .class = MODE_CLASS_D },
929 { .letter = 'M', .mode = MODE_MODREG, .required_oplevel = CHACCESS_HALFOP, .func = chm_simple, .class = MODE_CLASS_D },
930 { .letter = 'N', .mode = MODE_NONICKCHANGE, .required_oplevel = CHACCESS_HALFOP, .func = chm_simple, .class = MODE_CLASS_D },
931 { .letter = 'O', .mode = MODE_OPERONLY, .required_oplevel = CHACCESS_HALFOP, .only_opers = true, .func = chm_simple, .class = MODE_CLASS_D },
932 { .letter = 'R', .mode = MODE_REGONLY, .required_oplevel = CHACCESS_HALFOP, .func = chm_simple, .class = MODE_CLASS_D },
933 { .letter = 'S', .mode = MODE_SECUREONLY, .required_oplevel = CHACCESS_HALFOP, .func = chm_simple, .class = MODE_CLASS_D },
934 { .letter = 'T', .mode = MODE_NONOTICE, .required_oplevel = CHACCESS_HALFOP, .func = chm_simple, .class = MODE_CLASS_D },
935 { .letter = '\0' }
936 };
937
938 void
939 channel_mode_init(void)
940 {
941 for (const struct chan_mode *tab = cmode_tab; tab->letter; ++tab)
942 cmode_map[tab->letter] = tab;
943
944 for (unsigned int i = 0; i < 256; ++i)
945 {
946 const struct chan_mode *cmode = cmode_map[i];
947 if (cmode == NULL)
948 continue;
949
950 const char str[] = { cmode->letter, '\0' };
951 strlcat(cmode_rpl04[0], str, sizeof(cmode_rpl04[0]));
952
953 if (cmode->class != MODE_CLASS_D)
954 strlcat(cmode_rpl04[1], str, sizeof(cmode_rpl04[1]));
955
956 /*
957 * from draft-brocklesby-irc-isupport-03:
958 * The IRC server MUST NOT list modes in CHANMODES which are also
959 * present in the PREFIX parameter; however, for completeness, modes
960 * described in PREFIX may be treated as type B modes.
961 */
962 if (cmode->func != chm_flag)
963 strlcat(cmode_class[cmode->class], str, sizeof(cmode_class[cmode->class]));
964 }
965 }
966
967 /*
968 * Input: The the client this originated
969 * from, the channel, the parameter count starting at the modes,
970 * the parameters, the channel name.
971 * Output: None.
972 * Side-effects: Changes the channel membership and modes appropriately,
973 * sends the appropriate MODE messages to the appropriate
974 * clients.
975 */
976 void
977 channel_mode_set(struct Client *client, struct Channel *channel,
978 struct ChannelMember *member, int parc, char *parv[])
979 {
980 int dir = MODE_ADD;
981 int parn = 1;
982 int alevel = 0, errors = 0;
983
984 mode_count = 0;
985 mode_limit = 0;
986 simple_modes_mask = 0;
987
988 alevel = get_channel_access(client, member);
989
990 for (const char *ml = parv[0]; *ml; ++ml)
991 {
992 switch (*ml)
993 {
994 case '+':
995 dir = MODE_ADD;
996 break;
997 case '-':
998 dir = MODE_DEL;
999 break;
1000 case '=':
1001 dir = MODE_QUERY;
1002 break;
1003 default:
1004 {
1005 const struct chan_mode *mode = cmode_map[(unsigned char)*ml];
1006
1007 if (mode)
1008 mode->func(client, channel, parc, &parn, parv, &errors, alevel, dir, *ml, mode);
1009 else
1010 chm_nosuch(client, channel, parc, &parn, parv, &errors, alevel, dir, *ml, NULL);
1011 break;
1012 }
1013 }
1014 }
1015
1016 /* Bail out if we have nothing to do... */
1017 if (mode_count == 0)
1018 return;
1019
1020 send_mode_changes_client(client, channel);
1021 send_mode_changes_server(client, channel);
1022 }

Properties

Name Value
svn:eol-style native
svn:keywords Id