TDAP: add facility to protect usetable entries from being deleted while still needed.
[citadel.git] / citadel / modules / expire / serv_expire.c
index b0824e2fb23fea28011c13e7803b028421b400f5..7da3ef84ebbd53726c494e6c11ce68cb48475279 100644 (file)
@@ -1,25 +1,23 @@
 /*
- * $Id$
- *
  * This module handles the expiry of old messages and the purging of old users.
  *
  * You might also see this module affectionately referred to as the DAP (the Dreaded Auto-Purger).
  *
- * Copyright (c) 1988-2009 by citadel.org (Art Cancro, Wilifried Goesgens, and others)
+ * Copyright (c) 1988-2011 by citadel.org (Art Cancro, Wilifried Goesgens, and others)
  *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 3 of the License, or
- *  (at your option) any later version.
+ * This program is open source software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
  *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
  *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  *
  *
  * A brief technical discussion:
@@ -80,8 +78,8 @@
 #include "msgbase.h"
 #include "user_ops.h"
 #include "control.h"
-#include "serv_network.h"      /* Needed for definition of UseTable */
 #include "threads.h"
+#include "context.h"
 
 #include "ctdl_module.h"
 
@@ -136,7 +134,6 @@ int users_not_purged;
 char *users_corrupt_msg = NULL;
 char *users_zero_msg = NULL;
 struct ctdlroomref *rr = NULL;
-extern struct CitContext *ContextList;
 int force_purge_now = 0;                       /* set to nonzero to force a run right now */
 
 
@@ -150,7 +147,7 @@ void GatherPurgeMessages(struct ctdlroom *qrbuf, void *data) {
        time_t xtime, now;
        struct CtdlMessage *msg = NULL;
        int a;
-        struct cdbdata *cdbfr;
+       struct cdbdata *cdbfr;
        long *msglist = NULL;
        int num_msgs = 0;
        FILE *purgelist;
@@ -169,13 +166,13 @@ void GatherPurgeMessages(struct ctdlroom *qrbuf, void *data) {
        if (!strcasecmp(qrbuf->QRname, SYSCONFIGROOM)) return;
 
        /* Ok, we got this far ... now let's see what's in the room */
-        cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long));
+       cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long));
 
