Implement invitation announcements (RPL_ISSUEDINVITE).
[ircu2.10.12-pk.git] / ircd / m_burst.c
index ecde2f46c3eaed8d8983b8da58dbcede0a79f170..c03a6c004bcfc5ab16d2b5dc30b57de86dee3929 100644 (file)
@@ -86,7 +86,7 @@
 #include "hash.h"
 #include "ircd.h"
 #include "ircd_alloc.h"
-#include "ircd_policy.h"
+#include "ircd_features.h"
 #include "ircd_reply.h"
 #include "ircd_string.h"
 #include "list.h"
 #include "send.h"
 #include "struct.h"
 #include "support.h"
+#include "ircd_snprintf.h"
 
 #include <assert.h>
 #include <stdlib.h>
 #include <string.h>
+#include <ctype.h>
 
 /*
  * ms_burst - server message handler
  *   parv[n] = %<ban> <ban> <ban> ...
  * If parv[n] starts with another character:
  *   parv[n] = <nick>[:<mode>],<nick>[:<mode>],...
- *   where <mode> is the channel mode (ov) of nick and all following nicks.
+ *   where <mode> defines the mode and op-level
+ *   for nick and all following nicks until the
+ *   next <mode> field.
+ *   Digits in the <mode> field have of two meanings:
+ *   1) if it is the first field in this BURST message
+ *      that contains digits, and/or when a 'v' is
+ *      present in the <mode>:
+ *      The absolute value of the op-level.
+ *   2) if there are only digits in this field and
+ *      it is not the first field with digits:
+ *      An op-level increment relative to the previous
+ *      op-level.
+ *   First all modeless nicks must be emmitted,
+ *   then all combinations of modes without ops
+ *   (currently that is only 'v') followed by the same
+ *   series but then with ops (currently 'o','ov').
  *
  * Example:
- * "S BURST #channel 87654321 +ntkl key 123 AAA,AAB:o,BAA,BAB:ov :%ban1 ban2"
+ * "A8 B #test 87654321 +ntkAl key secret 123 A8AAG,A8AAC:v,A8AAA:0,A8AAF:2,A8AAD,A8AAB:v1,A8AAE:1 :%ban1 ban2"
+ *
+ * <mode> list example:
+ *
+ * "xxx,sss:v,ttt,aaa:123,bbb,ccc:2,ddd,kkk:v2,lll:2,mmm"
+ *
+ * means
+ *
+ *  xxx                // first modeless nicks
+ *  sss +v     // then opless nicks
+ *  ttt +v     // no ":<mode>": everything stays the same
+ *  aaa -123   // first field with digit: absolute value
+ *  bbb -123
+ *  ccc -125   // only digits, not first field: increment
+ *  ddd -125
+ *  kkk -2 +v  // field with a 'v': absolute value
+ *  lll -4 +v  // only digits: increment
+ *  mmm -4 +v
  *
  * Anti net.ride code.
  *
- * When the channel already exist, and its TS is larger then
+ * When the channel already exist, and its TS is larger than
  * the TS in the BURST message, then we cancel all existing modes.
  * If its is smaller then the received BURST message is ignored.
  * If it's equal, then the received modes are just added.
+ *
+ * BURST is also accepted outside a netburst now because it
+ * is sent upstream as reaction to a DESTRUCT message.  For
+ * these BURST messages it is possible that the listed channel
+ * members are already joined.
  */
 int ms_burst(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
 {
   struct ModeBuf modebuf, *mbuf = 0;
   struct Channel *chptr;
   time_t timestamp;
-  struct Membership *member;
+  struct Membership *member, *nmember;
   struct SLink *lp, **lp_p;
   unsigned int parse_flags = (MODE_PARSE_FORCE | MODE_PARSE_BURST);
   int param, nickpos = 0, banpos = 0;
@@ -148,18 +187,46 @@ int ms_burst(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
   if (parc < 3)
     return protocol_violation(sptr,"Too few parameters for BURST");
 
-  if (!IsBurst(sptr)) /* don't allow BURST outside of burst */
-    return exit_client_msg(cptr, cptr, &me, "HACK: BURST message outside "
-                          "net.burst from %s", cli_name(sptr));
-
   if (!(chptr = get_channel(sptr, parv[1], CGT_CREATE)))
     return 0; /* can't create the channel? */
 
   timestamp = atoi(parv[2]);
 
+  if (!chptr->creationtime || chptr->creationtime > timestamp) {
+    /*
+     * Kick local members if channel is +i or +k and our TS was larger
+     * than the burst TS (anti net.ride). The modes hack is here because
+     * we have to do this before mode_parse, as chptr may go away.
+     */
+    for (param = 3; param < parc; param++)
+    {
+      if (parv[param][0] != '+')
+        continue;
+      if (strchr(parv[param], 'i') || strchr(parv[param], 'k'))
+      {
+        /* Clear any outstanding rogue invites */
+        mode_invite_clear(chptr);
+        for (member = chptr->members; member; member = nmember)
+        {
+          nmember = member->next_member;
+          if (!MyUser(member->user) || IsZombie(member))
+            continue;
+          sendcmdto_serv_butone(&me, CMD_KICK, NULL, "%H %C :Net Rider", chptr, member->user);
+          sendcmdto_channel_butserv_butone(&me, CMD_KICK, chptr, NULL, 0, "%H %C :Net Rider", chptr, member->user);
+          make_zombie(member, member->user, &me, &me, chptr);
+        }
+      }
+      break;
+    }
+
+    /* If the channel had only locals, it went away by now. */
+    if (!(chptr = get_channel(sptr, parv[1], CGT_CREATE)))
+      return 0; /* can't create the channel? */
+  }
+
   /* turn off burst joined flag */
   for (member = chptr->members; member; member = member->next_member)
-    member->status &= ~CHFL_BURST_JOINED;
+    member->status &= ~(CHFL_BURST_JOINED|CHFL_BURST_ALREADY_OPPED|CHFL_BURST_ALREADY_VOICED);
 
   if (!chptr->creationtime) /* mark channel as created during BURST */
     chptr->mode.mode |= MODE_BURSTADDED;
@@ -168,19 +235,30 @@ int ms_burst(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
   if (!chptr->creationtime || chptr->creationtime > timestamp) {
     chptr->creationtime = timestamp;
 
-    modebuf_init(mbuf = &modebuf, &me, cptr, chptr, MODEBUF_DEST_CHANNEL);
+    modebuf_init(mbuf = &modebuf, &me, cptr, chptr,
+                MODEBUF_DEST_CHANNEL | MODEBUF_DEST_NOKEY);
     modebuf_mode(mbuf, MODE_DEL | chptr->mode.mode); /* wipeout modes */
     chptr->mode.mode &= ~(MODE_ADD | MODE_DEL | MODE_PRIVATE | MODE_SECRET |
                          MODE_MODERATED | MODE_TOPICLIMIT | MODE_INVITEONLY |
-                         MODE_NOPRIVMSGS);
+                         MODE_NOPRIVMSGS | MODE_DELJOINS);
 
     parse_flags |= (MODE_PARSE_SET | MODE_PARSE_WIPEOUT); /* wipeout keys */
 
     /* mark bans for wipeout */
     for (lp = chptr->banlist; lp; lp = lp->next)
       lp->flags |= CHFL_BURST_BAN_WIPEOUT;
+
+    /* clear topic set by netrider (if set) */
+    if (*chptr->topic) {
+      *chptr->topic = '\0';
+      *chptr->topic_nick = '\0';
+      chptr->topic_time = 0;
+      sendcmdto_channel_butserv_butone(&me, CMD_TOPIC, chptr, NULL, 0,
+                                       "%H :%s", chptr, chptr->topic);
+    }
   } else if (chptr->creationtime == timestamp) {
-    modebuf_init(mbuf = &modebuf, &me, cptr, chptr, MODEBUF_DEST_CHANNEL);
+    modebuf_init(mbuf = &modebuf, &me, cptr, chptr,
+                MODEBUF_DEST_CHANNEL | MODEBUF_DEST_NOKEY);
 
     parse_flags |= MODE_PARSE_SET; /* set new modes */
   }
@@ -190,7 +268,7 @@ int ms_burst(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
     switch (*parv[param]) {
     case '+': /* parameter introduces a mode string */
       param += mode_parse(mbuf, cptr, sptr, chptr, parc - param,
-                         parv + param, parse_flags);
+                         parv + param, parse_flags, NULL);
       break;
 
     case '%': /* parameter contains bans */
@@ -240,11 +318,9 @@ int ms_burst(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
            newban = make_link(); /* create new ban */
 
            DupString(newban->value.ban.banstr, ban);
-#ifdef HEAD_IN_SAND_BANWHO
-           DupString(newban->value.ban.who, cli_name(&me));
-#else
-           DupString(newban->value.ban.who, cli_name(sptr));
-#endif
+
+            DupString(newban->value.ban.who, 
+                      cli_name(feature_bool(FEAT_HIS_BANWHO) ? &me : sptr));
            newban->value.ban.when = TStime();
 
            newban->flags = CHFL_BAN | CHFL_BURST_BAN; /* set flags */
@@ -266,8 +342,15 @@ int ms_burst(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
       {
        struct Client *acptr;
        char *nicklist = parv[param], *p = 0, *nick, *ptr;
-       int default_mode = CHFL_DEOPPED | CHFL_BURST_JOINED;
-       int last_mode = CHFL_DEOPPED | CHFL_BURST_JOINED;
+       int current_mode, last_mode, base_mode;
+       int oplevel = -1;       /* Mark first field with digits: means the same as 'o' (but with level). */
+       int last_oplevel = 0;
+       struct Membership* member;
+
+        base_mode = CHFL_DEOPPED | CHFL_BURST_JOINED;
+        if (chptr->mode.mode & MODE_DELJOINS)
+            base_mode |= CHFL_DELAYED;
+        current_mode = last_mode = base_mode;
 
        for (nick = ircd_strtok(&p, nicklist, ","); nick;
             nick = ircd_strtok(&p, 0, ",")) {
@@ -276,12 +359,44 @@ int ms_burst(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
            *ptr++ = '\0';
 
            if (parse_flags & MODE_PARSE_SET) {
-             for (default_mode = CHFL_DEOPPED | CHFL_BURST_JOINED; *ptr;
-                  ptr++) {
-               if (*ptr == 'o') /* has oper status */
-                 default_mode = (default_mode & ~CHFL_DEOPPED) | CHFL_CHANOP;
-               else if (*ptr == 'v') /* has voice status */
-                 default_mode |= CHFL_VOICE;
+             int current_mode_needs_reset;
+             for (current_mode_needs_reset = 1; *ptr; ptr++) {
+               if (*ptr == 'o') { /* has oper status */
+                 /*
+                  * An 'o' is pre-oplevel protocol, so this is only for
+                  * backwards compatibility.  Give them an op-level of
+                  * MAXOPLEVEL so everyone can deop them.
+                  */
+                 oplevel = MAXOPLEVEL;
+                 if (current_mode_needs_reset) {
+                   current_mode = base_mode;
+                   current_mode_needs_reset = 0;
+                 }
+                 current_mode = (current_mode & ~(CHFL_DEOPPED | CHFL_DELAYED)) | CHFL_CHANOP;
+               }
+               else if (*ptr == 'v') { /* has voice status */
+                 if (current_mode_needs_reset) {
+                    current_mode = base_mode;
+                   current_mode_needs_reset = 0;
+                 }
+                 current_mode = (current_mode & ~CHFL_DELAYED) | CHFL_VOICE;
+                 oplevel = -1; /* subsequential digits are an absolute op-level value. */
+                }
+               else if (isdigit(*ptr)) {
+                 int level_increment = 0;
+                 if (oplevel == -1) { /* op-level is absolute value? */
+                   if (current_mode_needs_reset) {
+                     current_mode = base_mode;
+                     current_mode_needs_reset = 0;
+                   }
+                   oplevel = 0;
+                 }
+                 current_mode = (current_mode & ~(CHFL_DEOPPED | CHFL_DELAYED)) | CHFL_CHANOP;
+                 do {
+                   level_increment = 10 * level_increment + *ptr++ - '0';
+                 } while(isdigit(*ptr));
+                 oplevel += level_increment;
+               }
                else /* I don't recognize that flag */
                  break; /* so stop processing */
              }
@@ -298,24 +413,45 @@ int ms_burst(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
          for (ptr = nick; *ptr; ptr++) /* store nick */
            nickstr[nickpos++] = *ptr;
 
-         if (default_mode != last_mode) { /* if mode changed... */
-           last_mode = default_mode;
+         if (current_mode != last_mode) { /* if mode changed... */
+           last_mode = current_mode;
+           last_oplevel = oplevel;
 
            nickstr[nickpos++] = ':'; /* add a specifier */
-           if (default_mode & CHFL_CHANOP)
-             nickstr[nickpos++] = 'o';
-           if (default_mode & CHFL_VOICE)
+           if (current_mode & CHFL_VOICE)
              nickstr[nickpos++] = 'v';
+           if (current_mode & CHFL_CHANOP)
+             nickpos += ircd_snprintf(0, nickstr + nickpos, sizeof(nickstr) - nickpos, "%u", oplevel);
+         } else if (current_mode & CHFL_CHANOP && oplevel != last_oplevel) { /* if just op level changed... */
+           nickstr[nickpos++] = ':'; /* add a specifier */
+           nickpos += ircd_snprintf(0, nickstr + nickpos, sizeof(nickstr) - nickpos, "%u", oplevel - last_oplevel);
+            last_oplevel = oplevel;
          }
 
-         add_user_to_channel(chptr, acptr, default_mode);
-         sendcmdto_channel_butserv(acptr, CMD_JOIN, chptr, "%H", chptr);
+         if (IsBurst(sptr) || !(member = find_member_link(chptr, acptr)))
+         {
+           add_user_to_channel(chptr, acptr, current_mode, oplevel);
+            if (!(current_mode & CHFL_DELAYED))
+              sendcmdto_channel_butserv_butone(acptr, CMD_JOIN, chptr, NULL, 0, "%H", chptr);
+         }
+         else
+         {
+           /* The member was already joined (either by CREATE or JOIN).
+              Remember the current mode. */
+           if (member->status & CHFL_CHANOP)
+             member->status |= CHFL_BURST_ALREADY_OPPED;
+           if (member->status & CHFL_VOICE)
+             member->status |= CHFL_BURST_ALREADY_VOICED;
+           /* Synchronize with the burst. */
+           member->status |= CHFL_BURST_JOINED | (current_mode & (CHFL_CHANOP|CHFL_VOICE));
+           member->oplevel = oplevel;
+         }
        }
       }
       param++;
       break;
-    } /* switch (*parv[param]) */
-  } /* while (param < parc) */
+    } /* switch (*parv[param]) */
+  } /* while (param < parc) */
 
   nickstr[nickpos] = '\0';
   banstr[banpos] = '\0';
@@ -336,9 +472,9 @@ int ms_burst(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
     /* first deal with channel members */
     for (member = chptr->members; member; member = member->next_member) {
       if (member->status & CHFL_BURST_JOINED) { /* joined during burst */
-       if (member->status & CHFL_CHANOP)
+       if ((member->status & CHFL_CHANOP) && !(member->status & CHFL_BURST_ALREADY_OPPED))
          modebuf_mode_client(mbuf, MODE_ADD | CHFL_CHANOP, member->user);
-       if (member->status & CHFL_VOICE)
+       if ((member->status & CHFL_VOICE) && !(member->status & CHFL_BURST_ALREADY_VOICED))
          modebuf_mode_client(mbuf, MODE_ADD | CHFL_VOICE, member->user);
       } else if (parse_flags & MODE_PARSE_WIPEOUT) { /* wipeout old ops */
        if (member->status & CHFL_CHANOP)