]>
Commit | Line | Data |
---|---|---|
3e3692bf CP |
1 | #include "../control/control.h" |
2 | #include "../localuser/localuser.h" | |
3 | #include "../core/schedule.h" | |
43e82ceb | 4 | #include "../core/modules.h" |
3e3692bf CP |
5 | #include "../lib/splitline.h" |
6 | #include "../lib/flags.h" | |
7 | #include "../lib/irc_string.h" | |
8 | #include "../lib/strlfunc.h" | |
9 | ||
10 | #include "noperserv.h" | |
11 | #include "noperserv_db.h" | |
12 | #include "noperserv_hooks.h" | |
13 | #include "noperserv_policy.h" | |
14 | ||
15 | #include <stdlib.h> | |
16 | #include <string.h> | |
17 | #include <stdarg.h> | |
18 | ||
19 | struct storedhook { | |
20 | CommandHandler old; | |
21 | sstring *name; | |
22 | char *oldhelp; | |
23 | char *newhelp; | |
24 | struct storedhook *next; | |
25 | } storedhook; | |
26 | ||
27 | struct storedhook *storedhooks = NULL; | |
28 | ||
29 | int firsttime = 0; | |
30 | nick *replynick = NULL; | |
31 | ||
32 | UserMessageHandler oldhandler; | |
33 | ControlMsg oldreply; | |
34 | ControlWall oldwall; | |
35 | ||
36 | void noperserv_trap_registration(int hooknum, void *arg); | |
37 | int noperserv_showcommands(void *sender, int cargc, char **cargv); | |
38 | int noperserv_rmmod(void *sender, int cargc, char **cargv); | |
39 | int noperserv_reload(void *sender, int cargc, char **cargv); | |
40 | int noperserv_whois(void *sender, int cargc, char **cargv); | |
41 | int noperserv_help(void *sender, int cargc, char **cargv); | |
42 | void noperserv_whois_handler(int hooknum, void *arg); | |
43 | void noperserv_whois_account_handler(int hooknum, void *arg); | |
44 | void noperserv_handle_messages(nick *target, int messagetype, void **args); | |
45 | void noperserv_reply(nick *np, char *format, ...); | |
46 | void noperserv_wall(flag_t permissionlevel, flag_t noticelevel, char *format, ...); | |
47 | ||
48 | struct specialsched special; | |
49 | ||
50 | #define HOOK_CONTROL_WHOISREQUEST_AUTHNAME -1 | |
51 | #define HOOK_CONTROL_WHOISREQUEST_AUTHEDUSER -2 | |
52 | ||
53 | void noperserv_setup_hooks(void) { | |
54 | oldreply = controlreply; | |
55 | controlreply = &noperserv_reply; | |
56 | ||
57 | oldwall = controlwall; | |
58 | controlwall = &noperserv_wall; | |
59 | ||
60 | memset(&special, 0, sizeof(specialsched)); | |
61 | ||
62 | if(!mynick) { | |
63 | registerhook(HOOK_CONTROL_REGISTERED, &noperserv_trap_registration); | |
64 | } else { | |
65 | noperserv_trap_registration(0, (void *)mynick); | |
66 | } | |
67 | ||
68 | registerhook(HOOK_CONTROL_WHOISREQUEST, &noperserv_whois_handler); | |
69 | } | |
70 | ||
71 | int noperserv_hook_command(char *command, CommandHandler newcommand, char *newhelp) { | |
72 | struct storedhook *newhook; | |
73 | Command *fetchcommand = findcommandintree(controlcmds, command, 1); | |
74 | ||
75 | if(!fetchcommand) | |
76 | return 1; | |
77 | ||
78 | newhook = (struct storedhook *)malloc(sizeof(struct storedhook)); | |
79 | if(!newhook) | |
80 | return 1; | |
81 | ||
82 | newhook->name = getsstring(command, strlen(command)); | |
83 | if(!newhook->name) { | |
84 | free(newhook); | |
85 | return 1; | |
86 | } | |
87 | ||
88 | newhook->old = fetchcommand->handler; | |
89 | if(newhelp) { | |
90 | int len = strlen(newhelp) + 1; | |
91 | newhook->newhelp = (char *)malloc(len); | |
92 | if(!newhook->newhelp) { | |
93 | freesstring(newhook->name); | |
94 | free(newhook); | |
95 | } | |
96 | strlcpy(newhook->newhelp, newhelp, len); | |
97 | newhook->oldhelp = fetchcommand->help; | |
98 | fetchcommand->help = newhook->newhelp; | |
99 | } else { | |
100 | newhook->newhelp = NULL; | |
101 | } | |
102 | ||
103 | newhook->next = storedhooks; | |
104 | storedhooks = newhook; | |
105 | ||
106 | fetchcommand->handler = newcommand; | |
107 | ||
108 | return 0; | |
109 | } | |
110 | ||
111 | void noperserv_unhook_all_commands(void) { | |
112 | struct storedhook *nh, *ch = storedhooks; | |
113 | Command *fetchcommand; | |
114 | ||
115 | while(ch) { | |
116 | if(ch->old && (fetchcommand = findcommandintree(controlcmds, ch->name->content, 1))) { | |
117 | fetchcommand->handler = ch->old; | |
118 | if(ch->newhelp) { | |
119 | fetchcommand->help = ch->oldhelp; | |
120 | free(ch->newhelp); | |
121 | } | |
122 | } | |
123 | nh = ch->next; | |
124 | freesstring(ch->name); | |
125 | free(ch); | |
126 | ch = nh; | |
127 | } | |
128 | } | |
129 | ||
130 | void noperserv_cleanup_hooks(void) { | |
131 | deregisterhook(HOOK_CONTROL_WHOISREQUEST, &noperserv_whois_handler); | |
132 | deregisterhook(HOOK_CONTROL_REGISTERED, &noperserv_trap_registration); | |
133 | ||
134 | if(firsttime) { | |
135 | noperserv_unhook_all_commands(); | |
136 | firsttime = 0; | |
137 | } | |
138 | ||
139 | if(oldhandler) | |
140 | hooklocaluserhandler(mynick, oldhandler); | |
141 | ||
142 | controlwall = oldwall; | |
143 | controlreply = oldreply; | |
144 | } | |
145 | ||
146 | void noperserv_trap_registration(int hooknum, void *arg) { | |
147 | oldhandler = hooklocaluserhandler((nick *)arg, &noperserv_handle_messages); | |
148 | if(!oldhandler) | |
149 | return; | |
150 | ||
151 | if(!firsttime) { | |
152 | firsttime = 1; | |
153 | noperserv_hook_command("rmmod", &noperserv_rmmod, NULL); | |
154 | noperserv_hook_command("reload", &noperserv_reload, NULL); | |
155 | noperserv_hook_command("showcommands", &noperserv_showcommands, NULL); | |
156 | noperserv_hook_command("whois", &noperserv_whois, "Usage: whois <nickname|#authname|*numeric>\nDisplays lots of information about the specified nickname, auth name or numeric."); | |
157 | noperserv_hook_command("help", &noperserv_help, NULL); | |
158 | } | |
159 | } | |
160 | ||
161 | CommandHandler noperserv_find_hook(char *command) { | |
162 | struct storedhook *hh = storedhooks; | |
163 | for(;hh;hh=hh->next) | |
164 | if(!ircd_strcmp(hh->name->content, command)) | |
165 | return hh->old; | |
166 | ||
167 | return NULL; | |
168 | } | |
169 | ||
43e82ceb CP |
170 | int noperserv_modules_loaded(char *mask) { |
171 | int i; | |
172 | char *ptr; | |
173 | ||
174 | for(i=0,ptr=lsmod(i);ptr;ptr=lsmod(++i)) | |
175 | if(match2strings(mask, ptr)) | |
176 | return 1; | |
177 | ||
178 | return 0; | |
179 | } | |
180 | ||
3e3692bf CP |
181 | int noperserv_specialmod(nick *np, char *command, ScheduleCallback reloadhandler, int cargc, char **cargv) { |
182 | CommandHandler oldcommand = noperserv_find_hook(command); | |
183 | if(cargc < 1) { | |
184 | if(oldcommand) | |
185 | return oldcommand(np, cargc, cargv); | |
186 | return CMD_ERROR; | |
187 | } | |
188 | ||
189 | if(!strcmp(cargv[0], "noperserv")) { | |
43e82ceb CP |
190 | if(noperserv_modules_loaded("noperserv_*")) { |
191 | controlreply(np, "NOT UNLOADING. Unload all dependencies first."); | |
192 | return CMD_ERROR; | |
193 | } | |
3e3692bf CP |
194 | if(special.schedule) { |
195 | controlreply(np, "Previous attempt at un/reload still in progress."); | |
196 | return CMD_OK; | |
197 | } else { | |
198 | special.modulename = getsstring(cargv[0], strlen(cargv[0])); | |
199 | if(!special.modulename) { | |
200 | controlreply(np, "Unable to copy module name. Seek cow herd to trample on server."); | |
201 | return CMD_ERROR; | |
202 | } else { | |
203 | special.schedule = scheduleoneshot(time(NULL) + 1, reloadhandler, &special); | |
204 | if(!special.schedule) { | |
205 | freesstring(special.modulename); | |
206 | special.modulename = NULL; | |
207 | controlreply(np, "Unable to allocate schedule. Seek cow herd to trample on server."); | |
208 | return CMD_ERROR; | |
209 | } | |
210 | controlreply(np, "Special case un/reload in <1 second, no response will be sent, standby. . ."); | |
211 | return CMD_OK; | |
212 | } | |
213 | } | |
214 | } else { | |
215 | if(oldcommand) | |
216 | return oldcommand(np, cargc, cargv); | |
217 | return CMD_ERROR; | |
218 | } | |
219 | } | |
220 | ||
221 | int noperserv_rmmod(void *sender, int cargc, char **cargv) { | |
222 | return noperserv_specialmod(sender, "rmmod", &controlspecialrmmod, cargc, cargv); | |
223 | } | |
224 | ||
225 | int noperserv_reload(void *sender, int cargc, char **cargv) { | |
226 | return noperserv_specialmod(sender, "reload", &controlspecialreloadmod, cargc, cargv); | |
227 | } | |
228 | ||
229 | void noperserv_whois_hook(int hooknum, void *arg) { | |
230 | controlreply(replynick, "%s", (char *)arg); | |
231 | } | |
232 | ||
233 | int noperserv_whois(void *sender, int cargc, char **cargv) { | |
234 | no_autheduser *au; | |
235 | nick *np = (nick *)sender; | |
236 | CommandHandler oldwhois = noperserv_find_hook("whois"); | |
237 | ||
238 | if(cargc < 1) { | |
239 | if(oldwhois) | |
240 | return oldwhois(sender, cargc, cargv); | |
241 | return CMD_ERROR; | |
242 | } | |
243 | ||
244 | if(cargv[0][0] != '#') { | |
245 | if(cargv[0][0] == '*') | |
246 | cargv[0][0] = '#'; | |
247 | if(oldwhois) | |
248 | return oldwhois(sender, cargc, cargv); | |
249 | return CMD_ERROR; | |
250 | } | |
251 | ||
252 | au = noperserv_get_autheduser(cargv[0] + 1); | |
253 | if(!au) { | |
254 | controlreply(np, "Account not registered."); | |
255 | return CMD_OK; | |
256 | } | |
257 | ||
258 | controlreply(np, "Account : %s", au->authname->content); | |
259 | ||
260 | replynick = np; | |
261 | ||
262 | registerhook(HOOK_CONTROL_WHOISREPLY, &noperserv_whois_hook); | |
263 | noperserv_whois_account_handler(HOOK_CONTROL_WHOISREQUEST_AUTHEDUSER, (void *)au); | |
264 | deregisterhook(HOOK_CONTROL_WHOISREPLY, &noperserv_whois_hook); | |
265 | ||
266 | controlreply(np, "Flags : %s", printflags(NOGetAuthLevel(au), no_userflags)); | |
267 | ||
268 | return CMD_OK; | |
269 | } | |
270 | ||
271 | int noperserv_showcommands(void *sender, int cargc, char **cargv) { | |
272 | nick *np = (nick *)sender; | |
273 | Command *cmdlist[100]; | |
274 | int i, n; | |
275 | ||
276 | n = getcommandlist(controlcmds, cmdlist, 100); | |
277 | ||
278 | controlreply(np, "The following commands are registered at present:"); | |
279 | ||
280 | for(i=0;i<n;i++) | |
281 | controlreply(np, "%s (%s)", cmdlist[i]->command->content, printflags(cmdlist[i]->level, no_commandflags)); | |
282 | ||
283 | controlreply(np, "End of list."); | |
284 | return CMD_OK; | |
285 | } | |
286 | ||
287 | void noperserv_whois_handler(int hooknum, void *arg) { | |
288 | char message[100]; | |
289 | nick *np = (nick *)arg; | |
290 | no_autheduser *au; | |
291 | if(!np) | |
292 | return; | |
293 | ||
294 | if(IsAccount(np)) { | |
295 | au = NOGetAuthedUser(np); | |
296 | if(au) { | |
297 | snprintf(message, sizeof(message), "Flags : %s", printflags(NOGetAuthLevel(au), no_userflags)); | |
298 | noperserv_whois_account_handler(HOOK_CONTROL_WHOISREQUEST_AUTHEDUSER, (void *)au); | |
299 | } else { | |
300 | snprintf(message, sizeof(message), "Flags : (user not known)"); | |
301 | noperserv_whois_account_handler(HOOK_CONTROL_WHOISREQUEST_AUTHNAME, (void *)np->authname); | |
302 | } | |
303 | triggerhook(HOOK_CONTROL_WHOISREPLY, message); | |
304 | } | |
305 | } | |
306 | ||
307 | /* mmm, hacky */ | |
308 | void noperserv_whois_account_handler(int hooknum, void *arg) { | |
309 | int count = 0, found = 0; | |
310 | char nickbuffer[(NICKLEN + 2) * NO_NICKS_PER_WHOIS_LINE - 1]; /* since we don't need space or comma for the first item we're fine NULL wise */ | |
311 | char accountspace[NICKLEN + 3]; /* space, comma, null */ | |
312 | char message[1024]; | |
313 | ||
314 | nickbuffer[0] = '\0'; | |
315 | if(hooknum == HOOK_CONTROL_WHOISREQUEST_AUTHEDUSER) { | |
316 | /* we can just read out the authed user linked list */ | |
317 | no_autheduser *au = (void *)arg; | |
318 | no_nicklist *nl = au->nick; | |
319 | ||
320 | if(nl) | |
321 | found = 1; | |
322 | ||
323 | for(;nl;nl=nl->next) { | |
324 | snprintf(accountspace, sizeof(accountspace), "%s%s", count++?", ":"", nl->nick->nick); | |
325 | strlcat(nickbuffer, accountspace, sizeof(nickbuffer)); | |
326 | ||
327 | if(count >= NO_NICKS_PER_WHOIS_LINE) { | |
328 | snprintf(message, sizeof(message), "Authed : %s", nickbuffer); | |
329 | triggerhook(HOOK_CONTROL_WHOISREPLY, message); | |
330 | nickbuffer[0] = '\0'; | |
331 | count = 0; | |
332 | } | |
333 | } | |
334 | } else { | |
335 | /* inefficient way */ | |
336 | char *authname = (char *)arg; | |
337 | int i = 0; | |
338 | nick *sp; | |
339 | ||
340 | for(;i<NICKHASHSIZE;i++) | |
341 | for(sp=nicktable[i];sp;sp=sp->next) | |
342 | if(IsAccount(sp) && !ircd_strcmp(sp->authname, authname)) { | |
343 | found = 1; | |
344 | ||
345 | snprintf(accountspace, sizeof(accountspace), "%s%s", count++?", ":"", sp->nick); | |
346 | strlcat(nickbuffer, accountspace, sizeof(nickbuffer)); | |
347 | ||
348 | if(count >= NO_NICKS_PER_WHOIS_LINE) { | |
349 | snprintf(message, sizeof(message), "Authed : %s", nickbuffer); | |
350 | triggerhook(HOOK_CONTROL_WHOISREPLY, message); | |
351 | nickbuffer[0] = '\0'; | |
352 | count = 0; | |
353 | } | |
354 | } | |
355 | } | |
356 | ||
357 | if(!found) { | |
358 | snprintf(message, sizeof(message), "Authed : (no nicks authed)"); | |
359 | triggerhook(HOOK_CONTROL_WHOISREPLY, message); | |
360 | } else if(nickbuffer[0]) { | |
361 | snprintf(message, sizeof(message), "Authed : %s", nickbuffer); | |
362 | triggerhook(HOOK_CONTROL_WHOISREPLY, message); | |
363 | } | |
364 | } | |
365 | ||
366 | /* Obviously pinched from control.c */ | |
367 | void noperserv_handle_messages(nick *target, int messagetype, void **args) { | |
368 | Command *cmd; | |
369 | char *cargv[50]; | |
370 | int cargc; | |
371 | nick *sender; | |
372 | ||
373 | switch(messagetype) { | |
374 | case LU_PRIVMSG: /* override these two commands only */ | |
375 | case LU_SECUREMSG: | |
376 | /* If it's a message, first arg is nick and second is message */ | |
377 | sender = (nick *)args[0]; | |
378 | ||
379 | controlwall(NO_DEVELOPER, NL_ALL_COMMANDS, "From: %s!%s@%s%s%s: %s", sender->nick, sender->ident, sender->host->name->content, IsAccount(sender)?"/":"", IsAccount(sender)?sender->authname:"", (char *)args[1]); | |
380 | ||
381 | /* Split the line into params */ | |
382 | cargc = splitline((char *)args[1], cargv, 50, 0); | |
383 | ||
384 | if(!cargc) /* Blank line */ | |
385 | return; | |
386 | ||
387 | cmd = findcommandintree(controlcmds,cargv[0],1); | |
388 | if(!cmd) { | |
389 | controlreply(sender, "Unknown command."); | |
390 | return; | |
391 | } | |
392 | ||
393 | /* If we were doing "authed user tracking" here we'd put a check in for authlevel */ | |
394 | /* Here it is! */ | |
395 | if (!noperserv_policy_command_permitted(cmd->level, sender)) { | |
396 | controlreply(sender, "Access denied."); | |
397 | return; | |
398 | } | |
399 | ||
400 | /* Check the maxargs */ | |
401 | if(cmd->maxparams < (cargc - 1)) { | |
402 | /* We need to do some rejoining */ | |
403 | rejoinline(cargv[cmd->maxparams], cargc - (cmd->maxparams)); | |
404 | cargc = (cmd->maxparams) + 1; | |
405 | } | |
406 | ||
407 | if((cmd->handler)((void *)sender,cargc-1,&(cargv[1])) == CMD_USAGE) | |
408 | controlhelp(sender, cmd); | |
409 | ||
410 | break; | |
411 | default: | |
412 | if(oldhandler) | |
413 | oldhandler(target, messagetype, args); | |
414 | break; | |
415 | } | |
416 | } | |
417 | ||
418 | void noperserv_reply(nick *np, char *format, ...) { | |
419 | char buf[512]; | |
420 | va_list va; | |
421 | no_autheduser *au = NOGetAuthedUser(np); | |
422 | ||
423 | va_start(va, format); | |
424 | vsnprintf(buf, sizeof(buf), format, va); | |
425 | va_end(va); | |
426 | ||
427 | if(au && !(NOGetNoticeLevel(au) & NL_NOTICES)) { | |
428 | controlmessage(np, "%s", buf); | |
429 | } else { | |
430 | controlnotice(np, "%s", buf); | |
431 | } | |
432 | } | |
433 | ||
434 | int noperserv_help(void *sender, int cargc, char **cargv) { | |
435 | Command *cmd; | |
436 | nick *np = (nick *)sender; | |
437 | ||
438 | if(cargc < 1) | |
439 | return CMD_USAGE; | |
440 | ||
441 | cmd = findcommandintree(controlcmds, cargv[0], 1); | |
442 | if(!cmd) { | |
443 | controlreply(np, "Unknown command."); | |
444 | return CMD_ERROR; | |
445 | } | |
446 | ||
447 | if(!noperserv_policy_command_permitted(cmd->level, np)) { | |
448 | controlreply(np, "Access denied."); | |
449 | return CMD_ERROR; | |
450 | } | |
451 | ||
452 | controlhelp(np, cmd); | |
453 | return CMD_OK; | |
454 | } | |
455 | ||
456 | void noperserv_wall(flag_t permissionlevel, flag_t noticelevel, char *format, ...) { | |
457 | char buf[512]; | |
458 | va_list va; | |
459 | no_autheduser *au = authedusers; | |
460 | no_nicklist *nl; | |
461 | char *flags = printflags(noticelevel, no_noticeflags) + 1; | |
462 | ||
463 | va_start(va, format); | |
464 | vsnprintf(buf, sizeof(buf), format, va); | |
465 | va_end(va); | |
466 | ||
467 | Error("noperserv", ERR_INFO, "$%s$ %s", flags, buf); | |
468 | ||
469 | for(;au;au=au->next) { | |
470 | if(NOGetNoticeLevel(au) & noticelevel) { | |
471 | for(nl=au->nick;nl;nl=nl->next) | |
472 | if(noperserv_policy_command_permitted(permissionlevel, nl->nick)) | |
473 | controlreply(nl->nick, "$%s$ %s", flags, buf); | |
474 | } | |
475 | } | |
476 | } |