Author: Isomer <isomer@coders.net>
[ircu2.10.12-pk.git] / ircd / gline.c
index 911fa26bbd024bc342cdd82f7fae5b5e62246f5c..bc18f39ce2c49eb9671364535e1122bab176f4f7 100644 (file)
 #include "client.h"
 #include "ircd.h"
 #include "ircd_alloc.h"
+#include "ircd_reply.h"
 #include "ircd_string.h"
 #include "match.h"
 #include "numeric.h"
 #include "s_bsd.h"
+#include "s_debug.h"
 #include "s_misc.h"
 #include "send.h"
 #include "struct.h"
+#include "support.h"
+#include "msg.h"
+#include "numnicks.h"
+#include "numeric.h"
 #include "sys.h"    /* FALSE bleah */
 
 #include <assert.h>
+#include <string.h>
+#include <stdio.h>
+#include <arpa/inet.h> /* for inet_ntoa */
 
 struct Gline* GlobalGlineList  = 0;
 struct Gline* BadChanGlineList = 0;
@@ -56,13 +65,37 @@ static struct Gline *
 make_gline(char *userhost, char *reason, time_t expire, time_t lastmod,
           unsigned int flags)
 {
-  struct Gline *agline;
+  struct Gline *gline, *sgline, *after = 0;
   char *user, *host;
 
-  agline = (struct Gline *)MyMalloc(sizeof(struct Gline)); /* alloc memory */
-  assert(0 != agline);
+  if (!(flags & GLINE_BADCHAN)) { /* search for overlapping glines first */
+    canon_userhost(userhost, &user, &host, "*"); /* find user and host */
+
+    for (gline = GlobalGlineList; gline; gline = sgline) {
+      sgline = gline->gl_next;
 
-  gline->gl_expire = expire; /* initialize gline... */
+      if (gline->gl_expire <= CurrentTime)
+       gline_free(gline);
+      else if ((gline->gl_flags & GLINE_LOCAL) != (flags & GLINE_LOCAL))
+       continue;
+      else if (!mmatch(gline->gl_user, user) && /* gline contains new mask */
+              !mmatch(gline->gl_host, host)) {
+       if (expire <= gline->gl_expire) /* will expire before wider gline */
+         return 0;
+       else
+         after = gline; /* stick new gline after this one */
+      } else if (!mmatch(user, gline->gl_user) && /* new mask contains gline */
+                !mmatch(host, gline->gl_host) &&
+                gline->gl_expire <= expire) /* gline expires before new one */
+       gline_free(gline); /* save some memory */
+    }
+  }
+
+  gline = (struct Gline *)MyMalloc(sizeof(struct Gline)); /* alloc memory */
+  assert(0 != gline);
+
+  DupString(gline->gl_reason, reason); /* initialize gline... */
+  gline->gl_expire = expire;
   gline->gl_lastmod = lastmod;
   gline->gl_flags = flags & GLINE_MASK;
 
@@ -73,25 +106,48 @@ make_gline(char *userhost, char *reason, time_t expire, time_t lastmod,
     gline->gl_next = BadChanGlineList; /* then link it into list */
     gline->gl_prev_p = &BadChanGlineList;
     if (BadChanGlineList)
-      BadChanGlineList->gl_prev_p = &agline->gl_next;
-    BadChanGlineList = agline;
+      BadChanGlineList->gl_prev_p = &gline->gl_next;
+    BadChanGlineList = gline;
   } else {
-    canon_userhost(userhost, &user, &host, "*"); /* find user and host */
-
     DupString(gline->gl_user, user); /* remember them... */
     DupString(gline->gl_host, host);
 
-    if (check_if_ipmask(host)) /* mark if it's an IP mask */
+    if (check_if_ipmask(host)) { /* mark if it's an IP mask */
+      int class;
+      char ipname[16];
+      int ad[4] = { 0 };
+      int bits2 = 0;
+       
+      class = sscanf(host,"%d.%d.%d.%d/%d",
+                     &ad[0],&ad[1],&ad[2],&ad[3], &bits2);
+      if (class!=5) {
+        gline->bits=class*8;
+      }
+      else {
+        gline->bits=bits2;
+      }
+      sprintf_irc(ipname,"%d.%d.%d.%d",ad[0],ad[1],ad[2],ad[3]);
+      gline->ipnum.s_addr = inet_addr(ipname);
+      Debug((DEBUG_DEBUG,"IP gline: %08x/%i",gline->ipnum.s_addr,gline->bits));
       gline->gl_flags |= GLINE_IPMASK;
+    }
 
-    gline->gl_next = GlobalGlineList; /* then link it into list */
-    gline->gl_prev_p = &GlobalGlineList;
-    if (GlobalGlineList)
-      GlobalGlineList->gl_prev_p = &agline->gl_next;
-    GlobalGlineList = agline;
+    if (after) {
+      gline->gl_next = after->gl_next;
+      gline->gl_prev_p = &after->gl_next;
+      if (after->gl_next)
+       after->gl_next->gl_prev_p = &gline->gl_next;
+      after->gl_next = gline;
+    } else {
+      gline->gl_next = GlobalGlineList; /* then link it into list */
+      gline->gl_prev_p = &GlobalGlineList;
+      if (GlobalGlineList)
+       GlobalGlineList->gl_prev_p = &gline->gl_next;
+      GlobalGlineList = gline;
+    }
   }
 
-  return agline;
+  return gline;
 }
 
 static int
