]>
Commit | Line | Data |
---|---|---|
de8c57b4 C |
1 | /* |
2 | * CUMODEPCT functionality | |
3 | */ | |
4 | ||
5 | #include "newsearch.h" | |
6 | ||
7 | #include <stdio.h> | |
8 | #include <stdlib.h> | |
9 | ||
10 | struct cumodepct_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 | ||
20 | const flag cumodepctlist[] = { | |
21 | { 'o', 1 }, | |
22 | { 'v', 2 }, | |
23 | { '\0', 0 } | |
24 | }; | |
25 | ||
26 | void *cumodepct_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput); | |
27 | void cumodepct_free(searchCtx *ctx, struct searchNode *thenode); | |
28 | ||
29 | struct searchNode *cumodepct_parse(searchCtx *ctx, int argc, char **argv) { | |
30 | struct cumodepct_localdata *localdata; | |
31 | struct searchNode *thenode, *flagstr; | |
32 | flag_t fset, fclear; | |
33 | char *p; | |
34 | ||
35 | if (argc!=1) { | |
36 | parseError="cumodes: usage: cumodepct (mode string)"; | |
37 | return NULL; | |
38 | } | |
39 | ||
40 | if (!(localdata=(struct cumodepct_localdata *)malloc(sizeof(struct cumodepct_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("cumodepct", 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, cumodepctlist, REJECT_NONE); | |
55 | setflags(&(fclear), CU_ALL, p, cumodepctlist, 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 = cumodepct_exe; | |
82 | thenode->free = cumodepct_free; | |
83 | ||
84 | return thenode; | |
85 | } | |
86 | ||
87 | void *cumodepct_exe(searchCtx *ctx, struct searchNode *thenode, void *value) { | |
88 | struct cumodepct_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 cumodepct_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 * 100) / cip->channel->users->totalusers); | |
113 | } | |
114 | ||
115 | void cumodepct_free(searchCtx *ctx, struct searchNode *thenode) { | |
116 | free (thenode->localdata); | |
117 | free (thenode); | |
118 | } |