]>
Commit | Line | Data |
---|---|---|
d2c08930 | 1 | #include <stdio.h> |
b76fd8e6 | 2 | #include <string.h> |
be2823bc | 3 | #include "../control/control.h" |
2d4ba67d | 4 | #include "../lib/irc_string.h" |
b76fd8e6 | 5 | #include "../lib/strlfunc.h" |
be2823bc CP |
6 | #include "trusts.h" |
7 | ||
83bccee3 CP |
8 | static void registercommands(int, void *); |
9 | static void deregistercommands(int, void *); | |
be2823bc | 10 | |
dee7de1b | 11 | void calculatespaces(int spaces, int width, char *str, char **_prebuf, char **_postbuf) { |
b76fd8e6 CP |
12 | static char prebuf[512], postbuf[512]; |
13 | int spacelen; | |
14 | ||
15 | if(spaces + 5 >= sizeof(prebuf)) { | |
16 | prebuf[0] = prebuf[1] = '\0'; | |
17 | } else { | |
18 | memset(prebuf, ' ', spaces); | |
19 | prebuf[spaces] = '\0'; | |
20 | } | |
21 | ||
dee7de1b | 22 | spacelen = width - (strlen(str) + spaces); |
b76fd8e6 CP |
23 | if(spacelen <= 0 || spacelen + 5 >= sizeof(postbuf)) { |
24 | postbuf[0] = postbuf[1] = '\0'; | |
25 | } else { | |
26 | memset(postbuf, ' ', spacelen); | |
27 | postbuf[spacelen] = '\0'; | |
28 | } | |
29 | ||
30 | *_prebuf = prebuf; | |
31 | *_postbuf = postbuf; | |
32 | } | |
33 | ||
dee7de1b CP |
34 | static void traverseandmark(unsigned int marker, trusthost *th) { |
35 | th->marker = marker; | |
b76fd8e6 | 36 | |
dee7de1b CP |
37 | for(th=th->children;th;th=th->nextbychild) { |
38 | th->marker = marker; | |
39 | traverseandmark(marker, th); | |
40 | } | |
41 | } | |
b76fd8e6 | 42 | |
dee7de1b CP |
43 | static void marktree(array *parents, unsigned int marker, trusthost *th) { |
44 | trusthost *pth; | |
45 | ||
46 | for(pth=th->parent;pth;pth=pth->next) { | |
47 | trusthost **p2 = (trusthost **)(parents->content); | |
48 | int i; | |
49 | ||
50 | /* this eliminates common subtrees */ | |
51 | for(i=0;i<parents->cursi;i++) | |
52 | if(p2[i] == pth) | |
53 | break; | |
54 | ||
55 | if(i == parents->cursi) { | |
56 | int pos = array_getfreeslot(parents); | |
57 | ((trusthost **)(parents->content))[pos] = pth; | |
b76fd8e6 | 58 | } |
dee7de1b CP |
59 | |
60 | pth->marker = marker; | |
b76fd8e6 CP |
61 | } |
62 | ||
dee7de1b CP |
63 | /* sadly we need to recurse down */ |
64 | traverseandmark(marker, th); | |
65 | } | |
b76fd8e6 | 66 | |
dee7de1b CP |
67 | static void outputtree(nick *np, unsigned int marker, trustgroup *originalgroup, trusthost *th, int depth) { |
68 | char *cidrstr, *prespacebuf, *postspacebuf, parentbuf[512]; | |
b76fd8e6 | 69 | |
dee7de1b CP |
70 | if(th->marker != marker) |
71 | return; | |
72 | ||
73 | cidrstr = trusts_cidr2str(th->ip, th->mask); | |
74 | calculatespaces(depth + 1, 20 + 1, cidrstr, &prespacebuf, &postspacebuf); | |
75 | ||
76 | if(th->group == originalgroup) { | |
b76fd8e6 CP |
77 | prespacebuf[0] = '>'; |
78 | ||
79 | parentbuf[0] = '\0'; | |
80 | } else { | |
81 | /* show the ids of other groups */ | |
82 | ||
dee7de1b | 83 | snprintf(parentbuf, sizeof(parentbuf), "%-10d %s", th->group->id, th->group->name->content); |
b76fd8e6 CP |
84 | } |
85 | ||
dee7de1b | 86 | controlreply(np, "%s%s%s %-10d %-10d %-20s%s", prespacebuf, cidrstr, postspacebuf, th->count, th->maxusage, (th->count>0)?"(now)":((th->lastseen>0)?trusts_timetostr(th->lastseen):"(never)"), parentbuf); |
b76fd8e6 | 87 | |
dee7de1b CP |
88 | for(th=th->children;th;th=th->nextbychild) |
89 | outputtree(np, marker, originalgroup, th, depth + 1); | |
b76fd8e6 CP |
90 | } |
91 | ||
2d4ba67d CP |
92 | static int trusts_cmdtrustlist(void *source, int cargc, char **cargv) { |
93 | nick *sender = source; | |
94 | trustgroup *tg; | |
dee7de1b | 95 | trusthost *th, **p2; |
2d4ba67d | 96 | time_t t; |
dee7de1b CP |
97 | unsigned int marker; |
98 | array parents; | |
99 | int i; | |
2d4ba67d CP |
100 | |
101 | if(cargc < 1) | |
102 | return CMD_USAGE; | |
103 | ||
104 | tg = tg_strtotg(cargv[0]); | |
105 | if(!tg) { | |
106 | controlreply(sender, "Couldn't find a trustgroup with that id."); | |
107 | return CMD_ERROR; | |
108 | } | |
109 | ||
110 | t = time(NULL); | |
111 | ||
d36ca89c | 112 | /* abusing the ternary operator a bit :( */ |
4b003d19 | 113 | controlreply(sender, "Name: : %s", tg->name->content); |
2d4ba67d | 114 | controlreply(sender, "Trusted for : %d", tg->trustedfor); |
1bbe1ac3 | 115 | controlreply(sender, "Currently using : %d", tg->count); |
2d4ba67d | 116 | controlreply(sender, "Clients per user : %d (%senforcing ident)", tg->maxperident, tg->mode?"":"not "); |
4b003d19 | 117 | controlreply(sender, "Contact: : %s", tg->contact->content); |
d36ca89c | 118 | controlreply(sender, "Expires in : %s", (tg->expires>t)?longtoduration(tg->expires - t, 2):"(the past -- BUG)"); |
4b003d19 CP |
119 | controlreply(sender, "Last changed by : %s", tg->createdby->content); |
120 | controlreply(sender, "Comment: : %s", tg->comment->content); | |
2d4ba67d | 121 | controlreply(sender, "ID: : %u", tg->id); |
d36ca89c | 122 | controlreply(sender, "Last used : %s", (tg->count>0)?"(now)":((tg->lastseen>0)?trusts_timetostr(tg->lastseen):"(never)")); |
4be1aaf2 | 123 | controlreply(sender, "Max usage : %d", tg->maxusage); |
2d4ba67d CP |
124 | controlreply(sender, "Last max reset : %s", tg->lastmaxuserreset?trusts_timetostr(tg->lastmaxuserreset):"(never)"); |
125 | ||
b76fd8e6 | 126 | controlreply(sender, "Host Current Max Last seen Group ID Group name"); |
2d4ba67d | 127 | |
dee7de1b CP |
128 | marker = nextthmarker(); |
129 | array_init(&parents, sizeof(trusthost *)); | |
130 | ||
2d4ba67d | 131 | for(th=tg->hosts;th;th=th->next) |
dee7de1b CP |
132 | marktree(&parents, marker, th); |
133 | ||
134 | p2 = (trusthost **)(parents.content); | |
135 | for(i=0;i<parents.cursi;i++) | |
136 | outputtree(sender, marker, tg, p2[i], 0); | |
137 | ||
138 | array_free(&parents); | |
2d4ba67d CP |
139 | |
140 | controlreply(sender, "End of list."); | |
141 | ||
142 | return CMD_OK; | |
143 | } | |
144 | ||
a99a2041 CP |
145 | static int commandsregistered; |
146 | ||
147 | static void registercommands(int hooknum, void *arg) { | |
148 | if(commandsregistered) | |
149 | return; | |
150 | commandsregistered = 1; | |
151 | ||
a99a2041 CP |
152 | registercontrolhelpcmd("trustlist", NO_OPER, 1, trusts_cmdtrustlist, "Usage: trustlist <#id|name|id>\nShows trust data for the specified trust group."); |
153 | } | |
154 | ||
83bccee3 | 155 | static void deregistercommands(int hooknum, void *arg) { |
a99a2041 CP |
156 | if(!commandsregistered) |
157 | return; | |
158 | commandsregistered = 0; | |
159 | ||
a99a2041 | 160 | deregistercontrolcmd("trustlist", trusts_cmdtrustlist); |
be2823bc CP |
161 | } |
162 | ||
163 | void _init(void) { | |
a99a2041 | 164 | registerhook(HOOK_TRUSTS_DB_LOADED, registercommands); |
83bccee3 | 165 | registerhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands); |
be2823bc CP |
166 | |
167 | if(trustsdbloaded) | |
a99a2041 | 168 | registercommands(0, NULL); |
be2823bc CP |
169 | } |
170 | ||
171 | void _fini(void) { | |
a99a2041 | 172 | deregisterhook(HOOK_TRUSTS_DB_LOADED, registercommands); |
83bccee3 | 173 | deregisterhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands); |
be2823bc | 174 | |
83bccee3 CP |
175 | deregistercommands(0, NULL); |
176 | } |