4 * Server functions which perform operations on room objects.
16 #if TIME_WITH_SYS_TIME
17 # include <sys/time.h>
21 # include <sys/time.h>
34 #include "sysdep_decls.h"
38 #include "citserver.h"
41 #include "citadel_dirs.h"
43 struct floor *floorcache[MAXFLOORS];
46 * Retrieve access control information for any user/room pair
48 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf,
49 int *result, int *view)
54 /* for internal programs, always do everything */
55 if (((CC->internal_pgm)) && (roombuf->QRflags & QR_INUSE)) {
56 retval = (UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED);
61 /* Locate any applicable user/room relationships */
62 CtdlGetRelationship(&vbuf, userbuf, roombuf);
64 /* Force the properties of the Aide room */
65 if (!strcasecmp(roombuf->QRname, config.c_aideroom)) {
66 if (userbuf->axlevel >= 6) {
67 retval = UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
74 /* If this is a public room, it's accessible... */
75 if ( ((roombuf->QRflags & QR_PRIVATE) == 0)
76 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
77 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
80 /* If this is a preferred users only room, check access level */
81 if (roombuf->QRflags & QR_PREFONLY) {
82 if (userbuf->axlevel < 5) {
83 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
87 /* For private rooms, check the generation number matchups */
88 if ( (roombuf->QRflags & QR_PRIVATE)
89 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
91 /* An explicit match means the user belongs in this room */
92 if (vbuf.v_flags & V_ACCESS) {
93 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
95 /* Otherwise, check if this is a guess-name or passworded
96 * room. If it is, a goto may at least be attempted
98 else if ((roombuf->QRflags & QR_PRIVATE)
99 || (roombuf->QRflags & QR_PASSWORDED)) {
100 retval = retval & ~UA_KNOWN;
101 retval = retval | UA_GOTOALLOWED;
105 /* For mailbox rooms, also check the namespace */
106 /* Also, mailbox owners can delete their messages */
107 if (roombuf->QRflags & QR_MAILBOX) {
108 if (userbuf->usernum == atol(roombuf->QRname)) {
109 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
111 /* An explicit match means the user belongs in this room */
112 if (vbuf.v_flags & V_ACCESS) {
113 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
117 /* For non-mailbox rooms... */
120 /* User is allowed to post in the room unless:
121 * - User is not validated
122 * - User has no net privileges and it is a shared network room
123 * - It is a read-only room
125 int post_allowed = 1;
126 if (CC->user.axlevel < 2) post_allowed = 0;
127 if ((CC->user.axlevel < 4) && (CC->room.QRflags & QR_NETWORK)) post_allowed = 0;
128 if (roombuf->QRflags & QR_READONLY) post_allowed = 0;
130 retval = retval | UA_POSTALLOWED;
133 /* If "collaborative deletion" is active for this room, any user who can post
134 * is also allowed to delete
136 if (CC->room.QRflags2 & QR2_COLLABDEL) {
137 if (retval & UA_POSTALLOWED) {
138 retval = retval | UA_DELETEALLOWED;
144 /* Check to see if the user has forgotten this room */
145 if (vbuf.v_flags & V_FORGET) {
146 retval = retval & ~UA_KNOWN;
147 if ( ( ((roombuf->QRflags & QR_PRIVATE) == 0)
148 && ((roombuf->QRflags & QR_MAILBOX) == 0) )
149 || ( (roombuf->QRflags & QR_MAILBOX)
150 && (atol(roombuf->QRname) == CC->user.usernum))) {
151 retval = retval | UA_ZAPPED;
154 /* If user is explicitly locked out of this room, deny everything */
155 if (vbuf.v_flags & V_LOCKOUT) {
156 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED & ~UA_POSTALLOWED;
159 /* Aides get access to all private rooms */
160 if ( (userbuf->axlevel >= 6)
161 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
162 if (vbuf.v_flags & V_FORGET) {
163 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED;
166 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED;
170 /* Aides can gain access to mailboxes as well, but they don't show
173 if ( (userbuf->axlevel >= 6)
174 && (roombuf->QRflags & QR_MAILBOX) ) {
175 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED;
178 /* Aides and Room Aides have admin privileges */
179 if ( (userbuf->axlevel >= 6)
180 || (userbuf->usernum == roombuf->QRroomaide)
182 retval = retval | UA_ADMINALLOWED | UA_DELETEALLOWED;
185 NEWMSG: /* By the way, we also check for the presence of new messages */
186 if (is_msg_in_sequence_set(vbuf.v_seen, roombuf->QRhighest) == 0) {
187 retval = retval | UA_HASNEWMSGS;
190 /* System rooms never show up in the list. */
191 if (roombuf->QRflags2 & QR2_SYSTEM) {
192 retval = retval & ~UA_KNOWN;
196 /* Now give the caller the information it wants. */
197 if (result != NULL) *result = retval;
198 if (view != NULL) *view = vbuf.v_view;
202 * Self-checking stuff for a room record read into memory
204 void room_sanity_check(struct ctdlroom *qrbuf)
206 /* Mailbox rooms are always on the lowest floor */
207 if (qrbuf->QRflags & QR_MAILBOX) {
210 /* Listing order of 0 is illegal except for base rooms */
211 if (qrbuf->QRorder == 0)
212 if (!(qrbuf->QRflags & QR_MAILBOX) &&
213 strncasecmp(qrbuf->QRname, config.c_baseroom, ROOMNAMELEN)
215 strncasecmp(qrbuf->QRname, config.c_aideroom, ROOMNAMELEN))
221 * getroom() - retrieve room data from disk
223 int getroom(struct ctdlroom *qrbuf, char *room_name)
225 struct cdbdata *cdbqr;
226 char lowercase_name[ROOMNAMELEN];
227 char personal_lowercase_name[ROOMNAMELEN];
230 for (a = 0; room_name[a] && a < sizeof lowercase_name - 1; ++a) {
231 lowercase_name[a] = tolower(room_name[a]);
233 lowercase_name[a] = 0;
235 memset(qrbuf, 0, sizeof(struct ctdlroom));
237 /* First, try the public namespace */
238 cdbqr = cdb_fetch(CDB_ROOMS,
239 lowercase_name, strlen(lowercase_name));
241 /* If that didn't work, try the user's personal namespace */
243 snprintf(personal_lowercase_name,
244 sizeof personal_lowercase_name, "%010ld.%s",
245 CC->user.usernum, lowercase_name);
246 cdbqr = cdb_fetch(CDB_ROOMS,
247 personal_lowercase_name,
248 strlen(personal_lowercase_name));
251 memcpy(qrbuf, cdbqr->ptr,
252 ((cdbqr->len > sizeof(struct ctdlroom)) ?
253 sizeof(struct ctdlroom) : cdbqr->len));
256 room_sanity_check(qrbuf);
265 * lgetroom() - same as getroom() but locks the record (if supported)
267 int lgetroom(struct ctdlroom *qrbuf, char *room_name)
270 retval = getroom(qrbuf, room_name);
271 if (retval == 0) begin_critical_section(S_ROOMS);
277 * b_putroom() - back end to putroom() and b_deleteroom()
278 * (if the supplied buffer is NULL, delete the room record)
280 void b_putroom(struct ctdlroom *qrbuf, char *room_name)
282 char lowercase_name[ROOMNAMELEN];
287 bptr = lowercase_name;
288 while (!IsEmptyStr(aptr))
290 *bptr = tolower(*aptr);
296 len = bptr - lowercase_name;
298 cdb_delete(CDB_ROOMS,
299 lowercase_name, len);
301 time(&qrbuf->QRmtime);
304 qrbuf, sizeof(struct ctdlroom));
310 * putroom() - store room data to disk
312 void putroom(struct ctdlroom *qrbuf) {
313 b_putroom(qrbuf, qrbuf->QRname);
318 * b_deleteroom() - delete a room record from disk
320 void b_deleteroom(char *room_name) {
321 b_putroom(NULL, room_name);
327 * lputroom() - same as putroom() but unlocks the record (if supported)
329 void lputroom(struct ctdlroom *qrbuf)
333 end_critical_section(S_ROOMS);
337 /****************************************************************************/
340 * getfloor() - retrieve floor data from disk
342 void getfloor(struct floor *flbuf, int floor_num)
344 struct cdbdata *cdbfl;
346 memset(flbuf, 0, sizeof(struct floor));
347 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
349 memcpy(flbuf, cdbfl->ptr,
350 ((cdbfl->len > sizeof(struct floor)) ?
351 sizeof(struct floor) : cdbfl->len));
354 if (floor_num == 0) {
355 safestrncpy(flbuf->f_name, "Main Floor",
356 sizeof flbuf->f_name);
357 flbuf->f_flags = F_INUSE;
358 flbuf->f_ref_count = 3;
365 * lgetfloor() - same as getfloor() but locks the record (if supported)
367 void lgetfloor(struct floor *flbuf, int floor_num)
370 begin_critical_section(S_FLOORTAB);
371 getfloor(flbuf, floor_num);
376 * cgetfloor() - Get floor record from *cache* (loads from disk if needed)
378 * This is strictly a performance hack.
380 struct floor *cgetfloor(int floor_num) {
381 static int initialized = 0;
384 struct floor *fl = NULL;
386 begin_critical_section(S_FLOORCACHE);
387 if (initialized == 0) {
388 for (i=0; i<MAXFLOORS; ++i) {
389 floorcache[floor_num] = NULL;
393 if (floorcache[floor_num] == NULL) {
396 end_critical_section(S_FLOORCACHE);
399 fl = malloc(sizeof(struct floor));
400 getfloor(fl, floor_num);
401 begin_critical_section(S_FLOORCACHE);
402 if (floorcache[floor_num] != NULL) {
403 free(floorcache[floor_num]);
405 floorcache[floor_num] = fl;
406 end_critical_section(S_FLOORCACHE);
409 return(floorcache[floor_num]);
415 * putfloor() - store floor data on disk
417 void putfloor(struct floor *flbuf, int floor_num)
419 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
420 begin_critical_section(S_FLOORCACHE);
421 if (floorcache[floor_num] != NULL) {
422 free(floorcache[floor_num]);
423 floorcache[floor_num] = malloc(sizeof(struct floor));
424 memcpy(floorcache[floor_num], flbuf, sizeof(struct floor));
426 end_critical_section(S_FLOORCACHE);
428 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
429 flbuf, sizeof(struct floor));
434 * lputfloor() - same as putfloor() but unlocks the record (if supported)
436 void lputfloor(struct floor *flbuf, int floor_num)
439 putfloor(flbuf, floor_num);
440 end_critical_section(S_FLOORTAB);
446 * Traverse the room file...
448 void ForEachRoom(void (*CallBack) (struct ctdlroom *EachRoom, void *out_data),
451 struct ctdlroom qrbuf;
452 struct cdbdata *cdbqr;
454 cdb_rewind(CDB_ROOMS);
456 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
457 memset(&qrbuf, 0, sizeof(struct ctdlroom));
458 memcpy(&qrbuf, cdbqr->ptr,
459 ((cdbqr->len > sizeof(struct ctdlroom)) ?
460 sizeof(struct ctdlroom) : cdbqr->len));
462 room_sanity_check(&qrbuf);
463 if (qrbuf.QRflags & QR_INUSE)
464 (*CallBack)(&qrbuf, in_data);
470 * delete_msglist() - delete room message pointers
472 void delete_msglist(struct ctdlroom *whichroom)
474 struct cdbdata *cdbml;
476 /* Make sure the msglist we're deleting actually exists, otherwise
477 * gdbm will complain when we try to delete an invalid record
479 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
483 /* Go ahead and delete it */
484 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
492 * sort message pointers
493 * (returns new msg count)
495 int sort_msglist(long listptrs[], int oldcount)
506 for (a = numitems - 2; a >= 0; --a) {
507 for (b = 0; b <= a; ++b) {
508 if (listptrs[b] > (listptrs[b + 1])) {
510 hold2 = listptrs[b + 1];
512 listptrs[b + 1] = hold1;
517 /* and yank any nulls */
518 while ((numitems > 0) && (listptrs[0] == 0L)) {
519 memcpy(&listptrs[0], &listptrs[1],
520 (sizeof(long) * (numitems - 1)));
529 * Determine whether a given room is non-editable.
531 int is_noneditable(struct ctdlroom *qrbuf)
534 /* Mail> rooms are non-editable */
535 if ( (qrbuf->QRflags & QR_MAILBOX)
536 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
539 /* Everything else is editable */
546 * Back-back-end for all room listing commands
548 void list_roomname(struct ctdlroom *qrbuf, int ra, int current_view, int default_view)
550 char truncated_roomname[ROOMNAMELEN];
552 /* For my own mailbox rooms, chop off the owner prefix */
553 if ( (qrbuf->QRflags & QR_MAILBOX)
554 && (atol(qrbuf->QRname) == CC->user.usernum) ) {
555 safestrncpy(truncated_roomname, qrbuf->QRname, sizeof truncated_roomname);
556 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
557 cprintf("%s", truncated_roomname);
559 /* For all other rooms, just display the name in its entirety */
561 cprintf("%s", qrbuf->QRname);
564 /* ...and now the other parameters */
565 cprintf("|%u|%d|%d|%d|%d|%d|%d|%ld|\n",
567 (int) qrbuf->QRfloor,
568 (int) qrbuf->QRorder,
569 (int) qrbuf->QRflags2,
579 * cmd_lrms() - List all accessible rooms, known or forgotten
581 void cmd_lrms_backend(struct ctdlroom *qrbuf, void *data)
583 int FloorBeingSearched = (-1);
587 FloorBeingSearched = *(int *)data;
588 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
590 if ((( ra & (UA_KNOWN | UA_ZAPPED)))
591 && ((qrbuf->QRfloor == (FloorBeingSearched))
592 || ((FloorBeingSearched) < 0)))
593 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
596 void cmd_lrms(char *argbuf)
598 int FloorBeingSearched = (-1);
599 if (!IsEmptyStr(argbuf))
600 FloorBeingSearched = extract_int(argbuf, 0);
602 if (CtdlAccessCheck(ac_logged_in)) return;
604 if (getuser(&CC->user, CC->curr_user)) {
605 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
608 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
610 ForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
617 * cmd_lkra() - List all known rooms
619 void cmd_lkra_backend(struct ctdlroom *qrbuf, void *data)
621 int FloorBeingSearched = (-1);
625 FloorBeingSearched = *(int *)data;
626 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
628 if ((( ra & (UA_KNOWN)))
629 && ((qrbuf->QRfloor == (FloorBeingSearched))
630 || ((FloorBeingSearched) < 0)))
631 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
634 void cmd_lkra(char *argbuf)
636 int FloorBeingSearched = (-1);
637 if (!IsEmptyStr(argbuf))
638 FloorBeingSearched = extract_int(argbuf, 0);
640 if (CtdlAccessCheck(ac_logged_in)) return;
642 if (getuser(&CC->user, CC->curr_user)) {
643 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
646 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
648 ForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
654 void cmd_lprm_backend(struct ctdlroom *qrbuf, void *data)
656 int FloorBeingSearched = (-1);
660 FloorBeingSearched = *(int *)data;
661 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
663 if ( ((qrbuf->QRflags & QR_PRIVATE) == 0)
664 && ((qrbuf->QRflags & QR_MAILBOX) == 0)
665 && ((qrbuf->QRfloor == (FloorBeingSearched))
666 || ((FloorBeingSearched) < 0)))
667 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
670 void cmd_lprm(char *argbuf)
672 int FloorBeingSearched = (-1);
673 if (!IsEmptyStr(argbuf))
674 FloorBeingSearched = extract_int(argbuf, 0);
676 cprintf("%d Publiic rooms:\n", LISTING_FOLLOWS);
678 ForEachRoom(cmd_lprm_backend, &FloorBeingSearched);
685 * cmd_lkrn() - List all known rooms with new messages
687 void cmd_lkrn_backend(struct ctdlroom *qrbuf, void *data)
689 int FloorBeingSearched = (-1);
693 FloorBeingSearched = *(int *)data;
694 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
697 && (ra & UA_HASNEWMSGS)
698 && ((qrbuf->QRfloor == (FloorBeingSearched))
699 || ((FloorBeingSearched) < 0)))
700 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
703 void cmd_lkrn(char *argbuf)
705 int FloorBeingSearched = (-1);
706 if (!IsEmptyStr(argbuf))
707 FloorBeingSearched = extract_int(argbuf, 0);
709 if (CtdlAccessCheck(ac_logged_in)) return;
711 if (getuser(&CC->user, CC->curr_user)) {
712 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
715 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
717 ForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
724 * cmd_lkro() - List all known rooms
726 void cmd_lkro_backend(struct ctdlroom *qrbuf, void *data)
728 int FloorBeingSearched = (-1);
732 FloorBeingSearched = *(int *)data;
733 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
736 && ((ra & UA_HASNEWMSGS) == 0)
737 && ((qrbuf->QRfloor == (FloorBeingSearched))
738 || ((FloorBeingSearched) < 0)))
739 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
742 void cmd_lkro(char *argbuf)
744 int FloorBeingSearched = (-1);
745 if (!IsEmptyStr(argbuf))
746 FloorBeingSearched = extract_int(argbuf, 0);
748 if (CtdlAccessCheck(ac_logged_in)) return;
750 if (getuser(&CC->user, CC->curr_user)) {
751 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
754 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
756 ForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
763 * cmd_lzrm() - List all forgotten rooms
765 void cmd_lzrm_backend(struct ctdlroom *qrbuf, void *data)
767 int FloorBeingSearched = (-1);
771 FloorBeingSearched = *(int *)data;
772 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
774 if ((ra & UA_GOTOALLOWED)
776 && ((qrbuf->QRfloor == (FloorBeingSearched))
777 || ((FloorBeingSearched) < 0)))
778 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
781 void cmd_lzrm(char *argbuf)
783 int FloorBeingSearched = (-1);
784 if (!IsEmptyStr(argbuf))
785 FloorBeingSearched = extract_int(argbuf, 0);
787 if (CtdlAccessCheck(ac_logged_in)) return;
789 if (getuser(&CC->user, CC->curr_user)) {
790 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
793 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
795 ForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
801 * Make the specified room the current room for this session. No validation
802 * or access control is done here -- the caller should make sure that the
803 * specified room exists and is ok to access.
805 void usergoto(char *where, int display_result, int transiently,
806 int *retmsgs, int *retnew)
809 int new_messages = 0;
810 int old_messages = 0;
811 int total_messages = 0;
815 int newmailcount = 0;
817 char truncated_roomname[ROOMNAMELEN];
818 struct cdbdata *cdbfr;
819 long *msglist = NULL;
821 unsigned int original_v_flags;
824 char setstr[128], lostr[64], histr[64];
828 /* If the supplied room name is NULL, the caller wants us to know that
829 * it has already copied the room record into CC->room, so
830 * we can skip the extra database fetch.
833 safestrncpy(CC->room.QRname, where, sizeof CC->room.QRname);
834 getroom(&CC->room, where);
837 /* Take care of all the formalities. */
839 begin_critical_section(S_USERS);
840 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
841 original_v_flags = vbuf.v_flags;
843 /* Know the room ... but not if it's the page log room, or if the
844 * caller specified that we're only entering this room transiently.
846 if ((strcasecmp(CC->room.QRname, config.c_logpages))
847 && (transiently == 0) ) {
848 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
849 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
852 /* Only rewrite the database record if we changed something */
853 if (vbuf.v_flags != original_v_flags) {
854 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
856 end_critical_section(S_USERS);
858 /* Check for new mail */
859 newmailcount = NewMailCount();
861 /* set info to 1 if the user needs to read the room's info file */
862 if (CC->room.QRinfo > vbuf.v_lastseen) {
866 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
868 msglist = (long *) cdbfr->ptr;
869 cdbfr->ptr = NULL; /* usergoto() now owns this memory */
870 num_msgs = cdbfr->len / sizeof(long);
875 for (a=0; a<num_msgs; ++a) {
876 if (msglist[a] > 0L) ++total_messages;
878 new_messages = num_msgs;
879 num_sets = num_tokens(vbuf.v_seen, ',');
880 for (s=0; s<num_sets; ++s) {
881 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
883 extract_token(lostr, setstr, 0, ':', sizeof lostr);
884 if (num_tokens(setstr, ':') >= 2) {
885 extract_token(histr, setstr, 1, ':', sizeof histr);
886 if (!strcmp(histr, "*")) {
887 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
891 strcpy(histr, lostr);
896 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
897 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
903 new_messages = total_messages - old_messages;
905 if (msglist != NULL) free(msglist);
907 if (CC->room.QRflags & QR_MAILBOX)
912 if ((CC->room.QRroomaide == CC->user.usernum)
913 || (CC->user.axlevel >= 6))
918 safestrncpy(truncated_roomname, CC->room.QRname, sizeof truncated_roomname);
919 if ( (CC->room.QRflags & QR_MAILBOX)
920 && (atol(CC->room.QRname) == CC->user.usernum) ) {
921 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
924 if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
928 if (retmsgs != NULL) *retmsgs = total_messages;
929 if (retnew != NULL) *retnew = new_messages;
930 lprintf(CTDL_DEBUG, "<%s> %d new of %d total messages\n",
932 new_messages, total_messages
935 CC->curr_view = (int)vbuf.v_view;
937 if (display_result) {
938 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|%d|\n",
939 CIT_OK, CtdlCheckExpress(),
944 (int)CC->room.QRflags,
945 (long)CC->room.QRhighest,
946 (long)vbuf.v_lastseen,
950 (int)CC->room.QRfloor,
952 (int)CC->room.QRdefaultview,
954 (int)CC->room.QRflags2
961 * Handle some of the macro named rooms
963 void convert_room_name_macros(char *towhere, size_t maxlen) {
964 if (!strcasecmp(towhere, "_BASEROOM_")) {
965 safestrncpy(towhere, config.c_baseroom, maxlen);
967 else if (!strcasecmp(towhere, "_MAIL_")) {
968 safestrncpy(towhere, MAILROOM, maxlen);
970 else if (!strcasecmp(towhere, "_TRASH_")) {
971 safestrncpy(towhere, USERTRASHROOM, maxlen);
973 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
974 safestrncpy(towhere, config.c_twitroom, maxlen);
976 else if (!strcasecmp(towhere, "_CALENDAR_")) {
977 safestrncpy(towhere, USERCALENDARROOM, maxlen);
979 else if (!strcasecmp(towhere, "_TASKS_")) {
980 safestrncpy(towhere, USERTASKSROOM, maxlen);
982 else if (!strcasecmp(towhere, "_CONTACTS_")) {
983 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
985 else if (!strcasecmp(towhere, "_NOTES_")) {
986 safestrncpy(towhere, USERNOTESROOM, maxlen);
992 * cmd_goto() - goto a new room
994 void cmd_goto(char *gargs)
996 struct ctdlroom QRscratch;
1000 char augmented_roomname[ROOMNAMELEN];
1001 char towhere[ROOMNAMELEN];
1003 int transiently = 0;
1005 if (CtdlAccessCheck(ac_logged_in)) return;
1007 extract_token(towhere, gargs, 0, '|', sizeof towhere);
1008 extract_token(password, gargs, 1, '|', sizeof password);
1009 transiently = extract_int(gargs, 2);
1011 getuser(&CC->user, CC->curr_user);
1014 * Handle some of the macro named rooms
1016 convert_room_name_macros(towhere, sizeof towhere);
1018 /* First try a regular match */
1019 c = getroom(&QRscratch, towhere);
1021 /* Then try a mailbox name match */
1023 MailboxName(augmented_roomname, sizeof augmented_roomname,
1024 &CC->user, towhere);
1025 c = getroom(&QRscratch, augmented_roomname);
1027 safestrncpy(towhere, augmented_roomname, sizeof towhere);
1030 /* And if the room was found... */
1033 /* Let internal programs go directly to any room. */
1034 if (CC->internal_pgm) {
1035 memcpy(&CC->room, &QRscratch,
1036 sizeof(struct ctdlroom));
1037 usergoto(NULL, 1, transiently, NULL, NULL);
1041 /* See if there is an existing user/room relationship */
1042 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
1044 /* normal clients have to pass through security */
1045 if (ra & UA_GOTOALLOWED) {
1050 if ((QRscratch.QRflags & QR_MAILBOX) &&
1051 ((ra & UA_GOTOALLOWED))) {
1052 memcpy(&CC->room, &QRscratch,
1053 sizeof(struct ctdlroom));
1054 usergoto(NULL, 1, transiently, NULL, NULL);
1056 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
1057 ((ra & UA_KNOWN) == 0) &&
1058 (strcasecmp(QRscratch.QRpasswd, password)) &&
1059 (CC->user.axlevel < 6)
1061 cprintf("%d wrong or missing passwd\n",
1062 ERROR + PASSWORD_REQUIRED);
1064 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
1065 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
1066 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
1067 ((ra & UA_KNOWN) == 0) &&
1068 (CC->user.axlevel < 6)
1070 lprintf(CTDL_DEBUG, "Failed to acquire private room\n");
1072 memcpy(&CC->room, &QRscratch,
1073 sizeof(struct ctdlroom));
1074 usergoto(NULL, 1, transiently, NULL, NULL);
1080 cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
1086 struct ctdluser temp;
1087 struct cdbdata *cdbus;
1090 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
1091 cdb_rewind(CDB_USERS);
1092 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1093 memset(&temp, 0, sizeof temp);
1094 memcpy(&temp, cdbus->ptr, sizeof temp);
1097 CtdlRoomAccess(&CC->room, &temp, &ra, NULL);
1098 if ((CC->room.QRflags & QR_INUSE)
1101 cprintf("%s\n", temp.fullname);
1108 * RDIR command for room directory
1116 struct stat statbuf;
1117 char tempfilename[PATH_MAX];
1119 if (CtdlAccessCheck(ac_logged_in)) return;
1120 CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
1122 getroom(&CC->room, CC->room.QRname);
1123 getuser(&CC->user, CC->curr_user);
1125 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
1126 cprintf("%d not here.\n", ERROR + NOT_HERE);
1129 if (((CC->room.QRflags & QR_VISDIR) == 0)
1130 && (CC->user.axlevel < 6)
1131 && (CC->user.usernum != CC->room.QRroomaide)) {
1132 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1135 cprintf("%d %s|%s/%s\n",
1139 CC->room.QRdirname);
1141 snprintf(buf, sizeof buf,
1142 "ls %s/%s >%s 2>/dev/null",
1148 snprintf(buf, sizeof buf,
1151 CC->room.QRdirname);
1152 fd = fopen(buf, "r");
1154 fd = fopen("/dev/null", "r");
1156 ls = fopen(tempfilename, "r");
1157 while (fgets(flnm, sizeof flnm, ls) != NULL) {
1158 flnm[strlen(flnm) - 1] = 0;
1159 if (strcasecmp(flnm, "filedir")) {
1160 snprintf(buf, sizeof buf,
1165 stat(buf, &statbuf);
1166 safestrncpy(comment, "", sizeof comment);
1168 while ((fgets(buf, sizeof buf, fd) != NULL)
1169 && (IsEmptyStr(comment))) {
1170 buf[strlen(buf) - 1] = 0;
1171 if ((!strncasecmp(buf, flnm, strlen(flnm)))
1172 && (buf[strlen(flnm)] == ' '))
1173 safestrncpy(comment,
1174 &buf[strlen(flnm) + 1],
1177 cprintf("%s|%ld|%s\n", flnm, (long)statbuf.st_size, comment);
1182 unlink(tempfilename);
1188 * get room parameters (aide or room aide command)
1192 if (CtdlAccessCheck(ac_room_aide)) return;
1194 getroom(&CC->room, CC->room.QRname);
1195 cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1199 ((CC->room.QRflags & QR_MAILBOX) ?
1200 &CC->room.QRname[11] : CC->room.QRname),
1202 ((CC->room.QRflags & QR_PASSWORDED) ?
1203 CC->room.QRpasswd : ""),
1205 ((CC->room.QRflags & QR_DIRECTORY) ?
1206 CC->room.QRdirname : ""),
1209 (int) CC->room.QRfloor,
1210 (int) CC->room.QRorder,
1212 CC->room.QRdefaultview,
1219 * Back end function to rename a room.
1220 * You can also specify which floor to move the room to, or specify -1 to
1221 * keep the room on the same floor it was on.
1223 * If you are renaming a mailbox room, you must supply the namespace prefix
1224 * in *at least* the old name!
1226 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1228 struct ctdlroom qrbuf;
1229 struct ctdlroom qrtmp;
1234 char actual_old_name[ROOMNAMELEN];
1236 lprintf(CTDL_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1237 old_name, new_name, new_floor);
1239 if (new_floor >= 0) {
1240 fl = cgetfloor(new_floor);
1241 if ((fl->f_flags & F_INUSE) == 0) {
1242 return(crr_invalid_floor);
1246 begin_critical_section(S_ROOMS);
1248 if ( (getroom(&qrtmp, new_name) == 0)
1249 && (strcasecmp(new_name, old_name)) ) {
1250 ret = crr_already_exists;
1253 else if (getroom(&qrbuf, old_name) != 0) {
1254 ret = crr_room_not_found;
1257 else if ( (CC->user.axlevel < 6) && (!CC->internal_pgm)
1258 && (CC->user.usernum != qrbuf.QRroomaide)
1259 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) ) {
1260 ret = crr_access_denied;
1263 else if (is_noneditable(&qrbuf)) {
1264 ret = crr_noneditable;
1269 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1270 if (qrbuf.QRflags & QR_MAILBOX) {
1271 owner = atol(qrbuf.QRname);
1273 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1274 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1275 "%010ld.%s", owner, new_name);
1278 safestrncpy(qrbuf.QRname, new_name,
1279 sizeof(qrbuf.QRname));
1282 /* Reject change of floor for baseroom/aideroom */
1283 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1284 !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1288 /* Take care of floor stuff */
1289 old_floor = qrbuf.QRfloor;
1290 if (new_floor < 0) {
1291 new_floor = old_floor;
1293 qrbuf.QRfloor = new_floor;
1296 begin_critical_section(S_CONFIG);
1298 /* If baseroom/aideroom name changes, update config */
1299 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1300 safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1303 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1304 safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1308 end_critical_section(S_CONFIG);
1310 /* If the room name changed, then there are now two room
1311 * records, so we have to delete the old one.
1313 if (strcasecmp(new_name, old_name)) {
1314 b_deleteroom(actual_old_name);
1320 end_critical_section(S_ROOMS);
1322 /* Adjust the floor reference counts if necessary */
1323 if (new_floor != old_floor) {
1324 lgetfloor(&flbuf, old_floor);
1325 --flbuf.f_ref_count;
1326 lputfloor(&flbuf, old_floor);
1327 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1328 lgetfloor(&flbuf, new_floor);
1329 ++flbuf.f_ref_count;
1330 lputfloor(&flbuf, new_floor);
1331 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1334 /* ...and everybody say "YATTA!" */
1340 * set room parameters (aide or room aide command)
1342 void cmd_setr(char *args)
1348 char new_name[ROOMNAMELEN];
1350 if (CtdlAccessCheck(ac_logged_in)) return;
1352 if (num_parms(args) >= 6) {
1353 new_floor = extract_int(args, 5);
1355 new_floor = (-1); /* don't change the floor */
1358 /* When is a new name more than just a new name? When the old name
1359 * has a namespace prefix.
1361 if (CC->room.QRflags & QR_MAILBOX) {
1362 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
1364 safestrncpy(new_name, "", sizeof new_name);
1366 extract_token(&new_name[strlen(new_name)], args, 0, '|', (sizeof new_name - strlen(new_name)));
1368 r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
1370 if (r == crr_room_not_found) {
1371 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
1372 } else if (r == crr_already_exists) {
1373 cprintf("%d '%s' already exists.\n",
1374 ERROR + ALREADY_EXISTS, new_name);
1375 } else if (r == crr_noneditable) {
1376 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
1377 } else if (r == crr_invalid_floor) {
1378 cprintf("%d Target floor does not exist.\n",
1379 ERROR + INVALID_FLOOR_OPERATION);
1380 } else if (r == crr_access_denied) {
1381 cprintf("%d You do not have permission to edit '%s'\n",
1382 ERROR + HIGHER_ACCESS_REQUIRED,
1384 } else if (r != crr_ok) {
1385 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1386 ERROR + INTERNAL_ERROR, r);
1393 getroom(&CC->room, new_name);
1395 /* Now we have to do a bunch of other stuff */
1397 if (num_parms(args) >= 7) {
1398 new_order = extract_int(args, 6);
1401 if (new_order > 127)
1405 lgetroom(&CC->room, CC->room.QRname);
1407 /* Directory room */
1408 extract_token(buf, args, 2, '|', sizeof buf);
1410 safestrncpy(CC->room.QRdirname, buf,
1411 sizeof CC->room.QRdirname);
1414 if (num_parms(args) >= 8) {
1415 CC->room.QRdefaultview = extract_int(args, 7);
1418 /* Second set of flags */
1419 if (num_parms(args) >= 9) {
1420 CC->room.QRflags2 = extract_int(args, 8);
1424 CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
1425 /* Clean up a client boo-boo: if the client set the room to
1426 * guess-name or passworded, ensure that the private flag is
1429 if ((CC->room.QRflags & QR_GUESSNAME)
1430 || (CC->room.QRflags & QR_PASSWORDED))
1431 CC->room.QRflags |= QR_PRIVATE;
1433 /* Some changes can't apply to BASEROOM */
1434 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1436 CC->room.QRorder = 0;
1437 CC->room.QRpasswd[0] = '\0';
1438 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1439 QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1440 CC->room.QRflags |= QR_PERMANENT;
1442 /* March order (doesn't apply to AIDEROOM) */
1443 if (num_parms(args) >= 7)
1444 CC->room.QRorder = (char) new_order;
1446 extract_token(buf, args, 1, '|', sizeof buf);
1448 safestrncpy(CC->room.QRpasswd, buf,
1449 sizeof CC->room.QRpasswd);
1450 /* Kick everyone out if the client requested it
1451 * (by changing the room's generation number)
1453 if (extract_int(args, 4)) {
1454 time(&CC->room.QRgen);
1457 /* Some changes can't apply to AIDEROOM */
1458 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1460 CC->room.QRorder = 0;
1461 CC->room.QRflags &= ~QR_MAILBOX;
1462 CC->room.QRflags |= QR_PERMANENT;
1465 /* Write the room record back to disk */
1466 lputroom(&CC->room);
1468 /* Create a room directory if necessary */
1469 if (CC->room.QRflags & QR_DIRECTORY) {
1470 snprintf(buf, sizeof buf,"%s/%s",
1472 CC->room.QRdirname);
1475 snprintf(buf, sizeof buf, "The room \"%s\" has been edited by %s.\n",
1476 CC->room.QRname, CC->curr_user);
1477 aide_message(buf, "Room modification Message");
1478 cprintf("%d Ok\n", CIT_OK);
1484 * get the name of the room aide for this room
1488 struct ctdluser usbuf;
1490 if (CtdlAccessCheck(ac_logged_in)) return;
1492 if (getuserbynumber(&usbuf, CC->room.QRroomaide) == 0) {
1493 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1495 cprintf("%d \n", CIT_OK);
1501 * set the room aide for this room
1503 void cmd_seta(char *new_ra)
1505 struct ctdluser usbuf;
1510 if (CtdlAccessCheck(ac_room_aide)) return;
1512 if (getuser(&usbuf, new_ra) != 0) {
1515 newu = usbuf.usernum;
1518 lgetroom(&CC->room, CC->room.QRname);
1520 if (CC->room.QRroomaide != newu) {
1523 CC->room.QRroomaide = newu;
1524 lputroom(&CC->room);
1527 * We have to post the change notice _after_ writing changes to
1528 * the room table, otherwise it would deadlock!
1530 if (post_notice == 1) {
1531 if (!IsEmptyStr(usbuf.fullname))
1532 snprintf(buf, sizeof buf,
1533 "%s is now the room aide for \"%s\".\n",
1534 usbuf.fullname, CC->room.QRname);
1536 snprintf(buf, sizeof buf,
1537 "There is now no room aide for \"%s\".\n",
1539 aide_message(buf, "Aide Room Modification");
1541 cprintf("%d Ok\n", CIT_OK);
1545 * retrieve info file for this room
1553 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_info_dir);
1554 info_fp = fopen(filename, "r");
1556 if (info_fp == NULL) {
1557 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
1560 cprintf("%d Info:\n", LISTING_FOLLOWS);
1561 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1562 if (!IsEmptyStr(buf))
1563 buf[strlen(buf) - 1] = 0;
1564 cprintf("%s\n", buf);
1571 * Asynchronously schedule a room for deletion. The room will appear
1572 * deleted to the user(s), but it won't actually get purged from the
1573 * database until THE DREADED AUTO-PURGER makes its next run.
1575 void schedule_room_for_deletion(struct ctdlroom *qrbuf)
1577 char old_name[ROOMNAMELEN];
1580 lprintf(CTDL_NOTICE, "Scheduling room <%s> for deletion\n",
1583 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1585 getroom(qrbuf, qrbuf->QRname);
1587 /* Turn the room into a private mailbox owned by a user who doesn't
1588 * exist. This will immediately make the room invisible to everyone,
1589 * and qualify the room for purging.
1591 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1596 qrbuf->QRflags |= QR_MAILBOX;
1597 time(&qrbuf->QRgen); /* Use a timestamp as the new generation number */
1601 b_deleteroom(old_name);
1607 * Back end processing to delete a room and everything associated with it
1608 * (This one is synchronous and should only get called by THE DREADED
1609 * AUTO-PURGER in serv_expire.c. All user-facing code should call
1610 * the asynchronous schedule_room_for_deletion() instead.)
1612 void delete_room(struct ctdlroom *qrbuf)
1616 /* TODO: filename magic? does this realy work? */
1618 lprintf(CTDL_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1620 /* Delete the info file */
1621 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_info_dir);
1624 /* Delete the image file */
1625 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_image_dir);
1628 /* Delete the room's network config file */
1629 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1632 /* Delete the messages in the room
1633 * (Careful: this opens an S_ROOMS critical section!)
1635 CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
1637 /* Flag the room record as not in use */
1638 lgetroom(qrbuf, qrbuf->QRname);
1642 /* then decrement the reference count for the floor */
1643 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1644 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1645 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1647 /* Delete the room record from the database! */
1648 b_deleteroom(qrbuf->QRname);
1654 * Check access control for deleting a room
1656 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1658 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1662 if (is_noneditable(qr)) {
1667 * For mailboxes, check stuff
1669 if (qr->QRflags & QR_MAILBOX) {
1671 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1673 if (atol(qr->QRname) != CC->user.usernum) {
1674 return(0); /* not my room */
1677 /* Can't delete your Mail> room */
1678 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1680 /* Otherwise it's ok */
1685 * For normal rooms, just check for aide or room aide status.
1687 return(is_room_aide());
1691 * aide command: kill the current room
1693 void cmd_kill(char *argbuf)
1695 char deleted_room_name[ROOMNAMELEN];
1699 kill_ok = extract_int(argbuf, 0);
1701 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
1702 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1706 if (CC->room.QRflags & QR_MAILBOX) {
1707 safestrncpy(deleted_room_name, &CC->room.QRname[11], sizeof deleted_room_name);
1710 safestrncpy(deleted_room_name, CC->room.QRname, sizeof deleted_room_name);
1713 /* Do the dirty work */
1714 schedule_room_for_deletion(&CC->room);
1716 /* Return to the Lobby */
1717 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1719 /* tell the world what we did */
1720 snprintf(msg, sizeof msg, "The room \"%s\" has been deleted by %s.\n",
1721 deleted_room_name, CC->curr_user);
1722 aide_message(msg, "Room Purger Message");
1723 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1725 cprintf("%d ok to delete.\n", CIT_OK);
1731 * Internal code to create a new room (returns room flags)
1733 * Room types: 0=public, 1=guessname, 2=passworded, 3=inv-only,
1734 * 4=mailbox, 5=mailbox, but caller supplies namespace
1736 unsigned create_room(char *new_room_name,
1738 char *new_room_pass,
1745 struct ctdlroom qrbuf;
1749 lprintf(CTDL_DEBUG, "create_room(name=%s, type=%d, view=%d)\n",
1750 new_room_name, new_room_type, new_room_view);
1752 if (getroom(&qrbuf, new_room_name) == 0) {
1753 lprintf(CTDL_DEBUG, "%s already exists.\n", new_room_name);
1757 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1758 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1759 qrbuf.QRflags = QR_INUSE;
1760 if (new_room_type > 0)
1761 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1762 if (new_room_type == 1)
1763 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1764 if (new_room_type == 2)
1765 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1766 if ( (new_room_type == 4) || (new_room_type == 5) ) {
1767 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1768 /* qrbuf.QRflags2 |= QR2_SUBJECTREQ; */
1771 /* If the user is requesting a personal room, set up the room
1772 * name accordingly (prepend the user number)
1774 if (new_room_type == 4) {
1775 MailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1778 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1781 /* If the room is private, and the system administrator has elected
1782 * to automatically grant room aide privileges, do so now; otherwise,
1783 * set the room aide to undefined.
1785 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1786 qrbuf.QRroomaide = CC->user.usernum;
1788 qrbuf.QRroomaide = (-1L);
1792 * If the caller is only interested in testing whether this will work,
1793 * return now without creating the room.
1795 if (!really_create) return (qrbuf.QRflags);
1797 qrbuf.QRnumber = get_new_room_number();
1798 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1799 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1800 qrbuf.QRfloor = new_room_floor;
1801 qrbuf.QRdefaultview = new_room_view;
1803 /* save what we just did... */
1806 /* bump the reference count on whatever floor the room is on */
1807 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1808 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1809 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1811 /* Grant the creator access to the room unless the avoid_access
1812 * parameter was specified.
1814 if ( (CC->logged_in) && (avoid_access == 0) ) {
1815 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1816 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1817 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1818 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1821 /* resume our happy day */
1822 return (qrbuf.QRflags);
1829 void cmd_cre8(char *args)
1832 char new_room_name[ROOMNAMELEN];
1834 char new_room_pass[32];
1837 char *notification_message = NULL;
1840 int avoid_access = 0;
1842 cre8_ok = extract_int(args, 0);
1843 extract_token(new_room_name, args, 1, '|', sizeof new_room_name);
1844 new_room_name[ROOMNAMELEN - 1] = 0;
1845 new_room_type = extract_int(args, 2);
1846 extract_token(new_room_pass, args, 3, '|', sizeof new_room_pass);
1847 avoid_access = extract_int(args, 5);
1848 new_room_view = extract_int(args, 6);
1849 new_room_pass[9] = 0;
1852 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 1)) {
1853 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
1857 if (!strcasecmp(new_room_name, MAILROOM)) {
1858 cprintf("%d '%s' already exists.\n",
1859 ERROR + ALREADY_EXISTS, new_room_name);
1863 if (num_parms(args) >= 5) {
1864 fl = cgetfloor(extract_int(args, 4));
1866 cprintf("%d Invalid floor number.\n",
1867 ERROR + INVALID_FLOOR_OPERATION);
1870 else if ((fl->f_flags & F_INUSE) == 0) {
1871 cprintf("%d Invalid floor number.\n",
1872 ERROR + INVALID_FLOOR_OPERATION);
1875 new_room_floor = extract_int(args, 4);
1879 if (CtdlAccessCheck(ac_logged_in)) return;
1881 if (CC->user.axlevel < config.c_createax || CC->internal_pgm) {
1882 cprintf("%d You need higher access to create rooms.\n",
1883 ERROR + HIGHER_ACCESS_REQUIRED);
1887 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 0)) {
1888 cprintf("%d Ok to create rooms.\n", CIT_OK);
1892 if ((new_room_type < 0) || (new_room_type > 5)) {
1893 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
1897 if (new_room_type == 5) {
1898 if (CC->user.axlevel < 6) {
1899 cprintf("%d Higher access required\n",
1900 ERROR + HIGHER_ACCESS_REQUIRED);
1905 /* Check to make sure the requested room name doesn't already exist */
1906 newflags = create_room(new_room_name,
1907 new_room_type, new_room_pass, new_room_floor,
1908 0, avoid_access, new_room_view);
1909 if (newflags == 0) {
1910 cprintf("%d '%s' already exists.\n",
1911 ERROR + ALREADY_EXISTS, new_room_name);
1916 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
1920 /* If we reach this point, the room needs to be created. */
1922 newflags = create_room(new_room_name,
1923 new_room_type, new_room_pass, new_room_floor, 1, 0,
1926 /* post a message in Aide> describing the new room */
1927 notification_message = malloc(1024);
1928 snprintf(notification_message, 1024,
1929 "A new room called \"%s\" has been created by %s%s%s%s%s%s\n",
1932 ((newflags & QR_MAILBOX) ? " [personal]" : ""),
1933 ((newflags & QR_PRIVATE) ? " [private]" : ""),
1934 ((newflags & QR_GUESSNAME) ? " [hidden]" : ""),
1935 ((newflags & QR_PASSWORDED) ? " Password: " : ""),
1936 ((newflags & QR_PASSWORDED) ? new_room_pass : "")
1938 aide_message(notification_message, "Room Creation Message");
1939 free(notification_message);
1941 cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
1946 void cmd_einf(char *ok)
1947 { /* enter info file for current room */
1949 char infofilename[SIZ];
1954 if (CtdlAccessCheck(ac_room_aide)) return;
1956 if (atoi(ok) == 0) {
1957 cprintf("%d Ok.\n", CIT_OK);
1960 assoc_file_name(infofilename, sizeof infofilename, &CC->room, ctdl_info_dir);
1961 lprintf(CTDL_DEBUG, "opening\n");
1962 fp = fopen(infofilename, "w");
1963 lprintf(CTDL_DEBUG, "checking\n");
1965 cprintf("%d Cannot open %s: %s\n",
1966 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1969 cprintf("%d Send info...\n", SEND_LISTING);
1972 client_getln(buf, sizeof buf);
1973 if (strcmp(buf, "000"))
1974 fprintf(fp, "%s\n", buf);
1975 } while (strcmp(buf, "000"));
1978 /* now update the room index so people will see our new info */
1979 lgetroom(&CC->room, CC->room.QRname); /* lock so no one steps on us */
1980 CC->room.QRinfo = CC->room.QRhighest + 1L;
1981 lputroom(&CC->room);
1986 * cmd_lflr() - List all known floors
1993 if (CtdlAccessCheck(ac_logged_in)) return;
1995 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1997 for (a = 0; a < MAXFLOORS; ++a) {
1998 getfloor(&flbuf, a);
1999 if (flbuf.f_flags & F_INUSE) {
2000 cprintf("%d|%s|%d\n",
2012 * create a new floor
2014 void cmd_cflr(char *argbuf)
2016 char new_floor_name[256];
2019 int free_slot = (-1);
2022 extract_token(new_floor_name, argbuf, 0, '|', sizeof new_floor_name);
2023 cflr_ok = extract_int(argbuf, 1);
2025 if (CtdlAccessCheck(ac_aide)) return;
2027 if (IsEmptyStr(new_floor_name)) {
2028 cprintf("%d Blank floor name not allowed.\n",
2029 ERROR + ILLEGAL_VALUE);
2033 for (a = 0; a < MAXFLOORS; ++a) {
2034 getfloor(&flbuf, a);
2036 /* note any free slots while we're scanning... */
2037 if (((flbuf.f_flags & F_INUSE) == 0)
2041 /* check to see if it already exists */
2042 if ((!strcasecmp(flbuf.f_name, new_floor_name))
2043 && (flbuf.f_flags & F_INUSE)) {
2044 cprintf("%d Floor '%s' already exists.\n",
2045 ERROR + ALREADY_EXISTS,
2051 if (free_slot < 0) {
2052 cprintf("%d There is no space available for a new floor.\n",
2053 ERROR + INVALID_FLOOR_OPERATION);
2057 cprintf("%d ok to create...\n", CIT_OK);
2060 lgetfloor(&flbuf, free_slot);
2061 flbuf.f_flags = F_INUSE;
2062 flbuf.f_ref_count = 0;
2063 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
2064 lputfloor(&flbuf, free_slot);
2065 cprintf("%d %d\n", CIT_OK, free_slot);
2073 void cmd_kflr(char *argbuf)
2076 int floor_to_delete;
2080 floor_to_delete = extract_int(argbuf, 0);
2081 kflr_ok = extract_int(argbuf, 1);
2083 if (CtdlAccessCheck(ac_aide)) return;
2085 lgetfloor(&flbuf, floor_to_delete);
2088 if ((flbuf.f_flags & F_INUSE) == 0) {
2089 cprintf("%d Floor %d not in use.\n",
2090 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
2093 if (flbuf.f_ref_count != 0) {
2094 cprintf("%d Cannot delete; floor contains %d rooms.\n",
2095 ERROR + INVALID_FLOOR_OPERATION,
2100 cprintf("%d Ok\n", CIT_OK);
2102 cprintf("%d Ok to delete...\n", CIT_OK);
2109 if ((delete_ok == 1) && (kflr_ok == 1))
2111 lputfloor(&flbuf, floor_to_delete);
2117 void cmd_eflr(char *argbuf)
2123 np = num_parms(argbuf);
2125 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
2129 if (CtdlAccessCheck(ac_aide)) return;
2131 floor_num = extract_int(argbuf, 0);
2132 lgetfloor(&flbuf, floor_num);
2133 if ((flbuf.f_flags & F_INUSE) == 0) {
2134 lputfloor(&flbuf, floor_num);
2135 cprintf("%d Floor %d is not in use.\n",
2136 ERROR + INVALID_FLOOR_OPERATION, floor_num);
2140 extract_token(flbuf.f_name, argbuf, 1, '|', sizeof flbuf.f_name);
2141 lputfloor(&flbuf, floor_num);
2143 cprintf("%d Ok\n", CIT_OK);