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;
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;
140 * Self-checking stuff for a room record read into memory
142 void room_sanity_check(struct quickroom *qrbuf)
144 /* Mailbox rooms are always on the lowest floor */
145 if (qrbuf->QRflags & QR_MAILBOX) {
148 /* Listing order of 0 is illegal except for base rooms */
149 if (qrbuf->QRorder == 0)
150 if (!is_noneditable(qrbuf))
156 * getroom() - retrieve room data from disk
158 int getroom(struct quickroom *qrbuf, char *room_name)
160 struct cdbdata *cdbqr;
161 char lowercase_name[ROOMNAMELEN];
162 char personal_lowercase_name[ROOMNAMELEN];
165 for (a = 0; room_name[a] && a < sizeof lowercase_name - 1; ++a) {
166 lowercase_name[a] = tolower(room_name[a]);
168 lowercase_name[a] = 0;
170 memset(qrbuf, 0, sizeof(struct quickroom));
172 /* First, try the public namespace */
173 cdbqr = cdb_fetch(CDB_QUICKROOM,
174 lowercase_name, strlen(lowercase_name));
176 /* If that didn't work, try the user's personal namespace */
178 sprintf(personal_lowercase_name, "%010ld.%s",
179 CC->usersupp.usernum, lowercase_name);
180 cdbqr = cdb_fetch(CDB_QUICKROOM,
181 personal_lowercase_name,
182 strlen(personal_lowercase_name));
185 memcpy(qrbuf, cdbqr->ptr,
186 ((cdbqr->len > sizeof(struct quickroom)) ?
187 sizeof(struct quickroom) : cdbqr->len));
190 room_sanity_check(qrbuf);
199 * lgetroom() - same as getroom() but locks the record (if supported)
201 int lgetroom(struct quickroom *qrbuf, char *room_name)
204 retval = getroom(qrbuf, room_name);
205 if (retval == 0) begin_critical_section(S_QUICKROOM);
211 * b_putroom() - back end to putroom() and b_deleteroom()
212 * (if the supplied buffer is NULL, delete the room record)
214 void b_putroom(struct quickroom *qrbuf, char *room_name)
216 char lowercase_name[ROOMNAMELEN];
219 for (a = 0; a <= strlen(room_name); ++a) {
220 lowercase_name[a] = tolower(room_name[a]);
224 cdb_delete(CDB_QUICKROOM,
225 lowercase_name, strlen(lowercase_name));
227 time(&qrbuf->QRmtime);
228 cdb_store(CDB_QUICKROOM,
229 lowercase_name, strlen(lowercase_name),
230 qrbuf, sizeof(struct quickroom));
236 * putroom() - store room data to disk
238 void putroom(struct quickroom *qrbuf) {
239 b_putroom(qrbuf, qrbuf->QRname);
244 * b_deleteroom() - delete a room record from disk
246 void b_deleteroom(char *room_name) {
247 b_putroom(NULL, room_name);
253 * lputroom() - same as putroom() but unlocks the record (if supported)
255 void lputroom(struct quickroom *qrbuf)
259 end_critical_section(S_QUICKROOM);
263 /****************************************************************************/
266 * getfloor() - retrieve floor data from disk
268 void getfloor(struct floor *flbuf, int floor_num)
270 struct cdbdata *cdbfl;
272 memset(flbuf, 0, sizeof(struct floor));
273 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
275 memcpy(flbuf, cdbfl->ptr,
276 ((cdbfl->len > sizeof(struct floor)) ?
277 sizeof(struct floor) : cdbfl->len));
280 if (floor_num == 0) {
281 strcpy(flbuf->f_name, "Main Floor");
282 flbuf->f_flags = F_INUSE;
283 flbuf->f_ref_count = 3;
290 * lgetfloor() - same as getfloor() but locks the record (if supported)
292 void lgetfloor(struct floor *flbuf, int floor_num)
295 begin_critical_section(S_FLOORTAB);
296 getfloor(flbuf, floor_num);
301 * cgetfloor() - Get floor record from *cache* (loads from disk if needed)
303 * This is strictly a performance hack.
305 struct floor *cgetfloor(int floor_num) {
306 static int initialized = 0;
309 if (initialized == 0) {
310 for (i=0; i<MAXFLOORS; ++i) {
311 floorcache[floor_num] = NULL;
316 if (floorcache[floor_num] == NULL) {
317 floorcache[floor_num] = mallok(sizeof(struct floor));
318 getfloor(floorcache[floor_num], floor_num);
321 return(floorcache[floor_num]);
327 * putfloor() - store floor data on disk
329 void putfloor(struct floor *flbuf, int floor_num)
331 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
332 flbuf, sizeof(struct floor));
334 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
335 if (floorcache[floor_num] != NULL) {
336 phree(floorcache[floor_num]);
337 floorcache[floor_num] = NULL;
343 * lputfloor() - same as putfloor() but unlocks the record (if supported)
345 void lputfloor(struct floor *flbuf, int floor_num)
348 putfloor(flbuf, floor_num);
349 end_critical_section(S_FLOORTAB);
355 * Traverse the room file...
357 void ForEachRoom(void (*CallBack) (struct quickroom *EachRoom, void *out_data),
360 struct quickroom qrbuf;
361 struct cdbdata *cdbqr;
363 cdb_rewind(CDB_QUICKROOM);
365 while (cdbqr = cdb_next_item(CDB_QUICKROOM), cdbqr != NULL) {
366 memset(&qrbuf, 0, sizeof(struct quickroom));
367 memcpy(&qrbuf, cdbqr->ptr,
368 ((cdbqr->len > sizeof(struct quickroom)) ?
369 sizeof(struct quickroom) : cdbqr->len));
371 room_sanity_check(&qrbuf);
372 if (qrbuf.QRflags & QR_INUSE)
373 (*CallBack)(&qrbuf, in_data);
379 * delete_msglist() - delete room message pointers
381 void delete_msglist(struct quickroom *whichroom)
383 struct cdbdata *cdbml;
385 /* Make sure the msglist we're deleting actually exists, otherwise
386 * gdbm will complain when we try to delete an invalid record
388 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
392 /* Go ahead and delete it */
393 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
401 * sort message pointers
402 * (returns new msg count)
404 int sort_msglist(long listptrs[], int oldcount)
415 for (a = numitems - 2; a >= 0; --a) {
416 for (b = 0; b <= a; ++b) {
417 if (listptrs[b] > (listptrs[b + 1])) {
419 hold2 = listptrs[b + 1];
421 listptrs[b + 1] = hold1;
426 /* and yank any nulls */
427 while ((numitems > 0) && (listptrs[0] == 0L)) {
428 memcpy(&listptrs[0], &listptrs[1],
429 (sizeof(long) * (numitems - 1)));
438 * Determine whether a given room is non-editable.
440 int is_noneditable(struct quickroom *qrbuf)
443 /* Lobby> and Aide> are non-editable */
444 if (!strcasecmp(qrbuf->QRname, BASEROOM))
446 else if (!strcasecmp(qrbuf->QRname, AIDEROOM))
449 /* Mail> rooms are also non-editable */
450 else if ( (qrbuf->QRflags & QR_MAILBOX)
451 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
454 /* Everything else is editable */
462 * Back-back-end for all room listing commands
464 void list_roomname(struct quickroom *qrbuf)
466 char truncated_roomname[ROOMNAMELEN];
468 /* For mailbox rooms, chop off the owner prefix */
469 if (qrbuf->QRflags & QR_MAILBOX) {
470 strcpy(truncated_roomname, qrbuf->QRname);
471 strcpy(truncated_roomname, &truncated_roomname[11]);
472 cprintf("%s", truncated_roomname);
474 /* For all other rooms, just display the name in its entirety */
476 cprintf("%s", qrbuf->QRname);
479 /* ...and now the other parameters */
480 cprintf("|%u|%d|%d\n",
482 (int) qrbuf->QRfloor,
483 (int) qrbuf->QRorder);
488 * cmd_lrms() - List all accessible rooms, known or forgotten
490 void cmd_lrms_backend(struct quickroom *qrbuf, void *data)
492 int FloorBeingSearched = (-1);
493 FloorBeingSearched = *(int *)data;
495 if (((CtdlRoomAccess(qrbuf, &CC->usersupp)
496 & (UA_KNOWN | UA_ZAPPED)))
497 && ((qrbuf->QRfloor == (FloorBeingSearched))
498 || ((FloorBeingSearched) < 0)))
499 list_roomname(qrbuf);
502 void cmd_lrms(char *argbuf)
504 int FloorBeingSearched = (-1);
505 if (strlen(argbuf) > 0)
506 FloorBeingSearched = extract_int(argbuf, 0);
508 if (CtdlAccessCheck(ac_logged_in)) return;
510 if (getuser(&CC->usersupp, CC->curr_user)) {
511 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
514 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
516 ForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
523 * cmd_lkra() - List all known rooms
525 void cmd_lkra_backend(struct quickroom *qrbuf, void *data)
527 int FloorBeingSearched = (-1);
528 FloorBeingSearched = *(int *)data;
530 if (((CtdlRoomAccess(qrbuf, &CC->usersupp)
532 && ((qrbuf->QRfloor == (FloorBeingSearched))
533 || ((FloorBeingSearched) < 0)))
534 list_roomname(qrbuf);
537 void cmd_lkra(char *argbuf)
539 int FloorBeingSearched = (-1);
540 if (strlen(argbuf) > 0)
541 FloorBeingSearched = extract_int(argbuf, 0);
543 if (CtdlAccessCheck(ac_logged_in)) return;
545 if (getuser(&CC->usersupp, CC->curr_user)) {
546 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
549 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
551 ForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
558 * cmd_lkrn() - List all known rooms with new messages
560 void cmd_lkrn_backend(struct quickroom *qrbuf, void *data)
563 int FloorBeingSearched = (-1);
564 FloorBeingSearched = *(int *)data;
566 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
568 && (ra & UA_HASNEWMSGS)
569 && ((qrbuf->QRfloor == (FloorBeingSearched))
570 || ((FloorBeingSearched) < 0)))
571 list_roomname(qrbuf);
574 void cmd_lkrn(char *argbuf)
576 int FloorBeingSearched = (-1);
577 if (strlen(argbuf) > 0)
578 FloorBeingSearched = extract_int(argbuf, 0);
580 if (CtdlAccessCheck(ac_logged_in)) return;
582 if (getuser(&CC->usersupp, CC->curr_user)) {
583 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
586 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
588 ForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
595 * cmd_lkro() - List all known rooms
597 void cmd_lkro_backend(struct quickroom *qrbuf, void *data)
600 int FloorBeingSearched = (-1);
601 FloorBeingSearched = *(int *)data;
603 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
605 && ((ra & UA_HASNEWMSGS) == 0)
606 && ((qrbuf->QRfloor == (FloorBeingSearched))
607 || ((FloorBeingSearched) < 0)))
608 list_roomname(qrbuf);
611 void cmd_lkro(char *argbuf)
613 int FloorBeingSearched = (-1);
614 if (strlen(argbuf) > 0)
615 FloorBeingSearched = extract_int(argbuf, 0);
617 if (CtdlAccessCheck(ac_logged_in)) return;
619 if (getuser(&CC->usersupp, CC->curr_user)) {
620 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
623 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
625 ForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
632 * cmd_lzrm() - List all forgotten rooms
634 void cmd_lzrm_backend(struct quickroom *qrbuf, void *data)
637 int FloorBeingSearched = (-1);
638 FloorBeingSearched = *(int *)data;
640 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
641 if ((ra & UA_GOTOALLOWED)
643 && ((qrbuf->QRfloor == (FloorBeingSearched))
644 || ((FloorBeingSearched) < 0)))
645 list_roomname(qrbuf);
648 void cmd_lzrm(char *argbuf)
650 int FloorBeingSearched = (-1);
651 if (strlen(argbuf) > 0)
652 FloorBeingSearched = extract_int(argbuf, 0);
654 if (CtdlAccessCheck(ac_logged_in)) return;
656 if (getuser(&CC->usersupp, CC->curr_user)) {
657 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
660 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
662 ForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
668 void usergoto(char *where, int display_result, int *retmsgs, int *retnew)
671 int new_messages = 0;
672 int total_messages = 0;
676 int newmailcount = 0;
678 char truncated_roomname[ROOMNAMELEN];
679 struct cdbdata *cdbfr;
680 long *msglist = NULL;
683 strcpy(CC->quickroom.QRname, where);
684 getroom(&CC->quickroom, where);
686 lgetuser(&CC->usersupp, CC->curr_user);
687 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
689 /* Know the room ... but not if it's the page log room */
690 if (strcasecmp(where, config.c_logpages)) {
691 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
692 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
694 CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
695 lputuser(&CC->usersupp);
697 /* check for new mail */
698 newmailcount = NewMailCount();
700 /* set info to 1 if the user needs to read the room's info file */
701 if (CC->quickroom.QRinfo > vbuf.v_lastseen)
705 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->quickroom.QRnumber, sizeof(long));
707 msglist = mallok(cdbfr->len);
708 memcpy(msglist, cdbfr->ptr, cdbfr->len);
709 num_msgs = cdbfr->len / sizeof(long);
713 if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
714 if (msglist[a] > 0L) {
716 if (is_msg_in_mset(vbuf.v_seen, msglist[a]) == 0) {
722 if (msglist != NULL) phree(msglist);
724 if (CC->quickroom.QRflags & QR_MAILBOX)
729 if ((CC->quickroom.QRroomaide == CC->usersupp.usernum)
730 || (CC->usersupp.axlevel >= 6))
735 strcpy(truncated_roomname, CC->quickroom.QRname);
736 if (CC->quickroom.QRflags & QR_MAILBOX) {
737 strcpy(truncated_roomname, &truncated_roomname[11]);
740 if (retmsgs != NULL) *retmsgs = total_messages;
741 if (retnew != NULL) *retnew = new_messages;
742 lprintf(9, "<%s> %d new of %d total messages\n",
743 CC->quickroom.QRname, new_messages, total_messages);
746 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d\n",
747 OK, CtdlCheckExpress(),
749 new_messages, total_messages,
750 info, CC->quickroom.QRflags,
751 CC->quickroom.QRhighest,
753 rmailflag, raideflag, newmailcount,
754 CC->quickroom.QRfloor);
760 * cmd_goto() - goto a new room
762 void cmd_goto(char *gargs)
764 struct quickroom QRscratch;
768 char augmented_roomname[SIZ];
772 if (CtdlAccessCheck(ac_logged_in)) return;
774 extract(towhere, gargs, 0);
775 extract(password, gargs, 1);
777 getuser(&CC->usersupp, CC->curr_user);
779 if (!strcasecmp(towhere, "_BASEROOM_"))
780 strcpy(towhere, BASEROOM);
782 if (!strcasecmp(towhere, "_MAIL_"))
783 strcpy(towhere, MAILROOM);
785 if (!strcasecmp(towhere, "_BITBUCKET_"))
786 strcpy(towhere, config.c_twitroom);
789 /* First try a regular match */
790 c = getroom(&QRscratch, towhere);
792 /* Then try a mailbox name match */
794 MailboxName(augmented_roomname, &CC->usersupp, towhere);
795 c = getroom(&QRscratch, augmented_roomname);
797 strcpy(towhere, augmented_roomname);
800 /* And if the room was found... */
803 /* let internal programs go directly to any room */
804 if (CC->internal_pgm) {
805 usergoto(towhere, 1, NULL, NULL);
809 /* See if there is an existing user/room relationship */
810 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
812 /* normal clients have to pass through security */
813 if (ra & UA_GOTOALLOWED)
817 if ((QRscratch.QRflags & QR_PASSWORDED) &&
818 ((ra & UA_KNOWN) == 0) &&
819 (strcasecmp(QRscratch.QRpasswd, password)) &&
820 (CC->usersupp.axlevel < 6)
822 cprintf("%d wrong or missing passwd\n",
823 ERROR + PASSWORD_REQUIRED);
825 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
826 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
827 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
828 ((ra & UA_KNOWN) == 0) &&
829 (CC->usersupp.axlevel < 6)
833 usergoto(towhere, 1, NULL, NULL);
839 NOPE: cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
845 struct usersupp temp;
846 struct cdbdata *cdbus;
848 getuser(&CC->usersupp, CC->curr_user);
849 if (CtdlAccessCheck(ac_room_aide)) return;
851 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
852 cdb_rewind(CDB_USERSUPP);
853 while (cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
854 memset(&temp, 0, sizeof temp);
855 memcpy(&temp, cdbus->ptr, sizeof temp);
858 if ((CC->quickroom.QRflags & QR_INUSE)
859 && (CtdlRoomAccess(&CC->quickroom, &temp) & UA_KNOWN)
861 cprintf("%s\n", temp.fullname);
868 * RDIR command for room directory
878 if (CtdlAccessCheck(ac_logged_in)) return;
880 getroom(&CC->quickroom, CC->quickroom.QRname);
881 getuser(&CC->usersupp, CC->curr_user);
883 if ((CC->quickroom.QRflags & QR_DIRECTORY) == 0) {
884 cprintf("%d not here.\n", ERROR + NOT_HERE);
887 if (((CC->quickroom.QRflags & QR_VISDIR) == 0)
888 && (CC->usersupp.axlevel < 6)
889 && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
890 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
893 cprintf("%d %s|%s/files/%s\n",
894 LISTING_FOLLOWS, config.c_fqdn, BBSDIR, CC->quickroom.QRdirname);
896 sprintf(buf, "ls %s/files/%s >%s 2> /dev/null",
897 BBSDIR, CC->quickroom.QRdirname, CC->temp);
900 sprintf(buf, "%s/files/%s/filedir", BBSDIR, CC->quickroom.QRdirname);
901 fd = fopen(buf, "r");
903 fd = fopen("/dev/null", "r");
905 ls = fopen(CC->temp, "r");
906 while (fgets(flnm, sizeof flnm, ls) != NULL) {
907 flnm[strlen(flnm) - 1] = 0;
908 if (strcasecmp(flnm, "filedir")) {
909 sprintf(buf, "%s/files/%s/%s",
910 BBSDIR, CC->quickroom.QRdirname, flnm);
914 while ((fgets(buf, sizeof buf, fd) != NULL)
915 && (strlen(comment) == 0)) {
916 buf[strlen(buf) - 1] = 0;
917 if ((!strncasecmp(buf, flnm, strlen(flnm)))
918 && (buf[strlen(flnm)] == ' '))
920 &buf[strlen(flnm) + 1],
923 cprintf("%s|%ld|%s\n", flnm, statbuf.st_size, comment);
934 * get room parameters (aide or room aide command)
938 if (CtdlAccessCheck(ac_room_aide)) return;
941 if (is_noneditable(&CC->quickroom)) {
942 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
947 getroom(&CC->quickroom, CC->quickroom.QRname);
948 cprintf("%d%c%s|%s|%s|%d|%d|%d\n",
949 OK, CtdlCheckExpress(),
950 CC->quickroom.QRname,
951 ((CC->quickroom.QRflags & QR_PASSWORDED) ? CC->quickroom.QRpasswd : ""),
952 ((CC->quickroom.QRflags & QR_DIRECTORY) ? CC->quickroom.QRdirname : ""),
953 CC->quickroom.QRflags,
954 (int) CC->quickroom.QRfloor,
955 (int) CC->quickroom.QRorder);
960 * set room parameters (aide or room aide command)
962 void cmd_setr(char *args)
967 char old_name[ROOMNAMELEN];
972 if (CtdlAccessCheck(ac_room_aide)) return;
974 if (is_noneditable(&CC->quickroom)) {
979 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
985 if (num_parms(args) >= 6) {
986 fl = cgetfloor(extract_int(args, 5));
987 if ((fl->f_flags & F_INUSE) == 0) {
988 cprintf("%d Invalid floor number.\n",
989 ERROR + INVALID_FLOOR_OPERATION);
993 if (num_parms(args) >= 7) {
994 new_order = extract_int(args, 6);
1000 lgetroom(&CC->quickroom, CC->quickroom.QRname);
1002 /* Non-editable base rooms can't be renamed */
1003 strcpy(old_name, CC->quickroom.QRname);
1005 extract(buf, args, 0);
1006 buf[ROOMNAMELEN] = 0;
1007 safestrncpy(CC->quickroom.QRname, buf,
1008 sizeof CC->quickroom.QRname);
1011 extract(buf, args, 1);
1013 safestrncpy(CC->quickroom.QRpasswd, buf, sizeof CC->quickroom.QRpasswd);
1014 extract(buf, args, 2);
1016 safestrncpy(CC->quickroom.QRdirname, buf,
1017 sizeof CC->quickroom.QRdirname);
1018 CC->quickroom.QRflags = (extract_int(args, 3) | QR_INUSE);
1019 if (num_parms(args) >= 7)
1020 CC->quickroom.QRorder = (char) new_order;
1022 /* Clean up a client boo-boo: if the client set the room to
1023 * guess-name or passworded, ensure that the private flag is
1026 if ((CC->quickroom.QRflags & QR_GUESSNAME)
1027 || (CC->quickroom.QRflags & QR_PASSWORDED))
1028 CC->quickroom.QRflags |= QR_PRIVATE;
1030 /* Kick everyone out if the client requested it (by changing the
1031 * room's generation number)
1033 if (extract_int(args, 4)) {
1034 time(&CC->quickroom.QRgen);
1036 old_floor = CC->quickroom.QRfloor;
1037 if (num_parms(args) >= 6) {
1038 CC->quickroom.QRfloor = extract_int(args, 5);
1040 /* Write the room record back to disk */
1041 lputroom(&CC->quickroom);
1043 /* If the room name changed, then there are now two room records,
1044 * so we have to delete the old one.
1046 if (strcasecmp(CC->quickroom.QRname, old_name)) {
1047 b_deleteroom(old_name);
1049 /* adjust the floor reference counts */
1050 lgetfloor(&flbuf, old_floor);
1051 --flbuf.f_ref_count;
1052 lputfloor(&flbuf, old_floor);
1053 lgetfloor(&flbuf, CC->quickroom.QRfloor);
1054 ++flbuf.f_ref_count;
1055 lputfloor(&flbuf, CC->quickroom.QRfloor);
1057 /* create a room directory if necessary */
1058 if (CC->quickroom.QRflags & QR_DIRECTORY) {
1060 "mkdir ./files/%s </dev/null >/dev/null 2>/dev/null",
1061 CC->quickroom.QRdirname);
1064 sprintf(buf, "%s> edited by %s\n", CC->quickroom.QRname, CC->curr_user);
1066 cprintf("%d Ok\n", OK);
1072 * get the name of the room aide for this room
1076 struct usersupp usbuf;
1078 if (CtdlAccessCheck(ac_logged_in)) return;
1080 if (is_noneditable(&CC->quickroom)) {
1081 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
1084 if (getuserbynumber(&usbuf, CC->quickroom.QRroomaide) == 0) {
1085 cprintf("%d %s\n", OK, usbuf.fullname);
1087 cprintf("%d \n", OK);
1093 * set the room aide for this room
1095 void cmd_seta(char *new_ra)
1097 struct usersupp usbuf;
1102 if (CtdlAccessCheck(ac_room_aide)) return;
1104 if (getuser(&usbuf, new_ra) != 0) {
1107 newu = usbuf.usernum;
1110 lgetroom(&CC->quickroom, CC->quickroom.QRname);
1112 if (CC->quickroom.QRroomaide != newu) {
1115 CC->quickroom.QRroomaide = newu;
1116 lputroom(&CC->quickroom);
1119 * We have to post the change notice _after_ writing changes to
1120 * the room table, otherwise it would deadlock!
1122 if (post_notice == 1) {
1123 sprintf(buf, "%s is now room aide for %s>\n",
1124 usbuf.fullname, CC->quickroom.QRname);
1127 cprintf("%d Ok\n", OK);
1131 * Generate an associated file name for a room
1133 void assoc_file_name(char *buf, struct quickroom *qrbuf, char *prefix)
1135 sprintf(buf, "./%s/%ld", prefix, qrbuf->QRnumber);
1139 * retrieve info file for this room
1147 assoc_file_name(filename, &CC->quickroom, "info");
1148 info_fp = fopen(filename, "r");
1150 if (info_fp == NULL) {
1151 cprintf("%d No info file.\n", ERROR);
1154 cprintf("%d Info:\n", LISTING_FOLLOWS);
1155 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1156 if (strlen(buf) > 0)
1157 buf[strlen(buf) - 1] = 0;
1158 cprintf("%s\n", buf);
1165 * Back end processing to delete a room and everything associated with it
1167 void delete_room(struct quickroom *qrbuf)
1172 lprintf(9, "Deleting room <%s>\n", qrbuf->QRname);
1174 /* Delete the info file */
1175 assoc_file_name(filename, qrbuf, "info");
1178 /* Delete the image file */
1179 assoc_file_name(filename, qrbuf, "images");
1182 /* Delete the room's network config file */
1183 assoc_file_name(filename, qrbuf, "netconfigs");
1186 /* Delete the messages in the room
1187 * (Careful: this opens an S_QUICKROOM critical section!)
1189 CtdlDeleteMessages(qrbuf->QRname, 0L, "");
1191 /* Flag the room record as not in use */
1192 lgetroom(qrbuf, qrbuf->QRname);
1196 /* then decrement the reference count for the floor */
1197 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1198 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1199 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1201 /* Delete the room record from the database! */
1202 b_deleteroom(qrbuf->QRname);
1208 * Check access control for deleting a room
1210 int CtdlDoIHavePermissionToDeleteThisRoom(struct quickroom *qr) {
1212 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1216 if (is_noneditable(qr)) {
1221 * For mailboxes, check stuff
1223 if (qr->QRflags & QR_MAILBOX) {
1225 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1227 if (atol(qr->QRname) != CC->usersupp.usernum) {
1228 return(0); /* not my room */
1231 /* Can't delete your Mail> room */
1232 if (!strcasecmp(&qr->QRname[12], MAILROOM)) return(0);
1234 /* Otherwise it's ok */
1239 * For normal rooms, just check for aide or room aide status.
1242 return(is_room_aide());
1245 /* Should never get to this point, but to keep the compiler quiet... */
1250 * aide command: kill the current room
1252 void cmd_kill(char *argbuf)
1255 char deleted_room_name[ROOMNAMELEN];
1258 kill_ok = extract_int(argbuf, 0);
1260 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->quickroom) == 0) {
1261 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1265 strcpy(deleted_room_name, CC->quickroom.QRname);
1266 delete_room(&CC->quickroom); /* Do the dirty work */
1267 usergoto(BASEROOM, 0, NULL, NULL); /* Return to the Lobby */
1269 /* tell the world what we did */
1270 sprintf(aaa, "%s> killed by %s\n",
1271 deleted_room_name, CC->curr_user);
1273 cprintf("%d '%s' deleted.\n", OK, deleted_room_name);
1275 cprintf("%d ok to delete.\n", OK);
1281 * Internal code to create a new room (returns room flags)
1283 * Room types: 0=public, 1=guessname, 2=passworded, 3=inv-only,
1284 * 4=mailbox, 5=mailbox, but caller supplies namespace
1286 unsigned create_room(char *new_room_name,
1288 char *new_room_pass,
1293 struct quickroom qrbuf;
1297 lprintf(9, "create_room(%s)\n", new_room_name);
1298 if (getroom(&qrbuf, new_room_name) == 0) {
1299 lprintf(9, "%s already exists.\n", new_room_name);
1300 return (0); /* already exists */
1304 memset(&qrbuf, 0, sizeof(struct quickroom));
1305 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1306 qrbuf.QRflags = QR_INUSE;
1307 if (new_room_type > 0)
1308 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1309 if (new_room_type == 1)
1310 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1311 if (new_room_type == 2)
1312 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1313 if ( (new_room_type == 4) || (new_room_type == 5) )
1314 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1316 /* If the user is requesting a personal room, set up the room
1317 * name accordingly (prepend the user number)
1319 if (new_room_type == 4) {
1320 MailboxName(qrbuf.QRname, &CC->usersupp, new_room_name);
1323 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1326 /* If the room is private, and the system administrator has elected
1327 * to automatically grant room aide privileges, do so now; otherwise,
1328 * set the room aide to undefined.
1330 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1331 qrbuf.QRroomaide = CC->usersupp.usernum;
1333 qrbuf.QRroomaide = (-1L);
1337 * If the caller is only interested in testing whether this will work,
1338 * return now without creating the room.
1340 if (!really_create) return (qrbuf.QRflags);
1342 qrbuf.QRnumber = get_new_room_number();
1343 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1344 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1345 qrbuf.QRfloor = new_room_floor;
1347 /* save what we just did... */
1350 /* bump the reference count on whatever floor the room is on */
1351 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1352 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1353 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1355 /* be sure not to kick the creator out of the room! */
1356 lgetuser(&CC->usersupp, CC->curr_user);
1357 CtdlGetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1358 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1359 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1360 CtdlSetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1361 lputuser(&CC->usersupp);
1363 /* resume our happy day */
1364 return (qrbuf.QRflags);
1371 void cmd_cre8(char *args)
1374 char new_room_name[SIZ];
1376 char new_room_pass[SIZ];
1380 struct quickroom qrbuf;
1383 cre8_ok = extract_int(args, 0);
1384 extract(new_room_name, args, 1);
1385 new_room_name[ROOMNAMELEN - 1] = 0;
1386 new_room_type = extract_int(args, 2);
1387 extract(new_room_pass, args, 3);
1388 new_room_pass[9] = 0;
1391 if ((strlen(new_room_name) == 0) && (cre8_ok == 1)) {
1392 cprintf("%d Invalid room name.\n", ERROR);
1396 if (!strcasecmp(new_room_name, MAILROOM)) {
1397 cprintf("%d '%s' already exists.\n",
1398 ERROR + ALREADY_EXISTS, new_room_name);
1402 if (num_parms(args) >= 5) {
1403 fl = cgetfloor(extract_int(args, 4));
1404 if ((fl->f_flags & F_INUSE) == 0) {
1405 cprintf("%d Invalid floor number.\n",
1406 ERROR + INVALID_FLOOR_OPERATION);
1409 new_room_floor = extract_int(args, 4);
1413 if (CtdlAccessCheck(ac_logged_in)) return;
1415 if (CC->usersupp.axlevel < config.c_createax) {
1416 cprintf("%d You need higher access to create rooms.\n",
1417 ERROR + HIGHER_ACCESS_REQUIRED);
1421 if ((strlen(new_room_name) == 0) && (cre8_ok == 0)) {
1422 cprintf("%d Ok to create rooms.\n", OK);
1426 if ((new_room_type < 0) || (new_room_type > 4)) {
1427 cprintf("%d Invalid room type.\n", ERROR);
1431 /* Check to make sure the requested room name doesn't already exist */
1432 newflags = create_room(new_room_name,
1433 new_room_type, new_room_pass, new_room_floor, 0);
1434 if (newflags == 0) {
1435 cprintf("%d '%s' already exists.\n",
1436 ERROR + ALREADY_EXISTS, qrbuf.QRname);
1441 cprintf("%d OK to create '%s'\n", OK, new_room_name);
1445 /* If we reach this point, the room needs to be created. */
1447 newflags = create_room(new_room_name,
1448 new_room_type, new_room_pass, new_room_floor, 1);
1450 /* post a message in Aide> describing the new room */
1451 safestrncpy(aaa, new_room_name, sizeof aaa);
1452 strcat(aaa, "> created by ");
1453 strcat(aaa, CC->usersupp.fullname);
1454 if (newflags & QR_MAILBOX)
1455 strcat(aaa, " [personal]");
1456 else if (newflags & QR_PRIVATE)
1457 strcat(aaa, " [private]");
1458 if (newflags & QR_GUESSNAME)
1459 strcat(aaa, "[guessname] ");
1460 if (newflags & QR_PASSWORDED) {
1461 strcat(aaa, "\n Password: ");
1462 strcat(aaa, new_room_pass);
1467 cprintf("%d '%s' has been created.\n", OK, qrbuf.QRname);
1472 void cmd_einf(char *ok)
1473 { /* enter info file for current room */
1475 char infofilename[SIZ];
1478 if (CtdlAccessCheck(ac_room_aide)) return;
1480 if (atoi(ok) == 0) {
1481 cprintf("%d Ok.\n", OK);
1484 assoc_file_name(infofilename, &CC->quickroom, "info");
1485 lprintf(9, "opening\n");
1486 fp = fopen(infofilename, "w");
1487 lprintf(9, "checking\n");
1489 cprintf("%d Cannot open %s: %s\n",
1490 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1493 cprintf("%d Send info...\n", SEND_LISTING);
1497 if (strcmp(buf, "000"))
1498 fprintf(fp, "%s\n", buf);
1499 } while (strcmp(buf, "000"));
1502 /* now update the room index so people will see our new info */
1503 lgetroom(&CC->quickroom, CC->quickroom.QRname); /* lock so no one steps on us */
1504 CC->quickroom.QRinfo = CC->quickroom.QRhighest + 1L;
1505 lputroom(&CC->quickroom);
1510 * cmd_lflr() - List all known floors
1517 if (CtdlAccessCheck(ac_logged_in)) return;
1519 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1521 for (a = 0; a < MAXFLOORS; ++a) {
1522 getfloor(&flbuf, a);
1523 if (flbuf.f_flags & F_INUSE) {
1524 cprintf("%d|%s|%d\n",
1536 * create a new floor
1538 void cmd_cflr(char *argbuf)
1540 char new_floor_name[SIZ];
1543 int free_slot = (-1);
1546 extract(new_floor_name, argbuf, 0);
1547 cflr_ok = extract_int(argbuf, 1);
1550 if (CtdlAccessCheck(ac_aide)) return;
1552 for (a = 0; a < MAXFLOORS; ++a) {
1553 getfloor(&flbuf, a);
1555 /* note any free slots while we're scanning... */
1556 if (((flbuf.f_flags & F_INUSE) == 0)
1560 /* check to see if it already exists */
1561 if ((!strcasecmp(flbuf.f_name, new_floor_name))
1562 && (flbuf.f_flags & F_INUSE)) {
1563 cprintf("%d Floor '%s' already exists.\n",
1564 ERROR + ALREADY_EXISTS,
1570 if (free_slot < 0) {
1571 cprintf("%d There is no space available for a new floor.\n",
1572 ERROR + INVALID_FLOOR_OPERATION);
1576 cprintf("%d ok to create...\n", OK);
1579 lgetfloor(&flbuf, free_slot);
1580 flbuf.f_flags = F_INUSE;
1581 flbuf.f_ref_count = 0;
1582 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
1583 lputfloor(&flbuf, free_slot);
1584 cprintf("%d %d\n", OK, free_slot);
1592 void cmd_kflr(char *argbuf)
1595 int floor_to_delete;
1599 floor_to_delete = extract_int(argbuf, 0);
1600 kflr_ok = extract_int(argbuf, 1);
1602 if (CtdlAccessCheck(ac_aide)) return;
1604 lgetfloor(&flbuf, floor_to_delete);
1607 if ((flbuf.f_flags & F_INUSE) == 0) {
1608 cprintf("%d Floor %d not in use.\n",
1609 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
1612 if (flbuf.f_ref_count != 0) {
1613 cprintf("%d Cannot delete; floor contains %d rooms.\n",
1614 ERROR + INVALID_FLOOR_OPERATION,
1619 cprintf("%d Ok\n", OK);
1621 cprintf("%d Ok to delete...\n", OK);
1628 if ((delete_ok == 1) && (kflr_ok == 1))
1630 lputfloor(&flbuf, floor_to_delete);
1636 void cmd_eflr(char *argbuf)
1642 np = num_parms(argbuf);
1644 cprintf("%d Usage error.\n", ERROR);
1648 if (CtdlAccessCheck(ac_aide)) return;
1650 floor_num = extract_int(argbuf, 0);
1651 lgetfloor(&flbuf, floor_num);
1652 if ((flbuf.f_flags & F_INUSE) == 0) {
1653 lputfloor(&flbuf, floor_num);
1654 cprintf("%d Floor %d is not in use.\n",
1655 ERROR + INVALID_FLOOR_OPERATION, floor_num);
1659 extract(flbuf.f_name, argbuf, 1);
1660 lputfloor(&flbuf, floor_num);
1662 cprintf("%d Ok\n", OK);