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"
102 #include "ircd_snprintf.h"
110 * ms_burst - server message handler
112 * -- by Run carlo@runaway.xs4all.nl december 1995 till march 1997
114 * parv[0] = sender prefix
115 * parv[1] = channel name
116 * parv[2] = channel timestamp
117 * The meaning of the following parv[]'s depend on their first character:
118 * If parv[n] starts with a '+':
119 * Net burst, additive modes
121 * parv[n+1] = <param> (optional)
122 * parv[n+2] = <param> (optional)
123 * If parv[n] starts with a '%', then n will be parc-1:
124 * parv[n] = %<ban> <ban> <ban> ...
125 * If parv[n] starts with another character:
126 * parv[n] = <nick>[:<mode>],<nick>[:<mode>],...
127 * where <mode> defines the mode and op-level
128 * for nick and all following nicks until the
130 * Digits in the <mode> field have of two meanings:
131 * 1) if it is the first field in this BURST message
132 * that contains digits, and/or when a 'v' is
133 * present in the <mode>:
134 * The absolute value of the op-level.
135 * 2) if there are only digits in this field and
136 * it is not the first field with digits:
137 * An op-level increment relative to the previous
139 * First all modeless nicks must be emmitted,
140 * then all combinations of modes without ops
141 * (currently that is only 'v') followed by the same
142 * series but then with ops (currently 'o','ov').
145 * "A8 B #test 87654321 +ntkAl key secret 123 A8AAG,A8AAC:v,A8AAA:0,A8AAF:2,A8AAD,A8AAB:v1,A8AAE:1 :%ban1 ban2"
147 * <mode> list example:
149 * "xxx,sss:v,ttt,aaa:123,bbb,ccc:2,ddd,kkk:v2,lll:2,mmm"
153 * xxx // first modeless nicks
154 * sss +v // then opless nicks
155 * ttt +v // no ":<mode>": everything stays the same
156 * aaa -123 // first field with digit: absolute value
158 * ccc -125 // only digits, not first field: increment
160 * kkk -2 +v // field with a 'v': absolute value
161 * lll -4 +v // only digits: increment
164 * Anti net.ride code.
166 * When the channel already exist, and its TS is larger than
167 * the TS in the BURST message, then we cancel all existing modes.
168 * If its is smaller then the received BURST message is ignored.
169 * If it's equal, then the received modes are just added.
171 * BURST is also accepted outside a netburst now because it
172 * is sent upstream as reaction to a DESTRUCT message. For
173 * these BURST messages it is possible that the listed channel
174 * members are already joined.
176 int ms_burst(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
178 struct ModeBuf modebuf, *mbuf = 0;
179 struct Channel *chptr;
181 struct Membership *member, *nmember;
182 struct SLink *lp, **lp_p;
183 unsigned int parse_flags = (MODE_PARSE_FORCE | MODE_PARSE_BURST);
184 int param, nickpos = 0, banpos = 0;
185 char modestr[BUFSIZE], nickstr[BUFSIZE], banstr[BUFSIZE];
188 return protocol_violation(sptr,"Too few parameters for BURST");
190 if (!(chptr = get_channel(sptr, parv[1], CGT_CREATE)))
191 return 0; /* can't create the channel? */
193 timestamp = atoi(parv[2]);
195 if (!chptr->creationtime || chptr->creationtime > timestamp) {
197 * Kick local members if channel is +i or +k and our TS was larger
198 * than the burst TS (anti net.ride). The modes hack is here because
199 * we have to do this before mode_parse, as chptr may go away.
201 for (param = 3; param < parc; param++)
203 if (parv[param][0] != '+')
205 if (strchr(parv[param], 'i') || strchr(parv[param], 'k'))
207 for (member = chptr->members; member; member = nmember)
209 nmember = member->next_member;
210 if (!MyUser(member->user) || IsZombie(member))
212 sendcmdto_serv_butone(&me, CMD_KICK, NULL, "%H %C :Net Rider", chptr, member->user);
213 sendcmdto_channel_butserv_butone(&me, CMD_KICK, chptr, NULL, "%H %C :Net Rider", chptr, member->user);
214 make_zombie(member, member->user, &me, &me, chptr);
220 /* If the channel had only locals, it went away by now. */
221 if (!(chptr = get_channel(sptr, parv[1], CGT_CREATE)))
222 return 0; /* can't create the channel? */
225 /* turn off burst joined flag */
226 for (member = chptr->members; member; member = member->next_member)
227 member->status &= ~(CHFL_BURST_JOINED|CHFL_BURST_ALREADY_OPPED|CHFL_BURST_ALREADY_VOICED);
229 if (!chptr->creationtime) /* mark channel as created during BURST */
230 chptr->mode.mode |= MODE_BURSTADDED;
232 /* new channel or an older one */
233 if (!chptr->creationtime || chptr->creationtime > timestamp) {
234 chptr->creationtime = timestamp;
236 modebuf_init(mbuf = &modebuf, &me, cptr, chptr,
237 MODEBUF_DEST_CHANNEL | MODEBUF_DEST_NOKEY);
238 modebuf_mode(mbuf, MODE_DEL | chptr->mode.mode); /* wipeout modes */
239 chptr->mode.mode &= ~(MODE_ADD | MODE_DEL | MODE_PRIVATE | MODE_SECRET |
240 MODE_MODERATED | MODE_TOPICLIMIT | MODE_INVITEONLY |
243 parse_flags |= (MODE_PARSE_SET | MODE_PARSE_WIPEOUT); /* wipeout keys */
245 /* mark bans for wipeout */
246 for (lp = chptr->banlist; lp; lp = lp->next)
247 lp->flags |= CHFL_BURST_BAN_WIPEOUT;
248 } else if (chptr->creationtime == timestamp) {
249 modebuf_init(mbuf = &modebuf, &me, cptr, chptr,
250 MODEBUF_DEST_CHANNEL | MODEBUF_DEST_NOKEY);
252 parse_flags |= MODE_PARSE_SET; /* set new modes */
255 param = 3; /* parse parameters */
256 while (param < parc) {
257 switch (*parv[param]) {
258 case '+': /* parameter introduces a mode string */
259 param += mode_parse(mbuf, cptr, sptr, chptr, parc - param,
260 parv + param, parse_flags, NULL);
263 case '%': /* parameter contains bans */
264 if (parse_flags & MODE_PARSE_SET) {
265 char *banlist = parv[param] + 1, *p = 0, *ban, *ptr;
266 struct SLink *newban;
268 for (ban = ircd_strtok(&p, banlist, " "); ban;
269 ban = ircd_strtok(&p, 0, " ")) {
270 ban = collapse(pretty_mask(ban));
273 * Yeah, we should probably do this elsewhere, and make it better
274 * and more general; this will hold until we get there, though.
275 * I dislike the current add_banid API... -Kev
277 * I wish there were a better algo. for this than the n^2 one
280 for (lp = chptr->banlist; lp; lp = lp->next) {
281 if (!ircd_strcmp(lp->value.ban.banstr, ban)) {
282 ban = 0; /* don't add ban */
283 lp->flags &= ~CHFL_BURST_BAN_WIPEOUT; /* not wiping out */
284 break; /* new ban already existed; don't even repropagate */
285 } else if (!(lp->flags & CHFL_BURST_BAN_WIPEOUT) &&
286 !mmatch(lp->value.ban.banstr, ban)) {
287 ban = 0; /* don't add ban unless wiping out bans */
288 break; /* new ban is encompassed by an existing one; drop */
289 } else if (!mmatch(ban, lp->value.ban.banstr))
290 lp->flags |= CHFL_BAN_OVERLAPPED; /* remove overlapping ban */
296 if (ban) { /* add the new ban to the end of the list */
297 /* Build ban buffer */
299 banstr[banpos++] = ' ';
300 banstr[banpos++] = ':';
301 banstr[banpos++] = '%';
303 banstr[banpos++] = ' ';
304 for (ptr = ban; *ptr; ptr++) /* add ban to buffer */
305 banstr[banpos++] = *ptr;
307 newban = make_link(); /* create new ban */
309 DupString(newban->value.ban.banstr, ban);
311 DupString(newban->value.ban.who,
312 cli_name(feature_bool(FEAT_HIS_BANWHO) ? &me : sptr));
313 newban->value.ban.when = TStime();
315 newban->flags = CHFL_BAN | CHFL_BURST_BAN; /* set flags */
316 if ((ptr = strrchr(ban, '@')) && check_if_ipmask(ptr + 1))
317 newban->flags |= CHFL_BAN_IPMASK;
321 lp->next = newban; /* link it in */
323 chptr->banlist = newban;
327 param++; /* look at next param */
330 default: /* parameter contains clients */
332 struct Client *acptr;
333 char *nicklist = parv[param], *p = 0, *nick, *ptr;
334 int current_mode = CHFL_DEOPPED | CHFL_BURST_JOINED;
335 int last_mode = CHFL_DEOPPED | CHFL_BURST_JOINED;
336 int oplevel = -1; /* Mark first field with digits: means the same as 'o' (but with level). */
337 int last_oplevel = 0;
338 struct Membership* member;
340 for (nick = ircd_strtok(&p, nicklist, ","); nick;
341 nick = ircd_strtok(&p, 0, ",")) {
343 if ((ptr = strchr(nick, ':'))) { /* new flags; deal */
346 if (parse_flags & MODE_PARSE_SET) {
347 int current_mode_needs_reset;
348 for (current_mode_needs_reset = 1; *ptr; ptr++) {
349 if (*ptr == 'o') { /* has oper status */
351 * An 'o' is pre-oplevel protocol, so this is only for
352 * backwards compatibility. Give them an op-level of
353 * MAXOPLEVEL so everyone can deop them.
355 oplevel = MAXOPLEVEL;
356 if (current_mode_needs_reset) {
357 current_mode = CHFL_DEOPPED | CHFL_BURST_JOINED;
358 current_mode_needs_reset = 0;
360 current_mode = (current_mode & ~CHFL_DEOPPED) | CHFL_CHANOP;
362 else if (*ptr == 'v') { /* has voice status */
363 if (current_mode_needs_reset) {
364 current_mode = CHFL_DEOPPED | CHFL_BURST_JOINED;
365 current_mode_needs_reset = 0;
367 current_mode |= CHFL_VOICE;
368 oplevel = -1; /* subsequential digits are an absolute op-level value. */
370 else if (isdigit(*ptr)) {
371 int level_increment = 0;
372 if (oplevel == -1) { /* op-level is absolute value? */
373 if (current_mode_needs_reset) {
374 current_mode = CHFL_DEOPPED | CHFL_BURST_JOINED;
375 current_mode_needs_reset = 0;
379 current_mode = (current_mode & ~CHFL_DEOPPED) | CHFL_CHANOP;
381 level_increment = 10 * level_increment + *ptr++ - '0';
382 } while(isdigit(*ptr));
383 oplevel += level_increment;
385 else /* I don't recognize that flag */
386 break; /* so stop processing */
391 if (!(acptr = findNUser(nick)) || cli_from(acptr) != cptr)
392 continue; /* ignore this client */
394 /* Build nick buffer */
395 nickstr[nickpos] = nickpos ? ',' : ' '; /* first char */
398 for (ptr = nick; *ptr; ptr++) /* store nick */
399 nickstr[nickpos++] = *ptr;
401 if (current_mode != last_mode) { /* if mode changed... */
402 last_mode = current_mode;
403 last_oplevel = oplevel;
405 nickstr[nickpos++] = ':'; /* add a specifier */
406 if (current_mode & CHFL_VOICE)
407 nickstr[nickpos++] = 'v';
408 if (current_mode & CHFL_CHANOP)
409 nickpos += ircd_snprintf(0, nickstr + nickpos, sizeof(nickstr) - nickpos, "%u", oplevel);
410 } else if (current_mode & CHFL_CHANOP && oplevel != last_oplevel) { /* if just op level changed... */
411 nickstr[nickpos++] = ':'; /* add a specifier */
412 nickpos += ircd_snprintf(0, nickstr + nickpos, sizeof(nickstr) - nickpos, "%u", oplevel - last_oplevel);
413 last_oplevel = oplevel;
416 if (IsBurst(sptr) || !(member = find_member_link(chptr, acptr)))
418 add_user_to_channel(chptr, acptr, current_mode, oplevel);
419 sendcmdto_channel_butserv_butone(acptr, CMD_JOIN, chptr, NULL, "%H", chptr);
423 /* The member was already joined (either by CREATE or JOIN).
424 Remember the current mode. */
425 if (member->status & CHFL_CHANOP)
426 member->status |= CHFL_BURST_ALREADY_OPPED;
427 if (member->status & CHFL_VOICE)
428 member->status |= CHFL_BURST_ALREADY_VOICED;
429 /* Synchronize with the burst. */
430 member->status |= CHFL_BURST_JOINED | (current_mode & (CHFL_CHANOP|CHFL_VOICE));
431 member->oplevel = oplevel;
437 } /* switch (*parv[param]) */
438 } /* while (param < parc) */
440 nickstr[nickpos] = '\0';
441 banstr[banpos] = '\0';
443 if (parse_flags & MODE_PARSE_SET) {
444 modebuf_extract(mbuf, modestr + 1); /* for sending BURST onward */
445 modestr[0] = modestr[1] ? ' ' : '\0';
449 sendcmdto_serv_butone(sptr, CMD_BURST, cptr, "%H %Tu%s%s%s", chptr,
450 chptr->creationtime, modestr, nickstr, banstr);
452 if (parse_flags & MODE_PARSE_WIPEOUT || banpos)
453 mode_ban_invalidate(chptr);
455 if (parse_flags & MODE_PARSE_SET) { /* any modes changed? */
456 /* first deal with channel members */
457 for (member = chptr->members; member; member = member->next_member) {
458 if (member->status & CHFL_BURST_JOINED) { /* joined during burst */
459 if ((member->status & CHFL_CHANOP) && !(member->status & CHFL_BURST_ALREADY_OPPED))
460 modebuf_mode_client(mbuf, MODE_ADD | CHFL_CHANOP, member->user);
461 if ((member->status & CHFL_VOICE) && !(member->status & CHFL_BURST_ALREADY_VOICED))
462 modebuf_mode_client(mbuf, MODE_ADD | CHFL_VOICE, member->user);
463 } else if (parse_flags & MODE_PARSE_WIPEOUT) { /* wipeout old ops */
464 if (member->status & CHFL_CHANOP)
465 modebuf_mode_client(mbuf, MODE_DEL | CHFL_CHANOP, member->user);
466 if (member->status & CHFL_VOICE)
467 modebuf_mode_client(mbuf, MODE_DEL | CHFL_VOICE, member->user);
468 member->status = ((member->status & ~(CHFL_CHANOP | CHFL_VOICE)) |
473 /* Now deal with channel bans */
474 lp_p = &chptr->banlist;
478 /* remove ban from channel */
479 if (lp->flags & (CHFL_BAN_OVERLAPPED | CHFL_BURST_BAN_WIPEOUT)) {
480 modebuf_mode_string(mbuf, MODE_DEL | MODE_BAN,
481 lp->value.ban.banstr, 1); /* let it free banstr */
483 *lp_p = lp->next; /* clip out of list */
484 MyFree(lp->value.ban.who); /* free who */
485 free_link(lp); /* free ban */
487 } else if (lp->flags & CHFL_BURST_BAN) /* add ban to channel */
488 modebuf_mode_string(mbuf, MODE_ADD | MODE_BAN,
489 lp->value.ban.banstr, 0); /* don't free banstr */
491 lp->flags &= (CHFL_BAN | CHFL_BAN_IPMASK); /* reset the flag */
492 lp_p = &(*lp_p)->next;
496 return mbuf ? modebuf_flush(mbuf) : 0;