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 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
222 if (vuptr->vu_usernum == atol(qrbuf->QRname)) {
232 /* Any of these attributes render a room non-purgable */
233 if (qrbuf->QRflags & QR_PERMANENT) return;
234 if (qrbuf->QRflags & QR_DIRECTORY) return;
235 if (qrbuf->QRflags & QR_NETWORK) return;
236 if (!strcasecmp(qrbuf->QRname, SYSCONFIGROOM)) return;
237 if (is_noneditable(qrbuf)) return;
239 /* If we don't know the modification date, be safe and don't purge */
240 if (qrbuf->QRmtime <= (time_t)0) return;
242 /* If no room purge time is set, be safe and don't purge */
243 if (config.c_roompurge < 0) return;
245 /* Otherwise, check the date of last modification */
246 age = time(NULL) - (qrbuf->QRmtime);
247 purge_secs = (time_t)config.c_roompurge * (time_t)86400;
248 if (purge_secs <= (time_t)0) return;
249 lprintf(9, "<%s> is <%ld> seconds old\n", qrbuf->QRname, age);
250 if (age > purge_secs) do_purge = 1;
252 BYPASS: if (do_purge) {
253 pptr = (struct PurgeList *) mallok(sizeof(struct PurgeList));
254 pptr->next = RoomPurgeList;
255 strcpy(pptr->name, qrbuf->QRname);
256 RoomPurgeList = pptr;
263 int PurgeRooms(void) {
264 struct PurgeList *pptr;
265 int num_rooms_purged = 0;
266 struct quickroom qrbuf;
267 struct ValidUser *vuptr;
268 char *transcript = NULL;
270 lprintf(5, "PurgeRooms() called\n");
273 /* Load up a table full of valid user numbers so we can delete
274 * user-owned rooms for users who no longer exist */
275 ForEachUser(AddValidUser, NULL);
277 /* Then cycle through the room file */
278 ForEachRoom(DoPurgeRooms, NULL);
280 /* Free the valid user list */
281 while (ValidUserList != NULL) {
282 vuptr = ValidUserList->next;
283 phree(ValidUserList);
284 ValidUserList = vuptr;
288 transcript = mallok(256);
289 strcpy(transcript, "The following rooms have been auto-purged:\n");
291 while (RoomPurgeList != NULL) {
292 if (getroom(&qrbuf, RoomPurgeList->name) == 0) {
293 transcript=reallok(transcript, strlen(transcript)+256);
294 sprintf(&transcript[strlen(transcript)], " %s\n",
298 pptr = RoomPurgeList->next;
299 phree(RoomPurgeList);
300 RoomPurgeList = pptr;
304 if (num_rooms_purged > 0) aide_message(transcript);
307 lprintf(5, "Purged %d rooms.\n", num_rooms_purged);
308 return(num_rooms_purged);
312 void do_user_purge(struct usersupp *us, void *data) {
316 struct PurgeList *pptr;
318 /* Set purge time; if the user overrides the system default, use it */
319 if (us->USuserpurge > 0) {
320 purge_time = ((time_t)us->USuserpurge) * 86400L;
323 purge_time = ((time_t)config.c_userpurge) * 86400L;
326 /* The default rule is to not purge. */
329 /* If the user hasn't called in two months, his/her account
330 * has expired, so purge the record.
333 if ((now - us->lastcall) > purge_time) purge = 1;
335 /* If the user set his/her password to 'deleteme', he/she
336 * wishes to be deleted, so purge the record.
338 if (!strcasecmp(us->password, "deleteme")) purge = 1;
340 /* If the record is marked as permanent, don't purge it.
342 if (us->flags & US_PERM) purge = 0;
344 /* If the access level is 0, the record should already have been
345 * deleted, but maybe the user was logged in at the time or something.
346 * Delete the record now.
348 if (us->axlevel == 0) purge = 1;
350 /* 0 calls is impossible. If there are 0 calls, it must
351 * be a corrupted record, so purge it.
353 if (us->timescalled == 0) purge = 1;
356 pptr = (struct PurgeList *) mallok(sizeof(struct PurgeList));
357 pptr->next = UserPurgeList;
358 strcpy(pptr->name, us->fullname);
359 UserPurgeList = pptr;
366 int PurgeUsers(void) {
367 struct PurgeList *pptr;
368 int num_users_purged = 0;
369 char *transcript = NULL;
371 lprintf(5, "PurgeUsers() called\n");
372 if (config.c_userpurge > 0) {
373 ForEachUser(do_user_purge, NULL);
376 transcript = mallok(256);
377 strcpy(transcript, "The following users have been auto-purged:\n");
379 while (UserPurgeList != NULL) {
380 transcript=reallok(transcript, strlen(transcript)+256);
381 sprintf(&transcript[strlen(transcript)], " %s\n",
382 UserPurgeList->name);
383 purge_user(UserPurgeList->name);
384 pptr = UserPurgeList->next;
385 phree(UserPurgeList);
386 UserPurgeList = pptr;
390 if (num_users_purged > 0) aide_message(transcript);
393 lprintf(5, "Purged %d users.\n", num_users_purged);
394 return(num_users_purged);
401 * This is a really cumbersome "garbage collection" function. We have to
402 * delete visits which refer to rooms and/or users which no longer exist. In
403 * order to prevent endless traversals of the room and user files, we first
404 * build linked lists of rooms and users which _do_ exist on the system, then
405 * traverse the visit file, checking each record against those two lists and
406 * purging the ones that do not have a match on _both_ lists. (Remember, if
407 * either the room or user being referred to is no longer on the system, the
408 * record is completely useless.)
410 int PurgeVisits(void) {
411 struct cdbdata *cdbvisit;
413 struct VPurgeList *VisitPurgeList = NULL;
414 struct VPurgeList *vptr;
418 struct ValidRoom *vrptr;
419 struct ValidUser *vuptr;
420 int RoomIsValid, UserIsValid;
422 /* First, load up a table full of valid room/gen combinations */
423 ForEachRoom(AddValidRoom, NULL);
425 /* Then load up a table full of valid user numbers */
426 ForEachUser(AddValidUser, NULL);
428 /* Now traverse through the visits, purging irrelevant records... */
429 cdb_rewind(CDB_VISIT);
430 while(cdbvisit = cdb_next_item(CDB_VISIT), cdbvisit != NULL) {
431 memset(&vbuf, 0, sizeof(struct visit));
432 memcpy(&vbuf, cdbvisit->ptr,
433 ( (cdbvisit->len > sizeof(struct visit)) ?
434 sizeof(struct visit) : cdbvisit->len) );
440 /* Check to see if the room exists */
441 for (vrptr=ValidRoomList; vrptr!=NULL; vrptr=vrptr->next) {
442 if ( (vrptr->vr_roomnum==vbuf.v_roomnum)
443 && (vrptr->vr_roomgen==vbuf.v_roomgen))
447 /* Check to see if the user exists */
448 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
449 if (vuptr->vu_usernum == vbuf.v_usernum)
453 /* Put the record on the purge list if it's dead */
454 if ((RoomIsValid==0) || (UserIsValid==0)) {
455 vptr = (struct VPurgeList *)
456 mallok(sizeof(struct VPurgeList));
457 vptr->next = VisitPurgeList;
458 vptr->vp_roomnum = vbuf.v_roomnum;
459 vptr->vp_roomgen = vbuf.v_roomgen;
460 vptr->vp_usernum = vbuf.v_usernum;
461 VisitPurgeList = vptr;
466 /* Free the valid room/gen combination list */
467 while (ValidRoomList != NULL) {
468 vrptr = ValidRoomList->next;
469 phree(ValidRoomList);
470 ValidRoomList = vrptr;
473 /* Free the valid user list */
474 while (ValidUserList != NULL) {
475 vuptr = ValidUserList->next;
476 phree(ValidUserList);
477 ValidUserList = vuptr;
480 /* Now delete every visit on the purged list */
481 while (VisitPurgeList != NULL) {
482 IndexLen = GenerateRelationshipIndex(IndexBuf,
483 VisitPurgeList->vp_roomnum,
484 VisitPurgeList->vp_roomgen,
485 VisitPurgeList->vp_usernum);
486 cdb_delete(CDB_VISIT, IndexBuf, IndexLen);
487 vptr = VisitPurgeList->next;
488 phree(VisitPurgeList);
489 VisitPurgeList = vptr;
497 void cmd_expi(char *argbuf) {
501 if (CtdlAccessCheck(ac_aide)) return;
503 extract(cmd, argbuf, 0);
504 if (!strcasecmp(cmd, "users")) {
505 retval = PurgeUsers();
506 cprintf("%d Purged %d users.\n", OK, retval);
509 else if (!strcasecmp(cmd, "messages")) {
511 cprintf("%d Expired %d messages.\n", OK, messages_purged);
514 else if (!strcasecmp(cmd, "rooms")) {
515 retval = PurgeRooms();
516 cprintf("%d Expired %d rooms.\n", OK, retval);
519 else if (!strcasecmp(cmd, "visits")) {
520 retval = PurgeVisits();
521 cprintf("%d Purged %d visits.\n", OK, retval);
523 else if (!strcasecmp(cmd, "defrag")) {
525 cprintf("%d Defragmented the databases.\n", OK);
528 cprintf("%d Invalid command.\n", ERROR+ILLEGAL_VALUE);
533 /*****************************************************************************/
536 void do_fsck_msg(long msgnum) {
539 ptr = (struct roomref *)mallok(sizeof(struct roomref));
541 ptr->msgnum = msgnum;
545 void do_fsck_room(struct quickroom *qrbuf, void *data)
547 getroom(&CC->quickroom, qrbuf->QRname);
548 CtdlForEachMessage(MSGS_ALL, 0L, (-127), NULL, NULL, do_fsck_msg);
552 * Check message reference counts
554 void cmd_fsck(char *argbuf) {
556 struct cdbdata *cdbmsg;
557 struct SuppMsgInfo smi;
561 if (CtdlAccessCheck(ac_aide)) return;
563 /* Lame way of checking whether anyone else is doing this now */
565 cprintf("%d Another FSCK is already running.\n", ERROR);
569 cprintf("%d Checking message reference counts\n", LISTING_FOLLOWS);
571 cprintf("\nThis could take a while. Please be patient!\n\n");
572 cprintf("Gathering pointers...\n");
573 ForEachRoom(do_fsck_room, NULL);
576 cprintf("Checking message base...\n");
577 for (msgnum = 0L; msgnum <= CitControl.MMhighest; ++msgnum) {
579 cdbmsg = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
580 if (cdbmsg != NULL) {
582 cprintf("Message %7ld ", msgnum);
584 GetSuppMsgInfo(&smi, msgnum);
585 cprintf("refcount=%-2d ", smi.smi_refcount);
588 for (ptr = rr; ptr != NULL; ptr = ptr->next) {
589 if (ptr->msgnum == msgnum) ++realcount;
591 cprintf("realcount=%-2d\n", realcount);
593 if ( (smi.smi_refcount != realcount)
594 || (realcount == 0) ) {
595 smi.smi_refcount = realcount;
596 PutSuppMsgInfo(&smi);
597 AdjRefCount(msgnum, 0); /* deletes if needed */
604 cprintf("Freeing memory...\n");
619 /*****************************************************************************/
621 char *Dynamic_Module_Init(void)
623 CtdlRegisterProtoHook(cmd_expi, "EXPI", "Expire old system objects");
624 CtdlRegisterProtoHook(cmd_fsck, "FSCK", "Check message ref counts");