]>
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 | ||
7 | int ch_clonehistogram(void *source, int cargc, char **cargv); | |
8 | int ch_chanhistogram(void *source, int cargc, char **cargv); | |
9 | ||
10 | #define MAX_CLONES 5 | |
11 | #define MAX_CHANS 20 | |
12 | ||
13 | void _init() { | |
14 | registercontrolhelpcmd("clonehistogram", NO_OPER, 1, ch_clonehistogram, "Usage: clonehistogram <hostmask>\nShows the distribution of user clone counts of a given mask."); | |
15 | registercontrolhelpcmd("chanhistogram", NO_OPER, 1, ch_chanhistogram, "Usage: chanhistogram\nShows the channel histogram."); | |
16 | } | |
17 | ||
18 | void _fini () { | |
19 | deregistercontrolcmd("clonehistogram", ch_clonehistogram); | |
20 | deregistercontrolcmd("chanhistogram", ch_chanhistogram); | |
21 | } | |
22 | ||
23 | void histoutput(nick *np, int clonecount, int amount, int total) { | |
24 | char max[51]; | |
25 | float percentage = ((float)amount / (float)total) * 100; | |
26 | ||
27 | if(percentage > 1) | |
28 | memset(max, '#', (int)(percentage / 2)); | |
29 | max[(int)(percentage / 2)] = '\0'; | |
30 | ||
31 | controlreply(np, "%s%d %06.2f%% %s", (clonecount<1)?">":"=", abs(clonecount), percentage, max); | |
32 | } | |
33 | ||
34 | int 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 | ||
62 | int ch_clonehistogram(void *source, int cargc, char **cargv) { | |
63 | nick *np = (nick *)source; | |
64 | int count[MAX_CLONES + 1], j, total = 0, totalusers = 0; | |
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)); | |
74 | ||
75 | for (j=0;j<HOSTHASHSIZE;j++) | |
76 | for(hp=hosttable[j];hp;hp=hp->next) | |
77 | if (match2strings(pattern, hp->name->content)) { | |
78 | total++; | |
79 | totalusers+=hp->clonecount; | |
80 | ||
81 | if(hp->clonecount && (hp->clonecount > MAX_CLONES)) { | |
82 | count[0]++; | |
83 | } else { | |
84 | count[hp->clonecount]++; | |
85 | } | |
86 | } | |
87 | ||
88 | if(total == 0) { | |
89 | controlreply(np, "No hosts matched."); | |
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 | ||
96 | controlreply(np, "%d hosts/%d users matched.", total, totalusers); | |
97 | } | |
98 | ||
99 | controlreply(np, "Done."); | |
100 | return CMD_OK; | |
101 | } | |
102 |