]> jfr.im git - irc/quakenet/newserv.git/blob - newsearch/ns-modes.c
Merge pull request #132 from retropc/lua_country
[irc/quakenet/newserv.git] / newsearch / ns-modes.c
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 {
32 flaglist=umodeflags;
33 }
34
35 if (!(localdata=(struct modes_localdata *)malloc(sizeof(struct modes_localdata)))) {
36 parseError = "malloc: could not allocate memory for this search.";
37 return NULL;
38 }
39
40 localdata->setmodes=0;
41 localdata->clearmodes = ~0;
42
43 if (!(modestring=argtoconststr("modes", ctx, argv[0], &p))) {
44 free(localdata);
45 return NULL;
46 }
47
48 setflags(&(localdata->setmodes), 0xFFFF, p, flaglist, REJECT_NONE);
49 setflags(&(localdata->clearmodes), 0xFFFF, p, flaglist, REJECT_NONE);
50 (modestring->free)(ctx, modestring);
51
52 localdata->clearmodes = ~(localdata->clearmodes);
53
54 if (!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) {
55 /* couldn't malloc() memory for thenode, so free localdata to avoid leakage */
56 parseError = "malloc: could not allocate memory for this search.";
57 free(localdata);
58 return NULL;
59 }
60
61 thenode->returntype = RETURNTYPE_BOOL;
62 thenode->localdata = (void *)localdata;
63 thenode->exe = modes_exe;
64 thenode->free = modes_free;
65
66 return thenode;
67 }
68
69 void *modes_exe(searchCtx *ctx, struct searchNode *thenode, void *value) {
70 struct modes_localdata *localdata;
71 nick *np;
72 chanindex *cip;
73 flag_t flags;
74
75 localdata = (struct modes_localdata *)thenode->localdata;
76
77 if (ctx->searchcmd == reg_chansearch) {
78 cip=(chanindex *)value;
79 if (!cip->channel)
80 return NULL;
81 flags=cip->channel->flags;
82 } else {
83 np=(nick *)value;
84 flags=np->umodes;
85 }
86
87 if (~flags & (localdata->setmodes))
88 return (void *)0;
89
90 if (flags & (localdata->clearmodes))
91 return (void *)0;
92
93 return (void *)1;
94 }
95
96 void modes_free(searchCtx *ctx, struct searchNode *thenode) {
97 free (thenode->localdata);
98 free (thenode);
99 }