]> jfr.im git - irc/quakenet/newserv.git/blame - newsearch/ns-cumodecount.c
merge
[irc/quakenet/newserv.git] / newsearch / ns-cumodecount.c
CommitLineData
de8c57b4
C
1/*
2 * CUMODECOUNT functionality
3 */
4
5#include "newsearch.h"
6
7#include <stdio.h>
8#include <stdlib.h>
9
10struct cumodecount_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 cumodecountlist[] = {
21 { 'o', 1 },
22 { 'v', 2 },
23 { '\0', 0 }
24};
25
26void *cumodecount_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput);
27void cumodecount_free(searchCtx *ctx, struct searchNode *thenode);
28
29struct searchNode *cumodecount_parse(searchCtx *ctx, int argc, char **argv) {
30 struct cumodecount_localdata *localdata;
31 struct searchNode *thenode, *flagstr;
32 flag_t fset, fclear;
33 char *p;
34
35 if (argc!=1) {
36 parseError="cumodes: usage: cumodecount (mode string)";
37 return NULL;
38 }
39
40 if (!(localdata=(struct cumodecount_localdata *)malloc(sizeof(struct cumodecount_localdata)))) {
41 parseError = "malloc: could not allocate memory for this search.";
42 return NULL;
43 }
44
45 fset=0;
46 fclear=~0;
47
48 if (!(flagstr=argtoconststr("cumodecount", ctx, argv[0], &p))) {
49 localdata->xnode->free(ctx, localdata->xnode);
50 free(localdata);
51 return NULL;
52 }
53
54 setflags(&(fset), CU_ALL, p, cumodecountlist, REJECT_NONE);
55 setflags(&(fclear), CU_ALL, p, cumodecountlist, REJECT_NONE);
56 flagstr->free(ctx, flagstr);
57
58 localdata->setmodes=0;
59 localdata->clearmodes=~0;
60
61 if(fset & CU_OP)
62 localdata->setmodes|=CUMODE_OP;
63 if(fset & CU_VOICE)
64 localdata->setmodes|=CUMODE_VOICE;
65 if(!(fclear & CU_OP))
66 localdata->clearmodes&=~CUMODE_OP;
67 if(!(fclear & CU_VOICE))
68 localdata->clearmodes&=~CUMODE_VOICE;
69
70 localdata->clearmodes = ~(localdata->clearmodes);
71
72 if (!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) {
73 /* couldn't malloc() memory for thenode, so free localdata to avoid leakage */
74 parseError = "malloc: could not allocate memory for this search.";
75 free(localdata);
76 return NULL;
77 }
78
79 thenode->returntype = RETURNTYPE_INT;
80 thenode->localdata = (void *)localdata;
81 thenode->exe = cumodecount_exe;
82 thenode->free = cumodecount_free;
83
84 return thenode;
85}
86
87void *cumodecount_exe(searchCtx *ctx, struct searchNode *thenode, void *value) {
88 struct cumodecount_localdata *localdata;
89 chanindex *cip = (chanindex *)value;
90 int i, count;
91 unsigned long flags;
92
93 if(!cip->channel || !cip->channel->users)
94 return (void *)0;
95
96 localdata = (struct cumodecount_localdata *)thenode->localdata;
97 count = 0;
98
99 for (i=0;i<cip->channel->users->hashsize;i++) {
100 if (cip->channel->users->content[i]!=nouser) {
101 flags = cip->channel->users->content[i];
102
103 if (~flags & (localdata->setmodes))
104 continue;
105 else if (flags & (localdata->clearmodes))
106 continue;
107
108 count++;
109 }
110 }
111
112 return (void *)(long)count;
113}
114
115void cumodecount_free(searchCtx *ctx, struct searchNode *thenode) {
116 free (thenode->localdata);
117 free (thenode);
118}