1 #include "../control/control.h"
2 #include "../localuser/localuser.h"
3 #include "../core/schedule.h"
4 #include "../core/modules.h"
5 #include "../lib/splitline.h"
6 #include "../lib/flags.h"
7 #include "../lib/irc_string.h"
8 #include "../lib/strlfunc.h"
10 #include "noperserv.h"
11 #include "noperserv_db.h"
12 #include "noperserv_hooks.h"
13 #include "noperserv_policy.h"
24 struct storedhook
*next
;
27 struct storedhook
*storedhooks
= NULL
;
30 nick
*replynick
= NULL
;
32 UserMessageHandler oldhandler
;
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
, ...);
48 struct specialsched special
;
50 #define HOOK_CONTROL_WHOISREQUEST_AUTHNAME -1
51 #define HOOK_CONTROL_WHOISREQUEST_AUTHEDUSER -2
53 void noperserv_setup_hooks(void) {
54 oldreply
= controlreply
;
55 controlreply
= &noperserv_reply
;
57 oldwall
= controlwall
;
58 controlwall
= &noperserv_wall
;
60 memset(&special
, 0, sizeof(struct specialsched
));
63 registerhook(HOOK_CONTROL_REGISTERED
, &noperserv_trap_registration
);
65 noperserv_trap_registration(0, (void *)mynick
);
68 registerhook(HOOK_CONTROL_WHOISREQUEST
, &noperserv_whois_handler
);
71 int noperserv_hook_command(char *command
, CommandHandler newcommand
, char *newhelp
) {
72 struct storedhook
*newhook
;
73 Command
*fetchcommand
= findcommandintree(controlcmds
, command
, 1);
78 newhook
= (struct storedhook
*)malloc(sizeof(struct storedhook
));
82 newhook
->name
= getsstring(command
, strlen(command
));
88 newhook
->old
= fetchcommand
->handler
;
90 newhook
->newhelp
= getsstring(newhelp
, 512);
91 if(!newhook
->newhelp
) {
92 freesstring(newhook
->name
);
96 newhook
->oldhelp
= fetchcommand
->help
;
97 fetchcommand
->help
= newhook
->newhelp
;
99 newhook
->newhelp
= NULL
;
102 newhook
->next
= storedhooks
;
103 storedhooks
= newhook
;
105 fetchcommand
->handler
= newcommand
;
110 void noperserv_unhook_all_commands(void) {
111 struct storedhook
*nh
, *ch
= storedhooks
;
112 Command
*fetchcommand
;
115 if(ch
->old
&& (fetchcommand
= findcommandintree(controlcmds
, ch
->name
->content
, 1))) {
116 fetchcommand
->handler
= ch
->old
;
118 fetchcommand
->help
= ch
->oldhelp
;
119 freesstring(ch
->newhelp
);
123 freesstring(ch
->name
);
129 void noperserv_cleanup_hooks(void) {
130 deregisterhook(HOOK_CONTROL_WHOISREQUEST
, &noperserv_whois_handler
);
131 deregisterhook(HOOK_CONTROL_REGISTERED
, &noperserv_trap_registration
);
134 noperserv_unhook_all_commands();
138 if(oldhandler
&& mynick
)
139 hooklocaluserhandler(mynick
, oldhandler
);
141 controlwall
= oldwall
;
142 controlreply
= oldreply
;
145 void noperserv_trap_registration(int hooknum
, void *arg
) {
146 nick
*np
= (nick
*)arg
;
150 oldhandler
= hooklocaluserhandler((nick
*)arg
, &noperserv_handle_messages
);
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
);
164 CommandHandler
noperserv_find_hook(char *command
) {
165 struct storedhook
*hh
= storedhooks
;
167 if(!ircd_strcmp(hh
->name
->content
, command
))
173 int noperserv_modules_loaded(char *mask
) {
177 for(i
=0,ptr
=lsmod(i
,NULL
,NULL
,NULL
);ptr
;ptr
=lsmod(++i
,NULL
,NULL
,NULL
))
178 if(match2strings(mask
, ptr
))
184 int noperserv_specialmod(nick
*np
, char *command
, ScheduleCallback reloadhandler
, int cargc
, char **cargv
) {
185 CommandHandler oldcommand
= noperserv_find_hook(command
);
188 return oldcommand(np
, cargc
, cargv
);
192 if(!strcmp(cargv
[0], "noperserv")) {
193 if(special
.schedule
) {
194 controlreply(np
, "Previous attempt at un/reload still in progress.");
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.");
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.");
209 controlreply(np
, "Special case un/reload in <1 second, no response will be sent, standby. . .");
215 return oldcommand(np
, cargc
, cargv
);
220 int noperserv_rmmod(void *sender
, int cargc
, char **cargv
) {
221 return noperserv_specialmod(sender
, "rmmod", &controlspecialrmmod
, cargc
, cargv
);
224 int noperserv_reload(void *sender
, int cargc
, char **cargv
) {
225 return noperserv_specialmod(sender
, "reload", &controlspecialreloadmod
, cargc
, cargv
);
228 void noperserv_whois_hook(int hooknum
, void *arg
) {
229 controlreply(replynick
, "%s", (char *)arg
);
232 int noperserv_whois(void *sender
, int cargc
, char **cargv
) {
234 nick
*np
= (nick
*)sender
;
235 CommandHandler oldwhois
= noperserv_find_hook("whois");
239 return oldwhois(sender
, cargc
, cargv
);
243 if(cargv
[0][0] != '#') {
244 if(cargv
[0][0] == '*')
247 return oldwhois(sender
, cargc
, cargv
);
251 au
= noperserv_get_autheduser(cargv
[0] + 1);
253 controlreply(np
, "Account not registered.");
257 controlreply(np
, "Account : %s", au
->authname
->content
);
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
);
265 controlreply(np
, "Flags : %s", printflags(NOGetAuthLevel(au
), no_userflags
));
270 int noperserv_showcommands(void *sender
, int cargc
, char **cargv
) {
271 nick
*np
= (nick
*)sender
;
272 Command
*cmdlist
[100];
275 n
= getcommandlist(controlcmds
, cmdlist
, 100);
277 controlreply(np
, "The following commands are registered at present:");
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
));
283 controlreply(np
, "End of list.");
287 void noperserv_whois_handler(int hooknum
, void *arg
) {
289 nick
*np
= (nick
*)arg
;
295 au
= NOGetAuthedUser(np
);
297 snprintf(message
, sizeof(message
), "Flags : %s", printflags(NOGetAuthLevel(au
), no_userflags
));
298 noperserv_whois_account_handler(HOOK_CONTROL_WHOISREQUEST_AUTHEDUSER
, (void *)au
);
300 snprintf(message
, sizeof(message
), "Flags : (user not known)");
301 noperserv_whois_account_handler(HOOK_CONTROL_WHOISREQUEST_AUTHNAME
, (void *)np
->authname
);
303 triggerhook(HOOK_CONTROL_WHOISREPLY
, message
);
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 */
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
;
323 for(;nl
;nl
=nl
->next
) {
324 snprintf(accountspace
, sizeof(accountspace
), "%s%s", count
++?", ":"", nl
->nick
->nick
);
325 strlcat(nickbuffer
, accountspace
, sizeof(nickbuffer
));
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';
335 /* inefficient way */
336 char *authname
= (char *)arg
;
340 for(;i
<NICKHASHSIZE
;i
++)
341 for(sp
=nicktable
[i
];sp
;sp
=sp
->next
)
342 if(IsAccount(sp
) && !ircd_strcmp(sp
->authname
, authname
)) {
345 snprintf(accountspace
, sizeof(accountspace
), "%s%s", count
++?", ":"", sp
->nick
);
346 strlcat(nickbuffer
, accountspace
, sizeof(nickbuffer
));
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';
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
);
366 /* Obviously pinched from control.c */
367 void noperserv_handle_messages(nick
*target
, int messagetype
, void **args
) {
373 switch(messagetype
) {
374 case LU_PRIVMSG
: /* override these two commands only */
376 /* If it's a message, first arg is nick and second is message */
377 sender
= (nick
*)args
[0];
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]);
381 /* Split the line into params */
382 cargc
= splitline((char *)args
[1], cargv
, 50, 0);
384 if(!cargc
) /* Blank line */
387 cmd
= findcommandintree(controlcmds
,cargv
[0],1);
389 controlreply(sender
, "Unknown command.");
393 /* If we were doing "authed user tracking" here we'd put a check in for authlevel */
395 if (!noperserv_policy_command_permitted(cmd
->level
, sender
)) {
396 controlreply(sender
, "Access denied.");
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;
407 if((cmd
->handler
)((void *)sender
,cargc
-1,&(cargv
[1])) == CMD_USAGE
)
408 controlhelp(sender
, cmd
);
413 oldhandler(target
, messagetype
, args
);
418 void noperserv_reply(nick
*np
, char *format
, ...) {
421 no_autheduser
*au
= NOGetAuthedUser(np
);
423 va_start(va
, format
);
424 vsnprintf(buf
, sizeof(buf
), format
, va
);
427 if(au
&& !(NOGetNoticeLevel(au
) & NL_NOTICES
)) {
428 controlmessage(np
, "%s", buf
);
430 controlnotice(np
, "%s", buf
);
434 int noperserv_help(void *sender
, int cargc
, char **cargv
) {
436 nick
*np
= (nick
*)sender
;
441 cmd
= findcommandintree(controlcmds
, cargv
[0], 1);
443 controlreply(np
, "Unknown command.");
447 if(!noperserv_policy_command_permitted(cmd
->level
, np
)) {
448 controlreply(np
, "Access denied.");
452 controlhelp(np
, cmd
);
456 void noperserv_wall(flag_t permissionlevel
, flag_t noticelevel
, char *format
, ...) {
459 no_autheduser
*au
= authedusers
;
461 char *flags
= printflags(noticelevel
, no_noticeflags
) + 1;
463 va_start(va
, format
);
464 vsnprintf(buf
, sizeof(buf
), format
, va
);
467 Error("noperserv", ERR_INFO
, "$%s$ %s", flags
, buf
);
469 for(;au
;au
=au
->next
) {
470 if((NOGetNoticeLevel(au
) & noticelevel
) && !(NOGetAuthLevel(au
) & __NO_RELAY
)) {
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
);