#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;
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;
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;
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;
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);
}