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)
57 if (userbuf == &CC->user) {
61 /*** temporary ObviousLog(tm) -- disregard this
63 CtdlLogPrintf(CTDL_DEBUG, "\033[32muserbuf==CC\033[0m\n");
66 CtdlLogPrintf(CTDL_DEBUG, "\033[31muserbuf!=CC\033[0m\n");
70 /* for internal programs, always do everything */
71 if (((CC->internal_pgm)) && (roombuf->QRflags & QR_INUSE)) {
72 retval = (UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED);
77 /* If guest mode is enabled, always grant access to the Lobby */
81 && (config.c_guest_logins)
82 && (!strcasecmp(roombuf->QRname, BASEROOM))
84 retval = (UA_KNOWN | UA_GOTOALLOWED);
89 /* Locate any applicable user/room relationships */
90 CtdlGetRelationship(&vbuf, userbuf, roombuf);
92 /* Force the properties of the Aide room */
93 if (!strcasecmp(roombuf->QRname, config.c_aideroom)) {
94 if (userbuf->axlevel >= AxAideU) {
95 retval = UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
102 /* If this is a public room, it's accessible... */
103 if ( ((roombuf->QRflags & QR_PRIVATE) == 0)
104 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
105 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
108 /* If this is a preferred users only room, check access level */
109 if (roombuf->QRflags & QR_PREFONLY) {
110 if (userbuf->axlevel < AxPrefU) {
111 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
115 /* For private rooms, check the generation number matchups */
116 if ( (roombuf->QRflags & QR_PRIVATE)
117 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
119 /* An explicit match means the user belongs in this room */
120 if (vbuf.v_flags & V_ACCESS) {
121 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
123 /* Otherwise, check if this is a guess-name or passworded
124 * room. If it is, a goto may at least be attempted
126 else if ((roombuf->QRflags & QR_PRIVATE)
127 || (roombuf->QRflags & QR_PASSWORDED)) {
128 retval = retval & ~UA_KNOWN;
129 retval = retval | UA_GOTOALLOWED;
133 /* For mailbox rooms, also check the namespace */
134 /* Also, mailbox owners can delete their messages */
135 if (roombuf->QRflags & QR_MAILBOX) {
136 if (userbuf->usernum == atol(roombuf->QRname)) {
137 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
139 /* An explicit match means the user belongs in this room */
140 if (vbuf.v_flags & V_ACCESS) {
141 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
145 /* For non-mailbox rooms... */
148 /* User is allowed to post in the room unless:
149 * - User is not validated
150 * - User has no net privileges and it is a shared network room
151 * - It is a read-only room
153 int post_allowed = 1;
154 if (userbuf->axlevel < AxProbU) post_allowed = 0;
155 if ((userbuf->axlevel < AxNetU) && (roombuf->QRflags & QR_NETWORK)) post_allowed = 0;
156 if (roombuf->QRflags & QR_READONLY) post_allowed = 0;
158 retval = retval | UA_POSTALLOWED;
161 /* If "collaborative deletion" is active for this room, any user who can post
162 * is also allowed to delete
164 if (roombuf->QRflags2 & QR2_COLLABDEL) {
165 if (retval & UA_POSTALLOWED) {
166 retval = retval | UA_DELETEALLOWED;
172 /* Check to see if the user has forgotten this room */
173 if (vbuf.v_flags & V_FORGET) {
174 retval = retval & ~UA_KNOWN;
175 if ( ( ((roombuf->QRflags & QR_PRIVATE) == 0)
176 && ((roombuf->QRflags & QR_MAILBOX) == 0) )
177 || ( (roombuf->QRflags & QR_MAILBOX)
178 && (atol(roombuf->QRname) == CC->user.usernum))) {
179 retval = retval | UA_ZAPPED;
182 /* If user is explicitly locked out of this room, deny everything */
183 if (vbuf.v_flags & V_LOCKOUT) {
184 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED & ~UA_POSTALLOWED;
187 /* Aides get access to all private rooms */
188 if ( (userbuf->axlevel >= AxAideU)
189 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
190 if (vbuf.v_flags & V_FORGET) {
191 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED;
194 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED;
198 /* Aides can gain access to mailboxes as well, but they don't show
201 if ( (userbuf->axlevel >= AxAideU)
202 && (roombuf->QRflags & QR_MAILBOX) ) {
203 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED;
206 /* Aides and Room Aides have admin privileges */
207 if ( (userbuf->axlevel >= AxAideU)
208 || (userbuf->usernum == roombuf->QRroomaide)
210 retval = retval | UA_ADMINALLOWED | UA_DELETEALLOWED | UA_POSTALLOWED;
213 NEWMSG: /* By the way, we also check for the presence of new messages */
214 if (is_msg_in_sequence_set(vbuf.v_seen, roombuf->QRhighest) == 0) {
215 retval = retval | UA_HASNEWMSGS;
218 /* System rooms never show up in the list. */
219 if (roombuf->QRflags2 & QR2_SYSTEM) {
220 retval = retval & ~UA_KNOWN;
224 /* Now give the caller the information it wants. */
225 if (result != NULL) *result = retval;
226 if (view != NULL) *view = vbuf.v_view;
230 * Self-checking stuff for a room record read into memory
232 void room_sanity_check(struct ctdlroom *qrbuf)
234 /* Mailbox rooms are always on the lowest floor */
235 if (qrbuf->QRflags & QR_MAILBOX) {
238 /* Listing order of 0 is illegal except for base rooms */
239 if (qrbuf->QRorder == 0)
240 if (!(qrbuf->QRflags & QR_MAILBOX) &&
241 strncasecmp(qrbuf->QRname, config.c_baseroom, ROOMNAMELEN)
243 strncasecmp(qrbuf->QRname, config.c_aideroom, ROOMNAMELEN))
249 * CtdlGetRoom() - retrieve room data from disk
251 int CtdlGetRoom(struct ctdlroom *qrbuf, char *room_name)
253 struct cdbdata *cdbqr;
254 char lowercase_name[ROOMNAMELEN];
255 char personal_lowercase_name[ROOMNAMELEN];
256 char *dptr, *sptr, *eptr;
258 dptr = lowercase_name;
260 eptr = (dptr + (sizeof lowercase_name - 1));
261 while (!IsEmptyStr(sptr) && (dptr < eptr)){
262 *dptr = tolower(*sptr);
267 memset(qrbuf, 0, sizeof(struct ctdlroom));
269 /* First, try the public namespace */
270 cdbqr = cdb_fetch(CDB_ROOMS,
271 lowercase_name, strlen(lowercase_name));
273 /* If that didn't work, try the user's personal namespace */
275 snprintf(personal_lowercase_name,
276 sizeof personal_lowercase_name, "%010ld.%s",
277 CC->user.usernum, lowercase_name);
278 cdbqr = cdb_fetch(CDB_ROOMS,
279 personal_lowercase_name,
280 strlen(personal_lowercase_name));
283 memcpy(qrbuf, cdbqr->ptr,
284 ((cdbqr->len > sizeof(struct ctdlroom)) ?
285 sizeof(struct ctdlroom) : cdbqr->len));
288 room_sanity_check(qrbuf);
297 * CtdlGetRoomLock() - same as getroom() but locks the record (if supported)
299 int CtdlGetRoomLock(struct ctdlroom *qrbuf, char *room_name)
302 retval = CtdlGetRoom(qrbuf, room_name);
303 if (retval == 0) begin_critical_section(S_ROOMS);
309 * b_putroom() - back end to putroom() and b_deleteroom()
310 * (if the supplied buffer is NULL, delete the room record)
312 void b_putroom(struct ctdlroom *qrbuf, char *room_name)
314 char lowercase_name[ROOMNAMELEN];
319 bptr = lowercase_name;
320 while (!IsEmptyStr(aptr))
322 *bptr = tolower(*aptr);
328 len = bptr - lowercase_name;
330 cdb_delete(CDB_ROOMS,
331 lowercase_name, len);
333 time(&qrbuf->QRmtime);
336 qrbuf, sizeof(struct ctdlroom));
342 * CtdlPutRoom() - store room data to disk
344 void CtdlPutRoom(struct ctdlroom *qrbuf) {
345 b_putroom(qrbuf, qrbuf->QRname);
350 * b_deleteroom() - delete a room record from disk
352 void b_deleteroom(char *room_name) {
353 b_putroom(NULL, room_name);
359 * CtdlPutRoomLock() - same as CtdlPutRoom() but unlocks the record (if supported)
361 void CtdlPutRoomLock(struct ctdlroom *qrbuf)
365 end_critical_section(S_ROOMS);
369 /****************************************************************************/
373 * CtdlGetFloorByName() - retrieve the number of the named floor
374 * return < 0 if not found else return floor number
376 int CtdlGetFloorByName(const char *floor_name)
379 struct floor *flbuf = NULL;
381 for (a = 0; a < MAXFLOORS; ++a) {
382 flbuf = CtdlGetCachedFloor(a);
384 /* check to see if it already exists */
385 if ((!strcasecmp(flbuf->f_name, floor_name))
386 && (flbuf->f_flags & F_INUSE)) {
396 * CtdlGetFloorByNameLock() - retrieve floor number for given floor and lock the floor list.
398 int CtdlGetFloorByNameLock(const char *floor_name)
400 begin_critical_section(S_FLOORTAB);
401 return CtdlGetFloorByName(floor_name);
407 * CtdlGetAvailableFloor() - Return number of first unused floor
408 * return < 0 if none available
410 int CtdlGetAvailableFloor(void)
413 struct floor *flbuf = NULL;
415 for (a = 0; a < MAXFLOORS; a++) {
416 flbuf = CtdlGetCachedFloor(a);
418 /* check to see if it already exists */
419 if ((flbuf->f_flags & F_INUSE) == 0) {
428 * CtdlGetFloor() - retrieve floor data from disk
430 void CtdlGetFloor(struct floor *flbuf, int floor_num)
432 struct cdbdata *cdbfl;
434 memset(flbuf, 0, sizeof(struct floor));
435 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
437 memcpy(flbuf, cdbfl->ptr,
438 ((cdbfl->len > sizeof(struct floor)) ?
439 sizeof(struct floor) : cdbfl->len));
442 if (floor_num == 0) {
443 safestrncpy(flbuf->f_name, "Main Floor",
444 sizeof flbuf->f_name);
445 flbuf->f_flags = F_INUSE;
446 flbuf->f_ref_count = 3;
453 * lgetfloor() - same as CtdlGetFloor() but locks the record (if supported)
455 void lgetfloor(struct floor *flbuf, int floor_num)
458 begin_critical_section(S_FLOORTAB);
459 CtdlGetFloor(flbuf, floor_num);
464 * CtdlGetCachedFloor() - Get floor record from *cache* (loads from disk if needed)
466 * This is strictly a performance hack.
468 struct floor *CtdlGetCachedFloor(int floor_num) {
469 static int initialized = 0;
472 struct floor *fl = NULL;
474 begin_critical_section(S_FLOORCACHE);
475 if (initialized == 0) {
476 for (i=0; i<MAXFLOORS; ++i) {
477 floorcache[floor_num] = NULL;
481 if (floorcache[floor_num] == NULL) {
484 end_critical_section(S_FLOORCACHE);
487 fl = malloc(sizeof(struct floor));
488 CtdlGetFloor(fl, floor_num);
489 begin_critical_section(S_FLOORCACHE);
490 if (floorcache[floor_num] != NULL) {
491 free(floorcache[floor_num]);
493 floorcache[floor_num] = fl;
494 end_critical_section(S_FLOORCACHE);
497 return(floorcache[floor_num]);
503 * CtdlPutFloor() - store floor data on disk
505 void CtdlPutFloor(struct floor *flbuf, int floor_num)
507 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
508 begin_critical_section(S_FLOORCACHE);
509 if (floorcache[floor_num] != NULL) {
510 free(floorcache[floor_num]);
511 floorcache[floor_num] = malloc(sizeof(struct floor));
512 memcpy(floorcache[floor_num], flbuf, sizeof(struct floor));
514 end_critical_section(S_FLOORCACHE);
516 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
517 flbuf, sizeof(struct floor));
523 * CtdlPutFloorLock() - same as CtdlPutFloor() but unlocks the record (if supported)
525 void CtdlPutFloorLock(struct floor *flbuf, int floor_num)
528 CtdlPutFloor(flbuf, floor_num);
529 end_critical_section(S_FLOORTAB);
536 * lputfloor() - same as CtdlPutFloor() but unlocks the record (if supported)
538 void lputfloor(struct floor *flbuf, int floor_num)
540 CtdlPutFloorLock(flbuf, floor_num);
545 * Traverse the room file...
547 void CtdlForEachRoom(void (*CallBack) (struct ctdlroom *EachRoom, void *out_data),
550 struct ctdlroom qrbuf;
551 struct cdbdata *cdbqr;
553 cdb_rewind(CDB_ROOMS);
555 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
556 memset(&qrbuf, 0, sizeof(struct ctdlroom));
557 memcpy(&qrbuf, cdbqr->ptr,
558 ((cdbqr->len > sizeof(struct ctdlroom)) ?
559 sizeof(struct ctdlroom) : cdbqr->len));
561 room_sanity_check(&qrbuf);
562 if (qrbuf.QRflags & QR_INUSE)
563 (*CallBack)(&qrbuf, in_data);
569 * delete_msglist() - delete room message pointers
571 void delete_msglist(struct ctdlroom *whichroom)
573 struct cdbdata *cdbml;
575 /* Make sure the msglist we're deleting actually exists, otherwise
576 * gdbm will complain when we try to delete an invalid record
578 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
582 /* Go ahead and delete it */
583 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
590 * Message pointer compare function for sort_msglist()
592 int sort_msglist_cmp(const void *m1, const void *m2) {
593 if ((*(const long *)m1) > (*(const long *)m2)) return(1);
594 if ((*(const long *)m1) < (*(const long *)m2)) return(-1);
600 * sort message pointers
601 * (returns new msg count)
603 int sort_msglist(long listptrs[], int oldcount)
613 qsort(listptrs, numitems, sizeof(long), sort_msglist_cmp);
615 /* and yank any nulls */
616 while ((numitems > 0) && (listptrs[0] == 0L)) {
617 memmove(&listptrs[0], &listptrs[1],
618 (sizeof(long) * (numitems - 1)));
627 * Determine whether a given room is non-editable.
629 int CtdlIsNonEditable(struct ctdlroom *qrbuf)
632 /* Mail> rooms are non-editable */
633 if ( (qrbuf->QRflags & QR_MAILBOX)
634 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
637 /* Everything else is editable */
644 * Back-back-end for all room listing commands
646 void list_roomname(struct ctdlroom *qrbuf, int ra, int current_view, int default_view)
648 char truncated_roomname[ROOMNAMELEN];
650 /* For my own mailbox rooms, chop off the owner prefix */
651 if ( (qrbuf->QRflags & QR_MAILBOX)
652 && (atol(qrbuf->QRname) == CC->user.usernum) ) {
653 safestrncpy(truncated_roomname, qrbuf->QRname, sizeof truncated_roomname);
654 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
655 cprintf("%s", truncated_roomname);
657 /* For all other rooms, just display the name in its entirety */
659 cprintf("%s", qrbuf->QRname);
662 /* ...and now the other parameters */
663 cprintf("|%u|%d|%d|%d|%d|%d|%d|%ld|\n",
665 (int) qrbuf->QRfloor,
666 (int) qrbuf->QRorder,
667 (int) qrbuf->QRflags2,
677 * cmd_lrms() - List all accessible rooms, known or forgotten
679 void cmd_lrms_backend(struct ctdlroom *qrbuf, void *data)
681 int FloorBeingSearched = (-1);
685 FloorBeingSearched = *(int *)data;
686 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
688 if ((( ra & (UA_KNOWN | UA_ZAPPED)))
689 && ((qrbuf->QRfloor == (FloorBeingSearched))
690 || ((FloorBeingSearched) < 0)))
691 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
694 void cmd_lrms(char *argbuf)
696 int FloorBeingSearched = (-1);
697 if (!IsEmptyStr(argbuf))
698 FloorBeingSearched = extract_int(argbuf, 0);
700 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
702 if (CtdlGetUser(&CC->user, CC->curr_user)) {
703 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
706 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
708 CtdlForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
715 * cmd_lkra() - List all known rooms
717 void cmd_lkra_backend(struct ctdlroom *qrbuf, void *data)
719 int FloorBeingSearched = (-1);
723 FloorBeingSearched = *(int *)data;
724 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
726 if ((( ra & (UA_KNOWN)))
727 && ((qrbuf->QRfloor == (FloorBeingSearched))
728 || ((FloorBeingSearched) < 0)))
729 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
732 void cmd_lkra(char *argbuf)
734 int FloorBeingSearched = (-1);
735 if (!IsEmptyStr(argbuf))
736 FloorBeingSearched = extract_int(argbuf, 0);
738 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
740 if (CtdlGetUser(&CC->user, CC->curr_user)) {
741 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
744 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
746 CtdlForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
752 void cmd_lprm_backend(struct ctdlroom *qrbuf, void *data)
754 int FloorBeingSearched = (-1);
758 FloorBeingSearched = *(int *)data;
759 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
761 if ( ((qrbuf->QRflags & QR_PRIVATE) == 0)
762 && ((qrbuf->QRflags & QR_MAILBOX) == 0)
763 && ((qrbuf->QRfloor == (FloorBeingSearched))
764 || ((FloorBeingSearched) < 0)))
765 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
768 void cmd_lprm(char *argbuf)
770 int FloorBeingSearched = (-1);
771 if (!IsEmptyStr(argbuf))
772 FloorBeingSearched = extract_int(argbuf, 0);
774 cprintf("%d Publiic rooms:\n", LISTING_FOLLOWS);
776 CtdlForEachRoom(cmd_lprm_backend, &FloorBeingSearched);
783 * cmd_lkrn() - List all known rooms with new messages
785 void cmd_lkrn_backend(struct ctdlroom *qrbuf, void *data)
787 int FloorBeingSearched = (-1);
791 FloorBeingSearched = *(int *)data;
792 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
795 && (ra & UA_HASNEWMSGS)
796 && ((qrbuf->QRfloor == (FloorBeingSearched))
797 || ((FloorBeingSearched) < 0)))
798 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
801 void cmd_lkrn(char *argbuf)
803 int FloorBeingSearched = (-1);
804 if (!IsEmptyStr(argbuf))
805 FloorBeingSearched = extract_int(argbuf, 0);
807 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
809 if (CtdlGetUser(&CC->user, CC->curr_user)) {
810 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
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 if (CtdlGetUser(&CC->user, CC->curr_user)) {
849 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
852 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
854 CtdlForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
861 * cmd_lzrm() - List all forgotten rooms
863 void cmd_lzrm_backend(struct ctdlroom *qrbuf, void *data)
865 int FloorBeingSearched = (-1);
869 FloorBeingSearched = *(int *)data;
870 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
872 if ((ra & UA_GOTOALLOWED)
874 && ((qrbuf->QRfloor == (FloorBeingSearched))
875 || ((FloorBeingSearched) < 0)))
876 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
879 void cmd_lzrm(char *argbuf)
881 int FloorBeingSearched = (-1);
882 if (!IsEmptyStr(argbuf))
883 FloorBeingSearched = extract_int(argbuf, 0);
885 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
887 if (CtdlGetUser(&CC->user, CC->curr_user)) {
888 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
891 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
893 CtdlForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
899 * Make the specified room the current room for this session. No validation
900 * or access control is done here -- the caller should make sure that the
901 * specified room exists and is ok to access.
903 void CtdlUserGoto(char *where, int display_result, int transiently,
904 int *retmsgs, int *retnew)
907 int new_messages = 0;
908 int old_messages = 0;
909 int total_messages = 0;
913 int newmailcount = 0;
915 char truncated_roomname[ROOMNAMELEN];
916 struct cdbdata *cdbfr;
917 long *msglist = NULL;
919 unsigned int original_v_flags;
922 char setstr[128], lostr[64], histr[64];
926 /* If the supplied room name is NULL, the caller wants us to know that
927 * it has already copied the room record into CC->room, so
928 * we can skip the extra database fetch.
931 safestrncpy(CC->room.QRname, where, sizeof CC->room.QRname);
932 CtdlGetRoom(&CC->room, where);
935 /* Take care of all the formalities. */
937 begin_critical_section(S_USERS);
938 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
939 original_v_flags = vbuf.v_flags;
941 /* Know the room ... but not if it's the page log room, or if the
942 * caller specified that we're only entering this room transiently.
944 if ((strcasecmp(CC->room.QRname, config.c_logpages))
945 && (transiently == 0) ) {
946 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
947 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
950 /* Only rewrite the database record if we changed something */
951 if (vbuf.v_flags != original_v_flags) {
952 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
954 end_critical_section(S_USERS);
956 /* Check for new mail */
957 newmailcount = NewMailCount();
959 /* set info to 1 if the user needs to read the room's info file */
960 if (CC->room.QRinfo > vbuf.v_lastseen) {
964 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
966 msglist = (long *) cdbfr->ptr;
967 cdbfr->ptr = NULL; /* CtdlUserGoto() now owns this memory */
968 num_msgs = cdbfr->len / sizeof(long);
973 for (a=0; a<num_msgs; ++a) {
974 if (msglist[a] > 0L) ++total_messages;
976 new_messages = num_msgs;
977 num_sets = num_tokens(vbuf.v_seen, ',');
978 for (s=0; s<num_sets; ++s) {
979 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
981 extract_token(lostr, setstr, 0, ':', sizeof lostr);
982 if (num_tokens(setstr, ':') >= 2) {
983 extract_token(histr, setstr, 1, ':', sizeof histr);
984 if (!strcmp(histr, "*")) {
985 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
989 strcpy(histr, lostr);
994 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
995 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
1001 new_messages = total_messages - old_messages;
1003 if (msglist != NULL) free(msglist);
1005 if (CC->room.QRflags & QR_MAILBOX)
1010 if ((CC->room.QRroomaide == CC->user.usernum)
1011 || (CC->user.axlevel >= AxAideU))
1016 safestrncpy(truncated_roomname, CC->room.QRname, sizeof truncated_roomname);
1017 if ( (CC->room.QRflags & QR_MAILBOX)
1018 && (atol(CC->room.QRname) == CC->user.usernum) ) {
1019 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
1022 if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
1026 if (retmsgs != NULL) *retmsgs = total_messages;
1027 if (retnew != NULL) *retnew = new_messages;
1028 CtdlLogPrintf(CTDL_DEBUG, "<%s> %d new of %d total messages\n",
1030 new_messages, total_messages
1033 CC->curr_view = (int)vbuf.v_view;
1035 if (display_result) {
1036 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|%d|\n",
1037 CIT_OK, CtdlCheckExpress(),
1040 (int)total_messages,
1042 (int)CC->room.QRflags,
1043 (long)CC->room.QRhighest,
1044 (long)vbuf.v_lastseen,
1048 (int)CC->room.QRfloor,
1050 (int)CC->room.QRdefaultview,
1052 (int)CC->room.QRflags2
1059 * Handle some of the macro named rooms
1061 void convert_room_name_macros(char *towhere, size_t maxlen) {
1062 if (!strcasecmp(towhere, "_BASEROOM_")) {
1063 safestrncpy(towhere, config.c_baseroom, maxlen);
1065 else if (!strcasecmp(towhere, "_MAIL_")) {
1066 safestrncpy(towhere, MAILROOM, maxlen);
1068 else if (!strcasecmp(towhere, "_TRASH_")) {
1069 safestrncpy(towhere, USERTRASHROOM, maxlen);
1071 else if (!strcasecmp(towhere, "_DRAFTS_")) {
1072 safestrncpy(towhere, USERDRAFTROOM, maxlen);
1074 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
1075 safestrncpy(towhere, config.c_twitroom, maxlen);
1077 else if (!strcasecmp(towhere, "_CALENDAR_")) {
1078 safestrncpy(towhere, USERCALENDARROOM, maxlen);
1080 else if (!strcasecmp(towhere, "_TASKS_")) {
1081 safestrncpy(towhere, USERTASKSROOM, maxlen);
1083 else if (!strcasecmp(towhere, "_CONTACTS_")) {
1084 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
1086 else if (!strcasecmp(towhere, "_NOTES_")) {
1087 safestrncpy(towhere, USERNOTESROOM, maxlen);
1093 * cmd_goto() - goto a new room
1095 void cmd_goto(char *gargs)
1097 struct ctdlroom QRscratch;
1101 char augmented_roomname[ROOMNAMELEN];
1102 char towhere[ROOMNAMELEN];
1104 int transiently = 0;
1106 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
1108 extract_token(towhere, gargs, 0, '|', sizeof towhere);
1109 extract_token(password, gargs, 1, '|', sizeof password);
1110 transiently = extract_int(gargs, 2);
1112 CtdlGetUser(&CC->user, CC->curr_user);
1115 * Handle some of the macro named rooms
1117 convert_room_name_macros(towhere, sizeof towhere);
1119 /* First try a regular match */
1120 c = CtdlGetRoom(&QRscratch, towhere);
1122 /* Then try a mailbox name match */
1124 CtdlMailboxName(augmented_roomname, sizeof augmented_roomname,
1125 &CC->user, towhere);
1126 c = CtdlGetRoom(&QRscratch, augmented_roomname);
1128 safestrncpy(towhere, augmented_roomname, sizeof towhere);
1131 /* And if the room was found... */
1134 /* Let internal programs go directly to any room. */
1135 if (CC->internal_pgm) {
1136 memcpy(&CC->room, &QRscratch,
1137 sizeof(struct ctdlroom));
1138 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1142 /* See if there is an existing user/room relationship */
1143 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
1145 /* normal clients have to pass through security */
1146 if (ra & UA_GOTOALLOWED) {
1151 if ((QRscratch.QRflags & QR_MAILBOX) &&
1152 ((ra & UA_GOTOALLOWED))) {
1153 memcpy(&CC->room, &QRscratch,
1154 sizeof(struct ctdlroom));
1155 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1157 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
1158 ((ra & UA_KNOWN) == 0) &&
1159 (strcasecmp(QRscratch.QRpasswd, password)) &&
1160 (CC->user.axlevel < AxAideU)
1162 cprintf("%d wrong or missing passwd\n",
1163 ERROR + PASSWORD_REQUIRED);
1165 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
1166 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
1167 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
1168 ((ra & UA_KNOWN) == 0) &&
1169 (CC->user.axlevel < AxAideU)
1171 CtdlLogPrintf(CTDL_DEBUG, "Failed to acquire private room\n");
1173 memcpy(&CC->room, &QRscratch,
1174 sizeof(struct ctdlroom));
1175 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1181 cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
1185 void cmd_whok(char *cmdbuf)
1187 struct ctdluser temp;
1188 struct cdbdata *cdbus;
1191 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
1192 cdb_rewind(CDB_USERS);
1193 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1194 memset(&temp, 0, sizeof temp);
1195 memcpy(&temp, cdbus->ptr, sizeof temp);
1198 CtdlRoomAccess(&CC->room, &temp, &ra, NULL);
1199 if ((CC->room.QRflags & QR_INUSE)
1202 cprintf("%s\n", temp.fullname);
1209 * RDIR command for room directory
1211 void cmd_rdir(char *cmdbuf)
1216 struct stat statbuf;
1217 DIR *filedir = NULL;
1218 struct dirent *filedir_entry;
1224 if (CtdlAccessCheck(ac_logged_in)) return;
1226 CtdlGetRoom(&CC->room, CC->room.QRname);
1227 CtdlGetUser(&CC->user, CC->curr_user);
1229 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
1230 cprintf("%d not here.\n", ERROR + NOT_HERE);
1233 if (((CC->room.QRflags & QR_VISDIR) == 0)
1234 && (CC->user.axlevel < AxAideU)
1235 && (CC->user.usernum != CC->room.QRroomaide)) {
1236 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1240 snprintf(buf, sizeof buf, "%s/%s", ctdl_file_dir, CC->room.QRdirname);
1241 filedir = opendir (buf);
1243 if (filedir == NULL) {
1244 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1247 cprintf("%d %s|%s/%s\n", LISTING_FOLLOWS, config.c_fqdn, ctdl_file_dir, CC->room.QRdirname);
1249 snprintf(buf, sizeof buf, "%s/%s/filedir", ctdl_file_dir, CC->room.QRdirname);
1250 fd = fopen(buf, "r");
1252 fd = fopen("/dev/null", "r");
1253 while ((filedir_entry = readdir(filedir)))
1255 if (strcasecmp(filedir_entry->d_name, "filedir") && filedir_entry->d_name[0] != '.')
1257 #ifdef _DIRENT_HAVE_D_NAMELEN
1258 d_namelen = filedir_entry->d_namelen;
1260 d_namelen = strlen(filedir_entry->d_name);
1262 snprintf(buf, sizeof buf, "%s/%s/%s", ctdl_file_dir, CC->room.QRdirname, filedir_entry->d_name);
1263 stat(buf, &statbuf); /* stat the file */
1264 if (!(statbuf.st_mode & S_IFREG))
1266 snprintf(buf2, sizeof buf2,
1267 "\"%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",
1268 buf, CC->room.QRname
1270 CtdlAideMessage(buf2, "Unusable data found in room directory");
1271 continue; /* not a useable file type so don't show it */
1273 safestrncpy(comment, "", sizeof comment);
1274 fseek(fd, 0L, 0); /* rewind descriptions file */
1275 /* Get the description from the descriptions file */
1276 while ((fgets(buf, sizeof buf, fd) != NULL) && (IsEmptyStr(comment)))
1278 buf[strlen(buf) - 1] = 0;
1279 if ((!strncasecmp(buf, filedir_entry->d_name, d_namelen)) && (buf[d_namelen] == ' '))
1280 safestrncpy(comment, &buf[d_namelen + 1], sizeof comment);
1282 len = extract_token (mimebuf, comment, 0,' ', 64);
1283 if ((len <0) || strchr(mimebuf, '/') == NULL)
1285 snprintf (mimebuf, 64, "application/octetstream");
1288 cprintf("%s|%ld|%s|%s\n",
1289 filedir_entry->d_name,
1290 (long)statbuf.st_size,
1302 * get room parameters (aide or room aide command)
1304 void cmd_getr(char *cmdbuf)
1306 if (CtdlAccessCheck(ac_room_aide)) return;
1308 CtdlGetRoom(&CC->room, CC->room.QRname);
1309 cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1313 ((CC->room.QRflags & QR_MAILBOX) ?
1314 &CC->room.QRname[11] : CC->room.QRname),
1316 ((CC->room.QRflags & QR_PASSWORDED) ?
1317 CC->room.QRpasswd : ""),
1319 ((CC->room.QRflags & QR_DIRECTORY) ?
1320 CC->room.QRdirname : ""),
1323 (int) CC->room.QRfloor,
1324 (int) CC->room.QRorder,
1326 CC->room.QRdefaultview,
1333 * Back end function to rename a room.
1334 * You can also specify which floor to move the room to, or specify -1 to
1335 * keep the room on the same floor it was on.
1337 * If you are renaming a mailbox room, you must supply the namespace prefix
1338 * in *at least* the old name!
1340 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1342 struct ctdlroom qrbuf;
1343 struct ctdlroom qrtmp;
1348 char actual_old_name[ROOMNAMELEN];
1350 CtdlLogPrintf(CTDL_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1351 old_name, new_name, new_floor);
1353 if (new_floor >= 0) {
1354 fl = CtdlGetCachedFloor(new_floor);
1355 if ((fl->f_flags & F_INUSE) == 0) {
1356 return(crr_invalid_floor);
1360 begin_critical_section(S_ROOMS);
1362 if ( (CtdlGetRoom(&qrtmp, new_name) == 0)
1363 && (strcasecmp(new_name, old_name)) ) {
1364 ret = crr_already_exists;
1367 else if (CtdlGetRoom(&qrbuf, old_name) != 0) {
1368 ret = crr_room_not_found;
1371 else if ( (CC->user.axlevel < AxAideU) && (!CC->internal_pgm)
1372 && (CC->user.usernum != qrbuf.QRroomaide)
1373 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) ) {
1374 ret = crr_access_denied;
1377 else if (CtdlIsNonEditable(&qrbuf)) {
1378 ret = crr_noneditable;
1383 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1384 if (qrbuf.QRflags & QR_MAILBOX) {
1385 owner = atol(qrbuf.QRname);
1387 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1388 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1389 "%010ld.%s", owner, new_name);
1392 safestrncpy(qrbuf.QRname, new_name,
1393 sizeof(qrbuf.QRname));
1396 /* Reject change of floor for baseroom/aideroom */
1397 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1398 !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1402 /* Take care of floor stuff */
1403 old_floor = qrbuf.QRfloor;
1404 if (new_floor < 0) {
1405 new_floor = old_floor;
1407 qrbuf.QRfloor = new_floor;
1408 CtdlPutRoom(&qrbuf);
1410 begin_critical_section(S_CONFIG);
1412 /* If baseroom/aideroom name changes, update config */
1413 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1414 safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1417 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1418 safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1422 end_critical_section(S_CONFIG);
1424 /* If the room name changed, then there are now two room
1425 * records, so we have to delete the old one.
1427 if (strcasecmp(new_name, old_name)) {
1428 b_deleteroom(actual_old_name);
1434 end_critical_section(S_ROOMS);
1436 /* Adjust the floor reference counts if necessary */
1437 if (new_floor != old_floor) {
1438 lgetfloor(&flbuf, old_floor);
1439 --flbuf.f_ref_count;
1440 lputfloor(&flbuf, old_floor);
1441 CtdlLogPrintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1442 lgetfloor(&flbuf, new_floor);
1443 ++flbuf.f_ref_count;
1444 lputfloor(&flbuf, new_floor);
1445 CtdlLogPrintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1448 /* ...and everybody say "YATTA!" */
1454 * set room parameters (aide or room aide command)
1456 void cmd_setr(char *args)
1462 char new_name[ROOMNAMELEN];
1464 if (CtdlAccessCheck(ac_logged_in)) return;
1466 if (num_parms(args) >= 6) {
1467 new_floor = extract_int(args, 5);
1469 new_floor = (-1); /* don't change the floor */
1472 /* When is a new name more than just a new name? When the old name
1473 * has a namespace prefix.
1475 if (CC->room.QRflags & QR_MAILBOX) {
1476 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
1478 safestrncpy(new_name, "", sizeof new_name);
1480 extract_token(&new_name[strlen(new_name)], args, 0, '|', (sizeof new_name - strlen(new_name)));
1482 r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
1484 if (r == crr_room_not_found) {
1485 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
1486 } else if (r == crr_already_exists) {
1487 cprintf("%d '%s' already exists.\n",
1488 ERROR + ALREADY_EXISTS, new_name);
1489 } else if (r == crr_noneditable) {
1490 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
1491 } else if (r == crr_invalid_floor) {
1492 cprintf("%d Target floor does not exist.\n",
1493 ERROR + INVALID_FLOOR_OPERATION);
1494 } else if (r == crr_access_denied) {
1495 cprintf("%d You do not have permission to edit '%s'\n",
1496 ERROR + HIGHER_ACCESS_REQUIRED,
1498 } else if (r != crr_ok) {
1499 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1500 ERROR + INTERNAL_ERROR, r);
1507 CtdlGetRoom(&CC->room, new_name);
1509 /* Now we have to do a bunch of other stuff */
1511 if (num_parms(args) >= 7) {
1512 new_order = extract_int(args, 6);
1515 if (new_order > 127)
1519 CtdlGetRoomLock(&CC->room, CC->room.QRname);
1521 /* Directory room */
1522 extract_token(buf, args, 2, '|', sizeof buf);
1524 safestrncpy(CC->room.QRdirname, buf,
1525 sizeof CC->room.QRdirname);
1528 if (num_parms(args) >= 8) {
1529 CC->room.QRdefaultview = extract_int(args, 7);
1532 /* Second set of flags */
1533 if (num_parms(args) >= 9) {
1534 CC->room.QRflags2 = extract_int(args, 8);
1538 CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
1539 /* Clean up a client boo-boo: if the client set the room to
1540 * guess-name or passworded, ensure that the private flag is
1543 if ((CC->room.QRflags & QR_GUESSNAME)
1544 || (CC->room.QRflags & QR_PASSWORDED))
1545 CC->room.QRflags |= QR_PRIVATE;
1547 /* Some changes can't apply to BASEROOM */
1548 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1550 CC->room.QRorder = 0;
1551 CC->room.QRpasswd[0] = '\0';
1552 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1553 QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1554 CC->room.QRflags |= QR_PERMANENT;
1556 /* March order (doesn't apply to AIDEROOM) */
1557 if (num_parms(args) >= 7)
1558 CC->room.QRorder = (char) new_order;
1560 extract_token(buf, args, 1, '|', sizeof buf);
1562 safestrncpy(CC->room.QRpasswd, buf,
1563 sizeof CC->room.QRpasswd);
1564 /* Kick everyone out if the client requested it
1565 * (by changing the room's generation number)
1567 if (extract_int(args, 4)) {
1568 time(&CC->room.QRgen);
1571 /* Some changes can't apply to AIDEROOM */
1572 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1574 CC->room.QRorder = 0;
1575 CC->room.QRflags &= ~QR_MAILBOX;
1576 CC->room.QRflags |= QR_PERMANENT;
1579 /* Write the room record back to disk */
1580 CtdlPutRoomLock(&CC->room);
1582 /* Create a room directory if necessary */
1583 if (CC->room.QRflags & QR_DIRECTORY) {
1584 snprintf(buf, sizeof buf,"%s/%s",
1586 CC->room.QRdirname);
1589 snprintf(buf, sizeof buf, "The room \"%s\" has been edited by %s.\n",
1590 CC->room.QRname, CC->curr_user);
1591 CtdlAideMessage(buf, "Room modification Message");
1592 cprintf("%d Ok\n", CIT_OK);
1598 * get the name of the room aide for this room
1600 void cmd_geta(char *cmdbuf)
1602 struct ctdluser usbuf;
1604 if (CtdlAccessCheck(ac_logged_in)) return;
1606 if (CtdlGetUserByNumber(&usbuf, CC->room.QRroomaide) == 0) {
1607 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1609 cprintf("%d \n", CIT_OK);
1615 * set the room aide for this room
1617 void cmd_seta(char *new_ra)
1619 struct ctdluser usbuf;
1624 if (CtdlAccessCheck(ac_room_aide)) return;
1626 if (CtdlGetUser(&usbuf, new_ra) != 0) {
1629 newu = usbuf.usernum;
1632 CtdlGetRoomLock(&CC->room, CC->room.QRname);
1634 if (CC->room.QRroomaide != newu) {
1637 CC->room.QRroomaide = newu;
1638 CtdlPutRoomLock(&CC->room);
1641 * We have to post the change notice _after_ writing changes to
1642 * the room table, otherwise it would deadlock!
1644 if (post_notice == 1) {
1645 if (!IsEmptyStr(usbuf.fullname))
1646 snprintf(buf, sizeof buf,
1647 "%s is now the room aide for \"%s\".\n",
1648 usbuf.fullname, CC->room.QRname);
1650 snprintf(buf, sizeof buf,
1651 "There is now no room aide for \"%s\".\n",
1653 CtdlAideMessage(buf, "Aide Room Modification");
1655 cprintf("%d Ok\n", CIT_OK);
1659 * retrieve info file for this room
1661 void cmd_rinf(char *gargs)
1667 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_info_dir);
1668 info_fp = fopen(filename, "r");
1670 if (info_fp == NULL) {
1671 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
1674 cprintf("%d Info:\n", LISTING_FOLLOWS);
1675 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1676 if (!IsEmptyStr(buf))
1677 buf[strlen(buf) - 1] = 0;
1678 cprintf("%s\n", buf);
1685 * Asynchronously schedule a room for deletion. The room will appear
1686 * deleted to the user(s), but it won't actually get purged from the
1687 * database until THE DREADED AUTO-PURGER makes its next run.
1689 void CtdlScheduleRoomForDeletion(struct ctdlroom *qrbuf)
1691 char old_name[ROOMNAMELEN];
1694 CtdlLogPrintf(CTDL_NOTICE, "Scheduling room <%s> for deletion\n",
1697 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1699 CtdlGetRoom(qrbuf, qrbuf->QRname);
1701 /* Turn the room into a private mailbox owned by a user who doesn't
1702 * exist. This will immediately make the room invisible to everyone,
1703 * and qualify the room for purging.
1705 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1710 qrbuf->QRflags |= QR_MAILBOX;
1711 time(&qrbuf->QRgen); /* Use a timestamp as the new generation number */
1715 b_deleteroom(old_name);
1721 * Back end processing to delete a room and everything associated with it
1722 * (This one is synchronous and should only get called by THE DREADED
1723 * AUTO-PURGER in serv_expire.c. All user-facing code should call
1724 * the asynchronous schedule_room_for_deletion() instead.)
1726 void CtdlDeleteRoom(struct ctdlroom *qrbuf)
1730 /* TODO: filename magic? does this realy work? */
1732 CtdlLogPrintf(CTDL_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1734 /* Delete the info file */
1735 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_info_dir);
1738 /* Delete the image file */
1739 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_image_dir);
1742 /* Delete the room's network config file */
1743 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1746 /* Delete the messages in the room
1747 * (Careful: this opens an S_ROOMS critical section!)
1749 CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
1751 /* Flag the room record as not in use */
1752 CtdlGetRoomLock(qrbuf, qrbuf->QRname);
1754 CtdlPutRoomLock(qrbuf);
1756 /* then decrement the reference count for the floor */
1757 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1758 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1759 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1761 /* Delete the room record from the database! */
1762 b_deleteroom(qrbuf->QRname);
1768 * Check access control for deleting a room
1770 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1772 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1776 if (CtdlIsNonEditable(qr)) {
1781 * For mailboxes, check stuff
1783 if (qr->QRflags & QR_MAILBOX) {
1785 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1787 if (atol(qr->QRname) != CC->user.usernum) {
1788 return(0); /* not my room */
1791 /* Can't delete your Mail> room */
1792 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1794 /* Otherwise it's ok */
1799 * For normal rooms, just check for aide or room aide status.
1801 return(is_room_aide());
1805 * aide command: kill the current room
1807 void cmd_kill(char *argbuf)
1809 char deleted_room_name[ROOMNAMELEN];
1813 kill_ok = extract_int(argbuf, 0);
1815 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
1816 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1820 if (CC->room.QRflags & QR_MAILBOX) {
1821 safestrncpy(deleted_room_name, &CC->room.QRname[11], sizeof deleted_room_name);
1824 safestrncpy(deleted_room_name, CC->room.QRname, sizeof deleted_room_name);
1827 /* Do the dirty work */
1828 CtdlScheduleRoomForDeletion(&CC->room);
1830 /* Return to the Lobby */
1831 CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
1833 /* tell the world what we did */
1834 snprintf(msg, sizeof msg, "The room \"%s\" has been deleted by %s.\n",
1835 deleted_room_name, CC->curr_user);
1836 CtdlAideMessage(msg, "Room Purger Message");
1837 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1839 cprintf("%d ok to delete.\n", CIT_OK);
1845 * Internal code to create a new room (returns room flags)
1847 * Room types: 0=public, 1=hidden, 2=passworded, 3=invitation-only,
1848 * 4=mailbox, 5=mailbox, but caller supplies namespace
1850 unsigned CtdlCreateRoom(char *new_room_name,
1852 char *new_room_pass,
1859 struct ctdlroom qrbuf;
1863 CtdlLogPrintf(CTDL_DEBUG, "CtdlCreateRoom(name=%s, type=%d, view=%d)\n",
1864 new_room_name, new_room_type, new_room_view);
1866 if (CtdlGetRoom(&qrbuf, new_room_name) == 0) {
1867 CtdlLogPrintf(CTDL_DEBUG, "%s already exists.\n", new_room_name);
1871 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1872 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1873 qrbuf.QRflags = QR_INUSE;
1874 if (new_room_type > 0)
1875 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1876 if (new_room_type == 1)
1877 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1878 if (new_room_type == 2)
1879 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1880 if ( (new_room_type == 4) || (new_room_type == 5) ) {
1881 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1882 /* qrbuf.QRflags2 |= QR2_SUBJECTREQ; */
1885 /* If the user is requesting a personal room, set up the room
1886 * name accordingly (prepend the user number)
1888 if (new_room_type == 4) {
1889 CtdlMailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1892 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1895 /* If the room is private, and the system administrator has elected
1896 * to automatically grant room aide privileges, do so now; otherwise,
1897 * set the room aide to undefined.
1899 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1900 qrbuf.QRroomaide = CC->user.usernum;
1902 qrbuf.QRroomaide = (-1L);
1906 * If the caller is only interested in testing whether this will work,
1907 * return now without creating the room.
1909 if (!really_create) return (qrbuf.QRflags);
1911 qrbuf.QRnumber = get_new_room_number();
1912 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1913 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1914 qrbuf.QRfloor = new_room_floor;
1915 qrbuf.QRdefaultview = new_room_view;
1917 /* save what we just did... */
1918 CtdlPutRoom(&qrbuf);
1920 /* bump the reference count on whatever floor the room is on */
1921 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1922 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1923 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1925 /* Grant the creator access to the room unless the avoid_access
1926 * parameter was specified.
1928 if ( (CC->logged_in) && (avoid_access == 0) ) {
1929 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1930 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1931 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1932 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1935 /* resume our happy day */
1936 return (qrbuf.QRflags);
1943 void cmd_cre8(char *args)
1946 char new_room_name[ROOMNAMELEN];
1948 char new_room_pass[32];
1951 char *notification_message = NULL;
1954 int avoid_access = 0;
1956 cre8_ok = extract_int(args, 0);
1957 extract_token(new_room_name, args, 1, '|', sizeof new_room_name);
1958 new_room_name[ROOMNAMELEN - 1] = 0;
1959 new_room_type = extract_int(args, 2);
1960 extract_token(new_room_pass, args, 3, '|', sizeof new_room_pass);
1961 avoid_access = extract_int(args, 5);
1962 new_room_view = extract_int(args, 6);
1963 new_room_pass[9] = 0;
1966 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 1)) {
1967 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
1971 if (!strcasecmp(new_room_name, MAILROOM)) {
1972 cprintf("%d '%s' already exists.\n",
1973 ERROR + ALREADY_EXISTS, new_room_name);
1977 if (num_parms(args) >= 5) {
1978 fl = CtdlGetCachedFloor(extract_int(args, 4));
1980 cprintf("%d Invalid floor number.\n",
1981 ERROR + INVALID_FLOOR_OPERATION);
1984 else if ((fl->f_flags & F_INUSE) == 0) {
1985 cprintf("%d Invalid floor number.\n",
1986 ERROR + INVALID_FLOOR_OPERATION);
1989 new_room_floor = extract_int(args, 4);
1993 if (CtdlAccessCheck(ac_logged_in)) return;
1995 if (CC->user.axlevel < config.c_createax && !CC->internal_pgm) {
1996 cprintf("%d You need higher access to create rooms.\n",
1997 ERROR + HIGHER_ACCESS_REQUIRED);
2001 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 0)) {
2002 cprintf("%d Ok to create rooms.\n", CIT_OK);
2006 if ((new_room_type < 0) || (new_room_type > 5)) {
2007 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
2011 if (new_room_type == 5) {
2012 if (CC->user.axlevel < AxAideU) {
2013 cprintf("%d Higher access required\n",
2014 ERROR + HIGHER_ACCESS_REQUIRED);
2019 /* Check to make sure the requested room name doesn't already exist */
2020 newflags = CtdlCreateRoom(new_room_name,
2021 new_room_type, new_room_pass, new_room_floor,
2022 0, avoid_access, new_room_view);
2023 if (newflags == 0) {
2024 cprintf("%d '%s' already exists.\n",
2025 ERROR + ALREADY_EXISTS, new_room_name);
2030 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
2034 /* If we reach this point, the room needs to be created. */
2036 newflags = CtdlCreateRoom(new_room_name,
2037 new_room_type, new_room_pass, new_room_floor, 1, 0,
2040 /* post a message in Aide> describing the new room */
2041 notification_message = malloc(1024);
2042 snprintf(notification_message, 1024,
2043 "A new room called \"%s\" has been created by %s%s%s%s%s%s\n",
2046 ((newflags & QR_MAILBOX) ? " [personal]" : ""),
2047 ((newflags & QR_PRIVATE) ? " [private]" : ""),
2048 ((newflags & QR_GUESSNAME) ? " [hidden]" : ""),
2049 ((newflags & QR_PASSWORDED) ? " Password: " : ""),
2050 ((newflags & QR_PASSWORDED) ? new_room_pass : "")
2052 CtdlAideMessage(notification_message, "Room Creation Message");
2053 free(notification_message);
2055 cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
2060 void cmd_einf(char *ok)
2061 { /* enter info file for current room */
2063 char infofilename[SIZ];
2068 if (CtdlAccessCheck(ac_room_aide)) return;
2070 if (atoi(ok) == 0) {
2071 cprintf("%d Ok.\n", CIT_OK);
2074 assoc_file_name(infofilename, sizeof infofilename, &CC->room, ctdl_info_dir);
2075 CtdlLogPrintf(CTDL_DEBUG, "opening\n");
2076 fp = fopen(infofilename, "w");
2077 CtdlLogPrintf(CTDL_DEBUG, "checking\n");
2079 cprintf("%d Cannot open %s: %s\n",
2080 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
2083 cprintf("%d Send info...\n", SEND_LISTING);
2086 client_getln(buf, sizeof buf);
2087 if (strcmp(buf, "000"))
2088 fprintf(fp, "%s\n", buf);
2089 } while (strcmp(buf, "000"));
2092 /* now update the room index so people will see our new info */
2093 CtdlGetRoomLock(&CC->room, CC->room.QRname); /* lock so no one steps on us */
2094 CC->room.QRinfo = CC->room.QRhighest + 1L;
2095 CtdlPutRoomLock(&CC->room);
2100 * cmd_lflr() - List all known floors
2102 void cmd_lflr(char *gargs)
2107 if (CtdlAccessCheck(ac_logged_in)) return;
2109 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
2111 for (a = 0; a < MAXFLOORS; ++a) {
2112 CtdlGetFloor(&flbuf, a);
2113 if (flbuf.f_flags & F_INUSE) {
2114 cprintf("%d|%s|%d\n",
2126 * create a new floor
2128 void cmd_cflr(char *argbuf)
2130 char new_floor_name[256];
2133 int free_slot = (-1);
2136 extract_token(new_floor_name, argbuf, 0, '|', sizeof new_floor_name);
2137 cflr_ok = extract_int(argbuf, 1);
2139 if (CtdlAccessCheck(ac_aide)) return;
2141 if (IsEmptyStr(new_floor_name)) {
2142 cprintf("%d Blank floor name not allowed.\n",
2143 ERROR + ILLEGAL_VALUE);
2147 for (a = 0; a < MAXFLOORS; ++a) {
2148 CtdlGetFloor(&flbuf, a);
2150 /* note any free slots while we're scanning... */
2151 if (((flbuf.f_flags & F_INUSE) == 0)
2155 /* check to see if it already exists */
2156 if ((!strcasecmp(flbuf.f_name, new_floor_name))
2157 && (flbuf.f_flags & F_INUSE)) {
2158 cprintf("%d Floor '%s' already exists.\n",
2159 ERROR + ALREADY_EXISTS,
2165 if (free_slot < 0) {
2166 cprintf("%d There is no space available for a new floor.\n",
2167 ERROR + INVALID_FLOOR_OPERATION);
2171 cprintf("%d ok to create...\n", CIT_OK);
2174 lgetfloor(&flbuf, free_slot);
2175 flbuf.f_flags = F_INUSE;
2176 flbuf.f_ref_count = 0;
2177 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
2178 lputfloor(&flbuf, free_slot);
2179 cprintf("%d %d\n", CIT_OK, free_slot);
2187 void cmd_kflr(char *argbuf)
2190 int floor_to_delete;
2194 floor_to_delete = extract_int(argbuf, 0);
2195 kflr_ok = extract_int(argbuf, 1);
2197 if (CtdlAccessCheck(ac_aide)) return;
2199 lgetfloor(&flbuf, floor_to_delete);
2202 if ((flbuf.f_flags & F_INUSE) == 0) {
2203 cprintf("%d Floor %d not in use.\n",
2204 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
2207 if (flbuf.f_ref_count != 0) {
2208 cprintf("%d Cannot delete; floor contains %d rooms.\n",
2209 ERROR + INVALID_FLOOR_OPERATION,
2214 cprintf("%d Ok\n", CIT_OK);
2216 cprintf("%d Ok to delete...\n", CIT_OK);
2223 if ((delete_ok == 1) && (kflr_ok == 1))
2225 lputfloor(&flbuf, floor_to_delete);
2231 void cmd_eflr(char *argbuf)
2237 np = num_parms(argbuf);
2239 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
2243 if (CtdlAccessCheck(ac_aide)) return;
2245 floor_num = extract_int(argbuf, 0);
2246 lgetfloor(&flbuf, floor_num);
2247 if ((flbuf.f_flags & F_INUSE) == 0) {
2248 lputfloor(&flbuf, floor_num);
2249 cprintf("%d Floor %d is not in use.\n",
2250 ERROR + INVALID_FLOOR_OPERATION, floor_num);
2254 extract_token(flbuf.f_name, argbuf, 1, '|', sizeof flbuf.f_name);
2255 lputfloor(&flbuf, floor_num);
2257 cprintf("%d Ok\n", CIT_OK);
2261 /*****************************************************************************/
2262 /* MODULE INITIALIZATION STUFF */
2263 /*****************************************************************************/
2265 CTDL_MODULE_INIT(room_ops)
2268 CtdlRegisterProtoHook(cmd_lrms, "LRMS", "Autoconverted. TODO: document me.");
2269 CtdlRegisterProtoHook(cmd_lkra, "LKRA", "Autoconverted. TODO: document me.");
2270 CtdlRegisterProtoHook(cmd_lkrn, "LKRN", "Autoconverted. TODO: document me.");
2271 CtdlRegisterProtoHook(cmd_lkro, "LKRO", "Autoconverted. TODO: document me.");
2272 CtdlRegisterProtoHook(cmd_lzrm, "LZRM", "Autoconverted. TODO: document me.");
2273 CtdlRegisterProtoHook(cmd_lprm, "LPRM", "Autoconverted. TODO: document me.");
2274 CtdlRegisterProtoHook(cmd_goto, "GOTO", "Autoconverted. TODO: document me.");
2275 CtdlRegisterProtoHook(cmd_whok, "WHOK", "Autoconverted. TODO: document me.");
2276 CtdlRegisterProtoHook(cmd_rdir, "RDIR", "Autoconverted. TODO: document me.");
2277 CtdlRegisterProtoHook(cmd_getr, "GETR", "Autoconverted. TODO: document me.");
2278 CtdlRegisterProtoHook(cmd_setr, "SETR", "Autoconverted. TODO: document me.");
2279 CtdlRegisterProtoHook(cmd_geta, "GETA", "Autoconverted. TODO: document me.");
2280 CtdlRegisterProtoHook(cmd_seta, "SETA", "Autoconverted. TODO: document me.");
2281 CtdlRegisterProtoHook(cmd_rinf, "RINF", "Autoconverted. TODO: document me.");
2282 CtdlRegisterProtoHook(cmd_kill, "KILL", "Autoconverted. TODO: document me.");
2283 CtdlRegisterProtoHook(cmd_cre8, "CRE8", "Autoconverted. TODO: document me.");
2284 CtdlRegisterProtoHook(cmd_einf, "EINF", "Autoconverted. TODO: document me.");
2285 CtdlRegisterProtoHook(cmd_lflr, "LFLR", "Autoconverted. TODO: document me.");
2286 CtdlRegisterProtoHook(cmd_cflr, "CFLR", "Autoconverted. TODO: document me.");
2287 CtdlRegisterProtoHook(cmd_kflr, "KFLR", "Autoconverted. TODO: document me.");
2288 CtdlRegisterProtoHook(cmd_eflr, "EFLR", "Autoconverted. TODO: document me.");
2290 /* return our Subversion id for the Log */