]>
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; | |
a8d791b4 CP |
22 | sstring *oldhelp; |
23 | sstring *newhelp; | |
3e3692bf CP |
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 | ||
62a602c3 | 60 | memset(&special, 0, sizeof(struct specialsched)); |
3e3692bf CP |
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) { | |
a8d791b4 | 90 | newhook->newhelp = getsstring(newhelp, 512); |
3e3692bf CP |
91 | if(!newhook->newhelp) { |
92 | freesstring(newhook->name); | |
93 | free(newhook); | |
a8d791b4 | 94 | return 1; |
3e3692bf | 95 | } |
3e3692bf CP |
96 | newhook->oldhelp = fetchcommand->help; |
97 | fetchcommand->help = newhook->newhelp; | |
98 | } else { | |
99 | newhook->newhelp = NULL; | |
100 | } | |
101 | ||
102 | newhook->next = storedhooks; | |
103 | storedhooks = newhook; | |
104 | ||
105 | fetchcommand->handler = newcommand; | |
106 | ||
107 | return 0; | |
108 | } | |
109 | ||
110 | void noperserv_unhook_all_commands(void) { | |
111 | struct storedhook *nh, *ch = storedhooks; | |
112 | Command *fetchcommand; | |
113 | ||
114 | while(ch) { | |
115 | if(ch->old && (fetchcommand = findcommandintree(controlcmds, ch->name->content, 1))) { | |
116 | fetchcommand->handler = ch->old; | |
117 | if(ch->newhelp) { | |
118 | fetchcommand->help = ch->oldhelp; | |
a8d791b4 | 119 | freesstring(ch->newhelp); |
3e3692bf CP |
120 | } |
121 | } | |
122 | nh = ch->next; | |
123 | freesstring(ch->name); | |
124 | free(ch); | |
125 | ch = nh; | |
126 | } | |
127 | } | |
128 | ||
129 | void noperserv_cleanup_hooks(void) { | |
130 | deregisterhook(HOOK_CONTROL_WHOISREQUEST, &noperserv_whois_handler); | |
131 | deregisterhook(HOOK_CONTROL_REGISTERED, &noperserv_trap_registration); | |
132 | ||
133 | if(firsttime) { | |
134 | noperserv_unhook_all_commands(); | |
135 | firsttime = 0; | |
136 | } | |
137 | ||
4c551b15 | 138 | if(oldhandler && mynick) |
3e3692bf CP |
139 | hooklocaluserhandler(mynick, oldhandler); |
140 | ||
141 | controlwall = oldwall; | |
142 | controlreply = oldreply; | |
143 | } | |
144 | ||
145 | void noperserv_trap_registration(int hooknum, void *arg) { | |
4c551b15 CP |
146 | nick *np = (nick *)arg; |
147 | if(!np) | |
148 | return; | |
149 | ||
3e3692bf CP |
150 | oldhandler = hooklocaluserhandler((nick *)arg, &noperserv_handle_messages); |
151 | if(!oldhandler) | |
152 | return; | |
153 | ||
154 | if(!firsttime) { | |
155 | firsttime = 1; | |
156 | noperserv_hook_command("rmmod", &noperserv_rmmod, NULL); | |
157 | noperserv_hook_command("reload", &noperserv_reload, NULL); | |
158 | noperserv_hook_command("showcommands", &noperserv_showcommands, NULL); | |
159 | noperserv_hook_command("whois", &noperserv_whois, "Usage: whois <nickname|#authname|*numeric>\nDisplays lots of information about the specified nickname, auth name or numeric."); | |
160 | noperserv_hook_command("help", &noperserv_help, NULL); | |
161 | } | |
162 | } | |
163 | ||
164 | CommandHandler noperserv_find_hook(char *command) { | |
165 | struct storedhook *hh = storedhooks; | |
166 | for(;hh;hh=hh->next) | |
167 | if(!ircd_strcmp(hh->name->content, command)) | |
168 | return hh->old; | |
169 | ||
170 | return NULL; | |
171 | } | |
172 | ||
43e82ceb CP |
173 | int noperserv_modules_loaded(char *mask) { |
174 | int i; | |
175 | char *ptr; | |
176 | ||
2fb74c5e | 177 | for(i=0,ptr=lsmod(i,NULL,NULL,NULL);ptr;ptr=lsmod(++i,NULL,NULL,NULL)) |
43e82ceb CP |
178 | if(match2strings(mask, ptr)) |
179 | return 1; | |
180 | ||
181 | return 0; | |
182 | } | |
183 | ||
3e3692bf CP |
184 | int noperserv_specialmod(nick *np, char *command, ScheduleCallback reloadhandler, int cargc, char **cargv) { |
185 | CommandHandler oldcommand = noperserv_find_hook(command); | |
186 | if(cargc < 1) { | |
187 | if(oldcommand) | |
188 | return oldcommand(np, cargc, cargv); | |
189 | return CMD_ERROR; | |
190 | } | |
191 | ||
192 | if(!strcmp(cargv[0], "noperserv")) { | |
193 | if(special.schedule) { | |
194 | controlreply(np, "Previous attempt at un/reload still in progress."); | |
195 | return CMD_OK; | |
196 | } else { | |
197 | special.modulename = getsstring(cargv[0], strlen(cargv[0])); | |
198 | if(!special.modulename) { | |
199 | controlreply(np, "Unable to copy module name. Seek cow herd to trample on server."); | |
200 | return CMD_ERROR; | |
201 | } else { | |
202 | special.schedule = scheduleoneshot(time(NULL) + 1, reloadhandler, &special); | |
203 | if(!special.schedule) { | |
204 | freesstring(special.modulename); | |
205 | special.modulename = NULL; | |
206 | controlreply(np, "Unable to allocate schedule. Seek cow herd to trample on server."); | |
207 | return CMD_ERROR; | |
208 | } | |
209 | controlreply(np, "Special case un/reload in <1 second, no response will be sent, standby. . ."); | |
210 | return CMD_OK; | |
211 | } | |
212 | } | |
213 | } else { | |
214 | if(oldcommand) | |
215 | return oldcommand(np, cargc, cargv); | |
216 | return CMD_ERROR; | |
217 | } | |
218 | } | |
219 | ||
220 | int noperserv_rmmod(void *sender, int cargc, char **cargv) { | |
221 | return noperserv_specialmod(sender, "rmmod", &controlspecialrmmod, cargc, cargv); | |
222 | } | |
223 | ||
224 | int noperserv_reload(void *sender, int cargc, char **cargv) { | |
225 | return noperserv_specialmod(sender, "reload", &controlspecialreloadmod, cargc, cargv); | |
226 | } | |
227 | ||
228 | void noperserv_whois_hook(int hooknum, void *arg) { | |
229 | controlreply(replynick, "%s", (char *)arg); | |
230 | } | |
231 | ||
232 | int noperserv_whois(void *sender, int cargc, char **cargv) { | |
233 | no_autheduser *au; | |
234 | nick *np = (nick *)sender; | |
235 | CommandHandler oldwhois = noperserv_find_hook("whois"); | |
236 | ||
237 | if(cargc < 1) { | |
238 | if(oldwhois) | |
239 | return oldwhois(sender, cargc, cargv); | |
240 | return CMD_ERROR; | |
241 | } | |
242 | ||
243 | if(cargv[0][0] != '#') { | |
244 | if(cargv[0][0] == '*') | |
245 | cargv[0][0] = '#'; | |
246 | if(oldwhois) | |
247 | return oldwhois(sender, cargc, cargv); | |
248 | return CMD_ERROR; | |
249 | } | |
250 | ||
251 | au = noperserv_get_autheduser(cargv[0] + 1); | |
252 | if(!au) { | |
253 | controlreply(np, "Account not registered."); | |
254 | return CMD_OK; | |
255 | } | |
256 | ||
257 | controlreply(np, "Account : %s", au->authname->content); | |
258 | ||
259 | replynick = np; | |
260 | ||
261 | registerhook(HOOK_CONTROL_WHOISREPLY, &noperserv_whois_hook); | |
262 | noperserv_whois_account_handler(HOOK_CONTROL_WHOISREQUEST_AUTHEDUSER, (void *)au); | |
263 | deregisterhook(HOOK_CONTROL_WHOISREPLY, &noperserv_whois_hook); | |
264 | ||
265 | controlreply(np, "Flags : %s", printflags(NOGetAuthLevel(au), no_userflags)); | |
266 | ||
267 | return CMD_OK; | |
268 | } | |
269 | ||
270 | int noperserv_showcommands(void *sender, int cargc, char **cargv) { | |
271 | nick *np = (nick *)sender; | |
272 | Command *cmdlist[100]; | |
273 | int i, n; | |
274 | ||
275 | n = getcommandlist(controlcmds, cmdlist, 100); | |
276 | ||
277 | controlreply(np, "The following commands are registered at present:"); | |
278 | ||
279 | for(i=0;i<n;i++) | |
ad4fd3ca P |
280 | if(noperserv_policy_command_permitted(cmdlist[i]->level, np)) |
281 | controlreply(np, "%s (%s)", cmdlist[i]->command->content, printflags(cmdlist[i]->level, no_commandflags)); | |
3e3692bf CP |
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) { | |
01c8f85b | 470 | if((NOGetNoticeLevel(au) & noticelevel) && !(NOGetAuthLevel(au) & __NO_RELAY)) { |
3e3692bf CP |
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 | } |