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"
43 struct floor *floorcache[MAXFLOORS];
46 * Generic routine for determining user access to rooms
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);
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;
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 generation number matchups */
106 if (roombuf->QRflags & QR_MAILBOX) {
107 if (userbuf->usernum == atol(roombuf->QRname)) {
108 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
110 /* An explicit match means the user belongs in this room */
111 if (vbuf.v_flags & V_ACCESS) {
112 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
116 /* Check to see if the user has forgotten this room */
117 if (vbuf.v_flags & V_FORGET) {
118 retval = retval & ~UA_KNOWN;
119 if ( ( ((roombuf->QRflags & QR_PRIVATE) == 0)
120 && ((roombuf->QRflags & QR_MAILBOX) == 0) )
121 || ( (roombuf->QRflags & QR_MAILBOX)
122 && (atol(roombuf->QRname) == CC->user.usernum))) {
123 retval = retval | UA_ZAPPED;
126 /* If user is explicitly locked out of this room, deny everything */
127 if (vbuf.v_flags & V_LOCKOUT) {
128 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
131 /* Aides get access to all private rooms */
132 if ( (userbuf->axlevel >= 6)
133 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
134 if (vbuf.v_flags & V_FORGET) {
135 retval = retval | UA_GOTOALLOWED;
138 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
142 /* Aides can gain access to mailboxes as well, but they don't show
145 if ( (userbuf->axlevel >= 6)
146 && (roombuf->QRflags & QR_MAILBOX) ) {
147 retval = retval | UA_GOTOALLOWED;
150 NEWMSG: /* By the way, we also check for the presence of new messages */
151 if (is_msg_in_sequence_set(vbuf.v_seen, roombuf->QRhighest) == 0) {
152 retval = retval | UA_HASNEWMSGS;
155 /* System rooms never show up in the list. */
156 if (roombuf->QRflags2 & QR2_SYSTEM) {
157 retval = retval & ~UA_KNOWN;
161 /* Now give the caller the information it wants. */
162 if (result != NULL) *result = retval;
163 if (view != NULL) *view = vbuf.v_view;
167 * Self-checking stuff for a room record read into memory
169 void room_sanity_check(struct ctdlroom *qrbuf)
171 /* Mailbox rooms are always on the lowest floor */
172 if (qrbuf->QRflags & QR_MAILBOX) {
175 /* Listing order of 0 is illegal except for base rooms */
176 if (qrbuf->QRorder == 0)
177 if (!(qrbuf->QRflags & QR_MAILBOX) &&
178 strncasecmp(qrbuf->QRname, config.c_baseroom, ROOMNAMELEN)
180 strncasecmp(qrbuf->QRname, config.c_aideroom, ROOMNAMELEN))
186 * getroom() - retrieve room data from disk
188 int getroom(struct ctdlroom *qrbuf, char *room_name)
190 struct cdbdata *cdbqr;
191 char lowercase_name[ROOMNAMELEN];
192 char personal_lowercase_name[ROOMNAMELEN];
195 for (a = 0; room_name[a] && a < sizeof lowercase_name - 1; ++a) {
196 lowercase_name[a] = tolower(room_name[a]);
198 lowercase_name[a] = 0;
200 memset(qrbuf, 0, sizeof(struct ctdlroom));
202 /* First, try the public namespace */
203 cdbqr = cdb_fetch(CDB_ROOMS,
204 lowercase_name, strlen(lowercase_name));
206 /* If that didn't work, try the user's personal namespace */
208 snprintf(personal_lowercase_name,
209 sizeof personal_lowercase_name, "%010ld.%s",
210 CC->user.usernum, lowercase_name);
211 cdbqr = cdb_fetch(CDB_ROOMS,
212 personal_lowercase_name,
213 strlen(personal_lowercase_name));
216 memcpy(qrbuf, cdbqr->ptr,
217 ((cdbqr->len > sizeof(struct ctdlroom)) ?
218 sizeof(struct ctdlroom) : cdbqr->len));
221 room_sanity_check(qrbuf);
230 * lgetroom() - same as getroom() but locks the record (if supported)
232 int lgetroom(struct ctdlroom *qrbuf, char *room_name)
235 retval = getroom(qrbuf, room_name);
236 if (retval == 0) begin_critical_section(S_ROOMS);
242 * b_putroom() - back end to putroom() and b_deleteroom()
243 * (if the supplied buffer is NULL, delete the room record)
245 void b_putroom(struct ctdlroom *qrbuf, char *room_name)
247 char lowercase_name[ROOMNAMELEN];
250 for (a = 0; a <= strlen(room_name); ++a) {
251 lowercase_name[a] = tolower(room_name[a]);
255 cdb_delete(CDB_ROOMS,
256 lowercase_name, strlen(lowercase_name));
258 time(&qrbuf->QRmtime);
260 lowercase_name, strlen(lowercase_name),
261 qrbuf, sizeof(struct ctdlroom));
267 * putroom() - store room data to disk
269 void putroom(struct ctdlroom *qrbuf) {
270 b_putroom(qrbuf, qrbuf->QRname);
275 * b_deleteroom() - delete a room record from disk
277 void b_deleteroom(char *room_name) {
278 b_putroom(NULL, room_name);
284 * lputroom() - same as putroom() but unlocks the record (if supported)
286 void lputroom(struct ctdlroom *qrbuf)
290 end_critical_section(S_ROOMS);
294 /****************************************************************************/
297 * getfloor() - retrieve floor data from disk
299 void getfloor(struct floor *flbuf, int floor_num)
301 struct cdbdata *cdbfl;
303 memset(flbuf, 0, sizeof(struct floor));
304 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
306 memcpy(flbuf, cdbfl->ptr,
307 ((cdbfl->len > sizeof(struct floor)) ?
308 sizeof(struct floor) : cdbfl->len));
311 if (floor_num == 0) {
312 safestrncpy(flbuf->f_name, "Main Floor",
313 sizeof flbuf->f_name);
314 flbuf->f_flags = F_INUSE;
315 flbuf->f_ref_count = 3;
322 * lgetfloor() - same as getfloor() but locks the record (if supported)
324 void lgetfloor(struct floor *flbuf, int floor_num)
327 begin_critical_section(S_FLOORTAB);
328 getfloor(flbuf, floor_num);
333 * cgetfloor() - Get floor record from *cache* (loads from disk if needed)
335 * This is strictly a performance hack.
337 struct floor *cgetfloor(int floor_num) {
338 static int initialized = 0;
341 struct floor *fl = NULL;
343 begin_critical_section(S_FLOORCACHE);
344 if (initialized == 0) {
345 for (i=0; i<MAXFLOORS; ++i) {
346 floorcache[floor_num] = NULL;
350 if (floorcache[floor_num] == NULL) {
353 end_critical_section(S_FLOORCACHE);
356 fl = malloc(sizeof(struct floor));
357 getfloor(fl, floor_num);
358 begin_critical_section(S_FLOORCACHE);
359 if (floorcache[floor_num] != NULL) {
360 free(floorcache[floor_num]);
362 floorcache[floor_num] = fl;
363 end_critical_section(S_FLOORCACHE);
366 return(floorcache[floor_num]);
372 * putfloor() - store floor data on disk
374 void putfloor(struct floor *flbuf, int floor_num)
376 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
377 begin_critical_section(S_FLOORCACHE);
378 if (floorcache[floor_num] != NULL) {
379 free(floorcache[floor_num]);
380 floorcache[floor_num] = malloc(sizeof(struct floor));
381 memcpy(floorcache[floor_num], flbuf, sizeof(struct floor));
383 end_critical_section(S_FLOORCACHE);
385 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
386 flbuf, sizeof(struct floor));
391 * lputfloor() - same as putfloor() but unlocks the record (if supported)
393 void lputfloor(struct floor *flbuf, int floor_num)
396 putfloor(flbuf, floor_num);
397 end_critical_section(S_FLOORTAB);
403 * Traverse the room file...
405 void ForEachRoom(void (*CallBack) (struct ctdlroom *EachRoom, void *out_data),
408 struct ctdlroom qrbuf;
409 struct cdbdata *cdbqr;
411 cdb_rewind(CDB_ROOMS);
413 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
414 memset(&qrbuf, 0, sizeof(struct ctdlroom));
415 memcpy(&qrbuf, cdbqr->ptr,
416 ((cdbqr->len > sizeof(struct ctdlroom)) ?
417 sizeof(struct ctdlroom) : cdbqr->len));
419 room_sanity_check(&qrbuf);
420 if (qrbuf.QRflags & QR_INUSE)
421 (*CallBack)(&qrbuf, in_data);
427 * delete_msglist() - delete room message pointers
429 void delete_msglist(struct ctdlroom *whichroom)
431 struct cdbdata *cdbml;
433 /* Make sure the msglist we're deleting actually exists, otherwise
434 * gdbm will complain when we try to delete an invalid record
436 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
440 /* Go ahead and delete it */
441 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
449 * sort message pointers
450 * (returns new msg count)
452 int sort_msglist(long listptrs[], int oldcount)
463 for (a = numitems - 2; a >= 0; --a) {
464 for (b = 0; b <= a; ++b) {
465 if (listptrs[b] > (listptrs[b + 1])) {
467 hold2 = listptrs[b + 1];
469 listptrs[b + 1] = hold1;
474 /* and yank any nulls */
475 while ((numitems > 0) && (listptrs[0] == 0L)) {
476 memcpy(&listptrs[0], &listptrs[1],
477 (sizeof(long) * (numitems - 1)));
486 * Determine whether a given room is non-editable.
488 int is_noneditable(struct ctdlroom *qrbuf)
491 /* Mail> rooms are non-editable */
492 if ( (qrbuf->QRflags & QR_MAILBOX)
493 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
496 /* Everything else is editable */
503 * Back-back-end for all room listing commands
505 void list_roomname(struct ctdlroom *qrbuf, int ra, int view)
507 char truncated_roomname[ROOMNAMELEN];
509 /* For my own mailbox rooms, chop off the owner prefix */
510 if ( (qrbuf->QRflags & QR_MAILBOX)
511 && (atol(qrbuf->QRname) == CC->user.usernum) ) {
512 safestrncpy(truncated_roomname, qrbuf->QRname, sizeof truncated_roomname);
513 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
514 cprintf("%s", truncated_roomname);
516 /* For all other rooms, just display the name in its entirety */
518 cprintf("%s", qrbuf->QRname);
521 /* ...and now the other parameters */
522 cprintf("|%u|%d|%d|%d|%d|%d|\n",
524 (int) qrbuf->QRfloor,
525 (int) qrbuf->QRorder,
526 (int) qrbuf->QRflags2,
534 * cmd_lrms() - List all accessible rooms, known or forgotten
536 void cmd_lrms_backend(struct ctdlroom *qrbuf, void *data)
538 int FloorBeingSearched = (-1);
542 FloorBeingSearched = *(int *)data;
543 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
545 if ((( ra & (UA_KNOWN | UA_ZAPPED)))
546 && ((qrbuf->QRfloor == (FloorBeingSearched))
547 || ((FloorBeingSearched) < 0)))
548 list_roomname(qrbuf, ra, view);
551 void cmd_lrms(char *argbuf)
553 int FloorBeingSearched = (-1);
554 if (strlen(argbuf) > 0)
555 FloorBeingSearched = extract_int(argbuf, 0);
557 if (CtdlAccessCheck(ac_logged_in)) return;
559 if (getuser(&CC->user, CC->curr_user)) {
560 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
563 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
565 ForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
572 * cmd_lkra() - List all known rooms
574 void cmd_lkra_backend(struct ctdlroom *qrbuf, void *data)
576 int FloorBeingSearched = (-1);
580 FloorBeingSearched = *(int *)data;
581 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
583 if ((( ra & (UA_KNOWN)))
584 && ((qrbuf->QRfloor == (FloorBeingSearched))
585 || ((FloorBeingSearched) < 0)))
586 list_roomname(qrbuf, ra, view);
589 void cmd_lkra(char *argbuf)
591 int FloorBeingSearched = (-1);
592 if (strlen(argbuf) > 0)
593 FloorBeingSearched = extract_int(argbuf, 0);
595 if (CtdlAccessCheck(ac_logged_in)) return;
597 if (getuser(&CC->user, CC->curr_user)) {
598 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
601 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
603 ForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
609 void cmd_lprm_backend(struct ctdlroom *qrbuf, void *data)
611 int FloorBeingSearched = (-1);
615 FloorBeingSearched = *(int *)data;
616 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
618 if ( ((qrbuf->QRflags & QR_PRIVATE) == 0)
619 && ((qrbuf->QRflags & QR_MAILBOX) == 0)
620 && ((qrbuf->QRfloor == (FloorBeingSearched))
621 || ((FloorBeingSearched) < 0)))
622 list_roomname(qrbuf, ra, view);
625 void cmd_lprm(char *argbuf)
627 int FloorBeingSearched = (-1);
628 if (strlen(argbuf) > 0)
629 FloorBeingSearched = extract_int(argbuf, 0);
631 cprintf("%d Publiic rooms:\n", LISTING_FOLLOWS);
633 ForEachRoom(cmd_lprm_backend, &FloorBeingSearched);
640 * cmd_lkrn() - List all known rooms with new messages
642 void cmd_lkrn_backend(struct ctdlroom *qrbuf, void *data)
644 int FloorBeingSearched = (-1);
648 FloorBeingSearched = *(int *)data;
649 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
652 && (ra & UA_HASNEWMSGS)
653 && ((qrbuf->QRfloor == (FloorBeingSearched))
654 || ((FloorBeingSearched) < 0)))
655 list_roomname(qrbuf, ra, view);
658 void cmd_lkrn(char *argbuf)
660 int FloorBeingSearched = (-1);
661 if (strlen(argbuf) > 0)
662 FloorBeingSearched = extract_int(argbuf, 0);
664 if (CtdlAccessCheck(ac_logged_in)) return;
666 if (getuser(&CC->user, CC->curr_user)) {
667 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
670 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
672 ForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
679 * cmd_lkro() - List all known rooms
681 void cmd_lkro_backend(struct ctdlroom *qrbuf, void *data)
683 int FloorBeingSearched = (-1);
687 FloorBeingSearched = *(int *)data;
688 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
691 && ((ra & UA_HASNEWMSGS) == 0)
692 && ((qrbuf->QRfloor == (FloorBeingSearched))
693 || ((FloorBeingSearched) < 0)))
694 list_roomname(qrbuf, ra, view);
697 void cmd_lkro(char *argbuf)
699 int FloorBeingSearched = (-1);
700 if (strlen(argbuf) > 0)
701 FloorBeingSearched = extract_int(argbuf, 0);
703 if (CtdlAccessCheck(ac_logged_in)) return;
705 if (getuser(&CC->user, CC->curr_user)) {
706 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
709 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
711 ForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
718 * cmd_lzrm() - List all forgotten rooms
720 void cmd_lzrm_backend(struct ctdlroom *qrbuf, void *data)
722 int FloorBeingSearched = (-1);
726 FloorBeingSearched = *(int *)data;
727 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
729 if ((ra & UA_GOTOALLOWED)
731 && ((qrbuf->QRfloor == (FloorBeingSearched))
732 || ((FloorBeingSearched) < 0)))
733 list_roomname(qrbuf, ra, view);
736 void cmd_lzrm(char *argbuf)
738 int FloorBeingSearched = (-1);
739 if (strlen(argbuf) > 0)
740 FloorBeingSearched = extract_int(argbuf, 0);
742 if (CtdlAccessCheck(ac_logged_in)) return;
744 if (getuser(&CC->user, CC->curr_user)) {
745 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
748 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
750 ForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
756 * Make the specified room the current room for this session. No validation
757 * or access control is done here -- the caller should make sure that the
758 * specified room exists and is ok to access.
760 void usergoto(char *where, int display_result, int transiently,
761 int *retmsgs, int *retnew)
764 int new_messages = 0;
765 int old_messages = 0;
766 int total_messages = 0;
770 int newmailcount = 0;
772 char truncated_roomname[ROOMNAMELEN];
773 struct cdbdata *cdbfr;
774 long *msglist = NULL;
776 unsigned int original_v_flags;
779 char setstr[128], lostr[64], histr[64];
783 /* If the supplied room name is NULL, the caller wants us to know that
784 * it has already copied the room record into CC->room, so
785 * we can skip the extra database fetch.
788 safestrncpy(CC->room.QRname, where, sizeof CC->room.QRname);
789 getroom(&CC->room, where);
792 /* Take care of all the formalities. */
794 begin_critical_section(S_USERS);
795 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
796 original_v_flags = vbuf.v_flags;
798 /* Know the room ... but not if it's the page log room, or if the
799 * caller specified that we're only entering this room transiently.
801 if ((strcasecmp(CC->room.QRname, config.c_logpages))
802 && (transiently == 0) ) {
803 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
804 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
807 /* Only rewrite the database record if we changed something */
808 if (vbuf.v_flags != original_v_flags) {
809 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
811 end_critical_section(S_USERS);
813 /* Check for new mail */
814 newmailcount = NewMailCount();
816 /* set info to 1 if the user needs to read the room's info file */
817 if (CC->room.QRinfo > vbuf.v_lastseen) {
822 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
824 msglist = (long *) cdbfr->ptr;
825 cdbfr->ptr = NULL; /* usergoto() now owns this memory */
826 num_msgs = cdbfr->len / sizeof(long);
831 for (a=0; a<num_msgs; ++a) {
832 if (msglist[a] > 0L) ++total_messages;
834 new_messages = num_msgs;
835 num_sets = num_tokens(vbuf.v_seen, ',');
836 for (s=0; s<num_sets; ++s) {
837 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
839 extract_token(lostr, setstr, 0, ':', sizeof lostr);
840 if (num_tokens(setstr, ':') >= 2) {
841 extract_token(histr, setstr, 1, ':', sizeof histr);
842 if (!strcmp(histr, "*")) {
843 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
847 strcpy(histr, lostr);
852 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
853 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
859 new_messages = total_messages - old_messages;
861 if (msglist != NULL) free(msglist);
863 if (CC->room.QRflags & QR_MAILBOX)
868 if ((CC->room.QRroomaide == CC->user.usernum)
869 || (CC->user.axlevel >= 6))
874 safestrncpy(truncated_roomname, CC->room.QRname, sizeof truncated_roomname);
875 if ( (CC->room.QRflags & QR_MAILBOX)
876 && (atol(CC->room.QRname) == CC->user.usernum) ) {
877 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
880 if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
884 if (retmsgs != NULL) *retmsgs = total_messages;
885 if (retnew != NULL) *retnew = new_messages;
886 lprintf(CTDL_DEBUG, "<%s> %d new of %d total messages\n",
888 new_messages, total_messages
891 CC->curr_view = (int)vbuf.v_view;
893 if (display_result) {
894 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|\n",
895 CIT_OK, CtdlCheckExpress(),
900 (int)CC->room.QRflags,
901 (long)CC->room.QRhighest,
902 (long)vbuf.v_lastseen,
906 (int)CC->room.QRfloor,
908 (int)CC->room.QRdefaultview,
916 * Handle some of the macro named rooms
918 void convert_room_name_macros(char *towhere, size_t maxlen) {
919 if (!strcasecmp(towhere, "_BASEROOM_")) {
920 safestrncpy(towhere, config.c_baseroom, maxlen);
922 else if (!strcasecmp(towhere, "_MAIL_")) {
923 safestrncpy(towhere, MAILROOM, maxlen);
925 else if (!strcasecmp(towhere, "_TRASH_")) {
926 safestrncpy(towhere, USERTRASHROOM, maxlen);
928 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
929 safestrncpy(towhere, config.c_twitroom, maxlen);
931 else if (!strcasecmp(towhere, "_CALENDAR_")) {
932 safestrncpy(towhere, USERCALENDARROOM, maxlen);
934 else if (!strcasecmp(towhere, "_TASKS_")) {
935 safestrncpy(towhere, USERTASKSROOM, maxlen);
937 else if (!strcasecmp(towhere, "_CONTACTS_")) {
938 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
940 else if (!strcasecmp(towhere, "_NOTES_")) {
941 safestrncpy(towhere, USERNOTESROOM, maxlen);
947 * cmd_goto() - goto a new room
949 void cmd_goto(char *gargs)
951 struct ctdlroom QRscratch;
955 char augmented_roomname[ROOMNAMELEN];
956 char towhere[ROOMNAMELEN];
960 if (CtdlAccessCheck(ac_logged_in)) return;
962 extract_token(towhere, gargs, 0, '|', sizeof towhere);
963 extract_token(password, gargs, 1, '|', sizeof password);
964 transiently = extract_int(gargs, 2);
966 getuser(&CC->user, CC->curr_user);
969 * Handle some of the macro named rooms
971 convert_room_name_macros(towhere, sizeof towhere);
973 /* First try a regular match */
974 c = getroom(&QRscratch, towhere);
976 /* Then try a mailbox name match */
978 MailboxName(augmented_roomname, sizeof augmented_roomname,
980 c = getroom(&QRscratch, augmented_roomname);
982 safestrncpy(towhere, augmented_roomname, sizeof towhere);
985 /* And if the room was found... */
988 /* Let internal programs go directly to any room. */
989 if (CC->internal_pgm) {
990 memcpy(&CC->room, &QRscratch,
991 sizeof(struct ctdlroom));
992 usergoto(NULL, 1, transiently, NULL, NULL);
996 /* See if there is an existing user/room relationship */
997 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
999 /* normal clients have to pass through security */
1000 if (ra & UA_GOTOALLOWED) {
1005 if ((QRscratch.QRflags & QR_MAILBOX) &&
1006 ((ra & UA_GOTOALLOWED))) {
1007 memcpy(&CC->room, &QRscratch,
1008 sizeof(struct ctdlroom));
1009 usergoto(NULL, 1, transiently, NULL, NULL);
1011 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
1012 ((ra & UA_KNOWN) == 0) &&
1013 (strcasecmp(QRscratch.QRpasswd, password)) &&
1014 (CC->user.axlevel < 6)
1016 cprintf("%d wrong or missing passwd\n",
1017 ERROR + PASSWORD_REQUIRED);
1019 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
1020 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
1021 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
1022 ((ra & UA_KNOWN) == 0) &&
1023 (CC->user.axlevel < 6)
1025 lprintf(CTDL_DEBUG, "Failed to acquire private room\n");
1027 memcpy(&CC->room, &QRscratch,
1028 sizeof(struct ctdlroom));
1029 usergoto(NULL, 1, transiently, NULL, NULL);
1035 cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
1041 struct ctdluser temp;
1042 struct cdbdata *cdbus;
1045 getuser(&CC->user, CC->curr_user);
1048 * This command is only allowed by aides, room aides,
1049 * and room namespace owners
1052 || (atol(CC->room.QRname) == CC->user.usernum) ) {
1053 /* access granted */
1057 cprintf("%d Higher access or room ownership required.\n",
1058 ERROR + HIGHER_ACCESS_REQUIRED);
1062 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
1063 cdb_rewind(CDB_USERS);
1064 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1065 memset(&temp, 0, sizeof temp);
1066 memcpy(&temp, cdbus->ptr, sizeof temp);
1069 CtdlRoomAccess(&CC->room, &temp, &ra, NULL);
1070 if ((CC->room.QRflags & QR_INUSE)
1073 cprintf("%s\n", temp.fullname);
1080 * RDIR command for room directory
1088 struct stat statbuf;
1089 char tempfilename[PATH_MAX];
1091 if (CtdlAccessCheck(ac_logged_in)) return;
1092 CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
1094 getroom(&CC->room, CC->room.QRname);
1095 getuser(&CC->user, CC->curr_user);
1097 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
1098 cprintf("%d not here.\n", ERROR + NOT_HERE);
1101 if (((CC->room.QRflags & QR_VISDIR) == 0)
1102 && (CC->user.axlevel < 6)
1103 && (CC->user.usernum != CC->room.QRroomaide)) {
1104 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1108 #ifndef HAVE_DATA_DIR
1114 LISTING_FOLLOWS, config.c_fqdn, CC->room.QRdirname);
1116 snprintf(buf, sizeof buf, "ls "
1117 #ifndef HAVE_DATA_DIR
1122 "/files/%s >%s 2> /dev/null",
1123 CC->room.QRdirname, tempfilename);
1126 snprintf(buf, sizeof buf,
1127 #ifndef HAVE_DATA_DIR
1132 "/files/%s/filedir", CC->room.QRdirname);
1133 fd = fopen(buf, "r");
1135 fd = fopen("/dev/null", "r");
1137 ls = fopen(tempfilename, "r");
1138 while (fgets(flnm, sizeof flnm, ls) != NULL) {
1139 flnm[strlen(flnm) - 1] = 0;
1140 if (strcasecmp(flnm, "filedir")) {
1141 snprintf(buf, sizeof buf,
1142 #ifndef HAVE_DATA_DIR
1148 CC->room.QRdirname, flnm);
1149 stat(buf, &statbuf);
1150 safestrncpy(comment, "", sizeof comment);
1152 while ((fgets(buf, sizeof buf, fd) != NULL)
1153 && (strlen(comment) == 0)) {
1154 buf[strlen(buf) - 1] = 0;
1155 if ((!strncasecmp(buf, flnm, strlen(flnm)))
1156 && (buf[strlen(flnm)] == ' '))
1157 safestrncpy(comment,
1158 &buf[strlen(flnm) + 1],
1161 cprintf("%s|%ld|%s\n", flnm, (long)statbuf.st_size, comment);
1166 unlink(tempfilename);
1172 * get room parameters (aide or room aide command)
1176 if (CtdlAccessCheck(ac_room_aide)) return;
1178 getroom(&CC->room, CC->room.QRname);
1179 cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1183 ((CC->room.QRflags & QR_MAILBOX) ?
1184 &CC->room.QRname[11] : CC->room.QRname),
1186 ((CC->room.QRflags & QR_PASSWORDED) ?
1187 CC->room.QRpasswd : ""),
1189 ((CC->room.QRflags & QR_DIRECTORY) ?
1190 CC->room.QRdirname : ""),
1193 (int) CC->room.QRfloor,
1194 (int) CC->room.QRorder,
1196 CC->room.QRdefaultview,
1203 * Back end function to rename a room.
1204 * You can also specify which floor to move the room to, or specify -1 to
1205 * keep the room on the same floor it was on.
1207 * If you are renaming a mailbox room, you must supply the namespace prefix
1208 * in *at least* the old name!
1210 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1212 struct ctdlroom qrbuf;
1213 struct ctdlroom qrtmp;
1218 char actual_old_name[ROOMNAMELEN];
1220 lprintf(CTDL_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1221 old_name, new_name, new_floor);
1223 if (new_floor >= 0) {
1224 fl = cgetfloor(new_floor);
1225 if ((fl->f_flags & F_INUSE) == 0) {
1226 return(crr_invalid_floor);
1230 begin_critical_section(S_ROOMS);
1232 if ( (getroom(&qrtmp, new_name) == 0)
1233 && (strcasecmp(new_name, old_name)) ) {
1234 ret = crr_already_exists;
1237 else if (getroom(&qrbuf, old_name) != 0) {
1238 ret = crr_room_not_found;
1241 else if ( (CC->user.axlevel < 6)
1242 && (CC->user.usernum != qrbuf.QRroomaide)
1243 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) ) {
1244 ret = crr_access_denied;
1247 else if (is_noneditable(&qrbuf)) {
1248 ret = crr_noneditable;
1253 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1254 if (qrbuf.QRflags & QR_MAILBOX) {
1255 owner = atol(qrbuf.QRname);
1257 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1258 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1259 "%010ld.%s", owner, new_name);
1262 safestrncpy(qrbuf.QRname, new_name,
1263 sizeof(qrbuf.QRname));
1266 /* Reject change of floor for baseroom/aideroom */
1267 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1268 !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1272 /* Take care of floor stuff */
1273 old_floor = qrbuf.QRfloor;
1274 if (new_floor < 0) {
1275 new_floor = old_floor;
1277 qrbuf.QRfloor = new_floor;
1280 begin_critical_section(S_CONFIG);
1282 /* If baseroom/aideroom name changes, update config */
1283 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1284 safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1287 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1288 safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1292 end_critical_section(S_CONFIG);
1294 /* If the room name changed, then there are now two room
1295 * records, so we have to delete the old one.
1297 if (strcasecmp(new_name, old_name)) {
1298 b_deleteroom(actual_old_name);
1304 end_critical_section(S_ROOMS);
1306 /* Adjust the floor reference counts if necessary */
1307 if (new_floor != old_floor) {
1308 lgetfloor(&flbuf, old_floor);
1309 --flbuf.f_ref_count;
1310 lputfloor(&flbuf, old_floor);
1311 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1312 lgetfloor(&flbuf, new_floor);
1313 ++flbuf.f_ref_count;
1314 lputfloor(&flbuf, new_floor);
1315 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1318 /* ...and everybody say "YATTA!" */
1324 * set room parameters (aide or room aide command)
1326 void cmd_setr(char *args)
1332 char new_name[ROOMNAMELEN];
1334 if (CtdlAccessCheck(ac_logged_in)) return;
1336 if (num_parms(args) >= 6) {
1337 new_floor = extract_int(args, 5);
1339 new_floor = (-1); /* don't change the floor */
1342 /* When is a new name more than just a new name? When the old name
1343 * has a namespace prefix.
1345 if (CC->room.QRflags & QR_MAILBOX) {
1346 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
1348 safestrncpy(new_name, "", sizeof new_name);
1350 extract_token(&new_name[strlen(new_name)], args, 0, '|', (sizeof new_name - strlen(new_name)));
1352 r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
1354 if (r == crr_room_not_found) {
1355 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
1356 } else if (r == crr_already_exists) {
1357 cprintf("%d '%s' already exists.\n",
1358 ERROR + ALREADY_EXISTS, new_name);
1359 } else if (r == crr_noneditable) {
1360 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
1361 } else if (r == crr_invalid_floor) {
1362 cprintf("%d Target floor does not exist.\n",
1363 ERROR + INVALID_FLOOR_OPERATION);
1364 } else if (r == crr_access_denied) {
1365 cprintf("%d You do not have permission to edit '%s'\n",
1366 ERROR + HIGHER_ACCESS_REQUIRED,
1368 } else if (r != crr_ok) {
1369 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1370 ERROR + INTERNAL_ERROR, r);
1377 getroom(&CC->room, new_name);
1379 /* Now we have to do a bunch of other stuff */
1381 if (num_parms(args) >= 7) {
1382 new_order = extract_int(args, 6);
1385 if (new_order > 127)
1389 lgetroom(&CC->room, CC->room.QRname);
1391 /* Directory room */
1392 extract_token(buf, args, 2, '|', sizeof buf);
1394 safestrncpy(CC->room.QRdirname, buf,
1395 sizeof CC->room.QRdirname);
1398 if (num_parms(args) >= 8) {
1399 CC->room.QRdefaultview = extract_int(args, 7);
1402 /* Second set of flags */
1403 if (num_parms(args) >= 9) {
1404 CC->room.QRflags2 = extract_int(args, 8);
1408 CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
1409 /* Clean up a client boo-boo: if the client set the room to
1410 * guess-name or passworded, ensure that the private flag is
1413 if ((CC->room.QRflags & QR_GUESSNAME)
1414 || (CC->room.QRflags & QR_PASSWORDED))
1415 CC->room.QRflags |= QR_PRIVATE;
1417 /* Some changes can't apply to BASEROOM */
1418 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1420 CC->room.QRorder = 0;
1421 CC->room.QRpasswd[0] = '\0';
1422 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1423 QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1424 CC->room.QRflags |= QR_PERMANENT;
1426 /* March order (doesn't apply to AIDEROOM) */
1427 if (num_parms(args) >= 7)
1428 CC->room.QRorder = (char) new_order;
1430 extract_token(buf, args, 1, '|', sizeof buf);
1432 safestrncpy(CC->room.QRpasswd, buf,
1433 sizeof CC->room.QRpasswd);
1434 /* Kick everyone out if the client requested it
1435 * (by changing the room's generation number)
1437 if (extract_int(args, 4)) {
1438 time(&CC->room.QRgen);
1441 /* Some changes can't apply to AIDEROOM */
1442 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1444 CC->room.QRorder = 0;
1445 CC->room.QRflags &= ~QR_MAILBOX;
1446 CC->room.QRflags |= QR_PERMANENT;
1449 /* Write the room record back to disk */
1450 lputroom(&CC->room);
1452 /* Create a room directory if necessary */
1453 if (CC->room.QRflags & QR_DIRECTORY) {
1454 snprintf(buf, sizeof buf,
1455 #ifndef HAVE_DATA_DIR
1460 "/files/%s", CC->room.QRdirname);
1463 snprintf(buf, sizeof buf, "The room \"%s\" has been edited by %s.\n",
1464 CC->room.QRname, CC->curr_user);
1466 cprintf("%d Ok\n", CIT_OK);
1472 * get the name of the room aide for this room
1476 struct ctdluser usbuf;
1478 if (CtdlAccessCheck(ac_logged_in)) return;
1480 if (getuserbynumber(&usbuf, CC->room.QRroomaide) == 0) {
1481 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1483 cprintf("%d \n", CIT_OK);
1489 * set the room aide for this room
1491 void cmd_seta(char *new_ra)
1493 struct ctdluser usbuf;
1498 if (CtdlAccessCheck(ac_room_aide)) return;
1500 if (getuser(&usbuf, new_ra) != 0) {
1503 newu = usbuf.usernum;
1506 lgetroom(&CC->room, CC->room.QRname);
1508 if (CC->room.QRroomaide != newu) {
1511 CC->room.QRroomaide = newu;
1512 lputroom(&CC->room);
1515 * We have to post the change notice _after_ writing changes to
1516 * the room table, otherwise it would deadlock!
1518 if (post_notice == 1) {
1519 if (strlen(usbuf.fullname) > 0)
1520 snprintf(buf, sizeof buf,
1521 "%s is now the room aide for \"%s\".\n",
1522 usbuf.fullname, CC->room.QRname);
1524 snprintf(buf, sizeof buf,
1525 "There is now no room aide for \"%s\".\n",
1529 cprintf("%d Ok\n", CIT_OK);
1533 * Generate an associated file name for a room
1535 void assoc_file_name(char *buf, size_t n,
1536 struct ctdlroom *qrbuf, const char *prefix)
1538 snprintf(buf, n, "./%s/%ld", prefix, qrbuf->QRnumber);
1542 * retrieve info file for this room
1550 assoc_file_name(filename, sizeof filename, &CC->room, "info");
1551 info_fp = fopen(filename, "r");
1553 if (info_fp == NULL) {
1554 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
1557 cprintf("%d Info:\n", LISTING_FOLLOWS);
1558 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1559 if (strlen(buf) > 0)
1560 buf[strlen(buf) - 1] = 0;
1561 cprintf("%s\n", buf);
1568 * Asynchronously schedule a room for deletion. The room will appear
1569 * deleted to the user(s), but it won't actually get purged from the
1570 * database until THE DREADED AUTO-PURGER makes its next run.
1572 void schedule_room_for_deletion(struct ctdlroom *qrbuf)
1574 char old_name[ROOMNAMELEN];
1577 lprintf(CTDL_NOTICE, "Scheduling room <%s> for deletion\n",
1580 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1582 getroom(qrbuf, qrbuf->QRname);
1584 /* Turn the room into a private mailbox owned by a user who doesn't
1585 * exist. This will immediately make the room invisible to everyone,
1586 * and qualify the room for purging.
1588 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1593 qrbuf->QRflags |= QR_MAILBOX;
1594 time(&qrbuf->QRgen); /* Use a timestamp as the new generation number */
1598 b_deleteroom(old_name);
1604 * Back end processing to delete a room and everything associated with it
1605 * (This one is synchronous and should only get called by THE DREADED
1606 * AUTO-PURGER in serv_expire.c. All user-facing code should call
1607 * the asynchronous schedule_room_for_deletion() instead.)
1609 void delete_room(struct ctdlroom *qrbuf)
1614 lprintf(CTDL_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1616 /* Delete the info file */
1617 assoc_file_name(filename, sizeof filename, qrbuf, "info");
1620 /* Delete the image file */
1621 assoc_file_name(filename, sizeof filename, qrbuf, "images");
1624 /* Delete the room's network config file */
1625 assoc_file_name(filename, sizeof filename, qrbuf, "netconfigs");
1628 /* Delete the messages in the room
1629 * (Careful: this opens an S_ROOMS critical section!)
1631 CtdlDeleteMessages(qrbuf->QRname, 0L, "", 0);
1633 /* Flag the room record as not in use */
1634 lgetroom(qrbuf, qrbuf->QRname);
1638 /* then decrement the reference count for the floor */
1639 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1640 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1641 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1643 /* Delete the room record from the database! */
1644 b_deleteroom(qrbuf->QRname);
1650 * Check access control for deleting a room
1652 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1654 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1658 if (is_noneditable(qr)) {
1663 * For mailboxes, check stuff
1665 if (qr->QRflags & QR_MAILBOX) {
1667 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1669 if (atol(qr->QRname) != CC->user.usernum) {
1670 return(0); /* not my room */
1673 /* Can't delete your Mail> room */
1674 if (!strcasecmp(&qr->QRname[12], MAILROOM)) return(0);
1676 /* Otherwise it's ok */
1681 * For normal rooms, just check for aide or room aide status.
1683 return(is_room_aide());
1687 * aide command: kill the current room
1689 void cmd_kill(char *argbuf)
1691 char deleted_room_name[ROOMNAMELEN];
1695 kill_ok = extract_int(argbuf, 0);
1697 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
1698 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1702 if (CC->room.QRflags & QR_MAILBOX) {
1703 safestrncpy(deleted_room_name, &CC->room.QRname[11], sizeof deleted_room_name);
1706 safestrncpy(deleted_room_name, CC->room.QRname, sizeof deleted_room_name);
1709 /* Do the dirty work */
1710 schedule_room_for_deletion(&CC->room);
1712 /* Return to the Lobby */
1713 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1715 /* tell the world what we did */
1716 snprintf(msg, sizeof msg, "The room \"%s\" has been deleted by %s.\n",
1717 deleted_room_name, CC->curr_user);
1719 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1721 cprintf("%d ok to delete.\n", CIT_OK);
1727 * Internal code to create a new room (returns room flags)
1729 * Room types: 0=public, 1=guessname, 2=passworded, 3=inv-only,
1730 * 4=mailbox, 5=mailbox, but caller supplies namespace
1732 unsigned create_room(char *new_room_name,
1734 char *new_room_pass,
1741 struct ctdlroom qrbuf;
1745 lprintf(CTDL_DEBUG, "create_room(name=%s, type=%d, view=%d)\n",
1746 new_room_name, new_room_type, new_room_view);
1748 if (getroom(&qrbuf, new_room_name) == 0) {
1749 lprintf(CTDL_DEBUG, "%s already exists.\n", new_room_name);
1753 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1754 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1755 qrbuf.QRflags = QR_INUSE;
1756 if (new_room_type > 0)
1757 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1758 if (new_room_type == 1)
1759 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1760 if (new_room_type == 2)
1761 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1762 if ( (new_room_type == 4) || (new_room_type == 5) )
1763 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1765 /* If the user is requesting a personal room, set up the room
1766 * name accordingly (prepend the user number)
1768 if (new_room_type == 4) {
1769 MailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1772 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1775 /* If the room is private, and the system administrator has elected
1776 * to automatically grant room aide privileges, do so now; otherwise,
1777 * set the room aide to undefined.
1779 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1780 qrbuf.QRroomaide = CC->user.usernum;
1782 qrbuf.QRroomaide = (-1L);
1786 * If the caller is only interested in testing whether this will work,
1787 * return now without creating the room.
1789 if (!really_create) return (qrbuf.QRflags);
1791 qrbuf.QRnumber = get_new_room_number();
1792 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1793 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1794 qrbuf.QRfloor = new_room_floor;
1795 qrbuf.QRdefaultview = new_room_view;
1797 /* save what we just did... */
1800 /* bump the reference count on whatever floor the room is on */
1801 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1802 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1803 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1805 /* Grant the creator access to the room unless the avoid_access
1806 * parameter was specified.
1808 if ( (CC->logged_in) && (avoid_access == 0) ) {
1809 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1810 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1811 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1812 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1815 /* resume our happy day */
1816 return (qrbuf.QRflags);
1823 void cmd_cre8(char *args)
1826 char new_room_name[ROOMNAMELEN];
1828 char new_room_pass[32];
1831 char *notification_message = NULL;
1834 int avoid_access = 0;
1836 cre8_ok = extract_int(args, 0);
1837 extract_token(new_room_name, args, 1, '|', sizeof new_room_name);
1838 new_room_name[ROOMNAMELEN - 1] = 0;
1839 new_room_type = extract_int(args, 2);
1840 extract_token(new_room_pass, args, 3, '|', sizeof new_room_pass);
1841 avoid_access = extract_int(args, 5);
1842 new_room_view = extract_int(args, 6);
1843 new_room_pass[9] = 0;
1846 if ((strlen(new_room_name) == 0) && (cre8_ok == 1)) {
1847 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
1851 if (!strcasecmp(new_room_name, MAILROOM)) {
1852 cprintf("%d '%s' already exists.\n",
1853 ERROR + ALREADY_EXISTS, new_room_name);
1857 if (num_parms(args) >= 5) {
1858 fl = cgetfloor(extract_int(args, 4));
1860 cprintf("%d Invalid floor number.\n",
1861 ERROR + INVALID_FLOOR_OPERATION);
1864 else if ((fl->f_flags & F_INUSE) == 0) {
1865 cprintf("%d Invalid floor number.\n",
1866 ERROR + INVALID_FLOOR_OPERATION);
1869 new_room_floor = extract_int(args, 4);
1873 if (CtdlAccessCheck(ac_logged_in)) return;
1875 if (CC->user.axlevel < config.c_createax) {
1876 cprintf("%d You need higher access to create rooms.\n",
1877 ERROR + HIGHER_ACCESS_REQUIRED);
1881 if ((strlen(new_room_name) == 0) && (cre8_ok == 0)) {
1882 cprintf("%d Ok to create rooms.\n", CIT_OK);
1886 if ((new_room_type < 0) || (new_room_type > 5)) {
1887 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
1891 if (new_room_type == 5) {
1892 if (CC->user.axlevel < 6) {
1893 cprintf("%d Higher access required\n",
1894 ERROR + HIGHER_ACCESS_REQUIRED);
1899 /* Check to make sure the requested room name doesn't already exist */
1900 newflags = create_room(new_room_name,
1901 new_room_type, new_room_pass, new_room_floor,
1902 0, avoid_access, new_room_view);
1903 if (newflags == 0) {
1904 cprintf("%d '%s' already exists.\n",
1905 ERROR + ALREADY_EXISTS, new_room_name);
1910 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
1914 /* If we reach this point, the room needs to be created. */
1916 newflags = create_room(new_room_name,
1917 new_room_type, new_room_pass, new_room_floor, 1, 0,
1920 /* post a message in Aide> describing the new room */
1921 notification_message = malloc(1024);
1922 snprintf(notification_message, 1024,
1923 "A new room called \"%s\" has been created by %s%s%s%s%s%s\n",
1926 ((newflags & QR_MAILBOX) ? " [personal]" : ""),
1927 ((newflags & QR_PRIVATE) ? " [private]" : ""),
1928 ((newflags & QR_GUESSNAME) ? " [hidden]" : ""),
1929 ((newflags & QR_PASSWORDED) ? " Password: " : ""),
1930 ((newflags & QR_PASSWORDED) ? new_room_pass : "")
1932 aide_message(notification_message);
1933 free(notification_message);
1935 cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
1940 void cmd_einf(char *ok)
1941 { /* enter info file for current room */
1943 char infofilename[SIZ];
1948 if (CtdlAccessCheck(ac_room_aide)) return;
1950 if (atoi(ok) == 0) {
1951 cprintf("%d Ok.\n", CIT_OK);
1954 assoc_file_name(infofilename, sizeof infofilename, &CC->room, "info");
1955 lprintf(CTDL_DEBUG, "opening\n");
1956 fp = fopen(infofilename, "w");
1957 lprintf(CTDL_DEBUG, "checking\n");
1959 cprintf("%d Cannot open %s: %s\n",
1960 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1963 cprintf("%d Send info...\n", SEND_LISTING);
1966 client_getln(buf, sizeof buf);
1967 if (strcmp(buf, "000"))
1968 fprintf(fp, "%s\n", buf);
1969 } while (strcmp(buf, "000"));
1972 /* now update the room index so people will see our new info */
1973 lgetroom(&CC->room, CC->room.QRname); /* lock so no one steps on us */
1974 CC->room.QRinfo = CC->room.QRhighest + 1L;
1975 lputroom(&CC->room);
1980 * cmd_lflr() - List all known floors
1987 if (CtdlAccessCheck(ac_logged_in)) return;
1989 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1991 for (a = 0; a < MAXFLOORS; ++a) {
1992 getfloor(&flbuf, a);
1993 if (flbuf.f_flags & F_INUSE) {
1994 cprintf("%d|%s|%d\n",
2006 * create a new floor
2008 void cmd_cflr(char *argbuf)
2010 char new_floor_name[256];
2013 int free_slot = (-1);
2016 extract_token(new_floor_name, argbuf, 0, '|', sizeof new_floor_name);
2017 cflr_ok = extract_int(argbuf, 1);
2019 if (CtdlAccessCheck(ac_aide)) return;
2021 if (strlen(new_floor_name) == 0) {
2022 cprintf("%d Blank floor name not allowed.\n",
2023 ERROR + ILLEGAL_VALUE);
2027 for (a = 0; a < MAXFLOORS; ++a) {
2028 getfloor(&flbuf, a);
2030 /* note any free slots while we're scanning... */
2031 if (((flbuf.f_flags & F_INUSE) == 0)
2035 /* check to see if it already exists */
2036 if ((!strcasecmp(flbuf.f_name, new_floor_name))
2037 && (flbuf.f_flags & F_INUSE)) {
2038 cprintf("%d Floor '%s' already exists.\n",
2039 ERROR + ALREADY_EXISTS,
2045 if (free_slot < 0) {
2046 cprintf("%d There is no space available for a new floor.\n",
2047 ERROR + INVALID_FLOOR_OPERATION);
2051 cprintf("%d ok to create...\n", CIT_OK);
2054 lgetfloor(&flbuf, free_slot);
2055 flbuf.f_flags = F_INUSE;
2056 flbuf.f_ref_count = 0;
2057 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
2058 lputfloor(&flbuf, free_slot);
2059 cprintf("%d %d\n", CIT_OK, free_slot);
2067 void cmd_kflr(char *argbuf)
2070 int floor_to_delete;
2074 floor_to_delete = extract_int(argbuf, 0);
2075 kflr_ok = extract_int(argbuf, 1);
2077 if (CtdlAccessCheck(ac_aide)) return;
2079 lgetfloor(&flbuf, floor_to_delete);
2082 if ((flbuf.f_flags & F_INUSE) == 0) {
2083 cprintf("%d Floor %d not in use.\n",
2084 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
2087 if (flbuf.f_ref_count != 0) {
2088 cprintf("%d Cannot delete; floor contains %d rooms.\n",
2089 ERROR + INVALID_FLOOR_OPERATION,
2094 cprintf("%d Ok\n", CIT_OK);
2096 cprintf("%d Ok to delete...\n", CIT_OK);
2103 if ((delete_ok == 1) && (kflr_ok == 1))
2105 lputfloor(&flbuf, floor_to_delete);
2111 void cmd_eflr(char *argbuf)
2117 np = num_parms(argbuf);
2119 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
2123 if (CtdlAccessCheck(ac_aide)) return;
2125 floor_num = extract_int(argbuf, 0);
2126 lgetfloor(&flbuf, floor_num);
2127 if ((flbuf.f_flags & F_INUSE) == 0) {
2128 lputfloor(&flbuf, floor_num);
2129 cprintf("%d Floor %d is not in use.\n",
2130 ERROR + INVALID_FLOOR_OPERATION, floor_num);
2134 extract_token(flbuf.f_name, argbuf, 1, '|', sizeof flbuf.f_name);
2135 lputfloor(&flbuf, floor_num);
2137 cprintf("%d Ok\n", CIT_OK);