4 * A replacement for Germania's ageing Operservice2
6 * Copyright (C) 2005 Chris Porter.
9 #include "../localuser/localuser.h"
10 #include "../lib/irc_string.h"
11 #include "../lib/strlfunc.h"
12 #include "noperserv.h"
13 #include "noperserv_db.h"
14 #include "noperserv_hooks.h"
15 #include "noperserv_policy.h"
21 #define FLAGBUFLEN 100
23 #define NO_FOUND_NICKNAME 1
24 #define NO_FOUND_AUTHNAME 2
26 const flag no_commandflags
[] = {
31 { 'd', __NO_DEVELOPER
},
35 { 'R', __NO_ACCOUNT
},
39 const flag no_userflags
[] = {
44 { 'd', __NO_DEVELOPER
},
48 const flag no_noticeflags
[] = {
49 { 'm', NL_MANAGEMENT
}, /* hello, password, userflags, noticeflags */
50 { 't', NL_TRUSTS
}, /* trust stuff... */
51 { 'k', NL_KICKS
}, /* KICK command */
52 { 'K', NL_KILLS
}, /* KILL command */
53 { 'g', NL_GLINES
}, /* GLINE commands */
54 { 'h', NL_HITS
}, /* Where a gline or kill is set automatically by the bot */
55 { 'c', NL_CLONING
}, /* Clone detection */
56 { 'C', NL_CLEARCHAN
}, /* When someone clearchans */
57 { 'f', NL_FAKEUSERS
}, /* Fakeuser addition */
58 { 'b', NL_BROADCASTS
}, /* Broadcast/mbroadcast/sbroadcast */
59 { 'o', NL_OPERATIONS
}, /* insmod/rmmod/etc */
60 { 'O', NL_OPERING
}, /* when someone opers */
61 { 'n', NL_NOTICES
}, /* turn off to receive notices instead of privmsgs */
62 { 'A', NL_ALL_COMMANDS
}, /* all commands sent */
66 int noperserv_hello(void *sender
, int cargc
, char **cargv
);
67 int noperserv_noticeflags(void *sender
, int cargc
, char **cargv
);
68 int noperserv_userflags(void *sender
, int cargc
, char **cargv
);
69 int noperserv_deluser(void *sender
, int cargc
, char **cargv
);
70 void noperserv_oper_detection(int hooknum
, void *arg
);
71 void noperserv_reply(nick
*np
, char *format
, ...);
76 if(!noperserv_load_db())
79 noperserv_ext
= registernickext("noperserv");
81 noperserv_setup_hooks();
83 registercontrolhelpcmd("hello", NO_OPERED
| NO_AUTHED
, 1, &noperserv_hello
, "Syntax: HELLO ?nickname|#authname?\nCreates an account on the service for the specified nick, or if one isn't supplied, your nickname.");
84 registercontrolhelpcmd("userflags", NO_ACCOUNT
, 2, &noperserv_userflags
, "Syntax: USERFLAGS <nickname|#authname> ?modifications?\nViews and modifies user permissions.\nIf no nickname or authname is supplied, you are substituted for it.\nIf no flags are supplied, flags are just displayed instead of modified.");
85 registercontrolhelpcmd("noticeflags", NO_ACCOUNT
, 1, &noperserv_noticeflags
,
86 "Syntax: NOTICEFLAGS ?(nickname|#authname)|flags?\n"
87 " This command can view and modify your own notice flags, and view that of other users.\n"
89 " +m: Management (hello, password, userflags, noticeflags)\n"
93 " +g: GLINE commands\n"
94 " +h: Shows when glines are played automatically (hits)\n"
95 " +c: Clone information\n"
96 " +C: CLEARCHAN command\n"
97 " +f: FAKEUSER command\n"
98 " +b: BROADCAST commands\n"
99 " +o: Operation commands, such as insmod, rmmod, die, etc\n"
101 " +n: Sends notices instead of privmsgs\n"
102 " +A: Every single command sent to the service\n"
105 registercontrolhelpcmd("deluser", NO_OPERED
| NO_ACCOUNT
, 2, &noperserv_deluser
, "Syntax: DELUSER <nickname|#authname>\nDeletes the specified user.");
106 registerhook(HOOK_NICK_MODEOPER
, &noperserv_oper_detection
);
111 #ifdef BROKEN_DLCLOSE
119 deregisterhook(HOOK_NICK_MODEOPER
, &noperserv_oper_detection
);
121 deregistercontrolcmd("noticeflags", &noperserv_noticeflags
);
122 deregistercontrolcmd("userflags", &noperserv_userflags
);
123 deregistercontrolcmd("noticeflags", &noperserv_noticeflags
);
125 noperserv_cleanup_hooks();
127 noperserv_cleanup_db();
129 releasenickext(noperserv_ext
);
133 int noperserv_hello(void *sender
, int cargc
, char **cargv
) {
137 nick
*np
= (nick
*)sender
, *np2
, *target
= NULL
;
140 newaccount
= np
->authname
;
142 if(cargv
[0][0] == '#') {
144 for(i
=0;i
<NICKHASHSIZE
;i
++)
145 for(np2
=nicktable
[i
];np2
;np2
=np2
->next
)
146 if(IsAccount(np2
) && !ircd_strcmp(cargv
[0] + 1, np2
->authname
)) {
148 newaccount
= target
->authname
;
152 controlreply(np
, "Cannot find anyone with that authname on the network.");
156 target
= getnickbynick(cargv
[0]);
158 controlreply(np
, "Supplied nickname is not on the network.");
161 if(!IsAccount(target
)) {
162 controlreply(np
, "Supplied user is not authed with the network.");
165 newaccount
= target
->authname
;
168 au
= noperserv_get_autheduser(newaccount
);
170 controlreply(np
, "Authname already registered.");
174 au
= noperserv_new_autheduser(newaccount
);
176 controlreply(np
, "Memory allocation error.");
180 if(noperserv_get_autheduser_count() == 1) {
181 au
->authlevel
= NO_FIRST_USER_LEVEL
;
182 au
->noticelevel
= NO_FIRST_USER_DEFAULT_NOTICELEVEL
;
184 au
->authlevel
= NO_DEFAULT_LEVEL
;
185 au
->noticelevel
= NO_DEFAULT_NOTICELEVEL
;
188 au
->id
= noperserv_next_autheduser_id();
189 noperserv_update_autheduser(au
);
191 for(i
=0;i
<NICKHASHSIZE
;i
++)
192 for(np2
=nicktable
[i
];np2
;np2
=np2
->next
)
193 if(IsAccount(np2
) && !ircd_strcmp(newaccount
, np2
->authname
)) {
194 noperserv_add_to_autheduser(np2
, au
);
195 controlreply(np2
, "An account has been created for you (auth %s).", au
->authname
->content
);
196 if(NOGetAuthLevel(au
))
197 controlreply(np2
, "User flags: %s", printflags(NOGetAuthLevel(au
), no_userflags
));
198 controlreply(np2
, "Notice flags: %s", printflags(NOGetNoticeLevel(au
), no_noticeflags
));
201 if(ircd_strcmp(np
->authname
, newaccount
)) { /* send a message to the person who HELLO'ed if we haven't already been told */
202 controlreply(np
, "Account created for auth %s.", au
->authname
->content
);
203 if(NOGetAuthLevel(au
))
204 controlreply(np
, "User flags: %s", printflags(NOGetAuthLevel(au
), no_userflags
));
205 controlreply(np
, "Notice flags: %s", printflags(NOGetNoticeLevel(au
), no_noticeflags
));
206 controlreply(np
, "Instructions sent to all authed users.");
207 } else if(au
->nick
&& au
->nick
->next
) { /* if we have already been told, tell the user it was sent to more than themselves */
208 controlreply(np
, "Instructions sent to all authed users.");
211 controlwall(NO_OPERED
, NL_MANAGEMENT
, "%s/%s just HELLO'ed: %s", np
->nick
, np
->authname
, au
->authname
->content
);
215 no_autheduser
*noperserv_autheduser_from_command(nick
*np
, char *command
, int *typefound
, char **returned
) {
217 if(command
[0] == '#') {
218 au
= noperserv_get_autheduser(command
+ 1);
220 controlreply(np
, "Authname not found.");
222 *typefound
= NO_FOUND_AUTHNAME
;
223 *returned
= au
->authname
->content
;
227 nick
*np2
= getnickbynick(command
);
229 controlreply(np
, "Nickname not on the network.");
232 if(!IsAccount(np2
)) {
233 controlreply(np
, "User is not authed with the network.");
236 au
= NOGetAuthedUser(np2
);
238 controlreply(np
, "User does not have an account.");
240 *typefound
= NO_FOUND_NICKNAME
;
241 *returned
= np2
->nick
;
249 int noperserv_noticeflags(void *sender
, int cargc
, char **cargv
) {
250 nick
*np
= (nick
*)sender
;
254 if((cargv
[0][0] == '+') || (cargv
[0][0] == '-')) {
256 au
= NOGetAuthedUser(np
);
257 flag_t fwas
= NOGetNoticeLevel(au
), permittedchanges
= noperserv_policy_permitted_noticeflags(au
);
259 ret
= setflags(&au
->noticelevel
, permittedchanges
, cargv
[0], no_noticeflags
, REJECT_DISALLOWED
| REJECT_UNKNOWN
);
260 if(ret
!= REJECT_UNKNOWN
) {
261 if(ret
== REJECT_DISALLOWED
) {
263 setflags(&fnow
, NL_ALL
, cargv
[0], no_noticeflags
, REJECT_NONE
);
265 controlreply(np
, "No changes made to existing flags.");
267 char ourflags
[FLAGBUFLEN
], ournoticeflags
[FLAGBUFLEN
];
268 controlreply(np
, "Flag alterations denied.");
270 strlcpy(ourflags
, printflags(NOGetAuthLevel(au
), no_userflags
), sizeof(ourflags
));
271 strlcpy(ournoticeflags
, printflags(NOGetNoticeLevel(au
), no_noticeflags
), sizeof(ournoticeflags
));
272 controlwall(NO_OPER
, NL_MANAGEMENT
, "%s/%s (%s) attempted to NOTICEFLAGS (%s): %s", np
->nick
, np
->authname
, ourflags
, ournoticeflags
, printflagdiff(fwas
, fnow
, no_noticeflags
));
275 } else if(ret
== REJECT_NONE
) {
276 if(NOGetNoticeLevel(au
) == fwas
) {
277 controlreply(np
, "No changes made to existing flags.");
279 char ourflags
[FLAGBUFLEN
], ournoticeflags
[FLAGBUFLEN
], diff
[FLAGBUFLEN
* 2 + 1], finalflags
[FLAGBUFLEN
];
280 no_nicklist
*nl
= au
->nick
;
281 noperserv_update_autheduser(au
);
282 controlreply(np
, "Flag alterations complete.");
284 strlcpy(ourflags
, printflags(NOGetAuthLevel(au
), no_userflags
), sizeof(ourflags
));
285 strlcpy(ournoticeflags
, printflags(fwas
, no_noticeflags
), sizeof(ournoticeflags
));
286 strlcpy(diff
, printflagdiff(fwas
, NOGetNoticeLevel(au
), no_noticeflags
), sizeof(diff
));
287 controlwall(NO_OPER
, NL_MANAGEMENT
, "%s/%s (%s) successfully used NOTICEFLAGS (%s): %s", np
->nick
, np
->authname
, ourflags
, ournoticeflags
, diff
);
289 strlcpy(finalflags
, printflags(NOGetNoticeLevel(au
), no_noticeflags
), sizeof(finalflags
));
292 controlreply(nl
->nick
, "!!! %s just used NOTICEFLAGS (%s): %s", np
->nick
, ournoticeflags
, diff
);
293 controlreply(nl
->nick
, "Your notice flags are %s", finalflags
);
298 controlreply(np
, "Unknown flag(s) supplied.");
304 au
= noperserv_autheduser_from_command(np
, cargv
[0], &typefound
, &itemfound
);
308 if(au
!= NOGetAuthedUser(np
)) {
309 controlreply(np
, "Notice flags for %s %s are: %s", typefound
==NO_FOUND_NICKNAME
?"user":"authname", itemfound
, printflags(NOGetNoticeLevel(au
), no_noticeflags
));
314 au
= NOGetAuthedUser(np
);
317 if(!au
) /* shouldn't happen */
320 controlreply(np
, "Your notice flags are: %s", printflags(NOGetNoticeLevel(au
), no_noticeflags
));
326 int noperserv_deluser(void *sender
, int cargc
, char **cargv
) {
327 nick
*np
= (nick
*)sender
;
328 no_autheduser
*target
/* target user */, *au
= NOGetAuthedUser(np
); /* user executing command */
329 char *userreturned
= NULL
; /* nickname or authname of the target, pulled from the db */
330 int typefound
; /* whether it was an authname or a username */
332 char targetflags
[FLAGBUFLEN
], ourflags
[FLAGBUFLEN
], deleteduser
[NOMax(ACCOUNTLEN
, NICKLEN
) + 1];
337 target
= noperserv_autheduser_from_command(np
, cargv
[0], &typefound
, &userreturned
);
341 strlcpy(targetflags
, printflags(NOGetAuthLevel(target
), no_userflags
), sizeof(targetflags
));
342 strlcpy(ourflags
, printflags(NOGetAuthLevel(au
), no_userflags
), sizeof(ourflags
));
344 /* we have to copy it as it might point to an autheduser, which we're about to delete */
345 strlcpy(deleteduser
, userreturned
, sizeof(deleteduser
));
347 /* we have to check if target != au, because if successful policy_modification_permitted just returns the flags we're allowed
348 to modify, if we have no flags we won't be able to delete ourselves */
349 if((target
!= au
) && !noperserv_policy_permitted_modifications(au
, target
)) {
350 controlreply(np
, "Deletion denied.");
351 controlwall(NO_OPER
, NL_MANAGEMENT
, "%s/%s (%s) attempted to DELUSER %s (%s)", np
->nick
, np
->authname
, ourflags
, target
->authname
->content
, targetflags
);
356 for(nl
=target
->nick
;nl
;nl
=nl
->next
)
358 controlreply(nl
->nick
, "!!! %s/%s (%s) just DELUSERed you.", np
->nick
, np
->authname
, ourflags
);
360 noperserv_delete_autheduser(target
);
362 controlwall(NO_OPER
, NL_MANAGEMENT
, "%s/%s (%s) successfully used DELUSER on %s (%s)", np
->nick
, np
->authname
, ourflags
, target
->authname
->content
, targetflags
);
365 controlreply(np
, "You have been deleted.");
367 controlreply(np
, "%s %s deleted.", typefound
==NO_FOUND_AUTHNAME
?"Auth":"User", deleteduser
);
374 /* this command needs LOTS of checking */
375 int noperserv_userflags(void *sender
, int cargc
, char **cargv
) {
376 nick
*np
= (nick
*)sender
;
377 no_autheduser
*au
= NOGetAuthedUser(np
), *target
= NULL
;
378 char *flags
= NULL
, *nicktarget
= NULL
;
383 } else if(cargc
== 1) {
384 if((cargv
[0][0] == '+') || (cargv
[0][0] == '-')) { /* modify our own */
387 } else { /* viewing someone elses */
388 nicktarget
= cargv
[0];
390 } else if(cargc
== 2) {
391 nicktarget
= cargv
[0];
398 target
= noperserv_autheduser_from_command(np
, nicktarget
, &typefound
, &nicktarget
);
405 flag_t permitted
= noperserv_policy_permitted_modifications(au
, target
), fwas
= NOGetAuthLevel(target
), fours
= NOGetAuthLevel(au
);
407 ret
= setflags(&target
->authlevel
, permitted
, flags
, no_userflags
, REJECT_DISALLOWED
| REJECT_UNKNOWN
);
408 if(ret
!= REJECT_UNKNOWN
) {
409 if(ret
== REJECT_DISALLOWED
) {
411 setflags(&fnow
, NO_ALL_FLAGS
, flags
, no_userflags
, REJECT_NONE
);
413 controlreply(np
, "No changes made to existing flags.");
415 char targetflags
[FLAGBUFLEN
], ourflags
[FLAGBUFLEN
];
416 controlreply(np
, "Flag alterations denied.");
418 strlcpy(targetflags
, printflags(fwas
, no_userflags
), sizeof(targetflags
));
419 strlcpy(ourflags
, printflags(fours
, no_userflags
), sizeof(ourflags
));
421 controlwall(NO_OPER
, NL_MANAGEMENT
, "%s/%s (%s) attempted to use USERFLAGS on %s (%s): %s", np
->nick
, np
->authname
, ourflags
, target
->authname
->content
, targetflags
, printflagdiff(fwas
, fnow
, no_userflags
));
424 } else if(ret
== REJECT_NONE
) {
425 if(NOGetAuthLevel(target
) == fwas
) {
426 controlreply(np
, "No changes made to existing flags.");
428 char targetflags
[FLAGBUFLEN
], ourflags
[FLAGBUFLEN
], finalflags
[FLAGBUFLEN
];
429 no_nicklist
*nl
= target
->nick
;
431 noperserv_policy_update_noticeflags(fwas
, target
);
432 noperserv_update_autheduser(target
);
434 controlreply(np
, "Flag alterations complete.");
436 strlcpy(targetflags
, printflags(fwas
, no_userflags
), sizeof(targetflags
));
437 strlcpy(ourflags
, printflags(fours
, no_userflags
), sizeof(ourflags
));
439 controlwall(NO_OPER
, NL_MANAGEMENT
, "%s/%s (%s) successfully used USERFLAGS on %s (%s): %s", np
->nick
, np
->authname
, ourflags
, target
->authname
->content
, targetflags
, printflagdiff(fwas
, NOGetAuthLevel(target
), no_userflags
));
441 strlcpy(finalflags
, printflags(NOGetAuthLevel(target
), no_userflags
), sizeof(finalflags
));
444 controlreply(nl
->nick
, "!!! %s/%s (%s) just used USERFLAGS on you (%s): %s", np
->nick
, np
->authname
, ourflags
, targetflags
, printflagdiff(fwas
, NOGetAuthLevel(target
), no_userflags
));
445 controlreply(nl
->nick
, "Your user flags are now: %s", finalflags
);
446 controlreply(nl
->nick
, "Your notice flags are now: %s", printflags(target
->noticelevel
, no_noticeflags
));
451 controlreply(np
, "Unknown flag(s) supplied.");
457 controlreply(np
, "User flags for %s %s: %s", typefound
==NO_FOUND_AUTHNAME
?"auth":"user", nicktarget
, printflags(NOGetAuthLevel(target
), no_userflags
));
458 controlreply(np
, "Notice flags for %s %s: %s", typefound
==NO_FOUND_AUTHNAME
?"auth":"user", nicktarget
, printflags(target
->noticelevel
, no_noticeflags
));
460 controlreply(np
, "Your user flags are: %s", printflags(NOGetAuthLevel(target
), no_userflags
));
461 controlreply(np
, "Your notice flags are: %s", printflags(target
->noticelevel
, no_noticeflags
));
467 void noperserv_oper_detection(int hooknum
, void *arg
) {
468 void **args
= (void **)arg
;
470 char *modestr
= args
[1];
471 flag_t after
= np
->umodes
;
473 setflags(&after
, UMODE_ALL
, modestr
, umodeflags
, REJECT_NONE
);
474 if(np
->umodes
& UMODE_OPER
) {
475 if(!(after
& UMODE_OPER
))
476 controlwall(NO_OPER
, NL_OPERING
, "%s!%s@%s%s%s just DEOPERed", np
->nick
, np
->ident
, np
->host
->name
->content
, IsAccount(np
)?"/":"", IsAccount(np
)?np
->authname
:"");
478 if(after
& UMODE_OPER
)
479 controlwall(NO_OPER
, NL_OPERING
, "%s!%s@%s%s%s just OPERed", np
->nick
, np
->ident
, np
->host
->name
->content
, IsAccount(np
)?"/":"", IsAccount(np
)?np
->authname
:"");