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
, ...);
75 if(!noperserv_load_db())
78 noperserv_ext
= registernickext("noperserv");
80 noperserv_setup_hooks();
82 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.");
83 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.");
84 registercontrolhelpcmd("noticeflags", NO_ACCOUNT
, 1, &noperserv_noticeflags
,
85 "Syntax: NOTICEFLAGS ?(nickname|#authname)|flags?\n"
86 " This command can view and modify your own notice flags, and view that of other users.\n"
88 " +m: Management (hello, password, userflags, noticeflags)\n"
92 " +g: GLINE commands\n"
93 " +h: Shows when glines are played automatically (hits)\n"
94 " +c: Clone information\n"
95 " +C: CLEARCHAN command\n"
96 " +f: FAKEUSER command\n"
97 " +b: BROADCAST commands\n"
98 " +o: Operation commands, such as insmod, rmmod, die, etc\n"
100 " +n: Sends notices instead of privmsgs\n"
101 " +A: Every single command sent to the service\n"
104 registercontrolhelpcmd("deluser", NO_OPERED
| NO_ACCOUNT
, 2, &noperserv_deluser
, "Syntax: DELUSER <nickname|#authname>\nDeletes the specified user.");
105 registerhook(HOOK_NICK_MODEOPER
, &noperserv_oper_detection
);
110 #ifdef BROKEN_DLCLOSE
118 deregisterhook(HOOK_NICK_MODEOPER
, &noperserv_oper_detection
);
120 deregistercontrolcmd("noticeflags", &noperserv_noticeflags
);
121 deregistercontrolcmd("userflags", &noperserv_userflags
);
122 deregistercontrolcmd("noticeflags", &noperserv_noticeflags
);
124 noperserv_cleanup_hooks();
126 noperserv_cleanup_db();
128 releasenickext(noperserv_ext
);
132 int noperserv_hello(void *sender
, int cargc
, char **cargv
) {
136 nick
*np
= (nick
*)sender
, *np2
, *target
= NULL
;
139 newaccount
= np
->authname
;
141 if(cargv
[0][0] == '#') {
143 for(i
=0;i
<NICKHASHSIZE
;i
++)
144 for(np2
=nicktable
[i
];np2
;np2
=np2
->next
)
145 if(IsAccount(np2
) && !ircd_strcmp(cargv
[0] + 1, np2
->authname
)) {
147 newaccount
= target
->authname
;
151 controlreply(np
, "Cannot find anyone with that authname on the network.");
155 target
= getnickbynick(cargv
[0]);
157 controlreply(np
, "Supplied nickname is not on the network.");
160 if(!IsAccount(target
)) {
161 controlreply(np
, "Supplied user is not authed with the network.");
164 newaccount
= target
->authname
;
167 au
= noperserv_get_autheduser(newaccount
);
169 controlreply(np
, "Authname already registered.");
173 au
= noperserv_new_autheduser(newaccount
);
175 controlreply(np
, "Memory allocation error.");
179 if(noperserv_get_autheduser_count() == 1) {
180 au
->authlevel
= NO_FIRST_USER_LEVEL
;
181 au
->noticelevel
= NO_FIRST_USER_DEFAULT_NOTICELEVEL
;
183 au
->authlevel
= NO_DEFAULT_LEVEL
;
184 au
->noticelevel
= NO_DEFAULT_NOTICELEVEL
;
187 au
->id
= noperserv_next_autheduser_id();
188 noperserv_update_autheduser(au
);
190 for(i
=0;i
<NICKHASHSIZE
;i
++)
191 for(np2
=nicktable
[i
];np2
;np2
=np2
->next
)
192 if(IsAccount(np2
) && !ircd_strcmp(newaccount
, np2
->authname
)) {
193 noperserv_add_to_autheduser(np2
, au
);
194 controlreply(np2
, "An account has been created for you (auth %s).", au
->authname
->content
);
195 if(NOGetAuthLevel(au
))
196 controlreply(np2
, "User flags: %s", printflags(NOGetAuthLevel(au
), no_userflags
));
197 controlreply(np2
, "Notice flags: %s", printflags(NOGetNoticeLevel(au
), no_noticeflags
));
200 if(ircd_strcmp(np
->authname
, newaccount
)) { /* send a message to the person who HELLO'ed if we haven't already been told */
201 controlreply(np
, "Account created for auth %s.", au
->authname
->content
);
202 if(NOGetAuthLevel(au
))
203 controlreply(np
, "User flags: %s", printflags(NOGetAuthLevel(au
), no_userflags
));
204 controlreply(np
, "Notice flags: %s", printflags(NOGetNoticeLevel(au
), no_noticeflags
));
205 controlreply(np
, "Instructions sent to all authed users.");
206 } else if(au
->nick
&& au
->nick
->next
) { /* if we have already been told, tell the user it was sent to more than themselves */
207 controlreply(np
, "Instructions sent to all authed users.");
210 controlwall(NO_OPERED
, NL_MANAGEMENT
, "%s/%s just HELLO'ed: %s", np
->nick
, np
->authname
, au
->authname
->content
);
214 no_autheduser
*noperserv_autheduser_from_command(nick
*np
, char *command
, int *typefound
, char **returned
) {
216 if(command
[0] == '#') {
217 au
= noperserv_get_autheduser(command
+ 1);
219 controlreply(np
, "Authname not found.");
221 *typefound
= NO_FOUND_AUTHNAME
;
222 *returned
= au
->authname
->content
;
226 nick
*np2
= getnickbynick(command
);
228 controlreply(np
, "Nickname not on the network.");
231 if(!IsAccount(np2
)) {
232 controlreply(np
, "User is not authed with the network.");
235 au
= NOGetAuthedUser(np2
);
237 controlreply(np
, "User does not have an account.");
239 *typefound
= NO_FOUND_NICKNAME
;
240 *returned
= np2
->nick
;
248 int noperserv_noticeflags(void *sender
, int cargc
, char **cargv
) {
249 nick
*np
= (nick
*)sender
;
253 if((cargv
[0][0] == '+') || (cargv
[0][0] == '-')) {
255 au
= NOGetAuthedUser(np
);
256 flag_t fwas
= NOGetNoticeLevel(au
), permittedchanges
= noperserv_policy_permitted_noticeflags(au
);
258 ret
= setflags(&au
->noticelevel
, permittedchanges
, cargv
[0], no_noticeflags
, REJECT_DISALLOWED
| REJECT_UNKNOWN
);
259 if(ret
!= REJECT_UNKNOWN
) {
260 if(ret
== REJECT_DISALLOWED
) {
262 setflags(&fnow
, NL_ALL
, cargv
[0], no_noticeflags
, REJECT_NONE
);
264 controlreply(np
, "No changes made to existing flags.");
266 char ourflags
[FLAGBUFLEN
], ournoticeflags
[FLAGBUFLEN
];
267 controlreply(np
, "Flag alterations denied.");
269 strlcpy(ourflags
, printflags(NOGetAuthLevel(au
), no_userflags
), sizeof(ourflags
));
270 strlcpy(ournoticeflags
, printflags(NOGetNoticeLevel(au
), no_noticeflags
), sizeof(ournoticeflags
));
271 controlwall(NO_OPER
, NL_MANAGEMENT
, "%s/%s (%s) attempted to NOTICEFLAGS (%s): %s", np
->nick
, np
->authname
, ourflags
, ournoticeflags
, printflagdiff(fwas
, fnow
, no_noticeflags
));
274 } else if(ret
== REJECT_NONE
) {
275 if(NOGetNoticeLevel(au
) == fwas
) {
276 controlreply(np
, "No changes made to existing flags.");
278 char ourflags
[FLAGBUFLEN
], ournoticeflags
[FLAGBUFLEN
], diff
[FLAGBUFLEN
* 2 + 1], finalflags
[FLAGBUFLEN
];
279 no_nicklist
*nl
= au
->nick
;
280 noperserv_update_autheduser(au
);
281 controlreply(np
, "Flag alterations complete.");
283 strlcpy(ourflags
, printflags(NOGetAuthLevel(au
), no_userflags
), sizeof(ourflags
));
284 strlcpy(ournoticeflags
, printflags(fwas
, no_noticeflags
), sizeof(ournoticeflags
));
285 strlcpy(diff
, printflagdiff(fwas
, NOGetNoticeLevel(au
), no_noticeflags
), sizeof(diff
));
286 controlwall(NO_OPER
, NL_MANAGEMENT
, "%s/%s (%s) successfully used NOTICEFLAGS (%s): %s", np
->nick
, np
->authname
, ourflags
, ournoticeflags
, diff
);
288 strlcpy(finalflags
, printflags(NOGetNoticeLevel(au
), no_noticeflags
), sizeof(finalflags
));
291 controlreply(nl
->nick
, "!!! %s just used NOTICEFLAGS (%s): %s", np
->nick
, ournoticeflags
, diff
);
292 controlreply(nl
->nick
, "Your notice flags are %s", finalflags
);
297 controlreply(np
, "Unknown flag(s) supplied.");
303 au
= noperserv_autheduser_from_command(np
, cargv
[0], &typefound
, &itemfound
);
307 if(au
!= NOGetAuthedUser(np
)) {
308 controlreply(np
, "Notice flags for %s %s are: %s", typefound
==NO_FOUND_NICKNAME
?"user":"authname", itemfound
, printflags(NOGetNoticeLevel(au
), no_noticeflags
));
313 au
= NOGetAuthedUser(np
);
316 if(!au
) /* shouldn't happen */
319 controlreply(np
, "Your notice flags are: %s", printflags(NOGetNoticeLevel(au
), no_noticeflags
));
325 int noperserv_deluser(void *sender
, int cargc
, char **cargv
) {
326 nick
*np
= (nick
*)sender
;
327 no_autheduser
*target
/* target user */, *au
= NOGetAuthedUser(np
); /* user executing command */
328 char *userreturned
= NULL
; /* nickname or authname of the target, pulled from the db */
329 int typefound
; /* whether it was an authname or a username */
331 char targetflags
[FLAGBUFLEN
], ourflags
[FLAGBUFLEN
], deleteduser
[NOMax(ACCOUNTLEN
, NICKLEN
) + 1];
336 target
= noperserv_autheduser_from_command(np
, cargv
[0], &typefound
, &userreturned
);
340 strlcpy(targetflags
, printflags(NOGetAuthLevel(target
), no_userflags
), sizeof(targetflags
));
341 strlcpy(ourflags
, printflags(NOGetAuthLevel(au
), no_userflags
), sizeof(ourflags
));
343 /* we have to copy it as it might point to an autheduser, which we're about to delete */
344 strlcpy(deleteduser
, userreturned
, sizeof(deleteduser
));
346 /* we have to check if target != au, because if successful policy_modification_permitted just returns the flags we're allowed
347 to modify, if we have no flags we won't be able to delete ourselves */
348 if((target
!= au
) && !noperserv_policy_permitted_modifications(au
, target
)) {
349 controlreply(np
, "Deletion denied.");
350 controlwall(NO_OPER
, NL_MANAGEMENT
, "%s/%s (%s) attempted to DELUSER %s (%s)", np
->nick
, np
->authname
, ourflags
, target
->authname
->content
, targetflags
);
355 for(nl
=target
->nick
;nl
;nl
=nl
->next
)
357 controlreply(nl
->nick
, "!!! %s/%s (%s) just DELUSERed you.", np
->nick
, np
->authname
, ourflags
);
359 noperserv_delete_autheduser(target
);
361 controlwall(NO_OPER
, NL_MANAGEMENT
, "%s/%s (%s) successfully used DELUSER on %s (%s)", np
->nick
, np
->authname
, ourflags
, target
->authname
->content
, targetflags
);
364 controlreply(np
, "You have been deleted.");
366 controlreply(np
, "%s %s deleted.", typefound
==NO_FOUND_AUTHNAME
?"Auth":"User", deleteduser
);
373 /* this command needs LOTS of checking */
374 int noperserv_userflags(void *sender
, int cargc
, char **cargv
) {
375 nick
*np
= (nick
*)sender
;
376 no_autheduser
*au
= NOGetAuthedUser(np
), *target
= NULL
;
377 char *flags
= NULL
, *nicktarget
= NULL
;
382 } else if(cargc
== 1) {
383 if((cargv
[0][0] == '+') || (cargv
[0][0] == '-')) { /* modify our own */
386 } else { /* viewing someone elses */
387 nicktarget
= cargv
[0];
389 } else if(cargc
== 2) {
390 nicktarget
= cargv
[0];
397 target
= noperserv_autheduser_from_command(np
, nicktarget
, &typefound
, &nicktarget
);
404 flag_t permitted
= noperserv_policy_permitted_modifications(au
, target
), fwas
= NOGetAuthLevel(target
), fours
= NOGetAuthLevel(au
);
406 ret
= setflags(&target
->authlevel
, permitted
, flags
, no_userflags
, REJECT_DISALLOWED
| REJECT_UNKNOWN
);
407 if(ret
!= REJECT_UNKNOWN
) {
408 if(ret
== REJECT_DISALLOWED
) {
410 setflags(&fnow
, NO_ALL_FLAGS
, flags
, no_userflags
, REJECT_NONE
);
412 controlreply(np
, "No changes made to existing flags.");
414 char targetflags
[FLAGBUFLEN
], ourflags
[FLAGBUFLEN
];
415 controlreply(np
, "Flag alterations denied.");
417 strlcpy(targetflags
, printflags(fwas
, no_userflags
), sizeof(targetflags
));
418 strlcpy(ourflags
, printflags(fours
, no_userflags
), sizeof(ourflags
));
420 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
));
423 } else if(ret
== REJECT_NONE
) {
424 if(NOGetAuthLevel(target
) == fwas
) {
425 controlreply(np
, "No changes made to existing flags.");
427 char targetflags
[FLAGBUFLEN
], ourflags
[FLAGBUFLEN
], finalflags
[FLAGBUFLEN
];
428 no_nicklist
*nl
= target
->nick
;
430 noperserv_policy_update_noticeflags(fwas
, target
);
431 noperserv_update_autheduser(target
);
433 controlreply(np
, "Flag alterations complete.");
435 strlcpy(targetflags
, printflags(fwas
, no_userflags
), sizeof(targetflags
));
436 strlcpy(ourflags
, printflags(fours
, no_userflags
), sizeof(ourflags
));
438 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
));
440 strlcpy(finalflags
, printflags(NOGetAuthLevel(target
), no_userflags
), sizeof(finalflags
));
443 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
));
444 controlreply(nl
->nick
, "Your user flags are now: %s", finalflags
);
445 controlreply(nl
->nick
, "Your notice flags are now: %s", printflags(target
->noticelevel
, no_noticeflags
));
450 controlreply(np
, "Unknown flag(s) supplied.");
456 controlreply(np
, "User flags for %s %s: %s", typefound
==NO_FOUND_AUTHNAME
?"auth":"user", nicktarget
, printflags(NOGetAuthLevel(target
), no_userflags
));
457 controlreply(np
, "Notice flags for %s %s: %s", typefound
==NO_FOUND_AUTHNAME
?"auth":"user", nicktarget
, printflags(target
->noticelevel
, no_noticeflags
));
459 controlreply(np
, "Your user flags are: %s", printflags(NOGetAuthLevel(target
), no_userflags
));
460 controlreply(np
, "Your notice flags are: %s", printflags(target
->noticelevel
, no_noticeflags
));
466 void noperserv_oper_detection(int hooknum
, void *arg
) {
467 void **args
= (void **)arg
;
469 char *modestr
= args
[1];
470 flag_t after
= np
->umodes
;
472 setflags(&after
, UMODE_ALL
, modestr
, umodeflags
, REJECT_NONE
);
473 if(np
->umodes
& UMODE_OPER
) {
474 if(!(after
& UMODE_OPER
))
475 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
:"");
477 if(after
& UMODE_OPER
)
478 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
:"");