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