4 * This module handles the expiry of old messages and the purging of old users.
10 * A brief technical discussion:
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.
16 * At first glance this may seem cumbersome and unnecessary. The reason it is
17 * implemented in this way is because GDBM (and perhaps some other backends we
18 * 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.
33 #include <sys/types.h>
42 #include "sysdep_decls.h"
43 #include "citserver.h"
46 #include "dynloader.h"
57 char v_roomname[ROOMNAMELEN];
64 struct PurgeList *next;
65 char name[ROOMNAMELEN]; /* use the larger of username or roomname */
69 struct VPurgeList *next;
76 struct ValidRoom *next;
82 struct ValidUser *next;
93 struct PurgeList *UserPurgeList = NULL;
94 struct PurgeList *RoomPurgeList = NULL;
95 struct ValidRoom *ValidRoomList = NULL;
96 struct ValidUser *ValidUserList = NULL;
99 struct roomref *rr = NULL;
101 extern struct CitContext *ContextList;
103 void DoPurgeMessages(struct quickroom *qrbuf, void *data) {
104 struct ExpirePolicy epbuf;
107 struct CtdlMessage *msg;
109 struct cdbdata *cdbfr;
110 long *msglist = NULL;
114 GetExpirePolicy(&epbuf, qrbuf);
116 /* If the room is set to never expire messages ... do nothing */
117 if (epbuf.expire_mode == EXPIRE_NEXTLEVEL) return;
118 if (epbuf.expire_mode == EXPIRE_MANUAL) return;
120 begin_critical_section(S_QUICKROOM);
121 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long));
124 msglist = mallok(cdbfr->len);
125 memcpy(msglist, cdbfr->ptr, cdbfr->len);
126 num_msgs = cdbfr->len / sizeof(long);
130 /* Nothing to do if there aren't any messages */
132 end_critical_section(S_QUICKROOM);
136 /* If the room is set to expire by count, do that */
137 if (epbuf.expire_mode == EXPIRE_NUMMSGS) {
138 while (num_msgs > epbuf.expire_value) {
140 lprintf(5, "Expiring message %ld\n", delnum);
141 AdjRefCount(delnum, -1);
142 memcpy(&msglist[0], &msglist[1],
143 (sizeof(long)*(num_msgs - 1)));
149 /* If the room is set to expire by age... */
150 if (epbuf.expire_mode == EXPIRE_AGE) {
151 for (a=0; a<num_msgs; ++a) {
154 msg = CtdlFetchMessage(delnum);
156 xtime = atol(msg->cm_fields['T']);
157 CtdlFreeMessage(msg);
163 && (now - xtime > (time_t)(epbuf.expire_value * 86400L))) {
164 lprintf(5, "Expiring message %ld\n", delnum);
165 AdjRefCount(delnum, -1);
173 num_msgs = sort_msglist(msglist, num_msgs);
176 cdb_store(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long),
177 msglist, (num_msgs * sizeof(long)) );
179 if (msglist != NULL) phree(msglist);
181 end_critical_section(S_QUICKROOM);
184 void PurgeMessages(void) {
185 lprintf(5, "PurgeMessages() called\n");
187 ForEachRoom(DoPurgeMessages, NULL);
191 void AddValidUser(struct usersupp *usbuf, void *data) {
192 struct ValidUser *vuptr;
194 vuptr = (struct ValidUser *)mallok(sizeof(struct ValidUser));
195 vuptr->next = ValidUserList;
196 vuptr->vu_usernum = usbuf->usernum;
197 ValidUserList = vuptr;
200 void AddValidRoom(struct quickroom *qrbuf, void *data) {
201 struct ValidRoom *vrptr;
203 vrptr = (struct ValidRoom *)mallok(sizeof(struct ValidRoom));
204 vrptr->next = ValidRoomList;
205 vrptr->vr_roomnum = qrbuf->QRnumber;
206 vrptr->vr_roomgen = qrbuf->QRgen;
207 ValidRoomList = vrptr;
210 void DoPurgeRooms(struct quickroom *qrbuf, void *data) {
211 time_t age, purge_secs;
212 struct PurgeList *pptr;
213 struct ValidUser *vuptr;
216 /* For mailbox rooms, there's only one purging rule: if the user who
217 * owns the room still exists, we keep the room; otherwise, we purge
218 * it. Bypass any other rules.
220 if (qrbuf->QRflags & QR_MAILBOX) {
221 /* if user not found, do_purge will be 1 */
223 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
224 if (vuptr->vu_usernum == atol(qrbuf->QRname)) {
230 /* Any of these attributes render a room non-purgable */
231 if (qrbuf->QRflags & QR_PERMANENT) return;
232 if (qrbuf->QRflags & QR_DIRECTORY) return;
233 if (qrbuf->QRflags & QR_NETWORK) return;
234 if (!strcasecmp(qrbuf->QRname, SYSCONFIGROOM)) return;
235 if (is_noneditable(qrbuf)) return;
237 /* If we don't know the modification date, be safe and don't purge */
238 if (qrbuf->QRmtime <= (time_t)0) return;
240 /* If no room purge time is set, be safe and don't purge */
241 if (config.c_roompurge < 0) return;
243 /* Otherwise, check the date of last modification */
244 age = time(NULL) - (qrbuf->QRmtime);
245 purge_secs = (time_t)config.c_roompurge * (time_t)86400;
246 if (purge_secs <= (time_t)0) return;
247 lprintf(9, "<%s> is <%ld> seconds old\n", qrbuf->QRname, age);
248 if (age > purge_secs) do_purge = 1;
252 pptr = (struct PurgeList *) mallok(sizeof(struct PurgeList));
253 pptr->next = RoomPurgeList;
254 strcpy(pptr->name, qrbuf->QRname);
255 RoomPurgeList = pptr;
262 int PurgeRooms(void) {
263 struct PurgeList *pptr;
264 int num_rooms_purged = 0;
265 struct quickroom qrbuf;
266 struct ValidUser *vuptr;
267 char *transcript = NULL;
269 lprintf(5, "PurgeRooms() called\n");
272 /* Load up a table full of valid user numbers so we can delete
273 * user-owned rooms for users who no longer exist */
274 ForEachUser(AddValidUser, NULL);
276 /* Then cycle through the room file */
277 ForEachRoom(DoPurgeRooms, NULL);
279 /* Free the valid user list */
280 while (ValidUserList != NULL) {
281 vuptr = ValidUserList->next;
282 phree(ValidUserList);
283 ValidUserList = vuptr;
287 transcript = mallok(256);
288 strcpy(transcript, "The following rooms have been auto-purged:\n");
290 while (RoomPurgeList != NULL) {
291 if (getroom(&qrbuf, RoomPurgeList->name) == 0) {
292 transcript=reallok(transcript, strlen(transcript)+256);
293 sprintf(&transcript[strlen(transcript)], " %s\n",
297 pptr = RoomPurgeList->next;
298 phree(RoomPurgeList);
299 RoomPurgeList = pptr;
303 if (num_rooms_purged > 0) aide_message(transcript);
306 lprintf(5, "Purged %d rooms.\n", num_rooms_purged);
307 return(num_rooms_purged);
311 void do_user_purge(struct usersupp *us, void *data) {
315 struct PurgeList *pptr;
317 /* stupid recovery routine to re-create missing mailboxen.
319 struct quickroom qrbuf;
320 char mailboxname[ROOMNAMELEN];
321 MailboxName(mailboxname, us, MAILROOM);
322 create_room(mailboxname, 4, "", 0);
323 if (getroom(&qrbuf, mailboxname) != 0) return;
324 lprintf(9, "Got %s\n", qrbuf.QRname);
328 /* Set purge time; if the user overrides the system default, use it */
329 if (us->USuserpurge > 0) {
330 purge_time = ((time_t)us->USuserpurge) * 86400L;
333 purge_time = ((time_t)config.c_userpurge) * 86400L;
336 /* The default rule is to not purge. */
339 /* If the user hasn't called in two months, his/her account
340 * has expired, so purge the record.
343 if ((now - us->lastcall) > purge_time) purge = 1;
345 /* If the user set his/her password to 'deleteme', he/she
346 * wishes to be deleted, so purge the record.
348 if (!strcasecmp(us->password, "deleteme")) purge = 1;
350 /* If the record is marked as permanent, don't purge it.
352 if (us->flags & US_PERM) purge = 0;
354 /* If the access level is 0, the record should already have been
355 * deleted, but maybe the user was logged in at the time or something.
356 * Delete the record now.
358 if (us->axlevel == 0) purge = 1;
360 /* 0 calls is impossible. If there are 0 calls, it must
361 * be a corrupted record, so purge it.
363 if (us->timescalled == 0) purge = 1;
366 pptr = (struct PurgeList *) mallok(sizeof(struct PurgeList));
367 pptr->next = UserPurgeList;
368 strcpy(pptr->name, us->fullname);
369 UserPurgeList = pptr;
376 int PurgeUsers(void) {
377 struct PurgeList *pptr;
378 int num_users_purged = 0;
379 char *transcript = NULL;
381 lprintf(5, "PurgeUsers() called\n");
382 if (config.c_userpurge > 0) {
383 ForEachUser(do_user_purge, NULL);
386 transcript = mallok(256);
387 strcpy(transcript, "The following users have been auto-purged:\n");
389 while (UserPurgeList != NULL) {
390 transcript=reallok(transcript, strlen(transcript)+256);
391 sprintf(&transcript[strlen(transcript)], " %s\n",
392 UserPurgeList->name);
393 purge_user(UserPurgeList->name);
394 pptr = UserPurgeList->next;
395 phree(UserPurgeList);
396 UserPurgeList = pptr;
400 if (num_users_purged > 0) aide_message(transcript);
403 lprintf(5, "Purged %d users.\n", num_users_purged);
404 return(num_users_purged);
411 * This is a really cumbersome "garbage collection" function. We have to
412 * delete visits which refer to rooms and/or users which no longer exist. In
413 * order to prevent endless traversals of the room and user files, we first
414 * build linked lists of rooms and users which _do_ exist on the system, then
415 * traverse the visit file, checking each record against those two lists and
416 * purging the ones that do not have a match on _both_ lists. (Remember, if
417 * either the room or user being referred to is no longer on the system, the
418 * record is completely useless.)
420 int PurgeVisits(void) {
421 struct cdbdata *cdbvisit;
423 struct VPurgeList *VisitPurgeList = NULL;
424 struct VPurgeList *vptr;
428 struct ValidRoom *vrptr;
429 struct ValidUser *vuptr;
430 int RoomIsValid, UserIsValid;
432 /* First, load up a table full of valid room/gen combinations */
433 ForEachRoom(AddValidRoom, NULL);
435 /* Then load up a table full of valid user numbers */
436 ForEachUser(AddValidUser, NULL);
438 /* Now traverse through the visits, purging irrelevant records... */
439 cdb_rewind(CDB_VISIT);
440 while(cdbvisit = cdb_next_item(CDB_VISIT), cdbvisit != NULL) {
441 memset(&vbuf, 0, sizeof(struct visit));
442 memcpy(&vbuf, cdbvisit->ptr,
443 ( (cdbvisit->len > sizeof(struct visit)) ?
444 sizeof(struct visit) : cdbvisit->len) );
450 /* Check to see if the room exists */
451 for (vrptr=ValidRoomList; vrptr!=NULL; vrptr=vrptr->next) {
452 if ( (vrptr->vr_roomnum==vbuf.v_roomnum)
453 && (vrptr->vr_roomgen==vbuf.v_roomgen))
457 /* Check to see if the user exists */
458 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
459 if (vuptr->vu_usernum == vbuf.v_usernum)
463 /* Put the record on the purge list if it's dead */
464 if ((RoomIsValid==0) || (UserIsValid==0)) {
465 vptr = (struct VPurgeList *)
466 mallok(sizeof(struct VPurgeList));
467 vptr->next = VisitPurgeList;
468 vptr->vp_roomnum = vbuf.v_roomnum;
469 vptr->vp_roomgen = vbuf.v_roomgen;
470 vptr->vp_usernum = vbuf.v_usernum;
471 VisitPurgeList = vptr;
476 /* Free the valid room/gen combination list */
477 while (ValidRoomList != NULL) {
478 vrptr = ValidRoomList->next;
479 phree(ValidRoomList);
480 ValidRoomList = vrptr;
483 /* Free the valid user list */
484 while (ValidUserList != NULL) {
485 vuptr = ValidUserList->next;
486 phree(ValidUserList);
487 ValidUserList = vuptr;
490 /* Now delete every visit on the purged list */
491 while (VisitPurgeList != NULL) {
492 IndexLen = GenerateRelationshipIndex(IndexBuf,
493 VisitPurgeList->vp_roomnum,
494 VisitPurgeList->vp_roomgen,
495 VisitPurgeList->vp_usernum);
496 cdb_delete(CDB_VISIT, IndexBuf, IndexLen);
497 vptr = VisitPurgeList->next;
498 phree(VisitPurgeList);
499 VisitPurgeList = vptr;
507 void cmd_expi(char *argbuf) {
511 if (CtdlAccessCheck(ac_aide)) return;
513 extract(cmd, argbuf, 0);
514 if (!strcasecmp(cmd, "users")) {
515 retval = PurgeUsers();
516 cprintf("%d Purged %d users.\n", OK, retval);
519 else if (!strcasecmp(cmd, "messages")) {
521 cprintf("%d Expired %d messages.\n", OK, messages_purged);
524 else if (!strcasecmp(cmd, "rooms")) {
525 retval = PurgeRooms();
526 cprintf("%d Expired %d rooms.\n", OK, retval);
529 else if (!strcasecmp(cmd, "visits")) {
530 retval = PurgeVisits();
531 cprintf("%d Purged %d visits.\n", OK, retval);
533 else if (!strcasecmp(cmd, "defrag")) {
535 cprintf("%d Defragmented the databases.\n", OK);
538 cprintf("%d Invalid command.\n", ERROR+ILLEGAL_VALUE);
543 /*****************************************************************************/
546 void do_fsck_msg(long msgnum, void *userdata) {
549 ptr = (struct roomref *)mallok(sizeof(struct roomref));
551 ptr->msgnum = msgnum;
555 void do_fsck_room(struct quickroom *qrbuf, void *data)
557 getroom(&CC->quickroom, qrbuf->QRname);
558 CtdlForEachMessage(MSGS_ALL, 0L, (-127), NULL, NULL,
563 * Check message reference counts
565 void cmd_fsck(char *argbuf) {
567 struct cdbdata *cdbmsg;
568 struct SuppMsgInfo smi;
572 if (CtdlAccessCheck(ac_aide)) return;
574 /* Lame way of checking whether anyone else is doing this now */
576 cprintf("%d Another FSCK is already running.\n", ERROR);
580 cprintf("%d Checking message reference counts\n", LISTING_FOLLOWS);
582 cprintf("\nThis could take a while. Please be patient!\n\n");
583 cprintf("Gathering pointers...\n");
584 ForEachRoom(do_fsck_room, NULL);
587 cprintf("Checking message base...\n");
588 for (msgnum = 0L; msgnum <= CitControl.MMhighest; ++msgnum) {
590 cdbmsg = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
591 if (cdbmsg != NULL) {
593 cprintf("Message %7ld ", msgnum);
595 GetSuppMsgInfo(&smi, msgnum);
596 cprintf("refcount=%-2d ", smi.smi_refcount);
599 for (ptr = rr; ptr != NULL; ptr = ptr->next) {
600 if (ptr->msgnum == msgnum) ++realcount;
602 cprintf("realcount=%-2d\n", realcount);
604 if ( (smi.smi_refcount != realcount)
605 || (realcount == 0) ) {
606 smi.smi_refcount = realcount;
607 PutSuppMsgInfo(&smi);
608 AdjRefCount(msgnum, 0); /* deletes if needed */
615 cprintf("Freeing memory...\n");
630 /*****************************************************************************/
632 char *Dynamic_Module_Init(void)
634 CtdlRegisterProtoHook(cmd_expi, "EXPI", "Expire old system objects");
635 CtdlRegisterProtoHook(cmd_fsck, "FSCK", "Check message ref counts");