Moved to new module init structure.
[citadel.git] / citadel / serv_expire.c
index 5b6fda96abab792500278b7251387f1ac63f1ab5..66b46570d54153bd0b202af2b454ca053ceb2b94 100644 (file)
 #include <limits.h>
 #include "citadel.h"
 #include "server.h"
-#include "sysdep_decls.h"
 #include "citserver.h"
 #include "support.h"
 #include "config.h"
-#include "serv_extensions.h"
 #include "room_ops.h"
 #include "policy.h"
 #include "database.h"
@@ -66,6 +64,9 @@
 #include "tools.h"
 
 
+#include "ctdl_module.h"
+
+
 struct PurgeList {
        struct PurgeList *next;
        char name[ROOMNAMELEN]; /* use the larger of username or roomname */
@@ -112,6 +113,7 @@ struct PurgeList *RoomPurgeList = NULL;
 struct ValidRoom *ValidRoomList = NULL;
 struct ValidUser *ValidUserList = NULL;
 int messages_purged;
+int users_not_purged;
 
 struct ctdlroomref *rr = NULL;
 
@@ -126,7 +128,7 @@ void GatherPurgeMessages(struct ctdlroom *qrbuf, void *data) {
        struct ExpirePolicy epbuf;
        long delnum;
        time_t xtime, now;
-       struct CtdlMessage *msg;
+       struct CtdlMessage *msg = NULL;
        int a;
         struct cdbdata *cdbfr;
        long *msglist = NULL;
@@ -212,7 +214,7 @@ void DoPurgeMessages(FILE *purgelist) {
                if (!strncasecmp(buf, "m=", 2)) {
                        msgnum = atol(&buf[2]);
                        if (msgnum > 0L) {
-                               CtdlDeleteMessages(roomname, msgnum, "", 0);
+                               CtdlDeleteMessages(roomname, &msgnum, 1, "");
                        }
                }
        }
@@ -350,7 +352,7 @@ int PurgeRooms(void) {
                ++num_rooms_purged;
        }
 
-       if (num_rooms_purged > 0) aide_message(transcript);
+       if (num_rooms_purged > 0) aide_message(transcript, "Room Autopurger Message");
        free(transcript);
 
        lprintf(CTDL_DEBUG, "Purged %d rooms.\n", num_rooms_purged);
@@ -360,10 +362,9 @@ int PurgeRooms(void) {
 
 /*
  * Back end function to check user accounts for associated Unix accounts
- * which no longer exist.
+ * which no longer exist.  (Only relevant for host auth mode.)
  */
 void do_uid_user_purge(struct ctdluser *us, void *data) {
-#ifdef ENABLE_AUTOLOGIN
        struct PurgeList *pptr;
 
        if ((us->uid != (-1)) && (us->uid != CTDLUID)) {
@@ -375,8 +376,9 @@ void do_uid_user_purge(struct ctdluser *us, void *data) {
                        UserPurgeList = pptr;
                }
        }
-
-#endif /* ENABLE_AUTOLOGIN */
+       else {
+               ++users_not_purged;
+       }
 }
 
 
@@ -390,17 +392,6 @@ void do_user_purge(struct ctdluser *us, void *data) {
        time_t purge_time;
        struct PurgeList *pptr;
 
-       /* stupid recovery routine to re-create missing mailboxen.
-        * don't enable this.
-       struct ctdlroom qrbuf;
-       char mailboxname[ROOMNAMELEN];
-       MailboxName(mailboxname, us, MAILROOM);
-       create_room(mailboxname, 4, "", 0, 1, 1, VIEW_BBS);
-       if (getroom(&qrbuf, mailboxname) != 0) return;
-       lprintf(CTDL_DEBUG, "Got %s\n", qrbuf.QRname);
-        */
-
-
        /* Set purge time; if the user overrides the system default, use it */
        if (us->USuserpurge > 0) {
                purge_time = ((time_t)us->USuserpurge) * 86400L;
@@ -427,6 +418,10 @@ void do_user_purge(struct ctdluser *us, void *data) {
         */
        if (us->flags & US_PERM) purge = 0;
 
+       /* If the user is an Aide, don't purge him/her/it.
+        */
+       if (us->axlevel == 6) purge = 0;
+
        /* If the access level is 0, the record should already have been
         * deleted, but maybe the user was logged in at the time or something.
         * Delete the record now.
@@ -449,6 +444,9 @@ void do_user_purge(struct ctdluser *us, void *data) {
                strcpy(pptr->name, us->fullname);
                UserPurgeList = pptr;
        }
+       else {
+               ++users_not_purged;
+       }
 
 }
 
@@ -460,29 +458,49 @@ int PurgeUsers(void) {
        char *transcript = NULL;
 
        lprintf(CTDL_DEBUG, "PurgeUsers() called\n");
-#ifdef ENABLE_AUTOLOGIN
-       ForEachUser(do_uid_user_purge, NULL);
-#else
-       if (config.c_userpurge > 0) {
-               ForEachUser(do_user_purge, NULL);
+       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);
+               }
        }
-#endif
 
        transcript = malloc(SIZ);
-       strcpy(transcript, "The following users have been auto-purged:\n");
-
-       while (UserPurgeList != NULL) {
-               transcript=realloc(transcript, strlen(transcript)+SIZ);
-               snprintf(&transcript[strlen(transcript)], SIZ, " %s\n",
-                       UserPurgeList->name);
-               purge_user(UserPurgeList->name);
-               pptr = UserPurgeList->next;
-               free(UserPurgeList);
-               UserPurgeList = pptr;
-               ++num_users_purged;
+
+       if (users_not_purged == 0) {
+               strcpy(transcript, "The auto-purger was told to purge every user.  It is\n"
+                               "refusing to do this because it usually indicates a problem\n"
+                               "such as an inability to communicate with a name service.\n"
+               );
+               while (UserPurgeList != NULL) {
+                       pptr = UserPurgeList->next;
+                       free(UserPurgeList);
+                       UserPurgeList = pptr;
+                       ++num_users_purged;
+               }
+       }
+
+       else {
+               strcpy(transcript, "The following users have been auto-purged:\n");
+               while (UserPurgeList != NULL) {
+                       transcript=realloc(transcript, strlen(transcript)+SIZ);
+                       snprintf(&transcript[strlen(transcript)], SIZ, " %s\n",
+                               UserPurgeList->name);
+                       purge_user(UserPurgeList->name);
+                       pptr = UserPurgeList->next;
+                       free(UserPurgeList);
+                       UserPurgeList = pptr;
+                       ++num_users_purged;
+               }
        }
 
-       if (num_users_purged > 0) aide_message(transcript);
+       if (num_users_purged > 0) aide_message(transcript, "User Purge Message");
        free(transcript);
 
        lprintf(CTDL_DEBUG, "Purged %d users.\n", num_users_purged);
@@ -646,7 +664,7 @@ int PurgeEuidIndexTable(void) {
        struct EPurgeList *el = NULL;
        struct EPurgeList *eptr; 
        long msgnum;
-       struct CtdlMessage *msg;
+       struct CtdlMessage *msg = NULL;
 
        /* Phase 1: traverse through the table, discovering old records... */
        lprintf(CTDL_DEBUG, "Purge EUID index: phase 1\n");
@@ -725,6 +743,9 @@ void purge_databases(void) {
        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);
+
        lprintf(CTDL_INFO, "Auto-purger: finished.\n");
 
        last_purge = now;       /* So we don't do it again soon */
@@ -745,7 +766,7 @@ void do_fsck_msg(long msgnum, void *userdata) {
 void do_fsck_room(struct ctdlroom *qrbuf, void *data)
 {
        getroom(&CC->room, qrbuf->QRname);
-       CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, do_fsck_msg, NULL);
+       CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, do_fsck_msg, NULL);
 }
 
 /*
@@ -792,9 +813,7 @@ void cmd_fsck(char *argbuf) {
 
                        if ( (smi.meta_refcount != realcount)
                           || (realcount == 0) ) {
-                               smi.meta_refcount = realcount;
-                               PutMetaData(&smi);
-                               AdjRefCount(msgnum, 0); /* deletes if needed */
+                               AdjRefCount(msgnum, (smi.meta_refcount - realcount));
                        }
 
                }
@@ -818,9 +837,11 @@ void cmd_fsck(char *argbuf) {
 
 /*****************************************************************************/
 
-char *serv_expire_init(void)
+CTDL_MODULE_INIT(expire)
 {
        CtdlRegisterSessionHook(purge_databases, EVT_TIMER);
        CtdlRegisterProtoHook(cmd_fsck, "FSCK", "Check message ref counts");
+
+       /* return our Subversion id for the Log */
        return "$Id$";
 }