]> jfr.im git - irc/quakenet/newserv.git/blame - newsearch/ns-nick.c
LUA: port luadb to dbapi2 to drop postgres dependency
[irc/quakenet/newserv.git] / newsearch / ns-nick.c
CommitLineData
c86edd1d
Q
1/*
2 * NICK functionality
3 */
4
5#include "newsearch.h"
6
7#include <stdio.h>
8#include <stdlib.h>
9
3adfb300
IB
10struct nick_localdata {
11 nick *np;
3adfb300
IB
12};
13
c8be5183
CP
14void *nick_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput);
15void nick_free(searchCtx *ctx, struct searchNode *thenode);
c86edd1d 16
f33f3f52 17struct searchNode *nick_parse(searchCtx *ctx, int argc, char **argv) {
3adfb300 18 struct nick_localdata *localdata;
c86edd1d
Q
19 struct searchNode *thenode;
20
3adfb300
IB
21 if (!(localdata=(struct nick_localdata *)malloc(sizeof(struct nick_localdata)))) {
22 parseError = "malloc: could not allocate memory for this search.";
23 return NULL;
24 }
25
a92bb8e1 26 if (ctx->searchcmd == reg_chansearch) {
31686847
CP
27 struct searchNode *nickname;
28 char *p;
29
3adfb300
IB
30 if (argc!=1) {
31 parseError="nick: usage: (nick target)";
32 free(localdata);
33 return NULL;
34 }
31686847
CP
35
36 if (!(nickname=argtoconststr("nick", ctx, argv[0], &p))) {
37 free(localdata);
38 return NULL;
39 }
40
41 localdata->np=getnickbynick(p);
42 (nickname->free)(ctx, nickname);
43 if (localdata->np==NULL) {
3adfb300
IB
44 parseError="nick: unknown nickname";
45 free(localdata);
46 return NULL;
47 }
6d6b2cb9 48 } else {
3adfb300
IB
49 if (argc) {
50 parseError="nick: usage: (match (nick) target)";
51 free(localdata);
52 return NULL;
53 }
3adfb300 54 localdata->np = NULL;
c86edd1d
Q
55 }
56
3adfb300
IB
57 if (!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) {
58 /* couldn't malloc() memory for thenode, so free localdata to avoid leakage */
9ce4f0be 59 parseError = "malloc: could not allocate memory for this search.";
3adfb300 60 free(localdata);
9ce4f0be
IB
61 return NULL;
62 }
c86edd1d 63
a92bb8e1 64 if (ctx->searchcmd == reg_chansearch)
3adfb300
IB
65 thenode->returntype = RETURNTYPE_BOOL;
66 else
67 thenode->returntype = RETURNTYPE_STRING;
68 thenode->localdata = localdata;
c86edd1d
Q
69 thenode->exe = nick_exe;
70 thenode->free = nick_free;
71
72 return thenode;
73}
74
c8be5183 75void *nick_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
3adfb300
IB
76 struct nick_localdata *localdata;
77 nick *np;
78 chanindex *cip;
c86edd1d 79
3adfb300
IB
80 localdata = thenode->localdata;
81
a92bb8e1 82 if (ctx->searchcmd == reg_chansearch) {
3adfb300
IB
83 cip = (chanindex *)theinput;
84
85 if (cip->channel==NULL || getnumerichandlefromchanhash(cip->channel->users, localdata->np->numeric)==NULL)
c7f7a584 86 return (void *)0;
87
88 return (void *)1;
a92bb8e1 89 } else {
3adfb300
IB
90 np = (nick *)theinput;
91
3adfb300 92 return np->nick;
3adfb300 93 }
c86edd1d
Q
94}
95
c8be5183 96void nick_free(searchCtx *ctx, struct searchNode *thenode) {
3adfb300 97 free(thenode->localdata);
c86edd1d
Q
98 free(thenode);
99}
100