]> jfr.im git - irc/quakenet/newserv.git/blobdiff - newsearch/newsearch.c
Remove old newsearch parser.
[irc/quakenet/newserv.git] / newsearch / newsearch.c
index 39b59c2e781c2f603cd7b7bda85056c33280182a..bca5b6e2a08d82d45fe5ed691338eda6d517a31c 100644 (file)
@@ -34,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;
@@ -177,6 +178,7 @@ void _init() {
   /* Nickname operations */
   registersearchterm(reg_nicksearch, "hostmask",hostmask_parse, 0, "The user's nick!user@host; \"hostmask real\" returns nick!user@host\rreal");     /* nick only */
   registersearchterm(reg_nicksearch, "realname",realname_parse, 0, "User's current realname");     /* nick only */
+  registersearchterm(reg_nicksearch, "away",away_parse, 0, "User's current away message");       /* nick only */
   registersearchterm(reg_nicksearch, "authname",authname_parse, 0, "User's current authname or false");     /* nick only */
   registersearchterm(reg_nicksearch, "authts",authts_parse, 0, "User's Auth timestamp");         /* nick only */
   registersearchterm(reg_nicksearch, "ident",ident_parse, 0, "User's current ident");           /* nick only */
@@ -189,6 +191,7 @@ void _init() {
   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 */
@@ -197,6 +200,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 */
@@ -395,6 +400,10 @@ int parseopts(int cargc, char **cargv, int *arg, int *limit, void **subset, void
         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;
@@ -436,12 +445,7 @@ int do_nicksearch_real(replyFunc reply, wallFunc wall, void *source, int cargc,
   int arg=0;
   NickDisplayFunc display=defaultnickfn;
   int ret;
-#ifndef NEWSEARCH_NEWPARSER
-  searchCtx ctx;
-  struct searchNode *search;
-#else
   parsertree *tree;
-#endif
 
   if (cargc<1) {
     reply( sender, "Usage: [flags] <criteria>");
@@ -462,27 +466,15 @@ int do_nicksearch_real(replyFunc reply, wallFunc wall, void *source, int cargc,
     rejoinline(cargv[arg],cargc-arg);
   }
 
-#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);
-
-  (search->free)(&ctx, search);
-#else
   tree = parse_string(reg_nicksearch, cargv[arg]);
   if(!tree) {
-    reply(sender,"Parse error: %s", parseStrError);
+    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;
 }
@@ -543,12 +535,7 @@ int do_chansearch_real(replyFunc reply, wallFunc wall, void *source, int cargc,
   int arg=0;
   ChanDisplayFunc display=defaultchanfn;
   int ret;
-#ifndef NEWSEARCH_NEWPARSER
-  struct searchNode *search;
-  searchCtx ctx;
-#else
   parsertree *tree;
-#endif
 
   if (cargc<1) {
     reply( sender, "Usage: [flags] <criteria>");
@@ -569,27 +556,15 @@ int do_chansearch_real(replyFunc reply, wallFunc wall, void *source, int cargc,
     rejoinline(cargv[arg],cargc-arg);
   }
 
-#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);
-
-  (search->free)(&ctx, search);
-#else
   tree = parse_string(reg_chansearch, cargv[arg]);
   if(!tree) {
-    reply(sender,"Parse error: %s", parseStrError);
+    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;
 }
@@ -630,12 +605,7 @@ int do_usersearch_real(replyFunc reply, wallFunc wall, void *source, int cargc,
   int arg=0;
   UserDisplayFunc display=defaultuserfn;
   int ret;
-#ifndef NEWSEARCH_NEWPARSER
-  struct searchNode *search;
-  searchCtx ctx;
-#else
   parsertree *tree;
-#endif
 
   if (cargc<1) {
     reply( sender, "Usage: [flags] <criteria>");
@@ -656,28 +626,15 @@ int do_usersearch_real(replyFunc reply, wallFunc wall, void *source, int cargc,
     rejoinline(cargv[arg],cargc-arg);
   }
 
-#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);
-
-  (search->free)(&ctx, search);
-#else
   tree = parse_string(reg_usersearch, cargv[arg]);
   if(!tree) {
-    reply(sender,"Parse error: %s", parseStrError);
+    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;
 }
@@ -937,172 +894,6 @@ void literal_free(searchCtx *ctx, struct searchNode *thenode) {
   free(thenode);
 }
 
-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;
-    }
-
-    *(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;
-  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 */
-    for (ch=input;*ch;ch++);
-    if (*(ch-1) != ')') {
-      parseError = "Bracket mismatch!";
-      return NULL;
-    }
-    input++;
-    *(ch-1)='\0';
-
-    /* Split further args */
-    i=-1; /* i = -1 BoW, 0 = inword, 1 = bracket nest depth */
-    j=0;  /* j = current arg */
-    e=0;
-    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=='(') {
-          i=1;
-        } else if (*ch != ' ') {
-          i=0;
-          if (*ch=='\\') {
-            e=1;
-          } else if (*ch=='\"') {
-            q=1;
-          }
-        }
-      } else if (e==1) {
-        e=0;
-      } else if (q==1) {
-        if (*ch=='\\')  {
-          e=1;
-        } else if (*ch=='\"')  {
-          q=0;
-        }
-      } else if (i==0) {
-        if (*ch=='\\') {
-          e=1;
-        } else if (*ch=='\"') {
-          q=1;
-        } else if (*ch==' ') {
-          *ch='\0';
-          j++;
-          if(j >= (sizeof(argvector) / sizeof(*argvector))) {
-            parseError = "Too many arguments";
-            return NULL;
-          }
-          i=-1;
-        }
-      } else {
-        if (*ch=='\\') {
-          e=1;
-        } else if (*ch=='\"') {
-          q=1;
-        } else if (*ch=='(') {
-          i++;
-        } else if (*ch==')') {
-          i--;
-        }
-      }
-    }
-    
-    if (i>0) {
-      parseError = "Bracket mismatch!";
-      return NULL;
-    }
-
-    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>)";
-        return NULL;
-      }
-      return ((parseFunc)cmd->handler)(ctx, j, argvector+1);
-    }
-  } else {
-    /* Literal */
-    
-    /* 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;
-    }
-
-    thenode->localdata  = getsstring(input,512);
-    thenode->returntype = RETURNTYPE_CONST | RETURNTYPE_STRING;
-    thenode->exe        = literal_exe;
-    thenode->free       = literal_free;
-
-    return thenode;
-  }    
-}
-
 void nssnprintf(char *buf, size_t size, const char *format, nick *np) {
   StringBuf b;
   const char *p;
@@ -1240,3 +1031,46 @@ searchNode *var_get(searchCtx *ctx, char *arg) {
 void var_setstr(struct searchVariable *v, char *data) {
   v->cdata.u.stringbuf = data;
 }
+
+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);
+}
+
+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;
+}