]> jfr.im git - irc/quakenet/newserv.git/blob - newsearch/ns-modes.c
newsearch changes to support addition of trust_search/patriciasearch
[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;
21 const flag *flaglist;
22
23 if (argc!=1) {
24 parseError="modes: usage: modes (mode string)";
25 return NULL;
26 }
27
28 if (ctx->searchcmd == reg_chansearch) {
29 flaglist=cmodeflags;
30 } else if (ctx->searchcmd == reg_nicksearch) {
31 flaglist=umodeflags;
32 } else {
33 parseError="modes: unsupported search type";
34 return NULL;
35 }
36
37 if (!(localdata=(struct modes_localdata *)malloc(sizeof(struct modes_localdata)))) {
38 parseError = "malloc: could not allocate memory for this search.";
39 return NULL;
40 }
41
42 localdata->setmodes=0;
43 localdata->clearmodes = ~0;
44
45 setflags(&(localdata->setmodes), 0xFFFF, argv[0], flaglist, REJECT_NONE);
46 setflags(&(localdata->clearmodes), 0xFFFF, argv[0], flaglist, REJECT_NONE);
47
48 localdata->clearmodes = ~(localdata->clearmodes);
49
50 if (!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) {
51 /* couldn't malloc() memory for thenode, so free localdata to avoid leakage */
52 parseError = "malloc: could not allocate memory for this search.";
53 free(localdata);
54 return NULL;
55 }
56
57 thenode->returntype = RETURNTYPE_BOOL;
58 thenode->localdata = (void *)localdata;
59 thenode->exe = modes_exe;
60 thenode->free = modes_free;
61
62 return thenode;
63 }
64
65 void *modes_exe(searchCtx *ctx, struct searchNode *thenode, void *value) {
66 struct modes_localdata *localdata;
67 nick *np;
68 chanindex *cip;
69 flag_t flags;
70
71 localdata = (struct modes_localdata *)thenode->localdata;
72
73 if (ctx->searchcmd == reg_chansearch) {
74 cip=(chanindex *)value;
75 if (!cip->channel)
76 return NULL;
77 flags=cip->channel->flags;
78 } else if (ctx->searchcmd == reg_nicksearch) {
79 np=(nick *)value;
80 flags=np->umodes;
81 } else {
82 return NULL;
83 }
84
85 if (~flags & (localdata->setmodes))
86 return (void *)0;
87
88 if (flags & (localdata->clearmodes))
89 return (void *)0;
90
91 return (void *)1;
92 }
93
94 void modes_free(searchCtx *ctx, struct searchNode *thenode) {
95 free (thenode->localdata);
96 free (thenode);
97 }