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)
57 /* for internal programs, always do everything */
58 if (((CC->internal_pgm)) && (roombuf->QRflags & QR_INUSE)) {
59 return (UA_KNOWN | UA_GOTOALLOWED);
62 /* Locate any applicable user/room relationships */
63 CtdlGetRelationship(&vbuf, userbuf, roombuf);
65 /* Force the properties of the Aide room */
66 if (!strcasecmp(roombuf->QRname, AIDEROOM)) {
67 if (userbuf->axlevel >= 6) {
68 retval = UA_KNOWN | UA_GOTOALLOWED;
75 /* If this is a public room, it's accessible... */
76 if ( ((roombuf->QRflags & QR_PRIVATE) == 0)
77 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
78 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
81 /* If this is a preferred users only room, check access level */
82 if (roombuf->QRflags & QR_PREFONLY) {
83 if (userbuf->axlevel < 5) {
84 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
88 /* For private rooms, check the generation number matchups */
89 if ( (roombuf->QRflags & QR_PRIVATE)
90 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
92 /* An explicit match means the user belongs in this room */
93 if (vbuf.v_flags & V_ACCESS) {
94 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
96 /* Otherwise, check if this is a guess-name or passworded
97 * room. If it is, a goto may at least be attempted
99 else if ((roombuf->QRflags & QR_PRIVATE)
100 || (roombuf->QRflags & QR_PASSWORDED)) {
101 retval = retval & ~UA_KNOWN;
102 retval = retval | UA_GOTOALLOWED;
106 /* For mailbox rooms, also check the generation number matchups */
107 if (roombuf->QRflags & QR_MAILBOX) {
108 if (userbuf->usernum == atol(roombuf->QRname)) {
109 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
111 /* An explicit match means the user belongs in this room */
112 if (vbuf.v_flags & V_ACCESS) {
113 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
117 /* Check to see if the user has forgotten this room */
118 if (vbuf.v_flags & V_FORGET) {
119 retval = retval & ~UA_KNOWN;
120 retval = retval | UA_ZAPPED;
122 /* If user is explicitly locked out of this room, deny everything */
123 if (vbuf.v_flags & V_LOCKOUT) {
124 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
127 /* Aides get access to everything */
128 if (userbuf->axlevel >= 6) {
129 if (vbuf.v_flags & V_FORGET) {
130 retval = retval | UA_GOTOALLOWED;
133 retval = retval | UA_GOTOALLOWED;
134 if ((roombuf->QRflags & QR_MAILBOX) == 0) {
135 retval = retval | UA_KNOWN;
140 NEWMSG: /* By the way, we also check for the presence of new messages */
141 if (is_msg_in_mset(vbuf.v_seen, roombuf->QRhighest) == 0) {
142 retval = retval | UA_HASNEWMSGS;
145 /* System rooms never show up in the list. */
146 if (roombuf->QRflags2 & QR2_SYSTEM) {
147 retval = retval & ~UA_KNOWN;
153 * Self-checking stuff for a room record read into memory
155 void room_sanity_check(struct quickroom *qrbuf)
157 /* Mailbox rooms are always on the lowest floor */
158 if (qrbuf->QRflags & QR_MAILBOX) {
161 /* Listing order of 0 is illegal except for base rooms */
162 if (qrbuf->QRorder == 0)
163 if (!is_noneditable(qrbuf))
169 * getroom() - retrieve room data from disk
171 int getroom(struct quickroom *qrbuf, char *room_name)
173 struct cdbdata *cdbqr;
174 char lowercase_name[ROOMNAMELEN];
175 char personal_lowercase_name[ROOMNAMELEN];
178 for (a = 0; room_name[a] && a < sizeof lowercase_name - 1; ++a) {
179 lowercase_name[a] = tolower(room_name[a]);
181 lowercase_name[a] = 0;
183 memset(qrbuf, 0, sizeof(struct quickroom));
185 /* First, try the public namespace */
186 cdbqr = cdb_fetch(CDB_QUICKROOM,
187 lowercase_name, strlen(lowercase_name));
189 /* If that didn't work, try the user's personal namespace */
191 sprintf(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 mailbox rooms, chop off the owner prefix */
482 if (qrbuf->QRflags & QR_MAILBOX) {
483 strcpy(truncated_roomname, qrbuf->QRname);
484 strcpy(truncated_roomname, &truncated_roomname[11]);
485 cprintf("%s", truncated_roomname);
487 /* For all other rooms, just display the name in its entirety */
489 cprintf("%s", qrbuf->QRname);
492 /* ...and now the other parameters */
493 cprintf("|%u|%d|%d\n",
495 (int) qrbuf->QRfloor,
496 (int) qrbuf->QRorder);
501 * cmd_lrms() - List all accessible rooms, known or forgotten
503 void cmd_lrms_backend(struct quickroom *qrbuf, void *data)
505 int FloorBeingSearched = (-1);
506 FloorBeingSearched = *(int *)data;
508 if (((CtdlRoomAccess(qrbuf, &CC->usersupp)
509 & (UA_KNOWN | UA_ZAPPED)))
510 && ((qrbuf->QRfloor == (FloorBeingSearched))
511 || ((FloorBeingSearched) < 0)))
512 list_roomname(qrbuf);
515 void cmd_lrms(char *argbuf)
517 int FloorBeingSearched = (-1);
518 if (strlen(argbuf) > 0)
519 FloorBeingSearched = extract_int(argbuf, 0);
521 if (CtdlAccessCheck(ac_logged_in)) return;
523 if (getuser(&CC->usersupp, CC->curr_user)) {
524 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
527 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
529 ForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
536 * cmd_lkra() - List all known rooms
538 void cmd_lkra_backend(struct quickroom *qrbuf, void *data)
540 int FloorBeingSearched = (-1);
541 FloorBeingSearched = *(int *)data;
543 if (((CtdlRoomAccess(qrbuf, &CC->usersupp)
545 && ((qrbuf->QRfloor == (FloorBeingSearched))
546 || ((FloorBeingSearched) < 0)))
547 list_roomname(qrbuf);
550 void cmd_lkra(char *argbuf)
552 int FloorBeingSearched = (-1);
553 if (strlen(argbuf) > 0)
554 FloorBeingSearched = extract_int(argbuf, 0);
556 if (CtdlAccessCheck(ac_logged_in)) return;
558 if (getuser(&CC->usersupp, CC->curr_user)) {
559 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
562 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
564 ForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
571 * cmd_lkrn() - List all known rooms with new messages
573 void cmd_lkrn_backend(struct quickroom *qrbuf, void *data)
576 int FloorBeingSearched = (-1);
577 FloorBeingSearched = *(int *)data;
579 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
581 && (ra & UA_HASNEWMSGS)
582 && ((qrbuf->QRfloor == (FloorBeingSearched))
583 || ((FloorBeingSearched) < 0)))
584 list_roomname(qrbuf);
587 void cmd_lkrn(char *argbuf)
589 int FloorBeingSearched = (-1);
590 if (strlen(argbuf) > 0)
591 FloorBeingSearched = extract_int(argbuf, 0);
593 if (CtdlAccessCheck(ac_logged_in)) return;
595 if (getuser(&CC->usersupp, CC->curr_user)) {
596 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
599 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
601 ForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
608 * cmd_lkro() - List all known rooms
610 void cmd_lkro_backend(struct quickroom *qrbuf, void *data)
613 int FloorBeingSearched = (-1);
614 FloorBeingSearched = *(int *)data;
616 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
618 && ((ra & UA_HASNEWMSGS) == 0)
619 && ((qrbuf->QRfloor == (FloorBeingSearched))
620 || ((FloorBeingSearched) < 0)))
621 list_roomname(qrbuf);
624 void cmd_lkro(char *argbuf)
626 int FloorBeingSearched = (-1);
627 if (strlen(argbuf) > 0)
628 FloorBeingSearched = extract_int(argbuf, 0);
630 if (CtdlAccessCheck(ac_logged_in)) return;
632 if (getuser(&CC->usersupp, CC->curr_user)) {
633 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
636 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
638 ForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
645 * cmd_lzrm() - List all forgotten rooms
647 void cmd_lzrm_backend(struct quickroom *qrbuf, void *data)
650 int FloorBeingSearched = (-1);
651 FloorBeingSearched = *(int *)data;
653 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
654 if ((ra & UA_GOTOALLOWED)
656 && ((qrbuf->QRfloor == (FloorBeingSearched))
657 || ((FloorBeingSearched) < 0)))
658 list_roomname(qrbuf);
661 void cmd_lzrm(char *argbuf)
663 int FloorBeingSearched = (-1);
664 if (strlen(argbuf) > 0)
665 FloorBeingSearched = extract_int(argbuf, 0);
667 if (CtdlAccessCheck(ac_logged_in)) return;
669 if (getuser(&CC->usersupp, CC->curr_user)) {
670 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
673 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
675 ForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
681 void usergoto(char *where, int display_result, int *retmsgs, int *retnew)
684 int new_messages = 0;
685 int total_messages = 0;
689 int newmailcount = 0;
691 char truncated_roomname[ROOMNAMELEN];
692 struct cdbdata *cdbfr;
693 long *msglist = NULL;
696 strcpy(CC->quickroom.QRname, where);
697 getroom(&CC->quickroom, where);
699 lgetuser(&CC->usersupp, CC->curr_user);
700 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
702 /* Know the room ... but not if it's the page log room */
703 if (strcasecmp(where, config.c_logpages)) {
704 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
705 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
707 CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
708 lputuser(&CC->usersupp);
710 /* check for new mail */
711 newmailcount = NewMailCount();
713 /* set info to 1 if the user needs to read the room's info file */
714 if (CC->quickroom.QRinfo > vbuf.v_lastseen)
718 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->quickroom.QRnumber, sizeof(long));
720 msglist = mallok(cdbfr->len);
721 memcpy(msglist, cdbfr->ptr, cdbfr->len);
722 num_msgs = cdbfr->len / sizeof(long);
726 if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
727 if (msglist[a] > 0L) {
729 if (is_msg_in_mset(vbuf.v_seen, msglist[a]) == 0) {
735 if (msglist != NULL) phree(msglist);
737 if (CC->quickroom.QRflags & QR_MAILBOX)
742 if ((CC->quickroom.QRroomaide == CC->usersupp.usernum)
743 || (CC->usersupp.axlevel >= 6))
748 strcpy(truncated_roomname, CC->quickroom.QRname);
749 if (CC->quickroom.QRflags & QR_MAILBOX) {
750 strcpy(truncated_roomname, &truncated_roomname[11]);
753 if (retmsgs != NULL) *retmsgs = total_messages;
754 if (retnew != NULL) *retnew = new_messages;
755 lprintf(9, "<%s> %d new of %d total messages\n",
756 CC->quickroom.QRname, new_messages, total_messages);
759 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d\n",
760 OK, CtdlCheckExpress(),
762 new_messages, total_messages,
763 info, CC->quickroom.QRflags,
764 CC->quickroom.QRhighest,
766 rmailflag, raideflag, newmailcount,
767 CC->quickroom.QRfloor);
773 * cmd_goto() - goto a new room
775 void cmd_goto(char *gargs)
777 struct quickroom QRscratch;
781 char augmented_roomname[SIZ];
785 if (CtdlAccessCheck(ac_logged_in)) return;
787 extract(towhere, gargs, 0);
788 extract(password, gargs, 1);
790 getuser(&CC->usersupp, CC->curr_user);
792 if (!strcasecmp(towhere, "_BASEROOM_"))
793 strcpy(towhere, BASEROOM);
795 if (!strcasecmp(towhere, "_MAIL_"))
796 strcpy(towhere, MAILROOM);
798 if (!strcasecmp(towhere, "_BITBUCKET_"))
799 strcpy(towhere, config.c_twitroom);
802 /* First try a regular match */
803 c = getroom(&QRscratch, towhere);
805 /* Then try a mailbox name match */
807 MailboxName(augmented_roomname, &CC->usersupp, towhere);
808 c = getroom(&QRscratch, augmented_roomname);
810 strcpy(towhere, augmented_roomname);
813 /* And if the room was found... */
816 /* let internal programs go directly to any room */
817 if (CC->internal_pgm) {
818 usergoto(towhere, 1, NULL, NULL);
822 /* See if there is an existing user/room relationship */
823 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
825 /* normal clients have to pass through security */
826 if (ra & UA_GOTOALLOWED) {
831 if ((QRscratch.QRflags & QR_MAILBOX) &&
832 ((ra & UA_GOTOALLOWED))) {
833 usergoto(towhere, 1, NULL, NULL);
835 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
836 ((ra & UA_KNOWN) == 0) &&
837 (strcasecmp(QRscratch.QRpasswd, password)) &&
838 (CC->usersupp.axlevel < 6)
840 cprintf("%d wrong or missing passwd\n",
841 ERROR + PASSWORD_REQUIRED);
843 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
844 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
845 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
846 ((ra & UA_KNOWN) == 0) &&
847 (CC->usersupp.axlevel < 6)
849 lprintf(9, "Failed to acquire private room\n");
852 usergoto(towhere, 1, NULL, NULL);
858 NOPE: cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
864 struct usersupp temp;
865 struct cdbdata *cdbus;
867 getuser(&CC->usersupp, CC->curr_user);
868 if (CtdlAccessCheck(ac_room_aide)) return;
870 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
871 cdb_rewind(CDB_USERSUPP);
872 while (cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
873 memset(&temp, 0, sizeof temp);
874 memcpy(&temp, cdbus->ptr, sizeof temp);
877 if ((CC->quickroom.QRflags & QR_INUSE)
878 && (CtdlRoomAccess(&CC->quickroom, &temp) & UA_KNOWN)
880 cprintf("%s\n", temp.fullname);
887 * RDIR command for room directory
897 if (CtdlAccessCheck(ac_logged_in)) return;
899 getroom(&CC->quickroom, CC->quickroom.QRname);
900 getuser(&CC->usersupp, CC->curr_user);
902 if ((CC->quickroom.QRflags & QR_DIRECTORY) == 0) {
903 cprintf("%d not here.\n", ERROR + NOT_HERE);
906 if (((CC->quickroom.QRflags & QR_VISDIR) == 0)
907 && (CC->usersupp.axlevel < 6)
908 && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
909 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
912 cprintf("%d %s|%s/files/%s\n",
913 LISTING_FOLLOWS, config.c_fqdn, BBSDIR, CC->quickroom.QRdirname);
915 sprintf(buf, "ls %s/files/%s >%s 2> /dev/null",
916 BBSDIR, CC->quickroom.QRdirname, CC->temp);
919 sprintf(buf, "%s/files/%s/filedir", BBSDIR, CC->quickroom.QRdirname);
920 fd = fopen(buf, "r");
922 fd = fopen("/dev/null", "r");
924 ls = fopen(CC->temp, "r");
925 while (fgets(flnm, sizeof flnm, ls) != NULL) {
926 flnm[strlen(flnm) - 1] = 0;
927 if (strcasecmp(flnm, "filedir")) {
928 sprintf(buf, "%s/files/%s/%s",
929 BBSDIR, CC->quickroom.QRdirname, flnm);
933 while ((fgets(buf, sizeof buf, fd) != NULL)
934 && (strlen(comment) == 0)) {
935 buf[strlen(buf) - 1] = 0;
936 if ((!strncasecmp(buf, flnm, strlen(flnm)))
937 && (buf[strlen(flnm)] == ' '))
939 &buf[strlen(flnm) + 1],
942 cprintf("%s|%ld|%s\n", flnm, (long)statbuf.st_size, comment);
953 * get room parameters (aide or room aide command)
957 if (CtdlAccessCheck(ac_room_aide)) return;
960 if (is_noneditable(&CC->quickroom)) {
961 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
966 getroom(&CC->quickroom, CC->quickroom.QRname);
967 cprintf("%d%c%s|%s|%s|%d|%d|%d\n",
968 OK, CtdlCheckExpress(),
969 CC->quickroom.QRname,
970 ((CC->quickroom.QRflags & QR_PASSWORDED) ? CC->quickroom.QRpasswd : ""),
971 ((CC->quickroom.QRflags & QR_DIRECTORY) ? CC->quickroom.QRdirname : ""),
972 CC->quickroom.QRflags,
973 (int) CC->quickroom.QRfloor,
974 (int) CC->quickroom.QRorder);
979 * set room parameters (aide or room aide command)
981 void cmd_setr(char *args)
986 char old_name[ROOMNAMELEN];
991 if (CtdlAccessCheck(ac_room_aide)) return;
993 if (is_noneditable(&CC->quickroom)) {
998 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
1004 if (num_parms(args) >= 6) {
1005 fl = cgetfloor(extract_int(args, 5));
1006 if ((fl->f_flags & F_INUSE) == 0) {
1007 cprintf("%d Invalid floor number.\n",
1008 ERROR + INVALID_FLOOR_OPERATION);
1012 if (num_parms(args) >= 7) {
1013 new_order = extract_int(args, 6);
1016 if (new_order > 127)
1019 lgetroom(&CC->quickroom, CC->quickroom.QRname);
1021 /* Non-editable base rooms can't be renamed */
1022 strcpy(old_name, CC->quickroom.QRname);
1024 extract(buf, args, 0);
1025 buf[ROOMNAMELEN] = 0;
1026 safestrncpy(CC->quickroom.QRname, buf,
1027 sizeof CC->quickroom.QRname);
1030 extract(buf, args, 1);
1032 safestrncpy(CC->quickroom.QRpasswd, buf, sizeof CC->quickroom.QRpasswd);
1033 extract(buf, args, 2);
1035 safestrncpy(CC->quickroom.QRdirname, buf,
1036 sizeof CC->quickroom.QRdirname);
1037 CC->quickroom.QRflags = (extract_int(args, 3) | QR_INUSE);
1038 if (num_parms(args) >= 7)
1039 CC->quickroom.QRorder = (char) new_order;
1041 /* Clean up a client boo-boo: if the client set the room to
1042 * guess-name or passworded, ensure that the private flag is
1045 if ((CC->quickroom.QRflags & QR_GUESSNAME)
1046 || (CC->quickroom.QRflags & QR_PASSWORDED))
1047 CC->quickroom.QRflags |= QR_PRIVATE;
1049 /* Kick everyone out if the client requested it (by changing the
1050 * room's generation number)
1052 if (extract_int(args, 4)) {
1053 time(&CC->quickroom.QRgen);
1055 old_floor = CC->quickroom.QRfloor;
1056 if (num_parms(args) >= 6) {
1057 CC->quickroom.QRfloor = extract_int(args, 5);
1059 /* Write the room record back to disk */
1060 lputroom(&CC->quickroom);
1062 /* If the room name changed, then there are now two room records,
1063 * so we have to delete the old one.
1065 if (strcasecmp(CC->quickroom.QRname, old_name)) {
1066 b_deleteroom(old_name);
1068 /* adjust the floor reference counts */
1069 lgetfloor(&flbuf, old_floor);
1070 --flbuf.f_ref_count;
1071 lputfloor(&flbuf, old_floor);
1072 lgetfloor(&flbuf, CC->quickroom.QRfloor);
1073 ++flbuf.f_ref_count;
1074 lputfloor(&flbuf, CC->quickroom.QRfloor);
1076 /* create a room directory if necessary */
1077 if (CC->quickroom.QRflags & QR_DIRECTORY) {
1079 "mkdir ./files/%s </dev/null >/dev/null 2>/dev/null",
1080 CC->quickroom.QRdirname);
1083 sprintf(buf, "%s> edited by %s\n", CC->quickroom.QRname, CC->curr_user);
1085 cprintf("%d Ok\n", OK);
1091 * get the name of the room aide for this room
1095 struct usersupp usbuf;
1097 if (CtdlAccessCheck(ac_logged_in)) return;
1099 if (is_noneditable(&CC->quickroom)) {
1100 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
1103 if (getuserbynumber(&usbuf, CC->quickroom.QRroomaide) == 0) {
1104 cprintf("%d %s\n", OK, usbuf.fullname);
1106 cprintf("%d \n", OK);
1112 * set the room aide for this room
1114 void cmd_seta(char *new_ra)
1116 struct usersupp usbuf;
1121 if (CtdlAccessCheck(ac_room_aide)) return;
1123 if (getuser(&usbuf, new_ra) != 0) {
1126 newu = usbuf.usernum;
1129 lgetroom(&CC->quickroom, CC->quickroom.QRname);
1131 if (CC->quickroom.QRroomaide != newu) {
1134 CC->quickroom.QRroomaide = newu;
1135 lputroom(&CC->quickroom);
1138 * We have to post the change notice _after_ writing changes to
1139 * the room table, otherwise it would deadlock!
1141 if (post_notice == 1) {
1142 sprintf(buf, "%s is now room aide for %s>\n",
1143 usbuf.fullname, CC->quickroom.QRname);
1146 cprintf("%d Ok\n", OK);
1150 * Generate an associated file name for a room
1152 void assoc_file_name(char *buf, struct quickroom *qrbuf, char *prefix)
1154 sprintf(buf, "./%s/%ld", prefix, qrbuf->QRnumber);
1158 * retrieve info file for this room
1166 assoc_file_name(filename, &CC->quickroom, "info");
1167 info_fp = fopen(filename, "r");
1169 if (info_fp == NULL) {
1170 cprintf("%d No info file.\n", ERROR);
1173 cprintf("%d Info:\n", LISTING_FOLLOWS);
1174 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1175 if (strlen(buf) > 0)
1176 buf[strlen(buf) - 1] = 0;
1177 cprintf("%s\n", buf);
1184 * Back end processing to delete a room and everything associated with it
1186 void delete_room(struct quickroom *qrbuf)
1191 lprintf(9, "Deleting room <%s>\n", qrbuf->QRname);
1193 /* Delete the info file */
1194 assoc_file_name(filename, qrbuf, "info");
1197 /* Delete the image file */
1198 assoc_file_name(filename, qrbuf, "images");
1201 /* Delete the room's network config file */
1202 assoc_file_name(filename, qrbuf, "netconfigs");
1205 /* Delete the messages in the room
1206 * (Careful: this opens an S_QUICKROOM critical section!)
1208 CtdlDeleteMessages(qrbuf->QRname, 0L, "");
1210 /* Flag the room record as not in use */
1211 lgetroom(qrbuf, qrbuf->QRname);
1215 /* then decrement the reference count for the floor */
1216 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1217 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1218 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1220 /* Delete the room record from the database! */
1221 b_deleteroom(qrbuf->QRname);
1227 * Check access control for deleting a room
1229 int CtdlDoIHavePermissionToDeleteThisRoom(struct quickroom *qr) {
1231 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1235 if (is_noneditable(qr)) {
1240 * For mailboxes, check stuff
1242 if (qr->QRflags & QR_MAILBOX) {
1244 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1246 if (atol(qr->QRname) != CC->usersupp.usernum) {
1247 return(0); /* not my room */
1250 /* Can't delete your Mail> room */
1251 if (!strcasecmp(&qr->QRname[12], MAILROOM)) return(0);
1253 /* Otherwise it's ok */
1258 * For normal rooms, just check for aide or room aide status.
1261 return(is_room_aide());
1264 /* Should never get to this point, but to keep the compiler quiet... */
1269 * aide command: kill the current room
1271 void cmd_kill(char *argbuf)
1274 char deleted_room_name[ROOMNAMELEN];
1277 kill_ok = extract_int(argbuf, 0);
1279 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->quickroom) == 0) {
1280 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1284 strcpy(deleted_room_name, CC->quickroom.QRname);
1285 delete_room(&CC->quickroom); /* Do the dirty work */
1286 usergoto(BASEROOM, 0, NULL, NULL); /* Return to the Lobby */
1288 /* tell the world what we did */
1289 sprintf(aaa, "%s> killed by %s\n",
1290 deleted_room_name, CC->curr_user);
1292 cprintf("%d '%s' deleted.\n", OK, deleted_room_name);
1294 cprintf("%d ok to delete.\n", OK);
1300 * Internal code to create a new room (returns room flags)
1302 * Room types: 0=public, 1=guessname, 2=passworded, 3=inv-only,
1303 * 4=mailbox, 5=mailbox, but caller supplies namespace
1305 unsigned create_room(char *new_room_name,
1307 char *new_room_pass,
1312 struct quickroom qrbuf;
1316 lprintf(9, "create_room(%s)\n", new_room_name);
1317 if (getroom(&qrbuf, new_room_name) == 0) {
1318 lprintf(9, "%s already exists.\n", new_room_name);
1319 return (0); /* already exists */
1323 memset(&qrbuf, 0, sizeof(struct quickroom));
1324 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1325 qrbuf.QRflags = QR_INUSE;
1326 if (new_room_type > 0)
1327 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1328 if (new_room_type == 1)
1329 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1330 if (new_room_type == 2)
1331 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1332 if ( (new_room_type == 4) || (new_room_type == 5) )
1333 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1335 /* If the user is requesting a personal room, set up the room
1336 * name accordingly (prepend the user number)
1338 if (new_room_type == 4) {
1339 MailboxName(qrbuf.QRname, &CC->usersupp, new_room_name);
1342 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1345 /* If the room is private, and the system administrator has elected
1346 * to automatically grant room aide privileges, do so now; otherwise,
1347 * set the room aide to undefined.
1349 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1350 qrbuf.QRroomaide = CC->usersupp.usernum;
1352 qrbuf.QRroomaide = (-1L);
1356 * If the caller is only interested in testing whether this will work,
1357 * return now without creating the room.
1359 if (!really_create) return (qrbuf.QRflags);
1361 qrbuf.QRnumber = get_new_room_number();
1362 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1363 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1364 qrbuf.QRfloor = new_room_floor;
1366 /* save what we just did... */
1369 /* bump the reference count on whatever floor the room is on */
1370 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1371 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1372 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1374 /* be sure not to kick the creator out of the room! */
1375 lgetuser(&CC->usersupp, CC->curr_user);
1376 CtdlGetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1377 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1378 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1379 CtdlSetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1380 lputuser(&CC->usersupp);
1382 /* resume our happy day */
1383 return (qrbuf.QRflags);
1390 void cmd_cre8(char *args)
1393 char new_room_name[SIZ];
1395 char new_room_pass[SIZ];
1401 cre8_ok = extract_int(args, 0);
1402 extract(new_room_name, args, 1);
1403 new_room_name[ROOMNAMELEN - 1] = 0;
1404 new_room_type = extract_int(args, 2);
1405 extract(new_room_pass, args, 3);
1406 new_room_pass[9] = 0;
1409 if ((strlen(new_room_name) == 0) && (cre8_ok == 1)) {
1410 cprintf("%d Invalid room name.\n", ERROR);
1414 if (!strcasecmp(new_room_name, MAILROOM)) {
1415 cprintf("%d '%s' already exists.\n",
1416 ERROR + ALREADY_EXISTS, new_room_name);
1420 if (num_parms(args) >= 5) {
1421 fl = cgetfloor(extract_int(args, 4));
1422 if ((fl->f_flags & F_INUSE) == 0) {
1423 cprintf("%d Invalid floor number.\n",
1424 ERROR + INVALID_FLOOR_OPERATION);
1427 new_room_floor = extract_int(args, 4);
1431 if (CtdlAccessCheck(ac_logged_in)) return;
1433 if (CC->usersupp.axlevel < config.c_createax) {
1434 cprintf("%d You need higher access to create rooms.\n",
1435 ERROR + HIGHER_ACCESS_REQUIRED);
1439 if ((strlen(new_room_name) == 0) && (cre8_ok == 0)) {
1440 cprintf("%d Ok to create rooms.\n", OK);
1444 if ((new_room_type < 0) || (new_room_type > 4)) {
1445 cprintf("%d Invalid room type.\n", ERROR);
1449 /* Check to make sure the requested room name doesn't already exist */
1450 newflags = create_room(new_room_name,
1451 new_room_type, new_room_pass, new_room_floor, 0);
1452 if (newflags == 0) {
1453 cprintf("%d '%s' already exists.\n",
1454 ERROR + ALREADY_EXISTS, new_room_name);
1459 cprintf("%d OK to create '%s'\n", OK, new_room_name);
1463 /* If we reach this point, the room needs to be created. */
1465 newflags = create_room(new_room_name,
1466 new_room_type, new_room_pass, new_room_floor, 1);
1468 /* post a message in Aide> describing the new room */
1469 safestrncpy(aaa, new_room_name, sizeof aaa);
1470 strcat(aaa, "> created by ");
1471 strcat(aaa, CC->usersupp.fullname);
1472 if (newflags & QR_MAILBOX)
1473 strcat(aaa, " [personal]");
1474 else if (newflags & QR_PRIVATE)
1475 strcat(aaa, " [private]");
1476 if (newflags & QR_GUESSNAME)
1477 strcat(aaa, "[guessname] ");
1478 if (newflags & QR_PASSWORDED) {
1479 strcat(aaa, "\n Password: ");
1480 strcat(aaa, new_room_pass);
1485 cprintf("%d '%s' has been created.\n", OK, new_room_name);
1490 void cmd_einf(char *ok)
1491 { /* enter info file for current room */
1493 char infofilename[SIZ];
1496 if (CtdlAccessCheck(ac_room_aide)) return;
1498 if (atoi(ok) == 0) {
1499 cprintf("%d Ok.\n", OK);
1502 assoc_file_name(infofilename, &CC->quickroom, "info");
1503 lprintf(9, "opening\n");
1504 fp = fopen(infofilename, "w");
1505 lprintf(9, "checking\n");
1507 cprintf("%d Cannot open %s: %s\n",
1508 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1511 cprintf("%d Send info...\n", SEND_LISTING);
1515 if (strcmp(buf, "000"))
1516 fprintf(fp, "%s\n", buf);
1517 } while (strcmp(buf, "000"));
1520 /* now update the room index so people will see our new info */
1521 lgetroom(&CC->quickroom, CC->quickroom.QRname); /* lock so no one steps on us */
1522 CC->quickroom.QRinfo = CC->quickroom.QRhighest + 1L;
1523 lputroom(&CC->quickroom);
1528 * cmd_lflr() - List all known floors
1535 if (CtdlAccessCheck(ac_logged_in)) return;
1537 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1539 for (a = 0; a < MAXFLOORS; ++a) {
1540 getfloor(&flbuf, a);
1541 if (flbuf.f_flags & F_INUSE) {
1542 cprintf("%d|%s|%d\n",
1554 * create a new floor
1556 void cmd_cflr(char *argbuf)
1558 char new_floor_name[SIZ];
1561 int free_slot = (-1);
1564 extract(new_floor_name, argbuf, 0);
1565 cflr_ok = extract_int(argbuf, 1);
1568 if (CtdlAccessCheck(ac_aide)) return;
1570 for (a = 0; a < MAXFLOORS; ++a) {
1571 getfloor(&flbuf, a);
1573 /* note any free slots while we're scanning... */
1574 if (((flbuf.f_flags & F_INUSE) == 0)
1578 /* check to see if it already exists */
1579 if ((!strcasecmp(flbuf.f_name, new_floor_name))
1580 && (flbuf.f_flags & F_INUSE)) {
1581 cprintf("%d Floor '%s' already exists.\n",
1582 ERROR + ALREADY_EXISTS,
1588 if (free_slot < 0) {
1589 cprintf("%d There is no space available for a new floor.\n",
1590 ERROR + INVALID_FLOOR_OPERATION);
1594 cprintf("%d ok to create...\n", OK);
1597 lgetfloor(&flbuf, free_slot);
1598 flbuf.f_flags = F_INUSE;
1599 flbuf.f_ref_count = 0;
1600 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
1601 lputfloor(&flbuf, free_slot);
1602 cprintf("%d %d\n", OK, free_slot);
1610 void cmd_kflr(char *argbuf)
1613 int floor_to_delete;
1617 floor_to_delete = extract_int(argbuf, 0);
1618 kflr_ok = extract_int(argbuf, 1);
1620 if (CtdlAccessCheck(ac_aide)) return;
1622 lgetfloor(&flbuf, floor_to_delete);
1625 if ((flbuf.f_flags & F_INUSE) == 0) {
1626 cprintf("%d Floor %d not in use.\n",
1627 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
1630 if (flbuf.f_ref_count != 0) {
1631 cprintf("%d Cannot delete; floor contains %d rooms.\n",
1632 ERROR + INVALID_FLOOR_OPERATION,
1637 cprintf("%d Ok\n", OK);
1639 cprintf("%d Ok to delete...\n", OK);
1646 if ((delete_ok == 1) && (kflr_ok == 1))
1648 lputfloor(&flbuf, floor_to_delete);
1654 void cmd_eflr(char *argbuf)
1660 np = num_parms(argbuf);
1662 cprintf("%d Usage error.\n", ERROR);
1666 if (CtdlAccessCheck(ac_aide)) return;
1668 floor_num = extract_int(argbuf, 0);
1669 lgetfloor(&flbuf, floor_num);
1670 if ((flbuf.f_flags & F_INUSE) == 0) {
1671 lputfloor(&flbuf, floor_num);
1672 cprintf("%d Floor %d is not in use.\n",
1673 ERROR + INVALID_FLOOR_OPERATION, floor_num);
1677 extract(flbuf.f_name, argbuf, 1);
1678 lputfloor(&flbuf, floor_num);
1680 cprintf("%d Ok\n", OK);