]> jfr.im git - irc/quakenet/newserv.git/blobdiff - newsearch/ns-nick.c
LUA: add function for channel chanop notice
[irc/quakenet/newserv.git] / newsearch / ns-nick.c
index d774c04f1915e3a33990ca5ae503f26b16c05e75..e247684ce9325e63fccc7c1c48f401d330169da1 100644 (file)
@@ -7,41 +7,94 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-void *nick_exe(struct searchNode *thenode, int type, void *theinput);
-void nick_free(struct searchNode *thenode);
+struct nick_localdata {
+  nick *np;
+};
 
-struct searchNode *nick_parse(int type, int argc, char **argv) {
+void *nick_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput);
+void nick_free(searchCtx *ctx, struct searchNode *thenode);
+
+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;
+  }
 
-  if (!(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);
 }