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 /* for internal programs, always do everything */
57 if (((CC->internal_pgm)) && (roombuf->QRflags & QR_INUSE)) {
58 return (UA_KNOWN | UA_GOTOALLOWED);
61 /* Locate any applicable user/room relationships */
62 CtdlGetRelationship(&vbuf, userbuf, roombuf);
64 /* Force the properties of the Aide room */
65 if (!strcasecmp(roombuf->QRname, AIDEROOM)) {
66 if (userbuf->axlevel >= 6) {
67 retval = UA_KNOWN | UA_GOTOALLOWED;
74 /* If this is a public room, it's accessible... */
75 if ( ((roombuf->QRflags & QR_PRIVATE) == 0)
76 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
77 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
80 /* If this is a preferred users only room, check access level */
81 if (roombuf->QRflags & QR_PREFONLY) {
82 if (userbuf->axlevel < 5) {
83 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
87 /* For private rooms, check the generation number matchups */
88 if ( (roombuf->QRflags & QR_PRIVATE)
89 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
91 /* An explicit match means the user belongs in this room */
92 if (vbuf.v_flags & V_ACCESS) {
93 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
95 /* Otherwise, check if this is a guess-name or passworded
96 * room. If it is, a goto may at least be attempted
98 else if ((roombuf->QRflags & QR_PRIVATE)
99 || (roombuf->QRflags & QR_PASSWORDED)) {
100 retval = retval & ~UA_KNOWN;
101 retval = retval | UA_GOTOALLOWED;
105 /* For mailbox rooms, also check the generation number matchups */
106 if (roombuf->QRflags & QR_MAILBOX) {
107 if (userbuf->usernum == atol(roombuf->QRname)) {
108 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
110 /* An explicit match means the user belongs in this room */
111 if (vbuf.v_flags & V_ACCESS) {
112 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
116 /* Check to see if the user has forgotten this room */
117 if (vbuf.v_flags & V_FORGET) {
118 retval = retval & ~UA_KNOWN;
119 if ( ( ((roombuf->QRflags & QR_PRIVATE) == 0)
120 && ((roombuf->QRflags & QR_MAILBOX) == 0) )
121 || ( (roombuf->QRflags & QR_MAILBOX)
122 && (atol(roombuf->QRname) == CC->usersupp.usernum))) {
123 retval = retval | UA_ZAPPED;
126 /* If user is explicitly locked out of this room, deny everything */
127 if (vbuf.v_flags & V_LOCKOUT) {
128 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
131 /* Aides get access to everything */
132 if (userbuf->axlevel >= 6) {
133 if (vbuf.v_flags & V_FORGET) {
134 retval = retval | UA_GOTOALLOWED;
137 retval = retval | UA_GOTOALLOWED;
138 if ((roombuf->QRflags & QR_MAILBOX) == 0) {
139 retval = retval | UA_KNOWN;
144 NEWMSG: /* By the way, we also check for the presence of new messages */
145 if (is_msg_in_mset(vbuf.v_seen, roombuf->QRhighest) == 0) {
146 retval = retval | UA_HASNEWMSGS;
149 /* System rooms never show up in the list. */
150 if (roombuf->QRflags2 & QR2_SYSTEM) {
151 retval = retval & ~UA_KNOWN;
157 * Self-checking stuff for a room record read into memory
159 void room_sanity_check(struct quickroom *qrbuf)
161 /* Mailbox rooms are always on the lowest floor */
162 if (qrbuf->QRflags & QR_MAILBOX) {
165 /* Listing order of 0 is illegal except for base rooms */
166 if (qrbuf->QRorder == 0)
167 if (!is_noneditable(qrbuf))
173 * getroom() - retrieve room data from disk
175 int getroom(struct quickroom *qrbuf, char *room_name)
177 struct cdbdata *cdbqr;
178 char lowercase_name[ROOMNAMELEN];
179 char personal_lowercase_name[ROOMNAMELEN];
182 for (a = 0; room_name[a] && a < sizeof lowercase_name - 1; ++a) {
183 lowercase_name[a] = tolower(room_name[a]);
185 lowercase_name[a] = 0;
187 memset(qrbuf, 0, sizeof(struct quickroom));
189 /* First, try the public namespace */
190 cdbqr = cdb_fetch(CDB_QUICKROOM,
191 lowercase_name, strlen(lowercase_name));
193 /* If that didn't work, try the user's personal namespace */
195 sprintf(personal_lowercase_name, "%010ld.%s",
196 CC->usersupp.usernum, lowercase_name);
197 cdbqr = cdb_fetch(CDB_QUICKROOM,
198 personal_lowercase_name,
199 strlen(personal_lowercase_name));
202 memcpy(qrbuf, cdbqr->ptr,
203 ((cdbqr->len > sizeof(struct quickroom)) ?
204 sizeof(struct quickroom) : cdbqr->len));
207 room_sanity_check(qrbuf);
216 * lgetroom() - same as getroom() but locks the record (if supported)
218 int lgetroom(struct quickroom *qrbuf, char *room_name)
221 retval = getroom(qrbuf, room_name);
222 if (retval == 0) begin_critical_section(S_QUICKROOM);
228 * b_putroom() - back end to putroom() and b_deleteroom()
229 * (if the supplied buffer is NULL, delete the room record)
231 void b_putroom(struct quickroom *qrbuf, char *room_name)
233 char lowercase_name[ROOMNAMELEN];
236 for (a = 0; a <= strlen(room_name); ++a) {
237 lowercase_name[a] = tolower(room_name[a]);
241 cdb_delete(CDB_QUICKROOM,
242 lowercase_name, strlen(lowercase_name));
244 time(&qrbuf->QRmtime);
245 cdb_store(CDB_QUICKROOM,
246 lowercase_name, strlen(lowercase_name),
247 qrbuf, sizeof(struct quickroom));
253 * putroom() - store room data to disk
255 void putroom(struct quickroom *qrbuf) {
256 b_putroom(qrbuf, qrbuf->QRname);
261 * b_deleteroom() - delete a room record from disk
263 void b_deleteroom(char *room_name) {
264 b_putroom(NULL, room_name);
270 * lputroom() - same as putroom() but unlocks the record (if supported)
272 void lputroom(struct quickroom *qrbuf)
276 end_critical_section(S_QUICKROOM);
280 /****************************************************************************/
283 * getfloor() - retrieve floor data from disk
285 void getfloor(struct floor *flbuf, int floor_num)
287 struct cdbdata *cdbfl;
289 memset(flbuf, 0, sizeof(struct floor));
290 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
292 memcpy(flbuf, cdbfl->ptr,
293 ((cdbfl->len > sizeof(struct floor)) ?
294 sizeof(struct floor) : cdbfl->len));
297 if (floor_num == 0) {
298 strcpy(flbuf->f_name, "Main Floor");
299 flbuf->f_flags = F_INUSE;
300 flbuf->f_ref_count = 3;
307 * lgetfloor() - same as getfloor() but locks the record (if supported)
309 void lgetfloor(struct floor *flbuf, int floor_num)
312 begin_critical_section(S_FLOORTAB);
313 getfloor(flbuf, floor_num);
318 * cgetfloor() - Get floor record from *cache* (loads from disk if needed)
320 * This is strictly a performance hack.
322 struct floor *cgetfloor(int floor_num) {
323 static int initialized = 0;
326 if (initialized == 0) {
327 for (i=0; i<MAXFLOORS; ++i) {
328 floorcache[floor_num] = NULL;
333 if (floorcache[floor_num] == NULL) {
334 floorcache[floor_num] = mallok(sizeof(struct floor));
335 getfloor(floorcache[floor_num], floor_num);
338 return(floorcache[floor_num]);
344 * putfloor() - store floor data on disk
346 void putfloor(struct floor *flbuf, int floor_num)
348 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
349 flbuf, sizeof(struct floor));
351 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
352 if (floorcache[floor_num] != NULL) {
353 phree(floorcache[floor_num]);
354 floorcache[floor_num] = NULL;
360 * lputfloor() - same as putfloor() but unlocks the record (if supported)
362 void lputfloor(struct floor *flbuf, int floor_num)
365 putfloor(flbuf, floor_num);
366 end_critical_section(S_FLOORTAB);
372 * Traverse the room file...
374 void ForEachRoom(void (*CallBack) (struct quickroom *EachRoom, void *out_data),
377 struct quickroom qrbuf;
378 struct cdbdata *cdbqr;
380 cdb_rewind(CDB_QUICKROOM);
382 while (cdbqr = cdb_next_item(CDB_QUICKROOM), cdbqr != NULL) {
383 memset(&qrbuf, 0, sizeof(struct quickroom));
384 memcpy(&qrbuf, cdbqr->ptr,
385 ((cdbqr->len > sizeof(struct quickroom)) ?
386 sizeof(struct quickroom) : cdbqr->len));
388 room_sanity_check(&qrbuf);
389 if (qrbuf.QRflags & QR_INUSE)
390 (*CallBack)(&qrbuf, in_data);
396 * delete_msglist() - delete room message pointers
398 void delete_msglist(struct quickroom *whichroom)
400 struct cdbdata *cdbml;
402 /* Make sure the msglist we're deleting actually exists, otherwise
403 * gdbm will complain when we try to delete an invalid record
405 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
409 /* Go ahead and delete it */
410 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
418 * sort message pointers
419 * (returns new msg count)
421 int sort_msglist(long listptrs[], int oldcount)
432 for (a = numitems - 2; a >= 0; --a) {
433 for (b = 0; b <= a; ++b) {
434 if (listptrs[b] > (listptrs[b + 1])) {
436 hold2 = listptrs[b + 1];
438 listptrs[b + 1] = hold1;
443 /* and yank any nulls */
444 while ((numitems > 0) && (listptrs[0] == 0L)) {
445 memcpy(&listptrs[0], &listptrs[1],
446 (sizeof(long) * (numitems - 1)));
455 * Determine whether a given room is non-editable.
457 int is_noneditable(struct quickroom *qrbuf)
460 /* Lobby> and Aide> are non-editable */
461 if (!strcasecmp(qrbuf->QRname, BASEROOM))
463 else if (!strcasecmp(qrbuf->QRname, AIDEROOM))
466 /* Mail> rooms are also non-editable */
467 else if ( (qrbuf->QRflags & QR_MAILBOX)
468 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
471 /* Everything else is editable */
479 * Back-back-end for all room listing commands
481 void list_roomname(struct quickroom *qrbuf)
483 char truncated_roomname[ROOMNAMELEN];
485 /* For my own mailbox rooms, chop off the owner prefix */
486 if ( (qrbuf->QRflags & QR_MAILBOX)
487 && (atol(qrbuf->QRname) == CC->usersupp.usernum) ) {
488 strcpy(truncated_roomname, qrbuf->QRname);
489 strcpy(truncated_roomname, &truncated_roomname[11]);
490 cprintf("%s", truncated_roomname);
492 /* For all other rooms, just display the name in its entirety */
494 cprintf("%s", qrbuf->QRname);
497 /* ...and now the other parameters */
498 cprintf("|%u|%d|%d\n",
500 (int) qrbuf->QRfloor,
501 (int) qrbuf->QRorder);
506 * cmd_lrms() - List all accessible rooms, known or forgotten
508 void cmd_lrms_backend(struct quickroom *qrbuf, void *data)
510 int FloorBeingSearched = (-1);
511 FloorBeingSearched = *(int *)data;
513 if (((CtdlRoomAccess(qrbuf, &CC->usersupp)
514 & (UA_KNOWN | UA_ZAPPED)))
515 && ((qrbuf->QRfloor == (FloorBeingSearched))
516 || ((FloorBeingSearched) < 0)))
517 list_roomname(qrbuf);
520 void cmd_lrms(char *argbuf)
522 int FloorBeingSearched = (-1);
523 if (strlen(argbuf) > 0)
524 FloorBeingSearched = extract_int(argbuf, 0);
526 if (CtdlAccessCheck(ac_logged_in)) return;
528 if (getuser(&CC->usersupp, CC->curr_user)) {
529 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
532 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
534 ForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
541 * cmd_lkra() - List all known rooms
543 void cmd_lkra_backend(struct quickroom *qrbuf, void *data)
545 int FloorBeingSearched = (-1);
546 FloorBeingSearched = *(int *)data;
548 if (((CtdlRoomAccess(qrbuf, &CC->usersupp)
550 && ((qrbuf->QRfloor == (FloorBeingSearched))
551 || ((FloorBeingSearched) < 0)))
552 list_roomname(qrbuf);
555 void cmd_lkra(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->usersupp, CC->curr_user)) {
564 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
567 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
569 ForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
576 * cmd_lkrn() - List all known rooms with new messages
578 void cmd_lkrn_backend(struct quickroom *qrbuf, void *data)
581 int FloorBeingSearched = (-1);
582 FloorBeingSearched = *(int *)data;
584 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
586 && (ra & UA_HASNEWMSGS)
587 && ((qrbuf->QRfloor == (FloorBeingSearched))
588 || ((FloorBeingSearched) < 0)))
589 list_roomname(qrbuf);
592 void cmd_lkrn(char *argbuf)
594 int FloorBeingSearched = (-1);
595 if (strlen(argbuf) > 0)
596 FloorBeingSearched = extract_int(argbuf, 0);
598 if (CtdlAccessCheck(ac_logged_in)) return;
600 if (getuser(&CC->usersupp, CC->curr_user)) {
601 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
604 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
606 ForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
613 * cmd_lkro() - List all known rooms
615 void cmd_lkro_backend(struct quickroom *qrbuf, void *data)
618 int FloorBeingSearched = (-1);
619 FloorBeingSearched = *(int *)data;
621 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
623 && ((ra & UA_HASNEWMSGS) == 0)
624 && ((qrbuf->QRfloor == (FloorBeingSearched))
625 || ((FloorBeingSearched) < 0)))
626 list_roomname(qrbuf);
629 void cmd_lkro(char *argbuf)
631 int FloorBeingSearched = (-1);
632 if (strlen(argbuf) > 0)
633 FloorBeingSearched = extract_int(argbuf, 0);
635 if (CtdlAccessCheck(ac_logged_in)) return;
637 if (getuser(&CC->usersupp, CC->curr_user)) {
638 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
641 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
643 ForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
650 * cmd_lzrm() - List all forgotten rooms
652 void cmd_lzrm_backend(struct quickroom *qrbuf, void *data)
655 int FloorBeingSearched = (-1);
656 FloorBeingSearched = *(int *)data;
658 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
659 if ((ra & UA_GOTOALLOWED)
661 && ((qrbuf->QRfloor == (FloorBeingSearched))
662 || ((FloorBeingSearched) < 0)))
663 list_roomname(qrbuf);
666 void cmd_lzrm(char *argbuf)
668 int FloorBeingSearched = (-1);
669 if (strlen(argbuf) > 0)
670 FloorBeingSearched = extract_int(argbuf, 0);
672 if (CtdlAccessCheck(ac_logged_in)) return;
674 if (getuser(&CC->usersupp, CC->curr_user)) {
675 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
678 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
680 ForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
686 void usergoto(char *where, int display_result, int *retmsgs, int *retnew)
689 int new_messages = 0;
690 int total_messages = 0;
694 int newmailcount = 0;
696 char truncated_roomname[ROOMNAMELEN];
697 struct cdbdata *cdbfr;
698 long *msglist = NULL;
701 strcpy(CC->quickroom.QRname, where);
702 getroom(&CC->quickroom, where);
704 lgetuser(&CC->usersupp, CC->curr_user);
705 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
707 /* Know the room ... but not if it's the page log room */
708 if (strcasecmp(where, config.c_logpages)) {
709 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
710 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
712 CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
713 lputuser(&CC->usersupp);
715 /* check for new mail */
716 newmailcount = NewMailCount();
718 /* set info to 1 if the user needs to read the room's info file */
719 if (CC->quickroom.QRinfo > vbuf.v_lastseen)
723 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->quickroom.QRnumber, sizeof(long));
725 msglist = mallok(cdbfr->len);
726 memcpy(msglist, cdbfr->ptr, cdbfr->len);
727 num_msgs = cdbfr->len / sizeof(long);
731 if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
732 if (msglist[a] > 0L) {
734 if (is_msg_in_mset(vbuf.v_seen, msglist[a]) == 0) {
740 if (msglist != NULL) phree(msglist);
742 if (CC->quickroom.QRflags & QR_MAILBOX)
747 if ((CC->quickroom.QRroomaide == CC->usersupp.usernum)
748 || (CC->usersupp.axlevel >= 6))
753 strcpy(truncated_roomname, CC->quickroom.QRname);
754 if ( (CC->quickroom.QRflags & QR_MAILBOX)
755 && (atol(CC->quickroom.QRname) == CC->usersupp.usernum) ) {
756 strcpy(truncated_roomname, &truncated_roomname[11]);
759 if (retmsgs != NULL) *retmsgs = total_messages;
760 if (retnew != NULL) *retnew = new_messages;
761 lprintf(9, "<%s> %d new of %d total messages\n",
762 CC->quickroom.QRname, new_messages, total_messages);
765 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d\n",
766 OK, CtdlCheckExpress(),
768 new_messages, total_messages,
769 info, CC->quickroom.QRflags,
770 CC->quickroom.QRhighest,
772 rmailflag, raideflag, newmailcount,
773 CC->quickroom.QRfloor);
779 * cmd_goto() - goto a new room
781 void cmd_goto(char *gargs)
783 struct quickroom QRscratch;
787 char augmented_roomname[SIZ];
791 if (CtdlAccessCheck(ac_logged_in)) return;
793 extract(towhere, gargs, 0);
794 extract(password, gargs, 1);
796 getuser(&CC->usersupp, CC->curr_user);
798 if (!strcasecmp(towhere, "_BASEROOM_"))
799 strcpy(towhere, BASEROOM);
801 if (!strcasecmp(towhere, "_MAIL_"))
802 strcpy(towhere, MAILROOM);
804 if (!strcasecmp(towhere, "_BITBUCKET_"))
805 strcpy(towhere, config.c_twitroom);
808 /* First try a regular match */
809 c = getroom(&QRscratch, towhere);
811 /* Then try a mailbox name match */
813 MailboxName(augmented_roomname, &CC->usersupp, towhere);
814 c = getroom(&QRscratch, augmented_roomname);
816 strcpy(towhere, augmented_roomname);
819 /* And if the room was found... */
822 /* let internal programs go directly to any room */
823 if (CC->internal_pgm) {
824 usergoto(towhere, 1, NULL, NULL);
828 /* See if there is an existing user/room relationship */
829 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
831 /* normal clients have to pass through security */
832 if (ra & UA_GOTOALLOWED) {
837 if ((QRscratch.QRflags & QR_MAILBOX) &&
838 ((ra & UA_GOTOALLOWED))) {
839 usergoto(towhere, 1, NULL, NULL);
841 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
842 ((ra & UA_KNOWN) == 0) &&
843 (strcasecmp(QRscratch.QRpasswd, password)) &&
844 (CC->usersupp.axlevel < 6)
846 cprintf("%d wrong or missing passwd\n",
847 ERROR + PASSWORD_REQUIRED);
849 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
850 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
851 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
852 ((ra & UA_KNOWN) == 0) &&
853 (CC->usersupp.axlevel < 6)
855 lprintf(9, "Failed to acquire private room\n");
858 usergoto(towhere, 1, NULL, NULL);
864 NOPE: cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
870 struct usersupp temp;
871 struct cdbdata *cdbus;
873 getuser(&CC->usersupp, CC->curr_user);
874 if (CtdlAccessCheck(ac_room_aide)) return;
876 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
877 cdb_rewind(CDB_USERSUPP);
878 while (cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
879 memset(&temp, 0, sizeof temp);
880 memcpy(&temp, cdbus->ptr, sizeof temp);
883 if ((CC->quickroom.QRflags & QR_INUSE)
884 && (CtdlRoomAccess(&CC->quickroom, &temp) & UA_KNOWN)
886 cprintf("%s\n", temp.fullname);
893 * RDIR command for room directory
903 if (CtdlAccessCheck(ac_logged_in)) return;
905 getroom(&CC->quickroom, CC->quickroom.QRname);
906 getuser(&CC->usersupp, CC->curr_user);
908 if ((CC->quickroom.QRflags & QR_DIRECTORY) == 0) {
909 cprintf("%d not here.\n", ERROR + NOT_HERE);
912 if (((CC->quickroom.QRflags & QR_VISDIR) == 0)
913 && (CC->usersupp.axlevel < 6)
914 && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
915 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
918 cprintf("%d %s|%s/files/%s\n",
919 LISTING_FOLLOWS, config.c_fqdn, BBSDIR, CC->quickroom.QRdirname);
921 sprintf(buf, "ls %s/files/%s >%s 2> /dev/null",
922 BBSDIR, CC->quickroom.QRdirname, CC->temp);
925 sprintf(buf, "%s/files/%s/filedir", BBSDIR, CC->quickroom.QRdirname);
926 fd = fopen(buf, "r");
928 fd = fopen("/dev/null", "r");
930 ls = fopen(CC->temp, "r");
931 while (fgets(flnm, sizeof flnm, ls) != NULL) {
932 flnm[strlen(flnm) - 1] = 0;
933 if (strcasecmp(flnm, "filedir")) {
934 sprintf(buf, "%s/files/%s/%s",
935 BBSDIR, CC->quickroom.QRdirname, flnm);
939 while ((fgets(buf, sizeof buf, fd) != NULL)
940 && (strlen(comment) == 0)) {
941 buf[strlen(buf) - 1] = 0;
942 if ((!strncasecmp(buf, flnm, strlen(flnm)))
943 && (buf[strlen(flnm)] == ' '))
945 &buf[strlen(flnm) + 1],
948 cprintf("%s|%ld|%s\n", flnm, (long)statbuf.st_size, comment);
959 * get room parameters (aide or room aide command)
963 if (CtdlAccessCheck(ac_room_aide)) return;
966 if (is_noneditable(&CC->quickroom)) {
967 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
972 getroom(&CC->quickroom, CC->quickroom.QRname);
973 cprintf("%d%c%s|%s|%s|%d|%d|%d\n",
974 OK, CtdlCheckExpress(),
975 CC->quickroom.QRname,
976 ((CC->quickroom.QRflags & QR_PASSWORDED) ? CC->quickroom.QRpasswd : ""),
977 ((CC->quickroom.QRflags & QR_DIRECTORY) ? CC->quickroom.QRdirname : ""),
978 CC->quickroom.QRflags,
979 (int) CC->quickroom.QRfloor,
980 (int) CC->quickroom.QRorder);
985 * set room parameters (aide or room aide command)
987 void cmd_setr(char *args)
992 char old_name[ROOMNAMELEN];
997 if (CtdlAccessCheck(ac_room_aide)) return;
999 if (is_noneditable(&CC->quickroom)) {
1004 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
1010 if (num_parms(args) >= 6) {
1011 fl = cgetfloor(extract_int(args, 5));
1012 if ((fl->f_flags & F_INUSE) == 0) {
1013 cprintf("%d Invalid floor number.\n",
1014 ERROR + INVALID_FLOOR_OPERATION);
1018 if (num_parms(args) >= 7) {
1019 new_order = extract_int(args, 6);
1022 if (new_order > 127)
1025 lgetroom(&CC->quickroom, CC->quickroom.QRname);
1027 /* Non-editable base rooms can't be renamed */
1028 strcpy(old_name, CC->quickroom.QRname);
1030 extract(buf, args, 0);
1031 buf[ROOMNAMELEN] = 0;
1032 safestrncpy(CC->quickroom.QRname, buf,
1033 sizeof CC->quickroom.QRname);
1036 extract(buf, args, 1);
1038 safestrncpy(CC->quickroom.QRpasswd, buf, sizeof CC->quickroom.QRpasswd);
1039 extract(buf, args, 2);
1041 safestrncpy(CC->quickroom.QRdirname, buf,
1042 sizeof CC->quickroom.QRdirname);
1043 CC->quickroom.QRflags = (extract_int(args, 3) | QR_INUSE);
1044 if (num_parms(args) >= 7)
1045 CC->quickroom.QRorder = (char) new_order;
1047 /* Clean up a client boo-boo: if the client set the room to
1048 * guess-name or passworded, ensure that the private flag is
1051 if ((CC->quickroom.QRflags & QR_GUESSNAME)
1052 || (CC->quickroom.QRflags & QR_PASSWORDED))
1053 CC->quickroom.QRflags |= QR_PRIVATE;
1055 /* Kick everyone out if the client requested it (by changing the
1056 * room's generation number)
1058 if (extract_int(args, 4)) {
1059 time(&CC->quickroom.QRgen);
1061 old_floor = CC->quickroom.QRfloor;
1062 if (num_parms(args) >= 6) {
1063 CC->quickroom.QRfloor = extract_int(args, 5);
1065 /* Write the room record back to disk */
1066 lputroom(&CC->quickroom);
1068 /* If the room name changed, then there are now two room records,
1069 * so we have to delete the old one.
1071 if (strcasecmp(CC->quickroom.QRname, old_name)) {
1072 b_deleteroom(old_name);
1074 /* adjust the floor reference counts */
1075 lgetfloor(&flbuf, old_floor);
1076 --flbuf.f_ref_count;
1077 lputfloor(&flbuf, old_floor);
1078 lgetfloor(&flbuf, CC->quickroom.QRfloor);
1079 ++flbuf.f_ref_count;
1080 lputfloor(&flbuf, CC->quickroom.QRfloor);
1082 /* create a room directory if necessary */
1083 if (CC->quickroom.QRflags & QR_DIRECTORY) {
1085 "mkdir ./files/%s </dev/null >/dev/null 2>/dev/null",
1086 CC->quickroom.QRdirname);
1089 sprintf(buf, "%s> edited by %s\n", CC->quickroom.QRname, CC->curr_user);
1091 cprintf("%d Ok\n", OK);
1097 * get the name of the room aide for this room
1101 struct usersupp usbuf;
1103 if (CtdlAccessCheck(ac_logged_in)) return;
1105 if (is_noneditable(&CC->quickroom)) {
1106 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
1109 if (getuserbynumber(&usbuf, CC->quickroom.QRroomaide) == 0) {
1110 cprintf("%d %s\n", OK, usbuf.fullname);
1112 cprintf("%d \n", OK);
1118 * set the room aide for this room
1120 void cmd_seta(char *new_ra)
1122 struct usersupp usbuf;
1127 if (CtdlAccessCheck(ac_room_aide)) return;
1129 if (getuser(&usbuf, new_ra) != 0) {
1132 newu = usbuf.usernum;
1135 lgetroom(&CC->quickroom, CC->quickroom.QRname);
1137 if (CC->quickroom.QRroomaide != newu) {
1140 CC->quickroom.QRroomaide = newu;
1141 lputroom(&CC->quickroom);
1144 * We have to post the change notice _after_ writing changes to
1145 * the room table, otherwise it would deadlock!
1147 if (post_notice == 1) {
1148 sprintf(buf, "%s is now room aide for %s>\n",
1149 usbuf.fullname, CC->quickroom.QRname);
1152 cprintf("%d Ok\n", OK);
1156 * Generate an associated file name for a room
1158 void assoc_file_name(char *buf, struct quickroom *qrbuf, char *prefix)
1160 sprintf(buf, "./%s/%ld", prefix, qrbuf->QRnumber);
1164 * retrieve info file for this room
1172 assoc_file_name(filename, &CC->quickroom, "info");
1173 info_fp = fopen(filename, "r");
1175 if (info_fp == NULL) {
1176 cprintf("%d No info file.\n", ERROR);
1179 cprintf("%d Info:\n", LISTING_FOLLOWS);
1180 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1181 if (strlen(buf) > 0)
1182 buf[strlen(buf) - 1] = 0;
1183 cprintf("%s\n", buf);
1190 * Back end processing to delete a room and everything associated with it
1192 void delete_room(struct quickroom *qrbuf)
1197 lprintf(9, "Deleting room <%s>\n", qrbuf->QRname);
1199 /* Delete the info file */
1200 assoc_file_name(filename, qrbuf, "info");
1203 /* Delete the image file */
1204 assoc_file_name(filename, qrbuf, "images");
1207 /* Delete the room's network config file */
1208 assoc_file_name(filename, qrbuf, "netconfigs");
1211 /* Delete the messages in the room
1212 * (Careful: this opens an S_QUICKROOM critical section!)
1214 CtdlDeleteMessages(qrbuf->QRname, 0L, "");
1216 /* Flag the room record as not in use */
1217 lgetroom(qrbuf, qrbuf->QRname);
1221 /* then decrement the reference count for the floor */
1222 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1223 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1224 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1226 /* Delete the room record from the database! */
1227 b_deleteroom(qrbuf->QRname);
1233 * Check access control for deleting a room
1235 int CtdlDoIHavePermissionToDeleteThisRoom(struct quickroom *qr) {
1237 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1241 if (is_noneditable(qr)) {
1246 * For mailboxes, check stuff
1248 if (qr->QRflags & QR_MAILBOX) {
1250 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1252 if (atol(qr->QRname) != CC->usersupp.usernum) {
1253 return(0); /* not my room */
1256 /* Can't delete your Mail> room */
1257 if (!strcasecmp(&qr->QRname[12], MAILROOM)) return(0);
1259 /* Otherwise it's ok */
1264 * For normal rooms, just check for aide or room aide status.
1267 return(is_room_aide());
1270 /* Should never get to this point, but to keep the compiler quiet... */
1275 * aide command: kill the current room
1277 void cmd_kill(char *argbuf)
1280 char deleted_room_name[ROOMNAMELEN];
1283 kill_ok = extract_int(argbuf, 0);
1285 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->quickroom) == 0) {
1286 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1290 strcpy(deleted_room_name, CC->quickroom.QRname);
1291 delete_room(&CC->quickroom); /* Do the dirty work */
1292 usergoto(BASEROOM, 0, NULL, NULL); /* Return to the Lobby */
1294 /* tell the world what we did */
1295 sprintf(aaa, "%s> killed by %s\n",
1296 deleted_room_name, CC->curr_user);
1298 cprintf("%d '%s' deleted.\n", OK, deleted_room_name);
1300 cprintf("%d ok to delete.\n", OK);
1306 * Internal code to create a new room (returns room flags)
1308 * Room types: 0=public, 1=guessname, 2=passworded, 3=inv-only,
1309 * 4=mailbox, 5=mailbox, but caller supplies namespace
1311 unsigned create_room(char *new_room_name,
1313 char *new_room_pass,
1318 struct quickroom qrbuf;
1322 lprintf(9, "create_room(%s)\n", new_room_name);
1323 if (getroom(&qrbuf, new_room_name) == 0) {
1324 lprintf(9, "%s already exists.\n", new_room_name);
1325 return (0); /* already exists */
1329 memset(&qrbuf, 0, sizeof(struct quickroom));
1330 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1331 qrbuf.QRflags = QR_INUSE;
1332 if (new_room_type > 0)
1333 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1334 if (new_room_type == 1)
1335 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1336 if (new_room_type == 2)
1337 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1338 if ( (new_room_type == 4) || (new_room_type == 5) )
1339 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1341 /* If the user is requesting a personal room, set up the room
1342 * name accordingly (prepend the user number)
1344 if (new_room_type == 4) {
1345 MailboxName(qrbuf.QRname, &CC->usersupp, new_room_name);
1348 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1351 /* If the room is private, and the system administrator has elected
1352 * to automatically grant room aide privileges, do so now; otherwise,
1353 * set the room aide to undefined.
1355 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1356 qrbuf.QRroomaide = CC->usersupp.usernum;
1358 qrbuf.QRroomaide = (-1L);
1362 * If the caller is only interested in testing whether this will work,
1363 * return now without creating the room.
1365 if (!really_create) return (qrbuf.QRflags);
1367 qrbuf.QRnumber = get_new_room_number();
1368 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1369 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1370 qrbuf.QRfloor = new_room_floor;
1372 /* save what we just did... */
1375 /* bump the reference count on whatever floor the room is on */
1376 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1377 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1378 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1380 /* be sure not to kick the creator out of the room! */
1381 lgetuser(&CC->usersupp, CC->curr_user);
1382 CtdlGetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1383 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1384 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1385 CtdlSetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1386 lputuser(&CC->usersupp);
1388 /* resume our happy day */
1389 return (qrbuf.QRflags);
1396 void cmd_cre8(char *args)
1399 char new_room_name[SIZ];
1401 char new_room_pass[SIZ];
1407 cre8_ok = extract_int(args, 0);
1408 extract(new_room_name, args, 1);
1409 new_room_name[ROOMNAMELEN - 1] = 0;
1410 new_room_type = extract_int(args, 2);
1411 extract(new_room_pass, args, 3);
1412 new_room_pass[9] = 0;
1415 if ((strlen(new_room_name) == 0) && (cre8_ok == 1)) {
1416 cprintf("%d Invalid room name.\n", ERROR);
1420 if (!strcasecmp(new_room_name, MAILROOM)) {
1421 cprintf("%d '%s' already exists.\n",
1422 ERROR + ALREADY_EXISTS, new_room_name);
1426 if (num_parms(args) >= 5) {
1427 fl = cgetfloor(extract_int(args, 4));
1428 if ((fl->f_flags & F_INUSE) == 0) {
1429 cprintf("%d Invalid floor number.\n",
1430 ERROR + INVALID_FLOOR_OPERATION);
1433 new_room_floor = extract_int(args, 4);
1437 if (CtdlAccessCheck(ac_logged_in)) return;
1439 if (CC->usersupp.axlevel < config.c_createax) {
1440 cprintf("%d You need higher access to create rooms.\n",
1441 ERROR + HIGHER_ACCESS_REQUIRED);
1445 if ((strlen(new_room_name) == 0) && (cre8_ok == 0)) {
1446 cprintf("%d Ok to create rooms.\n", OK);
1450 if ((new_room_type < 0) || (new_room_type > 4)) {
1451 cprintf("%d Invalid room type.\n", ERROR);
1455 /* Check to make sure the requested room name doesn't already exist */
1456 newflags = create_room(new_room_name,
1457 new_room_type, new_room_pass, new_room_floor, 0);
1458 if (newflags == 0) {
1459 cprintf("%d '%s' already exists.\n",
1460 ERROR + ALREADY_EXISTS, new_room_name);
1465 cprintf("%d OK to create '%s'\n", OK, new_room_name);
1469 /* If we reach this point, the room needs to be created. */
1471 newflags = create_room(new_room_name,
1472 new_room_type, new_room_pass, new_room_floor, 1);
1474 /* post a message in Aide> describing the new room */
1475 safestrncpy(aaa, new_room_name, sizeof aaa);
1476 strcat(aaa, "> created by ");
1477 strcat(aaa, CC->usersupp.fullname);
1478 if (newflags & QR_MAILBOX)
1479 strcat(aaa, " [personal]");
1480 else if (newflags & QR_PRIVATE)
1481 strcat(aaa, " [private]");
1482 if (newflags & QR_GUESSNAME)
1483 strcat(aaa, "[guessname] ");
1484 if (newflags & QR_PASSWORDED) {
1485 strcat(aaa, "\n Password: ");
1486 strcat(aaa, new_room_pass);
1491 cprintf("%d '%s' has been created.\n", OK, new_room_name);
1496 void cmd_einf(char *ok)
1497 { /* enter info file for current room */
1499 char infofilename[SIZ];
1502 if (CtdlAccessCheck(ac_room_aide)) return;
1504 if (atoi(ok) == 0) {
1505 cprintf("%d Ok.\n", OK);
1508 assoc_file_name(infofilename, &CC->quickroom, "info");
1509 lprintf(9, "opening\n");
1510 fp = fopen(infofilename, "w");
1511 lprintf(9, "checking\n");
1513 cprintf("%d Cannot open %s: %s\n",
1514 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1517 cprintf("%d Send info...\n", SEND_LISTING);
1521 if (strcmp(buf, "000"))
1522 fprintf(fp, "%s\n", buf);
1523 } while (strcmp(buf, "000"));
1526 /* now update the room index so people will see our new info */
1527 lgetroom(&CC->quickroom, CC->quickroom.QRname); /* lock so no one steps on us */
1528 CC->quickroom.QRinfo = CC->quickroom.QRhighest + 1L;
1529 lputroom(&CC->quickroom);
1534 * cmd_lflr() - List all known floors
1541 if (CtdlAccessCheck(ac_logged_in)) return;
1543 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1545 for (a = 0; a < MAXFLOORS; ++a) {
1546 getfloor(&flbuf, a);
1547 if (flbuf.f_flags & F_INUSE) {
1548 cprintf("%d|%s|%d\n",
1560 * create a new floor
1562 void cmd_cflr(char *argbuf)
1564 char new_floor_name[SIZ];
1567 int free_slot = (-1);
1570 extract(new_floor_name, argbuf, 0);
1571 cflr_ok = extract_int(argbuf, 1);
1574 if (CtdlAccessCheck(ac_aide)) return;
1576 for (a = 0; a < MAXFLOORS; ++a) {
1577 getfloor(&flbuf, a);
1579 /* note any free slots while we're scanning... */
1580 if (((flbuf.f_flags & F_INUSE) == 0)
1584 /* check to see if it already exists */
1585 if ((!strcasecmp(flbuf.f_name, new_floor_name))
1586 && (flbuf.f_flags & F_INUSE)) {
1587 cprintf("%d Floor '%s' already exists.\n",
1588 ERROR + ALREADY_EXISTS,
1594 if (free_slot < 0) {
1595 cprintf("%d There is no space available for a new floor.\n",
1596 ERROR + INVALID_FLOOR_OPERATION);
1600 cprintf("%d ok to create...\n", OK);
1603 lgetfloor(&flbuf, free_slot);
1604 flbuf.f_flags = F_INUSE;
1605 flbuf.f_ref_count = 0;
1606 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
1607 lputfloor(&flbuf, free_slot);
1608 cprintf("%d %d\n", OK, free_slot);
1616 void cmd_kflr(char *argbuf)
1619 int floor_to_delete;
1623 floor_to_delete = extract_int(argbuf, 0);
1624 kflr_ok = extract_int(argbuf, 1);
1626 if (CtdlAccessCheck(ac_aide)) return;
1628 lgetfloor(&flbuf, floor_to_delete);
1631 if ((flbuf.f_flags & F_INUSE) == 0) {
1632 cprintf("%d Floor %d not in use.\n",
1633 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
1636 if (flbuf.f_ref_count != 0) {
1637 cprintf("%d Cannot delete; floor contains %d rooms.\n",
1638 ERROR + INVALID_FLOOR_OPERATION,
1643 cprintf("%d Ok\n", OK);
1645 cprintf("%d Ok to delete...\n", OK);
1652 if ((delete_ok == 1) && (kflr_ok == 1))
1654 lputfloor(&flbuf, floor_to_delete);
1660 void cmd_eflr(char *argbuf)
1666 np = num_parms(argbuf);
1668 cprintf("%d Usage error.\n", ERROR);
1672 if (CtdlAccessCheck(ac_aide)) return;
1674 floor_num = extract_int(argbuf, 0);
1675 lgetfloor(&flbuf, floor_num);
1676 if ((flbuf.f_flags & F_INUSE) == 0) {
1677 lputfloor(&flbuf, floor_num);
1678 cprintf("%d Floor %d is not in use.\n",
1679 ERROR + INVALID_FLOOR_OPERATION, floor_num);
1683 extract(flbuf.f_name, argbuf, 1);
1684 lputfloor(&flbuf, floor_num);
1686 cprintf("%d Ok\n", OK);