@@ -110,61 +166,57 @@ do_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
     if ((acptr = LocalClientArray[fd])) {
       if (!acptr->user)
        continue;
-
-      if ((GlineIsIpMask(gline) ? match(gline->host, acptr->sock_ip) :
-          match(gline->host, acptr->sockhost)) == 0 &&
-         (!acptr->user->username ||
-          match(gline->name, acptr->user->username) == 0)) {
-       /* ok, here's one that got G-lined */
-       sendto_one(acptr, ":%s %d %s :*** %s.", me.name, ERR_YOUREBANNEDCREEP,
-                  acptr->name, gline->reason);
-
-       /* let the ops know about it */
-       sendto_op_mask(SNO_GLINE, "G-line active for %s",
-                      get_client_name(acptr, FALSE));
-
-       /* and get rid of him */
-       if ((tval = exit_client_msg(cptr, acptr, &me, "G-lined (%s)",
-                                   gline->reason)))
-         retval = tval; /* retain killed status */
+       
+      if (acptr->user->username && 
+          match(gline->gl_user, acptr->user->username) == 0)
+          continue;
+          
+      if (GlineIsIpMask(gline)) {
+        Debug((DEBUG_DEBUG,"IP gline: %08x %08x/%i",cptr->ip.s_addr,gline->ipnum.s_addr,gline->bits));
+        if ((cptr->ip.s_addr & NETMASK(gline->bits)) != gline->ipnum.s_addr)
+          continue;
+      }
+      else {
+        if (match(gline->gl_host, acptr->sock_ip) != 0)
+          continue;
       }
+
+      /* ok, here's one that got G-lined */
+      send_reply(acptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s",
+          gline->gl_reason);
+
+      /* let the ops know about it */
+      sendto_opmask_butone(0, SNO_GLINE, "G-line active for %s",
+                    get_client_name(acptr, FALSE));
+
+      /* and get rid of him */
+      if ((tval = exit_client_msg(cptr, acptr, &me, "G-lined (%s)",
+          gline->gl_reason)))
+        retval = tval; /* retain killed status */
     }
   }
-
   return retval;
 }
 
 static void
 propagate_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
 {
-  if (GlineIsLocal(gline)) /* don't propagate local glines */
+  if (GlineIsLocal(gline) || (IsUser(sptr) && !gline->gl_lastmod))
     return;
 
-  if (IsUser(sptr)) { /* select appropriate source */
-    assert(0 != gline->gl_lastmod);
-    sendto_serv_butone(cptr, "%s%s " TOK_GLINE " * %c%s%s%s " TIME_T_FMT " "
-                      TIME_T_FMT " :%s", NumNick(sptr),
-                      GlineIsActive(gline) ? '+' : '-', gline->gl_user,
-                      GlineIsBadChan(gline) ? "" : "@",
-                      GlineIsBadChan(gline) ? "" : gline->gl_host,
-                      gline->gl_expire, gline->gl_lastmod, gline->gl_reason);
-  } else {
-    if (gline->gl_lastmod)
-      sendto_serv_butone(cptr, "%s " TOK_GLINE " * %c%s%s%s " TIME_T_FMT " "
-                        TIME_T_FMT " :%s", NumServ(sptr),
-                        GlineIsActive(gline) ? '+' : '-', gline->gl_user,
-                        GlineIsBadChan(gline) ? "" : "@",
-                        GlineIsBadChan(gline) ? "" : gline->gl_host,
-                        gline->gl_expire, gline->gl_lastmod,
-                        gline->gl_reason);
-    else
-      sendto_serv_butone(cptr, "%s " TOK_GLINE " * %c%s%s%s " TIME_T_FMT
-                        " :%s", NumServ(sptr),
-                        GlineIsActive(gline) ? '+' : '-', gline->gl_user,
-                        GlineIsBadChan(gline) ? "" : "@",
-                        GlineIsBadChan(gline) ? "" : gline->gl_host,
-                        gline->gl_expire, gline->gl_reason);
-  }
+  if (gline->gl_lastmod)
+    sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
+                         GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
+                         GlineIsBadChan(gline) ? "" : "@",
+                         GlineIsBadChan(gline) ? "" : gline->gl_host,
+                         gline->gl_expire - CurrentTime, gline->gl_lastmod,
+                         gline->gl_reason);
+  else
+    sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu :%s",
+                         GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
+                         GlineIsBadChan(gline) ? "" : "@",
+                         GlineIsBadChan(gline) ? "" : gline->gl_host,
+                         gline->gl_expire - CurrentTime, gline->gl_reason);
 }
 
 int 
