]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/expire/serv_expire.c
Prepared some of the authmode stuff for the imminent addition
[citadel.git] / citadel / modules / expire / serv_expire.c
index 66b46570d54153bd0b202af2b454ca053ceb2b94..eaff4280b9831e6ebcb1da5efc345e70180a0bc0 100644 (file)
@@ -49,6 +49,7 @@
 #include <sys/wait.h>
 #include <string.h>
 #include <limits.h>
+#include <libcitadel.h>
 #include "citadel.h"
 #include "server.h"
 #include "citserver.h"
@@ -60,9 +61,8 @@
 #include "msgbase.h"
 #include "user_ops.h"
 #include "control.h"
-#include "serv_network.h"
-#include "tools.h"
-
+#include "serv_network.h"      /* Needed for defenition of UseTable */
+#include "threads.h"
 
 #include "ctdl_module.h"
 
@@ -460,15 +460,18 @@ int PurgeUsers(void) {
        lprintf(CTDL_DEBUG, "PurgeUsers() called\n");
        users_not_purged = 0;
 
-       if (config.c_auth_mode == 1) {
-               /* host auth mode */
-               ForEachUser(do_uid_user_purge, NULL);
-       }
-       else {
-               /* native auth mode */
-               if (config.c_userpurge > 0) {
-                       ForEachUser(do_user_purge, NULL);
-               }
+       switch(config.c_auth_mode) {
+               case AUTHMODE_NATIVE:
+                       if (config.c_userpurge > 0) {
+                               ForEachUser(do_user_purge, NULL);
+                       }
+                       break;
+               case AUTHMODE_HOST:
+                       ForEachUser(do_uid_user_purge, NULL);
+                       break;
+               default:
+                       lprintf(CTDL_DEBUG, "Unknown authentication mode!\n");
+                       break;
        }
 
        transcript = malloc(SIZ);
@@ -622,6 +625,11 @@ int PurgeUseTable(void) {
        cdb_rewind(CDB_USETABLE);
        while(cdbut = cdb_next_item(CDB_USETABLE), cdbut != NULL) {
 
+       /*
+        * TODODRW: change this to create a new function time_t cdb_get_timestamp( struct cdbdata *)
+        * this will release this file from the serv_network.h
+        * Maybe it could be a macro that extracts and casts the reult
+        */
                 memcpy(&ut, cdbut->ptr,
                        ((cdbut->len > sizeof(struct UseTable)) ?
                         sizeof(struct UseTable) : cdbut->len));
@@ -708,49 +716,81 @@ int PurgeEuidIndexTable(void) {
 }
 
 
-void purge_databases(void) {
-       int retval;
-       static time_t last_purge = 0;
-       time_t now;
-       struct tm tm;
-
-       /* Do the auto-purge if the current hour equals the purge hour,
-        * but not if the operation has already been performed in the
-        * last twelve hours.  This is usually enough granularity.
-        */
-       now = time(NULL);
-       localtime_r(&now, &tm);
-       if (tm.tm_hour != config.c_purge_hour) return;
-       if ((now - last_purge) < 43200) return;
-
-       lprintf(CTDL_INFO, "Auto-purger: starting.\n");
-
-       retval = PurgeUsers();
-       lprintf(CTDL_NOTICE, "Purged %d users.\n", retval);
-
-       PurgeMessages();
-       lprintf(CTDL_NOTICE, "Expired %d messages.\n", messages_purged);
+void *purge_databases(void *args)
+{
+        int retval;
+        static time_t last_purge = 0;
+        time_t now;
+        struct tm tm;
+
+        while (!CtdlThreadCheckStop()) {
+                /* Do the auto-purge if the current hour equals the purge hour,
+                 * but not if the operation has already been performed in the
+                 * last twelve hours.  This is usually enough granularity.
+                 */
+                now = time(NULL);
+                localtime_r(&now, &tm);
+                if ((tm.tm_hour != config.c_purge_hour) || ((now - last_purge) < 43200)) {
+                        CtdlThreadSleep(60);
+                        continue;
+                }
+
+
+                lprintf(CTDL_INFO, "Auto-purger: starting.\n");
+
+               if (!CtdlThreadCheckStop())
+               {
+                       retval = PurgeUsers();
+                       lprintf(CTDL_NOTICE, "Purged %d users.\n", retval);
+               }
+               
+               if (!CtdlThreadCheckStop())
+               {
+                       PurgeMessages();
+                       lprintf(CTDL_NOTICE, "Expired %d messages.\n", messages_purged);
+               }
 
-       retval = PurgeRooms();
-       lprintf(CTDL_NOTICE, "Expired %d rooms.\n", retval);
+               if (!CtdlThreadCheckStop())
+               {
+                       retval = PurgeRooms();
+                       lprintf(CTDL_NOTICE, "Expired %d rooms.\n", retval);
+               }
 
-       retval = PurgeVisits();
-       lprintf(CTDL_NOTICE, "Purged %d visits.\n", retval);
+               if (!CtdlThreadCheckStop())
+               {
+                       retval = PurgeVisits();
+                       lprintf(CTDL_NOTICE, "Purged %d visits.\n", retval);
+               }
 
-       retval = PurgeUseTable();
-       lprintf(CTDL_NOTICE, "Purged %d entries from the use table.\n", retval);
+               if (!CtdlThreadCheckStop())
+               {
+                       retval = PurgeUseTable();
+                       lprintf(CTDL_NOTICE, "Purged %d entries from the use table.\n", retval);
+               }
 
-       retval = PurgeEuidIndexTable();
-       lprintf(CTDL_NOTICE, "Purged %d entries from the EUID index.\n", retval);
+               if (!CtdlThreadCheckStop())
+               {
+                       retval = PurgeEuidIndexTable();
+                       lprintf(CTDL_NOTICE, "Purged %d entries from the EUID index.\n", retval);
+               }
 
-       retval = TDAP_ProcessAdjRefCountQueue();
-       lprintf(CTDL_NOTICE, "Processed %d message reference count adjustments.\n", retval);
+               if (!CtdlThreadCheckStop())
+               {
+                       retval = TDAP_ProcessAdjRefCountQueue();
+                       lprintf(CTDL_NOTICE, "Processed %d message reference count adjustments.\n", retval);
+               }
 
-       lprintf(CTDL_INFO, "Auto-purger: finished.\n");
+               if (!CtdlThreadCheckStop())
+               {
+                       lprintf(CTDL_INFO, "Auto-purger: finished.\n");
+                       last_purge = now;       /* So we don't do it again soon */
+               }
+               else
+                       lprintf(CTDL_INFO, "Auto-purger: STOPPED.\n");
 
-       last_purge = now;       /* So we don't do it again soon */
+        }
+        return NULL;
 }
-
 /*****************************************************************************/
 
 
@@ -839,9 +879,12 @@ void cmd_fsck(char *argbuf) {
 
 CTDL_MODULE_INIT(expire)
 {
-       CtdlRegisterSessionHook(purge_databases, EVT_TIMER);
-       CtdlRegisterProtoHook(cmd_fsck, "FSCK", "Check message ref counts");
-
+       if (!threading)
+       {
+               CtdlRegisterProtoHook(cmd_fsck, "FSCK", "Check message ref counts");
+       }
+       else
+               CtdlThreadCreate("Auto Purger", CTDLTHREAD_BIGSTACK, purge_databases, NULL);
        /* return our Subversion id for the Log */
        return "$Id$";
 }