]> jfr.im git - irc/quakenet/newserv.git/blame - clonehistogram/clonehistogram.c
BANS/CHANNEL: Changed IP parsing to use ircd functions. Adds IPv6 support and fixes
[irc/quakenet/newserv.git] / clonehistogram / clonehistogram.c
CommitLineData
b068ff04
CP
1#include <string.h>
2
3#include "../control/control.h"
4#include "../lib/irc_string.h"
5#include "../localuser/localuserchannel.h"
6
7int ch_clonehistogram(void *source, int cargc, char **cargv);
30bcc371 8int ch_chanhistogram(void *source, int cargc, char **cargv);
b068ff04 9
c5e90151 10#define MAX_CLONES 5
30bcc371 11#define MAX_CHANS 20
b068ff04
CP
12
13void _init() {
14 registercontrolhelpcmd("clonehistogram", NO_OPER, 1, ch_clonehistogram, "Usage: clonehistogram <hostmask>\nShows the distribution of user clone counts of a given mask.");
30bcc371 15 registercontrolhelpcmd("chanhistogram", NO_OPER, 1, ch_chanhistogram, "Usage: chanhistogram\nShows the channel histogram.");
b068ff04
CP
16}
17
18void _fini () {
19 deregistercontrolcmd("clonehistogram", ch_clonehistogram);
30bcc371 20 deregistercontrolcmd("chanhistogram", ch_chanhistogram);
b068ff04
CP
21}
22
23void histoutput(nick *np, int clonecount, int amount, int total) {
24 char max[51];
b08a991c 25 float percentage = ((float)amount / (float)total) * 100;
b068ff04
CP
26
27 if(percentage > 1)
28 memset(max, '#', (int)(percentage / 2));
29 max[(int)(percentage / 2)] = '\0';
30
b08a991c 31 controlreply(np, "%s%d %06.2f%% %s", (clonecount<1)?">":"=", abs(clonecount), percentage, max);
b068ff04
CP
32}
33
30bcc371
CP
34int ch_chanhistogram(void *source, int cargc, char **cargv) {
35 nick *np = (nick *)source;
36 nick *np2;
37 int count[MAX_CHANS + 2], j, n, total = 0;
38
39 memset(count, 0, sizeof(count));
40
41 for (j=0;j<NICKHASHSIZE;j++) {
42 for(np2=nicktable[j];np2;np2=np2->next) {
43 total++;
44 n = np2->channels->cursi;
45 if(n > MAX_CHANS) {
46 count[MAX_CHANS + 1]++;
47 } else {
48 count[n]++;
49 }
50 }
51 }
52
53
54 for(j=1;j<=MAX_CHANS;j++)
55 histoutput(np, j, count[j], total);
56
57 histoutput(np, -(MAX_CHANS + 1), count[MAX_CHANS + 1], total);
58
59 return CMD_OK;
60}
61
b068ff04
CP
62int ch_clonehistogram(void *source, int cargc, char **cargv) {
63 nick *np = (nick *)source;
b08a991c 64 int count[MAX_CLONES + 1], j, total = 0, totalusers = 0;
b068ff04
CP
65 host *hp;
66 char *pattern;
67
68 if(cargc < 1)
69 return CMD_USAGE;
70
71 pattern = cargv[0];
72
73 memset(count, 0, sizeof(count));
b068ff04
CP
74
75 for (j=0;j<HOSTHASHSIZE;j++)
76 for(hp=hosttable[j];hp;hp=hp->next)
77 if (match2strings(pattern, hp->name->content)) {
b08a991c
CP
78 total++;
79 totalusers+=hp->clonecount;
80
b068ff04
CP
81 if(hp->clonecount && (hp->clonecount > MAX_CLONES)) {
82 count[0]++;
83 } else {
b08a991c 84 count[hp->clonecount]++;
b068ff04
CP
85 }
86 }
87
88 if(total == 0) {
b08a991c 89 controlreply(np, "No hosts matched.");
b068ff04
CP
90 } else {
91 for(j=1;j<=MAX_CLONES;j++)
92 histoutput(np, j, count[j], total);
93
94 histoutput(np, -MAX_CLONES, count[0], total);
95
b08a991c 96 controlreply(np, "%d hosts/%d users matched.", total, totalusers);
b068ff04
CP
97 }
98
99 controlreply(np, "Done.");
100 return CMD_OK;
101}
102