@@ -182,45 +234,47 @@ gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
    */
   if (!(flags & GLINE_FORCE) && (expire <= 0 || expire > GLINE_MAX_EXPIRE)) {
     if (!IsServer(sptr) && MyConnect(sptr))
-      send_error_to_client(sptr, ERR_BADEXPIRE, expire);
+      send_reply(sptr, ERR_BADEXPIRE, expire);
     return 0;
   }
 
-  expire += TStime(); /* convert from lifetime to timestamp */
+  expire += CurrentTime; /* convert from lifetime to timestamp */
 
-#ifdef BADCHAN
-  if (*userhost == '#' || *userhost == '&' || *userhost == '+')
+  /* NO_OLD_GLINE allows *@#channel to work correctly */
+  if (*userhost == '#' || *userhost == '&' || *userhost == '+'
+# ifndef NO_OLD_GLINE
+      || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
+# endif /* OLD_GLINE */
+      ) {
+# ifndef LOCAL_BADCHAN
+    if (flags & GLINE_LOCAL)
+      return 0;
+# endif
     flags |= GLINE_BADCHAN;
-#endif
+  }
 
   /* Inform ops... */
-  sendto_op_mask(SNO_GLINE, "%s adding %s %s for %s, expiring at "
-                TIME_T_FMT ": %s",
-                IsServer(sptr) ? sptr->name : sptr->user->server->name,
-                flags & GLINE_LOCAL ? "local" : "global",
-                flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
-                expire, reason);
+  sendto_opmask_butone(0, SNO_GLINE, "%s adding %s %s for %s, expiring at "
+                      "%Tu: %s",
+                      IsServer(sptr) ? sptr->name : sptr->user->server->name,
+                      flags & GLINE_LOCAL ? "local" : "global",
+                      flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
+                      expire + TSoffset, reason);
 
 #ifdef GPATH
   /* and log it */
-  if (IsServer(sptr))
-    write_log(GPATH, "# " TIME_T_FMT " %s adding %s %s for %s, expiring at "
-             TIME_T_FMT ": %s\n", TStime(), sptr->name,
-             flags & GLINE_LOCAL ? "local" : "global",
-             flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost, expire,
-             reason);
-  else
-    write_log(GPATH, "# " TIME_T_FMT " %s!%s@%s adding %s %s for %s, "
-             "expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
-             sptr->user->username, sptr->user->host,
-             flags & GLINE_LOCAL ? "local" : "global",
-             flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost, expire,
-             reason);
+  write_log(GPATH, "# %Tu %C adding %s %s for %s, expiring at %Tu: %s\n",
+           TStime(), sptr, flags & GLINE_LOCAL ? "local" : "global",
+           flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
+           expire + TSoffset, reason);
 #endif /* GPATH */
 
   /* make the gline */
   agline = make_gline(userhost, reason, expire, lastmod, flags);
 
+  if (!agline) /* if it overlapped, silently return */
+    return 0;
+
   propagate_gline(cptr, sptr, agline);
 
   if (GlineIsBadChan(agline))
@@ -237,88 +291,106 @@ gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
 
 int
 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
