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)
56 /* for internal programs, always do everything */
57 if (((CC->internal_pgm)) && (roombuf->QRflags & QR_INUSE)) {
58 retval = (UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED);
63 /* Locate any applicable user/room relationships */
64 CtdlGetRelationship(&vbuf, userbuf, roombuf);
66 /* Force the properties of the Aide room */
67 if (!strcasecmp(roombuf->QRname, config.c_aideroom)) {
68 if (userbuf->axlevel >= AxAideU) {
69 retval = UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
76 /* If this is a public room, it's accessible... */
77 if ( ((roombuf->QRflags & QR_PRIVATE) == 0)
78 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
79 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
82 /* If this is a preferred users only room, check access level */
83 if (roombuf->QRflags & QR_PREFONLY) {
84 if (userbuf->axlevel < AxPrefU) {
85 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
89 /* For private rooms, check the generation number matchups */
90 if ( (roombuf->QRflags & QR_PRIVATE)
91 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
93 /* An explicit match means the user belongs in this room */
94 if (vbuf.v_flags & V_ACCESS) {
95 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
97 /* Otherwise, check if this is a guess-name or passworded
98 * room. If it is, a goto may at least be attempted
100 else if ((roombuf->QRflags & QR_PRIVATE)
101 || (roombuf->QRflags & QR_PASSWORDED)) {
102 retval = retval & ~UA_KNOWN;
103 retval = retval | UA_GOTOALLOWED;
107 /* For mailbox rooms, also check the namespace */
108 /* Also, mailbox owners can delete their messages */
109 if (roombuf->QRflags & QR_MAILBOX) {
110 if (userbuf->usernum == atol(roombuf->QRname)) {
111 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
113 /* An explicit match means the user belongs in this room */
114 if (vbuf.v_flags & V_ACCESS) {
115 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
119 /* For non-mailbox rooms... */
122 /* User is allowed to post in the room unless:
123 * - User is not validated
124 * - User has no net privileges and it is a shared network room
125 * - It is a read-only room
127 int post_allowed = 1;
128 if (CC->user.axlevel < AxProbU) post_allowed = 0;
129 if ((CC->user.axlevel < AxNetU) && (CC->room.QRflags & QR_NETWORK)) post_allowed = 0;
130 if (roombuf->QRflags & QR_READONLY) post_allowed = 0;
132 retval = retval | UA_POSTALLOWED;
135 /* If "collaborative deletion" is active for this room, any user who can post
136 * is also allowed to delete
138 if (CC->room.QRflags2 & QR2_COLLABDEL) {
139 if (retval & UA_POSTALLOWED) {
140 retval = retval | UA_DELETEALLOWED;
146 /* Check to see if the user has forgotten this room */
147 if (vbuf.v_flags & V_FORGET) {
148 retval = retval & ~UA_KNOWN;
149 if ( ( ((roombuf->QRflags & QR_PRIVATE) == 0)
150 && ((roombuf->QRflags & QR_MAILBOX) == 0) )
151 || ( (roombuf->QRflags & QR_MAILBOX)
152 && (atol(roombuf->QRname) == CC->user.usernum))) {
153 retval = retval | UA_ZAPPED;
156 /* If user is explicitly locked out of this room, deny everything */
157 if (vbuf.v_flags & V_LOCKOUT) {
158 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED & ~UA_POSTALLOWED;
161 /* Aides get access to all private rooms */
162 if ( (userbuf->axlevel >= AxAideU)
163 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
164 if (vbuf.v_flags & V_FORGET) {
165 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED;
168 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED;
172 /* Aides can gain access to mailboxes as well, but they don't show
175 if ( (userbuf->axlevel >= AxAideU)
176 && (roombuf->QRflags & QR_MAILBOX) ) {
177 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED;
180 /* Aides and Room Aides have admin privileges */
181 if ( (userbuf->axlevel >= AxAideU)
182 || (userbuf->usernum == roombuf->QRroomaide)
184 retval = retval | UA_ADMINALLOWED | UA_DELETEALLOWED | UA_POSTALLOWED;
187 NEWMSG: /* By the way, we also check for the presence of new messages */
188 if (is_msg_in_sequence_set(vbuf.v_seen, roombuf->QRhighest) == 0) {
189 retval = retval | UA_HASNEWMSGS;
192 /* System rooms never show up in the list. */
193 if (roombuf->QRflags2 & QR2_SYSTEM) {
194 retval = retval & ~UA_KNOWN;
198 /* Now give the caller the information it wants. */
199 if (result != NULL) *result = retval;
200 if (view != NULL) *view = vbuf.v_view;
204 * Self-checking stuff for a room record read into memory
206 void room_sanity_check(struct ctdlroom *qrbuf)
208 /* Mailbox rooms are always on the lowest floor */
209 if (qrbuf->QRflags & QR_MAILBOX) {
212 /* Listing order of 0 is illegal except for base rooms */
213 if (qrbuf->QRorder == 0)
214 if (!(qrbuf->QRflags & QR_MAILBOX) &&
215 strncasecmp(qrbuf->QRname, config.c_baseroom, ROOMNAMELEN)
217 strncasecmp(qrbuf->QRname, config.c_aideroom, ROOMNAMELEN))
223 * CtdlGetRoom() - retrieve room data from disk
225 int CtdlGetRoom(struct ctdlroom *qrbuf, char *room_name)
227 struct cdbdata *cdbqr;
228 char lowercase_name[ROOMNAMELEN];
229 char personal_lowercase_name[ROOMNAMELEN];
230 char *dptr, *sptr, *eptr;
232 dptr = lowercase_name;
234 eptr = (dptr + (sizeof lowercase_name - 1));
235 while (!IsEmptyStr(sptr) && (dptr < eptr)){
236 *dptr = tolower(*sptr);
241 memset(qrbuf, 0, sizeof(struct ctdlroom));
243 /* First, try the public namespace */
244 cdbqr = cdb_fetch(CDB_ROOMS,
245 lowercase_name, strlen(lowercase_name));
247 /* If that didn't work, try the user's personal namespace */
249 snprintf(personal_lowercase_name,
250 sizeof personal_lowercase_name, "%010ld.%s",
251 CC->user.usernum, lowercase_name);
252 cdbqr = cdb_fetch(CDB_ROOMS,
253 personal_lowercase_name,
254 strlen(personal_lowercase_name));
257 memcpy(qrbuf, cdbqr->ptr,
258 ((cdbqr->len > sizeof(struct ctdlroom)) ?
259 sizeof(struct ctdlroom) : cdbqr->len));
262 room_sanity_check(qrbuf);
271 * CtdlGetRoomLock() - same as getroom() but locks the record (if supported)
273 int CtdlGetRoomLock(struct ctdlroom *qrbuf, char *room_name)
276 retval = CtdlGetRoom(qrbuf, room_name);
277 if (retval == 0) begin_critical_section(S_ROOMS);
283 * b_putroom() - back end to putroom() and b_deleteroom()
284 * (if the supplied buffer is NULL, delete the room record)
286 void b_putroom(struct ctdlroom *qrbuf, char *room_name)
288 char lowercase_name[ROOMNAMELEN];
293 bptr = lowercase_name;
294 while (!IsEmptyStr(aptr))
296 *bptr = tolower(*aptr);
302 len = bptr - lowercase_name;
304 cdb_delete(CDB_ROOMS,
305 lowercase_name, len);
307 time(&qrbuf->QRmtime);
310 qrbuf, sizeof(struct ctdlroom));
316 * CtdlPutRoom() - store room data to disk
318 void CtdlPutRoom(struct ctdlroom *qrbuf) {
319 b_putroom(qrbuf, qrbuf->QRname);
324 * b_deleteroom() - delete a room record from disk
326 void b_deleteroom(char *room_name) {
327 b_putroom(NULL, room_name);
333 * CtdlPutRoomLock() - same as CtdlPutRoom() but unlocks the record (if supported)
335 void CtdlPutRoomLock(struct ctdlroom *qrbuf)
339 end_critical_section(S_ROOMS);
343 /****************************************************************************/
347 * CtdlGetFloorByName() - retrieve the number of the named floor
348 * return < 0 if not found else return floor number
350 int CtdlGetFloorByName(const char *floor_name)
353 struct floor *flbuf = NULL;
355 for (a = 0; a < MAXFLOORS; ++a) {
356 flbuf = CtdlGetCachedFloor(a);
358 /* check to see if it already exists */
359 if ((!strcasecmp(flbuf->f_name, floor_name))
360 && (flbuf->f_flags & F_INUSE)) {
370 * CtdlGetFloorByNameLock() - retrieve floor number for given floor and lock the floor list.
372 int CtdlGetFloorByNameLock(const char *floor_name)
374 begin_critical_section(S_FLOORTAB);
375 return CtdlGetFloorByName(floor_name);
381 * CtdlGetAvailableFloor() - Return number of first unused floor
382 * return < 0 if none available
384 int CtdlGetAvailableFloor(void)
387 struct floor *flbuf = NULL;
389 for (a = 0; a < MAXFLOORS; a++) {
390 flbuf = CtdlGetCachedFloor(a);
392 /* check to see if it already exists */
393 if ((flbuf->f_flags & F_INUSE) == 0) {
402 * CtdlGetFloor() - retrieve floor data from disk
404 void CtdlGetFloor(struct floor *flbuf, int floor_num)
406 struct cdbdata *cdbfl;
408 memset(flbuf, 0, sizeof(struct floor));
409 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
411 memcpy(flbuf, cdbfl->ptr,
412 ((cdbfl->len > sizeof(struct floor)) ?
413 sizeof(struct floor) : cdbfl->len));
416 if (floor_num == 0) {
417 safestrncpy(flbuf->f_name, "Main Floor",
418 sizeof flbuf->f_name);
419 flbuf->f_flags = F_INUSE;
420 flbuf->f_ref_count = 3;
427 * lgetfloor() - same as CtdlGetFloor() but locks the record (if supported)
429 void lgetfloor(struct floor *flbuf, int floor_num)
432 begin_critical_section(S_FLOORTAB);
433 CtdlGetFloor(flbuf, floor_num);
438 * CtdlGetCachedFloor() - Get floor record from *cache* (loads from disk if needed)
440 * This is strictly a performance hack.
442 struct floor *CtdlGetCachedFloor(int floor_num) {
443 static int initialized = 0;
446 struct floor *fl = NULL;
448 begin_critical_section(S_FLOORCACHE);
449 if (initialized == 0) {
450 for (i=0; i<MAXFLOORS; ++i) {
451 floorcache[floor_num] = NULL;
455 if (floorcache[floor_num] == NULL) {
458 end_critical_section(S_FLOORCACHE);
461 fl = malloc(sizeof(struct floor));
462 CtdlGetFloor(fl, floor_num);
463 begin_critical_section(S_FLOORCACHE);
464 if (floorcache[floor_num] != NULL) {
465 free(floorcache[floor_num]);
467 floorcache[floor_num] = fl;
468 end_critical_section(S_FLOORCACHE);
471 return(floorcache[floor_num]);
477 * CtdlPutFloor() - store floor data on disk
479 void CtdlPutFloor(struct floor *flbuf, int floor_num)
481 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
482 begin_critical_section(S_FLOORCACHE);
483 if (floorcache[floor_num] != NULL) {
484 free(floorcache[floor_num]);
485 floorcache[floor_num] = malloc(sizeof(struct floor));
486 memcpy(floorcache[floor_num], flbuf, sizeof(struct floor));
488 end_critical_section(S_FLOORCACHE);
490 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
491 flbuf, sizeof(struct floor));
497 * CtdlPutFloorLock() - same as CtdlPutFloor() but unlocks the record (if supported)
499 void CtdlPutFloorLock(struct floor *flbuf, int floor_num)
502 CtdlPutFloor(flbuf, floor_num);
503 end_critical_section(S_FLOORTAB);
510 * lputfloor() - same as CtdlPutFloor() but unlocks the record (if supported)
512 void lputfloor(struct floor *flbuf, int floor_num)
514 CtdlPutFloorLock(flbuf, floor_num);
519 * Traverse the room file...
521 void CtdlForEachRoom(void (*CallBack) (struct ctdlroom *EachRoom, void *out_data),
524 struct ctdlroom qrbuf;
525 struct cdbdata *cdbqr;
527 cdb_rewind(CDB_ROOMS);
529 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
530 memset(&qrbuf, 0, sizeof(struct ctdlroom));
531 memcpy(&qrbuf, cdbqr->ptr,
532 ((cdbqr->len > sizeof(struct ctdlroom)) ?
533 sizeof(struct ctdlroom) : cdbqr->len));
535 room_sanity_check(&qrbuf);
536 if (qrbuf.QRflags & QR_INUSE)
537 (*CallBack)(&qrbuf, in_data);
543 * delete_msglist() - delete room message pointers
545 void delete_msglist(struct ctdlroom *whichroom)
547 struct cdbdata *cdbml;
549 /* Make sure the msglist we're deleting actually exists, otherwise
550 * gdbm will complain when we try to delete an invalid record
552 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
556 /* Go ahead and delete it */
557 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
564 * Message pointer compare function for sort_msglist()
566 int sort_msglist_cmp(const void *m1, const void *m2) {
567 if ((*(const long *)m1) > (*(const long *)m2)) return(1);
568 if ((*(const long *)m1) < (*(const long *)m2)) return(-1);
574 * sort message pointers
575 * (returns new msg count)
577 int sort_msglist(long listptrs[], int oldcount)
587 qsort(listptrs, numitems, sizeof(long), sort_msglist_cmp);
589 /* and yank any nulls */
590 while ((numitems > 0) && (listptrs[0] == 0L)) {
591 memmove(&listptrs[0], &listptrs[1],
592 (sizeof(long) * (numitems - 1)));
601 * Determine whether a given room is non-editable.
603 int CtdlIsNonEditable(struct ctdlroom *qrbuf)
606 /* Mail> rooms are non-editable */
607 if ( (qrbuf->QRflags & QR_MAILBOX)
608 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
611 /* Everything else is editable */
618 * Back-back-end for all room listing commands
620 void list_roomname(struct ctdlroom *qrbuf, int ra, int current_view, int default_view)
622 char truncated_roomname[ROOMNAMELEN];
624 /* For my own mailbox rooms, chop off the owner prefix */
625 if ( (qrbuf->QRflags & QR_MAILBOX)
626 && (atol(qrbuf->QRname) == CC->user.usernum) ) {
627 safestrncpy(truncated_roomname, qrbuf->QRname, sizeof truncated_roomname);
628 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
629 cprintf("%s", truncated_roomname);
631 /* For all other rooms, just display the name in its entirety */
633 cprintf("%s", qrbuf->QRname);
636 /* ...and now the other parameters */
637 cprintf("|%u|%d|%d|%d|%d|%d|%d|%ld|\n",
639 (int) qrbuf->QRfloor,
640 (int) qrbuf->QRorder,
641 (int) qrbuf->QRflags2,
651 * cmd_lrms() - List all accessible rooms, known or forgotten
653 void cmd_lrms_backend(struct ctdlroom *qrbuf, void *data)
655 int FloorBeingSearched = (-1);
659 FloorBeingSearched = *(int *)data;
660 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
662 if ((( ra & (UA_KNOWN | UA_ZAPPED)))
663 && ((qrbuf->QRfloor == (FloorBeingSearched))
664 || ((FloorBeingSearched) < 0)))
665 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
668 void cmd_lrms(char *argbuf)
670 int FloorBeingSearched = (-1);
671 if (!IsEmptyStr(argbuf))
672 FloorBeingSearched = extract_int(argbuf, 0);
674 if (CtdlAccessCheck(ac_logged_in)) return;
676 if (CtdlGetUser(&CC->user, CC->curr_user)) {
677 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
680 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
682 CtdlForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
689 * cmd_lkra() - List all known rooms
691 void cmd_lkra_backend(struct ctdlroom *qrbuf, void *data)
693 int FloorBeingSearched = (-1);
697 FloorBeingSearched = *(int *)data;
698 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
700 if ((( ra & (UA_KNOWN)))
701 && ((qrbuf->QRfloor == (FloorBeingSearched))
702 || ((FloorBeingSearched) < 0)))
703 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
706 void cmd_lkra(char *argbuf)
708 int FloorBeingSearched = (-1);
709 if (!IsEmptyStr(argbuf))
710 FloorBeingSearched = extract_int(argbuf, 0);
712 if (CtdlAccessCheck(ac_logged_in)) return;
714 if (CtdlGetUser(&CC->user, CC->curr_user)) {
715 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
718 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
720 CtdlForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
726 void cmd_lprm_backend(struct ctdlroom *qrbuf, void *data)
728 int FloorBeingSearched = (-1);
732 FloorBeingSearched = *(int *)data;
733 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
735 if ( ((qrbuf->QRflags & QR_PRIVATE) == 0)
736 && ((qrbuf->QRflags & QR_MAILBOX) == 0)
737 && ((qrbuf->QRfloor == (FloorBeingSearched))
738 || ((FloorBeingSearched) < 0)))
739 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
742 void cmd_lprm(char *argbuf)
744 int FloorBeingSearched = (-1);
745 if (!IsEmptyStr(argbuf))
746 FloorBeingSearched = extract_int(argbuf, 0);
748 cprintf("%d Publiic rooms:\n", LISTING_FOLLOWS);
750 CtdlForEachRoom(cmd_lprm_backend, &FloorBeingSearched);
757 * cmd_lkrn() - List all known rooms with new messages
759 void cmd_lkrn_backend(struct ctdlroom *qrbuf, void *data)
761 int FloorBeingSearched = (-1);
765 FloorBeingSearched = *(int *)data;
766 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
769 && (ra & UA_HASNEWMSGS)
770 && ((qrbuf->QRfloor == (FloorBeingSearched))
771 || ((FloorBeingSearched) < 0)))
772 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
775 void cmd_lkrn(char *argbuf)
777 int FloorBeingSearched = (-1);
778 if (!IsEmptyStr(argbuf))
779 FloorBeingSearched = extract_int(argbuf, 0);
781 if (CtdlAccessCheck(ac_logged_in)) return;
783 if (CtdlGetUser(&CC->user, CC->curr_user)) {
784 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
787 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
789 CtdlForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
796 * cmd_lkro() - List all known rooms
798 void cmd_lkro_backend(struct ctdlroom *qrbuf, void *data)
800 int FloorBeingSearched = (-1);
804 FloorBeingSearched = *(int *)data;
805 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
808 && ((ra & UA_HASNEWMSGS) == 0)
809 && ((qrbuf->QRfloor == (FloorBeingSearched))
810 || ((FloorBeingSearched) < 0)))
811 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
814 void cmd_lkro(char *argbuf)
816 int FloorBeingSearched = (-1);
817 if (!IsEmptyStr(argbuf))
818 FloorBeingSearched = extract_int(argbuf, 0);
820 if (CtdlAccessCheck(ac_logged_in)) return;
822 if (CtdlGetUser(&CC->user, CC->curr_user)) {
823 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
826 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
828 CtdlForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
835 * cmd_lzrm() - List all forgotten rooms
837 void cmd_lzrm_backend(struct ctdlroom *qrbuf, void *data)
839 int FloorBeingSearched = (-1);
843 FloorBeingSearched = *(int *)data;
844 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
846 if ((ra & UA_GOTOALLOWED)
848 && ((qrbuf->QRfloor == (FloorBeingSearched))
849 || ((FloorBeingSearched) < 0)))
850 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
853 void cmd_lzrm(char *argbuf)
855 int FloorBeingSearched = (-1);
856 if (!IsEmptyStr(argbuf))
857 FloorBeingSearched = extract_int(argbuf, 0);
859 if (CtdlAccessCheck(ac_logged_in)) return;
861 if (CtdlGetUser(&CC->user, CC->curr_user)) {
862 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
865 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
867 CtdlForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
873 * Make the specified room the current room for this session. No validation
874 * or access control is done here -- the caller should make sure that the
875 * specified room exists and is ok to access.
877 void CtdlUserGoto(char *where, int display_result, int transiently,
878 int *retmsgs, int *retnew)
881 int new_messages = 0;
882 int old_messages = 0;
883 int total_messages = 0;
887 int newmailcount = 0;
889 char truncated_roomname[ROOMNAMELEN];
890 struct cdbdata *cdbfr;
891 long *msglist = NULL;
893 unsigned int original_v_flags;
896 char setstr[128], lostr[64], histr[64];
900 /* If the supplied room name is NULL, the caller wants us to know that
901 * it has already copied the room record into CC->room, so
902 * we can skip the extra database fetch.
905 safestrncpy(CC->room.QRname, where, sizeof CC->room.QRname);
906 CtdlGetRoom(&CC->room, where);
909 /* Take care of all the formalities. */
911 begin_critical_section(S_USERS);
912 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
913 original_v_flags = vbuf.v_flags;
915 /* Know the room ... but not if it's the page log room, or if the
916 * caller specified that we're only entering this room transiently.
918 if ((strcasecmp(CC->room.QRname, config.c_logpages))
919 && (transiently == 0) ) {
920 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
921 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
924 /* Only rewrite the database record if we changed something */
925 if (vbuf.v_flags != original_v_flags) {
926 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
928 end_critical_section(S_USERS);
930 /* Check for new mail */
931 newmailcount = NewMailCount();
933 /* set info to 1 if the user needs to read the room's info file */
934 if (CC->room.QRinfo > vbuf.v_lastseen) {
938 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
940 msglist = (long *) cdbfr->ptr;
941 cdbfr->ptr = NULL; /* CtdlUserGoto() now owns this memory */
942 num_msgs = cdbfr->len / sizeof(long);
947 for (a=0; a<num_msgs; ++a) {
948 if (msglist[a] > 0L) ++total_messages;
950 new_messages = num_msgs;
951 num_sets = num_tokens(vbuf.v_seen, ',');
952 for (s=0; s<num_sets; ++s) {
953 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
955 extract_token(lostr, setstr, 0, ':', sizeof lostr);
956 if (num_tokens(setstr, ':') >= 2) {
957 extract_token(histr, setstr, 1, ':', sizeof histr);
958 if (!strcmp(histr, "*")) {
959 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
963 strcpy(histr, lostr);
968 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
969 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
975 new_messages = total_messages - old_messages;
977 if (msglist != NULL) free(msglist);
979 if (CC->room.QRflags & QR_MAILBOX)
984 if ((CC->room.QRroomaide == CC->user.usernum)
985 || (CC->user.axlevel >= AxAideU))
990 safestrncpy(truncated_roomname, CC->room.QRname, sizeof truncated_roomname);
991 if ( (CC->room.QRflags & QR_MAILBOX)
992 && (atol(CC->room.QRname) == CC->user.usernum) ) {
993 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
996 if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
1000 if (retmsgs != NULL) *retmsgs = total_messages;
1001 if (retnew != NULL) *retnew = new_messages;
1002 CtdlLogPrintf(CTDL_DEBUG, "<%s> %d new of %d total messages\n",
1004 new_messages, total_messages
1007 CC->curr_view = (int)vbuf.v_view;
1009 if (display_result) {
1010 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|%d|\n",
1011 CIT_OK, CtdlCheckExpress(),
1014 (int)total_messages,
1016 (int)CC->room.QRflags,
1017 (long)CC->room.QRhighest,
1018 (long)vbuf.v_lastseen,
1022 (int)CC->room.QRfloor,
1024 (int)CC->room.QRdefaultview,
1026 (int)CC->room.QRflags2
1033 * Handle some of the macro named rooms
1035 void convert_room_name_macros(char *towhere, size_t maxlen) {
1036 if (!strcasecmp(towhere, "_BASEROOM_")) {
1037 safestrncpy(towhere, config.c_baseroom, maxlen);
1039 else if (!strcasecmp(towhere, "_MAIL_")) {
1040 safestrncpy(towhere, MAILROOM, maxlen);
1042 else if (!strcasecmp(towhere, "_TRASH_")) {
1043 safestrncpy(towhere, USERTRASHROOM, maxlen);
1045 else if (!strcasecmp(towhere, "_DRAFTS_")) {
1046 safestrncpy(towhere, USERDRAFTROOM, maxlen);
1048 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
1049 safestrncpy(towhere, config.c_twitroom, maxlen);
1051 else if (!strcasecmp(towhere, "_CALENDAR_")) {
1052 safestrncpy(towhere, USERCALENDARROOM, maxlen);
1054 else if (!strcasecmp(towhere, "_TASKS_")) {
1055 safestrncpy(towhere, USERTASKSROOM, maxlen);
1057 else if (!strcasecmp(towhere, "_CONTACTS_")) {
1058 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
1060 else if (!strcasecmp(towhere, "_NOTES_")) {
1061 safestrncpy(towhere, USERNOTESROOM, maxlen);
1067 * cmd_goto() - goto a new room
1069 void cmd_goto(char *gargs)
1071 struct ctdlroom QRscratch;
1075 char augmented_roomname[ROOMNAMELEN];
1076 char towhere[ROOMNAMELEN];
1078 int transiently = 0;
1080 if (CtdlAccessCheck(ac_logged_in)) return;
1082 extract_token(towhere, gargs, 0, '|', sizeof towhere);
1083 extract_token(password, gargs, 1, '|', sizeof password);
1084 transiently = extract_int(gargs, 2);
1086 CtdlGetUser(&CC->user, CC->curr_user);
1089 * Handle some of the macro named rooms
1091 convert_room_name_macros(towhere, sizeof towhere);
1093 /* First try a regular match */
1094 c = CtdlGetRoom(&QRscratch, towhere);
1096 /* Then try a mailbox name match */
1098 CtdlMailboxName(augmented_roomname, sizeof augmented_roomname,
1099 &CC->user, towhere);
1100 c = CtdlGetRoom(&QRscratch, augmented_roomname);
1102 safestrncpy(towhere, augmented_roomname, sizeof towhere);
1105 /* And if the room was found... */
1108 /* Let internal programs go directly to any room. */
1109 if (CC->internal_pgm) {
1110 memcpy(&CC->room, &QRscratch,
1111 sizeof(struct ctdlroom));
1112 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1116 /* See if there is an existing user/room relationship */
1117 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
1119 /* normal clients have to pass through security */
1120 if (ra & UA_GOTOALLOWED) {
1125 if ((QRscratch.QRflags & QR_MAILBOX) &&
1126 ((ra & UA_GOTOALLOWED))) {
1127 memcpy(&CC->room, &QRscratch,
1128 sizeof(struct ctdlroom));
1129 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1131 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
1132 ((ra & UA_KNOWN) == 0) &&
1133 (strcasecmp(QRscratch.QRpasswd, password)) &&
1134 (CC->user.axlevel < AxAideU)
1136 cprintf("%d wrong or missing passwd\n",
1137 ERROR + PASSWORD_REQUIRED);
1139 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
1140 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
1141 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
1142 ((ra & UA_KNOWN) == 0) &&
1143 (CC->user.axlevel < AxAideU)
1145 CtdlLogPrintf(CTDL_DEBUG, "Failed to acquire private room\n");
1147 memcpy(&CC->room, &QRscratch,
1148 sizeof(struct ctdlroom));
1149 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1155 cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
1159 void cmd_whok(char *cmdbuf)
1161 struct ctdluser temp;
1162 struct cdbdata *cdbus;
1165 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
1166 cdb_rewind(CDB_USERS);
1167 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1168 memset(&temp, 0, sizeof temp);
1169 memcpy(&temp, cdbus->ptr, sizeof temp);
1172 CtdlRoomAccess(&CC->room, &temp, &ra, NULL);
1173 if ((CC->room.QRflags & QR_INUSE)
1176 cprintf("%s\n", temp.fullname);
1183 * RDIR command for room directory
1185 void cmd_rdir(char *cmdbuf)
1190 struct stat statbuf;
1191 DIR *filedir = NULL;
1192 struct dirent *filedir_entry;
1198 if (CtdlAccessCheck(ac_logged_in)) return;
1200 CtdlGetRoom(&CC->room, CC->room.QRname);
1201 CtdlGetUser(&CC->user, CC->curr_user);
1203 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
1204 cprintf("%d not here.\n", ERROR + NOT_HERE);
1207 if (((CC->room.QRflags & QR_VISDIR) == 0)
1208 && (CC->user.axlevel < AxAideU)
1209 && (CC->user.usernum != CC->room.QRroomaide)) {
1210 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1214 snprintf(buf, sizeof buf, "%s/%s", ctdl_file_dir, CC->room.QRdirname);
1215 filedir = opendir (buf);
1217 if (filedir == NULL) {
1218 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1221 cprintf("%d %s|%s/%s\n", LISTING_FOLLOWS, config.c_fqdn, ctdl_file_dir, CC->room.QRdirname);
1223 snprintf(buf, sizeof buf, "%s/%s/filedir", ctdl_file_dir, CC->room.QRdirname);
1224 fd = fopen(buf, "r");
1226 fd = fopen("/dev/null", "r");
1227 while ((filedir_entry = readdir(filedir)))
1229 if (strcasecmp(filedir_entry->d_name, "filedir") && filedir_entry->d_name[0] != '.')
1231 #ifdef _DIRENT_HAVE_D_NAMELEN
1232 d_namelen = filedir_entry->d_namelen;
1234 d_namelen = strlen(filedir_entry->d_name);
1236 snprintf(buf, sizeof buf, "%s/%s/%s", ctdl_file_dir, CC->room.QRdirname, filedir_entry->d_name);
1237 stat(buf, &statbuf); /* stat the file */
1238 if (!(statbuf.st_mode & S_IFREG))
1240 snprintf(buf2, sizeof buf2,
1241 "\"%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",
1242 buf, CC->room.QRname
1244 CtdlAideMessage(buf2, "Unusable data found in room directory");
1245 continue; /* not a useable file type so don't show it */
1247 safestrncpy(comment, "", sizeof comment);
1248 fseek(fd, 0L, 0); /* rewind descriptions file */
1249 /* Get the description from the descriptions file */
1250 while ((fgets(buf, sizeof buf, fd) != NULL) && (IsEmptyStr(comment)))
1252 buf[strlen(buf) - 1] = 0;
1253 if ((!strncasecmp(buf, filedir_entry->d_name, d_namelen)) && (buf[d_namelen] == ' '))
1254 safestrncpy(comment, &buf[d_namelen + 1], sizeof comment);
1256 len = extract_token (mimebuf, comment, 0,' ', 64);
1257 if ((len <0) || strchr(mimebuf, '/') == NULL)
1259 snprintf (mimebuf, 64, "application/octetstream");
1262 cprintf("%s|%ld|%s|%s\n",
1263 filedir_entry->d_name,
1264 (long)statbuf.st_size,
1276 * get room parameters (aide or room aide command)
1278 void cmd_getr(char *cmdbuf)
1280 if (CtdlAccessCheck(ac_room_aide)) return;
1282 CtdlGetRoom(&CC->room, CC->room.QRname);
1283 cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1287 ((CC->room.QRflags & QR_MAILBOX) ?
1288 &CC->room.QRname[11] : CC->room.QRname),
1290 ((CC->room.QRflags & QR_PASSWORDED) ?
1291 CC->room.QRpasswd : ""),
1293 ((CC->room.QRflags & QR_DIRECTORY) ?
1294 CC->room.QRdirname : ""),
1297 (int) CC->room.QRfloor,
1298 (int) CC->room.QRorder,
1300 CC->room.QRdefaultview,
1307 * Back end function to rename a room.
1308 * You can also specify which floor to move the room to, or specify -1 to
1309 * keep the room on the same floor it was on.
1311 * If you are renaming a mailbox room, you must supply the namespace prefix
1312 * in *at least* the old name!
1314 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1316 struct ctdlroom qrbuf;
1317 struct ctdlroom qrtmp;
1322 char actual_old_name[ROOMNAMELEN];
1324 CtdlLogPrintf(CTDL_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1325 old_name, new_name, new_floor);
1327 if (new_floor >= 0) {
1328 fl = CtdlGetCachedFloor(new_floor);
1329 if ((fl->f_flags & F_INUSE) == 0) {
1330 return(crr_invalid_floor);
1334 begin_critical_section(S_ROOMS);
1336 if ( (CtdlGetRoom(&qrtmp, new_name) == 0)
1337 && (strcasecmp(new_name, old_name)) ) {
1338 ret = crr_already_exists;
1341 else if (CtdlGetRoom(&qrbuf, old_name) != 0) {
1342 ret = crr_room_not_found;
1345 else if ( (CC->user.axlevel < AxAideU) && (!CC->internal_pgm)
1346 && (CC->user.usernum != qrbuf.QRroomaide)
1347 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) ) {
1348 ret = crr_access_denied;
1351 else if (CtdlIsNonEditable(&qrbuf)) {
1352 ret = crr_noneditable;
1357 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1358 if (qrbuf.QRflags & QR_MAILBOX) {
1359 owner = atol(qrbuf.QRname);
1361 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1362 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1363 "%010ld.%s", owner, new_name);
1366 safestrncpy(qrbuf.QRname, new_name,
1367 sizeof(qrbuf.QRname));
1370 /* Reject change of floor for baseroom/aideroom */
1371 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1372 !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1376 /* Take care of floor stuff */
1377 old_floor = qrbuf.QRfloor;
1378 if (new_floor < 0) {
1379 new_floor = old_floor;
1381 qrbuf.QRfloor = new_floor;
1382 CtdlPutRoom(&qrbuf);
1384 begin_critical_section(S_CONFIG);
1386 /* If baseroom/aideroom name changes, update config */
1387 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1388 safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1391 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1392 safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1396 end_critical_section(S_CONFIG);
1398 /* If the room name changed, then there are now two room
1399 * records, so we have to delete the old one.
1401 if (strcasecmp(new_name, old_name)) {
1402 b_deleteroom(actual_old_name);
1408 end_critical_section(S_ROOMS);
1410 /* Adjust the floor reference counts if necessary */
1411 if (new_floor != old_floor) {
1412 lgetfloor(&flbuf, old_floor);
1413 --flbuf.f_ref_count;
1414 lputfloor(&flbuf, old_floor);
1415 CtdlLogPrintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1416 lgetfloor(&flbuf, new_floor);
1417 ++flbuf.f_ref_count;
1418 lputfloor(&flbuf, new_floor);
1419 CtdlLogPrintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1422 /* ...and everybody say "YATTA!" */
1428 * set room parameters (aide or room aide command)
1430 void cmd_setr(char *args)
1436 char new_name[ROOMNAMELEN];
1438 if (CtdlAccessCheck(ac_logged_in)) return;
1440 if (num_parms(args) >= 6) {
1441 new_floor = extract_int(args, 5);
1443 new_floor = (-1); /* don't change the floor */
1446 /* When is a new name more than just a new name? When the old name
1447 * has a namespace prefix.
1449 if (CC->room.QRflags & QR_MAILBOX) {
1450 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
1452 safestrncpy(new_name, "", sizeof new_name);
1454 extract_token(&new_name[strlen(new_name)], args, 0, '|', (sizeof new_name - strlen(new_name)));
1456 r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
1458 if (r == crr_room_not_found) {
1459 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
1460 } else if (r == crr_already_exists) {
1461 cprintf("%d '%s' already exists.\n",
1462 ERROR + ALREADY_EXISTS, new_name);
1463 } else if (r == crr_noneditable) {
1464 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
1465 } else if (r == crr_invalid_floor) {
1466 cprintf("%d Target floor does not exist.\n",
1467 ERROR + INVALID_FLOOR_OPERATION);
1468 } else if (r == crr_access_denied) {
1469 cprintf("%d You do not have permission to edit '%s'\n",
1470 ERROR + HIGHER_ACCESS_REQUIRED,
1472 } else if (r != crr_ok) {
1473 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1474 ERROR + INTERNAL_ERROR, r);
1481 CtdlGetRoom(&CC->room, new_name);
1483 /* Now we have to do a bunch of other stuff */
1485 if (num_parms(args) >= 7) {
1486 new_order = extract_int(args, 6);
1489 if (new_order > 127)
1493 CtdlGetRoomLock(&CC->room, CC->room.QRname);
1495 /* Directory room */
1496 extract_token(buf, args, 2, '|', sizeof buf);
1498 safestrncpy(CC->room.QRdirname, buf,
1499 sizeof CC->room.QRdirname);
1502 if (num_parms(args) >= 8) {
1503 CC->room.QRdefaultview = extract_int(args, 7);
1506 /* Second set of flags */
1507 if (num_parms(args) >= 9) {
1508 CC->room.QRflags2 = extract_int(args, 8);
1512 CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
1513 /* Clean up a client boo-boo: if the client set the room to
1514 * guess-name or passworded, ensure that the private flag is
1517 if ((CC->room.QRflags & QR_GUESSNAME)
1518 || (CC->room.QRflags & QR_PASSWORDED))
1519 CC->room.QRflags |= QR_PRIVATE;
1521 /* Some changes can't apply to BASEROOM */
1522 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1524 CC->room.QRorder = 0;
1525 CC->room.QRpasswd[0] = '\0';
1526 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1527 QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1528 CC->room.QRflags |= QR_PERMANENT;
1530 /* March order (doesn't apply to AIDEROOM) */
1531 if (num_parms(args) >= 7)
1532 CC->room.QRorder = (char) new_order;
1534 extract_token(buf, args, 1, '|', sizeof buf);
1536 safestrncpy(CC->room.QRpasswd, buf,
1537 sizeof CC->room.QRpasswd);
1538 /* Kick everyone out if the client requested it
1539 * (by changing the room's generation number)
1541 if (extract_int(args, 4)) {
1542 time(&CC->room.QRgen);
1545 /* Some changes can't apply to AIDEROOM */
1546 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1548 CC->room.QRorder = 0;
1549 CC->room.QRflags &= ~QR_MAILBOX;
1550 CC->room.QRflags |= QR_PERMANENT;
1553 /* Write the room record back to disk */
1554 CtdlPutRoomLock(&CC->room);
1556 /* Create a room directory if necessary */
1557 if (CC->room.QRflags & QR_DIRECTORY) {
1558 snprintf(buf, sizeof buf,"%s/%s",
1560 CC->room.QRdirname);
1563 snprintf(buf, sizeof buf, "The room \"%s\" has been edited by %s.\n",
1564 CC->room.QRname, CC->curr_user);
1565 CtdlAideMessage(buf, "Room modification Message");
1566 cprintf("%d Ok\n", CIT_OK);
1572 * get the name of the room aide for this room
1574 void cmd_geta(char *cmdbuf)
1576 struct ctdluser usbuf;
1578 if (CtdlAccessCheck(ac_logged_in)) return;
1580 if (CtdlGetUserByNumber(&usbuf, CC->room.QRroomaide) == 0) {
1581 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1583 cprintf("%d \n", CIT_OK);
1589 * set the room aide for this room
1591 void cmd_seta(char *new_ra)
1593 struct ctdluser usbuf;
1598 if (CtdlAccessCheck(ac_room_aide)) return;
1600 if (CtdlGetUser(&usbuf, new_ra) != 0) {
1603 newu = usbuf.usernum;
1606 CtdlGetRoomLock(&CC->room, CC->room.QRname);
1608 if (CC->room.QRroomaide != newu) {
1611 CC->room.QRroomaide = newu;
1612 CtdlPutRoomLock(&CC->room);
1615 * We have to post the change notice _after_ writing changes to
1616 * the room table, otherwise it would deadlock!
1618 if (post_notice == 1) {
1619 if (!IsEmptyStr(usbuf.fullname))
1620 snprintf(buf, sizeof buf,
1621 "%s is now the room aide for \"%s\".\n",
1622 usbuf.fullname, CC->room.QRname);
1624 snprintf(buf, sizeof buf,
1625 "There is now no room aide for \"%s\".\n",
1627 CtdlAideMessage(buf, "Aide Room Modification");
1629 cprintf("%d Ok\n", CIT_OK);
1633 * retrieve info file for this room
1635 void cmd_rinf(char *gargs)
1641 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_info_dir);
1642 info_fp = fopen(filename, "r");
1644 if (info_fp == NULL) {
1645 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
1648 cprintf("%d Info:\n", LISTING_FOLLOWS);
1649 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1650 if (!IsEmptyStr(buf))
1651 buf[strlen(buf) - 1] = 0;
1652 cprintf("%s\n", buf);
1659 * Asynchronously schedule a room for deletion. The room will appear
1660 * deleted to the user(s), but it won't actually get purged from the
1661 * database until THE DREADED AUTO-PURGER makes its next run.
1663 void CtdlScheduleRoomForDeletion(struct ctdlroom *qrbuf)
1665 char old_name[ROOMNAMELEN];
1668 CtdlLogPrintf(CTDL_NOTICE, "Scheduling room <%s> for deletion\n",
1671 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1673 CtdlGetRoom(qrbuf, qrbuf->QRname);
1675 /* Turn the room into a private mailbox owned by a user who doesn't
1676 * exist. This will immediately make the room invisible to everyone,
1677 * and qualify the room for purging.
1679 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1684 qrbuf->QRflags |= QR_MAILBOX;
1685 time(&qrbuf->QRgen); /* Use a timestamp as the new generation number */
1689 b_deleteroom(old_name);
1695 * Back end processing to delete a room and everything associated with it
1696 * (This one is synchronous and should only get called by THE DREADED
1697 * AUTO-PURGER in serv_expire.c. All user-facing code should call
1698 * the asynchronous schedule_room_for_deletion() instead.)
1700 void CtdlDeleteRoom(struct ctdlroom *qrbuf)
1704 /* TODO: filename magic? does this realy work? */
1706 CtdlLogPrintf(CTDL_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1708 /* Delete the info file */
1709 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_info_dir);
1712 /* Delete the image file */
1713 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_image_dir);
1716 /* Delete the room's network config file */
1717 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1720 /* Delete the messages in the room
1721 * (Careful: this opens an S_ROOMS critical section!)
1723 CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
1725 /* Flag the room record as not in use */
1726 CtdlGetRoomLock(qrbuf, qrbuf->QRname);
1728 CtdlPutRoomLock(qrbuf);
1730 /* then decrement the reference count for the floor */
1731 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1732 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1733 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1735 /* Delete the room record from the database! */
1736 b_deleteroom(qrbuf->QRname);
1742 * Check access control for deleting a room
1744 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1746 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1750 if (CtdlIsNonEditable(qr)) {
1755 * For mailboxes, check stuff
1757 if (qr->QRflags & QR_MAILBOX) {
1759 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1761 if (atol(qr->QRname) != CC->user.usernum) {
1762 return(0); /* not my room */
1765 /* Can't delete your Mail> room */
1766 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1768 /* Otherwise it's ok */
1773 * For normal rooms, just check for aide or room aide status.
1775 return(is_room_aide());
1779 * aide command: kill the current room
1781 void cmd_kill(char *argbuf)
1783 char deleted_room_name[ROOMNAMELEN];
1787 kill_ok = extract_int(argbuf, 0);
1789 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
1790 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1794 if (CC->room.QRflags & QR_MAILBOX) {
1795 safestrncpy(deleted_room_name, &CC->room.QRname[11], sizeof deleted_room_name);
1798 safestrncpy(deleted_room_name, CC->room.QRname, sizeof deleted_room_name);
1801 /* Do the dirty work */
1802 CtdlScheduleRoomForDeletion(&CC->room);
1804 /* Return to the Lobby */
1805 CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
1807 /* tell the world what we did */
1808 snprintf(msg, sizeof msg, "The room \"%s\" has been deleted by %s.\n",
1809 deleted_room_name, CC->curr_user);
1810 CtdlAideMessage(msg, "Room Purger Message");
1811 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1813 cprintf("%d ok to delete.\n", CIT_OK);
1819 * Internal code to create a new room (returns room flags)
1821 * Room types: 0=public, 1=hidden, 2=passworded, 3=invitation-only,
1822 * 4=mailbox, 5=mailbox, but caller supplies namespace
1824 unsigned CtdlCreateRoom(char *new_room_name,
1826 char *new_room_pass,
1833 struct ctdlroom qrbuf;
1837 CtdlLogPrintf(CTDL_DEBUG, "CtdlCreateRoom(name=%s, type=%d, view=%d)\n",
1838 new_room_name, new_room_type, new_room_view);
1840 if (CtdlGetRoom(&qrbuf, new_room_name) == 0) {
1841 CtdlLogPrintf(CTDL_DEBUG, "%s already exists.\n", new_room_name);
1845 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1846 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1847 qrbuf.QRflags = QR_INUSE;
1848 if (new_room_type > 0)
1849 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1850 if (new_room_type == 1)
1851 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1852 if (new_room_type == 2)
1853 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1854 if ( (new_room_type == 4) || (new_room_type == 5) ) {
1855 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1856 /* qrbuf.QRflags2 |= QR2_SUBJECTREQ; */
1859 /* If the user is requesting a personal room, set up the room
1860 * name accordingly (prepend the user number)
1862 if (new_room_type == 4) {
1863 CtdlMailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1866 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1869 /* If the room is private, and the system administrator has elected
1870 * to automatically grant room aide privileges, do so now; otherwise,
1871 * set the room aide to undefined.
1873 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1874 qrbuf.QRroomaide = CC->user.usernum;
1876 qrbuf.QRroomaide = (-1L);
1880 * If the caller is only interested in testing whether this will work,
1881 * return now without creating the room.
1883 if (!really_create) return (qrbuf.QRflags);
1885 qrbuf.QRnumber = get_new_room_number();
1886 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1887 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1888 qrbuf.QRfloor = new_room_floor;
1889 qrbuf.QRdefaultview = new_room_view;
1891 /* save what we just did... */
1892 CtdlPutRoom(&qrbuf);
1894 /* bump the reference count on whatever floor the room is on */
1895 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1896 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1897 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1899 /* Grant the creator access to the room unless the avoid_access
1900 * parameter was specified.
1902 if ( (CC->logged_in) && (avoid_access == 0) ) {
1903 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1904 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1905 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1906 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1909 /* resume our happy day */
1910 return (qrbuf.QRflags);
1917 void cmd_cre8(char *args)
1920 char new_room_name[ROOMNAMELEN];
1922 char new_room_pass[32];
1925 char *notification_message = NULL;
1928 int avoid_access = 0;
1930 cre8_ok = extract_int(args, 0);
1931 extract_token(new_room_name, args, 1, '|', sizeof new_room_name);
1932 new_room_name[ROOMNAMELEN - 1] = 0;
1933 new_room_type = extract_int(args, 2);
1934 extract_token(new_room_pass, args, 3, '|', sizeof new_room_pass);
1935 avoid_access = extract_int(args, 5);
1936 new_room_view = extract_int(args, 6);
1937 new_room_pass[9] = 0;
1940 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 1)) {
1941 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
1945 if (!strcasecmp(new_room_name, MAILROOM)) {
1946 cprintf("%d '%s' already exists.\n",
1947 ERROR + ALREADY_EXISTS, new_room_name);
1951 if (num_parms(args) >= 5) {
1952 fl = CtdlGetCachedFloor(extract_int(args, 4));
1954 cprintf("%d Invalid floor number.\n",
1955 ERROR + INVALID_FLOOR_OPERATION);
1958 else if ((fl->f_flags & F_INUSE) == 0) {
1959 cprintf("%d Invalid floor number.\n",
1960 ERROR + INVALID_FLOOR_OPERATION);
1963 new_room_floor = extract_int(args, 4);
1967 if (CtdlAccessCheck(ac_logged_in)) return;
1969 if (CC->user.axlevel < config.c_createax && !CC->internal_pgm) {
1970 cprintf("%d You need higher access to create rooms.\n",
1971 ERROR + HIGHER_ACCESS_REQUIRED);
1975 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 0)) {
1976 cprintf("%d Ok to create rooms.\n", CIT_OK);
1980 if ((new_room_type < 0) || (new_room_type > 5)) {
1981 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
1985 if (new_room_type == 5) {
1986 if (CC->user.axlevel < AxAideU) {
1987 cprintf("%d Higher access required\n",
1988 ERROR + HIGHER_ACCESS_REQUIRED);
1993 /* Check to make sure the requested room name doesn't already exist */
1994 newflags = CtdlCreateRoom(new_room_name,
1995 new_room_type, new_room_pass, new_room_floor,
1996 0, avoid_access, new_room_view);
1997 if (newflags == 0) {
1998 cprintf("%d '%s' already exists.\n",
1999 ERROR + ALREADY_EXISTS, new_room_name);
2004 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
2008 /* If we reach this point, the room needs to be created. */
2010 newflags = CtdlCreateRoom(new_room_name,
2011 new_room_type, new_room_pass, new_room_floor, 1, 0,
2014 /* post a message in Aide> describing the new room */
2015 notification_message = malloc(1024);
2016 snprintf(notification_message, 1024,
2017 "A new room called \"%s\" has been created by %s%s%s%s%s%s\n",
2020 ((newflags & QR_MAILBOX) ? " [personal]" : ""),
2021 ((newflags & QR_PRIVATE) ? " [private]" : ""),
2022 ((newflags & QR_GUESSNAME) ? " [hidden]" : ""),
2023 ((newflags & QR_PASSWORDED) ? " Password: " : ""),
2024 ((newflags & QR_PASSWORDED) ? new_room_pass : "")
2026 CtdlAideMessage(notification_message, "Room Creation Message");
2027 free(notification_message);
2029 cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
2034 void cmd_einf(char *ok)
2035 { /* enter info file for current room */
2037 char infofilename[SIZ];
2042 if (CtdlAccessCheck(ac_room_aide)) return;
2044 if (atoi(ok) == 0) {
2045 cprintf("%d Ok.\n", CIT_OK);
2048 assoc_file_name(infofilename, sizeof infofilename, &CC->room, ctdl_info_dir);
2049 CtdlLogPrintf(CTDL_DEBUG, "opening\n");
2050 fp = fopen(infofilename, "w");
2051 CtdlLogPrintf(CTDL_DEBUG, "checking\n");
2053 cprintf("%d Cannot open %s: %s\n",
2054 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
2057 cprintf("%d Send info...\n", SEND_LISTING);
2060 client_getln(buf, sizeof buf);
2061 if (strcmp(buf, "000"))
2062 fprintf(fp, "%s\n", buf);
2063 } while (strcmp(buf, "000"));
2066 /* now update the room index so people will see our new info */
2067 CtdlGetRoomLock(&CC->room, CC->room.QRname); /* lock so no one steps on us */
2068 CC->room.QRinfo = CC->room.QRhighest + 1L;
2069 CtdlPutRoomLock(&CC->room);
2074 * cmd_lflr() - List all known floors
2076 void cmd_lflr(char *gargs)
2081 if (CtdlAccessCheck(ac_logged_in)) return;
2083 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
2085 for (a = 0; a < MAXFLOORS; ++a) {
2086 CtdlGetFloor(&flbuf, a);
2087 if (flbuf.f_flags & F_INUSE) {
2088 cprintf("%d|%s|%d\n",
2100 * create a new floor
2102 void cmd_cflr(char *argbuf)
2104 char new_floor_name[256];
2107 int free_slot = (-1);
2110 extract_token(new_floor_name, argbuf, 0, '|', sizeof new_floor_name);
2111 cflr_ok = extract_int(argbuf, 1);
2113 if (CtdlAccessCheck(ac_aide)) return;
2115 if (IsEmptyStr(new_floor_name)) {
2116 cprintf("%d Blank floor name not allowed.\n",
2117 ERROR + ILLEGAL_VALUE);
2121 for (a = 0; a < MAXFLOORS; ++a) {
2122 CtdlGetFloor(&flbuf, a);
2124 /* note any free slots while we're scanning... */
2125 if (((flbuf.f_flags & F_INUSE) == 0)
2129 /* check to see if it already exists */
2130 if ((!strcasecmp(flbuf.f_name, new_floor_name))
2131 && (flbuf.f_flags & F_INUSE)) {
2132 cprintf("%d Floor '%s' already exists.\n",
2133 ERROR + ALREADY_EXISTS,
2139 if (free_slot < 0) {
2140 cprintf("%d There is no space available for a new floor.\n",
2141 ERROR + INVALID_FLOOR_OPERATION);
2145 cprintf("%d ok to create...\n", CIT_OK);
2148 lgetfloor(&flbuf, free_slot);
2149 flbuf.f_flags = F_INUSE;
2150 flbuf.f_ref_count = 0;
2151 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
2152 lputfloor(&flbuf, free_slot);
2153 cprintf("%d %d\n", CIT_OK, free_slot);
2161 void cmd_kflr(char *argbuf)
2164 int floor_to_delete;
2168 floor_to_delete = extract_int(argbuf, 0);
2169 kflr_ok = extract_int(argbuf, 1);
2171 if (CtdlAccessCheck(ac_aide)) return;
2173 lgetfloor(&flbuf, floor_to_delete);
2176 if ((flbuf.f_flags & F_INUSE) == 0) {
2177 cprintf("%d Floor %d not in use.\n",
2178 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
2181 if (flbuf.f_ref_count != 0) {
2182 cprintf("%d Cannot delete; floor contains %d rooms.\n",
2183 ERROR + INVALID_FLOOR_OPERATION,
2188 cprintf("%d Ok\n", CIT_OK);
2190 cprintf("%d Ok to delete...\n", CIT_OK);
2197 if ((delete_ok == 1) && (kflr_ok == 1))
2199 lputfloor(&flbuf, floor_to_delete);
2205 void cmd_eflr(char *argbuf)
2211 np = num_parms(argbuf);
2213 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
2217 if (CtdlAccessCheck(ac_aide)) return;
2219 floor_num = extract_int(argbuf, 0);
2220 lgetfloor(&flbuf, floor_num);
2221 if ((flbuf.f_flags & F_INUSE) == 0) {
2222 lputfloor(&flbuf, floor_num);
2223 cprintf("%d Floor %d is not in use.\n",
2224 ERROR + INVALID_FLOOR_OPERATION, floor_num);
2228 extract_token(flbuf.f_name, argbuf, 1, '|', sizeof flbuf.f_name);
2229 lputfloor(&flbuf, floor_num);
2231 cprintf("%d Ok\n", CIT_OK);
2235 /*****************************************************************************/
2236 /* MODULE INITIALIZATION STUFF */
2237 /*****************************************************************************/
2239 CTDL_MODULE_INIT(room_ops)
2242 CtdlRegisterProtoHook(cmd_lrms, "LRMS", "Autoconverted. TODO: document me.");
2243 CtdlRegisterProtoHook(cmd_lkra, "LKRA", "Autoconverted. TODO: document me.");
2244 CtdlRegisterProtoHook(cmd_lkrn, "LKRN", "Autoconverted. TODO: document me.");
2245 CtdlRegisterProtoHook(cmd_lkro, "LKRO", "Autoconverted. TODO: document me.");
2246 CtdlRegisterProtoHook(cmd_lzrm, "LZRM", "Autoconverted. TODO: document me.");
2247 CtdlRegisterProtoHook(cmd_lprm, "LPRM", "Autoconverted. TODO: document me.");
2248 CtdlRegisterProtoHook(cmd_goto, "GOTO", "Autoconverted. TODO: document me.");
2249 CtdlRegisterProtoHook(cmd_whok, "WHOK", "Autoconverted. TODO: document me.");
2250 CtdlRegisterProtoHook(cmd_rdir, "RDIR", "Autoconverted. TODO: document me.");
2251 CtdlRegisterProtoHook(cmd_getr, "GETR", "Autoconverted. TODO: document me.");
2252 CtdlRegisterProtoHook(cmd_setr, "SETR", "Autoconverted. TODO: document me.");
2253 CtdlRegisterProtoHook(cmd_geta, "GETA", "Autoconverted. TODO: document me.");
2254 CtdlRegisterProtoHook(cmd_seta, "SETA", "Autoconverted. TODO: document me.");
2255 CtdlRegisterProtoHook(cmd_rinf, "RINF", "Autoconverted. TODO: document me.");
2256 CtdlRegisterProtoHook(cmd_kill, "KILL", "Autoconverted. TODO: document me.");
2257 CtdlRegisterProtoHook(cmd_cre8, "CRE8", "Autoconverted. TODO: document me.");
2258 CtdlRegisterProtoHook(cmd_einf, "EINF", "Autoconverted. TODO: document me.");
2259 CtdlRegisterProtoHook(cmd_lflr, "LFLR", "Autoconverted. TODO: document me.");
2260 CtdlRegisterProtoHook(cmd_cflr, "CFLR", "Autoconverted. TODO: document me.");
2261 CtdlRegisterProtoHook(cmd_kflr, "KFLR", "Autoconverted. TODO: document me.");
2262 CtdlRegisterProtoHook(cmd_eflr, "EFLR", "Autoconverted. TODO: document me.");
2264 /* return our Subversion id for the Log */