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];
288 bptr = lowercase_name;
289 while (!IsEmptyStr(aptr))
291 *bptr = tolower(*aptr);
297 len = bptr - lowercase_name;
299 cdb_delete(CDB_ROOMS,
300 lowercase_name, len);
302 time(&qrbuf->QRmtime);
305 qrbuf, sizeof(struct ctdlroom));
311 * putroom() - store room data to disk
313 void putroom(struct ctdlroom *qrbuf) {
314 b_putroom(qrbuf, qrbuf->QRname);
319 * b_deleteroom() - delete a room record from disk
321 void b_deleteroom(char *room_name) {
322 b_putroom(NULL, room_name);
328 * lputroom() - same as putroom() but unlocks the record (if supported)
330 void lputroom(struct ctdlroom *qrbuf)
334 end_critical_section(S_ROOMS);
338 /****************************************************************************/
341 * getfloor() - retrieve floor data from disk
343 void getfloor(struct floor *flbuf, int floor_num)
345 struct cdbdata *cdbfl;
347 memset(flbuf, 0, sizeof(struct floor));
348 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
350 memcpy(flbuf, cdbfl->ptr,
351 ((cdbfl->len > sizeof(struct floor)) ?
352 sizeof(struct floor) : cdbfl->len));
355 if (floor_num == 0) {
356 safestrncpy(flbuf->f_name, "Main Floor",
357 sizeof flbuf->f_name);
358 flbuf->f_flags = F_INUSE;
359 flbuf->f_ref_count = 3;
366 * lgetfloor() - same as getfloor() but locks the record (if supported)
368 void lgetfloor(struct floor *flbuf, int floor_num)
371 begin_critical_section(S_FLOORTAB);
372 getfloor(flbuf, floor_num);
377 * cgetfloor() - Get floor record from *cache* (loads from disk if needed)
379 * This is strictly a performance hack.
381 struct floor *cgetfloor(int floor_num) {
382 static int initialized = 0;
385 struct floor *fl = NULL;
387 begin_critical_section(S_FLOORCACHE);
388 if (initialized == 0) {
389 for (i=0; i<MAXFLOORS; ++i) {
390 floorcache[floor_num] = NULL;
394 if (floorcache[floor_num] == NULL) {
397 end_critical_section(S_FLOORCACHE);
400 fl = malloc(sizeof(struct floor));
401 getfloor(fl, floor_num);
402 begin_critical_section(S_FLOORCACHE);
403 if (floorcache[floor_num] != NULL) {
404 free(floorcache[floor_num]);
406 floorcache[floor_num] = fl;
407 end_critical_section(S_FLOORCACHE);
410 return(floorcache[floor_num]);
416 * putfloor() - store floor data on disk
418 void putfloor(struct floor *flbuf, int floor_num)
420 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
421 begin_critical_section(S_FLOORCACHE);
422 if (floorcache[floor_num] != NULL) {
423 free(floorcache[floor_num]);
424 floorcache[floor_num] = malloc(sizeof(struct floor));
425 memcpy(floorcache[floor_num], flbuf, sizeof(struct floor));
427 end_critical_section(S_FLOORCACHE);
429 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
430 flbuf, sizeof(struct floor));
435 * lputfloor() - same as putfloor() but unlocks the record (if supported)
437 void lputfloor(struct floor *flbuf, int floor_num)
440 putfloor(flbuf, floor_num);
441 end_critical_section(S_FLOORTAB);
447 * Traverse the room file...
449 void ForEachRoom(void (*CallBack) (struct ctdlroom *EachRoom, void *out_data),
452 struct ctdlroom qrbuf;
453 struct cdbdata *cdbqr;
455 cdb_rewind(CDB_ROOMS);
457 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
458 memset(&qrbuf, 0, sizeof(struct ctdlroom));
459 memcpy(&qrbuf, cdbqr->ptr,
460 ((cdbqr->len > sizeof(struct ctdlroom)) ?
461 sizeof(struct ctdlroom) : cdbqr->len));
463 room_sanity_check(&qrbuf);
464 if (qrbuf.QRflags & QR_INUSE)
465 (*CallBack)(&qrbuf, in_data);
471 * delete_msglist() - delete room message pointers
473 void delete_msglist(struct ctdlroom *whichroom)
475 struct cdbdata *cdbml;
477 /* Make sure the msglist we're deleting actually exists, otherwise
478 * gdbm will complain when we try to delete an invalid record
480 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
484 /* Go ahead and delete it */
485 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
493 * sort message pointers
494 * (returns new msg count)
496 int sort_msglist(long listptrs[], int oldcount)
507 for (a = numitems - 2; a >= 0; --a) {
508 for (b = 0; b <= a; ++b) {
509 if (listptrs[b] > (listptrs[b + 1])) {
511 hold2 = listptrs[b + 1];
513 listptrs[b + 1] = hold1;
518 /* and yank any nulls */
519 while ((numitems > 0) && (listptrs[0] == 0L)) {
520 memcpy(&listptrs[0], &listptrs[1],
521 (sizeof(long) * (numitems - 1)));
530 * Determine whether a given room is non-editable.
532 int is_noneditable(struct ctdlroom *qrbuf)
535 /* Mail> rooms are non-editable */
536 if ( (qrbuf->QRflags & QR_MAILBOX)
537 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
540 /* Everything else is editable */
547 * Back-back-end for all room listing commands
549 void list_roomname(struct ctdlroom *qrbuf, int ra, int current_view, int default_view)
551 char truncated_roomname[ROOMNAMELEN];
553 /* For my own mailbox rooms, chop off the owner prefix */
554 if ( (qrbuf->QRflags & QR_MAILBOX)
555 && (atol(qrbuf->QRname) == CC->user.usernum) ) {
556 safestrncpy(truncated_roomname, qrbuf->QRname, sizeof truncated_roomname);
557 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
558 cprintf("%s", truncated_roomname);
560 /* For all other rooms, just display the name in its entirety */
562 cprintf("%s", qrbuf->QRname);
565 /* ...and now the other parameters */
566 cprintf("|%u|%d|%d|%d|%d|%d|%d|%ld|\n",
568 (int) qrbuf->QRfloor,
569 (int) qrbuf->QRorder,
570 (int) qrbuf->QRflags2,
580 * cmd_lrms() - List all accessible rooms, known or forgotten
582 void cmd_lrms_backend(struct ctdlroom *qrbuf, void *data)
584 int FloorBeingSearched = (-1);
588 FloorBeingSearched = *(int *)data;
589 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
591 if ((( ra & (UA_KNOWN | UA_ZAPPED)))
592 && ((qrbuf->QRfloor == (FloorBeingSearched))
593 || ((FloorBeingSearched) < 0)))
594 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
597 void cmd_lrms(char *argbuf)
599 int FloorBeingSearched = (-1);
600 if (!IsEmptyStr(argbuf))
601 FloorBeingSearched = extract_int(argbuf, 0);
603 if (CtdlAccessCheck(ac_logged_in)) return;
605 if (getuser(&CC->user, CC->curr_user)) {
606 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
609 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
611 ForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
618 * cmd_lkra() - List all known rooms
620 void cmd_lkra_backend(struct ctdlroom *qrbuf, void *data)
622 int FloorBeingSearched = (-1);
626 FloorBeingSearched = *(int *)data;
627 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
629 if ((( ra & (UA_KNOWN)))
630 && ((qrbuf->QRfloor == (FloorBeingSearched))
631 || ((FloorBeingSearched) < 0)))
632 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
635 void cmd_lkra(char *argbuf)
637 int FloorBeingSearched = (-1);
638 if (!IsEmptyStr(argbuf))
639 FloorBeingSearched = extract_int(argbuf, 0);
641 if (CtdlAccessCheck(ac_logged_in)) return;
643 if (getuser(&CC->user, CC->curr_user)) {
644 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
647 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
649 ForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
655 void cmd_lprm_backend(struct ctdlroom *qrbuf, void *data)
657 int FloorBeingSearched = (-1);
661 FloorBeingSearched = *(int *)data;
662 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
664 if ( ((qrbuf->QRflags & QR_PRIVATE) == 0)
665 && ((qrbuf->QRflags & QR_MAILBOX) == 0)
666 && ((qrbuf->QRfloor == (FloorBeingSearched))
667 || ((FloorBeingSearched) < 0)))
668 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
671 void cmd_lprm(char *argbuf)
673 int FloorBeingSearched = (-1);
674 if (!IsEmptyStr(argbuf))
675 FloorBeingSearched = extract_int(argbuf, 0);
677 cprintf("%d Publiic rooms:\n", LISTING_FOLLOWS);
679 ForEachRoom(cmd_lprm_backend, &FloorBeingSearched);
686 * cmd_lkrn() - List all known rooms with new messages
688 void cmd_lkrn_backend(struct ctdlroom *qrbuf, void *data)
690 int FloorBeingSearched = (-1);
694 FloorBeingSearched = *(int *)data;
695 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
698 && (ra & UA_HASNEWMSGS)
699 && ((qrbuf->QRfloor == (FloorBeingSearched))
700 || ((FloorBeingSearched) < 0)))
701 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
704 void cmd_lkrn(char *argbuf)
706 int FloorBeingSearched = (-1);
707 if (!IsEmptyStr(argbuf))
708 FloorBeingSearched = extract_int(argbuf, 0);
710 if (CtdlAccessCheck(ac_logged_in)) return;
712 if (getuser(&CC->user, CC->curr_user)) {
713 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
716 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
718 ForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
725 * cmd_lkro() - List all known rooms
727 void cmd_lkro_backend(struct ctdlroom *qrbuf, void *data)
729 int FloorBeingSearched = (-1);
733 FloorBeingSearched = *(int *)data;
734 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
737 && ((ra & UA_HASNEWMSGS) == 0)
738 && ((qrbuf->QRfloor == (FloorBeingSearched))
739 || ((FloorBeingSearched) < 0)))
740 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
743 void cmd_lkro(char *argbuf)
745 int FloorBeingSearched = (-1);
746 if (!IsEmptyStr(argbuf))
747 FloorBeingSearched = extract_int(argbuf, 0);
749 if (CtdlAccessCheck(ac_logged_in)) return;
751 if (getuser(&CC->user, CC->curr_user)) {
752 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
755 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
757 ForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
764 * cmd_lzrm() - List all forgotten rooms
766 void cmd_lzrm_backend(struct ctdlroom *qrbuf, void *data)
768 int FloorBeingSearched = (-1);
772 FloorBeingSearched = *(int *)data;
773 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
775 if ((ra & UA_GOTOALLOWED)
777 && ((qrbuf->QRfloor == (FloorBeingSearched))
778 || ((FloorBeingSearched) < 0)))
779 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
782 void cmd_lzrm(char *argbuf)
784 int FloorBeingSearched = (-1);
785 if (!IsEmptyStr(argbuf))
786 FloorBeingSearched = extract_int(argbuf, 0);
788 if (CtdlAccessCheck(ac_logged_in)) return;
790 if (getuser(&CC->user, CC->curr_user)) {
791 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
794 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
796 ForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
802 * Make the specified room the current room for this session. No validation
803 * or access control is done here -- the caller should make sure that the
804 * specified room exists and is ok to access.
806 void usergoto(char *where, int display_result, int transiently,
807 int *retmsgs, int *retnew)
810 int new_messages = 0;
811 int old_messages = 0;
812 int total_messages = 0;
816 int newmailcount = 0;
818 char truncated_roomname[ROOMNAMELEN];
819 struct cdbdata *cdbfr;
820 long *msglist = NULL;
822 unsigned int original_v_flags;
825 char setstr[128], lostr[64], histr[64];
829 /* If the supplied room name is NULL, the caller wants us to know that
830 * it has already copied the room record into CC->room, so
831 * we can skip the extra database fetch.
834 safestrncpy(CC->room.QRname, where, sizeof CC->room.QRname);
835 getroom(&CC->room, where);
838 /* Take care of all the formalities. */
840 begin_critical_section(S_USERS);
841 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
842 original_v_flags = vbuf.v_flags;
844 /* Know the room ... but not if it's the page log room, or if the
845 * caller specified that we're only entering this room transiently.
847 if ((strcasecmp(CC->room.QRname, config.c_logpages))
848 && (transiently == 0) ) {
849 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
850 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
853 /* Only rewrite the database record if we changed something */
854 if (vbuf.v_flags != original_v_flags) {
855 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
857 end_critical_section(S_USERS);
859 /* Check for new mail */
860 newmailcount = NewMailCount();
862 /* set info to 1 if the user needs to read the room's info file */
863 if (CC->room.QRinfo > vbuf.v_lastseen) {
867 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
869 msglist = (long *) cdbfr->ptr;
870 cdbfr->ptr = NULL; /* usergoto() now owns this memory */
871 num_msgs = cdbfr->len / sizeof(long);
876 for (a=0; a<num_msgs; ++a) {
877 if (msglist[a] > 0L) ++total_messages;
879 new_messages = num_msgs;
880 num_sets = num_tokens(vbuf.v_seen, ',');
881 for (s=0; s<num_sets; ++s) {
882 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
884 extract_token(lostr, setstr, 0, ':', sizeof lostr);
885 if (num_tokens(setstr, ':') >= 2) {
886 extract_token(histr, setstr, 1, ':', sizeof histr);
887 if (!strcmp(histr, "*")) {
888 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
892 strcpy(histr, lostr);
897 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
898 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
904 new_messages = total_messages - old_messages;
906 if (msglist != NULL) free(msglist);
908 if (CC->room.QRflags & QR_MAILBOX)
913 if ((CC->room.QRroomaide == CC->user.usernum)
914 || (CC->user.axlevel >= 6))
919 safestrncpy(truncated_roomname, CC->room.QRname, sizeof truncated_roomname);
920 if ( (CC->room.QRflags & QR_MAILBOX)
921 && (atol(CC->room.QRname) == CC->user.usernum) ) {
922 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
925 if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
929 if (retmsgs != NULL) *retmsgs = total_messages;
930 if (retnew != NULL) *retnew = new_messages;
931 lprintf(CTDL_DEBUG, "<%s> %d new of %d total messages\n",
933 new_messages, total_messages
936 CC->curr_view = (int)vbuf.v_view;
938 if (display_result) {
939 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|%d|\n",
940 CIT_OK, CtdlCheckExpress(),
945 (int)CC->room.QRflags,
946 (long)CC->room.QRhighest,
947 (long)vbuf.v_lastseen,
951 (int)CC->room.QRfloor,
953 (int)CC->room.QRdefaultview,
955 (int)CC->room.QRflags2
962 * Handle some of the macro named rooms
964 void convert_room_name_macros(char *towhere, size_t maxlen) {
965 if (!strcasecmp(towhere, "_BASEROOM_")) {
966 safestrncpy(towhere, config.c_baseroom, maxlen);
968 else if (!strcasecmp(towhere, "_MAIL_")) {
969 safestrncpy(towhere, MAILROOM, maxlen);
971 else if (!strcasecmp(towhere, "_TRASH_")) {
972 safestrncpy(towhere, USERTRASHROOM, maxlen);
974 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
975 safestrncpy(towhere, config.c_twitroom, maxlen);
977 else if (!strcasecmp(towhere, "_CALENDAR_")) {
978 safestrncpy(towhere, USERCALENDARROOM, maxlen);
980 else if (!strcasecmp(towhere, "_TASKS_")) {
981 safestrncpy(towhere, USERTASKSROOM, maxlen);
983 else if (!strcasecmp(towhere, "_CONTACTS_")) {
984 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
986 else if (!strcasecmp(towhere, "_NOTES_")) {
987 safestrncpy(towhere, USERNOTESROOM, maxlen);
993 * cmd_goto() - goto a new room
995 void cmd_goto(char *gargs)
997 struct ctdlroom QRscratch;
1001 char augmented_roomname[ROOMNAMELEN];
1002 char towhere[ROOMNAMELEN];
1004 int transiently = 0;
1006 if (CtdlAccessCheck(ac_logged_in)) return;
1008 extract_token(towhere, gargs, 0, '|', sizeof towhere);
1009 extract_token(password, gargs, 1, '|', sizeof password);
1010 transiently = extract_int(gargs, 2);
1012 getuser(&CC->user, CC->curr_user);
1015 * Handle some of the macro named rooms
1017 convert_room_name_macros(towhere, sizeof towhere);
1019 /* First try a regular match */
1020 c = getroom(&QRscratch, towhere);
1022 /* Then try a mailbox name match */
1024 MailboxName(augmented_roomname, sizeof augmented_roomname,
1025 &CC->user, towhere);
1026 c = getroom(&QRscratch, augmented_roomname);
1028 safestrncpy(towhere, augmented_roomname, sizeof towhere);
1031 /* And if the room was found... */
1034 /* Let internal programs go directly to any room. */
1035 if (CC->internal_pgm) {
1036 memcpy(&CC->room, &QRscratch,
1037 sizeof(struct ctdlroom));
1038 usergoto(NULL, 1, transiently, NULL, NULL);
1042 /* See if there is an existing user/room relationship */
1043 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
1045 /* normal clients have to pass through security */
1046 if (ra & UA_GOTOALLOWED) {
1051 if ((QRscratch.QRflags & QR_MAILBOX) &&
1052 ((ra & UA_GOTOALLOWED))) {
1053 memcpy(&CC->room, &QRscratch,
1054 sizeof(struct ctdlroom));
1055 usergoto(NULL, 1, transiently, NULL, NULL);
1057 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
1058 ((ra & UA_KNOWN) == 0) &&
1059 (strcasecmp(QRscratch.QRpasswd, password)) &&
1060 (CC->user.axlevel < 6)
1062 cprintf("%d wrong or missing passwd\n",
1063 ERROR + PASSWORD_REQUIRED);
1065 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
1066 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
1067 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
1068 ((ra & UA_KNOWN) == 0) &&
1069 (CC->user.axlevel < 6)
1071 lprintf(CTDL_DEBUG, "Failed to acquire private room\n");
1073 memcpy(&CC->room, &QRscratch,
1074 sizeof(struct ctdlroom));
1075 usergoto(NULL, 1, transiently, NULL, NULL);
1081 cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
1087 struct ctdluser temp;
1088 struct cdbdata *cdbus;
1091 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
1092 cdb_rewind(CDB_USERS);
1093 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1094 memset(&temp, 0, sizeof temp);
1095 memcpy(&temp, cdbus->ptr, sizeof temp);
1098 CtdlRoomAccess(&CC->room, &temp, &ra, NULL);
1099 if ((CC->room.QRflags & QR_INUSE)
1102 cprintf("%s\n", temp.fullname);
1109 * RDIR command for room directory
1117 struct stat statbuf;
1118 char tempfilename[PATH_MAX];
1120 if (CtdlAccessCheck(ac_logged_in)) return;
1121 CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
1123 getroom(&CC->room, CC->room.QRname);
1124 getuser(&CC->user, CC->curr_user);
1126 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
1127 cprintf("%d not here.\n", ERROR + NOT_HERE);
1130 if (((CC->room.QRflags & QR_VISDIR) == 0)
1131 && (CC->user.axlevel < 6)
1132 && (CC->user.usernum != CC->room.QRroomaide)) {
1133 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1136 cprintf("%d %s|%s/%s\n",
1140 CC->room.QRdirname);
1142 snprintf(buf, sizeof buf,
1143 "ls %s/%s >%s 2>/dev/null",
1149 snprintf(buf, sizeof buf,
1152 CC->room.QRdirname);
1153 fd = fopen(buf, "r");
1155 fd = fopen("/dev/null", "r");
1157 ls = fopen(tempfilename, "r");
1158 while (fgets(flnm, sizeof flnm, ls) != NULL) {
1159 flnm[strlen(flnm) - 1] = 0;
1160 if (strcasecmp(flnm, "filedir")) {
1161 snprintf(buf, sizeof buf,
1166 stat(buf, &statbuf);
1167 safestrncpy(comment, "", sizeof comment);
1169 while ((fgets(buf, sizeof buf, fd) != NULL)
1170 && (IsEmptyStr(comment))) {
1171 buf[strlen(buf) - 1] = 0;
1172 if ((!strncasecmp(buf, flnm, strlen(flnm)))
1173 && (buf[strlen(flnm)] == ' '))
1174 safestrncpy(comment,
1175 &buf[strlen(flnm) + 1],
1178 cprintf("%s|%ld|%s\n", flnm, (long)statbuf.st_size, comment);
1183 unlink(tempfilename);
1189 * get room parameters (aide or room aide command)
1193 if (CtdlAccessCheck(ac_room_aide)) return;
1195 getroom(&CC->room, CC->room.QRname);
1196 cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1200 ((CC->room.QRflags & QR_MAILBOX) ?
1201 &CC->room.QRname[11] : CC->room.QRname),
1203 ((CC->room.QRflags & QR_PASSWORDED) ?
1204 CC->room.QRpasswd : ""),
1206 ((CC->room.QRflags & QR_DIRECTORY) ?
1207 CC->room.QRdirname : ""),
1210 (int) CC->room.QRfloor,
1211 (int) CC->room.QRorder,
1213 CC->room.QRdefaultview,
1220 * Back end function to rename a room.
1221 * You can also specify which floor to move the room to, or specify -1 to
1222 * keep the room on the same floor it was on.
1224 * If you are renaming a mailbox room, you must supply the namespace prefix
1225 * in *at least* the old name!
1227 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1229 struct ctdlroom qrbuf;
1230 struct ctdlroom qrtmp;
1235 char actual_old_name[ROOMNAMELEN];
1237 lprintf(CTDL_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1238 old_name, new_name, new_floor);
1240 if (new_floor >= 0) {
1241 fl = cgetfloor(new_floor);
1242 if ((fl->f_flags & F_INUSE) == 0) {
1243 return(crr_invalid_floor);
1247 begin_critical_section(S_ROOMS);
1249 if ( (getroom(&qrtmp, new_name) == 0)
1250 && (strcasecmp(new_name, old_name)) ) {
1251 ret = crr_already_exists;
1254 else if (getroom(&qrbuf, old_name) != 0) {
1255 ret = crr_room_not_found;
1258 else if ( (CC->user.axlevel < 6) && (!CC->internal_pgm)
1259 && (CC->user.usernum != qrbuf.QRroomaide)
1260 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) ) {
1261 ret = crr_access_denied;
1264 else if (is_noneditable(&qrbuf)) {
1265 ret = crr_noneditable;
1270 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1271 if (qrbuf.QRflags & QR_MAILBOX) {
1272 owner = atol(qrbuf.QRname);
1274 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1275 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1276 "%010ld.%s", owner, new_name);
1279 safestrncpy(qrbuf.QRname, new_name,
1280 sizeof(qrbuf.QRname));
1283 /* Reject change of floor for baseroom/aideroom */
1284 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1285 !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1289 /* Take care of floor stuff */
1290 old_floor = qrbuf.QRfloor;
1291 if (new_floor < 0) {
1292 new_floor = old_floor;
1294 qrbuf.QRfloor = new_floor;
1297 begin_critical_section(S_CONFIG);
1299 /* If baseroom/aideroom name changes, update config */
1300 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1301 safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1304 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1305 safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1309 end_critical_section(S_CONFIG);
1311 /* If the room name changed, then there are now two room
1312 * records, so we have to delete the old one.
1314 if (strcasecmp(new_name, old_name)) {
1315 b_deleteroom(actual_old_name);
1321 end_critical_section(S_ROOMS);
1323 /* Adjust the floor reference counts if necessary */
1324 if (new_floor != old_floor) {
1325 lgetfloor(&flbuf, old_floor);
1326 --flbuf.f_ref_count;
1327 lputfloor(&flbuf, old_floor);
1328 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1329 lgetfloor(&flbuf, new_floor);
1330 ++flbuf.f_ref_count;
1331 lputfloor(&flbuf, new_floor);
1332 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1335 /* ...and everybody say "YATTA!" */
1341 * set room parameters (aide or room aide command)
1343 void cmd_setr(char *args)
1349 char new_name[ROOMNAMELEN];
1351 if (CtdlAccessCheck(ac_logged_in)) return;
1353 if (num_parms(args) >= 6) {
1354 new_floor = extract_int(args, 5);
1356 new_floor = (-1); /* don't change the floor */
1359 /* When is a new name more than just a new name? When the old name
1360 * has a namespace prefix.
1362 if (CC->room.QRflags & QR_MAILBOX) {
1363 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
1365 safestrncpy(new_name, "", sizeof new_name);
1367 extract_token(&new_name[strlen(new_name)], args, 0, '|', (sizeof new_name - strlen(new_name)));
1369 r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
1371 if (r == crr_room_not_found) {
1372 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
1373 } else if (r == crr_already_exists) {
1374 cprintf("%d '%s' already exists.\n",
1375 ERROR + ALREADY_EXISTS, new_name);
1376 } else if (r == crr_noneditable) {
1377 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
1378 } else if (r == crr_invalid_floor) {
1379 cprintf("%d Target floor does not exist.\n",
1380 ERROR + INVALID_FLOOR_OPERATION);
1381 } else if (r == crr_access_denied) {
1382 cprintf("%d You do not have permission to edit '%s'\n",
1383 ERROR + HIGHER_ACCESS_REQUIRED,
1385 } else if (r != crr_ok) {
1386 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1387 ERROR + INTERNAL_ERROR, r);
1394 getroom(&CC->room, new_name);
1396 /* Now we have to do a bunch of other stuff */
1398 if (num_parms(args) >= 7) {
1399 new_order = extract_int(args, 6);
1402 if (new_order > 127)
1406 lgetroom(&CC->room, CC->room.QRname);
1408 /* Directory room */
1409 extract_token(buf, args, 2, '|', sizeof buf);
1411 safestrncpy(CC->room.QRdirname, buf,
1412 sizeof CC->room.QRdirname);
1415 if (num_parms(args) >= 8) {
1416 CC->room.QRdefaultview = extract_int(args, 7);
1419 /* Second set of flags */
1420 if (num_parms(args) >= 9) {
1421 CC->room.QRflags2 = extract_int(args, 8);
1425 CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
1426 /* Clean up a client boo-boo: if the client set the room to
1427 * guess-name or passworded, ensure that the private flag is
1430 if ((CC->room.QRflags & QR_GUESSNAME)
1431 || (CC->room.QRflags & QR_PASSWORDED))
1432 CC->room.QRflags |= QR_PRIVATE;
1434 /* Some changes can't apply to BASEROOM */
1435 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1437 CC->room.QRorder = 0;
1438 CC->room.QRpasswd[0] = '\0';
1439 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1440 QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1441 CC->room.QRflags |= QR_PERMANENT;
1443 /* March order (doesn't apply to AIDEROOM) */
1444 if (num_parms(args) >= 7)
1445 CC->room.QRorder = (char) new_order;
1447 extract_token(buf, args, 1, '|', sizeof buf);
1449 safestrncpy(CC->room.QRpasswd, buf,
1450 sizeof CC->room.QRpasswd);
1451 /* Kick everyone out if the client requested it
1452 * (by changing the room's generation number)
1454 if (extract_int(args, 4)) {
1455 time(&CC->room.QRgen);
1458 /* Some changes can't apply to AIDEROOM */
1459 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1461 CC->room.QRorder = 0;
1462 CC->room.QRflags &= ~QR_MAILBOX;
1463 CC->room.QRflags |= QR_PERMANENT;
1466 /* Write the room record back to disk */
1467 lputroom(&CC->room);
1469 /* Create a room directory if necessary */
1470 if (CC->room.QRflags & QR_DIRECTORY) {
1471 snprintf(buf, sizeof buf,"%s/%s",
1473 CC->room.QRdirname);
1476 snprintf(buf, sizeof buf, "The room \"%s\" has been edited by %s.\n",
1477 CC->room.QRname, CC->curr_user);
1478 aide_message(buf, "Room modification Message");
1479 cprintf("%d Ok\n", CIT_OK);
1485 * get the name of the room aide for this room
1489 struct ctdluser usbuf;
1491 if (CtdlAccessCheck(ac_logged_in)) return;
1493 if (getuserbynumber(&usbuf, CC->room.QRroomaide) == 0) {
1494 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1496 cprintf("%d \n", CIT_OK);
1502 * set the room aide for this room
1504 void cmd_seta(char *new_ra)
1506 struct ctdluser usbuf;
1511 if (CtdlAccessCheck(ac_room_aide)) return;
1513 if (getuser(&usbuf, new_ra) != 0) {
1516 newu = usbuf.usernum;
1519 lgetroom(&CC->room, CC->room.QRname);
1521 if (CC->room.QRroomaide != newu) {
1524 CC->room.QRroomaide = newu;
1525 lputroom(&CC->room);
1528 * We have to post the change notice _after_ writing changes to
1529 * the room table, otherwise it would deadlock!
1531 if (post_notice == 1) {
1532 if (!IsEmptyStr(usbuf.fullname))
1533 snprintf(buf, sizeof buf,
1534 "%s is now the room aide for \"%s\".\n",
1535 usbuf.fullname, CC->room.QRname);
1537 snprintf(buf, sizeof buf,
1538 "There is now no room aide for \"%s\".\n",
1540 aide_message(buf, "Aide Room Modification");
1542 cprintf("%d Ok\n", CIT_OK);
1546 * retrieve info file for this room
1554 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_info_dir);
1555 info_fp = fopen(filename, "r");
1557 if (info_fp == NULL) {
1558 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
1561 cprintf("%d Info:\n", LISTING_FOLLOWS);
1562 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1563 if (!IsEmptyStr(buf))
1564 buf[strlen(buf) - 1] = 0;
1565 cprintf("%s\n", buf);
1572 * Asynchronously schedule a room for deletion. The room will appear
1573 * deleted to the user(s), but it won't actually get purged from the
1574 * database until THE DREADED AUTO-PURGER makes its next run.
1576 void schedule_room_for_deletion(struct ctdlroom *qrbuf)
1578 char old_name[ROOMNAMELEN];
1581 lprintf(CTDL_NOTICE, "Scheduling room <%s> for deletion\n",
1584 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1586 getroom(qrbuf, qrbuf->QRname);
1588 /* Turn the room into a private mailbox owned by a user who doesn't
1589 * exist. This will immediately make the room invisible to everyone,
1590 * and qualify the room for purging.
1592 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1597 qrbuf->QRflags |= QR_MAILBOX;
1598 time(&qrbuf->QRgen); /* Use a timestamp as the new generation number */
1602 b_deleteroom(old_name);
1608 * Back end processing to delete a room and everything associated with it
1609 * (This one is synchronous and should only get called by THE DREADED
1610 * AUTO-PURGER in serv_expire.c. All user-facing code should call
1611 * the asynchronous schedule_room_for_deletion() instead.)
1613 void delete_room(struct ctdlroom *qrbuf)
1617 /* TODO: filename magic? does this realy work? */
1619 lprintf(CTDL_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1621 /* Delete the info file */
1622 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_info_dir);
1625 /* Delete the image file */
1626 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_image_dir);
1629 /* Delete the room's network config file */
1630 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1633 /* Delete the messages in the room
1634 * (Careful: this opens an S_ROOMS critical section!)
1636 CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
1638 /* Flag the room record as not in use */
1639 lgetroom(qrbuf, qrbuf->QRname);
1643 /* then decrement the reference count for the floor */
1644 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1645 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1646 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1648 /* Delete the room record from the database! */
1649 b_deleteroom(qrbuf->QRname);
1655 * Check access control for deleting a room
1657 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1659 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1663 if (is_noneditable(qr)) {
1668 * For mailboxes, check stuff
1670 if (qr->QRflags & QR_MAILBOX) {
1672 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1674 if (atol(qr->QRname) != CC->user.usernum) {
1675 return(0); /* not my room */
1678 /* Can't delete your Mail> room */
1679 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1681 /* Otherwise it's ok */
1686 * For normal rooms, just check for aide or room aide status.
1688 return(is_room_aide());
1692 * aide command: kill the current room
1694 void cmd_kill(char *argbuf)
1696 char deleted_room_name[ROOMNAMELEN];
1700 kill_ok = extract_int(argbuf, 0);
1702 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
1703 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1707 if (CC->room.QRflags & QR_MAILBOX) {
1708 safestrncpy(deleted_room_name, &CC->room.QRname[11], sizeof deleted_room_name);
1711 safestrncpy(deleted_room_name, CC->room.QRname, sizeof deleted_room_name);
1714 /* Do the dirty work */
1715 schedule_room_for_deletion(&CC->room);
1717 /* Return to the Lobby */
1718 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1720 /* tell the world what we did */
1721 snprintf(msg, sizeof msg, "The room \"%s\" has been deleted by %s.\n",
1722 deleted_room_name, CC->curr_user);
1723 aide_message(msg, "Room Purger Message");
1724 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1726 cprintf("%d ok to delete.\n", CIT_OK);
1732 * Internal code to create a new room (returns room flags)
1734 * Room types: 0=public, 1=guessname, 2=passworded, 3=inv-only,
1735 * 4=mailbox, 5=mailbox, but caller supplies namespace
1737 unsigned create_room(char *new_room_name,
1739 char *new_room_pass,
1746 struct ctdlroom qrbuf;
1750 lprintf(CTDL_DEBUG, "create_room(name=%s, type=%d, view=%d)\n",
1751 new_room_name, new_room_type, new_room_view);
1753 if (getroom(&qrbuf, new_room_name) == 0) {
1754 lprintf(CTDL_DEBUG, "%s already exists.\n", new_room_name);
1758 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1759 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1760 qrbuf.QRflags = QR_INUSE;
1761 if (new_room_type > 0)
1762 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1763 if (new_room_type == 1)
1764 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1765 if (new_room_type == 2)
1766 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1767 if ( (new_room_type == 4) || (new_room_type == 5) ) {
1768 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1769 /* qrbuf.QRflags2 |= QR2_SUBJECTREQ; */
1772 /* If the user is requesting a personal room, set up the room
1773 * name accordingly (prepend the user number)
1775 if (new_room_type == 4) {
1776 MailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1779 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1782 /* If the room is private, and the system administrator has elected
1783 * to automatically grant room aide privileges, do so now; otherwise,
1784 * set the room aide to undefined.
1786 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1787 qrbuf.QRroomaide = CC->user.usernum;
1789 qrbuf.QRroomaide = (-1L);
1793 * If the caller is only interested in testing whether this will work,
1794 * return now without creating the room.
1796 if (!really_create) return (qrbuf.QRflags);
1798 qrbuf.QRnumber = get_new_room_number();
1799 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1800 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1801 qrbuf.QRfloor = new_room_floor;
1802 qrbuf.QRdefaultview = new_room_view;
1804 /* save what we just did... */
1807 /* bump the reference count on whatever floor the room is on */
1808 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1809 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1810 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1812 /* Grant the creator access to the room unless the avoid_access
1813 * parameter was specified.
1815 if ( (CC->logged_in) && (avoid_access == 0) ) {
1816 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1817 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1818 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1819 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1822 /* resume our happy day */
1823 return (qrbuf.QRflags);
1830 void cmd_cre8(char *args)
1833 char new_room_name[ROOMNAMELEN];
1835 char new_room_pass[32];
1838 char *notification_message = NULL;
1841 int avoid_access = 0;
1843 cre8_ok = extract_int(args, 0);
1844 extract_token(new_room_name, args, 1, '|', sizeof new_room_name);
1845 new_room_name[ROOMNAMELEN - 1] = 0;
1846 new_room_type = extract_int(args, 2);
1847 extract_token(new_room_pass, args, 3, '|', sizeof new_room_pass);
1848 avoid_access = extract_int(args, 5);
1849 new_room_view = extract_int(args, 6);
1850 new_room_pass[9] = 0;
1853 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 1)) {
1854 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
1858 if (!strcasecmp(new_room_name, MAILROOM)) {
1859 cprintf("%d '%s' already exists.\n",
1860 ERROR + ALREADY_EXISTS, new_room_name);
1864 if (num_parms(args) >= 5) {
1865 fl = cgetfloor(extract_int(args, 4));
1867 cprintf("%d Invalid floor number.\n",
1868 ERROR + INVALID_FLOOR_OPERATION);
1871 else if ((fl->f_flags & F_INUSE) == 0) {
1872 cprintf("%d Invalid floor number.\n",
1873 ERROR + INVALID_FLOOR_OPERATION);
1876 new_room_floor = extract_int(args, 4);
1880 if (CtdlAccessCheck(ac_logged_in)) return;
1882 if (CC->user.axlevel < config.c_createax || CC->internal_pgm) {
1883 cprintf("%d You need higher access to create rooms.\n",
1884 ERROR + HIGHER_ACCESS_REQUIRED);
1888 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 0)) {
1889 cprintf("%d Ok to create rooms.\n", CIT_OK);
1893 if ((new_room_type < 0) || (new_room_type > 5)) {
1894 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
1898 if (new_room_type == 5) {
1899 if (CC->user.axlevel < 6) {
1900 cprintf("%d Higher access required\n",
1901 ERROR + HIGHER_ACCESS_REQUIRED);
1906 /* Check to make sure the requested room name doesn't already exist */
1907 newflags = create_room(new_room_name,
1908 new_room_type, new_room_pass, new_room_floor,
1909 0, avoid_access, new_room_view);
1910 if (newflags == 0) {
1911 cprintf("%d '%s' already exists.\n",
1912 ERROR + ALREADY_EXISTS, new_room_name);
1917 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
1921 /* If we reach this point, the room needs to be created. */
1923 newflags = create_room(new_room_name,
1924 new_room_type, new_room_pass, new_room_floor, 1, 0,
1927 /* post a message in Aide> describing the new room */
1928 notification_message = malloc(1024);
1929 snprintf(notification_message, 1024,
1930 "A new room called \"%s\" has been created by %s%s%s%s%s%s\n",
1933 ((newflags & QR_MAILBOX) ? " [personal]" : ""),
1934 ((newflags & QR_PRIVATE) ? " [private]" : ""),
1935 ((newflags & QR_GUESSNAME) ? " [hidden]" : ""),
1936 ((newflags & QR_PASSWORDED) ? " Password: " : ""),
1937 ((newflags & QR_PASSWORDED) ? new_room_pass : "")
1939 aide_message(notification_message, "Room Creation Message");
1940 free(notification_message);
1942 cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
1947 void cmd_einf(char *ok)
1948 { /* enter info file for current room */
1950 char infofilename[SIZ];
1955 if (CtdlAccessCheck(ac_room_aide)) return;
1957 if (atoi(ok) == 0) {
1958 cprintf("%d Ok.\n", CIT_OK);
1961 assoc_file_name(infofilename, sizeof infofilename, &CC->room, ctdl_info_dir);
1962 lprintf(CTDL_DEBUG, "opening\n");
1963 fp = fopen(infofilename, "w");
1964 lprintf(CTDL_DEBUG, "checking\n");
1966 cprintf("%d Cannot open %s: %s\n",
1967 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1970 cprintf("%d Send info...\n", SEND_LISTING);
1973 client_getln(buf, sizeof buf);
1974 if (strcmp(buf, "000"))
1975 fprintf(fp, "%s\n", buf);
1976 } while (strcmp(buf, "000"));
1979 /* now update the room index so people will see our new info */
1980 lgetroom(&CC->room, CC->room.QRname); /* lock so no one steps on us */
1981 CC->room.QRinfo = CC->room.QRhighest + 1L;
1982 lputroom(&CC->room);
1987 * cmd_lflr() - List all known floors
1994 if (CtdlAccessCheck(ac_logged_in)) return;
1996 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1998 for (a = 0; a < MAXFLOORS; ++a) {
1999 getfloor(&flbuf, a);
2000 if (flbuf.f_flags & F_INUSE) {
2001 cprintf("%d|%s|%d\n",
2013 * create a new floor
2015 void cmd_cflr(char *argbuf)
2017 char new_floor_name[256];
2020 int free_slot = (-1);
2023 extract_token(new_floor_name, argbuf, 0, '|', sizeof new_floor_name);
2024 cflr_ok = extract_int(argbuf, 1);
2026 if (CtdlAccessCheck(ac_aide)) return;
2028 if (IsEmptyStr(new_floor_name)) {
2029 cprintf("%d Blank floor name not allowed.\n",
2030 ERROR + ILLEGAL_VALUE);
2034 for (a = 0; a < MAXFLOORS; ++a) {
2035 getfloor(&flbuf, a);
2037 /* note any free slots while we're scanning... */
2038 if (((flbuf.f_flags & F_INUSE) == 0)
2042 /* check to see if it already exists */
2043 if ((!strcasecmp(flbuf.f_name, new_floor_name))
2044 && (flbuf.f_flags & F_INUSE)) {
2045 cprintf("%d Floor '%s' already exists.\n",
2046 ERROR + ALREADY_EXISTS,
2052 if (free_slot < 0) {
2053 cprintf("%d There is no space available for a new floor.\n",
2054 ERROR + INVALID_FLOOR_OPERATION);
2058 cprintf("%d ok to create...\n", CIT_OK);
2061 lgetfloor(&flbuf, free_slot);
2062 flbuf.f_flags = F_INUSE;
2063 flbuf.f_ref_count = 0;
2064 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
2065 lputfloor(&flbuf, free_slot);
2066 cprintf("%d %d\n", CIT_OK, free_slot);
2074 void cmd_kflr(char *argbuf)
2077 int floor_to_delete;
2081 floor_to_delete = extract_int(argbuf, 0);
2082 kflr_ok = extract_int(argbuf, 1);
2084 if (CtdlAccessCheck(ac_aide)) return;
2086 lgetfloor(&flbuf, floor_to_delete);
2089 if ((flbuf.f_flags & F_INUSE) == 0) {
2090 cprintf("%d Floor %d not in use.\n",
2091 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
2094 if (flbuf.f_ref_count != 0) {
2095 cprintf("%d Cannot delete; floor contains %d rooms.\n",
2096 ERROR + INVALID_FLOOR_OPERATION,
2101 cprintf("%d Ok\n", CIT_OK);
2103 cprintf("%d Ok to delete...\n", CIT_OK);
2110 if ((delete_ok == 1) && (kflr_ok == 1))
2112 lputfloor(&flbuf, floor_to_delete);
2118 void cmd_eflr(char *argbuf)
2124 np = num_parms(argbuf);
2126 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
2130 if (CtdlAccessCheck(ac_aide)) return;
2132 floor_num = extract_int(argbuf, 0);
2133 lgetfloor(&flbuf, floor_num);
2134 if ((flbuf.f_flags & F_INUSE) == 0) {
2135 lputfloor(&flbuf, floor_num);
2136 cprintf("%d Floor %d is not in use.\n",
2137 ERROR + INVALID_FLOOR_OPERATION, floor_num);
2141 extract_token(flbuf.f_name, argbuf, 1, '|', sizeof flbuf.f_name);
2142 lputfloor(&flbuf, floor_num);
2144 cprintf("%d Ok\n", CIT_OK);