]>
Commit | Line | Data |
---|---|---|
c86edd1d Q |
1 | /* |
2 | * NICK functionality | |
3 | */ | |
4 | ||
5 | #include "newsearch.h" | |
6 | ||
7 | #include <stdio.h> | |
8 | #include <stdlib.h> | |
9 | ||
3adfb300 IB |
10 | struct nick_localdata { |
11 | nick *np; | |
12 | int type; | |
13 | }; | |
14 | ||
c86edd1d Q |
15 | void *nick_exe(struct searchNode *thenode, int type, void *theinput); |
16 | void nick_free(struct searchNode *thenode); | |
17 | ||
18 | struct searchNode *nick_parse(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 | ||
76 | void *nick_exe(struct searchNode *thenode, int type, 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) | |
88 | return falseval(type); /* user/channel not found */ | |
89 | return trueval(type); /* found user on channel */ | |
c86edd1d | 90 | |
3adfb300 IB |
91 | case SEARCHTYPE_NICK: |
92 | np = (nick *)theinput; | |
93 | ||
94 | if (type != RETURNTYPE_STRING) | |
95 | return (void *)1; | |
96 | return np->nick; | |
97 | ||
98 | default: | |
99 | if (type != RETURNTYPE_STRING) | |
100 | return (void *)1; | |
101 | return ""; | |
102 | } | |
c86edd1d Q |
103 | } |
104 | ||
105 | void nick_free(struct searchNode *thenode) { | |
3adfb300 | 106 | free(thenode->localdata); |
c86edd1d Q |
107 | free(thenode); |
108 | } | |
109 |