2 * IRC - Internet Relay Chat, ircd/m_burst.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
6 * See file AUTHORS in IRC package for additional names of
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 1, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 * m_functions execute protocol messages on this server:
29 * cptr is always NON-NULL, pointing to a *LOCAL* client
30 * structure (with an open socket connected!). This
31 * identifies the physical socket where the message
32 * originated (or which caused the m_function to be
33 * executed--some m_functions may call others...).
35 * sptr is the source of the message, defined by the
36 * prefix part of the message if present. If not
37 * or prefix not found, then sptr==cptr.
39 * (!IsServer(cptr)) => (cptr == sptr), because
40 * prefixes are taken *only* from servers...
43 * (sptr == cptr) => the message didn't
46 * (sptr != cptr && IsServer(sptr) means
47 * the prefix specified servername. (?)
49 * (sptr != cptr && !IsServer(sptr) means
50 * that message originated from a remote
55 * (!IsServer(sptr)) means that, sptr can safely
56 * taken as defining the target structure of the
57 * message in this server.
59 * *Always* true (if 'parse' and others are working correct):
61 * 1) sptr->from == cptr (note: cptr->from == cptr)
63 * 2) MyConnect(sptr) <=> sptr == cptr (e.g. sptr
64 * *cannot* be a local connection, unless it's
65 * actually cptr!). [MyConnect(x) should probably
66 * be defined as (x == x->from) --msa ]
68 * parc number of variable parameter strings (if zero,
69 * parv is allowed to be NULL)
71 * parv a NULL terminated list of parameter pointers,
73 * parv[0], sender (prefix string), if not present
74 * this points to an empty string.
75 * parv[1]...parv[parc-1]
76 * pointers to additional parameters
77 * parv[parc] == NULL, *always*
79 * note: it is guaranteed that parv[0]..parv[parc-1] are all
88 #include "ircd_alloc.h"
89 #include "ircd_features.h"
90 #include "ircd_reply.h"
91 #include "ircd_string.h"
101 #include "ircd_snprintf.h"
109 netride_modes(int parc, char **parv, const char *curr_key)
111 char *modes = parv[0];
114 assert(modes && modes[0] == '+');
118 result |= MODE_INVITEONLY;
121 if (strcmp(curr_key, *++parv))
128 result |= MODE_REGONLY;
136 * ms_burst - server message handler
138 * -- by Run carlo@runaway.xs4all.nl december 1995 till march 1997
140 * parv[0] = sender prefix
141 * parv[1] = channel name
142 * parv[2] = channel timestamp
143 * The meaning of the following parv[]'s depend on their first character:
144 * If parv[n] starts with a '+':
145 * Net burst, additive modes
147 * parv[n+1] = <param> (optional)
148 * parv[n+2] = <param> (optional)
149 * If parv[n] starts with a '%', then n will be parc-1:
150 * parv[n] = %<ban> <ban> <ban> ...
151 * If parv[n] starts with another character:
152 * parv[n] = <nick>[:<mode>],<nick>[:<mode>],...
153 * where <mode> defines the mode and op-level
154 * for nick and all following nicks until the
156 * Digits in the <mode> field have of two meanings:
157 * 1) if it is the first field in this BURST message
158 * that contains digits, and/or when a 'v' is
159 * present in the <mode>:
160 * The absolute value of the op-level.
161 * 2) if there are only digits in this field and
162 * it is not the first field with digits:
163 * An op-level increment relative to the previous
165 * First all modeless nicks must be emmitted,
166 * then all combinations of modes without ops
167 * (currently that is only 'v') followed by the same
168 * series but then with ops (currently 'o','ov').
171 * "A8 B #test 87654321 +ntkAl key secret 123 A8AAG,A8AAC:v,A8AAA:0,A8AAF:2,A8AAD,A8AAB:v1,A8AAE:1 :%ban1 ban2"
173 * <mode> list example:
175 * "xxx,sss:v,ttt,aaa:123,bbb,ccc:2,ddd,kkk:v2,lll:2,mmm"
179 * xxx // first modeless nicks
180 * sss +v // then opless nicks
181 * ttt +v // no ":<mode>": everything stays the same
182 * aaa -123 // first field with digit: absolute value
184 * ccc -125 // only digits, not first field: increment
186 * kkk -2 +v // field with a 'v': absolute value
187 * lll -4 +v // only digits: increment
190 * Anti net.ride code.
192 * When the channel already exist, and its TS is larger than
193 * the TS in the BURST message, then we cancel all existing modes.
194 * If its is smaller then the received BURST message is ignored.
195 * If it's equal, then the received modes are just added.
197 * BURST is also accepted outside a netburst now because it
198 * is sent upstream as reaction to a DESTRUCT message. For
199 * these BURST messages it is possible that the listed channel
200 * members are already joined.
202 int ms_burst(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
204 struct ModeBuf modebuf, *mbuf = 0;
205 struct Channel *chptr;
207 struct Membership *member, *nmember;
208 struct SLink *lp, **lp_p;
209 unsigned int parse_flags = (MODE_PARSE_FORCE | MODE_PARSE_BURST);
210 int param, nickpos = 0, banpos = 0;
211 char modestr[BUFSIZE], nickstr[BUFSIZE], banstr[BUFSIZE];
214 return protocol_violation(sptr,"Too few parameters for BURST");
216 if (!(chptr = get_channel(sptr, parv[1], CGT_CREATE)))
217 return 0; /* can't create the channel? */
219 timestamp = atoi(parv[2]);
221 if (!chptr->creationtime || chptr->creationtime > timestamp) {
223 * Kick local members if channel is +i or +k and our TS was larger
224 * than the burst TS (anti net.ride). The modes hack is here because
225 * we have to do this before mode_parse, as chptr may go away.
227 for (param = 3; param < parc; param++)
230 if (parv[param][0] != '+')
232 check_modes = netride_modes(parc - param, parv + param, chptr->mode.key);
235 /* Clear any outstanding rogue invites */
236 mode_invite_clear(chptr);
237 for (member = chptr->members; member; member = nmember)
239 nmember = member->next_member;
240 if (!MyUser(member->user) || IsZombie(member))
242 /* Kick as netrider if key mismatch *or* remote channel is
243 * +i (unless user is an oper) *or* remote channel is +r
244 * (unless user has an account).
246 if (!(check_modes & MODE_KEY)
247 && (!(check_modes & MODE_INVITEONLY) || IsAnOper(member->user))
248 && (!(check_modes & MODE_REGONLY) || IsAccount(member->user)))
250 sendcmdto_serv_butone(&me, CMD_KICK, NULL, "%H %C :Net Rider", chptr, member->user);
251 sendcmdto_channel_butserv_butone(&me, CMD_KICK, chptr, NULL, 0, "%H %C :Net Rider", chptr, member->user);
252 make_zombie(member, member->user, &me, &me, chptr);
258 /* If the channel had only locals, it went away by now. */
259 if (!(chptr = get_channel(sptr, parv[1], CGT_CREATE)))
260 return 0; /* can't create the channel? */
263 /* turn off burst joined flag */
264 for (member = chptr->members; member; member = member->next_member)
265 member->status &= ~(CHFL_BURST_JOINED|CHFL_BURST_ALREADY_OPPED|CHFL_BURST_ALREADY_VOICED);
267 if (!chptr->creationtime) /* mark channel as created during BURST */
268 chptr->mode.mode |= MODE_BURSTADDED;
270 /* new channel or an older one */
271 if (!chptr->creationtime || chptr->creationtime > timestamp) {
272 chptr->creationtime = timestamp;
274 modebuf_init(mbuf = &modebuf, &me, cptr, chptr,
275 MODEBUF_DEST_CHANNEL | MODEBUF_DEST_NOKEY);
276 modebuf_mode(mbuf, MODE_DEL | chptr->mode.mode); /* wipeout modes */
277 chptr->mode.mode &= ~(MODE_ADD | MODE_DEL | MODE_PRIVATE | MODE_SECRET |
278 MODE_MODERATED | MODE_TOPICLIMIT | MODE_INVITEONLY |
279 MODE_NOPRIVMSGS | MODE_DELJOINS);
281 parse_flags |= (MODE_PARSE_SET | MODE_PARSE_WIPEOUT); /* wipeout keys */
283 /* mark bans for wipeout */
284 for (lp = chptr->banlist; lp; lp = lp->next)
285 lp->flags |= CHFL_BURST_BAN_WIPEOUT;
287 /* clear topic set by netrider (if set) */
289 *chptr->topic = '\0';
290 *chptr->topic_nick = '\0';
291 chptr->topic_time = 0;
292 sendcmdto_channel_butserv_butone(&me, CMD_TOPIC, chptr, NULL, 0,
293 "%H :%s", chptr, chptr->topic);
295 } else if (chptr->creationtime == timestamp) {
296 modebuf_init(mbuf = &modebuf, &me, cptr, chptr,
297 MODEBUF_DEST_CHANNEL | MODEBUF_DEST_NOKEY);
299 parse_flags |= MODE_PARSE_SET; /* set new modes */
302 param = 3; /* parse parameters */
303 while (param < parc) {
304 switch (*parv[param]) {
305 case '+': /* parameter introduces a mode string */
306 param += mode_parse(mbuf, cptr, sptr, chptr, parc - param,
307 parv + param, parse_flags, NULL);
310 case '%': /* parameter contains bans */
311 if (parse_flags & MODE_PARSE_SET) {
312 char *banlist = parv[param] + 1, *p = 0, *ban, *ptr;
313 struct SLink *newban;
315 for (ban = ircd_strtok(&p, banlist, " "); ban;
316 ban = ircd_strtok(&p, 0, " ")) {
317 ban = collapse(pretty_mask(ban));
320 * Yeah, we should probably do this elsewhere, and make it better
321 * and more general; this will hold until we get there, though.
322 * I dislike the current add_banid API... -Kev
324 * I wish there were a better algo. for this than the n^2 one
327 for (lp = chptr->banlist; lp; lp = lp->next) {
328 if (!ircd_strcmp(lp->value.ban.banstr, ban)) {
329 ban = 0; /* don't add ban */
330 lp->flags &= ~CHFL_BURST_BAN_WIPEOUT; /* not wiping out */
331 break; /* new ban already existed; don't even repropagate */
332 } else if (!(lp->flags & CHFL_BURST_BAN_WIPEOUT) &&
333 !mmatch(lp->value.ban.banstr, ban)) {
334 ban = 0; /* don't add ban unless wiping out bans */
335 break; /* new ban is encompassed by an existing one; drop */
336 } else if (!mmatch(ban, lp->value.ban.banstr))
337 lp->flags |= CHFL_BAN_OVERLAPPED; /* remove overlapping ban */
343 if (ban) { /* add the new ban to the end of the list */
344 /* Build ban buffer */
346 banstr[banpos++] = ' ';
347 banstr[banpos++] = ':';
348 banstr[banpos++] = '%';
350 banstr[banpos++] = ' ';
351 for (ptr = ban; *ptr; ptr++) /* add ban to buffer */
352 banstr[banpos++] = *ptr;
354 newban = make_link(); /* create new ban */
356 DupString(newban->value.ban.banstr, ban);
358 DupString(newban->value.ban.who,
359 cli_name(feature_bool(FEAT_HIS_BANWHO) ? &me : sptr));
360 newban->value.ban.when = TStime();
362 newban->flags = CHFL_BAN | CHFL_BURST_BAN; /* set flags */
363 if ((ptr = strrchr(ban, '@')) && check_if_ipmask(ptr + 1))
364 newban->flags |= CHFL_BAN_IPMASK;
368 lp->next = newban; /* link it in */
370 chptr->banlist = newban;
374 param++; /* look at next param */
377 default: /* parameter contains clients */
379 struct Client *acptr;
380 char *nicklist = parv[param], *p = 0, *nick, *ptr;
381 int current_mode, last_mode, base_mode;
382 int oplevel = -1; /* Mark first field with digits: means the same as 'o' (but with level). */
383 int last_oplevel = 0;
384 struct Membership* member;
386 base_mode = CHFL_DEOPPED | CHFL_BURST_JOINED;
387 if (chptr->mode.mode & MODE_DELJOINS)
388 base_mode |= CHFL_DELAYED;
389 current_mode = last_mode = base_mode;
391 for (nick = ircd_strtok(&p, nicklist, ","); nick;
392 nick = ircd_strtok(&p, 0, ",")) {
394 if ((ptr = strchr(nick, ':'))) { /* new flags; deal */
397 if (parse_flags & MODE_PARSE_SET) {
398 int current_mode_needs_reset;
399 for (current_mode_needs_reset = 1; *ptr; ptr++) {
400 if (*ptr == 'o') { /* has oper status */
402 * An 'o' is pre-oplevel protocol, so this is only for
403 * backwards compatibility. Give them an op-level of
404 * MAXOPLEVEL so everyone can deop them.
406 oplevel = MAXOPLEVEL;
407 if (current_mode_needs_reset) {
408 current_mode = base_mode;
409 current_mode_needs_reset = 0;
411 current_mode = (current_mode & ~(CHFL_DEOPPED | CHFL_DELAYED)) | CHFL_CHANOP;
413 else if (*ptr == 'v') { /* has voice status */
414 if (current_mode_needs_reset) {
415 current_mode = base_mode;
416 current_mode_needs_reset = 0;
418 current_mode = (current_mode & ~CHFL_DELAYED) | CHFL_VOICE;
419 oplevel = -1; /* subsequential digits are an absolute op-level value. */
421 else if (isdigit(*ptr)) {
422 int level_increment = 0;
423 if (oplevel == -1) { /* op-level is absolute value? */
424 if (current_mode_needs_reset) {
425 current_mode = base_mode;
426 current_mode_needs_reset = 0;
430 current_mode = (current_mode & ~(CHFL_DEOPPED | CHFL_DELAYED)) | CHFL_CHANOP;
432 level_increment = 10 * level_increment + *ptr++ - '0';
433 } while(isdigit(*ptr));
434 oplevel += level_increment;
436 else /* I don't recognize that flag */
437 break; /* so stop processing */
442 if (!(acptr = findNUser(nick)) || cli_from(acptr) != cptr)
443 continue; /* ignore this client */
445 /* Build nick buffer */
446 nickstr[nickpos] = nickpos ? ',' : ' '; /* first char */
449 for (ptr = nick; *ptr; ptr++) /* store nick */
450 nickstr[nickpos++] = *ptr;
452 if (current_mode != last_mode) { /* if mode changed... */
453 last_mode = current_mode;
454 last_oplevel = oplevel;
456 nickstr[nickpos++] = ':'; /* add a specifier */
457 if (current_mode & CHFL_VOICE)
458 nickstr[nickpos++] = 'v';
459 if (current_mode & CHFL_CHANOP)
460 nickpos += ircd_snprintf(0, nickstr + nickpos, sizeof(nickstr) - nickpos, "%u", oplevel);
461 } else if (current_mode & CHFL_CHANOP && oplevel != last_oplevel) { /* if just op level changed... */
462 nickstr[nickpos++] = ':'; /* add a specifier */
463 nickpos += ircd_snprintf(0, nickstr + nickpos, sizeof(nickstr) - nickpos, "%u", oplevel - last_oplevel);
464 last_oplevel = oplevel;
467 if (IsBurst(sptr) || !(member = find_member_link(chptr, acptr)))
469 add_user_to_channel(chptr, acptr, current_mode, oplevel);
470 if (!(current_mode & CHFL_DELAYED))
471 sendcmdto_channel_butserv_butone(acptr, CMD_JOIN, chptr, NULL, 0, "%H", chptr);
475 /* The member was already joined (either by CREATE or JOIN).
476 Remember the current mode. */
477 if (member->status & CHFL_CHANOP)
478 member->status |= CHFL_BURST_ALREADY_OPPED;
479 if (member->status & CHFL_VOICE)
480 member->status |= CHFL_BURST_ALREADY_VOICED;
481 /* Synchronize with the burst. */
482 member->status |= CHFL_BURST_JOINED | (current_mode & (CHFL_CHANOP|CHFL_VOICE));
483 member->oplevel = oplevel;
489 } /* switch (*parv[param]) */
490 } /* while (param < parc) */
492 nickstr[nickpos] = '\0';
493 banstr[banpos] = '\0';
495 if (parse_flags & MODE_PARSE_SET) {
496 modebuf_extract(mbuf, modestr + 1); /* for sending BURST onward */
497 modestr[0] = modestr[1] ? ' ' : '\0';
501 sendcmdto_serv_butone(sptr, CMD_BURST, cptr, "%H %Tu%s%s%s", chptr,
502 chptr->creationtime, modestr, nickstr, banstr);
504 if (parse_flags & MODE_PARSE_WIPEOUT || banpos)
505 mode_ban_invalidate(chptr);
507 if (parse_flags & MODE_PARSE_SET) { /* any modes changed? */
508 /* first deal with channel members */
509 for (member = chptr->members; member; member = member->next_member) {
510 if (member->status & CHFL_BURST_JOINED) { /* joined during burst */
511 if ((member->status & CHFL_CHANOP) && !(member->status & CHFL_BURST_ALREADY_OPPED))
512 modebuf_mode_client(mbuf, MODE_ADD | CHFL_CHANOP, member->user);
513 if ((member->status & CHFL_VOICE) && !(member->status & CHFL_BURST_ALREADY_VOICED))
514 modebuf_mode_client(mbuf, MODE_ADD | CHFL_VOICE, member->user);
515 } else if (parse_flags & MODE_PARSE_WIPEOUT) { /* wipeout old ops */
516 if (member->status & CHFL_CHANOP)
517 modebuf_mode_client(mbuf, MODE_DEL | CHFL_CHANOP, member->user);
518 if (member->status & CHFL_VOICE)
519 modebuf_mode_client(mbuf, MODE_DEL | CHFL_VOICE, member->user);
520 member->status = ((member->status & ~(CHFL_CHANOP | CHFL_VOICE)) |
525 /* Now deal with channel bans */
526 lp_p = &chptr->banlist;
530 /* remove ban from channel */
531 if (lp->flags & (CHFL_BAN_OVERLAPPED | CHFL_BURST_BAN_WIPEOUT)) {
532 modebuf_mode_string(mbuf, MODE_DEL | MODE_BAN,
533 lp->value.ban.banstr, 1); /* let it free banstr */
535 *lp_p = lp->next; /* clip out of list */
536 MyFree(lp->value.ban.who); /* free who */
537 free_link(lp); /* free ban */
539 } else if (lp->flags & CHFL_BURST_BAN) /* add ban to channel */
540 modebuf_mode_string(mbuf, MODE_ADD | MODE_BAN,
541 lp->value.ban.banstr, 0); /* don't free banstr */
543 lp->flags &= (CHFL_BAN | CHFL_BAN_IPMASK); /* reset the flag */
544 lp_p = &(*lp_p)->next;
548 return mbuf ? modebuf_flush(mbuf) : 0;