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