Author: Carlo Wood <run@alinoe.com> (Via Isomer <isomer@undernet.org>)
[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_policy.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 int ms_burst(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
172 {
173   struct ModeBuf modebuf, *mbuf = 0;
174   struct Channel *chptr;
175   time_t timestamp;
176   struct Membership *member;
177   struct SLink *lp, **lp_p;
178   unsigned int parse_flags = (MODE_PARSE_FORCE | MODE_PARSE_BURST);
179   int param, nickpos = 0, banpos = 0;
180   char modestr[BUFSIZE], nickstr[BUFSIZE], banstr[BUFSIZE];
181
182   if (parc < 3)
183     return protocol_violation(sptr,"Too few parameters for BURST");
184
185   if (!IsBurst(sptr)) /* don't allow BURST outside of burst */
186     return exit_client_msg(cptr, cptr, &me, "HACK: BURST message outside "
187                            "net.burst from %s", cli_name(sptr));
188
189   if (!(chptr = get_channel(sptr, parv[1], CGT_CREATE)))
190     return 0; /* can't create the channel? */
191
192   timestamp = atoi(parv[2]);
193
194   /* turn off burst joined flag */
195   for (member = chptr->members; member; member = member->next_member)
196     member->status &= ~CHFL_BURST_JOINED;
197
198   if (!chptr->creationtime) /* mark channel as created during BURST */
199     chptr->mode.mode |= MODE_BURSTADDED;
200
201   /* new channel or an older one */
202   if (!chptr->creationtime || chptr->creationtime > timestamp) {
203     chptr->creationtime = timestamp;
204
205     modebuf_init(mbuf = &modebuf, &me, cptr, chptr, MODEBUF_DEST_CHANNEL);
206     modebuf_mode(mbuf, MODE_DEL | chptr->mode.mode); /* wipeout modes */
207     chptr->mode.mode &= ~(MODE_ADD | MODE_DEL | MODE_PRIVATE | MODE_SECRET |
208                           MODE_MODERATED | MODE_TOPICLIMIT | MODE_INVITEONLY |
209                           MODE_NOPRIVMSGS);
210
211     parse_flags |= (MODE_PARSE_SET | MODE_PARSE_WIPEOUT); /* wipeout keys */
212
213     /* mark bans for wipeout */
214     for (lp = chptr->banlist; lp; lp = lp->next)
215       lp->flags |= CHFL_BURST_BAN_WIPEOUT;
216   } else if (chptr->creationtime == timestamp) {
217     modebuf_init(mbuf = &modebuf, &me, cptr, chptr, MODEBUF_DEST_CHANNEL);
218
219     parse_flags |= MODE_PARSE_SET; /* set new modes */
220   }
221
222   param = 3; /* parse parameters */
223   while (param < parc) {
224     switch (*parv[param]) {
225     case '+': /* parameter introduces a mode string */
226       param += mode_parse(mbuf, cptr, sptr, chptr, parc - param,
227                           parv + param, parse_flags, NULL);
228       break;
229
230     case '%': /* parameter contains bans */
231       if (parse_flags & MODE_PARSE_SET) {
232         char *banlist = parv[param] + 1, *p = 0, *ban, *ptr;
233         struct SLink *newban;
234
235         for (ban = ircd_strtok(&p, banlist, " "); ban;
236              ban = ircd_strtok(&p, 0, " ")) {
237           ban = collapse(pretty_mask(ban));
238
239             /*
240              * Yeah, we should probably do this elsewhere, and make it better
241              * and more general; this will hold until we get there, though.
242              * I dislike the current add_banid API... -Kev
243              *
244              * I wish there were a better algo. for this than the n^2 one
245              * shown below *sigh*
246              */
247           for (lp = chptr->banlist; lp; lp = lp->next) {
248             if (!ircd_strcmp(lp->value.ban.banstr, ban)) {
249               ban = 0; /* don't add ban */
250               lp->flags &= ~CHFL_BURST_BAN_WIPEOUT; /* not wiping out */
251               break; /* new ban already existed; don't even repropagate */
252             } else if (!(lp->flags & CHFL_BURST_BAN_WIPEOUT) &&
253                        !mmatch(lp->value.ban.banstr, ban)) {
254               ban = 0; /* don't add ban unless wiping out bans */
255               break; /* new ban is encompassed by an existing one; drop */
256             } else if (!mmatch(ban, lp->value.ban.banstr))
257               lp->flags |= CHFL_BAN_OVERLAPPED; /* remove overlapping ban */
258
259             if (!lp->next)
260               break;
261           }
262
263           if (ban) { /* add the new ban to the end of the list */
264             /* Build ban buffer */
265             if (!banpos) {
266               banstr[banpos++] = ' ';
267               banstr[banpos++] = ':';
268               banstr[banpos++] = '%';
269             } else
270               banstr[banpos++] = ' ';
271             for (ptr = ban; *ptr; ptr++) /* add ban to buffer */
272               banstr[banpos++] = *ptr;
273
274             newban = make_link(); /* create new ban */
275
276             DupString(newban->value.ban.banstr, ban);
277 #ifdef HEAD_IN_SAND_BANWHO
278             DupString(newban->value.ban.who, cli_name(&me));
279 #else
280             DupString(newban->value.ban.who, cli_name(sptr));
281 #endif
282             newban->value.ban.when = TStime();
283
284             newban->flags = CHFL_BAN | CHFL_BURST_BAN; /* set flags */
285             if ((ptr = strrchr(ban, '@')) && check_if_ipmask(ptr + 1))
286               newban->flags |= CHFL_BAN_IPMASK;
287
288             newban->next = 0;
289             if (lp)
290               lp->next = newban; /* link it in */
291             else
292               chptr->banlist = newban;
293           }
294         }
295       } 
296       param++; /* look at next param */
297       break;
298
299     default: /* parameter contains clients */
300       {
301         struct Client *acptr;
302         char *nicklist = parv[param], *p = 0, *nick, *ptr;
303         int current_mode = CHFL_DEOPPED | CHFL_BURST_JOINED;
304         int last_mode = CHFL_DEOPPED | CHFL_BURST_JOINED;
305         int oplevel = -1;       /* Mark first field with digits: means the same as 'o' (but with level). */
306         int last_oplevel = 0;
307
308         for (nick = ircd_strtok(&p, nicklist, ","); nick;
309              nick = ircd_strtok(&p, 0, ",")) {
310
311           if ((ptr = strchr(nick, ':'))) { /* new flags; deal */
312             *ptr++ = '\0';
313
314             if (parse_flags & MODE_PARSE_SET) {
315               int current_mode_needs_reset;
316               for (current_mode_needs_reset = 1; *ptr; ptr++) {
317                 if (*ptr == 'o') { /* has oper status */
318                   /*
319                    * An 'o' is pre-oplevel protocol, so this is only for
320                    * backwards compatibility.  Give them an op-level of
321                    * MAXOPLEVEL so everyone can deop them.
322                    */
323                   oplevel = MAXOPLEVEL;
324                   if (current_mode_needs_reset) {
325                     current_mode = CHFL_DEOPPED | CHFL_BURST_JOINED;
326                     current_mode_needs_reset = 0;
327                   }
328                   current_mode = (current_mode & ~CHFL_DEOPPED) | CHFL_CHANOP;
329                 }
330                 else if (*ptr == 'v') { /* has voice status */
331                   if (current_mode_needs_reset) {
332                     current_mode = CHFL_DEOPPED | CHFL_BURST_JOINED;
333                     current_mode_needs_reset = 0;
334                   }
335                   current_mode |= CHFL_VOICE;
336                   oplevel = -1; /* subsequential digits are an absolute op-level value. */
337                 }
338                 else if (isdigit(*ptr)) {
339                   int level_increment = 0;
340                   if (oplevel == -1) { /* op-level is absolute value? */
341                     if (current_mode_needs_reset) {
342                       current_mode = CHFL_DEOPPED | CHFL_BURST_JOINED;
343                       current_mode_needs_reset = 0;
344                     }
345                     oplevel = 0;
346                   }
347                   current_mode = (current_mode & ~CHFL_DEOPPED) | CHFL_CHANOP;
348                   do {
349                     level_increment = 10 * level_increment + *ptr++ - '0';
350                   } while(isdigit(*ptr));
351                   oplevel += level_increment;
352                 }
353                 else /* I don't recognize that flag */
354                   break; /* so stop processing */
355               }
356             }
357           }
358
359           if (!(acptr = findNUser(nick)) || cli_from(acptr) != cptr)
360             continue; /* ignore this client */
361
362           /* Build nick buffer */
363           nickstr[nickpos] = nickpos ? ',' : ' '; /* first char */
364           nickpos++;
365
366           for (ptr = nick; *ptr; ptr++) /* store nick */
367             nickstr[nickpos++] = *ptr;
368
369           if (current_mode != last_mode) { /* if mode changed... */
370             last_mode = current_mode;
371             last_oplevel = oplevel;
372
373             nickstr[nickpos++] = ':'; /* add a specifier */
374             if (current_mode & CHFL_VOICE)
375               nickstr[nickpos++] = 'v';
376             if (current_mode & CHFL_CHANOP)
377               nickpos += ircd_snprintf(0, nickstr + nickpos, sizeof(nickstr) - nickpos, "%u", oplevel);
378           } else if (current_mode & CHFL_CHANOP && oplevel != last_oplevel) { /* if just op level changed... */
379             nickstr[nickpos++] = ':'; /* add a specifier */
380             nickpos += ircd_snprintf(0, nickstr + nickpos, sizeof(nickstr) - nickpos, "%u", oplevel - last_oplevel);
381             last_oplevel = oplevel;
382           }
383
384           add_user_to_channel(chptr, acptr, current_mode, oplevel);
385           sendcmdto_channel_butserv_butone(acptr, CMD_JOIN, chptr, NULL, "%H", chptr);
386         }
387       }
388       param++;
389       break;
390     } /* switch (*parv[param]) { */
391   } /* while (param < parc) { */
392
393   nickstr[nickpos] = '\0';
394   banstr[banpos] = '\0';
395
396   if (parse_flags & MODE_PARSE_SET) {
397     modebuf_extract(mbuf, modestr + 1); /* for sending BURST onward */
398     modestr[0] = modestr[1] ? ' ' : '\0';
399   } else
400     modestr[0] = '\0';
401
402   sendcmdto_serv_butone(sptr, CMD_BURST, cptr, "%H %Tu%s%s%s", chptr,
403                         chptr->creationtime, modestr, nickstr, banstr);
404
405   if (parse_flags & MODE_PARSE_WIPEOUT || banpos)
406     mode_ban_invalidate(chptr);
407
408   if (parse_flags & MODE_PARSE_SET) { /* any modes changed? */
409     /* first deal with channel members */
410     for (member = chptr->members; member; member = member->next_member) {
411       if (member->status & CHFL_BURST_JOINED) { /* joined during burst */
412         if (member->status & CHFL_CHANOP)
413           modebuf_mode_client(mbuf, MODE_ADD | CHFL_CHANOP, member->user);
414         if (member->status & CHFL_VOICE)
415           modebuf_mode_client(mbuf, MODE_ADD | CHFL_VOICE, member->user);
416       } else if (parse_flags & MODE_PARSE_WIPEOUT) { /* wipeout old ops */
417         if (member->status & CHFL_CHANOP)
418           modebuf_mode_client(mbuf, MODE_DEL | CHFL_CHANOP, member->user);
419         if (member->status & CHFL_VOICE)
420           modebuf_mode_client(mbuf, MODE_DEL | CHFL_VOICE, member->user);
421         member->status = ((member->status & ~(CHFL_CHANOP | CHFL_VOICE)) |
422                           CHFL_DEOPPED);
423       }
424     }
425
426     /* Now deal with channel bans */
427     lp_p = &chptr->banlist;
428     while (*lp_p) {
429       lp = *lp_p;
430
431       /* remove ban from channel */
432       if (lp->flags & (CHFL_BAN_OVERLAPPED | CHFL_BURST_BAN_WIPEOUT)) {
433         modebuf_mode_string(mbuf, MODE_DEL | MODE_BAN,
434                             lp->value.ban.banstr, 1); /* let it free banstr */
435
436         *lp_p = lp->next; /* clip out of list */
437         MyFree(lp->value.ban.who); /* free who */
438         free_link(lp); /* free ban */
439         continue;
440       } else if (lp->flags & CHFL_BURST_BAN) /* add ban to channel */
441         modebuf_mode_string(mbuf, MODE_ADD | MODE_BAN,
442                             lp->value.ban.banstr, 0); /* don't free banstr */
443
444       lp->flags &= (CHFL_BAN | CHFL_BAN_IPMASK); /* reset the flag */
445       lp_p = &(*lp_p)->next;
446     }
447   }
448
449   return mbuf ? modebuf_flush(mbuf) : 0;
450 }