MSGS command can now do full text search on the room
[citadel.git] / citadel / serv_expire.c
index cac821ff95d0ace4db68107ebf955c1d3458a55b..6927a6509dbf5112935bcc1ca884a42d1d5494ff 100644 (file)
@@ -97,7 +97,13 @@ struct ctdlroomref {
 
 struct UPurgeList {
        struct UPurgeList *next;
-       char up_key[SIZ];
+       char up_key[256];
+};
+
+struct EPurgeList {
+       struct EPurgeList *next;
+       int ep_keylen;
+       char *ep_key;
 };
 
 
@@ -206,7 +212,7 @@ void DoPurgeMessages(FILE *purgelist) {
                if (!strncasecmp(buf, "m=", 2)) {
                        msgnum = atol(&buf[2]);
                        if (msgnum > 0L) {
-                               CtdlDeleteMessages(roomname, msgnum, "");
+                               CtdlDeleteMessages(roomname, &msgnum, 1, "", 0);
                        }
                }
        }
@@ -360,7 +366,7 @@ void do_uid_user_purge(struct ctdluser *us, void *data) {
 #ifdef ENABLE_AUTOLOGIN
        struct PurgeList *pptr;
 
-       if ((us->uid != (-1)) && (us->uid != BBSUID)) {
+       if ((us->uid != (-1)) && (us->uid != CTDLUID)) {
                if (getpwuid(us->uid) == NULL) {
                        pptr = (struct PurgeList *)
                                malloc(sizeof(struct PurgeList));
@@ -389,7 +395,7 @@ void do_user_purge(struct ctdluser *us, void *data) {
        struct ctdlroom qrbuf;
        char mailboxname[ROOMNAMELEN];
        MailboxName(mailboxname, us, MAILROOM);
-       create_room(mailboxname, 4, "", 0, 1, 1);
+       create_room(mailboxname, 4, "", 0, 1, 1, VIEW_BBS);
        if (getroom(&qrbuf, mailboxname) != 0) return;
        lprintf(CTDL_DEBUG, "Got %s\n", qrbuf.QRname);
         */
@@ -432,6 +438,11 @@ void do_user_purge(struct ctdluser *us, void *data) {
         */
        if (us->timescalled == 0) purge = 1;
 
+       /* User number 0, as well as any negative user number, is
+        * also impossible.
+        */
+       if (us->usernum < 1L) purge = 1;
+
        if (purge == 1) {
                pptr = (struct PurgeList *) malloc(sizeof(struct PurgeList));
                pptr->next = UserPurgeList;
@@ -449,10 +460,13 @@ 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);
        }
-       ForEachUser(do_uid_user_purge, NULL);
+#endif
 
        transcript = malloc(SIZ);
        strcpy(transcript, "The following users have been auto-purged:\n");
@@ -599,7 +613,7 @@ int PurgeUseTable(void) {
                        uptr = (struct UPurgeList *) malloc(sizeof(struct UPurgeList));
                        if (uptr != NULL) {
                                uptr->next = ul;
-                               safestrncpy(uptr->up_key, ut.ut_msgid, SIZ);
+                               safestrncpy(uptr->up_key, ut.ut_msgid, sizeof uptr->up_key);
                                ul = uptr;
                        }
                        ++purged;
@@ -621,6 +635,61 @@ int PurgeUseTable(void) {
 }
 
 
+
+/*
+ * Purge the EUID Index of old records.
+ *
+ */
+int PurgeEuidIndexTable(void) {
+       int purged = 0;
+       struct cdbdata *cdbei;
+       struct EPurgeList *el = NULL;
+       struct EPurgeList *eptr; 
+       long msgnum;
+       struct CtdlMessage *msg;
+
+       /* Phase 1: traverse through the table, discovering old records... */
+       lprintf(CTDL_DEBUG, "Purge EUID index: phase 1\n");
+       cdb_rewind(CDB_EUIDINDEX);
+       while(cdbei = cdb_next_item(CDB_EUIDINDEX), cdbei != NULL) {
+
+               memcpy(&msgnum, cdbei->ptr, sizeof(long));
+
+               msg = CtdlFetchMessage(msgnum, 0);
+               if (msg != NULL) {
+                       CtdlFreeMessage(msg);   /* it still exists, so do nothing */
+               }
+               else {
+                       eptr = (struct EPurgeList *) malloc(sizeof(struct EPurgeList));
+                       if (eptr != NULL) {
+                               eptr->next = el;
+                               eptr->ep_keylen = cdbei->len - sizeof(long);
+                               eptr->ep_key = malloc(cdbei->len);
+                               memcpy(eptr->ep_key, &cdbei->ptr[sizeof(long)], eptr->ep_keylen);
+                               el = eptr;
+                       }
+                       ++purged;
+               }
+
+                cdb_free(cdbei);
+
+       }
+
+       /* Phase 2: delete the records */
+       lprintf(CTDL_DEBUG, "Purge euid index: phase 2\n");
+       while (el != NULL) {
+               cdb_delete(CDB_EUIDINDEX, el->ep_key, el->ep_keylen);
+               free(el->ep_key);
+               eptr = el->next;
+               free(el);
+               el = eptr;
+       }
+
+       lprintf(CTDL_DEBUG, "Purge euid index: finished (purged %d records)\n", purged);
+       return(purged);
+}
+
+
 void purge_databases(void) {
        int retval;
        static time_t last_purge = 0;
@@ -632,7 +701,7 @@ void purge_databases(void) {
         * last twelve hours.  This is usually enough granularity.
         */
        now = time(NULL);
-       memcpy(&tm, localtime(&now), sizeof(struct tm));
+       localtime_r(&now, &tm);
        if (tm.tm_hour != config.c_purge_hour) return;
        if ((now - last_purge) < 43200) return;
 
@@ -653,6 +722,9 @@ void purge_databases(void) {
        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);
+
        lprintf(CTDL_INFO, "Auto-purger: finished.\n");
 
        last_purge = now;       /* So we don't do it again soon */
@@ -673,7 +745,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);
 }
 
 /*