]> code.citadel.org Git - citadel.git/blobdiff - citadel/context.c
Grabbed that previously noted bad code from rename_user that walked
[citadel.git] / citadel / context.c
index 4985de9e1f6ae36b97a5d4f1500b2906d2c4fc11..3122e5c06b102dd55cc63fc2ef996e25ab18e0f2 100644 (file)
@@ -75,9 +75,8 @@
 citthread_key_t MyConKey;                              /* TSD key for MyContext() */
 
 
-struct CitContext masterCC;
-struct CitContext *ContextList = NULL;
-// struct CitContext* next_session = NULL;
+CitContext masterCC;
+CitContext *ContextList = NULL;
 
 time_t last_purge = 0;                         /* Last dead session purge */
 int num_sessions = 0;                          /* Current number of sessions */
@@ -128,6 +127,30 @@ int CtdlIsSingleUser(void)
 }
 
 
+
+
+/*
+ * Check to see if a user is currently logged in
+ * Take care with what you do as a result of this test.
+ * The user may not have been logged in when this function was called BUT
+ * because of threading the user might be logged in before you test the result.
+ */
+int CtdlIsUserLoggedIn (char *user_name)
+{
+       CitContext *cptr;
+       int ret = 0;
+
+       begin_critical_section (S_SESSION_TABLE);
+       for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
+               if (!strcasecmp(cptr->user.fullname, user_name)) {
+                       ret = 1;
+                       break;
+               }
+       }
+       end_critical_section(S_SESSION_TABLE);
+       return ret;
+}
+
 /*
  * Return a pointer to the CitContext structure bound to the thread which
  * called this function.  If there's no such binding (for example, if it's
@@ -135,11 +158,11 @@ int CtdlIsSingleUser(void)
  *
  * This function is used *VERY* frequently and must be kept small.
  */
-struct CitContext *MyContext(void) {
+CitContext *MyContext(void) {
 
-       register struct CitContext *c;
+       register CitContext *c;
 
-       return ((c = (struct CitContext *) citthread_getspecific(MyConKey),
+       return ((c = (CitContext *) citthread_getspecific(MyConKey),
                c == NULL) ? &masterCC : c
        );
 }
@@ -150,7 +173,7 @@ struct CitContext *MyContext(void) {
 /*
  * Terminate a session.
  */
-void RemoveContext (struct CitContext *con)
+void RemoveContext (CitContext *con)
 {
        if (con==NULL) {
                CtdlLogPrintf(CTDL_ERR,
@@ -186,24 +209,22 @@ void RemoveContext (struct CitContext *con)
 
 
 
-
 /*
  * Initialize a new context and place it in the list.  The session number
  * used to be the PID (which is why it's called cs_pid), but that was when we
  * had one process per session.  Now we just assign them sequentially, starting
  * at 1 (don't change it to 0 because masterCC uses 0).
  */
-struct CitContext *CreateNewContext(void) {
-       struct CitContext *me;
+CitContext *CreateNewContext(void) {
+       CitContext *me;
        static int next_pid = 0;
 
-       me = (struct CitContext *) malloc(sizeof(struct CitContext));
+       me = (CitContext *) malloc(sizeof(CitContext));
        if (me == NULL) {
                CtdlLogPrintf(CTDL_ALERT, "citserver: can't allocate memory!!\n");
                return NULL;
        }
-
-       memset(me, 0, sizeof(struct CitContext));
+       memset(me, 0, sizeof(CitContext));
        
        /* Give the contaxt a name. Hopefully makes it easier to track */
        strcpy (me->user.fullname, "SYS_notauth");
@@ -231,18 +252,18 @@ struct CitContext *CreateNewContext(void) {
 }
 
 
-struct CitContext *CtdlGetContextArray(int *count)
+CitContext *CtdlGetContextArray(int *count)
 {
        int nContexts, i;
-       struct CitContext *nptr, *cptr;
+       CitContext *nptr, *cptr;
        
        nContexts = num_sessions;
-       nptr = malloc(sizeof(struct CitContext) * nContexts);
+       nptr = malloc(sizeof(CitContext) * nContexts);
        if (!nptr)
                return NULL;
        begin_critical_section(S_SESSION_TABLE);
        for (cptr = ContextList, i=0; cptr != NULL && i < nContexts; cptr = cptr->next, i++)
-               memcpy(&nptr[i], cptr, sizeof (struct CitContext));
+               memcpy(&nptr[i], cptr, sizeof (CitContext));
        end_critical_section (S_SESSION_TABLE);
        
        *count = i;
@@ -255,11 +276,11 @@ struct CitContext *CtdlGetContextArray(int *count)
  * This function fills in a context and its user field correctly
  * Then creates/loads that user
  */
-void CtdlFillSystemContext(struct CitContext *context, char *name)
+void CtdlFillSystemContext(CitContext *context, char *name)
 {
        char sysname[USERNAME_SIZE];
 
-       memset(context, 0, sizeof(struct CitContext));
+       memset(context, 0, sizeof(CitContext));
        context->internal_pgm = 1;
        context->cs_pid = 0;
        strcpy (sysname, "SYS_");
@@ -285,8 +306,8 @@ void CtdlFillSystemContext(struct CitContext *context, char *name)
  */
 void context_cleanup(void)
 {
-       struct CitContext *ptr = NULL;
-       struct CitContext *rem = NULL;
+       CitContext *ptr = NULL;
+       CitContext *rem = NULL;
 
        /*
         * Clean up the contexts.
@@ -321,7 +342,7 @@ void context_cleanup(void)
  * no longer does anything that is system-dependent.)
  */
 void kill_session(int session_to_kill) {
-       struct CitContext *ptr;
+       CitContext *ptr;
 
        begin_critical_section(S_SESSION_TABLE);
        for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
@@ -340,8 +361,8 @@ void kill_session(int session_to_kill) {
  * anyway, set "force" to nonzero.
  */
 void dead_session_purge(int force) {
-       struct CitContext *ptr, *ptr2;          /* general-purpose utility pointer */
-       struct CitContext *rem = NULL;  /* list of sessions to be destroyed */
+       CitContext *ptr, *ptr2;         /* general-purpose utility pointer */
+       CitContext *rem = NULL; /* list of sessions to be destroyed */
        
        if (force == 0) {
                if ( (time(NULL) - last_purge) < 5 ) {
@@ -400,7 +421,7 @@ void dead_session_purge(int force) {
  * function initializes it.
  */
 void InitializeMasterCC(void) {
-       memset(&masterCC, 0, sizeof(struct CitContext));
+       memset(&masterCC, 0, sizeof( CitContext));
        masterCC.internal_pgm = 1;
        masterCC.cs_pid = 0;
 }
@@ -411,7 +432,7 @@ void InitializeMasterCC(void) {
 /*
  * Bind a thread to a context.  (It's inline merely to speed things up.)
  */
-INLINE void become_session(struct CitContext *which_con) {
+INLINE void become_session(CitContext *which_con) {
        citthread_setspecific(MyConKey, (void *)which_con );
 }