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