]> code.citadel.org Git - citadel.git/blobdiff - citadel/serv_expire.c
* Renamed "dynloader" to "serv_extensions" globally. We don't want people
[citadel.git] / citadel / serv_expire.c
index cffe99032193b2faf25ee7ab15588079f318549b..83a39308a7351268acfc13f08e283aa6aece282c 100644 (file)
 #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"
 
 
@@ -95,6 +96,11 @@ struct roomref {
        long msgnum;
 };
 
+struct UPurgeList {
+       struct UPurgeList *next;
+       char up_key[SIZ];
+};
+
 
 struct PurgeList *UserPurgeList = NULL;
 struct PurgeList *RoomPurgeList = NULL;
@@ -329,7 +335,7 @@ int PurgeRooms(void) {
        while (RoomPurgeList != NULL) {
                if (getroom(&qrbuf, RoomPurgeList->name) == 0) {
                        transcript=reallok(transcript, strlen(transcript)+SIZ);
-                       sprintf(&transcript[strlen(transcript)], " %s\n",
+                       snprintf(&transcript[strlen(transcript)], SIZ, " %s\n",
                                qrbuf.QRname);
                        delete_room(&qrbuf);
                }
@@ -358,7 +364,7 @@ void do_user_purge(struct usersupp *us, void *data) {
        struct quickroom qrbuf;
        char mailboxname[ROOMNAMELEN];
        MailboxName(mailboxname, us, MAILROOM);
-       create_room(mailboxname, 4, "", 0, 1);
+       create_room(mailboxname, 4, "", 0, 1, 1);
        if (getroom(&qrbuf, mailboxname) != 0) return;
        lprintf(9, "Got %s\n", qrbuf.QRname);
         */
@@ -427,7 +433,7 @@ int PurgeUsers(void) {
 
        while (UserPurgeList != NULL) {
                transcript=reallok(transcript, strlen(transcript)+SIZ);
-               sprintf(&transcript[strlen(transcript)], " %s\n",
+               snprintf(&transcript[strlen(transcript)], SIZ, " %s\n",
                        UserPurgeList->name);
                purge_user(UserPurgeList->name);
                pptr = UserPurgeList->next;
@@ -542,6 +548,52 @@ int PurgeVisits(void) {
        return(purged);
 }
 
+/*
+ * 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;
+               }
+
+       }
+
+       /* 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];
@@ -552,26 +604,31 @@ void cmd_expi(char *argbuf) {
        extract(cmd, argbuf, 0);
        if (!strcasecmp(cmd, "users")) {
                retval = PurgeUsers();
-               cprintf("%d Purged %d users.\n", OK, retval);
+               cprintf("%d Purged %d users.\n", CIT_OK, retval);
                return;
        }
        else if (!strcasecmp(cmd, "messages")) {
                PurgeMessages();
-               cprintf("%d Expired %d messages.\n", OK, messages_purged);
+               cprintf("%d Expired %d messages.\n", CIT_OK, messages_purged);
                return;
        }
        else if (!strcasecmp(cmd, "rooms")) {
                retval = PurgeRooms();
-               cprintf("%d Expired %d rooms.\n", OK, retval);
+               cprintf("%d Expired %d rooms.\n", CIT_OK, retval);
                return;
        }
        else if (!strcasecmp(cmd, "visits")) {
                retval = PurgeVisits();
-               cprintf("%d Purged %d visits.\n", OK, retval);
+               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", OK);
+               cprintf("%d Defragmented the databases.\n", CIT_OK);
        }
        else {
                cprintf("%d Invalid command.\n", ERROR+ILLEGAL_VALUE);
@@ -594,8 +651,7 @@ void do_fsck_msg(long msgnum, void *userdata) {
 void do_fsck_room(struct quickroom *qrbuf, void *data)
 {
        getroom(&CC->quickroom, qrbuf->QRname);
-       CtdlForEachMessage(MSGS_ALL, 0L, (-127), NULL, NULL,
-               do_fsck_msg, NULL);
+       CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, do_fsck_msg, NULL);
 }
 
 /*
@@ -604,7 +660,7 @@ void do_fsck_room(struct quickroom *qrbuf, void *data)
 void cmd_fsck(char *argbuf) {
        long msgnum;
        struct cdbdata *cdbmsg;
-       struct SuppMsgInfo smi;
+       struct MetaData smi;
        struct roomref *ptr;
        int realcount;
 
@@ -631,8 +687,8 @@ void cmd_fsck(char *argbuf) {
                        cdb_free(cdbmsg);
                        cprintf("Message %7ld    ", msgnum);
 
-                       GetSuppMsgInfo(&smi, msgnum);
-                       cprintf("refcount=%-2d   ", smi.smi_refcount);
+                       GetMetaData(&smi, msgnum);
+                       cprintf("refcount=%-2d   ", smi.meta_refcount);
 
                        realcount = 0;
                        for (ptr = rr; ptr != NULL; ptr = ptr->next) {
@@ -640,10 +696,10 @@ void cmd_fsck(char *argbuf) {
                        }
                        cprintf("realcount=%-2d\n", realcount);
 
-                       if ( (smi.smi_refcount != realcount)
+                       if ( (smi.meta_refcount != realcount)
                           || (realcount == 0) ) {
-                               smi.smi_refcount = realcount;
-                               PutSuppMsgInfo(&smi);
+                               smi.meta_refcount = realcount;
+                               PutMetaData(&smi);
                                AdjRefCount(msgnum, 0); /* deletes if needed */
                        }
 
@@ -668,7 +724,7 @@ void cmd_fsck(char *argbuf) {
 
 /*****************************************************************************/
 
-char *Dynamic_Module_Init(void)
+char *serv_expire_init(void)
 {
        CtdlRegisterProtoHook(cmd_expi, "EXPI", "Expire old system objects");
        CtdlRegisterProtoHook(cmd_fsck, "FSCK", "Check message ref counts");