]> jfr.im git - irc/quakenet/newserv.git/blame - trusts/trusts_commands.c
chanserv: fixed cleanupdb to also clear history databases
[irc/quakenet/newserv.git] / trusts / trusts_commands.c
CommitLineData
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"
82a316e7 6#include "../core/nsmalloc.h"
be2823bc
CP
7#include "trusts.h"
8
83bccee3
CP
9static void registercommands(int, void *);
10static void deregistercommands(int, void *);
be2823bc 11
dee7de1b 12void calculatespaces(int spaces, int width, char *str, char **_prebuf, char **_postbuf) {
b76fd8e6
CP
13 static char prebuf[512], postbuf[512];
14 int spacelen;
15
16 if(spaces + 5 >= sizeof(prebuf)) {
17 prebuf[0] = prebuf[1] = '\0';
18 } else {
19 memset(prebuf, ' ', spaces);
20 prebuf[spaces] = '\0';
21 }
22
dee7de1b 23 spacelen = width - (strlen(str) + spaces);
b76fd8e6
CP
24 if(spacelen <= 0 || spacelen + 5 >= sizeof(postbuf)) {
25 postbuf[0] = postbuf[1] = '\0';
26 } else {
27 memset(postbuf, ' ', spacelen);
28 postbuf[spacelen] = '\0';
29 }
30
31 *_prebuf = prebuf;
32 *_postbuf = postbuf;
33}
34
dee7de1b
CP
35static void traverseandmark(unsigned int marker, trusthost *th) {
36 th->marker = marker;
b76fd8e6 37
dee7de1b
CP
38 for(th=th->children;th;th=th->nextbychild) {
39 th->marker = marker;
40 traverseandmark(marker, th);
41 }
42}
b76fd8e6 43
82a316e7
CP
44static void insertth(array *parents, trusthost *th) {
45 int i;
46 trusthost **p2 = (trusthost **)(parents->content);
47
48 /* this eliminates common subtrees */
49 for(i=0;i<parents->cursi;i++)
50 if(p2[i] == th)
51 break;
52
53 if(i == parents->cursi) {
54 int pos = array_getfreeslot(parents);
55 ((trusthost **)(parents->content))[pos] = th;
56 }
57}
58
dee7de1b
CP
59static void marktree(array *parents, unsigned int marker, trusthost *th) {
60 trusthost *pth;
82a316e7 61 int parentcount = 0;
dee7de1b
CP
62
63 for(pth=th->parent;pth;pth=pth->next) {
82a316e7 64 insertth(parents, pth);
dee7de1b
CP
65
66 pth->marker = marker;
b76fd8e6
CP
67 }
68
82a316e7
CP
69 if(parentcount == 0)
70 insertth(parents, th);
71
dee7de1b
CP
72 /* sadly we need to recurse down */
73 traverseandmark(marker, th);
74}
b76fd8e6 75
dee7de1b
CP
76static void outputtree(nick *np, unsigned int marker, trustgroup *originalgroup, trusthost *th, int depth) {
77 char *cidrstr, *prespacebuf, *postspacebuf, parentbuf[512];
b76fd8e6 78
dee7de1b
CP
79 if(th->marker != marker)
80 return;
81
82 cidrstr = trusts_cidr2str(th->ip, th->mask);
83 calculatespaces(depth + 1, 20 + 1, cidrstr, &prespacebuf, &postspacebuf);
84
85 if(th->group == originalgroup) {
b76fd8e6
CP
86 prespacebuf[0] = '>';
87
88 parentbuf[0] = '\0';
89 } else {
90 /* show the ids of other groups */
91
dee7de1b 92 snprintf(parentbuf, sizeof(parentbuf), "%-10d %s", th->group->id, th->group->name->content);
b76fd8e6
CP
93 }
94
82a316e7 95 controlreply(np, "%s%s%s %-10d %-10d %-21s%s", prespacebuf, cidrstr, postspacebuf, th->count, th->maxusage, (th->count>0)?"(now)":((th->lastseen>0)?trusts_timetostr(th->lastseen):"(never)"), parentbuf);
b76fd8e6 96
dee7de1b
CP
97 for(th=th->children;th;th=th->nextbychild)
98 outputtree(np, marker, originalgroup, th, depth + 1);
b76fd8e6
CP
99}
100
ee77bc7a 101static void displaygroup(nick *sender, trustgroup *tg) {
dee7de1b 102 trusthost *th, **p2;
dee7de1b
CP
103 unsigned int marker;
104 array parents;
105 int i;
ee77bc7a 106 time_t t = time(NULL);
2d4ba67d 107
d36ca89c 108 /* abusing the ternary operator a bit :( */
4b003d19 109 controlreply(sender, "Name: : %s", tg->name->content);
2d4ba67d 110 controlreply(sender, "Trusted for : %d", tg->trustedfor);
1bbe1ac3 111 controlreply(sender, "Currently using : %d", tg->count);
2d4ba67d 112 controlreply(sender, "Clients per user : %d (%senforcing ident)", tg->maxperident, tg->mode?"":"not ");
4b003d19 113 controlreply(sender, "Contact: : %s", tg->contact->content);
d36ca89c 114 controlreply(sender, "Expires in : %s", (tg->expires>t)?longtoduration(tg->expires - t, 2):"(the past -- BUG)");
4b003d19
CP
115 controlreply(sender, "Last changed by : %s", tg->createdby->content);
116 controlreply(sender, "Comment: : %s", tg->comment->content);
2d4ba67d 117 controlreply(sender, "ID: : %u", tg->id);
d36ca89c 118 controlreply(sender, "Last used : %s", (tg->count>0)?"(now)":((tg->lastseen>0)?trusts_timetostr(tg->lastseen):"(never)"));
4be1aaf2 119 controlreply(sender, "Max usage : %d", tg->maxusage);
1f685425 120 controlreply(sender, "Last max reset : %s", tg->lastmaxusereset?trusts_timetostr(tg->lastmaxusereset):"(never)");
2d4ba67d 121
82a316e7 122 controlreply(sender, "Host Current Max Last seen Group ID Group name");
2d4ba67d 123
dee7de1b
CP
124 marker = nextthmarker();
125 array_init(&parents, sizeof(trusthost *));
126
2d4ba67d 127 for(th=tg->hosts;th;th=th->next)
dee7de1b
CP
128 marktree(&parents, marker, th);
129
130 p2 = (trusthost **)(parents.content);
131 for(i=0;i<parents.cursi;i++)
132 outputtree(sender, marker, tg, p2[i], 0);
133
134 array_free(&parents);
2d4ba67d
CP
135
136 controlreply(sender, "End of list.");
ee77bc7a
CP
137}
138
139static int trusts_cmdtrustlist(void *source, int cargc, char **cargv) {
140 nick *sender = source;
141 char *name;
142 trustgroup *tg = NULL;
143 int id, found = 0, remaining = 50;
144
145 if(cargc < 1)
146 return CMD_USAGE;
147
148 name = cargv[0];
149
150 if(name[0] == '#')
151 tg = tg_strtotg(name + 1);
152
153 if((id = strtol(name, NULL, 10)))
154 tg = tg_getbyid(id);
155
156 if(tg) {
157 displaygroup(sender, tg);
158 return CMD_OK;
159 }
160
161 for(tg=tglist;tg;tg=tg->next) {
c215a421 162 if(match(name, tg->name->content))
ee77bc7a
CP
163 continue;
164
165 displaygroup(sender, tg);
166 if(--remaining == 0) {
167 controlreply(sender, "Maximum number of matches reached.");
168 return CMD_OK;
169 }
170 found = 1;
171 }
172
173 if(!found)
174 controlreply(sender, "No matches found.");
2d4ba67d
CP
175
176 return CMD_OK;
177}
178
82a316e7
CP
179static int comparetgs(const void *_a, const void *_b) {
180 const trustgroup *a = _a;
181 const trustgroup *b = _b;
182
183 if(a->id > b->id)
184 return 1;
185 if(a->id < b-> id)
186 return -1;
187 return 0;
188}
189
190static int trusts_cmdtrustdump(void *source, int argc, char **argv) {
191 trusthost *th;
192 trustgroup *tg, **atg;
193 unsigned int wanted, max, maxid, totalcount, i, groupcount, linecount;
194 nick *np = source;
195
196 if((argc < 2) || (argv[0][0] != '#'))
197 return CMD_USAGE;
198
199 wanted = atoi(&argv[0][1]);
200 max = atoi(argv[1]);
201
202 for(maxid=totalcount=0,tg=tglist;tg;tg=tg->next) {
203 if(totalcount == 0 || tg->id > maxid)
204 maxid = tg->id;
205
206 totalcount++;
207 }
208
209 if(maxid > totalcount) {
210 controlreply(np, "Start ID cannot exceed current maximum group ID (#%u)", maxid);
211 return CMD_OK;
212 }
213
214 atg = nsmalloc(POOL_TRUSTS, sizeof(trusthost *) * totalcount);
215 if(!atg) {
216 controlreply(np, "Memory error.");
217 return CMD_ERROR;
218 }
219
220 for(i=0,tg=tglist;i<totalcount&&th;tg=tg->next,i++)
221 atg[i] = tg;
222
223 qsort(atg, totalcount, sizeof(trustgroup *), comparetgs);
224
225 for(i=0;i<totalcount;i++)
226 if(atg[i]->id >= wanted)
227 break;
228
229 for(groupcount=linecount=0;i<totalcount;i++) {
230 linecount++;
231 groupcount++;
232
233 controlreply(np, "G,%s", dumptg(atg[i], 1));
234
235 for(th=atg[i]->hosts;th;th=th->next) {
236 linecount++;
237 controlreply(np, "H,%s", dumpth(th, 1));
238 }
239
240 if(--max == 0)
241 break;
242 }
243 nsfree(POOL_TRUSTS, atg);
244
245 controlreply(np, "End of list, %u groups and %u lines returned.", groupcount, linecount);
246 return CMD_OK;
247}
248
a99a2041
CP
249static int commandsregistered;
250
251static void registercommands(int hooknum, void *arg) {
252 if(commandsregistered)
253 return;
254 commandsregistered = 1;
255
a99a2041 256 registercontrolhelpcmd("trustlist", NO_OPER, 1, trusts_cmdtrustlist, "Usage: trustlist <#id|name|id>\nShows trust data for the specified trust group.");
82a316e7 257 registercontrolhelpcmd("trustdump", NO_OPER, 2, trusts_cmdtrustdump, "Usage: trustdump <#id> <number>");
a99a2041
CP
258}
259
83bccee3 260static void deregistercommands(int hooknum, void *arg) {
a99a2041
CP
261 if(!commandsregistered)
262 return;
263 commandsregistered = 0;
264
a99a2041 265 deregistercontrolcmd("trustlist", trusts_cmdtrustlist);
82a316e7 266 deregistercontrolcmd("trustdump", trusts_cmdtrustdump);
be2823bc
CP
267}
268
269void _init(void) {
a99a2041 270 registerhook(HOOK_TRUSTS_DB_LOADED, registercommands);
83bccee3 271 registerhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands);
be2823bc
CP
272
273 if(trustsdbloaded)
a99a2041 274 registercommands(0, NULL);
be2823bc
CP
275}
276
277void _fini(void) {
a99a2041 278 deregisterhook(HOOK_TRUSTS_DB_LOADED, registercommands);
83bccee3 279 deregisterhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands);
be2823bc 280
83bccee3
CP
281 deregistercommands(0, NULL);
282}