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>
31 #include "serv_extensions.h"
35 #include "sysdep_decls.h"
39 #include "citserver.h"
42 #include "citadel_dirs.h"
44 struct floor *floorcache[MAXFLOORS];
47 * Generic routine for determining user access to rooms
49 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf,
50 int *result, int *view)
55 /* for internal programs, always do everything */
56 if (((CC->internal_pgm)) && (roombuf->QRflags & QR_INUSE)) {
57 retval = (UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED);
62 /* Locate any applicable user/room relationships */
63 CtdlGetRelationship(&vbuf, userbuf, roombuf);
65 /* Force the properties of the Aide room */
66 if (!strcasecmp(roombuf->QRname, config.c_aideroom)) {
67 if (userbuf->axlevel >= 6) {
68 retval = UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED;
75 /* If this is a public room, it's accessible... */
76 if ( ((roombuf->QRflags & QR_PRIVATE) == 0)
77 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
78 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
81 /* If this is a preferred users only room, check access level */
82 if (roombuf->QRflags & QR_PREFONLY) {
83 if (userbuf->axlevel < 5) {
84 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
88 /* For private rooms, check the generation number matchups */
89 if ( (roombuf->QRflags & QR_PRIVATE)
90 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
92 /* An explicit match means the user belongs in this room */
93 if (vbuf.v_flags & V_ACCESS) {
94 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
96 /* Otherwise, check if this is a guess-name or passworded
97 * room. If it is, a goto may at least be attempted
99 else if ((roombuf->QRflags & QR_PRIVATE)
100 || (roombuf->QRflags & QR_PASSWORDED)) {
101 retval = retval & ~UA_KNOWN;
102 retval = retval | UA_GOTOALLOWED;
106 /* For mailbox rooms, also check the namespace */
107 if (roombuf->QRflags & QR_MAILBOX) {
108 if (userbuf->usernum == atol(roombuf->QRname)) {
109 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED;
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;
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;
135 /* Check to see if the user has forgotten this room */
136 if (vbuf.v_flags & V_FORGET) {
137 retval = retval & ~UA_KNOWN;
138 if ( ( ((roombuf->QRflags & QR_PRIVATE) == 0)
139 && ((roombuf->QRflags & QR_MAILBOX) == 0) )
140 || ( (roombuf->QRflags & QR_MAILBOX)
141 && (atol(roombuf->QRname) == CC->user.usernum))) {
142 retval = retval | UA_ZAPPED;
145 /* If user is explicitly locked out of this room, deny everything */
146 if (vbuf.v_flags & V_LOCKOUT) {
147 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED & ~UA_POSTALLOWED;
150 /* Aides get access to all private rooms */
151 if ( (userbuf->axlevel >= 6)
152 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
153 if (vbuf.v_flags & V_FORGET) {
154 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED;
157 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED;
161 /* Aides can gain access to mailboxes as well, but they don't show
164 if ( (userbuf->axlevel >= 6)
165 && (roombuf->QRflags & QR_MAILBOX) ) {
166 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED;
169 NEWMSG: /* By the way, we also check for the presence of new messages */
170 if (is_msg_in_sequence_set(vbuf.v_seen, roombuf->QRhighest) == 0) {
171 retval = retval | UA_HASNEWMSGS;
174 /* System rooms never show up in the list. */
175 if (roombuf->QRflags2 & QR2_SYSTEM) {
176 retval = retval & ~UA_KNOWN;
180 /* Now give the caller the information it wants. */
181 if (result != NULL) *result = retval;
182 if (view != NULL) *view = vbuf.v_view;
186 * Self-checking stuff for a room record read into memory
188 void room_sanity_check(struct ctdlroom *qrbuf)
190 /* Mailbox rooms are always on the lowest floor */
191 if (qrbuf->QRflags & QR_MAILBOX) {
194 /* Listing order of 0 is illegal except for base rooms */
195 if (qrbuf->QRorder == 0)
196 if (!(qrbuf->QRflags & QR_MAILBOX) &&
197 strncasecmp(qrbuf->QRname, config.c_baseroom, ROOMNAMELEN)
199 strncasecmp(qrbuf->QRname, config.c_aideroom, ROOMNAMELEN))
205 * getroom() - retrieve room data from disk
207 int getroom(struct ctdlroom *qrbuf, char *room_name)
209 struct cdbdata *cdbqr;
210 char lowercase_name[ROOMNAMELEN];
211 char personal_lowercase_name[ROOMNAMELEN];
214 for (a = 0; room_name[a] && a < sizeof lowercase_name - 1; ++a) {
215 lowercase_name[a] = tolower(room_name[a]);
217 lowercase_name[a] = 0;
219 memset(qrbuf, 0, sizeof(struct ctdlroom));
221 /* First, try the public namespace */
222 cdbqr = cdb_fetch(CDB_ROOMS,
223 lowercase_name, strlen(lowercase_name));
225 /* If that didn't work, try the user's personal namespace */
227 snprintf(personal_lowercase_name,
228 sizeof personal_lowercase_name, "%010ld.%s",
229 CC->user.usernum, lowercase_name);
230 cdbqr = cdb_fetch(CDB_ROOMS,
231 personal_lowercase_name,
232 strlen(personal_lowercase_name));
235 memcpy(qrbuf, cdbqr->ptr,
236 ((cdbqr->len > sizeof(struct ctdlroom)) ?
237 sizeof(struct ctdlroom) : cdbqr->len));
240 room_sanity_check(qrbuf);
249 * lgetroom() - same as getroom() but locks the record (if supported)
251 int lgetroom(struct ctdlroom *qrbuf, char *room_name)
254 retval = getroom(qrbuf, room_name);
255 if (retval == 0) begin_critical_section(S_ROOMS);
261 * b_putroom() - back end to putroom() and b_deleteroom()
262 * (if the supplied buffer is NULL, delete the room record)
264 void b_putroom(struct ctdlroom *qrbuf, char *room_name)
266 char lowercase_name[ROOMNAMELEN];
269 for (a = 0; a <= strlen(room_name); ++a) {
270 lowercase_name[a] = tolower(room_name[a]);
274 cdb_delete(CDB_ROOMS,
275 lowercase_name, strlen(lowercase_name));
277 time(&qrbuf->QRmtime);
279 lowercase_name, strlen(lowercase_name),
280 qrbuf, sizeof(struct ctdlroom));
286 * putroom() - store room data to disk
288 void putroom(struct ctdlroom *qrbuf) {
289 b_putroom(qrbuf, qrbuf->QRname);
294 * b_deleteroom() - delete a room record from disk
296 void b_deleteroom(char *room_name) {
297 b_putroom(NULL, room_name);
303 * lputroom() - same as putroom() but unlocks the record (if supported)
305 void lputroom(struct ctdlroom *qrbuf)
309 end_critical_section(S_ROOMS);
313 /****************************************************************************/
316 * getfloor() - retrieve floor data from disk
318 void getfloor(struct floor *flbuf, int floor_num)
320 struct cdbdata *cdbfl;
322 memset(flbuf, 0, sizeof(struct floor));
323 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
325 memcpy(flbuf, cdbfl->ptr,
326 ((cdbfl->len > sizeof(struct floor)) ?
327 sizeof(struct floor) : cdbfl->len));
330 if (floor_num == 0) {
331 safestrncpy(flbuf->f_name, "Main Floor",
332 sizeof flbuf->f_name);
333 flbuf->f_flags = F_INUSE;
334 flbuf->f_ref_count = 3;
341 * lgetfloor() - same as getfloor() but locks the record (if supported)
343 void lgetfloor(struct floor *flbuf, int floor_num)
346 begin_critical_section(S_FLOORTAB);
347 getfloor(flbuf, floor_num);
352 * cgetfloor() - Get floor record from *cache* (loads from disk if needed)
354 * This is strictly a performance hack.
356 struct floor *cgetfloor(int floor_num) {
357 static int initialized = 0;
360 struct floor *fl = NULL;
362 begin_critical_section(S_FLOORCACHE);
363 if (initialized == 0) {
364 for (i=0; i<MAXFLOORS; ++i) {
365 floorcache[floor_num] = NULL;
369 if (floorcache[floor_num] == NULL) {
372 end_critical_section(S_FLOORCACHE);
375 fl = malloc(sizeof(struct floor));
376 getfloor(fl, floor_num);
377 begin_critical_section(S_FLOORCACHE);
378 if (floorcache[floor_num] != NULL) {
379 free(floorcache[floor_num]);
381 floorcache[floor_num] = fl;
382 end_critical_section(S_FLOORCACHE);
385 return(floorcache[floor_num]);
391 * putfloor() - store floor data on disk
393 void putfloor(struct floor *flbuf, int floor_num)
395 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
396 begin_critical_section(S_FLOORCACHE);
397 if (floorcache[floor_num] != NULL) {
398 free(floorcache[floor_num]);
399 floorcache[floor_num] = malloc(sizeof(struct floor));
400 memcpy(floorcache[floor_num], flbuf, sizeof(struct floor));
402 end_critical_section(S_FLOORCACHE);
404 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
405 flbuf, sizeof(struct floor));
410 * lputfloor() - same as putfloor() but unlocks the record (if supported)
412 void lputfloor(struct floor *flbuf, int floor_num)
415 putfloor(flbuf, floor_num);
416 end_critical_section(S_FLOORTAB);
422 * Traverse the room file...
424 void ForEachRoom(void (*CallBack) (struct ctdlroom *EachRoom, void *out_data),
427 struct ctdlroom qrbuf;
428 struct cdbdata *cdbqr;
430 cdb_rewind(CDB_ROOMS);
432 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
433 memset(&qrbuf, 0, sizeof(struct ctdlroom));
434 memcpy(&qrbuf, cdbqr->ptr,
435 ((cdbqr->len > sizeof(struct ctdlroom)) ?
436 sizeof(struct ctdlroom) : cdbqr->len));
438 room_sanity_check(&qrbuf);
439 if (qrbuf.QRflags & QR_INUSE)
440 (*CallBack)(&qrbuf, in_data);
446 * delete_msglist() - delete room message pointers
448 void delete_msglist(struct ctdlroom *whichroom)
450 struct cdbdata *cdbml;
452 /* Make sure the msglist we're deleting actually exists, otherwise
453 * gdbm will complain when we try to delete an invalid record
455 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
459 /* Go ahead and delete it */
460 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
468 * sort message pointers
469 * (returns new msg count)
471 int sort_msglist(long listptrs[], int oldcount)
482 for (a = numitems - 2; a >= 0; --a) {
483 for (b = 0; b <= a; ++b) {
484 if (listptrs[b] > (listptrs[b + 1])) {
486 hold2 = listptrs[b + 1];
488 listptrs[b + 1] = hold1;
493 /* and yank any nulls */
494 while ((numitems > 0) && (listptrs[0] == 0L)) {
495 memcpy(&listptrs[0], &listptrs[1],
496 (sizeof(long) * (numitems - 1)));
505 * Determine whether a given room is non-editable.
507 int is_noneditable(struct ctdlroom *qrbuf)
510 /* Mail> rooms are non-editable */
511 if ( (qrbuf->QRflags & QR_MAILBOX)
512 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
515 /* Everything else is editable */
522 * Back-back-end for all room listing commands
524 void list_roomname(struct ctdlroom *qrbuf, int ra, int current_view, int default_view)
526 char truncated_roomname[ROOMNAMELEN];
528 /* For my own mailbox rooms, chop off the owner prefix */
529 if ( (qrbuf->QRflags & QR_MAILBOX)
530 && (atol(qrbuf->QRname) == CC->user.usernum) ) {
531 safestrncpy(truncated_roomname, qrbuf->QRname, sizeof truncated_roomname);
532 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
533 cprintf("%s", truncated_roomname);
535 /* For all other rooms, just display the name in its entirety */
537 cprintf("%s", qrbuf->QRname);
540 /* ...and now the other parameters */
541 cprintf("|%u|%d|%d|%d|%d|%d|%d|%ld|\n",
543 (int) qrbuf->QRfloor,
544 (int) qrbuf->QRorder,
545 (int) qrbuf->QRflags2,
555 * cmd_lrms() - List all accessible rooms, known or forgotten
557 void cmd_lrms_backend(struct ctdlroom *qrbuf, void *data)
559 int FloorBeingSearched = (-1);
563 FloorBeingSearched = *(int *)data;
564 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
566 if ((( ra & (UA_KNOWN | UA_ZAPPED)))
567 && ((qrbuf->QRfloor == (FloorBeingSearched))
568 || ((FloorBeingSearched) < 0)))
569 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
572 void cmd_lrms(char *argbuf)
574 int FloorBeingSearched = (-1);
575 if (strlen(argbuf) > 0)
576 FloorBeingSearched = extract_int(argbuf, 0);
578 if (CtdlAccessCheck(ac_logged_in)) return;
580 if (getuser(&CC->user, CC->curr_user)) {
581 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
584 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
586 ForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
593 * cmd_lkra() - List all known rooms
595 void cmd_lkra_backend(struct ctdlroom *qrbuf, void *data)
597 int FloorBeingSearched = (-1);
601 FloorBeingSearched = *(int *)data;
602 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
604 if ((( ra & (UA_KNOWN)))
605 && ((qrbuf->QRfloor == (FloorBeingSearched))
606 || ((FloorBeingSearched) < 0)))
607 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
610 void cmd_lkra(char *argbuf)
612 int FloorBeingSearched = (-1);
613 if (strlen(argbuf) > 0)
614 FloorBeingSearched = extract_int(argbuf, 0);
616 if (CtdlAccessCheck(ac_logged_in)) return;
618 if (getuser(&CC->user, CC->curr_user)) {
619 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
622 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
624 ForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
630 void cmd_lprm_backend(struct ctdlroom *qrbuf, void *data)
632 int FloorBeingSearched = (-1);
636 FloorBeingSearched = *(int *)data;
637 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
639 if ( ((qrbuf->QRflags & QR_PRIVATE) == 0)
640 && ((qrbuf->QRflags & QR_MAILBOX) == 0)
641 && ((qrbuf->QRfloor == (FloorBeingSearched))
642 || ((FloorBeingSearched) < 0)))
643 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
646 void cmd_lprm(char *argbuf)
648 int FloorBeingSearched = (-1);
649 if (strlen(argbuf) > 0)
650 FloorBeingSearched = extract_int(argbuf, 0);
652 cprintf("%d Publiic rooms:\n", LISTING_FOLLOWS);
654 ForEachRoom(cmd_lprm_backend, &FloorBeingSearched);
661 * cmd_lkrn() - List all known rooms with new messages
663 void cmd_lkrn_backend(struct ctdlroom *qrbuf, void *data)
665 int FloorBeingSearched = (-1);
669 FloorBeingSearched = *(int *)data;
670 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
673 && (ra & UA_HASNEWMSGS)
674 && ((qrbuf->QRfloor == (FloorBeingSearched))
675 || ((FloorBeingSearched) < 0)))
676 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
679 void cmd_lkrn(char *argbuf)
681 int FloorBeingSearched = (-1);
682 if (strlen(argbuf) > 0)
683 FloorBeingSearched = extract_int(argbuf, 0);
685 if (CtdlAccessCheck(ac_logged_in)) return;
687 if (getuser(&CC->user, CC->curr_user)) {
688 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
691 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
693 ForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
700 * cmd_lkro() - List all known rooms
702 void cmd_lkro_backend(struct ctdlroom *qrbuf, void *data)
704 int FloorBeingSearched = (-1);
708 FloorBeingSearched = *(int *)data;
709 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
712 && ((ra & UA_HASNEWMSGS) == 0)
713 && ((qrbuf->QRfloor == (FloorBeingSearched))
714 || ((FloorBeingSearched) < 0)))
715 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
718 void cmd_lkro(char *argbuf)
720 int FloorBeingSearched = (-1);
721 if (strlen(argbuf) > 0)
722 FloorBeingSearched = extract_int(argbuf, 0);
724 if (CtdlAccessCheck(ac_logged_in)) return;
726 if (getuser(&CC->user, CC->curr_user)) {
727 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
730 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
732 ForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
739 * cmd_lzrm() - List all forgotten rooms
741 void cmd_lzrm_backend(struct ctdlroom *qrbuf, void *data)
743 int FloorBeingSearched = (-1);
747 FloorBeingSearched = *(int *)data;
748 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
750 if ((ra & UA_GOTOALLOWED)
752 && ((qrbuf->QRfloor == (FloorBeingSearched))
753 || ((FloorBeingSearched) < 0)))
754 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
757 void cmd_lzrm(char *argbuf)
759 int FloorBeingSearched = (-1);
760 if (strlen(argbuf) > 0)
761 FloorBeingSearched = extract_int(argbuf, 0);
763 if (CtdlAccessCheck(ac_logged_in)) return;
765 if (getuser(&CC->user, CC->curr_user)) {
766 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
769 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
771 ForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
777 * Make the specified room the current room for this session. No validation
778 * or access control is done here -- the caller should make sure that the
779 * specified room exists and is ok to access.
781 void usergoto(char *where, int display_result, int transiently,
782 int *retmsgs, int *retnew)
785 int new_messages = 0;
786 int old_messages = 0;
787 int total_messages = 0;
791 int newmailcount = 0;
793 char truncated_roomname[ROOMNAMELEN];
794 struct cdbdata *cdbfr;
795 long *msglist = NULL;
797 unsigned int original_v_flags;
800 char setstr[128], lostr[64], histr[64];
804 /* If the supplied room name is NULL, the caller wants us to know that
805 * it has already copied the room record into CC->room, so
806 * we can skip the extra database fetch.
809 safestrncpy(CC->room.QRname, where, sizeof CC->room.QRname);
810 getroom(&CC->room, where);
813 /* Take care of all the formalities. */
815 begin_critical_section(S_USERS);
816 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
817 original_v_flags = vbuf.v_flags;
819 /* Know the room ... but not if it's the page log room, or if the
820 * caller specified that we're only entering this room transiently.
822 if ((strcasecmp(CC->room.QRname, config.c_logpages))
823 && (transiently == 0) ) {
824 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
825 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
828 /* Only rewrite the database record if we changed something */
829 if (vbuf.v_flags != original_v_flags) {
830 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
832 end_critical_section(S_USERS);
834 /* Check for new mail */
835 newmailcount = NewMailCount();
837 /* set info to 1 if the user needs to read the room's info file */
838 if (CC->room.QRinfo > vbuf.v_lastseen) {
843 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
845 msglist = (long *) cdbfr->ptr;
846 cdbfr->ptr = NULL; /* usergoto() now owns this memory */
847 num_msgs = cdbfr->len / sizeof(long);
852 for (a=0; a<num_msgs; ++a) {
853 if (msglist[a] > 0L) ++total_messages;
855 new_messages = num_msgs;
856 num_sets = num_tokens(vbuf.v_seen, ',');
857 for (s=0; s<num_sets; ++s) {
858 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
860 extract_token(lostr, setstr, 0, ':', sizeof lostr);
861 if (num_tokens(setstr, ':') >= 2) {
862 extract_token(histr, setstr, 1, ':', sizeof histr);
863 if (!strcmp(histr, "*")) {
864 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
868 strcpy(histr, lostr);
873 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
874 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
880 new_messages = total_messages - old_messages;
882 if (msglist != NULL) free(msglist);
884 if (CC->room.QRflags & QR_MAILBOX)
889 if ((CC->room.QRroomaide == CC->user.usernum)
890 || (CC->user.axlevel >= 6))
895 safestrncpy(truncated_roomname, CC->room.QRname, sizeof truncated_roomname);
896 if ( (CC->room.QRflags & QR_MAILBOX)
897 && (atol(CC->room.QRname) == CC->user.usernum) ) {
898 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
901 if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
905 if (retmsgs != NULL) *retmsgs = total_messages;
906 if (retnew != NULL) *retnew = new_messages;
907 lprintf(CTDL_DEBUG, "<%s> %d new of %d total messages\n",
909 new_messages, total_messages
912 CC->curr_view = (int)vbuf.v_view;
914 if (display_result) {
915 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|\n",
916 CIT_OK, CtdlCheckExpress(),
921 (int)CC->room.QRflags,
922 (long)CC->room.QRhighest,
923 (long)vbuf.v_lastseen,
927 (int)CC->room.QRfloor,
929 (int)CC->room.QRdefaultview,
937 * Handle some of the macro named rooms
939 void convert_room_name_macros(char *towhere, size_t maxlen) {
940 if (!strcasecmp(towhere, "_BASEROOM_")) {
941 safestrncpy(towhere, config.c_baseroom, maxlen);
943 else if (!strcasecmp(towhere, "_MAIL_")) {
944 safestrncpy(towhere, MAILROOM, maxlen);
946 else if (!strcasecmp(towhere, "_TRASH_")) {
947 safestrncpy(towhere, USERTRASHROOM, maxlen);
949 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
950 safestrncpy(towhere, config.c_twitroom, maxlen);
952 else if (!strcasecmp(towhere, "_CALENDAR_")) {
953 safestrncpy(towhere, USERCALENDARROOM, maxlen);
955 else if (!strcasecmp(towhere, "_TASKS_")) {
956 safestrncpy(towhere, USERTASKSROOM, maxlen);
958 else if (!strcasecmp(towhere, "_CONTACTS_")) {
959 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
961 else if (!strcasecmp(towhere, "_NOTES_")) {
962 safestrncpy(towhere, USERNOTESROOM, maxlen);
968 * cmd_goto() - goto a new room
970 void cmd_goto(char *gargs)
972 struct ctdlroom QRscratch;
976 char augmented_roomname[ROOMNAMELEN];
977 char towhere[ROOMNAMELEN];
981 if (CtdlAccessCheck(ac_logged_in)) return;
983 extract_token(towhere, gargs, 0, '|', sizeof towhere);
984 extract_token(password, gargs, 1, '|', sizeof password);
985 transiently = extract_int(gargs, 2);
987 getuser(&CC->user, CC->curr_user);
990 * Handle some of the macro named rooms
992 convert_room_name_macros(towhere, sizeof towhere);
994 /* First try a regular match */
995 c = getroom(&QRscratch, towhere);
997 /* Then try a mailbox name match */
999 MailboxName(augmented_roomname, sizeof augmented_roomname,
1000 &CC->user, towhere);
1001 c = getroom(&QRscratch, augmented_roomname);
1003 safestrncpy(towhere, augmented_roomname, sizeof towhere);
1006 /* And if the room was found... */
1009 /* Let internal programs go directly to any room. */
1010 if (CC->internal_pgm) {
1011 memcpy(&CC->room, &QRscratch,
1012 sizeof(struct ctdlroom));
1013 usergoto(NULL, 1, transiently, NULL, NULL);
1017 /* See if there is an existing user/room relationship */
1018 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
1020 /* normal clients have to pass through security */
1021 if (ra & UA_GOTOALLOWED) {
1026 if ((QRscratch.QRflags & QR_MAILBOX) &&
1027 ((ra & UA_GOTOALLOWED))) {
1028 memcpy(&CC->room, &QRscratch,
1029 sizeof(struct ctdlroom));
1030 usergoto(NULL, 1, transiently, NULL, NULL);
1032 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
1033 ((ra & UA_KNOWN) == 0) &&
1034 (strcasecmp(QRscratch.QRpasswd, password)) &&
1035 (CC->user.axlevel < 6)
1037 cprintf("%d wrong or missing passwd\n",
1038 ERROR + PASSWORD_REQUIRED);
1040 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
1041 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
1042 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
1043 ((ra & UA_KNOWN) == 0) &&
1044 (CC->user.axlevel < 6)
1046 lprintf(CTDL_DEBUG, "Failed to acquire private room\n");
1048 memcpy(&CC->room, &QRscratch,
1049 sizeof(struct ctdlroom));
1050 usergoto(NULL, 1, transiently, NULL, NULL);
1056 cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
1062 struct ctdluser temp;
1063 struct cdbdata *cdbus;
1066 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
1067 cdb_rewind(CDB_USERS);
1068 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1069 memset(&temp, 0, sizeof temp);
1070 memcpy(&temp, cdbus->ptr, sizeof temp);
1073 CtdlRoomAccess(&CC->room, &temp, &ra, NULL);
1074 if ((CC->room.QRflags & QR_INUSE)
1077 cprintf("%s\n", temp.fullname);
1084 * RDIR command for room directory
1092 struct stat statbuf;
1093 char tempfilename[PATH_MAX];
1095 if (CtdlAccessCheck(ac_logged_in)) return;
1096 CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
1098 getroom(&CC->room, CC->room.QRname);
1099 getuser(&CC->user, CC->curr_user);
1101 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
1102 cprintf("%d not here.\n", ERROR + NOT_HERE);
1105 if (((CC->room.QRflags & QR_VISDIR) == 0)
1106 && (CC->user.axlevel < 6)
1107 && (CC->user.usernum != CC->room.QRroomaide)) {
1108 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1111 cprintf("%d %s|%s/%s\n",
1115 CC->room.QRdirname);
1117 snprintf(buf, sizeof buf,
1118 "ls %s/%s >%s 2>/dev/null",
1124 snprintf(buf, sizeof buf,
1127 CC->room.QRdirname);
1128 fd = fopen(buf, "r");
1130 fd = fopen("/dev/null", "r");
1132 ls = fopen(tempfilename, "r");
1133 while (fgets(flnm, sizeof flnm, ls) != NULL) {
1134 flnm[strlen(flnm) - 1] = 0;
1135 if (strcasecmp(flnm, "filedir")) {
1136 snprintf(buf, sizeof buf,
1141 stat(buf, &statbuf);
1142 safestrncpy(comment, "", sizeof comment);
1144 while ((fgets(buf, sizeof buf, fd) != NULL)
1145 && (strlen(comment) == 0)) {
1146 buf[strlen(buf) - 1] = 0;
1147 if ((!strncasecmp(buf, flnm, strlen(flnm)))
1148 && (buf[strlen(flnm)] == ' '))
1149 safestrncpy(comment,
1150 &buf[strlen(flnm) + 1],
1153 cprintf("%s|%ld|%s\n", flnm, (long)statbuf.st_size, comment);
1158 unlink(tempfilename);
1164 * get room parameters (aide or room aide command)
1168 if (CtdlAccessCheck(ac_room_aide)) return;
1170 getroom(&CC->room, CC->room.QRname);
1171 cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1175 ((CC->room.QRflags & QR_MAILBOX) ?
1176 &CC->room.QRname[11] : CC->room.QRname),
1178 ((CC->room.QRflags & QR_PASSWORDED) ?
1179 CC->room.QRpasswd : ""),
1181 ((CC->room.QRflags & QR_DIRECTORY) ?
1182 CC->room.QRdirname : ""),
1185 (int) CC->room.QRfloor,
1186 (int) CC->room.QRorder,
1188 CC->room.QRdefaultview,
1195 * Back end function to rename a room.
1196 * You can also specify which floor to move the room to, or specify -1 to
1197 * keep the room on the same floor it was on.
1199 * If you are renaming a mailbox room, you must supply the namespace prefix
1200 * in *at least* the old name!
1202 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1204 struct ctdlroom qrbuf;
1205 struct ctdlroom qrtmp;
1210 char actual_old_name[ROOMNAMELEN];
1212 lprintf(CTDL_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1213 old_name, new_name, new_floor);
1215 if (new_floor >= 0) {
1216 fl = cgetfloor(new_floor);
1217 if ((fl->f_flags & F_INUSE) == 0) {
1218 return(crr_invalid_floor);
1222 begin_critical_section(S_ROOMS);
1224 if ( (getroom(&qrtmp, new_name) == 0)
1225 && (strcasecmp(new_name, old_name)) ) {
1226 ret = crr_already_exists;
1229 else if (getroom(&qrbuf, old_name) != 0) {
1230 ret = crr_room_not_found;
1233 else if ( (CC->user.axlevel < 6) && (!CC->internal_pgm)
1234 && (CC->user.usernum != qrbuf.QRroomaide)
1235 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) ) {
1236 ret = crr_access_denied;
1239 else if (is_noneditable(&qrbuf)) {
1240 ret = crr_noneditable;
1245 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1246 if (qrbuf.QRflags & QR_MAILBOX) {
1247 owner = atol(qrbuf.QRname);
1249 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1250 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1251 "%010ld.%s", owner, new_name);
1254 safestrncpy(qrbuf.QRname, new_name,
1255 sizeof(qrbuf.QRname));
1258 /* Reject change of floor for baseroom/aideroom */
1259 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1260 !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1264 /* Take care of floor stuff */
1265 old_floor = qrbuf.QRfloor;
1266 if (new_floor < 0) {
1267 new_floor = old_floor;
1269 qrbuf.QRfloor = new_floor;
1272 begin_critical_section(S_CONFIG);
1274 /* If baseroom/aideroom name changes, update config */
1275 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1276 safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1279 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1280 safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1284 end_critical_section(S_CONFIG);
1286 /* If the room name changed, then there are now two room
1287 * records, so we have to delete the old one.
1289 if (strcasecmp(new_name, old_name)) {
1290 b_deleteroom(actual_old_name);
1296 end_critical_section(S_ROOMS);
1298 /* Adjust the floor reference counts if necessary */
1299 if (new_floor != old_floor) {
1300 lgetfloor(&flbuf, old_floor);
1301 --flbuf.f_ref_count;
1302 lputfloor(&flbuf, old_floor);
1303 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1304 lgetfloor(&flbuf, new_floor);
1305 ++flbuf.f_ref_count;
1306 lputfloor(&flbuf, new_floor);
1307 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1310 /* ...and everybody say "YATTA!" */
1316 * set room parameters (aide or room aide command)
1318 void cmd_setr(char *args)
1324 char new_name[ROOMNAMELEN];
1326 if (CtdlAccessCheck(ac_logged_in)) return;
1328 if (num_parms(args) >= 6) {
1329 new_floor = extract_int(args, 5);
1331 new_floor = (-1); /* don't change the floor */
1334 /* When is a new name more than just a new name? When the old name
1335 * has a namespace prefix.
1337 if (CC->room.QRflags & QR_MAILBOX) {
1338 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
1340 safestrncpy(new_name, "", sizeof new_name);
1342 extract_token(&new_name[strlen(new_name)], args, 0, '|', (sizeof new_name - strlen(new_name)));
1344 r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
1346 if (r == crr_room_not_found) {
1347 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
1348 } else if (r == crr_already_exists) {
1349 cprintf("%d '%s' already exists.\n",
1350 ERROR + ALREADY_EXISTS, new_name);
1351 } else if (r == crr_noneditable) {
1352 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
1353 } else if (r == crr_invalid_floor) {
1354 cprintf("%d Target floor does not exist.\n",
1355 ERROR + INVALID_FLOOR_OPERATION);
1356 } else if (r == crr_access_denied) {
1357 cprintf("%d You do not have permission to edit '%s'\n",
1358 ERROR + HIGHER_ACCESS_REQUIRED,
1360 } else if (r != crr_ok) {
1361 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1362 ERROR + INTERNAL_ERROR, r);
1369 getroom(&CC->room, new_name);
1371 /* Now we have to do a bunch of other stuff */
1373 if (num_parms(args) >= 7) {
1374 new_order = extract_int(args, 6);
1377 if (new_order > 127)
1381 lgetroom(&CC->room, CC->room.QRname);
1383 /* Directory room */
1384 extract_token(buf, args, 2, '|', sizeof buf);
1386 safestrncpy(CC->room.QRdirname, buf,
1387 sizeof CC->room.QRdirname);
1390 if (num_parms(args) >= 8) {
1391 CC->room.QRdefaultview = extract_int(args, 7);
1394 /* Second set of flags */
1395 if (num_parms(args) >= 9) {
1396 CC->room.QRflags2 = extract_int(args, 8);
1400 CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
1401 /* Clean up a client boo-boo: if the client set the room to
1402 * guess-name or passworded, ensure that the private flag is
1405 if ((CC->room.QRflags & QR_GUESSNAME)
1406 || (CC->room.QRflags & QR_PASSWORDED))
1407 CC->room.QRflags |= QR_PRIVATE;
1409 /* Some changes can't apply to BASEROOM */
1410 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1412 CC->room.QRorder = 0;
1413 CC->room.QRpasswd[0] = '\0';
1414 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1415 QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1416 CC->room.QRflags |= QR_PERMANENT;
1418 /* March order (doesn't apply to AIDEROOM) */
1419 if (num_parms(args) >= 7)
1420 CC->room.QRorder = (char) new_order;
1422 extract_token(buf, args, 1, '|', sizeof buf);
1424 safestrncpy(CC->room.QRpasswd, buf,
1425 sizeof CC->room.QRpasswd);
1426 /* Kick everyone out if the client requested it
1427 * (by changing the room's generation number)
1429 if (extract_int(args, 4)) {
1430 time(&CC->room.QRgen);
1433 /* Some changes can't apply to AIDEROOM */
1434 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1436 CC->room.QRorder = 0;
1437 CC->room.QRflags &= ~QR_MAILBOX;
1438 CC->room.QRflags |= QR_PERMANENT;
1441 /* Write the room record back to disk */
1442 lputroom(&CC->room);
1444 /* Create a room directory if necessary */
1445 if (CC->room.QRflags & QR_DIRECTORY) {
1446 snprintf(buf, sizeof buf,"%s/%s",
1448 CC->room.QRdirname);
1451 snprintf(buf, sizeof buf, "The room \"%s\" has been edited by %s.\n",
1452 CC->room.QRname, CC->curr_user);
1453 aide_message(buf, "Room modification Message");
1454 cprintf("%d Ok\n", CIT_OK);
1460 * get the name of the room aide for this room
1464 struct ctdluser usbuf;
1466 if (CtdlAccessCheck(ac_logged_in)) return;
1468 if (getuserbynumber(&usbuf, CC->room.QRroomaide) == 0) {
1469 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1471 cprintf("%d \n", CIT_OK);
1477 * set the room aide for this room
1479 void cmd_seta(char *new_ra)
1481 struct ctdluser usbuf;
1486 if (CtdlAccessCheck(ac_room_aide)) return;
1488 if (getuser(&usbuf, new_ra) != 0) {
1491 newu = usbuf.usernum;
1494 lgetroom(&CC->room, CC->room.QRname);
1496 if (CC->room.QRroomaide != newu) {
1499 CC->room.QRroomaide = newu;
1500 lputroom(&CC->room);
1503 * We have to post the change notice _after_ writing changes to
1504 * the room table, otherwise it would deadlock!
1506 if (post_notice == 1) {
1507 if (strlen(usbuf.fullname) > 0)
1508 snprintf(buf, sizeof buf,
1509 "%s is now the room aide for \"%s\".\n",
1510 usbuf.fullname, CC->room.QRname);
1512 snprintf(buf, sizeof buf,
1513 "There is now no room aide for \"%s\".\n",
1515 aide_message(buf, "Aide Room Modification");
1517 cprintf("%d Ok\n", CIT_OK);
1521 * retrieve info file for this room
1529 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_info_dir);
1530 info_fp = fopen(filename, "r");
1532 if (info_fp == NULL) {
1533 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
1536 cprintf("%d Info:\n", LISTING_FOLLOWS);
1537 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1538 if (strlen(buf) > 0)
1539 buf[strlen(buf) - 1] = 0;
1540 cprintf("%s\n", buf);
1547 * Asynchronously schedule a room for deletion. The room will appear
1548 * deleted to the user(s), but it won't actually get purged from the
1549 * database until THE DREADED AUTO-PURGER makes its next run.
1551 void schedule_room_for_deletion(struct ctdlroom *qrbuf)
1553 char old_name[ROOMNAMELEN];
1556 lprintf(CTDL_NOTICE, "Scheduling room <%s> for deletion\n",
1559 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1561 getroom(qrbuf, qrbuf->QRname);
1563 /* Turn the room into a private mailbox owned by a user who doesn't
1564 * exist. This will immediately make the room invisible to everyone,
1565 * and qualify the room for purging.
1567 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1572 qrbuf->QRflags |= QR_MAILBOX;
1573 time(&qrbuf->QRgen); /* Use a timestamp as the new generation number */
1577 b_deleteroom(old_name);
1583 * Back end processing to delete a room and everything associated with it
1584 * (This one is synchronous and should only get called by THE DREADED
1585 * AUTO-PURGER in serv_expire.c. All user-facing code should call
1586 * the asynchronous schedule_room_for_deletion() instead.)
1588 void delete_room(struct ctdlroom *qrbuf)
1592 /* TODO: filename magic? does this realy work? */
1594 lprintf(CTDL_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1596 /* Delete the info file */
1597 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_info_dir);
1600 /* Delete the image file */
1601 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_image_dir);
1604 /* Delete the room's network config file */
1605 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1608 /* Delete the messages in the room
1609 * (Careful: this opens an S_ROOMS critical section!)
1611 CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
1613 /* Flag the room record as not in use */
1614 lgetroom(qrbuf, qrbuf->QRname);
1618 /* then decrement the reference count for the floor */
1619 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1620 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1621 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1623 /* Delete the room record from the database! */
1624 b_deleteroom(qrbuf->QRname);
1630 * Check access control for deleting a room
1632 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1634 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1638 if (is_noneditable(qr)) {
1643 * For mailboxes, check stuff
1645 if (qr->QRflags & QR_MAILBOX) {
1647 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1649 if (atol(qr->QRname) != CC->user.usernum) {
1650 return(0); /* not my room */
1653 /* Can't delete your Mail> room */
1654 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1656 /* Otherwise it's ok */
1661 * For normal rooms, just check for aide or room aide status.
1663 return(is_room_aide());
1667 * aide command: kill the current room
1669 void cmd_kill(char *argbuf)
1671 char deleted_room_name[ROOMNAMELEN];
1675 kill_ok = extract_int(argbuf, 0);
1677 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
1678 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1682 if (CC->room.QRflags & QR_MAILBOX) {
1683 safestrncpy(deleted_room_name, &CC->room.QRname[11], sizeof deleted_room_name);
1686 safestrncpy(deleted_room_name, CC->room.QRname, sizeof deleted_room_name);
1689 /* Do the dirty work */
1690 schedule_room_for_deletion(&CC->room);
1692 /* Return to the Lobby */
1693 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1695 /* tell the world what we did */
1696 snprintf(msg, sizeof msg, "The room \"%s\" has been deleted by %s.\n",
1697 deleted_room_name, CC->curr_user);
1698 aide_message(msg, "Room Purger Message");
1699 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1701 cprintf("%d ok to delete.\n", CIT_OK);
1707 * Internal code to create a new room (returns room flags)
1709 * Room types: 0=public, 1=guessname, 2=passworded, 3=inv-only,
1710 * 4=mailbox, 5=mailbox, but caller supplies namespace
1712 unsigned create_room(char *new_room_name,
1714 char *new_room_pass,
1721 struct ctdlroom qrbuf;
1725 lprintf(CTDL_DEBUG, "create_room(name=%s, type=%d, view=%d)\n",
1726 new_room_name, new_room_type, new_room_view);
1728 if (getroom(&qrbuf, new_room_name) == 0) {
1729 lprintf(CTDL_DEBUG, "%s already exists.\n", new_room_name);
1733 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1734 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1735 qrbuf.QRflags = QR_INUSE;
1736 if (new_room_type > 0)
1737 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1738 if (new_room_type == 1)
1739 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1740 if (new_room_type == 2)
1741 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1742 if ( (new_room_type == 4) || (new_room_type == 5) )
1743 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1745 /* If the user is requesting a personal room, set up the room
1746 * name accordingly (prepend the user number)
1748 if (new_room_type == 4) {
1749 MailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1752 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1755 /* If the room is private, and the system administrator has elected
1756 * to automatically grant room aide privileges, do so now; otherwise,
1757 * set the room aide to undefined.
1759 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1760 qrbuf.QRroomaide = CC->user.usernum;
1762 qrbuf.QRroomaide = (-1L);
1766 * If the caller is only interested in testing whether this will work,
1767 * return now without creating the room.
1769 if (!really_create) return (qrbuf.QRflags);
1771 qrbuf.QRnumber = get_new_room_number();
1772 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1773 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1774 qrbuf.QRfloor = new_room_floor;
1775 qrbuf.QRdefaultview = new_room_view;
1777 /* save what we just did... */
1780 /* bump the reference count on whatever floor the room is on */
1781 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1782 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1783 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1785 /* Grant the creator access to the room unless the avoid_access
1786 * parameter was specified.
1788 if ( (CC->logged_in) && (avoid_access == 0) ) {
1789 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1790 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1791 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1792 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1795 /* resume our happy day */
1796 return (qrbuf.QRflags);
1803 void cmd_cre8(char *args)
1806 char new_room_name[ROOMNAMELEN];
1808 char new_room_pass[32];
1811 char *notification_message = NULL;
1814 int avoid_access = 0;
1816 cre8_ok = extract_int(args, 0);
1817 extract_token(new_room_name, args, 1, '|', sizeof new_room_name);
1818 new_room_name[ROOMNAMELEN - 1] = 0;
1819 new_room_type = extract_int(args, 2);
1820 extract_token(new_room_pass, args, 3, '|', sizeof new_room_pass);
1821 avoid_access = extract_int(args, 5);
1822 new_room_view = extract_int(args, 6);
1823 new_room_pass[9] = 0;
1826 if ((strlen(new_room_name) == 0) && (cre8_ok == 1)) {
1827 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
1831 if (!strcasecmp(new_room_name, MAILROOM)) {
1832 cprintf("%d '%s' already exists.\n",
1833 ERROR + ALREADY_EXISTS, new_room_name);
1837 if (num_parms(args) >= 5) {
1838 fl = cgetfloor(extract_int(args, 4));
1840 cprintf("%d Invalid floor number.\n",
1841 ERROR + INVALID_FLOOR_OPERATION);
1844 else if ((fl->f_flags & F_INUSE) == 0) {
1845 cprintf("%d Invalid floor number.\n",
1846 ERROR + INVALID_FLOOR_OPERATION);
1849 new_room_floor = extract_int(args, 4);
1853 if (CtdlAccessCheck(ac_logged_in)) return;
1855 if (CC->user.axlevel < config.c_createax || CC->internal_pgm) {
1856 cprintf("%d You need higher access to create rooms.\n",
1857 ERROR + HIGHER_ACCESS_REQUIRED);
1861 if ((strlen(new_room_name) == 0) && (cre8_ok == 0)) {
1862 cprintf("%d Ok to create rooms.\n", CIT_OK);
1866 if ((new_room_type < 0) || (new_room_type > 5)) {
1867 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
1871 if (new_room_type == 5) {
1872 if (CC->user.axlevel < 6) {
1873 cprintf("%d Higher access required\n",
1874 ERROR + HIGHER_ACCESS_REQUIRED);
1879 /* Check to make sure the requested room name doesn't already exist */
1880 newflags = create_room(new_room_name,
1881 new_room_type, new_room_pass, new_room_floor,
1882 0, avoid_access, new_room_view);
1883 if (newflags == 0) {
1884 cprintf("%d '%s' already exists.\n",
1885 ERROR + ALREADY_EXISTS, new_room_name);
1890 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
1894 /* If we reach this point, the room needs to be created. */
1896 newflags = create_room(new_room_name,
1897 new_room_type, new_room_pass, new_room_floor, 1, 0,
1900 /* post a message in Aide> describing the new room */
1901 notification_message = malloc(1024);
1902 snprintf(notification_message, 1024,
1903 "A new room called \"%s\" has been created by %s%s%s%s%s%s\n",
1906 ((newflags & QR_MAILBOX) ? " [personal]" : ""),
1907 ((newflags & QR_PRIVATE) ? " [private]" : ""),
1908 ((newflags & QR_GUESSNAME) ? " [hidden]" : ""),
1909 ((newflags & QR_PASSWORDED) ? " Password: " : ""),
1910 ((newflags & QR_PASSWORDED) ? new_room_pass : "")
1912 aide_message(notification_message, "Room Creation Message");
1913 free(notification_message);
1915 cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
1920 void cmd_einf(char *ok)
1921 { /* enter info file for current room */
1923 char infofilename[SIZ];
1928 if (CtdlAccessCheck(ac_room_aide)) return;
1930 if (atoi(ok) == 0) {
1931 cprintf("%d Ok.\n", CIT_OK);
1934 assoc_file_name(infofilename, sizeof infofilename, &CC->room, ctdl_info_dir);
1935 lprintf(CTDL_DEBUG, "opening\n");
1936 fp = fopen(infofilename, "w");
1937 lprintf(CTDL_DEBUG, "checking\n");
1939 cprintf("%d Cannot open %s: %s\n",
1940 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1943 cprintf("%d Send info...\n", SEND_LISTING);
1946 client_getln(buf, sizeof buf);
1947 if (strcmp(buf, "000"))
1948 fprintf(fp, "%s\n", buf);
1949 } while (strcmp(buf, "000"));
1952 /* now update the room index so people will see our new info */
1953 lgetroom(&CC->room, CC->room.QRname); /* lock so no one steps on us */
1954 CC->room.QRinfo = CC->room.QRhighest + 1L;
1955 lputroom(&CC->room);
1960 * cmd_lflr() - List all known floors
1967 if (CtdlAccessCheck(ac_logged_in)) return;
1969 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1971 for (a = 0; a < MAXFLOORS; ++a) {
1972 getfloor(&flbuf, a);
1973 if (flbuf.f_flags & F_INUSE) {
1974 cprintf("%d|%s|%d\n",
1986 * create a new floor
1988 void cmd_cflr(char *argbuf)
1990 char new_floor_name[256];
1993 int free_slot = (-1);
1996 extract_token(new_floor_name, argbuf, 0, '|', sizeof new_floor_name);
1997 cflr_ok = extract_int(argbuf, 1);
1999 if (CtdlAccessCheck(ac_aide)) return;
2001 if (strlen(new_floor_name) == 0) {
2002 cprintf("%d Blank floor name not allowed.\n",
2003 ERROR + ILLEGAL_VALUE);
2007 for (a = 0; a < MAXFLOORS; ++a) {
2008 getfloor(&flbuf, a);
2010 /* note any free slots while we're scanning... */
2011 if (((flbuf.f_flags & F_INUSE) == 0)
2015 /* check to see if it already exists */
2016 if ((!strcasecmp(flbuf.f_name, new_floor_name))
2017 && (flbuf.f_flags & F_INUSE)) {
2018 cprintf("%d Floor '%s' already exists.\n",
2019 ERROR + ALREADY_EXISTS,
2025 if (free_slot < 0) {
2026 cprintf("%d There is no space available for a new floor.\n",
2027 ERROR + INVALID_FLOOR_OPERATION);
2031 cprintf("%d ok to create...\n", CIT_OK);
2034 lgetfloor(&flbuf, free_slot);
2035 flbuf.f_flags = F_INUSE;
2036 flbuf.f_ref_count = 0;
2037 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
2038 lputfloor(&flbuf, free_slot);
2039 cprintf("%d %d\n", CIT_OK, free_slot);
2047 void cmd_kflr(char *argbuf)
2050 int floor_to_delete;
2054 floor_to_delete = extract_int(argbuf, 0);
2055 kflr_ok = extract_int(argbuf, 1);
2057 if (CtdlAccessCheck(ac_aide)) return;
2059 lgetfloor(&flbuf, floor_to_delete);
2062 if ((flbuf.f_flags & F_INUSE) == 0) {
2063 cprintf("%d Floor %d not in use.\n",
2064 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
2067 if (flbuf.f_ref_count != 0) {
2068 cprintf("%d Cannot delete; floor contains %d rooms.\n",
2069 ERROR + INVALID_FLOOR_OPERATION,
2074 cprintf("%d Ok\n", CIT_OK);
2076 cprintf("%d Ok to delete...\n", CIT_OK);
2083 if ((delete_ok == 1) && (kflr_ok == 1))
2085 lputfloor(&flbuf, floor_to_delete);
2091 void cmd_eflr(char *argbuf)
2097 np = num_parms(argbuf);
2099 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
2103 if (CtdlAccessCheck(ac_aide)) return;
2105 floor_num = extract_int(argbuf, 0);
2106 lgetfloor(&flbuf, floor_num);
2107 if ((flbuf.f_flags & F_INUSE) == 0) {
2108 lputfloor(&flbuf, floor_num);
2109 cprintf("%d Floor %d is not in use.\n",
2110 ERROR + INVALID_FLOOR_OPERATION, floor_num);
2114 extract_token(flbuf.f_name, argbuf, 1, '|', sizeof flbuf.f_name);
2115 lputfloor(&flbuf, floor_num);
2117 cprintf("%d Ok\n", CIT_OK);