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"
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;
86 struct PurgeList *UserPurgeList = NULL;
87 struct PurgeList *RoomPurgeList = NULL;
88 struct ValidRoom *ValidRoomList = NULL;
89 struct ValidUser *ValidUserList = NULL;
92 extern struct CitContext *ContextList;
94 #define MODULE_NAME "Expire old messages, users, rooms"
95 #define MODULE_AUTHOR "Art Cancro"
96 #define MODULE_EMAIL "ajc@uncnsrd.mt-kisco.ny.us"
97 #define MAJOR_VERSION 0
98 #define MINOR_VERSION 1
100 static struct DLModule_Info info = {
108 void DoPurgeMessages(struct quickroom *qrbuf) {
109 struct ExpirePolicy epbuf;
117 GetExpirePolicy(&epbuf, qrbuf);
120 lprintf(9, "ExpirePolicy for <%s> is <%d> <%d>\n",
121 qrbuf->QRname, epbuf.expire_mode, epbuf.expire_value);
124 /* If the room is set to never expire messages ... do nothing */
125 if (epbuf.expire_mode == EXPIRE_NEXTLEVEL) return;
126 if (epbuf.expire_mode == EXPIRE_MANUAL) return;
128 begin_critical_section(S_QUICKROOM);
131 /* Nothing to do if there aren't any messages */
132 if (CC->num_msgs == 0) {
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 (CC->num_msgs > epbuf.expire_value) {
140 delnum = MessageFromList(0);
141 lprintf(5, "Expiring message %ld\n", delnum);
142 cdb_delete(CDB_MSGMAIN, &delnum, sizeof(long));
143 memcpy(&CC->msglist[0], &CC->msglist[1],
144 (sizeof(long)*(CC->num_msgs - 1)));
145 CC->num_msgs = CC->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<(CC->num_msgs); ++a) {
153 delnum = MessageFromList(a);
154 sprintf(msgid, "%ld", delnum);
155 xtime = output_message(msgid, MT_DATE, 0, 0);
158 && (now - xtime > (time_t)(epbuf.expire_value * 86400L))) {
159 cdb_delete(CDB_MSGMAIN, &delnum, sizeof(long));
160 SetMessageInList(a, 0L);
161 lprintf(5, "Expiring message %ld\n", delnum);
165 CC->num_msgs = sort_msglist(CC->msglist, CC->num_msgs);
167 end_critical_section(S_QUICKROOM);
170 void PurgeMessages(void) {
171 lprintf(5, "PurgeMessages() called\n");
172 ForEachRoom(DoPurgeMessages);
176 void DoPurgeRooms(struct quickroom *qrbuf) {
178 struct PurgeList *pptr;
180 /* Any of these attributes render a room non-purgable */
181 if (qrbuf->QRflags & QR_PERMANENT) return;
182 if (qrbuf->QRflags & QR_DIRECTORY) return;
183 if (qrbuf->QRflags & QR_NETWORK) return;
184 if (qrbuf->QRflags & QR_MAILBOX) return;
185 if (is_noneditable(qrbuf)) return;
187 /* Otherwise, check the date of last modification */
189 age = now - (qrbuf->QRmtime);
190 lprintf(9, "<%s> is <%ld> seconds old\n", qrbuf->QRname, age);
191 if ( (qrbuf->QRmtime > 0L)
192 && (age > (time_t)(config.c_roompurge * 86400L))) {
194 pptr = (struct PurgeList *) malloc(sizeof(struct PurgeList));
195 pptr->next = RoomPurgeList;
196 strcpy(pptr->name, qrbuf->QRname);
197 RoomPurgeList = pptr;
203 int PurgeRooms(void) {
204 struct PurgeList *pptr;
205 int num_rooms_purged = 0;
206 struct quickroom qrbuf;
208 lprintf(5, "PurgeRooms() called\n");
209 if (config.c_roompurge > 0) {
210 ForEachRoom(DoPurgeRooms);
213 while (RoomPurgeList != NULL) {
214 if (getroom(&qrbuf, RoomPurgeList->name) == 0) {
217 pptr = RoomPurgeList->next;
219 RoomPurgeList = pptr;
223 lprintf(5, "Purged %d rooms.\n", num_rooms_purged);
224 return(num_rooms_purged);
228 void do_user_purge(struct usersupp *us) {
232 struct PurgeList *pptr;
234 /* Set purge time; if the user overrides the system default, use it */
235 if (us->USuserpurge > 0) {
236 purge_time = ((time_t)us->USuserpurge) * 86400L;
239 purge_time = ((time_t)config.c_userpurge) * 86400L;
242 /* The default rule is to not purge. */
245 /* If the user hasn't called in two months, his/her account
246 * has expired, so purge the record.
249 if ((now - us->lastcall) > purge_time) purge = 1;
251 /* If the user set his/her password to 'deleteme', he/she
252 * wishes to be deleted, so purge the record.
254 if (!strcasecmp(us->password, "deleteme")) purge = 1;
256 /* If the record is marked as permanent, don't purge it.
258 if (us->flags & US_PERM) purge = 0;
260 /* If the access level is 0, the record should already have been
261 * deleted, but maybe the user was logged in at the time or something.
262 * Delete the record now.
264 if (us->axlevel == 0) purge = 1;
266 /* 0 calls is impossible. If there are 0 calls, it must
267 * be a corrupted record, so purge it.
269 if (us->timescalled == 0) purge = 1;
272 pptr = (struct PurgeList *) malloc(sizeof(struct PurgeList));
273 pptr->next = UserPurgeList;
274 strcpy(pptr->name, us->fullname);
275 UserPurgeList = pptr;
282 int PurgeUsers(void) {
283 struct PurgeList *pptr;
284 int num_users_purged = 0;
286 lprintf(5, "PurgeUsers() called\n");
287 if (config.c_userpurge > 0) {
288 ForEachUser(do_user_purge);
291 while (UserPurgeList != NULL) {
292 purge_user(UserPurgeList->name);
293 pptr = UserPurgeList->next;
295 UserPurgeList = pptr;
299 lprintf(5, "Purged %d users.\n", num_users_purged);
300 return(num_users_purged);
303 void AddValidUser(struct usersupp *usbuf) {
304 struct ValidUser *vuptr;
306 vuptr = (struct ValidUser *)malloc(sizeof(struct ValidUser));
307 vuptr->next = ValidUserList;
308 vuptr->vu_usernum = usbuf->usernum;
309 ValidUserList = vuptr;
312 void AddValidRoom(struct quickroom *qrbuf) {
313 struct ValidRoom *vrptr;
315 vrptr = (struct ValidRoom *)malloc(sizeof(struct ValidRoom));
316 vrptr->next = ValidRoomList;
317 vrptr->vr_roomnum = qrbuf->QRnumber;
318 vrptr->vr_roomgen = qrbuf->QRgen;
319 ValidRoomList = vrptr;
326 * This is a really cumbersome "garbage collection" function. We have to
327 * delete visits which refer to rooms and/or users which no longer exist. In
328 * order to prevent endless traversals of the room and user files, we first
329 * build linked lists of rooms and users which _do_ exist on the system, then
330 * traverse the visit file, checking each record against those two lists and
331 * purging the ones that do not have a match on _both_ lists. (Remember, if
332 * either the room or user being referred to is no longer on the system, the
333 * record is completely useless.)
335 int PurgeVisits(void) {
336 struct cdbdata *cdbvisit;
338 struct VPurgeList *VisitPurgeList = NULL;
339 struct VPurgeList *vptr;
343 struct ValidRoom *vrptr;
344 struct ValidUser *vuptr;
345 int RoomIsValid, UserIsValid;
347 /* First, load up a table full of valid room/gen combinations */
348 ForEachRoom(AddValidRoom);
350 /* Then load up a table full of valid user numbers */
351 ForEachUser(AddValidUser);
353 /* Now traverse through the visits, purging irrelevant records... */
354 cdb_rewind(CDB_VISIT);
355 while(cdbvisit = cdb_next_item(CDB_VISIT), cdbvisit != NULL) {
356 memset(&vbuf, 0, sizeof(struct visit));
357 memcpy(&vbuf, cdbvisit->ptr,
358 ( (cdbvisit->len > sizeof(struct visit)) ?
359 sizeof(struct visit) : cdbvisit->len) );
365 /* Check to see if the room exists */
366 for (vrptr=ValidRoomList; vrptr!=NULL; vrptr=vrptr->next) {
367 if ( (vrptr->vr_roomnum==vbuf.v_roomnum)
368 && (vrptr->vr_roomgen==vbuf.v_roomgen))
372 /* Check to see if the user exists */
373 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
374 if (vuptr->vu_usernum == vbuf.v_usernum)
378 /* Put the record on the purge list if it's dead */
379 if ((RoomIsValid==0) || (UserIsValid==0)) {
380 vptr = (struct VPurgeList *)
381 malloc(sizeof(struct VPurgeList));
382 vptr->next = VisitPurgeList;
383 vptr->vp_roomnum = vbuf.v_roomnum;
384 vptr->vp_roomgen = vbuf.v_roomgen;
385 vptr->vp_usernum = vbuf.v_usernum;
386 VisitPurgeList = vptr;
391 /* Free the valid room/gen combination list */
392 while (ValidRoomList != NULL) {
393 vrptr = ValidRoomList->next;
395 ValidRoomList = vrptr;
398 /* Free the valid user list */
399 while (ValidUserList != NULL) {
400 vuptr = ValidUserList->next;
402 ValidUserList = vuptr;
405 /* Now delete every visit on the purged list */
406 while (VisitPurgeList != NULL) {
407 IndexLen = GenerateRelationshipIndex(IndexBuf,
408 VisitPurgeList->vp_roomnum,
409 VisitPurgeList->vp_roomgen,
410 VisitPurgeList->vp_usernum);
411 cdb_delete(CDB_VISIT, IndexBuf, IndexLen);
412 vptr = VisitPurgeList->next;
413 free(VisitPurgeList);
414 VisitPurgeList = vptr;
422 void cmd_expi(char *argbuf) {
427 if ((!(CC->logged_in))&&(!(CC->internal_pgm))) {
428 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
432 if ((!is_room_aide()) && (!(CC->internal_pgm)) ) {
433 cprintf("%d Higher access required.\n",
434 ERROR+HIGHER_ACCESS_REQUIRED);
438 extract(cmd, argbuf, 0);
439 if (!strcasecmp(cmd, "users")) {
440 retval = PurgeUsers();
441 cprintf("%d Purged %d users.\n", OK, retval);
444 else if (!strcasecmp(cmd, "messages")) {
446 cprintf("%d Expired %d messages.\n", OK, messages_purged);
449 else if (!strcasecmp(cmd, "rooms")) {
450 retval = PurgeRooms();
451 cprintf("%d Expired %d rooms.\n", OK, retval);
454 else if (!strcasecmp(cmd, "visits")) {
455 retval = PurgeVisits();
456 cprintf("%d Purged %d visits.\n", OK, retval);
458 else if (!strcasecmp(cmd, "defrag")) {
460 cprintf("%d Defragmented the databases.\n", OK);
463 cprintf("%d Invalid command.\n", ERROR+ILLEGAL_VALUE);
470 struct DLModule_Info *Dynamic_Module_Init(void)
472 CtdlRegisterProtoHook(cmd_expi, "EXPI", "Expire old system objects");