]> jfr.im git - irc/quakenet/newserv.git/blame - newsearch/ns-hostpct.c
Added (+ enabled) uniquehostpct functionality in newsearch's chansearch
[irc/quakenet/newserv.git] / newsearch / ns-hostpct.c
CommitLineData
9cbe50dd
IB
1/*
2 * hostpct functionality
3 */
4
5#include "newsearch.h"
6
7#include <stdio.h>
8#include <stdlib.h>
9
10struct hostpct_localdata {
11 long pct;
12};
13
14void *hostpct_exe(struct searchNode *thenode, int type, void *theinput);
15void hostpct_free(struct searchNode *thenode);
16
17struct searchNode *hostpct_parse(int type, int argc, char **argv) {
18 struct hostpct_localdata *localdata;
19 struct searchNode *thenode;
20
21 if (type != SEARCHTYPE_CHANNEL) {
22 parseError = "uniquehostpct: this function is only valid for channel searches.";
23 return NULL;
24 }
25
26 if (argc!=1) {
27 parseError="uniquehostpct: usage: (uniquehostpct number)";
28 return NULL;
29 }
30
31 if (!(localdata=(struct hostpct_localdata *)malloc(sizeof(struct hostpct_localdata)))) {
32 parseError = "malloc: could not allocate memory for this search.";
33 return NULL;
34 }
35
36 localdata->pct = strtoul(argv[0],NULL,10);
37
38 if (!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) {
39 /* couldn't malloc() memory for thenode, so free localdata to avoid leakage */
40 parseError = "malloc: could not allocate memory for this search.";
41 free(localdata);
42 return NULL;
43 }
44
45 thenode->returntype = RETURNTYPE_BOOL;
46 thenode->localdata = localdata;
47 thenode->exe = hostpct_exe;
48 thenode->free = hostpct_free;
49
50 return thenode;
51}
52
53void *hostpct_exe(struct searchNode *thenode, int type, void *theinput) {
54 int hostreq;
55 int i;
56 unsigned int marker;
57 nick *np;
58 chanindex *cip = (chanindex *)theinput;
59 struct hostpct_localdata *localdata;
60
61 localdata = thenode->localdata;
62
63 if (cip->channel==NULL)
64 return falseval(type);
65
66 marker=nexthostmarker();
67
68 hostreq=(cip->channel->users->totalusers * localdata->pct) / 100;
69
70 for (i=0;i<cip->channel->users->hashsize;i++) {
71 if (cip->channel->users->content[i]==nouser)
72 continue;
73
74 if (!(np=getnickbynumeric(cip->channel->users->content[i])))
75 continue;
76
77 if (np->host->marker!=marker) {
78 /* new unique host */
79 if (--hostreq <= 0) {
80 return trueval(type);
81 }
82 np->host->marker=marker;
83 }
84 }
85
86 return falseval(type);
87}
88
89void hostpct_free(struct searchNode *thenode) {
90 free(thenode->localdata);
91 free(thenode);
92}
93