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