]> jfr.im git - irc/quakenet/newserv.git/blame - newsearch/ns-nick.c
Fix various warnings.
[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;
12 int type;
13};
14
c8be5183
CP
15void *nick_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput);
16void nick_free(searchCtx *ctx, struct searchNode *thenode);
c86edd1d 17
c8be5183 18struct searchNode *nick_parse(searchCtx *ctx, int type, int argc, char **argv) {
3adfb300 19 struct nick_localdata *localdata;
c86edd1d
Q
20 struct searchNode *thenode;
21
3adfb300
IB
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);
c86edd1d
Q
55 return NULL;
56 }
57
3adfb300
IB
58 if (!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) {
59 /* couldn't malloc() memory for thenode, so free localdata to avoid leakage */
9ce4f0be 60 parseError = "malloc: could not allocate memory for this search.";
3adfb300 61 free(localdata);
9ce4f0be
IB
62 return NULL;
63 }
c86edd1d 64
3adfb300
IB
65 if (type == SEARCHTYPE_CHANNEL)
66 thenode->returntype = RETURNTYPE_BOOL;
67 else
68 thenode->returntype = RETURNTYPE_STRING;
69 thenode->localdata = localdata;
c86edd1d
Q
70 thenode->exe = nick_exe;
71 thenode->free = nick_free;
72
73 return thenode;
74}
75
c8be5183 76void *nick_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
3adfb300
IB
77 struct nick_localdata *localdata;
78 nick *np;
79 chanindex *cip;
c86edd1d 80
3adfb300
IB
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)
c7f7a584 88 return (void *)0;
89
90 return (void *)1;
91
92 default:
3adfb300
IB
93 case SEARCHTYPE_NICK:
94 np = (nick *)theinput;
95
3adfb300 96 return np->nick;
3adfb300 97 }
c86edd1d
Q
98}
99
c8be5183 100void nick_free(searchCtx *ctx, struct searchNode *thenode) {
3adfb300 101 free(thenode->localdata);
c86edd1d
Q
102 free(thenode);
103}
104