-              time_t lastmod)
+              time_t lastmod, unsigned int flags)
 {
+  unsigned int saveflags = 0;
+
   assert(0 != gline);
 
-  gline->gl_flags |= GLINE_ACTIVE;
-  gline->gl_lastmod = lastmod;
+  saveflags = gline->gl_flags;
+
+  if (flags & GLINE_LOCAL)
+    gline->gl_flags &= ~GLINE_LDEACT;
+  else {
+    gline->gl_flags |= GLINE_ACTIVE;
+
+    if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
+      gline->gl_lastmod++;
+    else
+      gline->gl_lastmod = lastmod;
+  }
+
+  if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
+    return 0; /* was active to begin with */
 
   /* Inform ops and log it */
-  sendto_op_mask(SNO_GLINE, "%s activating %s %s for %s%s%s, expiring at "
-                TIME_T_FMT ": %s",
-                IsServer(sptr) ? sptr->name : sptr->user->server->name,
-                GlineIsLocal(gline) ? "local" : "global",
-                GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
-                gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
-                GlineIsBadChan(gline) ? "" : gline->gl_host, gline->gl_expire,
-                gline->gl_reason);
+  sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
+                      "expiring at %Tu: %s",
+                      IsServer(sptr) ? sptr->name : sptr->user->server->name,
+                      GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
+                      gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
+                      GlineIsBadChan(gline) ? "" : gline->gl_host,
+                      gline->gl_expire + TSoffset, gline->gl_reason);
 
 #ifdef GPATH
-  if (IsServer(sptr))
-    write_log(GPATH, "# " TIME_T_FMT " %s activating %s %s for %s%s%s, "
-             "expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
-             GlineIsLocal(gline) ? "local" : "global",
-             GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
-             gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
-             GlineIsBadChan(gline) ? "" : gline->gl_host, gline->gl_expire,
-             gline->gl_reason);
-  else
-    write_log(GPATH, "# " TIME_T_FMT " %s!%s@%s activating %s %s for "
-             "%s%s%s, expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
-             sptr->user->username, sptr->user->host,
-             GlineIsLocal(gline) ? "local" : "global",
-             GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
-             gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
-             GlineIsBadChan(gline) ? "" : gline->gl_host, gline->gl_expire,
-             gline->gl_reason);
+  write_log(GPATH, "# %Tu %C activating global %s for %s%s%s, expiring at "
+           "%Tu: %s\n", TStime(), sptr,
+           GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
+           gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
+           GlineIsBadChan(gline) ? "" : gline->gl_host,
+           gline->gl_expire + TSoffset, gline->gl_reason);
 #endif /* GPATH */
 
-  propagate_gline(cptr, sptr, gline);
+  if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
+    propagate_gline(cptr, sptr, gline);
 
   return GlineIsBadChan(gline) ? 0 : do_gline(cptr, sptr, gline);
 }
 
 int
 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
-                time_t lastmod)
+                time_t lastmod, unsigned int flags)
 {
+  unsigned int saveflags = 0;
+  char *msg;
+
   assert(0 != gline);
 
-  gline->gl_flags &= ~GLINE_ACTIVE;
-  gline->gl_lastmod = lastmod;
+  saveflags = gline->gl_flags;
+
+  if (GlineIsLocal(gline))
+    msg = "removing local";
+  else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL))
+    msg = "removing global";
+  else {
+    msg = "deactivating global";
+
+    if (flags & GLINE_LOCAL)
+      gline->gl_flags |= GLINE_LDEACT;
+    else {
+      gline->gl_flags &= ~GLINE_ACTIVE;
+
+      if (gline->gl_lastmod >= lastmod)
+       gline->gl_lastmod++;
+      else
+       gline->gl_lastmod = lastmod;
+    }
+
+    if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
+      return 0; /* was inactive to begin with */
+  }
 
   /* Inform ops and log it */
-  sendto_op_mask(SNO_GLINE, "%s deactivating %s %s for %s%s%s, expiring at "
-                TIME_T_FMT ": %s",
-                IsServer(sptr) ? sptr->name : sptr->user->server->name,
-                GlineIsLocal(gline) ? "local" : "global",
-                GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
-                gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
-                GlineIsBadChan(gline) ? "" : gline->gl_host, gline->gl_expire,
-                gline->gl_reason);
+  sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
+                      "%s",
+                      IsServer(sptr) ? sptr->name : sptr->user->server->name,
+                      msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
+                      gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
+                      GlineIsBadChan(gline) ? "" : gline->gl_host,
+                      gline->gl_expire + TSoffset, gline->gl_reason);
 
 #ifdef GPATH
