Author: Isomer <isomer@coders.net>
[ircu2.10.12-pk.git] / ircd / jupe.c
index bde16cbf9665a3decccb4e8b19721032f5dc39dd..c49b4faee2f87cfc1ecb543eb86fb97a6fe6c99d 100644 (file)
@@ -25,6 +25,7 @@
 #include "hash.h"
 #include "ircd.h"
 #include "ircd_alloc.h"
+#include "ircd_reply.h"
 #include "ircd_string.h"
 #include "match.h"
 #include "msg.h"
@@ -38,8 +39,9 @@
 #include "sys.h"    /* FALSE bleah */
 
 #include <assert.h>
+#include <string.h>
 
-static struct Jupe* GlobalJupeList  = 0;
+static struct Jupe *GlobalJupeList = 0;
 
 static struct Jupe *
 make_jupe(char *server, char *reason, time_t expire, time_t lastmod,
@@ -50,13 +52,13 @@ make_jupe(char *server, char *reason, time_t expire, time_t lastmod,
   ajupe = (struct Jupe*) MyMalloc(sizeof(struct Jupe)); /* alloc memory */
   assert(0 != ajupe);
 
-  DupString(ajupe->ju_server, server);        /* copy vital information */
+  DupString(ajupe->ju_server, server); /* copy vital information */
   DupString(ajupe->ju_reason, reason);
   ajupe->ju_expire = expire;
   ajupe->ju_lastmod = lastmod;
-  ajupe->ju_flags = flags;        /* set jupe flags */
+  ajupe->ju_flags = flags & JUPE_MASK; /* set jupe flags */
 
-  ajupe->ju_next = GlobalJupeList;       /* link it into the list */
+  ajupe->ju_next = GlobalJupeList; /* link it into the list */
   ajupe->ju_prev_p = &GlobalJupeList;
   if (GlobalJupeList)
     GlobalJupeList->ju_prev_p = &ajupe->ju_next;
@@ -88,26 +90,17 @@ propagate_jupe(struct Client *cptr, struct Client *sptr, struct Jupe *jupe)
   if (JupeIsLocal(jupe)) /* don't propagate local jupes */
     return;
 
-  if (IsUser(sptr)) /* select correct prefix */
-    sendto_serv_butone(cptr, "%s%s " TOK_JUPE " * %c%s " TIME_T_FMT " "
-                      TIME_T_FMT " :%s", NumNick(sptr),
-                      JupeIsActive(jupe) ? '+' : '-', jupe->ju_server,
-                      jupe->ju_expire - TStime(), jupe->ju_lastmod,
-                      jupe->ju_reason);
-  else
-    sendto_serv_butone(cptr, "%s " TOK_JUPE " * %c%s " TIME_T_FMT " "
-                      TIME_T_FMT " :%s", NumServ(sptr),
-                      JupeIsActive(jupe) ? '+' : '-', jupe->ju_server,
-                      jupe->ju_expire - TStime(), jupe->ju_lastmod,
-                      jupe->ju_reason);
+  sendcmdto_serv_butone(sptr, CMD_JUPE, cptr, "* %c%s %Tu %Tu :%s",
+                       JupeIsRemActive(jupe) ? '+' : '-', jupe->ju_server,
+                       jupe->ju_expire - CurrentTime, jupe->ju_lastmod,
+                       jupe->ju_reason);
 }
 
 int
 jupe_add(struct Client *cptr, struct Client *sptr, char *server, char *reason,
-        time_t expire, time_t lastmod, int local, int active)
+        time_t expire, time_t lastmod, unsigned int flags)
 {
   struct Jupe *ajupe;
-  unsigned int flags = 0;
 
   assert(0 != server);
   assert(0 != reason);
@@ -118,38 +111,24 @@ jupe_add(struct Client *cptr, struct Client *sptr, char *server, char *reason,
    */
   if (expire <= 0 || expire > JUPE_MAX_EXPIRE) {
     if (!IsServer(cptr) && MyConnect(cptr))
-      sendto_one(cptr, err_str(ERR_BADEXPIRE), me.name, cptr->name, expire);
+      send_reply(cptr, ERR_BADEXPIRE, expire);
     return 0;
   }
 
-  expire += TStime(); /* convert from lifetime to timestamp */
+  expire += CurrentTime; /* convert from lifetime to timestamp */
 
   /* Inform ops and log it */
-  if (IsServer(sptr)) {
-    sendto_op_mask(SNO_NETWORK, "%s adding %sJUPE for %s, expiring at "
-                  TIME_T_FMT ": %s", sptr->name, local ? "local " : "",
-                  server, expire, reason);
-#ifdef JPATH
-    write_log(JPATH, TIME_T_FMT " %s adding %sJUPE for %s, expiring at "
-             TIME_T_FMT ": %s\n", TStime(), sptr->name,
-             local ? "local " : "", server, expire, reason);
-#endif /* JPATH */
-  } else {
-    sendto_op_mask(SNO_NETWORK, "%s adding %sJUPE for %s, expiring at "
-                  TIME_T_FMT ": %s", sptr->user->server->name,
-                  local ? "local " : "", server, expire,
-                  reason);
+  sendto_opmask_butone(0, SNO_NETWORK, "%s adding %sJUPE for %s, expiring at "
+                      "%Tu: %s", IsServer(sptr) ? sptr->name :
+                      sptr->user->server->name,
+                      flags & JUPE_LOCAL ? "local " : "", server,
+                      expire + TSoffset, reason);
+
 #ifdef JPATH
-    write_log(JPATH, TIME_T_FMT, " %s!%s@%s adding %sJUPE for %s, expiring at "
-             TIME_T_FMT ": %s\n", TStime(), sptr->name, sptr->user->username,
-             sptr->user->host, local ? "local " : "", server, expire, reason);
+  write_log(JPATH, "%Tu %C adding %sJUPE for %s, expiring at %Tu: %s\n",
+           TStime(), sptr, flags & JUPE_LOCAL ? "local " : "", server,
+           expire + TSoffset, reason);
 #endif /* JPATH */
-  }
-
-  if (active) /* compute initial flags */
-    flags |= JUPE_ACTIVE;
-  if (local)
-    flags |= JUPE_LOCAL;
 
   /* make the jupe */
   ajupe = make_jupe(server, reason, expire, lastmod, flags);
@@ -161,80 +140,92 @@ jupe_add(struct Client *cptr, struct Client *sptr, char *server, char *reason,
 
 int
 jupe_activate(struct Client *cptr, struct Client *sptr, struct Jupe *jupe,
-             time_t lastmod)
+             time_t lastmod, unsigned int flags)
 {
+  unsigned int saveflags = 0;
+
   assert(0 != jupe);
+  assert(!JupeIsLocal(jupe));
 
-  jupe->ju_flags |= JUPE_ACTIVE;
-  jupe->ju_lastmod = lastmod;
+  saveflags = jupe->ju_flags;
+
+  if (flags & JUPE_LOCAL)
+    jupe->ju_flags &= ~JUPE_LDEACT;
+  else {
+    jupe->ju_flags |= JUPE_ACTIVE;
+
+    if (jupe->ju_lastmod >= lastmod) /* force lastmod to increase */
+      jupe->ju_lastmod++;
+    else
+      jupe->ju_lastmod = lastmod;
+  }
+
+  if ((saveflags & JUPE_ACTMASK) == JUPE_ACTIVE)
+    return 0; /* was active to begin with */
 
   /* Inform ops and log it */
-  if (IsServer(sptr)) {
-    sendto_op_mask(SNO_NETWORK, "%s activating %sJUPE for %s, expiring at "
-                  TIME_T_FMT ": %s", sptr->name, JupeIsLocal(jupe) ?
-                  "local " : "", jupe->ju_server, jupe->ju_expire,
-                  jupe->ju_reason);
-#ifdef JPATH
-    write_log(JPATH, TIME_T_FMT " %s activating %sJUPE for %s, expiring at "
-             TIME_T_FMT ": %s\n", TStime(), sptr->name, JupeIsLocal(jupe) ?
-             "local " : "", jupe->ju_server, jupe->ju_expire,
-             jupe->ju_reason);
-#endif /* JPATH */
-  } else {
-    sendto_op_mask(SNO_NETWORK, "%s activating %sJUPE for %s, expiring at "
-                  TIME_T_FMT ": %s", sptr->user->server->name,
-                  JupeIsLocal(jupe) ? "local " : "", jupe->ju_server,
-                  jupe->ju_expire, jupe->ju_reason);
+  sendto_opmask_butone(0, SNO_NETWORK, "%s activating JUPE for %s, expiring "
+                      "at %Tu: %s",
+                      IsServer(sptr) ? sptr->name : sptr->user->server->name,
+                      jupe->ju_server, jupe->ju_expire + TSoffset,
+                      jupe->ju_reason);
+
 #ifdef JPATH
-    write_log(JPATH, TIME_T_FMT, " %s!%s@%s activating %sJUPE for %s, "
-             "expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
-             sptr->user->username, sptr->user->host, JupeIsLocal(jupe) ?
-             "local " : "", jupe->ju_server, jupe->ju_expire,
-             jupe->ju_reason);
+  write_log(JPATH, "%Tu %C activating JUPE for %s, expiring at %Tu: %s\n",
+           TStime(), sptr, jupe->ju_server, jupe->ju_expire + TSoffset,
+           jupe->ju_reason);
 #endif /* JPATH */
-  }
 
-  propagate_jupe(cptr, sptr, jupe);
+  if (!(flags & JUPE_LOCAL)) /* don't propagate local changes */
+    propagate_jupe(cptr, sptr, jupe);
 
   return do_jupe(cptr, sptr, jupe);
 }
 
 int
 jupe_deactivate(struct Client *cptr, struct Client *sptr, struct Jupe *jupe,
-               time_t lastmod)
+               time_t lastmod, unsigned int flags)
 {
+  unsigned int saveflags = 0;
+
   assert(0 != jupe);
 
-  jupe->ju_flags &= ~JUPE_ACTIVE;
-  jupe->ju_lastmod = lastmod;
+  saveflags = jupe->ju_flags;
+
+  if (!JupeIsLocal(jupe)) {
+    if (flags & JUPE_LOCAL)
+      jupe->ju_flags |= JUPE_LDEACT;
+    else {
+      jupe->ju_flags &= ~JUPE_ACTIVE;
+
+      if (jupe->ju_lastmod >= lastmod) /* force lastmod to increase */
+       jupe->ju_lastmod++;
+      else
+       jupe->ju_lastmod = lastmod;
+    }
+
+    if ((saveflags & JUPE_ACTMASK) != JUPE_ACTIVE)
+      return 0; /* was inactive to begin with */
+  }
 
   /* Inform ops and log it */
-  if (IsServer(sptr)) {
-    sendto_op_mask(SNO_NETWORK, "%s deactivating %sJUPE for %s, expiring at "
-                  TIME_T_FMT ": %s", sptr->name, JupeIsLocal(jupe) ?
-                  "local " : "", jupe->ju_server, jupe->ju_expire,
-                  jupe->ju_reason);
-#ifdef JPATH
-    write_log(JPATH, TIME_T_FMT " %s deactivating %sJUPE for %s, expiring at "
-             TIME_T_FMT ": %s\n", TStime(), sptr->name, JupeIsLocal(jupe) ?
-             "local " : "", jupe->ju_server, jupe->ju_expire,
-             jupe->ju_reason);
-#endif /* JPATH */
-  } else {
-    sendto_op_mask(SNO_NETWORK, "%s deactivating %sJUPE for %s, expiring at "
-                  TIME_T_FMT ": %s", sptr->user->server->name,
-                  JupeIsLocal(jupe) ? "local " : "", jupe->ju_server,
-                  jupe->ju_expire, jupe->ju_reason);
+  sendto_opmask_butone(0, SNO_NETWORK, "%s %s JUPE for %s, expiring at %Tu: "
+                      "%s",
+                      IsServer(sptr) ? sptr->name : sptr->user->server->name,
+                      JupeIsLocal(jupe) ? "removing local" : "deactivating",
+                      jupe->ju_server, jupe->ju_expire + TSoffset,
+                      jupe->ju_reason);
+
 #ifdef JPATH
-    write_log(JPATH, TIME_T_FMT, " %s!%s@%s deactivating %sJUPE for %s, "
-             "expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
-             sptr->user->username, sptr->user->host, JupeIsLocal(jupe) ?
-             "local " : "", jupe->ju_server, jupe->ju_expire,
-             jupe->ju_reason);
+  write_log(JPATH, "%Tu %s %s JUPE for %s, expiring at %Tu: %s\n", TStime(),
+           sptr, JupeIsLocal(jupe) ? "removing local" : "deactivating",
+           jupe->ju_server, jupe->ju_expire + TSoffset, jupe->ju_reason);
 #endif /* JPATH */
-  }
 
-  propagate_jupe(cptr, sptr, jupe);
+  if (JupeIsLocal(jupe))
+    jupe_free(jupe);
+  else if (!(flags & JUPE_LOCAL)) /* don't propagate local changes */
+    propagate_jupe(cptr, sptr, jupe);
 
   return 0;
 }
@@ -248,7 +239,7 @@ jupe_find(char *server)
   for (jupe = GlobalJupeList; jupe; jupe = sjupe) { /* go through jupes */
     sjupe = jupe->ju_next;
 
-    if (jupe->ju_expire <= TStime()) /* expire any that need expiring */
+    if (jupe->ju_expire <= CurrentTime) /* expire any that need expiring */
       jupe_free(jupe);
     else if (0 == ircd_strcmp(server, jupe->ju_server)) /* found it yet? */
       return jupe;
@@ -280,13 +271,13 @@ jupe_burst(struct Client *cptr)
   for (jupe = GlobalJupeList; jupe; jupe = sjupe) { /* go through jupes */
     sjupe = jupe->ju_next;
 
-    if (jupe->ju_expire <= TStime()) /* expire any that need expiring */
+    if (jupe->ju_expire <= CurrentTime) /* expire any that need expiring */
       jupe_free(jupe);
     else if (!JupeIsLocal(jupe)) /* forward global jupes */
-      sendto_one(cptr, "%s " TOK_JUPE " * %c%s "TIME_T_FMT" "TIME_T_FMT" :%s",
-                NumServ(&me), JupeIsActive(jupe) ? '+' : '-',
-                jupe->ju_server, jupe->ju_expire - TStime(),
-                jupe->ju_lastmod, jupe->ju_reason);
+      sendcmdto_one(&me, CMD_JUPE, cptr, "* %c%s %Tu %Tu :%s",
+                   JupeIsRemActive(jupe) ? '+' : '-', jupe->ju_server,
+                   jupe->ju_expire - CurrentTime, jupe->ju_lastmod,
+                   jupe->ju_reason);
   }
 }
 
@@ -296,9 +287,10 @@ jupe_resend(struct Client *cptr, struct Jupe *jupe)
   if (JupeIsLocal(jupe)) /* don't propagate local jupes */
     return 0;
 
-  sendto_one(cptr, "%s " TOK_JUPE " * %c%s " TIME_T_FMT " " TIME_T_FMT " :%s",
-            NumServ(&me), JupeIsActive(jupe) ? '+' : '-', jupe->ju_server,
-            jupe->ju_expire - TStime(), jupe->ju_lastmod, jupe->ju_reason);
+  sendcmdto_one(&me, CMD_JUPE, cptr, "* %c%s %Tu %Tu :%s",
+               JupeIsRemActive(jupe) ? '+' : '-', jupe->ju_server,
+               jupe->ju_expire - CurrentTime, jupe->ju_lastmod,
+               jupe->ju_reason);
 
   return 0;
 }
