X-Git-Url: https://jfr.im/git/irc/quakenet/newserv.git/blobdiff_plain/c86edd1d9e5994aea33cfad3164e4827e591e7e6..f28b61f26dec986fb87602b5c75ad812b8297d85:/newsearch/ns-nick.c diff --git a/newsearch/ns-nick.c b/newsearch/ns-nick.c index dcbb2b08..e247684c 100644 --- a/newsearch/ns-nick.c +++ b/newsearch/ns-nick.c @@ -7,40 +7,94 @@ #include #include -#include "../nick/nick.h" +struct nick_localdata { + nick *np; +}; -void *nick_exe(struct searchNode *thenode, int type, void *theinput); -void nick_free(struct searchNode *thenode); +void *nick_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput); +void nick_free(searchCtx *ctx, struct searchNode *thenode); -struct searchNode *nick_parse(int type, int argc, char **argv) { +struct searchNode *nick_parse(searchCtx *ctx, int argc, char **argv) { + struct nick_localdata *localdata; struct searchNode *thenode; - if (type != SEARCHTYPE_NICK) { - parseError = "nick: this function is only valid for nick searches."; + if (!(localdata=(struct nick_localdata *)malloc(sizeof(struct nick_localdata)))) { + parseError = "malloc: could not allocate memory for this search."; return NULL; } + + if (ctx->searchcmd == reg_chansearch) { + struct searchNode *nickname; + char *p; + + if (argc!=1) { + parseError="nick: usage: (nick target)"; + free(localdata); + return NULL; + } + + if (!(nickname=argtoconststr("nick", ctx, argv[0], &p))) { + free(localdata); + return NULL; + } + + localdata->np=getnickbynick(p); + (nickname->free)(ctx, nickname); + if (localdata->np==NULL) { + parseError="nick: unknown nickname"; + free(localdata); + return NULL; + } + } else { + if (argc) { + parseError="nick: usage: (match (nick) target)"; + free(localdata); + return NULL; + } + localdata->np = NULL; + } - 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_STRING; - thenode->localdata = NULL; + if (ctx->searchcmd == reg_chansearch) + thenode->returntype = RETURNTYPE_BOOL; + else + thenode->returntype = RETURNTYPE_STRING; + thenode->localdata = localdata; thenode->exe = nick_exe; thenode->free = nick_free; return thenode; } -void *nick_exe(struct searchNode *thenode, int type, void *theinput) { - nick *np = (nick *)theinput; +void *nick_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) { + struct nick_localdata *localdata; + nick *np; + chanindex *cip; + + localdata = thenode->localdata; - if (type != RETURNTYPE_STRING) { + if (ctx->searchcmd == reg_chansearch) { + cip = (chanindex *)theinput; + + if (cip->channel==NULL || getnumerichandlefromchanhash(cip->channel->users, localdata->np->numeric)==NULL) + return (void *)0; + return (void *)1; - } + } else { + np = (nick *)theinput; - return np->nick; + return np->nick; + } } -void nick_free(struct searchNode *thenode) { +void nick_free(searchCtx *ctx, struct searchNode *thenode) { + free(thenode->localdata); free(thenode); }