-  if (IsServer(sptr))
-    write_log(GPATH, "# " TIME_T_FMT " %s deactivating %s %s for %s%s%s, "
-             "expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
-             GlineIsLocal(gline) ? "local" : "global",
-             GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
-             gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
-             GlineIsBadChan(gline) ? "" : gline->gl_host, gline->gl_expire,
-             gline->gl_reason);
-  else
-    write_log(GPATH, "# " TIME_T_FMT " %s!%s@%s deactivating %s %s for "
-             "%s%s%s, expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
-             sptr->user->username, sptr->user->host,
-             GlineIsLocal(gline) ? "local" : "global",
-             GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
-             gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
-             GlineIsBadChan(gline) ? "" : gline->gl_host, gline->gl_expire,
-             gline->gl_reason);
+  write_log(GPATH, "# %Tu %C %s %s for %s%s%s, expiring at %Tu: %s\n",
+           TStime(), sptr, msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
+           gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
+           GlineIsBadChan(gline) ? "" : gline->gl_host,
+           gline->gl_expire + TSoffset, gline->gl_reason);
 #endif /* GPATH */
 
-  propagate_gline(cptr, sptr, gline);
+  if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
+    gline_free(gline);
+  else if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
+    propagate_gline(cptr, sptr, gline);
 
   return 0;
 }
@@ -334,14 +406,23 @@ gline_find(char *userhost, unsigned int flags)
     for (gline = BadChanGlineList; gline; gline = sgline) {
       sgline = gline->gl_next;
 
-      if (gline->gl_expire <= TStime())
+      if (gline->gl_expire <= CurrentTime)
        gline_free(gline);
-      else if (match(gline->gl_user, userhost) == 0)
+      else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
+              (flags & GLINE_LASTMOD && !gline->gl_lastmod))
+       continue;
+      else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
+               match(gline->gl_user, userhost)) == 0)
        return gline;
     }
   }
 
-  if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN)
+  if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
+      *userhost == '#' || *userhost == '&' || *userhost == '+'
+#ifndef NO_OLD_GLINE
+      || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
+#endif /* NO_OLD_GLINE */
+      )
     return 0;
 
   DupString(t_uh, userhost);
@@ -350,12 +431,22 @@ gline_find(char *userhost, unsigned int flags)
   for (gline = GlobalGlineList; gline; gline = sgline) {
     sgline = gline->gl_next;
 
-    if (gline->gl_expire <= TStime())
+    if (gline->gl_expire <= CurrentTime)
       gline_free(gline);
-    else if (match(gline->host, host) == 0 &&
-            ((!user && ircd_strcmp(gline->user, "*") == 0) ||
-             match(gline->user, user) == 0))
+    else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
+            (flags & GLINE_LASTMOD && !gline->gl_lastmod))
+      continue;
+    else if (flags & GLINE_EXACT) {
+      if (ircd_strcmp(gline->gl_host, host) == 0 &&
+         ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
+          ircd_strcmp(gline->gl_user, user) == 0))
+       break;
+    } else {
+      if (match(gline->gl_host, host) == 0 &&
+         ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
+          match(gline->gl_user, user) == 0))
       break;
+    }
   }
 
   MyFree(t_uh);
@@ -364,7 +455,7 @@ gline_find(char *userhost, unsigned int flags)
 }
 
 struct Gline *
-gline_lookup(struct Client *cptr)
+gline_lookup(struct Client *cptr, unsigned int flags)
 {
   struct Gline *gline;
   struct Gline *sgline;
@@ -372,15 +463,32 @@ gline_lookup(struct Client *cptr)
   for (gline = GlobalGlineList; gline; gline = sgline) {
     sgline = gline->gl_next;
 
-    if (gline->gl_expire <= TStime())
+    if (gline->gl_expire <= CurrentTime) {
       gline_free(gline);
-    else if ((GlineIsIpMask(gline) ?
-             match(gline->gl_host, cptr->sock_ip) :
-             match(gline->gl_host, cptr->sockhost)) == 0 &&
-            match(gline->gl_user, cptr->user->username) == 0)
-      return gline;
+      continue;
+    }
+    
+    if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
+            (flags & GLINE_LASTMOD && !gline->gl_lastmod))
+      continue;
+     
+    if (match(gline->gl_user, cptr->user->username) != 0)
+      continue;
+        
+    if (GlineIsIpMask(gline)) {
+      Debug((DEBUG_DEBUG,"IP gline: %08x %08x/%i",cptr->ip.s_addr,gline->ipnum.s_addr,gline->bits));
+      if ((cptr->ip.s_addr & NETMASK(gline->bits)) != gline->ipnum.s_addr)
+        continue;
+    }
+    else {
+      if (match(gline->gl_host, cptr->user->host) != 0) 
+        continue;
+    }
+    return gline;
   }
