* Renamed "struct user" to "struct ctdluser"
[citadel.git] / citadel / serv_expire.c
index 57a40dac454de1f55c7f13d51db2c1d71aafdb54..fdf2a01da65ff3006f8fbb5f24dae6116b180f0a 100644 (file)
@@ -1,11 +1,31 @@
 /*
- * serv_expire.c
+ * $Id$
  *
  * This module handles the expiry of old messages and the purging of old users.
  *
  */
-/* $Id$ */
 
+
+/*
+ * A brief technical discussion:
+ *
+ * Several of the purge operations found in this module operate in two
+ * stages: the first stage generates a linked list of objects to be deleted,
+ * then the second stage deletes all listed objects from the database.
+ *
+ * At first glance this may seem cumbersome and unnecessary.  The reason it is
+ * implemented in this way is because GDBM (and perhaps some other backends we
+ * may hook into in the future) explicitly do _not_ support the deletion of
+ * records from a file while the file is being traversed.  The delete operation
+ * will succeed, but the traversal is not guaranteed to visit every object if
+ * this is done.  Therefore we utilize the two-stage purge.
+ *
+ * When using Berkeley DB, there's another reason for the two-phase purge: we
+ * don't want the entire thing being done as one huge transaction.
+ */
+
+
+#include "sysdep.h"
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdio.h>
 #include <pwd.h>
 #include <errno.h>
 #include <sys/types.h>
-#include <sys/time.h>
+
+#if TIME_WITH_SYS_TIME
+# include <sys/time.h>
+# include <time.h>
+#else
+# if HAVE_SYS_TIME_H
+#  include <sys/time.h>
+# else
+#  include <time.h>
+# endif
+#endif
+
 #include <sys/wait.h>
 #include <string.h>
 #include <limits.h>
-#include <pthread.h>
 #include "citadel.h"
 #include "server.h"
 #include <syslog.h>
-#include <time.h>
 #include "sysdep_decls.h"
 #include "citserver.h"
 #include "support.h"
 #include "config.h"
-#include "dynloader.h"
+#include "serv_extensions.h"
 #include "room_ops.h"
 #include "policy.h"
 #include "database.h"
 #include "msgbase.h"
 #include "user_ops.h"
+#include "control.h"
+#include "serv_network.h"
+#include "tools.h"
+
 
-struct PurgedUser {
-       struct PurgedUser *next;
-       char name[26];
-       };
+struct PurgeList {
+       struct PurgeList *next;
+       char name[ROOMNAMELEN]; /* use the larger of username or roomname */
+};
 
-struct PurgedUser *plist = NULL;
+struct VPurgeList {
+       struct VPurgeList *next;
+       long vp_roomnum;
+       long vp_roomgen;
+       long vp_usernum;
+};
+
+struct ValidRoom {
+       struct ValidRoom *next;
+       long vr_roomnum;
+       long vr_roomgen;
+};
+
+struct ValidUser {
+       struct ValidUser *next;
+       long vu_usernum;
+};
+
+
+struct ctdlroomref {
+       struct ctdlroomref *next;
+       long msgnum;
+};
+
+struct UPurgeList {
+       struct UPurgeList *next;
+       char up_key[SIZ];
+};
+
+
+struct PurgeList *UserPurgeList = NULL;
+struct PurgeList *RoomPurgeList = NULL;
+struct ValidRoom *ValidRoomList = NULL;
+struct ValidUser *ValidUserList = NULL;
+int messages_purged;
+
+struct ctdlroomref *rr = NULL;
 
 extern struct CitContext *ContextList;
 
