]> jfr.im git - irc/quakenet/newserv.git/blobdiff - chanstats/chanstats.c
merge
[irc/quakenet/newserv.git] / chanstats / chanstats.c
index d9c1fba698a03c0468d44ea5bacb15e24e87a6af..6f072878e68a856ec8c1778c4949b3a1d6ce3b08 100644 (file)
@@ -13,6 +13,7 @@
 
 #include <stdio.h>
 #include <string.h>
+#include <stdint.h>
 
 MODULE_VERSION("");
 
@@ -32,6 +33,7 @@ void updatechanstats(chanindex *cip, time_t now);
 void rotatechanstats();
 void savechanstats();
 void loadchanstats();
+int dochanstatssave(void *source, int argc, char **argv);
 int dochanstats(void *source, int argc, char **argv);
 int doexpirecheck(void *source, int cargc, char **cargv);
 int douserhistogram(void *source, int cargc, char **cargv);
@@ -41,6 +43,8 @@ void dohist_today(int *data, float *bounds, int cats);
 void dohist_days(int *data, float *bounds, int cats, int days);
 void dohist_namelen(int *data, float *bounds, int cats);
 
+#define EXPIREMIN 4
+
 void _init() {
   time_t now,when;
 
@@ -63,7 +67,7 @@ void _init() {
     /* Work out when to take the next sample */
     now=getnettime();
     if (now < chanstats_lastsample) {
-      Error("chanstats",ERR_WARNING,"Last sample time in future (%d > %d)",chanstats_lastsample,now);
+      Error("chanstats",ERR_WARNING,"Last sample time in future (%jd > %jd)",(intmax_t)chanstats_lastsample,(intmax_t)now);
       when=now;
     } else if (now<(chanstats_lastsample+SAMPLEINTERVAL)) {
       lastday=chanstats_lastsample/(24*3600);
@@ -76,10 +80,11 @@ void _init() {
     }
     
     lastsave=now;
-    registercontrolcmd("chanstats",10,1,&dochanstats);
-    registercontrolcmd("channelhistogram",10,13,&dochanhistogram);
-    registercontrolcmd("userhistogram",10,1,&douserhistogram);
-    registercontrolcmd("expirecheck",10,1,&doexpirecheck);
+    registercontrolhelpcmd("chanstats",NO_OPER,1,&dochanstats, "Show usage statistics for a channel");
+    registercontrolhelpcmd("channelhistogram",NO_OPER,13,&dochanhistogram, "Display a histogram of network channel sizes");
+    registercontrolhelpcmd("userhistogram",NO_OPER,1,&douserhistogram, "Display a user histogram of channel size");
+    registercontrolhelpcmd("expirecheck",NO_DEVELOPER,1,&doexpirecheck, "Check if channel has too few users for services");
+    registercontrolhelpcmd("chanstatssave",NO_DEVELOPER,1, &dochanstatssave, "Usage: chanstatssave\nForce a save of chanstats data");
     schedulerecurring(when,0,SAMPLEINTERVAL,&doupdate,NULL);  
   }
 }
@@ -92,6 +97,7 @@ void _fini() {
     deregistercontrolcmd("channelhistogram",&dochanhistogram);
     deregistercontrolcmd("userhistogram",&douserhistogram);
     deregistercontrolcmd("expirecheck",&doexpirecheck);
+    deregistercontrolcmd("chanstatssave",&dochanstatssave);
     releasechanext(csext);
     cstsfreeall();
   }
@@ -154,7 +160,25 @@ void updatechanstats(chanindex *cip, time_t now) {
   }
 
   if (cip->channel!=NULL) {
-    currentusers=countuniquehosts(cip->channel);
+    channel *cp = cip->channel;
+    int i;
+    nick *np;
+
+    currentusers=countuniquehosts(cp);
+
+    for (i=0;i<cp->users->hashsize;i++) {
+      if (cp->users->content[i]==nouser)
+        continue;
+
+      if ((np=getnickbynumeric(cp->users->content[i]))==NULL) {
+        Error("channel",ERR_ERROR,"Found unknown numeric %lu on channel %s",cp->users->content[i],cp->index->name->content);
+        continue;
+      }
+
+      if (IsXOper(np) || IsService(np))
+        currentusers--;
+    }
+
     csp->todaysamples++;
   } else {
     currentusers=0;
@@ -226,7 +250,7 @@ void savechanstats() {
   chanindex *cip;
   chanstats *chp;
   
-  if ((fp=fopen("chanstats","w"))==NULL) {
+  if ((fp=fopen("data/chanstats","w"))==NULL) {
     return;
   }
   
@@ -265,7 +289,7 @@ void loadchanstats() {
   chanstats *chp;
   chanindex *cip;
   
-  if ((fp=fopen("chanstats","r"))==NULL) {
+  if ((fp=fopen("data/chanstats","r"))==NULL) {
     Error("chanstats",ERR_ERROR,"Unable to load channel stats file");
     return;
   }
@@ -403,8 +427,6 @@ int dochanstats(void *source, int cargc, char **cargv) {
   return CMD_OK;
 }
 
-#define EXPIREMIN 4
-
 int doexpirecheck(void *source, int cargc, char **cargv) {
   nick *sender=(nick *)source;
   chanindex *cip;
@@ -432,12 +454,14 @@ int doexpirecheck(void *source, int cargc, char **cargv) {
  
   /* Did they hit the minimum today? */
   if (csp->todaymax >= EXPIREMIN) {
+    controlreply(sender,"OK %s",cargv[0]);
     return CMD_OK;
   }
   
   /* Or recently? */
   for (i=0;i<HISTORYDAYS;i++) {
     if (csp->lastmax[i] >= EXPIREMIN) {
+      controlreply(sender,"OK %s",cargv[0]);
       return CMD_OK;
     }
   }
@@ -693,3 +717,12 @@ void dohist_namelen(int *data, float *bounds, int cats) {
     }
   }
 }
+
+int dochanstatssave(void *source, int cargc, char **cargv) {
+  nick *sender=(nick *)source;
+
+  controlreply(sender,"Saving Chanstats..");
+  savechanstats();   
+  controlreply(sender,"Chanstats saved"); 
+  return CMD_OK;
+}