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>
46 #include "sysdep_decls.h"
47 #include "citserver.h"
50 #include "dynloader.h"
61 char v_roomname[ROOMNAMELEN];
68 struct PurgeList *next;
69 char name[ROOMNAMELEN]; /* use the larger of username or roomname */
73 struct VPurgeList *next;
80 struct ValidRoom *next;
86 struct ValidUser *next;
90 struct PurgeList *UserPurgeList = NULL;
91 struct PurgeList *RoomPurgeList = NULL;
92 struct ValidRoom *ValidRoomList = NULL;
93 struct ValidUser *ValidUserList = NULL;
96 extern struct CitContext *ContextList;
98 #define MODULE_NAME "Expire old messages, users, rooms"
99 #define MODULE_AUTHOR "Art Cancro"
100 #define MODULE_EMAIL "ajc@uncnsrd.mt-kisco.ny.us"
101 #define MAJOR_VERSION 1
102 #define MINOR_VERSION 0
104 static struct DLModule_Info info = {
112 void DoPurgeMessages(struct quickroom *qrbuf) {
113 struct ExpirePolicy epbuf;
120 GetExpirePolicy(&epbuf, qrbuf);
123 lprintf(9, "ExpirePolicy for <%s> is <%d> <%d>\n",
124 qrbuf->QRname, epbuf.expire_mode, epbuf.expire_value);
127 /* If the room is set to never expire messages ... do nothing */
128 if (epbuf.expire_mode == EXPIRE_NEXTLEVEL) return;
129 if (epbuf.expire_mode == EXPIRE_MANUAL) return;
131 begin_critical_section(S_QUICKROOM);
134 /* Nothing to do if there aren't any messages */
135 if (CC->num_msgs == 0) {
136 end_critical_section(S_QUICKROOM);
140 /* If the room is set to expire by count, do that */
141 if (epbuf.expire_mode == EXPIRE_NUMMSGS) {
142 while (CC->num_msgs > epbuf.expire_value) {
143 delnum = MessageFromList(0);
144 lprintf(5, "Expiring message %ld\n", delnum);
145 cdb_delete(CDB_MSGMAIN, &delnum, sizeof(long));
146 memcpy(&CC->msglist[0], &CC->msglist[1],
147 (sizeof(long)*(CC->num_msgs - 1)));
148 CC->num_msgs = CC->num_msgs - 1;
153 /* If the room is set to expire by age... */
154 if (epbuf.expire_mode == EXPIRE_AGE) {
155 for (a=0; a<(CC->num_msgs); ++a) {
156 delnum = MessageFromList(a);
157 sprintf(msgid, "%ld", delnum);
158 xtime = output_message(msgid, MT_DATE, 0);
161 && (now - xtime > (time_t)(epbuf.expire_value * 86400L))) {
162 lprintf(5, "Expiring message %ld\n", delnum);
163 cdb_delete(CDB_MSGMAIN, &delnum, sizeof(long));
164 SetMessageInList(a, 0L);
169 CC->num_msgs = sort_msglist(CC->msglist, CC->num_msgs);
171 end_critical_section(S_QUICKROOM);
174 void PurgeMessages(void) {
175 lprintf(5, "PurgeMessages() called\n");
177 ForEachRoom(DoPurgeMessages);
181 void DoPurgeRooms(struct quickroom *qrbuf) {
182 time_t age, purge_secs;
183 struct PurgeList *pptr;
185 /* Any of these attributes render a room non-purgable */
186 if (qrbuf->QRflags & QR_PERMANENT) return;
187 if (qrbuf->QRflags & QR_DIRECTORY) return;
188 if (qrbuf->QRflags & QR_NETWORK) return;
189 if (qrbuf->QRflags & QR_MAILBOX) return;
190 if (is_noneditable(qrbuf)) return;
192 /* If we don't know the modification date, be safe and don't purge */
193 if (qrbuf->QRmtime <= (time_t)0) return;
195 /* Otherwise, check the date of last modification */
196 age = time(NULL) - (qrbuf->QRmtime);
197 purge_secs = (time_t)config.c_roompurge * (time_t)86400;
198 if (purge_secs <= (time_t)0) return;
199 lprintf(9, "<%s> is <%ld> seconds old\n", qrbuf->QRname, age);
201 if (age > purge_secs) {
203 pptr = (struct PurgeList *) mallok(sizeof(struct PurgeList));
204 pptr->next = RoomPurgeList;
205 strcpy(pptr->name, qrbuf->QRname);
206 RoomPurgeList = pptr;
212 int PurgeRooms(void) {
213 struct PurgeList *pptr;
214 int num_rooms_purged = 0;
215 struct quickroom qrbuf;
217 lprintf(5, "PurgeRooms() called\n");
218 if (config.c_roompurge > 0) {
219 ForEachRoom(DoPurgeRooms);
222 while (RoomPurgeList != NULL) {
223 if (getroom(&qrbuf, RoomPurgeList->name) == 0) {
226 pptr = RoomPurgeList->next;
227 phree(RoomPurgeList);
228 RoomPurgeList = pptr;
232 lprintf(5, "Purged %d rooms.\n", num_rooms_purged);
233 return(num_rooms_purged);
237 void do_user_purge(struct usersupp *us) {
241 struct PurgeList *pptr;
243 /* Set purge time; if the user overrides the system default, use it */
244 if (us->USuserpurge > 0) {
245 purge_time = ((time_t)us->USuserpurge) * 86400L;
248 purge_time = ((time_t)config.c_userpurge) * 86400L;
251 /* The default rule is to not purge. */
254 /* If the user hasn't called in two months, his/her account
255 * has expired, so purge the record.
258 if ((now - us->lastcall) > purge_time) purge = 1;
260 /* If the user set his/her password to 'deleteme', he/she
261 * wishes to be deleted, so purge the record.
263 if (!strcasecmp(us->password, "deleteme")) purge = 1;
265 /* If the record is marked as permanent, don't purge it.
267 if (us->flags & US_PERM) purge = 0;
269 /* If the access level is 0, the record should already have been
270 * deleted, but maybe the user was logged in at the time or something.
271 * Delete the record now.
273 if (us->axlevel == 0) purge = 1;
275 /* 0 calls is impossible. If there are 0 calls, it must
276 * be a corrupted record, so purge it.
278 if (us->timescalled == 0) purge = 1;
281 pptr = (struct PurgeList *) mallok(sizeof(struct PurgeList));
282 pptr->next = UserPurgeList;
283 strcpy(pptr->name, us->fullname);
284 UserPurgeList = pptr;
291 int PurgeUsers(void) {
292 struct PurgeList *pptr;
293 int num_users_purged = 0;
295 lprintf(5, "PurgeUsers() called\n");
296 if (config.c_userpurge > 0) {
297 ForEachUser(do_user_purge);
300 while (UserPurgeList != NULL) {
301 purge_user(UserPurgeList->name);
302 pptr = UserPurgeList->next;
303 phree(UserPurgeList);
304 UserPurgeList = pptr;
308 lprintf(5, "Purged %d users.\n", num_users_purged);
309 return(num_users_purged);
312 void AddValidUser(struct usersupp *usbuf) {
313 struct ValidUser *vuptr;
315 vuptr = (struct ValidUser *)mallok(sizeof(struct ValidUser));
316 vuptr->next = ValidUserList;
317 vuptr->vu_usernum = usbuf->usernum;
318 ValidUserList = vuptr;
321 void AddValidRoom(struct quickroom *qrbuf) {
322 struct ValidRoom *vrptr;
324 vrptr = (struct ValidRoom *)mallok(sizeof(struct ValidRoom));
325 vrptr->next = ValidRoomList;
326 vrptr->vr_roomnum = qrbuf->QRnumber;
327 vrptr->vr_roomgen = qrbuf->QRgen;
328 ValidRoomList = vrptr;
335 * This is a really cumbersome "garbage collection" function. We have to
336 * delete visits which refer to rooms and/or users which no longer exist. In
337 * order to prevent endless traversals of the room and user files, we first
338 * build linked lists of rooms and users which _do_ exist on the system, then
339 * traverse the visit file, checking each record against those two lists and
340 * purging the ones that do not have a match on _both_ lists. (Remember, if
341 * either the room or user being referred to is no longer on the system, the
342 * record is completely useless.)
344 int PurgeVisits(void) {
345 struct cdbdata *cdbvisit;
347 struct VPurgeList *VisitPurgeList = NULL;
348 struct VPurgeList *vptr;
352 struct ValidRoom *vrptr;
353 struct ValidUser *vuptr;
354 int RoomIsValid, UserIsValid;
356 /* First, load up a table full of valid room/gen combinations */
357 ForEachRoom(AddValidRoom);
359 /* Then load up a table full of valid user numbers */
360 ForEachUser(AddValidUser);
362 /* Now traverse through the visits, purging irrelevant records... */
363 cdb_rewind(CDB_VISIT);
364 while(cdbvisit = cdb_next_item(CDB_VISIT), cdbvisit != NULL) {
365 memset(&vbuf, 0, sizeof(struct visit));
366 memcpy(&vbuf, cdbvisit->ptr,
367 ( (cdbvisit->len > sizeof(struct visit)) ?
368 sizeof(struct visit) : cdbvisit->len) );
374 /* Check to see if the room exists */
375 for (vrptr=ValidRoomList; vrptr!=NULL; vrptr=vrptr->next) {
376 if ( (vrptr->vr_roomnum==vbuf.v_roomnum)
377 && (vrptr->vr_roomgen==vbuf.v_roomgen))
381 /* Check to see if the user exists */
382 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
383 if (vuptr->vu_usernum == vbuf.v_usernum)
387 /* Put the record on the purge list if it's dead */
388 if ((RoomIsValid==0) || (UserIsValid==0)) {
389 vptr = (struct VPurgeList *)
390 mallok(sizeof(struct VPurgeList));
391 vptr->next = VisitPurgeList;
392 vptr->vp_roomnum = vbuf.v_roomnum;
393 vptr->vp_roomgen = vbuf.v_roomgen;
394 vptr->vp_usernum = vbuf.v_usernum;
395 VisitPurgeList = vptr;
400 /* Free the valid room/gen combination list */
401 while (ValidRoomList != NULL) {
402 vrptr = ValidRoomList->next;
403 phree(ValidRoomList);
404 ValidRoomList = vrptr;
407 /* Free the valid user list */
408 while (ValidUserList != NULL) {
409 vuptr = ValidUserList->next;
410 phree(ValidUserList);
411 ValidUserList = vuptr;
414 /* Now delete every visit on the purged list */
415 while (VisitPurgeList != NULL) {
416 IndexLen = GenerateRelationshipIndex(IndexBuf,
417 VisitPurgeList->vp_roomnum,
418 VisitPurgeList->vp_roomgen,
419 VisitPurgeList->vp_usernum);
420 cdb_delete(CDB_VISIT, IndexBuf, IndexLen);
421 vptr = VisitPurgeList->next;
422 phree(VisitPurgeList);
423 VisitPurgeList = vptr;
431 void cmd_expi(char *argbuf) {
436 if ((!(CC->logged_in))&&(!(CC->internal_pgm))) {
437 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
441 if ((!is_room_aide()) && (!(CC->internal_pgm)) ) {
442 cprintf("%d Higher access required.\n",
443 ERROR+HIGHER_ACCESS_REQUIRED);
447 extract(cmd, argbuf, 0);
448 if (!strcasecmp(cmd, "users")) {
449 retval = PurgeUsers();
450 cprintf("%d Purged %d users.\n", OK, retval);
453 else if (!strcasecmp(cmd, "messages")) {
455 cprintf("%d Expired %d messages.\n", OK, messages_purged);
458 else if (!strcasecmp(cmd, "rooms")) {
459 retval = PurgeRooms();
460 cprintf("%d Expired %d rooms.\n", OK, retval);
463 else if (!strcasecmp(cmd, "visits")) {
464 retval = PurgeVisits();
465 cprintf("%d Purged %d visits.\n", OK, retval);
467 else if (!strcasecmp(cmd, "defrag")) {
469 cprintf("%d Defragmented the databases.\n", OK);
472 cprintf("%d Invalid command.\n", ERROR+ILLEGAL_VALUE);
479 struct DLModule_Info *Dynamic_Module_Init(void)
481 CtdlRegisterProtoHook(cmd_expi, "EXPI", "Expire old system objects");