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