]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * MODES functionality | |
3 | */ | |
4 | ||
5 | #include "newsearch.h" | |
6 | ||
7 | #include <stdio.h> | |
8 | #include <stdlib.h> | |
9 | ||
10 | struct modes_localdata { | |
11 | flag_t setmodes; | |
12 | flag_t clearmodes; | |
13 | }; | |
14 | ||
15 | void *modes_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput); | |
16 | void modes_free(searchCtx *ctx, struct searchNode *thenode); | |
17 | ||
18 | struct searchNode *modes_parse(searchCtx *ctx, int argc, char **argv) { | |
19 | struct modes_localdata *localdata; | |
20 | struct searchNode *thenode, *modestring; | |
21 | const flag *flaglist; | |
22 | char *p; | |
23 | ||
24 | if (argc!=1) { | |
25 | parseError="modes: usage: modes (mode string)"; | |
26 | return NULL; | |
27 | } | |
28 | ||
29 | if (ctx->searchcmd == reg_chansearch) { | |
30 | flaglist=cmodeflags; | |
31 | } else if (ctx->searchcmd == reg_nicksearch) { | |
32 | flaglist=umodeflags; | |
33 | } else { | |
34 | parseError="modes: unsupported search type"; | |
35 | return NULL; | |
36 | } | |
37 | ||
38 | if (!(localdata=(struct modes_localdata *)malloc(sizeof(struct modes_localdata)))) { | |
39 | parseError = "malloc: could not allocate memory for this search."; | |
40 | return NULL; | |
41 | } | |
42 | ||
43 | localdata->setmodes=0; | |
44 | localdata->clearmodes = ~0; | |
45 | ||
46 | if (!(modestring=argtoconststr("modes", ctx, argv[0], &p))) { | |
47 | free(localdata); | |
48 | return NULL; | |
49 | } | |
50 | ||
51 | setflags(&(localdata->setmodes), 0xFFFF, p, flaglist, REJECT_NONE); | |
52 | setflags(&(localdata->clearmodes), 0xFFFF, p, flaglist, REJECT_NONE); | |
53 | (modestring->free)(ctx, modestring); | |
54 | ||
55 | localdata->clearmodes = ~(localdata->clearmodes); | |
56 | ||
57 | if (!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) { | |
58 | /* couldn't malloc() memory for thenode, so free localdata to avoid leakage */ | |
59 | parseError = "malloc: could not allocate memory for this search."; | |
60 | free(localdata); | |
61 | return NULL; | |
62 | } | |
63 | ||
64 | thenode->returntype = RETURNTYPE_BOOL; | |
65 | thenode->localdata = (void *)localdata; | |
66 | thenode->exe = modes_exe; | |
67 | thenode->free = modes_free; | |
68 | ||
69 | return thenode; | |
70 | } | |
71 | ||
72 | void *modes_exe(searchCtx *ctx, struct searchNode *thenode, void *value) { | |
73 | struct modes_localdata *localdata; | |
74 | nick *np; | |
75 | chanindex *cip; | |
76 | flag_t flags; | |
77 | ||
78 | localdata = (struct modes_localdata *)thenode->localdata; | |
79 | ||
80 | if (ctx->searchcmd == reg_chansearch) { | |
81 | cip=(chanindex *)value; | |
82 | if (!cip->channel) | |
83 | return NULL; | |
84 | flags=cip->channel->flags; | |
85 | } else if (ctx->searchcmd == reg_nicksearch) { | |
86 | np=(nick *)value; | |
87 | flags=np->umodes; | |
88 | } else { | |
89 | return NULL; | |
90 | } | |
91 | ||
92 | if (~flags & (localdata->setmodes)) | |
93 | return (void *)0; | |
94 | ||
95 | if (flags & (localdata->clearmodes)) | |
96 | return (void *)0; | |
97 | ||
98 | return (void *)1; | |
99 | } | |
100 | ||
101 | void modes_free(searchCtx *ctx, struct searchNode *thenode) { | |
102 | free (thenode->localdata); | |
103 | free (thenode); | |
104 | } |