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