]> jfr.im git - irc/quakenet/newserv.git/blame - newsearch/ns-cumodes.c
merge
[irc/quakenet/newserv.git] / newsearch / ns-cumodes.c
CommitLineData
90334b4c
CP
1/*
2 * CUMODES functionality
3 */
4
5#include "newsearch.h"
6
7#include <stdio.h>
8#include <stdlib.h>
9
10struct cumodes_localdata {
11 long setmodes;
12 long clearmodes;
13 struct searchNode *xnode;
14};
15
16#define CU_OP 0x01
17#define CU_VOICE 0x02
18#define CU_ALL 0x03
19
20const flag cumodelist[] = {
21 { 'o', 1 },
22 { 'v', 2 },
23 { '\0', 0 }
24};
25
26void *cumodes_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput);
27void cumodes_free(searchCtx *ctx, struct searchNode *thenode);
28
29struct searchNode *cumodes_parse(searchCtx *ctx, int argc, char **argv) {
30 struct cumodes_localdata *localdata;
31 struct searchNode *thenode;
32 flag_t fset, fclear;
33
34 if (argc!=2) {
35 parseError="cumodes: usage: cumodes (string) (mode string)";
36 return NULL;
37 }
38
39 if (!(localdata=(struct cumodes_localdata *)malloc(sizeof(struct cumodes_localdata)))) {
40 parseError = "malloc: could not allocate memory for this search.";
41 return NULL;
42 }
43
44 localdata->xnode = ctx->parser(ctx, argv[0]);
45 if (!(localdata->xnode = coerceNode(ctx, localdata->xnode, RETURNTYPE_STRING))) {
46 free(localdata);
47 return NULL;
48 }
49
50 fset=0;
51 fclear=~0;
52
53 setflags(&(fset), CU_ALL, argv[1], cumodelist, REJECT_NONE);
54 setflags(&(fclear), CU_ALL, argv[1], cumodelist, REJECT_NONE);
55
56 localdata->setmodes=0;
57 localdata->clearmodes=~0;
58
59 if(fset & CU_OP)
60 localdata->setmodes|=CUMODE_OP;
61 if(fset & CU_VOICE)
62 localdata->setmodes|=CUMODE_VOICE;
63 if(!(fclear & CU_OP))
64 localdata->clearmodes&=~CUMODE_OP;
65 if(!(fclear & CU_VOICE))
66 localdata->clearmodes&=~CUMODE_VOICE;
67
68 localdata->clearmodes = ~(localdata->clearmodes);
69
70 if (!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) {
71 /* couldn't malloc() memory for thenode, so free localdata to avoid leakage */
72 parseError = "malloc: could not allocate memory for this search.";
73 (localdata->xnode->free)(ctx, localdata->xnode);
74 free(localdata);
75 return NULL;
76 }
77
78 thenode->returntype = RETURNTYPE_BOOL;
79 thenode->localdata = (void *)localdata;
80 thenode->exe = cumodes_exe;
81 thenode->free = cumodes_free;
82
83 return thenode;
84}
85
86void *cumodes_exe(searchCtx *ctx, struct searchNode *thenode, void *value) {
87 struct cumodes_localdata *localdata;
88 nick *np = (nick *)value;
89 channel *cp;
90 char *channame;
91 unsigned long *lp, flags;
92
93 localdata = (struct cumodes_localdata *)thenode->localdata;
94
95 /* MEGA SLOW */
96 channame = (char *)(localdata->xnode->exe) (ctx, localdata->xnode, value);
97 cp = findchannel(channame);
98 if(!cp)
99 return (void *)0;
100
101 lp = getnumerichandlefromchanhash(cp->users,np->numeric);
102 if(!lp)
103 return (void *)0;
104
105 flags = *lp;
106
107 if (~flags & (localdata->setmodes))
108 return (void *)0;
109
110 if (flags & (localdata->clearmodes))
111 return (void *)0;
112
113 return (void *)1;
114}
115
116void cumodes_free(searchCtx *ctx, struct searchNode *thenode) {
117 struct cumodes_localdata *localdata = (struct cumodes_localdata *)thenode->localdata;
118 (localdata->xnode->free)(ctx, localdata->xnode);
119 free (thenode->localdata);
120 free (thenode);
121}