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