Author: Perry Lorier <isomer@coders.net>
[ircu2.10.12-pk.git] / ircd / m_mode.c
index 8758e54cdb52d2ca9367dca14ff30df173ef16bf..e3c6a21a4d9e35744732c1d131b8186cdd5fa6b0 100644 (file)
@@ -89,6 +89,7 @@
 #include "msg.h"
 #include "numeric.h"
 #include "numnicks.h"
+#include "s_conf.h"
 #include "s_debug.h"
 #include "s_user.h"
 #include "send.h"
 #include <stdlib.h>
 #include <string.h>
 
+#ifdef CONFIG_NEW_MODE
+int
+m_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
+{
+  struct Channel *chptr = 0;
+  struct ModeBuf mbuf;
+  struct Membership *member;
+
+  if (parc < 2)
+    return need_more_params(sptr, "MODE");
+
+  clean_channelname(parv[1]);
+
+  if (('#' != *parv[1] && '&' != *parv[1] && '+' != *parv[1]) || 
+      !(chptr = FindChannel(parv[1])))
+    return set_user_mode(cptr, sptr, parc, parv);
+
+  sptr->flags &= ~FLAGS_TS8;
+
+  if (parc < 3) {
+    char modebuf[MODEBUFLEN];
+    char parabuf[MODEBUFLEN];
+
+    *modebuf = *parabuf = '\0';
+    modebuf[1] = '\0';
+    channel_modes(sptr, modebuf, parabuf, chptr);
+    send_reply(sptr, RPL_CHANNELMODEIS, chptr->chname, modebuf, parabuf);
+    send_reply(sptr, RPL_CREATIONTIME, chptr->chname, chptr->creationtime);
+    return 0;
+  }
+
+  if (!(member = find_member_link(chptr, sptr)) || !IsChanOp(member)) {
+#ifdef OPER_MODE_LCHAN
+    if (IsOperOnLocalChannel(sptr, chptr->chname)) {
+      modebuf_init(&mbuf, sptr, cptr, chptr,
+                  (MODEBUF_DEST_CHANNEL | /* Send mode to channel */
+                   MODEBUF_DEST_HACK4));  /* Send HACK(4) notice */
+      mode_parse(&mbuf, cptr, sptr, chptr, parc - 2, parv + 2,
+                (MODE_PARSE_SET |    /* Set the mode */
+                 MODE_PARSE_FORCE)); /* Force it to take */
+      return modebuf_flush(&mbuf);
+    } else
+#endif
+      mode_parse(0, cptr, sptr, chptr, parc - 2, parv + 2,
+                (member ? MODE_PARSE_NOTOPER : MODE_PARSE_NOTMEMBER));
+    return 0;
+  }
+
+  modebuf_init(&mbuf, sptr, cptr, chptr,
+              (MODEBUF_DEST_CHANNEL | /* Send mode to channel */
+               MODEBUF_DEST_SERVER)); /* Send mode to servers */
+  mode_parse(&mbuf, cptr, sptr, chptr, parc - 2, parv + 2, MODE_PARSE_SET);
+  return modebuf_flush(&mbuf);
+}
+
+int
+ms_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
+{
+  struct Channel *chptr = 0;
+  struct ModeBuf mbuf;
+  struct Membership *member;
+
+  if (parc < 3)
+    return need_more_params(sptr, "MODE");
+
+  if (IsLocalChannel(parv[1]))
+    return 0;
+
+  if (('#' != *parv[1] && '+' != *parv[1])|| !(chptr = FindChannel(parv[1])))
+    return set_user_mode(cptr, sptr, parc, parv);
+
+  sptr->flags &= ~FLAGS_TS8;
+
+  if (IsServer(sptr)) {
+    if (find_conf_byhost(cptr->confs, sptr->name, CONF_UWORLD))
+      modebuf_init(&mbuf, sptr, cptr, chptr,
+                  (MODEBUF_DEST_CHANNEL | /* Send mode to clients */
+                   MODEBUF_DEST_SERVER  | /* Send mode to servers */
+                   MODEBUF_DEST_HACK4));  /* Send a HACK(4) message */
+    else
+      modebuf_init(&mbuf, sptr, cptr, chptr,
+                  (MODEBUF_DEST_CHANNEL | /* Send mode to clients */
+                   MODEBUF_DEST_SERVER  | /* Send mode to servers */
+                   MODEBUF_DEST_HACK3));  /* Send a HACK(3) message */
+
+    mode_parse(&mbuf, cptr, sptr, chptr, parc - 2, parv + 2,
+              (MODE_PARSE_SET    | /* Set the mode */
+               MODE_PARSE_STRICT | /* Interpret it strictly */
+               MODE_PARSE_FORCE)); /* And force it to be accepted */
+  } else {
+    if (!(member = find_member_link(chptr, sptr)) || !IsChanOp(member)) {
+      modebuf_init(&mbuf, sptr, cptr, chptr,
+                  (MODEBUF_DEST_SERVER |  /* Send mode to server */
+                   MODEBUF_DEST_HACK2  |  /* Send a HACK(2) message */
+                   MODEBUF_DEST_DEOP   |  /* Deop the source */
+                   MODEBUF_DEST_BOUNCE)); /* And bounce the MODE */
+      mode_parse(&mbuf, cptr, sptr, chptr, parc - 2, parv + 2,
+                (MODE_PARSE_STRICT |  /* Interpret it strictly */
+                 MODE_PARSE_BOUNCE)); /* And bounce the MODE */
+    } else {
+      modebuf_init(&mbuf, sptr, cptr, chptr,
+                  (MODEBUF_DEST_CHANNEL | /* Send mode to clients */
+                   MODEBUF_DEST_SERVER)); /* Send mode to servers */
+      mode_parse(&mbuf, cptr, sptr, chptr, parc - 2, parv + 2,
+                (MODE_PARSE_SET    | /* Set the mode */
+                 MODE_PARSE_STRICT | /* Interpret it strictly */
+                 MODE_PARSE_FORCE)); /* And force it to be accepted */
+    }
+  }
+
+  return modebuf_flush(&mbuf);
+}
+#else /* CONFIG_NEW_MODE */
 /*
  * m_mode - generic message handler
  */