-#define MODULE_NAME    "Expire old messages, users, rooms"
-#define MODULE_AUTHOR  "Art Cancro"
-#define MODULE_EMAIL   "ajc@uncnsrd.mt-kisco.ny.us"
-#define MAJOR_VERSION  0
-#define MINOR_VERSION  1
-
-static struct DLModule_Info info = {
-       MODULE_NAME,
-       MODULE_AUTHOR,
-       MODULE_EMAIL,
-       MAJOR_VERSION,
-       MINOR_VERSION
-       };
-
-void DoPurgeMessages(struct quickroom *qrbuf) {
+
+/*
+ * First phase of message purge -- gather the locations of messages which
+ * qualify for purging and write them to a temp file.
+ */
+void GatherPurgeMessages(struct ctdlroom *qrbuf, void *data) {
        struct ExpirePolicy epbuf;
        long delnum;
        time_t xtime, now;
-       char msgid[64];
+       struct CtdlMessage *msg;
        int a;
+        struct cdbdata *cdbfr;
+       long *msglist = NULL;
+       int num_msgs = 0;
+       FILE *purgelist;
+
+       purgelist = (FILE *)data;
+       fprintf(purgelist, "r=%s\n", qrbuf->QRname);
 
        time(&now);
        GetExpirePolicy(&epbuf, qrbuf);
-       
-       /* lprintf(9, "ExpirePolicy for <%s> is <%d> <%d>\n",
-               qrbuf->QRname, epbuf.expire_mode, epbuf.expire_value);
-        */
 
        /* If the room is set to never expire messages ... do nothing */
        if (epbuf.expire_mode == EXPIRE_NEXTLEVEL) return;
        if (epbuf.expire_mode == EXPIRE_MANUAL) return;
 
-       get_msglist(qrbuf);
-       
+        cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long));
+
+        if (cdbfr != NULL) {
+               msglist = mallok(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 */
-       if (CC->num_msgs == 0) return;
+       if (num_msgs == 0) {
+               if (msglist != NULL) phree(msglist);
+               return;
+       }
 
        /* If the room is set to expire by count, do that */
        if (epbuf.expire_mode == EXPIRE_NUMMSGS) {
-               while (CC->num_msgs > epbuf.expire_value) {
-                       delnum = MessageFromList(0);
-                       lprintf(5, "Expiring message %ld\n", delnum);
-                       cdb_delete(CDB_MSGMAIN, &delnum, sizeof(long));
-                       memcpy(&CC->msglist[0], &CC->msglist[1],
-                               (sizeof(long)*(CC->num_msgs - 1)));
-                       CC->num_msgs = CC->num_msgs - 1;
+               if (num_msgs > epbuf.expire_value) {
+                       for (a=0; a<(num_msgs - epbuf.expire_value); ++a) {
+                               fprintf(purgelist, "m=%ld\n", msglist[a]);
+                               ++messages_purged;
                        }
                }
+       }
 
        /* If the room is set to expire by age... */
        if (epbuf.expire_mode == EXPIRE_AGE) {
-               for (a=0; a<(CC->num_msgs); ++a) {
-                       delnum = MessageFromList(a);
-                       sprintf(msgid, "%ld", delnum);
-                       xtime = output_message(msgid, MT_DATE, 0, 0);
+               for (a=0; a<num_msgs; ++a) {
+                       delnum = msglist[a];
+
+                       msg = CtdlFetchMessage(delnum);
+                       if (msg != NULL) {
+                               xtime = atol(msg->cm_fields['T']);
+                               CtdlFreeMessage(msg);
+                       } else {
+                               xtime = 0L;
+                       }
 
                        if ((xtime > 0L)
                           && (now - xtime > (time_t)(epbuf.expire_value * 86400L))) {
-                               cdb_delete(CDB_MSGMAIN, &delnum, sizeof(long));
-                               SetMessageInList(a, 0L);
-                               lprintf(5, "Expiring message %ld\n", delnum);
-                               }
+                               fprintf(purgelist, "m=%ld\n", delnum);
+                               ++messages_purged;
+                       }
+               }
+       }
+
+       if (msglist != NULL) phree(msglist);
+}
+
+
+/*
+ * Second phase of message purge -- read list of msgs from temp file and
+ * delete them.
+ */
+void DoPurgeMessages(FILE *purgelist) {
+       char roomname[ROOMNAMELEN];
+       long msgnum;
+       char buf[SIZ];
+
+       rewind(purgelist);
+       strcpy(roomname, "nonexistent room ___ ___");
+       while (fgets(buf, sizeof buf, purgelist) != NULL) {
+               buf[strlen(buf)-1]=0;
+               if (!strncasecmp(buf, "r=", 2)) {
+                       strcpy(roomname, &buf[2]);
+               }
+               if (!strncasecmp(buf, "m=", 2)) {
+                       msgnum = atol(&buf[2]);
+                       if (msgnum > 0L) {
+                               CtdlDeleteMessages(roomname, msgnum, "");
                        }
                }
-       CC->num_msgs = sort_msglist(CC->msglist, CC->num_msgs);
-       put_msglist(qrbuf);
        }
+}
+
 
 void PurgeMessages(void) {
-       ForEachRoom(DoPurgeMessages);
+       FILE *purgelist;
+
+       lprintf(5, "PurgeMessages() called\n");
+       messages_purged = 0;
+
+       purgelist = tmpfile();
+       if (purgelist == NULL) {
+               lprintf(3, "Can't create purgelist temp file: %s\n",
+                       strerror(errno));
+               return;
        }
 
+       ForEachRoom(GatherPurgeMessages, (void *)purgelist );
+       DoPurgeMessages(purgelist);
+       fclose(purgelist);
+}
+
+
+void AddValidUser(struct ctdluser *usbuf, void *data) {
+       struct ValidUser *vuptr;
+
+       vuptr = (struct ValidUser *)mallok(sizeof(struct ValidUser));
+       vuptr->next = ValidUserList;
+       vuptr->vu_usernum = usbuf->usernum;
+       ValidUserList = vuptr;
+}
+
+void AddValidRoom(struct ctdlroom *qrbuf, void *data) {
+       struct ValidRoom *vrptr;
+
+       vrptr = (struct ValidRoom *)mallok(sizeof(struct ValidRoom));
+       vrptr->next = ValidRoomList;
+       vrptr->vr_roomnum = qrbuf->QRnumber;
+       vrptr->vr_roomgen = qrbuf->QRgen;
+       ValidRoomList = vrptr;
+}
+
+void DoPurgeRooms(struct ctdlroom *qrbuf, void *data) {
+       time_t age, purge_secs;
+       struct PurgeList *pptr;
+       struct ValidUser *vuptr;
+       int do_purge = 0;
+
+       /* For mailbox rooms, there's only one purging rule: if the user who
+        * owns the room still exists, we keep the room; otherwise, we purge
+        * it.  Bypass any other rules.
+        */
+       if (qrbuf->QRflags & QR_MAILBOX) {
+               /* if user not found, do_purge will be 1 */
+               do_purge = 1;
+               for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
+                       if (vuptr->vu_usernum == atol(qrbuf->QRname)) {
+                               do_purge = 0;
+                       }
+               }
+       }
+       else {
+               /* Any of these attributes render a room non-purgable */
+               if (qrbuf->QRflags & QR_PERMANENT) return;
+               if (qrbuf->QRflags & QR_DIRECTORY) return;
+               if (qrbuf->QRflags & QR_NETWORK) return;
+               if (!strcasecmp(qrbuf->QRname, SYSCONFIGROOM)) return;
+               if (is_noneditable(qrbuf)) return;
+
+               /* If we don't know the modification date, be safe and don't purge */
+               if (qrbuf->QRmtime <= (time_t)0) return;
+
+               /* If no room purge time is set, be safe and don't purge */
+               if (config.c_roompurge < 0) return;
+
+               /* Otherwise, check the date of last modification */
+               age = time(NULL) - (qrbuf->QRmtime);
+               purge_secs = (time_t)config.c_roompurge * (time_t)86400;
+               if (purge_secs <= (time_t)0) return;
+               lprintf(9, "<%s> is <%ld> seconds old\n", qrbuf->QRname, (long)age);
+               if (age > purge_secs) do_purge = 1;
+       } /* !QR_MAILBOX */
+
+       if (do_purge) {
+               pptr = (struct PurgeList *) mallok(sizeof(struct PurgeList));
+               pptr->next = RoomPurgeList;
+               strcpy(pptr->name, qrbuf->QRname);
+               RoomPurgeList = pptr;
+       }
+
+}
+
 
-void do_user_purge(struct usersupp *us) {
+
+int PurgeRooms(void) {
+       struct PurgeList *pptr;
+       int num_rooms_purged = 0;
+       struct ctdlroom qrbuf;
+       struct ValidUser *vuptr;
+       char *transcript = NULL;
+
+       lprintf(5, "PurgeRooms() called\n");
+
+
+       /* Load up a table full of valid user numbers so we can delete
+        * user-owned rooms for users who no longer exist */
+       ForEachUser(AddValidUser, NULL);
+
+       /* Then cycle through the room file */
+       ForEachRoom(DoPurgeRooms, NULL);
+
+       /* Free the valid user list */
+       while (ValidUserList != NULL) {
+               vuptr = ValidUserList->next;
+               phree(ValidUserList);
+               ValidUserList = vuptr;
+       }
+
+
+       transcript = mallok(SIZ);
+       strcpy(transcript, "The following rooms have been auto-purged:\n");
+
+       while (RoomPurgeList != NULL) {
+               if (getroom(&qrbuf, RoomPurgeList->name) == 0) {
+                       transcript=reallok(transcript, strlen(transcript)+SIZ);
+                       snprintf(&transcript[strlen(transcript)], SIZ, " %s\n",
+                               qrbuf.QRname);
+                       delete_room(&qrbuf);
+               }
+               pptr = RoomPurgeList->next;
+               phree(RoomPurgeList);
+               RoomPurgeList = pptr;
+               ++num_rooms_purged;
+       }
+
+       if (num_rooms_purged > 0) aide_message(transcript);
+       phree(transcript);
+
+       lprintf(5, "Purged %d rooms.\n", num_rooms_purged);
+       return(num_rooms_purged);
+}
+
+
+void do_user_purge(struct ctdluser *us, void *data) {
        int purge;
        time_t now;
        time_t purge_time;
-       struct PurgedUser *pptr;
+       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);
+       if (getroom(&qrbuf, mailboxname) != 0) return;
+       lprintf(9, "Got %s\n", qrbuf.QRname);
+        */
 
-       purge_time = ((time_t)config.c_userpurge) * 86400L;
+
+       /* Set purge time; if the user overrides the system default, use it */
+       if (us->USuserpurge > 0) {
+               purge_time = ((time_t)us->USuserpurge) * 86400L;
+       }
+       else {
+               purge_time = ((time_t)config.c_userpurge) * 86400L;
+       }
 
        /* The default rule is to not purge. */
        purge = 0;
@@ -154,36 +408,325 @@ void do_user_purge(struct usersupp *us) {
        if (us->timescalled == 0) purge = 1;
 
        if (purge == 1) {
-               pptr = (struct PurgedUser *) malloc(sizeof(struct PurgedUser));
-               pptr->next = plist;
+               pptr = (struct PurgeList *) mallok(sizeof(struct PurgeList));
+               pptr->next = UserPurgeList;
                strcpy(pptr->name, us->fullname);
-               plist = pptr;
+               UserPurgeList = pptr;
+       }
+
+}
+
+
+
+int PurgeUsers(void) {
+       struct PurgeList *pptr;
+       int num_users_purged = 0;
+       char *transcript = NULL;
+
+       lprintf(5, "PurgeUsers() called\n");
+       if (config.c_userpurge > 0) {
+               ForEachUser(do_user_purge, NULL);
+       }
+
+       transcript = mallok(SIZ);
+       strcpy(transcript, "The following users have been auto-purged:\n");
+
+       while (UserPurgeList != NULL) {
+               transcript=reallok(transcript, strlen(transcript)+SIZ);
+               snprintf(&transcript[strlen(transcript)], SIZ, " %s\n",
+                       UserPurgeList->name);
+               purge_user(UserPurgeList->name);
+               pptr = UserPurgeList->next;
+               phree(UserPurgeList);
+               UserPurgeList = pptr;
+               ++num_users_purged;
+       }
+
+       if (num_users_purged > 0) aide_message(transcript);
+       phree(transcript);
+
+       lprintf(5, "Purged %d users.\n", num_users_purged);
+       return(num_users_purged);
+}
+
+
+/*
+ * Purge visits
+ *
+ * This is a really cumbersome "garbage collection" function.  We have to
+ * delete visits which refer to rooms and/or users which no longer exist.  In
+ * order to prevent endless traversals of the room and user files, we first
+ * build linked lists of rooms and users which _do_ exist on the system, then
+ * traverse the visit file, checking each record against those two lists and
+ * purging the ones that do not have a match on _both_ lists.  (Remember, if
+ * either the room or user being referred to is no longer on the system, the
+ * record is completely useless.)
+ */
+int PurgeVisits(void) {
+       struct cdbdata *cdbvisit;
+       struct visit vbuf;
+       struct VPurgeList *VisitPurgeList = NULL;
+       struct VPurgeList *vptr;
+       int purged = 0;
+       char IndexBuf[32];
+       int IndexLen;
+       struct ValidRoom *vrptr;
+       struct ValidUser *vuptr;
+       int RoomIsValid, UserIsValid;
+
+       /* First, load up a table full of valid room/gen combinations */
+       ForEachRoom(AddValidRoom, NULL);
+
+       /* Then load up a table full of valid user numbers */
+       ForEachUser(AddValidUser, NULL);
+
+       /* 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));
+               memcpy(&vbuf, cdbvisit->ptr,
+                       ( (cdbvisit->len > sizeof(struct visit)) ?
+                       sizeof(struct visit) : cdbvisit->len) );
+               cdb_free(cdbvisit);
+
+               RoomIsValid = 0;
+               UserIsValid = 0;
+
+               /* Check to see if the room exists */
+               for (vrptr=ValidRoomList; vrptr!=NULL; vrptr=vrptr->next) {
+                       if ( (vrptr->vr_roomnum==vbuf.v_roomnum)
+                            && (vrptr->vr_roomgen==vbuf.v_roomgen))
+                               RoomIsValid = 1;
                }
 
+               /* Check to see if the user exists */
+               for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
+                       if (vuptr->vu_usernum == vbuf.v_usernum)
+                               UserIsValid = 1;
+               }
+
+               /* Put the record on the purge list if it's dead */
+               if ((RoomIsValid==0) || (UserIsValid==0)) {
+                       vptr = (struct VPurgeList *)
+                               mallok(sizeof(struct VPurgeList));
+                       vptr->next = VisitPurgeList;
+                       vptr->vp_roomnum = vbuf.v_roomnum;
+                       vptr->vp_roomgen = vbuf.v_roomgen;
+                       vptr->vp_usernum = vbuf.v_usernum;
+                       VisitPurgeList = vptr;
+               }
+
+       }
+
+       /* Free the valid room/gen combination list */
+       while (ValidRoomList != NULL) {
+               vrptr = ValidRoomList->next;
+               phree(ValidRoomList);
+               ValidRoomList = vrptr;
        }
 
+       /* Free the valid user list */
+       while (ValidUserList != NULL) {
+               vuptr = ValidUserList->next;
+               phree(ValidUserList);
+               ValidUserList = vuptr;
+       }
 
+       /* Now delete every visit on the purged list */
+       while (VisitPurgeList != NULL) {
+               IndexLen = GenerateRelationshipIndex(IndexBuf,
+                               VisitPurgeList->vp_roomnum,
+                               VisitPurgeList->vp_roomgen,
+                               VisitPurgeList->vp_usernum);
+               cdb_delete(CDB_VISIT, IndexBuf, IndexLen);
+               vptr = VisitPurgeList->next;
+               phree(VisitPurgeList);
+               VisitPurgeList = vptr;
+               ++purged;
+       }
 
-void PurgeUsers(void) {
-       struct PurgedUser *pptr;
+       return(purged);
+}
 
-       if (config.c_userpurge > 0) {
-               ForEachUser(do_user_purge);
+/*
+ * Purge the use table of old entries.
+ *
+ */
+int PurgeUseTable(void) {
+       int purged = 0;
+       struct cdbdata *cdbut;
+       struct UseTable ut;
+       struct UPurgeList *ul = NULL;
+       struct UPurgeList *uptr; 
+
+       /* Phase 1: traverse through the table, discovering old records... */
+       lprintf(9, "Purge use table: phase 1\n");
+       cdb_rewind(CDB_USETABLE);
+       while(cdbut = cdb_next_item(CDB_USETABLE), cdbut != NULL) {
+
+                memcpy(&ut, cdbut->ptr,
+                       ((cdbut->len > sizeof(struct UseTable)) ?
+                        sizeof(struct UseTable) : cdbut->len));
+                cdb_free(cdbut);
+
+               if ( (time(NULL) - ut.ut_timestamp) > USETABLE_RETAIN ) {
+                       uptr = (struct UPurgeList *) mallok(sizeof(struct UPurgeList));
+                       if (uptr != NULL) {
+                               uptr->next = ul;
+                               safestrncpy(uptr->up_key, ut.ut_msgid, SIZ);
+                               ul = uptr;
+                       }
+                       ++purged;
                }
 
-       while (plist != NULL) {
-               purge_user(plist->name);
-               pptr = plist->next;
-               free(plist);
-               plist = pptr;
+       }
+
+       /* Phase 2: delete the records */
+       lprintf(9, "Purge use table: phase 2\n");
+       while (ul != NULL) {
+               cdb_delete(CDB_USETABLE, ul->up_key, strlen(ul->up_key));
+               uptr = ul->next;
+               phree(ul);
+               ul = uptr;
+       }
+
+       lprintf(9, "Purge use table: finished (purged %d records)\n", purged);
+       return(purged);
+}
+
+
+void cmd_expi(char *argbuf) {
+       char cmd[SIZ];
+       int retval;
+
+       if (CtdlAccessCheck(ac_aide)) return;
+
+       extract(cmd, argbuf, 0);
+       if (!strcasecmp(cmd, "users")) {
+               retval = PurgeUsers();
+               cprintf("%d Purged %d users.\n", CIT_OK, retval);
+               return;
+       }
+       else if (!strcasecmp(cmd, "messages")) {
+               PurgeMessages();
+               cprintf("%d Expired %d messages.\n", CIT_OK, messages_purged);
+               return;
+       }
+       else if (!strcasecmp(cmd, "rooms")) {
+               retval = PurgeRooms();
+               cprintf("%d Expired %d rooms.\n", CIT_OK, retval);
+               return;
+       }
+       else if (!strcasecmp(cmd, "visits")) {
+               retval = PurgeVisits();
+               cprintf("%d Purged %d visits.\n", CIT_OK, retval);
+       }
+       else if (!strcasecmp(cmd, "usetable")) {
+               retval = PurgeUseTable();
+               cprintf("%d Purged %d entries from the use table.\n",
+                       CIT_OK, retval);
+       }
+       else if (!strcasecmp(cmd, "defrag")) {
+               defrag_databases();
+               cprintf("%d Defragmented the databases.\n", CIT_OK);
+       }
+       else {
+               cprintf("%d Invalid command.\n", ERROR+ILLEGAL_VALUE);
+               return;
+       }
+}
+
+/*****************************************************************************/
+
+
+void do_fsck_msg(long msgnum, void *userdata) {
+       struct ctdlroomref *ptr;
+
+       ptr = (struct ctdlroomref *)mallok(sizeof(struct ctdlroomref));
+       ptr->next = rr;
+       ptr->msgnum = msgnum;
+       rr = ptr;
+}
+
+void do_fsck_room(struct ctdlroom *qrbuf, void *data)
+{
+       getroom(&CC->room, qrbuf->QRname);
+       CtdlForEachMessage(MSGS_ALL, 0L, 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;
+
+       /* Lame way of checking whether anyone else is doing this now */
+       if (rr != NULL) {
+               cprintf("%d Another FSCK is already running.\n", ERROR);
+               return;
+       }
+
+       cprintf("%d Checking message reference counts\n", LISTING_FOLLOWS);
+
+       cprintf("\nThis could take a while.  Please be patient!\n\n");
+       cprintf("Gathering pointers...\n");
+       ForEachRoom(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);
+
+                       GetMetaData(&smi, msgnum);
+                       cprintf("refcount=%-2d   ", smi.meta_refcount);
+
+                       realcount = 0;
+                       for (ptr = rr; ptr != NULL; ptr = ptr->next) {
+                               if (ptr->msgnum == msgnum) ++realcount;
+                       }
+                       cprintf("realcount=%-2d\n", realcount);
+
+                       if ( (smi.meta_refcount != realcount)
+                          || (realcount == 0) ) {
+                               smi.meta_refcount = realcount;
+                               PutMetaData(&smi);
+                               AdjRefCount(msgnum, 0); /* deletes if needed */
+                       }
+
                }
 
        }
 
+       cprintf("Freeing memory...\n");
+       while (rr != NULL) {
+               ptr = rr->next;
+               phree(rr);
+               rr = ptr;
+       }
+
+       cprintf("Done!\n");
+       cprintf("000\n");
+
+}
+
+
+
+
+/*****************************************************************************/
 
-struct DLModule_Info *Dynamic_Module_Init(void)
+char *serv_expire_init(void)
 {
-   CtdlRegisterCleanupHook(PurgeMessages);
-   CtdlRegisterCleanupHook(PurgeUsers);
-   return &info;
+       CtdlRegisterProtoHook(cmd_expi, "EXPI", "Expire old system objects");
+       CtdlRegisterProtoHook(cmd_fsck, "FSCK", "Check message ref counts");
+       return "$Id$";
 }