]> code.citadel.org Git - citadel.git/blobdiff - citadel/user_ops.c
* fix for above
[citadel.git] / citadel / user_ops.c
index cedb01e4af5d403db8a9b2441aadf388f313e148..b9cef9627601a018da8a87da14190d309e7387c7 100644 (file)
@@ -81,9 +81,6 @@ int getuser(struct usersupp *usbuf, char name[])
                sizeof(struct usersupp) : cdbus->len));
        cdb_free(cdbus);
 
-       if (usbuf->version < 573) {
-               CC->usersupp.moderation_filter = config.c_default_filter;
-       }
        return (0);
 }
 
@@ -229,17 +226,23 @@ void CtdlGetRelationship(struct visit *vbuf,
                        sizeof(struct visit) : cdbvisit->len));
                cdb_free(cdbvisit);
        }
+       else {
+               /* If this is the first time the user has seen this room,
+                * set the view to be the default for the room.
+                */
+               vbuf->v_view = rel_room->QRdefaultview;
+       }
 
        /* Set v_seen if necessary */
        if (vbuf->v_seen[0] == 0) {
-               sprintf(vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
+               snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
        }
 }
 
 
-void MailboxName(char *buf, struct usersupp *who, char *prefix)
+void MailboxName(char *buf, size_t n, const struct usersupp *who, const char *prefix)
 {
-       sprintf(buf, "%010ld.%s", who->usernum, prefix);
+       snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
 }
 
 
@@ -400,21 +403,27 @@ void session_startup(void)
        }
        lputuser(&CC->usersupp);
 
-       /* Run any cleanup routines registered by loadable modules */
+       /* Run any startup routines registered by loadable modules */
        PerformSessionHooks(EVT_LOGIN);
 
-       usergoto(BASEROOM, 0, NULL, NULL);      /* Enter the lobby */
+       /* Create any personal rooms required by the system */
+       create_room(SENTITEMS, 4, "", 0, 1, 0);
+
+       /* Enter the lobby */
+       usergoto(BASEROOM, 0, NULL, NULL);
+
+       /* Record this login in the Citadel log */
        rec_log(CL_LOGIN, CC->curr_user);
 }
 
 
 void logged_in_response(void)
 {
-       cprintf("%d %s|%d|%d|%d|%u|%ld\n",
-               OK, CC->usersupp.fullname, CC->usersupp.axlevel,
+       cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
+               CIT_OK, CC->usersupp.fullname, CC->usersupp.axlevel,
                CC->usersupp.timescalled, CC->usersupp.posted,
-               CC->usersupp.flags,
-               CC->usersupp.usernum);
+               CC->usersupp.flags, CC->usersupp.usernum,
+               CC->usersupp.lastcall);
 }
 
 
@@ -448,6 +457,14 @@ void logout(struct CitContext *who)
                network_talking_to(who->net_node, NTT_REMOVE);
        }
 
+       /*
+        * Yes, we really need to free EVERY LAST BYTE we allocated.
+        */
+       if (who->cs_inet_email != NULL) {
+               phree(who->cs_inet_email);
+               who->cs_inet_email = NULL;
+       }
+
        /* Do modular stuff... */
        PerformSessionHooks(EVT_LOGOUT);
 }
@@ -465,13 +482,13 @@ static int validpw(uid_t uid, const char *pass)
 
        if (pipe(pipev)) {
                lprintf(1, "pipe failed (%s): denying autologin access for "
-                       "uid %u\n", strerror(errno), uid);
+                       "uid %ld\n", strerror(errno), (long)uid);
                return 0;
        }
        switch (pid = fork()) {
        case -1:
                lprintf(1, "fork failed (%s): denying autologin access for "
-                       "uid %u\n", strerror(errno), uid);
+                       "uid %ld\n", strerror(errno), (long)uid);
                close(pipev[0]);
                close(pipev[1]);
                return 0;
@@ -490,7 +507,8 @@ static int validpw(uid_t uid, const char *pass)
        }
 
        close(pipev[0]);
-       write(pipev[1], buf, sprintf(buf, "%lu\n", (unsigned long) uid));
+       write(pipev[1], buf,
+             snprintf(buf, sizeof buf, "%lu\n", (unsigned long) uid));
        write(pipev[1], pass, strlen(pass));
        write(pipev[1], "\n", 1);
        close(pipev[1]);
