]> jfr.im git - irc/quakenet/newserv.git/blob - newsearch/ns-modes.c
Whoops, bit too much copy paste there...
[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 int type;
12 flag_t setmodes;
13 flag_t clearmodes;
14 };
15
16 void *modes_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput);
17 void modes_free(searchCtx *ctx, struct searchNode *thenode);
18
19 struct searchNode *modes_parse(searchCtx *ctx, int type, int argc, char **argv) {
20 struct modes_localdata *localdata;
21 struct searchNode *thenode;
22 const flag *flaglist;
23
24 if (argc!=1) {
25 parseError="modes: usage: modes (mode string)";
26 return NULL;
27 }
28
29 switch (type) {
30 case SEARCHTYPE_CHANNEL:
31 flaglist=cmodeflags;
32 break;
33
34 case SEARCHTYPE_NICK:
35 flaglist=umodeflags;
36 break;
37
38 default:
39 parseError="modes: unsupported search type";
40 return NULL;
41 }
42
43 if (!(localdata=(struct modes_localdata *)malloc(sizeof(struct modes_localdata)))) {
44 parseError = "malloc: could not allocate memory for this search.";
45 return NULL;
46 }
47
48 localdata->type=type;
49 localdata->setmodes=0;
50 localdata->clearmodes = ~0;
51
52 setflags(&(localdata->setmodes), 0xFFFF, argv[0], flaglist, REJECT_NONE);
53 setflags(&(localdata->clearmodes), 0xFFFF, argv[0], flaglist, REJECT_NONE);
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 switch (localdata->type) {
81 case SEARCHTYPE_CHANNEL:
82 cip=(chanindex *)value;
83 if (!cip->channel)
84 return NULL;
85 flags=cip->channel->flags;
86 break;
87
88 case SEARCHTYPE_NICK:
89 np=(nick *)value;
90 flags=np->umodes;
91 break;
92
93 default:
94 return NULL;
95 }
96
97 if (~flags & (localdata->setmodes))
98 return (void *)0;
99
100 if (flags & (localdata->clearmodes))
101 return (void *)0;
102
103 return (void *)1;
104 }
105
106 void modes_free(searchCtx *ctx, struct searchNode *thenode) {
107 free (thenode->localdata);
108 free (thenode);
109 }