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