I was checking something in serv_user.c and just couldn't help myself -- style cleanup
[citadel.git] / citadel / modules / ctdlproto / serv_user.c
1 // Server functions which perform operations on user objects.
2 //
3 // Copyright (c) 1987-2022 by the citadel.org team
4 //
5 // This program is open source software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License, version 3.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12
13
14 #include "support.h"
15 #include "control.h"
16 #include "ctdl_module.h"
17 #include "citserver.h"
18 #include "config.h"
19 #include "user_ops.h"
20 #include "internet_addressing.h"
21
22
23 // USER command -- attempt to log in as an existing user
24 void cmd_user(char *cmdbuf) {
25         char username[256];
26         int a;
27
28         extract_token(username, cmdbuf, 0, '|', sizeof username);
29         striplt(username);
30         syslog(LOG_DEBUG, "user_ops: cmd_user(%s)", username);
31
32         a = CtdlLoginExistingUser(username);
33         switch (a) {
34         case login_already_logged_in:
35                 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
36                 return;
37         case login_too_many_users:
38                 cprintf("%d %s: "
39                         "Too many users are already online "
40                         "(maximum is %d)\n",
41                         ERROR + MAX_SESSIONS_EXCEEDED,
42                         CtdlGetConfigStr("c_nodename"), CtdlGetConfigInt("c_maxsessions"));
43                 return;
44         case login_ok:
45                 cprintf("%d Password required for %s\n", MORE_DATA, CC->curr_user);
46                 return;
47         case login_not_found:
48                 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER, username);
49                 return;
50         default:
51                 cprintf("%d Internal error\n", ERROR + INTERNAL_ERROR);
52         }
53 }
54
55
56 // PASS command -- complete logging in as an existing user (used after USER returns MORE_DATA)
57 void cmd_pass(char *buf) {
58         char password[SIZ];
59         int a;
60         long len;
61
62         memset(password, 0, sizeof(password));
63         len = extract_token(password, buf, 0, '|', sizeof password);
64         a = CtdlTryPassword(password, len);
65
66         switch (a) {
67         case pass_already_logged_in:
68                 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
69                 return;
70         case pass_no_user:
71                 cprintf("%d You must send a name with USER first.\n",
72                         ERROR + USERNAME_REQUIRED);
73                 return;
74         case pass_wrong_password:
75                 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
76                 return;
77         case pass_ok:
78                 logged_in_response();
79                 return;
80         }
81 }
82
83
84 // cmd_newu()  -  create a new user account and log in as that user
85 void cmd_newu(char *cmdbuf) {
86         int a;
87         char username[SIZ];
88
89         if (CtdlGetConfigInt("c_auth_mode") != AUTHMODE_NATIVE) {
90                 cprintf("%d This system does not use native mode authentication.\n",
91                         ERROR + NOT_HERE);
92                 return;
93         }
94
95         if (CtdlGetConfigInt("c_disable_newu")) {
96                 cprintf("%d Self-service user account creation is disabled on this system.\n", ERROR + NOT_HERE);
97                 return;
98         }
99
100         if (CC->logged_in) {
101                 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
102                 return;
103         }
104         if (CC->nologin) {
105                 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
106                         ERROR + MAX_SESSIONS_EXCEEDED,
107                         CtdlGetConfigStr("c_nodename"), CtdlGetConfigInt("c_maxsessions"));
108                 return;
109         }
110         extract_token(username, cmdbuf, 0, '|', sizeof username);
111         strproc(username);
112
113         if (IsEmptyStr(username)) {
114                 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
115                 return;
116         }
117
118         if ((!strcasecmp(username, "bbs")) ||
119             (!strcasecmp(username, "new")) ||
120             (!strcasecmp(username, "."))
121         ) {
122                 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
123                 return;
124         }
125
126         a = create_user(username, CREATE_USER_BECOME_USER, NATIVE_AUTH_UID);
127
128         if (a == 0) {
129                 logged_in_response();
130         }
131         else if (a == ERROR + ALREADY_EXISTS) {
132                 cprintf("%d '%s' already exists.\n",
133                         ERROR + ALREADY_EXISTS, username);
134                 return;
135         }
136         else if (a == ERROR + INTERNAL_ERROR) {
137                 cprintf("%d Internal error - user record disappeared?\n",
138                         ERROR + INTERNAL_ERROR);
139                 return;
140         }
141         else {
142                 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
143         }
144 }
145
146
147 // set password - citadel protocol implementation
148 void cmd_setp(char *new_pw) {
149         if (CtdlAccessCheck(ac_logged_in)) {
150                 return;
151         }
152         if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
153                 cprintf("%d Not allowed.  Use the 'passwd' command.\n", ERROR + NOT_HERE);
154                 return;
155         }
156
157         if (!strcasecmp(new_pw, "GENERATE_RANDOM_PASSWORD")) {
158                 char random_password[17];
159                 snprintf(random_password, sizeof random_password, "%08lx%08lx", random(), random());
160                 CtdlSetPassword(random_password);
161                 cprintf("%d %s\n", CIT_OK, random_password);
162         }
163         else {
164                 strproc(new_pw);
165                 if (IsEmptyStr(new_pw)) {
166                         cprintf("%d Password unchanged.\n", CIT_OK);
167                         return;
168                 }
169                 CtdlSetPassword(new_pw);
170                 cprintf("%d Password changed.\n", CIT_OK);
171         }
172 }
173
174
175 // cmd_creu() - administratively create a new user account (do not log in to it)
176 void cmd_creu(char *cmdbuf) {
177         int a;
178         char username[SIZ];
179         char password[SIZ];
180         struct ctdluser tmp;
181
182         if (CtdlAccessCheck(ac_aide)) {
183                 return;
184         }
185
186         extract_token(username, cmdbuf, 0, '|', sizeof username);
187         strproc(username);
188         strproc(password);
189         if (IsEmptyStr(username)) {
190                 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
191                 return;
192         }
193
194         extract_token(password, cmdbuf, 1, '|', sizeof password);
195
196         a = create_user(username, CREATE_USER_DO_NOT_BECOME_USER, NATIVE_AUTH_UID);
197
198         if (a == 0) {
199                 if (!IsEmptyStr(password)) {
200                         CtdlGetUserLock(&tmp, username);
201                         safestrncpy(tmp.password, password, sizeof(tmp.password));
202                         CtdlPutUserLock(&tmp);
203                 }
204                 cprintf("%d User '%s' created %s.\n", CIT_OK, username, (!IsEmptyStr(password)) ? "and password set" : "with no password");
205                 return;
206         }
207         else if (a == ERROR + ALREADY_EXISTS) {
208                 cprintf("%d '%s' already exists.\n", ERROR + ALREADY_EXISTS, username);
209                 return;
210         }
211         else if ( (CtdlGetConfigInt("c_auth_mode") != AUTHMODE_NATIVE) && (a == ERROR + NO_SUCH_USER) ) {
212                 cprintf("%d User accounts are not created within Citadel in host authentication mode.\n", ERROR + NO_SUCH_USER);
213                 return;
214         }
215         else {
216                 cprintf("%d An error occurred creating the user account.\n", ERROR + INTERNAL_ERROR);
217         }
218 }
219
220
221 // get user parameters
222 void cmd_getu(char *cmdbuf) {
223         if (CtdlAccessCheck(ac_logged_in)) {
224                 return;
225         }
226
227         CtdlGetUser(&CC->user, CC->curr_user);
228         cprintf("%d 80|24|%d|\n", CIT_OK, (CC->user.flags & US_USER_SET));
229 }
230
231
232 // set user parameters
233 void cmd_setu(char *new_parms) {
234         if (CtdlAccessCheck(ac_logged_in)) {
235                 return;
236         }
237
238         if (num_parms(new_parms) < 3) {
239                 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
240                 return;
241         }
242         CtdlLockGetCurrentUser();
243         CC->user.flags = CC->user.flags & (~US_USER_SET);
244         CC->user.flags = CC->user.flags | (extract_int(new_parms, 2) & US_USER_SET);
245         CtdlPutCurrentUserLock();
246         cprintf("%d Ok\n", CIT_OK);
247 }
248
249
250 // set last read pointer (marks all messages in the current room as read, up to the specified point)
251 void cmd_slrp(char *new_ptr) {
252         long newlr;
253         visit vbuf;
254         visit original_vbuf;
255
256         if (CtdlAccessCheck(ac_logged_in)) {
257                 return;
258         }
259
260         if (!strncasecmp(new_ptr, "highest", 7)) {
261                 newlr = CC->room.QRhighest;
262         }
263         else {
264                 newlr = atol(new_ptr);
265         }
266
267         CtdlLockGetCurrentUser();
268
269         CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
270         memcpy(&original_vbuf, &vbuf, sizeof(visit));
271         vbuf.v_lastseen = newlr;
272         snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
273
274         // Only rewrite the record if it changed
275         if (    (vbuf.v_lastseen != original_vbuf.v_lastseen)
276                 || (strcmp(vbuf.v_seen, original_vbuf.v_seen))
277         ) {
278                 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
279         }
280
281         CtdlPutCurrentUserLock();
282         cprintf("%d %ld\n", CIT_OK, newlr);
283 }
284
285
286 void cmd_seen(char *argbuf) {
287         long target_msgnum = 0L;
288         int target_setting = 0;
289
290         if (CtdlAccessCheck(ac_logged_in)) {
291                 return;
292         }
293
294         if (num_parms(argbuf) != 2) {
295                 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
296                 return;
297         }
298
299         target_msgnum = extract_long(argbuf, 0);
300         target_setting = extract_int(argbuf, 1);
301
302         CtdlSetSeen(&target_msgnum, 1, target_setting,
303                         ctdlsetseen_seen, NULL, NULL);
304         cprintf("%d OK\n", CIT_OK);
305 }
306
307
308 void cmd_gtsn(char *argbuf) {
309         visit vbuf;
310
311         if (CtdlAccessCheck(ac_logged_in)) {
312                 return;
313         }
314
315         // Learn about the user and room in question
316         CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
317
318         cprintf("%d ", CIT_OK);
319         client_write(vbuf.v_seen, strlen(vbuf.v_seen));
320         client_write(HKEY("\n"));
321 }
322
323
324 // INVT and KICK commands (grant/revoke access to an invitation-only room)
325 void cmd_invt_kick(char *iuser, int op) {
326
327         // These commands are only allowed by admins, room admins,
328         // and room namespace owners
329         if (is_room_aide()) {
330                 // access granted
331         }
332         else if ( ((atol(CC->room.QRname) == CC->user.usernum) ) && (CC->user.usernum != 0) ) {
333                 // access granted
334         }
335         else {
336                 // access denied
337                 cprintf("%d Higher access or room ownership required.\n", ERROR + HIGHER_ACCESS_REQUIRED);
338                 return;
339         }
340
341         if (!strncasecmp(CC->room.QRname, CtdlGetConfigStr("c_baseroom"), ROOMNAMELEN)) {
342                 cprintf("%d Can't add/remove users from this room.\n", ERROR + NOT_HERE);
343                 return;
344         }
345
346         if (CtdlInvtKick(iuser, op) != 0) {
347                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
348                 return;
349         }
350
351         cprintf("%d %s %s %s.\n",
352                 CIT_OK, iuser,
353                 ((op == 1) ? "invited to" : "kicked out of"),
354                 CC->room.QRname);
355         return;
356 }
357
358
359 void cmd_invt(char *iuser) {
360         cmd_invt_kick(iuser, 1);
361 }
362
363
364 void cmd_kick(char *iuser) {
365         cmd_invt_kick(iuser, 0);
366 }
367
368
369 // forget (Zap) the current room
370 void cmd_forg(char *argbuf) {
371
372         if (CtdlAccessCheck(ac_logged_in)) {
373                 return;
374         }
375
376         if (CtdlForgetThisRoom() == 0) {
377                 cprintf("%d Ok\n", CIT_OK);
378         }
379         else {
380                 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
381         }
382 }
383
384
385 // Get Next Unregistered User
386 void cmd_gnur(char *argbuf) {
387         struct cdbdata *cdbus;
388         struct ctdluser usbuf;
389
390         if (CtdlAccessCheck(ac_aide)) {
391                 return;
392         }
393
394         if ((CtdlGetConfigInt("MMflags") & MM_VALID) == 0) {
395                 cprintf("%d There are no unvalidated users.\n", CIT_OK);
396                 return;
397         }
398
399         // There are unvalidated users.  Traverse the user database, and return the first user we find that needs validation.
400         cdb_rewind(CDB_USERS);
401         while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
402                 memset(&usbuf, 0, sizeof(struct ctdluser));
403                 memcpy(&usbuf, cdbus->ptr, ((cdbus->len > sizeof(struct ctdluser)) ?  sizeof(struct ctdluser) : cdbus->len));
404                 cdb_free(cdbus);
405                 if ((usbuf.flags & US_NEEDVALID) && (usbuf.axlevel > AxDeleted)) {
406                         cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
407                         cdb_close_cursor(CDB_USERS);
408                         return;
409                 }
410         }
411
412         // If we get to this point, there are no more unvalidated users.  Therefore we clear the "users need validation" flag.
413         begin_critical_section(S_CONTROL);
414         int flags;
415         flags = CtdlGetConfigInt("MMflags");
416         flags = flags & (~MM_VALID);
417         CtdlSetConfigInt("MMflags", flags);
418         end_critical_section(S_CONTROL);
419         cprintf("%d *** End of registration.\n", CIT_OK);
420 }
421
422
423 // validate a user
424 void cmd_vali(char *v_args) {
425         char user[128];
426         int newax;
427         struct ctdluser userbuf;
428
429         extract_token(user, v_args, 0, '|', sizeof user);
430         newax = extract_int(v_args, 1);
431
432         if (CtdlAccessCheck(ac_aide) || (newax > AxAideU) || (newax < AxDeleted)) {
433                 return;
434         }
435
436         if (CtdlGetUserLock(&userbuf, user) != 0) {
437                 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
438                 return;
439         }
440
441         userbuf.axlevel = newax;
442         userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
443
444         CtdlPutUserLock(&userbuf);
445
446         // If the access level was set to zero, delete the user
447         if (newax == 0) {
448                 if (purge_user(user) == 0) {
449                         cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
450                         return;
451                 }
452         }
453         cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
454 }
455
456
457 // List one user (this works with cmd_list)
458 void ListThisUser(char *username, void *data) {
459         char *searchstring;
460         struct ctdluser usbuf;
461
462         if (CtdlGetUser(&usbuf, username) != 0) {
463                 return;
464         }
465
466         searchstring = (char *)data;
467         if (bmstrcasestr(usbuf.fullname, searchstring) == NULL) {
468                 return;
469         }
470
471         if (usbuf.axlevel > AxDeleted) {
472                 if ((CC->user.axlevel >= AxAideU)
473                     || ((usbuf.flags & US_UNLISTED) == 0)
474                     || ((CC->internal_pgm))) {
475                         cprintf("%s|%d|%ld|%ld|%ld|%ld||\n",
476                                 usbuf.fullname,
477                                 usbuf.axlevel,
478                                 usbuf.usernum,
479                                 (long)usbuf.lastcall,
480                                 usbuf.timescalled,
481                                 usbuf.posted);
482                 }
483         }
484 }
485
486
487 //  List users (searchstring may be empty to list all users)
488 void cmd_list(char *cmdbuf) {
489         char searchstring[256];
490         extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
491         striplt(searchstring);
492         cprintf("%d \n", LISTING_FOLLOWS);
493         ForEachUser(ListThisUser, (void *)searchstring );
494         cprintf("000\n");
495 }
496
497
498 // assorted info we need to check at login
499 void cmd_chek(char *argbuf) {
500         int mail = 0;
501         int regis = 0;
502         int vali = 0;
503
504         if (CtdlAccessCheck(ac_logged_in)) {
505                 return;
506         }
507
508         CtdlGetUser(&CC->user, CC->curr_user);  // no lock is needed here 
509         if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0)) {
510                 regis = 1;
511         }
512
513         if (CC->user.axlevel >= AxAideU) {
514                 if (CtdlGetConfigInt("MMflags") & MM_VALID) {
515                         vali = 1;
516                 }
517         }
518
519         mail = InitialMailCheck();              // check for mail
520         cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
521 }
522
523
524 // check to see if a user exists
525 void cmd_qusr(char *who) {
526         struct ctdluser usbuf;
527
528         if (CtdlGetUser(&usbuf, who) == 0) {
529                 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
530         }
531         else {
532                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
533         }
534 }
535
536
537 // Administrative Get User Parameters
538 void cmd_agup(char *cmdbuf) {
539         struct ctdluser usbuf;
540         char requested_user[128];
541
542         if (CtdlAccessCheck(ac_aide)) {
543                 return;
544         }
545
546         extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
547         if (CtdlGetUser(&usbuf, requested_user) != 0) {
548                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
549                 return;
550         }
551         cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
552                 CIT_OK,
553                 usbuf.fullname,
554                 usbuf.password,
555                 usbuf.flags,
556                 usbuf.timescalled,
557                 usbuf.posted,
558                 (int) usbuf.axlevel,
559                 usbuf.usernum,
560                 (long)usbuf.lastcall,
561                 usbuf.USuserpurge);
562 }
563
564
565 // Administrative Set User Parameters
566 void cmd_asup(char *cmdbuf) {
567         struct ctdluser usbuf;
568         char requested_user[128];
569         char notify[SIZ];
570         int np;
571         int newax;
572         int deleted = 0;
573
574         if (CtdlAccessCheck(ac_aide))
575                 return;
576
577         extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
578         if (CtdlGetUserLock(&usbuf, requested_user) != 0) {
579                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
580                 return;
581         }
582         np = num_parms(cmdbuf);
583         if (np > 1)
584                 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
585         if (np > 2)
586                 usbuf.flags = extract_int(cmdbuf, 2);
587         if (np > 3)
588                 usbuf.timescalled = extract_int(cmdbuf, 3);
589         if (np > 4)
590                 usbuf.posted = extract_int(cmdbuf, 4);
591         if (np > 5) {
592                 newax = extract_int(cmdbuf, 5);
593                 if ((newax >= AxDeleted) && (newax <= AxAideU)) {
594                         usbuf.axlevel = newax;
595                 }
596         }
597         if (np > 7) {
598                 usbuf.lastcall = extract_long(cmdbuf, 7);
599         }
600         if (np > 8) {
601                 usbuf.USuserpurge = extract_int(cmdbuf, 8);
602         }
603         CtdlPutUserLock(&usbuf);
604         if (usbuf.axlevel == AxDeleted) {
605                 if (purge_user(requested_user) == 0) {
606                         deleted = 1;
607                 }
608         }
609
610         if (deleted) {
611                 snprintf(notify, SIZ, 
612                         "User \"%s\" has been deleted by %s.\n",
613                         usbuf.fullname, (CC->logged_in ? CC->user.fullname : "an administrator")
614                 );
615                 CtdlAideMessage(notify, "User Deletion Message");
616         }
617
618         cprintf("%d Ok", CIT_OK);
619         if (deleted) {
620                 cprintf(" (%s deleted)", requested_user);
621         }
622         cprintf("\n");
623 }
624
625
626 // Citadel protocol command to do the same
627 void cmd_isme(char *argbuf) {
628         char addr[256];
629
630         if (CtdlAccessCheck(ac_logged_in)) return;
631         extract_token(addr, argbuf, 0, '|', sizeof addr);
632
633         if (CtdlIsMe(addr, sizeof addr)) {
634                 cprintf("%d %s\n", CIT_OK, addr);
635         }
636         else {
637                 cprintf("%d Not you.\n", ERROR + ILLEGAL_VALUE);
638         }
639
640 }
641
642
643 // Retrieve all Internet email addresses/aliases for the specified user
644 void cmd_agea(char *cmdbuf) {
645         struct ctdluser usbuf;
646         char requested_user[128];
647         int i, num_e;
648         char e[512];
649
650         if (CtdlAccessCheck(ac_aide)) {
651                 return;
652         }
653
654         extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
655         if (CtdlGetUser(&usbuf, requested_user) != 0) {
656                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
657                 return;
658         }
659         cprintf("%d internet email addresses for %s\n", LISTING_FOLLOWS, usbuf.fullname);
660         num_e = num_tokens(usbuf.emailaddrs, '|');
661         for (i=0; i<num_e; ++i) {
662                 extract_token(e, usbuf.emailaddrs, i, '|', sizeof e);
663                 cprintf("%s\n", e);
664         }
665         cprintf("000\n");
666 }
667
668
669 // Set the Internet email addresses/aliases for the specified user
670 void cmd_asea(char *cmdbuf) {
671         struct ctdluser usbuf;
672         char requested_user[128];
673         char buf[SIZ];
674         char whodat[64];
675         char new_emailaddrs[512] = { 0 } ;
676
677         if (CtdlAccessCheck(ac_aide)) return;
678
679         extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
680         if (CtdlGetUser(&usbuf, requested_user) != 0) {
681                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
682                 return;
683         }
684
685         cprintf("%d Ok\n", SEND_LISTING);
686         while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
687                 if (IsEmptyStr(buf)) {
688                         syslog(LOG_ERR, "user_ops: address <%s> is empty - not using", buf);
689                 }
690                 else if ((strlen(new_emailaddrs) + strlen(buf) + 2) > sizeof(new_emailaddrs)) {
691                         syslog(LOG_ERR, "user_ops: address <%s> does not fit in buffer - not using", buf);
692                 }
693                 else if (!IsDirectory(buf, 0)) {
694                         syslog(LOG_ERR, "user_ops: address <%s> is not in one of our domains - not using", buf);
695                 }
696                 else if ( (CtdlDirectoryLookup(whodat, buf, sizeof whodat) == 0) && (CtdlUserCmp(whodat, requested_user)) ) {
697                         syslog(LOG_ERR, "user_ops: address <%s> already belongs to <%s> - not using", buf, whodat);
698                 }
699                 else {
700                         syslog(LOG_DEBUG, "user_ops: address <%s> validated", buf);
701                         if (!IsEmptyStr(new_emailaddrs)) {
702                                 strcat(new_emailaddrs, "|");
703                         }
704                         strcat(new_emailaddrs, buf);
705                 }
706         }
707
708         CtdlSetEmailAddressesForUser(requested_user, new_emailaddrs);
709 }
710
711
712 // Set the preferred view for the current user/room combination
713 void cmd_view(char *cmdbuf) {
714         int requested_view;
715         visit vbuf;
716
717         if (CtdlAccessCheck(ac_logged_in)) {
718                 return;
719         }
720
721         requested_view = extract_int(cmdbuf, 0);
722
723         CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
724         vbuf.v_view = requested_view;
725         CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
726         
727         cprintf("%d ok\n", CIT_OK);
728 }
729
730
731 // Rename a user
732 void cmd_renu(char *cmdbuf) {
733         int retcode;
734         char oldname[USERNAME_SIZE];
735         char newname[USERNAME_SIZE];
736
737         if (CtdlAccessCheck(ac_aide)) {
738                 return;
739         }
740
741         extract_token(oldname, cmdbuf, 0, '|', sizeof oldname);
742         extract_token(newname, cmdbuf, 1, '|', sizeof newname);
743
744         retcode = rename_user(oldname, newname);
745         switch(retcode) {
746                 case RENAMEUSER_OK:
747                         cprintf("%d '%s' has been renamed to '%s'.\n", CIT_OK, oldname, newname);
748                         return;
749                 case RENAMEUSER_LOGGED_IN:
750                         cprintf("%d '%s' is currently logged in and cannot be renamed.\n",
751                                 ERROR + ALREADY_LOGGED_IN , oldname
752                         );
753                         return;
754                 case RENAMEUSER_NOT_FOUND:
755                         cprintf("%d '%s' does not exist.\n", ERROR + NO_SUCH_USER, oldname);
756                         return;
757                 case RENAMEUSER_ALREADY_EXISTS:
758                         cprintf("%d A user named '%s' already exists.\n", ERROR + ALREADY_EXISTS, newname);
759                         return;
760         }
761
762         cprintf("%d An unknown error occurred.\n", ERROR);
763 }
764
765
766 void cmd_quit(char *argbuf) {
767         cprintf("%d Goodbye.\n", CIT_OK);
768         CC->kill_me = KILLME_CLIENT_LOGGED_OUT;
769 }
770
771
772 void cmd_lout(char *argbuf) {
773         if (CC->logged_in) 
774                 CtdlUserLogout();
775         cprintf("%d logged out.\n", CIT_OK);
776 }
777
778
779 /*****************************************************************************/
780 /*                      MODULE INITIALIZATION STUFF                          */
781 /*****************************************************************************/
782
783
784 CTDL_MODULE_INIT(serv_user)
785 {
786         if (!threading) {
787                 CtdlRegisterProtoHook(cmd_user, "USER", "Submit username for login");
788                 CtdlRegisterProtoHook(cmd_pass, "PASS", "Complete login by submitting a password");
789                 CtdlRegisterProtoHook(cmd_quit, "QUIT", "log out and disconnect from server");
790                 CtdlRegisterProtoHook(cmd_lout, "LOUT", "log out but do not disconnect from server");
791                 CtdlRegisterProtoHook(cmd_creu, "CREU", "Create User");
792                 CtdlRegisterProtoHook(cmd_setp, "SETP", "Set the password for an account");
793                 CtdlRegisterProtoHook(cmd_getu, "GETU", "Get User parameters");
794                 CtdlRegisterProtoHook(cmd_setu, "SETU", "Set User parameters");
795                 CtdlRegisterProtoHook(cmd_slrp, "SLRP", "Set Last Read Pointer");
796                 CtdlRegisterProtoHook(cmd_invt, "INVT", "Invite a user to a room");
797                 CtdlRegisterProtoHook(cmd_kick, "KICK", "Kick a user out of a room");
798                 CtdlRegisterProtoHook(cmd_forg, "FORG", "Forget a room");
799                 CtdlRegisterProtoHook(cmd_gnur, "GNUR", "Get Next Unregistered User");
800                 CtdlRegisterProtoHook(cmd_vali, "VALI", "Validate new users");
801                 CtdlRegisterProtoHook(cmd_list, "LIST", "List users");
802                 CtdlRegisterProtoHook(cmd_chek, "CHEK", "assorted info we need to check at login");
803                 CtdlRegisterProtoHook(cmd_qusr, "QUSR", "check to see if a user exists");
804                 CtdlRegisterProtoHook(cmd_agup, "AGUP", "Administratively Get User Parameters");
805                 CtdlRegisterProtoHook(cmd_asup, "ASUP", "Administratively Set User Parameters");
806                 CtdlRegisterProtoHook(cmd_agea, "AGEA", "Administratively Get Email Addresses");
807                 CtdlRegisterProtoHook(cmd_asea, "ASEA", "Administratively Set Email Addresses");
808                 CtdlRegisterProtoHook(cmd_seen, "SEEN", "Manipulate seen/unread message flags");
809                 CtdlRegisterProtoHook(cmd_gtsn, "GTSN", "Fetch seen/unread message flags");
810                 CtdlRegisterProtoHook(cmd_view, "VIEW", "Set preferred view for user/room combination");
811                 CtdlRegisterProtoHook(cmd_renu, "RENU", "Rename a user");
812                 CtdlRegisterProtoHook(cmd_newu, "NEWU", "Log in as a new user");
813                 CtdlRegisterProtoHook(cmd_isme, "ISME", "Determine whether an email address belongs to a user");
814         }
815         /* return our Subversion id for the Log */
816         return "user";
817 }