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.
33 #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;
87 struct PurgeList *UserPurgeList = NULL;
88 struct PurgeList *RoomPurgeList = NULL;
89 struct ValidRoom *ValidRoomList = NULL;
90 struct ValidUser *ValidUserList = NULL;
93 extern struct CitContext *ContextList;
95 #define MODULE_NAME "Expire old messages, users, rooms"
96 #define MODULE_AUTHOR "Art Cancro"
97 #define MODULE_EMAIL "ajc@uncnsrd.mt-kisco.ny.us"
98 #define MAJOR_VERSION 0
99 #define MINOR_VERSION 1
101 static struct DLModule_Info info = {
109 void DoPurgeMessages(struct quickroom *qrbuf) {
110 struct ExpirePolicy epbuf;
118 GetExpirePolicy(&epbuf, qrbuf);
121 lprintf(9, "ExpirePolicy for <%s> is <%d> <%d>\n",
122 qrbuf->QRname, epbuf.expire_mode, epbuf.expire_value);
125 /* If the room is set to never expire messages ... do nothing */
126 if (epbuf.expire_mode == EXPIRE_NEXTLEVEL) return;
127 if (epbuf.expire_mode == EXPIRE_MANUAL) return;
129 begin_critical_section(S_QUICKROOM);
132 /* Nothing to do if there aren't any messages */
133 if (CC->num_msgs == 0) {
134 end_critical_section(S_QUICKROOM);
138 /* If the room is set to expire by count, do that */
139 if (epbuf.expire_mode == EXPIRE_NUMMSGS) {
140 while (CC->num_msgs > epbuf.expire_value) {
141 delnum = MessageFromList(0);
142 lprintf(5, "Expiring message %ld\n", delnum);
143 cdb_delete(CDB_MSGMAIN, &delnum, sizeof(long));
144 memcpy(&CC->msglist[0], &CC->msglist[1],
145 (sizeof(long)*(CC->num_msgs - 1)));
146 CC->num_msgs = CC->num_msgs - 1;
151 /* If the room is set to expire by age... */
152 if (epbuf.expire_mode == EXPIRE_AGE) {
153 for (a=0; a<(CC->num_msgs); ++a) {
154 delnum = MessageFromList(a);
155 sprintf(msgid, "%ld", delnum);
156 xtime = output_message(msgid, MT_DATE, 0, 0);
159 && (now - xtime > (time_t)(epbuf.expire_value * 86400L))) {
160 cdb_delete(CDB_MSGMAIN, &delnum, sizeof(long));
161 SetMessageInList(a, 0L);
162 lprintf(5, "Expiring message %ld\n", delnum);
166 CC->num_msgs = sort_msglist(CC->msglist, CC->num_msgs);
168 end_critical_section(S_QUICKROOM);
171 void PurgeMessages(void) {
172 lprintf(5, "PurgeMessages() called\n");
173 ForEachRoom(DoPurgeMessages);
177 void DoPurgeRooms(struct quickroom *qrbuf) {
178 time_t age, purge_secs;
179 struct PurgeList *pptr;
181 /* Any of these attributes render a room non-purgable */
182 if (qrbuf->QRflags & QR_PERMANENT) return;
183 if (qrbuf->QRflags & QR_DIRECTORY) return;
184 if (qrbuf->QRflags & QR_NETWORK) return;
185 if (qrbuf->QRflags & QR_MAILBOX) return;
186 if (is_noneditable(qrbuf)) return;
188 /* If we don't know the modification date, be safe and don't purge */
189 if (qrbuf->QRmtime <= (time_t)0) return;
191 /* Otherwise, check the date of last modification */
192 age = time(NULL) - (qrbuf->QRmtime);
193 purge_secs = (time_t)config.c_roompurge * (time_t)86400;
194 if (purge_secs <= (time_t)0) return;
195 lprintf(9, "<%s> is <%ld> seconds old\n", qrbuf->QRname, age);
197 if (age > purge_secs) {
199 pptr = (struct PurgeList *) malloc(sizeof(struct PurgeList));
200 pptr->next = RoomPurgeList;
201 strcpy(pptr->name, qrbuf->QRname);
202 RoomPurgeList = pptr;
208 int PurgeRooms(void) {
209 struct PurgeList *pptr;
210 int num_rooms_purged = 0;
211 struct quickroom qrbuf;
213 lprintf(5, "PurgeRooms() called\n");
214 if (config.c_roompurge > 0) {
215 ForEachRoom(DoPurgeRooms);
218 while (RoomPurgeList != NULL) {
219 if (getroom(&qrbuf, RoomPurgeList->name) == 0) {
222 pptr = RoomPurgeList->next;
224 RoomPurgeList = pptr;
228 lprintf(5, "Purged %d rooms.\n", num_rooms_purged);
229 return(num_rooms_purged);
233 void do_user_purge(struct usersupp *us) {
237 struct PurgeList *pptr;
239 /* Set purge time; if the user overrides the system default, use it */
240 if (us->USuserpurge > 0) {
241 purge_time = ((time_t)us->USuserpurge) * 86400L;
244 purge_time = ((time_t)config.c_userpurge) * 86400L;
247 /* The default rule is to not purge. */
250 /* If the user hasn't called in two months, his/her account
251 * has expired, so purge the record.
254 if ((now - us->lastcall) > purge_time) purge = 1;
256 /* If the user set his/her password to 'deleteme', he/she
257 * wishes to be deleted, so purge the record.
259 if (!strcasecmp(us->password, "deleteme")) purge = 1;
261 /* If the record is marked as permanent, don't purge it.
263 if (us->flags & US_PERM) purge = 0;
265 /* If the access level is 0, the record should already have been
266 * deleted, but maybe the user was logged in at the time or something.
267 * Delete the record now.
269 if (us->axlevel == 0) purge = 1;
271 /* 0 calls is impossible. If there are 0 calls, it must
272 * be a corrupted record, so purge it.
274 if (us->timescalled == 0) purge = 1;
277 pptr = (struct PurgeList *) malloc(sizeof(struct PurgeList));
278 pptr->next = UserPurgeList;
279 strcpy(pptr->name, us->fullname);
280 UserPurgeList = pptr;
287 int PurgeUsers(void) {
288 struct PurgeList *pptr;
289 int num_users_purged = 0;
291 lprintf(5, "PurgeUsers() called\n");
292 if (config.c_userpurge > 0) {
293 ForEachUser(do_user_purge);
296 while (UserPurgeList != NULL) {
297 purge_user(UserPurgeList->name);
298 pptr = UserPurgeList->next;
300 UserPurgeList = pptr;
304 lprintf(5, "Purged %d users.\n", num_users_purged);
305 return(num_users_purged);
308 void AddValidUser(struct usersupp *usbuf) {
309 struct ValidUser *vuptr;
311 vuptr = (struct ValidUser *)malloc(sizeof(struct ValidUser));
312 vuptr->next = ValidUserList;
313 vuptr->vu_usernum = usbuf->usernum;
314 ValidUserList = vuptr;
317 void AddValidRoom(struct quickroom *qrbuf) {
318 struct ValidRoom *vrptr;
320 vrptr = (struct ValidRoom *)malloc(sizeof(struct ValidRoom));
321 vrptr->next = ValidRoomList;
322 vrptr->vr_roomnum = qrbuf->QRnumber;
323 vrptr->vr_roomgen = qrbuf->QRgen;
324 ValidRoomList = vrptr;
331 * This is a really cumbersome "garbage collection" function. We have to
332 * delete visits which refer to rooms and/or users which no longer exist. In
333 * order to prevent endless traversals of the room and user files, we first
334 * build linked lists of rooms and users which _do_ exist on the system, then
335 * traverse the visit file, checking each record against those two lists and
336 * purging the ones that do not have a match on _both_ lists. (Remember, if
337 * either the room or user being referred to is no longer on the system, the
338 * record is completely useless.)
340 int PurgeVisits(void) {
341 struct cdbdata *cdbvisit;
343 struct VPurgeList *VisitPurgeList = NULL;
344 struct VPurgeList *vptr;
348 struct ValidRoom *vrptr;
349 struct ValidUser *vuptr;
350 int RoomIsValid, UserIsValid;
352 /* First, load up a table full of valid room/gen combinations */
353 ForEachRoom(AddValidRoom);
355 /* Then load up a table full of valid user numbers */
356 ForEachUser(AddValidUser);
358 /* Now traverse through the visits, purging irrelevant records... */
359 cdb_rewind(CDB_VISIT);
360 while(cdbvisit = cdb_next_item(CDB_VISIT), cdbvisit != NULL) {
361 memset(&vbuf, 0, sizeof(struct visit));
362 memcpy(&vbuf, cdbvisit->ptr,
363 ( (cdbvisit->len > sizeof(struct visit)) ?
364 sizeof(struct visit) : cdbvisit->len) );
370 /* Check to see if the room exists */
371 for (vrptr=ValidRoomList; vrptr!=NULL; vrptr=vrptr->next) {
372 if ( (vrptr->vr_roomnum==vbuf.v_roomnum)
373 && (vrptr->vr_roomgen==vbuf.v_roomgen))
377 /* Check to see if the user exists */
378 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
379 if (vuptr->vu_usernum == vbuf.v_usernum)
383 /* Put the record on the purge list if it's dead */
384 if ((RoomIsValid==0) || (UserIsValid==0)) {
385 vptr = (struct VPurgeList *)
386 malloc(sizeof(struct VPurgeList));
387 vptr->next = VisitPurgeList;
388 vptr->vp_roomnum = vbuf.v_roomnum;
389 vptr->vp_roomgen = vbuf.v_roomgen;
390 vptr->vp_usernum = vbuf.v_usernum;
391 VisitPurgeList = vptr;
396 /* Free the valid room/gen combination list */
397 while (ValidRoomList != NULL) {
398 vrptr = ValidRoomList->next;
400 ValidRoomList = vrptr;
403 /* Free the valid user list */
404 while (ValidUserList != NULL) {
405 vuptr = ValidUserList->next;
407 ValidUserList = vuptr;
410 /* Now delete every visit on the purged list */
411 while (VisitPurgeList != NULL) {
412 IndexLen = GenerateRelationshipIndex(IndexBuf,
413 VisitPurgeList->vp_roomnum,
414 VisitPurgeList->vp_roomgen,
415 VisitPurgeList->vp_usernum);
416 cdb_delete(CDB_VISIT, IndexBuf, IndexLen);
417 vptr = VisitPurgeList->next;
418 free(VisitPurgeList);
419 VisitPurgeList = vptr;
427 void cmd_expi(char *argbuf) {
432 if ((!(CC->logged_in))&&(!(CC->internal_pgm))) {
433 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
437 if ((!is_room_aide()) && (!(CC->internal_pgm)) ) {
438 cprintf("%d Higher access required.\n",
439 ERROR+HIGHER_ACCESS_REQUIRED);
443 extract(cmd, argbuf, 0);
444 if (!strcasecmp(cmd, "users")) {
445 retval = PurgeUsers();
446 cprintf("%d Purged %d users.\n", OK, retval);
449 else if (!strcasecmp(cmd, "messages")) {
451 cprintf("%d Expired %d messages.\n", OK, messages_purged);
454 else if (!strcasecmp(cmd, "rooms")) {
455 retval = PurgeRooms();
456 cprintf("%d Expired %d rooms.\n", OK, retval);
459 else if (!strcasecmp(cmd, "visits")) {
460 retval = PurgeVisits();
461 cprintf("%d Purged %d visits.\n", OK, retval);
463 else if (!strcasecmp(cmd, "defrag")) {
465 cprintf("%d Defragmented the databases.\n", OK);
468 cprintf("%d Invalid command.\n", ERROR+ILLEGAL_VALUE);
475 struct DLModule_Info *Dynamic_Module_Init(void)
477 CtdlRegisterProtoHook(cmd_expi, "EXPI", "Expire old system objects");