]>
Commit | Line | Data |
---|---|---|
b068ff04 CP |
1 | #include <string.h> |
2 | ||
3 | #include "../control/control.h" | |
4 | #include "../lib/irc_string.h" | |
5 | #include "../localuser/localuserchannel.h" | |
6 | ||
7 | int ch_clonehistogram(void *source, int cargc, char **cargv); | |
8 | ||
c5e90151 | 9 | #define MAX_CLONES 5 |
b068ff04 CP |
10 | |
11 | void _init() { | |
12 | registercontrolhelpcmd("clonehistogram", NO_OPER, 1, ch_clonehistogram, "Usage: clonehistogram <hostmask>\nShows the distribution of user clone counts of a given mask."); | |
13 | } | |
14 | ||
15 | void _fini () { | |
16 | deregistercontrolcmd("clonehistogram", ch_clonehistogram); | |
17 | } | |
18 | ||
19 | void histoutput(nick *np, int clonecount, int amount, int total) { | |
20 | char max[51]; | |
b08a991c | 21 | float percentage = ((float)amount / (float)total) * 100; |
b068ff04 CP |
22 | |
23 | if(percentage > 1) | |
24 | memset(max, '#', (int)(percentage / 2)); | |
25 | max[(int)(percentage / 2)] = '\0'; | |
26 | ||
b08a991c | 27 | controlreply(np, "%s%d %06.2f%% %s", (clonecount<1)?">":"=", abs(clonecount), percentage, max); |
b068ff04 CP |
28 | } |
29 | ||
30 | int ch_clonehistogram(void *source, int cargc, char **cargv) { | |
31 | nick *np = (nick *)source; | |
b08a991c | 32 | int count[MAX_CLONES + 1], j, total = 0, totalusers = 0; |
b068ff04 CP |
33 | host *hp; |
34 | char *pattern; | |
35 | ||
36 | if(cargc < 1) | |
37 | return CMD_USAGE; | |
38 | ||
39 | pattern = cargv[0]; | |
40 | ||
41 | memset(count, 0, sizeof(count)); | |
b068ff04 CP |
42 | |
43 | for (j=0;j<HOSTHASHSIZE;j++) | |
44 | for(hp=hosttable[j];hp;hp=hp->next) | |
45 | if (match2strings(pattern, hp->name->content)) { | |
b08a991c CP |
46 | total++; |
47 | totalusers+=hp->clonecount; | |
48 | ||
b068ff04 CP |
49 | if(hp->clonecount && (hp->clonecount > MAX_CLONES)) { |
50 | count[0]++; | |
51 | } else { | |
b08a991c | 52 | count[hp->clonecount]++; |
b068ff04 CP |
53 | } |
54 | } | |
55 | ||
56 | if(total == 0) { | |
b08a991c | 57 | controlreply(np, "No hosts matched."); |
b068ff04 CP |
58 | } else { |
59 | for(j=1;j<=MAX_CLONES;j++) | |
60 | histoutput(np, j, count[j], total); | |
61 | ||
62 | histoutput(np, -MAX_CLONES, count[0], total); | |
63 | ||
b08a991c | 64 | controlreply(np, "%d hosts/%d users matched.", total, totalusers); |
b068ff04 CP |
65 | } |
66 | ||
67 | controlreply(np, "Done."); | |
68 | return CMD_OK; | |
69 | } | |
70 |