]> jfr.im git - irc/quakenet/newserv.git/blobdiff - newsearch/newsearch.c
LUA: add function for channel chanop notice
[irc/quakenet/newserv.git] / newsearch / newsearch.c
index d8a9b87c1d6b46850f0a8b833f56649a96df4ca6..3d4e46f93ad8a74cd865fdd251ef0b0802259b64 100644 (file)
@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include <stdarg.h>
 #include <string.h>
+#include <assert.h>
 
 #include "../irc/irc_config.h"
 #include "../lib/irc_string.h"
@@ -195,7 +196,8 @@ void _init() {
   registersearchterm(reg_whowassearch, "ident",ident_parse, 0, "User's current ident");
   registersearchterm(reg_nicksearch, "host",host_parse, 0, "User's host, allow \"host real\" to match real host");
   registersearchterm(reg_whowassearch, "host",host_parse, 0, "User's host, allow \"host real\" to match real host");             
-  registersearchterm(reg_nicksearch, "channel",channel_parse, 0, "Valid Channel Name to match users against");       /* nick only */
+  registersearchterm(reg_nicksearch, "channel",channel_parse, 0, "Valid Channel Name to match users against");
+  registersearchterm(reg_whowassearch, "channel",channel_parse, 0, "Valid Channel Name to match users against");       
   registersearchterm(reg_nicksearch, "timestamp",timestamp_parse, 0, "User's Timestamp");
   registersearchterm(reg_whowassearch, "timestamp",timestamp_parse, 0, "User's Timestamp");
   registersearchterm(reg_nicksearch, "country",country_parse, 0, "2 letter country code (data source is geoip)");
@@ -211,12 +213,15 @@ void _init() {
   registersearchterm(reg_whowassearch, "cidr",cidr_parse, 0, "CIDR matching");
   registersearchterm(reg_nicksearch, "ipvsix",ipv6_parse, 0, "IPv6 user");
   registersearchterm(reg_whowassearch, "ipvsix",ipv6_parse, 0, "IPv6 user");
+  registersearchterm(reg_nicksearch, "message",message_parse, 0, "Last message");
+  registersearchterm(reg_nicksearch, "age",age_parse, 0, "Nick record age in seconds");
+  registersearchterm(reg_whowassearch, "age",age_parse, 0, "Whowas record age in seconds");
+
 
   /* Whowas operations */
   registersearchterm(reg_whowassearch, "quit",quit_parse, 0, "User quit");
   registersearchterm(reg_whowassearch, "killed",killed_parse, 0, "User was killed");
   registersearchterm(reg_whowassearch, "renamed",renamed_parse, 0, "User changed nick");
-  registersearchterm(reg_whowassearch, "age",age_parse, 0, "Whowas record age in seconds");
   registersearchterm(reg_whowassearch, "newnick",newnick_parse, 0, "New nick (for rename whowas records)");
   registersearchterm(reg_whowassearch, "reason",reason_parse, 0, "Quit/kill reason");
 
@@ -303,6 +308,7 @@ void _fini() {
   deregistersearchcommand( reg_nicksearch );
   deregistersearchcommand( reg_chansearch );
   deregistersearchcommand( reg_usersearch );
+  deregistersearchcommand( reg_whowassearch );
   destroycommandtree( searchCmdTree );
 }
 
@@ -322,6 +328,8 @@ void registerglobalsearchterm(char *term, parseFunc parsefunc, char *help) {
     int len=strlen(help);
     sl->help=(char *)malloc(len+1);
     if(!sl->help) {
+      freesstring(sl->name);
+      free(sl);
       Error("newsearch", ERR_ERROR, "malloc failed: registerglobalsearchterm");
       return;
     }
@@ -381,7 +389,7 @@ void deregistersearchterm(searchCmd *cmd, char *term, parseFunc parsefunc) {
   deletecommandfromtree(cmd->searchtree, term, (CommandHandler) parsefunc);
 }
 
-static void controlwallwrapper(int level, char *format, ...) __attribute__ ((format (printf, 2,         3)));
+static void controlwallwrapper(int level, char *format, ...) __attribute__ ((format (printf, 2, 3)));
 static void controlwallwrapper(int level, char *format, ...) {
   char buf[1024];
   va_list ap;
@@ -456,7 +464,7 @@ int parseopts(int cargc, char **cargv, int *arg, int *limit, void **subset, void
   return CMD_OK;
 }
 
-void newsearch_ctxinit(searchCtx *ctx, searchParseFunc searchfn, replyFunc replyfn, wallFunc wallfn, void *arg, searchCmd *cmd, nick *np, void *displayfn, int limit, void *target) {
+void newsearch_ctxinit(searchCtx *ctx, searchParseFunc searchfn, replyFunc replyfn, wallFunc wallfn, void *arg, searchCmd *cmd, nick *np, void *displayfn, int limit, array *targets) {
   memset(ctx, 0, sizeof(searchCtx));
   
   ctx->reply = replyfn;
@@ -466,7 +474,7 @@ void newsearch_ctxinit(searchCtx *ctx, searchParseFunc searchfn, replyFunc reply
   ctx->searchcmd = cmd;
   ctx->sender = np;
   ctx->limit = limit;
-  ctx->target = target;
+  ctx->targets = targets;
   ctx->displayfn = displayfn;
 }
 
@@ -515,7 +523,7 @@ int do_nicksearch(void *source, int cargc, char **cargv) {
 }
 
 void nicksearch_exe(struct searchNode *search, searchCtx *ctx) {
-  int i, j;
+  int i, j, k;
   int matches = 0;
   unsigned int cmarker;
   unsigned int tchans=0,uchans=0;
@@ -532,7 +540,13 @@ void nicksearch_exe(struct searchNode *search, searchCtx *ctx) {
   search=coerceNode(ctx, search, RETURNTYPE_BOOL);
   
   for (i=0;i<NICKHASHSIZE;i++) {
-    for (np=ctx->target ? ctx->target : nicktable[i];np;np=np->next) {
+    for (np=nicktable[i], k = 0;ctx->targets ? (k < ctx->targets->cursi) : (np != NULL);np=np->next, k++) {
+      if (ctx->targets) {
+        np = ((nick **)ctx->targets->content)[k];
+        if (!np)
+          continue;
+      }
+
       if ((search->exe)(ctx, search, np)) {
         /* Add total channels */
         tchans += np->channels->cursi;
@@ -553,13 +567,12 @@ void nicksearch_exe(struct searchNode *search, searchCtx *ctx) {
          ctx->reply(sender, "--- More than %d matches, skipping the rest",limit);
        matches++;
       }
-
-      if (ctx->target)
-        goto done;
     }
+
+    if (ctx->targets)
+      break;
   }
 
-done:
   ctx->reply(sender,"--- End of list: %d matches; users were on %u channels (%u unique, %.1f average clones)", 
                 matches, tchans, uchans, (float)tchans/uchans);
 }
@@ -616,18 +629,16 @@ void whowassearch_exe(struct searchNode *search, searchCtx *ctx) {
   WhowasDisplayFunc display = ctx->displayfn;
   int limit = ctx->limit;
 
+  assert(!ctx->targets);
+
   /* The top-level node needs to return a BOOL */
   search=coerceNode(ctx, search, RETURNTYPE_BOOL);
 
-  for (i = whowasoffset; i < whowasoffset + WW_MAXENTRIES; i++) {
-    if (ctx->target) {
-      ww = ctx->target;
-    } else {
-      ww = &whowasrecs[i % WW_MAXENTRIES];
+  for (i = whowasoffset; i < whowasoffset + whowasmax; i++) {
+    ww = &whowasrecs[i % whowasmax];
 
-      if (ww->type == WHOWAS_UNUSED)
-        continue;
-    }
+    if (ww->type == WHOWAS_UNUSED)
+      continue;
 
     /* Note: We're passing the nick to the filter function. The original
      * whowas record is in the nick's ->next field. */
@@ -639,9 +650,6 @@ void whowassearch_exe(struct searchNode *search, searchCtx *ctx) {
         ctx->reply(sender, "--- More than %d matches, skipping the rest",limit);
       matches++;
     }
-
-    if (ctx->target)
-      break;
   }
 
   ctx->reply(sender,"--- End of list: %d matches", matches);
@@ -699,25 +707,23 @@ void chansearch_exe(struct searchNode *search, searchCtx *ctx) {
   senderNSExtern = sender;
   ChanDisplayFunc display = ctx->displayfn;
   int limit = ctx->limit;
-  
+
+  assert(!ctx->targets);  
+
   search=coerceNode(ctx, search, RETURNTYPE_BOOL);
   
   for (i=0;i<CHANNELHASHSIZE;i++) {
-    for (cip=ctx->target ? ctx->target : chantable[i];cip;cip=cip->next) {
+    for (cip=chantable[i];cip;cip=cip->next) {
       if ((search->exe)(ctx, search, cip)) {
        if (matches<limit)
          display(ctx, sender, cip);
        if (matches==limit)
          ctx->reply(sender, "--- More than %d matches, skipping the rest",limit);
        matches++;
-
-        if (ctx->target)
-          goto done;
       }
     }
   }
 
-done:
   ctx->reply(sender,"--- End of list: %d matches", matches);
 }
 
@@ -774,10 +780,12 @@ void usersearch_exe(struct searchNode *search, searchCtx *ctx) {
   UserDisplayFunc display = ctx->displayfn;
   senderNSExtern = sender;
 
+  assert(!ctx->targets);
+
   search=coerceNode(ctx, search, RETURNTYPE_BOOL);
   
   for (i=0;i<AUTHNAMEHASHSIZE;i++) {
-    for (aup=ctx->target ? ctx->target : authnametable[i];aup;aup=aup->next) {
+    for (aup=authnametable[i];aup;aup=aup->next) {
       if ((search->exe)(ctx, search, aup)) {
        if (matches<limit)
          display(ctx, sender, aup);
@@ -785,13 +793,9 @@ void usersearch_exe(struct searchNode *search, searchCtx *ctx) {
          ctx->reply(sender, "--- More than %d matches, skipping the rest",limit);
        matches++;
       }
-
-      if (ctx->target)
-        goto done;
     }
   }
 
-done:
   ctx->reply(sender,"--- End of list: %d matches", matches);
 }