4 * Server functions which perform operations on room objects.
14 #if TIME_WITH_SYS_TIME
15 # include <sys/time.h>
19 # include <sys/time.h>
32 #include "sysdep_decls.h"
36 #include "citserver.h"
40 struct floor *floorcache[MAXFLOORS];
43 * Generic routine for determining user access to rooms
45 int CtdlRoomAccess(struct quickroom *roombuf, struct usersupp *userbuf)
50 /* for internal programs, always do everything */
51 if (((CC->internal_pgm)) && (roombuf->QRflags & QR_INUSE)) {
52 return (UA_KNOWN | UA_GOTOALLOWED);
54 /* For mailbox rooms, only allow access to the owner */
55 if (roombuf->QRflags & QR_MAILBOX) {
56 if (userbuf->usernum != atol(roombuf->QRname)) {
60 /* Locate any applicable user/room relationships */
61 CtdlGetRelationship(&vbuf, userbuf, roombuf);
63 /* Force the properties of the Aide room */
64 if (!strcasecmp(roombuf->QRname, AIDEROOM)) {
65 if (userbuf->axlevel >= 6) {
66 retval = UA_KNOWN | UA_GOTOALLOWED;
72 /* For mailboxes, we skip all the access stuff (and we've
73 * already checked by this point that the mailbox belongs
76 if (roombuf->QRflags & QR_MAILBOX) {
77 retval = UA_KNOWN | UA_GOTOALLOWED;
80 /* If this is a public room, it's accessible... */
81 if ((roombuf->QRflags & QR_PRIVATE) == 0) {
82 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
84 /* If this is a preferred users only room, check access level */
85 if (roombuf->QRflags & QR_PREFONLY) {
86 if (userbuf->axlevel < 5) {
87 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
90 /* For private rooms, check the generation number matchups */
91 if (roombuf->QRflags & QR_PRIVATE) {
93 /* An explicit match means the user belongs in this room */
94 if (vbuf.v_flags & V_ACCESS) {
95 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
97 /* Otherwise, check if this is a guess-name or passworded
98 * room. If it is, a goto may at least be attempted
100 else if ((roombuf->QRflags & QR_PRIVATE)
101 || (roombuf->QRflags & QR_PASSWORDED)) {
102 retval = retval & ~UA_KNOWN;
103 retval = retval | UA_GOTOALLOWED;
106 /* Check to see if the user has forgotten this room */
107 if (vbuf.v_flags & V_FORGET) {
108 retval = retval & ~UA_KNOWN;
109 retval = retval | UA_ZAPPED;
111 /* If user is explicitly locked out of this room, deny everything */
112 if (vbuf.v_flags & V_LOCKOUT) {
113 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
116 /* Aides get access to everything */
117 if (userbuf->axlevel >= 6) {
118 if (vbuf.v_flags & V_FORGET) {
119 retval = retval | UA_GOTOALLOWED;
122 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
126 NEWMSG: /* By the way, we also check for the presence of new messages */
127 if (is_msg_in_mset(vbuf.v_seen, roombuf->QRhighest) == 0) {
128 retval = retval | UA_HASNEWMSGS;
134 * Self-checking stuff for a room record read into memory
136 void room_sanity_check(struct quickroom *qrbuf)
138 /* Mailbox rooms are always on the lowest floor */
139 if (qrbuf->QRflags & QR_MAILBOX) {
142 /* Listing order of 0 is illegal except for base rooms */
143 if (qrbuf->QRorder == 0)
144 if (!is_noneditable(qrbuf))
150 * getroom() - retrieve room data from disk
152 int getroom(struct quickroom *qrbuf, char *room_name)
154 struct cdbdata *cdbqr;
155 char lowercase_name[ROOMNAMELEN];
156 char personal_lowercase_name[ROOMNAMELEN];
159 for (a = 0; room_name[a] && a < sizeof lowercase_name - 1; ++a) {
160 lowercase_name[a] = tolower(room_name[a]);
162 lowercase_name[a] = 0;
164 memset(qrbuf, 0, sizeof(struct quickroom));
166 /* First, try the public namespace */
167 cdbqr = cdb_fetch(CDB_QUICKROOM,
168 lowercase_name, strlen(lowercase_name));
170 /* If that didn't work, try the user's personal namespace */
172 sprintf(personal_lowercase_name, "%010ld.%s",
173 CC->usersupp.usernum, lowercase_name);
174 cdbqr = cdb_fetch(CDB_QUICKROOM,
175 personal_lowercase_name,
176 strlen(personal_lowercase_name));
179 memcpy(qrbuf, cdbqr->ptr,
180 ((cdbqr->len > sizeof(struct quickroom)) ?
181 sizeof(struct quickroom) : cdbqr->len));
184 room_sanity_check(qrbuf);
193 * lgetroom() - same as getroom() but locks the record (if supported)
195 int lgetroom(struct quickroom *qrbuf, char *room_name)
198 retval = getroom(qrbuf, room_name);
199 if (retval == 0) begin_critical_section(S_QUICKROOM);
205 * b_putroom() - back end to putroom() and b_deleteroom()
206 * (if the supplied buffer is NULL, delete the room record)
208 void b_putroom(struct quickroom *qrbuf, char *room_name)
210 char lowercase_name[ROOMNAMELEN];
213 for (a = 0; a <= strlen(room_name); ++a) {
214 lowercase_name[a] = tolower(room_name[a]);
218 cdb_delete(CDB_QUICKROOM,
219 lowercase_name, strlen(lowercase_name));
221 time(&qrbuf->QRmtime);
222 cdb_store(CDB_QUICKROOM,
223 lowercase_name, strlen(lowercase_name),
224 qrbuf, sizeof(struct quickroom));
230 * putroom() - store room data to disk
232 void putroom(struct quickroom *qrbuf) {
233 b_putroom(qrbuf, qrbuf->QRname);
238 * b_deleteroom() - delete a room record from disk
240 void b_deleteroom(char *room_name) {
241 b_putroom(NULL, room_name);
247 * lputroom() - same as putroom() but unlocks the record (if supported)
249 void lputroom(struct quickroom *qrbuf)
253 end_critical_section(S_QUICKROOM);
257 /****************************************************************************/
260 * getfloor() - retrieve floor data from disk
262 void getfloor(struct floor *flbuf, int floor_num)
264 struct cdbdata *cdbfl;
266 memset(flbuf, 0, sizeof(struct floor));
267 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
269 memcpy(flbuf, cdbfl->ptr,
270 ((cdbfl->len > sizeof(struct floor)) ?
271 sizeof(struct floor) : cdbfl->len));
274 if (floor_num == 0) {
275 strcpy(flbuf->f_name, "Main Floor");
276 flbuf->f_flags = F_INUSE;
277 flbuf->f_ref_count = 3;
284 * lgetfloor() - same as getfloor() but locks the record (if supported)
286 void lgetfloor(struct floor *flbuf, int floor_num)
289 begin_critical_section(S_FLOORTAB);
290 getfloor(flbuf, floor_num);
295 * cgetfloor() - Get floor record from *cache* (loads from disk if needed)
297 * This is strictly a performance hack.
299 struct floor *cgetfloor(int floor_num) {
300 static int initialized = 0;
303 if (initialized == 0) {
304 for (i=0; i<MAXFLOORS; ++i) {
305 floorcache[floor_num] = NULL;
310 if (floorcache[floor_num] == NULL) {
311 floorcache[floor_num] = mallok(sizeof(struct floor));
312 getfloor(floorcache[floor_num], floor_num);
315 return(floorcache[floor_num]);
321 * putfloor() - store floor data on disk
323 void putfloor(struct floor *flbuf, int floor_num)
325 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
326 flbuf, sizeof(struct floor));
328 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
329 if (floorcache[floor_num] != NULL) {
330 phree(floorcache[floor_num]);
331 floorcache[floor_num] = NULL;
337 * lputfloor() - same as putfloor() but unlocks the record (if supported)
339 void lputfloor(struct floor *flbuf, int floor_num)
342 putfloor(flbuf, floor_num);
343 end_critical_section(S_FLOORTAB);
349 * Traverse the room file...
351 void ForEachRoom(void (*CallBack) (struct quickroom *EachRoom, void *out_data),
354 struct quickroom qrbuf;
355 struct cdbdata *cdbqr;
357 cdb_rewind(CDB_QUICKROOM);
359 while (cdbqr = cdb_next_item(CDB_QUICKROOM), cdbqr != NULL) {
360 memset(&qrbuf, 0, sizeof(struct quickroom));
361 memcpy(&qrbuf, cdbqr->ptr,
362 ((cdbqr->len > sizeof(struct quickroom)) ?
363 sizeof(struct quickroom) : cdbqr->len));
365 room_sanity_check(&qrbuf);
366 if (qrbuf.QRflags & QR_INUSE)
367 (*CallBack)(&qrbuf, in_data);
373 * delete_msglist() - delete room message pointers
375 void delete_msglist(struct quickroom *whichroom)
377 struct cdbdata *cdbml;
379 /* Make sure the msglist we're deleting actually exists, otherwise
380 * gdbm will complain when we try to delete an invalid record
382 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
386 /* Go ahead and delete it */
387 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
395 * sort message pointers
396 * (returns new msg count)
398 int sort_msglist(long listptrs[], int oldcount)
409 for (a = numitems - 2; a >= 0; --a) {
410 for (b = 0; b <= a; ++b) {
411 if (listptrs[b] > (listptrs[b + 1])) {
413 hold2 = listptrs[b + 1];
415 listptrs[b + 1] = hold1;
420 /* and yank any nulls */
421 while ((numitems > 0) && (listptrs[0] == 0L)) {
422 memcpy(&listptrs[0], &listptrs[1],
423 (sizeof(long) * (numitems - 1)));
432 * Determine whether a given room is non-editable.
434 int is_noneditable(struct quickroom *qrbuf)
437 /* Lobby> and Aide> are non-editable */
438 if (!strcasecmp(qrbuf->QRname, BASEROOM))
440 else if (!strcasecmp(qrbuf->QRname, AIDEROOM))
443 /* Mail> rooms are also non-editable */
444 else if ( (qrbuf->QRflags & QR_MAILBOX)
445 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
448 /* Everything else is editable */
456 * Back-back-end for all room listing commands
458 void list_roomname(struct quickroom *qrbuf)
460 char truncated_roomname[ROOMNAMELEN];
462 /* For mailbox rooms, chop off the owner prefix */
463 if (qrbuf->QRflags & QR_MAILBOX) {
464 strcpy(truncated_roomname, qrbuf->QRname);
465 strcpy(truncated_roomname, &truncated_roomname[11]);
466 cprintf("%s", truncated_roomname);
468 /* For all other rooms, just display the name in its entirety */
470 cprintf("%s", qrbuf->QRname);
473 /* ...and now the other parameters */
474 cprintf("|%u|%d|%d\n",
476 (int) qrbuf->QRfloor,
477 (int) qrbuf->QRorder);
482 * cmd_lrms() - List all accessible rooms, known or forgotten
484 void cmd_lrms_backend(struct quickroom *qrbuf, void *data)
486 int FloorBeingSearched = (-1);
487 FloorBeingSearched = *(int *)data;
489 if (((CtdlRoomAccess(qrbuf, &CC->usersupp)
490 & (UA_KNOWN | UA_ZAPPED)))
491 && ((qrbuf->QRfloor == (FloorBeingSearched))
492 || ((FloorBeingSearched) < 0)))
493 list_roomname(qrbuf);
496 void cmd_lrms(char *argbuf)
498 int FloorBeingSearched = (-1);
499 if (strlen(argbuf) > 0)
500 FloorBeingSearched = extract_int(argbuf, 0);
502 if (CtdlAccessCheck(ac_logged_in)) return;
504 if (getuser(&CC->usersupp, CC->curr_user)) {
505 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
508 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
510 ForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
517 * cmd_lkra() - List all known rooms
519 void cmd_lkra_backend(struct quickroom *qrbuf, void *data)
521 int FloorBeingSearched = (-1);
522 FloorBeingSearched = *(int *)data;
524 if (((CtdlRoomAccess(qrbuf, &CC->usersupp)
526 && ((qrbuf->QRfloor == (FloorBeingSearched))
527 || ((FloorBeingSearched) < 0)))
528 list_roomname(qrbuf);
531 void cmd_lkra(char *argbuf)
533 int FloorBeingSearched = (-1);
534 if (strlen(argbuf) > 0)
535 FloorBeingSearched = extract_int(argbuf, 0);
537 if (CtdlAccessCheck(ac_logged_in)) return;
539 if (getuser(&CC->usersupp, CC->curr_user)) {
540 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
543 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
545 ForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
552 * cmd_lkrn() - List all known rooms with new messages
554 void cmd_lkrn_backend(struct quickroom *qrbuf, void *data)
557 int FloorBeingSearched = (-1);
558 FloorBeingSearched = *(int *)data;
560 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
562 && (ra & UA_HASNEWMSGS)
563 && ((qrbuf->QRfloor == (FloorBeingSearched))
564 || ((FloorBeingSearched) < 0)))
565 list_roomname(qrbuf);
568 void cmd_lkrn(char *argbuf)
570 int FloorBeingSearched = (-1);
571 if (strlen(argbuf) > 0)
572 FloorBeingSearched = extract_int(argbuf, 0);
574 if (CtdlAccessCheck(ac_logged_in)) return;
576 if (getuser(&CC->usersupp, CC->curr_user)) {
577 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
580 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
582 ForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
589 * cmd_lkro() - List all known rooms
591 void cmd_lkro_backend(struct quickroom *qrbuf, void *data)
594 int FloorBeingSearched = (-1);
595 FloorBeingSearched = *(int *)data;
597 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
599 && ((ra & UA_HASNEWMSGS) == 0)
600 && ((qrbuf->QRfloor == (FloorBeingSearched))
601 || ((FloorBeingSearched) < 0)))
602 list_roomname(qrbuf);
605 void cmd_lkro(char *argbuf)
607 int FloorBeingSearched = (-1);
608 if (strlen(argbuf) > 0)
609 FloorBeingSearched = extract_int(argbuf, 0);
611 if (CtdlAccessCheck(ac_logged_in)) return;
613 if (getuser(&CC->usersupp, CC->curr_user)) {
614 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
617 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
619 ForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
626 * cmd_lzrm() - List all forgotten rooms
628 void cmd_lzrm_backend(struct quickroom *qrbuf, void *data)
631 int FloorBeingSearched = (-1);
632 FloorBeingSearched = *(int *)data;
634 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
635 if ((ra & UA_GOTOALLOWED)
637 && ((qrbuf->QRfloor == (FloorBeingSearched))
638 || ((FloorBeingSearched) < 0)))
639 list_roomname(qrbuf);
642 void cmd_lzrm(char *argbuf)
644 int FloorBeingSearched = (-1);
645 if (strlen(argbuf) > 0)
646 FloorBeingSearched = extract_int(argbuf, 0);
648 if (CtdlAccessCheck(ac_logged_in)) return;
650 if (getuser(&CC->usersupp, CC->curr_user)) {
651 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
654 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
656 ForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
662 void usergoto(char *where, int display_result, int *retmsgs, int *retnew)
665 int new_messages = 0;
666 int total_messages = 0;
670 int newmailcount = 0;
672 char truncated_roomname[ROOMNAMELEN];
673 struct cdbdata *cdbfr;
674 long *msglist = NULL;
677 strcpy(CC->quickroom.QRname, where);
678 getroom(&CC->quickroom, where);
680 lgetuser(&CC->usersupp, CC->curr_user);
681 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
683 /* Know the room ... but not if it's the page log room */
684 if (strcasecmp(where, config.c_logpages)) {
685 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
686 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
688 CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
689 lputuser(&CC->usersupp);
691 /* check for new mail */
692 newmailcount = NewMailCount();
694 /* set info to 1 if the user needs to read the room's info file */
695 if (CC->quickroom.QRinfo > vbuf.v_lastseen)
699 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->quickroom.QRnumber, sizeof(long));
701 msglist = mallok(cdbfr->len);
702 memcpy(msglist, cdbfr->ptr, cdbfr->len);
703 num_msgs = cdbfr->len / sizeof(long);
707 if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
708 if (msglist[a] > 0L) {
710 if (is_msg_in_mset(vbuf.v_seen, msglist[a]) == 0) {
716 if (msglist != NULL) phree(msglist);
718 if (CC->quickroom.QRflags & QR_MAILBOX)
723 if ((CC->quickroom.QRroomaide == CC->usersupp.usernum)
724 || (CC->usersupp.axlevel >= 6))
729 strcpy(truncated_roomname, CC->quickroom.QRname);
730 if (CC->quickroom.QRflags & QR_MAILBOX) {
731 strcpy(truncated_roomname, &truncated_roomname[11]);
734 if (retmsgs != NULL) *retmsgs = total_messages;
735 if (retnew != NULL) *retnew = new_messages;
736 lprintf(9, "<%s> %d new of %d total messages\n",
737 CC->quickroom.QRname, new_messages, total_messages);
740 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d\n",
741 OK, CtdlCheckExpress(),
743 new_messages, total_messages,
744 info, CC->quickroom.QRflags,
745 CC->quickroom.QRhighest,
747 rmailflag, raideflag, newmailcount,
748 CC->quickroom.QRfloor);
754 * cmd_goto() - goto a new room
756 void cmd_goto(char *gargs)
758 struct quickroom QRscratch;
762 char augmented_roomname[SIZ];
766 if (CtdlAccessCheck(ac_logged_in)) return;
768 extract(towhere, gargs, 0);
769 extract(password, gargs, 1);
771 getuser(&CC->usersupp, CC->curr_user);
773 if (!strcasecmp(towhere, "_BASEROOM_"))
774 strcpy(towhere, BASEROOM);
776 if (!strcasecmp(towhere, "_MAIL_"))
777 strcpy(towhere, MAILROOM);
779 if (!strcasecmp(towhere, "_BITBUCKET_"))
780 strcpy(towhere, config.c_twitroom);
783 /* First try a regular match */
784 c = getroom(&QRscratch, towhere);
786 /* Then try a mailbox name match */
788 MailboxName(augmented_roomname, &CC->usersupp, towhere);
789 c = getroom(&QRscratch, augmented_roomname);
791 strcpy(towhere, augmented_roomname);
794 /* And if the room was found... */
797 /* let internal programs go directly to any room */
798 if (CC->internal_pgm) {
799 usergoto(towhere, 1, NULL, NULL);
803 /* See if there is an existing user/room relationship */
804 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
806 /* normal clients have to pass through security */
807 if (ra & UA_GOTOALLOWED)
811 if ((QRscratch.QRflags & QR_PASSWORDED) &&
812 ((ra & UA_KNOWN) == 0) &&
813 (strcasecmp(QRscratch.QRpasswd, password)) &&
814 (CC->usersupp.axlevel < 6)
816 cprintf("%d wrong or missing passwd\n",
817 ERROR + PASSWORD_REQUIRED);
819 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
820 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
821 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
822 ((ra & UA_KNOWN) == 0) &&
823 (CC->usersupp.axlevel < 6)
827 usergoto(towhere, 1, NULL, NULL);
833 NOPE: cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
839 struct usersupp temp;
840 struct cdbdata *cdbus;
842 getuser(&CC->usersupp, CC->curr_user);
843 if (CtdlAccessCheck(ac_room_aide)) return;
845 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
846 cdb_rewind(CDB_USERSUPP);
847 while (cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
848 memset(&temp, 0, sizeof temp);
849 memcpy(&temp, cdbus->ptr, sizeof temp);
852 if ((CC->quickroom.QRflags & QR_INUSE)
853 && (CtdlRoomAccess(&CC->quickroom, &temp) & UA_KNOWN)
855 cprintf("%s\n", temp.fullname);
862 * RDIR command for room directory
872 if (CtdlAccessCheck(ac_logged_in)) return;
874 getroom(&CC->quickroom, CC->quickroom.QRname);
875 getuser(&CC->usersupp, CC->curr_user);
877 if ((CC->quickroom.QRflags & QR_DIRECTORY) == 0) {
878 cprintf("%d not here.\n", ERROR + NOT_HERE);
881 if (((CC->quickroom.QRflags & QR_VISDIR) == 0)
882 && (CC->usersupp.axlevel < 6)
883 && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
884 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
887 cprintf("%d %s|%s/files/%s\n",
888 LISTING_FOLLOWS, config.c_fqdn, BBSDIR, CC->quickroom.QRdirname);
890 sprintf(buf, "ls %s/files/%s >%s 2> /dev/null",
891 BBSDIR, CC->quickroom.QRdirname, CC->temp);
894 sprintf(buf, "%s/files/%s/filedir", BBSDIR, CC->quickroom.QRdirname);
895 fd = fopen(buf, "r");
897 fd = fopen("/dev/null", "r");
899 ls = fopen(CC->temp, "r");
900 while (fgets(flnm, sizeof flnm, ls) != NULL) {
901 flnm[strlen(flnm) - 1] = 0;
902 if (strcasecmp(flnm, "filedir")) {
903 sprintf(buf, "%s/files/%s/%s",
904 BBSDIR, CC->quickroom.QRdirname, flnm);
908 while ((fgets(buf, sizeof buf, fd) != NULL)
909 && (strlen(comment) == 0)) {
910 buf[strlen(buf) - 1] = 0;
911 if ((!strncasecmp(buf, flnm, strlen(flnm)))
912 && (buf[strlen(flnm)] == ' '))
914 &buf[strlen(flnm) + 1],
917 cprintf("%s|%ld|%s\n", flnm, statbuf.st_size, comment);
928 * get room parameters (aide or room aide command)
932 if (CtdlAccessCheck(ac_room_aide)) return;
935 if (is_noneditable(&CC->quickroom)) {
936 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
941 getroom(&CC->quickroom, CC->quickroom.QRname);
942 cprintf("%d%c%s|%s|%s|%d|%d|%d\n",
943 OK, CtdlCheckExpress(),
944 CC->quickroom.QRname,
945 ((CC->quickroom.QRflags & QR_PASSWORDED) ? CC->quickroom.QRpasswd : ""),
946 ((CC->quickroom.QRflags & QR_DIRECTORY) ? CC->quickroom.QRdirname : ""),
947 CC->quickroom.QRflags,
948 (int) CC->quickroom.QRfloor,
949 (int) CC->quickroom.QRorder);
954 * set room parameters (aide or room aide command)
956 void cmd_setr(char *args)
961 char old_name[ROOMNAMELEN];
966 if (CtdlAccessCheck(ac_room_aide)) return;
968 if (is_noneditable(&CC->quickroom)) {
973 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
979 if (num_parms(args) >= 6) {
980 fl = cgetfloor(extract_int(args, 5));
981 if ((fl->f_flags & F_INUSE) == 0) {
982 cprintf("%d Invalid floor number.\n",
983 ERROR + INVALID_FLOOR_OPERATION);
987 if (num_parms(args) >= 7) {
988 new_order = extract_int(args, 6);
994 lgetroom(&CC->quickroom, CC->quickroom.QRname);
996 /* Non-editable base rooms can't be renamed */
997 strcpy(old_name, CC->quickroom.QRname);
999 extract(buf, args, 0);
1000 buf[ROOMNAMELEN] = 0;
1001 safestrncpy(CC->quickroom.QRname, buf,
1002 sizeof CC->quickroom.QRname);
1005 extract(buf, args, 1);
1007 safestrncpy(CC->quickroom.QRpasswd, buf, sizeof CC->quickroom.QRpasswd);
1008 extract(buf, args, 2);
1010 safestrncpy(CC->quickroom.QRdirname, buf,
1011 sizeof CC->quickroom.QRdirname);
1012 CC->quickroom.QRflags = (extract_int(args, 3) | QR_INUSE);
1013 if (num_parms(args) >= 7)
1014 CC->quickroom.QRorder = (char) new_order;
1016 /* Clean up a client boo-boo: if the client set the room to
1017 * guess-name or passworded, ensure that the private flag is
1020 if ((CC->quickroom.QRflags & QR_GUESSNAME)
1021 || (CC->quickroom.QRflags & QR_PASSWORDED))
1022 CC->quickroom.QRflags |= QR_PRIVATE;
1024 /* Kick everyone out if the client requested it (by changing the
1025 * room's generation number)
1027 if (extract_int(args, 4)) {
1028 time(&CC->quickroom.QRgen);
1030 old_floor = CC->quickroom.QRfloor;
1031 if (num_parms(args) >= 6) {
1032 CC->quickroom.QRfloor = extract_int(args, 5);
1034 /* Write the room record back to disk */
1035 lputroom(&CC->quickroom);
1037 /* If the room name changed, then there are now two room records,
1038 * so we have to delete the old one.
1040 if (strcasecmp(CC->quickroom.QRname, old_name)) {
1041 b_deleteroom(old_name);
1043 /* adjust the floor reference counts */
1044 lgetfloor(&flbuf, old_floor);
1045 --flbuf.f_ref_count;
1046 lputfloor(&flbuf, old_floor);
1047 lgetfloor(&flbuf, CC->quickroom.QRfloor);
1048 ++flbuf.f_ref_count;
1049 lputfloor(&flbuf, CC->quickroom.QRfloor);
1051 /* create a room directory if necessary */
1052 if (CC->quickroom.QRflags & QR_DIRECTORY) {
1054 "mkdir ./files/%s </dev/null >/dev/null 2>/dev/null",
1055 CC->quickroom.QRdirname);
1058 sprintf(buf, "%s> edited by %s\n", CC->quickroom.QRname, CC->curr_user);
1060 cprintf("%d Ok\n", OK);
1066 * get the name of the room aide for this room
1070 struct usersupp usbuf;
1072 if (CtdlAccessCheck(ac_logged_in)) return;
1074 if (is_noneditable(&CC->quickroom)) {
1075 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
1078 if (getuserbynumber(&usbuf, CC->quickroom.QRroomaide) == 0) {
1079 cprintf("%d %s\n", OK, usbuf.fullname);
1081 cprintf("%d \n", OK);
1087 * set the room aide for this room
1089 void cmd_seta(char *new_ra)
1091 struct usersupp usbuf;
1096 if (CtdlAccessCheck(ac_room_aide)) return;
1098 if (getuser(&usbuf, new_ra) != 0) {
1101 newu = usbuf.usernum;
1104 lgetroom(&CC->quickroom, CC->quickroom.QRname);
1106 if (CC->quickroom.QRroomaide != newu) {
1109 CC->quickroom.QRroomaide = newu;
1110 lputroom(&CC->quickroom);
1113 * We have to post the change notice _after_ writing changes to
1114 * the room table, otherwise it would deadlock!
1116 if (post_notice == 1) {
1117 sprintf(buf, "%s is now room aide for %s>\n",
1118 usbuf.fullname, CC->quickroom.QRname);
1121 cprintf("%d Ok\n", OK);
1125 * Generate an associated file name for a room
1127 void assoc_file_name(char *buf, struct quickroom *qrbuf, char *prefix)
1129 sprintf(buf, "./%s/%ld", prefix, qrbuf->QRnumber);
1133 * retrieve info file for this room
1141 assoc_file_name(filename, &CC->quickroom, "info");
1142 info_fp = fopen(filename, "r");
1144 if (info_fp == NULL) {
1145 cprintf("%d No info file.\n", ERROR);
1148 cprintf("%d Info:\n", LISTING_FOLLOWS);
1149 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1150 if (strlen(buf) > 0)
1151 buf[strlen(buf) - 1] = 0;
1152 cprintf("%s\n", buf);
1159 * Back end processing to delete a room and everything associated with it
1161 void delete_room(struct quickroom *qrbuf)
1166 lprintf(9, "Deleting room <%s>\n", qrbuf->QRname);
1168 /* Delete the info file */
1169 assoc_file_name(filename, qrbuf, "info");
1172 /* Delete the image file */
1173 assoc_file_name(filename, qrbuf, "images");
1176 /* Delete the room's network config file */
1177 assoc_file_name(filename, qrbuf, "netconfigs");
1180 /* Delete the messages in the room
1181 * (Careful: this opens an S_QUICKROOM critical section!)
1183 CtdlDeleteMessages(qrbuf->QRname, 0L, "");
1185 /* Flag the room record as not in use */
1186 lgetroom(qrbuf, qrbuf->QRname);
1190 /* then decrement the reference count for the floor */
1191 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1192 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1193 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1195 /* Delete the room record from the database! */
1196 b_deleteroom(qrbuf->QRname);
1202 * Check access control for deleting a room
1204 int CtdlDoIHavePermissionToDeleteThisRoom(struct quickroom *qr) {
1206 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1210 if (is_noneditable(qr)) {
1215 * For mailboxes, check stuff
1217 if (qr->QRflags & QR_MAILBOX) {
1219 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1221 if (atol(qr->QRname) != CC->usersupp.usernum) {
1222 return(0); /* not my room */
1225 /* Can't delete your Mail> room */
1226 if (!strcasecmp(&qr->QRname[12], MAILROOM)) return(0);
1228 /* Otherwise it's ok */
1233 * For normal rooms, just check for aide or room aide status.
1236 return(is_room_aide());
1239 /* Should never get to this point, but to keep the compiler quiet... */
1244 * aide command: kill the current room
1246 void cmd_kill(char *argbuf)
1249 char deleted_room_name[ROOMNAMELEN];
1252 kill_ok = extract_int(argbuf, 0);
1254 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->quickroom) == 0) {
1255 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1259 strcpy(deleted_room_name, CC->quickroom.QRname);
1260 delete_room(&CC->quickroom); /* Do the dirty work */
1261 usergoto(BASEROOM, 0, NULL, NULL); /* Return to the Lobby */
1263 /* tell the world what we did */
1264 sprintf(aaa, "%s> killed by %s\n",
1265 deleted_room_name, CC->curr_user);
1267 cprintf("%d '%s' deleted.\n", OK, deleted_room_name);
1269 cprintf("%d ok to delete.\n", OK);
1275 * Internal code to create a new room (returns room flags)
1277 * Room types: 0=public, 1=guessname, 2=passworded, 3=inv-only,
1278 * 4=mailbox, 5=mailbox, but caller supplies namespace
1280 unsigned create_room(char *new_room_name,
1282 char *new_room_pass,
1287 struct quickroom qrbuf;
1291 lprintf(9, "create_room(%s)\n", new_room_name);
1292 if (getroom(&qrbuf, new_room_name) == 0) {
1293 lprintf(9, "%s already exists.\n", new_room_name);
1294 return (0); /* already exists */
1298 memset(&qrbuf, 0, sizeof(struct quickroom));
1299 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1300 qrbuf.QRflags = QR_INUSE;
1301 if (new_room_type > 0)
1302 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1303 if (new_room_type == 1)
1304 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1305 if (new_room_type == 2)
1306 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1307 if ( (new_room_type == 4) || (new_room_type == 5) )
1308 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1310 /* If the user is requesting a personal room, set up the room
1311 * name accordingly (prepend the user number)
1313 if (new_room_type == 4) {
1314 MailboxName(qrbuf.QRname, &CC->usersupp, new_room_name);
1317 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1320 /* If the room is private, and the system administrator has elected
1321 * to automatically grant room aide privileges, do so now; otherwise,
1322 * set the room aide to undefined.
1324 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1325 qrbuf.QRroomaide = CC->usersupp.usernum;
1327 qrbuf.QRroomaide = (-1L);
1331 * If the caller is only interested in testing whether this will work,
1332 * return now without creating the room.
1334 if (!really_create) return (qrbuf.QRflags);
1336 qrbuf.QRnumber = get_new_room_number();
1337 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1338 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1339 qrbuf.QRfloor = new_room_floor;
1341 /* save what we just did... */
1344 /* bump the reference count on whatever floor the room is on */
1345 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1346 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1347 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1349 /* be sure not to kick the creator out of the room! */
1350 lgetuser(&CC->usersupp, CC->curr_user);
1351 CtdlGetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1352 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1353 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1354 CtdlSetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1355 lputuser(&CC->usersupp);
1357 /* resume our happy day */
1358 return (qrbuf.QRflags);
1365 void cmd_cre8(char *args)
1368 char new_room_name[SIZ];
1370 char new_room_pass[SIZ];
1374 struct quickroom qrbuf;
1377 cre8_ok = extract_int(args, 0);
1378 extract(new_room_name, args, 1);
1379 new_room_name[ROOMNAMELEN - 1] = 0;
1380 new_room_type = extract_int(args, 2);
1381 extract(new_room_pass, args, 3);
1382 new_room_pass[9] = 0;
1385 if ((strlen(new_room_name) == 0) && (cre8_ok == 1)) {
1386 cprintf("%d Invalid room name.\n", ERROR);
1390 if (!strcasecmp(new_room_name, MAILROOM)) {
1391 cprintf("%d '%s' already exists.\n",
1392 ERROR + ALREADY_EXISTS, new_room_name);
1396 if (num_parms(args) >= 5) {
1397 fl = cgetfloor(extract_int(args, 4));
1398 if ((fl->f_flags & F_INUSE) == 0) {
1399 cprintf("%d Invalid floor number.\n",
1400 ERROR + INVALID_FLOOR_OPERATION);
1403 new_room_floor = extract_int(args, 4);
1407 if (CtdlAccessCheck(ac_logged_in)) return;
1409 if (CC->usersupp.axlevel < config.c_createax) {
1410 cprintf("%d You need higher access to create rooms.\n",
1411 ERROR + HIGHER_ACCESS_REQUIRED);
1415 if ((strlen(new_room_name) == 0) && (cre8_ok == 0)) {
1416 cprintf("%d Ok to create rooms.\n", OK);
1420 if ((new_room_type < 0) || (new_room_type > 4)) {
1421 cprintf("%d Invalid room type.\n", ERROR);
1425 /* Check to make sure the requested room name doesn't already exist */
1426 newflags = create_room(new_room_name,
1427 new_room_type, new_room_pass, new_room_floor, 0);
1428 if (newflags == 0) {
1429 cprintf("%d '%s' already exists.\n",
1430 ERROR + ALREADY_EXISTS, qrbuf.QRname);
1435 cprintf("%d OK to create '%s'\n", OK, new_room_name);
1439 /* If we reach this point, the room needs to be created. */
1441 newflags = create_room(new_room_name,
1442 new_room_type, new_room_pass, new_room_floor, 1);
1444 /* post a message in Aide> describing the new room */
1445 safestrncpy(aaa, new_room_name, sizeof aaa);
1446 strcat(aaa, "> created by ");
1447 strcat(aaa, CC->usersupp.fullname);
1448 if (newflags & QR_MAILBOX)
1449 strcat(aaa, " [personal]");
1450 else if (newflags & QR_PRIVATE)
1451 strcat(aaa, " [private]");
1452 if (newflags & QR_GUESSNAME)
1453 strcat(aaa, "[guessname] ");
1454 if (newflags & QR_PASSWORDED) {
1455 strcat(aaa, "\n Password: ");
1456 strcat(aaa, new_room_pass);
1461 cprintf("%d '%s' has been created.\n", OK, qrbuf.QRname);
1466 void cmd_einf(char *ok)
1467 { /* enter info file for current room */
1469 char infofilename[SIZ];
1472 if (CtdlAccessCheck(ac_room_aide)) return;
1474 if (atoi(ok) == 0) {
1475 cprintf("%d Ok.\n", OK);
1478 assoc_file_name(infofilename, &CC->quickroom, "info");
1479 lprintf(9, "opening\n");
1480 fp = fopen(infofilename, "w");
1481 lprintf(9, "checking\n");
1483 cprintf("%d Cannot open %s: %s\n",
1484 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1487 cprintf("%d Send info...\n", SEND_LISTING);
1491 if (strcmp(buf, "000"))
1492 fprintf(fp, "%s\n", buf);
1493 } while (strcmp(buf, "000"));
1496 /* now update the room index so people will see our new info */
1497 lgetroom(&CC->quickroom, CC->quickroom.QRname); /* lock so no one steps on us */
1498 CC->quickroom.QRinfo = CC->quickroom.QRhighest + 1L;
1499 lputroom(&CC->quickroom);
1504 * cmd_lflr() - List all known floors
1511 if (CtdlAccessCheck(ac_logged_in)) return;
1513 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1515 for (a = 0; a < MAXFLOORS; ++a) {
1516 getfloor(&flbuf, a);
1517 if (flbuf.f_flags & F_INUSE) {
1518 cprintf("%d|%s|%d\n",
1530 * create a new floor
1532 void cmd_cflr(char *argbuf)
1534 char new_floor_name[SIZ];
1537 int free_slot = (-1);
1540 extract(new_floor_name, argbuf, 0);
1541 cflr_ok = extract_int(argbuf, 1);
1544 if (CtdlAccessCheck(ac_aide)) return;
1546 for (a = 0; a < MAXFLOORS; ++a) {
1547 getfloor(&flbuf, a);
1549 /* note any free slots while we're scanning... */
1550 if (((flbuf.f_flags & F_INUSE) == 0)
1554 /* check to see if it already exists */
1555 if ((!strcasecmp(flbuf.f_name, new_floor_name))
1556 && (flbuf.f_flags & F_INUSE)) {
1557 cprintf("%d Floor '%s' already exists.\n",
1558 ERROR + ALREADY_EXISTS,
1564 if (free_slot < 0) {
1565 cprintf("%d There is no space available for a new floor.\n",
1566 ERROR + INVALID_FLOOR_OPERATION);
1570 cprintf("%d ok to create...\n", OK);
1573 lgetfloor(&flbuf, free_slot);
1574 flbuf.f_flags = F_INUSE;
1575 flbuf.f_ref_count = 0;
1576 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
1577 lputfloor(&flbuf, free_slot);
1578 cprintf("%d %d\n", OK, free_slot);
1586 void cmd_kflr(char *argbuf)
1589 int floor_to_delete;
1593 floor_to_delete = extract_int(argbuf, 0);
1594 kflr_ok = extract_int(argbuf, 1);
1596 if (CtdlAccessCheck(ac_aide)) return;
1598 lgetfloor(&flbuf, floor_to_delete);
1601 if ((flbuf.f_flags & F_INUSE) == 0) {
1602 cprintf("%d Floor %d not in use.\n",
1603 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
1606 if (flbuf.f_ref_count != 0) {
1607 cprintf("%d Cannot delete; floor contains %d rooms.\n",
1608 ERROR + INVALID_FLOOR_OPERATION,
1613 cprintf("%d Ok\n", OK);
1615 cprintf("%d Ok to delete...\n", OK);
1622 if ((delete_ok == 1) && (kflr_ok == 1))
1624 lputfloor(&flbuf, floor_to_delete);
1630 void cmd_eflr(char *argbuf)
1636 np = num_parms(argbuf);
1638 cprintf("%d Usage error.\n", ERROR);
1642 if (CtdlAccessCheck(ac_aide)) return;
1644 floor_num = extract_int(argbuf, 0);
1645 lgetfloor(&flbuf, floor_num);
1646 if ((flbuf.f_flags & F_INUSE) == 0) {
1647 lputfloor(&flbuf, floor_num);
1648 cprintf("%d Floor %d is not in use.\n",
1649 ERROR + INVALID_FLOOR_OPERATION, floor_num);
1653 extract(flbuf.f_name, argbuf, 1);
1654 lputfloor(&flbuf, floor_num);
1656 cprintf("%d Ok\n", OK);