]> code.citadel.org Git - citadel.git/blobdiff - citadel/context.c
Grabbed a chunk of code out of citserver.c cmd_term() and used it to create
[citadel.git] / citadel / context.c
index 3122e5c06b102dd55cc63fc2ef996e25ab18e0f2..70fdd2795051dc4c6caffd453eba4c3e814c7ddf 100644 (file)
@@ -129,6 +129,66 @@ int CtdlIsSingleUser(void)
 
 
 
+
+/*
+ * Locate a context by its session number and terminate it if the user is able.
+ * User can NOT terminate their current session.
+ * User CAN terminate any other session that has them logged in.
+ * Aide CAN terminate any session except the current one.
+ */
+int CtdlTerminateOtherSession (int session_num)
+{
+       int ret = 0;
+       CitContext *ccptr;
+
+       if (session_num == CC->cs_pid) {
+               return TERM_NOTALLOWED;
+       }
+
+       CtdlLogPrintf(CTDL_DEBUG, "Locating session to kill\n");
+       begin_critical_section(S_SESSION_TABLE);
+       for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
+               if (session_num == ccptr->cs_pid) {
+                       ret |= TERM_FOUND;
+                       if ((ccptr->user.usernum == CC->user.usernum)
+                          || (CC->user.axlevel >= 6)) {
+                               ret |= TERM_ALLOWED;
+                               ccptr->kill_me = 1;
+                       }
+               }
+       }
+       end_critical_section(S_SESSION_TABLE);
+       return ret;
+}
+
+
+
+/*
+ * Check to see if the user who we just sent mail to is logged in.  If yes,
+ * bump the 'new mail' counter for their session.  That enables them to
+ * receive a new mail notification without having to hit the database.
+ */
+void BumpNewMailCounter(long which_user) 
+{
+       CtdlBumpNewMailCounter(which_user);
+}
+
+void CtdlBumpNewMailCounter(long which_user)
+{
+       CitContext *ptr;
+
+       begin_critical_section(S_SESSION_TABLE);
+
+       for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
+               if (ptr->user.usernum == which_user) {
+                       ptr->newmail += 1;
+               }
+       }
+
+       end_critical_section(S_SESSION_TABLE);
+}
+
+
 /*
  * Check to see if a user is currently logged in
  * Take care with what you do as a result of this test.
@@ -151,6 +211,32 @@ int CtdlIsUserLoggedIn (char *user_name)
        return ret;
 }
 
+
+
+/*
+ * Check to see if a user is currently logged in.
+ * Basically same as CtdlIsUserLoggedIn() but uses the user number instead.
+ * 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 CtdlIsUserLoggedInByNum (long usernum)
+{
+       CitContext *cptr;
+       int ret = 0;
+
+       begin_critical_section(S_SESSION_TABLE);
+       for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
+               if (cptr->user.usernum == usernum) {
+                       ret = 1;
+               }
+       }
+       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
@@ -170,6 +256,44 @@ CitContext *MyContext(void) {
 
 
 
+/*
+ * Terminate idle sessions.  This function pounds through the session table
+ * comparing the current time to each session's time-of-last-command.  If an
+ * idle session is found it is terminated, then the search restarts at the
+ * beginning because the pointer to our place in the list becomes invalid.
+ */
+void terminate_idle_sessions(void)
+{
+       CitContext *ccptr;
+       time_t now;
+       int session_to_kill;
+       int killed = 0;
+       int longrunners = 0;
+
+       now = time(NULL);
+       session_to_kill = 0;
+       begin_critical_section(S_SESSION_TABLE);
+       for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
+               if (  (ccptr!=CC)
+               && (config.c_sleeping > 0)
+               && (now - (ccptr->lastcmd) > config.c_sleeping) ) {
+                       if (!ccptr->dont_term) {
+                               ccptr->kill_me = 1;
+                               ++killed;
+                       }
+                       else 
+                               longrunners ++;
+               }
+       }
+       end_critical_section(S_SESSION_TABLE);
+       if (killed > 0)
+               CtdlLogPrintf(CTDL_INFO, "Terminated %d idle sessions\n", killed);
+       if (longrunners > 0)
+               CtdlLogPrintf(CTDL_INFO, "Didn't terminate %d protected idle sessions;\n", killed);
+}
+
+
+
 /*
  * Terminate a session.
  */
@@ -187,7 +311,7 @@ void RemoveContext (CitContext *con)
         *       might make references to "CC" assuming it's the right one.
         */
        become_session(con);
-       logout();
+       CtdlUserLogout();
        PerformSessionHooks(EVT_STOP);
        become_session(NULL);