-
+  /*
+   * No Glines matched
+   */
   return 0;
 }
 
@@ -409,25 +517,25 @@ gline_burst(struct Client *cptr)
   for (gline = GlobalGlineList; gline; gline = sgline) { /* all glines */
     sgline = gline->gl_next;
 
-    if (gline->gl_expire <= TStime()) /* expire any that need expiring */
+    if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
       gline_free(gline);
     else if (!GlineIsLocal(gline) && gline->gl_lastmod)
-      sendto_one(cptr, "%s " TOK_GLINE " * %c%s@%s " TIME_T_FMT " " TIME_T_FMT
-                " :%s", NumServ(&me), GlineIsActive(gline) ? '+' : '-',
-                gline->gl_user, gline->gl_host, gline->gl_expire,
-                gline->gl_lastmod, gline->gl_reason);
+      sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s@%s %Tu %Tu :%s",
+                   GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
+                   gline->gl_host, gline->gl_expire - CurrentTime,
+                   gline->gl_lastmod, gline->gl_reason);
   }
 
   for (gline = BadChanGlineList; gline; gline = sgline) { /* all glines */
     sgline = gline->gl_next;
 
-    if (gline->gl_expire <= TStime()) /* expire any that need expiring */
+    if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
       gline_free(gline);
     else if (!GlineIsLocal(gline) && gline->gl_lastmod)
-      sendto_one(cptr, "%s " TOK_GLINE " * %c%s " TIME_T_FMT " " TIME_T_FMT
-                " :%s", NumServ(&me), GlineIsActive(gline) ? '+' : '-',
-                gline->gl_user, gline->gl_expire, gline->gl_lastmod,
-                gline->gl_reason);
+      sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu :%s",
+                   GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
+                   gline->gl_expire - CurrentTime, gline->gl_lastmod,
+                   gline->gl_reason);
   }
 }
 
@@ -437,16 +545,12 @@ gline_resend(struct Client *cptr, struct Gline *gline)
   if (GlineIsLocal(gline) || !gline->gl_lastmod)
     return 0;
 
-  if (GlineIsBadChan(gline))
-    sendto_one(cptr, "%s " TOK_GLINE " * %c%s " TIME_T_FMT " " TIME_T_FMT
-              " :%s", NumServ(&me), GlineIsActive(gline) ? '+' : '-',
-              gline->gl_user, gline->gl_expire, gline->gl_lastmod,
-              gline->gl_reason);
-  else
-    sendto_one(cptr, "%s " TOK_GLINE " * %c%s@%s " TIME_T_FMT " " TIME_T_FMT
-              " :%s", NumServ(&me), GlineIsActive(gline) ? '+' : '-',
-              gline->gl_user, gline->gl_host, gline->gl_expire,
-              gline->gl_lastmod, gline->gl_reason);
+  sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
+               GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
+               GlineIsBadChan(gline) ? "" : "@",
+               GlineIsBadChan(gline) ? "" : gline->gl_host,
+               gline->gl_expire - CurrentTime, gline->gl_lastmod,
+               gline->gl_reason);
 
   return 0;
 }
