4 * Server functions which perform operations on room objects.
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
24 # include <sys/time.h>
34 #include "dynloader.h"
38 #include "sysdep_decls.h"
42 #include "citserver.h"
46 struct floor *floorcache[MAXFLOORS];
49 * Generic routine for determining user access to rooms
51 int CtdlRoomAccess(struct quickroom *roombuf, struct usersupp *userbuf)
56 /* for internal programs, always do everything */
57 if (((CC->internal_pgm)) && (roombuf->QRflags & QR_INUSE)) {
58 return (UA_KNOWN | UA_GOTOALLOWED);
60 /* For mailbox rooms, only allow access to the owner */
61 if (roombuf->QRflags & QR_MAILBOX) {
62 if (userbuf->usernum != atol(roombuf->QRname)) {
66 /* Locate any applicable user/room relationships */
67 CtdlGetRelationship(&vbuf, userbuf, roombuf);
69 /* Force the properties of the Aide room */
70 if (!strcasecmp(roombuf->QRname, AIDEROOM)) {
71 if (userbuf->axlevel >= 6) {
72 retval = UA_KNOWN | UA_GOTOALLOWED;
78 /* For mailboxes, we skip all the access stuff (and we've
79 * already checked by this point that the mailbox belongs
82 if (roombuf->QRflags & QR_MAILBOX) {
83 retval = UA_KNOWN | UA_GOTOALLOWED;
84 /* goto NEWMSG; allow users to zap mailboxes now */
86 /* If this is a public room, it's accessible... */
87 if ((roombuf->QRflags & QR_PRIVATE) == 0) {
88 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
90 /* If this is a preferred users only room, check access level */
91 if (roombuf->QRflags & QR_PREFONLY) {
92 if (userbuf->axlevel < 5) {
93 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
96 /* For private rooms, check the generation number matchups */
97 if (roombuf->QRflags & QR_PRIVATE) {
99 /* An explicit match means the user belongs in this room */
100 if (vbuf.v_flags & V_ACCESS) {
101 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
103 /* Otherwise, check if this is a guess-name or passworded
104 * room. If it is, a goto may at least be attempted
106 else if ((roombuf->QRflags & QR_PRIVATE)
107 || (roombuf->QRflags & QR_PASSWORDED)) {
108 retval = retval & ~UA_KNOWN;
109 retval = retval | UA_GOTOALLOWED;
112 /* Check to see if the user has forgotten this room */
113 if (vbuf.v_flags & V_FORGET) {
114 retval = retval & ~UA_KNOWN;
115 retval = retval | UA_ZAPPED;
117 /* If user is explicitly locked out of this room, deny everything */
118 if (vbuf.v_flags & V_LOCKOUT) {
119 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
122 /* Aides get access to everything */
123 if (userbuf->axlevel >= 6) {
124 if (vbuf.v_flags & V_FORGET) {
125 retval = retval | UA_GOTOALLOWED;
128 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
132 NEWMSG: /* By the way, we also check for the presence of new messages */
133 if (is_msg_in_mset(vbuf.v_seen, roombuf->QRhighest) == 0) {
134 retval = retval | UA_HASNEWMSGS;
137 /* System rooms never show up in the list. */
138 if (roombuf->QRflags2 & QR2_SYSTEM) {
139 retval = retval & ~UA_KNOWN;
145 * Self-checking stuff for a room record read into memory
147 void room_sanity_check(struct quickroom *qrbuf)
149 /* Mailbox rooms are always on the lowest floor */
150 if (qrbuf->QRflags & QR_MAILBOX) {
153 /* Listing order of 0 is illegal except for base rooms */
154 if (qrbuf->QRorder == 0)
155 if (!is_noneditable(qrbuf))
161 * getroom() - retrieve room data from disk
163 int getroom(struct quickroom *qrbuf, char *room_name)
165 struct cdbdata *cdbqr;
166 char lowercase_name[ROOMNAMELEN];
167 char personal_lowercase_name[ROOMNAMELEN];
170 for (a = 0; room_name[a] && a < sizeof lowercase_name - 1; ++a) {
171 lowercase_name[a] = tolower(room_name[a]);
173 lowercase_name[a] = 0;
175 memset(qrbuf, 0, sizeof(struct quickroom));
177 /* First, try the public namespace */
178 cdbqr = cdb_fetch(CDB_QUICKROOM,
179 lowercase_name, strlen(lowercase_name));
181 /* If that didn't work, try the user's personal namespace */
183 sprintf(personal_lowercase_name, "%010ld.%s",
184 CC->usersupp.usernum, lowercase_name);
185 cdbqr = cdb_fetch(CDB_QUICKROOM,
186 personal_lowercase_name,
187 strlen(personal_lowercase_name));
190 memcpy(qrbuf, cdbqr->ptr,
191 ((cdbqr->len > sizeof(struct quickroom)) ?
192 sizeof(struct quickroom) : cdbqr->len));
195 room_sanity_check(qrbuf);
204 * lgetroom() - same as getroom() but locks the record (if supported)
206 int lgetroom(struct quickroom *qrbuf, char *room_name)
209 retval = getroom(qrbuf, room_name);
210 if (retval == 0) begin_critical_section(S_QUICKROOM);
216 * b_putroom() - back end to putroom() and b_deleteroom()
217 * (if the supplied buffer is NULL, delete the room record)
219 void b_putroom(struct quickroom *qrbuf, char *room_name)
221 char lowercase_name[ROOMNAMELEN];
224 for (a = 0; a <= strlen(room_name); ++a) {
225 lowercase_name[a] = tolower(room_name[a]);
229 cdb_delete(CDB_QUICKROOM,
230 lowercase_name, strlen(lowercase_name));
232 time(&qrbuf->QRmtime);
233 cdb_store(CDB_QUICKROOM,
234 lowercase_name, strlen(lowercase_name),
235 qrbuf, sizeof(struct quickroom));
241 * putroom() - store room data to disk
243 void putroom(struct quickroom *qrbuf) {
244 b_putroom(qrbuf, qrbuf->QRname);
249 * b_deleteroom() - delete a room record from disk
251 void b_deleteroom(char *room_name) {
252 b_putroom(NULL, room_name);
258 * lputroom() - same as putroom() but unlocks the record (if supported)
260 void lputroom(struct quickroom *qrbuf)
264 end_critical_section(S_QUICKROOM);
268 /****************************************************************************/
271 * getfloor() - retrieve floor data from disk
273 void getfloor(struct floor *flbuf, int floor_num)
275 struct cdbdata *cdbfl;
277 memset(flbuf, 0, sizeof(struct floor));
278 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
280 memcpy(flbuf, cdbfl->ptr,
281 ((cdbfl->len > sizeof(struct floor)) ?
282 sizeof(struct floor) : cdbfl->len));
285 if (floor_num == 0) {
286 strcpy(flbuf->f_name, "Main Floor");
287 flbuf->f_flags = F_INUSE;
288 flbuf->f_ref_count = 3;
295 * lgetfloor() - same as getfloor() but locks the record (if supported)
297 void lgetfloor(struct floor *flbuf, int floor_num)
300 begin_critical_section(S_FLOORTAB);
301 getfloor(flbuf, floor_num);
306 * cgetfloor() - Get floor record from *cache* (loads from disk if needed)
308 * This is strictly a performance hack.
310 struct floor *cgetfloor(int floor_num) {
311 static int initialized = 0;
314 if (initialized == 0) {
315 for (i=0; i<MAXFLOORS; ++i) {
316 floorcache[floor_num] = NULL;
321 if (floorcache[floor_num] == NULL) {
322 floorcache[floor_num] = mallok(sizeof(struct floor));
323 getfloor(floorcache[floor_num], floor_num);
326 return(floorcache[floor_num]);
332 * putfloor() - store floor data on disk
334 void putfloor(struct floor *flbuf, int floor_num)
336 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
337 flbuf, sizeof(struct floor));
339 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
340 if (floorcache[floor_num] != NULL) {
341 phree(floorcache[floor_num]);
342 floorcache[floor_num] = NULL;
348 * lputfloor() - same as putfloor() but unlocks the record (if supported)
350 void lputfloor(struct floor *flbuf, int floor_num)
353 putfloor(flbuf, floor_num);
354 end_critical_section(S_FLOORTAB);
360 * Traverse the room file...
362 void ForEachRoom(void (*CallBack) (struct quickroom *EachRoom, void *out_data),
365 struct quickroom qrbuf;
366 struct cdbdata *cdbqr;
368 cdb_rewind(CDB_QUICKROOM);
370 while (cdbqr = cdb_next_item(CDB_QUICKROOM), cdbqr != NULL) {
371 memset(&qrbuf, 0, sizeof(struct quickroom));
372 memcpy(&qrbuf, cdbqr->ptr,
373 ((cdbqr->len > sizeof(struct quickroom)) ?
374 sizeof(struct quickroom) : cdbqr->len));
376 room_sanity_check(&qrbuf);
377 if (qrbuf.QRflags & QR_INUSE)
378 (*CallBack)(&qrbuf, in_data);
384 * delete_msglist() - delete room message pointers
386 void delete_msglist(struct quickroom *whichroom)
388 struct cdbdata *cdbml;
390 /* Make sure the msglist we're deleting actually exists, otherwise
391 * gdbm will complain when we try to delete an invalid record
393 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
397 /* Go ahead and delete it */
398 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
406 * sort message pointers
407 * (returns new msg count)
409 int sort_msglist(long listptrs[], int oldcount)
420 for (a = numitems - 2; a >= 0; --a) {
421 for (b = 0; b <= a; ++b) {
422 if (listptrs[b] > (listptrs[b + 1])) {
424 hold2 = listptrs[b + 1];
426 listptrs[b + 1] = hold1;
431 /* and yank any nulls */
432 while ((numitems > 0) && (listptrs[0] == 0L)) {
433 memcpy(&listptrs[0], &listptrs[1],
434 (sizeof(long) * (numitems - 1)));
443 * Determine whether a given room is non-editable.
445 int is_noneditable(struct quickroom *qrbuf)
448 /* Lobby> and Aide> are non-editable */
449 if (!strcasecmp(qrbuf->QRname, BASEROOM))
451 else if (!strcasecmp(qrbuf->QRname, AIDEROOM))
454 /* Mail> rooms are also non-editable */
455 else if ( (qrbuf->QRflags & QR_MAILBOX)
456 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
459 /* Everything else is editable */
467 * Back-back-end for all room listing commands
469 void list_roomname(struct quickroom *qrbuf)
471 char truncated_roomname[ROOMNAMELEN];
473 /* For mailbox rooms, chop off the owner prefix */
474 if (qrbuf->QRflags & QR_MAILBOX) {
475 strcpy(truncated_roomname, qrbuf->QRname);
476 strcpy(truncated_roomname, &truncated_roomname[11]);
477 cprintf("%s", truncated_roomname);
479 /* For all other rooms, just display the name in its entirety */
481 cprintf("%s", qrbuf->QRname);
484 /* ...and now the other parameters */
485 cprintf("|%u|%d|%d\n",
487 (int) qrbuf->QRfloor,
488 (int) qrbuf->QRorder);
493 * cmd_lrms() - List all accessible rooms, known or forgotten
495 void cmd_lrms_backend(struct quickroom *qrbuf, void *data)
497 int FloorBeingSearched = (-1);
498 FloorBeingSearched = *(int *)data;
500 if (((CtdlRoomAccess(qrbuf, &CC->usersupp)
501 & (UA_KNOWN | UA_ZAPPED)))
502 && ((qrbuf->QRfloor == (FloorBeingSearched))
503 || ((FloorBeingSearched) < 0)))
504 list_roomname(qrbuf);
507 void cmd_lrms(char *argbuf)
509 int FloorBeingSearched = (-1);
510 if (strlen(argbuf) > 0)
511 FloorBeingSearched = extract_int(argbuf, 0);
513 if (CtdlAccessCheck(ac_logged_in)) return;
515 if (getuser(&CC->usersupp, CC->curr_user)) {
516 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
519 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
521 ForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
528 * cmd_lkra() - List all known rooms
530 void cmd_lkra_backend(struct quickroom *qrbuf, void *data)
532 int FloorBeingSearched = (-1);
533 FloorBeingSearched = *(int *)data;
535 if (((CtdlRoomAccess(qrbuf, &CC->usersupp)
537 && ((qrbuf->QRfloor == (FloorBeingSearched))
538 || ((FloorBeingSearched) < 0)))
539 list_roomname(qrbuf);
542 void cmd_lkra(char *argbuf)
544 int FloorBeingSearched = (-1);
545 if (strlen(argbuf) > 0)
546 FloorBeingSearched = extract_int(argbuf, 0);
548 if (CtdlAccessCheck(ac_logged_in)) return;
550 if (getuser(&CC->usersupp, CC->curr_user)) {
551 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
554 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
556 ForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
563 * cmd_lkrn() - List all known rooms with new messages
565 void cmd_lkrn_backend(struct quickroom *qrbuf, void *data)
568 int FloorBeingSearched = (-1);
569 FloorBeingSearched = *(int *)data;
571 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
573 && (ra & UA_HASNEWMSGS)
574 && ((qrbuf->QRfloor == (FloorBeingSearched))
575 || ((FloorBeingSearched) < 0)))
576 list_roomname(qrbuf);
579 void cmd_lkrn(char *argbuf)
581 int FloorBeingSearched = (-1);
582 if (strlen(argbuf) > 0)
583 FloorBeingSearched = extract_int(argbuf, 0);
585 if (CtdlAccessCheck(ac_logged_in)) return;
587 if (getuser(&CC->usersupp, CC->curr_user)) {
588 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
591 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
593 ForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
600 * cmd_lkro() - List all known rooms
602 void cmd_lkro_backend(struct quickroom *qrbuf, void *data)
605 int FloorBeingSearched = (-1);
606 FloorBeingSearched = *(int *)data;
608 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
610 && ((ra & UA_HASNEWMSGS) == 0)
611 && ((qrbuf->QRfloor == (FloorBeingSearched))
612 || ((FloorBeingSearched) < 0)))
613 list_roomname(qrbuf);
616 void cmd_lkro(char *argbuf)
618 int FloorBeingSearched = (-1);
619 if (strlen(argbuf) > 0)
620 FloorBeingSearched = extract_int(argbuf, 0);
622 if (CtdlAccessCheck(ac_logged_in)) return;
624 if (getuser(&CC->usersupp, CC->curr_user)) {
625 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
628 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
630 ForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
637 * cmd_lzrm() - List all forgotten rooms
639 void cmd_lzrm_backend(struct quickroom *qrbuf, void *data)
642 int FloorBeingSearched = (-1);
643 FloorBeingSearched = *(int *)data;
645 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
646 if ((ra & UA_GOTOALLOWED)
648 && ((qrbuf->QRfloor == (FloorBeingSearched))
649 || ((FloorBeingSearched) < 0)))
650 list_roomname(qrbuf);
653 void cmd_lzrm(char *argbuf)
655 int FloorBeingSearched = (-1);
656 if (strlen(argbuf) > 0)
657 FloorBeingSearched = extract_int(argbuf, 0);
659 if (CtdlAccessCheck(ac_logged_in)) return;
661 if (getuser(&CC->usersupp, CC->curr_user)) {
662 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
665 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
667 ForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
673 void usergoto(char *where, int display_result, int *retmsgs, int *retnew)
676 int new_messages = 0;
677 int total_messages = 0;
681 int newmailcount = 0;
683 char truncated_roomname[ROOMNAMELEN];
684 struct cdbdata *cdbfr;
685 long *msglist = NULL;
688 strcpy(CC->quickroom.QRname, where);
689 getroom(&CC->quickroom, where);
691 lgetuser(&CC->usersupp, CC->curr_user);
692 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
694 /* Know the room ... but not if it's the page log room */
695 if (strcasecmp(where, config.c_logpages)) {
696 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
697 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
699 CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
700 lputuser(&CC->usersupp);
702 /* check for new mail */
703 newmailcount = NewMailCount();
705 /* set info to 1 if the user needs to read the room's info file */
706 if (CC->quickroom.QRinfo > vbuf.v_lastseen)
710 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->quickroom.QRnumber, sizeof(long));
712 msglist = mallok(cdbfr->len);
713 memcpy(msglist, cdbfr->ptr, cdbfr->len);
714 num_msgs = cdbfr->len / sizeof(long);
718 if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
719 if (msglist[a] > 0L) {
721 if (is_msg_in_mset(vbuf.v_seen, msglist[a]) == 0) {
727 if (msglist != NULL) phree(msglist);
729 if (CC->quickroom.QRflags & QR_MAILBOX)
734 if ((CC->quickroom.QRroomaide == CC->usersupp.usernum)
735 || (CC->usersupp.axlevel >= 6))
740 strcpy(truncated_roomname, CC->quickroom.QRname);
741 if (CC->quickroom.QRflags & QR_MAILBOX) {
742 strcpy(truncated_roomname, &truncated_roomname[11]);
745 if (retmsgs != NULL) *retmsgs = total_messages;
746 if (retnew != NULL) *retnew = new_messages;
747 lprintf(9, "<%s> %d new of %d total messages\n",
748 CC->quickroom.QRname, new_messages, total_messages);
751 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d\n",
752 OK, CtdlCheckExpress(),
754 new_messages, total_messages,
755 info, CC->quickroom.QRflags,
756 CC->quickroom.QRhighest,
758 rmailflag, raideflag, newmailcount,
759 CC->quickroom.QRfloor);
765 * cmd_goto() - goto a new room
767 void cmd_goto(char *gargs)
769 struct quickroom QRscratch;
773 char augmented_roomname[SIZ];
777 if (CtdlAccessCheck(ac_logged_in)) return;
779 extract(towhere, gargs, 0);
780 extract(password, gargs, 1);
782 getuser(&CC->usersupp, CC->curr_user);
784 if (!strcasecmp(towhere, "_BASEROOM_"))
785 strcpy(towhere, BASEROOM);
787 if (!strcasecmp(towhere, "_MAIL_"))
788 strcpy(towhere, MAILROOM);
790 if (!strcasecmp(towhere, "_BITBUCKET_"))
791 strcpy(towhere, config.c_twitroom);
794 /* First try a regular match */
795 c = getroom(&QRscratch, towhere);
797 /* Then try a mailbox name match */
799 MailboxName(augmented_roomname, &CC->usersupp, towhere);
800 c = getroom(&QRscratch, augmented_roomname);
802 strcpy(towhere, augmented_roomname);
805 /* And if the room was found... */
808 /* let internal programs go directly to any room */
809 if (CC->internal_pgm) {
810 usergoto(towhere, 1, NULL, NULL);
814 /* See if there is an existing user/room relationship */
815 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
817 /* normal clients have to pass through security */
818 if (ra & UA_GOTOALLOWED)
822 if ((QRscratch.QRflags & QR_PASSWORDED) &&
823 ((ra & UA_KNOWN) == 0) &&
824 (strcasecmp(QRscratch.QRpasswd, password)) &&
825 (CC->usersupp.axlevel < 6)
827 cprintf("%d wrong or missing passwd\n",
828 ERROR + PASSWORD_REQUIRED);
830 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
831 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
832 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
833 ((ra & UA_KNOWN) == 0) &&
834 (CC->usersupp.axlevel < 6)
838 usergoto(towhere, 1, NULL, NULL);
844 NOPE: cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
850 struct usersupp temp;
851 struct cdbdata *cdbus;
853 getuser(&CC->usersupp, CC->curr_user);
854 if (CtdlAccessCheck(ac_room_aide)) return;
856 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
857 cdb_rewind(CDB_USERSUPP);
858 while (cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
859 memset(&temp, 0, sizeof temp);
860 memcpy(&temp, cdbus->ptr, sizeof temp);
863 if ((CC->quickroom.QRflags & QR_INUSE)
864 && (CtdlRoomAccess(&CC->quickroom, &temp) & UA_KNOWN)
866 cprintf("%s\n", temp.fullname);
873 * RDIR command for room directory
883 if (CtdlAccessCheck(ac_logged_in)) return;
885 getroom(&CC->quickroom, CC->quickroom.QRname);
886 getuser(&CC->usersupp, CC->curr_user);
888 if ((CC->quickroom.QRflags & QR_DIRECTORY) == 0) {
889 cprintf("%d not here.\n", ERROR + NOT_HERE);
892 if (((CC->quickroom.QRflags & QR_VISDIR) == 0)
893 && (CC->usersupp.axlevel < 6)
894 && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
895 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
898 cprintf("%d %s|%s/files/%s\n",
899 LISTING_FOLLOWS, config.c_fqdn, BBSDIR, CC->quickroom.QRdirname);
901 sprintf(buf, "ls %s/files/%s >%s 2> /dev/null",
902 BBSDIR, CC->quickroom.QRdirname, CC->temp);
905 sprintf(buf, "%s/files/%s/filedir", BBSDIR, CC->quickroom.QRdirname);
906 fd = fopen(buf, "r");
908 fd = fopen("/dev/null", "r");
910 ls = fopen(CC->temp, "r");
911 while (fgets(flnm, sizeof flnm, ls) != NULL) {
912 flnm[strlen(flnm) - 1] = 0;
913 if (strcasecmp(flnm, "filedir")) {
914 sprintf(buf, "%s/files/%s/%s",
915 BBSDIR, CC->quickroom.QRdirname, flnm);
919 while ((fgets(buf, sizeof buf, fd) != NULL)
920 && (strlen(comment) == 0)) {
921 buf[strlen(buf) - 1] = 0;
922 if ((!strncasecmp(buf, flnm, strlen(flnm)))
923 && (buf[strlen(flnm)] == ' '))
925 &buf[strlen(flnm) + 1],
928 cprintf("%s|%ld|%s\n", flnm, (long)statbuf.st_size, comment);
939 * get room parameters (aide or room aide command)
943 if (CtdlAccessCheck(ac_room_aide)) return;
946 if (is_noneditable(&CC->quickroom)) {
947 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
952 getroom(&CC->quickroom, CC->quickroom.QRname);
953 cprintf("%d%c%s|%s|%s|%d|%d|%d\n",
954 OK, CtdlCheckExpress(),
955 CC->quickroom.QRname,
956 ((CC->quickroom.QRflags & QR_PASSWORDED) ? CC->quickroom.QRpasswd : ""),
957 ((CC->quickroom.QRflags & QR_DIRECTORY) ? CC->quickroom.QRdirname : ""),
958 CC->quickroom.QRflags,
959 (int) CC->quickroom.QRfloor,
960 (int) CC->quickroom.QRorder);
965 * set room parameters (aide or room aide command)
967 void cmd_setr(char *args)
972 char old_name[ROOMNAMELEN];
977 if (CtdlAccessCheck(ac_room_aide)) return;
979 if (is_noneditable(&CC->quickroom)) {
984 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
990 if (num_parms(args) >= 6) {
991 fl = cgetfloor(extract_int(args, 5));
992 if ((fl->f_flags & F_INUSE) == 0) {
993 cprintf("%d Invalid floor number.\n",
994 ERROR + INVALID_FLOOR_OPERATION);
998 if (num_parms(args) >= 7) {
999 new_order = extract_int(args, 6);
1002 if (new_order > 127)
1005 lgetroom(&CC->quickroom, CC->quickroom.QRname);
1007 /* Non-editable base rooms can't be renamed */
1008 strcpy(old_name, CC->quickroom.QRname);
1010 extract(buf, args, 0);
1011 buf[ROOMNAMELEN] = 0;
1012 safestrncpy(CC->quickroom.QRname, buf,
1013 sizeof CC->quickroom.QRname);
1016 extract(buf, args, 1);
1018 safestrncpy(CC->quickroom.QRpasswd, buf, sizeof CC->quickroom.QRpasswd);
1019 extract(buf, args, 2);
1021 safestrncpy(CC->quickroom.QRdirname, buf,
1022 sizeof CC->quickroom.QRdirname);
1023 CC->quickroom.QRflags = (extract_int(args, 3) | QR_INUSE);
1024 if (num_parms(args) >= 7)
1025 CC->quickroom.QRorder = (char) new_order;
1027 /* Clean up a client boo-boo: if the client set the room to
1028 * guess-name or passworded, ensure that the private flag is
1031 if ((CC->quickroom.QRflags & QR_GUESSNAME)
1032 || (CC->quickroom.QRflags & QR_PASSWORDED))
1033 CC->quickroom.QRflags |= QR_PRIVATE;
1035 /* Kick everyone out if the client requested it (by changing the
1036 * room's generation number)
1038 if (extract_int(args, 4)) {
1039 time(&CC->quickroom.QRgen);
1041 old_floor = CC->quickroom.QRfloor;
1042 if (num_parms(args) >= 6) {
1043 CC->quickroom.QRfloor = extract_int(args, 5);
1045 /* Write the room record back to disk */
1046 lputroom(&CC->quickroom);
1048 /* If the room name changed, then there are now two room records,
1049 * so we have to delete the old one.
1051 if (strcasecmp(CC->quickroom.QRname, old_name)) {
1052 b_deleteroom(old_name);
1054 /* adjust the floor reference counts */
1055 lgetfloor(&flbuf, old_floor);
1056 --flbuf.f_ref_count;
1057 lputfloor(&flbuf, old_floor);
1058 lgetfloor(&flbuf, CC->quickroom.QRfloor);
1059 ++flbuf.f_ref_count;
1060 lputfloor(&flbuf, CC->quickroom.QRfloor);
1062 /* create a room directory if necessary */
1063 if (CC->quickroom.QRflags & QR_DIRECTORY) {
1065 "mkdir ./files/%s </dev/null >/dev/null 2>/dev/null",
1066 CC->quickroom.QRdirname);
1069 sprintf(buf, "%s> edited by %s\n", CC->quickroom.QRname, CC->curr_user);
1071 cprintf("%d Ok\n", OK);
1077 * get the name of the room aide for this room
1081 struct usersupp usbuf;
1083 if (CtdlAccessCheck(ac_logged_in)) return;
1085 if (is_noneditable(&CC->quickroom)) {
1086 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
1089 if (getuserbynumber(&usbuf, CC->quickroom.QRroomaide) == 0) {
1090 cprintf("%d %s\n", OK, usbuf.fullname);
1092 cprintf("%d \n", OK);
1098 * set the room aide for this room
1100 void cmd_seta(char *new_ra)
1102 struct usersupp usbuf;
1107 if (CtdlAccessCheck(ac_room_aide)) return;
1109 if (getuser(&usbuf, new_ra) != 0) {
1112 newu = usbuf.usernum;
1115 lgetroom(&CC->quickroom, CC->quickroom.QRname);
1117 if (CC->quickroom.QRroomaide != newu) {
1120 CC->quickroom.QRroomaide = newu;
1121 lputroom(&CC->quickroom);
1124 * We have to post the change notice _after_ writing changes to
1125 * the room table, otherwise it would deadlock!
1127 if (post_notice == 1) {
1128 sprintf(buf, "%s is now room aide for %s>\n",
1129 usbuf.fullname, CC->quickroom.QRname);
1132 cprintf("%d Ok\n", OK);
1136 * Generate an associated file name for a room
1138 void assoc_file_name(char *buf, struct quickroom *qrbuf, char *prefix)
1140 sprintf(buf, "./%s/%ld", prefix, qrbuf->QRnumber);
1144 * retrieve info file for this room
1152 assoc_file_name(filename, &CC->quickroom, "info");
1153 info_fp = fopen(filename, "r");
1155 if (info_fp == NULL) {
1156 cprintf("%d No info file.\n", ERROR);
1159 cprintf("%d Info:\n", LISTING_FOLLOWS);
1160 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1161 if (strlen(buf) > 0)
1162 buf[strlen(buf) - 1] = 0;
1163 cprintf("%s\n", buf);
1170 * Back end processing to delete a room and everything associated with it
1172 void delete_room(struct quickroom *qrbuf)
1177 lprintf(9, "Deleting room <%s>\n", qrbuf->QRname);
1179 /* Delete the info file */
1180 assoc_file_name(filename, qrbuf, "info");
1183 /* Delete the image file */
1184 assoc_file_name(filename, qrbuf, "images");
1187 /* Delete the room's network config file */
1188 assoc_file_name(filename, qrbuf, "netconfigs");
1191 /* Delete the messages in the room
1192 * (Careful: this opens an S_QUICKROOM critical section!)
1194 CtdlDeleteMessages(qrbuf->QRname, 0L, "");
1196 /* Flag the room record as not in use */
1197 lgetroom(qrbuf, qrbuf->QRname);
1201 /* then decrement the reference count for the floor */
1202 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1203 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1204 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1206 /* Delete the room record from the database! */
1207 b_deleteroom(qrbuf->QRname);
1213 * Check access control for deleting a room
1215 int CtdlDoIHavePermissionToDeleteThisRoom(struct quickroom *qr) {
1217 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1221 if (is_noneditable(qr)) {
1226 * For mailboxes, check stuff
1228 if (qr->QRflags & QR_MAILBOX) {
1230 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1232 if (atol(qr->QRname) != CC->usersupp.usernum) {
1233 return(0); /* not my room */
1236 /* Can't delete your Mail> room */
1237 if (!strcasecmp(&qr->QRname[12], MAILROOM)) return(0);
1239 /* Otherwise it's ok */
1244 * For normal rooms, just check for aide or room aide status.
1247 return(is_room_aide());
1250 /* Should never get to this point, but to keep the compiler quiet... */
1255 * aide command: kill the current room
1257 void cmd_kill(char *argbuf)
1260 char deleted_room_name[ROOMNAMELEN];
1263 kill_ok = extract_int(argbuf, 0);
1265 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->quickroom) == 0) {
1266 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1270 strcpy(deleted_room_name, CC->quickroom.QRname);
1271 delete_room(&CC->quickroom); /* Do the dirty work */
1272 usergoto(BASEROOM, 0, NULL, NULL); /* Return to the Lobby */
1274 /* tell the world what we did */
1275 sprintf(aaa, "%s> killed by %s\n",
1276 deleted_room_name, CC->curr_user);
1278 cprintf("%d '%s' deleted.\n", OK, deleted_room_name);
1280 cprintf("%d ok to delete.\n", OK);
1286 * Internal code to create a new room (returns room flags)
1288 * Room types: 0=public, 1=guessname, 2=passworded, 3=inv-only,
1289 * 4=mailbox, 5=mailbox, but caller supplies namespace
1291 unsigned create_room(char *new_room_name,
1293 char *new_room_pass,
1298 struct quickroom qrbuf;
1302 lprintf(9, "create_room(%s)\n", new_room_name);
1303 if (getroom(&qrbuf, new_room_name) == 0) {
1304 lprintf(9, "%s already exists.\n", new_room_name);
1305 return (0); /* already exists */
1309 memset(&qrbuf, 0, sizeof(struct quickroom));
1310 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1311 qrbuf.QRflags = QR_INUSE;
1312 if (new_room_type > 0)
1313 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1314 if (new_room_type == 1)
1315 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1316 if (new_room_type == 2)
1317 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1318 if ( (new_room_type == 4) || (new_room_type == 5) )
1319 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1321 /* If the user is requesting a personal room, set up the room
1322 * name accordingly (prepend the user number)
1324 if (new_room_type == 4) {
1325 MailboxName(qrbuf.QRname, &CC->usersupp, new_room_name);
1328 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1331 /* If the room is private, and the system administrator has elected
1332 * to automatically grant room aide privileges, do so now; otherwise,
1333 * set the room aide to undefined.
1335 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1336 qrbuf.QRroomaide = CC->usersupp.usernum;
1338 qrbuf.QRroomaide = (-1L);
1342 * If the caller is only interested in testing whether this will work,
1343 * return now without creating the room.
1345 if (!really_create) return (qrbuf.QRflags);
1347 qrbuf.QRnumber = get_new_room_number();
1348 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1349 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1350 qrbuf.QRfloor = new_room_floor;
1352 /* save what we just did... */
1355 /* bump the reference count on whatever floor the room is on */
1356 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1357 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1358 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1360 /* be sure not to kick the creator out of the room! */
1361 lgetuser(&CC->usersupp, CC->curr_user);
1362 CtdlGetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1363 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1364 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1365 CtdlSetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1366 lputuser(&CC->usersupp);
1368 /* resume our happy day */
1369 return (qrbuf.QRflags);
1376 void cmd_cre8(char *args)
1379 char new_room_name[SIZ];
1381 char new_room_pass[SIZ];
1385 struct quickroom qrbuf;
1388 cre8_ok = extract_int(args, 0);
1389 extract(new_room_name, args, 1);
1390 new_room_name[ROOMNAMELEN - 1] = 0;
1391 new_room_type = extract_int(args, 2);
1392 extract(new_room_pass, args, 3);
1393 new_room_pass[9] = 0;
1396 if ((strlen(new_room_name) == 0) && (cre8_ok == 1)) {
1397 cprintf("%d Invalid room name.\n", ERROR);
1401 if (!strcasecmp(new_room_name, MAILROOM)) {
1402 cprintf("%d '%s' already exists.\n",
1403 ERROR + ALREADY_EXISTS, new_room_name);
1407 if (num_parms(args) >= 5) {
1408 fl = cgetfloor(extract_int(args, 4));
1409 if ((fl->f_flags & F_INUSE) == 0) {
1410 cprintf("%d Invalid floor number.\n",
1411 ERROR + INVALID_FLOOR_OPERATION);
1414 new_room_floor = extract_int(args, 4);
1418 if (CtdlAccessCheck(ac_logged_in)) return;
1420 if (CC->usersupp.axlevel < config.c_createax) {
1421 cprintf("%d You need higher access to create rooms.\n",
1422 ERROR + HIGHER_ACCESS_REQUIRED);
1426 if ((strlen(new_room_name) == 0) && (cre8_ok == 0)) {
1427 cprintf("%d Ok to create rooms.\n", OK);
1431 if ((new_room_type < 0) || (new_room_type > 4)) {
1432 cprintf("%d Invalid room type.\n", ERROR);
1436 /* Check to make sure the requested room name doesn't already exist */
1437 newflags = create_room(new_room_name,
1438 new_room_type, new_room_pass, new_room_floor, 0);
1439 if (newflags == 0) {
1440 cprintf("%d '%s' already exists.\n",
1441 ERROR + ALREADY_EXISTS, qrbuf.QRname);
1446 cprintf("%d OK to create '%s'\n", OK, new_room_name);
1450 /* If we reach this point, the room needs to be created. */
1452 newflags = create_room(new_room_name,
1453 new_room_type, new_room_pass, new_room_floor, 1);
1455 /* post a message in Aide> describing the new room */
1456 safestrncpy(aaa, new_room_name, sizeof aaa);
1457 strcat(aaa, "> created by ");
1458 strcat(aaa, CC->usersupp.fullname);
1459 if (newflags & QR_MAILBOX)
1460 strcat(aaa, " [personal]");
1461 else if (newflags & QR_PRIVATE)
1462 strcat(aaa, " [private]");
1463 if (newflags & QR_GUESSNAME)
1464 strcat(aaa, "[guessname] ");
1465 if (newflags & QR_PASSWORDED) {
1466 strcat(aaa, "\n Password: ");
1467 strcat(aaa, new_room_pass);
1472 cprintf("%d '%s' has been created.\n", OK, qrbuf.QRname);
1477 void cmd_einf(char *ok)
1478 { /* enter info file for current room */
1480 char infofilename[SIZ];
1483 if (CtdlAccessCheck(ac_room_aide)) return;
1485 if (atoi(ok) == 0) {
1486 cprintf("%d Ok.\n", OK);
1489 assoc_file_name(infofilename, &CC->quickroom, "info");
1490 lprintf(9, "opening\n");
1491 fp = fopen(infofilename, "w");
1492 lprintf(9, "checking\n");
1494 cprintf("%d Cannot open %s: %s\n",
1495 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1498 cprintf("%d Send info...\n", SEND_LISTING);
1502 if (strcmp(buf, "000"))
1503 fprintf(fp, "%s\n", buf);
1504 } while (strcmp(buf, "000"));
1507 /* now update the room index so people will see our new info */
1508 lgetroom(&CC->quickroom, CC->quickroom.QRname); /* lock so no one steps on us */
1509 CC->quickroom.QRinfo = CC->quickroom.QRhighest + 1L;
1510 lputroom(&CC->quickroom);
1515 * cmd_lflr() - List all known floors
1522 if (CtdlAccessCheck(ac_logged_in)) return;
1524 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1526 for (a = 0; a < MAXFLOORS; ++a) {
1527 getfloor(&flbuf, a);
1528 if (flbuf.f_flags & F_INUSE) {
1529 cprintf("%d|%s|%d\n",
1541 * create a new floor
1543 void cmd_cflr(char *argbuf)
1545 char new_floor_name[SIZ];
1548 int free_slot = (-1);
1551 extract(new_floor_name, argbuf, 0);
1552 cflr_ok = extract_int(argbuf, 1);
1555 if (CtdlAccessCheck(ac_aide)) return;
1557 for (a = 0; a < MAXFLOORS; ++a) {
1558 getfloor(&flbuf, a);
1560 /* note any free slots while we're scanning... */
1561 if (((flbuf.f_flags & F_INUSE) == 0)
1565 /* check to see if it already exists */
1566 if ((!strcasecmp(flbuf.f_name, new_floor_name))
1567 && (flbuf.f_flags & F_INUSE)) {
1568 cprintf("%d Floor '%s' already exists.\n",
1569 ERROR + ALREADY_EXISTS,
1575 if (free_slot < 0) {
1576 cprintf("%d There is no space available for a new floor.\n",
1577 ERROR + INVALID_FLOOR_OPERATION);
1581 cprintf("%d ok to create...\n", OK);
1584 lgetfloor(&flbuf, free_slot);
1585 flbuf.f_flags = F_INUSE;
1586 flbuf.f_ref_count = 0;
1587 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
1588 lputfloor(&flbuf, free_slot);
1589 cprintf("%d %d\n", OK, free_slot);
1597 void cmd_kflr(char *argbuf)
1600 int floor_to_delete;
1604 floor_to_delete = extract_int(argbuf, 0);
1605 kflr_ok = extract_int(argbuf, 1);
1607 if (CtdlAccessCheck(ac_aide)) return;
1609 lgetfloor(&flbuf, floor_to_delete);
1612 if ((flbuf.f_flags & F_INUSE) == 0) {
1613 cprintf("%d Floor %d not in use.\n",
1614 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
1617 if (flbuf.f_ref_count != 0) {
1618 cprintf("%d Cannot delete; floor contains %d rooms.\n",
1619 ERROR + INVALID_FLOOR_OPERATION,
1624 cprintf("%d Ok\n", OK);
1626 cprintf("%d Ok to delete...\n", OK);
1633 if ((delete_ok == 1) && (kflr_ok == 1))
1635 lputfloor(&flbuf, floor_to_delete);
1641 void cmd_eflr(char *argbuf)
1647 np = num_parms(argbuf);
1649 cprintf("%d Usage error.\n", ERROR);
1653 if (CtdlAccessCheck(ac_aide)) return;
1655 floor_num = extract_int(argbuf, 0);
1656 lgetfloor(&flbuf, floor_num);
1657 if ((flbuf.f_flags & F_INUSE) == 0) {
1658 lputfloor(&flbuf, floor_num);
1659 cprintf("%d Floor %d is not in use.\n",
1660 ERROR + INVALID_FLOOR_OPERATION, floor_num);
1664 extract(flbuf.f_name, argbuf, 1);
1665 lputfloor(&flbuf, floor_num);
1667 cprintf("%d Ok\n", OK);