]> jfr.im git - irc/quakenet/newserv.git/blobdiff - newsearch/newsearch.c
NEWSEARCH: check for NULL subset pointer in parseopts.
[irc/quakenet/newserv.git] / newsearch / newsearch.c
index 68785cfbe06906412366d31e546d7e3e426d48ab..100ddf94bd9dbaea7f2a874f5d9bed97571cdf31 100644 (file)
@@ -12,6 +12,7 @@
 #include "../lib/strlfunc.h"
 #include "../lib/array.h"
 #include "newsearch.h"
+#include "parser.h"
 
 MODULE_VERSION("");
 
@@ -33,6 +34,7 @@ ChanDisplayFunc defaultchanfn = printchannel;
 
 searchCmd *reg_nicksearch, *reg_chansearch, *reg_usersearch;
 void displaycommandhelp(nick *, Command *);
+void displaystrerror(replyFunc, nick *, const char *);
 
 searchCmd *registersearchcommand(char *name, int level, CommandHandler cmd, void *defaultdisplayfunc) {
   searchCmd *acmd;
@@ -53,7 +55,7 @@ searchCmd *registersearchcommand(char *name, int level, CommandHandler cmd, void
   addcommandtotree(searchCmdTree, name, 0, 0, (CommandHandler)acmd);
 
   for (sl=globalterms; sl; sl=sl->next) {
-    registersearchterm( acmd, sl->name->content, sl->cmd, 0, sl->help);
+    addcommandexttotree(acmd->searchtree, sl->name->content, 0, 1, (CommandHandler)sl->cmd, sl->help);
   }
 
   return acmd;
@@ -63,6 +65,7 @@ void deregistersearchcommand(searchCmd *scmd) {
   deregistercontrolcmd(scmd->name->content, (CommandHandler)scmd->handler);
   destroycommandtree(scmd->outputtree);
   destroycommandtree(scmd->searchtree);
+  deletecommandfromtree(searchCmdTree, scmd->name->content, (CommandHandler) scmd);
   freesstring(scmd->name);
   free(scmd);
 }
@@ -186,6 +189,8 @@ void _init() {
   registersearchterm(reg_nicksearch, "channels",channels_parse, 0, "Channel Count");     /* nick only */
   registersearchterm(reg_nicksearch, "server",server_parse, 0, "Server Name. Either (server string) or (match (server) string)");         /* nick only */
   registersearchterm(reg_nicksearch, "authid",authid_parse, 0, "User's Auth ID");         /* nick only */
+  registersearchterm(reg_nicksearch, "cidr",cidr_parse, 0, "CIDR matching");         /* nick only */
+  registersearchterm(reg_nicksearch, "ipvsix",ipv6_parse, 0, "IPv6 user");         /* nick only */
 
   /* Channel operations */
   registersearchterm(reg_chansearch, "exists",exists_parse, 0, "Returns if channel exists on network. Note: newserv may store data on empty channels");         /* channel only */
@@ -194,6 +199,8 @@ void _init() {
   registersearchterm(reg_chansearch, "name",name_parse, 0, "Channel Name");             /* channel only */
   registersearchterm(reg_chansearch, "topic",topic_parse, 0, "Channel topic");           /* channel only */
   registersearchterm(reg_chansearch, "oppct",oppct_parse, 0, "Percentage Opped");           /* channel only */
+  registersearchterm(reg_chansearch, "cumodecount",cumodecount_parse, 0, "Count of users with given channel modes");           /* channel only */
+  registersearchterm(reg_chansearch, "cumodepct",cumodepct_parse, 0, "Percentage of users with given channel modes");           /* channel only */
   registersearchterm(reg_chansearch, "uniquehostpct",hostpct_parse, 0, "uniquehost percent"); /* channel only */
   registersearchterm(reg_chansearch, "authedpct",authedpct_parse, 0, "Percentage of authed users");   /* channel only */
   registersearchterm(reg_chansearch, "kick",kick_parse, 0, "KICK users channels in newsearch result. Note: evaluation order");             /* channel only */
@@ -217,7 +224,12 @@ void _init() {
   
   /* Iterable functions */
   registersearchterm(reg_nicksearch, "channeliter",channeliter_parse, 0, "Channel Iterable function - usage: (any (channeliter x) (match (var x) #twilight*))");         /* nick only */
-  
+  registersearchterm(reg_chansearch, "nickiter",nickiter_parse, 0, "Nick Iterable function - usage: (any (nickiter x) (match (var x) nickname*))");         /* channel only */
+
+  /* Functions that work on strings?! */
+  registersearchterm(reg_nicksearch, "cumodes", cumodes_parse, 0, "usage: (cumodes (var x) <modes>)");
+  registersearchterm(reg_chansearch, "cumodes", cumodes_parse, 0, "usage: (cumodes (var x) <modes>)");
+    
   /* Notice functionality */
   registersearchterm(reg_chansearch,"notice",notice_parse, 0, "NOTICE users in newsearch result. Note: evaluation order");
   registersearchterm(reg_nicksearch,"notice",notice_parse, 0, "NOTICE users in newsearch result. Note: evaluation order");
@@ -349,7 +361,7 @@ static void controlwallwrapper(int level, char *format, ...) {
   va_end(ap);
 }
 
-int parseopts(int cargc, char **cargv, int *arg, int *limit, void **subset, void **display, CommandTree *sl, replyFunc reply, void *sender) {
+int parseopts(int cargc, char **cargv, int *arg, int *limit, void **subset, void *display, CommandTree *sl, replyFunc reply, void *sender) {
   char *ch;
   Command *cmd;
   struct irc_in_addr sin; unsigned char bits;
@@ -382,11 +394,15 @@ int parseopts(int cargc, char **cargv, int *arg, int *limit, void **subset, void
           reply(sender,"Error: Access Denied for output format %s (for help, see help <searchcmd>)", cargv[*arg]);
           return CMD_ERROR;
         }
-        *display=(void *)cmd->handler;
+        *((void **)display)=(void *)cmd->handler;
         (*arg)++;
         break;
 
       case 's':
+        if (subset == NULL) {
+          reply(sender,"Error: -s switch not supported for this search.");
+          return CMD_ERROR;
+        }
         if (cargc<*arg) {
           reply(sender,"Error: -s switch requires an argument (for help, see help <searchcmd>)");
           return CMD_ERROR;
@@ -409,7 +425,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 newsearch_ctxinit(searchCtx *ctx, searchParseFunc searchfn, replyFunc replyfn, wallFunc wallfn, void *arg, searchCmd *cmd, nick *np, void *displayfn, int limit) {
   memset(ctx, 0, sizeof(searchCtx));
   
   ctx->reply = replyfn;
@@ -418,16 +434,22 @@ void newsearch_ctxinit(searchCtx *ctx, searchParseFunc searchfn, replyFunc reply
   ctx->arg = arg;
   ctx->searchcmd = cmd;
   ctx->sender = np;
+  ctx->limit = limit;
+  ctx->displayfn = displayfn;
 }
 
 int do_nicksearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv) {
   nick *sender = source;
-  struct searchNode *search;
   int limit=500;
   int arg=0;
   NickDisplayFunc display=defaultnickfn;
-  searchCtx ctx;
   int ret;
+#ifndef NEWSEARCH_NEWPARSER
+  searchCtx ctx;
+  struct searchNode *search;
+#else
+  parsertree *tree;
+#endif
 
   if (cargc<1) {
     reply( sender, "Usage: [flags] <criteria>");
@@ -435,7 +457,7 @@ int do_nicksearch_real(replyFunc reply, wallFunc wall, void *source, int cargc,
     return CMD_OK;
   }
  
-  ret = parseopts(cargc, cargv, &arg, &limit, NULL, (void **)&display, reg_nicksearch->outputtree, reply, sender);
+  ret = parseopts(cargc, cargv, &arg, &limit, NULL, (void *)&display, reg_nicksearch->outputtree, reply, sender);
   if(ret != CMD_OK)
     return ret;
 
@@ -448,16 +470,27 @@ int do_nicksearch_real(replyFunc reply, wallFunc wall, void *source, int cargc,
     rejoinline(cargv[arg],cargc-arg);
   }
 
-  newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_nicksearch, sender);
-
+#ifndef NEWSEARCH_NEWPARSER
+  newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_nicksearch, sender, display, limit);
   if (!(search = ctx.parser(&ctx, cargv[arg]))) {
     reply(sender,"Parse error: %s",parseError);
     return CMD_ERROR;
   }
 
-  nicksearch_exe(search, &ctx, sender, display, limit);
+  nicksearch_exe(search, &ctx);
 
   (search->free)(&ctx, search);
+#else
+  tree = parse_string(reg_nicksearch, cargv[arg]);
+  if(!tree) {
+    displaystrerror(reply, sender, cargv[arg]);
+    return CMD_ERROR;
+  }
+
+  ast_nicksearch(tree->root, reply, sender, wall, display, NULL, NULL, limit);
+
+  parse_free(tree);
+#endif
 
   return CMD_OK;
 }
@@ -466,15 +499,17 @@ int do_nicksearch(void *source, int cargc, char **cargv) {
   return do_nicksearch_real(controlreply, controlwallwrapper, source, cargc, cargv);
 }
 
-void nicksearch_exe(struct searchNode *search, searchCtx *ctx, nick *sender, NickDisplayFunc display, int limit) {
+void nicksearch_exe(struct searchNode *search, searchCtx *ctx) {
   int i, j;
   int matches = 0;
   unsigned int cmarker;
   unsigned int tchans=0,uchans=0;
   struct channel **cs;
-  nick *np;
+  nick *np, *sender = ctx->sender;
   senderNSExtern = sender;
-  
+  NickDisplayFunc display = ctx->displayfn;
+  int limit = ctx->limit;
+
   /* Get a marker value to mark "seen" channels for unique count */
   cmarker=nextchanmarker();
   
@@ -512,12 +547,16 @@ void nicksearch_exe(struct searchNode *search, searchCtx *ctx, nick *sender, Nic
 
 int do_chansearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv) {
   nick *sender = source;
-  struct searchNode *search;
   int limit=500;
   int arg=0;
   ChanDisplayFunc display=defaultchanfn;
-  searchCtx ctx;
   int ret;
+#ifndef NEWSEARCH_NEWPARSER
+  struct searchNode *search;
+  searchCtx ctx;
+#else
+  parsertree *tree;
+#endif
 
   if (cargc<1) {
     reply( sender, "Usage: [flags] <criteria>");
@@ -525,7 +564,7 @@ int do_chansearch_real(replyFunc reply, wallFunc wall, void *source, int cargc,
     return CMD_OK;
   }
   
-  ret = parseopts(cargc, cargv, &arg, &limit, NULL, (void **)&display, reg_chansearch->outputtree, reply, sender);
+  ret = parseopts(cargc, cargv, &arg, &limit, NULL, (void *)&display, reg_chansearch->outputtree, reply, sender);
   if(ret != CMD_OK)
     return ret;
 
@@ -538,15 +577,27 @@ int do_chansearch_real(replyFunc reply, wallFunc wall, void *source, int cargc,
     rejoinline(cargv[arg],cargc-arg);
   }
 
-  newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_chansearch, sender);
+#ifndef NEWSEARCH_NEWPARSER
+  newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_chansearch, sender, display, limit);
   if (!(search = ctx.parser(&ctx, cargv[arg]))) {
     reply(sender,"Parse error: %s",parseError);
     return CMD_ERROR;
   }
 
-  chansearch_exe(search, &ctx, sender, display, limit);
+  chansearch_exe(search, &ctx);
 
   (search->free)(&ctx, search);
+#else
+  tree = parse_string(reg_chansearch, cargv[arg]);
+  if(!tree) {
+    displaystrerror(reply, sender, cargv[arg]);
+    return CMD_ERROR;
+  }
+
+  ast_chansearch(tree->root, reply, sender, wall, display, NULL, NULL, limit);
+
+  parse_free(tree);
+#endif
 
   return CMD_OK;
 }
@@ -555,11 +606,14 @@ int do_chansearch(void *source, int cargc, char **cargv) {
   return do_chansearch_real(controlreply, controlwallwrapper, source, cargc, cargv);
 }
 
-void chansearch_exe(struct searchNode *search, searchCtx *ctx, nick *sender, ChanDisplayFunc display, int limit) {  
+void chansearch_exe(struct searchNode *search, searchCtx *ctx) {  
   int i;
   chanindex *cip;
   int matches = 0;
+  nick *sender = ctx->sender;
   senderNSExtern = sender;
+  ChanDisplayFunc display = ctx->displayfn;
+  int limit = ctx->limit;
   
   search=coerceNode(ctx, search, RETURNTYPE_BOOL);
   
@@ -580,12 +634,16 @@ void chansearch_exe(struct searchNode *search, searchCtx *ctx, nick *sender, Cha
 
 int do_usersearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv) {
   nick *sender = source;
-  struct searchNode *search;
   int limit=500;
   int arg=0;
   UserDisplayFunc display=defaultuserfn;
-  searchCtx ctx;
   int ret;
+#ifndef NEWSEARCH_NEWPARSER
+  struct searchNode *search;
+  searchCtx ctx;
+#else
+  parsertree *tree;
+#endif
 
   if (cargc<1) {
     reply( sender, "Usage: [flags] <criteria>");
@@ -593,7 +651,7 @@ int do_usersearch_real(replyFunc reply, wallFunc wall, void *source, int cargc,
     return CMD_OK;
   }
  
-  ret = parseopts(cargc, cargv, &arg, &limit, NULL, (void **)&display, reg_usersearch->outputtree, reply, sender);
+  ret = parseopts(cargc, cargv, &arg, &limit, NULL, (void *)&display, reg_usersearch->outputtree, reply, sender);
   if(ret != CMD_OK)
     return ret;
 
@@ -606,16 +664,28 @@ int do_usersearch_real(replyFunc reply, wallFunc wall, void *source, int cargc,
     rejoinline(cargv[arg],cargc-arg);
   }
 
-  newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_usersearch, sender);
+#ifndef NEWSEARCH_NEWPARSER
+  newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_usersearch, sender, display, limit);
 
   if (!(search = ctx.parser(&ctx, cargv[arg]))) {
     reply(sender,"Parse error: %s",parseError);
     return CMD_ERROR;
   }
 
-  usersearch_exe(search, &ctx, sender, display, limit);
+  usersearch_exe(search, &ctx);
 
   (search->free)(&ctx, search);
+#else
+  tree = parse_string(reg_usersearch, cargv[arg]);
+  if(!tree) {
+    displaystrerror(reply, sender, cargv[arg]);
+    return CMD_ERROR;
+  }
+
+  ast_usersearch(tree->root, reply, sender, wall, display, NULL, NULL, limit);
+
+  parse_free(tree);
+#endif
 
   return CMD_OK;
 }
@@ -624,12 +694,15 @@ int do_usersearch(void *source, int cargc, char **cargv) {
   return do_usersearch_real(controlreply, controlwallwrapper, source, cargc, cargv);
 }
 
-void usersearch_exe(struct searchNode *search, searchCtx *ctx, nick *sender, UserDisplayFunc display, int limit) {  
+void usersearch_exe(struct searchNode *search, searchCtx *ctx) {  
   int i;
   authname *aup;
   int matches = 0;
+  nick *sender = ctx->sender;
+  int limit = ctx->limit;
+  UserDisplayFunc display = ctx->displayfn;
   senderNSExtern = sender;
-  
+
   search=coerceNode(ctx, search, RETURNTYPE_BOOL);
   
   for (i=0;i<AUTHNAMEHASHSIZE;i++) {
@@ -872,19 +945,62 @@ void literal_free(searchCtx *ctx, struct searchNode *thenode) {
   free(thenode);
 }
 
-/* search_parse:
- *  Given an input string, return a searchNode.
- */
+static int unescape(char *input, char *output, size_t buflen) {
+  char *ch, *ch2;
+  int e=0;
+  
+  if (*input=='\"') {
+    for (ch=input;*ch;ch++) {
+      if(ch - input >= buflen) {
+        parseError="Buffer overflow";
+        return 0;
+      }
+    }
+      
+    if (*(ch-1) != '\"') {
+      parseError="Quote mismatch";
+      return 0;
+    }
 
-struct searchNode *search_parse(searchCtx *ctx, char *input) {
+    *(ch-1)='\0';
+    input++;
+  }
+    
+  ch2=output;
+  for (ch=input;*ch;ch++) {
+    if(ch - input >= buflen) {
+      parseError="Buffer overflow";
+      return 0;
+    }
+    
+    if (e) {
+      e=0;
+      *ch2++=*ch;
+    } else if (*ch=='\\') {
+      e=1;
+    } else {
+      *ch2++=*ch;
+    }
+  }
+  *ch2='\0';
+  
+  return 1;
+}
+
+struct searchNode *search_parse(searchCtx *ctx, char *cinput) {
   /* OK, we need to split the input into chunks on spaces and brackets.. */
   char *argvector[100];
+  char inputb[1024];
+  char *input;
   char thestring[500];
   int i,j,q=0,e=0;
-  char *ch,*ch2;
+  char *ch;
   struct Command *cmd;
   struct searchNode *thenode;
 
+  strlcpy(inputb, cinput, sizeof(inputb));
+  input = inputb;
+  
   /* If it starts with a bracket, it's a function call.. */
   if (*input=='(') {
     /* Skip past string */
@@ -903,6 +1019,7 @@ struct searchNode *search_parse(searchCtx *ctx, char *input) {
     q=0;
     argvector[0]="";
     for (ch=input;*ch;ch++) {
+      /*printf("i: %d j: %d e: %d q: %d ch: '%c'\n", i, j, e, q, *ch);*/
       if (i==-1) {
         argvector[j]=ch;
         if (*ch=='(') {
@@ -918,8 +1035,11 @@ struct searchNode *search_parse(searchCtx *ctx, char *input) {
       } else if (e==1) {
         e=0;
       } else if (q==1) {
-        if (*ch=='\"') 
-        q=0;
+        if (*ch=='\\')  {
+          e=1;
+        } else if (*ch=='\"')  {
+          q=0;
+        }
       } else if (i==0) {
         if (*ch=='\\') {
           e=1;
@@ -955,43 +1075,28 @@ struct searchNode *search_parse(searchCtx *ctx, char *input) {
     if (*(ch-1) == 0) /* if the last character was a space */
       j--; /* remove an argument */
     
+/*    for(k=1;k<=j;k++)
+      if(!unescape(argvector[k], argvector[k], sizeof(inputb)))
+        return NULL;
+*/
+
     if (!(cmd=findcommandintree(ctx->searchcmd->searchtree,argvector[0],1))) {
       parseError = "Unknown command (for valid command list, see help <searchcmd>)";
       return NULL;
     } else {
-      if ( !controlpermitted( cmd->level, ctx->sender) ) { 
-        parseError = "Access Denied (for valid command list, see help <searchcmd>)";
+      if (!controlpermitted(cmd->level, ctx->sender)) { 
+        parseError = "Access denied (for valid command list, see help <searchcmd>)";
         return NULL;
       }
       return ((parseFunc)cmd->handler)(ctx, j, argvector+1);
     }
   } else {
     /* Literal */
-    if (*input=='\"') {
-      for (ch=input;*ch;ch++);
-      
-      if (*(ch-1) != '\"') {
-        parseError="Quote mismatch";
-        return NULL;
-      }
-
-      *(ch-1)='\0';
-      input++;
-    }
     
-    ch2=thestring;
-    for (ch=input;*ch;ch++) {
-      if (e) {
-        e=0;
-        *ch2++=*ch;
-      } else if (*ch=='\\') {
-        e=1;
-      } else {
-        *ch2++=*ch;
-      }
-    }
-    *ch2='\0';
-        
+    /* slug: disabled now we unescape during the main parse stage */
+    if(!unescape(input, thestring, sizeof(thestring)))
+      return NULL;
+
     if (!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) {
       parseError = "malloc: could not allocate memory for this search.";
       return NULL;
@@ -1143,3 +1248,48 @@ searchNode *var_get(searchCtx *ctx, char *arg) {
 void var_setstr(struct searchVariable *v, char *data) {
   v->cdata.u.stringbuf = data;
 }
+
+#ifdef NEWSEARCH_NEWPARSER
+void displaystrerror(replyFunc reply, nick *np, const char *input) {
+  char buf[515];
+
+  if((parseStrErrorPos >= 0) && (parseStrErrorPos < sizeof(buf) - 3)) {
+    int i;
+
+    for(i=0;i<parseStrErrorPos;i++)
+      buf[i] = ' ';
+
+    buf[i++] = '^';
+    buf[i] = '\0';
+
+    reply(np, "%s", input);
+    reply(np, "%s", buf);
+  }
+
+  reply(np, "Parse error: %s", parseStrError);
+}
+#endif
+
+struct searchNode *argtoconststr(char *command, searchCtx *ctx, char *arg, char **p) {
+  struct searchNode *c;
+  static char errorbuf[512];
+  
+  c = ctx->parser(ctx, arg);
+  if (!(c = coerceNode(ctx, c, RETURNTYPE_STRING))) {
+    snprintf(errorbuf, sizeof(errorbuf), "%s: unable to coerce argument to string", command);
+    parseError = errorbuf;
+    return NULL;
+  }
+  
+  if (!(c->returntype & RETURNTYPE_CONST)) {
+    snprintf(errorbuf, sizeof(errorbuf), "%s: constant argument required", command);
+    parseError = errorbuf;
+    (c->free)(ctx, c);
+    return NULL;
+  }
+  
+  if(p)
+    *p = (char *)(c->exe)(ctx, c, NULL);
+    
+  return c;
+}