]>
Commit | Line | Data |
---|---|---|
1 | #include <string.h> | |
2 | ||
3 | #include "../control/control.h" | |
4 | #include "../lib/irc_string.h" | |
5 | #include "../localuser/localuserchannel.h" | |
6 | #include "../lib/version.h" | |
7 | ||
8 | MODULE_VERSION(""); | |
9 | ||
10 | int ch_clonehistogram(void *source, int cargc, char **cargv); | |
11 | int ch_chanhistogram(void *source, int cargc, char **cargv); | |
12 | ||
13 | #define MAX_CLONES 5 | |
14 | #define MAX_CHANS 20 | |
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."); | |
18 | registercontrolhelpcmd("chanhistogram", NO_OPER, 1, ch_chanhistogram, "Usage: chanhistogram\nShows the channel histogram."); | |
19 | } | |
20 | ||
21 | void _fini () { | |
22 | deregistercontrolcmd("clonehistogram", ch_clonehistogram); | |
23 | deregistercontrolcmd("chanhistogram", ch_chanhistogram); | |
24 | } | |
25 | ||
26 | void histoutput(nick *np, int clonecount, int amount, int total) { | |
27 | char max[51]; | |
28 | float percentage = ((float)amount / (float)total) * 100; | |
29 | ||
30 | if(percentage > 1) | |
31 | memset(max, '#', (int)(percentage / 2)); | |
32 | max[(int)(percentage / 2)] = '\0'; | |
33 | ||
34 | controlreply(np, "%s%d %06.2f%% %s", (clonecount<1)?">":"=", abs(clonecount), percentage, max); | |
35 | } | |
36 | ||
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 | ||
65 | int ch_clonehistogram(void *source, int cargc, char **cargv) { | |
66 | nick *np = (nick *)source; | |
67 | int count[MAX_CLONES + 1], j, total = 0, totalusers = 0; | |
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)); | |
77 | ||
78 | for (j=0;j<HOSTHASHSIZE;j++) | |
79 | for(hp=hosttable[j];hp;hp=hp->next) | |
80 | if (match2strings(pattern, hp->name->content)) { | |
81 | total++; | |
82 | totalusers+=hp->clonecount; | |
83 | ||
84 | if(hp->clonecount && (hp->clonecount > MAX_CLONES)) { | |
85 | count[0]++; | |
86 | } else { | |
87 | count[hp->clonecount]++; | |
88 | } | |
89 | } | |
90 | ||
91 | if(total == 0) { | |
92 | controlreply(np, "No hosts matched."); | |
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 | ||
99 | controlreply(np, "%d hosts/%d users matched.", total, totalusers); | |
100 | } | |
101 | ||
102 | controlreply(np, "Done."); | |
103 | return CMD_OK; | |
104 | } | |
105 |