Author: Jeekay <jeekay@irc.planetarion.com>
[ircu2.10.12-pk.git] / ircd / m_join.c
index b09778a450633b777c8ae543d66f1150dd436f34..8843b5d3d2baf716b20bcb592d26ba6e33cb3ce5 100644 (file)
  *            note:   it is guaranteed that parv[0]..parv[parc-1] are all
  *                    non-NULL pointers.
  */
-#if 0
-/*
- * No need to include handlers.h here the signatures must match
- * and we don't need to force a rebuild of all the handlers everytime
- * we add a new one to the list. --Bleep
- */
-#include "handlers.h"
-#endif /* 0 */
+#include "config.h"
+
 #include "channel.h"
 #include "client.h"
 #include "gline.h"
 #include "hash.h"
 #include "ircd.h"
 #include "ircd_chattr.h"
+#include "ircd_features.h"
 #include "ircd_reply.h"
 #include "ircd_string.h"
 #include "msg.h"
 #include <stdlib.h>
 #include <string.h>
 
-#if !defined(XXX_BOGUS_TEMP_HACK)
-#include "handlers.h"      /* m_names */
-#endif
+/*
+ * Helper function to see if there are any control characters
+ * in a given string
+ */
+static char *
+HasControlChars(char *mstring)
+{
+  unsigned char *j;
+  for(j = mstring; *j ; j++) {
+    if(*j <= 32) { return j; }
+  }
+
+  return 0;
+}
 
 /*
  * Helper function to find last 0 in a comma-separated list of
@@ -159,7 +165,7 @@ join0(struct JoinBuf *join, struct Client *cptr, struct Client *sptr,
   joinbuf_init(&part, sptr, cptr, JOINBUF_TYPE_PARTALL,
               "Left all channels", 0);
 
-  while ((member = sptr->user->channel))
+  while ((member = cli_user(sptr)->channel))
     joinbuf_join(&part, member->channel,
                 IsZombie(member) ? CHFL_ZOMBIE : 0);
 
@@ -176,9 +182,8 @@ int m_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
   struct Channel *chptr;
   struct JoinBuf join;
   struct JoinBuf create;
-#ifdef BADCHAN
+  struct ModeBuf mbuf;
   struct Gline *gline;
-#endif
   unsigned int flags = 0;
   int i;
   char *p = 0;
@@ -196,8 +201,6 @@ int m_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
 
   keys = parv[2]; /* remember where keys are */
 