@@ -458,26 +562,25 @@ gline_list(struct Client *sptr, char *userhost)
   struct Gline *sgline;
 
   if (userhost) {
-    if (!(gline = gline_find(userhost, GLINE_ANY))) { /* no such gline */
-      send_error_to_client(sptr, ERR_NOSUCHGLINE, userhost);
-      return 0;
-    }
+    if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
+      return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
 
     /* send gline information along */
-    sendto_one(sptr, rpl_str(GLIST), me.name, sptr->name, gline->gl_user,
+    send_reply(sptr, RPL_GLIST, gline->gl_user,
               GlineIsBadChan(gline) ? "" : "@",
-              GlineIsBadChan(gline) ? "" : gline->gl_host, gline->gl_expire,
+              GlineIsBadChan(gline) ? "" : gline->gl_host,
+              gline->gl_expire + TSoffset,
               GlineIsLocal(gline) ? me.name : "*",
               GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
   } else {
     for (gline = GlobalGlineList; gline; gline = sgline) {
       sgline = gline->gl_next;
 
-      if (gline->gl_expire <= TStime())
+      if (gline->gl_expire <= CurrentTime)
        gline_free(gline);
       else
-       sendto_one(sptr, rpl_str(GLIST), me.name, sptr->name, gline->gl_user,
-                  "@", gline->gl_host, gline->gl_expire,
+       send_reply(sptr, RPL_GLIST, gline->gl_user, "@", gline->gl_host,
+                  gline->gl_expire + TSoffset,
                   GlineIsLocal(gline) ? me.name : "*",
                   GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
     }
@@ -485,19 +588,18 @@ gline_list(struct Client *sptr, char *userhost)
     for (gline = BadChanGlineList; gline; gline = sgline) {
       sgline = gline->gl_next;
 
-      if (gline->gl_expire <= TStime())
+      if (gline->gl_expire <= CurrentTime)
        gline_free(gline);
       else
-       sendto_one(sptr, rpl_str(GLIST), me.name, sptr->name, gline->gl_user,
-                  "", "", gline->gl_expire,
+       send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
+                  gline->gl_expire + TSoffset,
                   GlineIsLocal(gline) ? me.name : "*",
                   GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
     }
   }
 
   /* end of gline information */
-  sendto_one(cptr, rpl_str(RPL_ENDOFGLIST), me.name, sptr->name);
-  return 0;
+  return send_reply(sptr, RPL_ENDOFGLIST);
 }
 
 void
@@ -509,242 +611,10 @@ gline_stats(struct Client *sptr)
   for (gline = GlobalGlineList; gline; gline = sgline) {
     sgline = gline->gl_next;
 
-    if (gline->gl_expire <= TStime())
+    if (gline->gl_expire <= CurrentTime)
       gline_free(gline);
     else
-      sendto_one(sptr, rpl_str(RPL_STATSGLINE), me.name, sptr->name, 'G',
-                gline->gl_user, gline->gl_host, gline->gl_expire,
-                gline->gl_reason);
+      send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user, gline->gl_host,
+                gline->gl_expire + TSoffset, gline->gl_reason);
   }
 }
-
-
-#if 0 /* forget about it! */
-struct Gline *make_gline(int is_ipmask, char *host, char *reason,
-                         char *name, time_t expire)
-{
-  struct Gline *agline;
-
-#ifdef BADCHAN
-  int gtype = 0;
-  if ('#' == *host || '&' == *host || '+' == *host)
-    gtype = 1; /* BAD CHANNEL GLINE */
-#endif
-
-  agline = (struct Gline*) MyMalloc(sizeof(struct Gline)); /* alloc memory */
-  assert(0 != agline);
-  DupString(agline->host, host);        /* copy vital information */
-  DupString(agline->reason, reason);
-  DupString(agline->name, name);
-  agline->expire = expire;
-  agline->gflags = GLINE_ACTIVE;        /* gline is active */
-  if (is_ipmask)
-    SetGlineIsIpMask(agline);
-#ifdef BADCHAN
-  if (gtype)
-  { 
-    agline->next = BadChanGlineList;    /* link it into the list */
-    return (BadChanGlineList = agline);
-  }
-#endif
-  agline->next = GlobalGlineList;       /* link it into the list */
-  return (GlobalGlineList = agline);
-}
-
-struct Gline *find_gline(struct Client *cptr, struct Gline **pgline)
-{
-  struct Gline* gline = GlobalGlineList;
-  struct Gline* prev = 0;
-
-  while (gline) {
-    /*
-     * look through all glines
-     */
-    if (gline->expire <= TStime()) {
-      /*
-       * handle expired glines
-       */
-      free_gline(gline, prev);
-      gline = prev ? prev->next : GlobalGlineList;
-      if (!gline)
-        break;                  /* gline == NULL means gline == NULL */
-      continue;
-    }
-
-    /* Does gline match? */
-    /* Added a check against the user's IP address as well -Kev */
-    if ((GlineIsIpMask(gline) ?
-        match(gline->host, ircd_ntoa((const char*) &cptr->ip)) :
-        match(gline->host, cptr->sockhost)) == 0 &&
-        match(gline->name, cptr->user->username) == 0) {
-      if (pgline)
-        *pgline = prev; /* If they need it, give them the previous gline
-                                   entry (probably for free_gline, below) */
-      return gline;
-    }
-
-    prev = gline;
-    gline = gline->next;
-  }
-
-  return 0;                  /* found no glines */
-}
-
-void free_gline(struct Gline* gline, struct Gline* prev)
-{
-  assert(0 != gline);
-  if (prev)
-    prev->next = gline->next;   /* squeeze agline out */
-  else { 
-#ifdef BADCHAN
-    assert(0 != gline->host);
-    if ('#' == *gline->host ||
-        '&' == *gline->host ||
-        '+' == *gline->host) {
-      BadChanGlineList = gline->next;
-    }
-    else
-#endif
-      GlobalGlineList = gline->next;
-  }
-
-  MyFree(gline->host);  /* and free up the memory */
-  MyFree(gline->reason);
-  MyFree(gline->name);
-  MyFree(gline);
-}
-
-void gline_remove_expired(time_t now)
-{
-  struct Gline* gline;
-  struct Gline* prev = 0;
-  
-  for (gline = GlobalGlineList; gline; gline = gline->next) {
-    if (gline->expire < now) {
-      free_gline(gline, prev);
-      gline = (prev) ? prev : GlobalGlineList;
-      if (!gline)
-        break;
-      continue;
-    }
-    prev = gline;
-  }
-}
-
-#ifdef BADCHAN
-int bad_channel(const char* name)
-{ 
-  struct Gline* agline = BadChanGlineList;
-
-  while (agline)
-  { 
-    if ((agline->gflags & GLINE_ACTIVE) && (agline->expire > TStime()) && 
-         !mmatch(agline->host, name)) { 
-      return 1;
-    }
-    agline = agline->next;
-  }
-  return 0;
-}
-
-void bad_channel_remove_expired(time_t now)
-{
-  struct Gline* gline;
-  struct Gline* prev = 0;
-  
-  for (gline = BadChanGlineList; gline; gline = gline->next) {
-    if (gline->expire < now) {
-      free_gline(gline, prev);
-      gline = (prev) ? prev : BadChanGlineList;
-      if (!gline)
-        break;
-      continue;
-    }
-    prev = gline;
-  }
-}
-
-#endif
-
-
-void add_gline(struct Client *sptr, int ip_mask, char *host, char *comment,
-               char *user, time_t expire, int local)
-{
-  struct Client *acptr;
-  struct Gline *agline;
-  int fd;
-  int gtype = 0;
-  assert(0 != host);
-
-#ifdef BADCHAN
-  if ('#' == *host || '&' == *host || '+' == *host)
-    gtype = 1;   /* BAD CHANNEL */
-#endif
-
-  /* Inform ops */
-  sendto_op_mask(SNO_GLINE,
-      "%s adding %s%s for %s@%s, expiring at " TIME_T_FMT ": %s", sptr->name,
-      local ? "local " : "",
-      gtype ? "BADCHAN" : "GLINE", user, host, expire, comment);
-
-#ifdef GPATH
-  write_log(GPATH,
-      "# " TIME_T_FMT " %s adding %s %s for %s@%s, expiring at " TIME_T_FMT
-      ": %s\n", TStime(), sptr->name, local ? "local" : "global",
-      gtype ? "BADCHAN" : "GLINE", user, host, expire, comment);
-
-  /* this can be inserted into the conf */
-  if (!gtype)
-    write_log(GPATH, "%c:%s:%s:%s\n", ip_mask ? 'k' : 'K', host, comment, 
-      user);
-#endif /* GPATH */
-
-  agline = make_gline(ip_mask, host, comment, user, expire);
-  if (local)
-    SetGlineIsLocal(agline);
-
-#ifdef BADCHAN
-  if (gtype)
-    return;
-#endif
-
-  for (fd = HighestFd; fd >= 0; --fd) { 
-    /*
-     * get the users!
-     */ 
-    if ((acptr = LocalClientArray[fd])) {
-      if (!acptr->user)
-        continue;
-#if 0
-      /*
-       * whee!! :)
-       */
-      if (!acptr->user || strlen(acptr->sockhost) > HOSTLEN ||
-          (acptr->user->username ? strlen(acptr->user->username) : 0) > HOSTLEN)
-        continue;               /* these tests right out of
-                                   find_kill for safety's sake */
-#endif
-
-      if ((GlineIsIpMask(agline) ?  match(agline->host, acptr->sock_ip) :
-          match(agline->host, acptr->sockhost)) == 0 &&
-          (!acptr->user->username ||
-          match(agline->name, acptr->user->username) == 0))
-      {
-
-        /* ok, he was the one that got G-lined */
-        sendto_one(acptr, ":%s %d %s :*** %s.", me.name,
-            ERR_YOUREBANNEDCREEP, acptr->name, agline->reason);
-
-        /* let the ops know about my first kill */
-        sendto_op_mask(SNO_GLINE, "G-line active for %s",
-            get_client_name(acptr, FALSE));
-
-        /* and get rid of him */
-        if (sptr != acptr)
-          exit_client_msg(sptr->from, acptr, &me, "G-lined (%s)", agline->reason);
-      }
-    }
-  }
-}
-
-#endif /* 0 */