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_policy.h"
90 #include "ircd_reply.h"
91 #include "ircd_string.h"
108 * ms_burst - server message handler
110 * -- by Run carlo@runaway.xs4all.nl december 1995 till march 1997
112 * parv[0] = sender prefix
113 * parv[1] = channel name
114 * parv[2] = channel timestamp
115 * The meaning of the following parv[]'s depend on their first character:
116 * If parv[n] starts with a '+':
117 * Net burst, additive modes
119 * parv[n+1] = <param> (optional)
120 * parv[n+2] = <param> (optional)
121 * If parv[n] starts with a '%', then n will be parc-1:
122 * parv[n] = %<ban> <ban> <ban> ...
123 * If parv[n] starts with another character:
124 * parv[n] = <nick>[:<mode>],<nick>[:<mode>],...
125 * where <mode> is the channel mode (ov) of nick and all following nicks.
128 * "S BURST #channel 87654321 +ntkl key 123 AAA,AAB:o,BAA,BAB:ov :%ban1 ban2"
130 * Anti net.ride code.
132 * When the channel already exist, and its TS is larger then
133 * the TS in the BURST message, then we cancel all existing modes.
134 * If its is smaller then the received BURST message is ignored.
135 * If it's equal, then the received modes are just added.
137 int ms_burst(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
139 struct ModeBuf modebuf, *mbuf = 0;
140 struct Channel *chptr;
142 struct Membership *member;
143 struct SLink *lp, **lp_p;
144 unsigned int parse_flags = (MODE_PARSE_FORCE | MODE_PARSE_BURST);
145 int param, nickpos = 0, banpos = 0;
146 char modestr[BUFSIZE], nickstr[BUFSIZE], banstr[BUFSIZE];
149 return protocol_violation(sptr,"Too few parameters for BURST");
151 if (!IsBurst(sptr)) /* don't allow BURST outside of burst */
152 return exit_client_msg(cptr, cptr, &me, "HACK: BURST message outside "
153 "net.burst from %s", cli_name(sptr));
155 if (!(chptr = get_channel(sptr, parv[1], CGT_CREATE)))
156 return 0; /* can't create the channel? */
158 timestamp = atoi(parv[2]);
160 /* turn off burst joined flag */
161 for (member = chptr->members; member; member = member->next_member)
162 member->status &= ~CHFL_BURST_JOINED;
164 if (!chptr->creationtime) /* mark channel as created during BURST */
165 chptr->mode.mode |= MODE_BURSTADDED;
167 /* new channel or an older one */
168 if (!chptr->creationtime || chptr->creationtime > timestamp) {
169 chptr->creationtime = timestamp;
171 modebuf_init(mbuf = &modebuf, &me, cptr, chptr, MODEBUF_DEST_CHANNEL);
172 modebuf_mode(mbuf, MODE_DEL | chptr->mode.mode); /* wipeout modes */
173 chptr->mode.mode &= ~(MODE_ADD | MODE_DEL | MODE_PRIVATE | MODE_SECRET |
174 MODE_MODERATED | MODE_TOPICLIMIT | MODE_INVITEONLY |
177 parse_flags |= (MODE_PARSE_SET | MODE_PARSE_WIPEOUT); /* wipeout keys */
179 /* mark bans for wipeout */
180 for (lp = chptr->banlist; lp; lp = lp->next)
181 lp->flags |= CHFL_BURST_BAN_WIPEOUT;
182 } else if (chptr->creationtime == timestamp) {
183 modebuf_init(mbuf = &modebuf, &me, cptr, chptr, MODEBUF_DEST_CHANNEL);
185 parse_flags |= MODE_PARSE_SET; /* set new modes */
188 param = 3; /* parse parameters */
189 while (param < parc) {
190 switch (*parv[param]) {
191 case '+': /* parameter introduces a mode string */
192 param += mode_parse(mbuf, cptr, sptr, chptr, parc - param,
193 parv + param, parse_flags);
196 case '%': /* parameter contains bans */
197 if (parse_flags & MODE_PARSE_SET) {
198 char *banlist = parv[param] + 1, *p = 0, *ban, *ptr;
199 struct SLink *newban;
201 for (ban = ircd_strtok(&p, banlist, " "); ban;
202 ban = ircd_strtok(&p, 0, " ")) {
203 ban = collapse(pretty_mask(ban));
206 * Yeah, we should probably do this elsewhere, and make it better
207 * and more general; this will hold until we get there, though.
208 * I dislike the current add_banid API... -Kev
210 * I wish there were a better algo. for this than the n^2 one
213 for (lp = chptr->banlist; lp; lp = lp->next) {
214 if (!ircd_strcmp(lp->value.ban.banstr, ban)) {
215 ban = 0; /* don't add ban */
216 lp->flags &= ~CHFL_BURST_BAN_WIPEOUT; /* not wiping out */
217 break; /* new ban already existed; don't even repropagate */
218 } else if (!(lp->flags & CHFL_BURST_BAN_WIPEOUT) &&
219 !mmatch(lp->value.ban.banstr, ban)) {
220 ban = 0; /* don't add ban unless wiping out bans */
221 break; /* new ban is encompassed by an existing one; drop */
222 } else if (!mmatch(ban, lp->value.ban.banstr))
223 lp->flags |= CHFL_BAN_OVERLAPPED; /* remove overlapping ban */
229 if (ban) { /* add the new ban to the end of the list */
230 /* Build ban buffer */
232 banstr[banpos++] = ' ';
233 banstr[banpos++] = ':';
234 banstr[banpos++] = '%';
236 banstr[banpos++] = ' ';
237 for (ptr = ban; *ptr; ptr++) /* add ban to buffer */
238 banstr[banpos++] = *ptr;
240 newban = make_link(); /* create new ban */
242 DupString(newban->value.ban.banstr, ban);
243 #ifdef HEAD_IN_SAND_BANWHO
244 DupString(newban->value.ban.who, cli_name(&me));
246 DupString(newban->value.ban.who, cli_name(sptr));
248 newban->value.ban.when = TStime();
250 newban->flags = CHFL_BAN | CHFL_BURST_BAN; /* set flags */
251 if ((ptr = strrchr(ban, '@')) && check_if_ipmask(ptr + 1))
252 newban->flags |= CHFL_BAN_IPMASK;
256 lp->next = newban; /* link it in */
258 chptr->banlist = newban;
262 param++; /* look at next param */
265 default: /* parameter contains clients */
267 struct Client *acptr;
268 char *nicklist = parv[param], *p = 0, *nick, *ptr;
269 int default_mode = CHFL_DEOPPED | CHFL_BURST_JOINED;
270 int last_mode = CHFL_DEOPPED | CHFL_BURST_JOINED;
272 for (nick = ircd_strtok(&p, nicklist, ","); nick;
273 nick = ircd_strtok(&p, 0, ",")) {
275 if ((ptr = strchr(nick, ':'))) { /* new flags; deal */
278 if (parse_flags & MODE_PARSE_SET) {
279 for (default_mode = CHFL_DEOPPED | CHFL_BURST_JOINED; *ptr;
281 if (*ptr == 'o') /* has oper status */
282 default_mode = (default_mode & ~CHFL_DEOPPED) | CHFL_CHANOP;
283 else if (*ptr == 'v') /* has voice status */
284 default_mode |= CHFL_VOICE;
285 else /* I don't recognize that flag */
286 break; /* so stop processing */
291 if (!(acptr = findNUser(nick)) || cli_from(acptr) != cptr)
292 continue; /* ignore this client */
294 /* Build nick buffer */
295 nickstr[nickpos] = nickpos ? ',' : ' '; /* first char */
298 for (ptr = nick; *ptr; ptr++) /* store nick */
299 nickstr[nickpos++] = *ptr;
301 if (default_mode != last_mode) { /* if mode changed... */
302 last_mode = default_mode;
304 nickstr[nickpos++] = ':'; /* add a specifier */
305 if (default_mode & CHFL_CHANOP)
306 nickstr[nickpos++] = 'o';
307 if (default_mode & CHFL_VOICE)
308 nickstr[nickpos++] = 'v';
311 add_user_to_channel(chptr, acptr, default_mode);
312 sendcmdto_channel_butserv_butone(acptr, CMD_JOIN, chptr, NULL, "%H", chptr);
317 } /* switch (*parv[param]) { */
318 } /* while (param < parc) { */
320 nickstr[nickpos] = '\0';
321 banstr[banpos] = '\0';
323 if (parse_flags & MODE_PARSE_SET) {
324 modebuf_extract(mbuf, modestr + 1); /* for sending BURST onward */
325 modestr[0] = modestr[1] ? ' ' : '\0';
329 sendcmdto_serv_butone(sptr, CMD_BURST, cptr, "%H %Tu%s%s%s", chptr,
330 chptr->creationtime, modestr, nickstr, banstr);
332 if (parse_flags & MODE_PARSE_WIPEOUT || banpos)
333 mode_ban_invalidate(chptr);
335 if (parse_flags & MODE_PARSE_SET) { /* any modes changed? */
336 /* first deal with channel members */
337 for (member = chptr->members; member; member = member->next_member) {
338 if (member->status & CHFL_BURST_JOINED) { /* joined during burst */
339 if (member->status & CHFL_CHANOP)
340 modebuf_mode_client(mbuf, MODE_ADD | CHFL_CHANOP, member->user);
341 if (member->status & CHFL_VOICE)
342 modebuf_mode_client(mbuf, MODE_ADD | CHFL_VOICE, member->user);
343 } else if (parse_flags & MODE_PARSE_WIPEOUT) { /* wipeout old ops */
344 if (member->status & CHFL_CHANOP)
345 modebuf_mode_client(mbuf, MODE_DEL | CHFL_CHANOP, member->user);
346 if (member->status & CHFL_VOICE)
347 modebuf_mode_client(mbuf, MODE_DEL | CHFL_VOICE, member->user);
348 member->status = ((member->status & ~(CHFL_CHANOP | CHFL_VOICE)) |
353 /* Now deal with channel bans */
354 lp_p = &chptr->banlist;
358 /* remove ban from channel */
359 if (lp->flags & (CHFL_BAN_OVERLAPPED | CHFL_BURST_BAN_WIPEOUT)) {
360 modebuf_mode_string(mbuf, MODE_DEL | MODE_BAN,
361 lp->value.ban.banstr, 1); /* let it free banstr */
363 *lp_p = lp->next; /* clip out of list */
364 MyFree(lp->value.ban.who); /* free who */
365 free_link(lp); /* free ban */
367 } else if (lp->flags & CHFL_BURST_BAN) /* add ban to channel */
368 modebuf_mode_string(mbuf, MODE_ADD | MODE_BAN,
369 lp->value.ban.banstr, 0); /* don't free banstr */
371 lp->flags &= (CHFL_BAN | CHFL_BAN_IPMASK); /* reset the flag */
372 lp_p = &(*lp_p)->next;
376 return mbuf ? modebuf_flush(mbuf) : 0;