]> code.citadel.org Git - citadel.git/blobdiff - citadel/user_ops.c
Changeover to new room structure. See ChangeLog for details.
[citadel.git] / citadel / user_ops.c
index 1ca3b5291907cbaf081c467a4b46a46da5d127fc..4efd6b7f0c2e488a9441693f6889ed697cf9d78d 100644 (file)
@@ -2,6 +2,8 @@
 #define _XOPEN_SOURCE
 /* needed for str[n]casecmp() on some systems if the above is defined */
 #define _XOPEN_SOURCE_EXTENDED
+/* needed to enable threads on some systems if the above are defined */
+#define _POSIX_C_SOURCE 199506L
 
 #include <stdlib.h>
 #include <unistd.h>
@@ -13,6 +15,7 @@
 #include <sys/time.h>
 #include <string.h>
 #include <syslog.h>
+#include <limits.h>
 #include <pthread.h>
 #include "citadel.h"
 #include "server.h"
@@ -27,6 +30,7 @@
 #include "msgbase.h"
 #include "config.h"
 #include "dynloader.h"
+#include "sysdep.h"
 
 
 /*
@@ -100,6 +104,105 @@ void lputuser(struct usersupp *usbuf, char *name) {
        }
 
 
+/*
+ * Define a relationship between a user and a room
+ */
+void CtdlSetRelationship(struct visit *newvisit,
+                       struct usersupp *rel_user,
+                       struct quickroom *rel_room) {
+
+       struct cdbdata *cdbvisit;
+       struct visit *visits;
+       int num_visits;
+       int a;
+       int replaced = 0;
+
+       cdbvisit = cdb_fetch(CDB_VISIT, &rel_user->usernum, sizeof(long));
+       if (cdbvisit != NULL) {
+               num_visits = cdbvisit->len / sizeof(struct visit);
+               visits = (struct visit *)
+                       malloc(num_visits * sizeof(struct visit));
+               memcpy(visits, cdbvisit->ptr,
+                       (num_visits * sizeof(struct visit)));
+               cdb_free(cdbvisit);
+               }
+       else {
+               num_visits = 0;
+               visits = NULL;
+               }
+
+       /* Replace an existing relationship if possible */
+       if (num_visits > 0) for (a=0; a<num_visits; ++a) {
+               if ( (!strcasecmp(visits[a].v_roomname, rel_room->QRname))
+                  && (visits[a].v_generation == rel_room->QRgen) ) {
+                       memcpy(&visits[a], newvisit, sizeof(struct visit));
+                       replaced = 1;
+                       }
+               }
+
+       /* Otherwise, define a new one */
+       if (replaced == 0) {
+               ++num_visits;
+               visits = realloc(visits, 
+                       (num_visits * sizeof(struct visit)));
+               memcpy(&visits[num_visits-1], newvisit, sizeof(struct visit));
+               }
+
+       /* Now write the relationship back to disk */
+       cdb_store(CDB_VISIT,
+               &rel_user->usernum, sizeof(long),
+               visits,
+               (num_visits * sizeof(struct visit)));
+       free(visits);
+       }
+
+/*
+ * Locate a relationship between a user and a room
+ */
+void CtdlGetRelationship(struct visit *vbuf,
+                       struct usersupp *rel_user,
+                       struct quickroom *rel_room) {
+
+       struct cdbdata *cdbvisit;
+       struct visit *visits;
+       int num_visits;
+       int a;
+
+       bzero(vbuf, sizeof(struct visit));
+       strcpy(vbuf->v_roomname, rel_room->QRname);
+       vbuf->v_generation = rel_room->QRgen;
+
+       cdbvisit = cdb_fetch(CDB_VISIT, &rel_user->usernum, sizeof(long));
+       if (cdbvisit != NULL) {
+               if ((num_visits = cdbvisit->len / sizeof(struct visit)) == 0) {
+                       cdb_free(cdbvisit);
+                       return;
+               }
+               visits = (struct visit *)
+                       malloc(num_visits * sizeof(struct visit));
+               memcpy(visits, cdbvisit->ptr,
+                       (num_visits * sizeof(struct visit)));
+               cdb_free(cdbvisit);
+               }
+       else return;
+
+       for (a=0; a<num_visits; ++a) {
+       
+               if ( (!strcasecmp(visits[a].v_roomname, rel_room->QRname))
+                  && (visits[a].v_generation == rel_room->QRgen) ) {
+                       memcpy(vbuf, &visits[a], sizeof(struct visit));
+                       }
+               }
+       
+       free(visits);
+       }
+
+
+void MailboxName(char *buf, struct usersupp *who, char *prefix) {
+       sprintf(buf, "%010ld.%s", who->usernum, prefix);
+       }
+
+       
 /*
  * Is the user currently logged in an Aide?
  */
