Move back to single threaded structure for rss feed puller
[citadel.git] / citadel / modules / expire / serv_expire.c
index 70ddb316c4f2ae2e93218af2a3df5ac0ce8c194c..94999b9d71eba4bbdb61838034109560a57693ad 100644 (file)
@@ -3,7 +3,7 @@
  *
  * You might also see this module affectionately referred to as the DAP (the Dreaded Auto-Purger).
  *
- * Copyright (c) 1988-2011 by citadel.org (Art Cancro, Wilifried Goesgens, and others)
+ * Copyright (c) 1988-2015 by citadel.org (Art Cancro, Wilifried Goesgens, and others)
  *
  * 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
  * 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
- *
- *
  * A brief technical discussion:
  *
  * Several of the purge operations found in this module operate in two
@@ -78,7 +73,6 @@
 #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"
 
@@ -198,10 +192,10 @@ void GatherPurgeMessages(struct ctdlroom *qrbuf, void *data) {
                for (a=0; a<num_msgs; ++a) {
                        delnum = msglist[a];
 
-                       msg = CtdlFetchMessage(delnum, 0); /* dont need body */
+                       msg = CtdlFetchMessage(delnum, 0, 1); /* dont need body */
                        if (msg != NULL) {
-                               xtime = atol(msg->cm_fields['T']);
-                               CtdlFreeMessage(msg);
+                               xtime = atol(msg->cm_fields[eTimestamp]);
+                               CM_Free(msg);
                        } else {
                                xtime = 0L;
                        }
@@ -247,13 +241,12 @@ void DoPurgeMessages(FILE *purgelist) {
 void PurgeMessages(void) {
        FILE *purgelist;
 
-       syslog(LOG_DEBUG, "PurgeMessages() called\n");
+       syslog(LOG_DEBUG, "PurgeMessages() called");
        messages_purged = 0;
 
        purgelist = tmpfile();
        if (purgelist == NULL) {
-               syslog(LOG_CRIT, "Can't create purgelist temp file: %s\n",
-                       strerror(errno));
+               syslog(LOG_CRIT, "Can't create purgelist temp file: %s", strerror(errno));
                return;
        }
 
@@ -314,13 +307,13 @@ void DoPurgeRooms(struct ctdlroom *qrbuf, void *data) {
                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;
+               if (CtdlGetConfigLong("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;
+               purge_secs = CtdlGetConfigLong("c_roompurge") * 86400;
                if (purge_secs <= (time_t)0) return;
-               syslog(LOG_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 */
 
@@ -342,7 +335,7 @@ int PurgeRooms(void) {
        struct ValidUser *vuptr;
        char *transcript = NULL;
 
-       syslog(LOG_DEBUG, "PurgeRooms() called\n");
+       syslog(LOG_DEBUG, "PurgeRooms() called");
 
 
        /* Load up a table full of valid user numbers so we can delete
@@ -379,7 +372,7 @@ int PurgeRooms(void) {
        if (num_rooms_purged > 0) CtdlAideMessage(transcript, "Room Autopurger Message");
        free(transcript);
 
-       syslog(LOG_DEBUG, "Purged %d rooms.\n", num_rooms_purged);
+       syslog(LOG_DEBUG, "Purged %d rooms.", num_rooms_purged);
        return(num_rooms_purged);
 }
 
@@ -419,23 +412,19 @@ void do_user_purge(struct ctdluser *us, void *data) {
 
        /* Set purge time; if the user overrides the system default, use it */
        if (us->USuserpurge > 0) {
-               purge_time = ((time_t)us->USuserpurge) * 86400L;
+               purge_time = ((time_t)us->USuserpurge) * 86400;
        }
        else {
-               purge_time = ((time_t)config.c_userpurge) * 86400L;
+               purge_time = CtdlGetConfigLong("c_userpurge") * 86400;
        }
 
        /* 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.
         */
-       if (config.c_userpurge > 0)
+       if (CtdlGetConfigLong("c_userpurge") > 0)
        {
                now = time(NULL);
                if ((now - us->lastcall) > purge_time) purge = 1;
@@ -472,7 +461,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
@@ -480,7 +469,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.
                 */
-               syslog(LOG_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;
        }
        
@@ -498,11 +487,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);
@@ -510,8 +501,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;
@@ -531,10 +520,10 @@ int PurgeUsers(void) {
        int num_users_purged = 0;
        char *transcript = NULL;
 
-       syslog(LOG_DEBUG, "PurgeUsers() called\n");
+       syslog(LOG_DEBUG, "PurgeUsers() called");
        users_not_purged = 0;
 
-       switch(config.c_auth_mode) {
+       switch(CtdlGetConfigInt("c_auth_mode")) {
                case AUTHMODE_NATIVE:
                        ForEachUser(do_user_purge, NULL);
                        break;
@@ -542,8 +531,7 @@ int PurgeUsers(void) {
                        ForEachUser(do_uid_user_purge, NULL);
                        break;
                default:
-                       syslog(LOG_DEBUG, "User purge for auth mode %d is not implemented.\n",
-                               config.c_auth_mode);
+                       syslog(LOG_DEBUG, "User purge for auth mode %d is not implemented.", CtdlGetConfigInt("c_auth_mode"));
                        break;
        }
 
@@ -593,7 +581,7 @@ int PurgeUsers(void) {
                users_zero_msg = NULL;
        }
                
-       syslog(LOG_DEBUG, "Purged %d users.\n", num_users_purged);
+       syslog(LOG_DEBUG, "Purged %d users.", num_users_purged);
        return(num_users_purged);
 }
 
@@ -700,7 +688,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;
@@ -708,23 +696,32 @@ int PurgeUseTable(void) {
        struct UPurgeList *uptr; 
 
        /* Phase 1: traverse through the table, discovering old records... */
-       syslog(LOG_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) {
+       while(cdbut = cdb_next_item(CDB_USETABLE), cdbut != NULL)
+       {
+               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);
 
-       /*
-        * 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);
+               syslog(LOG_DEBUG, "UT: [%s] at %s", ut.ut_msgid, asctime(localtime(&ut.ut_timestamp))); // FIXME take this out ajc
 
-               if ( (time(NULL) - ut.ut_timestamp) > USETABLE_RETAIN ) {
+               if ( (time(NULL) - ut.ut_timestamp) > USETABLE_RETAIN )
+               {
                        uptr = (struct UPurgeList *) malloc(sizeof(struct UPurgeList));
-                       if (uptr != NULL) {
+                       if (uptr != NULL)
+                       {
                                uptr->next = ul;
                                safestrncpy(uptr->up_key, ut.ut_msgid, sizeof uptr->up_key);
                                ul = uptr;
@@ -735,7 +732,7 @@ int PurgeUseTable(void) {
        }
 
        /* Phase 2: delete the records */
-       syslog(LOG_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;
@@ -743,7 +740,7 @@ int PurgeUseTable(void) {
                ul = uptr;
        }
 
-       syslog(LOG_DEBUG, "Purge use table: finished (purged %d records)\n", purged);
+       syslog(LOG_DEBUG, "Purge use table: finished (purged %d records)", purged);
        return(purged);
 }
 
@@ -762,15 +759,15 @@ int PurgeEuidIndexTable(void) {
        struct CtdlMessage *msg = NULL;
 
        /* Phase 1: traverse through the table, discovering old records... */
-       syslog(LOG_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) {
 
                memcpy(&msgnum, cdbei->ptr, sizeof(long));
 
-               msg = CtdlFetchMessage(msgnum, 0);
+               msg = CtdlFetchMessage(msgnum, 0, 1);
                if (msg != NULL) {
-                       CtdlFreeMessage(msg);   /* it still exists, so do nothing */
+                       CM_Free(msg);   /* it still exists, so do nothing */
                }
                else {
                        eptr = (struct EPurgeList *) malloc(sizeof(struct EPurgeList));
@@ -789,7 +786,7 @@ int PurgeEuidIndexTable(void) {
        }
 
        /* Phase 2: delete the records */
-       syslog(LOG_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);
@@ -798,7 +795,7 @@ int PurgeEuidIndexTable(void) {
                el = eptr;
        }
 
-       syslog(LOG_DEBUG, "Purge euid index: finished (purged %d records)\n", purged);
+       syslog(LOG_DEBUG, "Purge euid index: finished (purged %d records)", purged);
        return(purged);
 }
 
@@ -840,7 +837,7 @@ int PurgeStaleOpenIDassociations(void) {
        HashPos = GetNewHashPos(keys, 0);
        while (GetNextHashPos(keys, HashPos, &len, &Key, &Value)!=0)
        {
-               syslog(LOG_DEBUG, "Deleting associated OpenID <%s>\n",  (char*)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;
@@ -867,74 +864,75 @@ void purge_databases(void)
         */
        now = time(NULL);
        localtime_r(&now, &tm);
-       if (
-               ((tm.tm_hour != config.c_purge_hour) || ((now - last_purge) < 43200))
-               && (force_purge_now == 0)
-       ) {
+       if (((tm.tm_hour != CtdlGetConfigInt("c_purge_hour")) || ((now - last_purge) < 43200)) && (force_purge_now == 0))
+       {
                        return;
        }
 
-       syslog(LOG_INFO, "Auto-purger: starting.\n");
+       syslog(LOG_INFO, "Auto-purger: starting.");
 
-       if (!CtdlThreadCheckStop())
+       if (!server_shutting_down)
        {
                retval = PurgeUsers();
-               syslog(LOG_NOTICE, "Purged %d users.\n", retval);
+               syslog(LOG_NOTICE, "Purged %d users.", retval);
        }
                
-       if (!CtdlThreadCheckStop())
+       if (!server_shutting_down)
        {
                PurgeMessages();
-               syslog(LOG_NOTICE, "Expired %d messages.\n", messages_purged);
+               syslog(LOG_NOTICE, "Expired %d messages.", messages_purged);
        }
 
-       if (!CtdlThreadCheckStop())
+       if (!server_shutting_down)
        {
                        retval = PurgeRooms();
-                       syslog(LOG_NOTICE, "Expired %d rooms.\n", retval);
+                       syslog(LOG_NOTICE, "Expired %d rooms.", retval);
        }
 
-       if (!CtdlThreadCheckStop())
+       if (!server_shutting_down)
        {
                        retval = PurgeVisits();
-                       syslog(LOG_NOTICE, "Purged %d visits.\n", retval);
+                       syslog(LOG_NOTICE, "Purged %d visits.", retval);
        }
 
-       if (!CtdlThreadCheckStop())
+       if (!server_shutting_down)
        {
-               retval = PurgeUseTable();
-                       syslog(LOG_NOTICE, "Purged %d entries from the use table.\n", retval);
+               StrBuf *ErrMsg;
+
+               ErrMsg = NewStrBuf ();
+               retval = PurgeUseTable(ErrMsg);
+                       syslog(LOG_NOTICE, "Purged %d entries from the use table.", retval);
+////TODO: fix errmsg
+               FreeStrBuf(&ErrMsg);
        }
 
-       if (!CtdlThreadCheckStop())
+       if (!server_shutting_down)
        {
                retval = PurgeEuidIndexTable();
-               syslog(LOG_NOTICE, "Purged %d entries from the EUID index.\n", retval);
+               syslog(LOG_NOTICE, "Purged %d entries from the EUID index.", retval);
        }
 
-       if (!CtdlThreadCheckStop())
+       if (!server_shutting_down)
        {
                retval = PurgeStaleOpenIDassociations();
-               syslog(LOG_NOTICE, "Purged %d stale OpenID associations.\n", retval);
+               syslog(LOG_NOTICE, "Purged %d stale OpenID associations.", retval);
        }
 
-       if (!CtdlThreadCheckStop())
+       if (!server_shutting_down)
        {
                        retval = TDAP_ProcessAdjRefCountQueue();
-               syslog(LOG_NOTICE, "Processed %d message reference count adjustments.\n", retval);
+               syslog(LOG_NOTICE, "Processed %d message reference count adjustments.", retval);
        }
 
-       if (!CtdlThreadCheckStop())
+       if (!server_shutting_down)
        {
-               syslog(LOG_INFO, "Auto-purger: finished.\n");
+               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.\n");
+               syslog(LOG_INFO, "Auto-purger: STOPPED.");
        }
-
-       CtdlClearSystemContext();
 }
 
 
@@ -956,9 +954,9 @@ CTDL_MODULE_INIT(expire)
                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);
+               CtdlRegisterSessionHook(purge_databases, EVT_TIMER, PRIO_CLEANUP + 20);
        }
 
-       /* return our Subversion id for the Log */
+       /* return our module name for the log */
        return "expire";
 }