-  parv[2] = 0; /* for call to m_names below */
-
   for (name = ircd_strtok(&p, chanlist, ","); name;
        name = ircd_strtok(&p, 0, ",")) {
     clean_channelname(name);
@@ -205,19 +208,17 @@ int m_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
     if (join0(&join, cptr, sptr, name)) /* did client do a JOIN 0? */
       continue;
 
-    if (!IsChannelName(name)) { /* bad channel name */
+    if (!IsChannelName(name) || HasControlChars(name)) { /* bad channel name */
       send_reply(sptr, ERR_NOSUCHCHANNEL, name);
       continue;
     }
 
-#ifdef BADCHAN
     /* BADCHANed channel */
     if ((gline = gline_find(name, GLINE_BADCHAN | GLINE_EXACT)) &&
        GlineIsActive(gline) && !IsAnOper(sptr)) {
-      send_reply(sptr, ERR_BADCHANNAME, name);
+      send_reply(sptr, ERR_BANNEDFROMCHAN, name);
       continue;
     }
-#endif
 
     if ((chptr = FindChannel(name))) {
       if (find_member_link(chptr, sptr))
@@ -227,21 +228,31 @@ int m_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
     } else
       flags = IsModelessChannel(name) ? CHFL_DEOPPED : CHFL_CHANOP;
 
-    if (sptr->user->joined >= MAXCHANNELSPERUSER
-#ifdef OPER_NO_CHAN_LIMIT
-       /* Opers are allowed to join any number of channels */
-       && !IsAnOper(sptr)
-#endif
-       ) {
+    if (cli_user(sptr)->joined >= feature_int(FEAT_MAXCHANNELSPERUSER) &&
+       !HasPriv(sptr, PRIV_CHAN_LIMIT)) {
       send_reply(sptr, ERR_TOOMANYCHANNELS, chptr ? chptr->chname : name);
       break; /* no point processing the other channels */
     }
 
     if (chptr) {
+      int is_level0_op = 0;
+      if (!BadPtr(keys) && *chptr->mode.apass) {
+       /* Don't use compall for the apass, only a single key is allowed.
+          Test Apass first in case someone set Apass and upass equal. */
+       if (strcmp(chptr->mode.apass, keys) == 0) {
+         is_level0_op = 1;
+         flags &= ~CHFL_DEOPPED;
+         flags |= CHFL_CHANOP | CHFL_CHANNEL_MANAGER;
+       }
+       else if (*chptr->mode.upass && strcmp(chptr->mode.upass, keys) == 0) {
+         is_level0_op = 1;
+         flags &= ~CHFL_DEOPPED;
+         flags |= CHFL_CHANOP;
+       }
+      }
       if (check_target_limit(sptr, chptr, chptr->chname, 0))
        continue; /* exceeded target limit */
-      else if ((i = can_join(sptr, chptr, keys))) {
-#ifdef OPER_WALK_THROUGH_LMODES
+      else if (!is_level0_op && (i = can_join(sptr, chptr, keys))) {
        if (i > MAGIC_OPER_OVERRIDE) { /* oper overrode mode */
          switch (i - MAGIC_OPER_OVERRIDE) {
          case ERR_CHANNELISFULL: /* figure out which mode */
@@ -268,21 +279,28 @@ int m_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
          send_reply(sptr, i, chptr->chname);
          continue;
        }
-#else
-       send_reply(sptr, i, chptr->chname);
-       continue;
-#endif
-      } /* else if ((i = can_join(sptr, chptr, keys))) { */
+      } /* else if ((i = can_join(sptr, chptr, keys))) */
 
       joinbuf_join(&join, chptr, flags);
+      if (is_level0_op)
+      {
+       joinbuf_flush(&join);
+       modebuf_init(&mbuf, &me, cptr, chptr,
+           MODEBUF_DEST_CHANNEL |              /* Send mode to channel */
+           MODEBUF_DEST_SERVER);               /* And send it to the other servers */
+       modebuf_mode_client(&mbuf,
+           MODE_ADD | MODE_CHANOP, sptr);      /* Give ops to the level0 op */
+       modebuf_flush(&mbuf);
+      }
     } else if (!(chptr = get_channel(sptr, name, CGT_CREATE)))
       continue; /* couldn't get channel */
     else if (check_target_limit(sptr, chptr, chptr->chname, 1)) {
       /* Note: check_target_limit will only ever return 0 here */
-      sub1_from_channel(chptr); /* created it... */
+      chptr->members = 0;
+      destruct_channel(chptr); /* created it... */
       continue;
     } else
-      joinbuf_join(&create, chptr, flags);
+      joinbuf_join(&create, chptr, flags | CHFL_CHANNEL_MANAGER);
 
     del_invite(sptr, chptr);
 
@@ -292,8 +310,7 @@ int m_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
                 chptr->topic_time);
     }
 
-    parv[1] = name;
-    m_names(cptr, sptr, 2, parv); /* XXX find a better way */
+    do_names(sptr, chptr, NAMES_ALL|NAMES_EON); /* send /names list */
   }
 
   joinbuf_flush(&join); /* must be first, if there's a JOIN 0 */
@@ -310,22 +327,23 @@ int ms_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
   struct Membership *member;
   struct Channel *chptr;
   struct JoinBuf join;
-  struct JoinBuf create;
   unsigned int flags = 0;
+  time_t creation = 0;
   char *p = 0;
   char *chanlist;
   char *name;
 
   if (IsServer(sptr)) {
-    Debug((DEBUG_ERROR, "%s tried to JOIN a channel", sptr->name));
-    return 0;
+    return protocol_violation(sptr,"%s tried to JOIN a channel, duh!", cli_name(sptr));
   }
 
   if (parc < 2 || *parv[1] == '\0')
     return need_more_params(sptr, "JOIN");
 
+  if (parc > 2 && parv[2])
+    creation = atoi(parv[2]);
+
   joinbuf_init(&join, sptr, cptr, JOINBUF_TYPE_JOIN, 0, 0);
-  joinbuf_init(&create, sptr, cptr, JOINBUF_TYPE_CREATE, 0, TStime());
 
   chanlist = last0(parv[1]); /* find last "JOIN 0" */
 
@@ -336,11 +354,26 @@ int ms_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
     if (join0(&join, cptr, sptr, name)) /* did client do a JOIN 0? */
       continue;
 
-    if (IsLocalChannel(name) || !IsChannelName(name))
+    if (IsLocalChannel(name) || !IsChannelName(name)) {
+      protocol_violation(sptr,"%s tried to join %s",cli_name(cptr),name);
       continue;
+    }
 
-    if ((chptr = FindChannel(name))) {
+    if (!(chptr = FindChannel(name))) {
+      /* No channel exists, so create one */
+      if (!(chptr = get_channel(sptr, name, CGT_CREATE))) {
+        protocol_violation(sptr,"couldn't get channel %s for %s",
+                          name,cli_name(sptr));
+       continue;
+      }
+      flags = CHFL_DEOPPED | ((cli_flags(sptr) & FLAGS_TS8) ? CHFL_SERVOPOK : 0);
+
+      /* when the network is 2.10.11+ then remove MAGIC_REMOTE_JOIN_TS */ 
+      chptr->creationtime = creation ? creation : MAGIC_REMOTE_JOIN_TS;
+    }
+    else { /* We have a valid channel? */
       if ((member = find_member_link(chptr, sptr))) {
+       /* It is impossible to get here --Run */
        if (!IsZombie(member)) /* already on channel */
          continue;
 
@@ -348,30 +381,17 @@ int ms_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
        remove_user_from_channel(sptr, chptr);
        chptr = FindChannel(name);
       } else
-       flags = CHFL_DEOPPED | ((sptr->flags & FLAGS_TS8) ? CHFL_SERVOPOK : 0);
-    } else
-      flags = IsModelessChannel(name) ? CHFL_DEOPPED : CHFL_CHANOP;
-
-    if (chptr)
-      joinbuf_join(&join, chptr, flags);
-    /* Strange that a channel should get created by a JOIN, but as long
-     * as there are desynchs, we have to assume it's possible.  Previous
-     * solutions involved sending a TS with the JOIN, but that seems
-     * unworkable to me; my solution is to transmute the JOIN into a
-     * CREATE, and let the next BURST try to fix the problem.  Another
-     * solution would be to simply issue an upstream KICK, but that
-     * won't currently be accepted.  Is this what DESTRUCT is for?
-     */
-    else if (!(chptr = get_channel(sptr, name, CGT_CREATE)))
-      continue; /* couldn't get channel */
-    else {
-      chptr->creationtime = TStime(); /* have to set the creation TS */
-      joinbuf_join(&create, chptr, flags);
-    }
+       flags = CHFL_DEOPPED | ((cli_flags(sptr) & FLAGS_TS8) ? CHFL_SERVOPOK : 0);
+      /* Always copy the timestamp when it is older, that is the only way to
+         ensure network-wide synchronization of creation times. */
+      if (creation && creation < chptr->creationtime)
+       chptr->creationtime = creation;
+    } 
+
+    joinbuf_join(&join, chptr, flags);
   }
 
-  joinbuf_flush(&join); /* must be first, if there's a JOIN 0 */
-  joinbuf_flush(&create);
+  joinbuf_flush(&join); /* flush joins... */
 
   return 0;
 }