4 * Server functions which perform operations on room objects.
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
24 # include <sys/time.h>
34 #include "dynloader.h"
38 #include "sysdep_decls.h"
42 #include "citserver.h"
46 struct floor *floorcache[MAXFLOORS];
49 * Generic routine for determining user access to rooms
51 int CtdlRoomAccess(struct quickroom *roombuf, struct usersupp *userbuf)
56 lprintf(9, "CtdlRoomAccess( %s , %s )\n",
57 roombuf->QRname, userbuf->fullname);
59 /* for internal programs, always do everything */
60 if (((CC->internal_pgm)) && (roombuf->QRflags & QR_INUSE)) {
61 return (UA_KNOWN | UA_GOTOALLOWED);
64 /* Locate any applicable user/room relationships */
65 CtdlGetRelationship(&vbuf, userbuf, roombuf);
67 /* Force the properties of the Aide room */
68 if (!strcasecmp(roombuf->QRname, AIDEROOM)) {
69 if (userbuf->axlevel >= 6) {
70 retval = UA_KNOWN | UA_GOTOALLOWED;
77 /* If this is a public room, it's accessible... */
78 if ( ((roombuf->QRflags & QR_PRIVATE) == 0)
79 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
80 lprintf(9, " -- public room, known/allowed\n");
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 lprintf(9, " -- not a preferred user!\n");
88 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
92 /* For private rooms, check the generation number matchups */
93 if ( (roombuf->QRflags & QR_PRIVATE)
94 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
95 lprintf(9, " -- checking private room access\n");
97 /* An explicit match means the user belongs in this room */
98 if (vbuf.v_flags & V_ACCESS) {
99 lprintf(9, " -- explicit match! known/allowed\n");
100 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
102 /* Otherwise, check if this is a guess-name or passworded
103 * room. If it is, a goto may at least be attempted
105 else if ((roombuf->QRflags & QR_PRIVATE)
106 || (roombuf->QRflags & QR_PASSWORDED)) {
107 lprintf(9, " -- guessable.\n");
108 retval = retval & ~UA_KNOWN;
109 retval = retval | UA_GOTOALLOWED;
113 /* For mailbox rooms, also check the generation number matchups */
114 if (roombuf->QRflags & QR_MAILBOX) {
115 lprintf(9, " -- checking mailbox room access\n");
116 if (userbuf->usernum == atol(roombuf->QRname)) {
117 lprintf(9, " -- owner! known/allowed\n");
118 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
120 /* An explicit match means the user belongs in this room */
121 if (vbuf.v_flags & V_ACCESS) {
122 lprintf(9, " -- explicit match! known/allowed\n");
123 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
127 /* Check to see if the user has forgotten this room */
128 if (vbuf.v_flags & V_FORGET) {
129 lprintf(9, " -- forgotten room!\n");
130 retval = retval & ~UA_KNOWN;
131 retval = retval | UA_ZAPPED;
133 /* If user is explicitly locked out of this room, deny everything */
134 if (vbuf.v_flags & V_LOCKOUT) {
135 lprintf(9, " -- explicit lockout!\n");
136 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
139 /* Aides get access to everything */
140 if (userbuf->axlevel >= 6) {
141 lprintf(9, " -- user is an Aide\n");
142 if (vbuf.v_flags & V_FORGET) {
143 lprintf(9, " -- forgotten room\n");
144 retval = retval | UA_GOTOALLOWED;
147 lprintf(9, " -- goto allowed\n");
148 retval = retval | UA_GOTOALLOWED;
149 if ((roombuf->QRflags & QR_MAILBOX) == 0) {
150 lprintf(9, " -- not mailbox, make it known\n");
151 retval = retval | UA_KNOWN;
156 NEWMSG: /* By the way, we also check for the presence of new messages */
157 if (is_msg_in_mset(vbuf.v_seen, roombuf->QRhighest) == 0) {
158 lprintf(9, " -- has new messages\n");
159 retval = retval | UA_HASNEWMSGS;
162 /* System rooms never show up in the list. */
163 if (roombuf->QRflags2 & QR2_SYSTEM) {
164 lprintf(9, " -- system room -- hide it\n");
165 retval = retval & ~UA_KNOWN;
171 * Self-checking stuff for a room record read into memory
173 void room_sanity_check(struct quickroom *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 (!is_noneditable(qrbuf))
187 * getroom() - retrieve room data from disk
189 int getroom(struct quickroom *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 quickroom));
203 /* First, try the public namespace */
204 cdbqr = cdb_fetch(CDB_QUICKROOM,
205 lowercase_name, strlen(lowercase_name));
207 /* If that didn't work, try the user's personal namespace */
209 sprintf(personal_lowercase_name, "%010ld.%s",
210 CC->usersupp.usernum, lowercase_name);
211 cdbqr = cdb_fetch(CDB_QUICKROOM,
212 personal_lowercase_name,
213 strlen(personal_lowercase_name));
216 memcpy(qrbuf, cdbqr->ptr,
217 ((cdbqr->len > sizeof(struct quickroom)) ?
218 sizeof(struct quickroom) : cdbqr->len));
221 room_sanity_check(qrbuf);
230 * lgetroom() - same as getroom() but locks the record (if supported)
232 int lgetroom(struct quickroom *qrbuf, char *room_name)
235 retval = getroom(qrbuf, room_name);
236 if (retval == 0) begin_critical_section(S_QUICKROOM);
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 quickroom *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_QUICKROOM,
256 lowercase_name, strlen(lowercase_name));
258 time(&qrbuf->QRmtime);
259 cdb_store(CDB_QUICKROOM,
260 lowercase_name, strlen(lowercase_name),
261 qrbuf, sizeof(struct quickroom));
267 * putroom() - store room data to disk
269 void putroom(struct quickroom *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 quickroom *qrbuf)
290 end_critical_section(S_QUICKROOM);
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 strcpy(flbuf->f_name, "Main Floor");
313 flbuf->f_flags = F_INUSE;
314 flbuf->f_ref_count = 3;
321 * lgetfloor() - same as getfloor() but locks the record (if supported)
323 void lgetfloor(struct floor *flbuf, int floor_num)
326 begin_critical_section(S_FLOORTAB);
327 getfloor(flbuf, floor_num);
332 * cgetfloor() - Get floor record from *cache* (loads from disk if needed)
334 * This is strictly a performance hack.
336 struct floor *cgetfloor(int floor_num) {
337 static int initialized = 0;
340 if (initialized == 0) {
341 for (i=0; i<MAXFLOORS; ++i) {
342 floorcache[floor_num] = NULL;
347 if (floorcache[floor_num] == NULL) {
348 floorcache[floor_num] = mallok(sizeof(struct floor));
349 getfloor(floorcache[floor_num], floor_num);
352 return(floorcache[floor_num]);
358 * putfloor() - store floor data on disk
360 void putfloor(struct floor *flbuf, int floor_num)
362 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
363 flbuf, sizeof(struct floor));
365 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
366 if (floorcache[floor_num] != NULL) {
367 phree(floorcache[floor_num]);
368 floorcache[floor_num] = NULL;
374 * lputfloor() - same as putfloor() but unlocks the record (if supported)
376 void lputfloor(struct floor *flbuf, int floor_num)
379 putfloor(flbuf, floor_num);
380 end_critical_section(S_FLOORTAB);
386 * Traverse the room file...
388 void ForEachRoom(void (*CallBack) (struct quickroom *EachRoom, void *out_data),
391 struct quickroom qrbuf;
392 struct cdbdata *cdbqr;
394 cdb_rewind(CDB_QUICKROOM);
396 while (cdbqr = cdb_next_item(CDB_QUICKROOM), cdbqr != NULL) {
397 memset(&qrbuf, 0, sizeof(struct quickroom));
398 memcpy(&qrbuf, cdbqr->ptr,
399 ((cdbqr->len > sizeof(struct quickroom)) ?
400 sizeof(struct quickroom) : cdbqr->len));
402 room_sanity_check(&qrbuf);
403 if (qrbuf.QRflags & QR_INUSE)
404 (*CallBack)(&qrbuf, in_data);
410 * delete_msglist() - delete room message pointers
412 void delete_msglist(struct quickroom *whichroom)
414 struct cdbdata *cdbml;
416 /* Make sure the msglist we're deleting actually exists, otherwise
417 * gdbm will complain when we try to delete an invalid record
419 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
423 /* Go ahead and delete it */
424 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
432 * sort message pointers
433 * (returns new msg count)
435 int sort_msglist(long listptrs[], int oldcount)
446 for (a = numitems - 2; a >= 0; --a) {
447 for (b = 0; b <= a; ++b) {
448 if (listptrs[b] > (listptrs[b + 1])) {
450 hold2 = listptrs[b + 1];
452 listptrs[b + 1] = hold1;
457 /* and yank any nulls */
458 while ((numitems > 0) && (listptrs[0] == 0L)) {
459 memcpy(&listptrs[0], &listptrs[1],
460 (sizeof(long) * (numitems - 1)));
469 * Determine whether a given room is non-editable.
471 int is_noneditable(struct quickroom *qrbuf)
474 /* Lobby> and Aide> are non-editable */
475 if (!strcasecmp(qrbuf->QRname, BASEROOM))
477 else if (!strcasecmp(qrbuf->QRname, AIDEROOM))
480 /* Mail> rooms are also non-editable */
481 else if ( (qrbuf->QRflags & QR_MAILBOX)
482 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
485 /* Everything else is editable */
493 * Back-back-end for all room listing commands
495 void list_roomname(struct quickroom *qrbuf)
497 char truncated_roomname[ROOMNAMELEN];
499 /* For mailbox rooms, chop off the owner prefix */
500 if (qrbuf->QRflags & QR_MAILBOX) {
501 strcpy(truncated_roomname, qrbuf->QRname);
502 strcpy(truncated_roomname, &truncated_roomname[11]);
503 cprintf("%s", truncated_roomname);
505 /* For all other rooms, just display the name in its entirety */
507 cprintf("%s", qrbuf->QRname);
510 /* ...and now the other parameters */
511 cprintf("|%u|%d|%d\n",
513 (int) qrbuf->QRfloor,
514 (int) qrbuf->QRorder);
519 * cmd_lrms() - List all accessible rooms, known or forgotten
521 void cmd_lrms_backend(struct quickroom *qrbuf, void *data)
523 int FloorBeingSearched = (-1);
524 FloorBeingSearched = *(int *)data;
526 if (((CtdlRoomAccess(qrbuf, &CC->usersupp)
527 & (UA_KNOWN | UA_ZAPPED)))
528 && ((qrbuf->QRfloor == (FloorBeingSearched))
529 || ((FloorBeingSearched) < 0)))
530 list_roomname(qrbuf);
533 void cmd_lrms(char *argbuf)
535 int FloorBeingSearched = (-1);
536 if (strlen(argbuf) > 0)
537 FloorBeingSearched = extract_int(argbuf, 0);
539 if (CtdlAccessCheck(ac_logged_in)) return;
541 if (getuser(&CC->usersupp, CC->curr_user)) {
542 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
545 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
547 ForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
554 * cmd_lkra() - List all known rooms
556 void cmd_lkra_backend(struct quickroom *qrbuf, void *data)
558 int FloorBeingSearched = (-1);
559 FloorBeingSearched = *(int *)data;
561 if (((CtdlRoomAccess(qrbuf, &CC->usersupp)
563 && ((qrbuf->QRfloor == (FloorBeingSearched))
564 || ((FloorBeingSearched) < 0)))
565 list_roomname(qrbuf);
568 void cmd_lkra(char *argbuf)
570 int FloorBeingSearched = (-1);
571 if (strlen(argbuf) > 0)
572 FloorBeingSearched = extract_int(argbuf, 0);
574 if (CtdlAccessCheck(ac_logged_in)) return;
576 if (getuser(&CC->usersupp, CC->curr_user)) {
577 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
580 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
582 ForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
589 * cmd_lkrn() - List all known rooms with new messages
591 void cmd_lkrn_backend(struct quickroom *qrbuf, void *data)
594 int FloorBeingSearched = (-1);
595 FloorBeingSearched = *(int *)data;
597 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
599 && (ra & UA_HASNEWMSGS)
600 && ((qrbuf->QRfloor == (FloorBeingSearched))
601 || ((FloorBeingSearched) < 0)))
602 list_roomname(qrbuf);
605 void cmd_lkrn(char *argbuf)
607 int FloorBeingSearched = (-1);
608 if (strlen(argbuf) > 0)
609 FloorBeingSearched = extract_int(argbuf, 0);
611 if (CtdlAccessCheck(ac_logged_in)) return;
613 if (getuser(&CC->usersupp, CC->curr_user)) {
614 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
617 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
619 ForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
626 * cmd_lkro() - List all known rooms
628 void cmd_lkro_backend(struct quickroom *qrbuf, void *data)
631 int FloorBeingSearched = (-1);
632 FloorBeingSearched = *(int *)data;
634 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
636 && ((ra & UA_HASNEWMSGS) == 0)
637 && ((qrbuf->QRfloor == (FloorBeingSearched))
638 || ((FloorBeingSearched) < 0)))
639 list_roomname(qrbuf);
642 void cmd_lkro(char *argbuf)
644 int FloorBeingSearched = (-1);
645 if (strlen(argbuf) > 0)
646 FloorBeingSearched = extract_int(argbuf, 0);
648 if (CtdlAccessCheck(ac_logged_in)) return;
650 if (getuser(&CC->usersupp, CC->curr_user)) {
651 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
654 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
656 ForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
663 * cmd_lzrm() - List all forgotten rooms
665 void cmd_lzrm_backend(struct quickroom *qrbuf, void *data)
668 int FloorBeingSearched = (-1);
669 FloorBeingSearched = *(int *)data;
671 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
672 if ((ra & UA_GOTOALLOWED)
674 && ((qrbuf->QRfloor == (FloorBeingSearched))
675 || ((FloorBeingSearched) < 0)))
676 list_roomname(qrbuf);
679 void cmd_lzrm(char *argbuf)
681 int FloorBeingSearched = (-1);
682 if (strlen(argbuf) > 0)
683 FloorBeingSearched = extract_int(argbuf, 0);
685 if (CtdlAccessCheck(ac_logged_in)) return;
687 if (getuser(&CC->usersupp, CC->curr_user)) {
688 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
691 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
693 ForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
699 void usergoto(char *where, int display_result, int *retmsgs, int *retnew)
702 int new_messages = 0;
703 int total_messages = 0;
707 int newmailcount = 0;
709 char truncated_roomname[ROOMNAMELEN];
710 struct cdbdata *cdbfr;
711 long *msglist = NULL;
714 strcpy(CC->quickroom.QRname, where);
715 getroom(&CC->quickroom, where);
717 lgetuser(&CC->usersupp, CC->curr_user);
718 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
720 /* Know the room ... but not if it's the page log room */
721 if (strcasecmp(where, config.c_logpages)) {
722 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
723 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
725 CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
726 lputuser(&CC->usersupp);
728 /* check for new mail */
729 newmailcount = NewMailCount();
731 /* set info to 1 if the user needs to read the room's info file */
732 if (CC->quickroom.QRinfo > vbuf.v_lastseen)
736 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->quickroom.QRnumber, sizeof(long));
738 msglist = mallok(cdbfr->len);
739 memcpy(msglist, cdbfr->ptr, cdbfr->len);
740 num_msgs = cdbfr->len / sizeof(long);
744 if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
745 if (msglist[a] > 0L) {
747 if (is_msg_in_mset(vbuf.v_seen, msglist[a]) == 0) {
753 if (msglist != NULL) phree(msglist);
755 if (CC->quickroom.QRflags & QR_MAILBOX)
760 if ((CC->quickroom.QRroomaide == CC->usersupp.usernum)
761 || (CC->usersupp.axlevel >= 6))
766 strcpy(truncated_roomname, CC->quickroom.QRname);
767 if (CC->quickroom.QRflags & QR_MAILBOX) {
768 strcpy(truncated_roomname, &truncated_roomname[11]);
771 if (retmsgs != NULL) *retmsgs = total_messages;
772 if (retnew != NULL) *retnew = new_messages;
773 lprintf(9, "<%s> %d new of %d total messages\n",
774 CC->quickroom.QRname, new_messages, total_messages);
777 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d\n",
778 OK, CtdlCheckExpress(),
780 new_messages, total_messages,
781 info, CC->quickroom.QRflags,
782 CC->quickroom.QRhighest,
784 rmailflag, raideflag, newmailcount,
785 CC->quickroom.QRfloor);
791 * cmd_goto() - goto a new room
793 void cmd_goto(char *gargs)
795 struct quickroom QRscratch;
799 char augmented_roomname[SIZ];
803 if (CtdlAccessCheck(ac_logged_in)) return;
805 extract(towhere, gargs, 0);
806 extract(password, gargs, 1);
808 getuser(&CC->usersupp, CC->curr_user);
810 if (!strcasecmp(towhere, "_BASEROOM_"))
811 strcpy(towhere, BASEROOM);
813 if (!strcasecmp(towhere, "_MAIL_"))
814 strcpy(towhere, MAILROOM);
816 if (!strcasecmp(towhere, "_BITBUCKET_"))
817 strcpy(towhere, config.c_twitroom);
820 /* First try a regular match */
821 c = getroom(&QRscratch, towhere);
823 /* Then try a mailbox name match */
825 MailboxName(augmented_roomname, &CC->usersupp, towhere);
826 c = getroom(&QRscratch, augmented_roomname);
828 strcpy(towhere, augmented_roomname);
831 /* And if the room was found... */
834 /* let internal programs go directly to any room */
835 if (CC->internal_pgm) {
836 usergoto(towhere, 1, NULL, NULL);
840 /* See if there is an existing user/room relationship */
841 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
843 /* normal clients have to pass through security */
844 if (ra & UA_GOTOALLOWED) {
849 if ((QRscratch.QRflags & QR_MAILBOX) &&
850 ((ra & UA_GOTOALLOWED))) {
851 usergoto(towhere, 1, NULL, NULL);
853 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
854 ((ra & UA_KNOWN) == 0) &&
855 (strcasecmp(QRscratch.QRpasswd, password)) &&
856 (CC->usersupp.axlevel < 6)
858 cprintf("%d wrong or missing passwd\n",
859 ERROR + PASSWORD_REQUIRED);
861 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
862 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
863 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
864 ((ra & UA_KNOWN) == 0) &&
865 (CC->usersupp.axlevel < 6)
867 lprintf(9, "Failed to acquire private room\n");
870 usergoto(towhere, 1, NULL, NULL);
876 NOPE: cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
882 struct usersupp temp;
883 struct cdbdata *cdbus;
885 getuser(&CC->usersupp, CC->curr_user);
886 if (CtdlAccessCheck(ac_room_aide)) return;
888 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
889 cdb_rewind(CDB_USERSUPP);
890 while (cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
891 memset(&temp, 0, sizeof temp);
892 memcpy(&temp, cdbus->ptr, sizeof temp);
895 if ((CC->quickroom.QRflags & QR_INUSE)
896 && (CtdlRoomAccess(&CC->quickroom, &temp) & UA_KNOWN)
898 cprintf("%s\n", temp.fullname);
905 * RDIR command for room directory
915 if (CtdlAccessCheck(ac_logged_in)) return;
917 getroom(&CC->quickroom, CC->quickroom.QRname);
918 getuser(&CC->usersupp, CC->curr_user);
920 if ((CC->quickroom.QRflags & QR_DIRECTORY) == 0) {
921 cprintf("%d not here.\n", ERROR + NOT_HERE);
924 if (((CC->quickroom.QRflags & QR_VISDIR) == 0)
925 && (CC->usersupp.axlevel < 6)
926 && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
927 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
930 cprintf("%d %s|%s/files/%s\n",
931 LISTING_FOLLOWS, config.c_fqdn, BBSDIR, CC->quickroom.QRdirname);
933 sprintf(buf, "ls %s/files/%s >%s 2> /dev/null",
934 BBSDIR, CC->quickroom.QRdirname, CC->temp);
937 sprintf(buf, "%s/files/%s/filedir", BBSDIR, CC->quickroom.QRdirname);
938 fd = fopen(buf, "r");
940 fd = fopen("/dev/null", "r");
942 ls = fopen(CC->temp, "r");
943 while (fgets(flnm, sizeof flnm, ls) != NULL) {
944 flnm[strlen(flnm) - 1] = 0;
945 if (strcasecmp(flnm, "filedir")) {
946 sprintf(buf, "%s/files/%s/%s",
947 BBSDIR, CC->quickroom.QRdirname, flnm);
951 while ((fgets(buf, sizeof buf, fd) != NULL)
952 && (strlen(comment) == 0)) {
953 buf[strlen(buf) - 1] = 0;
954 if ((!strncasecmp(buf, flnm, strlen(flnm)))
955 && (buf[strlen(flnm)] == ' '))
957 &buf[strlen(flnm) + 1],
960 cprintf("%s|%ld|%s\n", flnm, (long)statbuf.st_size, comment);
971 * get room parameters (aide or room aide command)
975 if (CtdlAccessCheck(ac_room_aide)) return;
978 if (is_noneditable(&CC->quickroom)) {
979 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
984 getroom(&CC->quickroom, CC->quickroom.QRname);
985 cprintf("%d%c%s|%s|%s|%d|%d|%d\n",
986 OK, CtdlCheckExpress(),
987 CC->quickroom.QRname,
988 ((CC->quickroom.QRflags & QR_PASSWORDED) ? CC->quickroom.QRpasswd : ""),
989 ((CC->quickroom.QRflags & QR_DIRECTORY) ? CC->quickroom.QRdirname : ""),
990 CC->quickroom.QRflags,
991 (int) CC->quickroom.QRfloor,
992 (int) CC->quickroom.QRorder);
997 * set room parameters (aide or room aide command)
999 void cmd_setr(char *args)
1004 char old_name[ROOMNAMELEN];
1009 if (CtdlAccessCheck(ac_room_aide)) return;
1011 if (is_noneditable(&CC->quickroom)) {
1016 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
1022 if (num_parms(args) >= 6) {
1023 fl = cgetfloor(extract_int(args, 5));
1024 if ((fl->f_flags & F_INUSE) == 0) {
1025 cprintf("%d Invalid floor number.\n",
1026 ERROR + INVALID_FLOOR_OPERATION);
1030 if (num_parms(args) >= 7) {
1031 new_order = extract_int(args, 6);
1034 if (new_order > 127)
1037 lgetroom(&CC->quickroom, CC->quickroom.QRname);
1039 /* Non-editable base rooms can't be renamed */
1040 strcpy(old_name, CC->quickroom.QRname);
1042 extract(buf, args, 0);
1043 buf[ROOMNAMELEN] = 0;
1044 safestrncpy(CC->quickroom.QRname, buf,
1045 sizeof CC->quickroom.QRname);
1048 extract(buf, args, 1);
1050 safestrncpy(CC->quickroom.QRpasswd, buf, sizeof CC->quickroom.QRpasswd);
1051 extract(buf, args, 2);
1053 safestrncpy(CC->quickroom.QRdirname, buf,
1054 sizeof CC->quickroom.QRdirname);
1055 CC->quickroom.QRflags = (extract_int(args, 3) | QR_INUSE);
1056 if (num_parms(args) >= 7)
1057 CC->quickroom.QRorder = (char) new_order;
1059 /* Clean up a client boo-boo: if the client set the room to
1060 * guess-name or passworded, ensure that the private flag is
1063 if ((CC->quickroom.QRflags & QR_GUESSNAME)
1064 || (CC->quickroom.QRflags & QR_PASSWORDED))
1065 CC->quickroom.QRflags |= QR_PRIVATE;
1067 /* Kick everyone out if the client requested it (by changing the
1068 * room's generation number)
1070 if (extract_int(args, 4)) {
1071 time(&CC->quickroom.QRgen);
1073 old_floor = CC->quickroom.QRfloor;
1074 if (num_parms(args) >= 6) {
1075 CC->quickroom.QRfloor = extract_int(args, 5);
1077 /* Write the room record back to disk */
1078 lputroom(&CC->quickroom);
1080 /* If the room name changed, then there are now two room records,
1081 * so we have to delete the old one.
1083 if (strcasecmp(CC->quickroom.QRname, old_name)) {
1084 b_deleteroom(old_name);
1086 /* adjust the floor reference counts */
1087 lgetfloor(&flbuf, old_floor);
1088 --flbuf.f_ref_count;
1089 lputfloor(&flbuf, old_floor);
1090 lgetfloor(&flbuf, CC->quickroom.QRfloor);
1091 ++flbuf.f_ref_count;
1092 lputfloor(&flbuf, CC->quickroom.QRfloor);
1094 /* create a room directory if necessary */
1095 if (CC->quickroom.QRflags & QR_DIRECTORY) {
1097 "mkdir ./files/%s </dev/null >/dev/null 2>/dev/null",
1098 CC->quickroom.QRdirname);
1101 sprintf(buf, "%s> edited by %s\n", CC->quickroom.QRname, CC->curr_user);
1103 cprintf("%d Ok\n", OK);
1109 * get the name of the room aide for this room
1113 struct usersupp usbuf;
1115 if (CtdlAccessCheck(ac_logged_in)) return;
1117 if (is_noneditable(&CC->quickroom)) {
1118 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
1121 if (getuserbynumber(&usbuf, CC->quickroom.QRroomaide) == 0) {
1122 cprintf("%d %s\n", OK, usbuf.fullname);
1124 cprintf("%d \n", OK);
1130 * set the room aide for this room
1132 void cmd_seta(char *new_ra)
1134 struct usersupp usbuf;
1139 if (CtdlAccessCheck(ac_room_aide)) return;
1141 if (getuser(&usbuf, new_ra) != 0) {
1144 newu = usbuf.usernum;
1147 lgetroom(&CC->quickroom, CC->quickroom.QRname);
1149 if (CC->quickroom.QRroomaide != newu) {
1152 CC->quickroom.QRroomaide = newu;
1153 lputroom(&CC->quickroom);
1156 * We have to post the change notice _after_ writing changes to
1157 * the room table, otherwise it would deadlock!
1159 if (post_notice == 1) {
1160 sprintf(buf, "%s is now room aide for %s>\n",
1161 usbuf.fullname, CC->quickroom.QRname);
1164 cprintf("%d Ok\n", OK);
1168 * Generate an associated file name for a room
1170 void assoc_file_name(char *buf, struct quickroom *qrbuf, char *prefix)
1172 sprintf(buf, "./%s/%ld", prefix, qrbuf->QRnumber);
1176 * retrieve info file for this room
1184 assoc_file_name(filename, &CC->quickroom, "info");
1185 info_fp = fopen(filename, "r");
1187 if (info_fp == NULL) {
1188 cprintf("%d No info file.\n", ERROR);
1191 cprintf("%d Info:\n", LISTING_FOLLOWS);
1192 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1193 if (strlen(buf) > 0)
1194 buf[strlen(buf) - 1] = 0;
1195 cprintf("%s\n", buf);
1202 * Back end processing to delete a room and everything associated with it
1204 void delete_room(struct quickroom *qrbuf)
1209 lprintf(9, "Deleting room <%s>\n", qrbuf->QRname);
1211 /* Delete the info file */
1212 assoc_file_name(filename, qrbuf, "info");
1215 /* Delete the image file */
1216 assoc_file_name(filename, qrbuf, "images");
1219 /* Delete the room's network config file */
1220 assoc_file_name(filename, qrbuf, "netconfigs");
1223 /* Delete the messages in the room
1224 * (Careful: this opens an S_QUICKROOM critical section!)
1226 CtdlDeleteMessages(qrbuf->QRname, 0L, "");
1228 /* Flag the room record as not in use */
1229 lgetroom(qrbuf, qrbuf->QRname);
1233 /* then decrement the reference count for the floor */
1234 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1235 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1236 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1238 /* Delete the room record from the database! */
1239 b_deleteroom(qrbuf->QRname);
1245 * Check access control for deleting a room
1247 int CtdlDoIHavePermissionToDeleteThisRoom(struct quickroom *qr) {
1249 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1253 if (is_noneditable(qr)) {
1258 * For mailboxes, check stuff
1260 if (qr->QRflags & QR_MAILBOX) {
1262 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1264 if (atol(qr->QRname) != CC->usersupp.usernum) {
1265 return(0); /* not my room */
1268 /* Can't delete your Mail> room */
1269 if (!strcasecmp(&qr->QRname[12], MAILROOM)) return(0);
1271 /* Otherwise it's ok */
1276 * For normal rooms, just check for aide or room aide status.
1279 return(is_room_aide());
1282 /* Should never get to this point, but to keep the compiler quiet... */
1287 * aide command: kill the current room
1289 void cmd_kill(char *argbuf)
1292 char deleted_room_name[ROOMNAMELEN];
1295 kill_ok = extract_int(argbuf, 0);
1297 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->quickroom) == 0) {
1298 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1302 strcpy(deleted_room_name, CC->quickroom.QRname);
1303 delete_room(&CC->quickroom); /* Do the dirty work */
1304 usergoto(BASEROOM, 0, NULL, NULL); /* Return to the Lobby */
1306 /* tell the world what we did */
1307 sprintf(aaa, "%s> killed by %s\n",
1308 deleted_room_name, CC->curr_user);
1310 cprintf("%d '%s' deleted.\n", OK, deleted_room_name);
1312 cprintf("%d ok to delete.\n", OK);
1318 * Internal code to create a new room (returns room flags)
1320 * Room types: 0=public, 1=guessname, 2=passworded, 3=inv-only,
1321 * 4=mailbox, 5=mailbox, but caller supplies namespace
1323 unsigned create_room(char *new_room_name,
1325 char *new_room_pass,
1330 struct quickroom qrbuf;
1334 lprintf(9, "create_room(%s)\n", new_room_name);
1335 if (getroom(&qrbuf, new_room_name) == 0) {
1336 lprintf(9, "%s already exists.\n", new_room_name);
1337 return (0); /* already exists */
1341 memset(&qrbuf, 0, sizeof(struct quickroom));
1342 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1343 qrbuf.QRflags = QR_INUSE;
1344 if (new_room_type > 0)
1345 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1346 if (new_room_type == 1)
1347 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1348 if (new_room_type == 2)
1349 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1350 if ( (new_room_type == 4) || (new_room_type == 5) )
1351 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1353 /* If the user is requesting a personal room, set up the room
1354 * name accordingly (prepend the user number)
1356 if (new_room_type == 4) {
1357 MailboxName(qrbuf.QRname, &CC->usersupp, new_room_name);
1360 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1363 /* If the room is private, and the system administrator has elected
1364 * to automatically grant room aide privileges, do so now; otherwise,
1365 * set the room aide to undefined.
1367 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1368 qrbuf.QRroomaide = CC->usersupp.usernum;
1370 qrbuf.QRroomaide = (-1L);
1374 * If the caller is only interested in testing whether this will work,
1375 * return now without creating the room.
1377 if (!really_create) return (qrbuf.QRflags);
1379 qrbuf.QRnumber = get_new_room_number();
1380 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1381 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1382 qrbuf.QRfloor = new_room_floor;
1384 /* save what we just did... */
1387 /* bump the reference count on whatever floor the room is on */
1388 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1389 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1390 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1392 /* be sure not to kick the creator out of the room! */
1393 lgetuser(&CC->usersupp, CC->curr_user);
1394 CtdlGetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1395 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1396 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1397 CtdlSetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1398 lputuser(&CC->usersupp);
1400 /* resume our happy day */
1401 return (qrbuf.QRflags);
1408 void cmd_cre8(char *args)
1411 char new_room_name[SIZ];
1413 char new_room_pass[SIZ];
1419 cre8_ok = extract_int(args, 0);
1420 extract(new_room_name, args, 1);
1421 new_room_name[ROOMNAMELEN - 1] = 0;
1422 new_room_type = extract_int(args, 2);
1423 extract(new_room_pass, args, 3);
1424 new_room_pass[9] = 0;
1427 if ((strlen(new_room_name) == 0) && (cre8_ok == 1)) {
1428 cprintf("%d Invalid room name.\n", ERROR);
1432 if (!strcasecmp(new_room_name, MAILROOM)) {
1433 cprintf("%d '%s' already exists.\n",
1434 ERROR + ALREADY_EXISTS, new_room_name);
1438 if (num_parms(args) >= 5) {
1439 fl = cgetfloor(extract_int(args, 4));
1440 if ((fl->f_flags & F_INUSE) == 0) {
1441 cprintf("%d Invalid floor number.\n",
1442 ERROR + INVALID_FLOOR_OPERATION);
1445 new_room_floor = extract_int(args, 4);
1449 if (CtdlAccessCheck(ac_logged_in)) return;
1451 if (CC->usersupp.axlevel < config.c_createax) {
1452 cprintf("%d You need higher access to create rooms.\n",
1453 ERROR + HIGHER_ACCESS_REQUIRED);
1457 if ((strlen(new_room_name) == 0) && (cre8_ok == 0)) {
1458 cprintf("%d Ok to create rooms.\n", OK);
1462 if ((new_room_type < 0) || (new_room_type > 4)) {
1463 cprintf("%d Invalid room type.\n", ERROR);
1467 /* Check to make sure the requested room name doesn't already exist */
1468 newflags = create_room(new_room_name,
1469 new_room_type, new_room_pass, new_room_floor, 0);
1470 if (newflags == 0) {
1471 cprintf("%d '%s' already exists.\n",
1472 ERROR + ALREADY_EXISTS, new_room_name);
1477 cprintf("%d OK to create '%s'\n", OK, new_room_name);
1481 /* If we reach this point, the room needs to be created. */
1483 newflags = create_room(new_room_name,
1484 new_room_type, new_room_pass, new_room_floor, 1);
1486 /* post a message in Aide> describing the new room */
1487 safestrncpy(aaa, new_room_name, sizeof aaa);
1488 strcat(aaa, "> created by ");
1489 strcat(aaa, CC->usersupp.fullname);
1490 if (newflags & QR_MAILBOX)
1491 strcat(aaa, " [personal]");
1492 else if (newflags & QR_PRIVATE)
1493 strcat(aaa, " [private]");
1494 if (newflags & QR_GUESSNAME)
1495 strcat(aaa, "[guessname] ");
1496 if (newflags & QR_PASSWORDED) {
1497 strcat(aaa, "\n Password: ");
1498 strcat(aaa, new_room_pass);
1503 cprintf("%d '%s' has been created.\n", OK, new_room_name);
1508 void cmd_einf(char *ok)
1509 { /* enter info file for current room */
1511 char infofilename[SIZ];
1514 if (CtdlAccessCheck(ac_room_aide)) return;
1516 if (atoi(ok) == 0) {
1517 cprintf("%d Ok.\n", OK);
1520 assoc_file_name(infofilename, &CC->quickroom, "info");
1521 lprintf(9, "opening\n");
1522 fp = fopen(infofilename, "w");
1523 lprintf(9, "checking\n");
1525 cprintf("%d Cannot open %s: %s\n",
1526 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1529 cprintf("%d Send info...\n", SEND_LISTING);
1533 if (strcmp(buf, "000"))
1534 fprintf(fp, "%s\n", buf);
1535 } while (strcmp(buf, "000"));
1538 /* now update the room index so people will see our new info */
1539 lgetroom(&CC->quickroom, CC->quickroom.QRname); /* lock so no one steps on us */
1540 CC->quickroom.QRinfo = CC->quickroom.QRhighest + 1L;
1541 lputroom(&CC->quickroom);
1546 * cmd_lflr() - List all known floors
1553 if (CtdlAccessCheck(ac_logged_in)) return;
1555 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1557 for (a = 0; a < MAXFLOORS; ++a) {
1558 getfloor(&flbuf, a);
1559 if (flbuf.f_flags & F_INUSE) {
1560 cprintf("%d|%s|%d\n",
1572 * create a new floor
1574 void cmd_cflr(char *argbuf)
1576 char new_floor_name[SIZ];
1579 int free_slot = (-1);
1582 extract(new_floor_name, argbuf, 0);
1583 cflr_ok = extract_int(argbuf, 1);
1586 if (CtdlAccessCheck(ac_aide)) return;
1588 for (a = 0; a < MAXFLOORS; ++a) {
1589 getfloor(&flbuf, a);
1591 /* note any free slots while we're scanning... */
1592 if (((flbuf.f_flags & F_INUSE) == 0)
1596 /* check to see if it already exists */
1597 if ((!strcasecmp(flbuf.f_name, new_floor_name))
1598 && (flbuf.f_flags & F_INUSE)) {
1599 cprintf("%d Floor '%s' already exists.\n",
1600 ERROR + ALREADY_EXISTS,
1606 if (free_slot < 0) {
1607 cprintf("%d There is no space available for a new floor.\n",
1608 ERROR + INVALID_FLOOR_OPERATION);
1612 cprintf("%d ok to create...\n", OK);
1615 lgetfloor(&flbuf, free_slot);
1616 flbuf.f_flags = F_INUSE;
1617 flbuf.f_ref_count = 0;
1618 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
1619 lputfloor(&flbuf, free_slot);
1620 cprintf("%d %d\n", OK, free_slot);
1628 void cmd_kflr(char *argbuf)
1631 int floor_to_delete;
1635 floor_to_delete = extract_int(argbuf, 0);
1636 kflr_ok = extract_int(argbuf, 1);
1638 if (CtdlAccessCheck(ac_aide)) return;
1640 lgetfloor(&flbuf, floor_to_delete);
1643 if ((flbuf.f_flags & F_INUSE) == 0) {
1644 cprintf("%d Floor %d not in use.\n",
1645 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
1648 if (flbuf.f_ref_count != 0) {
1649 cprintf("%d Cannot delete; floor contains %d rooms.\n",
1650 ERROR + INVALID_FLOOR_OPERATION,
1655 cprintf("%d Ok\n", OK);
1657 cprintf("%d Ok to delete...\n", OK);
1664 if ((delete_ok == 1) && (kflr_ok == 1))
1666 lputfloor(&flbuf, floor_to_delete);
1672 void cmd_eflr(char *argbuf)
1678 np = num_parms(argbuf);
1680 cprintf("%d Usage error.\n", ERROR);
1684 if (CtdlAccessCheck(ac_aide)) return;
1686 floor_num = extract_int(argbuf, 0);
1687 lgetfloor(&flbuf, floor_num);
1688 if ((flbuf.f_flags & F_INUSE) == 0) {
1689 lputfloor(&flbuf, floor_num);
1690 cprintf("%d Floor %d is not in use.\n",
1691 ERROR + INVALID_FLOOR_OPERATION, floor_num);
1695 extract(flbuf.f_name, argbuf, 1);
1696 lputfloor(&flbuf, floor_num);
1698 cprintf("%d Ok\n", OK);