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 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
134 if (vbuf.v_flags & V_FORGET) {
135 retval = retval | UA_GOTOALLOWED;
139 NEWMSG: /* By the way, we also check for the presence of new messages */
140 if (is_msg_in_mset(vbuf.v_seen, roombuf->QRhighest) == 0) {
141 retval = retval | UA_HASNEWMSGS;
144 /* System rooms never show up in the list. */
145 if (roombuf->QRflags2 & QR2_SYSTEM) {
146 retval = retval & ~UA_KNOWN;
152 * Self-checking stuff for a room record read into memory
154 void room_sanity_check(struct quickroom *qrbuf)
156 /* Mailbox rooms are always on the lowest floor */
157 if (qrbuf->QRflags & QR_MAILBOX) {
160 /* Listing order of 0 is illegal except for base rooms */
161 if (qrbuf->QRorder == 0)
162 if (!is_noneditable(qrbuf))
168 * getroom() - retrieve room data from disk
170 int getroom(struct quickroom *qrbuf, char *room_name)
172 struct cdbdata *cdbqr;
173 char lowercase_name[ROOMNAMELEN];
174 char personal_lowercase_name[ROOMNAMELEN];
177 for (a = 0; room_name[a] && a < sizeof lowercase_name - 1; ++a) {
178 lowercase_name[a] = tolower(room_name[a]);
180 lowercase_name[a] = 0;
182 memset(qrbuf, 0, sizeof(struct quickroom));
184 /* First, try the public namespace */
185 cdbqr = cdb_fetch(CDB_QUICKROOM,
186 lowercase_name, strlen(lowercase_name));
188 /* If that didn't work, try the user's personal namespace */
190 snprintf(personal_lowercase_name,
191 sizeof personal_lowercase_name, "%010ld.%s",
192 CC->usersupp.usernum, lowercase_name);
193 cdbqr = cdb_fetch(CDB_QUICKROOM,
194 personal_lowercase_name,
195 strlen(personal_lowercase_name));
198 memcpy(qrbuf, cdbqr->ptr,
199 ((cdbqr->len > sizeof(struct quickroom)) ?
200 sizeof(struct quickroom) : cdbqr->len));
203 room_sanity_check(qrbuf);
212 * lgetroom() - same as getroom() but locks the record (if supported)
214 int lgetroom(struct quickroom *qrbuf, char *room_name)
217 retval = getroom(qrbuf, room_name);
218 if (retval == 0) begin_critical_section(S_QUICKROOM);
224 * b_putroom() - back end to putroom() and b_deleteroom()
225 * (if the supplied buffer is NULL, delete the room record)
227 void b_putroom(struct quickroom *qrbuf, char *room_name)
229 char lowercase_name[ROOMNAMELEN];
232 for (a = 0; a <= strlen(room_name); ++a) {
233 lowercase_name[a] = tolower(room_name[a]);
237 cdb_delete(CDB_QUICKROOM,
238 lowercase_name, strlen(lowercase_name));
240 time(&qrbuf->QRmtime);
241 cdb_store(CDB_QUICKROOM,
242 lowercase_name, strlen(lowercase_name),
243 qrbuf, sizeof(struct quickroom));
249 * putroom() - store room data to disk
251 void putroom(struct quickroom *qrbuf) {
252 b_putroom(qrbuf, qrbuf->QRname);
257 * b_deleteroom() - delete a room record from disk
259 void b_deleteroom(char *room_name) {
260 b_putroom(NULL, room_name);
266 * lputroom() - same as putroom() but unlocks the record (if supported)
268 void lputroom(struct quickroom *qrbuf)
272 end_critical_section(S_QUICKROOM);
276 /****************************************************************************/
279 * getfloor() - retrieve floor data from disk
281 void getfloor(struct floor *flbuf, int floor_num)
283 struct cdbdata *cdbfl;
285 memset(flbuf, 0, sizeof(struct floor));
286 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
288 memcpy(flbuf, cdbfl->ptr,
289 ((cdbfl->len > sizeof(struct floor)) ?
290 sizeof(struct floor) : cdbfl->len));
293 if (floor_num == 0) {
294 strcpy(flbuf->f_name, "Main Floor");
295 flbuf->f_flags = F_INUSE;
296 flbuf->f_ref_count = 3;
303 * lgetfloor() - same as getfloor() but locks the record (if supported)
305 void lgetfloor(struct floor *flbuf, int floor_num)
308 begin_critical_section(S_FLOORTAB);
309 getfloor(flbuf, floor_num);
314 * cgetfloor() - Get floor record from *cache* (loads from disk if needed)
316 * This is strictly a performance hack.
318 struct floor *cgetfloor(int floor_num) {
319 static int initialized = 0;
322 if (initialized == 0) {
323 for (i=0; i<MAXFLOORS; ++i) {
324 floorcache[floor_num] = NULL;
329 if (floorcache[floor_num] == NULL) {
330 floorcache[floor_num] = mallok(sizeof(struct floor));
331 getfloor(floorcache[floor_num], floor_num);
334 return(floorcache[floor_num]);
340 * putfloor() - store floor data on disk
342 void putfloor(struct floor *flbuf, int floor_num)
344 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
345 flbuf, sizeof(struct floor));
347 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
348 if (floorcache[floor_num] != NULL) {
349 phree(floorcache[floor_num]);
350 floorcache[floor_num] = NULL;
356 * lputfloor() - same as putfloor() but unlocks the record (if supported)
358 void lputfloor(struct floor *flbuf, int floor_num)
361 putfloor(flbuf, floor_num);
362 end_critical_section(S_FLOORTAB);
368 * Traverse the room file...
370 void ForEachRoom(void (*CallBack) (struct quickroom *EachRoom, void *out_data),
373 struct quickroom qrbuf;
374 struct cdbdata *cdbqr;
376 cdb_rewind(CDB_QUICKROOM);
378 while (cdbqr = cdb_next_item(CDB_QUICKROOM), cdbqr != NULL) {
379 memset(&qrbuf, 0, sizeof(struct quickroom));
380 memcpy(&qrbuf, cdbqr->ptr,
381 ((cdbqr->len > sizeof(struct quickroom)) ?
382 sizeof(struct quickroom) : cdbqr->len));
384 room_sanity_check(&qrbuf);
385 if (qrbuf.QRflags & QR_INUSE)
386 (*CallBack)(&qrbuf, in_data);
392 * delete_msglist() - delete room message pointers
394 void delete_msglist(struct quickroom *whichroom)
396 struct cdbdata *cdbml;
398 /* Make sure the msglist we're deleting actually exists, otherwise
399 * gdbm will complain when we try to delete an invalid record
401 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
405 /* Go ahead and delete it */
406 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
414 * sort message pointers
415 * (returns new msg count)
417 int sort_msglist(long listptrs[], int oldcount)
428 for (a = numitems - 2; a >= 0; --a) {
429 for (b = 0; b <= a; ++b) {
430 if (listptrs[b] > (listptrs[b + 1])) {
432 hold2 = listptrs[b + 1];
434 listptrs[b + 1] = hold1;
439 /* and yank any nulls */
440 while ((numitems > 0) && (listptrs[0] == 0L)) {
441 memcpy(&listptrs[0], &listptrs[1],
442 (sizeof(long) * (numitems - 1)));
451 * Determine whether a given room is non-editable.
453 int is_noneditable(struct quickroom *qrbuf)
456 /* Lobby> and Aide> are non-editable */
457 if (!strcasecmp(qrbuf->QRname, BASEROOM))
459 else if (!strcasecmp(qrbuf->QRname, AIDEROOM))
462 /* Mail> rooms are also non-editable */
463 else if ( (qrbuf->QRflags & QR_MAILBOX)
464 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
467 /* Everything else is editable */
475 * Back-back-end for all room listing commands
477 void list_roomname(struct quickroom *qrbuf)
479 char truncated_roomname[ROOMNAMELEN];
481 /* For my own mailbox rooms, chop off the owner prefix */
482 if ( (qrbuf->QRflags & QR_MAILBOX)
483 && (atol(qrbuf->QRname) == CC->usersupp.usernum) ) {
484 strcpy(truncated_roomname, qrbuf->QRname);
485 strcpy(truncated_roomname, &truncated_roomname[11]);
486 cprintf("%s", truncated_roomname);
488 /* For all other rooms, just display the name in its entirety */
490 cprintf("%s", qrbuf->QRname);
493 /* ...and now the other parameters */
494 cprintf("|%u|%d|%d\n",
496 (int) qrbuf->QRfloor,
497 (int) qrbuf->QRorder);
502 * cmd_lrms() - List all accessible rooms, known or forgotten
504 void cmd_lrms_backend(struct quickroom *qrbuf, void *data)
506 int FloorBeingSearched = (-1);
507 FloorBeingSearched = *(int *)data;
509 if (((CtdlRoomAccess(qrbuf, &CC->usersupp)
510 & (UA_KNOWN | UA_ZAPPED)))
511 && ((qrbuf->QRfloor == (FloorBeingSearched))
512 || ((FloorBeingSearched) < 0)))
513 list_roomname(qrbuf);
516 void cmd_lrms(char *argbuf)
518 int FloorBeingSearched = (-1);
519 if (strlen(argbuf) > 0)
520 FloorBeingSearched = extract_int(argbuf, 0);
522 if (CtdlAccessCheck(ac_logged_in)) return;
524 if (getuser(&CC->usersupp, CC->curr_user)) {
525 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
528 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
530 ForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
537 * cmd_lkra() - List all known rooms
539 void cmd_lkra_backend(struct quickroom *qrbuf, void *data)
541 int FloorBeingSearched = (-1);
542 FloorBeingSearched = *(int *)data;
544 if (((CtdlRoomAccess(qrbuf, &CC->usersupp)
546 && ((qrbuf->QRfloor == (FloorBeingSearched))
547 || ((FloorBeingSearched) < 0)))
548 list_roomname(qrbuf);
551 void cmd_lkra(char *argbuf)
553 int FloorBeingSearched = (-1);
554 if (strlen(argbuf) > 0)
555 FloorBeingSearched = extract_int(argbuf, 0);
557 if (CtdlAccessCheck(ac_logged_in)) return;
559 if (getuser(&CC->usersupp, CC->curr_user)) {
560 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
563 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
565 ForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
572 * cmd_lkrn() - List all known rooms with new messages
574 void cmd_lkrn_backend(struct quickroom *qrbuf, void *data)
577 int FloorBeingSearched = (-1);
578 FloorBeingSearched = *(int *)data;
580 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
582 && (ra & UA_HASNEWMSGS)
583 && ((qrbuf->QRfloor == (FloorBeingSearched))
584 || ((FloorBeingSearched) < 0)))
585 list_roomname(qrbuf);
588 void cmd_lkrn(char *argbuf)
590 int FloorBeingSearched = (-1);
591 if (strlen(argbuf) > 0)
592 FloorBeingSearched = extract_int(argbuf, 0);
594 if (CtdlAccessCheck(ac_logged_in)) return;
596 if (getuser(&CC->usersupp, CC->curr_user)) {
597 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
600 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
602 ForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
609 * cmd_lkro() - List all known rooms
611 void cmd_lkro_backend(struct quickroom *qrbuf, void *data)
614 int FloorBeingSearched = (-1);
615 FloorBeingSearched = *(int *)data;
617 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
619 && ((ra & UA_HASNEWMSGS) == 0)
620 && ((qrbuf->QRfloor == (FloorBeingSearched))
621 || ((FloorBeingSearched) < 0)))
622 list_roomname(qrbuf);
625 void cmd_lkro(char *argbuf)
627 int FloorBeingSearched = (-1);
628 if (strlen(argbuf) > 0)
629 FloorBeingSearched = extract_int(argbuf, 0);
631 if (CtdlAccessCheck(ac_logged_in)) return;
633 if (getuser(&CC->usersupp, CC->curr_user)) {
634 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
637 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
639 ForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
646 * cmd_lzrm() - List all forgotten rooms
648 void cmd_lzrm_backend(struct quickroom *qrbuf, void *data)
651 int FloorBeingSearched = (-1);
652 FloorBeingSearched = *(int *)data;
654 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
655 if ((ra & UA_GOTOALLOWED)
657 && ((qrbuf->QRfloor == (FloorBeingSearched))
658 || ((FloorBeingSearched) < 0)))
659 list_roomname(qrbuf);
662 void cmd_lzrm(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->usersupp, CC->curr_user)) {
671 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
674 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
676 ForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
682 void usergoto(char *where, int display_result, int *retmsgs, int *retnew)
685 int new_messages = 0;
686 int total_messages = 0;
690 int newmailcount = 0;
692 char truncated_roomname[ROOMNAMELEN];
693 struct cdbdata *cdbfr;
694 long *msglist = NULL;
697 strcpy(CC->quickroom.QRname, where);
698 getroom(&CC->quickroom, where);
700 lgetuser(&CC->usersupp, CC->curr_user);
701 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
703 /* Know the room ... but not if it's the page log room */
704 if (strcasecmp(where, config.c_logpages)) {
705 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
706 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
708 CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
709 lputuser(&CC->usersupp);
711 /* check for new mail */
712 newmailcount = NewMailCount();
714 /* set info to 1 if the user needs to read the room's info file */
715 if (CC->quickroom.QRinfo > vbuf.v_lastseen)
719 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->quickroom.QRnumber, sizeof(long));
721 msglist = mallok(cdbfr->len);
722 memcpy(msglist, cdbfr->ptr, cdbfr->len);
723 num_msgs = cdbfr->len / sizeof(long);
727 if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
728 if (msglist[a] > 0L) {
730 if (is_msg_in_mset(vbuf.v_seen, msglist[a]) == 0) {
736 if (msglist != NULL) phree(msglist);
738 if (CC->quickroom.QRflags & QR_MAILBOX)
743 if ((CC->quickroom.QRroomaide == CC->usersupp.usernum)
744 || (CC->usersupp.axlevel >= 6))
749 strcpy(truncated_roomname, CC->quickroom.QRname);
750 if ( (CC->quickroom.QRflags & QR_MAILBOX)
751 && (atol(CC->quickroom.QRname) == CC->usersupp.usernum) ) {
752 strcpy(truncated_roomname, &truncated_roomname[11]);
755 if (retmsgs != NULL) *retmsgs = total_messages;
756 if (retnew != NULL) *retnew = new_messages;
757 lprintf(9, "<%s> %d new of %d total messages\n",
758 CC->quickroom.QRname, new_messages, total_messages);
761 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d\n",
762 OK, CtdlCheckExpress(),
764 new_messages, total_messages,
765 info, CC->quickroom.QRflags,
766 CC->quickroom.QRhighest,
768 rmailflag, raideflag, newmailcount,
769 CC->quickroom.QRfloor);
775 * cmd_goto() - goto a new room
777 void cmd_goto(char *gargs)
779 struct quickroom QRscratch;
783 char augmented_roomname[SIZ];
787 if (CtdlAccessCheck(ac_logged_in)) return;
789 extract(towhere, gargs, 0);
790 extract(password, gargs, 1);
792 getuser(&CC->usersupp, CC->curr_user);
794 if (!strcasecmp(towhere, "_BASEROOM_"))
795 strcpy(towhere, BASEROOM);
797 if (!strcasecmp(towhere, "_MAIL_"))
798 strcpy(towhere, MAILROOM);
800 if (!strcasecmp(towhere, "_BITBUCKET_"))
801 strcpy(towhere, config.c_twitroom);
804 /* First try a regular match */
805 c = getroom(&QRscratch, towhere);
807 /* Then try a mailbox name match */
809 MailboxName(augmented_roomname, sizeof augmented_roomname,
810 &CC->usersupp, towhere);
811 c = getroom(&QRscratch, augmented_roomname);
813 strcpy(towhere, augmented_roomname);
816 /* And if the room was found... */
819 /* let internal programs go directly to any room */
820 if (CC->internal_pgm) {
821 usergoto(towhere, 1, NULL, NULL);
825 /* See if there is an existing user/room relationship */
826 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
828 /* normal clients have to pass through security */
829 if (ra & UA_GOTOALLOWED) {
834 if ((QRscratch.QRflags & QR_MAILBOX) &&
835 ((ra & UA_GOTOALLOWED))) {
836 usergoto(towhere, 1, NULL, NULL);
838 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
839 ((ra & UA_KNOWN) == 0) &&
840 (strcasecmp(QRscratch.QRpasswd, password)) &&
841 (CC->usersupp.axlevel < 6)
843 cprintf("%d wrong or missing passwd\n",
844 ERROR + PASSWORD_REQUIRED);
846 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
847 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
848 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
849 ((ra & UA_KNOWN) == 0) &&
850 (CC->usersupp.axlevel < 6)
852 lprintf(9, "Failed to acquire private room\n");
855 usergoto(towhere, 1, NULL, NULL);
861 NOPE: cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
867 struct usersupp temp;
868 struct cdbdata *cdbus;
870 getuser(&CC->usersupp, CC->curr_user);
873 * This command is only allowed by aides, room aides,
874 * and room namespace owners
877 || (atol(CC->quickroom.QRname) == CC->usersupp.usernum) ) {
882 cprintf("%d Higher access or room ownership required.\n",
883 ERROR + HIGHER_ACCESS_REQUIRED);
887 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
888 cdb_rewind(CDB_USERSUPP);
889 while (cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
890 memset(&temp, 0, sizeof temp);
891 memcpy(&temp, cdbus->ptr, sizeof temp);
894 if ((CC->quickroom.QRflags & QR_INUSE)
895 && (CtdlRoomAccess(&CC->quickroom, &temp) & UA_KNOWN)
897 cprintf("%s\n", temp.fullname);
904 * RDIR command for room directory
914 if (CtdlAccessCheck(ac_logged_in)) return;
916 getroom(&CC->quickroom, CC->quickroom.QRname);
917 getuser(&CC->usersupp, CC->curr_user);
919 if ((CC->quickroom.QRflags & QR_DIRECTORY) == 0) {
920 cprintf("%d not here.\n", ERROR + NOT_HERE);
923 if (((CC->quickroom.QRflags & QR_VISDIR) == 0)
924 && (CC->usersupp.axlevel < 6)
925 && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
926 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
929 cprintf("%d %s|%s/files/%s\n",
930 LISTING_FOLLOWS, config.c_fqdn, BBSDIR, CC->quickroom.QRdirname);
932 snprintf(buf, sizeof buf, "ls %s/files/%s >%s 2> /dev/null",
933 BBSDIR, CC->quickroom.QRdirname, CC->temp);
936 snprintf(buf, sizeof buf, "%s/files/%s/filedir", BBSDIR, CC->quickroom.QRdirname);
937 fd = fopen(buf, "r");
939 fd = fopen("/dev/null", "r");
941 ls = fopen(CC->temp, "r");
942 while (fgets(flnm, sizeof flnm, ls) != NULL) {
943 flnm[strlen(flnm) - 1] = 0;
944 if (strcasecmp(flnm, "filedir")) {
945 snprintf(buf, sizeof buf, "%s/files/%s/%s",
946 BBSDIR, CC->quickroom.QRdirname, flnm);
950 while ((fgets(buf, sizeof buf, fd) != NULL)
951 && (strlen(comment) == 0)) {
952 buf[strlen(buf) - 1] = 0;
953 if ((!strncasecmp(buf, flnm, strlen(flnm)))
954 && (buf[strlen(flnm)] == ' '))
956 &buf[strlen(flnm) + 1],
959 cprintf("%s|%ld|%s\n", flnm, (long)statbuf.st_size, comment);
970 * get room parameters (aide or room aide command)
974 if (CtdlAccessCheck(ac_room_aide)) return;
977 if (is_noneditable(&CC->quickroom)) {
978 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
983 getroom(&CC->quickroom, CC->quickroom.QRname);
984 cprintf("%d%c%s|%s|%s|%d|%d|%d\n",
985 OK, CtdlCheckExpress(),
986 CC->quickroom.QRname,
987 ((CC->quickroom.QRflags & QR_PASSWORDED) ? CC->quickroom.QRpasswd : ""),
988 ((CC->quickroom.QRflags & QR_DIRECTORY) ? CC->quickroom.QRdirname : ""),
989 CC->quickroom.QRflags,
990 (int) CC->quickroom.QRfloor,
991 (int) CC->quickroom.QRorder);
996 * set room parameters (aide or room aide command)
998 void cmd_setr(char *args)
1003 char old_name[ROOMNAMELEN];
1008 if (CtdlAccessCheck(ac_room_aide)) return;
1010 if (is_noneditable(&CC->quickroom)) {
1015 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
1021 if (num_parms(args) >= 6) {
1022 fl = cgetfloor(extract_int(args, 5));
1023 if ((fl->f_flags & F_INUSE) == 0) {
1024 cprintf("%d Invalid floor number.\n",
1025 ERROR + INVALID_FLOOR_OPERATION);
1029 if (num_parms(args) >= 7) {
1030 new_order = extract_int(args, 6);
1033 if (new_order > 127)
1036 lgetroom(&CC->quickroom, CC->quickroom.QRname);
1038 /* Non-editable base rooms can't be renamed */
1039 strcpy(old_name, CC->quickroom.QRname);
1041 extract(buf, args, 0);
1042 buf[ROOMNAMELEN] = 0;
1043 safestrncpy(CC->quickroom.QRname, buf,
1044 sizeof CC->quickroom.QRname);
1047 extract(buf, args, 1);
1049 safestrncpy(CC->quickroom.QRpasswd, buf, sizeof CC->quickroom.QRpasswd);
1050 extract(buf, args, 2);
1052 safestrncpy(CC->quickroom.QRdirname, buf,
1053 sizeof CC->quickroom.QRdirname);
1054 CC->quickroom.QRflags = (extract_int(args, 3) | QR_INUSE);
1055 if (num_parms(args) >= 7)
1056 CC->quickroom.QRorder = (char) new_order;
1058 /* Clean up a client boo-boo: if the client set the room to
1059 * guess-name or passworded, ensure that the private flag is
1062 if ((CC->quickroom.QRflags & QR_GUESSNAME)
1063 || (CC->quickroom.QRflags & QR_PASSWORDED))
1064 CC->quickroom.QRflags |= QR_PRIVATE;
1066 /* Kick everyone out if the client requested it (by changing the
1067 * room's generation number)
1069 if (extract_int(args, 4)) {
1070 time(&CC->quickroom.QRgen);
1072 old_floor = CC->quickroom.QRfloor;
1073 if (num_parms(args) >= 6) {
1074 CC->quickroom.QRfloor = extract_int(args, 5);
1076 /* Write the room record back to disk */
1077 lputroom(&CC->quickroom);
1079 /* If the room name changed, then there are now two room records,
1080 * so we have to delete the old one.
1082 if (strcasecmp(CC->quickroom.QRname, old_name)) {
1083 b_deleteroom(old_name);
1085 /* adjust the floor reference counts */
1086 lgetfloor(&flbuf, old_floor);
1087 --flbuf.f_ref_count;
1088 lputfloor(&flbuf, old_floor);
1089 lgetfloor(&flbuf, CC->quickroom.QRfloor);
1090 ++flbuf.f_ref_count;
1091 lputfloor(&flbuf, CC->quickroom.QRfloor);
1093 /* create a room directory if necessary */
1094 if (CC->quickroom.QRflags & QR_DIRECTORY) {
1095 snprintf(buf, sizeof buf,
1096 "mkdir ./files/%s </dev/null >/dev/null 2>/dev/null",
1097 CC->quickroom.QRdirname);
1100 snprintf(buf, sizeof buf, "%s> edited by %s\n", CC->quickroom.QRname, CC->curr_user);
1102 cprintf("%d Ok\n", OK);
1108 * get the name of the room aide for this room
1112 struct usersupp usbuf;
1114 if (CtdlAccessCheck(ac_logged_in)) return;
1116 if (is_noneditable(&CC->quickroom)) {
1117 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
1120 if (getuserbynumber(&usbuf, CC->quickroom.QRroomaide) == 0) {
1121 cprintf("%d %s\n", OK, usbuf.fullname);
1123 cprintf("%d \n", OK);
1129 * set the room aide for this room
1131 void cmd_seta(char *new_ra)
1133 struct usersupp usbuf;
1138 if (CtdlAccessCheck(ac_room_aide)) return;
1140 if (getuser(&usbuf, new_ra) != 0) {
1143 newu = usbuf.usernum;
1146 lgetroom(&CC->quickroom, CC->quickroom.QRname);
1148 if (CC->quickroom.QRroomaide != newu) {
1151 CC->quickroom.QRroomaide = newu;
1152 lputroom(&CC->quickroom);
1155 * We have to post the change notice _after_ writing changes to
1156 * the room table, otherwise it would deadlock!
1158 if (post_notice == 1) {
1159 snprintf(buf, sizeof buf, "%s is now room aide for %s>\n",
1160 usbuf.fullname, CC->quickroom.QRname);
1163 cprintf("%d Ok\n", OK);
1167 * Generate an associated file name for a room
1169 void assoc_file_name(char *buf, size_t n,
1170 struct quickroom *qrbuf, const char *prefix)
1172 snprintf(buf, n, "./%s/%ld", prefix, qrbuf->QRnumber);
1176 * retrieve info file for this room
1184 assoc_file_name(filename, sizeof 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, sizeof filename, qrbuf, "info");
1215 /* Delete the image file */
1216 assoc_file_name(filename, sizeof filename, qrbuf, "images");
1219 /* Delete the room's network config file */
1220 assoc_file_name(filename, sizeof 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 snprintf(aaa, sizeof 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, sizeof 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, sizeof 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);