Forward port change to clear invites when wiping a channel during burst
[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 #include "channel.h"
85 #include "client.h"
86 #include "hash.h"
87 #include "ircd.h"
88 #include "ircd_alloc.h"
89 #include "ircd_features.h"
90 #include "ircd_reply.h"
91 #include "ircd_string.h"
92 #include "list.h"
93 #include "match.h"
94 #include "msg.h"
95 #include "numeric.h"
96 #include "numnicks.h"
97 #include "s_conf.h"
98 #include "s_misc.h"
99 #include "send.h"
100 #include "struct.h"
101 #include "support.h"
102 #include "ircd_snprintf.h"
103
104 #include <assert.h>
105 #include <stdlib.h>
106 #include <string.h>
107 #include <ctype.h>
108
109 /*
110  * ms_burst - server message handler
111  *
112  * --  by Run carlo@runaway.xs4all.nl  december 1995 till march 1997
113  *
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
120  *   parv[n] = <mode>
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
129  *   next <mode> field.
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
138  *      op-level.
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').
143  *
144  * Example:
145  * "A8 B #test 87654321 +ntkAl key secret 123 A8AAG,A8AAC:v,A8AAA:0,A8AAF:2,A8AAD,A8AAB:v1,A8AAE:1 :%ban1 ban2"
146  *
147  * <mode> list example:
148  *
149  * "xxx,sss:v,ttt,aaa:123,bbb,ccc:2,ddd,kkk:v2,lll:2,mmm"
150  *
151  * means
152  *
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
157  *  bbb -123
158  *  ccc -125    // only digits, not first field: increment
159  *  ddd -125
160  *  kkk -2 +v   // field with a 'v': absolute value
161  *  lll -4 +v   // only digits: increment
162  *  mmm -4 +v
163  *
164  * Anti net.ride code.
165  *
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.
170  *
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.
175  */
176 int ms_burst(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
177 {
178   struct ModeBuf modebuf, *mbuf = 0;
179   struct Channel *chptr;
180   time_t timestamp;
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];
186
187   if (parc < 3)
188     return protocol_violation(sptr,"Too few parameters for BURST");
189
190   if (!(chptr = get_channel(sptr, parv[1], CGT_CREATE)))
191     return 0; /* can't create the channel? */
192
193   timestamp = atoi(parv[2]);
194
195   if (!chptr->creationtime || chptr->creationtime > timestamp) {
196     /*
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.
200      */
201     for (param = 3; param < parc; param++)
202     {
203       if (parv[param][0] != '+')
204         continue;
205       if (strchr(parv[param], 'i') || strchr(parv[param], 'k'))
206       {
207         /* Clear any outstanding rogue invites */
208         mode_invite_clear(chptr);
209         for (member = chptr->members; member; member = nmember)
210         {
211           nmember = member->next_member;
212           if (!MyUser(member->user) || IsZombie(member))
213             continue;
214           sendcmdto_serv_butone(&me, CMD_KICK, NULL, "%H %C :Net Rider", chptr, member->user);
215           sendcmdto_channel_butserv_butone(&me, CMD_KICK, chptr, NULL, "%H %C :Net Rider", chptr, member->user);
216           make_zombie(member, member->user, &me, &me, chptr);
217         }
218       }
219       break;
220     }
221
222     /* If the channel had only locals, it went away by now. */
223     if (!(chptr = get_channel(sptr, parv[1], CGT_CREATE)))
224       return 0; /* can't create the channel? */
225   }
226
227   /* turn off burst joined flag */
228   for (member = chptr->members; member; member = member->next_member)
229     member->status &= ~(CHFL_BURST_JOINED|CHFL_BURST_ALREADY_OPPED|CHFL_BURST_ALREADY_VOICED);
230
231   if (!chptr->creationtime) /* mark channel as created during BURST */
232     chptr->mode.mode |= MODE_BURSTADDED;
233
234   /* new channel or an older one */
235   if (!chptr->creationtime || chptr->creationtime > timestamp) {
236     chptr->creationtime = timestamp;
237
238     modebuf_init(mbuf = &modebuf, &me, cptr, chptr,
239                  MODEBUF_DEST_CHANNEL | MODEBUF_DEST_NOKEY);
240     modebuf_mode(mbuf, MODE_DEL | chptr->mode.mode); /* wipeout modes */
241     chptr->mode.mode &= ~(MODE_ADD | MODE_DEL | MODE_PRIVATE | MODE_SECRET |
242                           MODE_MODERATED | MODE_TOPICLIMIT | MODE_INVITEONLY |
243                           MODE_NOPRIVMSGS | MODE_DELJOINS);
244
245     parse_flags |= (MODE_PARSE_SET | MODE_PARSE_WIPEOUT); /* wipeout keys */
246
247     /* mark bans for wipeout */
248     for (lp = chptr->banlist; lp; lp = lp->next)
249       lp->flags |= CHFL_BURST_BAN_WIPEOUT;
250
251     /* clear topic set by netrider (if set) */
252     if (*chptr->topic) {
253       *chptr->topic = '\0';
254       *chptr->topic_nick = '\0';
255       chptr->topic_time = 0;
256       sendcmdto_channel_butserv_butone(&me, CMD_TOPIC, chptr, NULL,
257                                        "%H :%s", chptr, chptr->topic);
258     }
259   } else if (chptr->creationtime == timestamp) {
260     modebuf_init(mbuf = &modebuf, &me, cptr, chptr,
261                  MODEBUF_DEST_CHANNEL | MODEBUF_DEST_NOKEY);
262
263     parse_flags |= MODE_PARSE_SET; /* set new modes */
264   }
265
266   param = 3; /* parse parameters */
267   while (param < parc) {
268     switch (*parv[param]) {
269     case '+': /* parameter introduces a mode string */
270       param += mode_parse(mbuf, cptr, sptr, chptr, parc - param,
271                           parv + param, parse_flags, NULL);
272       break;
273
274     case '%': /* parameter contains bans */
275       if (parse_flags & MODE_PARSE_SET) {
276         char *banlist = parv[param] + 1, *p = 0, *ban, *ptr;
277         struct SLink *newban;
278
279         for (ban = ircd_strtok(&p, banlist, " "); ban;
280              ban = ircd_strtok(&p, 0, " ")) {
281           ban = collapse(pretty_mask(ban));
282
283             /*
284              * Yeah, we should probably do this elsewhere, and make it better
285              * and more general; this will hold until we get there, though.
286              * I dislike the current add_banid API... -Kev
287              *
288              * I wish there were a better algo. for this than the n^2 one
289              * shown below *sigh*
290              */
291           for (lp = chptr->banlist; lp; lp = lp->next) {
292             if (!ircd_strcmp(lp->value.ban.banstr, ban)) {
293               ban = 0; /* don't add ban */
294               lp->flags &= ~CHFL_BURST_BAN_WIPEOUT; /* not wiping out */
295               break; /* new ban already existed; don't even repropagate */
296             } else if (!(lp->flags & CHFL_BURST_BAN_WIPEOUT) &&
297                        !mmatch(lp->value.ban.banstr, ban)) {
298               ban = 0; /* don't add ban unless wiping out bans */
299               break; /* new ban is encompassed by an existing one; drop */
300             } else if (!mmatch(ban, lp->value.ban.banstr))
301               lp->flags |= CHFL_BAN_OVERLAPPED; /* remove overlapping ban */
302
303             if (!lp->next)
304               break;
305           }
306
307           if (ban) { /* add the new ban to the end of the list */
308             /* Build ban buffer */
309             if (!banpos) {
310               banstr[banpos++] = ' ';
311               banstr[banpos++] = ':';
312               banstr[banpos++] = '%';
313             } else
314               banstr[banpos++] = ' ';
315             for (ptr = ban; *ptr; ptr++) /* add ban to buffer */
316               banstr[banpos++] = *ptr;
317
318             newban = make_link(); /* create new ban */
319
320             DupString(newban->value.ban.banstr, ban);
321
322             DupString(newban->value.ban.who, 
323                       cli_name(feature_bool(FEAT_HIS_BANWHO) ? &me : sptr));
324             newban->value.ban.when = TStime();
325
326             newban->flags = CHFL_BAN | CHFL_BURST_BAN; /* set flags */
327             if ((ptr = strrchr(ban, '@')) && check_if_ipmask(ptr + 1))
328               newban->flags |= CHFL_BAN_IPMASK;
329
330             newban->next = 0;
331             if (lp)
332               lp->next = newban; /* link it in */
333             else
334               chptr->banlist = newban;
335           }
336         }
337       } 
338       param++; /* look at next param */
339       break;
340
341     default: /* parameter contains clients */
342       {
343         struct Client *acptr;
344         char *nicklist = parv[param], *p = 0, *nick, *ptr;
345         int current_mode, last_mode, base_mode;
346         int oplevel = -1;       /* Mark first field with digits: means the same as 'o' (but with level). */
347         int last_oplevel = 0;
348         struct Membership* member;
349
350         base_mode = CHFL_DEOPPED | CHFL_BURST_JOINED;
351         if (chptr->mode.mode & MODE_DELJOINS)
352             base_mode |= CHFL_DELAYED;
353         current_mode = last_mode = base_mode;
354
355         for (nick = ircd_strtok(&p, nicklist, ","); nick;
356              nick = ircd_strtok(&p, 0, ",")) {
357
358           if ((ptr = strchr(nick, ':'))) { /* new flags; deal */
359             *ptr++ = '\0';
360
361             if (parse_flags & MODE_PARSE_SET) {
362               int current_mode_needs_reset;
363               for (current_mode_needs_reset = 1; *ptr; ptr++) {
364                 if (*ptr == 'o') { /* has oper status */
365                   /*
366                    * An 'o' is pre-oplevel protocol, so this is only for
367                    * backwards compatibility.  Give them an op-level of
368                    * MAXOPLEVEL so everyone can deop them.
369                    */
370                   oplevel = MAXOPLEVEL;
371                   if (current_mode_needs_reset) {
372                     current_mode = base_mode;
373                     current_mode_needs_reset = 0;
374                   }
375                   current_mode = (current_mode & ~(CHFL_DEOPPED | CHFL_DELAYED)) | CHFL_CHANOP;
376                 }
377                 else if (*ptr == 'v') { /* has voice status */
378                   if (current_mode_needs_reset) {
379                     current_mode = base_mode;
380                     current_mode_needs_reset = 0;
381                   }
382                   current_mode = (current_mode & ~CHFL_DELAYED) | CHFL_VOICE;
383                   oplevel = -1; /* subsequential digits are an absolute op-level value. */
384                 }
385                 else if (isdigit(*ptr)) {
386                   int level_increment = 0;
387                   if (oplevel == -1) { /* op-level is absolute value? */
388                     if (current_mode_needs_reset) {
389                       current_mode = base_mode;
390                       current_mode_needs_reset = 0;
391                     }
392                     oplevel = 0;
393                   }
394                   current_mode = (current_mode & ~(CHFL_DEOPPED | CHFL_DELAYED)) | CHFL_CHANOP;
395                   do {
396                     level_increment = 10 * level_increment + *ptr++ - '0';
397                   } while(isdigit(*ptr));
398                   oplevel += level_increment;
399                 }
400                 else /* I don't recognize that flag */
401                   break; /* so stop processing */
402               }
403             }
404           }
405
406           if (!(acptr = findNUser(nick)) || cli_from(acptr) != cptr)
407             continue; /* ignore this client */
408
409           /* Build nick buffer */
410           nickstr[nickpos] = nickpos ? ',' : ' '; /* first char */
411           nickpos++;
412
413           for (ptr = nick; *ptr; ptr++) /* store nick */
414             nickstr[nickpos++] = *ptr;
415
416           if (current_mode != last_mode) { /* if mode changed... */
417             last_mode = current_mode;
418             last_oplevel = oplevel;
419
420             nickstr[nickpos++] = ':'; /* add a specifier */
421             if (current_mode & CHFL_VOICE)
422               nickstr[nickpos++] = 'v';
423             if (current_mode & CHFL_CHANOP)
424               nickpos += ircd_snprintf(0, nickstr + nickpos, sizeof(nickstr) - nickpos, "%u", oplevel);
425           } else if (current_mode & CHFL_CHANOP && oplevel != last_oplevel) { /* if just op level changed... */
426             nickstr[nickpos++] = ':'; /* add a specifier */
427             nickpos += ircd_snprintf(0, nickstr + nickpos, sizeof(nickstr) - nickpos, "%u", oplevel - last_oplevel);
428             last_oplevel = oplevel;
429           }
430
431           if (IsBurst(sptr) || !(member = find_member_link(chptr, acptr)))
432           {
433             add_user_to_channel(chptr, acptr, current_mode, oplevel);
434             if (!(current_mode & CHFL_DELAYED))
435               sendcmdto_channel_butserv_butone(acptr, CMD_JOIN, chptr, NULL, "%H", chptr);
436           }
437           else
438           {
439             /* The member was already joined (either by CREATE or JOIN).
440                Remember the current mode. */
441             if (member->status & CHFL_CHANOP)
442               member->status |= CHFL_BURST_ALREADY_OPPED;
443             if (member->status & CHFL_VOICE)
444               member->status |= CHFL_BURST_ALREADY_VOICED;
445             /* Synchronize with the burst. */
446             member->status |= CHFL_BURST_JOINED | (current_mode & (CHFL_CHANOP|CHFL_VOICE));
447             member->oplevel = oplevel;
448           }
449         }
450       }
451       param++;
452       break;
453     } /* switch (*parv[param]) */
454   } /* while (param < parc) */
455
456   nickstr[nickpos] = '\0';
457   banstr[banpos] = '\0';
458
459   if (parse_flags & MODE_PARSE_SET) {
460     modebuf_extract(mbuf, modestr + 1); /* for sending BURST onward */
461     modestr[0] = modestr[1] ? ' ' : '\0';
462   } else
463     modestr[0] = '\0';
464
465   sendcmdto_serv_butone(sptr, CMD_BURST, cptr, "%H %Tu%s%s%s", chptr,
466                         chptr->creationtime, modestr, nickstr, banstr);
467
468   if (parse_flags & MODE_PARSE_WIPEOUT || banpos)
469     mode_ban_invalidate(chptr);
470
471   if (parse_flags & MODE_PARSE_SET) { /* any modes changed? */
472     /* first deal with channel members */
473     for (member = chptr->members; member; member = member->next_member) {
474       if (member->status & CHFL_BURST_JOINED) { /* joined during burst */
475         if ((member->status & CHFL_CHANOP) && !(member->status & CHFL_BURST_ALREADY_OPPED))
476           modebuf_mode_client(mbuf, MODE_ADD | CHFL_CHANOP, member->user);
477         if ((member->status & CHFL_VOICE) && !(member->status & CHFL_BURST_ALREADY_VOICED))
478           modebuf_mode_client(mbuf, MODE_ADD | CHFL_VOICE, member->user);
479       } else if (parse_flags & MODE_PARSE_WIPEOUT) { /* wipeout old ops */
480         if (member->status & CHFL_CHANOP)
481           modebuf_mode_client(mbuf, MODE_DEL | CHFL_CHANOP, member->user);
482         if (member->status & CHFL_VOICE)
483           modebuf_mode_client(mbuf, MODE_DEL | CHFL_VOICE, member->user);
484         member->status = ((member->status & ~(CHFL_CHANOP | CHFL_VOICE)) |
485                           CHFL_DEOPPED);
486       }
487     }
488
489     /* Now deal with channel bans */
490     lp_p = &chptr->banlist;
491     while (*lp_p) {
492       lp = *lp_p;
493
494       /* remove ban from channel */
495       if (lp->flags & (CHFL_BAN_OVERLAPPED | CHFL_BURST_BAN_WIPEOUT)) {
496         modebuf_mode_string(mbuf, MODE_DEL | MODE_BAN,
497                             lp->value.ban.banstr, 1); /* let it free banstr */
498
499         *lp_p = lp->next; /* clip out of list */
500         MyFree(lp->value.ban.who); /* free who */
501         free_link(lp); /* free ban */
502         continue;
503       } else if (lp->flags & CHFL_BURST_BAN) /* add ban to channel */
504         modebuf_mode_string(mbuf, MODE_ADD | MODE_BAN,
505                             lp->value.ban.banstr, 0); /* don't free banstr */
506
507       lp->flags &= (CHFL_BAN | CHFL_BAN_IPMASK); /* reset the flag */
508       lp_p = &(*lp_p)->next;
509     }
510   }
511
512   return mbuf ? modebuf_flush(mbuf) : 0;
513 }