@@ -310,30 +302,27 @@ jupe_list(struct Client *sptr, char *server)
   struct Jupe *sjupe;
 
   if (server) {
-    if (!(jupe = jupe_find(server))) { /* no such jupe */
-      sendto_one(sptr, err_str(ERR_NOSUCHJUPE), me.name, sptr->name, server);
-      return 0;
-    }
+    if (!(jupe = jupe_find(server))) /* no such jupe */
+      return send_reply(sptr, ERR_NOSUCHJUPE, server);
 
     /* send jupe information along */
-    sendto_one(sptr, rpl_str(RPL_JUPELIST), me.name, sptr->name,
-              jupe->ju_server, jupe->ju_expire, JupeIsLocal(jupe) ?
-              me.name : "*", JupeIsActive(jupe) ? '+' : '-', jupe->ju_reason);
+    send_reply(sptr, RPL_JUPELIST, jupe->ju_server, jupe->ju_expire + TSoffset,
+              JupeIsLocal(jupe) ? me.name : "*",
+              JupeIsActive(jupe) ? '+' : '-', jupe->ju_reason);
   } else {
     for (jupe = GlobalJupeList; jupe; jupe = sjupe) { /* go through jupes */
       sjupe = jupe->ju_next;
 
-      if (jupe->ju_expire <= TStime()) /* expire any that need expiring */
+      if (jupe->ju_expire <= CurrentTime) /* expire any that need expiring */
        jupe_free(jupe);
       else /* send jupe information along */
-       sendto_one(sptr, rpl_str(RPL_JUPELIST), me.name, sptr->name,
-                  jupe->ju_server, jupe->ju_expire, JupeIsLocal(jupe) ?
-                  me.name : "*", JupeIsActive(jupe) ? '+' : '-',
-                  jupe->ju_reason);
+       send_reply(sptr, RPL_JUPELIST, jupe->ju_server,
+                  jupe->ju_expire + TSoffset,
+                  JupeIsLocal(jupe) ? me.name : "*",
+                  JupeIsActive(jupe) ? '+' : '-', jupe->ju_reason);
     }
   }
 
   /* end of jupe information */
-  sendto_one(sptr, rpl_str(RPL_ENDOFJUPELIST), me.name, sptr->name);
-  return 0;
+  return send_reply(sptr, RPL_ENDOFJUPELIST);
 }