2 * Server functions which perform operations on room objects.
13 #include <dirent.h> /* for cmd_rdir to read contents of the directory */
15 #if TIME_WITH_SYS_TIME
16 # include <sys/time.h>
20 # include <sys/time.h>
29 #include <libcitadel.h>
34 #include "sysdep_decls.h"
37 #include "citserver.h"
39 #include "citadel_dirs.h"
42 #include "ctdl_module.h"
45 struct floor *floorcache[MAXFLOORS];
48 * Retrieve access control information for any user/room pair
50 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf,
51 int *result, int *view)
58 if (userbuf == &CC->user) {
62 if ((is_me) && (config.c_guest_logins) && (!CC->logged_in)) {
66 /* for internal programs, always do everything */
67 if (((CC->internal_pgm)) && (roombuf->QRflags & QR_INUSE)) {
68 retval = (UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED);
73 /* If guest mode is enabled, always grant access to the Lobby */
74 if ((is_guest) && (!strcasecmp(roombuf->QRname, BASEROOM))) {
75 retval = (UA_KNOWN | UA_GOTOALLOWED);
80 /* Locate any applicable user/room relationships */
82 memset(&vbuf, 0, sizeof vbuf);
85 CtdlGetRelationship(&vbuf, userbuf, roombuf);
88 /* Force the properties of the Aide room */
89 if (!strcasecmp(roombuf->QRname, config.c_aideroom)) {
90 if (userbuf->axlevel >= AxAideU) {
91 retval = UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED;
98 /* If this is a public room, it's accessible... */
99 if ( ((roombuf->QRflags & QR_PRIVATE) == 0)
100 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
101 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
104 /* If this is a preferred users only room, check access level */
105 if (roombuf->QRflags & QR_PREFONLY) {
106 if (userbuf->axlevel < AxPrefU) {
107 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
111 /* For private rooms, check the generation number matchups */
112 if ( (roombuf->QRflags & QR_PRIVATE)
113 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
115 /* An explicit match means the user belongs in this room */
116 if (vbuf.v_flags & V_ACCESS) {
117 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
119 /* Otherwise, check if this is a guess-name or passworded
120 * room. If it is, a goto may at least be attempted
122 else if ((roombuf->QRflags & QR_PRIVATE)
123 || (roombuf->QRflags & QR_PASSWORDED)) {
124 retval = retval & ~UA_KNOWN;
125 retval = retval | UA_GOTOALLOWED;
129 /* For mailbox rooms, also check the namespace */
130 /* Also, mailbox owners can delete their messages */
131 if (roombuf->QRflags & QR_MAILBOX) {
132 if (userbuf->usernum == atol(roombuf->QRname)) {
133 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED;
135 /* An explicit match means the user belongs in this room */
136 if (vbuf.v_flags & V_ACCESS) {
137 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED;
141 /* For non-mailbox rooms... */
144 /* User is allowed to post in the room unless:
145 * - User is not validated
146 * - User has no net privileges and it is a shared network room
147 * - It is a read-only room
149 int post_allowed = 1;
150 int reply_allowed = 1;
151 if (userbuf->axlevel < AxProbU) {
155 if ((userbuf->axlevel < AxNetU) && (roombuf->QRflags & QR_NETWORK)) {
159 if (roombuf->QRflags & QR_READONLY) {
164 retval = retval | UA_POSTALLOWED | UA_REPLYALLOWED;
167 retval = retval | UA_REPLYALLOWED;
170 /* If "collaborative deletion" is active for this room, any user who can post
171 * is also allowed to delete
173 if (roombuf->QRflags2 & QR2_COLLABDEL) {
174 if (retval & UA_POSTALLOWED) {
175 retval = retval | UA_DELETEALLOWED;
181 /* Check to see if the user has forgotten this room */
182 if (vbuf.v_flags & V_FORGET) {
183 retval = retval & ~UA_KNOWN;
184 if ( ( ((roombuf->QRflags & QR_PRIVATE) == 0)
185 && ((roombuf->QRflags & QR_MAILBOX) == 0) )
186 || ( (roombuf->QRflags & QR_MAILBOX)
187 && (atol(roombuf->QRname) == CC->user.usernum))) {
188 retval = retval | UA_ZAPPED;
191 /* If user is explicitly locked out of this room, deny everything */
192 if (vbuf.v_flags & V_LOCKOUT) {
193 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED & ~UA_POSTALLOWED & ~UA_REPLYALLOWED;
196 /* Aides get access to all private rooms */
197 if ( (userbuf->axlevel >= AxAideU)
198 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
199 if (vbuf.v_flags & V_FORGET) {
200 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
203 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
207 /* Aides can gain access to mailboxes as well, but they don't show
210 if ( (userbuf->axlevel >= AxAideU)
211 && (roombuf->QRflags & QR_MAILBOX) ) {
212 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
215 /* Aides and Room Aides have admin privileges */
216 if ( (userbuf->axlevel >= AxAideU)
217 || (userbuf->usernum == roombuf->QRroomaide)
219 retval = retval | UA_ADMINALLOWED | UA_DELETEALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
222 NEWMSG: /* By the way, we also check for the presence of new messages */
223 if (is_msg_in_sequence_set(vbuf.v_seen, roombuf->QRhighest) == 0) {
224 retval = retval | UA_HASNEWMSGS;
227 /* System rooms never show up in the list. */
228 if (roombuf->QRflags2 & QR2_SYSTEM) {
229 retval = retval & ~UA_KNOWN;
233 /* Now give the caller the information it wants. */
234 if (result != NULL) *result = retval;
235 if (view != NULL) *view = vbuf.v_view;
239 * Self-checking stuff for a room record read into memory
241 void room_sanity_check(struct ctdlroom *qrbuf)
243 /* Mailbox rooms are always on the lowest floor */
244 if (qrbuf->QRflags & QR_MAILBOX) {
247 /* Listing order of 0 is illegal except for base rooms */
248 if (qrbuf->QRorder == 0)
249 if (!(qrbuf->QRflags & QR_MAILBOX) &&
250 strncasecmp(qrbuf->QRname, config.c_baseroom, ROOMNAMELEN)
252 strncasecmp(qrbuf->QRname, config.c_aideroom, ROOMNAMELEN))
258 * CtdlGetRoom() - retrieve room data from disk
260 int CtdlGetRoom(struct ctdlroom *qrbuf, char *room_name)
262 struct cdbdata *cdbqr;
263 char lowercase_name[ROOMNAMELEN];
264 char personal_lowercase_name[ROOMNAMELEN];
265 char *dptr, *sptr, *eptr;
267 dptr = lowercase_name;
269 eptr = (dptr + (sizeof lowercase_name - 1));
270 while (!IsEmptyStr(sptr) && (dptr < eptr)){
271 *dptr = tolower(*sptr);
276 memset(qrbuf, 0, sizeof(struct ctdlroom));
278 /* First, try the public namespace */
279 cdbqr = cdb_fetch(CDB_ROOMS,
280 lowercase_name, strlen(lowercase_name));
282 /* If that didn't work, try the user's personal namespace */
284 snprintf(personal_lowercase_name,
285 sizeof personal_lowercase_name, "%010ld.%s",
286 CC->user.usernum, lowercase_name);
287 cdbqr = cdb_fetch(CDB_ROOMS,
288 personal_lowercase_name,
289 strlen(personal_lowercase_name));
292 memcpy(qrbuf, cdbqr->ptr,
293 ((cdbqr->len > sizeof(struct ctdlroom)) ?
294 sizeof(struct ctdlroom) : cdbqr->len));
297 room_sanity_check(qrbuf);
306 * CtdlGetRoomLock() - same as getroom() but locks the record (if supported)
308 int CtdlGetRoomLock(struct ctdlroom *qrbuf, char *room_name)
311 retval = CtdlGetRoom(qrbuf, room_name);
312 if (retval == 0) begin_critical_section(S_ROOMS);
318 * b_putroom() - back end to putroom() and b_deleteroom()
319 * (if the supplied buffer is NULL, delete the room record)
321 void b_putroom(struct ctdlroom *qrbuf, char *room_name)
323 char lowercase_name[ROOMNAMELEN];
328 bptr = lowercase_name;
329 while (!IsEmptyStr(aptr))
331 *bptr = tolower(*aptr);
337 len = bptr - lowercase_name;
339 cdb_delete(CDB_ROOMS,
340 lowercase_name, len);
342 time(&qrbuf->QRmtime);
345 qrbuf, sizeof(struct ctdlroom));
351 * CtdlPutRoom() - store room data to disk
353 void CtdlPutRoom(struct ctdlroom *qrbuf) {
354 b_putroom(qrbuf, qrbuf->QRname);
359 * b_deleteroom() - delete a room record from disk
361 void b_deleteroom(char *room_name) {
362 b_putroom(NULL, room_name);
368 * CtdlPutRoomLock() - same as CtdlPutRoom() but unlocks the record (if supported)
370 void CtdlPutRoomLock(struct ctdlroom *qrbuf)
374 end_critical_section(S_ROOMS);
378 /****************************************************************************/
382 * CtdlGetFloorByName() - retrieve the number of the named floor
383 * return < 0 if not found else return floor number
385 int CtdlGetFloorByName(const char *floor_name)
388 struct floor *flbuf = NULL;
390 for (a = 0; a < MAXFLOORS; ++a) {
391 flbuf = CtdlGetCachedFloor(a);
393 /* check to see if it already exists */
394 if ((!strcasecmp(flbuf->f_name, floor_name))
395 && (flbuf->f_flags & F_INUSE)) {
405 * CtdlGetFloorByNameLock() - retrieve floor number for given floor and lock the floor list.
407 int CtdlGetFloorByNameLock(const char *floor_name)
409 begin_critical_section(S_FLOORTAB);
410 return CtdlGetFloorByName(floor_name);
416 * CtdlGetAvailableFloor() - Return number of first unused floor
417 * return < 0 if none available
419 int CtdlGetAvailableFloor(void)
422 struct floor *flbuf = NULL;
424 for (a = 0; a < MAXFLOORS; a++) {
425 flbuf = CtdlGetCachedFloor(a);
427 /* check to see if it already exists */
428 if ((flbuf->f_flags & F_INUSE) == 0) {
437 * CtdlGetFloor() - retrieve floor data from disk
439 void CtdlGetFloor(struct floor *flbuf, int floor_num)
441 struct cdbdata *cdbfl;
443 memset(flbuf, 0, sizeof(struct floor));
444 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
446 memcpy(flbuf, cdbfl->ptr,
447 ((cdbfl->len > sizeof(struct floor)) ?
448 sizeof(struct floor) : cdbfl->len));
451 if (floor_num == 0) {
452 safestrncpy(flbuf->f_name, "Main Floor",
453 sizeof flbuf->f_name);
454 flbuf->f_flags = F_INUSE;
455 flbuf->f_ref_count = 3;
462 * lgetfloor() - same as CtdlGetFloor() but locks the record (if supported)
464 void lgetfloor(struct floor *flbuf, int floor_num)
467 begin_critical_section(S_FLOORTAB);
468 CtdlGetFloor(flbuf, floor_num);
473 * CtdlGetCachedFloor() - Get floor record from *cache* (loads from disk if needed)
475 * This is strictly a performance hack.
477 struct floor *CtdlGetCachedFloor(int floor_num) {
478 static int initialized = 0;
481 struct floor *fl = NULL;
483 begin_critical_section(S_FLOORCACHE);
484 if (initialized == 0) {
485 for (i=0; i<MAXFLOORS; ++i) {
486 floorcache[floor_num] = NULL;
490 if (floorcache[floor_num] == NULL) {
493 end_critical_section(S_FLOORCACHE);
496 fl = malloc(sizeof(struct floor));
497 CtdlGetFloor(fl, floor_num);
498 begin_critical_section(S_FLOORCACHE);
499 if (floorcache[floor_num] != NULL) {
500 free(floorcache[floor_num]);
502 floorcache[floor_num] = fl;
503 end_critical_section(S_FLOORCACHE);
506 return(floorcache[floor_num]);
512 * CtdlPutFloor() - store floor data on disk
514 void CtdlPutFloor(struct floor *flbuf, int floor_num)
516 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
517 begin_critical_section(S_FLOORCACHE);
518 if (floorcache[floor_num] != NULL) {
519 free(floorcache[floor_num]);
520 floorcache[floor_num] = malloc(sizeof(struct floor));
521 memcpy(floorcache[floor_num], flbuf, sizeof(struct floor));
523 end_critical_section(S_FLOORCACHE);
525 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
526 flbuf, sizeof(struct floor));
532 * CtdlPutFloorLock() - same as CtdlPutFloor() but unlocks the record (if supported)
534 void CtdlPutFloorLock(struct floor *flbuf, int floor_num)
537 CtdlPutFloor(flbuf, floor_num);
538 end_critical_section(S_FLOORTAB);
545 * lputfloor() - same as CtdlPutFloor() but unlocks the record (if supported)
547 void lputfloor(struct floor *flbuf, int floor_num)
549 CtdlPutFloorLock(flbuf, floor_num);
554 * Traverse the room file...
556 void CtdlForEachRoom(void (*CallBack) (struct ctdlroom *EachRoom, void *out_data),
559 struct ctdlroom qrbuf;
560 struct cdbdata *cdbqr;
562 cdb_rewind(CDB_ROOMS);
564 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
565 memset(&qrbuf, 0, sizeof(struct ctdlroom));
566 memcpy(&qrbuf, cdbqr->ptr,
567 ((cdbqr->len > sizeof(struct ctdlroom)) ?
568 sizeof(struct ctdlroom) : cdbqr->len));
570 room_sanity_check(&qrbuf);
571 if (qrbuf.QRflags & QR_INUSE)
572 (*CallBack)(&qrbuf, in_data);
578 * delete_msglist() - delete room message pointers
580 void delete_msglist(struct ctdlroom *whichroom)
582 struct cdbdata *cdbml;
584 /* Make sure the msglist we're deleting actually exists, otherwise
585 * gdbm will complain when we try to delete an invalid record
587 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
591 /* Go ahead and delete it */
592 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
599 * Message pointer compare function for sort_msglist()
601 int sort_msglist_cmp(const void *m1, const void *m2) {
602 if ((*(const long *)m1) > (*(const long *)m2)) return(1);
603 if ((*(const long *)m1) < (*(const long *)m2)) return(-1);
609 * sort message pointers
610 * (returns new msg count)
612 int sort_msglist(long listptrs[], int oldcount)
622 qsort(listptrs, numitems, sizeof(long), sort_msglist_cmp);
624 /* and yank any nulls */
625 while ((numitems > 0) && (listptrs[0] == 0L)) {
626 memmove(&listptrs[0], &listptrs[1],
627 (sizeof(long) * (numitems - 1)));
636 * Determine whether a given room is non-editable.
638 int CtdlIsNonEditable(struct ctdlroom *qrbuf)
641 /* Mail> rooms are non-editable */
642 if ( (qrbuf->QRflags & QR_MAILBOX)
643 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
646 /* Everything else is editable */
653 * Back-back-end for all room listing commands
655 void list_roomname(struct ctdlroom *qrbuf, int ra, int current_view, int default_view)
657 char truncated_roomname[ROOMNAMELEN];
659 /* For my own mailbox rooms, chop off the owner prefix */
660 if ( (qrbuf->QRflags & QR_MAILBOX)
661 && (atol(qrbuf->QRname) == CC->user.usernum) ) {
662 safestrncpy(truncated_roomname, qrbuf->QRname, sizeof truncated_roomname);
663 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
664 cprintf("%s", truncated_roomname);
666 /* For all other rooms, just display the name in its entirety */
668 cprintf("%s", qrbuf->QRname);
671 /* ...and now the other parameters */
672 cprintf("|%u|%d|%d|%d|%d|%d|%d|%ld|\n",
674 (int) qrbuf->QRfloor,
675 (int) qrbuf->QRorder,
676 (int) qrbuf->QRflags2,
686 * cmd_lrms() - List all accessible rooms, known or forgotten
688 void cmd_lrms_backend(struct ctdlroom *qrbuf, void *data)
690 int FloorBeingSearched = (-1);
694 FloorBeingSearched = *(int *)data;
695 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
697 if ((( ra & (UA_KNOWN | UA_ZAPPED)))
698 && ((qrbuf->QRfloor == (FloorBeingSearched))
699 || ((FloorBeingSearched) < 0)))
700 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
703 void cmd_lrms(char *argbuf)
705 int FloorBeingSearched = (-1);
706 if (!IsEmptyStr(argbuf))
707 FloorBeingSearched = extract_int(argbuf, 0);
709 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
711 CtdlGetUser(&CC->user, CC->curr_user);
712 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
714 CtdlForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
721 * cmd_lkra() - List all known rooms
723 void cmd_lkra_backend(struct ctdlroom *qrbuf, void *data)
725 int FloorBeingSearched = (-1);
729 FloorBeingSearched = *(int *)data;
730 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
732 if ((( ra & (UA_KNOWN)))
733 && ((qrbuf->QRfloor == (FloorBeingSearched))
734 || ((FloorBeingSearched) < 0)))
735 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
738 void cmd_lkra(char *argbuf)
740 int FloorBeingSearched = (-1);
741 if (!IsEmptyStr(argbuf))
742 FloorBeingSearched = extract_int(argbuf, 0);
744 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
746 CtdlGetUser(&CC->user, CC->curr_user);
747 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
749 CtdlForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
755 void cmd_lprm_backend(struct ctdlroom *qrbuf, void *data)
757 int FloorBeingSearched = (-1);
761 FloorBeingSearched = *(int *)data;
762 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
764 if ( ((qrbuf->QRflags & QR_PRIVATE) == 0)
765 && ((qrbuf->QRflags & QR_MAILBOX) == 0)
766 && ((qrbuf->QRfloor == (FloorBeingSearched))
767 || ((FloorBeingSearched) < 0)))
768 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
771 void cmd_lprm(char *argbuf)
773 int FloorBeingSearched = (-1);
774 if (!IsEmptyStr(argbuf))
775 FloorBeingSearched = extract_int(argbuf, 0);
777 cprintf("%d Publiic rooms:\n", LISTING_FOLLOWS);
779 CtdlForEachRoom(cmd_lprm_backend, &FloorBeingSearched);
786 * cmd_lkrn() - List all known rooms with new messages
788 void cmd_lkrn_backend(struct ctdlroom *qrbuf, void *data)
790 int FloorBeingSearched = (-1);
794 FloorBeingSearched = *(int *)data;
795 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
798 && (ra & UA_HASNEWMSGS)
799 && ((qrbuf->QRfloor == (FloorBeingSearched))
800 || ((FloorBeingSearched) < 0)))
801 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
804 void cmd_lkrn(char *argbuf)
806 int FloorBeingSearched = (-1);
807 if (!IsEmptyStr(argbuf))
808 FloorBeingSearched = extract_int(argbuf, 0);
810 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
812 CtdlGetUser(&CC->user, CC->curr_user);
813 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
815 CtdlForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
822 * cmd_lkro() - List all known rooms
824 void cmd_lkro_backend(struct ctdlroom *qrbuf, void *data)
826 int FloorBeingSearched = (-1);
830 FloorBeingSearched = *(int *)data;
831 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
834 && ((ra & UA_HASNEWMSGS) == 0)
835 && ((qrbuf->QRfloor == (FloorBeingSearched))
836 || ((FloorBeingSearched) < 0)))
837 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
840 void cmd_lkro(char *argbuf)
842 int FloorBeingSearched = (-1);
843 if (!IsEmptyStr(argbuf))
844 FloorBeingSearched = extract_int(argbuf, 0);
846 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
848 CtdlGetUser(&CC->user, CC->curr_user);
849 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
851 CtdlForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
858 * cmd_lzrm() - List all forgotten rooms
860 void cmd_lzrm_backend(struct ctdlroom *qrbuf, void *data)
862 int FloorBeingSearched = (-1);
866 FloorBeingSearched = *(int *)data;
867 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
869 if ((ra & UA_GOTOALLOWED)
871 && ((qrbuf->QRfloor == (FloorBeingSearched))
872 || ((FloorBeingSearched) < 0)))
873 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
876 void cmd_lzrm(char *argbuf)
878 int FloorBeingSearched = (-1);
879 if (!IsEmptyStr(argbuf))
880 FloorBeingSearched = extract_int(argbuf, 0);
882 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
884 CtdlGetUser(&CC->user, CC->curr_user);
885 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
887 CtdlForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
893 * Make the specified room the current room for this session. No validation
894 * or access control is done here -- the caller should make sure that the
895 * specified room exists and is ok to access.
897 void CtdlUserGoto(char *where, int display_result, int transiently,
898 int *retmsgs, int *retnew)
901 int new_messages = 0;
902 int old_messages = 0;
903 int total_messages = 0;
907 int newmailcount = 0;
909 char truncated_roomname[ROOMNAMELEN];
910 struct cdbdata *cdbfr;
911 long *msglist = NULL;
913 unsigned int original_v_flags;
916 char setstr[128], lostr[64], histr[64];
920 /* If the supplied room name is NULL, the caller wants us to know that
921 * it has already copied the room record into CC->room, so
922 * we can skip the extra database fetch.
925 safestrncpy(CC->room.QRname, where, sizeof CC->room.QRname);
926 CtdlGetRoom(&CC->room, where);
929 /* Take care of all the formalities. */
931 begin_critical_section(S_USERS);
932 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
933 original_v_flags = vbuf.v_flags;
935 /* Know the room ... but not if it's the page log room, or if the
936 * caller specified that we're only entering this room transiently.
938 if ((strcasecmp(CC->room.QRname, config.c_logpages))
939 && (transiently == 0) ) {
940 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
941 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
944 /* Only rewrite the database record if we changed something */
945 if (vbuf.v_flags != original_v_flags) {
946 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
948 end_critical_section(S_USERS);
950 /* Check for new mail */
951 newmailcount = NewMailCount();
953 /* set info to 1 if the user needs to read the room's info file */
954 if (CC->room.QRinfo > vbuf.v_lastseen) {
958 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
960 msglist = (long *) cdbfr->ptr;
961 cdbfr->ptr = NULL; /* CtdlUserGoto() now owns this memory */
962 num_msgs = cdbfr->len / sizeof(long);
967 for (a=0; a<num_msgs; ++a) {
968 if (msglist[a] > 0L) ++total_messages;
970 new_messages = num_msgs;
971 num_sets = num_tokens(vbuf.v_seen, ',');
972 for (s=0; s<num_sets; ++s) {
973 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
975 extract_token(lostr, setstr, 0, ':', sizeof lostr);
976 if (num_tokens(setstr, ':') >= 2) {
977 extract_token(histr, setstr, 1, ':', sizeof histr);
978 if (!strcmp(histr, "*")) {
979 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
983 strcpy(histr, lostr);
988 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
989 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
995 new_messages = total_messages - old_messages;
997 if (msglist != NULL) free(msglist);
999 if (CC->room.QRflags & QR_MAILBOX)
1004 if ((CC->room.QRroomaide == CC->user.usernum)
1005 || (CC->user.axlevel >= AxAideU))
1010 safestrncpy(truncated_roomname, CC->room.QRname, sizeof truncated_roomname);
1011 if ( (CC->room.QRflags & QR_MAILBOX)
1012 && (atol(CC->room.QRname) == CC->user.usernum) ) {
1013 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
1016 if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
1020 if (retmsgs != NULL) *retmsgs = total_messages;
1021 if (retnew != NULL) *retnew = new_messages;
1022 CtdlLogPrintf(CTDL_DEBUG, "<%s> %d new of %d total messages\n",
1024 new_messages, total_messages
1027 CC->curr_view = (int)vbuf.v_view;
1029 if (display_result) {
1030 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|%d|\n",
1031 CIT_OK, CtdlCheckExpress(),
1034 (int)total_messages,
1036 (int)CC->room.QRflags,
1037 (long)CC->room.QRhighest,
1038 (long)vbuf.v_lastseen,
1042 (int)CC->room.QRfloor,
1044 (int)CC->room.QRdefaultview,
1046 (int)CC->room.QRflags2
1053 * Handle some of the macro named rooms
1055 void convert_room_name_macros(char *towhere, size_t maxlen) {
1056 if (!strcasecmp(towhere, "_BASEROOM_")) {
1057 safestrncpy(towhere, config.c_baseroom, maxlen);
1059 else if (!strcasecmp(towhere, "_MAIL_")) {
1060 safestrncpy(towhere, MAILROOM, maxlen);
1062 else if (!strcasecmp(towhere, "_TRASH_")) {
1063 safestrncpy(towhere, USERTRASHROOM, maxlen);
1065 else if (!strcasecmp(towhere, "_DRAFTS_")) {
1066 safestrncpy(towhere, USERDRAFTROOM, maxlen);
1068 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
1069 safestrncpy(towhere, config.c_twitroom, maxlen);
1071 else if (!strcasecmp(towhere, "_CALENDAR_")) {
1072 safestrncpy(towhere, USERCALENDARROOM, maxlen);
1074 else if (!strcasecmp(towhere, "_TASKS_")) {
1075 safestrncpy(towhere, USERTASKSROOM, maxlen);
1077 else if (!strcasecmp(towhere, "_CONTACTS_")) {
1078 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
1080 else if (!strcasecmp(towhere, "_NOTES_")) {
1081 safestrncpy(towhere, USERNOTESROOM, maxlen);
1087 * cmd_goto() - goto a new room
1089 void cmd_goto(char *gargs)
1091 struct ctdlroom QRscratch;
1095 char augmented_roomname[ROOMNAMELEN];
1096 char towhere[ROOMNAMELEN];
1098 int transiently = 0;
1100 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
1102 extract_token(towhere, gargs, 0, '|', sizeof towhere);
1103 extract_token(password, gargs, 1, '|', sizeof password);
1104 transiently = extract_int(gargs, 2);
1106 CtdlGetUser(&CC->user, CC->curr_user);
1109 * Handle some of the macro named rooms
1111 convert_room_name_macros(towhere, sizeof towhere);
1113 /* First try a regular match */
1114 c = CtdlGetRoom(&QRscratch, towhere);
1116 /* Then try a mailbox name match */
1118 CtdlMailboxName(augmented_roomname, sizeof augmented_roomname,
1119 &CC->user, towhere);
1120 c = CtdlGetRoom(&QRscratch, augmented_roomname);
1122 safestrncpy(towhere, augmented_roomname, sizeof towhere);
1125 /* And if the room was found... */
1128 /* Let internal programs go directly to any room. */
1129 if (CC->internal_pgm) {
1130 memcpy(&CC->room, &QRscratch,
1131 sizeof(struct ctdlroom));
1132 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1136 /* See if there is an existing user/room relationship */
1137 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
1139 /* normal clients have to pass through security */
1140 if (ra & UA_GOTOALLOWED) {
1145 if ((QRscratch.QRflags & QR_MAILBOX) &&
1146 ((ra & UA_GOTOALLOWED))) {
1147 memcpy(&CC->room, &QRscratch,
1148 sizeof(struct ctdlroom));
1149 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1151 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
1152 ((ra & UA_KNOWN) == 0) &&
1153 (strcasecmp(QRscratch.QRpasswd, password)) &&
1154 (CC->user.axlevel < AxAideU)
1156 cprintf("%d wrong or missing passwd\n",
1157 ERROR + PASSWORD_REQUIRED);
1159 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
1160 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
1161 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
1162 ((ra & UA_KNOWN) == 0) &&
1163 (CC->user.axlevel < AxAideU)
1165 CtdlLogPrintf(CTDL_DEBUG, "Failed to acquire private room\n");
1167 memcpy(&CC->room, &QRscratch,
1168 sizeof(struct ctdlroom));
1169 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1175 cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
1179 void cmd_whok(char *cmdbuf)
1181 struct ctdluser temp;
1182 struct cdbdata *cdbus;
1185 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
1186 cdb_rewind(CDB_USERS);
1187 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1188 memset(&temp, 0, sizeof temp);
1189 memcpy(&temp, cdbus->ptr, sizeof temp);
1192 CtdlRoomAccess(&CC->room, &temp, &ra, NULL);
1193 if ((!IsEmptyStr(temp.fullname)) &&
1194 (CC->room.QRflags & QR_INUSE) &&
1197 cprintf("%s\n", temp.fullname);
1204 * RDIR command for room directory
1206 void cmd_rdir(char *cmdbuf)
1211 struct stat statbuf;
1212 DIR *filedir = NULL;
1213 struct dirent *filedir_entry;
1219 if (CtdlAccessCheck(ac_logged_in)) return;
1221 CtdlGetRoom(&CC->room, CC->room.QRname);
1222 CtdlGetUser(&CC->user, CC->curr_user);
1224 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
1225 cprintf("%d not here.\n", ERROR + NOT_HERE);
1228 if (((CC->room.QRflags & QR_VISDIR) == 0)
1229 && (CC->user.axlevel < AxAideU)
1230 && (CC->user.usernum != CC->room.QRroomaide)) {
1231 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1235 snprintf(buf, sizeof buf, "%s/%s", ctdl_file_dir, CC->room.QRdirname);
1236 filedir = opendir (buf);
1238 if (filedir == NULL) {
1239 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1242 cprintf("%d %s|%s/%s\n", LISTING_FOLLOWS, config.c_fqdn, ctdl_file_dir, CC->room.QRdirname);
1244 snprintf(buf, sizeof buf, "%s/%s/filedir", ctdl_file_dir, CC->room.QRdirname);
1245 fd = fopen(buf, "r");
1247 fd = fopen("/dev/null", "r");
1248 while ((filedir_entry = readdir(filedir)))
1250 if (strcasecmp(filedir_entry->d_name, "filedir") && filedir_entry->d_name[0] != '.')
1252 #ifdef _DIRENT_HAVE_D_NAMELEN
1253 d_namelen = filedir_entry->d_namelen;
1255 d_namelen = strlen(filedir_entry->d_name);
1257 snprintf(buf, sizeof buf, "%s/%s/%s", ctdl_file_dir, CC->room.QRdirname, filedir_entry->d_name);
1258 stat(buf, &statbuf); /* stat the file */
1259 if (!(statbuf.st_mode & S_IFREG))
1261 snprintf(buf2, sizeof buf2,
1262 "\"%s\" appears in the file directory for room \"%s\" but is not a regular file. Directories, named pipes, sockets, etc. are not usable in Citadel room directories.\n",
1263 buf, CC->room.QRname
1265 CtdlAideMessage(buf2, "Unusable data found in room directory");
1266 continue; /* not a useable file type so don't show it */
1268 safestrncpy(comment, "", sizeof comment);
1269 fseek(fd, 0L, 0); /* rewind descriptions file */
1270 /* Get the description from the descriptions file */
1271 while ((fgets(buf, sizeof buf, fd) != NULL) && (IsEmptyStr(comment)))
1273 buf[strlen(buf) - 1] = 0;
1274 if ((!strncasecmp(buf, filedir_entry->d_name, d_namelen)) && (buf[d_namelen] == ' '))
1275 safestrncpy(comment, &buf[d_namelen + 1], sizeof comment);
1277 len = extract_token (mimebuf, comment, 0,' ', 64);
1278 if ((len <0) || strchr(mimebuf, '/') == NULL)
1280 snprintf (mimebuf, 64, "application/octetstream");
1283 cprintf("%s|%ld|%s|%s\n",
1284 filedir_entry->d_name,
1285 (long)statbuf.st_size,
1297 * get room parameters (aide or room aide command)
1299 void cmd_getr(char *cmdbuf)
1301 if (CtdlAccessCheck(ac_room_aide)) return;
1303 CtdlGetRoom(&CC->room, CC->room.QRname);
1304 cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1308 ((CC->room.QRflags & QR_MAILBOX) ?
1309 &CC->room.QRname[11] : CC->room.QRname),
1311 ((CC->room.QRflags & QR_PASSWORDED) ?
1312 CC->room.QRpasswd : ""),
1314 ((CC->room.QRflags & QR_DIRECTORY) ?
1315 CC->room.QRdirname : ""),
1318 (int) CC->room.QRfloor,
1319 (int) CC->room.QRorder,
1321 CC->room.QRdefaultview,
1328 * Back end function to rename a room.
1329 * You can also specify which floor to move the room to, or specify -1 to
1330 * keep the room on the same floor it was on.
1332 * If you are renaming a mailbox room, you must supply the namespace prefix
1333 * in *at least* the old name!
1335 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1337 struct ctdlroom qrbuf;
1338 struct ctdlroom qrtmp;
1343 char actual_old_name[ROOMNAMELEN];
1345 CtdlLogPrintf(CTDL_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1346 old_name, new_name, new_floor);
1348 if (new_floor >= 0) {
1349 fl = CtdlGetCachedFloor(new_floor);
1350 if ((fl->f_flags & F_INUSE) == 0) {
1351 return(crr_invalid_floor);
1355 begin_critical_section(S_ROOMS);
1357 if ( (CtdlGetRoom(&qrtmp, new_name) == 0)
1358 && (strcasecmp(new_name, old_name)) ) {
1359 ret = crr_already_exists;
1362 else if (CtdlGetRoom(&qrbuf, old_name) != 0) {
1363 ret = crr_room_not_found;
1366 else if ( (CC->user.axlevel < AxAideU) && (!CC->internal_pgm)
1367 && (CC->user.usernum != qrbuf.QRroomaide)
1368 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) ) {
1369 ret = crr_access_denied;
1372 else if (CtdlIsNonEditable(&qrbuf)) {
1373 ret = crr_noneditable;
1378 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1379 if (qrbuf.QRflags & QR_MAILBOX) {
1380 owner = atol(qrbuf.QRname);
1382 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1383 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1384 "%010ld.%s", owner, new_name);
1387 safestrncpy(qrbuf.QRname, new_name,
1388 sizeof(qrbuf.QRname));
1391 /* Reject change of floor for baseroom/aideroom */
1392 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1393 !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1397 /* Take care of floor stuff */
1398 old_floor = qrbuf.QRfloor;
1399 if (new_floor < 0) {
1400 new_floor = old_floor;
1402 qrbuf.QRfloor = new_floor;
1403 CtdlPutRoom(&qrbuf);
1405 begin_critical_section(S_CONFIG);
1407 /* If baseroom/aideroom name changes, update config */
1408 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1409 safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1412 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1413 safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1417 end_critical_section(S_CONFIG);
1419 /* If the room name changed, then there are now two room
1420 * records, so we have to delete the old one.
1422 if (strcasecmp(new_name, old_name)) {
1423 b_deleteroom(actual_old_name);
1429 end_critical_section(S_ROOMS);
1431 /* Adjust the floor reference counts if necessary */
1432 if (new_floor != old_floor) {
1433 lgetfloor(&flbuf, old_floor);
1434 --flbuf.f_ref_count;
1435 lputfloor(&flbuf, old_floor);
1436 CtdlLogPrintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1437 lgetfloor(&flbuf, new_floor);
1438 ++flbuf.f_ref_count;
1439 lputfloor(&flbuf, new_floor);
1440 CtdlLogPrintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1443 /* ...and everybody say "YATTA!" */
1449 * set room parameters (aide or room aide command)
1451 void cmd_setr(char *args)
1457 char new_name[ROOMNAMELEN];
1459 if (CtdlAccessCheck(ac_logged_in)) return;
1461 if (num_parms(args) >= 6) {
1462 new_floor = extract_int(args, 5);
1464 new_floor = (-1); /* don't change the floor */
1467 /* When is a new name more than just a new name? When the old name
1468 * has a namespace prefix.
1470 if (CC->room.QRflags & QR_MAILBOX) {
1471 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
1473 safestrncpy(new_name, "", sizeof new_name);
1475 extract_token(&new_name[strlen(new_name)], args, 0, '|', (sizeof new_name - strlen(new_name)));
1477 r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
1479 if (r == crr_room_not_found) {
1480 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
1481 } else if (r == crr_already_exists) {
1482 cprintf("%d '%s' already exists.\n",
1483 ERROR + ALREADY_EXISTS, new_name);
1484 } else if (r == crr_noneditable) {
1485 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
1486 } else if (r == crr_invalid_floor) {
1487 cprintf("%d Target floor does not exist.\n",
1488 ERROR + INVALID_FLOOR_OPERATION);
1489 } else if (r == crr_access_denied) {
1490 cprintf("%d You do not have permission to edit '%s'\n",
1491 ERROR + HIGHER_ACCESS_REQUIRED,
1493 } else if (r != crr_ok) {
1494 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1495 ERROR + INTERNAL_ERROR, r);
1502 CtdlGetRoom(&CC->room, new_name);
1504 /* Now we have to do a bunch of other stuff */
1506 if (num_parms(args) >= 7) {
1507 new_order = extract_int(args, 6);
1510 if (new_order > 127)
1514 CtdlGetRoomLock(&CC->room, CC->room.QRname);
1516 /* Directory room */
1517 extract_token(buf, args, 2, '|', sizeof buf);
1519 safestrncpy(CC->room.QRdirname, buf,
1520 sizeof CC->room.QRdirname);
1523 if (num_parms(args) >= 8) {
1524 CC->room.QRdefaultview = extract_int(args, 7);
1527 /* Second set of flags */
1528 if (num_parms(args) >= 9) {
1529 CC->room.QRflags2 = extract_int(args, 8);
1533 CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
1534 /* Clean up a client boo-boo: if the client set the room to
1535 * guess-name or passworded, ensure that the private flag is
1538 if ((CC->room.QRflags & QR_GUESSNAME)
1539 || (CC->room.QRflags & QR_PASSWORDED))
1540 CC->room.QRflags |= QR_PRIVATE;
1542 /* Some changes can't apply to BASEROOM */
1543 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1545 CC->room.QRorder = 0;
1546 CC->room.QRpasswd[0] = '\0';
1547 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1548 QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1549 CC->room.QRflags |= QR_PERMANENT;
1551 /* March order (doesn't apply to AIDEROOM) */
1552 if (num_parms(args) >= 7)
1553 CC->room.QRorder = (char) new_order;
1555 extract_token(buf, args, 1, '|', sizeof buf);
1557 safestrncpy(CC->room.QRpasswd, buf,
1558 sizeof CC->room.QRpasswd);
1559 /* Kick everyone out if the client requested it
1560 * (by changing the room's generation number)
1562 if (extract_int(args, 4)) {
1563 time(&CC->room.QRgen);
1566 /* Some changes can't apply to AIDEROOM */
1567 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1569 CC->room.QRorder = 0;
1570 CC->room.QRflags &= ~QR_MAILBOX;
1571 CC->room.QRflags |= QR_PERMANENT;
1574 /* Write the room record back to disk */
1575 CtdlPutRoomLock(&CC->room);
1577 /* Create a room directory if necessary */
1578 if (CC->room.QRflags & QR_DIRECTORY) {
1579 snprintf(buf, sizeof buf,"%s/%s",
1581 CC->room.QRdirname);
1584 snprintf(buf, sizeof buf, "The room \"%s\" has been edited by %s.\n",
1585 CC->room.QRname, CC->curr_user);
1586 CtdlAideMessage(buf, "Room modification Message");
1587 cprintf("%d Ok\n", CIT_OK);
1593 * get the name of the room aide for this room
1595 void cmd_geta(char *cmdbuf)
1597 struct ctdluser usbuf;
1599 if (CtdlAccessCheck(ac_logged_in)) return;
1601 if (CtdlGetUserByNumber(&usbuf, CC->room.QRroomaide) == 0) {
1602 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1604 cprintf("%d \n", CIT_OK);
1610 * set the room aide for this room
1612 void cmd_seta(char *new_ra)
1614 struct ctdluser usbuf;
1619 if (CtdlAccessCheck(ac_room_aide)) return;
1621 if (CtdlGetUser(&usbuf, new_ra) != 0) {
1624 newu = usbuf.usernum;
1627 CtdlGetRoomLock(&CC->room, CC->room.QRname);
1629 if (CC->room.QRroomaide != newu) {
1632 CC->room.QRroomaide = newu;
1633 CtdlPutRoomLock(&CC->room);
1636 * We have to post the change notice _after_ writing changes to
1637 * the room table, otherwise it would deadlock!
1639 if (post_notice == 1) {
1640 if (!IsEmptyStr(usbuf.fullname))
1641 snprintf(buf, sizeof buf,
1642 "%s is now the room aide for \"%s\".\n",
1643 usbuf.fullname, CC->room.QRname);
1645 snprintf(buf, sizeof buf,
1646 "There is now no room aide for \"%s\".\n",
1648 CtdlAideMessage(buf, "Aide Room Modification");
1650 cprintf("%d Ok\n", CIT_OK);
1654 * retrieve info file for this room
1656 void cmd_rinf(char *gargs)
1662 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_info_dir);
1663 info_fp = fopen(filename, "r");
1665 if (info_fp == NULL) {
1666 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
1669 cprintf("%d Info:\n", LISTING_FOLLOWS);
1670 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1671 if (!IsEmptyStr(buf))
1672 buf[strlen(buf) - 1] = 0;
1673 cprintf("%s\n", buf);
1680 * Asynchronously schedule a room for deletion. The room will appear
1681 * deleted to the user(s), but it won't actually get purged from the
1682 * database until THE DREADED AUTO-PURGER makes its next run.
1684 void CtdlScheduleRoomForDeletion(struct ctdlroom *qrbuf)
1686 char old_name[ROOMNAMELEN];
1689 CtdlLogPrintf(CTDL_NOTICE, "Scheduling room <%s> for deletion\n",
1692 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1694 CtdlGetRoom(qrbuf, qrbuf->QRname);
1696 /* Turn the room into a private mailbox owned by a user who doesn't
1697 * exist. This will immediately make the room invisible to everyone,
1698 * and qualify the room for purging.
1700 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1705 qrbuf->QRflags |= QR_MAILBOX;
1706 time(&qrbuf->QRgen); /* Use a timestamp as the new generation number */
1710 b_deleteroom(old_name);
1716 * Back end processing to delete a room and everything associated with it
1717 * (This one is synchronous and should only get called by THE DREADED
1718 * AUTO-PURGER in serv_expire.c. All user-facing code should call
1719 * the asynchronous schedule_room_for_deletion() instead.)
1721 void CtdlDeleteRoom(struct ctdlroom *qrbuf)
1725 /* TODO: filename magic? does this realy work? */
1727 CtdlLogPrintf(CTDL_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1729 /* Delete the info file */
1730 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_info_dir);
1733 /* Delete the image file */
1734 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_image_dir);
1737 /* Delete the room's network config file */
1738 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1741 /* Delete the messages in the room
1742 * (Careful: this opens an S_ROOMS critical section!)
1744 CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
1746 /* Flag the room record as not in use */
1747 CtdlGetRoomLock(qrbuf, qrbuf->QRname);
1749 CtdlPutRoomLock(qrbuf);
1751 /* then decrement the reference count for the floor */
1752 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1753 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1754 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1756 /* Delete the room record from the database! */
1757 b_deleteroom(qrbuf->QRname);
1763 * Check access control for deleting a room
1765 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1767 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1771 if (CtdlIsNonEditable(qr)) {
1776 * For mailboxes, check stuff
1778 if (qr->QRflags & QR_MAILBOX) {
1780 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1782 if (atol(qr->QRname) != CC->user.usernum) {
1783 return(0); /* not my room */
1786 /* Can't delete your Mail> room */
1787 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1789 /* Otherwise it's ok */
1794 * For normal rooms, just check for aide or room aide status.
1796 return(is_room_aide());
1800 * aide command: kill the current room
1802 void cmd_kill(char *argbuf)
1804 char deleted_room_name[ROOMNAMELEN];
1808 kill_ok = extract_int(argbuf, 0);
1810 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
1811 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1815 if (CC->room.QRflags & QR_MAILBOX) {
1816 safestrncpy(deleted_room_name, &CC->room.QRname[11], sizeof deleted_room_name);
1819 safestrncpy(deleted_room_name, CC->room.QRname, sizeof deleted_room_name);
1822 /* Do the dirty work */
1823 CtdlScheduleRoomForDeletion(&CC->room);
1825 /* Return to the Lobby */
1826 CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
1828 /* tell the world what we did */
1829 snprintf(msg, sizeof msg, "The room \"%s\" has been deleted by %s.\n",
1830 deleted_room_name, CC->curr_user);
1831 CtdlAideMessage(msg, "Room Purger Message");
1832 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1834 cprintf("%d ok to delete.\n", CIT_OK);
1840 * Internal code to create a new room (returns room flags)
1842 * Room types: 0=public, 1=hidden, 2=passworded, 3=invitation-only,
1843 * 4=mailbox, 5=mailbox, but caller supplies namespace
1845 unsigned CtdlCreateRoom(char *new_room_name,
1847 char *new_room_pass,
1854 struct ctdlroom qrbuf;
1858 CtdlLogPrintf(CTDL_DEBUG, "CtdlCreateRoom(name=%s, type=%d, view=%d)\n",
1859 new_room_name, new_room_type, new_room_view);
1861 if (CtdlGetRoom(&qrbuf, new_room_name) == 0) {
1862 CtdlLogPrintf(CTDL_DEBUG, "%s already exists.\n", new_room_name);
1866 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1867 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1868 qrbuf.QRflags = QR_INUSE;
1869 if (new_room_type > 0)
1870 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1871 if (new_room_type == 1)
1872 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1873 if (new_room_type == 2)
1874 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1875 if ( (new_room_type == 4) || (new_room_type == 5) ) {
1876 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1877 /* qrbuf.QRflags2 |= QR2_SUBJECTREQ; */
1880 /* If the user is requesting a personal room, set up the room
1881 * name accordingly (prepend the user number)
1883 if (new_room_type == 4) {
1884 CtdlMailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1887 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1890 /* If the room is private, and the system administrator has elected
1891 * to automatically grant room aide privileges, do so now; otherwise,
1892 * set the room aide to undefined.
1894 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1895 qrbuf.QRroomaide = CC->user.usernum;
1897 qrbuf.QRroomaide = (-1L);
1901 * If the caller is only interested in testing whether this will work,
1902 * return now without creating the room.
1904 if (!really_create) return (qrbuf.QRflags);
1906 qrbuf.QRnumber = get_new_room_number();
1907 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1908 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1909 qrbuf.QRfloor = new_room_floor;
1910 qrbuf.QRdefaultview = new_room_view;
1912 /* save what we just did... */
1913 CtdlPutRoom(&qrbuf);
1915 /* bump the reference count on whatever floor the room is on */
1916 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1917 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1918 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1920 /* Grant the creator access to the room unless the avoid_access
1921 * parameter was specified.
1923 if ( (CC->logged_in) && (avoid_access == 0) ) {
1924 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1925 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1926 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1927 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1930 /* resume our happy day */
1931 return (qrbuf.QRflags);
1938 void cmd_cre8(char *args)
1941 char new_room_name[ROOMNAMELEN];
1943 char new_room_pass[32];
1946 char *notification_message = NULL;
1949 int avoid_access = 0;
1951 cre8_ok = extract_int(args, 0);
1952 extract_token(new_room_name, args, 1, '|', sizeof new_room_name);
1953 new_room_name[ROOMNAMELEN - 1] = 0;
1954 new_room_type = extract_int(args, 2);
1955 extract_token(new_room_pass, args, 3, '|', sizeof new_room_pass);
1956 avoid_access = extract_int(args, 5);
1957 new_room_view = extract_int(args, 6);
1958 new_room_pass[9] = 0;
1961 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 1)) {
1962 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
1966 if (!strcasecmp(new_room_name, MAILROOM)) {
1967 cprintf("%d '%s' already exists.\n",
1968 ERROR + ALREADY_EXISTS, new_room_name);
1972 if (num_parms(args) >= 5) {
1973 fl = CtdlGetCachedFloor(extract_int(args, 4));
1975 cprintf("%d Invalid floor number.\n",
1976 ERROR + INVALID_FLOOR_OPERATION);
1979 else if ((fl->f_flags & F_INUSE) == 0) {
1980 cprintf("%d Invalid floor number.\n",
1981 ERROR + INVALID_FLOOR_OPERATION);
1984 new_room_floor = extract_int(args, 4);
1988 if (CtdlAccessCheck(ac_logged_in)) return;
1990 if (CC->user.axlevel < config.c_createax && !CC->internal_pgm) {
1991 cprintf("%d You need higher access to create rooms.\n",
1992 ERROR + HIGHER_ACCESS_REQUIRED);
1996 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 0)) {
1997 cprintf("%d Ok to create rooms.\n", CIT_OK);
2001 if ((new_room_type < 0) || (new_room_type > 5)) {
2002 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
2006 if (new_room_type == 5) {
2007 if (CC->user.axlevel < AxAideU) {
2008 cprintf("%d Higher access required\n",
2009 ERROR + HIGHER_ACCESS_REQUIRED);
2014 /* Check to make sure the requested room name doesn't already exist */
2015 newflags = CtdlCreateRoom(new_room_name,
2016 new_room_type, new_room_pass, new_room_floor,
2017 0, avoid_access, new_room_view);
2018 if (newflags == 0) {
2019 cprintf("%d '%s' already exists.\n",
2020 ERROR + ALREADY_EXISTS, new_room_name);
2025 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
2029 /* If we reach this point, the room needs to be created. */
2031 newflags = CtdlCreateRoom(new_room_name,
2032 new_room_type, new_room_pass, new_room_floor, 1, 0,
2035 /* post a message in Aide> describing the new room */
2036 notification_message = malloc(1024);
2037 snprintf(notification_message, 1024,
2038 "A new room called \"%s\" has been created by %s%s%s%s%s%s\n",
2041 ((newflags & QR_MAILBOX) ? " [personal]" : ""),
2042 ((newflags & QR_PRIVATE) ? " [private]" : ""),
2043 ((newflags & QR_GUESSNAME) ? " [hidden]" : ""),
2044 ((newflags & QR_PASSWORDED) ? " Password: " : ""),
2045 ((newflags & QR_PASSWORDED) ? new_room_pass : "")
2047 CtdlAideMessage(notification_message, "Room Creation Message");
2048 free(notification_message);
2050 cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
2055 void cmd_einf(char *ok)
2056 { /* enter info file for current room */
2058 char infofilename[SIZ];
2063 if (CtdlAccessCheck(ac_room_aide)) return;
2065 if (atoi(ok) == 0) {
2066 cprintf("%d Ok.\n", CIT_OK);
2069 assoc_file_name(infofilename, sizeof infofilename, &CC->room, ctdl_info_dir);
2070 CtdlLogPrintf(CTDL_DEBUG, "opening\n");
2071 fp = fopen(infofilename, "w");
2072 CtdlLogPrintf(CTDL_DEBUG, "checking\n");
2074 cprintf("%d Cannot open %s: %s\n",
2075 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
2078 cprintf("%d Send info...\n", SEND_LISTING);
2081 client_getln(buf, sizeof buf);
2082 if (strcmp(buf, "000"))
2083 fprintf(fp, "%s\n", buf);
2084 } while (strcmp(buf, "000"));
2087 /* now update the room index so people will see our new info */
2088 CtdlGetRoomLock(&CC->room, CC->room.QRname); /* lock so no one steps on us */
2089 CC->room.QRinfo = CC->room.QRhighest + 1L;
2090 CtdlPutRoomLock(&CC->room);
2095 * cmd_lflr() - List all known floors
2097 void cmd_lflr(char *gargs)
2102 if (CtdlAccessCheck(ac_logged_in)) return;
2104 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
2106 for (a = 0; a < MAXFLOORS; ++a) {
2107 CtdlGetFloor(&flbuf, a);
2108 if (flbuf.f_flags & F_INUSE) {
2109 cprintf("%d|%s|%d\n",
2121 * create a new floor
2123 void cmd_cflr(char *argbuf)
2125 char new_floor_name[256];
2128 int free_slot = (-1);
2131 extract_token(new_floor_name, argbuf, 0, '|', sizeof new_floor_name);
2132 cflr_ok = extract_int(argbuf, 1);
2134 if (CtdlAccessCheck(ac_aide)) return;
2136 if (IsEmptyStr(new_floor_name)) {
2137 cprintf("%d Blank floor name not allowed.\n",
2138 ERROR + ILLEGAL_VALUE);
2142 for (a = 0; a < MAXFLOORS; ++a) {
2143 CtdlGetFloor(&flbuf, a);
2145 /* note any free slots while we're scanning... */
2146 if (((flbuf.f_flags & F_INUSE) == 0)
2150 /* check to see if it already exists */
2151 if ((!strcasecmp(flbuf.f_name, new_floor_name))
2152 && (flbuf.f_flags & F_INUSE)) {
2153 cprintf("%d Floor '%s' already exists.\n",
2154 ERROR + ALREADY_EXISTS,
2160 if (free_slot < 0) {
2161 cprintf("%d There is no space available for a new floor.\n",
2162 ERROR + INVALID_FLOOR_OPERATION);
2166 cprintf("%d ok to create...\n", CIT_OK);
2169 lgetfloor(&flbuf, free_slot);
2170 flbuf.f_flags = F_INUSE;
2171 flbuf.f_ref_count = 0;
2172 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
2173 lputfloor(&flbuf, free_slot);
2174 cprintf("%d %d\n", CIT_OK, free_slot);
2182 void cmd_kflr(char *argbuf)
2185 int floor_to_delete;
2189 floor_to_delete = extract_int(argbuf, 0);
2190 kflr_ok = extract_int(argbuf, 1);
2192 if (CtdlAccessCheck(ac_aide)) return;
2194 lgetfloor(&flbuf, floor_to_delete);
2197 if ((flbuf.f_flags & F_INUSE) == 0) {
2198 cprintf("%d Floor %d not in use.\n",
2199 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
2202 if (flbuf.f_ref_count != 0) {
2203 cprintf("%d Cannot delete; floor contains %d rooms.\n",
2204 ERROR + INVALID_FLOOR_OPERATION,
2209 cprintf("%d Ok\n", CIT_OK);
2211 cprintf("%d Ok to delete...\n", CIT_OK);
2218 if ((delete_ok == 1) && (kflr_ok == 1))
2220 lputfloor(&flbuf, floor_to_delete);
2226 void cmd_eflr(char *argbuf)
2232 np = num_parms(argbuf);
2234 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
2238 if (CtdlAccessCheck(ac_aide)) return;
2240 floor_num = extract_int(argbuf, 0);
2241 lgetfloor(&flbuf, floor_num);
2242 if ((flbuf.f_flags & F_INUSE) == 0) {
2243 lputfloor(&flbuf, floor_num);
2244 cprintf("%d Floor %d is not in use.\n",
2245 ERROR + INVALID_FLOOR_OPERATION, floor_num);
2249 extract_token(flbuf.f_name, argbuf, 1, '|', sizeof flbuf.f_name);
2250 lputfloor(&flbuf, floor_num);
2252 cprintf("%d Ok\n", CIT_OK);
2256 /*****************************************************************************/
2257 /* MODULE INITIALIZATION STUFF */
2258 /*****************************************************************************/
2260 CTDL_MODULE_INIT(room_ops)
2263 CtdlRegisterProtoHook(cmd_lrms, "LRMS", "List rooms");
2264 CtdlRegisterProtoHook(cmd_lkra, "LKRA", "List all known rooms");
2265 CtdlRegisterProtoHook(cmd_lkrn, "LKRN", "List known rooms with new messages");
2266 CtdlRegisterProtoHook(cmd_lkro, "LKRO", "List known rooms without new messages");
2267 CtdlRegisterProtoHook(cmd_lzrm, "LZRM", "List zapped rooms");
2268 CtdlRegisterProtoHook(cmd_lprm, "LPRM", "Autoconverted. TODO: document me.");
2269 CtdlRegisterProtoHook(cmd_goto, "GOTO", "Goto a named room");
2270 CtdlRegisterProtoHook(cmd_whok, "WHOK", "List users who know this room");
2271 CtdlRegisterProtoHook(cmd_rdir, "RDIR", "List files in room directory");
2272 CtdlRegisterProtoHook(cmd_getr, "GETR", "Autoconverted. TODO: document me.");
2273 CtdlRegisterProtoHook(cmd_setr, "SETR", "Autoconverted. TODO: document me.");
2274 CtdlRegisterProtoHook(cmd_geta, "GETA", "Autoconverted. TODO: document me.");
2275 CtdlRegisterProtoHook(cmd_seta, "SETA", "Autoconverted. TODO: document me.");
2276 CtdlRegisterProtoHook(cmd_rinf, "RINF", "Autoconverted. TODO: document me.");
2277 CtdlRegisterProtoHook(cmd_kill, "KILL", "Autoconverted. TODO: document me.");
2278 CtdlRegisterProtoHook(cmd_cre8, "CRE8", "Create a new room");
2279 CtdlRegisterProtoHook(cmd_einf, "EINF", "Autoconverted. TODO: document me.");
2280 CtdlRegisterProtoHook(cmd_lflr, "LFLR", "Autoconverted. TODO: document me.");
2281 CtdlRegisterProtoHook(cmd_cflr, "CFLR", "Autoconverted. TODO: document me.");
2282 CtdlRegisterProtoHook(cmd_kflr, "KFLR", "Autoconverted. TODO: document me.");
2283 CtdlRegisterProtoHook(cmd_eflr, "EFLR", "Autoconverted. TODO: document me.");
2285 /* return our Subversion id for the Log */