]> jfr.im git - irc/quakenet/newserv.git/commitdiff
Implement ammending existing gline log entries.
authorGunnar Beutner <redacted>
Thu, 18 Jul 2013 15:05:43 +0000 (17:05 +0200)
committerGunnar Beutner <redacted>
Thu, 18 Jul 2013 15:05:43 +0000 (17:05 +0200)
--HG--
branch : shroudtrusts

glines/glines.h
glines/glines_buf.c
glines/glines_commands.c
glines/glines_util.c
newsearch/ns-gline.c
trusts/trusts_commands.c

index 944060df982669eb5641bcefb656c30f26b5908f..37a8be18b52ef57333359e184094276fc5aa58e8 100644 (file)
@@ -81,9 +81,9 @@ typedef struct gline {
 typedef struct glinebuf {
   int id;
   sstring *comment;
-  time_t flush;
+  time_t commit;
+  time_t ammend;
 
-  int merge;
   gline *glines;
 
   int userhits;
@@ -119,13 +119,14 @@ int glinebynick(nick *np, int duration, const char *reason, int flags, const cha
 void glineunsetmask(const char *mask);
 
 /* glines_buf.c */
-void glinebufinit(glinebuf *gbuf, int merge);
+void glinebufinit(glinebuf *gbuf, int id);
 gline *glinebufadd(glinebuf *gbuf, const char *mask, const char *creator, const char *reason, time_t expire, time_t lastmod, time_t lifetime);
 void glinebufaddbyip(glinebuf *gbuf, const char *user, struct irc_in_addr *ip, unsigned char bits, int flags, const char *creator, const char *reason, time_t expire, time_t lastmod, time_t lifetime);
 void glinebufaddbynick(glinebuf *gbuf, nick *, int flags, const char *creator, const char *reason, time_t expire, time_t lastmod, time_t lifetime);
 void glinebufcounthits(glinebuf *gbuf, int *users, int *channels, nick *spewto);
 int glinebufchecksane(glinebuf *gbuf, nick *spewto, int overridesanity, int overridelimit, int spewhits);
 void glinebufspew(glinebuf *gbuf, nick *spewto);
+void glinebufmerge(glinebuf *gbuf);
 void glinebufcommit(glinebuf *gbuf, int propagate);
 void glinebufabort(glinebuf *gbuf);
 int glinebufundo(int id);
index 3c906c1f896c64db46b2b0a49721e7ed64b5f22b..960c905535dacef47a0ef967246a9ceb2add5473 100644 (file)
@@ -12,18 +12,17 @@ static int nextglinebufid = 1;
 glinebuf *glinebuflog[MAXGLINELOG] = {};
 int glinebuflogoffset = 0;
 
-void glinebufinit(glinebuf *gbuf, int merge) {
-  gbuf->id = 0;
+void glinebufinit(glinebuf *gbuf, int id) {
+  gbuf->id = id;
   gbuf->comment = NULL;
   gbuf->glines = NULL;
-  gbuf->merge = merge;
   gbuf->userhits = 0;
   gbuf->channelhits = 0;
   array_init(&gbuf->hits, sizeof(sstring *));
 }
 
 gline *glinebufadd(glinebuf *gbuf, const char *mask, const char *creator, const char *reason, time_t expire, time_t lastmod, time_t lifetime) {
-  gline *gl, *sgl, **pnext;
+  gline *gl;
 
   gl = makegline(mask); /* sets up nick,user,host,node and flags */
 
@@ -33,29 +32,6 @@ gline *glinebufadd(glinebuf *gbuf, const char *mask, const char *creator, const
     return 0;
   }
 
-  if (gbuf->merge) {
-    /* Check if an existing gline supercedes this mask */
-    for (sgl = gbuf->glines; sgl; sgl = sgl->next) {
-      if (gline_match_mask(sgl, gl)) {
-        freegline(gl);
-        return sgl;
-      }
-    }
-
-    /* Remove older glines this gline matches */
-    for (pnext = &gbuf->glines; *pnext; pnext = &((*pnext)->next)) {
-      sgl = *pnext;
-
-      if (gline_match_mask(gl, sgl)) {
-        *pnext = sgl->next;
-        freegline(sgl);
-        
-        if (!*pnext)
-          break;
-      }
-    }
-  }
-
   gl->creator = getsstring(creator, 255);
 
   /* it's not unreasonable to assume gline is active, if we're adding a deactivated gline, we can remove this later */
@@ -226,15 +202,15 @@ int glinebufchecksane(glinebuf *gbuf, nick *spewto, int overridesanity, int over
 
 void glinebufspew(glinebuf *gbuf, nick *spewto) {
   gline *gl;
-  time_t ref;
   int i;
+  char timebuf[30];
   
-  ref = (gbuf->flush) ? gbuf->flush : getnettime();
-
-  controlreply(spewto, "Mask                                     Duration             Creator              Reason");
+  controlreply(spewto, "Mask                                     Duration             Last modified        Creator              Reason");
   
-  for (gl = gbuf->glines; gl; gl = gl->next)
-    controlreply(spewto, "%-40s %-20s %-20s %s", glinetostring(gl), longtoduration(gl->expire - ref, 0), gl->creator->content, gl->reason->content);
+  for (gl = gbuf->glines; gl; gl = gl->next) {
+    strftime(timebuf, sizeof(timebuf), "%d/%m/%y %H:%M:%S", localtime(&gl->lastmod));
+    controlreply(spewto, "%-40s %-20s %-20s %-20s %s", glinetostring(gl), longtoduration(gl->expire - gl->lastmod, 0), timebuf, gl->creator->content, gl->reason->content);
+  }
 
   controlreply(spewto, "Hit");
 
@@ -242,6 +218,35 @@ void glinebufspew(glinebuf *gbuf, nick *spewto) {
     controlreply(spewto, "%s", ((sstring **)gbuf->hits.content)[i]->content);
 }
 
+void glinebufmerge(glinebuf *gbuf) {
+  /* TODO: reimplement */
+
+/*
+  if (gbuf->merge) {
+    /-* Check if an existing gline supercedes this mask *-/
+    for (sgl = gbuf->glines; sgl; sgl = sgl->next) {
+      if (gline_match_mask(sgl, gl)) {
+        freegline(gl);
+        return sgl;
+      }
+    }
+
+    /-* Remove older glines this gline matches *-/
+    for (pnext = &gbuf->glines; *pnext; pnext = &((*pnext)->next)) {
+      sgl = *pnext;
+
+      if (gline_match_mask(gl, sgl)) {
+        *pnext = sgl->next;
+        freegline(sgl);
+
+        if (!*pnext)
+          break;
+      }
+    }
+  }
+*/
+}
+
 void glinebufcommit(glinebuf *gbuf, int propagate) {
   gline *gl, *next, *sgl;
   glinebuf *gbl;
@@ -256,20 +261,45 @@ void glinebufcommit(glinebuf *gbuf, int propagate) {
     return;
   }
 
-  time(&gbuf->flush);
+  time(&gbuf->commit);
 
   if (propagate) {
-    /* Make a copy of the glinebuf for the log */
-    gbl = malloc(sizeof(glinebuf));
-    gbl->id = nextglinebufid++;
-    gbl->comment = (gbuf->comment) ? getsstring(gbuf->comment->content, 512) : NULL;
-    gbl->flush = gbuf->flush;
-    gbl->merge = gbuf->merge;
-    gbl->glines = NULL; /* going to set this later */
-    gbl->userhits = gbuf->userhits;
-    gbl->channelhits = gbuf->channelhits;
-
-    array_init(&gbl->hits, sizeof(sstring *));
+    gbl = NULL;
+
+    /* Find an existing log buffer */
+    if (gbuf->id != 0) {
+      for (i = 0; i < MAXGLINELOG; i++) {
+        if (!glinebuflog[i])
+          continue;
+
+        if (glinebuflog[i]->id == gbuf->id) {
+          gbl = glinebuflog[i];
+          gbl->ammend = gbuf->commit;
+
+          /* We're going to re-insert this glinebuf, so remove it for now */
+          glinebuflog[i] = NULL;
+
+          break;
+        }
+      }
+    }
+
+    /* Make a new glinebuf for the log */
+    if (gbuf->id == 0 || !gbl) {
+      gbl = malloc(sizeof(glinebuf));
+      gbl->id = (gbuf->id == 0) ? nextglinebufid++ : gbuf->id;
+      gbl->comment = (gbuf->comment) ? getsstring(gbuf->comment->content, 512) : NULL;
+      gbl->glines = NULL; /* going to set this later */
+      gbl->userhits = 0;
+      gbl->channelhits = 0;
+      gbl->commit = gbuf->commit;
+      gbl->ammend = 0;
+
+      array_init(&gbl->hits, sizeof(sstring *));
+    }
+
+    gbl->userhits += gbuf->userhits;
+    gbl->channelhits += gbuf->channelhits;
 
     for (i = 0; i < gbuf->hits.cursi; i++) {
       slot = array_getfreeslot(&gbl->hits);
@@ -318,7 +348,7 @@ void glinebufcommit(glinebuf *gbuf, int propagate) {
     }
   }
 
-  /* We've moved all glines to the gline list. Clear glines link in the glinebuf. */  
+  /* We've moved all glines to the global gline list. Clear glines link in the glinebuf. */  
   gbuf->glines = NULL;
 
   if (propagate && gbl->glines) {
index 8f0ac16faf3d3d5dc9d5bf2f5f8859d0a50c014b..d541ed08a8cb1031fc42d667e4599b1d2326125c 100644 (file)
@@ -90,7 +90,7 @@ static int glines_cmdblock(void *source, int cargc, char **cargv) {
   else
     strncpy(creator, controlid(sender), sizeof(creator));
 
-  glinebufinit(&gbuf, 1);
+  glinebufinit(&gbuf, 0);
   glinebufcommentv(&gbuf, "BLOCK", cargc + coff - 1, cargv);
   glinebufaddbynick(&gbuf, target, 0, creator, reason, getnettime() + duration, getnettime(), getnettime() + duration);
 
@@ -167,7 +167,7 @@ static int glines_cmdgline(void *source, int cargc, char **cargv) {
   else
     strncpy(creator, controlid(sender), sizeof(creator));
 
-  glinebufinit(&gbuf, 1);
+  glinebufinit(&gbuf, 0);
   glinebufcommentv(&gbuf, "GLINE", cargc + coff - 1, cargv);
 
   if (!glinebufadd(&gbuf, mask, creator, reason, getnettime() + duration, getnettime(), getnettime() + duration)) {
@@ -270,7 +270,7 @@ static int glines_cmdsmartgline(void *source, int cargc, char **cargv) {
   else
     strncpy(creator, controlid(sender), sizeof(creator));
 
-  glinebufinit(&gbuf, 1);
+  glinebufinit(&gbuf, 0);
   glinebufcommentv(&gbuf, "SMARTGLINE", cargc + coff - 1, cargv);
   glinebufaddbyip(&gbuf, user, &ip, 128, 0, creator, reason, getnettime() + duration, getnettime(), getnettime() + duration);
 
@@ -434,7 +434,7 @@ static int glines_cmdclearchan(void *source, int cargc, char **cargv) {
   else
     strncpy(creator, controlid(sender), sizeof(creator));
 
-  glinebufinit(&gbuf, 1);
+  glinebufinit(&gbuf, 0);
   glinebufcommentv(&gbuf, "CLEARCHAN", cargc + coff - 1, cargv);
 
   for (i = 0; i < victims.cursi; i++) {
@@ -479,6 +479,7 @@ static int glines_cmdclearchan(void *source, int cargc, char **cargv) {
     return CMD_ERROR;
   }
 
+  glinebufmerge(&gbuf);
   glinebufcounthits(&gbuf, &hits, NULL, NULL);
   glinebufcommit(&gbuf, 1);
 
@@ -853,7 +854,8 @@ static int glines_cmdglinelog(void *source, int cargc, char **cargv) {
       for (gl = gbl->glines; gl; gl = gl->next)
        count++;
 
-      strftime(timebuf, sizeof(timebuf), "%d/%m/%y %H:%M:%S", localtime(&gbl->flush));
+      strftime(timebuf, sizeof(timebuf), "%d/%m/%y %H:%M:%S", localtime((gbl->ammend) ? &gbl->ammend : &gbl->commit));
+      strncat(timebuf, (gbl->ammend) ? "*" : " ", sizeof(timebuf));
       controlreply(sender, "%-20s %-10d %-10d %-15d %-15d %s", timebuf, gbl->id, count, gbl->userhits, gbl->channelhits, gbl->comment ? gbl->comment->content : "no comment");
     }
 
index 5f3448568f31bb3ed9b5ed48c9f2595ecc4ba841..70d1f0ded6410acb067bd81a068376caefc5fc11 100644 (file)
@@ -6,7 +6,7 @@ int glinebyip(const char *user, struct irc_in_addr *ip, unsigned char bits, int
   glinebuf gbuf;
   int hits;
 
-  glinebufinit(&gbuf, 1);
+  glinebufinit(&gbuf, 0);
   glinebufcommentf(&gbuf, "on IP mask %s@%s, set by %s", user, trusts_cidr2str(ip, bits), creator);
   glinebufaddbyip(&gbuf, user, ip, bits, flags, creator, reason, getnettime() + duration, getnettime(), getnettime() + duration);
 
@@ -24,7 +24,7 @@ int glinebynick(nick *np, int duration, const char *reason, int flags, const cha
   glinebuf gbuf;
   int hits;
 
-  glinebufinit(&gbuf, 1);
+  glinebufinit(&gbuf, 0);
   glinebufcommentf(&gbuf, "on nick %s!%s@%s, set by %s", np->nick, np->ident, np->host->name->content, creator);
   glinebufaddbynick(&gbuf, np, flags, creator, reason, getnettime() + duration, getnettime(), getnettime() + duration);
 
index 8c301cae45a87de928c39cb21d674eb6ec3ce505..e529255175a2b8fc0be6274fc77a1109c1c31864 100644 (file)
@@ -13,6 +13,7 @@
 #include "../irc/irc.h" /* irc_send() */
 #include "../lib/irc_string.h" /* IPtostr(), longtoduration(), durationtolong() */
 #include "../lib/strlfunc.h"
+#include "../glines/glines.h"
 
 /* used for *_free functions that need to warn users of certain things
    i.e. hitting too many users in a (kill) or (gline) - declared in newsearch.c */
@@ -146,11 +147,11 @@ void *gline_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
   return (void *)1;
 }
 
-static int glineuser(nick *np, struct gline_localdata *localdata, time_t ti) {
+static int glineuser(glinebuf *gbuf, nick *np, struct gline_localdata *localdata, time_t ti) {
   char msgbuf[512];
   if (!IsOper(np) && !IsService(np) && !IsXOper(np)) {
     nssnprintf(msgbuf, sizeof(msgbuf), localdata->reason, np);
-    glinebynick(np, localdata->duration, msgbuf, 0, "newsearch");
+    glinebufaddbynick(gbuf, np, 0, "newsearch", msgbuf, getnettime() + localdata->duration, getnettime(), getnettime() + localdata->duration);
     return 1;
   }
   
@@ -161,8 +162,9 @@ void gline_free(searchCtx *ctx, struct searchNode *thenode) {
   struct gline_localdata *localdata;
   nick *np, *nnp;
   chanindex *cip, *ncip;
-  int i, j, safe=0;
+  int i, j, hits, safe=0;
   time_t ti = time(NULL);
+  glinebuf gbuf;
 
   localdata = thenode->localdata;
 
@@ -174,6 +176,8 @@ void gline_free(searchCtx *ctx, struct searchNode *thenode) {
     return;
   }
 
+  glinebufinit(&gbuf, 0);
+
   if (ctx->searchcmd == reg_chansearch) {
     for (i=0;i<CHANNELHASHSIZE;i++) {
       for (cip=chantable[i];cip;cip=ncip) {
@@ -184,7 +188,7 @@ void gline_free(searchCtx *ctx, struct searchNode *thenode) {
               continue;
     
             if ((np=getnickbynumeric(cip->channel->users->content[j]))) {
-              if(!glineuser(np, localdata, ti))
+              if(!glineuser(&gbuf, np, localdata, ti))
                 safe++;
             }
           }
@@ -197,12 +201,16 @@ void gline_free(searchCtx *ctx, struct searchNode *thenode) {
       for (np=nicktable[i];np;np=nnp) {
         nnp = np->next;
         if (np->marker == localdata->marker) {
-          if(!glineuser(np, localdata, ti))
+          if(!glineuser(&gbuf, np, localdata, ti))
             safe++;
         }
       }
     }
   }
+
+  glinebufcounthits(&gbuf, &hits, NULL, NULL);
+  glinebufcommit(&gbuf, 1);
+
   if (safe)
     ctx->reply(senderNSExtern, "Warning: your pattern matched privileged users (%d in total) - these have not been touched.", safe);
   /* notify opers of the action */
index faf14111dd4bb3d5059b1e9103896339947407b0..d72f1d514d5bcd141209c5f3dd1eaca3655cdc0e 100644 (file)
@@ -365,7 +365,7 @@ static int trusts_cmdtrustglinesuggest(void *source, int cargc, char **cargv) {
 
   snprintf(creator, sizeof(creator), "#%s", sender->authname);
 
-  glinebufinit(&gbuf, 1);
+  glinebufinit(&gbuf, 0);
   glinebufaddbyip(&gbuf, user, &ip, 128, 0, creator, "Simulate", getnettime(), getnettime(), getnettime());
   glinebufcounthits(&gbuf, &count, NULL, NULL);
   glinebufspew(&gbuf, sender);