Fix SF bug #2721107 (Gline lifetime changes from servers change the reason.)
[ircu2.10.12-pk.git] / ircd / m_gline.c
index 96761e75dd76c7b625059eab168c81355cbbf05e..549ae140db667df9ebac742cc6efb0ec0ec25635 100644 (file)
@@ -1,4 +1,4 @@
-/*
+\/*
  * IRC - Internet Relay Chat, ircd/m_gline.c
  * Copyright (C) 1990 Jarkko Oikarinen and
  *                    University of Oulu, Computing Center
  *            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 "client.h"
 #include "gline.h"
 #include "hash.h"
 #include "ircd.h"
+#include "ircd_features.h"
+#include "ircd_log.h"
 #include "ircd_reply.h"
 #include "ircd_string.h"
 #include "match.h"
 #include "numeric.h"
 #include "numnicks.h"
 #include "s_conf.h"
+#include "s_debug.h"
 #include "s_misc.h"
 #include "send.h"
-#include "support.h"
 
-#include <assert.h>
+/* #include <assert.h> -- Now using assert in ircd_log.h */
 #include <stdlib.h>
 #include <string.h>
 
+#define PASTWATCH      157680000       /* number of seconds in 5 years */
+
+/*
+ * If the expiration value, interpreted as an absolute timestamp, is
+ * more recent than 5 years in the past, we interpret it as an
+ * absolute timestamp; otherwise, we assume it's relative and convert
+ * it to an absolute timestamp.  Either way, the output of this macro
+ * is an absolute timestamp--not guaranteed to be a *valid* timestamp,
+ * but you can't have everything in a macro ;)
+ */
+#define abs_expire(exp)                                                        \
+  ((exp) >= CurrentTime - PASTWATCH ? (exp) : (exp) + CurrentTime)
+
 /*
  * ms_gline - server message handler
  *
  * parv[0] = Sender prefix
  * parv[1] = Target: server numeric
  * parv[2] = (+|-)<G-line mask>
- * parv[3] = G-line lifetime
- *
- * From Uworld:
- *
- * parv[4] = Comment
- *
- * From somewhere else:
- *
- * parv[4] = Last modification time
- * parv[5] = Comment
  *
+ * For other parameters, see doc/readme.gline.
  */
 int
 ms_gline(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
 {
   struct Client *acptr = 0;
-  struct Gline *agline;
+  struct Gline *agline = 0;
   unsigned int flags = 0;
-  time_t expire_off, lastmod = 0;
-  char *mask = parv[2], *target = parv[1], *reason;
+  enum GlineAction action = GLINE_MODIFY;
+  time_t expire = 0, lastmod = 0, lifetime = 0;
+  char *mask = parv[2], *target = parv[1], *reason = "No reason", *tmp = 0;
 
-  if (parc == 4) {
-    if (!find_conf_byhost(cptr->confs, sptr->name, CONF_UWORLD))
-      return need_more_params(sptr, "GLINE");
-
-    reason = parv[4];
-    flags |= GLINE_FORCE;
-  } else if (parc >= 5) {
-    lastmod = atoi(parv[4]);
-    reason = parv[5];
-  } else
+  if (parc < 3)
     return need_more_params(sptr, "GLINE");
 
-  if (!(target[0] == '*' && target[1] == '\0')) {
-    if (!(acptr = FindNServer(target)))
-      return 0; /* no such server */
-
-    if (!IsMe(acptr)) { /* manually propagate */
-      if (IsServer(sptr)) {
-       if (!lastmod)
-         sendto_one(acptr, "%s " TOK_GLINE " %s %s %s :%s", NumServ(sptr),
-                    target, mask, parv[3], reason);
-       else
-         sendto_one(acptr, "%s " TOK_GLINE " %s %s %s %s :%s", NumServ(sptr),
-                    target, mask, parv[3], parv[4], reason);
-      } else
-       sendto_one(acptr, "%s%s " TOK_GLINE " %s %s %s %s :%s", NumNick(sptr),
-                  target, mask, parv[3], parv[4], reason);
-
-      return 0;
-    }
-
-    flags |= GLINE_LOCAL;
-  }
+  if (IsServer(sptr))
+    flags |= GLINE_FORCE;
 
-  if (*mask == '-')
-    mask++;
-  else if (*mask == '+') {
-    flags |= GLINE_ACTIVE;
+  if (*mask == '!') {
     mask++;
-  } else
-    flags |= GLINE_ACTIVE;
-
-  expire_off = atoi(parv[3]);
-
-  agline = gline_find(mask, GLINE_ANY);
-
-  if (agline) {
-    if (GlineIsLocal(agline) && !(flags & GLINE_LOCAL)) /* global over local */
-      gline_free(agline);
-    else if (!lastmod || GlineLastMod(agline) < lastmod) { /* new mod */
-      if (flags & GLINE_ACTIVE)
-       return gline_activate(cptr, sptr, agline, lastmod);
-      else
-       return gline_deactivate(cptr, sptr, agline, lastmod);
-    } else if (GlineLastMod(agline) == lastmod)
-      return 0;
-    else
-      return gline_resend(cptr, agline); /* other server desynched WRT gline */
+    flags |= GLINE_OPERFORCE; /* assume oper had WIDE_GLINE */
   }
 
-  return gline_add(cptr, sptr, mask, reason, expire_off, lastmod, flags);
-}
-
-/*
- * mo_gline - oper message handler
- *
- * parv[0] = Sender prefix
- * parv[1] = [[+|-]<G-line mask>]
- *
- * Old style:
- *
- * parv[2] = [Expiration offset]
- * parv[3] = [Comment]
- *
- * New style:
- *
- * parv[2] = [target]
- * parv[3] = [Expiration offset]
- * parv[4] = [Comment]
- *
- */
-int
-mo_gline(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
-{
-  struct Client *acptr = 0;
-  struct Gline *agline;
-  unsigned int flags = 0;
-  time_t expire_off;
-  char *mask = parv[1], *target = 0, *reason;
-
-  if (parc < 2)
-    return gline_list(sptr, 0);
+  switch (*mask) { /* handle +, -, <, and > */
+  case '+': /* activate the G-line */
+    action = GLINE_ACTIVATE;
+    mask++;
+    break;
 
-  if (*mask == '+') {
-    flags |= GLINE_ACTIVE;
+  case '-': /* deactivate the G-line */
+    action = GLINE_DEACTIVATE;
     mask++;
-  } else if (*mask == '-')
+    break;
+
+  case '>': /* locally activate the G-line */
+    action = GLINE_LOCAL_ACTIVATE;
     mask++;
-  else
-    return gline_list(sptr, mask);
+    break;
 
-#ifndef LOCOP_LGLINE
-  if (!IsOper(sptr)) {
-    send_error_to_client(sptr, ERR_NOPRIVILEGES);
-    return 0;
+  case '<': /* locally deactivate the G-line */
+    action = GLINE_LOCAL_DEACTIVATE;
+    mask++;
+    break;
   }
-#endif
 
-  if (parc == 3) {
-    expire_off = atoi(parv[2]);
-    reason = parv[3];
+  /* Now, let's figure out if it's a local or global G-line */
+  if (action == GLINE_LOCAL_ACTIVATE || action == GLINE_LOCAL_DEACTIVATE ||
+      (target[0] == '*' && target[1] == '\0'))
+    flags |= GLINE_GLOBAL;
+  else
     flags |= GLINE_LOCAL;
-  } else if (parc >= 4) {
-    target = parv[2];
-    expire_off = atoi(parv[3]);
-    reason = parv[4];
-  } else
-    return need_more_params(sptr, "GLINE");
 
-  if (target) {
-    if (!(target[0] == '*' && target[1] == '\0')) {
-      if (!(acptr = find_match_server(target))) {
-       send_error_to_client(sptr, ERR_NOSUCHSERVER, target);
-       return 0;
-      }
+  /* now figure out if we need to resolve a server */
+  if ((action == GLINE_LOCAL_ACTIVATE || action == GLINE_LOCAL_DEACTIVATE ||
+       (flags & GLINE_LOCAL)) && !(acptr = FindNServer(target)))
+    return 0; /* no such server, jump out */
 
-      if (!IsMe(acptr)) { /* manually propagate, since we don't set it */
-       if (!IsOper(sptr)) {
-         send_error_to_client(sptr, ERR_NOPRIVILEGES);
-         return 0;
-       }
+  /* If it's a local activate/deactivate and server isn't me, propagate it */
+  if ((action == GLINE_LOCAL_ACTIVATE || action == GLINE_LOCAL_DEACTIVATE) &&
+      !IsMe(acptr)) {
+    Debug((DEBUG_DEBUG, "I am forwarding a local change to a global gline "
+          "to a remote server; target %s, mask %s, operforce %s, action %c",
+          target, mask, flags & GLINE_OPERFORCE ? "YES" : "NO",
+          action == GLINE_LOCAL_ACTIVATE ? '>' : '<'));
 
-       sendto_one(acptr, "%s%s " TOK_GLINE " %s %c%s %s " TIME_T_FMT " :%s",
-                  NumNick(sptr), NumServ(acptr),
-                  flags & GLINE_ACTIVE ? '?' : '-', mask, parv[3], TStime(),
-                  reason);
-       return 0;
-      }
+    sendcmdto_one(sptr, CMD_GLINE, acptr, "%C %s%c%s", acptr,
+                 flags & GLINE_OPERFORCE ? "!" : "",
+                 action == GLINE_LOCAL_ACTIVATE ? '>' : '<', mask);
 
-      flags |= GLINE_LOCAL;
-    } else if (!IsOper(sptr)) {
-      send_error_to_client(sptr, ERR_NOPRIVILEGES);
-      return 0;
-    }
+    return 0; /* all done */
   }
 
-  agline = gline_find(mask, GLINE_ANY);
+  /* Next, try to find the G-line... */
+  if ((flags & GLINE_GLOBAL) || IsMe(acptr)) /* don't bother if it's not me! */
+    agline = gline_find(mask, flags | GLINE_ANY | GLINE_EXACT);
 
-  if (agline) {
-    if (GlineIsLocal(agline) && !(flags & GLINE_LOCAL)) /* global over local */
-      gline_free(agline);
-    else {
-      if (flags & GLINE_ACTIVE)
-       return gline_activate(cptr, sptr, agline, TStime());
-      else
-       return gline_deactivate(cptr, sptr, agline, TStime());
-    }
-  }
+  /* We now have all the pieces to tell us what we've got; let's put
+   * it all together and convert the rest of the arguments.
+   */
 
-  return gline_add(cptr, sptr, mask, reason, expire_off, TStime(), flags);
-}
+  /* Handle the local G-lines first... */
+  if (flags & GLINE_LOCAL) {
+    assert(acptr);
 
-/*
- * m_gline - user message handler
- *
- * parv[0] = Sender prefix
- * parv[1] = [<server name>]
- *
- */
-int
-m_gline(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
-{
-  if (parc < 2)
-    return gline_list(sptr, 0);
+    /* normalize the action, first */
+    if (action == GLINE_LOCAL_ACTIVATE || action == GLINE_MODIFY)
+      action = GLINE_ACTIVATE;
+    else if (action == GLINE_LOCAL_DEACTIVATE)
+      action = GLINE_DEACTIVATE;
 
-  return gline_list(sptr, parv[1]);
-}
+    if (action == GLINE_ACTIVATE) { /* get expiration and reason */
+      if (parc < 5) /* check parameter count... */
+       return need_more_params(sptr, "GLINE");
 
-#if 0
-/*
- * ms_gline - server message handler
- *
- * parv[0] = Send prefix
- *
- * From server:
- *
- * parv[1] = Target: server numeric
- * parv[2] = [+|-]<G-line mask>
- * parv[3] = Expiration offset
- * parv[4] = Comment
- *
- * From client:
- *
- * parv[1] = [+|-]<G-line mask>
- * parv[2] = Expiration offset
- * parv[3] = Comment
- *
- */
-int ms_gline(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
-{
-  struct Client* acptr = 0;  /* Init. to avoid compiler warning. */
-  struct Gline*  gline;
-  struct Gline*  prev;
-  char*          user;
-  char*          host;
-  int            active;
-  int            ip_mask;
-  int            gtype = 0;
-  time_t         expire = 0;
-
-  /*
-   * Remove expired G-lines
-   */
-  gline_remove_expired(TStime());
-#ifdef BADCHAN
-  /*
-   * Remove expired bad channels
-   */
-  bad_channel_remove_expired(TStime());
-#endif
-
-  if (IsServer(cptr)) {
-    if (find_conf_byhost(cptr->confs, sptr->name, CONF_UWORLD)) {
-      if (parc < 3 || (*parv[2] != '-' && (parc < 5 || *parv[4] == '\0')))
-        return need_more_params(sptr, "GLINE");
-
-      if (*parv[2] == '-') /* add mode or delete mode? */
-        active = 0;
-      else
-        active = 1;
-
-      if (*parv[2] == '+' || *parv[2] == '-')
-        parv[2]++; /* step past mode indicator */
-
-      /*
-       * forward the message appropriately
-       */
-      if (0 == ircd_strcmp(parv[1], "*")) {
-        /*
-         * global!
-         */
-        sendto_serv_butone(cptr,
-                     active ? "%s " TOK_GLINE " %s +%s %s :%s" : "%s " TOK_GLINE " %s -%s",
-                     NumServ(sptr), parv[1], parv[2], parv[3], parv[4]);
-      }
-      else if ((
-#if 1
-          /*
-           * REMOVE THIS after all servers upgraded to 2.10.01 and
-           * Uworld uses a numeric too
-           */
-          (strlen(parv[1]) != 1 && !(acptr = FindClient(parv[1])))) ||
-          (strlen(parv[1]) == 1 &&
-#endif
-          !(acptr = FindNServer(parv[1]))))
-        return 0;               /* no such server/user exists; forget it */
-      else
-#if 1
-/*
- * REMOVE THIS after all servers upgraded to 2.10.01 and
- * Uworld uses a numeric too
- */
-      if (IsServer(acptr) || !MyConnect(acptr))
-#endif
-      {
-        /* single destination */
-        sendto_one(acptr,
-               active ? "%s " TOK_GLINE " %s +%s %s :%s" : "%s " TOK_GLINE " %s -%s",
-               NumServ(sptr), parv[1], parv[2], parv[3], parv[4]);
-        return 0;               /* only the intended  destination
-                                   should add this gline */
-      }
+      expire = atoi(parv[3]); /* get expiration... */
+      expire = abs_expire(expire); /* convert to absolute... */
+      reason = parv[parc - 1]; /* and reason */
 
-      if (!(host = strchr(parv[2], '@'))) {
-        /*
-         * convert user@host no @'s; assume username is '*'
-         */
-        user = "*";     
-        host = parv[2];
-      }
-      else {
-        user = parv[2];
-        *(host++) = '\0';       /* break up string at the '@' */
-      }
-      ip_mask = check_if_ipmask(host);  /* Store this boolean */
-#ifdef BADCHAN
-      if ('#' == *host || '&' == *host || '+' == *host)
-         gtype = 1;                /* BAD CHANNEL GLINE */
-#endif
-      for (gline = (gtype) ? BadChanGlineList : GlobalGlineList, prev = 0; gline;
-           gline = gline->next)
-      {
-        if (0 == ircd_strcmp(gline->name, user) &&
-            0 == ircd_strcmp(gline->host, host))
-          break;
-        prev = gline;
-      }
+      if (IsMe(acptr)) {
+       if (agline) /* G-line already exists, so let's ignore it... */
+         return 0;
 
-      if (!active && gline)
-      {
-        /*
-         * removing the gline, notify opers
-         */
-        sendto_op_mask(SNO_GLINE, "%s removing %s for %s@%s", parv[0],
-            gtype ? "BADCHAN" : "GLINE", gline->name, gline->host);
-
-#ifdef GPATH
-       write_log(GPATH, "# " TIME_T_FMT " %s removing %s for %s@%s\n",
-           TStime(), parv[0], gtype ? "BADCHAN" : "GLINE", gline->name, 
-            gline->host);
-#endif /* GPATH */
-
-        free_gline(gline, prev);        /* remove the gline */
-      }
-      else if (active)
-      {                         /* must be adding a gline */
-        expire = atoi(parv[3]) + TStime();      /* expire time? */
-        if (gline && gline->expire < expire)
-        {                       /* new expire time? */
-          /* yes, notify the opers */
-          sendto_op_mask(SNO_GLINE,
-             "%s resetting expiration time on %s for %s@%s to " TIME_T_FMT,
-             parv[0], gtype ? "BADCHAN" : "GLINE", gline->name, gline->host, 
-             expire);
-#ifdef GPATH
-          write_log(GPATH, "# " TIME_T_FMT " %s resetting expiration time "
-              "on %s for %s@%s to " TIME_T_FMT "\n",
-              TStime(), parv[0], gtype ? "BADCHAN" : "GLINE",
-              gline->name, gline->host, expire);
-#endif /* GPATH */
-
-          gline->expire = expire;       /* reset the expire time */
-        }
-        else if (!gline)
-        {                       /* create gline */
-          for (gline = (gtype) ? BadChanGlineList : GlobalGlineList; gline; gline = gline->next)
-            if (!mmatch(gline->name, user) &&
-                (ip_mask ? GlineIsIpMask(gline) : !GlineIsIpMask(gline)) &&
-                !mmatch(gline->host, host))
-              return 0;         /* found an existing G-line that matches */
-
-          /* add the line: */
-          add_gline(sptr, ip_mask, host, parv[4], user, expire, 0);
-        }
-      }
-    }
-  }
-  else if (parc < 2 || *parv[1] == '\0')
-  {
-    /* Not enough args and a user; list glines */
-    for (gline = GlobalGlineList; gline; gline = gline->next)
-      sendto_one(cptr, rpl_str(RPL_GLIST), me.name, parv[0],
-          gline->name, gline->host, gline->expire, gline->reason,
-          GlineIsActive(gline) ? (GlineIsLocal(gline) ? " (local)" : "") :
-          " (Inactive)");
-    sendto_one(cptr, rpl_str(RPL_ENDOFGLIST), me.name, parv[0]);
+       /* OK, create the local G-line */
+       Debug((DEBUG_DEBUG, "I am creating a local G-line here; target %s, "
+              "mask %s, operforce %s, action %s, expire %Tu, reason: %s",
+              target, mask, flags & GLINE_OPERFORCE ? "YES" : "NO",
+              action == GLINE_ACTIVATE ? "+" : "-", expire, reason));
+
+       return gline_add(cptr, sptr, mask, reason, expire, lastmod,
+                        lifetime, flags | GLINE_ACTIVE);
+      }
+    } else if (IsMe(acptr)) { /* destroying a local G-line */
+      if (!agline) /* G-line doesn't exist, so let's complain... */
+       return send_reply(sptr, ERR_NOSUCHGLINE, mask);
+
+      /* Let's now destroy the G-line */;
+      Debug((DEBUG_DEBUG, "I am destroying a local G-line here; target %s, "
+            "mask %s, operforce %s, action %s", target, mask,
+            flags & GLINE_OPERFORCE ? "YES" : "NO",
+            action == GLINE_ACTIVATE ? "+" : "-"));
+
+      return gline_destroy(cptr, sptr, agline);
+    }
+
+    /* OK, we've converted arguments; if it's not for us, forward */
+    /* UPDATE NOTE: Once all servers are updated to u2.10.12.11, the
+     * format string in this sendcmdto_one() may be updated to omit
+     * <lastmod> for GLINE_ACTIVATE and to omit <expire>, <lastmod>,
+     * and <reason> for GLINE_DEACTIVATE.
+     */
+    assert(!IsMe(acptr));
+
+    Debug((DEBUG_DEBUG, "I am forwarding a local G-line to a remote server; "
+          "target %s, mask %s, operforce %s, action %c, expire %Tu, "
+          "lastmod %Tu, reason: %s", target, mask,
+          flags & GLINE_OPERFORCE ? "YES" : "NO",
+          action == GLINE_ACTIVATE ? '+' :  '-', expire, CurrentTime,
+          reason));
+
+    sendcmdto_one(sptr, CMD_GLINE, acptr, "%C %s%c%s %Tu %Tu :%s",
+                 acptr, flags & GLINE_OPERFORCE ? "!" : "",
+                 action == GLINE_ACTIVATE ? '+' : '-', mask,
+                 expire - CurrentTime, CurrentTime, reason);
+
+    return 0; /* all done */
   }
-  else
-  {
-    int priv;
-
-#ifdef LOCOP_LGLINE
-    priv = IsAnOper(cptr);
-#else
-    priv = IsOper(cptr);
-#endif
-
-    if (priv)
-    {                           /* non-oper not permitted to change things */
-      if (*parv[1] == '-')
-      {                         /* oper wants to deactivate the gline */
-        active = 0;
-        parv[1]++;
-      }
-      else if (*parv[1] == '+')
-      {                         /* oper wants to activate inactive gline */
-        active = 1;
-        parv[1]++;
-      }
-      else
-        active = -1;
-
-      if (parc > 2)
-        expire = atoi(parv[2]) + TStime();      /* oper wants to reset
-                                                   expire TS */
-    }
-    else
-      active = -1;
 
-    if (!(host = strchr(parv[1], '@')))
-    {
-      user = "*";               /* no @'s; assume username is '*' */
-      host = parv[1];
-    }
-    else
-    {
-      user = parv[1];
-      *(host++) = '\0';         /* break up string at the '@' */
-    }
-    ip_mask = check_if_ipmask(host);    /* Store this boolean */
-#ifdef BADCHAN
-    if ('#' == *host || '&' == *host || '+' == *host)
-#ifndef LOCAL_BADCHAN
-     return 0;
-#else
-     gtype = 1;  /* BAD CHANNEL */
-#endif
-#endif
-
-    for (gline = (gtype) ? BadChanGlineList : GlobalGlineList, prev = 0; gline;
-         gline = gline->next)
-    {
-      if (!mmatch(gline->name, user) &&
-          (ip_mask ? GlineIsIpMask(gline) : !GlineIsIpMask(gline)) &&
-          !mmatch(gline->host, host))
-        break;
-      prev = gline;
+  /* can't modify a G-line that doesn't exist, so remap to activate */
+  if (!agline && action == GLINE_MODIFY)
+    action = GLINE_ACTIVATE;
+
+  /* OK, let's figure out what other parameters we may have... */
+  switch (action) {
+  case GLINE_LOCAL_ACTIVATE: /* locally activating a G-line */
+  case GLINE_LOCAL_DEACTIVATE: /* locally deactivating a G-line */
+    if (!agline) /* no G-line to locally activate or deactivate? */
+      return send_reply(sptr, ERR_NOSUCHGLINE, mask);
+    lastmod = agline->gl_lastmod;
+    break; /* no additional parameters to manipulate */
+
+  case GLINE_ACTIVATE: /* activating a G-line */
+  case GLINE_DEACTIVATE: /* deactivating a G-line */
+    /* in either of these cases, we have at least a lastmod parameter */
+    if (parc < 4)
+      return need_more_params(sptr, "GLINE");
+    else if (parc == 4) /* lastmod only form... */
+      lastmod = atoi(parv[3]);
+    /*FALLTHROUGH*/
+  case GLINE_MODIFY: /* modifying a G-line */
+    /* convert expire and lastmod, look for lifetime and reason */
+    if (parc > 4) { /* protect against fall-through from 4-param form */
+      expire = atoi(parv[3]); /* convert expiration and lastmod */
+      expire = abs_expire(expire);
+      lastmod = atoi(parv[4]);
+
+      flags |= GLINE_EXPIRE; /* we have an expiration time update */
+
+      if (parc > 6) { /* no question, have a lifetime and reason */
+       lifetime = atoi(parv[5]);
+       reason = parv[parc - 1];
+
+       flags |= GLINE_LIFETIME | GLINE_REASON;
+      } else if (parc == 6) { /* either a lifetime or a reason */
+       if (!agline || /* gline creation, has to be the reason */
+           /* trial-convert as lifetime, and if it doesn't fully convert,
+            * it must be the reason */
+           (!(lifetime = strtoul(parv[5], &tmp, 10)) && !*tmp)) {
+         lifetime = 0;
+         reason = parv[5];
+
+         flags |= GLINE_REASON; /* have a reason update */
+       } else if (lifetime)
+         flags |= GLINE_LIFETIME; /* have a lifetime update */
+      }
     }
+  }
 
-    if (!gline)
-    {
-#ifdef OPER_LGLINE
-      if (priv && active && expire > CurrentTime)
-      {
-        /* Add local G-line */
-        if (parc < 4 || !strchr(parv[3], ' '))
-          return need_more_params(sptr, "GLINE");
+  if (!lastmod) /* must have a lastmod parameter by now */
+    return need_more_params(sptr, "GLINE");
 
-        add_gline(sptr, ip_mask, host, parv[3], user, expire, 1);
-      }
-      else
-#endif
-        sendto_one(cptr, err_str(ERR_NOSUCHGLINE), me.name, parv[0], user,
-            host);
+  Debug((DEBUG_DEBUG, "I have a global G-line I am acting upon now; "
+        "target %s, mask %s, operforce %s, action %s, expire %Tu, "
+        "lastmod %Tu, lifetime %Tu, reason: %s; gline %s!  (fields "
+        "present: %s %s %s)", target, mask,
+        flags & GLINE_OPERFORCE ? "YES" : "NO",
+        action == GLINE_ACTIVATE ? "+" :
+        (action == GLINE_DEACTIVATE ? "-" :
+         (action == GLINE_LOCAL_ACTIVATE ? ">" :
+          (action == GLINE_LOCAL_DEACTIVATE ? "<" : "(MODIFY)"))),
+        expire, lastmod, lifetime, reason,
+        agline ? "EXISTS" : "does not exist",
+        flags & GLINE_EXPIRE ? "expire" : "",
+        flags & GLINE_LIFETIME ? "lifetime" : "",
+        flags & GLINE_REASON ? "reason" : ""));
+
+  /* OK, at this point, we have converted all available parameters.
+   * Let's actually do the action!
+   */
+  if (agline)
+    return gline_modify(cptr, sptr, agline, action, reason, expire,
+                       lastmod, lifetime, flags);
 
-      return 0;
-    }
+  assert(action != GLINE_LOCAL_ACTIVATE);
+  assert(action != GLINE_LOCAL_DEACTIVATE);
+  assert(action != GLINE_MODIFY);
 
-    if (expire <= gline->expire)
-      expire = 0;
-
-    if ((active == -1 ||
-        (active ? GlineIsActive(gline) : !GlineIsActive(gline))) &&
-        expire == 0)
-    {
-      /* oper wants a list of one gline only */
-      sendto_one(cptr, rpl_str(RPL_GLIST), me.name, parv[0], gline->name,
-          gline->host, gline->expire, gline->reason,
-          GlineIsActive(gline) ? "" : " (Inactive)");
-      sendto_one(cptr, rpl_str(RPL_ENDOFGLIST), me.name, parv[0]);
-      return 0;
-    }
+  if (!expire) { /* Cannot *add* a G-line we don't have, but try hard */
+    Debug((DEBUG_DEBUG, "Propagating G-line %s for G-line we don't have",
+          action == GLINE_ACTIVATE ? "activation" : "deactivation"));
 
-    if (active != -1 &&
-        (active ? !GlineIsActive(gline) : GlineIsActive(gline)))
-    {
-      if (active)               /* reset activation on gline */
-        SetActive(gline);
-#ifdef OPER_LGLINE
-      else if (GlineIsLocal(gline))
-      {
-        /* Remove local G-line */
-        sendto_op_mask(SNO_GLINE, "%s removed local %s for %s@%s",
-            parv[0], gtype ? "BADCHAN" : "GLINE", gline->name, gline->host);
-#ifdef GPATH
-        write_log(GPATH, "# " TIME_T_FMT
-            " %s!%s@%s removed local %s for %s@%s\n",
-            TStime(), parv[0], cptr->user->username, cptr->user->host,
-            gtype ? "BADCHAN" : "GLINE",
-            gline->name, gline->host);
-#endif /* GPATH */
-        free_gline(gline, prev);        /* remove the gline */
-        return 0;
-      }
-#endif
-      else
-        ClearActive(gline);
-    }
-    else
-      active = -1;              /* for later sendto_ops and logging functions */
-
-    if (expire)
-      gline->expire = expire;   /* reset expiration time */
-
-    /* inform the operators what's up */
-    if (active != -1)
-    {                           /* changing the activation */
-       sendto_op_mask(SNO_GLINE, !expire ? "%s %sactivating %s for %s@%s" :
-          "%s %sactivating %s for %s@%s and "
-          "resetting expiration time to " TIME_T_FMT,
-          parv[0], active ? "re" : "de", gtype ? "BADCHAN" : "GLINE",
-          gline->name, gline->host, gline->expire);
-#ifdef GPATH
-      write_log(GPATH, !expire ? "# " TIME_T_FMT " %s!%s@%s %sactivating "
-          "%s for %s@%s\n" : "# " TIME_T_FMT " %s!%s@%s %sactivating %s "
-          "for %s@%s and resetting expiration time to " TIME_T_FMT "\n",
-          TStime(), parv[0], cptr->user->username, cptr->user->host,
-          active ? "re" : "de", gtype ? "BADCHAN" : "GLINE", gline->name, 
-          gline->host, gline->expire);
-#endif /* GPATH */
+    /* propagate the G-line, even though we don't have it */
+    sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s %Tu",
+                         action == GLINE_ACTIVATE ? '+' : '-',
+                         mask, lastmod);
 
-    }
-    else if (expire)
-    {                           /* changing only the expiration */
-      sendto_op_mask(SNO_GLINE,
-          "%s resetting expiration time on %s for %s@%s to " TIME_T_FMT,
-          parv[0], gtype ? "BADCHAN" : "GLINE", gline->name, gline->host, 
-          gline->expire);
-#ifdef GPATH
-      write_log(GPATH, "# " TIME_T_FMT " %s!%s@%s resetting expiration "
-          "time on %s for %s@%s to " TIME_T_FMT "\n", TStime(), parv[0],
-          cptr->user->username, cptr->user->host, gtype ? "BADCHAN" : "GLINE",
-          gline->name, gline->host, gline->expire);
-#endif /* GPATH */
-    }
+    return 0;
   }
-  return 0;
+
+  return gline_add(cptr, sptr, mask, reason, expire, lastmod, lifetime,
+                  flags | ((action == GLINE_ACTIVATE) ? GLINE_ACTIVE : 0));
 }
 
 /*
  * mo_gline - oper message handler
  *
- * parv[0] = Send prefix
- *
- * From server:
- *
- * parv[1] = Target: server numeric
- * parv[2] = [+|-]<G-line mask>
- * parv[3] = Expiration offset
- * parv[4] = Comment
- *
- * From client:
- *
- * parv[1] = [+|-]<G-line mask>
- * parv[2] = Expiration offset
- * parv[3] = Comment
+ * parv[0] = Sender prefix
+ * parv[1] = [[+|-]<G-line mask>]
  *
+ * For other parameters, see doc/readme.gline.
  */
-int mo_gline(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
+int
+mo_gline(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
 {
-  struct Client* acptr = 0;  /* Init. to avoid compiler warning. */
-  struct Gline*  gline;
-  struct Gline*  prev;
-  char*          user;
-  char*          host;
-  int            active;
-  int            ip_mask;
-  int            gtype = 0;
-  time_t         expire = 0;
-
-  /*
-   * Remove expired G-lines
-   */
-  gline_remove_expired(TStime());
-#ifdef BADCHAN
-  /*
-   * Remove expired bad channels
-   */
-  bad_channel_remove_expired(TStime());
-#endif
-
-  if (IsServer(cptr)) {
-    if (find_conf_byhost(cptr->confs, sptr->name, CONF_UWORLD)) {
-      if (parc < 3 || (*parv[2] != '-' && (parc < 5 || *parv[4] == '\0')))
-        return need_more_params(sptr, "GLINE");
-
-      if (*parv[2] == '-') /* add mode or delete mode? */
-        active = 0;
-      else
-        active = 1;
-
-      if (*parv[2] == '+' || *parv[2] == '-')
-        parv[2]++; /* step past mode indicator */
-
-      /*
-       * forward the message appropriately
-       */
-      if (0 == ircd_strcmp(parv[1], "*")) {
-        /*
-         * global!
-         */
-        sendto_serv_butone(cptr,
-                     active ? "%s " TOK_GLINE " %s +%s %s :%s" : "%s " TOK_GLINE " %s -%s",
-                     NumServ(sptr), parv[1], parv[2], parv[3], parv[4]);
-      }
-      else if ((
-#if 1
-          /*
-           * REMOVE THIS after all servers upgraded to 2.10.01 and
-           * Uworld uses a numeric too
-           */
-          (strlen(parv[1]) != 1 && !(acptr = FindClient(parv[1])))) ||
-          (strlen(parv[1]) == 1 &&
-#endif
-          !(acptr = FindNServer(parv[1]))))
-        return 0;               /* no such server/user exists; forget it */
-      else
-#if 1
-/*
- * REMOVE THIS after all servers upgraded to 2.10.01 and
- * Uworld uses a numeric too
- */
-      if (IsServer(acptr) || !MyConnect(acptr))
-#endif
-      {
-        /* single destination */
-        sendto_one(acptr,
-               active ? "%s " TOK_GLINE " %s +%s %s :%s" : "%s " TOK_GLINE " %s -%s",
-               NumServ(sptr), parv[1], parv[2], parv[3], parv[4]);
-        return 0;               /* only the intended  destination
-                                   should add this gline */
-      }
+  struct Client *acptr = 0;
+  struct Gline *agline = 0;
+  unsigned int flags = 0;
+  enum GlineAction action = GLINE_MODIFY;
+  time_t expire = 0;
+  char *mask = parv[1], *target = 0, *reason = 0, *end;
 
-      if (!(host = strchr(parv[2], '@'))) {
-        /*
-         * convert user@host no @'s; assume username is '*'
-         */
-        user = "*";     
-        host = parv[2];
-      }
-      else {
-        user = parv[2];
-        *(host++) = '\0';       /* break up string at the '@' */
-      }
-      ip_mask = check_if_ipmask(host);  /* Store this boolean */
-#ifdef BADCHAN
-      if ('#' == *host || '&' == *host || '+' == *host)
-         gtype = 1;                /* BAD CHANNEL GLINE */
-#endif
-      for (gline = (gtype) ? BadChanGlineList : GlobalGlineList, prev = 0; gline;
-           gline = gline->next)
-      {
-        if (0 == ircd_strcmp(gline->name, user) &&
-            0 == ircd_strcmp(gline->host, host))
-          break;
-        prev = gline;
-      }
+  if (parc < 2)
+    return gline_list(sptr, 0);
 
-      if (!active && gline)
-      {
-        /*
-         * removing the gline, notify opers
-         */
-        sendto_op_mask(SNO_GLINE, "%s removing %s for %s@%s", parv[0],
-            gtype ? "BADCHAN" : "GLINE", gline->name, gline->host);
-
-#ifdef GPATH
-       write_log(GPATH, "# " TIME_T_FMT " %s removing %s for %s@%s\n",
-           TStime(), parv[0], gtype ? "BADCHAN" : "GLINE", gline->name, 
-            gline->host);
-#endif /* GPATH */
-
-        free_gline(gline, prev);        /* remove the gline */
-      }
-      else if (active)
-      {                         /* must be adding a gline */
-        expire = atoi(parv[3]) + TStime();      /* expire time? */
-        if (gline && gline->expire < expire)
-        {                       /* new expire time? */
-          /* yes, notify the opers */
-          sendto_op_mask(SNO_GLINE,
-             "%s resetting expiration time on %s for %s@%s to " TIME_T_FMT,
-             parv[0], gtype ? "BADCHAN" : "GLINE", gline->name, gline->host, 
-             expire);
-#ifdef GPATH
-          write_log(GPATH, "# " TIME_T_FMT " %s resetting expiration time "
-              "on %s for %s@%s to " TIME_T_FMT "\n",
-              TStime(), parv[0], gtype ? "BADCHAN" : "GLINE",
-              gline->name, gline->host, expire);
-#endif /* GPATH */
-
-          gline->expire = expire;       /* reset the expire time */
-        }
-        else if (!gline)
-        {                       /* create gline */
-          for (gline = (gtype) ? BadChanGlineList : GlobalGlineList; gline; gline = gline->next)
-            if (!mmatch(gline->name, user) &&
-                (ip_mask ? GlineIsIpMask(gline) : !GlineIsIpMask(gline)) &&
-                !mmatch(gline->host, host))
-              return 0;         /* found an existing G-line that matches */
-
-          /* add the line: */
-          add_gline(sptr, ip_mask, host, parv[4], user, expire, 0);
-        }
-      }
-    }
-  }
-  else if (parc < 2 || *parv[1] == '\0')
-  {
-    /* Not enough args and a user; list glines */
-    for (gline = GlobalGlineList; gline; gline = gline->next)
-      sendto_one(cptr, rpl_str(RPL_GLIST), me.name, parv[0],
-          gline->name, gline->host, gline->expire, gline->reason,
-          GlineIsActive(gline) ? (GlineIsLocal(gline) ? " (local)" : "") :
-          " (Inactive)");
-    sendto_one(cptr, rpl_str(RPL_ENDOFGLIST), me.name, parv[0]);
+  if (*mask == '!') {
+    mask++;
+
+    if (HasPriv(sptr, PRIV_WIDE_GLINE))
+      flags |= GLINE_OPERFORCE;
   }
-  else
-  {
-    int priv;
-
-#ifdef LOCOP_LGLINE
-    priv = IsAnOper(cptr);
-#else
-    priv = IsOper(cptr);
-#endif
-
-    if (priv)
-    {                           /* non-oper not permitted to change things */
-      if (*parv[1] == '-')
-      {                         /* oper wants to deactivate the gline */
-        active = 0;
-        parv[1]++;
-      }
-      else if (*parv[1] == '+')
-      {                         /* oper wants to activate inactive gline */
-        active = 1;
-        parv[1]++;
-      }
-      else
-        active = -1;
 
-      if (parc > 2)
-        expire = atoi(parv[2]) + TStime();      /* oper wants to reset
-                                                   expire TS */
-    }
-    else
-      active = -1;
+  switch (*mask) { /* handle +, -, <, and > */
+  case '+': /* activate the G-line */
+    action = GLINE_ACTIVATE;
+    mask++;
+    break;
 
-    if (!(host = strchr(parv[1], '@')))
-    {
-      user = "*";               /* no @'s; assume username is '*' */
-      host = parv[1];
-    }
-    else
-    {
-      user = parv[1];
-      *(host++) = '\0';         /* break up string at the '@' */
-    }
-    ip_mask = check_if_ipmask(host);    /* Store this boolean */
-#ifdef BADCHAN
-    if ('#' == *host || '&' == *host || '+' == *host)
-#ifndef LOCAL_BADCHAN
-     return 0;
-#else
-     gtype = 1;  /* BAD CHANNEL */
-#endif
-#endif
-
-    for (gline = (gtype) ? BadChanGlineList : GlobalGlineList, prev = 0; gline;
-         gline = gline->next)
-    {
-      if (!mmatch(gline->name, user) &&
-          (ip_mask ? GlineIsIpMask(gline) : !GlineIsIpMask(gline)) &&
-          !mmatch(gline->host, host))
-        break;
-      prev = gline;
-    }
+  case '-': /* deactivate the G-line */
+    action = GLINE_DEACTIVATE;
+    mask++;
+    break;
 
-    if (!gline)
-    {
-#ifdef OPER_LGLINE
-      if (priv && active && expire > CurrentTime)
-      {
-        /* Add local G-line */
-        if (parc < 4 || !strchr(parv[3], ' '))
-          return need_more_params(sptr, "GLINE");
+  case '>': /* locally activate the G-line */
+    action = GLINE_LOCAL_ACTIVATE;
+    mask++;
+    break;
 
-        add_gline(sptr, ip_mask, host, parv[3], user, expire, 1);
-      }
-      else
-#endif
-        sendto_one(cptr, err_str(ERR_NOSUCHGLINE), me.name, parv[0], user,
-            host);
+  case '<': /* locally deactivate the G-line */
+    action = GLINE_LOCAL_DEACTIVATE;
+    mask++;
+    break;
+  }
 
-      return 0;
-    }
+  /* OK, let's figure out the parameters... */
+  switch (action) {
+  case GLINE_MODIFY: /* no specific action on the G-line... */
+    if (parc == 2) /* user wants a listing of a specific G-line */
+      return gline_list(sptr, mask);
+    else if (parc < 4) /* must have target and expire, minimum */
+      return need_more_params(sptr, "GLINE");
 
-    if (expire <= gline->expire)
-      expire = 0;
-
-    if ((active == -1 ||
-        (active ? GlineIsActive(gline) : !GlineIsActive(gline))) &&
-        expire == 0)
-    {
-      /* oper wants a list of one gline only */
-      sendto_one(cptr, rpl_str(RPL_GLIST), me.name, parv[0], gline->name,
-          gline->host, gline->expire, gline->reason,
-          GlineIsActive(gline) ? "" : " (Inactive)");
-      sendto_one(cptr, rpl_str(RPL_ENDOFGLIST), me.name, parv[0]);
-      return 0;
-    }
+    target = parv[2]; /* get the target... */
+    expire = strtol(parv[3], &end, 10) + CurrentTime; /* and the expiration */
+    if (*end != '\0')
+      return send_reply(sptr, SND_EXPLICIT | ERR_BADEXPIRE, "%s :Bad expire time", parv[3]);
 
-    if (active != -1 &&
-        (active ? !GlineIsActive(gline) : GlineIsActive(gline)))
-    {
-      if (active)               /* reset activation on gline */
-        SetActive(gline);
-#ifdef OPER_LGLINE
-      else if (GlineIsLocal(gline))
-      {
-        /* Remove local G-line */
-        sendto_op_mask(SNO_GLINE, "%s removed local %s for %s@%s",
-            parv[0], gtype ? "BADCHAN" : "GLINE", gline->name, gline->host);
-#ifdef GPATH
-        write_log(GPATH, "# " TIME_T_FMT
-            " %s!%s@%s removed local %s for %s@%s\n",
-            TStime(), parv[0], cptr->user->username, cptr->user->host,
-            gtype ? "BADCHAN" : "GLINE",
-            gline->name, gline->host);
-#endif /* GPATH */
-        free_gline(gline, prev);        /* remove the gline */
-        return 0;
-      }
-#endif
-      else
-        ClearActive(gline);
+    flags |= GLINE_EXPIRE; /* remember that we got an expire time */
+
+    if (parc > 4) { /* also got a reason... */
+      reason = parv[parc - 1];
+      flags |= GLINE_REASON;
     }
-    else
-      active = -1;              /* for later sendto_ops and logging functions */
-
-    if (expire)
-      gline->expire = expire;   /* reset expiration time */
-
-    /* inform the operators what's up */
-    if (active != -1)
-    {                           /* changing the activation */
-       sendto_op_mask(SNO_GLINE, !expire ? "%s %sactivating %s for %s@%s" :
-          "%s %sactivating %s for %s@%s and "
-          "resetting expiration time to " TIME_T_FMT,
-          parv[0], active ? "re" : "de", gtype ? "BADCHAN" : "GLINE",
-          gline->name, gline->host, gline->expire);
-#ifdef GPATH
-      write_log(GPATH, !expire ? "# " TIME_T_FMT " %s!%s@%s %sactivating "
-          "%s for %s@%s\n" : "# " TIME_T_FMT " %s!%s@%s %sactivating %s "
-          "for %s@%s and resetting expiration time to " TIME_T_FMT "\n",
-          TStime(), parv[0], cptr->user->username, cptr->user->host,
-          active ? "re" : "de", gtype ? "BADCHAN" : "GLINE", gline->name, 
-          gline->host, gline->expire);
-#endif /* GPATH */
 
+    /* target is not global, interpolate action and require reason */
+    if (target[0] != '*' || target[1] != '\0') {
+      if (!reason) /* have to have a reason for this */
+       return need_more_params(sptr, "GLINE");
+
+      action = GLINE_ACTIVATE;
     }
-    else if (expire)
-    {                           /* changing only the expiration */
-      sendto_op_mask(SNO_GLINE,
-          "%s resetting expiration time on %s for %s@%s to " TIME_T_FMT,
-          parv[0], gtype ? "BADCHAN" : "GLINE", gline->name, gline->host, 
-          gline->expire);
-#ifdef GPATH
-      write_log(GPATH, "# " TIME_T_FMT " %s!%s@%s resetting expiration "
-          "time on %s for %s@%s to " TIME_T_FMT "\n", TStime(), parv[0],
-          cptr->user->username, cptr->user->host, gtype ? "BADCHAN" : "GLINE",
-          gline->name, gline->host, gline->expire);
-#endif /* GPATH */
+    break;
+
+  case GLINE_LOCAL_ACTIVATE: /* locally activate a G-line */
+  case GLINE_LOCAL_DEACTIVATE: /* locally deactivate a G-line */
+    if (parc > 2) { /* if target is available, pick it */
+      target = parv[2];
+      if (target[0] == '*' && target[1] == '\0')
+        return send_reply(sptr, ERR_NOSUCHSERVER, target);
     }
-  }
-  return 0;
-}
+    break;
 
+  case GLINE_ACTIVATE: /* activating/adding a G-line */
+  case GLINE_DEACTIVATE: /* deactivating/removing a G-line */
+    if (parc < 3)
+      return need_more_params(sptr, "GLINE");
 
-#if 0
-/*
- * m_gline
- *
- * parv[0] = Send prefix
- *
- * From server:
- *
- * parv[1] = Target: server numeric
- * parv[2] = [+|-]<G-line mask>
- * parv[3] = Expiration offset
- * parv[4] = Comment
- *
- * From client:
- *
- * parv[1] = [+|-]<G-line mask>
- * parv[2] = Expiration offset
- * parv[3] = Comment
- *
- */
-int m_gline(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
-{
-  struct Client* acptr = 0;  /* Init. to avoid compiler warning. */
-  struct Gline*  gline;
-  struct Gline*  prev;
-  char*          user;
-  char*          host;
-  int            active;
-  int            ip_mask;
-  int            gtype = 0;
-  time_t         expire = 0;
-
-  /*
-   * Remove expired G-lines
-   */
-  gline_remove_expired(TStime());
-#ifdef BADCHAN
-  /*
-   * Remove expired bad channels
-   */
-  bad_channel_remove_expired(TStime());
-#endif
-
-  if (IsServer(cptr)) {
-    if (find_conf_byhost(cptr->confs, sptr->name, CONF_UWORLD)) {
-      if (parc < 3 || (*parv[2] != '-' && (parc < 5 || *parv[4] == '\0')))
-        return need_more_params(sptr, "GLINE");
-
-      if (*parv[2] == '-') /* add mode or delete mode? */
-        active = 0;
-      else
-        active = 1;
-
-      if (*parv[2] == '+' || *parv[2] == '-')
-        parv[2]++; /* step past mode indicator */
-
-      /*
-       * forward the message appropriately
-       */
-      if (0 == ircd_strcmp(parv[1], "*")) {
-        /*
-         * global!
-         */
-        sendto_serv_butone(cptr,
-                     active ? "%s " TOK_GLINE " %s +%s %s :%s" : "%s " TOK_GLINE " %s -%s",
-                     NumServ(cptr), parv[1], parv[2], parv[3], parv[4]);
-      }
-      else if ((
-#if 1
-          /*
-           * REMOVE THIS after all servers upgraded to 2.10.01 and
-           * Uworld uses a numeric too
-           */
-          (strlen(parv[1]) != 1 && !(acptr = FindClient(parv[1])))) ||
-          (strlen(parv[1]) == 1 &&
-#endif
-          !(acptr = FindNServer(parv[1]))))
-        return 0;               /* no such server/user exists; forget it */
-      else
-#if 1
-/*
- * REMOVE THIS after all servers upgraded to 2.10.01 and
- * Uworld uses a numeric too
- */
-      if (IsServer(acptr) || !MyConnect(acptr))
-#endif
-      {
-        /* single destination */
-        sendto_one(acptr,
-               active ? "%s " TOK_GLINE " %s +%s %s :%s" : "%s " TOK_GLINE " %s -%s",
-               NumServ(sptr), parv[1], parv[2], parv[3], parv[4]);
-        return 0;               /* only the intended  destination
-                                   should add this gline */
-      }
+    if (parc > 3) {
+      /* get expiration and target */
+      reason = parv[parc - 1];
+      expire = strtol(parv[parc - 2], &end, 10) + CurrentTime;
+      if (*end != '\0')
+        return send_reply(sptr, SND_EXPLICIT | ERR_BADEXPIRE, "%s :Bad expire time", parv[parc - 2]);
 
-      if (!(host = strchr(parv[2], '@'))) {
-        /*
-         * convert user@host no @'s; assume username is '*'
-         */
-        user = "*";     
-        host = parv[2];
-      }
-      else {
-        user = parv[2];
-        *(host++) = '\0';       /* break up string at the '@' */
-      }
-      ip_mask = check_if_ipmask(host);  /* Store this boolean */
-#ifdef BADCHAN
-      if ('#' == *host || '&' == *host || '+' == *host)
-         gtype = 1;                /* BAD CHANNEL GLINE */
-#endif
-      for (gline = (gtype) ? BadChanGlineList : GlobalGlineList, prev = 0; gline;
-           gline = gline->next)
-      {
-        if (0 == ircd_strcmp(gline->name, user) &&
-            0 == ircd_strcmp(gline->host, host))
-          break;
-        prev = gline;
-      }
+      flags |= GLINE_EXPIRE | GLINE_REASON; /* remember that we got 'em */
 
-      if (!active && gline)
-      {
-        /*
-         * removing the gline, notify opers
-         */
-        sendto_op_mask(SNO_GLINE, "%s removing %s for %s@%s", parv[0],
-            gtype ? "BADCHAN" : "GLINE", gline->name, gline->host);
-
-#ifdef GPATH
-       write_log(GPATH, "# " TIME_T_FMT " %s removing %s for %s@%s\n",
-           TStime(), parv[0], gtype ? "BADCHAN" : "GLINE", gline->name, 
-            gline->host);
-#endif /* GPATH */
-
-        free_gline(gline, prev);        /* remove the gline */
-      }
-      else if (active)
-      {                         /* must be adding a gline */
-        expire = atoi(parv[3]) + TStime();      /* expire time? */
-        if (gline && gline->expire < expire)
-        {                       /* new expire time? */
-          /* yes, notify the opers */
-          sendto_op_mask(SNO_GLINE,
-             "%s resetting expiration time on %s for %s@%s to " TIME_T_FMT,
-             parv[0], gtype ? "BADCHAN" : "GLINE", gline->name, gline->host, 
-             expire);
-#ifdef GPATH
-          write_log(GPATH, "# " TIME_T_FMT " %s resetting expiration time "
-              "on %s for %s@%s to " TIME_T_FMT "\n",
-              TStime(), parv[0], gtype ? "BADCHAN" : "GLINE",
-              gline->name, gline->host, expire);
-#endif /* GPATH */
-
-          gline->expire = expire;       /* reset the expire time */
-        }
-        else if (!gline)
-        {                       /* create gline */
-          for (gline = (gtype) ? BadChanGlineList : GlobalGlineList; gline; gline = gline->next)
-            if (!mmatch(gline->name, user) &&
-                (ip_mask ? GlineIsIpMask(gline) : !GlineIsIpMask(gline)) &&
-                !mmatch(gline->host, host))
-              return 0;         /* found an existing G-line that matches */
-
-          /* add the line: */
-          add_gline(sptr, ip_mask, host, parv[4], user, expire, 0);
-        }
-      }
+      if (parc > 4) /* also have a target! */
+       target = parv[2];
+    } else {
+      target = parv[2]; /* target has to be present, and has to be '*' */
+
+      if (target[0] != '*' || target[1] != '\0')
+       return need_more_params(sptr, "GLINE");
     }
+    break;
   }
-  else if (parc < 2 || *parv[1] == '\0')
-  {
-    /* Not enough args and a user; list glines */
-    for (gline = GlobalGlineList; gline; gline = gline->next)
-      sendto_one(cptr, rpl_str(RPL_GLIST), me.name, parv[0],
-          gline->name, gline->host, gline->expire, gline->reason,
-          GlineIsActive(gline) ? (GlineIsLocal(gline) ? " (local)" : "") :
-          " (Inactive)");
-    sendto_one(cptr, rpl_str(RPL_ENDOFGLIST), me.name, parv[0]);
+
+  /* Now let's figure out which is the target server */
+  if (!target) /* no target, has to be me... */
+    acptr = &me;
+  /* if it's not '*', look up the server */
+  else if ((target[0] != '*' || target[1] != '\0') &&
+          !(acptr = find_match_server(target)))
+    return send_reply(sptr, ERR_NOSUCHSERVER, target);
+
+  /* Now, is the G-line local or global? */
+  if (action == GLINE_LOCAL_ACTIVATE || action == GLINE_LOCAL_DEACTIVATE ||
+      !acptr)
+    flags |= GLINE_GLOBAL;
+  else /* it's some form of local G-line */
+    flags |= GLINE_LOCAL;
+
+  /* If it's a local activate/deactivate and server isn't me, propagate it */
+  if ((action == GLINE_LOCAL_ACTIVATE || action == GLINE_LOCAL_DEACTIVATE) &&
+      !IsMe(acptr)) {
+    /* check for permissions... */
+    if (!feature_bool(FEAT_CONFIG_OPERCMDS))
+      return send_reply(sptr, ERR_DISABLED, "GLINE");
+    else if (!HasPriv(sptr, PRIV_GLINE))
+      return send_reply(sptr, ERR_NOPRIVILEGES);
+
+    Debug((DEBUG_DEBUG, "I am forwarding a local change to a global gline "
+          "to a remote server; target %s, mask %s, operforce %s, action %c",
+          cli_name(acptr), mask, flags & GLINE_OPERFORCE ? "YES" : "NO",
+          action == GLINE_LOCAL_ACTIVATE ? '>' : '<'));
+
+    sendcmdto_one(sptr, CMD_GLINE, acptr, "%C %s%c%s", acptr,
+                  flags & GLINE_OPERFORCE ? "!" : "",
+                  action == GLINE_LOCAL_ACTIVATE ? '>' : '<', mask);
+
+    return 0; /* all done */
   }
-  else
-  {
-    int priv;
-
-#ifdef LOCOP_LGLINE
-    priv = IsAnOper(cptr);
-#else
-    priv = IsOper(cptr);
-#endif
-
-    if (priv)
-    {                           /* non-oper not permitted to change things */
-      if (*parv[1] == '-')
-      {                         /* oper wants to deactivate the gline */
-        active = 0;
-        parv[1]++;
-      }
-      else if (*parv[1] == '+')
-      {                         /* oper wants to activate inactive gline */
-        active = 1;
-        parv[1]++;
-      }
-      else
-        active = -1;
 
-      if (parc > 2)
-        expire = atoi(parv[2]) + TStime();      /* oper wants to reset
-                                                   expire TS */
-    }
-    else
-      active = -1;
+  /* Next, try to find the G-line... */
+  if ((flags & GLINE_GLOBAL) || IsMe(acptr)) /* don't bother if it's not me! */
+    agline = gline_find(mask, flags | GLINE_ANY | GLINE_EXACT);
 
-    if (!(host = strchr(parv[1], '@')))
-    {
-      user = "*";               /* no @'s; assume username is '*' */
-      host = parv[1];
-    }
-    else
-    {
-      user = parv[1];
-      *(host++) = '\0';         /* break up string at the '@' */
-    }
-    ip_mask = check_if_ipmask(host);    /* Store this boolean */
-#ifdef BADCHAN
-    if ('#' == *host || '&' == *host || '+' == *host)
-#ifndef LOCAL_BADCHAN
-     return 0;
-#else
-     gtype = 1;  /* BAD CHANNEL */
-#endif
-#endif
-
-    for (gline = (gtype) ? BadChanGlineList : GlobalGlineList, prev = 0; gline;
-         gline = gline->next)
-    {
-      if (!mmatch(gline->name, user) &&
-          (ip_mask ? GlineIsIpMask(gline) : !GlineIsIpMask(gline)) &&
-          !mmatch(gline->host, host))
-        break;
-      prev = gline;
-    }
+  /* We now have all the pieces to tell us what we've got; let's put
+   * it all together and convert the rest of the arguments.
+   */
 
-    if (!gline)
-    {
-#ifdef OPER_LGLINE
-      if (priv && active && expire > CurrentTime)
-      {
-        /* Add local G-line */
-        if (parc < 4 || !strchr(parv[3], ' '))
-          return need_more_params(sptr, "GLINE");
+  /* Handle the local G-lines first... */
+  if (flags & GLINE_LOCAL) {
+    assert(acptr);
+
+    /* normalize the action, first */
+    if (action == GLINE_LOCAL_ACTIVATE || action == GLINE_MODIFY)
+      action = GLINE_ACTIVATE;
+    else if (action == GLINE_LOCAL_DEACTIVATE)
+      action = GLINE_DEACTIVATE;
+
+    /* If it's not for us, forward */
+    /* UPDATE NOTE: Once all servers are updated to u2.10.12.11, the
+     * format string in this sendcmdto_one() may be updated to omit
+     * <lastmod> for GLINE_ACTIVATE and to omit <expire>, <lastmod>,
+     * and <reason> for GLINE_DEACTIVATE.
+     */
+
+    if (!IsMe(acptr)) {
+      /* check for permissions... */
+      if (!feature_bool(FEAT_CONFIG_OPERCMDS))
+       return send_reply(sptr, ERR_DISABLED, "GLINE");
+      else if (!HasPriv(sptr, PRIV_GLINE))
+       return send_reply(sptr, ERR_NOPRIVILEGES);
+
+      Debug((DEBUG_DEBUG, "I am forwarding a local G-line to a remote "
+            "server; target %s, mask %s, operforce %s, action %c, "
+            "expire %Tu, reason %s", target, mask,
+            flags & GLINE_OPERFORCE ? "YES" : "NO",
+            action == GLINE_ACTIVATE ? '+' : '-', expire, reason));
+
+      sendcmdto_one(sptr, CMD_GLINE, acptr, "%C %s%c%s %Tu %Tu :%s",
+                   acptr, flags & GLINE_OPERFORCE ? "!" : "",
+                   action == GLINE_ACTIVATE ? '+' : '-', mask,
+                   expire - CurrentTime, CurrentTime, reason);
+
+      return 0; /* all done */
+    }
+
+    /* check local G-line permissions... */
+    if (!HasPriv(sptr, PRIV_LOCAL_GLINE))
+      return send_reply(sptr, ERR_NOPRIVILEGES);
+
+    /* let's handle activation... */
+    if (action == GLINE_ACTIVATE) {
+      if (agline) /* G-line already exists, so let's ignore it... */
+       return 0;
 
-        add_gline(sptr, ip_mask, host, parv[3], user, expire, 1);
-      }
-      else
-#endif
-        sendto_one(cptr, err_str(ERR_NOSUCHGLINE), me.name, parv[0], user,
-            host);
+      /* OK, create the local G-line */
+      Debug((DEBUG_DEBUG, "I am creating a local G-line here; target %s, "
+            "mask %s, operforce %s, action  %s, expire %Tu, reason: %s",
+            target, mask, flags & GLINE_OPERFORCE ? "YES" : "NO",
+            action == GLINE_ACTIVATE ? "+" : "-", expire, reason));
 
-      return 0;
-    }
+      return gline_add(cptr, sptr, mask, reason, expire, 0, 0,
+                      flags | GLINE_ACTIVE);
+    } else { /* OK, it's a deactivation/destruction */
+      if (!agline) /* G-line doesn't exist, so let's complain... */
+       return send_reply(sptr, ERR_NOSUCHGLINE, mask);
 
-    if (expire <= gline->expire)
-      expire = 0;
-
-    if ((active == -1 ||
-        (active ? GlineIsActive(gline) : !GlineIsActive(gline))) &&
-        expire == 0)
-    {
-      /* oper wants a list of one gline only */
-      sendto_one(cptr, rpl_str(RPL_GLIST), me.name, parv[0], gline->name,
-          gline->host, gline->expire, gline->reason,
-          GlineIsActive(gline) ? "" : " (Inactive)");
-      sendto_one(cptr, rpl_str(RPL_ENDOFGLIST), me.name, parv[0]);
-      return 0;
-    }
+      /* Let's now destroy the G-line */
+      Debug((DEBUG_DEBUG, "I am destroying a local G-line here; target %s, "
+            "mask %s, operforce %s, action %s", target, mask,
+            flags & GLINE_OPERFORCE ? "YES" : "NO",
+            action == GLINE_ACTIVATE ? "+" : "-"));
 
-    if (active != -1 &&
-        (active ? !GlineIsActive(gline) : GlineIsActive(gline)))
-    {
-      if (active)               /* reset activation on gline */
-        SetActive(gline);
-#ifdef OPER_LGLINE
-      else if (GlineIsLocal(gline))
-      {
-        /* Remove local G-line */
-        sendto_op_mask(SNO_GLINE, "%s removed local %s for %s@%s",
-            parv[0], gtype ? "BADCHAN" : "GLINE", gline->name, gline->host);
-#ifdef GPATH
-        write_log(GPATH, "# " TIME_T_FMT
-            " %s!%s@%s removed local %s for %s@%s\n",
-            TStime(), parv[0], cptr->user->username, cptr->user->host,
-            gtype ? "BADCHAN" : "GLINE",
-            gline->name, gline->host);
-#endif /* GPATH */
-        free_gline(gline, prev);        /* remove the gline */
-        return 0;
-      }
-#endif
-      else
-        ClearActive(gline);
+      return gline_destroy(cptr, sptr, agline);
     }
-    else
-      active = -1;              /* for later sendto_ops and logging functions */
-
-    if (expire)
-      gline->expire = expire;   /* reset expiration time */
-
-    /* inform the operators what's up */
-    if (active != -1)
-    {                           /* changing the activation */
-       sendto_op_mask(SNO_GLINE, !expire ? "%s %sactivating %s for %s@%s" :
-          "%s %sactivating %s for %s@%s and "
-          "resetting expiration time to " TIME_T_FMT,
-          parv[0], active ? "re" : "de", gtype ? "BADCHAN" : "GLINE",
-          gline->name, gline->host, gline->expire);
-#ifdef GPATH
-      write_log(GPATH, !expire ? "# " TIME_T_FMT " %s!%s@%s %sactivating "
-          "%s for %s@%s\n" : "# " TIME_T_FMT " %s!%s@%s %sactivating %s "
-          "for %s@%s and resetting expiration time to " TIME_T_FMT "\n",
-          TStime(), parv[0], cptr->user->username, cptr->user->host,
-          active ? "re" : "de", gtype ? "BADCHAN" : "GLINE", gline->name, 
-          gline->host, gline->expire);
-#endif /* GPATH */
+  }
 
-    }
-    else if (expire)
-    {                           /* changing only the expiration */
-      sendto_op_mask(SNO_GLINE,
-          "%s resetting expiration time on %s for %s@%s to " TIME_T_FMT,
-          parv[0], gtype ? "BADCHAN" : "GLINE", gline->name, gline->host, 
-          gline->expire);
-#ifdef GPATH
-      write_log(GPATH, "# " TIME_T_FMT " %s!%s@%s resetting expiration "
-          "time on %s for %s@%s to " TIME_T_FMT "\n", TStime(), parv[0],
-          cptr->user->username, cptr->user->host, gtype ? "BADCHAN" : "GLINE",
-          gline->name, gline->host, gline->expire);
-#endif /* GPATH */
-    }
+  /* can't modify a G-line that doesn't exist...
+   * (and if we are creating a new one, we need a reason and expiration)
+   */
+  if (!agline &&
+      (action == GLINE_MODIFY || action == GLINE_LOCAL_ACTIVATE ||
+       action == GLINE_LOCAL_DEACTIVATE || !reason || !expire))
+    return send_reply(sptr, ERR_NOSUCHGLINE, mask);
+
+  /* check for G-line permissions... */
+  if (action == GLINE_LOCAL_ACTIVATE || action == GLINE_LOCAL_DEACTIVATE) {
+    /* only need local privileges for locally-limited status changes */
+    if (!HasPriv(sptr, PRIV_LOCAL_GLINE))
+      return send_reply(sptr, ERR_NOPRIVILEGES);
+  } else { /* global privileges required */
+    if (!feature_bool(FEAT_CONFIG_OPERCMDS))
+      return send_reply(sptr, ERR_DISABLED, "GLINE");
+    else if (!HasPriv(sptr, PRIV_GLINE))
+      return send_reply(sptr, ERR_NOPRIVILEGES);
   }
-  return 0;
+
+  Debug((DEBUG_DEBUG, "I have a global G-line I am acting upon now; "
+        "target %s, mask %s, operforce %s, action %s, expire %Tu, "
+        "reason: %s; gline %s!  (fields present: %s %s)", target, 
+        mask, flags & GLINE_OPERFORCE ? "YES" : "NO",
+        action == GLINE_ACTIVATE ? "+" :
+        (action == GLINE_DEACTIVATE ? "-" :
+         (action == GLINE_LOCAL_ACTIVATE ? ">" :
+          (action == GLINE_LOCAL_DEACTIVATE ? "<" : "(MODIFY)"))),
+        expire, reason, agline ? "EXISTS" : "does not exist",
+        flags & GLINE_EXPIRE ? "expire" : "",
+        flags & GLINE_REASON ? "reason" : ""));
+
+  if (agline) /* modifying an existing G-line */
+    return gline_modify(cptr, sptr, agline, action, reason, expire,
+                       CurrentTime, 0, flags);
+
+  assert(action != GLINE_LOCAL_ACTIVATE);
+  assert(action != GLINE_LOCAL_DEACTIVATE);
+  assert(action != GLINE_MODIFY);
+
+  /* create a new G-line */
+  return gline_add(cptr, sptr, mask, reason, expire, CurrentTime, 0,
+                  flags | ((action == GLINE_ACTIVATE) ? GLINE_ACTIVE : 0));
 }
 
-#endif /* 0 */
+/*
+ * m_gline - user message handler
+ *
+ * parv[0] = Sender prefix
+ * parv[1] = [<server name>]
+ *
+ */
+int
+m_gline(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
+{
+  if (parc < 2)
+    return send_reply(sptr, ERR_NOSUCHGLINE, "");
 
-#endif /* 0 */
+  return gline_list(sptr, parv[1]);
+}