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);
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;
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 generation number matchups */
107 if (roombuf->QRflags & QR_MAILBOX) {
108 if (userbuf->usernum == atol(roombuf->QRname)) {
109 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
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;
117 /* Check to see if the user has forgotten this room */
118 if (vbuf.v_flags & V_FORGET) {
119 retval = retval & ~UA_KNOWN;
120 if ( ( ((roombuf->QRflags & QR_PRIVATE) == 0)
121 && ((roombuf->QRflags & QR_MAILBOX) == 0) )
122 || ( (roombuf->QRflags & QR_MAILBOX)
123 && (atol(roombuf->QRname) == CC->user.usernum))) {
124 retval = retval | UA_ZAPPED;
127 /* If user is explicitly locked out of this room, deny everything */
128 if (vbuf.v_flags & V_LOCKOUT) {
129 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
132 /* Aides get access to all private rooms */
133 if ( (userbuf->axlevel >= 6)
134 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
135 if (vbuf.v_flags & V_FORGET) {
136 retval = retval | UA_GOTOALLOWED;
139 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
143 /* Aides can gain access to mailboxes as well, but they don't show
146 if ( (userbuf->axlevel >= 6)
147 && (roombuf->QRflags & QR_MAILBOX) ) {
148 retval = retval | UA_GOTOALLOWED;
151 NEWMSG: /* By the way, we also check for the presence of new messages */
152 if (is_msg_in_sequence_set(vbuf.v_seen, roombuf->QRhighest) == 0) {
153 retval = retval | UA_HASNEWMSGS;
156 /* System rooms never show up in the list. */
157 if (roombuf->QRflags2 & QR2_SYSTEM) {
158 retval = retval & ~UA_KNOWN;
162 /* Now give the caller the information it wants. */
163 if (result != NULL) *result = retval;
164 if (view != NULL) *view = vbuf.v_view;
168 * Self-checking stuff for a room record read into memory
170 void room_sanity_check(struct ctdlroom *qrbuf)
172 /* Mailbox rooms are always on the lowest floor */
173 if (qrbuf->QRflags & QR_MAILBOX) {
176 /* Listing order of 0 is illegal except for base rooms */
177 if (qrbuf->QRorder == 0)
178 if (!(qrbuf->QRflags & QR_MAILBOX) &&
179 strncasecmp(qrbuf->QRname, config.c_baseroom, ROOMNAMELEN)
181 strncasecmp(qrbuf->QRname, config.c_aideroom, ROOMNAMELEN))
187 * getroom() - retrieve room data from disk
189 int getroom(struct ctdlroom *qrbuf, char *room_name)
191 struct cdbdata *cdbqr;
192 char lowercase_name[ROOMNAMELEN];
193 char personal_lowercase_name[ROOMNAMELEN];
196 for (a = 0; room_name[a] && a < sizeof lowercase_name - 1; ++a) {
197 lowercase_name[a] = tolower(room_name[a]);
199 lowercase_name[a] = 0;
201 memset(qrbuf, 0, sizeof(struct ctdlroom));
203 /* First, try the public namespace */
204 cdbqr = cdb_fetch(CDB_ROOMS,
205 lowercase_name, strlen(lowercase_name));
207 /* If that didn't work, try the user's personal namespace */
209 snprintf(personal_lowercase_name,
210 sizeof personal_lowercase_name, "%010ld.%s",
211 CC->user.usernum, lowercase_name);
212 cdbqr = cdb_fetch(CDB_ROOMS,
213 personal_lowercase_name,
214 strlen(personal_lowercase_name));
217 memcpy(qrbuf, cdbqr->ptr,
218 ((cdbqr->len > sizeof(struct ctdlroom)) ?
219 sizeof(struct ctdlroom) : cdbqr->len));
222 room_sanity_check(qrbuf);
231 * lgetroom() - same as getroom() but locks the record (if supported)
233 int lgetroom(struct ctdlroom *qrbuf, char *room_name)
236 retval = getroom(qrbuf, room_name);
237 if (retval == 0) begin_critical_section(S_ROOMS);
243 * b_putroom() - back end to putroom() and b_deleteroom()
244 * (if the supplied buffer is NULL, delete the room record)
246 void b_putroom(struct ctdlroom *qrbuf, char *room_name)
248 char lowercase_name[ROOMNAMELEN];
251 for (a = 0; a <= strlen(room_name); ++a) {
252 lowercase_name[a] = tolower(room_name[a]);
256 cdb_delete(CDB_ROOMS,
257 lowercase_name, strlen(lowercase_name));
259 time(&qrbuf->QRmtime);
261 lowercase_name, strlen(lowercase_name),
262 qrbuf, sizeof(struct ctdlroom));
268 * putroom() - store room data to disk
270 void putroom(struct ctdlroom *qrbuf) {
271 b_putroom(qrbuf, qrbuf->QRname);
276 * b_deleteroom() - delete a room record from disk
278 void b_deleteroom(char *room_name) {
279 b_putroom(NULL, room_name);
285 * lputroom() - same as putroom() but unlocks the record (if supported)
287 void lputroom(struct ctdlroom *qrbuf)
291 end_critical_section(S_ROOMS);
295 /****************************************************************************/
298 * getfloor() - retrieve floor data from disk
300 void getfloor(struct floor *flbuf, int floor_num)
302 struct cdbdata *cdbfl;
304 memset(flbuf, 0, sizeof(struct floor));
305 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
307 memcpy(flbuf, cdbfl->ptr,
308 ((cdbfl->len > sizeof(struct floor)) ?
309 sizeof(struct floor) : cdbfl->len));
312 if (floor_num == 0) {
313 safestrncpy(flbuf->f_name, "Main Floor",
314 sizeof flbuf->f_name);
315 flbuf->f_flags = F_INUSE;
316 flbuf->f_ref_count = 3;
323 * lgetfloor() - same as getfloor() but locks the record (if supported)
325 void lgetfloor(struct floor *flbuf, int floor_num)
328 begin_critical_section(S_FLOORTAB);
329 getfloor(flbuf, floor_num);
334 * cgetfloor() - Get floor record from *cache* (loads from disk if needed)
336 * This is strictly a performance hack.
338 struct floor *cgetfloor(int floor_num) {
339 static int initialized = 0;
342 struct floor *fl = NULL;
344 begin_critical_section(S_FLOORCACHE);
345 if (initialized == 0) {
346 for (i=0; i<MAXFLOORS; ++i) {
347 floorcache[floor_num] = NULL;
351 if (floorcache[floor_num] == NULL) {
354 end_critical_section(S_FLOORCACHE);
357 fl = malloc(sizeof(struct floor));
358 getfloor(fl, floor_num);
359 begin_critical_section(S_FLOORCACHE);
360 if (floorcache[floor_num] != NULL) {
361 free(floorcache[floor_num]);
363 floorcache[floor_num] = fl;
364 end_critical_section(S_FLOORCACHE);
367 return(floorcache[floor_num]);
373 * putfloor() - store floor data on disk
375 void putfloor(struct floor *flbuf, int floor_num)
377 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
378 begin_critical_section(S_FLOORCACHE);
379 if (floorcache[floor_num] != NULL) {
380 free(floorcache[floor_num]);
381 floorcache[floor_num] = malloc(sizeof(struct floor));
382 memcpy(floorcache[floor_num], flbuf, sizeof(struct floor));
384 end_critical_section(S_FLOORCACHE);
386 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
387 flbuf, sizeof(struct floor));
392 * lputfloor() - same as putfloor() but unlocks the record (if supported)
394 void lputfloor(struct floor *flbuf, int floor_num)
397 putfloor(flbuf, floor_num);
398 end_critical_section(S_FLOORTAB);
404 * Traverse the room file...
406 void ForEachRoom(void (*CallBack) (struct ctdlroom *EachRoom, void *out_data),
409 struct ctdlroom qrbuf;
410 struct cdbdata *cdbqr;
412 cdb_rewind(CDB_ROOMS);
414 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
415 memset(&qrbuf, 0, sizeof(struct ctdlroom));
416 memcpy(&qrbuf, cdbqr->ptr,
417 ((cdbqr->len > sizeof(struct ctdlroom)) ?
418 sizeof(struct ctdlroom) : cdbqr->len));
420 room_sanity_check(&qrbuf);
421 if (qrbuf.QRflags & QR_INUSE)
422 (*CallBack)(&qrbuf, in_data);
428 * delete_msglist() - delete room message pointers
430 void delete_msglist(struct ctdlroom *whichroom)
432 struct cdbdata *cdbml;
434 /* Make sure the msglist we're deleting actually exists, otherwise
435 * gdbm will complain when we try to delete an invalid record
437 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
441 /* Go ahead and delete it */
442 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
450 * sort message pointers
451 * (returns new msg count)
453 int sort_msglist(long listptrs[], int oldcount)
464 for (a = numitems - 2; a >= 0; --a) {
465 for (b = 0; b <= a; ++b) {
466 if (listptrs[b] > (listptrs[b + 1])) {
468 hold2 = listptrs[b + 1];
470 listptrs[b + 1] = hold1;
475 /* and yank any nulls */
476 while ((numitems > 0) && (listptrs[0] == 0L)) {
477 memcpy(&listptrs[0], &listptrs[1],
478 (sizeof(long) * (numitems - 1)));
487 * Determine whether a given room is non-editable.
489 int is_noneditable(struct ctdlroom *qrbuf)
492 /* Mail> rooms are non-editable */
493 if ( (qrbuf->QRflags & QR_MAILBOX)
494 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
497 /* Everything else is editable */
504 * Back-back-end for all room listing commands
506 void list_roomname(struct ctdlroom *qrbuf, int ra, int view)
508 char truncated_roomname[ROOMNAMELEN];
510 /* For my own mailbox rooms, chop off the owner prefix */
511 if ( (qrbuf->QRflags & QR_MAILBOX)
512 && (atol(qrbuf->QRname) == CC->user.usernum) ) {
513 safestrncpy(truncated_roomname, qrbuf->QRname, sizeof truncated_roomname);
514 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
515 cprintf("%s", truncated_roomname);
517 /* For all other rooms, just display the name in its entirety */
519 cprintf("%s", qrbuf->QRname);
522 /* ...and now the other parameters */
523 cprintf("|%u|%d|%d|%d|%d|%d|\n",
525 (int) qrbuf->QRfloor,
526 (int) qrbuf->QRorder,
527 (int) qrbuf->QRflags2,
535 * cmd_lrms() - List all accessible rooms, known or forgotten
537 void cmd_lrms_backend(struct ctdlroom *qrbuf, void *data)
539 int FloorBeingSearched = (-1);
543 FloorBeingSearched = *(int *)data;
544 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
546 if ((( ra & (UA_KNOWN | UA_ZAPPED)))
547 && ((qrbuf->QRfloor == (FloorBeingSearched))
548 || ((FloorBeingSearched) < 0)))
549 list_roomname(qrbuf, ra, view);
552 void cmd_lrms(char *argbuf)
554 int FloorBeingSearched = (-1);
555 if (strlen(argbuf) > 0)
556 FloorBeingSearched = extract_int(argbuf, 0);
558 if (CtdlAccessCheck(ac_logged_in)) return;
560 if (getuser(&CC->user, CC->curr_user)) {
561 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
564 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
566 ForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
573 * cmd_lkra() - List all known rooms
575 void cmd_lkra_backend(struct ctdlroom *qrbuf, void *data)
577 int FloorBeingSearched = (-1);
581 FloorBeingSearched = *(int *)data;
582 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
584 if ((( ra & (UA_KNOWN)))
585 && ((qrbuf->QRfloor == (FloorBeingSearched))
586 || ((FloorBeingSearched) < 0)))
587 list_roomname(qrbuf, ra, view);
590 void cmd_lkra(char *argbuf)
592 int FloorBeingSearched = (-1);
593 if (strlen(argbuf) > 0)
594 FloorBeingSearched = extract_int(argbuf, 0);
596 if (CtdlAccessCheck(ac_logged_in)) return;
598 if (getuser(&CC->user, CC->curr_user)) {
599 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
602 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
604 ForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
610 void cmd_lprm_backend(struct ctdlroom *qrbuf, void *data)
612 int FloorBeingSearched = (-1);
616 FloorBeingSearched = *(int *)data;
617 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
619 if ( ((qrbuf->QRflags & QR_PRIVATE) == 0)
620 && ((qrbuf->QRflags & QR_MAILBOX) == 0)
621 && ((qrbuf->QRfloor == (FloorBeingSearched))
622 || ((FloorBeingSearched) < 0)))
623 list_roomname(qrbuf, ra, view);
626 void cmd_lprm(char *argbuf)
628 int FloorBeingSearched = (-1);
629 if (strlen(argbuf) > 0)
630 FloorBeingSearched = extract_int(argbuf, 0);
632 cprintf("%d Publiic rooms:\n", LISTING_FOLLOWS);
634 ForEachRoom(cmd_lprm_backend, &FloorBeingSearched);
641 * cmd_lkrn() - List all known rooms with new messages
643 void cmd_lkrn_backend(struct ctdlroom *qrbuf, void *data)
645 int FloorBeingSearched = (-1);
649 FloorBeingSearched = *(int *)data;
650 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
653 && (ra & UA_HASNEWMSGS)
654 && ((qrbuf->QRfloor == (FloorBeingSearched))
655 || ((FloorBeingSearched) < 0)))
656 list_roomname(qrbuf, ra, view);
659 void cmd_lkrn(char *argbuf)
661 int FloorBeingSearched = (-1);
662 if (strlen(argbuf) > 0)
663 FloorBeingSearched = extract_int(argbuf, 0);
665 if (CtdlAccessCheck(ac_logged_in)) return;
667 if (getuser(&CC->user, CC->curr_user)) {
668 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
671 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
673 ForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
680 * cmd_lkro() - List all known rooms
682 void cmd_lkro_backend(struct ctdlroom *qrbuf, void *data)
684 int FloorBeingSearched = (-1);
688 FloorBeingSearched = *(int *)data;
689 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
692 && ((ra & UA_HASNEWMSGS) == 0)
693 && ((qrbuf->QRfloor == (FloorBeingSearched))
694 || ((FloorBeingSearched) < 0)))
695 list_roomname(qrbuf, ra, view);
698 void cmd_lkro(char *argbuf)
700 int FloorBeingSearched = (-1);
701 if (strlen(argbuf) > 0)
702 FloorBeingSearched = extract_int(argbuf, 0);
704 if (CtdlAccessCheck(ac_logged_in)) return;
706 if (getuser(&CC->user, CC->curr_user)) {
707 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
710 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
712 ForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
719 * cmd_lzrm() - List all forgotten rooms
721 void cmd_lzrm_backend(struct ctdlroom *qrbuf, void *data)
723 int FloorBeingSearched = (-1);
727 FloorBeingSearched = *(int *)data;
728 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
730 if ((ra & UA_GOTOALLOWED)
732 && ((qrbuf->QRfloor == (FloorBeingSearched))
733 || ((FloorBeingSearched) < 0)))
734 list_roomname(qrbuf, ra, view);
737 void cmd_lzrm(char *argbuf)
739 int FloorBeingSearched = (-1);
740 if (strlen(argbuf) > 0)
741 FloorBeingSearched = extract_int(argbuf, 0);
743 if (CtdlAccessCheck(ac_logged_in)) return;
745 if (getuser(&CC->user, CC->curr_user)) {
746 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
749 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
751 ForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
757 * Make the specified room the current room for this session. No validation
758 * or access control is done here -- the caller should make sure that the
759 * specified room exists and is ok to access.
761 void usergoto(char *where, int display_result, int transiently,
762 int *retmsgs, int *retnew)
765 int new_messages = 0;
766 int old_messages = 0;
767 int total_messages = 0;
771 int newmailcount = 0;
773 char truncated_roomname[ROOMNAMELEN];
774 struct cdbdata *cdbfr;
775 long *msglist = NULL;
777 unsigned int original_v_flags;
780 char setstr[128], lostr[64], histr[64];
784 /* If the supplied room name is NULL, the caller wants us to know that
785 * it has already copied the room record into CC->room, so
786 * we can skip the extra database fetch.
789 safestrncpy(CC->room.QRname, where, sizeof CC->room.QRname);
790 getroom(&CC->room, where);
793 /* Take care of all the formalities. */
795 begin_critical_section(S_USERS);
796 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
797 original_v_flags = vbuf.v_flags;
799 /* Know the room ... but not if it's the page log room, or if the
800 * caller specified that we're only entering this room transiently.
802 if ((strcasecmp(CC->room.QRname, config.c_logpages))
803 && (transiently == 0) ) {
804 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
805 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
808 /* Only rewrite the database record if we changed something */
809 if (vbuf.v_flags != original_v_flags) {
810 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
812 end_critical_section(S_USERS);
814 /* Check for new mail */
815 newmailcount = NewMailCount();
817 /* set info to 1 if the user needs to read the room's info file */
818 if (CC->room.QRinfo > vbuf.v_lastseen) {
823 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
825 msglist = (long *) cdbfr->ptr;
826 cdbfr->ptr = NULL; /* usergoto() now owns this memory */
827 num_msgs = cdbfr->len / sizeof(long);
832 for (a=0; a<num_msgs; ++a) {
833 if (msglist[a] > 0L) ++total_messages;
835 new_messages = num_msgs;
836 num_sets = num_tokens(vbuf.v_seen, ',');
837 for (s=0; s<num_sets; ++s) {
838 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
840 extract_token(lostr, setstr, 0, ':', sizeof lostr);
841 if (num_tokens(setstr, ':') >= 2) {
842 extract_token(histr, setstr, 1, ':', sizeof histr);
843 if (!strcmp(histr, "*")) {
844 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
848 strcpy(histr, lostr);
853 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
854 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
860 new_messages = total_messages - old_messages;
862 if (msglist != NULL) free(msglist);
864 if (CC->room.QRflags & QR_MAILBOX)
869 if ((CC->room.QRroomaide == CC->user.usernum)
870 || (CC->user.axlevel >= 6))
875 safestrncpy(truncated_roomname, CC->room.QRname, sizeof truncated_roomname);
876 if ( (CC->room.QRflags & QR_MAILBOX)
877 && (atol(CC->room.QRname) == CC->user.usernum) ) {
878 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
881 if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
885 if (retmsgs != NULL) *retmsgs = total_messages;
886 if (retnew != NULL) *retnew = new_messages;
887 lprintf(CTDL_DEBUG, "<%s> %d new of %d total messages\n",
889 new_messages, total_messages
892 CC->curr_view = (int)vbuf.v_view;
894 if (display_result) {
895 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|\n",
896 CIT_OK, CtdlCheckExpress(),
901 (int)CC->room.QRflags,
902 (long)CC->room.QRhighest,
903 (long)vbuf.v_lastseen,
907 (int)CC->room.QRfloor,
909 (int)CC->room.QRdefaultview,
917 * Handle some of the macro named rooms
919 void convert_room_name_macros(char *towhere, size_t maxlen) {
920 if (!strcasecmp(towhere, "_BASEROOM_")) {
921 safestrncpy(towhere, config.c_baseroom, maxlen);
923 else if (!strcasecmp(towhere, "_MAIL_")) {
924 safestrncpy(towhere, MAILROOM, maxlen);
926 else if (!strcasecmp(towhere, "_TRASH_")) {
927 safestrncpy(towhere, USERTRASHROOM, maxlen);
929 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
930 safestrncpy(towhere, config.c_twitroom, maxlen);
932 else if (!strcasecmp(towhere, "_CALENDAR_")) {
933 safestrncpy(towhere, USERCALENDARROOM, maxlen);
935 else if (!strcasecmp(towhere, "_TASKS_")) {
936 safestrncpy(towhere, USERTASKSROOM, maxlen);
938 else if (!strcasecmp(towhere, "_CONTACTS_")) {
939 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
941 else if (!strcasecmp(towhere, "_NOTES_")) {
942 safestrncpy(towhere, USERNOTESROOM, maxlen);
948 * cmd_goto() - goto a new room
950 void cmd_goto(char *gargs)
952 struct ctdlroom QRscratch;
956 char augmented_roomname[ROOMNAMELEN];
957 char towhere[ROOMNAMELEN];
961 if (CtdlAccessCheck(ac_logged_in)) return;
963 extract_token(towhere, gargs, 0, '|', sizeof towhere);
964 extract_token(password, gargs, 1, '|', sizeof password);
965 transiently = extract_int(gargs, 2);
967 getuser(&CC->user, CC->curr_user);
970 * Handle some of the macro named rooms
972 convert_room_name_macros(towhere, sizeof towhere);
974 /* First try a regular match */
975 c = getroom(&QRscratch, towhere);
977 /* Then try a mailbox name match */
979 MailboxName(augmented_roomname, sizeof augmented_roomname,
981 c = getroom(&QRscratch, augmented_roomname);
983 safestrncpy(towhere, augmented_roomname, sizeof towhere);
986 /* And if the room was found... */
989 /* Let internal programs go directly to any room. */
990 if (CC->internal_pgm) {
991 memcpy(&CC->room, &QRscratch,
992 sizeof(struct ctdlroom));
993 usergoto(NULL, 1, transiently, NULL, NULL);
997 /* See if there is an existing user/room relationship */
998 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
1000 /* normal clients have to pass through security */
1001 if (ra & UA_GOTOALLOWED) {
1006 if ((QRscratch.QRflags & QR_MAILBOX) &&
1007 ((ra & UA_GOTOALLOWED))) {
1008 memcpy(&CC->room, &QRscratch,
1009 sizeof(struct ctdlroom));
1010 usergoto(NULL, 1, transiently, NULL, NULL);
1012 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
1013 ((ra & UA_KNOWN) == 0) &&
1014 (strcasecmp(QRscratch.QRpasswd, password)) &&
1015 (CC->user.axlevel < 6)
1017 cprintf("%d wrong or missing passwd\n",
1018 ERROR + PASSWORD_REQUIRED);
1020 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
1021 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
1022 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
1023 ((ra & UA_KNOWN) == 0) &&
1024 (CC->user.axlevel < 6)
1026 lprintf(CTDL_DEBUG, "Failed to acquire private room\n");
1028 memcpy(&CC->room, &QRscratch,
1029 sizeof(struct ctdlroom));
1030 usergoto(NULL, 1, transiently, NULL, NULL);
1036 cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
1042 struct ctdluser temp;
1043 struct cdbdata *cdbus;
1046 getuser(&CC->user, CC->curr_user);
1049 * This command is only allowed by aides, room aides,
1050 * and room namespace owners
1053 || (atol(CC->room.QRname) == CC->user.usernum) ) {
1054 /* access granted */
1058 cprintf("%d Higher access or room ownership required.\n",
1059 ERROR + HIGHER_ACCESS_REQUIRED);
1063 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
1064 cdb_rewind(CDB_USERS);
1065 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1066 memset(&temp, 0, sizeof temp);
1067 memcpy(&temp, cdbus->ptr, sizeof temp);
1070 CtdlRoomAccess(&CC->room, &temp, &ra, NULL);
1071 if ((CC->room.QRflags & QR_INUSE)
1074 cprintf("%s\n", temp.fullname);
1081 * RDIR command for room directory
1089 struct stat statbuf;
1090 char tempfilename[PATH_MAX];
1092 if (CtdlAccessCheck(ac_logged_in)) return;
1093 CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
1095 getroom(&CC->room, CC->room.QRname);
1096 getuser(&CC->user, CC->curr_user);
1098 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
1099 cprintf("%d not here.\n", ERROR + NOT_HERE);
1102 if (((CC->room.QRflags & QR_VISDIR) == 0)
1103 && (CC->user.axlevel < 6)
1104 && (CC->user.usernum != CC->room.QRroomaide)) {
1105 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1108 cprintf("%d %s|%s/%s\n",
1112 CC->room.QRdirname);
1114 snprintf(buf, sizeof buf,
1115 "ls %s/%s >%s 2>/dev/null",
1121 snprintf(buf, sizeof buf,
1124 CC->room.QRdirname);
1125 fd = fopen(buf, "r");
1127 fd = fopen("/dev/null", "r");
1129 ls = fopen(tempfilename, "r");
1130 while (fgets(flnm, sizeof flnm, ls) != NULL) {
1131 flnm[strlen(flnm) - 1] = 0;
1132 if (strcasecmp(flnm, "filedir")) {
1133 snprintf(buf, sizeof buf,
1138 stat(buf, &statbuf);
1139 safestrncpy(comment, "", sizeof comment);
1141 while ((fgets(buf, sizeof buf, fd) != NULL)
1142 && (strlen(comment) == 0)) {
1143 buf[strlen(buf) - 1] = 0;
1144 if ((!strncasecmp(buf, flnm, strlen(flnm)))
1145 && (buf[strlen(flnm)] == ' '))
1146 safestrncpy(comment,
1147 &buf[strlen(flnm) + 1],
1150 cprintf("%s|%ld|%s\n", flnm, (long)statbuf.st_size, comment);
1155 unlink(tempfilename);
1161 * get room parameters (aide or room aide command)
1165 if (CtdlAccessCheck(ac_room_aide)) return;
1167 getroom(&CC->room, CC->room.QRname);
1168 cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1172 ((CC->room.QRflags & QR_MAILBOX) ?
1173 &CC->room.QRname[11] : CC->room.QRname),
1175 ((CC->room.QRflags & QR_PASSWORDED) ?
1176 CC->room.QRpasswd : ""),
1178 ((CC->room.QRflags & QR_DIRECTORY) ?
1179 CC->room.QRdirname : ""),
1182 (int) CC->room.QRfloor,
1183 (int) CC->room.QRorder,
1185 CC->room.QRdefaultview,
1192 * Back end function to rename a room.
1193 * You can also specify which floor to move the room to, or specify -1 to
1194 * keep the room on the same floor it was on.
1196 * If you are renaming a mailbox room, you must supply the namespace prefix
1197 * in *at least* the old name!
1199 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1201 struct ctdlroom qrbuf;
1202 struct ctdlroom qrtmp;
1207 char actual_old_name[ROOMNAMELEN];
1209 lprintf(CTDL_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1210 old_name, new_name, new_floor);
1212 if (new_floor >= 0) {
1213 fl = cgetfloor(new_floor);
1214 if ((fl->f_flags & F_INUSE) == 0) {
1215 return(crr_invalid_floor);
1219 begin_critical_section(S_ROOMS);
1221 if ( (getroom(&qrtmp, new_name) == 0)
1222 && (strcasecmp(new_name, old_name)) ) {
1223 ret = crr_already_exists;
1226 else if (getroom(&qrbuf, old_name) != 0) {
1227 ret = crr_room_not_found;
1230 else if ( (CC->user.axlevel < 6)
1231 && (CC->user.usernum != qrbuf.QRroomaide)
1232 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) ) {
1233 ret = crr_access_denied;
1236 else if (is_noneditable(&qrbuf)) {
1237 ret = crr_noneditable;
1242 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1243 if (qrbuf.QRflags & QR_MAILBOX) {
1244 owner = atol(qrbuf.QRname);
1246 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1247 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1248 "%010ld.%s", owner, new_name);
1251 safestrncpy(qrbuf.QRname, new_name,
1252 sizeof(qrbuf.QRname));
1255 /* Reject change of floor for baseroom/aideroom */
1256 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1257 !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1261 /* Take care of floor stuff */
1262 old_floor = qrbuf.QRfloor;
1263 if (new_floor < 0) {
1264 new_floor = old_floor;
1266 qrbuf.QRfloor = new_floor;
1269 begin_critical_section(S_CONFIG);
1271 /* If baseroom/aideroom name changes, update config */
1272 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1273 safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1276 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1277 safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1281 end_critical_section(S_CONFIG);
1283 /* If the room name changed, then there are now two room
1284 * records, so we have to delete the old one.
1286 if (strcasecmp(new_name, old_name)) {
1287 b_deleteroom(actual_old_name);
1293 end_critical_section(S_ROOMS);
1295 /* Adjust the floor reference counts if necessary */
1296 if (new_floor != old_floor) {
1297 lgetfloor(&flbuf, old_floor);
1298 --flbuf.f_ref_count;
1299 lputfloor(&flbuf, old_floor);
1300 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1301 lgetfloor(&flbuf, new_floor);
1302 ++flbuf.f_ref_count;
1303 lputfloor(&flbuf, new_floor);
1304 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1307 /* ...and everybody say "YATTA!" */
1313 * set room parameters (aide or room aide command)
1315 void cmd_setr(char *args)
1321 char new_name[ROOMNAMELEN];
1323 if (CtdlAccessCheck(ac_logged_in)) return;
1325 if (num_parms(args) >= 6) {
1326 new_floor = extract_int(args, 5);
1328 new_floor = (-1); /* don't change the floor */
1331 /* When is a new name more than just a new name? When the old name
1332 * has a namespace prefix.
1334 if (CC->room.QRflags & QR_MAILBOX) {
1335 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
1337 safestrncpy(new_name, "", sizeof new_name);
1339 extract_token(&new_name[strlen(new_name)], args, 0, '|', (sizeof new_name - strlen(new_name)));
1341 r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
1343 if (r == crr_room_not_found) {
1344 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
1345 } else if (r == crr_already_exists) {
1346 cprintf("%d '%s' already exists.\n",
1347 ERROR + ALREADY_EXISTS, new_name);
1348 } else if (r == crr_noneditable) {
1349 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
1350 } else if (r == crr_invalid_floor) {
1351 cprintf("%d Target floor does not exist.\n",
1352 ERROR + INVALID_FLOOR_OPERATION);
1353 } else if (r == crr_access_denied) {
1354 cprintf("%d You do not have permission to edit '%s'\n",
1355 ERROR + HIGHER_ACCESS_REQUIRED,
1357 } else if (r != crr_ok) {
1358 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1359 ERROR + INTERNAL_ERROR, r);
1366 getroom(&CC->room, new_name);
1368 /* Now we have to do a bunch of other stuff */
1370 if (num_parms(args) >= 7) {
1371 new_order = extract_int(args, 6);
1374 if (new_order > 127)
1378 lgetroom(&CC->room, CC->room.QRname);
1380 /* Directory room */
1381 extract_token(buf, args, 2, '|', sizeof buf);
1383 safestrncpy(CC->room.QRdirname, buf,
1384 sizeof CC->room.QRdirname);
1387 if (num_parms(args) >= 8) {
1388 CC->room.QRdefaultview = extract_int(args, 7);
1391 /* Second set of flags */
1392 if (num_parms(args) >= 9) {
1393 CC->room.QRflags2 = extract_int(args, 8);
1397 CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
1398 /* Clean up a client boo-boo: if the client set the room to
1399 * guess-name or passworded, ensure that the private flag is
1402 if ((CC->room.QRflags & QR_GUESSNAME)
1403 || (CC->room.QRflags & QR_PASSWORDED))
1404 CC->room.QRflags |= QR_PRIVATE;
1406 /* Some changes can't apply to BASEROOM */
1407 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1409 CC->room.QRorder = 0;
1410 CC->room.QRpasswd[0] = '\0';
1411 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1412 QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1413 CC->room.QRflags |= QR_PERMANENT;
1415 /* March order (doesn't apply to AIDEROOM) */
1416 if (num_parms(args) >= 7)
1417 CC->room.QRorder = (char) new_order;
1419 extract_token(buf, args, 1, '|', sizeof buf);
1421 safestrncpy(CC->room.QRpasswd, buf,
1422 sizeof CC->room.QRpasswd);
1423 /* Kick everyone out if the client requested it
1424 * (by changing the room's generation number)
1426 if (extract_int(args, 4)) {
1427 time(&CC->room.QRgen);
1430 /* Some changes can't apply to AIDEROOM */
1431 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1433 CC->room.QRorder = 0;
1434 CC->room.QRflags &= ~QR_MAILBOX;
1435 CC->room.QRflags |= QR_PERMANENT;
1438 /* Write the room record back to disk */
1439 lputroom(&CC->room);
1441 /* Create a room directory if necessary */
1442 if (CC->room.QRflags & QR_DIRECTORY) {
1443 snprintf(buf, sizeof buf,"%s/%s",
1445 CC->room.QRdirname);
1448 snprintf(buf, sizeof buf, "The room \"%s\" has been edited by %s.\n",
1449 CC->room.QRname, CC->curr_user);
1451 cprintf("%d Ok\n", CIT_OK);
1457 * get the name of the room aide for this room
1461 struct ctdluser usbuf;
1463 if (CtdlAccessCheck(ac_logged_in)) return;
1465 if (getuserbynumber(&usbuf, CC->room.QRroomaide) == 0) {
1466 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1468 cprintf("%d \n", CIT_OK);
1474 * set the room aide for this room
1476 void cmd_seta(char *new_ra)
1478 struct ctdluser usbuf;
1483 if (CtdlAccessCheck(ac_room_aide)) return;
1485 if (getuser(&usbuf, new_ra) != 0) {
1488 newu = usbuf.usernum;
1491 lgetroom(&CC->room, CC->room.QRname);
1493 if (CC->room.QRroomaide != newu) {
1496 CC->room.QRroomaide = newu;
1497 lputroom(&CC->room);
1500 * We have to post the change notice _after_ writing changes to
1501 * the room table, otherwise it would deadlock!
1503 if (post_notice == 1) {
1504 if (strlen(usbuf.fullname) > 0)
1505 snprintf(buf, sizeof buf,
1506 "%s is now the room aide for \"%s\".\n",
1507 usbuf.fullname, CC->room.QRname);
1509 snprintf(buf, sizeof buf,
1510 "There is now no room aide for \"%s\".\n",
1514 cprintf("%d Ok\n", CIT_OK);
1518 * retrieve info file for this room
1526 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_info_dir);
1527 info_fp = fopen(filename, "r");
1529 if (info_fp == NULL) {
1530 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
1533 cprintf("%d Info:\n", LISTING_FOLLOWS);
1534 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1535 if (strlen(buf) > 0)
1536 buf[strlen(buf) - 1] = 0;
1537 cprintf("%s\n", buf);
1544 * Asynchronously schedule a room for deletion. The room will appear
1545 * deleted to the user(s), but it won't actually get purged from the
1546 * database until THE DREADED AUTO-PURGER makes its next run.
1548 void schedule_room_for_deletion(struct ctdlroom *qrbuf)
1550 char old_name[ROOMNAMELEN];
1553 lprintf(CTDL_NOTICE, "Scheduling room <%s> for deletion\n",
1556 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1558 getroom(qrbuf, qrbuf->QRname);
1560 /* Turn the room into a private mailbox owned by a user who doesn't
1561 * exist. This will immediately make the room invisible to everyone,
1562 * and qualify the room for purging.
1564 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1569 qrbuf->QRflags |= QR_MAILBOX;
1570 time(&qrbuf->QRgen); /* Use a timestamp as the new generation number */
1574 b_deleteroom(old_name);
1580 * Back end processing to delete a room and everything associated with it
1581 * (This one is synchronous and should only get called by THE DREADED
1582 * AUTO-PURGER in serv_expire.c. All user-facing code should call
1583 * the asynchronous schedule_room_for_deletion() instead.)
1585 void delete_room(struct ctdlroom *qrbuf)
1589 /* TODO: filename magic? does this realy work? */
1591 lprintf(CTDL_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1593 /* Delete the info file */
1594 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_info_dir);
1597 /* Delete the image file */
1598 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_image_dir);
1601 /* Delete the room's network config file */
1602 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1605 /* Delete the messages in the room
1606 * (Careful: this opens an S_ROOMS critical section!)
1608 CtdlDeleteMessages(qrbuf->QRname, 0L, "", 0);
1610 /* Flag the room record as not in use */
1611 lgetroom(qrbuf, qrbuf->QRname);
1615 /* then decrement the reference count for the floor */
1616 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1617 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1618 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1620 /* Delete the room record from the database! */
1621 b_deleteroom(qrbuf->QRname);
1627 * Check access control for deleting a room
1629 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1631 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1635 if (is_noneditable(qr)) {
1640 * For mailboxes, check stuff
1642 if (qr->QRflags & QR_MAILBOX) {
1644 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1646 if (atol(qr->QRname) != CC->user.usernum) {
1647 return(0); /* not my room */
1650 /* Can't delete your Mail> room */
1651 if (!strcasecmp(&qr->QRname[12], MAILROOM)) return(0);
1653 /* Otherwise it's ok */
1658 * For normal rooms, just check for aide or room aide status.
1660 return(is_room_aide());
1664 * aide command: kill the current room
1666 void cmd_kill(char *argbuf)
1668 char deleted_room_name[ROOMNAMELEN];
1672 kill_ok = extract_int(argbuf, 0);
1674 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
1675 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1679 if (CC->room.QRflags & QR_MAILBOX) {
1680 safestrncpy(deleted_room_name, &CC->room.QRname[11], sizeof deleted_room_name);
1683 safestrncpy(deleted_room_name, CC->room.QRname, sizeof deleted_room_name);
1686 /* Do the dirty work */
1687 schedule_room_for_deletion(&CC->room);
1689 /* Return to the Lobby */
1690 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1692 /* tell the world what we did */
1693 snprintf(msg, sizeof msg, "The room \"%s\" has been deleted by %s.\n",
1694 deleted_room_name, CC->curr_user);
1696 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1698 cprintf("%d ok to delete.\n", CIT_OK);
1704 * Internal code to create a new room (returns room flags)
1706 * Room types: 0=public, 1=guessname, 2=passworded, 3=inv-only,
1707 * 4=mailbox, 5=mailbox, but caller supplies namespace
1709 unsigned create_room(char *new_room_name,
1711 char *new_room_pass,
1718 struct ctdlroom qrbuf;
1722 lprintf(CTDL_DEBUG, "create_room(name=%s, type=%d, view=%d)\n",
1723 new_room_name, new_room_type, new_room_view);
1725 if (getroom(&qrbuf, new_room_name) == 0) {
1726 lprintf(CTDL_DEBUG, "%s already exists.\n", new_room_name);
1730 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1731 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1732 qrbuf.QRflags = QR_INUSE;
1733 if (new_room_type > 0)
1734 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1735 if (new_room_type == 1)
1736 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1737 if (new_room_type == 2)
1738 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1739 if ( (new_room_type == 4) || (new_room_type == 5) )
1740 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1742 /* If the user is requesting a personal room, set up the room
1743 * name accordingly (prepend the user number)
1745 if (new_room_type == 4) {
1746 MailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1749 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1752 /* If the room is private, and the system administrator has elected
1753 * to automatically grant room aide privileges, do so now; otherwise,
1754 * set the room aide to undefined.
1756 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1757 qrbuf.QRroomaide = CC->user.usernum;
1759 qrbuf.QRroomaide = (-1L);
1763 * If the caller is only interested in testing whether this will work,
1764 * return now without creating the room.
1766 if (!really_create) return (qrbuf.QRflags);
1768 qrbuf.QRnumber = get_new_room_number();
1769 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1770 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1771 qrbuf.QRfloor = new_room_floor;
1772 qrbuf.QRdefaultview = new_room_view;
1774 /* save what we just did... */
1777 /* bump the reference count on whatever floor the room is on */
1778 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1779 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1780 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1782 /* Grant the creator access to the room unless the avoid_access
1783 * parameter was specified.
1785 if ( (CC->logged_in) && (avoid_access == 0) ) {
1786 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1787 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1788 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1789 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1792 /* resume our happy day */
1793 return (qrbuf.QRflags);
1800 void cmd_cre8(char *args)
1803 char new_room_name[ROOMNAMELEN];
1805 char new_room_pass[32];
1808 char *notification_message = NULL;
1811 int avoid_access = 0;
1813 cre8_ok = extract_int(args, 0);
1814 extract_token(new_room_name, args, 1, '|', sizeof new_room_name);
1815 new_room_name[ROOMNAMELEN - 1] = 0;
1816 new_room_type = extract_int(args, 2);
1817 extract_token(new_room_pass, args, 3, '|', sizeof new_room_pass);
1818 avoid_access = extract_int(args, 5);
1819 new_room_view = extract_int(args, 6);
1820 new_room_pass[9] = 0;
1823 if ((strlen(new_room_name) == 0) && (cre8_ok == 1)) {
1824 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
1828 if (!strcasecmp(new_room_name, MAILROOM)) {
1829 cprintf("%d '%s' already exists.\n",
1830 ERROR + ALREADY_EXISTS, new_room_name);
1834 if (num_parms(args) >= 5) {
1835 fl = cgetfloor(extract_int(args, 4));
1837 cprintf("%d Invalid floor number.\n",
1838 ERROR + INVALID_FLOOR_OPERATION);
1841 else if ((fl->f_flags & F_INUSE) == 0) {
1842 cprintf("%d Invalid floor number.\n",
1843 ERROR + INVALID_FLOOR_OPERATION);
1846 new_room_floor = extract_int(args, 4);
1850 if (CtdlAccessCheck(ac_logged_in)) return;
1852 if (CC->user.axlevel < config.c_createax) {
1853 cprintf("%d You need higher access to create rooms.\n",
1854 ERROR + HIGHER_ACCESS_REQUIRED);
1858 if ((strlen(new_room_name) == 0) && (cre8_ok == 0)) {
1859 cprintf("%d Ok to create rooms.\n", CIT_OK);
1863 if ((new_room_type < 0) || (new_room_type > 5)) {
1864 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
1868 if (new_room_type == 5) {
1869 if (CC->user.axlevel < 6) {
1870 cprintf("%d Higher access required\n",
1871 ERROR + HIGHER_ACCESS_REQUIRED);
1876 /* Check to make sure the requested room name doesn't already exist */
1877 newflags = create_room(new_room_name,
1878 new_room_type, new_room_pass, new_room_floor,
1879 0, avoid_access, new_room_view);
1880 if (newflags == 0) {
1881 cprintf("%d '%s' already exists.\n",
1882 ERROR + ALREADY_EXISTS, new_room_name);
1887 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
1891 /* If we reach this point, the room needs to be created. */
1893 newflags = create_room(new_room_name,
1894 new_room_type, new_room_pass, new_room_floor, 1, 0,
1897 /* post a message in Aide> describing the new room */
1898 notification_message = malloc(1024);
1899 snprintf(notification_message, 1024,
1900 "A new room called \"%s\" has been created by %s%s%s%s%s%s\n",
1903 ((newflags & QR_MAILBOX) ? " [personal]" : ""),
1904 ((newflags & QR_PRIVATE) ? " [private]" : ""),
1905 ((newflags & QR_GUESSNAME) ? " [hidden]" : ""),
1906 ((newflags & QR_PASSWORDED) ? " Password: " : ""),
1907 ((newflags & QR_PASSWORDED) ? new_room_pass : "")
1909 aide_message(notification_message);
1910 free(notification_message);
1912 cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
1917 void cmd_einf(char *ok)
1918 { /* enter info file for current room */
1920 char infofilename[SIZ];
1925 if (CtdlAccessCheck(ac_room_aide)) return;
1927 if (atoi(ok) == 0) {
1928 cprintf("%d Ok.\n", CIT_OK);
1931 assoc_file_name(infofilename, sizeof infofilename, &CC->room, ctdl_info_dir);
1932 lprintf(CTDL_DEBUG, "opening\n");
1933 fp = fopen(infofilename, "w");
1934 lprintf(CTDL_DEBUG, "checking\n");
1936 cprintf("%d Cannot open %s: %s\n",
1937 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1940 cprintf("%d Send info...\n", SEND_LISTING);
1943 client_getln(buf, sizeof buf);
1944 if (strcmp(buf, "000"))
1945 fprintf(fp, "%s\n", buf);
1946 } while (strcmp(buf, "000"));
1949 /* now update the room index so people will see our new info */
1950 lgetroom(&CC->room, CC->room.QRname); /* lock so no one steps on us */
1951 CC->room.QRinfo = CC->room.QRhighest + 1L;
1952 lputroom(&CC->room);
1957 * cmd_lflr() - List all known floors
1964 if (CtdlAccessCheck(ac_logged_in)) return;
1966 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1968 for (a = 0; a < MAXFLOORS; ++a) {
1969 getfloor(&flbuf, a);
1970 if (flbuf.f_flags & F_INUSE) {
1971 cprintf("%d|%s|%d\n",
1983 * create a new floor
1985 void cmd_cflr(char *argbuf)
1987 char new_floor_name[256];
1990 int free_slot = (-1);
1993 extract_token(new_floor_name, argbuf, 0, '|', sizeof new_floor_name);
1994 cflr_ok = extract_int(argbuf, 1);
1996 if (CtdlAccessCheck(ac_aide)) return;
1998 if (strlen(new_floor_name) == 0) {
1999 cprintf("%d Blank floor name not allowed.\n",
2000 ERROR + ILLEGAL_VALUE);
2004 for (a = 0; a < MAXFLOORS; ++a) {
2005 getfloor(&flbuf, a);
2007 /* note any free slots while we're scanning... */
2008 if (((flbuf.f_flags & F_INUSE) == 0)
2012 /* check to see if it already exists */
2013 if ((!strcasecmp(flbuf.f_name, new_floor_name))
2014 && (flbuf.f_flags & F_INUSE)) {
2015 cprintf("%d Floor '%s' already exists.\n",
2016 ERROR + ALREADY_EXISTS,
2022 if (free_slot < 0) {
2023 cprintf("%d There is no space available for a new floor.\n",
2024 ERROR + INVALID_FLOOR_OPERATION);
2028 cprintf("%d ok to create...\n", CIT_OK);
2031 lgetfloor(&flbuf, free_slot);
2032 flbuf.f_flags = F_INUSE;
2033 flbuf.f_ref_count = 0;
2034 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
2035 lputfloor(&flbuf, free_slot);
2036 cprintf("%d %d\n", CIT_OK, free_slot);
2044 void cmd_kflr(char *argbuf)
2047 int floor_to_delete;
2051 floor_to_delete = extract_int(argbuf, 0);
2052 kflr_ok = extract_int(argbuf, 1);
2054 if (CtdlAccessCheck(ac_aide)) return;
2056 lgetfloor(&flbuf, floor_to_delete);
2059 if ((flbuf.f_flags & F_INUSE) == 0) {
2060 cprintf("%d Floor %d not in use.\n",
2061 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
2064 if (flbuf.f_ref_count != 0) {
2065 cprintf("%d Cannot delete; floor contains %d rooms.\n",
2066 ERROR + INVALID_FLOOR_OPERATION,
2071 cprintf("%d Ok\n", CIT_OK);
2073 cprintf("%d Ok to delete...\n", CIT_OK);
2080 if ((delete_ok == 1) && (kflr_ok == 1))
2082 lputfloor(&flbuf, floor_to_delete);
2088 void cmd_eflr(char *argbuf)
2094 np = num_parms(argbuf);
2096 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
2100 if (CtdlAccessCheck(ac_aide)) return;
2102 floor_num = extract_int(argbuf, 0);
2103 lgetfloor(&flbuf, floor_num);
2104 if ((flbuf.f_flags & F_INUSE) == 0) {
2105 lputfloor(&flbuf, floor_num);
2106 cprintf("%d Floor %d is not in use.\n",
2107 ERROR + INVALID_FLOOR_OPERATION, floor_num);
2111 extract_token(flbuf.f_name, argbuf, 1, '|', sizeof flbuf.f_name);
2112 lputfloor(&flbuf, floor_num);
2114 cprintf("%d Ok\n", CIT_OK);