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