4 * Server functions which perform operations on room objects.
20 #if TIME_WITH_SYS_TIME
21 # include <sys/time.h>
25 # include <sys/time.h>
35 #include "serv_extensions.h"
39 #include "sysdep_decls.h"
43 #include "citserver.h"
47 struct floor *floorcache[MAXFLOORS];
50 * Generic routine for determining user access to rooms
52 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf,
53 int *result, int *view)
58 /* for internal programs, always do everything */
59 if (((CC->internal_pgm)) && (roombuf->QRflags & QR_INUSE)) {
60 retval = (UA_KNOWN | UA_GOTOALLOWED);
65 /* Locate any applicable user/room relationships */
66 CtdlGetRelationship(&vbuf, userbuf, roombuf);
68 /* Force the properties of the Aide room */
69 if (!strcasecmp(roombuf->QRname, config.c_aideroom)) {
70 if (userbuf->axlevel >= 6) {
71 retval = UA_KNOWN | UA_GOTOALLOWED;
78 /* If this is a public room, it's accessible... */
79 if ( ((roombuf->QRflags & QR_PRIVATE) == 0)
80 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
81 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
84 /* If this is a preferred users only room, check access level */
85 if (roombuf->QRflags & QR_PREFONLY) {
86 if (userbuf->axlevel < 5) {
87 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
91 /* For private rooms, check the generation number matchups */
92 if ( (roombuf->QRflags & QR_PRIVATE)
93 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
95 /* An explicit match means the user belongs in this room */
96 if (vbuf.v_flags & V_ACCESS) {
97 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
99 /* Otherwise, check if this is a guess-name or passworded
100 * room. If it is, a goto may at least be attempted
102 else if ((roombuf->QRflags & QR_PRIVATE)
103 || (roombuf->QRflags & QR_PASSWORDED)) {
104 retval = retval & ~UA_KNOWN;
105 retval = retval | UA_GOTOALLOWED;
109 /* For mailbox rooms, also check the generation number matchups */
110 if (roombuf->QRflags & QR_MAILBOX) {
111 if (userbuf->usernum == atol(roombuf->QRname)) {
112 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
114 /* An explicit match means the user belongs in this room */
115 if (vbuf.v_flags & V_ACCESS) {
116 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
120 /* Check to see if the user has forgotten this room */
121 if (vbuf.v_flags & V_FORGET) {
122 retval = retval & ~UA_KNOWN;
123 if ( ( ((roombuf->QRflags & QR_PRIVATE) == 0)
124 && ((roombuf->QRflags & QR_MAILBOX) == 0) )
125 || ( (roombuf->QRflags & QR_MAILBOX)
126 && (atol(roombuf->QRname) == CC->user.usernum))) {
127 retval = retval | UA_ZAPPED;
130 /* If user is explicitly locked out of this room, deny everything */
131 if (vbuf.v_flags & V_LOCKOUT) {
132 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
135 /* Aides get access to all private rooms */
136 if ( (userbuf->axlevel >= 6)
137 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
138 if (vbuf.v_flags & V_FORGET) {
139 retval = retval | UA_GOTOALLOWED;
142 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
146 /* Aides can gain access to mailboxes as well, but they don't show
149 if ( (userbuf->axlevel >= 6)
150 && (roombuf->QRflags & QR_MAILBOX) ) {
151 retval = retval | UA_GOTOALLOWED;
154 NEWMSG: /* By the way, we also check for the presence of new messages */
155 if (is_msg_in_sequence_set(vbuf.v_seen, roombuf->QRhighest) == 0) {
156 retval = retval | UA_HASNEWMSGS;
159 /* System rooms never show up in the list. */
160 if (roombuf->QRflags2 & QR2_SYSTEM) {
161 retval = retval & ~UA_KNOWN;
165 /* Now give the caller the information it wants. */
166 if (result != NULL) *result = retval;
167 if (view != NULL) *view = vbuf.v_view;
171 * Self-checking stuff for a room record read into memory
173 void room_sanity_check(struct ctdlroom *qrbuf)
175 /* Mailbox rooms are always on the lowest floor */
176 if (qrbuf->QRflags & QR_MAILBOX) {
179 /* Listing order of 0 is illegal except for base rooms */
180 if (qrbuf->QRorder == 0)
181 if (!(qrbuf->QRflags & QR_MAILBOX) &&
182 strncasecmp(qrbuf->QRname, config.c_baseroom, ROOMNAMELEN)
184 strncasecmp(qrbuf->QRname, config.c_aideroom, ROOMNAMELEN))
190 * getroom() - retrieve room data from disk
192 int getroom(struct ctdlroom *qrbuf, char *room_name)
194 struct cdbdata *cdbqr;
195 char lowercase_name[ROOMNAMELEN];
196 char personal_lowercase_name[ROOMNAMELEN];
199 for (a = 0; room_name[a] && a < sizeof lowercase_name - 1; ++a) {
200 lowercase_name[a] = tolower(room_name[a]);
202 lowercase_name[a] = 0;
204 memset(qrbuf, 0, sizeof(struct ctdlroom));
206 /* First, try the public namespace */
207 cdbqr = cdb_fetch(CDB_ROOMS,
208 lowercase_name, strlen(lowercase_name));
210 /* If that didn't work, try the user's personal namespace */
212 snprintf(personal_lowercase_name,
213 sizeof personal_lowercase_name, "%010ld.%s",
214 CC->user.usernum, lowercase_name);
215 cdbqr = cdb_fetch(CDB_ROOMS,
216 personal_lowercase_name,
217 strlen(personal_lowercase_name));
220 memcpy(qrbuf, cdbqr->ptr,
221 ((cdbqr->len > sizeof(struct ctdlroom)) ?
222 sizeof(struct ctdlroom) : cdbqr->len));
225 room_sanity_check(qrbuf);
234 * lgetroom() - same as getroom() but locks the record (if supported)
236 int lgetroom(struct ctdlroom *qrbuf, char *room_name)
239 retval = getroom(qrbuf, room_name);
240 if (retval == 0) begin_critical_section(S_ROOMS);
246 * b_putroom() - back end to putroom() and b_deleteroom()
247 * (if the supplied buffer is NULL, delete the room record)
249 void b_putroom(struct ctdlroom *qrbuf, char *room_name)
251 char lowercase_name[ROOMNAMELEN];
254 for (a = 0; a <= strlen(room_name); ++a) {
255 lowercase_name[a] = tolower(room_name[a]);
259 cdb_delete(CDB_ROOMS,
260 lowercase_name, strlen(lowercase_name));
262 time(&qrbuf->QRmtime);
264 lowercase_name, strlen(lowercase_name),
265 qrbuf, sizeof(struct ctdlroom));
271 * putroom() - store room data to disk
273 void putroom(struct ctdlroom *qrbuf) {
274 b_putroom(qrbuf, qrbuf->QRname);
279 * b_deleteroom() - delete a room record from disk
281 void b_deleteroom(char *room_name) {
282 b_putroom(NULL, room_name);
288 * lputroom() - same as putroom() but unlocks the record (if supported)
290 void lputroom(struct ctdlroom *qrbuf)
294 end_critical_section(S_ROOMS);
298 /****************************************************************************/
301 * getfloor() - retrieve floor data from disk
303 void getfloor(struct floor *flbuf, int floor_num)
305 struct cdbdata *cdbfl;
307 memset(flbuf, 0, sizeof(struct floor));
308 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
310 memcpy(flbuf, cdbfl->ptr,
311 ((cdbfl->len > sizeof(struct floor)) ?
312 sizeof(struct floor) : cdbfl->len));
315 if (floor_num == 0) {
316 safestrncpy(flbuf->f_name, "Main Floor",
317 sizeof flbuf->f_name);
318 flbuf->f_flags = F_INUSE;
319 flbuf->f_ref_count = 3;
326 * lgetfloor() - same as getfloor() but locks the record (if supported)
328 void lgetfloor(struct floor *flbuf, int floor_num)
331 begin_critical_section(S_FLOORTAB);
332 getfloor(flbuf, floor_num);
337 * cgetfloor() - Get floor record from *cache* (loads from disk if needed)
339 * This is strictly a performance hack.
341 struct floor *cgetfloor(int floor_num) {
342 static int initialized = 0;
345 struct floor *fl = NULL;
347 begin_critical_section(S_FLOORCACHE);
348 if (initialized == 0) {
349 for (i=0; i<MAXFLOORS; ++i) {
350 floorcache[floor_num] = NULL;
354 if (floorcache[floor_num] == NULL) {
357 end_critical_section(S_FLOORCACHE);
360 fl = malloc(sizeof(struct floor));
361 getfloor(fl, floor_num);
362 begin_critical_section(S_FLOORCACHE);
363 if (floorcache[floor_num] != NULL) {
364 free(floorcache[floor_num]);
366 floorcache[floor_num] = fl;
367 end_critical_section(S_FLOORCACHE);
370 return(floorcache[floor_num]);
376 * putfloor() - store floor data on disk
378 void putfloor(struct floor *flbuf, int floor_num)
380 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
381 begin_critical_section(S_FLOORCACHE);
382 if (floorcache[floor_num] != NULL) {
383 free(floorcache[floor_num]);
384 floorcache[floor_num] = malloc(sizeof(struct floor));
385 memcpy(floorcache[floor_num], flbuf, sizeof(struct floor));
387 end_critical_section(S_FLOORCACHE);
389 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
390 flbuf, sizeof(struct floor));
395 * lputfloor() - same as putfloor() but unlocks the record (if supported)
397 void lputfloor(struct floor *flbuf, int floor_num)
400 putfloor(flbuf, floor_num);
401 end_critical_section(S_FLOORTAB);
407 * Traverse the room file...
409 void ForEachRoom(void (*CallBack) (struct ctdlroom *EachRoom, void *out_data),
412 struct ctdlroom qrbuf;
413 struct cdbdata *cdbqr;
415 cdb_rewind(CDB_ROOMS);
417 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
418 memset(&qrbuf, 0, sizeof(struct ctdlroom));
419 memcpy(&qrbuf, cdbqr->ptr,
420 ((cdbqr->len > sizeof(struct ctdlroom)) ?
421 sizeof(struct ctdlroom) : cdbqr->len));
423 room_sanity_check(&qrbuf);
424 if (qrbuf.QRflags & QR_INUSE)
425 (*CallBack)(&qrbuf, in_data);
431 * delete_msglist() - delete room message pointers
433 void delete_msglist(struct ctdlroom *whichroom)
435 struct cdbdata *cdbml;
437 /* Make sure the msglist we're deleting actually exists, otherwise
438 * gdbm will complain when we try to delete an invalid record
440 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
444 /* Go ahead and delete it */
445 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
453 * sort message pointers
454 * (returns new msg count)
456 int sort_msglist(long listptrs[], int oldcount)
467 for (a = numitems - 2; a >= 0; --a) {
468 for (b = 0; b <= a; ++b) {
469 if (listptrs[b] > (listptrs[b + 1])) {
471 hold2 = listptrs[b + 1];
473 listptrs[b + 1] = hold1;
478 /* and yank any nulls */
479 while ((numitems > 0) && (listptrs[0] == 0L)) {
480 memcpy(&listptrs[0], &listptrs[1],
481 (sizeof(long) * (numitems - 1)));
490 * Determine whether a given room is non-editable.
492 int is_noneditable(struct ctdlroom *qrbuf)
495 /* Mail> rooms are non-editable */
496 if ( (qrbuf->QRflags & QR_MAILBOX)
497 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
500 /* Everything else is editable */
507 * Back-back-end for all room listing commands
509 void list_roomname(struct ctdlroom *qrbuf, int ra, int view)
511 char truncated_roomname[ROOMNAMELEN];
513 /* For my own mailbox rooms, chop off the owner prefix */
514 if ( (qrbuf->QRflags & QR_MAILBOX)
515 && (atol(qrbuf->QRname) == CC->user.usernum) ) {
516 safestrncpy(truncated_roomname, qrbuf->QRname, sizeof truncated_roomname);
517 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
518 cprintf("%s", truncated_roomname);
520 /* For all other rooms, just display the name in its entirety */
522 cprintf("%s", qrbuf->QRname);
525 /* ...and now the other parameters */
526 cprintf("|%u|%d|%d|%d|%d|%d|\n",
528 (int) qrbuf->QRfloor,
529 (int) qrbuf->QRorder,
530 (int) qrbuf->QRflags2,
538 * cmd_lrms() - List all accessible rooms, known or forgotten
540 void cmd_lrms_backend(struct ctdlroom *qrbuf, void *data)
542 int FloorBeingSearched = (-1);
546 FloorBeingSearched = *(int *)data;
547 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
549 if ((( ra & (UA_KNOWN | UA_ZAPPED)))
550 && ((qrbuf->QRfloor == (FloorBeingSearched))
551 || ((FloorBeingSearched) < 0)))
552 list_roomname(qrbuf, ra, view);
555 void cmd_lrms(char *argbuf)
557 int FloorBeingSearched = (-1);
558 if (strlen(argbuf) > 0)
559 FloorBeingSearched = extract_int(argbuf, 0);
561 if (CtdlAccessCheck(ac_logged_in)) return;
563 if (getuser(&CC->user, CC->curr_user)) {
564 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
567 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
569 ForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
576 * cmd_lkra() - List all known rooms
578 void cmd_lkra_backend(struct ctdlroom *qrbuf, void *data)
580 int FloorBeingSearched = (-1);
584 FloorBeingSearched = *(int *)data;
585 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
587 if ((( ra & (UA_KNOWN)))
588 && ((qrbuf->QRfloor == (FloorBeingSearched))
589 || ((FloorBeingSearched) < 0)))
590 list_roomname(qrbuf, ra, view);
593 void cmd_lkra(char *argbuf)
595 int FloorBeingSearched = (-1);
596 if (strlen(argbuf) > 0)
597 FloorBeingSearched = extract_int(argbuf, 0);
599 if (CtdlAccessCheck(ac_logged_in)) return;
601 if (getuser(&CC->user, CC->curr_user)) {
602 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
605 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
607 ForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
613 void cmd_lprm_backend(struct ctdlroom *qrbuf, void *data)
615 int FloorBeingSearched = (-1);
619 FloorBeingSearched = *(int *)data;
620 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
622 if ( ((qrbuf->QRflags & QR_PRIVATE) == 0)
623 && ((qrbuf->QRflags & QR_MAILBOX) == 0)
624 && ((qrbuf->QRfloor == (FloorBeingSearched))
625 || ((FloorBeingSearched) < 0)))
626 list_roomname(qrbuf, ra, view);
629 void cmd_lprm(char *argbuf)
631 int FloorBeingSearched = (-1);
632 if (strlen(argbuf) > 0)
633 FloorBeingSearched = extract_int(argbuf, 0);
635 cprintf("%d Publiic rooms:\n", LISTING_FOLLOWS);
637 ForEachRoom(cmd_lprm_backend, &FloorBeingSearched);
644 * cmd_lkrn() - List all known rooms with new messages
646 void cmd_lkrn_backend(struct ctdlroom *qrbuf, void *data)
648 int FloorBeingSearched = (-1);
652 FloorBeingSearched = *(int *)data;
653 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
656 && (ra & UA_HASNEWMSGS)
657 && ((qrbuf->QRfloor == (FloorBeingSearched))
658 || ((FloorBeingSearched) < 0)))
659 list_roomname(qrbuf, ra, view);
662 void cmd_lkrn(char *argbuf)
664 int FloorBeingSearched = (-1);
665 if (strlen(argbuf) > 0)
666 FloorBeingSearched = extract_int(argbuf, 0);
668 if (CtdlAccessCheck(ac_logged_in)) return;
670 if (getuser(&CC->user, CC->curr_user)) {
671 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
674 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
676 ForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
683 * cmd_lkro() - List all known rooms
685 void cmd_lkro_backend(struct ctdlroom *qrbuf, void *data)
687 int FloorBeingSearched = (-1);
691 FloorBeingSearched = *(int *)data;
692 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
695 && ((ra & UA_HASNEWMSGS) == 0)
696 && ((qrbuf->QRfloor == (FloorBeingSearched))
697 || ((FloorBeingSearched) < 0)))
698 list_roomname(qrbuf, ra, view);
701 void cmd_lkro(char *argbuf)
703 int FloorBeingSearched = (-1);
704 if (strlen(argbuf) > 0)
705 FloorBeingSearched = extract_int(argbuf, 0);
707 if (CtdlAccessCheck(ac_logged_in)) return;
709 if (getuser(&CC->user, CC->curr_user)) {
710 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
713 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
715 ForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
722 * cmd_lzrm() - List all forgotten rooms
724 void cmd_lzrm_backend(struct ctdlroom *qrbuf, void *data)
726 int FloorBeingSearched = (-1);
730 FloorBeingSearched = *(int *)data;
731 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
733 if ((ra & UA_GOTOALLOWED)
735 && ((qrbuf->QRfloor == (FloorBeingSearched))
736 || ((FloorBeingSearched) < 0)))
737 list_roomname(qrbuf, ra, view);
740 void cmd_lzrm(char *argbuf)
742 int FloorBeingSearched = (-1);
743 if (strlen(argbuf) > 0)
744 FloorBeingSearched = extract_int(argbuf, 0);
746 if (CtdlAccessCheck(ac_logged_in)) return;
748 if (getuser(&CC->user, CC->curr_user)) {
749 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
752 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
754 ForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
760 * Make the specified room the current room for this session. No validation
761 * or access control is done here -- the caller should make sure that the
762 * specified room exists and is ok to access.
764 void usergoto(char *where, int display_result, int transiently,
765 int *retmsgs, int *retnew)
768 int new_messages = 0;
769 int old_messages = 0;
770 int total_messages = 0;
774 int newmailcount = 0;
776 char truncated_roomname[ROOMNAMELEN];
777 struct cdbdata *cdbfr;
778 long *msglist = NULL;
780 unsigned int original_v_flags;
783 char setstr[128], lostr[64], histr[64];
786 /* If the supplied room name is NULL, the caller wants us to know that
787 * it has already copied the room record into CC->room, so
788 * we can skip the extra database fetch.
791 safestrncpy(CC->room.QRname, where, sizeof CC->room.QRname);
792 getroom(&CC->room, where);
795 /* Take care of all the formalities. */
797 begin_critical_section(S_USERS);
798 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
799 original_v_flags = vbuf.v_flags;
801 /* Know the room ... but not if it's the page log room, or if the
802 * caller specified that we're only entering this room transiently.
804 if ((strcasecmp(CC->room.QRname, config.c_logpages))
805 && (transiently == 0) ) {
806 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
807 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
810 /* Only rewrite the database record if we changed something */
811 if (vbuf.v_flags != original_v_flags) {
812 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
814 end_critical_section(S_USERS);
816 /* Check for new mail */
817 newmailcount = NewMailCount();
819 /* set info to 1 if the user needs to read the room's info file */
820 if (CC->room.QRinfo > vbuf.v_lastseen) {
825 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
827 msglist = (long *) cdbfr->ptr;
828 cdbfr->ptr = NULL; /* usergoto() now owns this memory */
829 num_msgs = cdbfr->len / sizeof(long);
834 for (a=0; a<num_msgs; ++a) {
835 if (msglist[a] > 0L) ++total_messages;
837 new_messages = num_msgs;
838 num_sets = num_tokens(vbuf.v_seen, ',');
839 for (s=0; s<num_sets; ++s) {
840 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
842 extract_token(lostr, setstr, 0, ':', sizeof lostr);
843 if (num_tokens(setstr, ':') >= 2) {
844 extract_token(histr, setstr, 1, ':', sizeof histr);
845 if (!strcmp(histr, "*")) {
846 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
850 strcpy(histr, lostr);
855 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
856 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
862 new_messages = total_messages - old_messages;
864 if (msglist != NULL) free(msglist);
866 if (CC->room.QRflags & QR_MAILBOX)
871 if ((CC->room.QRroomaide == CC->user.usernum)
872 || (CC->user.axlevel >= 6))
877 safestrncpy(truncated_roomname, CC->room.QRname, sizeof truncated_roomname);
878 if ( (CC->room.QRflags & QR_MAILBOX)
879 && (atol(CC->room.QRname) == CC->user.usernum) ) {
880 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
883 if (retmsgs != NULL) *retmsgs = total_messages;
884 if (retnew != NULL) *retnew = new_messages;
885 lprintf(CTDL_DEBUG, "<%s> %d new of %d total messages\n",
887 new_messages, total_messages
890 CC->curr_view = (int)vbuf.v_view;
892 if (display_result) {
893 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|\n",
894 CIT_OK, CtdlCheckExpress(),
899 (int)CC->room.QRflags,
900 (long)CC->room.QRhighest,
901 (long)vbuf.v_lastseen,
905 (int)CC->room.QRfloor,
907 (int)CC->room.QRdefaultview
914 * cmd_goto() - goto a new room
916 void cmd_goto(char *gargs)
918 struct ctdlroom QRscratch;
922 char augmented_roomname[ROOMNAMELEN];
923 char towhere[ROOMNAMELEN];
927 if (CtdlAccessCheck(ac_logged_in)) return;
929 extract_token(towhere, gargs, 0, '|', sizeof towhere);
930 extract_token(password, gargs, 1, '|', sizeof password);
931 transiently = extract_int(gargs, 2);
933 getuser(&CC->user, CC->curr_user);
936 * Handle some of the macro named rooms
938 if (!strcasecmp(towhere, "_BASEROOM_")) {
939 safestrncpy(towhere, config.c_baseroom, sizeof towhere);
941 else if (!strcasecmp(towhere, "_MAIL_")) {
942 safestrncpy(towhere, MAILROOM, sizeof towhere);
944 else if (!strcasecmp(towhere, "_TRASH_")) {
945 safestrncpy(towhere, USERTRASHROOM, sizeof towhere);
947 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
948 safestrncpy(towhere, config.c_twitroom, sizeof towhere);
950 else if (!strcasecmp(towhere, "_CALENDAR_")) {
951 safestrncpy(towhere, USERCALENDARROOM, sizeof towhere);
953 else if (!strcasecmp(towhere, "_TASKS_")) {
954 safestrncpy(towhere, USERTASKSROOM, sizeof towhere);
956 else if (!strcasecmp(towhere, "_CONTACTS_")) {
957 safestrncpy(towhere, USERCONTACTSROOM, sizeof towhere);
959 else if (!strcasecmp(towhere, "_NOTES_")) {
960 safestrncpy(towhere, USERNOTESROOM, sizeof towhere);
963 /* First try a regular match */
964 c = getroom(&QRscratch, towhere);
966 /* Then try a mailbox name match */
968 MailboxName(augmented_roomname, sizeof augmented_roomname,
970 c = getroom(&QRscratch, augmented_roomname);
972 safestrncpy(towhere, augmented_roomname, sizeof towhere);
975 /* And if the room was found... */
978 /* Let internal programs go directly to any room. */
979 if (CC->internal_pgm) {
980 memcpy(&CC->room, &QRscratch,
981 sizeof(struct ctdlroom));
982 usergoto(NULL, 1, transiently, NULL, NULL);
986 /* See if there is an existing user/room relationship */
987 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
989 /* normal clients have to pass through security */
990 if (ra & UA_GOTOALLOWED) {
995 if ((QRscratch.QRflags & QR_MAILBOX) &&
996 ((ra & UA_GOTOALLOWED))) {
997 memcpy(&CC->room, &QRscratch,
998 sizeof(struct ctdlroom));
999 usergoto(NULL, 1, transiently, NULL, NULL);
1001 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
1002 ((ra & UA_KNOWN) == 0) &&
1003 (strcasecmp(QRscratch.QRpasswd, password)) &&
1004 (CC->user.axlevel < 6)
1006 cprintf("%d wrong or missing passwd\n",
1007 ERROR + PASSWORD_REQUIRED);
1009 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
1010 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
1011 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
1012 ((ra & UA_KNOWN) == 0) &&
1013 (CC->user.axlevel < 6)
1015 lprintf(CTDL_DEBUG, "Failed to acquire private room\n");
1017 memcpy(&CC->room, &QRscratch,
1018 sizeof(struct ctdlroom));
1019 usergoto(NULL, 1, transiently, NULL, NULL);
1025 cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
1031 struct ctdluser temp;
1032 struct cdbdata *cdbus;
1035 getuser(&CC->user, CC->curr_user);
1038 * This command is only allowed by aides, room aides,
1039 * and room namespace owners
1042 || (atol(CC->room.QRname) == CC->user.usernum) ) {
1043 /* access granted */
1047 cprintf("%d Higher access or room ownership required.\n",
1048 ERROR + HIGHER_ACCESS_REQUIRED);
1052 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
1053 cdb_rewind(CDB_USERS);
1054 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1055 memset(&temp, 0, sizeof temp);
1056 memcpy(&temp, cdbus->ptr, sizeof temp);
1059 CtdlRoomAccess(&CC->room, &temp, &ra, NULL);
1060 if ((CC->room.QRflags & QR_INUSE)
1063 cprintf("%s\n", temp.fullname);
1070 * RDIR command for room directory
1078 struct stat statbuf;
1079 char tempfilename[PATH_MAX];
1081 if (CtdlAccessCheck(ac_logged_in)) return;
1082 tmpnam(tempfilename);
1084 getroom(&CC->room, CC->room.QRname);
1085 getuser(&CC->user, CC->curr_user);
1087 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
1088 cprintf("%d not here.\n", ERROR + NOT_HERE);
1091 if (((CC->room.QRflags & QR_VISDIR) == 0)
1092 && (CC->user.axlevel < 6)
1093 && (CC->user.usernum != CC->room.QRroomaide)) {
1094 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1098 #ifndef HAVE_DATA_DIR
1104 LISTING_FOLLOWS, config.c_fqdn, CC->room.QRdirname);
1106 snprintf(buf, sizeof buf, "ls "
1107 #ifndef HAVE_DATA_DIR
1112 "/files/%s >%s 2> /dev/null",
1113 CC->room.QRdirname, tempfilename);
1116 snprintf(buf, sizeof buf,
1117 #ifndef HAVE_DATA_DIR
1122 "/files/%s/filedir", CC->room.QRdirname);
1123 fd = fopen(buf, "r");
1125 fd = fopen("/dev/null", "r");
1127 ls = fopen(tempfilename, "r");
1128 while (fgets(flnm, sizeof flnm, ls) != NULL) {
1129 flnm[strlen(flnm) - 1] = 0;
1130 if (strcasecmp(flnm, "filedir")) {
1131 snprintf(buf, sizeof buf,
1132 #ifndef HAVE_DATA_DIR
1138 CC->room.QRdirname, flnm);
1139 stat(buf, &statbuf);
1140 safestrncpy(comment, "", sizeof comment);
1142 while ((fgets(buf, sizeof buf, fd) != NULL)
1143 && (strlen(comment) == 0)) {
1144 buf[strlen(buf) - 1] = 0;
1145 if ((!strncasecmp(buf, flnm, strlen(flnm)))
1146 && (buf[strlen(flnm)] == ' '))
1147 safestrncpy(comment,
1148 &buf[strlen(flnm) + 1],
1151 cprintf("%s|%ld|%s\n", flnm, (long)statbuf.st_size, comment);
1156 unlink(tempfilename);
1162 * get room parameters (aide or room aide command)
1166 if (CtdlAccessCheck(ac_room_aide)) return;
1168 getroom(&CC->room, CC->room.QRname);
1169 cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1173 ((CC->room.QRflags & QR_MAILBOX) ?
1174 &CC->room.QRname[11] : CC->room.QRname),
1176 ((CC->room.QRflags & QR_PASSWORDED) ?
1177 CC->room.QRpasswd : ""),
1179 ((CC->room.QRflags & QR_DIRECTORY) ?
1180 CC->room.QRdirname : ""),
1183 (int) CC->room.QRfloor,
1184 (int) CC->room.QRorder,
1186 CC->room.QRdefaultview,
1193 * Back end function to rename a room.
1194 * You can also specify which floor to move the room to, or specify -1 to
1195 * keep the room on the same floor it was on.
1197 * If you are renaming a mailbox room, you must supply the namespace prefix
1198 * in *at least* the old name!
1200 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1202 struct ctdlroom qrbuf;
1203 struct ctdlroom qrtmp;
1208 char actual_old_name[ROOMNAMELEN];
1210 lprintf(CTDL_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1211 old_name, new_name, new_floor);
1213 if (new_floor >= 0) {
1214 fl = cgetfloor(new_floor);
1215 if ((fl->f_flags & F_INUSE) == 0) {
1216 return(crr_invalid_floor);
1220 begin_critical_section(S_ROOMS);
1222 if ( (getroom(&qrtmp, new_name) == 0)
1223 && (strcasecmp(new_name, old_name)) ) {
1224 ret = crr_already_exists;
1227 else if (getroom(&qrbuf, old_name) != 0) {
1228 ret = crr_room_not_found;
1231 else if ( (CC->user.axlevel < 6)
1232 && (CC->user.usernum != qrbuf.QRroomaide)
1233 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) ) {
1234 ret = crr_access_denied;
1237 else if (is_noneditable(&qrbuf)) {
1238 ret = crr_noneditable;
1243 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1244 if (qrbuf.QRflags & QR_MAILBOX) {
1245 owner = atol(qrbuf.QRname);
1247 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1248 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1249 "%010ld.%s", owner, new_name);
1252 safestrncpy(qrbuf.QRname, new_name,
1253 sizeof(qrbuf.QRname));
1256 /* Reject change of floor for baseroom/aideroom */
1257 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1258 !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1262 /* Take care of floor stuff */
1263 old_floor = qrbuf.QRfloor;
1264 if (new_floor < 0) {
1265 new_floor = old_floor;
1267 qrbuf.QRfloor = new_floor;
1270 begin_critical_section(S_CONFIG);
1272 /* If baseroom/aideroom name changes, update config */
1273 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1274 safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1277 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1278 safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1282 end_critical_section(S_CONFIG);
1284 /* If the room name changed, then there are now two room
1285 * records, so we have to delete the old one.
1287 if (strcasecmp(new_name, old_name)) {
1288 b_deleteroom(actual_old_name);
1294 end_critical_section(S_ROOMS);
1296 /* Adjust the floor reference counts if necessary */
1297 if (new_floor != old_floor) {
1298 lgetfloor(&flbuf, old_floor);
1299 --flbuf.f_ref_count;
1300 lputfloor(&flbuf, old_floor);
1301 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1302 lgetfloor(&flbuf, new_floor);
1303 ++flbuf.f_ref_count;
1304 lputfloor(&flbuf, new_floor);
1305 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1308 /* ...and everybody say "YATTA!" */
1314 * set room parameters (aide or room aide command)
1316 void cmd_setr(char *args)
1322 char new_name[ROOMNAMELEN];
1324 if (CtdlAccessCheck(ac_logged_in)) return;
1326 if (num_parms(args) >= 6) {
1327 new_floor = extract_int(args, 5);
1329 new_floor = (-1); /* don't change the floor */
1332 /* When is a new name more than just a new name? When the old name
1333 * has a namespace prefix.
1335 if (CC->room.QRflags & QR_MAILBOX) {
1336 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
1338 safestrncpy(new_name, "", sizeof new_name);
1340 extract_token(&new_name[strlen(new_name)], args, 0, '|', (sizeof new_name - strlen(new_name)));
1342 r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
1344 if (r == crr_room_not_found) {
1345 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
1346 } else if (r == crr_already_exists) {
1347 cprintf("%d '%s' already exists.\n",
1348 ERROR + ALREADY_EXISTS, new_name);
1349 } else if (r == crr_noneditable) {
1350 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
1351 } else if (r == crr_invalid_floor) {
1352 cprintf("%d Target floor does not exist.\n",
1353 ERROR + INVALID_FLOOR_OPERATION);
1354 } else if (r == crr_access_denied) {
1355 cprintf("%d You do not have permission to edit '%s'\n",
1356 ERROR + HIGHER_ACCESS_REQUIRED,
1358 } else if (r != crr_ok) {
1359 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1360 ERROR + INTERNAL_ERROR, r);
1367 getroom(&CC->room, new_name);
1369 /* Now we have to do a bunch of other stuff */
1371 if (num_parms(args) >= 7) {
1372 new_order = extract_int(args, 6);
1375 if (new_order > 127)
1379 lgetroom(&CC->room, CC->room.QRname);
1381 /* Directory room */
1382 extract_token(buf, args, 2, '|', sizeof buf);
1384 safestrncpy(CC->room.QRdirname, buf,
1385 sizeof CC->room.QRdirname);
1388 if (num_parms(args) >= 8) {
1389 CC->room.QRdefaultview = extract_int(args, 7);
1392 /* Second set of flags */
1393 if (num_parms(args) >= 9) {
1394 CC->room.QRflags2 = extract_int(args, 8);
1398 CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
1399 /* Clean up a client boo-boo: if the client set the room to
1400 * guess-name or passworded, ensure that the private flag is
1403 if ((CC->room.QRflags & QR_GUESSNAME)
1404 || (CC->room.QRflags & QR_PASSWORDED))
1405 CC->room.QRflags |= QR_PRIVATE;
1407 /* Some changes can't apply to BASEROOM */
1408 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1410 CC->room.QRorder = 0;
1411 CC->room.QRpasswd[0] = '\0';
1412 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1413 QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1414 CC->room.QRflags |= QR_PERMANENT;
1416 /* March order (doesn't apply to AIDEROOM) */
1417 if (num_parms(args) >= 7)
1418 CC->room.QRorder = (char) new_order;
1420 extract_token(buf, args, 1, '|', sizeof buf);
1422 safestrncpy(CC->room.QRpasswd, buf,
1423 sizeof CC->room.QRpasswd);
1424 /* Kick everyone out if the client requested it
1425 * (by changing the room's generation number)
1427 if (extract_int(args, 4)) {
1428 time(&CC->room.QRgen);
1431 /* Some changes can't apply to AIDEROOM */
1432 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1434 CC->room.QRorder = 0;
1435 CC->room.QRflags &= ~QR_MAILBOX;
1436 CC->room.QRflags |= QR_PERMANENT;
1439 /* Write the room record back to disk */
1440 lputroom(&CC->room);
1442 /* Create a room directory if necessary */
1443 if (CC->room.QRflags & QR_DIRECTORY) {
1444 snprintf(buf, sizeof buf,
1445 #ifndef HAVE_DATA_DIR
1450 "/files/%s", CC->room.QRdirname);
1453 snprintf(buf, sizeof buf, "%s> edited by %s\n", CC->room.QRname, CC->curr_user);
1455 cprintf("%d Ok\n", CIT_OK);
1461 * get the name of the room aide for this room
1465 struct ctdluser usbuf;
1467 if (CtdlAccessCheck(ac_logged_in)) return;
1469 if (getuserbynumber(&usbuf, CC->room.QRroomaide) == 0) {
1470 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1472 cprintf("%d \n", CIT_OK);
1478 * set the room aide for this room
1480 void cmd_seta(char *new_ra)
1482 struct ctdluser usbuf;
1487 if (CtdlAccessCheck(ac_room_aide)) return;
1489 if (getuser(&usbuf, new_ra) != 0) {
1492 newu = usbuf.usernum;
1495 lgetroom(&CC->room, CC->room.QRname);
1497 if (CC->room.QRroomaide != newu) {
1500 CC->room.QRroomaide = newu;
1501 lputroom(&CC->room);
1504 * We have to post the change notice _after_ writing changes to
1505 * the room table, otherwise it would deadlock!
1507 if (post_notice == 1) {
1508 if (strlen(usbuf.fullname) > 0)
1509 snprintf(buf, sizeof buf,
1510 "%s is now room aide for %s>\n",
1511 usbuf.fullname, CC->room.QRname);
1513 snprintf(buf, sizeof buf,
1514 "There is now no room aide for %s>\n",
1518 cprintf("%d Ok\n", CIT_OK);
1522 * Generate an associated file name for a room
1524 void assoc_file_name(char *buf, size_t n,
1525 struct ctdlroom *qrbuf, const char *prefix)
1527 snprintf(buf, n, "./%s/%ld", prefix, qrbuf->QRnumber);
1531 * retrieve info file for this room
1539 assoc_file_name(filename, sizeof filename, &CC->room, "info");
1540 info_fp = fopen(filename, "r");
1542 if (info_fp == NULL) {
1543 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
1546 cprintf("%d Info:\n", LISTING_FOLLOWS);
1547 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1548 if (strlen(buf) > 0)
1549 buf[strlen(buf) - 1] = 0;
1550 cprintf("%s\n", buf);
1557 * Asynchronously schedule a room for deletion. The room will appear
1558 * deleted to the user(s), but it won't actually get purged from the
1559 * database until THE DREADED AUTO-PURGER makes its next run.
1561 void schedule_room_for_deletion(struct ctdlroom *qrbuf)
1563 char old_name[ROOMNAMELEN];
1566 lprintf(CTDL_NOTICE, "Scheduling room <%s> for deletion\n",
1569 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1571 getroom(qrbuf, qrbuf->QRname);
1573 /* Turn the room into a private mailbox owned by a user who doesn't
1574 * exist. This will immediately make the room invisible to everyone,
1575 * and qualify the room for purging.
1577 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1582 qrbuf->QRflags |= QR_MAILBOX;
1583 time(&qrbuf->QRgen); /* Use a timestamp as the new generation number */
1587 b_deleteroom(old_name);
1593 * Back end processing to delete a room and everything associated with it
1594 * (This one is synchronous and should only get called by THE DREADED
1595 * AUTO-PURGER in serv_expire.c. All user-facing code should call
1596 * the asynchronous schedule_room_for_deletion() instead.)
1598 void delete_room(struct ctdlroom *qrbuf)
1603 lprintf(CTDL_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1605 /* Delete the info file */
1606 assoc_file_name(filename, sizeof filename, qrbuf, "info");
1609 /* Delete the image file */
1610 assoc_file_name(filename, sizeof filename, qrbuf, "images");
1613 /* Delete the room's network config file */
1614 assoc_file_name(filename, sizeof filename, qrbuf, "netconfigs");
1617 /* Delete the messages in the room
1618 * (Careful: this opens an S_ROOMS critical section!)
1620 CtdlDeleteMessages(qrbuf->QRname, 0L, "", 0);
1622 /* Flag the room record as not in use */
1623 lgetroom(qrbuf, qrbuf->QRname);
1627 /* then decrement the reference count for the floor */
1628 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1629 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1630 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1632 /* Delete the room record from the database! */
1633 b_deleteroom(qrbuf->QRname);
1639 * Check access control for deleting a room
1641 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1643 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1647 if (is_noneditable(qr)) {
1652 * For mailboxes, check stuff
1654 if (qr->QRflags & QR_MAILBOX) {
1656 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1658 if (atol(qr->QRname) != CC->user.usernum) {
1659 return(0); /* not my room */
1662 /* Can't delete your Mail> room */
1663 if (!strcasecmp(&qr->QRname[12], MAILROOM)) return(0);
1665 /* Otherwise it's ok */
1670 * For normal rooms, just check for aide or room aide status.
1672 return(is_room_aide());
1676 * aide command: kill the current room
1678 void cmd_kill(char *argbuf)
1680 char deleted_room_name[ROOMNAMELEN];
1684 kill_ok = extract_int(argbuf, 0);
1686 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
1687 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1691 if (CC->room.QRflags & QR_MAILBOX) {
1692 safestrncpy(deleted_room_name, &CC->room.QRname[11], sizeof deleted_room_name);
1695 safestrncpy(deleted_room_name, CC->room.QRname, sizeof deleted_room_name);
1698 /* Do the dirty work */
1699 schedule_room_for_deletion(&CC->room);
1701 /* Return to the Lobby */
1702 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1704 /* tell the world what we did */
1705 snprintf(msg, sizeof msg, "%s> killed by %s\n",
1706 deleted_room_name, CC->curr_user);
1708 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1710 cprintf("%d ok to delete.\n", CIT_OK);
1716 * Internal code to create a new room (returns room flags)
1718 * Room types: 0=public, 1=guessname, 2=passworded, 3=inv-only,
1719 * 4=mailbox, 5=mailbox, but caller supplies namespace
1721 unsigned create_room(char *new_room_name,
1723 char *new_room_pass,
1730 struct ctdlroom qrbuf;
1734 lprintf(CTDL_DEBUG, "create_room(name=%s, type=%d, view=%d)\n",
1735 new_room_name, new_room_type, new_room_view);
1737 if (getroom(&qrbuf, new_room_name) == 0) {
1738 lprintf(CTDL_DEBUG, "%s already exists.\n", new_room_name);
1742 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1743 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1744 qrbuf.QRflags = QR_INUSE;
1745 if (new_room_type > 0)
1746 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1747 if (new_room_type == 1)
1748 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1749 if (new_room_type == 2)
1750 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1751 if ( (new_room_type == 4) || (new_room_type == 5) )
1752 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1754 /* If the user is requesting a personal room, set up the room
1755 * name accordingly (prepend the user number)
1757 if (new_room_type == 4) {
1758 MailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1761 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1764 /* If the room is private, and the system administrator has elected
1765 * to automatically grant room aide privileges, do so now; otherwise,
1766 * set the room aide to undefined.
1768 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1769 qrbuf.QRroomaide = CC->user.usernum;
1771 qrbuf.QRroomaide = (-1L);
1775 * If the caller is only interested in testing whether this will work,
1776 * return now without creating the room.
1778 if (!really_create) return (qrbuf.QRflags);
1780 qrbuf.QRnumber = get_new_room_number();
1781 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1782 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1783 qrbuf.QRfloor = new_room_floor;
1784 qrbuf.QRdefaultview = new_room_view;
1786 /* save what we just did... */
1789 /* bump the reference count on whatever floor the room is on */
1790 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1791 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1792 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1794 /* Grant the creator access to the room unless the avoid_access
1795 * parameter was specified.
1797 if (avoid_access == 0) {
1798 lgetuser(&CC->user, CC->curr_user);
1799 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1800 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1801 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1802 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1803 lputuser(&CC->user);
1806 /* resume our happy day */
1807 return (qrbuf.QRflags);
1814 void cmd_cre8(char *args)
1817 char new_room_name[ROOMNAMELEN];
1819 char new_room_pass[32];
1822 char *notification_message = NULL;
1825 int avoid_access = 0;
1827 cre8_ok = extract_int(args, 0);
1828 extract_token(new_room_name, args, 1, '|', sizeof new_room_name);
1829 new_room_name[ROOMNAMELEN - 1] = 0;
1830 new_room_type = extract_int(args, 2);
1831 extract_token(new_room_pass, args, 3, '|', sizeof new_room_pass);
1832 avoid_access = extract_int(args, 5);
1833 new_room_view = extract_int(args, 6);
1834 new_room_pass[9] = 0;
1837 if ((strlen(new_room_name) == 0) && (cre8_ok == 1)) {
1838 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
1842 if (!strcasecmp(new_room_name, MAILROOM)) {
1843 cprintf("%d '%s' already exists.\n",
1844 ERROR + ALREADY_EXISTS, new_room_name);
1848 if (num_parms(args) >= 5) {
1849 fl = cgetfloor(extract_int(args, 4));
1851 cprintf("%d Invalid floor number.\n",
1852 ERROR + INVALID_FLOOR_OPERATION);
1855 else if ((fl->f_flags & F_INUSE) == 0) {
1856 cprintf("%d Invalid floor number.\n",
1857 ERROR + INVALID_FLOOR_OPERATION);
1860 new_room_floor = extract_int(args, 4);
1864 if (CtdlAccessCheck(ac_logged_in)) return;
1866 if (CC->user.axlevel < config.c_createax) {
1867 cprintf("%d You need higher access to create rooms.\n",
1868 ERROR + HIGHER_ACCESS_REQUIRED);
1872 if ((strlen(new_room_name) == 0) && (cre8_ok == 0)) {
1873 cprintf("%d Ok to create rooms.\n", CIT_OK);
1877 if ((new_room_type < 0) || (new_room_type > 5)) {
1878 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
1882 if (new_room_type == 5) {
1883 if (CC->user.axlevel < 6) {
1884 cprintf("%d Higher access required\n",
1885 ERROR + HIGHER_ACCESS_REQUIRED);
1890 /* Check to make sure the requested room name doesn't already exist */
1891 newflags = create_room(new_room_name,
1892 new_room_type, new_room_pass, new_room_floor,
1893 0, avoid_access, new_room_view);
1894 if (newflags == 0) {
1895 cprintf("%d '%s' already exists.\n",
1896 ERROR + ALREADY_EXISTS, new_room_name);
1901 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
1905 /* If we reach this point, the room needs to be created. */
1907 newflags = create_room(new_room_name,
1908 new_room_type, new_room_pass, new_room_floor, 1, 0,
1911 /* post a message in Aide> describing the new room */
1912 notification_message = malloc(1024);
1913 snprintf(notification_message, 1024,
1914 "%s> created by %s%s%s%s%s%s\n",
1917 ((newflags & QR_MAILBOX) ? " [personal]" : ""),
1918 ((newflags & QR_PRIVATE) ? " [private]" : ""),
1919 ((newflags & QR_GUESSNAME) ? " [hidden]" : ""),
1920 ((newflags & QR_PASSWORDED) ? " Password: " : ""),
1921 ((newflags & QR_PASSWORDED) ? new_room_pass : "")
1923 aide_message(notification_message);
1924 free(notification_message);
1926 cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
1931 void cmd_einf(char *ok)
1932 { /* enter info file for current room */
1934 char infofilename[SIZ];
1939 if (CtdlAccessCheck(ac_room_aide)) return;
1941 if (atoi(ok) == 0) {
1942 cprintf("%d Ok.\n", CIT_OK);
1945 assoc_file_name(infofilename, sizeof infofilename, &CC->room, "info");
1946 lprintf(CTDL_DEBUG, "opening\n");
1947 fp = fopen(infofilename, "w");
1948 lprintf(CTDL_DEBUG, "checking\n");
1950 cprintf("%d Cannot open %s: %s\n",
1951 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1954 cprintf("%d Send info...\n", SEND_LISTING);
1957 client_getln(buf, sizeof buf);
1958 if (strcmp(buf, "000"))
1959 fprintf(fp, "%s\n", buf);
1960 } while (strcmp(buf, "000"));
1963 /* now update the room index so people will see our new info */
1964 lgetroom(&CC->room, CC->room.QRname); /* lock so no one steps on us */
1965 CC->room.QRinfo = CC->room.QRhighest + 1L;
1966 lputroom(&CC->room);
1971 * cmd_lflr() - List all known floors
1978 if (CtdlAccessCheck(ac_logged_in)) return;
1980 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1982 for (a = 0; a < MAXFLOORS; ++a) {
1983 getfloor(&flbuf, a);
1984 if (flbuf.f_flags & F_INUSE) {
1985 cprintf("%d|%s|%d\n",
1997 * create a new floor
1999 void cmd_cflr(char *argbuf)
2001 char new_floor_name[256];
2004 int free_slot = (-1);
2007 extract_token(new_floor_name, argbuf, 0, '|', sizeof new_floor_name);
2008 cflr_ok = extract_int(argbuf, 1);
2010 if (CtdlAccessCheck(ac_aide)) return;
2012 if (strlen(new_floor_name) == 0) {
2013 cprintf("%d Blank floor name not allowed.\n",
2014 ERROR + ILLEGAL_VALUE);
2018 for (a = 0; a < MAXFLOORS; ++a) {
2019 getfloor(&flbuf, a);
2021 /* note any free slots while we're scanning... */
2022 if (((flbuf.f_flags & F_INUSE) == 0)
2026 /* check to see if it already exists */
2027 if ((!strcasecmp(flbuf.f_name, new_floor_name))
2028 && (flbuf.f_flags & F_INUSE)) {
2029 cprintf("%d Floor '%s' already exists.\n",
2030 ERROR + ALREADY_EXISTS,
2036 if (free_slot < 0) {
2037 cprintf("%d There is no space available for a new floor.\n",
2038 ERROR + INVALID_FLOOR_OPERATION);
2042 cprintf("%d ok to create...\n", CIT_OK);
2045 lgetfloor(&flbuf, free_slot);
2046 flbuf.f_flags = F_INUSE;
2047 flbuf.f_ref_count = 0;
2048 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
2049 lputfloor(&flbuf, free_slot);
2050 cprintf("%d %d\n", CIT_OK, free_slot);
2058 void cmd_kflr(char *argbuf)
2061 int floor_to_delete;
2065 floor_to_delete = extract_int(argbuf, 0);
2066 kflr_ok = extract_int(argbuf, 1);
2068 if (CtdlAccessCheck(ac_aide)) return;
2070 lgetfloor(&flbuf, floor_to_delete);
2073 if ((flbuf.f_flags & F_INUSE) == 0) {
2074 cprintf("%d Floor %d not in use.\n",
2075 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
2078 if (flbuf.f_ref_count != 0) {
2079 cprintf("%d Cannot delete; floor contains %d rooms.\n",
2080 ERROR + INVALID_FLOOR_OPERATION,
2085 cprintf("%d Ok\n", CIT_OK);
2087 cprintf("%d Ok to delete...\n", CIT_OK);
2094 if ((delete_ok == 1) && (kflr_ok == 1))
2096 lputfloor(&flbuf, floor_to_delete);
2102 void cmd_eflr(char *argbuf)
2108 np = num_parms(argbuf);
2110 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
2114 if (CtdlAccessCheck(ac_aide)) return;
2116 floor_num = extract_int(argbuf, 0);
2117 lgetfloor(&flbuf, floor_num);
2118 if ((flbuf.f_flags & F_INUSE) == 0) {
2119 lputfloor(&flbuf, floor_num);
2120 cprintf("%d Floor %d is not in use.\n",
2121 ERROR + INVALID_FLOOR_OPERATION, floor_num);
2125 extract_token(flbuf.f_name, argbuf, 1, '|', sizeof flbuf.f_name);
2126 lputfloor(&flbuf, floor_num);
2128 cprintf("%d Ok\n", CIT_OK);