]> jfr.im git - irc/quakenet/newserv.git/blame - newsearch/ns-hostpct.c
SearchCtx should contain 'type' - this is to make life easier when defining new searc...
[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
c8be5183
CP
10void *hostpct_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput);
11void hostpct_free(searchCtx *ctx, struct searchNode *thenode);
9cbe50dd 12
f33f3f52 13struct searchNode *hostpct_parse(searchCtx *ctx, int argc, char **argv) {
9cbe50dd
IB
14 struct searchNode *thenode;
15
f33f3f52 16 if (ctx->type != SEARCHTYPE_CHANNEL) {
9cbe50dd
IB
17 parseError = "uniquehostpct: this function is only valid for channel searches.";
18 return NULL;
19 }
20
9cbe50dd
IB
21 if (!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) {
22 /* couldn't malloc() memory for thenode, so free localdata to avoid leakage */
23 parseError = "malloc: could not allocate memory for this search.";
9cbe50dd
IB
24 return NULL;
25 }
26
c7f7a584 27 thenode->returntype = RETURNTYPE_INT;
28 thenode->localdata = NULL;
9cbe50dd
IB
29 thenode->exe = hostpct_exe;
30 thenode->free = hostpct_free;
31
32 return thenode;
33}
34
c8be5183 35void *hostpct_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
9cbe50dd
IB
36 int i;
37 unsigned int marker;
c7f7a584 38 unsigned int hosts=0;
9cbe50dd
IB
39 nick *np;
40 chanindex *cip = (chanindex *)theinput;
9cbe50dd 41
9cbe50dd 42 if (cip->channel==NULL)
c7f7a584 43 return (void *)0;
9cbe50dd
IB
44
45 marker=nexthostmarker();
46
9cbe50dd
IB
47 for (i=0;i<cip->channel->users->hashsize;i++) {
48 if (cip->channel->users->content[i]==nouser)
49 continue;
50
51 if (!(np=getnickbynumeric(cip->channel->users->content[i])))
52 continue;
53
54 if (np->host->marker!=marker) {
c7f7a584 55 hosts++;
9cbe50dd
IB
56 np->host->marker=marker;
57 }
58 }
59
c54295ef 60 return (void *)(long)((hosts * 100)/cip->channel->users->totalusers);
9cbe50dd
IB
61}
62
c8be5183 63void hostpct_free(searchCtx *ctx, struct searchNode *thenode) {
9cbe50dd
IB
64 free(thenode);
65}
66