@@ -498,8 +516,8 @@ static int validpw(uid_t uid, const char *pass)
        while (waitpid(pid, &status, 0) == -1)
                if (errno != EINTR) {
                        lprintf(1, "waitpid failed (%s): denying autologin "
-                               "access for uid %u\n",
-                               strerror(errno), uid);
+                               "access for uid %ld\n",
+                               strerror(errno), (long)uid);
                        return 0;
                }
        if (WIFEXITED(status) && !WEXITSTATUS(status))
@@ -643,11 +661,11 @@ int purge_user(char pname[])
        cdb_delete(CDB_USERSUPP, lowercase_name, strlen(lowercase_name));
 
        /* remove the user's bio file */
-       sprintf(filename, "./bio/%ld", usbuf.usernum);
+       snprintf(filename, sizeof filename, "./bio/%ld", usbuf.usernum);
        unlink(filename);
 
        /* remove the user's picture */
-       sprintf(filename, "./userpics/%ld.gif", usbuf.usernum);
+       snprintf(filename, sizeof filename, "./userpics/%ld.gif", usbuf.usernum);
        unlink(filename);
 
        return (0);
@@ -656,13 +674,18 @@ int purge_user(char pname[])
 
 /*
  * create_user()  -  back end processing to create a new user
+ *
+ * Set 'newusername' to the desired account name.
+ * Set 'become_user' to nonzero if this is self-service account creation and we want
+ * to actually log in as the user we just created, otherwise set it to 0.
  */
