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);
61 /* For mailbox rooms, only allow access to the owner */
62 if (roombuf->QRflags & QR_MAILBOX) {
63 if (userbuf->usernum != atol(roombuf->QRname)) {
67 /* Locate any applicable user/room relationships */
68 CtdlGetRelationship(&vbuf, userbuf, roombuf);
70 /* Force the properties of the Aide room */
71 if (!strcasecmp(roombuf->QRname, AIDEROOM)) {
72 if (userbuf->axlevel >= 6) {
73 retval = UA_KNOWN | UA_GOTOALLOWED;
79 /* For mailboxes, we skip all the access stuff (and we've
80 * already checked by this point that the mailbox belongs
83 if (roombuf->QRflags & QR_MAILBOX) {
84 retval = UA_KNOWN | UA_GOTOALLOWED;
85 /* goto NEWMSG; allow users to zap mailboxes now */
87 /* If this is a public room, it's accessible... */
88 if ((roombuf->QRflags & QR_PRIVATE) == 0) {
89 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
91 /* If this is a preferred users only room, check access level */
92 if (roombuf->QRflags & QR_PREFONLY) {
93 if (userbuf->axlevel < 5) {
94 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
97 /* For private rooms, check the generation number matchups */
98 if (roombuf->QRflags & QR_PRIVATE) {
100 /* An explicit match means the user belongs in this room */
101 if (vbuf.v_flags & V_ACCESS) {
102 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
104 /* Otherwise, check if this is a guess-name or passworded
105 * room. If it is, a goto may at least be attempted
107 else if ((roombuf->QRflags & QR_PRIVATE)
108 || (roombuf->QRflags & QR_PASSWORDED)) {
109 retval = retval & ~UA_KNOWN;
110 retval = retval | UA_GOTOALLOWED;
113 /* Check to see if the user has forgotten this room */
114 if (vbuf.v_flags & V_FORGET) {
115 retval = retval & ~UA_KNOWN;
116 retval = retval | UA_ZAPPED;
118 /* If user is explicitly locked out of this room, deny everything */
119 if (vbuf.v_flags & V_LOCKOUT) {
120 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
123 /* Aides get access to everything */
124 if (userbuf->axlevel >= 6) {
125 if (vbuf.v_flags & V_FORGET) {
126 retval = retval | UA_GOTOALLOWED;
129 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
133 NEWMSG: /* By the way, we also check for the presence of new messages */
134 if (is_msg_in_mset(vbuf.v_seen, roombuf->QRhighest) == 0) {
135 retval = retval | UA_HASNEWMSGS;
138 /* System rooms never show up in the list. */
139 if (roombuf->QRflags2 & QR2_SYSTEM) {
140 retval = retval & ~UA_KNOWN;
146 * Self-checking stuff for a room record read into memory
148 void room_sanity_check(struct quickroom *qrbuf)
150 /* Mailbox rooms are always on the lowest floor */
151 if (qrbuf->QRflags & QR_MAILBOX) {
154 /* Listing order of 0 is illegal except for base rooms */
155 if (qrbuf->QRorder == 0)
156 if (!is_noneditable(qrbuf))
162 * getroom() - retrieve room data from disk
164 int getroom(struct quickroom *qrbuf, char *room_name)
166 struct cdbdata *cdbqr;
167 char lowercase_name[ROOMNAMELEN];
168 char personal_lowercase_name[ROOMNAMELEN];
171 for (a = 0; room_name[a] && a < sizeof lowercase_name - 1; ++a) {
172 lowercase_name[a] = tolower(room_name[a]);
174 lowercase_name[a] = 0;
176 memset(qrbuf, 0, sizeof(struct quickroom));
178 /* First, try the public namespace */
179 cdbqr = cdb_fetch(CDB_QUICKROOM,
180 lowercase_name, strlen(lowercase_name));
182 /* If that didn't work, try the user's personal namespace */
184 sprintf(personal_lowercase_name, "%010ld.%s",
185 CC->usersupp.usernum, lowercase_name);
186 cdbqr = cdb_fetch(CDB_QUICKROOM,
187 personal_lowercase_name,
188 strlen(personal_lowercase_name));
191 memcpy(qrbuf, cdbqr->ptr,
192 ((cdbqr->len > sizeof(struct quickroom)) ?
193 sizeof(struct quickroom) : cdbqr->len));
196 room_sanity_check(qrbuf);
205 * lgetroom() - same as getroom() but locks the record (if supported)
207 int lgetroom(struct quickroom *qrbuf, char *room_name)
210 retval = getroom(qrbuf, room_name);
211 if (retval == 0) begin_critical_section(S_QUICKROOM);
217 * b_putroom() - back end to putroom() and b_deleteroom()
218 * (if the supplied buffer is NULL, delete the room record)
220 void b_putroom(struct quickroom *qrbuf, char *room_name)
222 char lowercase_name[ROOMNAMELEN];
225 for (a = 0; a <= strlen(room_name); ++a) {
226 lowercase_name[a] = tolower(room_name[a]);
230 cdb_delete(CDB_QUICKROOM,
231 lowercase_name, strlen(lowercase_name));
233 time(&qrbuf->QRmtime);
234 cdb_store(CDB_QUICKROOM,
235 lowercase_name, strlen(lowercase_name),
236 qrbuf, sizeof(struct quickroom));
242 * putroom() - store room data to disk
244 void putroom(struct quickroom *qrbuf) {
245 b_putroom(qrbuf, qrbuf->QRname);
250 * b_deleteroom() - delete a room record from disk
252 void b_deleteroom(char *room_name) {
253 b_putroom(NULL, room_name);
259 * lputroom() - same as putroom() but unlocks the record (if supported)
261 void lputroom(struct quickroom *qrbuf)
265 end_critical_section(S_QUICKROOM);
269 /****************************************************************************/
272 * getfloor() - retrieve floor data from disk
274 void getfloor(struct floor *flbuf, int floor_num)
276 struct cdbdata *cdbfl;
278 memset(flbuf, 0, sizeof(struct floor));
279 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
281 memcpy(flbuf, cdbfl->ptr,
282 ((cdbfl->len > sizeof(struct floor)) ?
283 sizeof(struct floor) : cdbfl->len));
286 if (floor_num == 0) {
287 strcpy(flbuf->f_name, "Main Floor");
288 flbuf->f_flags = F_INUSE;
289 flbuf->f_ref_count = 3;
296 * lgetfloor() - same as getfloor() but locks the record (if supported)
298 void lgetfloor(struct floor *flbuf, int floor_num)
301 begin_critical_section(S_FLOORTAB);
302 getfloor(flbuf, floor_num);
307 * cgetfloor() - Get floor record from *cache* (loads from disk if needed)
309 * This is strictly a performance hack.
311 struct floor *cgetfloor(int floor_num) {
312 static int initialized = 0;
315 if (initialized == 0) {
316 for (i=0; i<MAXFLOORS; ++i) {
317 floorcache[floor_num] = NULL;
322 if (floorcache[floor_num] == NULL) {
323 floorcache[floor_num] = mallok(sizeof(struct floor));
324 getfloor(floorcache[floor_num], floor_num);
327 return(floorcache[floor_num]);
333 * putfloor() - store floor data on disk
335 void putfloor(struct floor *flbuf, int floor_num)
337 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
338 flbuf, sizeof(struct floor));
340 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
341 if (floorcache[floor_num] != NULL) {
342 phree(floorcache[floor_num]);
343 floorcache[floor_num] = NULL;
349 * lputfloor() - same as putfloor() but unlocks the record (if supported)
351 void lputfloor(struct floor *flbuf, int floor_num)
354 putfloor(flbuf, floor_num);
355 end_critical_section(S_FLOORTAB);
361 * Traverse the room file...
363 void ForEachRoom(void (*CallBack) (struct quickroom *EachRoom, void *out_data),
366 struct quickroom qrbuf;
367 struct cdbdata *cdbqr;
369 cdb_rewind(CDB_QUICKROOM);
371 while (cdbqr = cdb_next_item(CDB_QUICKROOM), cdbqr != NULL) {
372 memset(&qrbuf, 0, sizeof(struct quickroom));
373 memcpy(&qrbuf, cdbqr->ptr,
374 ((cdbqr->len > sizeof(struct quickroom)) ?
375 sizeof(struct quickroom) : cdbqr->len));
377 room_sanity_check(&qrbuf);
378 if (qrbuf.QRflags & QR_INUSE)
379 (*CallBack)(&qrbuf, in_data);
385 * delete_msglist() - delete room message pointers
387 void delete_msglist(struct quickroom *whichroom)
389 struct cdbdata *cdbml;
391 /* Make sure the msglist we're deleting actually exists, otherwise
392 * gdbm will complain when we try to delete an invalid record
394 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
398 /* Go ahead and delete it */
399 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
407 * sort message pointers
408 * (returns new msg count)
410 int sort_msglist(long listptrs[], int oldcount)
421 for (a = numitems - 2; a >= 0; --a) {
422 for (b = 0; b <= a; ++b) {
423 if (listptrs[b] > (listptrs[b + 1])) {
425 hold2 = listptrs[b + 1];
427 listptrs[b + 1] = hold1;
432 /* and yank any nulls */
433 while ((numitems > 0) && (listptrs[0] == 0L)) {
434 memcpy(&listptrs[0], &listptrs[1],
435 (sizeof(long) * (numitems - 1)));
444 * Determine whether a given room is non-editable.
446 int is_noneditable(struct quickroom *qrbuf)
449 /* Lobby> and Aide> are non-editable */
450 if (!strcasecmp(qrbuf->QRname, BASEROOM))
452 else if (!strcasecmp(qrbuf->QRname, AIDEROOM))
455 /* Mail> rooms are also non-editable */
456 else if ( (qrbuf->QRflags & QR_MAILBOX)
457 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
460 /* Everything else is editable */
468 * Back-back-end for all room listing commands
470 void list_roomname(struct quickroom *qrbuf)
472 char truncated_roomname[ROOMNAMELEN];
474 /* For mailbox rooms, chop off the owner prefix */
475 if (qrbuf->QRflags & QR_MAILBOX) {
476 strcpy(truncated_roomname, qrbuf->QRname);
477 strcpy(truncated_roomname, &truncated_roomname[11]);
478 cprintf("%s", truncated_roomname);
480 /* For all other rooms, just display the name in its entirety */
482 cprintf("%s", qrbuf->QRname);
485 /* ...and now the other parameters */
486 cprintf("|%u|%d|%d\n",
488 (int) qrbuf->QRfloor,
489 (int) qrbuf->QRorder);
494 * cmd_lrms() - List all accessible rooms, known or forgotten
496 void cmd_lrms_backend(struct quickroom *qrbuf, void *data)
498 int FloorBeingSearched = (-1);
499 FloorBeingSearched = *(int *)data;
501 if (((CtdlRoomAccess(qrbuf, &CC->usersupp)
502 & (UA_KNOWN | UA_ZAPPED)))
503 && ((qrbuf->QRfloor == (FloorBeingSearched))
504 || ((FloorBeingSearched) < 0)))
505 list_roomname(qrbuf);
508 void cmd_lrms(char *argbuf)
510 int FloorBeingSearched = (-1);
511 if (strlen(argbuf) > 0)
512 FloorBeingSearched = extract_int(argbuf, 0);
514 if (CtdlAccessCheck(ac_logged_in)) return;
516 if (getuser(&CC->usersupp, CC->curr_user)) {
517 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
520 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
522 ForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
529 * cmd_lkra() - List all known rooms
531 void cmd_lkra_backend(struct quickroom *qrbuf, void *data)
533 int FloorBeingSearched = (-1);
534 FloorBeingSearched = *(int *)data;
536 if (((CtdlRoomAccess(qrbuf, &CC->usersupp)
538 && ((qrbuf->QRfloor == (FloorBeingSearched))
539 || ((FloorBeingSearched) < 0)))
540 list_roomname(qrbuf);
543 void cmd_lkra(char *argbuf)
545 int FloorBeingSearched = (-1);
546 if (strlen(argbuf) > 0)
547 FloorBeingSearched = extract_int(argbuf, 0);
549 if (CtdlAccessCheck(ac_logged_in)) return;
551 if (getuser(&CC->usersupp, CC->curr_user)) {
552 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
555 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
557 ForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
564 * cmd_lkrn() - List all known rooms with new messages
566 void cmd_lkrn_backend(struct quickroom *qrbuf, void *data)
569 int FloorBeingSearched = (-1);
570 FloorBeingSearched = *(int *)data;
572 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
574 && (ra & UA_HASNEWMSGS)
575 && ((qrbuf->QRfloor == (FloorBeingSearched))
576 || ((FloorBeingSearched) < 0)))
577 list_roomname(qrbuf);
580 void cmd_lkrn(char *argbuf)
582 int FloorBeingSearched = (-1);
583 if (strlen(argbuf) > 0)
584 FloorBeingSearched = extract_int(argbuf, 0);
586 if (CtdlAccessCheck(ac_logged_in)) return;
588 if (getuser(&CC->usersupp, CC->curr_user)) {
589 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
592 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
594 ForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
601 * cmd_lkro() - List all known rooms
603 void cmd_lkro_backend(struct quickroom *qrbuf, void *data)
606 int FloorBeingSearched = (-1);
607 FloorBeingSearched = *(int *)data;
609 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
611 && ((ra & UA_HASNEWMSGS) == 0)
612 && ((qrbuf->QRfloor == (FloorBeingSearched))
613 || ((FloorBeingSearched) < 0)))
614 list_roomname(qrbuf);
617 void cmd_lkro(char *argbuf)
619 int FloorBeingSearched = (-1);
620 if (strlen(argbuf) > 0)
621 FloorBeingSearched = extract_int(argbuf, 0);
623 if (CtdlAccessCheck(ac_logged_in)) return;
625 if (getuser(&CC->usersupp, CC->curr_user)) {
626 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
629 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
631 ForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
638 * cmd_lzrm() - List all forgotten rooms
640 void cmd_lzrm_backend(struct quickroom *qrbuf, void *data)
643 int FloorBeingSearched = (-1);
644 FloorBeingSearched = *(int *)data;
646 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
647 if ((ra & UA_GOTOALLOWED)
649 && ((qrbuf->QRfloor == (FloorBeingSearched))
650 || ((FloorBeingSearched) < 0)))
651 list_roomname(qrbuf);
654 void cmd_lzrm(char *argbuf)
656 int FloorBeingSearched = (-1);
657 if (strlen(argbuf) > 0)
658 FloorBeingSearched = extract_int(argbuf, 0);
660 if (CtdlAccessCheck(ac_logged_in)) return;
662 if (getuser(&CC->usersupp, CC->curr_user)) {
663 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
666 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
668 ForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
674 void usergoto(char *where, int display_result, int *retmsgs, int *retnew)
677 int new_messages = 0;
678 int total_messages = 0;
682 int newmailcount = 0;
684 char truncated_roomname[ROOMNAMELEN];
685 struct cdbdata *cdbfr;
686 long *msglist = NULL;
689 strcpy(CC->quickroom.QRname, where);
690 getroom(&CC->quickroom, where);
692 lgetuser(&CC->usersupp, CC->curr_user);
693 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
695 /* Know the room ... but not if it's the page log room */
696 if (strcasecmp(where, config.c_logpages)) {
697 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
698 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
700 CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
701 lputuser(&CC->usersupp);
703 /* check for new mail */
704 newmailcount = NewMailCount();
706 /* set info to 1 if the user needs to read the room's info file */
707 if (CC->quickroom.QRinfo > vbuf.v_lastseen)
711 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->quickroom.QRnumber, sizeof(long));
713 msglist = mallok(cdbfr->len);
714 memcpy(msglist, cdbfr->ptr, cdbfr->len);
715 num_msgs = cdbfr->len / sizeof(long);
719 if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
720 if (msglist[a] > 0L) {
722 if (is_msg_in_mset(vbuf.v_seen, msglist[a]) == 0) {
728 if (msglist != NULL) phree(msglist);
730 if (CC->quickroom.QRflags & QR_MAILBOX)
735 if ((CC->quickroom.QRroomaide == CC->usersupp.usernum)
736 || (CC->usersupp.axlevel >= 6))
741 strcpy(truncated_roomname, CC->quickroom.QRname);
742 if (CC->quickroom.QRflags & QR_MAILBOX) {
743 strcpy(truncated_roomname, &truncated_roomname[11]);
746 if (retmsgs != NULL) *retmsgs = total_messages;
747 if (retnew != NULL) *retnew = new_messages;
748 lprintf(9, "<%s> %d new of %d total messages\n",
749 CC->quickroom.QRname, new_messages, total_messages);
752 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d\n",
753 OK, CtdlCheckExpress(),
755 new_messages, total_messages,
756 info, CC->quickroom.QRflags,
757 CC->quickroom.QRhighest,
759 rmailflag, raideflag, newmailcount,
760 CC->quickroom.QRfloor);
766 * cmd_goto() - goto a new room
768 void cmd_goto(char *gargs)
770 struct quickroom QRscratch;
774 char augmented_roomname[SIZ];
778 if (CtdlAccessCheck(ac_logged_in)) return;
780 extract(towhere, gargs, 0);
781 extract(password, gargs, 1);
783 getuser(&CC->usersupp, CC->curr_user);
785 if (!strcasecmp(towhere, "_BASEROOM_"))
786 strcpy(towhere, BASEROOM);
788 if (!strcasecmp(towhere, "_MAIL_"))
789 strcpy(towhere, MAILROOM);
791 if (!strcasecmp(towhere, "_BITBUCKET_"))
792 strcpy(towhere, config.c_twitroom);
795 /* First try a regular match */
796 c = getroom(&QRscratch, towhere);
798 /* Then try a mailbox name match */
800 MailboxName(augmented_roomname, &CC->usersupp, towhere);
801 c = getroom(&QRscratch, augmented_roomname);
803 strcpy(towhere, augmented_roomname);
806 /* And if the room was found... */
809 /* let internal programs go directly to any room */
810 if (CC->internal_pgm) {
811 usergoto(towhere, 1, NULL, NULL);
815 /* See if there is an existing user/room relationship */
816 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
818 /* normal clients have to pass through security */
819 if (ra & UA_GOTOALLOWED) {
824 if ((QRscratch.QRflags & QR_MAILBOX) &&
825 ((ra & UA_GOTOALLOWED))) {
826 usergoto(towhere, 1, NULL, NULL);
828 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
829 ((ra & UA_KNOWN) == 0) &&
830 (strcasecmp(QRscratch.QRpasswd, password)) &&
831 (CC->usersupp.axlevel < 6)
833 cprintf("%d wrong or missing passwd\n",
834 ERROR + PASSWORD_REQUIRED);
836 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
837 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
838 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
839 ((ra & UA_KNOWN) == 0) &&
840 (CC->usersupp.axlevel < 6)
842 lprintf(9, "Failed to acquire private room\n");
845 usergoto(towhere, 1, NULL, NULL);
851 NOPE: cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
857 struct usersupp temp;
858 struct cdbdata *cdbus;
860 getuser(&CC->usersupp, CC->curr_user);
861 if (CtdlAccessCheck(ac_room_aide)) return;
863 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
864 cdb_rewind(CDB_USERSUPP);
865 while (cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
866 memset(&temp, 0, sizeof temp);
867 memcpy(&temp, cdbus->ptr, sizeof temp);
870 if ((CC->quickroom.QRflags & QR_INUSE)
871 && (CtdlRoomAccess(&CC->quickroom, &temp) & UA_KNOWN)
873 cprintf("%s\n", temp.fullname);
880 * RDIR command for room directory
890 if (CtdlAccessCheck(ac_logged_in)) return;
892 getroom(&CC->quickroom, CC->quickroom.QRname);
893 getuser(&CC->usersupp, CC->curr_user);
895 if ((CC->quickroom.QRflags & QR_DIRECTORY) == 0) {
896 cprintf("%d not here.\n", ERROR + NOT_HERE);
899 if (((CC->quickroom.QRflags & QR_VISDIR) == 0)
900 && (CC->usersupp.axlevel < 6)
901 && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
902 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
905 cprintf("%d %s|%s/files/%s\n",
906 LISTING_FOLLOWS, config.c_fqdn, BBSDIR, CC->quickroom.QRdirname);
908 sprintf(buf, "ls %s/files/%s >%s 2> /dev/null",
909 BBSDIR, CC->quickroom.QRdirname, CC->temp);
912 sprintf(buf, "%s/files/%s/filedir", BBSDIR, CC->quickroom.QRdirname);
913 fd = fopen(buf, "r");
915 fd = fopen("/dev/null", "r");
917 ls = fopen(CC->temp, "r");
918 while (fgets(flnm, sizeof flnm, ls) != NULL) {
919 flnm[strlen(flnm) - 1] = 0;
920 if (strcasecmp(flnm, "filedir")) {
921 sprintf(buf, "%s/files/%s/%s",
922 BBSDIR, CC->quickroom.QRdirname, flnm);
926 while ((fgets(buf, sizeof buf, fd) != NULL)
927 && (strlen(comment) == 0)) {
928 buf[strlen(buf) - 1] = 0;
929 if ((!strncasecmp(buf, flnm, strlen(flnm)))
930 && (buf[strlen(flnm)] == ' '))
932 &buf[strlen(flnm) + 1],
935 cprintf("%s|%ld|%s\n", flnm, (long)statbuf.st_size, comment);
946 * get room parameters (aide or room aide command)
950 if (CtdlAccessCheck(ac_room_aide)) return;
953 if (is_noneditable(&CC->quickroom)) {
954 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
959 getroom(&CC->quickroom, CC->quickroom.QRname);
960 cprintf("%d%c%s|%s|%s|%d|%d|%d\n",
961 OK, CtdlCheckExpress(),
962 CC->quickroom.QRname,
963 ((CC->quickroom.QRflags & QR_PASSWORDED) ? CC->quickroom.QRpasswd : ""),
964 ((CC->quickroom.QRflags & QR_DIRECTORY) ? CC->quickroom.QRdirname : ""),
965 CC->quickroom.QRflags,
966 (int) CC->quickroom.QRfloor,
967 (int) CC->quickroom.QRorder);
972 * set room parameters (aide or room aide command)
974 void cmd_setr(char *args)
979 char old_name[ROOMNAMELEN];
984 if (CtdlAccessCheck(ac_room_aide)) return;
986 if (is_noneditable(&CC->quickroom)) {
991 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
997 if (num_parms(args) >= 6) {
998 fl = cgetfloor(extract_int(args, 5));
999 if ((fl->f_flags & F_INUSE) == 0) {
1000 cprintf("%d Invalid floor number.\n",
1001 ERROR + INVALID_FLOOR_OPERATION);
1005 if (num_parms(args) >= 7) {
1006 new_order = extract_int(args, 6);
1009 if (new_order > 127)
1012 lgetroom(&CC->quickroom, CC->quickroom.QRname);
1014 /* Non-editable base rooms can't be renamed */
1015 strcpy(old_name, CC->quickroom.QRname);
1017 extract(buf, args, 0);
1018 buf[ROOMNAMELEN] = 0;
1019 safestrncpy(CC->quickroom.QRname, buf,
1020 sizeof CC->quickroom.QRname);
1023 extract(buf, args, 1);
1025 safestrncpy(CC->quickroom.QRpasswd, buf, sizeof CC->quickroom.QRpasswd);
1026 extract(buf, args, 2);
1028 safestrncpy(CC->quickroom.QRdirname, buf,
1029 sizeof CC->quickroom.QRdirname);
1030 CC->quickroom.QRflags = (extract_int(args, 3) | QR_INUSE);
1031 if (num_parms(args) >= 7)
1032 CC->quickroom.QRorder = (char) new_order;
1034 /* Clean up a client boo-boo: if the client set the room to
1035 * guess-name or passworded, ensure that the private flag is
1038 if ((CC->quickroom.QRflags & QR_GUESSNAME)
1039 || (CC->quickroom.QRflags & QR_PASSWORDED))
1040 CC->quickroom.QRflags |= QR_PRIVATE;
1042 /* Kick everyone out if the client requested it (by changing the
1043 * room's generation number)
1045 if (extract_int(args, 4)) {
1046 time(&CC->quickroom.QRgen);
1048 old_floor = CC->quickroom.QRfloor;
1049 if (num_parms(args) >= 6) {
1050 CC->quickroom.QRfloor = extract_int(args, 5);
1052 /* Write the room record back to disk */
1053 lputroom(&CC->quickroom);
1055 /* If the room name changed, then there are now two room records,
1056 * so we have to delete the old one.
1058 if (strcasecmp(CC->quickroom.QRname, old_name)) {
1059 b_deleteroom(old_name);
1061 /* adjust the floor reference counts */
1062 lgetfloor(&flbuf, old_floor);
1063 --flbuf.f_ref_count;
1064 lputfloor(&flbuf, old_floor);
1065 lgetfloor(&flbuf, CC->quickroom.QRfloor);
1066 ++flbuf.f_ref_count;
1067 lputfloor(&flbuf, CC->quickroom.QRfloor);
1069 /* create a room directory if necessary */
1070 if (CC->quickroom.QRflags & QR_DIRECTORY) {
1072 "mkdir ./files/%s </dev/null >/dev/null 2>/dev/null",
1073 CC->quickroom.QRdirname);
1076 sprintf(buf, "%s> edited by %s\n", CC->quickroom.QRname, CC->curr_user);
1078 cprintf("%d Ok\n", OK);
1084 * get the name of the room aide for this room
1088 struct usersupp usbuf;
1090 if (CtdlAccessCheck(ac_logged_in)) return;
1092 if (is_noneditable(&CC->quickroom)) {
1093 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
1096 if (getuserbynumber(&usbuf, CC->quickroom.QRroomaide) == 0) {
1097 cprintf("%d %s\n", OK, usbuf.fullname);
1099 cprintf("%d \n", OK);
1105 * set the room aide for this room
1107 void cmd_seta(char *new_ra)
1109 struct usersupp usbuf;
1114 if (CtdlAccessCheck(ac_room_aide)) return;
1116 if (getuser(&usbuf, new_ra) != 0) {
1119 newu = usbuf.usernum;
1122 lgetroom(&CC->quickroom, CC->quickroom.QRname);
1124 if (CC->quickroom.QRroomaide != newu) {
1127 CC->quickroom.QRroomaide = newu;
1128 lputroom(&CC->quickroom);
1131 * We have to post the change notice _after_ writing changes to
1132 * the room table, otherwise it would deadlock!
1134 if (post_notice == 1) {
1135 sprintf(buf, "%s is now room aide for %s>\n",
1136 usbuf.fullname, CC->quickroom.QRname);
1139 cprintf("%d Ok\n", OK);
1143 * Generate an associated file name for a room
1145 void assoc_file_name(char *buf, struct quickroom *qrbuf, char *prefix)
1147 sprintf(buf, "./%s/%ld", prefix, qrbuf->QRnumber);
1151 * retrieve info file for this room
1159 assoc_file_name(filename, &CC->quickroom, "info");
1160 info_fp = fopen(filename, "r");
1162 if (info_fp == NULL) {
1163 cprintf("%d No info file.\n", ERROR);
1166 cprintf("%d Info:\n", LISTING_FOLLOWS);
1167 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1168 if (strlen(buf) > 0)
1169 buf[strlen(buf) - 1] = 0;
1170 cprintf("%s\n", buf);
1177 * Back end processing to delete a room and everything associated with it
1179 void delete_room(struct quickroom *qrbuf)
1184 lprintf(9, "Deleting room <%s>\n", qrbuf->QRname);
1186 /* Delete the info file */
1187 assoc_file_name(filename, qrbuf, "info");
1190 /* Delete the image file */
1191 assoc_file_name(filename, qrbuf, "images");
1194 /* Delete the room's network config file */
1195 assoc_file_name(filename, qrbuf, "netconfigs");
1198 /* Delete the messages in the room
1199 * (Careful: this opens an S_QUICKROOM critical section!)
1201 CtdlDeleteMessages(qrbuf->QRname, 0L, "");
1203 /* Flag the room record as not in use */
1204 lgetroom(qrbuf, qrbuf->QRname);
1208 /* then decrement the reference count for the floor */
1209 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1210 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1211 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1213 /* Delete the room record from the database! */
1214 b_deleteroom(qrbuf->QRname);
1220 * Check access control for deleting a room
1222 int CtdlDoIHavePermissionToDeleteThisRoom(struct quickroom *qr) {
1224 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1228 if (is_noneditable(qr)) {
1233 * For mailboxes, check stuff
1235 if (qr->QRflags & QR_MAILBOX) {
1237 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1239 if (atol(qr->QRname) != CC->usersupp.usernum) {
1240 return(0); /* not my room */
1243 /* Can't delete your Mail> room */
1244 if (!strcasecmp(&qr->QRname[12], MAILROOM)) return(0);
1246 /* Otherwise it's ok */
1251 * For normal rooms, just check for aide or room aide status.
1254 return(is_room_aide());
1257 /* Should never get to this point, but to keep the compiler quiet... */
1262 * aide command: kill the current room
1264 void cmd_kill(char *argbuf)
1267 char deleted_room_name[ROOMNAMELEN];
1270 kill_ok = extract_int(argbuf, 0);
1272 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->quickroom) == 0) {
1273 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1277 strcpy(deleted_room_name, CC->quickroom.QRname);
1278 delete_room(&CC->quickroom); /* Do the dirty work */
1279 usergoto(BASEROOM, 0, NULL, NULL); /* Return to the Lobby */
1281 /* tell the world what we did */
1282 sprintf(aaa, "%s> killed by %s\n",
1283 deleted_room_name, CC->curr_user);
1285 cprintf("%d '%s' deleted.\n", OK, deleted_room_name);
1287 cprintf("%d ok to delete.\n", OK);
1293 * Internal code to create a new room (returns room flags)
1295 * Room types: 0=public, 1=guessname, 2=passworded, 3=inv-only,
1296 * 4=mailbox, 5=mailbox, but caller supplies namespace
1298 unsigned create_room(char *new_room_name,
1300 char *new_room_pass,
1305 struct quickroom qrbuf;
1309 lprintf(9, "create_room(%s)\n", new_room_name);
1310 if (getroom(&qrbuf, new_room_name) == 0) {
1311 lprintf(9, "%s already exists.\n", new_room_name);
1312 return (0); /* already exists */
1316 memset(&qrbuf, 0, sizeof(struct quickroom));
1317 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1318 qrbuf.QRflags = QR_INUSE;
1319 if (new_room_type > 0)
1320 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1321 if (new_room_type == 1)
1322 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1323 if (new_room_type == 2)
1324 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1325 if ( (new_room_type == 4) || (new_room_type == 5) )
1326 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1328 /* If the user is requesting a personal room, set up the room
1329 * name accordingly (prepend the user number)
1331 if (new_room_type == 4) {
1332 MailboxName(qrbuf.QRname, &CC->usersupp, new_room_name);
1335 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1338 /* If the room is private, and the system administrator has elected
1339 * to automatically grant room aide privileges, do so now; otherwise,
1340 * set the room aide to undefined.
1342 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1343 qrbuf.QRroomaide = CC->usersupp.usernum;
1345 qrbuf.QRroomaide = (-1L);
1349 * If the caller is only interested in testing whether this will work,
1350 * return now without creating the room.
1352 if (!really_create) return (qrbuf.QRflags);
1354 qrbuf.QRnumber = get_new_room_number();
1355 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1356 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1357 qrbuf.QRfloor = new_room_floor;
1359 /* save what we just did... */
1362 /* bump the reference count on whatever floor the room is on */
1363 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1364 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1365 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1367 /* be sure not to kick the creator out of the room! */
1368 lgetuser(&CC->usersupp, CC->curr_user);
1369 CtdlGetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1370 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1371 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1372 CtdlSetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1373 lputuser(&CC->usersupp);
1375 /* resume our happy day */
1376 return (qrbuf.QRflags);
1383 void cmd_cre8(char *args)
1386 char new_room_name[SIZ];
1388 char new_room_pass[SIZ];
1394 cre8_ok = extract_int(args, 0);
1395 extract(new_room_name, args, 1);
1396 new_room_name[ROOMNAMELEN - 1] = 0;
1397 new_room_type = extract_int(args, 2);
1398 extract(new_room_pass, args, 3);
1399 new_room_pass[9] = 0;
1402 if ((strlen(new_room_name) == 0) && (cre8_ok == 1)) {
1403 cprintf("%d Invalid room name.\n", ERROR);
1407 if (!strcasecmp(new_room_name, MAILROOM)) {
1408 cprintf("%d '%s' already exists.\n",
1409 ERROR + ALREADY_EXISTS, new_room_name);
1413 if (num_parms(args) >= 5) {
1414 fl = cgetfloor(extract_int(args, 4));
1415 if ((fl->f_flags & F_INUSE) == 0) {
1416 cprintf("%d Invalid floor number.\n",
1417 ERROR + INVALID_FLOOR_OPERATION);
1420 new_room_floor = extract_int(args, 4);
1424 if (CtdlAccessCheck(ac_logged_in)) return;
1426 if (CC->usersupp.axlevel < config.c_createax) {
1427 cprintf("%d You need higher access to create rooms.\n",
1428 ERROR + HIGHER_ACCESS_REQUIRED);
1432 if ((strlen(new_room_name) == 0) && (cre8_ok == 0)) {
1433 cprintf("%d Ok to create rooms.\n", OK);
1437 if ((new_room_type < 0) || (new_room_type > 4)) {
1438 cprintf("%d Invalid room type.\n", ERROR);
1442 /* Check to make sure the requested room name doesn't already exist */
1443 newflags = create_room(new_room_name,
1444 new_room_type, new_room_pass, new_room_floor, 0);
1445 if (newflags == 0) {
1446 cprintf("%d '%s' already exists.\n",
1447 ERROR + ALREADY_EXISTS, new_room_name);
1452 cprintf("%d OK to create '%s'\n", OK, new_room_name);
1456 /* If we reach this point, the room needs to be created. */
1458 newflags = create_room(new_room_name,
1459 new_room_type, new_room_pass, new_room_floor, 1);
1461 /* post a message in Aide> describing the new room */
1462 safestrncpy(aaa, new_room_name, sizeof aaa);
1463 strcat(aaa, "> created by ");
1464 strcat(aaa, CC->usersupp.fullname);
1465 if (newflags & QR_MAILBOX)
1466 strcat(aaa, " [personal]");
1467 else if (newflags & QR_PRIVATE)
1468 strcat(aaa, " [private]");
1469 if (newflags & QR_GUESSNAME)
1470 strcat(aaa, "[guessname] ");
1471 if (newflags & QR_PASSWORDED) {
1472 strcat(aaa, "\n Password: ");
1473 strcat(aaa, new_room_pass);
1478 cprintf("%d '%s' has been created.\n", OK, new_room_name);
1483 void cmd_einf(char *ok)
1484 { /* enter info file for current room */
1486 char infofilename[SIZ];
1489 if (CtdlAccessCheck(ac_room_aide)) return;
1491 if (atoi(ok) == 0) {
1492 cprintf("%d Ok.\n", OK);
1495 assoc_file_name(infofilename, &CC->quickroom, "info");
1496 lprintf(9, "opening\n");
1497 fp = fopen(infofilename, "w");
1498 lprintf(9, "checking\n");
1500 cprintf("%d Cannot open %s: %s\n",
1501 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1504 cprintf("%d Send info...\n", SEND_LISTING);
1508 if (strcmp(buf, "000"))
1509 fprintf(fp, "%s\n", buf);
1510 } while (strcmp(buf, "000"));
1513 /* now update the room index so people will see our new info */
1514 lgetroom(&CC->quickroom, CC->quickroom.QRname); /* lock so no one steps on us */
1515 CC->quickroom.QRinfo = CC->quickroom.QRhighest + 1L;
1516 lputroom(&CC->quickroom);
1521 * cmd_lflr() - List all known floors
1528 if (CtdlAccessCheck(ac_logged_in)) return;
1530 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1532 for (a = 0; a < MAXFLOORS; ++a) {
1533 getfloor(&flbuf, a);
1534 if (flbuf.f_flags & F_INUSE) {
1535 cprintf("%d|%s|%d\n",
1547 * create a new floor
1549 void cmd_cflr(char *argbuf)
1551 char new_floor_name[SIZ];
1554 int free_slot = (-1);
1557 extract(new_floor_name, argbuf, 0);
1558 cflr_ok = extract_int(argbuf, 1);
1561 if (CtdlAccessCheck(ac_aide)) return;
1563 for (a = 0; a < MAXFLOORS; ++a) {
1564 getfloor(&flbuf, a);
1566 /* note any free slots while we're scanning... */
1567 if (((flbuf.f_flags & F_INUSE) == 0)
1571 /* check to see if it already exists */
1572 if ((!strcasecmp(flbuf.f_name, new_floor_name))
1573 && (flbuf.f_flags & F_INUSE)) {
1574 cprintf("%d Floor '%s' already exists.\n",
1575 ERROR + ALREADY_EXISTS,
1581 if (free_slot < 0) {
1582 cprintf("%d There is no space available for a new floor.\n",
1583 ERROR + INVALID_FLOOR_OPERATION);
1587 cprintf("%d ok to create...\n", OK);
1590 lgetfloor(&flbuf, free_slot);
1591 flbuf.f_flags = F_INUSE;
1592 flbuf.f_ref_count = 0;
1593 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
1594 lputfloor(&flbuf, free_slot);
1595 cprintf("%d %d\n", OK, free_slot);
1603 void cmd_kflr(char *argbuf)
1606 int floor_to_delete;
1610 floor_to_delete = extract_int(argbuf, 0);
1611 kflr_ok = extract_int(argbuf, 1);
1613 if (CtdlAccessCheck(ac_aide)) return;
1615 lgetfloor(&flbuf, floor_to_delete);
1618 if ((flbuf.f_flags & F_INUSE) == 0) {
1619 cprintf("%d Floor %d not in use.\n",
1620 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
1623 if (flbuf.f_ref_count != 0) {
1624 cprintf("%d Cannot delete; floor contains %d rooms.\n",
1625 ERROR + INVALID_FLOOR_OPERATION,
1630 cprintf("%d Ok\n", OK);
1632 cprintf("%d Ok to delete...\n", OK);
1639 if ((delete_ok == 1) && (kflr_ok == 1))
1641 lputfloor(&flbuf, floor_to_delete);
1647 void cmd_eflr(char *argbuf)
1653 np = num_parms(argbuf);
1655 cprintf("%d Usage error.\n", ERROR);
1659 if (CtdlAccessCheck(ac_aide)) return;
1661 floor_num = extract_int(argbuf, 0);
1662 lgetfloor(&flbuf, floor_num);
1663 if ((flbuf.f_flags & F_INUSE) == 0) {
1664 lputfloor(&flbuf, floor_num);
1665 cprintf("%d Floor %d is not in use.\n",
1666 ERROR + INVALID_FLOOR_OPERATION, floor_num);
1670 extract(flbuf.f_name, argbuf, 1);
1671 lputfloor(&flbuf, floor_num);
1673 cprintf("%d Ok\n", OK);