7ccba3409c9548e513ab95402a7e092a3f74ec16
[citadel.git] / citadel / user_ops.c
1 /* 
2  * $Id$
3  *
4  * Server functions which perform operations on user objects.
5  *
6  */
7
8 #ifdef DLL_EXPORT
9 #define IN_LIBCIT
10 #endif
11
12 #include "sysdep.h"
13 #include <errno.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <stdio.h>
17 #include <fcntl.h>
18 #include <signal.h>
19 #include <pwd.h>
20 #include <ctype.h>
21 #include <sys/types.h>
22 #include <sys/wait.h>
23
24 #if TIME_WITH_SYS_TIME
25 # include <sys/time.h>
26 # include <time.h>
27 #else
28 # if HAVE_SYS_TIME_H
29 #  include <sys/time.h>
30 # else
31 #  include <time.h>
32 # endif
33 #endif
34
35 #include <string.h>
36 #include <limits.h>
37 #ifndef ENABLE_CHKPWD
38 #include "auth.h"
39 #endif
40 #include "citadel.h"
41 #include "server.h"
42 #include "database.h"
43 #include "user_ops.h"
44 #include "serv_extensions.h"
45 #include "sysdep_decls.h"
46 #include "support.h"
47 #include "room_ops.h"
48 #include "file_ops.h"
49 #include "control.h"
50 #include "msgbase.h"
51 #include "config.h"
52 #include "tools.h"
53 #include "citserver.h"
54
55
56 /*
57  * makeuserkey() - convert a username into the format used as a database key
58  *                 (it's just the username converted into lower case)
59  */
60 static INLINE void makeuserkey(char *key, char *username) {
61         int i, len;
62
63         len = strlen(username);
64         for (i=0; i<=len; ++i) {
65                 key[i] = tolower(username[i]);
66         }
67 }
68
69
70 /*
71  * getuser()  -  retrieve named user into supplied buffer.
72  *               returns 0 on success
73  */
74 int getuser(struct ctdluser *usbuf, char name[])
75 {
76
77         char usernamekey[USERNAME_SIZE];
78         char sysuser_name[USERNAME_SIZE];
79         struct cdbdata *cdbus;
80         int using_sysuser = 0;
81
82         if (usbuf != NULL) {
83                 memset(usbuf, 0, sizeof(struct ctdluser));
84         }
85
86 #ifdef ENABLE_AUTOLOGIN
87         if (CtdlAssociateSystemUser(sysuser_name, name) == 0) {
88                 ++using_sysuser;
89         }
90 #endif
91
92         if (using_sysuser) {
93                 makeuserkey(usernamekey, sysuser_name);
94         }
95         else {
96                 makeuserkey(usernamekey, name);
97         }
98
99         cdbus = cdb_fetch(CDB_USERS, usernamekey, strlen(usernamekey));
100         if (cdbus == NULL) {    /* user not found */
101                 return(1);
102         }
103         if (usbuf != NULL) {
104                 memcpy(usbuf, cdbus->ptr,
105                         ((cdbus->len > sizeof(struct ctdluser)) ?
106                          sizeof(struct ctdluser) : cdbus->len));
107         }
108         cdb_free(cdbus);
109
110         return (0);
111 }
112
113
114 /*
115  * lgetuser()  -  same as getuser() but locks the record
116  */
117 int lgetuser(struct ctdluser *usbuf, char *name)
118 {
119         int retcode;
120
121         retcode = getuser(usbuf, name);
122         if (retcode == 0) {
123                 begin_critical_section(S_USERS);
124         }
125         return (retcode);
126 }
127
128
129 /*
130  * putuser()  -  write user buffer into the correct place on disk
131  */
132 void putuser(struct ctdluser *usbuf)
133 {
134         char usernamekey[USERNAME_SIZE];
135
136         makeuserkey(usernamekey, usbuf->fullname);
137
138         usbuf->version = REV_LEVEL;
139         cdb_store(CDB_USERS,
140                   usernamekey, strlen(usernamekey),
141                   usbuf, sizeof(struct ctdluser));
142
143 }
144
145
146 /*
147  * lputuser()  -  same as putuser() but locks the record
148  */
149 void lputuser(struct ctdluser *usbuf)
150 {
151         putuser(usbuf);
152         end_critical_section(S_USERS);
153 }
154
155 /*
156  * Index-generating function used by Ctdl[Get|Set]Relationship
157  */
158 int GenerateRelationshipIndex(char *IndexBuf,
159                               long RoomID,
160                               long RoomGen,
161                               long UserID)
162 {
163
164         struct {
165                 long iRoomID;
166                 long iRoomGen;
167                 long iUserID;
168         } TheIndex;
169
170         TheIndex.iRoomID = RoomID;
171         TheIndex.iRoomGen = RoomGen;
172         TheIndex.iUserID = UserID;
173
174         memcpy(IndexBuf, &TheIndex, sizeof(TheIndex));
175         return (sizeof(TheIndex));
176 }
177
178
179
180 /*
181  * Back end for CtdlSetRelationship()
182  */
183 void put_visit(struct visit *newvisit)
184 {
185         char IndexBuf[32];
186         int IndexLen;
187
188         /* Generate an index */
189         IndexLen = GenerateRelationshipIndex(IndexBuf,
190                                              newvisit->v_roomnum,
191                                              newvisit->v_roomgen,
192                                              newvisit->v_usernum);
193
194         /* Store the record */
195         cdb_store(CDB_VISIT, IndexBuf, IndexLen,
196                   newvisit, sizeof(struct visit)
197         );
198 }
199
200
201
202
203 /*
204  * Define a relationship between a user and a room
205  */
206 void CtdlSetRelationship(struct visit *newvisit,
207                          struct ctdluser *rel_user,
208                          struct ctdlroom *rel_room)
209 {
210
211
212         /* We don't use these in Citadel because they're implicit by the
213          * index, but they must be present if the database is exported.
214          */
215         newvisit->v_roomnum = rel_room->QRnumber;
216         newvisit->v_roomgen = rel_room->QRgen;
217         newvisit->v_usernum = rel_user->usernum;
218
219         put_visit(newvisit);
220 }
221
222 /*
223  * Locate a relationship between a user and a room
224  */
225 void CtdlGetRelationship(struct visit *vbuf,
226                          struct ctdluser *rel_user,
227                          struct ctdlroom *rel_room)
228 {
229
230         char IndexBuf[32];
231         int IndexLen;
232         struct cdbdata *cdbvisit;
233
234         /* Generate an index */
235         IndexLen = GenerateRelationshipIndex(IndexBuf,
236                                              rel_room->QRnumber,
237                                              rel_room->QRgen,
238                                              rel_user->usernum);
239
240         /* Clear out the buffer */
241         memset(vbuf, 0, sizeof(struct visit));
242
243         cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
244         if (cdbvisit != NULL) {
245                 memcpy(vbuf, cdbvisit->ptr,
246                        ((cdbvisit->len > sizeof(struct visit)) ?
247                         sizeof(struct visit) : cdbvisit->len));
248                 cdb_free(cdbvisit);
249         }
250         else {
251                 /* If this is the first time the user has seen this room,
252                  * set the view to be the default for the room.
253                  */
254                 vbuf->v_view = rel_room->QRdefaultview;
255         }
256
257         /* Set v_seen if necessary */
258         if (vbuf->v_seen[0] == 0) {
259                 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
260         }
261 }
262
263
264 void MailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
265 {
266         snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
267 }
268
269
270 /*
271  * Is the user currently logged in an Aide?
272  */
273 int is_aide(void)
274 {
275         if (CC->user.axlevel >= 6)
276                 return (1);
277         else
278                 return (0);
279 }
280
281
282 /*
283  * Is the user currently logged in an Aide *or* the room aide for this room?
284  */
285 int is_room_aide(void)
286 {
287
288         if (!CC->logged_in) {
289                 return (0);
290         }
291
292         if ((CC->user.axlevel >= 6)
293             || (CC->room.QRroomaide == CC->user.usernum)) {
294                 return (1);
295         } else {
296                 return (0);
297         }
298 }
299
300 /*
301  * getuserbynumber()  -  get user by number
302  *                       returns 0 if user was found
303  *
304  * WARNING: don't use this function unless you absolutely have to.  It does
305  *          a sequential search and therefore is computationally expensive.
306  */
307 int getuserbynumber(struct ctdluser *usbuf, long int number)
308 {
309         struct cdbdata *cdbus;
310
311         cdb_rewind(CDB_USERS);
312
313         while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
314                 memset(usbuf, 0, sizeof(struct ctdluser));
315                 memcpy(usbuf, cdbus->ptr,
316                        ((cdbus->len > sizeof(struct ctdluser)) ?
317                         sizeof(struct ctdluser) : cdbus->len));
318                 cdb_free(cdbus);
319                 if (usbuf->usernum == number) {
320                         cdb_close_cursor(CDB_USERS);
321                         return (0);
322                 }
323         }
324         return (-1);
325 }
326
327
328 /*
329  * See if we can translate a system login name (i.e. from /etc/passwd)
330  * to a Citadel screen name.  Returns 0 if one is found.
331  */
332 int CtdlAssociateSystemUser(char *screenname, char *loginname) {
333         struct passwd *p;
334         int a;
335
336         p = (struct passwd *) getpwnam(loginname);
337         if (p != NULL) {
338                 strcpy(screenname, p->pw_gecos);
339                 for (a = 0; a < strlen(screenname); ++a) {
340                         if (screenname[a] == ',') {
341                                 screenname[a] = 0;
342                         }
343                 }
344                 return(0);
345         }
346         return(1);
347 }
348
349
350
351 /*
352  * Back end for cmd_user() and its ilk
353  */
354 int CtdlLoginExistingUser(char *trythisname)
355 {
356         char username[SIZ];
357         int found_user;
358         struct recptypes *valid = NULL;
359
360         if (trythisname == NULL) return login_not_found;
361         safestrncpy(username, trythisname, USERNAME_SIZE);
362         striplt(username);
363
364         if ((CC->logged_in)) {
365                 return login_already_logged_in;
366         }
367
368         /* First, try to log in as if the supplied name is a display name */
369         found_user = getuser(&CC->user, username);
370
371         /* If that didn't work, try to log in as if the supplied name
372          * is an e-mail address
373          */
374         if (found_user != 0) {
375                 valid = validate_recipients(username);
376                 if (valid != NULL) {
377                         if (valid->num_local == 1) {
378                                 found_user = getuser(&CC->user,
379                                                 valid->recp_local);
380                         }
381                         free(valid);
382                 }
383         }
384
385 #ifdef ENABLE_AUTOLOGIN
386         /* If we haven't found the account yet, and the supplied name
387          * is a login name on the underlying host system, create the
388          * account.
389          */
390         if (found_user != 0) {
391                 struct passwd *p = (struct passwd *) getpwnam(username);
392
393                 if (p != NULL) {
394                         create_user(username, 0);
395                         found_user = getuser(&CC->user, username);
396                 }
397         }
398 #endif /* ENABLE_AUTOLOGIN */
399
400         /* Did we find something? */
401         if (found_user == 0) {
402                 if (((CC->nologin)) && (CC->user.axlevel < 6)) {
403                         return login_too_many_users;
404                 } else {
405                         safestrncpy(CC->curr_user, CC->user.fullname,
406                                         sizeof CC->curr_user);
407                         return login_ok;
408                 }
409         }
410         return login_not_found;
411 }
412
413
414
415 /*
416  * USER cmd
417  */
418 void cmd_user(char *cmdbuf)
419 {
420         char username[256];
421         int a;
422
423         extract_token(username, cmdbuf, 0, '|', sizeof username);
424         striplt(username);
425
426         a = CtdlLoginExistingUser(username);
427         switch (a) {
428         case login_already_logged_in:
429                 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
430                 return;
431         case login_too_many_users:
432                 cprintf("%d %s: "
433                         "Too many users are already online "
434                         "(maximum is %d)\n",
435                         ERROR + MAX_SESSIONS_EXCEEDED,
436                         config.c_nodename, config.c_maxsessions);
437                 return;
438         case login_ok:
439                 cprintf("%d Password required for %s\n",
440                         MORE_DATA, CC->curr_user);
441                 return;
442         case login_not_found:
443                 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER, username);
444                 return;
445         default:
446                 cprintf("%d Internal error\n", ERROR + INTERNAL_ERROR);
447         }
448 }
449
450
451
452 /*
453  * session startup code which is common to both cmd_pass() and cmd_newu()
454  */
455 void session_startup(void)
456 {
457         int i;
458
459         lprintf(CTDL_NOTICE, "<%s> logged in\n", CC->curr_user);
460
461         lgetuser(&CC->user, CC->curr_user);
462         ++(CC->user.timescalled);
463         CC->previous_login = CC->user.lastcall;
464         time(&CC->user.lastcall);
465
466         /* If this user's name is the name of the system administrator
467          * (as specified in setup), automatically assign access level 6.
468          */
469         if (!strcasecmp(CC->user.fullname, config.c_sysadm)) {
470                 CC->user.axlevel = 6;
471         }
472         lputuser(&CC->user);
473
474         /*
475          * Populate CC->cs_inet_email with a default address.  This will be
476          * overwritten with the user's directory address, if one exists, when
477          * the vCard module's login hook runs.
478          */
479         snprintf(CC->cs_inet_email, sizeof CC->cs_inet_email, "%s@%s",
480                 CC->user.fullname, config.c_fqdn);
481         for (i=0; i<strlen(CC->cs_inet_email); ++i) {
482                 if (isspace(CC->cs_inet_email[i])) {
483                         CC->cs_inet_email[i] = '_';
484                 }
485         }
486
487         /* Create any personal rooms required by the system.
488          * (Technically, MAILROOM should be there already, but just in case...)
489          */
490         create_room(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
491         create_room(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
492
493         /* Run any startup routines registered by loadable modules */
494         PerformSessionHooks(EVT_LOGIN);
495
496         /* Enter the lobby */
497         usergoto(config.c_baseroom, 0, 0, NULL, NULL);
498 }
499
500
501 void logged_in_response(void)
502 {
503         cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
504                 CIT_OK, CC->user.fullname, CC->user.axlevel,
505                 CC->user.timescalled, CC->user.posted,
506                 CC->user.flags, CC->user.usernum,
507                 CC->previous_login);
508 }
509
510
511
512 /* 
513  * misc things to be taken care of when a user is logged out
514  */
515 void logout(struct CitContext *who)
516 {
517         /*
518          * Clear out some session data.  Most likely, the CitContext for this
519          * session is about to get nuked when the session disconnects, but
520          * since it's possible to log in again without reconnecting, we cannot
521          * make that assumption.
522          */
523         strcpy(who->fake_username, "");
524         strcpy(who->fake_postname, "");
525         strcpy(who->fake_hostname, "");
526         strcpy(who->fake_roomname, "");
527         who->logged_in = 0;
528
529         /*
530          * If there is a download in progress, abort it.
531          */
532         if (who->download_fp != NULL) {
533                 fclose(who->download_fp);
534                 who->download_fp = NULL;
535         }
536
537         /*
538          * If there is an upload in progress, abort it.
539          */
540         if (who->upload_fp != NULL) {
541                 abort_upl(who);
542         }
543
544         /*
545          * If we were talking to a network node, we're not anymore...
546          */
547         if (strlen(who->net_node) > 0) {
548                 network_talking_to(who->net_node, NTT_REMOVE);
549         }
550
551         /* Do modular stuff... */
552         PerformSessionHooks(EVT_LOGOUT);
553
554         /* Free any output buffers */
555         if (who->output_buffer != NULL) {
556                 unbuffer_output();
557         }
558 }
559
560 #ifdef ENABLE_CHKPWD
561 /*
562  * an alternate version of validpw() which executes `chkpwd' instead of
563  * verifying the password directly
564  */
565 static int validpw(uid_t uid, const char *pass)
566 {
567         pid_t pid;
568         int status, pipev[2];
569         char buf[24];
570
571         if (pipe(pipev)) {
572                 lprintf(CTDL_ERR, "pipe failed (%s): denying autologin access for "
573                         "uid %ld\n", strerror(errno), (long)uid);
574                 return 0;
575         }
576         switch (pid = fork()) {
577         case -1:
578                 lprintf(CTDL_ERR, "fork failed (%s): denying autologin access for "
579                         "uid %ld\n", strerror(errno), (long)uid);
580                 close(pipev[0]);
581                 close(pipev[1]);
582                 return 0;
583
584         case 0:
585                 close(pipev[1]);
586                 if (dup2(pipev[0], 0) == -1) {
587                         perror("dup2");
588                         exit(1);
589                 }
590                 close(pipev[0]);
591
592                 execl(CTDLDIR "/chkpwd", CTDLDIR "/chkpwd", NULL);
593                 perror(CTDLDIR "/chkpwd");
594                 exit(1);
595         }
596
597         close(pipev[0]);
598         write(pipev[1], buf,
599               snprintf(buf, sizeof buf, "%lu\n", (unsigned long) uid));
600         write(pipev[1], pass, strlen(pass));
601         write(pipev[1], "\n", 1);
602         close(pipev[1]);
603
604         while (waitpid(pid, &status, 0) == -1)
605                 if (errno != EINTR) {
606                         lprintf(CTDL_ERR, "waitpid failed (%s): denying autologin "
607                                 "access for uid %ld\n",
608                                 strerror(errno), (long)uid);
609                         return 0;
610                 }
611         if (WIFEXITED(status) && !WEXITSTATUS(status))
612                 return 1;
613
614         return 0;
615 }
616 #endif
617
618 void do_login()
619 {
620         (CC->logged_in) = 1;
621         session_startup();
622 }
623
624
625 int CtdlTryPassword(char *password)
626 {
627         int code;
628
629         if ((CC->logged_in)) {
630                 lprintf(CTDL_WARNING, "CtdlTryPassword: already logged in\n");
631                 return pass_already_logged_in;
632         }
633         if (!strcmp(CC->curr_user, NLI)) {
634                 lprintf(CTDL_WARNING, "CtdlTryPassword: no user selected\n");
635                 return pass_no_user;
636         }
637         if (getuser(&CC->user, CC->curr_user)) {
638                 lprintf(CTDL_ERR, "CtdlTryPassword: internal error\n");
639                 return pass_internal_error;
640         }
641         if (password == NULL) {
642                 lprintf(CTDL_INFO, "CtdlTryPassword: NULL password string supplied\n");
643                 return pass_wrong_password;
644         }
645         code = (-1);
646
647
648 #ifdef ENABLE_AUTOLOGIN
649         /* A uid of CTDLUID or -1 indicates that this user exists only in
650          * Citadel, not in the underlying operating system.
651          */
652         if ( (CC->user.uid == CTDLUID) || (CC->user.uid == (-1)) ) {
653                 strproc(password);
654                 strproc(CC->user.password);
655                 code = strcasecmp(CC->user.password, password);
656         }
657         /* Any other uid means we have to check the system password database */
658         else {
659                 if (validpw(CC->user.uid, password)) {
660                         code = 0;
661                         lgetuser(&CC->user, CC->curr_user);
662                         safestrncpy(CC->user.password, password,
663                                     sizeof CC->user.password);
664                         lputuser(&CC->user);
665                 }
666         }
667
668 #else /* ENABLE_AUTOLOGIN */
669         strproc(password);
670         strproc(CC->user.password);
671         code = strcasecmp(CC->user.password, password);
672
673 #endif /* ENABLE_AUTOLOGIN */
674
675         if (!code) {
676                 do_login();
677                 return pass_ok;
678         } else {
679                 lprintf(CTDL_WARNING, "Bad password specified for <%s>\n", CC->curr_user);
680                 return pass_wrong_password;
681         }
682 }
683
684
685 void cmd_pass(char *buf)
686 {
687         char password[256];
688         int a;
689
690         extract_token(password, buf, 0, '|', sizeof password);
691         a = CtdlTryPassword(password);
692
693         switch (a) {
694         case pass_already_logged_in:
695                 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
696                 return;
697         case pass_no_user:
698                 cprintf("%d You must send a name with USER first.\n",
699                         ERROR + USERNAME_REQUIRED);
700                 return;
701         case pass_wrong_password:
702                 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
703                 return;
704         case pass_ok:
705                 logged_in_response();
706                 return;
707         }
708 }
709
710
711
712 /*
713  * Delete a user record *and* all of its related resources.
714  */
715 int purge_user(char pname[])
716 {
717         char filename[64];
718         struct ctdluser usbuf;
719         char usernamekey[USERNAME_SIZE];
720         struct CitContext *ccptr;
721         int user_is_logged_in = 0;
722
723         makeuserkey(usernamekey, pname);
724
725         if (getuser(&usbuf, pname) != 0) {
726                 lprintf(CTDL_ERR, "Cannot purge user <%s> - not found\n", pname);
727                 return (ERROR + NO_SUCH_USER);
728         }
729         /* Don't delete a user who is currently logged in.  Instead, just
730          * set the access level to 0, and let the account get swept up
731          * during the next purge.
732          */
733         user_is_logged_in = 0;
734         begin_critical_section(S_SESSION_TABLE);
735         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
736                 if (ccptr->user.usernum == usbuf.usernum) {
737                         user_is_logged_in = 1;
738                 }
739         }
740         end_critical_section(S_SESSION_TABLE);
741         if (user_is_logged_in == 1) {
742                 lprintf(CTDL_WARNING, "User <%s> is logged in; not deleting.\n", pname);
743                 usbuf.axlevel = 0;
744                 putuser(&usbuf);
745                 return (1);
746         }
747         lprintf(CTDL_NOTICE, "Deleting user <%s>\n", pname);
748
749         /* Perform any purge functions registered by server extensions */
750         PerformUserHooks(&usbuf, EVT_PURGEUSER);
751
752         /* delete any existing user/room relationships */
753         cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
754
755         /* delete the userlog entry */
756         cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
757
758         /* remove the user's bio file */
759         snprintf(filename, sizeof filename, 
760 #ifndef HAVE_DATA_DIR
761                          "."
762 #else
763                          DATA_DIR
764 #endif
765                          "/bio/%ld", usbuf.usernum);
766         unlink(filename);
767
768         /* remove the user's picture */
769         snprintf(filename, sizeof filename, 
770 #ifndef HAVE_DATA_DIR
771                          "."
772 #else
773                          DATA_DIR
774 #endif
775                          "/userpics/%ld.gif", usbuf.usernum);
776         unlink(filename);
777
778         return (0);
779 }
780
781
782 /*
783  * create_user()  -  back end processing to create a new user
784  *
785  * Set 'newusername' to the desired account name.
786  * Set 'become_user' to nonzero if this is self-service account creation and we want
787  * to actually log in as the user we just created, otherwise set it to 0.
788  */
789 int create_user(char *newusername, int become_user)
790 {
791         struct ctdluser usbuf;
792         struct ctdlroom qrbuf;
793         char username[256];
794         char mailboxname[ROOMNAMELEN];
795         uid_t uid;
796
797         safestrncpy(username, newusername, sizeof username);
798         strproc(username);
799
800 #ifdef ENABLE_AUTOLOGIN
801         {
802                 struct passwd *p = (struct passwd *) getpwnam(username);
803
804                 if (p != NULL) {
805                         extract_token(username, p->pw_gecos, 0, ',', sizeof username);
806                         uid = p->pw_uid;
807                 } else {
808                         uid = (-1);
809                 }
810         }
811 #else
812         uid = (-1);
813 #endif
814
815         if (!getuser(&usbuf, username)) {
816                 return (ERROR + ALREADY_EXISTS);
817         }
818
819         /* Go ahead and initialize a new user record */
820         memset(&usbuf, 0, sizeof(struct ctdluser));
821         safestrncpy(usbuf.fullname, username, sizeof usbuf.fullname);
822         strcpy(usbuf.password, "");
823         usbuf.uid = uid;
824
825         /* These are the default flags on new accounts */
826         usbuf.flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
827
828         usbuf.timescalled = 0;
829         usbuf.posted = 0;
830         usbuf.axlevel = config.c_initax;
831         usbuf.USscreenwidth = 80;
832         usbuf.USscreenheight = 24;
833         usbuf.lastcall = time(NULL);
834
835         /* fetch a new user number */
836         usbuf.usernum = get_new_user_number();
837
838         /* The very first user created on the system will always be an Aide */
839         if (usbuf.usernum == 1L) {
840                 usbuf.axlevel = 6;
841         }
842
843         /* add user to userlog */
844         putuser(&usbuf);
845
846         /*
847          * Give the user a private mailbox and a configuration room.
848          * Make the latter an invisible system room.
849          */
850         MailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
851         create_room(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
852
853         MailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
854         create_room(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
855         if (lgetroom(&qrbuf, mailboxname) == 0) {
856                 qrbuf.QRflags2 |= QR2_SYSTEM;
857                 lputroom(&qrbuf);
858         }
859
860         /* Perform any create functions registered by server extensions */
861         PerformUserHooks(&usbuf, EVT_NEWUSER);
862
863         /* Everything below this line can be bypassed if administratively
864          * creating a user, instead of doing self-service account creation
865          */
866
867         if (become_user) {
868                 /* Now become the user we just created */
869                 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
870                 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
871                 CC->logged_in = 1;
872         
873                 /* Check to make sure we're still who we think we are */
874                 if (getuser(&CC->user, CC->curr_user)) {
875                         return (ERROR + INTERNAL_ERROR);
876                 }
877         }
878
879         lprintf(CTDL_NOTICE, "New user <%s> created\n", username);
880         return (0);
881 }
882
883
884
885
886 /*
887  * cmd_newu()  -  create a new user account and log in as that user
888  */
889 void cmd_newu(char *cmdbuf)
890 {
891         int a;
892         char username[26];
893
894         if (config.c_disable_newu) {
895                 cprintf("%d Self-service user account creation "
896                         "is disabled on this system.\n", ERROR + NOT_HERE);
897                 return;
898         }
899
900         if (CC->logged_in) {
901                 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
902                 return;
903         }
904         if (CC->nologin) {
905                 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
906                         ERROR + MAX_SESSIONS_EXCEEDED,
907                         config.c_nodename, config.c_maxsessions);
908         }
909         extract_token(username, cmdbuf, 0, '|', sizeof username);
910         username[25] = 0;
911         strproc(username);
912
913         if (strlen(username) == 0) {
914                 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
915                 return;
916         }
917
918         if ((!strcasecmp(username, "bbs")) ||
919             (!strcasecmp(username, "new")) ||
920             (!strcasecmp(username, "."))) {
921                 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
922                 return;
923         }
924
925         a = create_user(username, 1);
926
927         if (a == 0) {
928                 session_startup();
929                 logged_in_response();
930         } else if (a == ERROR + ALREADY_EXISTS) {
931                 cprintf("%d '%s' already exists.\n",
932                         ERROR + ALREADY_EXISTS, username);
933                 return;
934         } else if (a == ERROR + INTERNAL_ERROR) {
935                 cprintf("%d Internal error - user record disappeared?\n",
936                         ERROR + INTERNAL_ERROR);
937                 return;
938         } else {
939                 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
940         }
941 }
942
943
944
945 /*
946  * set password
947  */
948 void cmd_setp(char *new_pw)
949 {
950         if (CtdlAccessCheck(ac_logged_in)) {
951                 return;
952         }
953         if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
954                 cprintf("%d Not allowed.  Use the 'passwd' command.\n", ERROR + NOT_HERE);
955                 return;
956         }
957         strproc(new_pw);
958         if (strlen(new_pw) == 0) {
959                 cprintf("%d Password unchanged.\n", CIT_OK);
960                 return;
961         }
962         lgetuser(&CC->user, CC->curr_user);
963         safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
964         lputuser(&CC->user);
965         cprintf("%d Password changed.\n", CIT_OK);
966         lprintf(CTDL_INFO, "Password changed for user <%s>\n", CC->curr_user);
967         PerformSessionHooks(EVT_SETPASS);
968 }
969
970
971 /*
972  * cmd_creu() - administratively create a new user account (do not log in to it)
973  */
974 void cmd_creu(char *cmdbuf)
975 {
976         int a;
977         char username[26];
978         char password[32];
979         struct ctdluser tmp;
980
981         if (CtdlAccessCheck(ac_aide)) {
982                 return;
983         }
984
985         extract_token(username, cmdbuf, 0, '|', sizeof username);
986         extract_token(password, cmdbuf, 1, '|', sizeof password);
987         username[25] = 0;
988         password[31] = 0;
989         strproc(username);
990         strproc(password);
991
992         if (strlen(username) == 0) {
993                 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
994                 return;
995         }
996
997         a = create_user(username, 0);
998
999         if (a == 0) {
1000                 if (strlen(password) > 0) {
1001                         lgetuser(&tmp, username);
1002                         safestrncpy(tmp.password, password, sizeof(tmp.password));
1003                         lputuser(&tmp);
1004                 }
1005                 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
1006                                 (strlen(password) > 0) ? "and password set" :
1007                                 "with no password");
1008                 return;
1009         } else if (a == ERROR + ALREADY_EXISTS) {
1010                 cprintf("%d '%s' already exists.\n",
1011                         ERROR + ALREADY_EXISTS, username);
1012                 return;
1013         } else {
1014                 cprintf("%d An error occured creating the user account.\n", ERROR + INTERNAL_ERROR);
1015         }
1016 }
1017
1018
1019
1020 /*
1021  * get user parameters
1022  */
1023 void cmd_getu(void)
1024 {
1025
1026         if (CtdlAccessCheck(ac_logged_in))
1027                 return;
1028
1029         getuser(&CC->user, CC->curr_user);
1030         cprintf("%d %d|%d|%d|\n",
1031                 CIT_OK,
1032                 CC->user.USscreenwidth,
1033                 CC->user.USscreenheight,
1034                 (CC->user.flags & US_USER_SET)
1035             );
1036 }
1037
1038 /*
1039  * set user parameters
1040  */
1041 void cmd_setu(char *new_parms)
1042 {
1043         if (CtdlAccessCheck(ac_logged_in))
1044                 return;
1045
1046         if (num_parms(new_parms) < 3) {
1047                 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1048                 return;
1049         }
1050         lgetuser(&CC->user, CC->curr_user);
1051         CC->user.USscreenwidth = extract_int(new_parms, 0);
1052         CC->user.USscreenheight = extract_int(new_parms, 1);
1053         CC->user.flags = CC->user.flags & (~US_USER_SET);
1054         CC->user.flags = CC->user.flags |
1055             (extract_int(new_parms, 2) & US_USER_SET);
1056
1057         lputuser(&CC->user);
1058         cprintf("%d Ok\n", CIT_OK);
1059 }
1060
1061 /*
1062  * set last read pointer
1063  */
1064 void cmd_slrp(char *new_ptr)
1065 {
1066         long newlr;
1067         struct visit vbuf;
1068         struct visit original_vbuf;
1069
1070         if (CtdlAccessCheck(ac_logged_in)) {
1071                 return;
1072         }
1073
1074         if (!strncasecmp(new_ptr, "highest", 7)) {
1075                 newlr = CC->room.QRhighest;
1076         } else {
1077                 newlr = atol(new_ptr);
1078         }
1079
1080         lgetuser(&CC->user, CC->curr_user);
1081
1082         CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1083         memcpy(&original_vbuf, &vbuf, sizeof(struct visit));
1084         vbuf.v_lastseen = newlr;
1085         snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1086
1087         /* Only rewrite the record if it changed */
1088         if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1089            || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1090                 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1091         }
1092
1093         lputuser(&CC->user);
1094         cprintf("%d %ld\n", CIT_OK, newlr);
1095 }
1096
1097
1098 void cmd_seen(char *argbuf) {
1099         long target_msgnum = 0L;
1100         int target_setting = 0;
1101
1102         if (CtdlAccessCheck(ac_logged_in)) {
1103                 return;
1104         }
1105
1106         if (num_parms(argbuf) != 2) {
1107                 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
1108                 return;
1109         }
1110
1111         target_msgnum = extract_long(argbuf, 0);
1112         target_setting = extract_int(argbuf, 1);
1113
1114         CtdlSetSeen(&target_msgnum, 1, target_setting,
1115                         ctdlsetseen_seen, NULL, NULL);
1116         cprintf("%d OK\n", CIT_OK);
1117 }
1118
1119
1120 void cmd_gtsn(char *argbuf) {
1121         char buf[SIZ];
1122
1123         if (CtdlAccessCheck(ac_logged_in)) {
1124                 return;
1125         }
1126
1127         CtdlGetSeen(buf, ctdlsetseen_seen);
1128         cprintf("%d %s\n", CIT_OK, buf);
1129 }
1130
1131
1132 /*
1133  * API function for cmd_invt_kick() and anything else that needs to
1134  * invite or kick out a user to/from a room.
1135  * 
1136  * Set iuser to the name of the user, and op to 1=invite or 0=kick
1137  */
1138 int CtdlInvtKick(char *iuser, int op) {
1139         struct ctdluser USscratch;
1140         struct visit vbuf;
1141         char bbb[SIZ];
1142
1143         if (getuser(&USscratch, iuser) != 0) {
1144                 return(1);
1145         }
1146
1147         CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1148         if (op == 1) {
1149                 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1150                 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1151         }
1152         if (op == 0) {
1153                 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1154                 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1155         }
1156         CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1157
1158         /* post a message in Aide> saying what we just did */
1159         snprintf(bbb, sizeof bbb, "%s %s %s> by %s\n",
1160                 iuser,
1161                 ((op == 1) ? "invited to" : "kicked out of"),
1162                 CC->room.QRname,
1163                 CC->user.fullname);
1164         aide_message(bbb);
1165
1166         return(0);
1167 }
1168
1169
1170 /*
1171  * INVT and KICK commands
1172  */
1173 void cmd_invt_kick(char *iuser, int op) {
1174
1175         /*
1176          * These commands are only allowed by aides, room aides,
1177          * and room namespace owners
1178          */
1179         if (is_room_aide()
1180            || (atol(CC->room.QRname) == CC->user.usernum) ) {
1181                 /* access granted */
1182         } else {
1183                 /* access denied */
1184                 cprintf("%d Higher access or room ownership required.\n",
1185                         ERROR + HIGHER_ACCESS_REQUIRED);
1186                 return;
1187         }
1188
1189         if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1190                          ROOMNAMELEN)) {
1191                 cprintf("%d Can't add/remove users from this room.\n",
1192                         ERROR + NOT_HERE);
1193                 return;
1194         }
1195
1196         if (CtdlInvtKick(iuser, op) != 0) {
1197                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1198                 return;
1199         }
1200
1201         cprintf("%d %s %s %s.\n",
1202                 CIT_OK, iuser,
1203                 ((op == 1) ? "invited to" : "kicked out of"),
1204                 CC->room.QRname);
1205         return;
1206 }
1207
1208
1209 /*
1210  * Forget (Zap) the current room (API call)
1211  * Returns 0 on success
1212  */
1213 int CtdlForgetThisRoom(void) {
1214         struct visit vbuf;
1215
1216         /* On some systems, Aides are not allowed to forget rooms */
1217         if (is_aide() && (config.c_aide_zap == 0)
1218            && ((CC->room.QRflags & QR_MAILBOX) == 0)  ) {
1219                 return(1);
1220         }
1221
1222         lgetuser(&CC->user, CC->curr_user);
1223         CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1224
1225         vbuf.v_flags = vbuf.v_flags | V_FORGET;
1226         vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1227
1228         CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1229         lputuser(&CC->user);
1230
1231         /* Return to the Lobby, so we don't end up in an undefined room */
1232         usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1233         return(0);
1234
1235 }
1236
1237
1238 /*
1239  * forget (Zap) the current room
1240  */
1241 void cmd_forg(void)
1242 {
1243
1244         if (CtdlAccessCheck(ac_logged_in)) {
1245                 return;
1246         }
1247
1248         if (CtdlForgetThisRoom() == 0) {
1249                 cprintf("%d Ok\n", CIT_OK);
1250         }
1251         else {
1252                 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
1253         }
1254 }
1255
1256 /*
1257  * Get Next Unregistered User
1258  */
1259 void cmd_gnur(void)
1260 {
1261         struct cdbdata *cdbus;
1262         struct ctdluser usbuf;
1263
1264         if (CtdlAccessCheck(ac_aide)) {
1265                 return;
1266         }
1267
1268         if ((CitControl.MMflags & MM_VALID) == 0) {
1269                 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1270                 return;
1271         }
1272
1273         /* There are unvalidated users.  Traverse the user database,
1274          * and return the first user we find that needs validation.
1275          */
1276         cdb_rewind(CDB_USERS);
1277         while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1278                 memset(&usbuf, 0, sizeof(struct ctdluser));
1279                 memcpy(&usbuf, cdbus->ptr,
1280                        ((cdbus->len > sizeof(struct ctdluser)) ?
1281                         sizeof(struct ctdluser) : cdbus->len));
1282                 cdb_free(cdbus);
1283                 if ((usbuf.flags & US_NEEDVALID)
1284                     && (usbuf.axlevel > 0)) {
1285                         cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1286                         cdb_close_cursor(CDB_USERS);
1287                         return;
1288                 }
1289         }
1290
1291         /* If we get to this point, there are no more unvalidated users.
1292          * Therefore we clear the "users need validation" flag.
1293          */
1294
1295         begin_critical_section(S_CONTROL);
1296         get_control();
1297         CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1298         put_control();
1299         end_critical_section(S_CONTROL);
1300         cprintf("%d *** End of registration.\n", CIT_OK);
1301
1302
1303 }
1304
1305
1306 /*
1307  * validate a user
1308  */
1309 void cmd_vali(char *v_args)
1310 {
1311         char user[128];
1312         int newax;
1313         struct ctdluser userbuf;
1314
1315         extract_token(user, v_args, 0, '|', sizeof user);
1316         newax = extract_int(v_args, 1);
1317
1318         if (CtdlAccessCheck(ac_aide)) {
1319                 return;
1320         }
1321
1322         if (lgetuser(&userbuf, user) != 0) {
1323                 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1324                 return;
1325         }
1326
1327         userbuf.axlevel = newax;
1328         userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1329
1330         lputuser(&userbuf);
1331
1332         /* If the access level was set to zero, delete the user */
1333         if (newax == 0) {
1334                 if (purge_user(user) == 0) {
1335                         cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1336                         return;
1337                 }
1338         }
1339         cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1340 }
1341
1342
1343
1344 /* 
1345  *  Traverse the user file...
1346  */
1347 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1348                  void *in_data)
1349 {
1350         struct ctdluser usbuf;
1351         struct cdbdata *cdbus;
1352
1353         cdb_rewind(CDB_USERS);
1354
1355         while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1356                 memset(&usbuf, 0, sizeof(struct ctdluser));
1357                 memcpy(&usbuf, cdbus->ptr,
1358                        ((cdbus->len > sizeof(struct ctdluser)) ?
1359                         sizeof(struct ctdluser) : cdbus->len));
1360                 cdb_free(cdbus);
1361                 (*CallBack) (&usbuf, in_data);
1362         }
1363 }
1364
1365
1366 /*
1367  * List one user (this works with cmd_list)
1368  */
1369 void ListThisUser(struct ctdluser *usbuf, void *data)
1370 {
1371         char *searchstring;
1372
1373         searchstring = (char *)data;
1374         if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1375                 return;
1376         }
1377
1378         if (usbuf->axlevel > 0) {
1379                 if ((CC->user.axlevel >= 6)
1380                     || ((usbuf->flags & US_UNLISTED) == 0)
1381                     || ((CC->internal_pgm))) {
1382                         cprintf("%s|%d|%ld|%ld|%ld|%ld|",
1383                                 usbuf->fullname,
1384                                 usbuf->axlevel,
1385                                 usbuf->usernum,
1386                                 (long)usbuf->lastcall,
1387                                 usbuf->timescalled,
1388                                 usbuf->posted);
1389                         if (CC->user.axlevel >= 6)
1390                                 cprintf("%s", usbuf->password);
1391                         cprintf("\n");
1392                 }
1393         }
1394 }
1395
1396 /* 
1397  *  List users (searchstring may be empty to list all users)
1398  */
1399 void cmd_list(char *cmdbuf)
1400 {
1401         char searchstring[256];
1402         extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
1403         striplt(searchstring);
1404         cprintf("%d \n", LISTING_FOLLOWS);
1405         ForEachUser(ListThisUser, (void *)searchstring );
1406         cprintf("000\n");
1407 }
1408
1409
1410
1411
1412 /*
1413  * assorted info we need to check at login
1414  */
1415 void cmd_chek(void)
1416 {
1417         int mail = 0;
1418         int regis = 0;
1419         int vali = 0;
1420
1421         if (CtdlAccessCheck(ac_logged_in)) {
1422                 return;
1423         }
1424
1425         getuser(&CC->user, CC->curr_user);      /* no lock is needed here */
1426         if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1427                 regis = 1;
1428
1429         if (CC->user.axlevel >= 6) {
1430                 get_control();
1431                 if (CitControl.MMflags & MM_VALID)
1432                         vali = 1;
1433         }
1434
1435         /* check for mail */
1436         mail = InitialMailCheck();
1437
1438         cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1439 }
1440
1441
1442 /*
1443  * check to see if a user exists
1444  */
1445 void cmd_qusr(char *who)
1446 {
1447         struct ctdluser usbuf;
1448
1449         if (getuser(&usbuf, who) == 0) {
1450                 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1451         } else {
1452                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1453         }
1454 }
1455
1456
1457 /*
1458  * Administrative Get User Parameters
1459  */
1460 void cmd_agup(char *cmdbuf)
1461 {
1462         struct ctdluser usbuf;
1463         char requested_user[128];
1464
1465         if (CtdlAccessCheck(ac_aide)) {
1466                 return;
1467         }
1468
1469         extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1470         if (getuser(&usbuf, requested_user) != 0) {
1471                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1472                 return;
1473         }
1474         cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1475                 CIT_OK,
1476                 usbuf.fullname,
1477                 usbuf.password,
1478                 usbuf.flags,
1479                 usbuf.timescalled,
1480                 usbuf.posted,
1481                 (int) usbuf.axlevel,
1482                 usbuf.usernum,
1483                 (long)usbuf.lastcall,
1484                 usbuf.USuserpurge);
1485 }
1486
1487
1488
1489 /*
1490  * Administrative Set User Parameters
1491  */
1492 void cmd_asup(char *cmdbuf)
1493 {
1494         struct ctdluser usbuf;
1495         char requested_user[128];
1496         char notify[SIZ];
1497         int np;
1498         int newax;
1499         int deleted = 0;
1500
1501         if (CtdlAccessCheck(ac_aide))
1502                 return;
1503
1504         extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1505         if (lgetuser(&usbuf, requested_user) != 0) {
1506                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1507                 return;
1508         }
1509         np = num_parms(cmdbuf);
1510         if (np > 1)
1511                 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
1512         if (np > 2)
1513                 usbuf.flags = extract_int(cmdbuf, 2);
1514         if (np > 3)
1515                 usbuf.timescalled = extract_int(cmdbuf, 3);
1516         if (np > 4)
1517                 usbuf.posted = extract_int(cmdbuf, 4);
1518         if (np > 5) {
1519                 newax = extract_int(cmdbuf, 5);
1520                 if ((newax >= 0) && (newax <= 6)) {
1521                         usbuf.axlevel = extract_int(cmdbuf, 5);
1522                 }
1523         }
1524         if (np > 7) {
1525                 usbuf.lastcall = extract_long(cmdbuf, 7);
1526         }
1527         if (np > 8) {
1528                 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1529         }
1530         lputuser(&usbuf);
1531         if (usbuf.axlevel == 0) {
1532                 if (purge_user(requested_user) == 0) {
1533                         deleted = 1;
1534                 }
1535         }
1536
1537         if (deleted) {
1538                 sprintf(notify, "User <%s> deleted by %s\n",
1539                         usbuf.fullname, CC->user.fullname);
1540                 aide_message(notify);
1541         }
1542
1543         cprintf("%d Ok", CIT_OK);
1544         if (deleted)
1545                 cprintf(" (%s deleted)", requested_user);
1546         cprintf("\n");
1547 }
1548
1549
1550
1551 /*
1552  * Check to see if the user who we just sent mail to is logged in.  If yes,
1553  * bump the 'new mail' counter for their session.  That enables them to
1554  * receive a new mail notification without having to hit the database.
1555  */
1556 void BumpNewMailCounter(long which_user) {
1557         struct CitContext *ptr;
1558
1559         begin_critical_section(S_SESSION_TABLE);
1560
1561         for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1562                 if (ptr->user.usernum == which_user) {
1563                         ptr->newmail += 1;
1564                 }
1565         }
1566
1567         end_critical_section(S_SESSION_TABLE);
1568 }
1569
1570
1571 /*
1572  * Count the number of new mail messages the user has
1573  */
1574 int NewMailCount()
1575 {
1576         int num_newmsgs = 0;
1577
1578         num_newmsgs = CC->newmail;
1579         CC->newmail = 0;
1580
1581         return (num_newmsgs);
1582 }
1583
1584
1585 /*
1586  * Count the number of new mail messages the user has
1587  */
1588 int InitialMailCheck()
1589 {
1590         int num_newmsgs = 0;
1591         int a;
1592         char mailboxname[ROOMNAMELEN];
1593         struct ctdlroom mailbox;
1594         struct visit vbuf;
1595         struct cdbdata *cdbfr;
1596         long *msglist = NULL;
1597         int num_msgs = 0;
1598
1599         MailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1600         if (getroom(&mailbox, mailboxname) != 0)
1601                 return (0);
1602         CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1603
1604         cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1605
1606         if (cdbfr != NULL) {
1607                 msglist = malloc(cdbfr->len);
1608                 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1609                 num_msgs = cdbfr->len / sizeof(long);
1610                 cdb_free(cdbfr);
1611         }
1612         if (num_msgs > 0)
1613                 for (a = 0; a < num_msgs; ++a) {
1614                         if (msglist[a] > 0L) {
1615                                 if (msglist[a] > vbuf.v_lastseen) {
1616                                         ++num_newmsgs;
1617                                 }
1618                         }
1619                 }
1620         if (msglist != NULL)
1621                 free(msglist);
1622
1623         return (num_newmsgs);
1624 }
1625
1626
1627
1628 /*
1629  * Set the preferred view for the current user/room combination
1630  */
1631 void cmd_view(char *cmdbuf) {
1632         int requested_view;
1633         struct visit vbuf;
1634
1635         if (CtdlAccessCheck(ac_logged_in)) {
1636                 return;
1637         }
1638
1639         requested_view = extract_int(cmdbuf, 0);
1640
1641         CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1642         vbuf.v_view = requested_view;
1643         CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1644         
1645         cprintf("%d ok\n", CIT_OK);
1646 }