]> jfr.im git - irc/quakenet/newserv.git/blob - noperserv/noperserv.c
NICK: Add "away" functionality.
[irc/quakenet/newserv.git] / noperserv / noperserv.c
1 /*
2 * NOperserv v0.01
3 *
4 * A replacement for Germania's ageing Operservice2
5 *
6 * Copyright (C) 2005 Chris Porter.
7 */
8
9 #include "../localuser/localuser.h"
10 #include "../lib/irc_string.h"
11 #include "../lib/strlfunc.h"
12 #include "../lib/version.h"
13 #include "../authext/authext.h"
14 #include "noperserv.h"
15 #include "noperserv_db.h"
16 #include "noperserv_hooks.h"
17 #include "noperserv_policy.h"
18
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdarg.h>
22
23 MODULE_VERSION("");
24
25 #define FLAGBUFLEN 100
26
27 #define NO_FOUND_NICKNAME 1
28 #define NO_FOUND_AUTHNAME 2
29
30 const flag no_commandflags[] = {
31 { 'o', __NO_OPER },
32 { 't', __NO_TRUST },
33 { 's', __NO_STAFF },
34 { 'S', __NO_SEC },
35 { 'd', __NO_DEVELOPER },
36 { 'L', __NO_LEGACY },
37 { 'O', __NO_OPERED },
38 { 'r', __NO_AUTHED },
39 { 'R', __NO_ACCOUNT },
40 { 'Y', __NO_RELAY },
41 { '\0', 0 }
42 };
43
44 const flag no_userflags[] = {
45 { 'o', __NO_OPER },
46 { 't', __NO_TRUST },
47 { 's', __NO_STAFF },
48 { 'S', __NO_SEC },
49 { 'd', __NO_DEVELOPER },
50 { 'Y', __NO_RELAY },
51 { '\0', 0 }
52 };
53
54 const flag no_noticeflags[] = {
55 { 'm', NL_MANAGEMENT }, /* hello, password, userflags, noticeflags */
56 { 't', NL_TRUSTS }, /* trust stuff... */
57 { 'k', NL_KICKKILLS }, /* KICK/KILL commands */
58 { 'I', NL_MISC }, /* misc commands */
59 { 'g', NL_GLINES }, /* GLINE commands */
60 { 'h', NL_HITS }, /* Where a gline or kill is set automatically by the bot */
61 { 'c', NL_CLONING }, /* Clone detection */
62 { 'C', NL_CLEARCHAN }, /* When someone clearchans */
63 { 'f', NL_FAKEUSERS }, /* Fakeuser addition */
64 { 'b', NL_BROADCASTS }, /* Broadcast/mbroadcast/sbroadcast */
65 { 'o', NL_OPERATIONS }, /* insmod/rmmod/etc */
66 { 'O', NL_OPERING }, /* when someone opers */
67 { 'n', NL_NOTICES }, /* turn off to receive notices instead of privmsgs */
68 { 'A', NL_ALL_COMMANDS }, /* all commands sent */
69 { '\0', 0 }
70 };
71
72 int noperserv_hello(void *sender, int cargc, char **cargv);
73 int noperserv_noticeflags(void *sender, int cargc, char **cargv);
74 int noperserv_userflags(void *sender, int cargc, char **cargv);
75 int noperserv_deluser(void *sender, int cargc, char **cargv);
76 void noperserv_oper_detection(int hooknum, void *arg);
77 void noperserv_reply(nick *np, char *format, ...) __attribute__ ((format (printf, 2, 3)));
78
79 int init = 0;
80
81 void _init() {
82 if(!noperserv_load_db())
83 return;
84
85 noperserv_ext = registernickext("noperserv");
86
87 noperserv_setup_hooks();
88
89 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.");
90 registercontrolhelpcmd("userflags", NO_ACCOUNT, 2, &noperserv_userflags,
91 "Syntax: USERFLAGS <nickname|#authname> ?modifications?\n"
92 " Views and modifies user permissions.\n"
93 " If no nickname or authname is supplied, you are substituted for it.\n"
94 " If no flags are supplied, flags are just displayed instead of modified."
95 " Flags:\n"
96 " +o: Operator\n"
97 " +s: Staff member\n"
98 " +S: Security team member\n"
99 " +d: NOperserv developer\n"
100 " +t: Trust queue worker\n"
101 " +Y: Relay\n"
102 " Additional flags may show up in SHOWCOMMANDS but are not userflags as such:\n"
103 " +r: Authed user\n"
104 " +R: Registered NOperserv user\n"
105 " +O: Must be /OPER'ed\n"
106 " +L: Legacy command\n"
107 );
108 registercontrolhelpcmd("noticeflags", NO_ACCOUNT, 1, &noperserv_noticeflags,
109 "Syntax: NOTICEFLAGS ?(nickname|#authname)|flags?\n"
110 " This command can view and modify your own notice flags, and view that of other users.\n"
111 " Flags:\n"
112 " +m: Management (hello, password, userflags, noticeflags)\n"
113 " +t: Trusts\n"
114 " +k: KICK/KILL commands\n"
115 " +g: GLINE commands\n"
116 " +h: Shows when glines are played automatically (hits)\n"
117 " +c: Clone information\n"
118 " +C: CLEARCHAN command\n"
119 " +f: FAKEUSER commands\n"
120 " +b: BROADCAST commands\n"
121 " +o: Operation commands, such as insmod, rmmod, die, etc\n"
122 " +O: /OPER\n"
123 " +I: Misc commands (resync, etc)\n"
124 " +n: Sends notices instead of privmsgs\n"
125 " +A: Every single command sent to the service (spammy)\n"
126 );
127
128 registercontrolhelpcmd("deluser", NO_OPERED | NO_ACCOUNT, 2, &noperserv_deluser, "Syntax: DELUSER <nickname|#authname>\nDeletes the specified user.");
129 registerhook(HOOK_NICK_MODEOPER, &noperserv_oper_detection);
130
131 init = 1;
132 }
133
134 #ifdef BROKEN_DLCLOSE
135 void __fini() {
136 #else
137 void _fini() {
138 #endif
139 if(!init)
140 return;
141
142 deregisterhook(HOOK_NICK_MODEOPER, &noperserv_oper_detection);
143
144 deregistercontrolcmd("noticeflags", &noperserv_noticeflags);
145 deregistercontrolcmd("userflags", &noperserv_userflags);
146 deregistercontrolcmd("noticeflags", &noperserv_noticeflags);
147 deregistercontrolcmd("hello", &noperserv_hello);
148 deregistercontrolcmd("deluser", &noperserv_deluser);
149
150 noperserv_cleanup_hooks();
151
152 noperserv_cleanup_db();
153
154 releasenickext(noperserv_ext);
155 }
156
157 /* @test */
158 int noperserv_hello(void *sender, int cargc, char **cargv) {
159 char *newaccount = NULL;
160 no_autheduser *au;
161 int i;
162 nick *np = (nick *)sender, *np2, *target = NULL;
163
164 if(cargc == 0) {
165 newaccount = np->authname;
166 } else {
167 if(cargv[0][0] == '#') {
168 authname *a = getauthbyname(cargv[0] + 1);
169 if(!a) {
170 controlreply(np, "Cannot find anyone with that authname on the network.");
171 return CMD_ERROR;
172 }
173 newaccount = a->name;
174 target = a->nicks;
175 } else {
176 target = getnickbynick(cargv[0]);
177 if(!target) {
178 controlreply(np, "Supplied nickname is not on the network.");
179 return CMD_ERROR;
180 }
181 if(!IsAccount(target)) {
182 controlreply(np, "Supplied user is not authed with the network.");
183 return CMD_ERROR;
184 }
185 newaccount = target->authname;
186 }
187 }
188 au = noperserv_get_autheduser(newaccount);
189 if(au) {
190 controlreply(np, "Authname already registered.");
191 return CMD_ERROR;
192 }
193
194 au = noperserv_new_autheduser(newaccount);
195 if(!au) {
196 controlreply(np, "Memory allocation error.");
197 return CMD_ERROR;
198 }
199
200 if(noperserv_get_autheduser_count() == 1) {
201 au->authlevel = NO_FIRST_USER_LEVEL;
202 au->noticelevel = NO_FIRST_USER_DEFAULT_NOTICELEVEL;
203 } else {
204 au->authlevel = NO_DEFAULT_LEVEL;
205 au->noticelevel = NO_DEFAULT_NOTICELEVEL;
206 }
207
208 au->id = noperserv_next_autheduser_id();
209 noperserv_update_autheduser(au);
210
211 for(i=0;i<NICKHASHSIZE;i++)
212 for(np2=nicktable[i];np2;np2=np2->next)
213 if(IsAccount(np2) && !ircd_strcmp(newaccount, np2->authname)) {
214 noperserv_add_to_autheduser(np2, au);
215 controlreply(np2, "An account has been created for you (auth %s).", au->authname->content);
216 if(NOGetAuthLevel(au))
217 controlreply(np2, "User flags: %s", printflags(NOGetAuthLevel(au), no_userflags));
218 controlreply(np2, "Notice flags: %s", printflags(NOGetNoticeLevel(au), no_noticeflags));
219 }
220
221 if(ircd_strcmp(np->authname, newaccount)) { /* send a message to the person who HELLO'ed if we haven't already been told */
222 controlreply(np, "Account created for auth %s.", au->authname->content);
223 if(NOGetAuthLevel(au))
224 controlreply(np, "User flags: %s", printflags(NOGetAuthLevel(au), no_userflags));
225 controlreply(np, "Notice flags: %s", printflags(NOGetNoticeLevel(au), no_noticeflags));
226 controlreply(np, "Instructions sent to all authed users.");
227 } else if(au->nick && au->nick->next) { /* if we have already been told, tell the user it was sent to more than themselves */
228 controlreply(np, "Instructions sent to all authed users.");
229 }
230
231 controlwall(NO_OPERED, NL_MANAGEMENT, "%s/%s just HELLO'ed: %s", np->nick, np->authname, au->authname->content);
232 return CMD_OK;
233 }
234
235 no_autheduser *noperserv_autheduser_from_command(nick *np, char *command, int *typefound, char **returned) {
236 no_autheduser *au;
237 if(command[0] == '#') {
238 au = noperserv_get_autheduser(command + 1);
239 if(!au) {
240 controlreply(np, "Authname not found.");
241 } else {
242 *typefound = NO_FOUND_AUTHNAME;
243 *returned = au->authname->content;
244 return au;
245 }
246 } else {
247 nick *np2 = getnickbynick(command);
248 if(!np2) {
249 controlreply(np, "Nickname not on the network.");
250 return CMD_OK;
251 }
252 if(!IsAccount(np2)) {
253 controlreply(np, "User is not authed with the network.");
254 return CMD_OK;
255 }
256 au = NOGetAuthedUser(np2);
257 if(!au) {
258 controlreply(np, "User does not have an account.");
259 } else {
260 *typefound = NO_FOUND_NICKNAME;
261 *returned = np2->nick;
262 return au;
263 }
264 }
265
266 return NULL;
267 }
268
269 int noperserv_noticeflags(void *sender, int cargc, char **cargv) {
270 nick *np = (nick *)sender;
271 no_autheduser *au;
272
273 if(cargc == 1) {
274 if((cargv[0][0] == '+') || (cargv[0][0] == '-')) {
275 int ret;
276 au = NOGetAuthedUser(np);
277 flag_t fwas = NOGetNoticeLevel(au), permittedchanges = noperserv_policy_permitted_noticeflags(au);
278
279 ret = setflags(&au->noticelevel, permittedchanges, cargv[0], no_noticeflags, REJECT_DISALLOWED | REJECT_UNKNOWN);
280 if(ret != REJECT_UNKNOWN) {
281 if(ret == REJECT_DISALLOWED) {
282 flag_t fnow = fwas;
283 setflags(&fnow, NL_ALL, cargv[0], no_noticeflags, REJECT_NONE);
284 if(fnow == fwas) {
285 controlreply(np, "No changes made to existing flags.");
286 } else {
287 char ourflags[FLAGBUFLEN], ournoticeflags[FLAGBUFLEN];
288 controlreply(np, "Flag alterations denied.");
289
290 strlcpy(ourflags, printflags(NOGetAuthLevel(au), no_userflags), sizeof(ourflags));
291 strlcpy(ournoticeflags, printflags(NOGetNoticeLevel(au), no_noticeflags), sizeof(ournoticeflags));
292 controlwall(NO_OPER, NL_MANAGEMENT, "%s/%s (%s) attempted to NOTICEFLAGS (%s): %s", np->nick, np->authname, ourflags, ournoticeflags, printflagdiff(fwas, fnow, no_noticeflags));
293 return CMD_ERROR;
294 }
295 } else if(ret == REJECT_NONE) {
296 if(NOGetNoticeLevel(au) == fwas) {
297 controlreply(np, "No changes made to existing flags.");
298 } else {
299 char ourflags[FLAGBUFLEN], ournoticeflags[FLAGBUFLEN], diff[FLAGBUFLEN * 2 + 1], finalflags[FLAGBUFLEN];
300 no_nicklist *nl = au->nick;
301 noperserv_update_autheduser(au);
302 controlreply(np, "Flag alterations complete.");
303
304 strlcpy(ourflags, printflags(NOGetAuthLevel(au), no_userflags), sizeof(ourflags));
305 strlcpy(ournoticeflags, printflags(fwas, no_noticeflags), sizeof(ournoticeflags));
306 strlcpy(diff, printflagdiff(fwas, NOGetNoticeLevel(au), no_noticeflags), sizeof(diff));
307 controlwall(NO_OPER, NL_MANAGEMENT, "%s/%s (%s) successfully used NOTICEFLAGS (%s): %s", np->nick, np->authname, ourflags, ournoticeflags, diff);
308
309 strlcpy(finalflags, printflags(NOGetNoticeLevel(au), no_noticeflags), sizeof(finalflags));
310 for(;nl;nl=nl->next)
311 if(nl->nick != np) {
312 controlreply(nl->nick, "!!! %s just used NOTICEFLAGS (%s): %s", np->nick, ournoticeflags, diff);
313 controlreply(nl->nick, "Your notice flags are %s", finalflags);
314 }
315 }
316 }
317 } else {
318 controlreply(np, "Unknown flag(s) supplied.");
319 return CMD_ERROR;
320 }
321 } else {
322 int typefound;
323 char *itemfound;
324 au = noperserv_autheduser_from_command(np, cargv[0], &typefound, &itemfound);
325 if(!au)
326 return CMD_ERROR;
327
328 if(au != NOGetAuthedUser(np)) {
329 controlreply(np, "Notice flags for %s %s are: %s", typefound==NO_FOUND_NICKNAME?"user":"authname", itemfound, printflags(NOGetNoticeLevel(au), no_noticeflags));
330 return CMD_OK;
331 }
332 }
333 } else {
334 au = NOGetAuthedUser(np);
335 }
336
337 if(!au) /* shouldn't happen */
338 return CMD_ERROR;
339
340 controlreply(np, "Your notice flags are: %s", printflags(NOGetNoticeLevel(au), no_noticeflags));
341
342 return CMD_OK;
343 }
344
345 /* @test */
346 int noperserv_deluser(void *sender, int cargc, char **cargv) {
347 nick *np = (nick *)sender;
348 no_autheduser *target /* target user */, *au = NOGetAuthedUser(np); /* user executing command */
349 char *userreturned = NULL; /* nickname or authname of the target, pulled from the db */
350 int typefound; /* whether it was an authname or a username */
351 no_nicklist *nl;
352 char targetflags[FLAGBUFLEN], ourflags[FLAGBUFLEN], deleteduser[NOMax(ACCOUNTLEN, NICKLEN) + 1];
353
354 if(cargc != 1)
355 return CMD_USAGE;
356
357 target = noperserv_autheduser_from_command(np, cargv[0], &typefound, &userreturned);
358 if(!target)
359 return CMD_ERROR;
360
361 strlcpy(targetflags, printflags(NOGetAuthLevel(target), no_userflags), sizeof(targetflags));
362 strlcpy(ourflags, printflags(NOGetAuthLevel(au), no_userflags), sizeof(ourflags));
363
364 /* we have to copy it as it might point to an autheduser, which we're about to delete */
365 strlcpy(deleteduser, userreturned, sizeof(deleteduser));
366
367 /* we have to check if target != au, because if successful policy_modification_permitted just returns the flags we're allowed
368 to modify, if we have no flags we won't be able to delete ourselves */
369 if((target != au) && !noperserv_policy_permitted_modifications(au, target)) {
370 controlreply(np, "Deletion denied.");
371 controlwall(NO_OPER, NL_MANAGEMENT, "%s/%s (%s) attempted to DELUSER %s (%s)", np->nick, np->authname, ourflags, target->authname->content, targetflags);
372
373 return CMD_ERROR;
374 }
375
376 for(nl=target->nick;nl;nl=nl->next)
377 if(nl->nick != np)
378 controlreply(nl->nick, "!!! %s/%s (%s) just DELUSERed you.", np->nick, np->authname, ourflags);
379
380 noperserv_delete_autheduser(target);
381
382 controlwall(NO_OPER, NL_MANAGEMENT, "%s/%s (%s) successfully used DELUSER on %s (%s)", np->nick, np->authname, ourflags, target->authname->content, targetflags);
383
384 if(target == au) {
385 controlreply(np, "You have been deleted.");
386 } else {
387 controlreply(np, "%s %s deleted.", typefound==NO_FOUND_AUTHNAME?"Auth":"User", deleteduser);
388 }
389
390 return CMD_OK;
391 }
392
393 /* @test */
394 /* this command needs LOTS of checking */
395 int noperserv_userflags(void *sender, int cargc, char **cargv) {
396 nick *np = (nick *)sender;
397 no_autheduser *au = NOGetAuthedUser(np), *target = NULL;
398 char *flags = NULL, *nicktarget = NULL;
399 int typefound;
400
401 if(cargc == 0) {
402 target = au;
403 } else if(cargc == 1) {
404 if((cargv[0][0] == '+') || (cargv[0][0] == '-')) { /* modify our own */
405 flags = cargv[0];
406 target = au;
407 } else { /* viewing someone elses */
408 nicktarget = cargv[0];
409 }
410 } else if(cargc == 2) {
411 nicktarget = cargv[0];
412 flags = cargv[1];
413 } else {
414 return CMD_USAGE;
415 }
416
417 if(nicktarget) {
418 target = noperserv_autheduser_from_command(np, nicktarget, &typefound, &nicktarget);
419 if(!target)
420 return CMD_ERROR;
421 }
422
423 if(flags) {
424 int ret;
425 flag_t permitted = noperserv_policy_permitted_modifications(au, target), fwas = NOGetAuthLevel(target), fours = NOGetAuthLevel(au);
426
427 ret = setflags(&target->authlevel, permitted, flags, no_userflags, REJECT_DISALLOWED | REJECT_UNKNOWN);
428 if(ret != REJECT_UNKNOWN) {
429 if(ret == REJECT_DISALLOWED) {
430 flag_t fnow = fwas;
431 setflags(&fnow, NO_ALL_FLAGS, flags, no_userflags, REJECT_NONE);
432 if(fnow == fwas) {
433 controlreply(np, "No changes made to existing flags.");
434 } else {
435 char targetflags[FLAGBUFLEN], ourflags[FLAGBUFLEN];
436 controlreply(np, "Flag alterations denied.");
437
438 strlcpy(targetflags, printflags(fwas, no_userflags), sizeof(targetflags));
439 strlcpy(ourflags, printflags(fours, no_userflags), sizeof(ourflags));
440
441 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));
442 return CMD_ERROR;
443 }
444 } else if(ret == REJECT_NONE) {
445 if(NOGetAuthLevel(target) == fwas) {
446 controlreply(np, "No changes made to existing flags.");
447 } else {
448 char targetflags[FLAGBUFLEN], ourflags[FLAGBUFLEN], finalflags[FLAGBUFLEN];
449 no_nicklist *nl = target->nick;
450
451 noperserv_policy_update_noticeflags(fwas, target);
452 noperserv_update_autheduser(target);
453
454 controlreply(np, "Flag alterations complete.");
455
456 strlcpy(targetflags, printflags(fwas, no_userflags), sizeof(targetflags));
457 strlcpy(ourflags, printflags(fours, no_userflags), sizeof(ourflags));
458
459 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));
460
461 strlcpy(finalflags, printflags(NOGetAuthLevel(target), no_userflags), sizeof(finalflags));
462 for(;nl;nl=nl->next)
463 if(nl->nick != np) {
464 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));
465 controlreply(nl->nick, "Your user flags are now: %s", finalflags);
466 controlreply(nl->nick, "Your notice flags are now: %s", printflags(target->noticelevel, no_noticeflags));
467 }
468 }
469 }
470 } else {
471 controlreply(np, "Unknown flag(s) supplied.");
472 return CMD_ERROR;
473 }
474 }
475
476 if(target != au) {
477 controlreply(np, "User flags for %s %s: %s", typefound==NO_FOUND_AUTHNAME?"auth":"user", nicktarget, printflags(NOGetAuthLevel(target), no_userflags));
478 controlreply(np, "Notice flags for %s %s: %s", typefound==NO_FOUND_AUTHNAME?"auth":"user", nicktarget, printflags(target->noticelevel, no_noticeflags));
479 } else {
480 controlreply(np, "Your user flags are: %s", printflags(NOGetAuthLevel(target), no_userflags));
481 controlreply(np, "Your notice flags are: %s", printflags(target->noticelevel, no_noticeflags));
482 }
483
484 return CMD_OK;
485 }
486
487 void noperserv_oper_detection(int hooknum, void *arg) {
488 nick *np = (nick *)arg;
489
490 if(np->umodes & UMODE_OPER) {
491 if(np->opername && strcmp(np->opername->content, "-")) {
492 controlwall(NO_OPER, NL_OPERING, "%s!%s@%s%s%s just OPERed as %s", np->nick, np->ident, np->host->name->content, IsAccount(np)?"/":"", IsAccount(np)?np->authname:"", np->opername->content);
493 } else {
494 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:"");
495 }
496 } else {
497 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:"");
498 }
499 }