]>
Commit | Line | Data |
---|---|---|
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 | if (ctx->searchcmd == reg_chansearch) { | |
27 | struct searchNode *nickname; | |
28 | char *p; | |
29 | ||
30 | if (argc!=1) { | |
31 | parseError="nick: usage: (nick target)"; | |
32 | free(localdata); | |
33 | return NULL; | |
34 | } | |
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) { | |
44 | parseError="nick: unknown nickname"; | |
45 | free(localdata); | |
46 | return NULL; | |
47 | } | |
48 | } else if (ctx->searchcmd == reg_nicksearch) { | |
49 | if (argc) { | |
50 | parseError="nick: usage: (match (nick) target)"; | |
51 | free(localdata); | |
52 | return NULL; | |
53 | } | |
54 | localdata->np = NULL; | |
55 | } else { | |
56 | parseError="nick: invalid search command"; | |
57 | free(localdata); | |
58 | return NULL; | |
59 | } | |
60 | ||
61 | if (!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) { | |
62 | /* couldn't malloc() memory for thenode, so free localdata to avoid leakage */ | |
63 | parseError = "malloc: could not allocate memory for this search."; | |
64 | free(localdata); | |
65 | return NULL; | |
66 | } | |
67 | ||
68 | if (ctx->searchcmd == reg_chansearch) | |
69 | thenode->returntype = RETURNTYPE_BOOL; | |
70 | else | |
71 | thenode->returntype = RETURNTYPE_STRING; | |
72 | thenode->localdata = localdata; | |
73 | thenode->exe = nick_exe; | |
74 | thenode->free = nick_free; | |
75 | ||
76 | return thenode; | |
77 | } | |
78 | ||
79 | void *nick_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) { | |
80 | struct nick_localdata *localdata; | |
81 | nick *np; | |
82 | chanindex *cip; | |
83 | ||
84 | localdata = thenode->localdata; | |
85 | ||
86 | if (ctx->searchcmd == reg_chansearch) { | |
87 | cip = (chanindex *)theinput; | |
88 | ||
89 | if (cip->channel==NULL || getnumerichandlefromchanhash(cip->channel->users, localdata->np->numeric)==NULL) | |
90 | return (void *)0; | |
91 | ||
92 | return (void *)1; | |
93 | } else { | |
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 |