]> jfr.im git - irc/quakenet/newserv.git/blob - newsearch/ns-modes.c
Merged.
[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 #include "../channel/channel.h"
11 #include "../lib/flags.h"
12
13 struct modes_localdata {
14 int type;
15 flag_t setmodes;
16 flag_t clearmodes;
17 };
18
19 void *modes_exe(struct searchNode *thenode, int type, void *theinput);
20 void modes_free(struct searchNode *thenode);
21
22 struct searchNode *modes_parse(int type, int argc, char **argv) {
23 struct modes_localdata *localdata;
24 struct searchNode *thenode;
25 const flag *flaglist;
26
27 if (argc!=1) {
28 parseError="modes: usage: modes (mode string)";
29 return NULL;
30 }
31
32 switch (type) {
33 case SEARCHTYPE_CHANNEL:
34 flaglist=cmodeflags;
35 break;
36
37 case SEARCHTYPE_NICK:
38 flaglist=umodeflags;
39 break;
40
41 default:
42 parseError="modes: unsupported search type";
43 return NULL;
44 }
45
46 if (!(localdata=(struct modes_localdata *)malloc(sizeof(struct modes_localdata)))) {
47 parseError = "malloc: could not allocate memory for this search.";
48 return NULL;
49 }
50
51 localdata->type=type;
52 localdata->setmodes=0;
53 localdata->clearmodes = ~0;
54
55 setflags(&(localdata->setmodes), 0xFFFF, argv[0], flaglist, REJECT_NONE);
56 setflags(&(localdata->clearmodes), 0xFFFF, argv[0], flaglist, REJECT_NONE);
57
58 localdata->clearmodes = ~(localdata->clearmodes);
59
60 if (!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) {
61 /* couldn't malloc() memory for thenode, so free localdata to avoid leakage */
62 parseError = "malloc: could not allocate memory for this search.";
63 free(localdata);
64 return NULL;
65 }
66
67 thenode->returntype = RETURNTYPE_BOOL;
68 thenode->localdata = (void *)localdata;
69 thenode->exe = modes_exe;
70 thenode->free = modes_free;
71
72 return thenode;
73 }
74
75 void *modes_exe(struct searchNode *thenode, int type, void *value) {
76 struct modes_localdata *localdata;
77 nick *np;
78 chanindex *cip;
79 flag_t flags;
80
81 localdata = (struct modes_localdata *)thenode->localdata;
82
83 switch (localdata->type) {
84 case SEARCHTYPE_CHANNEL:
85 cip=(chanindex *)value;
86 if (!cip->channel)
87 return NULL;
88 flags=cip->channel->flags;
89 break;
90
91 case SEARCHTYPE_NICK:
92 np=(nick *)value;
93 flags=np->umodes;
94 break;
95
96 default:
97 return NULL;
98 }
99
100 if (~flags & (localdata->setmodes))
101 return falseval(type);
102
103 if (flags & (localdata->clearmodes))
104 return falseval(type);
105
106 return trueval(type);
107 }
108
109 void modes_free(struct searchNode *thenode) {
110 free (thenode->localdata);
111 free (thenode);
112 }