@@ -125,7 +239,7 @@ int m_mode(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
   /* 
    * try to find the channel
    */
-  if ('#' == *parv[1] || '&' == *parv[1])
+  if ('#' == *parv[1] || '&' == *parv[1] || '+' == *parv[1])
     chptr = FindChannel(parv[1]);
   if (!chptr)
     return set_user_mode(cptr, sptr, parc, parv);
@@ -136,8 +250,7 @@ int m_mode(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
    */
   if (IsModelessChannel(chptr->chname)) {
     if (IsUser(sptr))
-      sendto_one(sptr, rpl_str(RPL_CHANNELMODEIS), me.name, parv[0],
-                 chptr->chname, "+nt", "");
+      send_reply(sptr, RPL_CHANNELMODEIS, chptr->chname, "+nt", "");
     return 0;
   }
 
@@ -148,10 +261,8 @@ int m_mode(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
     *modebuf = *parabuf = '\0';
     modebuf[1] = '\0';
     channel_modes(sptr, modebuf, parabuf, chptr);
-    sendto_one(sptr, rpl_str(RPL_CHANNELMODEIS), me.name, parv[0],
-               chptr->chname, modebuf, parabuf);
-    sendto_one(sptr, rpl_str(RPL_CREATIONTIME), me.name, parv[0],
-               chptr->chname, chptr->creationtime);
+    send_reply(sptr, RPL_CHANNELMODEIS, chptr->chname, modebuf, parabuf);
+    send_reply(sptr, RPL_CREATIONTIME, chptr->chname, chptr->creationtime);
     return 0;
   }
 
@@ -159,27 +270,27 @@ int m_mode(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
 
   if (!(sendts = set_mode(cptr, sptr, chptr, parc - 2, parv + 2,
                           modebuf, parabuf, nparabuf, &badop))) {
-    sendto_one(sptr, err_str(find_channel_member(sptr, chptr) ? ERR_CHANOPRIVSNEEDED :
-        ERR_NOTONCHANNEL), me.name, parv[0], chptr->chname);
+    send_reply(sptr, (find_channel_member(sptr, chptr) ?
+                     ERR_CHANOPRIVSNEEDED : ERR_NOTONCHANNEL), chptr->chname);
     return 0;
   }
 
   if (badop >= 2)
-    send_hack_notice(cptr, sptr, parc, parv, badop, 1);
+    send_hack_notice(cptr, sptr, parc, parv, badop, 1); /* XXX DYING */
 
   if (strlen(modebuf) > 1 || sendts > 0) {
     if (badop != 2 && strlen(modebuf) > 1) {
 #ifdef OPER_MODE_LCHAN
       if (LocalChanOperMode) {
-        sendto_channel_butserv(chptr, &me, ":%s MODE %s %s %s",
-                               me.name, chptr->chname, modebuf, parabuf);
-        sendto_op_mask(SNO_HACK4,"OPER MODE: %s MODE %s %s %s",
-                       sptr->name, chptr->chname, modebuf, parabuf);
+       sendcmdto_channel_butserv(&me, CMD_MODE, chptr, "%H %s %s", chptr,
+                                 modebuf, parabuf);
+        sendto_opmask_butone(0, SNO_HACK4, "OPER MODE: %C MODE %H %s %s",
+                            sptr, chptr, modebuf, parabuf);
       }
       else
 #endif
-      sendto_channel_butserv(chptr, sptr, ":%s MODE %s %s %s",
-          parv[0], chptr->chname, modebuf, parabuf);
+      sendcmdto_channel_butserv(sptr, CMD_MODE, chptr, "%H %s %s", chptr,
+                               modebuf, parabuf);
     }
     if (IsLocalChannel(chptr->chname))
       return 0;
@@ -188,18 +299,14 @@ int m_mode(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
       if (*modebuf == '\0')
         strcpy(modebuf, "+");
       if (badop != 2) {
-        sendto_highprot_butone(cptr, 10, "%s " TOK_MODE " %s %s %s " TIME_T_FMT,
-            NumServ(sptr), chptr->chname, modebuf, nparabuf,
-            (badop == 4) ? (time_t) 0 : chptr->creationtime);
+       sendcmdto_serv_butone(sptr, CMD_MODE, cptr, "%H %s %s %Tu", chptr,
+                             modebuf, nparabuf, (badop == 4) ? (time_t) 0 :
+                             chptr->creationtime);
       }
     }
     else {
-      if (IsServer(sptr))
-         sendto_highprot_butone(cptr, 10, "%s " TOK_MODE " %s %s %s",
-           NumServ(sptr), chptr->chname, modebuf, nparabuf);
-      else
-         sendto_highprot_butone(cptr, 10, "%s%s " TOK_MODE " %s %s %s",
-           NumNick(sptr), chptr->chname, modebuf, nparabuf);
+      sendcmdto_serv_butone(sptr, CMD_MODE, cptr, "%H %s %s", chptr, modebuf,
+                           nparabuf);
     }
   }
   return 0;
@@ -232,7 +339,7 @@ int ms_mode(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
   /* 
    * try to find the channel
    */
-  if ('#' == *parv[1] || '&' == *parv[1])
+  if ('#' == *parv[1] || '&' == *parv[1] || '+' == *parv[1])
     chptr = FindChannel(parv[1]);
   if (!chptr)
     return set_user_mode(cptr, sptr, parc, parv);
@@ -243,8 +350,7 @@ int ms_mode(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
    */
   if (IsModelessChannel(chptr->chname)) {
     if (IsUser(sptr))
-      sendto_one(sptr, rpl_str(RPL_CHANNELMODEIS), me.name, parv[0],
-                 chptr->chname, "+nt", "");
+      send_reply(sptr, RPL_CHANNELMODEIS, chptr->chname, "+nt", "");
     return 0;
   }
 
@@ -255,10 +361,8 @@ int ms_mode(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
     *modebuf = *parabuf = '\0';
     modebuf[1] = '\0';
     channel_modes(sptr, modebuf, parabuf, chptr);
-    sendto_one(sptr, rpl_str(RPL_CHANNELMODEIS), me.name, parv[0],
-               chptr->chname, modebuf, parabuf);
-    sendto_one(sptr, rpl_str(RPL_CREATIONTIME), me.name, parv[0],
-               chptr->chname, chptr->creationtime);
+    send_reply(sptr, RPL_CHANNELMODEIS, chptr->chname, modebuf, parabuf);
+    send_reply(sptr, RPL_CREATIONTIME, chptr->chname, chptr->creationtime);
     return 0;
   }
 
@@ -266,27 +370,27 @@ int ms_mode(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
 
   if (!(sendts = set_mode(cptr, sptr, chptr, parc - 2, parv + 2,
                           modebuf, parabuf, nparabuf, &badop))) {
-    sendto_one(sptr, err_str(find_channel_member(sptr, chptr) ? ERR_CHANOPRIVSNEEDED :
-        ERR_NOTONCHANNEL), me.name, parv[0], chptr->chname);
+    send_reply(sptr, (find_channel_member(sptr, chptr) ?
+                     ERR_CHANOPRIVSNEEDED : ERR_NOTONCHANNEL), chptr->chname);
     return 0;
   }
 
   if (badop >= 2)
-    send_hack_notice(cptr, sptr, parc, parv, badop, 1);
+    send_hack_notice(cptr, sptr, parc, parv, badop, 1); /* XXX DYING */
 
   if (strlen(modebuf) > 1 || sendts > 0) {
     if (badop != 2 && strlen(modebuf) > 1) {
 #ifdef OPER_MODE_LCHAN
       if (LocalChanOperMode) {
-        sendto_channel_butserv(chptr, &me, ":%s MODE %s %s %s",
-                               me.name, chptr->chname, modebuf, parabuf);
-        sendto_op_mask(SNO_HACK4,"OPER MODE: %s MODE %s %s %s",
-                       me.name, chptr->chname, modebuf, parabuf);
+       sendcmdto_channel_butserv(&me, CMD_MODE, chptr, "%H %s %s", chptr,
+                                 modebuf, parabuf);
+        sendto_opmask_butone(0, SNO_HACK4, "OPER MODE: %C MODE %H %s %s",
+                            sptr, chptr, modebuf, parabuf);
       }
       else
 #endif
-      sendto_channel_butserv(chptr, sptr, ":%s MODE %s %s %s",
-          parv[0], chptr->chname, modebuf, parabuf);
+      sendcmdto_channel_butserv(sptr, CMD_MODE, chptr, "%H %s %s", chptr,
+                               modebuf, parabuf);
     }
     if (IsLocalChannel(chptr->chname))
       return 0;
@@ -295,22 +399,19 @@ int ms_mode(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
       if (*modebuf == '\0')
         strcpy(modebuf, "+");
       if (badop != 2) {
-        sendto_highprot_butone(cptr, 10, "%s " TOK_MODE " %s %s %s " TIME_T_FMT,
-            NumServ(sptr), chptr->chname, modebuf, nparabuf,
-            (badop == 4) ? (time_t) 0 : chptr->creationtime);
+       sendcmdto_serv_butone(sptr, CMD_MODE, cptr, "%H %s %s %Tu", chptr,
+                             modebuf, nparabuf, (badop == 4) ? (time_t) 0 :
+                             chptr->creationtime);
       }
     }
     else {
-      if (IsServer(sptr))
-         sendto_highprot_butone(cptr, 10, "%s " TOK_MODE " %s %s %s",
-           NumServ(sptr), chptr->chname, modebuf, nparabuf);
-      else
-         sendto_highprot_butone(cptr, 10, "%s%s " TOK_MODE " %s %s %s",
-           NumNick(sptr), chptr->chname, modebuf, nparabuf);
+      sendcmdto_serv_butone(sptr, CMD_MODE, cptr, "%H %s %s", chptr, modebuf,
+                           nparabuf);
     }
   }
   return 0;
 }
+#endif /* CONFIG_NEW_MODE */
 
 #if 0
 /*
@@ -339,7 +440,7 @@ int m_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
   /* 
    * try to find the channel
    */
-  if ('#' == *parv[1] || '&' == *parv[1])
+  if ('#' == *parv[1] || '&' == *parv[1] || '+' == *parv[1])
     chptr = FindChannel(parv[1]);
   if (!chptr)
     return set_user_mode(cptr, sptr, parc, parv);
@@ -350,7 +451,7 @@ int m_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
    */
   if (IsModelessChannel(chptr->chname)) {
     if (IsUser(sptr))
-      sendto_one(sptr, rpl_str(RPL_CHANNELMODEIS), me.name, parv[0],
+      sendto_one(sptr, rpl_str(RPL_CHANNELMODEIS), me.name, parv[0], /* XXX DEAD */
                  chptr->chname, "+nt", "");
     return 0;
   }
@@ -362,9 +463,9 @@ int m_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
     *modebuf = *parabuf = '\0';
     modebuf[1] = '\0';
     channel_modes(sptr, modebuf, parabuf, chptr);
-    sendto_one(sptr, rpl_str(RPL_CHANNELMODEIS), me.name, parv[0],
+    sendto_one(sptr, rpl_str(RPL_CHANNELMODEIS), me.name, parv[0], /* XXX DEAD */
                chptr->chname, modebuf, parabuf);
-    sendto_one(sptr, rpl_str(RPL_CREATIONTIME), me.name, parv[0],
+    sendto_one(sptr, rpl_str(RPL_CREATIONTIME), me.name, parv[0], /* XXX DEAD */
                chptr->chname, chptr->creationtime);
     return 0;
   }
@@ -373,7 +474,7 @@ int m_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
 
   if (!(sendts = set_mode(cptr, sptr, chptr, parc - 2, parv + 2,
                           modebuf, parabuf, nparabuf, &badop))) {
-    sendto_one(sptr, err_str(find_channel_member(sptr, chptr) ? ERR_CHANOPRIVSNEEDED :
+    sendto_one(sptr, err_str(find_channel_member(sptr, chptr) ? ERR_CHANOPRIVSNEEDED : /* XXX DEAD */
         ERR_NOTONCHANNEL), me.name, parv[0], chptr->chname);
     return 0;
   }
@@ -385,14 +486,14 @@ int m_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
     if (badop != 2 && strlen(modebuf) > 1) {
 #ifdef OPER_MODE_LCHAN
       if (LocalChanOperMode) {
-        sendto_channel_butserv(chptr, &me, ":%s MODE %s %s %s",
+        sendto_channel_butserv(chptr, &me, ":%s MODE %s %s %s", /* XXX DEAD */
                                me.name, chptr->chname, modebuf, parabuf);
-        sendto_op_mask(SNO_HACK4,"OPER MODE: %s MODE %s %s %s",
+        sendto_op_mask(SNO_HACK4,"OPER MODE: %s MODE %s %s %s", /* XXX DEAD */
                        me.name, chptr->chname, modebuf, parabuf);
       }
       else
 #endif
-      sendto_channel_butserv(chptr, sptr, ":%s MODE %s %s %s",
+      sendto_channel_butserv(chptr, sptr, ":%s MODE %s %s %s", /* XXX DEAD */
           parv[0], chptr->chname, modebuf, parabuf);
     }
     if (IsLocalChannel(chptr->chname))
@@ -402,17 +503,17 @@ int m_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
       if (*modebuf == '\0')
         strcpy(modebuf, "+");
       if (badop != 2) {
-        sendto_highprot_butone(cptr, 10, "%s " TOK_MODE " %s %s %s " TIME_T_FMT,
+        sendto_highprot_butone(cptr, 10, "%s " TOK_MODE " %s %s %s " TIME_T_FMT, /* XXX DEAD */
             NumServ(sptr), chptr->chname, modebuf, nparabuf,
             (badop == 4) ? (time_t) 0 : chptr->creationtime);
       }
     }
     else {
       if (IsServer(sptr))
-         sendto_highprot_butone(cptr, 10, "%s " TOK_MODE " %s %s %s",
+         sendto_highprot_butone(cptr, 10, "%s " TOK_MODE " %s %s %s", /* XXX DEAD */
            NumServ(sptr), chptr->chname, modebuf, nparabuf);
       else
-         sendto_highprot_butone(cptr, 10, "%s%s " TOK_MODE " %s %s %s",
+         sendto_highprot_butone(cptr, 10, "%s%s " TOK_MODE " %s %s %s", /* XXX DEAD */
            NumNick(sptr), chptr->chname, modebuf, nparabuf);
     }
   }