bb78d96bb4b735ad1b437e92287b5aa0e1d61f70
[citadel.git] / citadel / serv_expire.c
1 /*
2  * $Id$
3  *
4  * This module handles the expiry of old messages and the purging of old users.
5  *
6  */
7
8
9 /*
10  * A brief technical discussion:
11  *
12  * Several of the purge operations found in this module operate in two
13  * stages: the first stage generates a linked list of objects to be deleted,
14  * then the second stage deletes all listed objects from the database.
15  *
16  * At first glance this may seem cumbersome and unnecessary.  The reason it is
17  * implemented in this way is because Berkeley DB, and possibly other backends
18  * we may hook into in the future, explicitly do _not_ support the deletion of
19  * records from a file while the file is being traversed.  The delete operation
20  * will succeed, but the traversal is not guaranteed to visit every object if
21  * this is done.  Therefore we utilize the two-stage purge.
22  *
23  * When using Berkeley DB, there's another reason for the two-phase purge: we
24  * don't want the entire thing being done as one huge transaction.
25  */
26
27
28 #include "sysdep.h"
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <stdio.h>
32 #include <fcntl.h>
33 #include <signal.h>
34 #include <pwd.h>
35 #include <errno.h>
36 #include <sys/types.h>
37
38 #if TIME_WITH_SYS_TIME
39 # include <sys/time.h>
40 # include <time.h>
41 #else
42 # if HAVE_SYS_TIME_H
43 #  include <sys/time.h>
44 # else
45 #  include <time.h>
46 # endif
47 #endif
48
49 #include <sys/wait.h>
50 #include <string.h>
51 #include <limits.h>
52 #include "citadel.h"
53 #include "server.h"
54 #include "sysdep_decls.h"
55 #include "citserver.h"
56 #include "support.h"
57 #include "config.h"
58 #include "serv_extensions.h"
59 #include "room_ops.h"
60 #include "policy.h"
61 #include "database.h"
62 #include "msgbase.h"
63 #include "user_ops.h"
64 #include "control.h"
65 #include "serv_network.h"
66 #include "tools.h"
67
68
69 struct PurgeList {
70         struct PurgeList *next;
71         char name[ROOMNAMELEN]; /* use the larger of username or roomname */
72 };
73
74 struct VPurgeList {
75         struct VPurgeList *next;
76         long vp_roomnum;
77         long vp_roomgen;
78         long vp_usernum;
79 };
80
81 struct ValidRoom {
82         struct ValidRoom *next;
83         long vr_roomnum;
84         long vr_roomgen;
85 };
86
87 struct ValidUser {
88         struct ValidUser *next;
89         long vu_usernum;
90 };
91
92
93 struct ctdlroomref {
94         struct ctdlroomref *next;
95         long msgnum;
96 };
97
98 struct UPurgeList {
99         struct UPurgeList *next;
100         char up_key[256];
101 };
102
103 struct EPurgeList {
104         struct EPurgeList *next;
105         int ep_keylen;
106         char *ep_key;
107 };
108
109
110 struct PurgeList *UserPurgeList = NULL;
111 struct PurgeList *RoomPurgeList = NULL;
112 struct ValidRoom *ValidRoomList = NULL;
113 struct ValidUser *ValidUserList = NULL;
114 int messages_purged;
115
116 struct ctdlroomref *rr = NULL;
117
118 extern struct CitContext *ContextList;
119
120
121 /*
122  * First phase of message purge -- gather the locations of messages which
123  * qualify for purging and write them to a temp file.
124  */
125 void GatherPurgeMessages(struct ctdlroom *qrbuf, void *data) {
126         struct ExpirePolicy epbuf;
127         long delnum;
128         time_t xtime, now;
129         struct CtdlMessage *msg;
130         int a;
131         struct cdbdata *cdbfr;
132         long *msglist = NULL;
133         int num_msgs = 0;
134         FILE *purgelist;
135
136         purgelist = (FILE *)data;
137         fprintf(purgelist, "r=%s\n", qrbuf->QRname);
138
139         time(&now);
140         GetExpirePolicy(&epbuf, qrbuf);
141
142         /* If the room is set to never expire messages ... do nothing */
143         if (epbuf.expire_mode == EXPIRE_NEXTLEVEL) return;
144         if (epbuf.expire_mode == EXPIRE_MANUAL) return;
145
146         cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long));
147
148         if (cdbfr != NULL) {
149                 msglist = malloc(cdbfr->len);
150                 memcpy(msglist, cdbfr->ptr, cdbfr->len);
151                 num_msgs = cdbfr->len / sizeof(long);
152                 cdb_free(cdbfr);
153         }
154
155         /* Nothing to do if there aren't any messages */
156         if (num_msgs == 0) {
157                 if (msglist != NULL) free(msglist);
158                 return;
159         }
160
161         /* If the room is set to expire by count, do that */
162         if (epbuf.expire_mode == EXPIRE_NUMMSGS) {
163                 if (num_msgs > epbuf.expire_value) {
164                         for (a=0; a<(num_msgs - epbuf.expire_value); ++a) {
165                                 fprintf(purgelist, "m=%ld\n", msglist[a]);
166                                 ++messages_purged;
167                         }
168                 }
169         }
170
171         /* If the room is set to expire by age... */
172         if (epbuf.expire_mode == EXPIRE_AGE) {
173                 for (a=0; a<num_msgs; ++a) {
174                         delnum = msglist[a];
175
176                         msg = CtdlFetchMessage(delnum, 0); /* dont need body */
177                         if (msg != NULL) {
178                                 xtime = atol(msg->cm_fields['T']);
179                                 CtdlFreeMessage(msg);
180                         } else {
181                                 xtime = 0L;
182                         }
183
184                         if ((xtime > 0L)
185                            && (now - xtime > (time_t)(epbuf.expire_value * 86400L))) {
186                                 fprintf(purgelist, "m=%ld\n", delnum);
187                                 ++messages_purged;
188                         }
189                 }
190         }
191
192         if (msglist != NULL) free(msglist);
193 }
194
195
196 /*
197  * Second phase of message purge -- read list of msgs from temp file and
198  * delete them.
199  */
200 void DoPurgeMessages(FILE *purgelist) {
201         char roomname[ROOMNAMELEN];
202         long msgnum;
203         char buf[SIZ];
204
205         rewind(purgelist);
206         strcpy(roomname, "nonexistent room ___ ___");
207         while (fgets(buf, sizeof buf, purgelist) != NULL) {
208                 buf[strlen(buf)-1]=0;
209                 if (!strncasecmp(buf, "r=", 2)) {
210                         strcpy(roomname, &buf[2]);
211                 }
212                 if (!strncasecmp(buf, "m=", 2)) {
213                         msgnum = atol(&buf[2]);
214                         if (msgnum > 0L) {
215                                 CtdlDeleteMessages(roomname, &msgnum, 1, "", 0);
216                         }
217                 }
218         }
219 }
220
221
222 void PurgeMessages(void) {
223         FILE *purgelist;
224
225         lprintf(CTDL_DEBUG, "PurgeMessages() called\n");
226         messages_purged = 0;
227
228         purgelist = tmpfile();
229         if (purgelist == NULL) {
230                 lprintf(CTDL_CRIT, "Can't create purgelist temp file: %s\n",
231                         strerror(errno));
232                 return;
233         }
234
235         ForEachRoom(GatherPurgeMessages, (void *)purgelist );
236         DoPurgeMessages(purgelist);
237         fclose(purgelist);
238 }
239
240
241 void AddValidUser(struct ctdluser *usbuf, void *data) {
242         struct ValidUser *vuptr;
243
244         vuptr = (struct ValidUser *)malloc(sizeof(struct ValidUser));
245         vuptr->next = ValidUserList;
246         vuptr->vu_usernum = usbuf->usernum;
247         ValidUserList = vuptr;
248 }
249
250 void AddValidRoom(struct ctdlroom *qrbuf, void *data) {
251         struct ValidRoom *vrptr;
252
253         vrptr = (struct ValidRoom *)malloc(sizeof(struct ValidRoom));
254         vrptr->next = ValidRoomList;
255         vrptr->vr_roomnum = qrbuf->QRnumber;
256         vrptr->vr_roomgen = qrbuf->QRgen;
257         ValidRoomList = vrptr;
258 }
259
260 void DoPurgeRooms(struct ctdlroom *qrbuf, void *data) {
261         time_t age, purge_secs;
262         struct PurgeList *pptr;
263         struct ValidUser *vuptr;
264         int do_purge = 0;
265
266         /* For mailbox rooms, there's only one purging rule: if the user who
267          * owns the room still exists, we keep the room; otherwise, we purge
268          * it.  Bypass any other rules.
269          */
270         if (qrbuf->QRflags & QR_MAILBOX) {
271                 /* if user not found, do_purge will be 1 */
272                 do_purge = 1;
273                 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
274                         if (vuptr->vu_usernum == atol(qrbuf->QRname)) {
275                                 do_purge = 0;
276                         }
277                 }
278         }
279         else {
280                 /* Any of these attributes render a room non-purgable */
281                 if (qrbuf->QRflags & QR_PERMANENT) return;
282                 if (qrbuf->QRflags & QR_DIRECTORY) return;
283                 if (qrbuf->QRflags & QR_NETWORK) return;
284                 if (!strcasecmp(qrbuf->QRname, SYSCONFIGROOM)) return;
285                 if (is_noneditable(qrbuf)) return;
286
287                 /* If we don't know the modification date, be safe and don't purge */
288                 if (qrbuf->QRmtime <= (time_t)0) return;
289
290                 /* If no room purge time is set, be safe and don't purge */
291                 if (config.c_roompurge < 0) return;
292
293                 /* Otherwise, check the date of last modification */
294                 age = time(NULL) - (qrbuf->QRmtime);
295                 purge_secs = (time_t)config.c_roompurge * (time_t)86400;
296                 if (purge_secs <= (time_t)0) return;
297                 lprintf(CTDL_DEBUG, "<%s> is <%ld> seconds old\n", qrbuf->QRname, (long)age);
298                 if (age > purge_secs) do_purge = 1;
299         } /* !QR_MAILBOX */
300
301         if (do_purge) {
302                 pptr = (struct PurgeList *) malloc(sizeof(struct PurgeList));
303                 pptr->next = RoomPurgeList;
304                 strcpy(pptr->name, qrbuf->QRname);
305                 RoomPurgeList = pptr;
306         }
307
308 }
309
310
311
312 int PurgeRooms(void) {
313         struct PurgeList *pptr;
314         int num_rooms_purged = 0;
315         struct ctdlroom qrbuf;
316         struct ValidUser *vuptr;
317         char *transcript = NULL;
318
319         lprintf(CTDL_DEBUG, "PurgeRooms() called\n");
320
321
322         /* Load up a table full of valid user numbers so we can delete
323          * user-owned rooms for users who no longer exist */
324         ForEachUser(AddValidUser, NULL);
325
326         /* Then cycle through the room file */
327         ForEachRoom(DoPurgeRooms, NULL);
328
329         /* Free the valid user list */
330         while (ValidUserList != NULL) {
331                 vuptr = ValidUserList->next;
332                 free(ValidUserList);
333                 ValidUserList = vuptr;
334         }
335
336
337         transcript = malloc(SIZ);
338         strcpy(transcript, "The following rooms have been auto-purged:\n");
339
340         while (RoomPurgeList != NULL) {
341                 if (getroom(&qrbuf, RoomPurgeList->name) == 0) {
342                         transcript=realloc(transcript, strlen(transcript)+SIZ);
343                         snprintf(&transcript[strlen(transcript)], SIZ, " %s\n",
344                                 qrbuf.QRname);
345                         delete_room(&qrbuf);
346                 }
347                 pptr = RoomPurgeList->next;
348                 free(RoomPurgeList);
349                 RoomPurgeList = pptr;
350                 ++num_rooms_purged;
351         }
352
353         if (num_rooms_purged > 0) aide_message(transcript, "Room Autopurger Message");
354         free(transcript);
355
356         lprintf(CTDL_DEBUG, "Purged %d rooms.\n", num_rooms_purged);
357         return(num_rooms_purged);
358 }
359
360
361 /*
362  * Back end function to check user accounts for associated Unix accounts
363  * which no longer exist.  (Only relevant for host auth mode.)
364  */
365 void do_uid_user_purge(struct ctdluser *us, void *data) {
366         struct PurgeList *pptr;
367
368         if ((us->uid != (-1)) && (us->uid != CTDLUID)) {
369                 if (getpwuid(us->uid) == NULL) {
370                         pptr = (struct PurgeList *)
371                                 malloc(sizeof(struct PurgeList));
372                         pptr->next = UserPurgeList;
373                         strcpy(pptr->name, us->fullname);
374                         UserPurgeList = pptr;
375                 }
376         }
377 }
378
379
380
381 /*
382  * Back end function to check user accounts for expiration.
383  */
384 void do_user_purge(struct ctdluser *us, void *data) {
385         int purge;
386         time_t now;
387         time_t purge_time;
388         struct PurgeList *pptr;
389
390         /* stupid recovery routine to re-create missing mailboxen.
391          * don't enable this.
392         struct ctdlroom qrbuf;
393         char mailboxname[ROOMNAMELEN];
394         MailboxName(mailboxname, us, MAILROOM);
395         create_room(mailboxname, 4, "", 0, 1, 1, VIEW_BBS);
396         if (getroom(&qrbuf, mailboxname) != 0) return;
397         lprintf(CTDL_DEBUG, "Got %s\n", qrbuf.QRname);
398          */
399
400
401         /* Set purge time; if the user overrides the system default, use it */
402         if (us->USuserpurge > 0) {
403                 purge_time = ((time_t)us->USuserpurge) * 86400L;
404         }
405         else {
406                 purge_time = ((time_t)config.c_userpurge) * 86400L;
407         }
408
409         /* The default rule is to not purge. */
410         purge = 0;
411
412         /* If the user hasn't called in two months, his/her account
413          * has expired, so purge the record.
414          */
415         now = time(NULL);
416         if ((now - us->lastcall) > purge_time) purge = 1;
417
418         /* If the user set his/her password to 'deleteme', he/she
419          * wishes to be deleted, so purge the record.
420          */
421         if (!strcasecmp(us->password, "deleteme")) purge = 1;
422
423         /* If the record is marked as permanent, don't purge it.
424          */
425         if (us->flags & US_PERM) purge = 0;
426
427         /* If the access level is 0, the record should already have been
428          * deleted, but maybe the user was logged in at the time or something.
429          * Delete the record now.
430          */
431         if (us->axlevel == 0) purge = 1;
432
433         /* 0 calls is impossible.  If there are 0 calls, it must
434          * be a corrupted record, so purge it.
435          */
436         if (us->timescalled == 0) purge = 1;
437
438         /* User number 0, as well as any negative user number, is
439          * also impossible.
440          */
441         if (us->usernum < 1L) purge = 1;
442
443         if (purge == 1) {
444                 pptr = (struct PurgeList *) malloc(sizeof(struct PurgeList));
445                 pptr->next = UserPurgeList;
446                 strcpy(pptr->name, us->fullname);
447                 UserPurgeList = pptr;
448         }
449
450 }
451
452
453
454 int PurgeUsers(void) {
455         struct PurgeList *pptr;
456         int num_users_purged = 0;
457         char *transcript = NULL;
458
459         lprintf(CTDL_DEBUG, "PurgeUsers() called\n");
460
461         if (config.c_auth_mode == 1) {
462                 /* host auth mode */
463                 ForEachUser(do_uid_user_purge, NULL);
464         }
465         else {
466                 /* native auth mode */
467                 if (config.c_userpurge > 0) {
468                         ForEachUser(do_user_purge, NULL);
469                 }
470         }
471
472         transcript = malloc(SIZ);
473         strcpy(transcript, "The following users have been auto-purged:\n");
474
475         while (UserPurgeList != NULL) {
476                 transcript=realloc(transcript, strlen(transcript)+SIZ);
477                 snprintf(&transcript[strlen(transcript)], SIZ, " %s\n",
478                         UserPurgeList->name);
479                 purge_user(UserPurgeList->name);
480                 pptr = UserPurgeList->next;
481                 free(UserPurgeList);
482                 UserPurgeList = pptr;
483                 ++num_users_purged;
484         }
485
486         if (num_users_purged > 0) aide_message(transcript,"User Purge Message");
487         free(transcript);
488
489         lprintf(CTDL_DEBUG, "Purged %d users.\n", num_users_purged);
490         return(num_users_purged);
491 }
492
493
494 /*
495  * Purge visits
496  *
497  * This is a really cumbersome "garbage collection" function.  We have to
498  * delete visits which refer to rooms and/or users which no longer exist.  In
499  * order to prevent endless traversals of the room and user files, we first
500  * build linked lists of rooms and users which _do_ exist on the system, then
501  * traverse the visit file, checking each record against those two lists and
502  * purging the ones that do not have a match on _both_ lists.  (Remember, if
503  * either the room or user being referred to is no longer on the system, the
504  * record is completely useless.)
505  */
506 int PurgeVisits(void) {
507         struct cdbdata *cdbvisit;
508         struct visit vbuf;
509         struct VPurgeList *VisitPurgeList = NULL;
510         struct VPurgeList *vptr;
511         int purged = 0;
512         char IndexBuf[32];
513         int IndexLen;
514         struct ValidRoom *vrptr;
515         struct ValidUser *vuptr;
516         int RoomIsValid, UserIsValid;
517
518         /* First, load up a table full of valid room/gen combinations */
519         ForEachRoom(AddValidRoom, NULL);
520
521         /* Then load up a table full of valid user numbers */
522         ForEachUser(AddValidUser, NULL);
523
524         /* Now traverse through the visits, purging irrelevant records... */
525         cdb_rewind(CDB_VISIT);
526         while(cdbvisit = cdb_next_item(CDB_VISIT), cdbvisit != NULL) {
527                 memset(&vbuf, 0, sizeof(struct visit));
528                 memcpy(&vbuf, cdbvisit->ptr,
529                         ( (cdbvisit->len > sizeof(struct visit)) ?
530                         sizeof(struct visit) : cdbvisit->len) );
531                 cdb_free(cdbvisit);
532
533                 RoomIsValid = 0;
534                 UserIsValid = 0;
535
536                 /* Check to see if the room exists */
537                 for (vrptr=ValidRoomList; vrptr!=NULL; vrptr=vrptr->next) {
538                         if ( (vrptr->vr_roomnum==vbuf.v_roomnum)
539                              && (vrptr->vr_roomgen==vbuf.v_roomgen))
540                                 RoomIsValid = 1;
541                 }
542
543                 /* Check to see if the user exists */
544                 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
545                         if (vuptr->vu_usernum == vbuf.v_usernum)
546                                 UserIsValid = 1;
547                 }
548
549                 /* Put the record on the purge list if it's dead */
550                 if ((RoomIsValid==0) || (UserIsValid==0)) {
551                         vptr = (struct VPurgeList *)
552                                 malloc(sizeof(struct VPurgeList));
553                         vptr->next = VisitPurgeList;
554                         vptr->vp_roomnum = vbuf.v_roomnum;
555                         vptr->vp_roomgen = vbuf.v_roomgen;
556                         vptr->vp_usernum = vbuf.v_usernum;
557                         VisitPurgeList = vptr;
558                 }
559
560         }
561
562         /* Free the valid room/gen combination list */
563         while (ValidRoomList != NULL) {
564                 vrptr = ValidRoomList->next;
565                 free(ValidRoomList);
566                 ValidRoomList = vrptr;
567         }
568
569         /* Free the valid user list */
570         while (ValidUserList != NULL) {
571                 vuptr = ValidUserList->next;
572                 free(ValidUserList);
573                 ValidUserList = vuptr;
574         }
575
576         /* Now delete every visit on the purged list */
577         while (VisitPurgeList != NULL) {
578                 IndexLen = GenerateRelationshipIndex(IndexBuf,
579                                 VisitPurgeList->vp_roomnum,
580                                 VisitPurgeList->vp_roomgen,
581                                 VisitPurgeList->vp_usernum);
582                 cdb_delete(CDB_VISIT, IndexBuf, IndexLen);
583                 vptr = VisitPurgeList->next;
584                 free(VisitPurgeList);
585                 VisitPurgeList = vptr;
586                 ++purged;
587         }
588
589         return(purged);
590 }
591
592 /*
593  * Purge the use table of old entries.
594  *
595  */
596 int PurgeUseTable(void) {
597         int purged = 0;
598         struct cdbdata *cdbut;
599         struct UseTable ut;
600         struct UPurgeList *ul = NULL;
601         struct UPurgeList *uptr; 
602
603         /* Phase 1: traverse through the table, discovering old records... */
604         lprintf(CTDL_DEBUG, "Purge use table: phase 1\n");
605         cdb_rewind(CDB_USETABLE);
606         while(cdbut = cdb_next_item(CDB_USETABLE), cdbut != NULL) {
607
608                 memcpy(&ut, cdbut->ptr,
609                        ((cdbut->len > sizeof(struct UseTable)) ?
610                         sizeof(struct UseTable) : cdbut->len));
611                 cdb_free(cdbut);
612
613                 if ( (time(NULL) - ut.ut_timestamp) > USETABLE_RETAIN ) {
614                         uptr = (struct UPurgeList *) malloc(sizeof(struct UPurgeList));
615                         if (uptr != NULL) {
616                                 uptr->next = ul;
617                                 safestrncpy(uptr->up_key, ut.ut_msgid, sizeof uptr->up_key);
618                                 ul = uptr;
619                         }
620                         ++purged;
621                 }
622
623         }
624
625         /* Phase 2: delete the records */
626         lprintf(CTDL_DEBUG, "Purge use table: phase 2\n");
627         while (ul != NULL) {
628                 cdb_delete(CDB_USETABLE, ul->up_key, strlen(ul->up_key));
629                 uptr = ul->next;
630                 free(ul);
631                 ul = uptr;
632         }
633
634         lprintf(CTDL_DEBUG, "Purge use table: finished (purged %d records)\n", purged);
635         return(purged);
636 }
637
638
639
640 /*
641  * Purge the EUID Index of old records.
642  *
643  */
644 int PurgeEuidIndexTable(void) {
645         int purged = 0;
646         struct cdbdata *cdbei;
647         struct EPurgeList *el = NULL;
648         struct EPurgeList *eptr; 
649         long msgnum;
650         struct CtdlMessage *msg;
651
652         /* Phase 1: traverse through the table, discovering old records... */
653         lprintf(CTDL_DEBUG, "Purge EUID index: phase 1\n");
654         cdb_rewind(CDB_EUIDINDEX);
655         while(cdbei = cdb_next_item(CDB_EUIDINDEX), cdbei != NULL) {
656
657                 memcpy(&msgnum, cdbei->ptr, sizeof(long));
658
659                 msg = CtdlFetchMessage(msgnum, 0);
660                 if (msg != NULL) {
661                         CtdlFreeMessage(msg);   /* it still exists, so do nothing */
662                 }
663                 else {
664                         eptr = (struct EPurgeList *) malloc(sizeof(struct EPurgeList));
665                         if (eptr != NULL) {
666                                 eptr->next = el;
667                                 eptr->ep_keylen = cdbei->len - sizeof(long);
668                                 eptr->ep_key = malloc(cdbei->len);
669                                 memcpy(eptr->ep_key, &cdbei->ptr[sizeof(long)], eptr->ep_keylen);
670                                 el = eptr;
671                         }
672                         ++purged;
673                 }
674
675                 cdb_free(cdbei);
676
677         }
678
679         /* Phase 2: delete the records */
680         lprintf(CTDL_DEBUG, "Purge euid index: phase 2\n");
681         while (el != NULL) {
682                 cdb_delete(CDB_EUIDINDEX, el->ep_key, el->ep_keylen);
683                 free(el->ep_key);
684                 eptr = el->next;
685                 free(el);
686                 el = eptr;
687         }
688
689         lprintf(CTDL_DEBUG, "Purge euid index: finished (purged %d records)\n", purged);
690         return(purged);
691 }
692
693
694 void purge_databases(void) {
695         int retval;
696         static time_t last_purge = 0;
697         time_t now;
698         struct tm tm;
699
700         /* Do the auto-purge if the current hour equals the purge hour,
701          * but not if the operation has already been performed in the
702          * last twelve hours.  This is usually enough granularity.
703          */
704         now = time(NULL);
705         localtime_r(&now, &tm);
706         if (tm.tm_hour != config.c_purge_hour) return;
707         if ((now - last_purge) < 43200) return;
708
709         lprintf(CTDL_INFO, "Auto-purger: starting.\n");
710
711         retval = PurgeUsers();
712         lprintf(CTDL_NOTICE, "Purged %d users.\n", retval);
713
714         PurgeMessages();
715         lprintf(CTDL_NOTICE, "Expired %d messages.\n", messages_purged);
716
717         retval = PurgeRooms();
718         lprintf(CTDL_NOTICE, "Expired %d rooms.\n", retval);
719
720         retval = PurgeVisits();
721         lprintf(CTDL_NOTICE, "Purged %d visits.\n", retval);
722
723         retval = PurgeUseTable();
724         lprintf(CTDL_NOTICE, "Purged %d entries from the use table.\n", retval);
725
726         retval = PurgeEuidIndexTable();
727         lprintf(CTDL_NOTICE, "Purged %d entries from the EUID index.\n", retval);
728
729         lprintf(CTDL_INFO, "Auto-purger: finished.\n");
730
731         last_purge = now;       /* So we don't do it again soon */
732 }
733
734 /*****************************************************************************/
735
736
737 void do_fsck_msg(long msgnum, void *userdata) {
738         struct ctdlroomref *ptr;
739
740         ptr = (struct ctdlroomref *)malloc(sizeof(struct ctdlroomref));
741         ptr->next = rr;
742         ptr->msgnum = msgnum;
743         rr = ptr;
744 }
745
746 void do_fsck_room(struct ctdlroom *qrbuf, void *data)
747 {
748         getroom(&CC->room, qrbuf->QRname);
749         CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, do_fsck_msg, NULL);
750 }
751
752 /*
753  * Check message reference counts
754  */
755 void cmd_fsck(char *argbuf) {
756         long msgnum;
757         struct cdbdata *cdbmsg;
758         struct MetaData smi;
759         struct ctdlroomref *ptr;
760         int realcount;
761
762         if (CtdlAccessCheck(ac_aide)) return;
763
764         /* Lame way of checking whether anyone else is doing this now */
765         if (rr != NULL) {
766                 cprintf("%d Another FSCK is already running.\n", ERROR + RESOURCE_BUSY);
767                 return;
768         }
769
770         cprintf("%d Checking message reference counts\n", LISTING_FOLLOWS);
771
772         cprintf("\nThis could take a while.  Please be patient!\n\n");
773         cprintf("Gathering pointers...\n");
774         ForEachRoom(do_fsck_room, NULL);
775
776         get_control();
777         cprintf("Checking message base...\n");
778         for (msgnum = 0L; msgnum <= CitControl.MMhighest; ++msgnum) {
779
780                 cdbmsg = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
781                 if (cdbmsg != NULL) {
782                         cdb_free(cdbmsg);
783                         cprintf("Message %7ld    ", msgnum);
784
785                         GetMetaData(&smi, msgnum);
786                         cprintf("refcount=%-2d   ", smi.meta_refcount);
787
788                         realcount = 0;
789                         for (ptr = rr; ptr != NULL; ptr = ptr->next) {
790                                 if (ptr->msgnum == msgnum) ++realcount;
791                         }
792                         cprintf("realcount=%-2d\n", realcount);
793
794                         if ( (smi.meta_refcount != realcount)
795                            || (realcount == 0) ) {
796                                 smi.meta_refcount = realcount;
797                                 PutMetaData(&smi);
798                                 AdjRefCount(msgnum, 0); /* deletes if needed */
799                         }
800
801                 }
802
803         }
804
805         cprintf("Freeing memory...\n");
806         while (rr != NULL) {
807                 ptr = rr->next;
808                 free(rr);
809                 rr = ptr;
810         }
811
812         cprintf("Done!\n");
813         cprintf("000\n");
814
815 }
816
817
818
819
820 /*****************************************************************************/
821
822 char *serv_expire_init(void)
823 {
824         CtdlRegisterSessionHook(purge_databases, EVT_TIMER);
825         CtdlRegisterProtoHook(cmd_fsck, "FSCK", "Check message ref counts");
826         return "$Id$";
827 }