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