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