-int create_user(char *newusername)
+int create_user(char *newusername, int become_user)
 {
        struct usersupp usbuf;
-       int a;
        struct passwd *p = NULL;
-       char username[64];
+       char username[SIZ];
+       char mailboxname[ROOMNAMELEN];
+       uid_t uid;
 
        strcpy(username, newusername);
        strproc(username);
@@ -671,50 +694,67 @@ int create_user(char *newusername)
        p = (struct passwd *) getpwnam(username);
 #endif
        if (p != NULL) {
-               strcpy(username, p->pw_gecos);
-               for (a = 0; a < strlen(username); ++a) {
-                       if (username[a] == ',')
-                               username[a] = 0;
-               }
-               CC->usersupp.uid = p->pw_uid;
+               extract_token(username, p->pw_gecos, 0, ',');
+               uid = p->pw_uid;
        } else {
-               CC->usersupp.uid = BBSUID;
+               uid = BBSUID;
        }
 
        if (!getuser(&usbuf, username)) {
                return (ERROR + ALREADY_EXISTS);
        }
-       strcpy(CC->curr_user, username);
-       strcpy(CC->usersupp.fullname, username);
-       strcpy(CC->usersupp.password, "");
-       (CC->logged_in) = 1;
+
+       /* Go ahead and initialize a new user record */
+       memset(&usbuf, 0, sizeof(struct usersupp));
+       strcpy(usbuf.fullname, username);
+       strcpy(usbuf.password, "");
+       usbuf.uid = uid;
 
        /* These are the default flags on new accounts */
-       CC->usersupp.flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
+       usbuf.flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
 
-       CC->usersupp.timescalled = 0;
-       CC->usersupp.posted = 0;
-       CC->usersupp.axlevel = config.c_initax;
-       CC->usersupp.USscreenwidth = 80;
-       CC->usersupp.USscreenheight = 24;
-       time(&CC->usersupp.lastcall);
-       CC->usersupp.moderation_filter = config.c_default_filter;
+       usbuf.timescalled = 0;
+       usbuf.posted = 0;
+       usbuf.axlevel = config.c_initax;
+       usbuf.USscreenwidth = 80;
+       usbuf.USscreenheight = 24;
+       usbuf.lastcall = time(NULL);
 
        /* fetch a new user number */
-       CC->usersupp.usernum = get_new_user_number();
+       usbuf.usernum = get_new_user_number();
 
-       if (CC->usersupp.usernum == 1L) {
-               CC->usersupp.axlevel = 6;
+       /* The very first user created on the system will always be an Aide */
+       if (usbuf.usernum == 1L) {
+               usbuf.axlevel = 6;
        }
+
        /* add user to userlog */
-       putuser(&CC->usersupp);
-       if (getuser(&CC->usersupp, CC->curr_user)) {
-               return (ERROR + INTERNAL_ERROR);
+       putuser(&usbuf);
+
+       /* give the user a private mailbox and a configuration room */
+       MailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
+       create_room(mailboxname, 5, "", 0, 1, 1);
+       MailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
+       create_room(mailboxname, 5, "", 0, 1, 1);
+
+       /* Everything below this line can be bypassed if administratively
+          creating a user, instead of doing self-service account creation
+        */
+
+       if (become_user) {
+               /* Now become the user we just created */
+               memcpy(&CC->usersupp, &usbuf, sizeof(struct usersupp));
+               strcpy(CC->curr_user, username);
+               CC->logged_in = 1;
+       
+               /* Check to make sure we're still who we think we are */
+               if (getuser(&CC->usersupp, CC->curr_user)) {
+                       return (ERROR + INTERNAL_ERROR);
+               }
+       
+               rec_log(CL_NEWUSER, CC->curr_user);
        }
-       /* give the user a private mailbox */
-       create_room(MAILROOM, 4, "", 0, 1);
 
-       rec_log(CL_NEWUSER, CC->curr_user);
        return (0);
 }
 
@@ -722,18 +762,24 @@ int create_user(char *newusername)
 
 
 /*
- * cmd_newu()  -  create a new user account
+ * cmd_newu()  -  create a new user account and log in as that user
  */
 void cmd_newu(char *cmdbuf)
 {
        int a;
        char username[SIZ];
 
-       if ((CC->logged_in)) {
+       if (config.c_disable_newu) {
+               cprintf("%d Self-service user account creation "
+                       "is disabled on this system.\n", ERROR);
+               return;
+       }
+
+       if (CC->logged_in) {
                cprintf("%d Already logged in.\n", ERROR);
                return;
        }
-       if ((CC->nologin)) {
+       if (CC->nologin) {
                cprintf("%d %s: Too many users are already online (maximum is %d)\n",
                        ERROR + MAX_SESSIONS_EXCEEDED,
                        config.c_nodename, config.c_maxsessions);
@@ -746,14 +792,20 @@ void cmd_newu(char *cmdbuf)
                cprintf("%d You must supply a user name.\n", ERROR);
                return;
        }
-       a = create_user(username);
+
        if ((!strcasecmp(username, "bbs")) ||
            (!strcasecmp(username, "new")) ||
            (!strcasecmp(username, "."))) {
-               cprintf("%d '%s' is an invalid login name.\n", ERROR);
+               cprintf("%d '%s' is an invalid login name.\n", ERROR, username);
                return;
        }
-       if (a == ERROR + ALREADY_EXISTS) {
+
+       a = create_user(username, 1);
+
+       if (a == 0) {
+               session_startup();
+               logged_in_response();
+       } else if (a == ERROR + ALREADY_EXISTS) {
                cprintf("%d '%s' already exists.\n",
                        ERROR + ALREADY_EXISTS, username);
                return;
@@ -761,9 +813,6 @@ void cmd_newu(char *cmdbuf)
                cprintf("%d Internal error - user record disappeared?\n",
                        ERROR + INTERNAL_ERROR);
                return;
-       } else if (a == 0) {
-               session_startup();
-               logged_in_response();
        } else {
                cprintf("%d unknown error\n", ERROR);
        }
@@ -777,8 +826,9 @@ void cmd_newu(char *cmdbuf)
  */
 void cmd_setp(char *new_pw)
 {
-       if (CtdlAccessCheck(ac_logged_in))
+       if (CtdlAccessCheck(ac_logged_in)) {
                return;
+       }
 
        if (CC->usersupp.uid != BBSUID) {
                cprintf("%d Not allowed.  Use the 'passwd' command.\n", ERROR);
@@ -786,17 +836,56 @@ void cmd_setp(char *new_pw)
        }
        strproc(new_pw);
        if (strlen(new_pw) == 0) {
-               cprintf("%d Password unchanged.\n", OK);
+               cprintf("%d Password unchanged.\n", CIT_OK);
                return;
        }
        lgetuser(&CC->usersupp, CC->curr_user);
        strcpy(CC->usersupp.password, new_pw);
        lputuser(&CC->usersupp);
-       cprintf("%d Password changed.\n", OK);
+       cprintf("%d Password changed.\n", CIT_OK);
        rec_log(CL_PWCHANGE, CC->curr_user);
        PerformSessionHooks(EVT_SETPASS);
 }
 
+
+/*
+ * cmd_creu()  -  administratively create a new user account (do not log in to it)
+ */
+void cmd_creu(char *cmdbuf)
+{
+       int a;
+       char username[SIZ];
+
+       if (CtdlAccessCheck(ac_aide)) {
+               return;
+       }
+
+       extract(username, cmdbuf, 0);
+       username[25] = 0;
+       strproc(username);
+
+       if (strlen(username) == 0) {
+               cprintf("%d You must supply a user name.\n", ERROR);
+               return;
+       }
+
+       a = create_user(username, 0);
+
+       if (a == 0) {
+               cprintf("%d ok\n", CIT_OK);
+               return;
+       } else if (a == ERROR + ALREADY_EXISTS) {
+               cprintf("%d '%s' already exists.\n",
+                       ERROR + ALREADY_EXISTS, username);
+               return;
+       } else {
+               cprintf("%d An error occured creating the user account.\n", ERROR);
+       }
+       rec_log(CL_NEWUSER, username);
+}
+
+
+
 /*
  * get user parameters
  */
@@ -807,12 +896,11 @@ void cmd_getu(void)
                return;
 
        getuser(&CC->usersupp, CC->curr_user);
-       cprintf("%d %d|%d|%d|%d\n",
-               OK,
+       cprintf("%d %d|%d|%d|\n",
+               CIT_OK,
                CC->usersupp.USscreenwidth,
                CC->usersupp.USscreenheight,
-               (CC->usersupp.flags & US_USER_SET),
-               CC->usersupp.moderation_filter
+               (CC->usersupp.flags & US_USER_SET)
            );
 }
 
@@ -821,8 +909,6 @@ void cmd_getu(void)
  */
 void cmd_setu(char *new_parms)
 {
-       int new_mod;
-
        if (CtdlAccessCheck(ac_logged_in))
                return;
 
@@ -837,27 +923,8 @@ void cmd_setu(char *new_parms)
        CC->usersupp.flags = CC->usersupp.flags |
            (extract_int(new_parms, 2) & US_USER_SET);
 
-       if (num_parms(new_parms) >= 4) {
-               new_mod = extract_int(new_parms, 3);
-               lprintf(9, "new_mod extracted to %d\n", new_mod);
-
-               /* Aides cannot set the filter level lower than -100 */
-               if (new_mod < (-100))
-                       new_mod = -100;
-
-               /* Normal users cannot set the filter level lower than -63 */
-               if ((new_mod < (-63)) && (CC->usersupp.axlevel < 6))
-                       new_mod = -63;
-
-               /* Nobody can set the filter level higher than +63 */
-               if (new_mod > 63)
-                       new_mod = 63;
-
-               CC->usersupp.moderation_filter = new_mod;
-               lprintf(9, "new_mod processed to %d\n", new_mod);
-       }
        lputuser(&CC->usersupp);
-       cprintf("%d Ok\n", OK);
+       cprintf("%d Ok\n", CIT_OK);
 }
 
 /*
@@ -882,11 +949,11 @@ void cmd_slrp(char *new_ptr)
 
        CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
        vbuf.v_lastseen = newlr;
-       sprintf(vbuf.v_seen, "*:%ld", newlr);
+       snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
        CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
 
        lputuser(&CC->usersupp);
-       cprintf("%d %ld\n", OK, newlr);
+       cprintf("%d %ld\n", CIT_OK, newlr);
 }
 
 
@@ -907,10 +974,23 @@ void cmd_seen(char *argbuf) {
        target_setting = extract_int(argbuf, 1);
 
        CtdlSetSeen(target_msgnum, target_setting);
-       cprintf("%d OK\n", OK);
+       cprintf("%d OK\n", CIT_OK);
 }
 
 
+void cmd_gtsn(char *argbuf) {
+       char buf[SIZ];
+
+       if (CtdlAccessCheck(ac_logged_in)) {
+               return;
+       }
+
+       CtdlGetSeen(buf);
+       cprintf("%d %s\n", CIT_OK, buf);
+}
+
+
+
 /*
  * INVT and KICK commands
  */
@@ -921,8 +1001,20 @@ void cmd_invt_kick(char *iuser, int op)
        char bbb[SIZ];
        struct visit vbuf;
 
-       if (CtdlAccessCheck(ac_room_aide))
-               return;
+       /*
+        * These commands are only allowed by aides, room aides,
+        * and room namespace owners
+        */
+       if (is_room_aide()
+          || (atol(CC->quickroom.QRname) == CC->usersupp.usernum) ) {
+               /* access granted */
+       }
+       else {
+               /* access denied */
+                cprintf("%d Higher access or room ownership required.\n",
+                        ERROR + HIGHER_ACCESS_REQUIRED);
+                return;
+        }
 
        if (lgetuser(&USscratch, iuser) != 0) {
                cprintf("%d No such user.\n", ERROR);
@@ -943,7 +1035,7 @@ void cmd_invt_kick(char *iuser, int op)
        lputuser(&USscratch);
 
        /* post a message in Aide> saying what we just did */
-       sprintf(bbb, "%s %s %s> by %s\n",
+       snprintf(bbb, sizeof bbb, "%s %s %s> by %s\n",
                iuser,
                ((op == 1) ? "invited to" : "kicked out of"),
                CC->quickroom.QRname,
@@ -951,7 +1043,7 @@ void cmd_invt_kick(char *iuser, int op)
        aide_message(bbb);
 
        cprintf("%d %s %s %s.\n",
-               OK, iuser,
+               CIT_OK, iuser,
                ((op == 1) ? "invited to" : "kicked out of"),
                CC->quickroom.QRname);
        return;
@@ -966,7 +1058,8 @@ int CtdlForgetThisRoom(void) {
        struct visit vbuf;
 
        /* On some systems, Aides are not allowed to forget rooms */
-       if (is_aide() && (config.c_aide_zap == 0)) {
+       if (is_aide() && (config.c_aide_zap == 0)
+          && ((CC->quickroom.QRflags & QR_MAILBOX) == 0)  ) {
                return(1);
        }
 
@@ -997,7 +1090,7 @@ void cmd_forg(void)
        }
 
        if (CtdlForgetThisRoom() == 0) {
-               cprintf("%d Ok\n", OK);
+               cprintf("%d Ok\n", CIT_OK);
        }
        else {
                cprintf("%d You may not forget this room.\n", ERROR);
@@ -1017,7 +1110,7 @@ void cmd_gnur(void)
        }
 
        if ((CitControl.MMflags & MM_VALID) == 0) {
-               cprintf("%d There are no unvalidated users.\n", OK);
+               cprintf("%d There are no unvalidated users.\n", CIT_OK);
                return;
        }
 
@@ -1048,7 +1141,7 @@ void cmd_gnur(void)
        CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
        put_control();
        end_critical_section(S_CONTROL);
-       cprintf("%d *** End of registration.\n", OK);
+       cprintf("%d *** End of registration.\n", CIT_OK);
 
 
 }
@@ -1083,11 +1176,11 @@ void cmd_vali(char *v_args)
        /* If the access level was set to zero, delete the user */
        if (newax == 0) {
                if (purge_user(user) == 0) {
-                       cprintf("%d %s Deleted.\n", OK, userbuf.fullname);
+                       cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
                        return;
                }
        }
-       cprintf("%d User '%s' validated.\n", OK, userbuf.fullname);
+       cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
 }
 
 
@@ -1123,11 +1216,11 @@ void ListThisUser(struct usersupp *usbuf, void *data)
                if ((CC->usersupp.axlevel >= 6)
                    || ((usbuf->flags & US_UNLISTED) == 0)
                    || ((CC->internal_pgm))) {
-                       cprintf("%s|%d|%ld|%ld|%d|%d|",
+                       cprintf("%s|%d|%ld|%ld|%ld|%ld|",
                                usbuf->fullname,
                                usbuf->axlevel,
                                usbuf->usernum,
-                               usbuf->lastcall,
+                               (long)usbuf->lastcall,
                                usbuf->timescalled,
                                usbuf->posted);
                        if (CC->usersupp.axlevel >= 6)
@@ -1174,9 +1267,9 @@ void cmd_chek(void)
        }
 
        /* check for mail */
-       mail = NewMailCount();
+       mail = InitialMailCheck();
 
-       cprintf("%d %d|%d|%d\n", OK, mail, regis, vali);
+       cprintf("%d %d|%d|%d\n", CIT_OK, mail, regis, vali);
 }
 
 
@@ -1188,7 +1281,7 @@ void cmd_qusr(char *who)
        struct usersupp usbuf;
 
        if (getuser(&usbuf, who) == 0) {
-               cprintf("%d %s\n", OK, usbuf.fullname);
+               cprintf("%d %s\n", CIT_OK, usbuf.fullname);
        } else {
                cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
        }
@@ -1212,8 +1305,8 @@ void cmd_agup(char *cmdbuf)
                cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
                return;
        }
-       cprintf("%d %s|%s|%u|%d|%d|%d|%ld|%ld|%d\n",
-               OK,
+       cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
+               CIT_OK,
                usbuf.fullname,
                usbuf.password,
                usbuf.flags,
@@ -1221,7 +1314,7 @@ void cmd_agup(char *cmdbuf)
                usbuf.posted,
                (int) usbuf.axlevel,
                usbuf.usernum,
-               usbuf.lastcall,
+               (long)usbuf.lastcall,
                usbuf.USuserpurge);
 }
 
@@ -1273,50 +1366,107 @@ void cmd_asup(char *cmdbuf)
                        deleted = 1;
                }
        }
-       cprintf("%d Ok", OK);
+       cprintf("%d Ok", CIT_OK);
        if (deleted)
                cprintf(" (%s deleted)", requested_user);
        cprintf("\n");
 }
 
 
+
+/*
+ * Check to see if the user who we just sent mail to is logged in.  If yes,
+ * bump the 'new mail' counter for their session.  That enables them to
+ * receive a new mail notification without having to hit the database.
+ */
+void BumpNewMailCounter(long which_user) {
+       struct CitContext *ptr;
+
+       begin_critical_section(S_SESSION_TABLE);
+
+       for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
+               if (ptr->usersupp.usernum == which_user) {
+                       ptr->newmail += 1;
+               }
+       }
+
+       end_critical_section(S_SESSION_TABLE);
+}
+
+
 /*
  * Count the number of new mail messages the user has
  */
 int NewMailCount()
 {
        int num_newmsgs = 0;
-       int a;
-       char mailboxname[ROOMNAMELEN];
-       struct quickroom mailbox;
-       struct visit vbuf;
-       struct cdbdata *cdbfr;
-       long *msglist = NULL;
-       int num_msgs = 0;
 
-       MailboxName(mailboxname, &CC->usersupp, MAILROOM);
-       if (getroom(&mailbox, mailboxname) != 0)
-               return (0);
-       CtdlGetRelationship(&vbuf, &CC->usersupp, &mailbox);
-
-       cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
-
-       if (cdbfr != NULL) {
-               msglist = mallok(cdbfr->len);
-               memcpy(msglist, cdbfr->ptr, cdbfr->len);
-               num_msgs = cdbfr->len / sizeof(long);
-               cdb_free(cdbfr);
-       }
-       if (num_msgs > 0)
-               for (a = 0; a < num_msgs; ++a) {
-                       if (msglist[a] > 0L) {
-                               if (msglist[a] > vbuf.v_lastseen) {
-                                       ++num_newmsgs;
-                               }
-                       }
-               }
-       if (msglist != NULL)
-               phree(msglist);
+       num_newmsgs = CC->newmail;
+       CC->newmail = 0;
 
        return (num_newmsgs);
 }
+
+
+/*
+ * Count the number of new mail messages the user has
+ */
+int InitialMailCheck()
+{
+        int num_newmsgs = 0;
+        int a;
+        char mailboxname[ROOMNAMELEN];
+        struct quickroom mailbox;
+        struct visit vbuf;
+        struct cdbdata *cdbfr;
+        long *msglist = NULL;
+        int num_msgs = 0;
+
+        MailboxName(mailboxname, sizeof mailboxname, &CC->usersupp, MAILROOM);
+        if (getroom(&mailbox, mailboxname) != 0)
+                return (0);
+        CtdlGetRelationship(&vbuf, &CC->usersupp, &mailbox);
+
+        cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
+
+        if (cdbfr != NULL) {
+                msglist = mallok(cdbfr->len);
+                memcpy(msglist, cdbfr->ptr, cdbfr->len);
+                num_msgs = cdbfr->len / sizeof(long);
+                cdb_free(cdbfr);
+        }
+        if (num_msgs > 0)
+                for (a = 0; a < num_msgs; ++a) {
+                        if (msglist[a] > 0L) {
+                                if (msglist[a] > vbuf.v_lastseen) {
+                                        ++num_newmsgs;
+                                }
+                        }
+                }
+        if (msglist != NULL)
+                phree(msglist);
+
+        return (num_newmsgs);
+}
+
+
+
+/*
+ * Set the preferred view for the current user/room combination
+ */
+void cmd_view(char *cmdbuf) {
+       int requested_view;
+       struct visit vbuf;
+
+       if (CtdlAccessCheck(ac_logged_in)) {
+               return;
+       }
+
+       requested_view = extract_int(cmdbuf, 0);
+
+       CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
+       vbuf.v_view = requested_view;
+       CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
+       
+       cprintf("%d ok\n", CIT_OK);
+}