]> jfr.im git - irc/quakenet/newserv.git/blobdiff - newsearch/ns-modes.c
newsearch changes to support addition of trust_search/patriciasearch
[irc/quakenet/newserv.git] / newsearch / ns-modes.c
index f92292d9bbdc312c4439542ec0a60453b4a7f13a..48042917614fe5d97b7608bb7f76f2fcfb7ed540 100644 (file)
@@ -7,19 +7,15 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-#include "../channel/channel.h"
-#include "../lib/flags.h"
-
 struct modes_localdata {
-  int         type;
   flag_t      setmodes;
   flag_t      clearmodes;
 }; 
 
-void *modes_exe(struct searchNode *thenode, int type, void *theinput);
-void modes_free(struct searchNode *thenode);
+void *modes_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput);
+void modes_free(searchCtx *ctx, struct searchNode *thenode);
 
-struct searchNode *modes_parse(int type, int argc, char **argv) {
+struct searchNode *modes_parse(searchCtx *ctx, int argc, char **argv) {
   struct modes_localdata *localdata;
   struct searchNode *thenode;
   const flag *flaglist;
@@ -29,23 +25,20 @@ struct searchNode *modes_parse(int type, int argc, char **argv) {
     return NULL;
   }
     
-  switch (type) {
-  case SEARCHTYPE_CHANNEL:
+  if (ctx->searchcmd == reg_chansearch) {
     flaglist=cmodeflags;
-    break;
-
-  case SEARCHTYPE_NICK:
+  } else if (ctx->searchcmd == reg_nicksearch) {
     flaglist=umodeflags;
-    break;
-
-  default:
+  } else {
     parseError="modes: unsupported search type";
     return NULL;
   }
 
-  localdata=(struct modes_localdata *)malloc(sizeof(struct modes_localdata));
+  if (!(localdata=(struct modes_localdata *)malloc(sizeof(struct modes_localdata)))) {
+    parseError = "malloc: could not allocate memory for this search.";
+    return NULL;
+  }
   
-  localdata->type=type;
   localdata->setmodes=0;
   localdata->clearmodes = ~0;
   
@@ -54,7 +47,12 @@ struct searchNode *modes_parse(int type, int argc, char **argv) {
 
   localdata->clearmodes = ~(localdata->clearmodes);
   
-  thenode=(struct searchNode *)malloc(sizeof(struct searchNode));
+  if (!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) {
+    /* couldn't malloc() memory for thenode, so free localdata to avoid leakage */
+    parseError = "malloc: could not allocate memory for this search.";
+    free(localdata);
+    return NULL;
+  }
   
   thenode->returntype  = RETURNTYPE_BOOL;
   thenode->localdata   = (void *)localdata;
@@ -64,7 +62,7 @@ struct searchNode *modes_parse(int type, int argc, char **argv) {
   return thenode;
 }
 
-void *modes_exe(struct searchNode *thenode, int type, void *value) {
+void *modes_exe(searchCtx *ctx, struct searchNode *thenode, void *value) {
   struct modes_localdata *localdata;
   nick *np;
   chanindex *cip;
@@ -72,33 +70,28 @@ void *modes_exe(struct searchNode *thenode, int type, void *value) {
   
   localdata = (struct modes_localdata *)thenode->localdata;
 
-  switch (localdata->type) {
-  case SEARCHTYPE_CHANNEL:
+  if (ctx->searchcmd == reg_chansearch) {
     cip=(chanindex *)value;
     if (!cip->channel)
       return NULL;
     flags=cip->channel->flags;
-    break;
-
-  case SEARCHTYPE_NICK:
+  } else if (ctx->searchcmd == reg_nicksearch) {
     np=(nick *)value;
     flags=np->umodes;
-    break;
-
-  default:
+  } else {
     return NULL;
   }
 
   if (~flags & (localdata->setmodes))
-    return falseval(type);
+    return (void *)0;
 
   if (flags & (localdata->clearmodes))
-    return falseval(type);
+    return (void *)0;
   
-  return trueval(type);
+  return (void *)1;
 }
 
-void modes_free(struct searchNode *thenode) {
+void modes_free(searchCtx *ctx, struct searchNode *thenode) {
   free (thenode->localdata);
   free (thenode);
 }