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 snprintf(personal_lowercase_name,
196 sizeof personal_lowercase_name, "%010ld.%s",
197 CC->usersupp.usernum, lowercase_name);
198 cdbqr = cdb_fetch(CDB_QUICKROOM,
199 personal_lowercase_name,
200 strlen(personal_lowercase_name));
203 memcpy(qrbuf, cdbqr->ptr,
204 ((cdbqr->len > sizeof(struct quickroom)) ?
205 sizeof(struct quickroom) : cdbqr->len));
208 room_sanity_check(qrbuf);
217 * lgetroom() - same as getroom() but locks the record (if supported)
219 int lgetroom(struct quickroom *qrbuf, char *room_name)
222 retval = getroom(qrbuf, room_name);
223 if (retval == 0) begin_critical_section(S_QUICKROOM);
229 * b_putroom() - back end to putroom() and b_deleteroom()
230 * (if the supplied buffer is NULL, delete the room record)
232 void b_putroom(struct quickroom *qrbuf, char *room_name)
234 char lowercase_name[ROOMNAMELEN];
237 for (a = 0; a <= strlen(room_name); ++a) {
238 lowercase_name[a] = tolower(room_name[a]);
242 cdb_delete(CDB_QUICKROOM,
243 lowercase_name, strlen(lowercase_name));
245 time(&qrbuf->QRmtime);
246 cdb_store(CDB_QUICKROOM,
247 lowercase_name, strlen(lowercase_name),
248 qrbuf, sizeof(struct quickroom));
254 * putroom() - store room data to disk
256 void putroom(struct quickroom *qrbuf) {
257 b_putroom(qrbuf, qrbuf->QRname);
262 * b_deleteroom() - delete a room record from disk
264 void b_deleteroom(char *room_name) {
265 b_putroom(NULL, room_name);
271 * lputroom() - same as putroom() but unlocks the record (if supported)
273 void lputroom(struct quickroom *qrbuf)
277 end_critical_section(S_QUICKROOM);
281 /****************************************************************************/
284 * getfloor() - retrieve floor data from disk
286 void getfloor(struct floor *flbuf, int floor_num)
288 struct cdbdata *cdbfl;
290 memset(flbuf, 0, sizeof(struct floor));
291 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
293 memcpy(flbuf, cdbfl->ptr,
294 ((cdbfl->len > sizeof(struct floor)) ?
295 sizeof(struct floor) : cdbfl->len));
298 if (floor_num == 0) {
299 strcpy(flbuf->f_name, "Main Floor");
300 flbuf->f_flags = F_INUSE;
301 flbuf->f_ref_count = 3;
308 * lgetfloor() - same as getfloor() but locks the record (if supported)
310 void lgetfloor(struct floor *flbuf, int floor_num)
313 begin_critical_section(S_FLOORTAB);
314 getfloor(flbuf, floor_num);
319 * cgetfloor() - Get floor record from *cache* (loads from disk if needed)
321 * This is strictly a performance hack.
323 struct floor *cgetfloor(int floor_num) {
324 static int initialized = 0;
327 if (initialized == 0) {
328 for (i=0; i<MAXFLOORS; ++i) {
329 floorcache[floor_num] = NULL;
334 if (floorcache[floor_num] == NULL) {
335 floorcache[floor_num] = mallok(sizeof(struct floor));
336 getfloor(floorcache[floor_num], floor_num);
339 return(floorcache[floor_num]);
345 * putfloor() - store floor data on disk
347 void putfloor(struct floor *flbuf, int floor_num)
349 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
350 flbuf, sizeof(struct floor));
352 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
353 if (floorcache[floor_num] != NULL) {
354 phree(floorcache[floor_num]);
355 floorcache[floor_num] = NULL;
361 * lputfloor() - same as putfloor() but unlocks the record (if supported)
363 void lputfloor(struct floor *flbuf, int floor_num)
366 putfloor(flbuf, floor_num);
367 end_critical_section(S_FLOORTAB);
373 * Traverse the room file...
375 void ForEachRoom(void (*CallBack) (struct quickroom *EachRoom, void *out_data),
378 struct quickroom qrbuf;
379 struct cdbdata *cdbqr;
381 cdb_rewind(CDB_QUICKROOM);
383 while (cdbqr = cdb_next_item(CDB_QUICKROOM), cdbqr != NULL) {
384 memset(&qrbuf, 0, sizeof(struct quickroom));
385 memcpy(&qrbuf, cdbqr->ptr,
386 ((cdbqr->len > sizeof(struct quickroom)) ?
387 sizeof(struct quickroom) : cdbqr->len));
389 room_sanity_check(&qrbuf);
390 if (qrbuf.QRflags & QR_INUSE)
391 (*CallBack)(&qrbuf, in_data);
397 * delete_msglist() - delete room message pointers
399 void delete_msglist(struct quickroom *whichroom)
401 struct cdbdata *cdbml;
403 /* Make sure the msglist we're deleting actually exists, otherwise
404 * gdbm will complain when we try to delete an invalid record
406 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
410 /* Go ahead and delete it */
411 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
419 * sort message pointers
420 * (returns new msg count)
422 int sort_msglist(long listptrs[], int oldcount)
433 for (a = numitems - 2; a >= 0; --a) {
434 for (b = 0; b <= a; ++b) {
435 if (listptrs[b] > (listptrs[b + 1])) {
437 hold2 = listptrs[b + 1];
439 listptrs[b + 1] = hold1;
444 /* and yank any nulls */
445 while ((numitems > 0) && (listptrs[0] == 0L)) {
446 memcpy(&listptrs[0], &listptrs[1],
447 (sizeof(long) * (numitems - 1)));
456 * Determine whether a given room is non-editable.
458 int is_noneditable(struct quickroom *qrbuf)
461 /* Lobby> and Aide> are non-editable */
462 if (!strcasecmp(qrbuf->QRname, BASEROOM))
464 else if (!strcasecmp(qrbuf->QRname, AIDEROOM))
467 /* Mail> rooms are also non-editable */
468 else if ( (qrbuf->QRflags & QR_MAILBOX)
469 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
472 /* Everything else is editable */
480 * Back-back-end for all room listing commands
482 void list_roomname(struct quickroom *qrbuf)
484 char truncated_roomname[ROOMNAMELEN];
486 /* For my own mailbox rooms, chop off the owner prefix */
487 if ( (qrbuf->QRflags & QR_MAILBOX)
488 && (atol(qrbuf->QRname) == CC->usersupp.usernum) ) {
489 strcpy(truncated_roomname, qrbuf->QRname);
490 strcpy(truncated_roomname, &truncated_roomname[11]);
491 cprintf("%s", truncated_roomname);
493 /* For all other rooms, just display the name in its entirety */
495 cprintf("%s", qrbuf->QRname);
498 /* ...and now the other parameters */
499 cprintf("|%u|%d|%d\n",
501 (int) qrbuf->QRfloor,
502 (int) qrbuf->QRorder);
507 * cmd_lrms() - List all accessible rooms, known or forgotten
509 void cmd_lrms_backend(struct quickroom *qrbuf, void *data)
511 int FloorBeingSearched = (-1);
512 FloorBeingSearched = *(int *)data;
514 if (((CtdlRoomAccess(qrbuf, &CC->usersupp)
515 & (UA_KNOWN | UA_ZAPPED)))
516 && ((qrbuf->QRfloor == (FloorBeingSearched))
517 || ((FloorBeingSearched) < 0)))
518 list_roomname(qrbuf);
521 void cmd_lrms(char *argbuf)
523 int FloorBeingSearched = (-1);
524 if (strlen(argbuf) > 0)
525 FloorBeingSearched = extract_int(argbuf, 0);
527 if (CtdlAccessCheck(ac_logged_in)) return;
529 if (getuser(&CC->usersupp, CC->curr_user)) {
530 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
533 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
535 ForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
542 * cmd_lkra() - List all known rooms
544 void cmd_lkra_backend(struct quickroom *qrbuf, void *data)
546 int FloorBeingSearched = (-1);
547 FloorBeingSearched = *(int *)data;
549 if (((CtdlRoomAccess(qrbuf, &CC->usersupp)
551 && ((qrbuf->QRfloor == (FloorBeingSearched))
552 || ((FloorBeingSearched) < 0)))
553 list_roomname(qrbuf);
556 void cmd_lkra(char *argbuf)
558 int FloorBeingSearched = (-1);
559 if (strlen(argbuf) > 0)
560 FloorBeingSearched = extract_int(argbuf, 0);
562 if (CtdlAccessCheck(ac_logged_in)) return;
564 if (getuser(&CC->usersupp, CC->curr_user)) {
565 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
568 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
570 ForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
577 * cmd_lkrn() - List all known rooms with new messages
579 void cmd_lkrn_backend(struct quickroom *qrbuf, void *data)
582 int FloorBeingSearched = (-1);
583 FloorBeingSearched = *(int *)data;
585 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
587 && (ra & UA_HASNEWMSGS)
588 && ((qrbuf->QRfloor == (FloorBeingSearched))
589 || ((FloorBeingSearched) < 0)))
590 list_roomname(qrbuf);
593 void cmd_lkrn(char *argbuf)
595 int FloorBeingSearched = (-1);
596 if (strlen(argbuf) > 0)
597 FloorBeingSearched = extract_int(argbuf, 0);
599 if (CtdlAccessCheck(ac_logged_in)) return;
601 if (getuser(&CC->usersupp, CC->curr_user)) {
602 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
605 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
607 ForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
614 * cmd_lkro() - List all known rooms
616 void cmd_lkro_backend(struct quickroom *qrbuf, void *data)
619 int FloorBeingSearched = (-1);
620 FloorBeingSearched = *(int *)data;
622 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
624 && ((ra & UA_HASNEWMSGS) == 0)
625 && ((qrbuf->QRfloor == (FloorBeingSearched))
626 || ((FloorBeingSearched) < 0)))
627 list_roomname(qrbuf);
630 void cmd_lkro(char *argbuf)
632 int FloorBeingSearched = (-1);
633 if (strlen(argbuf) > 0)
634 FloorBeingSearched = extract_int(argbuf, 0);
636 if (CtdlAccessCheck(ac_logged_in)) return;
638 if (getuser(&CC->usersupp, CC->curr_user)) {
639 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
642 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
644 ForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
651 * cmd_lzrm() - List all forgotten rooms
653 void cmd_lzrm_backend(struct quickroom *qrbuf, void *data)
656 int FloorBeingSearched = (-1);
657 FloorBeingSearched = *(int *)data;
659 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
660 if ((ra & UA_GOTOALLOWED)
662 && ((qrbuf->QRfloor == (FloorBeingSearched))
663 || ((FloorBeingSearched) < 0)))
664 list_roomname(qrbuf);
667 void cmd_lzrm(char *argbuf)
669 int FloorBeingSearched = (-1);
670 if (strlen(argbuf) > 0)
671 FloorBeingSearched = extract_int(argbuf, 0);
673 if (CtdlAccessCheck(ac_logged_in)) return;
675 if (getuser(&CC->usersupp, CC->curr_user)) {
676 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
679 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
681 ForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
687 void usergoto(char *where, int display_result, int *retmsgs, int *retnew)
690 int new_messages = 0;
691 int total_messages = 0;
695 int newmailcount = 0;
697 char truncated_roomname[ROOMNAMELEN];
698 struct cdbdata *cdbfr;
699 long *msglist = NULL;
702 strcpy(CC->quickroom.QRname, where);
703 getroom(&CC->quickroom, where);
705 lgetuser(&CC->usersupp, CC->curr_user);
706 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
708 /* Know the room ... but not if it's the page log room */
709 if (strcasecmp(where, config.c_logpages)) {
710 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
711 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
713 CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
714 lputuser(&CC->usersupp);
716 /* check for new mail */
717 newmailcount = NewMailCount();
719 /* set info to 1 if the user needs to read the room's info file */
720 if (CC->quickroom.QRinfo > vbuf.v_lastseen)
724 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->quickroom.QRnumber, sizeof(long));
726 msglist = mallok(cdbfr->len);
727 memcpy(msglist, cdbfr->ptr, cdbfr->len);
728 num_msgs = cdbfr->len / sizeof(long);
732 if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
733 if (msglist[a] > 0L) {
735 if (is_msg_in_mset(vbuf.v_seen, msglist[a]) == 0) {
741 if (msglist != NULL) phree(msglist);
743 if (CC->quickroom.QRflags & QR_MAILBOX)
748 if ((CC->quickroom.QRroomaide == CC->usersupp.usernum)
749 || (CC->usersupp.axlevel >= 6))
754 strcpy(truncated_roomname, CC->quickroom.QRname);
755 if ( (CC->quickroom.QRflags & QR_MAILBOX)
756 && (atol(CC->quickroom.QRname) == CC->usersupp.usernum) ) {
757 strcpy(truncated_roomname, &truncated_roomname[11]);
760 if (retmsgs != NULL) *retmsgs = total_messages;
761 if (retnew != NULL) *retnew = new_messages;
762 lprintf(9, "<%s> %d new of %d total messages\n",
763 CC->quickroom.QRname, new_messages, total_messages);
766 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d\n",
767 OK, CtdlCheckExpress(),
769 new_messages, total_messages,
770 info, CC->quickroom.QRflags,
771 CC->quickroom.QRhighest,
773 rmailflag, raideflag, newmailcount,
774 CC->quickroom.QRfloor);
780 * cmd_goto() - goto a new room
782 void cmd_goto(char *gargs)
784 struct quickroom QRscratch;
788 char augmented_roomname[SIZ];
792 if (CtdlAccessCheck(ac_logged_in)) return;
794 extract(towhere, gargs, 0);
795 extract(password, gargs, 1);
797 getuser(&CC->usersupp, CC->curr_user);
799 if (!strcasecmp(towhere, "_BASEROOM_"))
800 strcpy(towhere, BASEROOM);
802 if (!strcasecmp(towhere, "_MAIL_"))
803 strcpy(towhere, MAILROOM);
805 if (!strcasecmp(towhere, "_BITBUCKET_"))
806 strcpy(towhere, config.c_twitroom);
809 /* First try a regular match */
810 c = getroom(&QRscratch, towhere);
812 /* Then try a mailbox name match */
814 MailboxName(augmented_roomname, &CC->usersupp, towhere);
815 c = getroom(&QRscratch, augmented_roomname);
817 strcpy(towhere, augmented_roomname);
820 /* And if the room was found... */
823 /* let internal programs go directly to any room */
824 if (CC->internal_pgm) {
825 usergoto(towhere, 1, NULL, NULL);
829 /* See if there is an existing user/room relationship */
830 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
832 /* normal clients have to pass through security */
833 if (ra & UA_GOTOALLOWED) {
838 if ((QRscratch.QRflags & QR_MAILBOX) &&
839 ((ra & UA_GOTOALLOWED))) {
840 usergoto(towhere, 1, NULL, NULL);
842 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
843 ((ra & UA_KNOWN) == 0) &&
844 (strcasecmp(QRscratch.QRpasswd, password)) &&
845 (CC->usersupp.axlevel < 6)
847 cprintf("%d wrong or missing passwd\n",
848 ERROR + PASSWORD_REQUIRED);
850 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
851 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
852 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
853 ((ra & UA_KNOWN) == 0) &&
854 (CC->usersupp.axlevel < 6)
856 lprintf(9, "Failed to acquire private room\n");
859 usergoto(towhere, 1, NULL, NULL);
865 NOPE: cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
871 struct usersupp temp;
872 struct cdbdata *cdbus;
874 getuser(&CC->usersupp, CC->curr_user);
875 if (CtdlAccessCheck(ac_room_aide)) return;
877 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
878 cdb_rewind(CDB_USERSUPP);
879 while (cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
880 memset(&temp, 0, sizeof temp);
881 memcpy(&temp, cdbus->ptr, sizeof temp);
884 if ((CC->quickroom.QRflags & QR_INUSE)
885 && (CtdlRoomAccess(&CC->quickroom, &temp) & UA_KNOWN)
887 cprintf("%s\n", temp.fullname);
894 * RDIR command for room directory
904 if (CtdlAccessCheck(ac_logged_in)) return;
906 getroom(&CC->quickroom, CC->quickroom.QRname);
907 getuser(&CC->usersupp, CC->curr_user);
909 if ((CC->quickroom.QRflags & QR_DIRECTORY) == 0) {
910 cprintf("%d not here.\n", ERROR + NOT_HERE);
913 if (((CC->quickroom.QRflags & QR_VISDIR) == 0)
914 && (CC->usersupp.axlevel < 6)
915 && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
916 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
919 cprintf("%d %s|%s/files/%s\n",
920 LISTING_FOLLOWS, config.c_fqdn, BBSDIR, CC->quickroom.QRdirname);
922 snprintf(buf, sizeof buf, "ls %s/files/%s >%s 2> /dev/null",
923 BBSDIR, CC->quickroom.QRdirname, CC->temp);
926 snprintf(buf, sizeof buf, "%s/files/%s/filedir", BBSDIR, CC->quickroom.QRdirname);
927 fd = fopen(buf, "r");
929 fd = fopen("/dev/null", "r");
931 ls = fopen(CC->temp, "r");
932 while (fgets(flnm, sizeof flnm, ls) != NULL) {
933 flnm[strlen(flnm) - 1] = 0;
934 if (strcasecmp(flnm, "filedir")) {
935 snprintf(buf, sizeof buf, "%s/files/%s/%s",
936 BBSDIR, CC->quickroom.QRdirname, flnm);
940 while ((fgets(buf, sizeof buf, fd) != NULL)
941 && (strlen(comment) == 0)) {
942 buf[strlen(buf) - 1] = 0;
943 if ((!strncasecmp(buf, flnm, strlen(flnm)))
944 && (buf[strlen(flnm)] == ' '))
946 &buf[strlen(flnm) + 1],
949 cprintf("%s|%ld|%s\n", flnm, (long)statbuf.st_size, comment);
960 * get room parameters (aide or room aide command)
964 if (CtdlAccessCheck(ac_room_aide)) return;
967 if (is_noneditable(&CC->quickroom)) {
968 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
973 getroom(&CC->quickroom, CC->quickroom.QRname);
974 cprintf("%d%c%s|%s|%s|%d|%d|%d\n",
975 OK, CtdlCheckExpress(),
976 CC->quickroom.QRname,
977 ((CC->quickroom.QRflags & QR_PASSWORDED) ? CC->quickroom.QRpasswd : ""),
978 ((CC->quickroom.QRflags & QR_DIRECTORY) ? CC->quickroom.QRdirname : ""),
979 CC->quickroom.QRflags,
980 (int) CC->quickroom.QRfloor,
981 (int) CC->quickroom.QRorder);
986 * set room parameters (aide or room aide command)
988 void cmd_setr(char *args)
993 char old_name[ROOMNAMELEN];
998 if (CtdlAccessCheck(ac_room_aide)) return;
1000 if (is_noneditable(&CC->quickroom)) {
1005 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
1011 if (num_parms(args) >= 6) {
1012 fl = cgetfloor(extract_int(args, 5));
1013 if ((fl->f_flags & F_INUSE) == 0) {
1014 cprintf("%d Invalid floor number.\n",
1015 ERROR + INVALID_FLOOR_OPERATION);
1019 if (num_parms(args) >= 7) {
1020 new_order = extract_int(args, 6);
1023 if (new_order > 127)
1026 lgetroom(&CC->quickroom, CC->quickroom.QRname);
1028 /* Non-editable base rooms can't be renamed */
1029 strcpy(old_name, CC->quickroom.QRname);
1031 extract(buf, args, 0);
1032 buf[ROOMNAMELEN] = 0;
1033 safestrncpy(CC->quickroom.QRname, buf,
1034 sizeof CC->quickroom.QRname);
1037 extract(buf, args, 1);
1039 safestrncpy(CC->quickroom.QRpasswd, buf, sizeof CC->quickroom.QRpasswd);
1040 extract(buf, args, 2);
1042 safestrncpy(CC->quickroom.QRdirname, buf,
1043 sizeof CC->quickroom.QRdirname);
1044 CC->quickroom.QRflags = (extract_int(args, 3) | QR_INUSE);
1045 if (num_parms(args) >= 7)
1046 CC->quickroom.QRorder = (char) new_order;
1048 /* Clean up a client boo-boo: if the client set the room to
1049 * guess-name or passworded, ensure that the private flag is
1052 if ((CC->quickroom.QRflags & QR_GUESSNAME)
1053 || (CC->quickroom.QRflags & QR_PASSWORDED))
1054 CC->quickroom.QRflags |= QR_PRIVATE;
1056 /* Kick everyone out if the client requested it (by changing the
1057 * room's generation number)
1059 if (extract_int(args, 4)) {
1060 time(&CC->quickroom.QRgen);
1062 old_floor = CC->quickroom.QRfloor;
1063 if (num_parms(args) >= 6) {
1064 CC->quickroom.QRfloor = extract_int(args, 5);
1066 /* Write the room record back to disk */
1067 lputroom(&CC->quickroom);
1069 /* If the room name changed, then there are now two room records,
1070 * so we have to delete the old one.
1072 if (strcasecmp(CC->quickroom.QRname, old_name)) {
1073 b_deleteroom(old_name);
1075 /* adjust the floor reference counts */
1076 lgetfloor(&flbuf, old_floor);
1077 --flbuf.f_ref_count;
1078 lputfloor(&flbuf, old_floor);
1079 lgetfloor(&flbuf, CC->quickroom.QRfloor);
1080 ++flbuf.f_ref_count;
1081 lputfloor(&flbuf, CC->quickroom.QRfloor);
1083 /* create a room directory if necessary */
1084 if (CC->quickroom.QRflags & QR_DIRECTORY) {
1085 snprintf(buf, sizeof buf,
1086 "mkdir ./files/%s </dev/null >/dev/null 2>/dev/null",
1087 CC->quickroom.QRdirname);
1090 snprintf(buf, sizeof buf, "%s> edited by %s\n", CC->quickroom.QRname, CC->curr_user);
1092 cprintf("%d Ok\n", OK);
1098 * get the name of the room aide for this room
1102 struct usersupp usbuf;
1104 if (CtdlAccessCheck(ac_logged_in)) return;
1106 if (is_noneditable(&CC->quickroom)) {
1107 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
1110 if (getuserbynumber(&usbuf, CC->quickroom.QRroomaide) == 0) {
1111 cprintf("%d %s\n", OK, usbuf.fullname);
1113 cprintf("%d \n", OK);
1119 * set the room aide for this room
1121 void cmd_seta(char *new_ra)
1123 struct usersupp usbuf;
1128 if (CtdlAccessCheck(ac_room_aide)) return;
1130 if (getuser(&usbuf, new_ra) != 0) {
1133 newu = usbuf.usernum;
1136 lgetroom(&CC->quickroom, CC->quickroom.QRname);
1138 if (CC->quickroom.QRroomaide != newu) {
1141 CC->quickroom.QRroomaide = newu;
1142 lputroom(&CC->quickroom);
1145 * We have to post the change notice _after_ writing changes to
1146 * the room table, otherwise it would deadlock!
1148 if (post_notice == 1) {
1149 snprintf(buf, sizeof buf, "%s is now room aide for %s>\n",
1150 usbuf.fullname, CC->quickroom.QRname);
1153 cprintf("%d Ok\n", OK);
1157 * Generate an associated file name for a room
1159 void assoc_file_name(char *buf, size_t n,
1160 struct quickroom *qrbuf, const char *prefix)
1162 snprintf(buf, n, "./%s/%ld", prefix, qrbuf->QRnumber);
1166 * retrieve info file for this room
1174 assoc_file_name(filename, sizeof filename, &CC->quickroom, "info");
1175 info_fp = fopen(filename, "r");
1177 if (info_fp == NULL) {
1178 cprintf("%d No info file.\n", ERROR);
1181 cprintf("%d Info:\n", LISTING_FOLLOWS);
1182 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1183 if (strlen(buf) > 0)
1184 buf[strlen(buf) - 1] = 0;
1185 cprintf("%s\n", buf);
1192 * Back end processing to delete a room and everything associated with it
1194 void delete_room(struct quickroom *qrbuf)
1199 lprintf(9, "Deleting room <%s>\n", qrbuf->QRname);
1201 /* Delete the info file */
1202 assoc_file_name(filename, sizeof filename, qrbuf, "info");
1205 /* Delete the image file */
1206 assoc_file_name(filename, sizeof filename, qrbuf, "images");
1209 /* Delete the room's network config file */
1210 assoc_file_name(filename, sizeof filename, qrbuf, "netconfigs");
1213 /* Delete the messages in the room
1214 * (Careful: this opens an S_QUICKROOM critical section!)
1216 CtdlDeleteMessages(qrbuf->QRname, 0L, "");
1218 /* Flag the room record as not in use */
1219 lgetroom(qrbuf, qrbuf->QRname);
1223 /* then decrement the reference count for the floor */
1224 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1225 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1226 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1228 /* Delete the room record from the database! */
1229 b_deleteroom(qrbuf->QRname);
1235 * Check access control for deleting a room
1237 int CtdlDoIHavePermissionToDeleteThisRoom(struct quickroom *qr) {
1239 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1243 if (is_noneditable(qr)) {
1248 * For mailboxes, check stuff
1250 if (qr->QRflags & QR_MAILBOX) {
1252 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1254 if (atol(qr->QRname) != CC->usersupp.usernum) {
1255 return(0); /* not my room */
1258 /* Can't delete your Mail> room */
1259 if (!strcasecmp(&qr->QRname[12], MAILROOM)) return(0);
1261 /* Otherwise it's ok */
1266 * For normal rooms, just check for aide or room aide status.
1269 return(is_room_aide());
1272 /* Should never get to this point, but to keep the compiler quiet... */
1277 * aide command: kill the current room
1279 void cmd_kill(char *argbuf)
1282 char deleted_room_name[ROOMNAMELEN];
1285 kill_ok = extract_int(argbuf, 0);
1287 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->quickroom) == 0) {
1288 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1292 strcpy(deleted_room_name, CC->quickroom.QRname);
1293 delete_room(&CC->quickroom); /* Do the dirty work */
1294 usergoto(BASEROOM, 0, NULL, NULL); /* Return to the Lobby */
1296 /* tell the world what we did */
1297 snprintf(aaa, sizeof aaa, "%s> killed by %s\n",
1298 deleted_room_name, CC->curr_user);
1300 cprintf("%d '%s' deleted.\n", OK, deleted_room_name);
1302 cprintf("%d ok to delete.\n", OK);
1308 * Internal code to create a new room (returns room flags)
1310 * Room types: 0=public, 1=guessname, 2=passworded, 3=inv-only,
1311 * 4=mailbox, 5=mailbox, but caller supplies namespace
1313 unsigned create_room(char *new_room_name,
1315 char *new_room_pass,
1320 struct quickroom qrbuf;
1324 lprintf(9, "create_room(%s)\n", new_room_name);
1325 if (getroom(&qrbuf, new_room_name) == 0) {
1326 lprintf(9, "%s already exists.\n", new_room_name);
1327 return (0); /* already exists */
1331 memset(&qrbuf, 0, sizeof(struct quickroom));
1332 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1333 qrbuf.QRflags = QR_INUSE;
1334 if (new_room_type > 0)
1335 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1336 if (new_room_type == 1)
1337 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1338 if (new_room_type == 2)
1339 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1340 if ( (new_room_type == 4) || (new_room_type == 5) )
1341 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1343 /* If the user is requesting a personal room, set up the room
1344 * name accordingly (prepend the user number)
1346 if (new_room_type == 4) {
1347 MailboxName(qrbuf.QRname, &CC->usersupp, new_room_name);
1350 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1353 /* If the room is private, and the system administrator has elected
1354 * to automatically grant room aide privileges, do so now; otherwise,
1355 * set the room aide to undefined.
1357 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1358 qrbuf.QRroomaide = CC->usersupp.usernum;
1360 qrbuf.QRroomaide = (-1L);
1364 * If the caller is only interested in testing whether this will work,
1365 * return now without creating the room.
1367 if (!really_create) return (qrbuf.QRflags);
1369 qrbuf.QRnumber = get_new_room_number();
1370 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1371 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1372 qrbuf.QRfloor = new_room_floor;
1374 /* save what we just did... */
1377 /* bump the reference count on whatever floor the room is on */
1378 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1379 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1380 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1382 /* be sure not to kick the creator out of the room! */
1383 lgetuser(&CC->usersupp, CC->curr_user);
1384 CtdlGetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1385 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1386 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1387 CtdlSetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1388 lputuser(&CC->usersupp);
1390 /* resume our happy day */
1391 return (qrbuf.QRflags);
1398 void cmd_cre8(char *args)
1401 char new_room_name[SIZ];
1403 char new_room_pass[SIZ];
1409 cre8_ok = extract_int(args, 0);
1410 extract(new_room_name, args, 1);
1411 new_room_name[ROOMNAMELEN - 1] = 0;
1412 new_room_type = extract_int(args, 2);
1413 extract(new_room_pass, args, 3);
1414 new_room_pass[9] = 0;
1417 if ((strlen(new_room_name) == 0) && (cre8_ok == 1)) {
1418 cprintf("%d Invalid room name.\n", ERROR);
1422 if (!strcasecmp(new_room_name, MAILROOM)) {
1423 cprintf("%d '%s' already exists.\n",
1424 ERROR + ALREADY_EXISTS, new_room_name);
1428 if (num_parms(args) >= 5) {
1429 fl = cgetfloor(extract_int(args, 4));
1430 if ((fl->f_flags & F_INUSE) == 0) {
1431 cprintf("%d Invalid floor number.\n",
1432 ERROR + INVALID_FLOOR_OPERATION);
1435 new_room_floor = extract_int(args, 4);
1439 if (CtdlAccessCheck(ac_logged_in)) return;
1441 if (CC->usersupp.axlevel < config.c_createax) {
1442 cprintf("%d You need higher access to create rooms.\n",
1443 ERROR + HIGHER_ACCESS_REQUIRED);
1447 if ((strlen(new_room_name) == 0) && (cre8_ok == 0)) {
1448 cprintf("%d Ok to create rooms.\n", OK);
1452 if ((new_room_type < 0) || (new_room_type > 4)) {
1453 cprintf("%d Invalid room type.\n", ERROR);
1457 /* Check to make sure the requested room name doesn't already exist */
1458 newflags = create_room(new_room_name,
1459 new_room_type, new_room_pass, new_room_floor, 0);
1460 if (newflags == 0) {
1461 cprintf("%d '%s' already exists.\n",
1462 ERROR + ALREADY_EXISTS, new_room_name);
1467 cprintf("%d OK to create '%s'\n", OK, new_room_name);
1471 /* If we reach this point, the room needs to be created. */
1473 newflags = create_room(new_room_name,
1474 new_room_type, new_room_pass, new_room_floor, 1);
1476 /* post a message in Aide> describing the new room */
1477 safestrncpy(aaa, new_room_name, sizeof aaa);
1478 strcat(aaa, "> created by ");
1479 strcat(aaa, CC->usersupp.fullname);
1480 if (newflags & QR_MAILBOX)
1481 strcat(aaa, " [personal]");
1482 else if (newflags & QR_PRIVATE)
1483 strcat(aaa, " [private]");
1484 if (newflags & QR_GUESSNAME)
1485 strcat(aaa, "[guessname] ");
1486 if (newflags & QR_PASSWORDED) {
1487 strcat(aaa, "\n Password: ");
1488 strcat(aaa, new_room_pass);
1493 cprintf("%d '%s' has been created.\n", OK, new_room_name);
1498 void cmd_einf(char *ok)
1499 { /* enter info file for current room */
1501 char infofilename[SIZ];
1504 if (CtdlAccessCheck(ac_room_aide)) return;
1506 if (atoi(ok) == 0) {
1507 cprintf("%d Ok.\n", OK);
1510 assoc_file_name(infofilename, sizeof infofilename, &CC->quickroom, "info");
1511 lprintf(9, "opening\n");
1512 fp = fopen(infofilename, "w");
1513 lprintf(9, "checking\n");
1515 cprintf("%d Cannot open %s: %s\n",
1516 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1519 cprintf("%d Send info...\n", SEND_LISTING);
1523 if (strcmp(buf, "000"))
1524 fprintf(fp, "%s\n", buf);
1525 } while (strcmp(buf, "000"));
1528 /* now update the room index so people will see our new info */
1529 lgetroom(&CC->quickroom, CC->quickroom.QRname); /* lock so no one steps on us */
1530 CC->quickroom.QRinfo = CC->quickroom.QRhighest + 1L;
1531 lputroom(&CC->quickroom);
1536 * cmd_lflr() - List all known floors
1543 if (CtdlAccessCheck(ac_logged_in)) return;
1545 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1547 for (a = 0; a < MAXFLOORS; ++a) {
1548 getfloor(&flbuf, a);
1549 if (flbuf.f_flags & F_INUSE) {
1550 cprintf("%d|%s|%d\n",
1562 * create a new floor
1564 void cmd_cflr(char *argbuf)
1566 char new_floor_name[SIZ];
1569 int free_slot = (-1);
1572 extract(new_floor_name, argbuf, 0);
1573 cflr_ok = extract_int(argbuf, 1);
1576 if (CtdlAccessCheck(ac_aide)) return;
1578 for (a = 0; a < MAXFLOORS; ++a) {
1579 getfloor(&flbuf, a);
1581 /* note any free slots while we're scanning... */
1582 if (((flbuf.f_flags & F_INUSE) == 0)
1586 /* check to see if it already exists */
1587 if ((!strcasecmp(flbuf.f_name, new_floor_name))
1588 && (flbuf.f_flags & F_INUSE)) {
1589 cprintf("%d Floor '%s' already exists.\n",
1590 ERROR + ALREADY_EXISTS,
1596 if (free_slot < 0) {
1597 cprintf("%d There is no space available for a new floor.\n",
1598 ERROR + INVALID_FLOOR_OPERATION);
1602 cprintf("%d ok to create...\n", OK);
1605 lgetfloor(&flbuf, free_slot);
1606 flbuf.f_flags = F_INUSE;
1607 flbuf.f_ref_count = 0;
1608 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
1609 lputfloor(&flbuf, free_slot);
1610 cprintf("%d %d\n", OK, free_slot);
1618 void cmd_kflr(char *argbuf)
1621 int floor_to_delete;
1625 floor_to_delete = extract_int(argbuf, 0);
1626 kflr_ok = extract_int(argbuf, 1);
1628 if (CtdlAccessCheck(ac_aide)) return;
1630 lgetfloor(&flbuf, floor_to_delete);
1633 if ((flbuf.f_flags & F_INUSE) == 0) {
1634 cprintf("%d Floor %d not in use.\n",
1635 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
1638 if (flbuf.f_ref_count != 0) {
1639 cprintf("%d Cannot delete; floor contains %d rooms.\n",
1640 ERROR + INVALID_FLOOR_OPERATION,
1645 cprintf("%d Ok\n", OK);
1647 cprintf("%d Ok to delete...\n", OK);
1654 if ((delete_ok == 1) && (kflr_ok == 1))
1656 lputfloor(&flbuf, floor_to_delete);
1662 void cmd_eflr(char *argbuf)
1668 np = num_parms(argbuf);
1670 cprintf("%d Usage error.\n", ERROR);
1674 if (CtdlAccessCheck(ac_aide)) return;
1676 floor_num = extract_int(argbuf, 0);
1677 lgetfloor(&flbuf, floor_num);
1678 if ((flbuf.f_flags & F_INUSE) == 0) {
1679 lputfloor(&flbuf, floor_num);
1680 cprintf("%d Floor %d is not in use.\n",
1681 ERROR + INVALID_FLOOR_OPERATION, floor_num);
1685 extract(flbuf.f_name, argbuf, 1);
1686 lputfloor(&flbuf, floor_num);
1688 cprintf("%d Ok\n", OK);