Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / m_burst.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_burst.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  *
6  * See file AUTHORS in IRC package for additional names of
7  * the programmers.
8  *
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)
12  * any later version.
13  *
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.
18  *
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.
22  *
23  * $Id$
24  */
25
26 /*
27  * m_functions execute protocol messages on this server:
28  *
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...).
34  *
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.
38  *
39  *            (!IsServer(cptr)) => (cptr == sptr), because
40  *            prefixes are taken *only* from servers...
41  *
42  *            (IsServer(cptr))
43  *                    (sptr == cptr) => the message didn't
44  *                    have the prefix.
45  *
46  *                    (sptr != cptr && IsServer(sptr) means
47  *                    the prefix specified servername. (?)
48  *
49  *                    (sptr != cptr && !IsServer(sptr) means
50  *                    that message originated from a remote
51  *                    user (not local).
52  *
53  *            combining
54  *
55  *            (!IsServer(sptr)) means that, sptr can safely
56  *            taken as defining the target structure of the
57  *            message in this server.
58  *
59  *    *Always* true (if 'parse' and others are working correct):
60  *
61  *    1)      sptr->from == cptr  (note: cptr->from == cptr)
62  *
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 ]
67  *
68  *    parc    number of variable parameter strings (if zero,
69  *            parv is allowed to be NULL)
70  *
71  *    parv    a NULL terminated list of parameter pointers,
72  *
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*
78  *
79  *            note:   it is guaranteed that parv[0]..parv[parc-1] are all
80  *                    non-NULL pointers.
81  */
82 #if 0
83 /*
84  * No need to include handlers.h here the signatures must match
85  * and we don't need to force a rebuild of all the handlers everytime
86  * we add a new one to the list. --Bleep
87  */
88 #include "handlers.h"
89 #endif /* 0 */
90 #include "channel.h"
91 #include "client.h"
92 #include "hash.h"
93 #include "ircd.h"
94 #include "ircd_alloc.h"
95 #include "ircd_reply.h"
96 #include "ircd_string.h"
97 #include "list.h"
98 #include "match.h"
99 #include "msg.h"
100 #include "numeric.h"
101 #include "numnicks.h"
102 #include "s_conf.h"
103 #include "s_misc.h"
104 #include "send.h"
105 #include "struct.h"
106 #include "support.h"
107
108 #include <assert.h>
109 #include <stdlib.h>
110 #include <string.h>
111
112 /*
113  * ms_burst - server message handler
114  *
115  * --  by Run carlo@runaway.xs4all.nl  december 1995 till march 1997
116  *
117  * parv[0] = sender prefix
118  * parv[1] = channel name
119  * parv[2] = channel timestamp
120  * The meaning of the following parv[]'s depend on their first character:
121  * If parv[n] starts with a '+':
122  * Net burst, additive modes
123  *   parv[n] = <mode>
124  *   parv[n+1] = <param> (optional)
125  *   parv[n+2] = <param> (optional)
126  * If parv[n] starts with a '%', then n will be parc-1:
127  *   parv[n] = %<ban> <ban> <ban> ...
128  * If parv[n] starts with another character:
129  *   parv[n] = <nick>[:<mode>],<nick>[:<mode>],...
130  *   where <mode> is the channel mode (ov) of nick and all following nicks.
131  *
132  * Example:
133  * "S BURST #channel 87654321 +ntkl key 123 AAA,AAB:o,BAA,BAB:ov :%ban1 ban2"
134  *
135  * Anti net.ride code.
136  *
137  * When the channel already exist, and its TS is larger then
138  * the TS in the BURST message, then we cancel all existing modes.
139  * If its is smaller then the received BURST message is ignored.
140  * If it's equal, then the received modes are just added.
141  */
142 int ms_burst(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
143 {
144   struct ModeBuf modebuf, *mbuf = 0;
145   struct Channel *chptr;
146   time_t timestamp;
147   struct Membership *member;
148   struct SLink *lp, **lp_p;
149   unsigned int parse_flags = (MODE_PARSE_FORCE | MODE_PARSE_BURST);
150   int param, nickpos = 0, banpos = 0;
151   char modestr[BUFSIZE], nickstr[BUFSIZE], banstr[BUFSIZE];
152
153   if (parc < 4)
154     return protocol_violation(sptr,"Too few parameters for BURST");
155
156   if (!IsBurst(sptr)) /* don't allow BURST outside of burst */
157     return exit_client_msg(cptr, cptr, &me, "HACK: BURST message outside "
158                            "net.burst from %s", cli_name(sptr));
159
160   if (!(chptr = get_channel(sptr, parv[1], CGT_CREATE)))
161     return 0; /* can't create the channel? */
162
163   timestamp = atoi(parv[2]);
164
165   /* turn off burst joined flag */
166   for (member = chptr->members; member; member = member->next_member)
167     member->status &= ~CHFL_BURST_JOINED;
168
169   if (!chptr->creationtime) /* mark channel as created during BURST */
170     chptr->mode.mode |= MODE_BURSTADDED;
171
172   /* new channel or an older one */
173   if (!chptr->creationtime || chptr->creationtime > timestamp) {
174     chptr->creationtime = timestamp;
175
176     modebuf_init(mbuf = &modebuf, sptr, cptr, chptr, MODEBUF_DEST_CHANNEL);
177     modebuf_mode(mbuf, MODE_DEL | chptr->mode.mode); /* wipeout modes */
178     chptr->mode.mode &= ~(MODE_ADD | MODE_DEL | MODE_PRIVATE | MODE_SECRET |
179                           MODE_MODERATED | MODE_TOPICLIMIT | MODE_INVITEONLY |
180                           MODE_NOPRIVMSGS);
181
182     parse_flags |= (MODE_PARSE_SET | MODE_PARSE_WIPEOUT); /* wipeout keys */
183
184     /* mark bans for wipeout */
185     for (lp = chptr->banlist; lp; lp = lp->next)
186       lp->flags |= CHFL_BURST_BAN_WIPEOUT;
187   } else if (chptr->creationtime == timestamp) {
188     modebuf_init(mbuf = &modebuf, sptr, cptr, chptr, MODEBUF_DEST_CHANNEL);
189
190     parse_flags |= MODE_PARSE_SET; /* set new modes */
191   }
192
193   param = 3; /* parse parameters */
194   while (param < parc) {
195     switch (*parv[param]) {
196     case '+': /* parameter introduces a mode string */
197       param += mode_parse(mbuf, cptr, sptr, chptr, parc - param,
198                           parv + param, parse_flags);
199       break;
200
201     case '%': /* parameter contains bans */
202       if (parse_flags & MODE_PARSE_SET) {
203         char *banlist = parv[param] + 1, *p = 0, *ban, *ptr;
204         struct SLink *newban;
205
206         for (ban = ircd_strtok(&p, banlist, " "); ban;
207              ban = ircd_strtok(&p, 0, " ")) {
208           ban = collapse(pretty_mask(ban));
209
210             /*
211              * Yeah, we should probably do this elsewhere, and make it better
212              * and more general; this will hold until we get there, though.
213              * I dislike the current add_banid API... -Kev
214              *
215              * I wish there were a better algo. for this than the n^2 one
216              * shown below *sigh*
217              */
218           for (lp = chptr->banlist; lp; lp = lp->next) {
219             if (!ircd_strcmp(lp->value.ban.banstr, ban)) {
220               ban = 0; /* don't add ban */
221               lp->flags &= ~CHFL_BURST_BAN_WIPEOUT; /* not wiping out */
222               break; /* new ban already existed; don't even repropagate */
223             } else if (!(lp->flags & CHFL_BURST_BAN_WIPEOUT) &&
224                        !mmatch(lp->value.ban.banstr, ban)) {
225               ban = 0; /* don't add ban unless wiping out bans */
226               break; /* new ban is encompassed by an existing one; drop */
227             } else if (!mmatch(ban, lp->value.ban.banstr))
228               lp->flags |= CHFL_BAN_OVERLAPPED; /* remove overlapping ban */
229
230             if (!lp->next)
231               break;
232           }
233
234           if (ban) { /* add the new ban to the end of the list */
235             /* Build ban buffer */
236             if (!banpos) {
237               banstr[banpos++] = ' ';
238               banstr[banpos++] = ':';
239               banstr[banpos++] = '%';
240             } else
241               banstr[banpos++] = ' ';
242             for (ptr = ban; *ptr; ptr++) /* add ban to buffer */
243               banstr[banpos++] = *ptr;
244
245             newban = make_link(); /* create new ban */
246
247             DupString(newban->value.ban.banstr, ban);
248             DupString(newban->value.ban.who, cli_name(sptr));
249             newban->value.ban.when = TStime();
250
251             newban->flags = CHFL_BAN | CHFL_BURST_BAN; /* set flags */
252             if ((ptr = strrchr(ban, '@')) && check_if_ipmask(ptr + 1))
253               newban->flags |= CHFL_BAN_IPMASK;
254
255             newban->next = 0;
256             if (lp)
257               lp->next = newban; /* link it in */
258             else
259               chptr->banlist = newban;
260           }
261         }
262       } 
263       param++; /* look at next param */
264       break;
265
266     default: /* parameter contains clients */
267       {
268         struct Client *acptr;
269         char *nicklist = parv[param], *p = 0, *nick, *ptr;
270         int default_mode = CHFL_DEOPPED | CHFL_BURST_JOINED;
271         int last_mode = CHFL_DEOPPED | CHFL_BURST_JOINED;
272
273         for (nick = ircd_strtok(&p, nicklist, ","); nick;
274              nick = ircd_strtok(&p, 0, ",")) {
275
276           if ((ptr = strchr(nick, ':'))) { /* new flags; deal */
277             *ptr++ = '\0';
278
279             if (parse_flags & MODE_PARSE_SET) {
280               for (default_mode = CHFL_DEOPPED | CHFL_BURST_JOINED; *ptr;
281                    ptr++) {
282                 if (*ptr == 'o') /* has oper status */
283                   default_mode = (default_mode & ~CHFL_DEOPPED) | CHFL_CHANOP;
284                 else if (*ptr == 'v') /* has voice status */
285                   default_mode |= CHFL_VOICE;
286                 else /* I don't recognize that flag */
287                   break; /* so stop processing */
288               }
289             }
290           }
291
292           if (!(acptr = findNUser(nick)) || cli_from(acptr) != cptr)
293             continue; /* ignore this client */
294
295           /* Build nick buffer */
296           nickstr[nickpos] = nickpos ? ',' : ' '; /* first char */
297           nickpos++;
298
299           for (ptr = nick; *ptr; ptr++) /* store nick */
300             nickstr[nickpos++] = *ptr;
301
302           if (default_mode != last_mode) { /* if mode changed... */
303             last_mode = default_mode;
304
305             nickstr[nickpos++] = ':'; /* add a specifier */
306             if (default_mode & CHFL_CHANOP)
307               nickstr[nickpos++] = 'o';
308             if (default_mode & CHFL_VOICE)
309               nickstr[nickpos++] = 'v';
310           }
311
312           add_user_to_channel(chptr, acptr, default_mode);
313           sendcmdto_channel_butserv(acptr, CMD_JOIN, chptr, "%H", chptr);
314         }
315       }
316       param++;
317       break;
318     } /* switch (*parv[param]) { */
319   } /* while (param < parc) { */
320
321   nickstr[nickpos] = '\0';
322   banstr[banpos] = '\0';
323
324   if (parse_flags & MODE_PARSE_SET) {
325     modebuf_extract(mbuf, modestr + 1); /* for sending BURST onward */
326     modestr[0] = modestr[1] ? ' ' : '\0';
327   } else
328     modestr[0] = '\0';
329
330   sendcmdto_serv_butone(sptr, CMD_BURST, cptr, "%H %Tu%s%s%s", chptr,
331                         chptr->creationtime, modestr, nickstr, banstr);
332
333   if (parse_flags & MODE_PARSE_WIPEOUT || banpos)
334     mode_ban_invalidate(chptr);
335
336   if (parse_flags & MODE_PARSE_SET) { /* any modes changed? */
337     /* first deal with channel members */
338     for (member = chptr->members; member; member = member->next_member) {
339       if (member->status & CHFL_BURST_JOINED) { /* joined during burst */
340         if (member->status & CHFL_CHANOP)
341           modebuf_mode_client(mbuf, MODE_ADD | CHFL_CHANOP, member->user);
342         if (member->status & CHFL_VOICE)
343           modebuf_mode_client(mbuf, MODE_ADD | CHFL_VOICE, member->user);
344       } else if (parse_flags & MODE_PARSE_WIPEOUT) { /* wipeout old ops */
345         if (member->status & CHFL_CHANOP)
346           modebuf_mode_client(mbuf, MODE_DEL | CHFL_CHANOP, member->user);
347         if (member->status & CHFL_VOICE)
348           modebuf_mode_client(mbuf, MODE_DEL | CHFL_VOICE, member->user);
349         member->status = ((member->status & ~(CHFL_CHANOP | CHFL_VOICE)) |
350                           CHFL_DEOPPED);
351       }
352     }
353
354     /* Now deal with channel bans */
355     lp_p = &chptr->banlist;
356     while (*lp_p) {
357       lp = *lp_p;
358
359       /* remove ban from channel */
360       if (lp->flags & (CHFL_BAN_OVERLAPPED | CHFL_BURST_BAN_WIPEOUT)) {
361         modebuf_mode_string(mbuf, MODE_DEL | MODE_BAN,
362                             lp->value.ban.banstr, 1); /* let it free banstr */
363
364         *lp_p = lp->next; /* clip out of list */
365         MyFree(lp->value.ban.who); /* free who */
366         free_link(lp); /* free ban */
367         continue;
368       } else if (lp->flags & CHFL_BURST_BAN) /* add ban to channel */
369         modebuf_mode_string(mbuf, MODE_ADD | MODE_BAN,
370                             lp->value.ban.banstr, 0); /* don't free banstr */
371
372       lp->flags &= (CHFL_BAN | CHFL_BAN_IPMASK); /* reset the flag */
373       lp_p = &(*lp_p)->next;
374     }
375   }
376
377   return mbuf ? modebuf_flush(mbuf) : 0;
378 }