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 "noperserv.h"
12 #include "noperserv_db.h"
13 #include "noperserv_hooks.h"
14 #include "noperserv_policy.h"
20 #define FLAGBUFLEN 100
22 #define NO_FOUND_NICKNAME 1
23 #define NO_FOUND_AUTHNAME 2
25 const flag no_commandflags
[] = {
30 { 'd', __NO_DEVELOPER
},
34 { 'R', __NO_ACCOUNT
},
38 const flag no_userflags
[] = {
43 { 'd', __NO_DEVELOPER
},
47 const flag no_noticeflags
[] = {
48 { 'm', NL_MANAGEMENT
}, /* hello, password, userflags, noticeflags */
49 { 't', NL_TRUSTS
}, /* trust stuff... */
50 { 'k', NL_KICKS
}, /* KICK command */
51 { 'K', NL_KILLS
}, /* KILL command */
52 { 'g', NL_GLINES
}, /* GLINE commands */
53 { 'h', NL_HITS
}, /* Where a gline or kill is set automatically by the bot */
54 { 'c', NL_CLONING
}, /* Clone detection */
55 { 'C', NL_CLEARCHAN
}, /* When someone clearchans */
56 { 'f', NL_FAKEUSERS
}, /* Fakeuser addition */
57 { 'b', NL_BROADCASTS
}, /* Broadcast/mbroadcast/sbroadcast */
58 { 'o', NL_OPERATIONS
}, /* insmod/rmmod/etc */
59 { 'O', NL_OPERING
}, /* when someone opers */
60 { 'n', NL_NOTICES
}, /* turn off to receive notices instead of privmsgs */
61 { 'A', NL_ALL_COMMANDS
}, /* all commands sent */
65 int noperserv_hello(void *sender
, int cargc
, char **cargv
);
66 int noperserv_noticeflags(void *sender
, int cargc
, char **cargv
);
67 int noperserv_userflags(void *sender
, int cargc
, char **cargv
);
68 int noperserv_deluser(void *sender
, int cargc
, char **cargv
);
69 void noperserv_oper_detection(int hooknum
, void *arg
);
70 void noperserv_reply(nick
*np
, char *format
, ...);
73 noperserv_ext
= registernickext("noperserv");
77 noperserv_setup_hooks();
79 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.");
80 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.");
81 registercontrolhelpcmd("noticeflags", NO_ACCOUNT
, 1, &noperserv_noticeflags
,
82 "Syntax: NOTICEFLAGS ?(nickname|#authname)|flags?\n"
83 " This command can view and modify your own notice flags, and view that of other users.\n"
85 " +m: Management (hello, password, userflags, noticeflags)\n"
89 " +g: GLINE commands\n"
90 " +h: Shows when glines are played automatically (hits)\n"
91 " +c: Clone information\n"
92 " +C: CLEARCHAN command\n"
93 " +f: FAKEUSER command\n"
94 " +b: BROADCAST commands\n"
95 " +o: Operation commands, such as insmod, rmmod, die, etc\n"
97 " +n: Sends notices instead of privmsgs\n"
98 " +A: Every single command sent to the service\n"
101 registercontrolhelpcmd("deluser", NO_OPERED
| NO_ACCOUNT
, 2, &noperserv_deluser
, "Syntax: DELUSER <nickname|#authname>\nDeletes the specified user.");
102 registerhook(HOOK_NICK_MODEOPER
, &noperserv_oper_detection
);
105 #ifdef BROKEN_DLCLOSE
110 deregisterhook(HOOK_NICK_MODEOPER
, &noperserv_oper_detection
);
112 deregistercontrolcmd("noticeflags", &noperserv_noticeflags
);
113 deregistercontrolcmd("userflags", &noperserv_userflags
);
114 deregistercontrolcmd("noticeflags", &noperserv_noticeflags
);
116 noperserv_cleanup_hooks();
118 noperserv_cleanup_db();
120 releasenickext(noperserv_ext
);
124 int noperserv_hello(void *sender
, int cargc
, char **cargv
) {
128 nick
*np
= (nick
*)sender
, *np2
, *target
= NULL
;
131 newaccount
= np
->authname
;
133 if(cargv
[0][0] == '#') {
135 for(i
=0;i
<NICKHASHSIZE
;i
++)
136 for(np2
=nicktable
[i
];np2
;np2
=np2
->next
)
137 if(IsAccount(np2
) && !ircd_strcmp(cargv
[0] + 1, np2
->authname
)) {
139 newaccount
= target
->authname
;
143 controlreply(np
, "Cannot find anyone with that authname on the network.");
147 target
= getnickbynick(cargv
[0]);
149 controlreply(np
, "Supplied nickname is not on the network.");
152 if(!IsAccount(target
)) {
153 controlreply(np
, "Supplied user is not authed with the network.");
156 newaccount
= target
->authname
;
159 au
= noperserv_get_autheduser(newaccount
);
161 controlreply(np
, "Authname already registered.");
165 au
= noperserv_new_autheduser(newaccount
);
167 controlreply(np
, "Memory allocation error.");
171 if(noperserv_get_autheduser_count() == 1) {
172 au
->authlevel
= NO_FIRST_USER_LEVEL
;
173 au
->noticelevel
= NO_FIRST_USER_DEFAULT_NOTICELEVEL
;
175 au
->authlevel
= NO_DEFAULT_LEVEL
;
176 au
->noticelevel
= NO_DEFAULT_NOTICELEVEL
;
179 au
->id
= noperserv_next_autheduser_id();
180 noperserv_update_autheduser(au
);
182 for(i
=0;i
<NICKHASHSIZE
;i
++)
183 for(np2
=nicktable
[i
];np2
;np2
=np2
->next
)
184 if(IsAccount(np2
) && !ircd_strcmp(newaccount
, np2
->authname
)) {
185 noperserv_add_to_autheduser(np2
, au
);
186 controlreply(np2
, "An account has been created for you (auth %s).", au
->authname
->content
);
187 if(NOGetAuthLevel(au
))
188 controlreply(np2
, "User flags: %s", printflags(NOGetAuthLevel(au
), no_userflags
));
189 controlreply(np2
, "Notice flags: %s", printflags(NOGetNoticeLevel(au
), no_noticeflags
));
192 if(ircd_strcmp(np
->authname
, newaccount
)) { /* send a message to the person who HELLO'ed if we haven't already been told */
193 controlreply(np
, "Account created for auth %s.", au
->authname
->content
);
194 if(NOGetAuthLevel(au
))
195 controlreply(np
, "User flags: %s", printflags(NOGetAuthLevel(au
), no_userflags
));
196 controlreply(np
, "Notice flags: %s", printflags(NOGetNoticeLevel(au
), no_noticeflags
));
197 controlreply(np
, "Instructions sent to all authed users.");
198 } else if(au
->nick
&& au
->nick
->next
) { /* if we have already been told, tell the user it was sent to more than themselves */
199 controlreply(np
, "Instructions sent to all authed users.");
202 controlwall(NO_OPERED
, NL_MANAGEMENT
, "%s/%s just HELLO'ed: %s", np
->nick
, np
->authname
, au
->authname
->content
);
206 no_autheduser
*noperserv_autheduser_from_command(nick
*np
, char *command
, int *typefound
, char **returned
) {
208 if(command
[0] == '#') {
209 au
= noperserv_get_autheduser(command
+ 1);
211 controlreply(np
, "Authname not found.");
213 *typefound
= NO_FOUND_AUTHNAME
;
214 *returned
= au
->authname
->content
;
218 nick
*np2
= getnickbynick(command
);
220 controlreply(np
, "Nickname not on the network.");
223 if(!IsAccount(np2
)) {
224 controlreply(np
, "User is not authed with the network.");
227 au
= NOGetAuthedUser(np2
);
229 controlreply(np
, "User does not have an account.");
231 *typefound
= NO_FOUND_NICKNAME
;
232 *returned
= np2
->nick
;
240 int noperserv_noticeflags(void *sender
, int cargc
, char **cargv
) {
241 nick
*np
= (nick
*)sender
;
245 if((cargv
[0][0] == '+') || (cargv
[0][0] == '-')) {
247 au
= NOGetAuthedUser(np
);
248 flag_t fwas
= NOGetNoticeLevel(au
), permittedchanges
= noperserv_policy_permitted_noticeflags(au
);
250 ret
= setflags(&au
->noticelevel
, permittedchanges
, cargv
[0], no_noticeflags
, REJECT_DISALLOWED
| REJECT_UNKNOWN
);
251 if(ret
!= REJECT_UNKNOWN
) {
252 if(ret
== REJECT_DISALLOWED
) {
254 setflags(&fnow
, NL_ALL
, cargv
[0], no_noticeflags
, REJECT_NONE
);
256 controlreply(np
, "No changes made to existing flags.");
258 char ourflags
[FLAGBUFLEN
], ournoticeflags
[FLAGBUFLEN
];
259 controlreply(np
, "Flag alterations denied.");
261 strlcpy(ourflags
, printflags(NOGetAuthLevel(au
), no_userflags
), sizeof(ourflags
));
262 strlcpy(ournoticeflags
, printflags(NOGetNoticeLevel(au
), no_noticeflags
), sizeof(ournoticeflags
));
263 controlwall(NO_OPER
, NL_MANAGEMENT
, "%s/%s (%s) attempted to NOTICEFLAGS (%s): %s", np
->nick
, np
->authname
, ourflags
, ournoticeflags
, printflagdiff(fwas
, fnow
, no_noticeflags
));
266 } else if(ret
== REJECT_NONE
) {
267 if(NOGetNoticeLevel(au
) == fwas
) {
268 controlreply(np
, "No changes made to existing flags.");
270 char ourflags
[FLAGBUFLEN
], ournoticeflags
[FLAGBUFLEN
], diff
[FLAGBUFLEN
* 2 + 1], finalflags
[FLAGBUFLEN
];
271 no_nicklist
*nl
= au
->nick
;
272 noperserv_update_autheduser(au
);
273 controlreply(np
, "Flag alterations complete.");
275 strlcpy(ourflags
, printflags(NOGetAuthLevel(au
), no_userflags
), sizeof(ourflags
));
276 strlcpy(ournoticeflags
, printflags(fwas
, no_noticeflags
), sizeof(ournoticeflags
));
277 strlcpy(diff
, printflagdiff(fwas
, NOGetNoticeLevel(au
), no_noticeflags
), sizeof(diff
));
278 controlwall(NO_OPER
, NL_MANAGEMENT
, "%s/%s (%s) successfully used NOTICEFLAGS (%s): %s", np
->nick
, np
->authname
, ourflags
, ournoticeflags
, diff
);
280 strlcpy(finalflags
, printflags(NOGetNoticeLevel(au
), no_noticeflags
), sizeof(finalflags
));
283 controlreply(nl
->nick
, "!!! %s just used NOTICEFLAGS (%s): %s", np
->nick
, ournoticeflags
, diff
);
284 controlreply(nl
->nick
, "Your notice flags are %s", finalflags
);
289 controlreply(np
, "Unknown flag(s) supplied.");
295 au
= noperserv_autheduser_from_command(np
, cargv
[0], &typefound
, &itemfound
);
299 if(au
!= NOGetAuthedUser(np
)) {
300 controlreply(np
, "Notice flags for %s %s are: %s", typefound
==NO_FOUND_NICKNAME
?"user":"authname", itemfound
, printflags(NOGetNoticeLevel(au
), no_noticeflags
));
305 au
= NOGetAuthedUser(np
);
308 if(!au
) /* shouldn't happen */
311 controlreply(np
, "Your notice flags are: %s", printflags(NOGetNoticeLevel(au
), no_noticeflags
));
317 int noperserv_deluser(void *sender
, int cargc
, char **cargv
) {
318 nick
*np
= (nick
*)sender
;
319 no_autheduser
*target
/* target user */, *au
= NOGetAuthedUser(np
); /* user executing command */
320 char *userreturned
= NULL
; /* nickname or authname of the target, pulled from the db */
321 int typefound
; /* whether it was an authname or a username */
323 char targetflags
[FLAGBUFLEN
], ourflags
[FLAGBUFLEN
], deleteduser
[NOMax(ACCOUNTLEN
, NICKLEN
) + 1];
328 target
= noperserv_autheduser_from_command(np
, cargv
[0], &typefound
, &userreturned
);
332 strlcpy(targetflags
, printflags(NOGetAuthLevel(target
), no_userflags
), sizeof(targetflags
));
333 strlcpy(ourflags
, printflags(NOGetAuthLevel(au
), no_userflags
), sizeof(ourflags
));
335 /* we have to copy it as it might point to an autheduser, which we're about to delete */
336 strlcpy(deleteduser
, userreturned
, sizeof(deleteduser
));
338 /* we have to check if target != au, because if successful policy_modification_permitted just returns the flags we're allowed
339 to modify, if we have no flags we won't be able to delete ourselves */
340 if((target
!= au
) && !noperserv_policy_permitted_modifications(au
, target
)) {
341 controlreply(np
, "Deletion denied.");
342 controlwall(NO_OPER
, NL_MANAGEMENT
, "%s/%s (%s) attempted to DELUSER %s (%s)", np
->nick
, np
->authname
, ourflags
, target
->authname
->content
, targetflags
);
347 for(nl
=target
->nick
;nl
;nl
=nl
->next
)
349 controlreply(nl
->nick
, "!!! %s/%s (%s) just DELUSERed you.", np
->nick
, np
->authname
, ourflags
);
351 noperserv_delete_autheduser(target
);
353 controlwall(NO_OPER
, NL_MANAGEMENT
, "%s/%s (%s) successfully used DELUSER on %s (%s)", np
->nick
, np
->authname
, ourflags
, target
->authname
->content
, targetflags
);
356 controlreply(np
, "You have been deleted.");
358 controlreply(np
, "%s %s deleted.", typefound
==NO_FOUND_AUTHNAME
?"Auth":"User", deleteduser
);
365 /* this command needs LOTS of checking */
366 int noperserv_userflags(void *sender
, int cargc
, char **cargv
) {
367 nick
*np
= (nick
*)sender
;
368 no_autheduser
*au
= NOGetAuthedUser(np
), *target
= NULL
;
369 char *flags
= NULL
, *nicktarget
= NULL
;
374 } else if(cargc
== 1) {
375 if((cargv
[0][0] == '+') || (cargv
[0][0] == '-')) { /* modify our own */
378 } else { /* viewing someone elses */
379 nicktarget
= cargv
[0];
381 } else if(cargc
== 2) {
382 nicktarget
= cargv
[0];
389 target
= noperserv_autheduser_from_command(np
, nicktarget
, &typefound
, &nicktarget
);
396 flag_t permitted
= noperserv_policy_permitted_modifications(au
, target
), fwas
= NOGetAuthLevel(target
), fours
= NOGetAuthLevel(au
);
398 ret
= setflags(&target
->authlevel
, permitted
, flags
, no_userflags
, REJECT_DISALLOWED
| REJECT_UNKNOWN
);
399 if(ret
!= REJECT_UNKNOWN
) {
400 if(ret
== REJECT_DISALLOWED
) {
402 setflags(&fnow
, NO_ALL_FLAGS
, flags
, no_userflags
, REJECT_NONE
);
404 controlreply(np
, "No changes made to existing flags.");
406 char targetflags
[FLAGBUFLEN
], ourflags
[FLAGBUFLEN
];
407 controlreply(np
, "Flag alterations denied.");
409 strlcpy(targetflags
, printflags(fwas
, no_userflags
), sizeof(targetflags
));
410 strlcpy(ourflags
, printflags(fours
, no_userflags
), sizeof(ourflags
));
412 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
));
415 } else if(ret
== REJECT_NONE
) {
416 if(NOGetAuthLevel(target
) == fwas
) {
417 controlreply(np
, "No changes made to existing flags.");
419 char targetflags
[FLAGBUFLEN
], ourflags
[FLAGBUFLEN
], finalflags
[FLAGBUFLEN
];
420 no_nicklist
*nl
= target
->nick
;
422 noperserv_policy_update_noticeflags(fwas
, target
);
423 noperserv_update_autheduser(target
);
425 controlreply(np
, "Flag alterations complete.");
427 strlcpy(targetflags
, printflags(fwas
, no_userflags
), sizeof(targetflags
));
428 strlcpy(ourflags
, printflags(fours
, no_userflags
), sizeof(ourflags
));
430 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
));
432 strlcpy(finalflags
, printflags(NOGetAuthLevel(target
), no_userflags
), sizeof(finalflags
));
435 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
));
436 controlreply(nl
->nick
, "Your user flags are now: %s", finalflags
);
437 controlreply(nl
->nick
, "Your notice flags are now: %s", printflags(target
->noticelevel
, no_noticeflags
));
442 controlreply(np
, "Unknown flag(s) supplied.");
448 controlreply(np
, "User flags for %s %s: %s", typefound
==NO_FOUND_AUTHNAME
?"auth":"user", nicktarget
, printflags(NOGetAuthLevel(target
), no_userflags
));
449 controlreply(np
, "Notice flags for %s %s: %s", typefound
==NO_FOUND_AUTHNAME
?"auth":"user", nicktarget
, printflags(target
->noticelevel
, no_noticeflags
));
451 controlreply(np
, "Your user flags are: %s", printflags(NOGetAuthLevel(target
), no_userflags
));
452 controlreply(np
, "Your notice flags are: %s", printflags(target
->noticelevel
, no_noticeflags
));
458 void noperserv_oper_detection(int hooknum
, void *arg
) {
459 void **args
= (void **)arg
;
461 char *modestr
= args
[1];
462 flag_t after
= np
->umodes
;
464 setflags(&after
, UMODE_ALL
, modestr
, umodeflags
, REJECT_NONE
);
465 if(np
->umodes
& UMODE_OPER
) {
466 if(!(after
& UMODE_OPER
))
467 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
:"");
469 if(after
& UMODE_OPER
)
470 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
:"");