4 * This module handles the expiry of old messages and the purging of old users.
11 * A brief technical discussion:
13 * Several of the purge operations found in this module operate in two
14 * stages: the first stage generates a linked list of objects to be deleted,
15 * then the second stage deletes all listed objects from the database.
17 * At first glance this may seem cumbersome and unnecessary. The reason it is
18 * implemented in this way is because GDBM (and perhaps some other backends we
19 * may hook into in the future) explicitly do _not_ support the deletion of
20 * records from a file while the file is being traversed. The delete operation
21 * will succeed, but the traversal is not guaranteed to visit every object if
22 * this is done. Therefore we utilize the two-stage purge.
34 #include <sys/types.h>
43 #include "sysdep_decls.h"
44 #include "citserver.h"
47 #include "dynloader.h"
58 char v_roomname[ROOMNAMELEN];
65 struct PurgeList *next;
66 char name[ROOMNAMELEN]; /* use the larger of username or roomname */
70 struct VPurgeList *next;
77 struct ValidRoom *next;
83 struct ValidUser *next;
94 struct PurgeList *UserPurgeList = NULL;
95 struct PurgeList *RoomPurgeList = NULL;
96 struct ValidRoom *ValidRoomList = NULL;
97 struct ValidUser *ValidUserList = NULL;
100 struct roomref *rr = NULL;
102 extern struct CitContext *ContextList;
104 void DoPurgeMessages(struct quickroom *qrbuf, void *data) {
105 struct ExpirePolicy epbuf;
108 struct CtdlMessage *msg;
110 struct cdbdata *cdbfr;
111 long *msglist = NULL;
115 GetExpirePolicy(&epbuf, qrbuf);
117 /* If the room is set to never expire messages ... do nothing */
118 if (epbuf.expire_mode == EXPIRE_NEXTLEVEL) return;
119 if (epbuf.expire_mode == EXPIRE_MANUAL) return;
121 begin_critical_section(S_QUICKROOM);
122 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long));
125 msglist = mallok(cdbfr->len);
126 memcpy(msglist, cdbfr->ptr, cdbfr->len);
127 num_msgs = cdbfr->len / sizeof(long);
131 /* Nothing to do if there aren't any messages */
133 end_critical_section(S_QUICKROOM);
137 /* If the room is set to expire by count, do that */
138 if (epbuf.expire_mode == EXPIRE_NUMMSGS) {
139 while (num_msgs > epbuf.expire_value) {
141 lprintf(5, "Expiring message %ld\n", delnum);
142 AdjRefCount(delnum, -1);
143 memcpy(&msglist[0], &msglist[1],
144 (sizeof(long)*(num_msgs - 1)));
150 /* If the room is set to expire by age... */
151 if (epbuf.expire_mode == EXPIRE_AGE) {
152 for (a=0; a<num_msgs; ++a) {
155 msg = CtdlFetchMessage(delnum);
157 xtime = atol(msg->cm_fields['T']);
158 CtdlFreeMessage(msg);
164 && (now - xtime > (time_t)(epbuf.expire_value * 86400L))) {
165 lprintf(5, "Expiring message %ld\n", delnum);
166 AdjRefCount(delnum, -1);
174 num_msgs = sort_msglist(msglist, num_msgs);
177 cdb_store(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long),
178 msglist, (num_msgs * sizeof(long)) );
180 if (msglist != NULL) phree(msglist);
182 end_critical_section(S_QUICKROOM);
185 void PurgeMessages(void) {
186 lprintf(5, "PurgeMessages() called\n");
188 ForEachRoom(DoPurgeMessages, NULL);
192 void AddValidUser(struct usersupp *usbuf, void *data) {
193 struct ValidUser *vuptr;
195 vuptr = (struct ValidUser *)mallok(sizeof(struct ValidUser));
196 vuptr->next = ValidUserList;
197 vuptr->vu_usernum = usbuf->usernum;
198 ValidUserList = vuptr;
201 void AddValidRoom(struct quickroom *qrbuf, void *data) {
202 struct ValidRoom *vrptr;
204 vrptr = (struct ValidRoom *)mallok(sizeof(struct ValidRoom));
205 vrptr->next = ValidRoomList;
206 vrptr->vr_roomnum = qrbuf->QRnumber;
207 vrptr->vr_roomgen = qrbuf->QRgen;
208 ValidRoomList = vrptr;
211 void DoPurgeRooms(struct quickroom *qrbuf, void *data) {
212 time_t age, purge_secs;
213 struct PurgeList *pptr;
214 struct ValidUser *vuptr;
217 /* For mailbox rooms, there's only one purging rule: if the user who
218 * owns the room still exists, we keep the room; otherwise, we purge
219 * it. Bypass any other rules.
221 if (qrbuf->QRflags & QR_MAILBOX) {
222 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
223 if (vuptr->vu_usernum == atol(qrbuf->QRname)) {
233 /* Any of these attributes render a room non-purgable */
234 if (qrbuf->QRflags & QR_PERMANENT) return;
235 if (qrbuf->QRflags & QR_DIRECTORY) return;
236 if (qrbuf->QRflags & QR_NETWORK) return;
237 if (!strcasecmp(qrbuf->QRname, SYSCONFIGROOM)) return;
238 if (is_noneditable(qrbuf)) return;
240 /* If we don't know the modification date, be safe and don't purge */
241 if (qrbuf->QRmtime <= (time_t)0) return;
243 /* If no room purge time is set, be safe and don't purge */
244 if (config.c_roompurge < 0) return;
246 /* Otherwise, check the date of last modification */
247 age = time(NULL) - (qrbuf->QRmtime);
248 purge_secs = (time_t)config.c_roompurge * (time_t)86400;
249 if (purge_secs <= (time_t)0) return;
250 lprintf(9, "<%s> is <%ld> seconds old\n", qrbuf->QRname, age);
251 if (age > purge_secs) do_purge = 1;
253 BYPASS: if (do_purge) {
254 pptr = (struct PurgeList *) mallok(sizeof(struct PurgeList));
255 pptr->next = RoomPurgeList;
256 strcpy(pptr->name, qrbuf->QRname);
257 RoomPurgeList = pptr;
264 int PurgeRooms(void) {
265 struct PurgeList *pptr;
266 int num_rooms_purged = 0;
267 struct quickroom qrbuf;
268 struct ValidUser *vuptr;
269 char *transcript = NULL;
271 lprintf(5, "PurgeRooms() called\n");
274 /* Load up a table full of valid user numbers so we can delete
275 * user-owned rooms for users who no longer exist */
276 ForEachUser(AddValidUser, NULL);
278 /* Then cycle through the room file */
279 ForEachRoom(DoPurgeRooms, NULL);
281 /* Free the valid user list */
282 while (ValidUserList != NULL) {
283 vuptr = ValidUserList->next;
284 phree(ValidUserList);
285 ValidUserList = vuptr;
289 transcript = mallok(256);
290 strcpy(transcript, "The following rooms have been auto-purged:\n");
292 while (RoomPurgeList != NULL) {
293 if (getroom(&qrbuf, RoomPurgeList->name) == 0) {
294 transcript=reallok(transcript, strlen(transcript)+256);
295 sprintf(&transcript[strlen(transcript)], " %s\n",
299 pptr = RoomPurgeList->next;
300 phree(RoomPurgeList);
301 RoomPurgeList = pptr;
305 if (num_rooms_purged > 0) aide_message(transcript);
308 lprintf(5, "Purged %d rooms.\n", num_rooms_purged);
309 return(num_rooms_purged);
313 void do_user_purge(struct usersupp *us, void *data) {
317 struct PurgeList *pptr;
319 /* Set purge time; if the user overrides the system default, use it */
320 if (us->USuserpurge > 0) {
321 purge_time = ((time_t)us->USuserpurge) * 86400L;
324 purge_time = ((time_t)config.c_userpurge) * 86400L;
327 /* The default rule is to not purge. */
330 /* If the user hasn't called in two months, his/her account
331 * has expired, so purge the record.
334 if ((now - us->lastcall) > purge_time) purge = 1;
336 /* If the user set his/her password to 'deleteme', he/she
337 * wishes to be deleted, so purge the record.
339 if (!strcasecmp(us->password, "deleteme")) purge = 1;
341 /* If the record is marked as permanent, don't purge it.
343 if (us->flags & US_PERM) purge = 0;
345 /* If the access level is 0, the record should already have been
346 * deleted, but maybe the user was logged in at the time or something.
347 * Delete the record now.
349 if (us->axlevel == 0) purge = 1;
351 /* 0 calls is impossible. If there are 0 calls, it must
352 * be a corrupted record, so purge it.
354 if (us->timescalled == 0) purge = 1;
357 pptr = (struct PurgeList *) mallok(sizeof(struct PurgeList));
358 pptr->next = UserPurgeList;
359 strcpy(pptr->name, us->fullname);
360 UserPurgeList = pptr;
367 int PurgeUsers(void) {
368 struct PurgeList *pptr;
369 int num_users_purged = 0;
370 char *transcript = NULL;
372 lprintf(5, "PurgeUsers() called\n");
373 if (config.c_userpurge > 0) {
374 ForEachUser(do_user_purge, NULL);
377 transcript = mallok(256);
378 strcpy(transcript, "The following users have been auto-purged:\n");
380 while (UserPurgeList != NULL) {
381 transcript=reallok(transcript, strlen(transcript)+256);
382 sprintf(&transcript[strlen(transcript)], " %s\n",
383 UserPurgeList->name);
384 purge_user(UserPurgeList->name);
385 pptr = UserPurgeList->next;
386 phree(UserPurgeList);
387 UserPurgeList = pptr;
391 if (num_users_purged > 0) aide_message(transcript);
394 lprintf(5, "Purged %d users.\n", num_users_purged);
395 return(num_users_purged);
402 * This is a really cumbersome "garbage collection" function. We have to
403 * delete visits which refer to rooms and/or users which no longer exist. In
404 * order to prevent endless traversals of the room and user files, we first
405 * build linked lists of rooms and users which _do_ exist on the system, then
406 * traverse the visit file, checking each record against those two lists and
407 * purging the ones that do not have a match on _both_ lists. (Remember, if
408 * either the room or user being referred to is no longer on the system, the
409 * record is completely useless.)
411 int PurgeVisits(void) {
412 struct cdbdata *cdbvisit;
414 struct VPurgeList *VisitPurgeList = NULL;
415 struct VPurgeList *vptr;
419 struct ValidRoom *vrptr;
420 struct ValidUser *vuptr;
421 int RoomIsValid, UserIsValid;
423 /* First, load up a table full of valid room/gen combinations */
424 ForEachRoom(AddValidRoom, NULL);
426 /* Then load up a table full of valid user numbers */
427 ForEachUser(AddValidUser, NULL);
429 /* Now traverse through the visits, purging irrelevant records... */
430 cdb_rewind(CDB_VISIT);
431 while(cdbvisit = cdb_next_item(CDB_VISIT), cdbvisit != NULL) {
432 memset(&vbuf, 0, sizeof(struct visit));
433 memcpy(&vbuf, cdbvisit->ptr,
434 ( (cdbvisit->len > sizeof(struct visit)) ?
435 sizeof(struct visit) : cdbvisit->len) );
441 /* Check to see if the room exists */
442 for (vrptr=ValidRoomList; vrptr!=NULL; vrptr=vrptr->next) {
443 if ( (vrptr->vr_roomnum==vbuf.v_roomnum)
444 && (vrptr->vr_roomgen==vbuf.v_roomgen))
448 /* Check to see if the user exists */
449 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
450 if (vuptr->vu_usernum == vbuf.v_usernum)
454 /* Put the record on the purge list if it's dead */
455 if ((RoomIsValid==0) || (UserIsValid==0)) {
456 vptr = (struct VPurgeList *)
457 mallok(sizeof(struct VPurgeList));
458 vptr->next = VisitPurgeList;
459 vptr->vp_roomnum = vbuf.v_roomnum;
460 vptr->vp_roomgen = vbuf.v_roomgen;
461 vptr->vp_usernum = vbuf.v_usernum;
462 VisitPurgeList = vptr;
467 /* Free the valid room/gen combination list */
468 while (ValidRoomList != NULL) {
469 vrptr = ValidRoomList->next;
470 phree(ValidRoomList);
471 ValidRoomList = vrptr;
474 /* Free the valid user list */
475 while (ValidUserList != NULL) {
476 vuptr = ValidUserList->next;
477 phree(ValidUserList);
478 ValidUserList = vuptr;
481 /* Now delete every visit on the purged list */
482 while (VisitPurgeList != NULL) {
483 IndexLen = GenerateRelationshipIndex(IndexBuf,
484 VisitPurgeList->vp_roomnum,
485 VisitPurgeList->vp_roomgen,
486 VisitPurgeList->vp_usernum);
487 cdb_delete(CDB_VISIT, IndexBuf, IndexLen);
488 vptr = VisitPurgeList->next;
489 phree(VisitPurgeList);
490 VisitPurgeList = vptr;
498 void cmd_expi(char *argbuf) {
502 if (CtdlAccessCheck(ac_aide)) return;
504 extract(cmd, argbuf, 0);
505 if (!strcasecmp(cmd, "users")) {
506 retval = PurgeUsers();
507 cprintf("%d Purged %d users.\n", OK, retval);
510 else if (!strcasecmp(cmd, "messages")) {
512 cprintf("%d Expired %d messages.\n", OK, messages_purged);
515 else if (!strcasecmp(cmd, "rooms")) {
516 retval = PurgeRooms();
517 cprintf("%d Expired %d rooms.\n", OK, retval);
520 else if (!strcasecmp(cmd, "visits")) {
521 retval = PurgeVisits();
522 cprintf("%d Purged %d visits.\n", OK, retval);
524 else if (!strcasecmp(cmd, "defrag")) {
526 cprintf("%d Defragmented the databases.\n", OK);
529 cprintf("%d Invalid command.\n", ERROR+ILLEGAL_VALUE);
534 /*****************************************************************************/
537 void do_fsck_msg(long msgnum) {
540 ptr = (struct roomref *)mallok(sizeof(struct roomref));
542 ptr->msgnum = msgnum;
546 void do_fsck_room(struct quickroom *qrbuf, void *data)
548 getroom(&CC->quickroom, qrbuf->QRname);
549 CtdlForEachMessage(MSGS_ALL, 0L, (-127), NULL, NULL, do_fsck_msg);
553 * Check message reference counts
555 void cmd_fsck(char *argbuf) {
557 struct cdbdata *cdbmsg;
558 struct SuppMsgInfo smi;
562 if (CtdlAccessCheck(ac_aide)) return;
564 /* Lame way of checking whether anyone else is doing this now */
566 cprintf("%d Another FSCK is already running.\n", ERROR);
570 cprintf("%d Checking message reference counts\n", LISTING_FOLLOWS);
572 cprintf("\nThis could take a while. Please be patient!\n\n");
573 cprintf("Gathering pointers...\n");
574 ForEachRoom(do_fsck_room, NULL);
577 cprintf("Checking message base...\n");
578 for (msgnum = 0L; msgnum <= CitControl.MMhighest; ++msgnum) {
580 cdbmsg = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
581 if (cdbmsg != NULL) {
583 cprintf("Message %7ld ", msgnum);
585 GetSuppMsgInfo(&smi, msgnum);
586 cprintf("refcount=%-2d ", smi.smi_refcount);
589 for (ptr = rr; ptr != NULL; ptr = ptr->next) {
590 if (ptr->msgnum == msgnum) ++realcount;
592 cprintf("realcount=%-2d\n", realcount);
594 if ( (smi.smi_refcount != realcount)
595 || (realcount == 0) ) {
596 smi.smi_refcount = realcount;
597 PutSuppMsgInfo(&smi);
598 AdjRefCount(msgnum, 0); /* deletes if needed */
605 cprintf("Freeing memory...\n");
620 /*****************************************************************************/
622 char *Dynamic_Module_Init(void)
624 CtdlRegisterProtoHook(cmd_expi, "EXPI", "Expire old system objects");
625 CtdlRegisterProtoHook(cmd_fsck, "FSCK", "Check message ref counts");