-        if (cdbfr != NULL) {
-               msglist = malloc(cdbfr->len);
-               memcpy(msglist, cdbfr->ptr, cdbfr->len);
-               num_msgs = cdbfr->len / sizeof(long);
-               cdb_free(cdbfr);
+       if (cdbfr != NULL) {
+               msglist = malloc(cdbfr->len);
+               memcpy(msglist, cdbfr->ptr, cdbfr->len);
+               num_msgs = cdbfr->len / sizeof(long);
+               cdb_free(cdbfr);
        }
 
        /* Nothing to do if there aren't any messages */
@@ -249,13 +246,12 @@ void DoPurgeMessages(FILE *purgelist) {
 void PurgeMessages(void) {
        FILE *purgelist;
 
-       CtdlLogPrintf(CTDL_DEBUG, "PurgeMessages() called\n");
+       syslog(LOG_DEBUG, "PurgeMessages() called");
        messages_purged = 0;
 
        purgelist = tmpfile();
        if (purgelist == NULL) {
-               CtdlLogPrintf(CTDL_CRIT, "Can't create purgelist temp file: %s\n",
-                       strerror(errno));
+               syslog(LOG_CRIT, "Can't create purgelist temp file: %s", strerror(errno));
                return;
        }
 
@@ -322,7 +318,7 @@ void DoPurgeRooms(struct ctdlroom *qrbuf, void *data) {
                age = time(NULL) - (qrbuf->QRmtime);
                purge_secs = (time_t)config.c_roompurge * (time_t)86400;
                if (purge_secs <= (time_t)0) return;
-               CtdlLogPrintf(CTDL_DEBUG, "<%s> is <%ld> seconds old\n", qrbuf->QRname, (long)age);
+               syslog(LOG_DEBUG, "<%s> is <%ld> seconds old", qrbuf->QRname, (long)age);
                if (age > purge_secs) do_purge = 1;
        } /* !QR_MAILBOX */
 
@@ -344,7 +340,7 @@ int PurgeRooms(void) {
        struct ValidUser *vuptr;
        char *transcript = NULL;
 
-       CtdlLogPrintf(CTDL_DEBUG, "PurgeRooms() called\n");
+       syslog(LOG_DEBUG, "PurgeRooms() called");
 
 
        /* Load up a table full of valid user numbers so we can delete
@@ -381,7 +377,7 @@ int PurgeRooms(void) {
        if (num_rooms_purged > 0) CtdlAideMessage(transcript, "Room Autopurger Message");
        free(transcript);
 
-       CtdlLogPrintf(CTDL_DEBUG, "Purged %d rooms.\n", num_rooms_purged);
+       syslog(LOG_DEBUG, "Purged %d rooms.", num_rooms_purged);
        return(num_rooms_purged);
 }
 
@@ -430,10 +426,6 @@ void do_user_purge(struct ctdluser *us, void *data) {
        /* The default rule is to not purge. */
        purge = 0;
        
-       /* don't attempt to purge system users. */
-       if (!strncmp(us->fullname, "SYS_", 4))
-               goto skip_all_this;
-
        /* If the user hasn't called in two months and expiring of accounts is turned on, his/her account
         * has expired, so purge the record.
         */
@@ -474,7 +466,7 @@ void do_user_purge(struct ctdluser *us, void *data) {
         */
        if (us->usernum < 0L) purge = 1;
        
-       /** Don't purge user 0. That user is there for the system */
+       /* Don't purge user 0. That user is there for the system */
        if (us->usernum == 0L)
        {
                /* FIXME: Temporary log message. Until we do unauth access with user 0 we should
@@ -482,7 +474,7 @@ void do_user_purge(struct ctdluser *us, void *data) {
                 * we will need to try and purge them from users data bases.Some will not have names but
                 * those with names should be purged.
                 */
-               CtdlLogPrintf(CTDL_DEBUG, "Auto purger found a user 0 with name \"%s\"\n", us->fullname);
+               syslog(LOG_DEBUG, "Auto purger found a user 0 with name <%s>", us->fullname);
                // purge = 0;
        }
        
@@ -500,11 +492,13 @@ void do_user_purge(struct ctdluser *us, void *data) {
                        if (users_corrupt_msg == NULL)
                        {
                                users_corrupt_msg = malloc(SIZ);
-                               strcpy(users_corrupt_msg, "The auto-purger found the following user numbers with no name.\n"
-                               "The system has no way to purge user with no name and should not be able to\n"
-                               "create them either.\n"
-                               "This indicates corruption of the user DB or possibly a bug.\n"
-                               "It may be a good idea to restore your DB from a backup.\n");
+                               strcpy(users_corrupt_msg,
+                                       "The auto-purger found the following user numbers with no name.\n"
+                                       "The system has no way to purge a user with no name,"
+                                       " and should not be able to create them either.\n"
+                                       "This indicates corruption of the user DB or possibly a bug.\n"
+                                       "It may be a good idea to restore your DB from a backup.\n"
+                               );
                        }
                
                        users_corrupt_msg=realloc(users_corrupt_msg, strlen(users_corrupt_msg)+30);
@@ -512,8 +506,6 @@ void do_user_purge(struct ctdluser *us, void *data) {
                }
        }
 
-skip_all_this:
-               
        if (purge == 1) {
                pptr = (struct PurgeList *) malloc(sizeof(struct PurgeList));
                pptr->next = UserPurgeList;
@@ -533,7 +525,7 @@ int PurgeUsers(void) {
        int num_users_purged = 0;
        char *transcript = NULL;
 
-       CtdlLogPrintf(CTDL_DEBUG, "PurgeUsers() called\n");
+       syslog(LOG_DEBUG, "PurgeUsers() called");
        users_not_purged = 0;
 
        switch(config.c_auth_mode) {
@@ -544,7 +536,7 @@ int PurgeUsers(void) {
                        ForEachUser(do_uid_user_purge, NULL);
                        break;
                default:
-                       CtdlLogPrintf(CTDL_DEBUG, "User purge for auth mode %d is not implemented.\n",
+                       syslog(LOG_DEBUG, "User purge for auth mode %d is not implemented.",
                                config.c_auth_mode);
                        break;
        }
@@ -595,7 +587,7 @@ int PurgeUsers(void) {
                users_zero_msg = NULL;
        }
                
-       CtdlLogPrintf(CTDL_DEBUG, "Purged %d users.\n", num_users_purged);
+       syslog(LOG_DEBUG, "Purged %d users.", num_users_purged);
        return(num_users_purged);
 }
 
@@ -614,7 +606,7 @@ int PurgeUsers(void) {
  */
 int PurgeVisits(void) {
        struct cdbdata *cdbvisit;
-       struct visit vbuf;
+       visit vbuf;
        struct VPurgeList *VisitPurgeList = NULL;
        struct VPurgeList *vptr;
        int purged = 0;
@@ -633,10 +625,10 @@ int PurgeVisits(void) {
        /* Now traverse through the visits, purging irrelevant records... */
        cdb_rewind(CDB_VISIT);
        while(cdbvisit = cdb_next_item(CDB_VISIT), cdbvisit != NULL) {
-               memset(&vbuf, 0, sizeof(struct visit));
+               memset(&vbuf, 0, sizeof(visit));
                memcpy(&vbuf, cdbvisit->ptr,
-                       ( (cdbvisit->len > sizeof(struct visit)) ?
-                       sizeof(struct visit) : cdbvisit->len) );
+                       ( (cdbvisit->len > sizeof(visit)) ?
+                         sizeof(visit) : cdbvisit->len) );
                cdb_free(cdbvisit);
 
                RoomIsValid = 0;
@@ -702,7 +694,7 @@ int PurgeVisits(void) {
  * Purge the use table of old entries.
  *
  */
-int PurgeUseTable(void) {
+int PurgeUseTable(StrBuf *ErrMsg) {
        int purged = 0;
        struct cdbdata *cdbut;
        struct UseTable ut;
@@ -710,19 +702,28 @@ int PurgeUseTable(void) {
        struct UPurgeList *uptr; 
 
        /* Phase 1: traverse through the table, discovering old records... */
-       CtdlLogPrintf(CTDL_DEBUG, "Purge use table: phase 1\n");
+       if (CheckTDAPVeto(CDB_USETABLE, ErrMsg))
+       {
+               syslog(LOG_DEBUG, "Purge use table: VETO!");
+               return 0;
+       }
+
+       syslog(LOG_DEBUG, "Purge use table: phase 1");
        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));
-                cdb_free(cdbut);
+               /*
+                * 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
+                */
+               if (cdbut->len > sizeof(struct UseTable))
+                       memcpy(&ut, cdbut->ptr, sizeof(struct UseTable));
+               else {
+                       memset(&ut, 0, sizeof(struct UseTable));
+                       memcpy(&ut, cdbut->ptr, cdbut->len);
+               }
+               cdb_free(cdbut);
 
                if ( (time(NULL) - ut.ut_timestamp) > USETABLE_RETAIN ) {
                        uptr = (struct UPurgeList *) malloc(sizeof(struct UPurgeList));
@@ -737,7 +738,7 @@ int PurgeUseTable(void) {
        }
 
        /* Phase 2: delete the records */
-       CtdlLogPrintf(CTDL_DEBUG, "Purge use table: phase 2\n");
+       syslog(LOG_DEBUG, "Purge use table: phase 2");
        while (ul != NULL) {
                cdb_delete(CDB_USETABLE, ul->up_key, strlen(ul->up_key));
                uptr = ul->next;
@@ -745,7 +746,7 @@ int PurgeUseTable(void) {
                ul = uptr;
        }
 
-       CtdlLogPrintf(CTDL_DEBUG, "Purge use table: finished (purged %d records)\n", purged);
+       syslog(LOG_DEBUG, "Purge use table: finished (purged %d records)", purged);
        return(purged);
 }
 
@@ -764,7 +765,7 @@ int PurgeEuidIndexTable(void) {
        struct CtdlMessage *msg = NULL;
 
        /* Phase 1: traverse through the table, discovering old records... */
-       CtdlLogPrintf(CTDL_DEBUG, "Purge EUID index: phase 1\n");
+       syslog(LOG_DEBUG, "Purge EUID index: phase 1");
        cdb_rewind(CDB_EUIDINDEX);
        while(cdbei = cdb_next_item(CDB_EUIDINDEX), cdbei != NULL) {
 
@@ -786,12 +787,12 @@ int PurgeEuidIndexTable(void) {
                        ++purged;
                }
 
-                cdb_free(cdbei);
+              cdb_free(cdbei);
 
        }
 
        /* Phase 2: delete the records */
-       CtdlLogPrintf(CTDL_DEBUG, "Purge euid index: phase 2\n");
+       syslog(LOG_DEBUG, "Purge euid index: phase 2");
        while (el != NULL) {
                cdb_delete(CDB_EUIDINDEX, el->ep_key, el->ep_keylen);
                free(el->ep_key);
@@ -800,7 +801,7 @@ int PurgeEuidIndexTable(void) {
                el = eptr;
        }
 
-       CtdlLogPrintf(CTDL_DEBUG, "Purge euid index: finished (purged %d records)\n", purged);
+       syslog(LOG_DEBUG, "Purge euid index: finished (purged %d records)", purged);
        return(purged);
 }
 
@@ -831,7 +832,7 @@ int PurgeStaleOpenIDassociations(void) {
                        memcpy(&usernum, cdboi->ptr, sizeof(long));
                        if (CtdlGetUserByNumber(&usbuf, usernum) != 0) {
                                deleteme = strdup(cdboi->ptr + sizeof(long)),
-                               Put(keys, deleteme, strlen(deleteme), deleteme, generic_free_handler);
+                               Put(keys, deleteme, strlen(deleteme), deleteme, NULL);
                        }
                }
                cdb_free(cdboi);
@@ -842,7 +843,7 @@ int PurgeStaleOpenIDassociations(void) {
        HashPos = GetNewHashPos(keys, 0);
        while (GetNextHashPos(keys, HashPos, &len, &Key, &Value)!=0)
        {
-               CtdlLogPrintf(CTDL_DEBUG, "Deleting associated OpenID <%s>\n", Value);
+               syslog(LOG_DEBUG, "Deleting associated OpenID <%s>",  (char*)Value);
                cdb_delete(CDB_OPENID, Value, strlen(Value));
                /* note: don't free(Value) -- deleting the hash list will handle this for us */
                ++num_deleted;
@@ -856,183 +857,92 @@ int PurgeStaleOpenIDassociations(void) {
 
 
 
-void *purge_databases(void *args)
+void purge_databases(void)
 {
-        int retval;
-        static time_t last_purge = 0;
-        time_t now;
-        struct tm tm;
-       struct CitContext purgerCC;
-
-       CtdlLogPrintf(CTDL_DEBUG, "Auto-purger_thread() initializing\n");
-
-       CtdlFillSystemContext(&purgerCC, "purger");
-       citthread_setspecific(MyConKey, (void *)&purgerCC );
-
-        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))
-                       && (force_purge_now == 0)
-               ) {
-                        CtdlThreadSleep(60);
-                        continue;
-                }
-
-
-                CtdlLogPrintf(CTDL_INFO, "Auto-purger: starting.\n");
-
-               if (!CtdlThreadCheckStop())
-               {
-                       retval = PurgeUsers();
-                       CtdlLogPrintf(CTDL_NOTICE, "Purged %d users.\n", retval);
-               }
-               
-               if (!CtdlThreadCheckStop())
-               {
-                       PurgeMessages();
-                       CtdlLogPrintf(CTDL_NOTICE, "Expired %d messages.\n", messages_purged);
-               }
-
-               if (!CtdlThreadCheckStop())
-               {
-                       retval = PurgeRooms();
-                       CtdlLogPrintf(CTDL_NOTICE, "Expired %d rooms.\n", retval);
-               }
-
-               if (!CtdlThreadCheckStop())
-               {
-                       retval = PurgeVisits();
-                       CtdlLogPrintf(CTDL_NOTICE, "Purged %d visits.\n", retval);
-               }
-
-               if (!CtdlThreadCheckStop())
-               {
-                       retval = PurgeUseTable();
-                       CtdlLogPrintf(CTDL_NOTICE, "Purged %d entries from the use table.\n", retval);
-               }
-
-               if (!CtdlThreadCheckStop())
-               {
-                       retval = PurgeEuidIndexTable();
-                       CtdlLogPrintf(CTDL_NOTICE, "Purged %d entries from the EUID index.\n", retval);
-               }
-
-               if (!CtdlThreadCheckStop())
-               {
-                       retval = PurgeStaleOpenIDassociations();
-                       CtdlLogPrintf(CTDL_NOTICE, "Purged %d stale OpenID associations.\n", retval);
-               }
-
-               if (!CtdlThreadCheckStop())
-               {
-                       retval = TDAP_ProcessAdjRefCountQueue();
-                       CtdlLogPrintf(CTDL_NOTICE, "Processed %d message reference count adjustments.\n", retval);
-               }
-
-               if (!CtdlThreadCheckStop())
-               {
-                       CtdlLogPrintf(CTDL_INFO, "Auto-purger: finished.\n");
-                       last_purge = now;       /* So we don't do it again soon */
-                       force_purge_now = 0;
-               }
-               else
-                       CtdlLogPrintf(CTDL_INFO, "Auto-purger: STOPPED.\n");
-
-        }
-        return NULL;
-}
-/*****************************************************************************/
-
-
-/* The FSCK command has been removed because people were misusing it */
-
-#if 0
-
-void do_fsck_msg(long msgnum, void *userdata) {
-       struct ctdlroomref *ptr;
-
-       ptr = (struct ctdlroomref *)malloc(sizeof(struct ctdlroomref));
-       ptr->next = rr;
-       ptr->msgnum = msgnum;
-       rr = ptr;
-}
-
-void do_fsck_room(struct ctdlroom *qrbuf, void *data)
-{
-       CtdlGetRoom(&CC->room, qrbuf->QRname);
-       CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, do_fsck_msg, NULL);
-}
-
-/*
- * Check message reference counts
- */
-void cmd_fsck(char *argbuf) {
-       long msgnum;
-       struct cdbdata *cdbmsg;
-       struct MetaData smi;
-       struct ctdlroomref *ptr;
-       int realcount;
-
-       if (CtdlAccessCheck(ac_aide)) return;
+       int retval;
+       static time_t last_purge = 0;
+       time_t now;
+       struct tm tm;
 
-       /* Lame way of checking whether anyone else is doing this now */
-       if (rr != NULL) {
-               cprintf("%d Another FSCK is already running.\n", ERROR + RESOURCE_BUSY);
-               return;
+       /* 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))
+               && (force_purge_now == 0)
+       ) {
+                       return;
        }
 
-       cprintf("%d Checking message reference counts\n", LISTING_FOLLOWS);
+       syslog(LOG_INFO, "Auto-purger: starting.");
 
-       cprintf("\nThis could take a while.  Please be patient!\n\n");
-       cprintf("Gathering pointers...\n");
-       CtdlForEachRoom(do_fsck_room, NULL);
-
-       get_control();
-       cprintf("Checking message base...\n");
-       for (msgnum = 0L; msgnum <= CitControl.MMhighest; ++msgnum) {
-
-               cdbmsg = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
-               if (cdbmsg != NULL) {
-                       cdb_free(cdbmsg);
-                       cprintf("Message %7ld    ", msgnum);
+       if (!server_shutting_down)
+       {
+               retval = PurgeUsers();
+               syslog(LOG_NOTICE, "Purged %d users.", retval);
+       }
+               
+       if (!server_shutting_down)
+       {
+               PurgeMessages();
+               syslog(LOG_NOTICE, "Expired %d messages.", messages_purged);
+       }
 
-                       GetMetaData(&smi, msgnum);
-                       cprintf("refcount=%-2d   ", smi.meta_refcount);
+       if (!server_shutting_down)
+       {
+                       retval = PurgeRooms();
+                       syslog(LOG_NOTICE, "Expired %d rooms.", retval);
+       }
 
-                       realcount = 0;
-                       for (ptr = rr; ptr != NULL; ptr = ptr->next) {
-                               if (ptr->msgnum == msgnum) ++realcount;
-                       }
-                       cprintf("realcount=%-2d\n", realcount);
+       if (!server_shutting_down)
+       {
+                       retval = PurgeVisits();
+                       syslog(LOG_NOTICE, "Purged %d visits.", retval);
+       }
 
-                       if ( (smi.meta_refcount != realcount)
-                          || (realcount == 0) ) {
-                               AdjRefCount(msgnum, (smi.meta_refcount - realcount));
-                       }
+       if (!server_shutting_down)
+       {
+               StrBuf *ErrMsg;
 
-               }
+               ErrMsg = NewStrBuf ();
+               retval = PurgeUseTable(ErrMsg);
+                       syslog(LOG_NOTICE, "Purged %d entries from the use table.", retval);
+////TODO: fix errmsg
+               FreeStrBuf(&ErrMsg);
+       }
 
+       if (!server_shutting_down)
+       {
+               retval = PurgeEuidIndexTable();
+               syslog(LOG_NOTICE, "Purged %d entries from the EUID index.", retval);
        }
 
-       cprintf("Freeing memory...\n");
-       while (rr != NULL) {
-               ptr = rr->next;
-               free(rr);
-               rr = ptr;
+       if (!server_shutting_down)
+       {
+               retval = PurgeStaleOpenIDassociations();
+               syslog(LOG_NOTICE, "Purged %d stale OpenID associations.", retval);
        }
 
-       cprintf("Done!\n");
-       cprintf("000\n");
+       if (!server_shutting_down)
+       {
+                       retval = TDAP_ProcessAdjRefCountQueue();
+               syslog(LOG_NOTICE, "Processed %d message reference count adjustments.", retval);
+       }
 
+       if (!server_shutting_down)
+       {
+               syslog(LOG_INFO, "Auto-purger: finished.");
+               last_purge = now;       /* So we don't do it again soon */
+               force_purge_now = 0;
+       }
+       else {
+               syslog(LOG_INFO, "Auto-purger: STOPPED.");
+       }
 }
 
-#endif /* end of commented-out fsck cmd */
 
 /*
  * Manually initiate a run of The Dreaded Auto-Purger (tm)
@@ -1044,17 +954,17 @@ void cmd_tdap(char *argbuf) {
 }
 
 
-/*****************************************************************************/
 
 CTDL_MODULE_INIT(expire)
 {
        if (!threading)
        {
-               /* CtdlRegisterProtoHook(cmd_fsck, "FSCK", "Check message ref counts"); */
                CtdlRegisterProtoHook(cmd_tdap, "TDAP", "Manually initiate auto-purger");
+               CtdlRegisterProtoHook(cmd_gpex, "GPEX", "Get expire policy");
+               CtdlRegisterProtoHook(cmd_spex, "SPEX", "Set expire policy");
+               CtdlRegisterSessionHook(purge_databases, EVT_TIMER, PRIO_CLEANUP + 20);
        }
-       else
-               CtdlThreadCreate("Auto Purger", CTDLTHREAD_BIGSTACK, purge_databases, NULL);
-       /* return our Subversion id for the Log */
-       return "$Id$";
+
+       /* return our module name for the log */
+       return "expire";
 }