@@ -199,9 +302,6 @@ void cmd_user(char *cmdbuf)
  * session startup code which is common to both cmd_pass() and cmd_newu()
  */
 void session_startup(void) {
-       int a;
-       struct quickroom qr;
-
        syslog(LOG_NOTICE,"user <%s> logged in",CC->curr_user);
 
        lgetuser(&CC->usersupp,CC->curr_user);
@@ -220,18 +320,6 @@ void session_startup(void) {
                CC->usersupp.axlevel = 6;
                }
 
-/* A room's generation number changes each time it is recycled. Users are kept
- * out of private rooms or forget rooms by matching the generation numbers. To
- * avoid an accidental matchup, unmatched numbers are set to -1 here.
- */
-       for (a=0; a<MAXROOMS; ++a) {
-               getroom(&qr,a);
-               if (CC->usersupp.generation[a] != qr.QRgen)
-                                       CC->usersupp.generation[a]=(-1);
-               if (CC->usersupp.forget[a] != qr.QRgen)
-                                       CC->usersupp.forget[a]=(-1);
-               }
-
        lputuser(&CC->usersupp,CC->curr_user);
 
         /* Run any cleanup routines registered by loadable modules */
@@ -240,7 +328,7 @@ void session_startup(void) {
        cprintf("%d %s|%d|%d|%d|%u|%ld\n",OK,CC->usersupp.fullname,CC->usersupp.axlevel,
                CC->usersupp.timescalled,CC->usersupp.posted,CC->usersupp.flags,
                CC->usersupp.usernum);
-       usergoto(0,0);          /* Enter the lobby */   
+       usergoto(BASEROOM,0);           /* Enter the lobby */   
        rec_log(CL_LOGIN,CC->curr_user);
        }
 
@@ -335,6 +423,9 @@ int purge_user(char *pname) {
 
        /* FIX   Don't delete a user who is currently logged in. */
 
+       /* Perform any purge functions registered by server extensions */
+       PerformUserHooks(usbuf.fullname, usbuf.usernum, EVT_PURGEUSER);
+
        /* delete any messages in the user's mailbox */
        cdbmb = cdb_fetch(CDB_MAILBOXES, &usbuf.usernum, sizeof(long));
        if (cdbmb != NULL) {
@@ -348,6 +439,8 @@ int purge_user(char *pname) {
                cdb_delete(CDB_MAILBOXES, &usbuf.usernum, sizeof(long));
                }
 
+       /* delete any existing user/room relationships */
+       cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
 
        /* delete the userlog entry */
        cdb_delete(CDB_USERSUPP, pname, strlen(pname));
@@ -373,6 +466,7 @@ int create_user(char *newusername)
        int a;
        struct passwd *p = NULL;
        char username[64];
+       char mailboxname[ROOMNAMELEN];
 
        strcpy(username, newusername);
        strproc(username);
@@ -397,14 +491,8 @@ int create_user(char *newusername)
 
        strcpy(CC->curr_user,username);
        strcpy(CC->usersupp.fullname,username);
-       (CC->logged_in) = 1;
-
-       for (a=0; a<MAXROOMS; ++a) {
-               CC->usersupp.lastseen[a]=0L;
-               CC->usersupp.generation[a]=(-1);
-               CC->usersupp.forget[a]=(-1);
-               }
        strcpy(CC->usersupp.password,"");
+       (CC->logged_in) = 1;
 
        /* These are the default flags on new accounts */
        CC->usersupp.flags =
@@ -412,7 +500,7 @@ int create_user(char *newusername)
 
        CC->usersupp.timescalled = 0;
        CC->usersupp.posted = 0;
-       CC->usersupp.axlevel = INITAX;
+       CC->usersupp.axlevel = config.c_initax;
        CC->usersupp.USscreenwidth = 80;
        CC->usersupp.USscreenheight = 24;
        time(&CC->usersupp.lastcall);
@@ -435,6 +523,11 @@ int create_user(char *newusername)
        if (getuser(&CC->usersupp,CC->curr_user)) {
                return(ERROR+INTERNAL_ERROR);
                }
+       
+       /* give the user a private mailbox */
+       MailboxName(mailboxname, &CC->usersupp, MAILROOM);
+       create_room(mailboxname, 4, "", 0);
+
        rec_log(CL_NEWUSER,CC->curr_user);
        return(0);
        }
@@ -572,29 +665,26 @@ void cmd_setu(char *new_parms)
 void cmd_slrp(char *new_ptr)
 {
        long newlr;
+       struct visit vbuf;
 
        if (!(CC->logged_in)) {
                cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
                return;
                }
 
-       if (CC->curr_rm < 0) {
-               cprintf("%d No current room.\n",ERROR);
-               return;
-               }
-
        if (!strncasecmp(new_ptr,"highest",7)) {
                newlr = CC->quickroom.QRhighest;
-/* FIX ... if the current room is 1 (Mail), newlr needs to be set to the
- * number of the highest mail message
- */
                }
        else {
                newlr = atol(new_ptr);
                }
 
        lgetuser(&CC->usersupp, CC->curr_user);
-       CC->usersupp.lastseen[CC->curr_rm] = newlr;
+
+       CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
+       vbuf.v_lastseen = newlr;
+       CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
+
        lputuser(&CC->usersupp, CC->curr_user);
        cprintf("%d %ld\n",OK,newlr);
        }
@@ -608,23 +698,21 @@ void cmd_invt_kick(char *iuser, int op)
         {              /* 1 = invite, 0 = kick out */
        struct usersupp USscratch;
        char bbb[256];
+       struct visit vbuf;
 
        if (!(CC->logged_in)) {
                cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
                return;
                }
 
-       if (CC->curr_rm < 0) {
-               cprintf("%d No current room.\n",ERROR);
-               return;
-               }
-
        if (is_room_aide()==0) {
                cprintf("%d Higher access required.\n",
                        ERROR+HIGHER_ACCESS_REQUIRED);
                return;
                }
 
+       /* FIX - with the new relationships scheme we can lock users out,
+          so it'll make sense to remove this routine */
        if ( (op==1) && ((CC->quickroom.QRflags&QR_PRIVATE)==0) ) {
                cprintf("%d Not a private room.\n",ERROR+NOT_HERE);
                return;
@@ -635,16 +723,20 @@ void cmd_invt_kick(char *iuser, int op)
                return;
                }
 
+       CtdlGetRelationship(&vbuf, &USscratch, &CC->quickroom);
+
        if (op==1) {
-               USscratch.generation[CC->curr_rm]=CC->quickroom.QRgen;
-               USscratch.forget[CC->curr_rm]=(-1);
+               vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
+               vbuf.v_flags = vbuf.v_flags | V_ACCESS;
                }
 
        if (op==0) {
-               USscratch.generation[CC->curr_rm]=(-1);
-               USscratch.forget[CC->curr_rm]=CC->quickroom.QRgen;
+               vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
+               vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
                }
 
+       CtdlSetRelationship(&vbuf, &USscratch, &CC->quickroom);
+
        lputuser(&USscratch,iuser);
 
        /* post a message in Aide> saying what we just did */
@@ -669,32 +761,27 @@ void cmd_invt_kick(char *iuser, int op)
  * forget (Zap) the current room
  */
 void cmd_forg(void) {
+       struct visit vbuf;
+
        if (!(CC->logged_in)) {
                cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
                return;
                }
 
-       if (CC->curr_rm < 0) {
-               cprintf("%d No current room.\n",ERROR);
-               return;
-               }
-
-       if (CC->curr_rm < 3) {
-               cprintf("%d You cannot forget this room.\n",ERROR+NOT_HERE);
-               return;
-               }
-
        if (is_aide()) {
                cprintf("%d Aides cannot forget rooms.\n",ERROR);
                return;
                }
 
        lgetuser(&CC->usersupp,CC->curr_user);
-       CC->usersupp.forget[CC->curr_rm] = CC->quickroom.QRgen;
-       CC->usersupp.generation[CC->curr_rm] = (-1);
+       CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
+
+       vbuf.v_flags = vbuf.v_flags | V_FORGET;
+
+       CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
        lputuser(&CC->usersupp,CC->curr_user);
        cprintf("%d Ok\n",OK);
-       CC->curr_rm = (-1);
+       usergoto(BASEROOM, 0);
        }
 
 /*
@@ -1205,14 +1292,22 @@ int NewMailCount() {
        int num_mails;
        long *mailbox;
        int a;
+       char mailboxname[32];
+
+       MailboxName(mailboxname, &CC->usersupp, MAILROOM);
+       for (a=0; a<=strlen(mailboxname); ++a) {
+               mailboxname[a] = tolower(mailboxname[a]);
+               }
 
-       cdbmb = cdb_fetch(CDB_MAILBOXES, &CC->usersupp.usernum, sizeof(long));
+       cdbmb = cdb_fetch(CDB_MAILBOXES, mailboxname, strlen(mailboxname));
        if (cdbmb != NULL) {
                num_mails = cdbmb->len / sizeof(long);
                mailbox = (long *) cdbmb->ptr;
                if (num_mails > 0) for (a=0; a<num_mails; ++a) {
-                       if (mailbox[a] > (CC->usersupp.lastseen[1]))
+                       /*
+                       if (message is new FIX FIX FIX)
                                ++num_newmsgs;
+                       */
                        }
                cdb_free(cdbmb);
                }