4 * Server functions which perform operations on room objects.
16 #if TIME_WITH_SYS_TIME
17 # include <sys/time.h>
21 # include <sys/time.h>
34 #include "sysdep_decls.h"
38 #include "citserver.h"
41 #include "citadel_dirs.h"
43 struct floor *floorcache[MAXFLOORS];
46 * Retrieve access control information for any user/room pair
48 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf,
49 int *result, int *view)
54 /* for internal programs, always do everything */
55 if (((CC->internal_pgm)) && (roombuf->QRflags & QR_INUSE)) {
56 retval = (UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED);
61 /* Locate any applicable user/room relationships */
62 CtdlGetRelationship(&vbuf, userbuf, roombuf);
64 /* Force the properties of the Aide room */
65 if (!strcasecmp(roombuf->QRname, config.c_aideroom)) {
66 if (userbuf->axlevel >= 6) {
67 retval = UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
74 /* If this is a public room, it's accessible... */
75 if ( ((roombuf->QRflags & QR_PRIVATE) == 0)
76 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
77 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
80 /* If this is a preferred users only room, check access level */
81 if (roombuf->QRflags & QR_PREFONLY) {
82 if (userbuf->axlevel < 5) {
83 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
87 /* For private rooms, check the generation number matchups */
88 if ( (roombuf->QRflags & QR_PRIVATE)
89 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
91 /* An explicit match means the user belongs in this room */
92 if (vbuf.v_flags & V_ACCESS) {
93 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
95 /* Otherwise, check if this is a guess-name or passworded
96 * room. If it is, a goto may at least be attempted
98 else if ((roombuf->QRflags & QR_PRIVATE)
99 || (roombuf->QRflags & QR_PASSWORDED)) {
100 retval = retval & ~UA_KNOWN;
101 retval = retval | UA_GOTOALLOWED;
105 /* For mailbox rooms, also check the namespace */
106 /* Also, mailbox owners can delete their messages */
107 if (roombuf->QRflags & QR_MAILBOX) {
108 if (userbuf->usernum == atol(roombuf->QRname)) {
109 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
111 /* An explicit match means the user belongs in this room */
112 if (vbuf.v_flags & V_ACCESS) {
113 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
117 /* For non-mailbox rooms... */
120 /* User is allowed to post in the room unless:
121 * - User is not validated
122 * - User has no net privileges and it is a shared network room
123 * - It is a read-only room
125 int post_allowed = 1;
126 if (CC->user.axlevel < 2) post_allowed = 0;
127 if ((CC->user.axlevel < 4) && (CC->room.QRflags & QR_NETWORK)) post_allowed = 0;
128 if (roombuf->QRflags & QR_READONLY) post_allowed = 0;
130 retval = retval | UA_POSTALLOWED;
133 /* If "collaborative deletion" is active for this room, any user who can post
134 * is also allowed to delete
136 if (CC->room.QRflags2 & QR2_COLLABDEL) {
137 if (retval & UA_POSTALLOWED) {
138 retval = retval | UA_DELETEALLOWED;
144 /* Check to see if the user has forgotten this room */
145 if (vbuf.v_flags & V_FORGET) {
146 retval = retval & ~UA_KNOWN;
147 if ( ( ((roombuf->QRflags & QR_PRIVATE) == 0)
148 && ((roombuf->QRflags & QR_MAILBOX) == 0) )
149 || ( (roombuf->QRflags & QR_MAILBOX)
150 && (atol(roombuf->QRname) == CC->user.usernum))) {
151 retval = retval | UA_ZAPPED;
154 /* If user is explicitly locked out of this room, deny everything */
155 if (vbuf.v_flags & V_LOCKOUT) {
156 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED & ~UA_POSTALLOWED;
159 /* Aides get access to all private rooms */
160 if ( (userbuf->axlevel >= 6)
161 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
162 if (vbuf.v_flags & V_FORGET) {
163 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED;
166 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED;
170 /* Aides can gain access to mailboxes as well, but they don't show
173 if ( (userbuf->axlevel >= 6)
174 && (roombuf->QRflags & QR_MAILBOX) ) {
175 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED;
178 /* Aides and Room Aides have admin privileges */
179 if ( (userbuf->axlevel >= 6)
180 || (userbuf->usernum == roombuf->QRroomaide)
182 retval = retval | UA_ADMINALLOWED | UA_DELETEALLOWED;
185 NEWMSG: /* By the way, we also check for the presence of new messages */
186 if (is_msg_in_sequence_set(vbuf.v_seen, roombuf->QRhighest) == 0) {
187 retval = retval | UA_HASNEWMSGS;
190 /* System rooms never show up in the list. */
191 if (roombuf->QRflags2 & QR2_SYSTEM) {
192 retval = retval & ~UA_KNOWN;
196 /* Now give the caller the information it wants. */
197 if (result != NULL) *result = retval;
198 if (view != NULL) *view = vbuf.v_view;
202 * Self-checking stuff for a room record read into memory
204 void room_sanity_check(struct ctdlroom *qrbuf)
206 /* Mailbox rooms are always on the lowest floor */
207 if (qrbuf->QRflags & QR_MAILBOX) {
210 /* Listing order of 0 is illegal except for base rooms */
211 if (qrbuf->QRorder == 0)
212 if (!(qrbuf->QRflags & QR_MAILBOX) &&
213 strncasecmp(qrbuf->QRname, config.c_baseroom, ROOMNAMELEN)
215 strncasecmp(qrbuf->QRname, config.c_aideroom, ROOMNAMELEN))
221 * getroom() - retrieve room data from disk
223 int getroom(struct ctdlroom *qrbuf, char *room_name)
225 struct cdbdata *cdbqr;
226 char lowercase_name[ROOMNAMELEN];
227 char personal_lowercase_name[ROOMNAMELEN];
230 for (a = 0; room_name[a] && a < sizeof lowercase_name - 1; ++a) {
231 lowercase_name[a] = tolower(room_name[a]);
233 lowercase_name[a] = 0;
235 memset(qrbuf, 0, sizeof(struct ctdlroom));
237 /* First, try the public namespace */
238 cdbqr = cdb_fetch(CDB_ROOMS,
239 lowercase_name, strlen(lowercase_name));
241 /* If that didn't work, try the user's personal namespace */
243 snprintf(personal_lowercase_name,
244 sizeof personal_lowercase_name, "%010ld.%s",
245 CC->user.usernum, lowercase_name);
246 cdbqr = cdb_fetch(CDB_ROOMS,
247 personal_lowercase_name,
248 strlen(personal_lowercase_name));
251 memcpy(qrbuf, cdbqr->ptr,
252 ((cdbqr->len > sizeof(struct ctdlroom)) ?
253 sizeof(struct ctdlroom) : cdbqr->len));
256 room_sanity_check(qrbuf);
265 * lgetroom() - same as getroom() but locks the record (if supported)
267 int lgetroom(struct ctdlroom *qrbuf, char *room_name)
270 retval = getroom(qrbuf, room_name);
271 if (retval == 0) begin_critical_section(S_ROOMS);
277 * b_putroom() - back end to putroom() and b_deleteroom()
278 * (if the supplied buffer is NULL, delete the room record)
280 void b_putroom(struct ctdlroom *qrbuf, char *room_name)
282 char lowercase_name[ROOMNAMELEN];
285 for (a = 0; a <= strlen(room_name); ++a) {
286 lowercase_name[a] = tolower(room_name[a]);
290 cdb_delete(CDB_ROOMS,
291 lowercase_name, strlen(lowercase_name));
293 time(&qrbuf->QRmtime);
295 lowercase_name, strlen(lowercase_name),
296 qrbuf, sizeof(struct ctdlroom));
302 * putroom() - store room data to disk
304 void putroom(struct ctdlroom *qrbuf) {
305 b_putroom(qrbuf, qrbuf->QRname);
310 * b_deleteroom() - delete a room record from disk
312 void b_deleteroom(char *room_name) {
313 b_putroom(NULL, room_name);
319 * lputroom() - same as putroom() but unlocks the record (if supported)
321 void lputroom(struct ctdlroom *qrbuf)
325 end_critical_section(S_ROOMS);
329 /****************************************************************************/
332 * getfloor() - retrieve floor data from disk
334 void getfloor(struct floor *flbuf, int floor_num)
336 struct cdbdata *cdbfl;
338 memset(flbuf, 0, sizeof(struct floor));
339 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
341 memcpy(flbuf, cdbfl->ptr,
342 ((cdbfl->len > sizeof(struct floor)) ?
343 sizeof(struct floor) : cdbfl->len));
346 if (floor_num == 0) {
347 safestrncpy(flbuf->f_name, "Main Floor",
348 sizeof flbuf->f_name);
349 flbuf->f_flags = F_INUSE;
350 flbuf->f_ref_count = 3;
357 * lgetfloor() - same as getfloor() but locks the record (if supported)
359 void lgetfloor(struct floor *flbuf, int floor_num)
362 begin_critical_section(S_FLOORTAB);
363 getfloor(flbuf, floor_num);
368 * cgetfloor() - Get floor record from *cache* (loads from disk if needed)
370 * This is strictly a performance hack.
372 struct floor *cgetfloor(int floor_num) {
373 static int initialized = 0;
376 struct floor *fl = NULL;
378 begin_critical_section(S_FLOORCACHE);
379 if (initialized == 0) {
380 for (i=0; i<MAXFLOORS; ++i) {
381 floorcache[floor_num] = NULL;
385 if (floorcache[floor_num] == NULL) {
388 end_critical_section(S_FLOORCACHE);
391 fl = malloc(sizeof(struct floor));
392 getfloor(fl, floor_num);
393 begin_critical_section(S_FLOORCACHE);
394 if (floorcache[floor_num] != NULL) {
395 free(floorcache[floor_num]);
397 floorcache[floor_num] = fl;
398 end_critical_section(S_FLOORCACHE);
401 return(floorcache[floor_num]);
407 * putfloor() - store floor data on disk
409 void putfloor(struct floor *flbuf, int floor_num)
411 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
412 begin_critical_section(S_FLOORCACHE);
413 if (floorcache[floor_num] != NULL) {
414 free(floorcache[floor_num]);
415 floorcache[floor_num] = malloc(sizeof(struct floor));
416 memcpy(floorcache[floor_num], flbuf, sizeof(struct floor));
418 end_critical_section(S_FLOORCACHE);
420 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
421 flbuf, sizeof(struct floor));
426 * lputfloor() - same as putfloor() but unlocks the record (if supported)
428 void lputfloor(struct floor *flbuf, int floor_num)
431 putfloor(flbuf, floor_num);
432 end_critical_section(S_FLOORTAB);
438 * Traverse the room file...
440 void ForEachRoom(void (*CallBack) (struct ctdlroom *EachRoom, void *out_data),
443 struct ctdlroom qrbuf;
444 struct cdbdata *cdbqr;
446 cdb_rewind(CDB_ROOMS);
448 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
449 memset(&qrbuf, 0, sizeof(struct ctdlroom));
450 memcpy(&qrbuf, cdbqr->ptr,
451 ((cdbqr->len > sizeof(struct ctdlroom)) ?
452 sizeof(struct ctdlroom) : cdbqr->len));
454 room_sanity_check(&qrbuf);
455 if (qrbuf.QRflags & QR_INUSE)
456 (*CallBack)(&qrbuf, in_data);
462 * delete_msglist() - delete room message pointers
464 void delete_msglist(struct ctdlroom *whichroom)
466 struct cdbdata *cdbml;
468 /* Make sure the msglist we're deleting actually exists, otherwise
469 * gdbm will complain when we try to delete an invalid record
471 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
475 /* Go ahead and delete it */
476 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
484 * sort message pointers
485 * (returns new msg count)
487 int sort_msglist(long listptrs[], int oldcount)
498 for (a = numitems - 2; a >= 0; --a) {
499 for (b = 0; b <= a; ++b) {
500 if (listptrs[b] > (listptrs[b + 1])) {
502 hold2 = listptrs[b + 1];
504 listptrs[b + 1] = hold1;
509 /* and yank any nulls */
510 while ((numitems > 0) && (listptrs[0] == 0L)) {
511 memcpy(&listptrs[0], &listptrs[1],
512 (sizeof(long) * (numitems - 1)));
521 * Determine whether a given room is non-editable.
523 int is_noneditable(struct ctdlroom *qrbuf)
526 /* Mail> rooms are non-editable */
527 if ( (qrbuf->QRflags & QR_MAILBOX)
528 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
531 /* Everything else is editable */
538 * Back-back-end for all room listing commands
540 void list_roomname(struct ctdlroom *qrbuf, int ra, int current_view, int default_view)
542 char truncated_roomname[ROOMNAMELEN];
544 /* For my own mailbox rooms, chop off the owner prefix */
545 if ( (qrbuf->QRflags & QR_MAILBOX)
546 && (atol(qrbuf->QRname) == CC->user.usernum) ) {
547 safestrncpy(truncated_roomname, qrbuf->QRname, sizeof truncated_roomname);
548 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
549 cprintf("%s", truncated_roomname);
551 /* For all other rooms, just display the name in its entirety */
553 cprintf("%s", qrbuf->QRname);
556 /* ...and now the other parameters */
557 cprintf("|%u|%d|%d|%d|%d|%d|%d|%ld|\n",
559 (int) qrbuf->QRfloor,
560 (int) qrbuf->QRorder,
561 (int) qrbuf->QRflags2,
571 * cmd_lrms() - List all accessible rooms, known or forgotten
573 void cmd_lrms_backend(struct ctdlroom *qrbuf, void *data)
575 int FloorBeingSearched = (-1);
579 FloorBeingSearched = *(int *)data;
580 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
582 if ((( ra & (UA_KNOWN | UA_ZAPPED)))
583 && ((qrbuf->QRfloor == (FloorBeingSearched))
584 || ((FloorBeingSearched) < 0)))
585 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
588 void cmd_lrms(char *argbuf)
590 int FloorBeingSearched = (-1);
591 if (strlen(argbuf) > 0)
592 FloorBeingSearched = extract_int(argbuf, 0);
594 if (CtdlAccessCheck(ac_logged_in)) return;
596 if (getuser(&CC->user, CC->curr_user)) {
597 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
600 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
602 ForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
609 * cmd_lkra() - List all known rooms
611 void cmd_lkra_backend(struct ctdlroom *qrbuf, void *data)
613 int FloorBeingSearched = (-1);
617 FloorBeingSearched = *(int *)data;
618 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
620 if ((( ra & (UA_KNOWN)))
621 && ((qrbuf->QRfloor == (FloorBeingSearched))
622 || ((FloorBeingSearched) < 0)))
623 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
626 void cmd_lkra(char *argbuf)
628 int FloorBeingSearched = (-1);
629 if (strlen(argbuf) > 0)
630 FloorBeingSearched = extract_int(argbuf, 0);
632 if (CtdlAccessCheck(ac_logged_in)) return;
634 if (getuser(&CC->user, CC->curr_user)) {
635 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
638 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
640 ForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
646 void cmd_lprm_backend(struct ctdlroom *qrbuf, void *data)
648 int FloorBeingSearched = (-1);
652 FloorBeingSearched = *(int *)data;
653 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
655 if ( ((qrbuf->QRflags & QR_PRIVATE) == 0)
656 && ((qrbuf->QRflags & QR_MAILBOX) == 0)
657 && ((qrbuf->QRfloor == (FloorBeingSearched))
658 || ((FloorBeingSearched) < 0)))
659 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
662 void cmd_lprm(char *argbuf)
664 int FloorBeingSearched = (-1);
665 if (strlen(argbuf) > 0)
666 FloorBeingSearched = extract_int(argbuf, 0);
668 cprintf("%d Publiic rooms:\n", LISTING_FOLLOWS);
670 ForEachRoom(cmd_lprm_backend, &FloorBeingSearched);
677 * cmd_lkrn() - List all known rooms with new messages
679 void cmd_lkrn_backend(struct ctdlroom *qrbuf, void *data)
681 int FloorBeingSearched = (-1);
685 FloorBeingSearched = *(int *)data;
686 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
689 && (ra & UA_HASNEWMSGS)
690 && ((qrbuf->QRfloor == (FloorBeingSearched))
691 || ((FloorBeingSearched) < 0)))
692 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
695 void cmd_lkrn(char *argbuf)
697 int FloorBeingSearched = (-1);
698 if (strlen(argbuf) > 0)
699 FloorBeingSearched = extract_int(argbuf, 0);
701 if (CtdlAccessCheck(ac_logged_in)) return;
703 if (getuser(&CC->user, CC->curr_user)) {
704 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
707 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
709 ForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
716 * cmd_lkro() - List all known rooms
718 void cmd_lkro_backend(struct ctdlroom *qrbuf, void *data)
720 int FloorBeingSearched = (-1);
724 FloorBeingSearched = *(int *)data;
725 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
728 && ((ra & UA_HASNEWMSGS) == 0)
729 && ((qrbuf->QRfloor == (FloorBeingSearched))
730 || ((FloorBeingSearched) < 0)))
731 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
734 void cmd_lkro(char *argbuf)
736 int FloorBeingSearched = (-1);
737 if (strlen(argbuf) > 0)
738 FloorBeingSearched = extract_int(argbuf, 0);
740 if (CtdlAccessCheck(ac_logged_in)) return;
742 if (getuser(&CC->user, CC->curr_user)) {
743 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
746 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
748 ForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
755 * cmd_lzrm() - List all forgotten rooms
757 void cmd_lzrm_backend(struct ctdlroom *qrbuf, void *data)
759 int FloorBeingSearched = (-1);
763 FloorBeingSearched = *(int *)data;
764 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
766 if ((ra & UA_GOTOALLOWED)
768 && ((qrbuf->QRfloor == (FloorBeingSearched))
769 || ((FloorBeingSearched) < 0)))
770 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
773 void cmd_lzrm(char *argbuf)
775 int FloorBeingSearched = (-1);
776 if (strlen(argbuf) > 0)
777 FloorBeingSearched = extract_int(argbuf, 0);
779 if (CtdlAccessCheck(ac_logged_in)) return;
781 if (getuser(&CC->user, CC->curr_user)) {
782 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
785 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
787 ForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
793 * Make the specified room the current room for this session. No validation
794 * or access control is done here -- the caller should make sure that the
795 * specified room exists and is ok to access.
797 void usergoto(char *where, int display_result, int transiently,
798 int *retmsgs, int *retnew)
801 int new_messages = 0;
802 int old_messages = 0;
803 int total_messages = 0;
807 int newmailcount = 0;
809 char truncated_roomname[ROOMNAMELEN];
810 struct cdbdata *cdbfr;
811 long *msglist = NULL;
813 unsigned int original_v_flags;
816 char setstr[128], lostr[64], histr[64];
820 /* If the supplied room name is NULL, the caller wants us to know that
821 * it has already copied the room record into CC->room, so
822 * we can skip the extra database fetch.
825 safestrncpy(CC->room.QRname, where, sizeof CC->room.QRname);
826 getroom(&CC->room, where);
829 /* Take care of all the formalities. */
831 begin_critical_section(S_USERS);
832 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
833 original_v_flags = vbuf.v_flags;
835 /* Know the room ... but not if it's the page log room, or if the
836 * caller specified that we're only entering this room transiently.
838 if ((strcasecmp(CC->room.QRname, config.c_logpages))
839 && (transiently == 0) ) {
840 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
841 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
844 /* Only rewrite the database record if we changed something */
845 if (vbuf.v_flags != original_v_flags) {
846 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
848 end_critical_section(S_USERS);
850 /* Check for new mail */
851 newmailcount = NewMailCount();
853 /* set info to 1 if the user needs to read the room's info file */
854 if (CC->room.QRinfo > vbuf.v_lastseen) {
858 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
860 msglist = (long *) cdbfr->ptr;
861 cdbfr->ptr = NULL; /* usergoto() now owns this memory */
862 num_msgs = cdbfr->len / sizeof(long);
867 for (a=0; a<num_msgs; ++a) {
868 if (msglist[a] > 0L) ++total_messages;
870 new_messages = num_msgs;
871 num_sets = num_tokens(vbuf.v_seen, ',');
872 for (s=0; s<num_sets; ++s) {
873 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
875 extract_token(lostr, setstr, 0, ':', sizeof lostr);
876 if (num_tokens(setstr, ':') >= 2) {
877 extract_token(histr, setstr, 1, ':', sizeof histr);
878 if (!strcmp(histr, "*")) {
879 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
883 strcpy(histr, lostr);
888 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
889 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
895 new_messages = total_messages - old_messages;
897 if (msglist != NULL) free(msglist);
899 if (CC->room.QRflags & QR_MAILBOX)
904 if ((CC->room.QRroomaide == CC->user.usernum)
905 || (CC->user.axlevel >= 6))
910 safestrncpy(truncated_roomname, CC->room.QRname, sizeof truncated_roomname);
911 if ( (CC->room.QRflags & QR_MAILBOX)
912 && (atol(CC->room.QRname) == CC->user.usernum) ) {
913 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
916 if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
920 if (retmsgs != NULL) *retmsgs = total_messages;
921 if (retnew != NULL) *retnew = new_messages;
922 lprintf(CTDL_DEBUG, "<%s> %d new of %d total messages\n",
924 new_messages, total_messages
927 CC->curr_view = (int)vbuf.v_view;
929 if (display_result) {
930 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|%d|\n",
931 CIT_OK, CtdlCheckExpress(),
936 (int)CC->room.QRflags,
937 (long)CC->room.QRhighest,
938 (long)vbuf.v_lastseen,
942 (int)CC->room.QRfloor,
944 (int)CC->room.QRdefaultview,
946 (int)CC->room.QRflags2
953 * Handle some of the macro named rooms
955 void convert_room_name_macros(char *towhere, size_t maxlen) {
956 if (!strcasecmp(towhere, "_BASEROOM_")) {
957 safestrncpy(towhere, config.c_baseroom, maxlen);
959 else if (!strcasecmp(towhere, "_MAIL_")) {
960 safestrncpy(towhere, MAILROOM, maxlen);
962 else if (!strcasecmp(towhere, "_TRASH_")) {
963 safestrncpy(towhere, USERTRASHROOM, maxlen);
965 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
966 safestrncpy(towhere, config.c_twitroom, maxlen);
968 else if (!strcasecmp(towhere, "_CALENDAR_")) {
969 safestrncpy(towhere, USERCALENDARROOM, maxlen);
971 else if (!strcasecmp(towhere, "_TASKS_")) {
972 safestrncpy(towhere, USERTASKSROOM, maxlen);
974 else if (!strcasecmp(towhere, "_CONTACTS_")) {
975 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
977 else if (!strcasecmp(towhere, "_NOTES_")) {
978 safestrncpy(towhere, USERNOTESROOM, maxlen);
984 * cmd_goto() - goto a new room
986 void cmd_goto(char *gargs)
988 struct ctdlroom QRscratch;
992 char augmented_roomname[ROOMNAMELEN];
993 char towhere[ROOMNAMELEN];
997 if (CtdlAccessCheck(ac_logged_in)) return;
999 extract_token(towhere, gargs, 0, '|', sizeof towhere);
1000 extract_token(password, gargs, 1, '|', sizeof password);
1001 transiently = extract_int(gargs, 2);
1003 getuser(&CC->user, CC->curr_user);
1006 * Handle some of the macro named rooms
1008 convert_room_name_macros(towhere, sizeof towhere);
1010 /* First try a regular match */
1011 c = getroom(&QRscratch, towhere);
1013 /* Then try a mailbox name match */
1015 MailboxName(augmented_roomname, sizeof augmented_roomname,
1016 &CC->user, towhere);
1017 c = getroom(&QRscratch, augmented_roomname);
1019 safestrncpy(towhere, augmented_roomname, sizeof towhere);
1022 /* And if the room was found... */
1025 /* Let internal programs go directly to any room. */
1026 if (CC->internal_pgm) {
1027 memcpy(&CC->room, &QRscratch,
1028 sizeof(struct ctdlroom));
1029 usergoto(NULL, 1, transiently, NULL, NULL);
1033 /* See if there is an existing user/room relationship */
1034 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
1036 /* normal clients have to pass through security */
1037 if (ra & UA_GOTOALLOWED) {
1042 if ((QRscratch.QRflags & QR_MAILBOX) &&
1043 ((ra & UA_GOTOALLOWED))) {
1044 memcpy(&CC->room, &QRscratch,
1045 sizeof(struct ctdlroom));
1046 usergoto(NULL, 1, transiently, NULL, NULL);
1048 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
1049 ((ra & UA_KNOWN) == 0) &&
1050 (strcasecmp(QRscratch.QRpasswd, password)) &&
1051 (CC->user.axlevel < 6)
1053 cprintf("%d wrong or missing passwd\n",
1054 ERROR + PASSWORD_REQUIRED);
1056 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
1057 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
1058 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
1059 ((ra & UA_KNOWN) == 0) &&
1060 (CC->user.axlevel < 6)
1062 lprintf(CTDL_DEBUG, "Failed to acquire private room\n");
1064 memcpy(&CC->room, &QRscratch,
1065 sizeof(struct ctdlroom));
1066 usergoto(NULL, 1, transiently, NULL, NULL);
1072 cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
1078 struct ctdluser temp;
1079 struct cdbdata *cdbus;
1082 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
1083 cdb_rewind(CDB_USERS);
1084 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1085 memset(&temp, 0, sizeof temp);
1086 memcpy(&temp, cdbus->ptr, sizeof temp);
1089 CtdlRoomAccess(&CC->room, &temp, &ra, NULL);
1090 if ((CC->room.QRflags & QR_INUSE)
1093 cprintf("%s\n", temp.fullname);
1100 * RDIR command for room directory
1108 struct stat statbuf;
1109 char tempfilename[PATH_MAX];
1111 if (CtdlAccessCheck(ac_logged_in)) return;
1112 CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
1114 getroom(&CC->room, CC->room.QRname);
1115 getuser(&CC->user, CC->curr_user);
1117 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
1118 cprintf("%d not here.\n", ERROR + NOT_HERE);
1121 if (((CC->room.QRflags & QR_VISDIR) == 0)
1122 && (CC->user.axlevel < 6)
1123 && (CC->user.usernum != CC->room.QRroomaide)) {
1124 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1127 cprintf("%d %s|%s/%s\n",
1131 CC->room.QRdirname);
1133 snprintf(buf, sizeof buf,
1134 "ls %s/%s >%s 2>/dev/null",
1140 snprintf(buf, sizeof buf,
1143 CC->room.QRdirname);
1144 fd = fopen(buf, "r");
1146 fd = fopen("/dev/null", "r");
1148 ls = fopen(tempfilename, "r");
1149 while (fgets(flnm, sizeof flnm, ls) != NULL) {
1150 flnm[strlen(flnm) - 1] = 0;
1151 if (strcasecmp(flnm, "filedir")) {
1152 snprintf(buf, sizeof buf,
1157 stat(buf, &statbuf);
1158 safestrncpy(comment, "", sizeof comment);
1160 while ((fgets(buf, sizeof buf, fd) != NULL)
1161 && (strlen(comment) == 0)) {
1162 buf[strlen(buf) - 1] = 0;
1163 if ((!strncasecmp(buf, flnm, strlen(flnm)))
1164 && (buf[strlen(flnm)] == ' '))
1165 safestrncpy(comment,
1166 &buf[strlen(flnm) + 1],
1169 cprintf("%s|%ld|%s\n", flnm, (long)statbuf.st_size, comment);
1174 unlink(tempfilename);
1180 * get room parameters (aide or room aide command)
1184 if (CtdlAccessCheck(ac_room_aide)) return;
1186 getroom(&CC->room, CC->room.QRname);
1187 cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1191 ((CC->room.QRflags & QR_MAILBOX) ?
1192 &CC->room.QRname[11] : CC->room.QRname),
1194 ((CC->room.QRflags & QR_PASSWORDED) ?
1195 CC->room.QRpasswd : ""),
1197 ((CC->room.QRflags & QR_DIRECTORY) ?
1198 CC->room.QRdirname : ""),
1201 (int) CC->room.QRfloor,
1202 (int) CC->room.QRorder,
1204 CC->room.QRdefaultview,
1211 * Back end function to rename a room.
1212 * You can also specify which floor to move the room to, or specify -1 to
1213 * keep the room on the same floor it was on.
1215 * If you are renaming a mailbox room, you must supply the namespace prefix
1216 * in *at least* the old name!
1218 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1220 struct ctdlroom qrbuf;
1221 struct ctdlroom qrtmp;
1226 char actual_old_name[ROOMNAMELEN];
1228 lprintf(CTDL_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1229 old_name, new_name, new_floor);
1231 if (new_floor >= 0) {
1232 fl = cgetfloor(new_floor);
1233 if ((fl->f_flags & F_INUSE) == 0) {
1234 return(crr_invalid_floor);
1238 begin_critical_section(S_ROOMS);
1240 if ( (getroom(&qrtmp, new_name) == 0)
1241 && (strcasecmp(new_name, old_name)) ) {
1242 ret = crr_already_exists;
1245 else if (getroom(&qrbuf, old_name) != 0) {
1246 ret = crr_room_not_found;
1249 else if ( (CC->user.axlevel < 6) && (!CC->internal_pgm)
1250 && (CC->user.usernum != qrbuf.QRroomaide)
1251 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) ) {
1252 ret = crr_access_denied;
1255 else if (is_noneditable(&qrbuf)) {
1256 ret = crr_noneditable;
1261 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1262 if (qrbuf.QRflags & QR_MAILBOX) {
1263 owner = atol(qrbuf.QRname);
1265 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1266 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1267 "%010ld.%s", owner, new_name);
1270 safestrncpy(qrbuf.QRname, new_name,
1271 sizeof(qrbuf.QRname));
1274 /* Reject change of floor for baseroom/aideroom */
1275 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1276 !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1280 /* Take care of floor stuff */
1281 old_floor = qrbuf.QRfloor;
1282 if (new_floor < 0) {
1283 new_floor = old_floor;
1285 qrbuf.QRfloor = new_floor;
1288 begin_critical_section(S_CONFIG);
1290 /* If baseroom/aideroom name changes, update config */
1291 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1292 safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1295 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1296 safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1300 end_critical_section(S_CONFIG);
1302 /* If the room name changed, then there are now two room
1303 * records, so we have to delete the old one.
1305 if (strcasecmp(new_name, old_name)) {
1306 b_deleteroom(actual_old_name);
1312 end_critical_section(S_ROOMS);
1314 /* Adjust the floor reference counts if necessary */
1315 if (new_floor != old_floor) {
1316 lgetfloor(&flbuf, old_floor);
1317 --flbuf.f_ref_count;
1318 lputfloor(&flbuf, old_floor);
1319 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1320 lgetfloor(&flbuf, new_floor);
1321 ++flbuf.f_ref_count;
1322 lputfloor(&flbuf, new_floor);
1323 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1326 /* ...and everybody say "YATTA!" */
1332 * set room parameters (aide or room aide command)
1334 void cmd_setr(char *args)
1340 char new_name[ROOMNAMELEN];
1342 if (CtdlAccessCheck(ac_logged_in)) return;
1344 if (num_parms(args) >= 6) {
1345 new_floor = extract_int(args, 5);
1347 new_floor = (-1); /* don't change the floor */
1350 /* When is a new name more than just a new name? When the old name
1351 * has a namespace prefix.
1353 if (CC->room.QRflags & QR_MAILBOX) {
1354 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
1356 safestrncpy(new_name, "", sizeof new_name);
1358 extract_token(&new_name[strlen(new_name)], args, 0, '|', (sizeof new_name - strlen(new_name)));
1360 r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
1362 if (r == crr_room_not_found) {
1363 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
1364 } else if (r == crr_already_exists) {
1365 cprintf("%d '%s' already exists.\n",
1366 ERROR + ALREADY_EXISTS, new_name);
1367 } else if (r == crr_noneditable) {
1368 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
1369 } else if (r == crr_invalid_floor) {
1370 cprintf("%d Target floor does not exist.\n",
1371 ERROR + INVALID_FLOOR_OPERATION);
1372 } else if (r == crr_access_denied) {
1373 cprintf("%d You do not have permission to edit '%s'\n",
1374 ERROR + HIGHER_ACCESS_REQUIRED,
1376 } else if (r != crr_ok) {
1377 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1378 ERROR + INTERNAL_ERROR, r);
1385 getroom(&CC->room, new_name);
1387 /* Now we have to do a bunch of other stuff */
1389 if (num_parms(args) >= 7) {
1390 new_order = extract_int(args, 6);
1393 if (new_order > 127)
1397 lgetroom(&CC->room, CC->room.QRname);
1399 /* Directory room */
1400 extract_token(buf, args, 2, '|', sizeof buf);
1402 safestrncpy(CC->room.QRdirname, buf,
1403 sizeof CC->room.QRdirname);
1406 if (num_parms(args) >= 8) {
1407 CC->room.QRdefaultview = extract_int(args, 7);
1410 /* Second set of flags */
1411 if (num_parms(args) >= 9) {
1412 CC->room.QRflags2 = extract_int(args, 8);
1416 CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
1417 /* Clean up a client boo-boo: if the client set the room to
1418 * guess-name or passworded, ensure that the private flag is
1421 if ((CC->room.QRflags & QR_GUESSNAME)
1422 || (CC->room.QRflags & QR_PASSWORDED))
1423 CC->room.QRflags |= QR_PRIVATE;
1425 /* Some changes can't apply to BASEROOM */
1426 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1428 CC->room.QRorder = 0;
1429 CC->room.QRpasswd[0] = '\0';
1430 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1431 QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1432 CC->room.QRflags |= QR_PERMANENT;
1434 /* March order (doesn't apply to AIDEROOM) */
1435 if (num_parms(args) >= 7)
1436 CC->room.QRorder = (char) new_order;
1438 extract_token(buf, args, 1, '|', sizeof buf);
1440 safestrncpy(CC->room.QRpasswd, buf,
1441 sizeof CC->room.QRpasswd);
1442 /* Kick everyone out if the client requested it
1443 * (by changing the room's generation number)
1445 if (extract_int(args, 4)) {
1446 time(&CC->room.QRgen);
1449 /* Some changes can't apply to AIDEROOM */
1450 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1452 CC->room.QRorder = 0;
1453 CC->room.QRflags &= ~QR_MAILBOX;
1454 CC->room.QRflags |= QR_PERMANENT;
1457 /* Write the room record back to disk */
1458 lputroom(&CC->room);
1460 /* Create a room directory if necessary */
1461 if (CC->room.QRflags & QR_DIRECTORY) {
1462 snprintf(buf, sizeof buf,"%s/%s",
1464 CC->room.QRdirname);
1467 snprintf(buf, sizeof buf, "The room \"%s\" has been edited by %s.\n",
1468 CC->room.QRname, CC->curr_user);
1469 aide_message(buf, "Room modification Message");
1470 cprintf("%d Ok\n", CIT_OK);
1476 * get the name of the room aide for this room
1480 struct ctdluser usbuf;
1482 if (CtdlAccessCheck(ac_logged_in)) return;
1484 if (getuserbynumber(&usbuf, CC->room.QRroomaide) == 0) {
1485 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1487 cprintf("%d \n", CIT_OK);
1493 * set the room aide for this room
1495 void cmd_seta(char *new_ra)
1497 struct ctdluser usbuf;
1502 if (CtdlAccessCheck(ac_room_aide)) return;
1504 if (getuser(&usbuf, new_ra) != 0) {
1507 newu = usbuf.usernum;
1510 lgetroom(&CC->room, CC->room.QRname);
1512 if (CC->room.QRroomaide != newu) {
1515 CC->room.QRroomaide = newu;
1516 lputroom(&CC->room);
1519 * We have to post the change notice _after_ writing changes to
1520 * the room table, otherwise it would deadlock!
1522 if (post_notice == 1) {
1523 if (strlen(usbuf.fullname) > 0)
1524 snprintf(buf, sizeof buf,
1525 "%s is now the room aide for \"%s\".\n",
1526 usbuf.fullname, CC->room.QRname);
1528 snprintf(buf, sizeof buf,
1529 "There is now no room aide for \"%s\".\n",
1531 aide_message(buf, "Aide Room Modification");
1533 cprintf("%d Ok\n", CIT_OK);
1537 * retrieve info file for this room
1545 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_info_dir);
1546 info_fp = fopen(filename, "r");
1548 if (info_fp == NULL) {
1549 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
1552 cprintf("%d Info:\n", LISTING_FOLLOWS);
1553 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1554 if (strlen(buf) > 0)
1555 buf[strlen(buf) - 1] = 0;
1556 cprintf("%s\n", buf);
1563 * Asynchronously schedule a room for deletion. The room will appear
1564 * deleted to the user(s), but it won't actually get purged from the
1565 * database until THE DREADED AUTO-PURGER makes its next run.
1567 void schedule_room_for_deletion(struct ctdlroom *qrbuf)
1569 char old_name[ROOMNAMELEN];
1572 lprintf(CTDL_NOTICE, "Scheduling room <%s> for deletion\n",
1575 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1577 getroom(qrbuf, qrbuf->QRname);
1579 /* Turn the room into a private mailbox owned by a user who doesn't
1580 * exist. This will immediately make the room invisible to everyone,
1581 * and qualify the room for purging.
1583 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1588 qrbuf->QRflags |= QR_MAILBOX;
1589 time(&qrbuf->QRgen); /* Use a timestamp as the new generation number */
1593 b_deleteroom(old_name);
1599 * Back end processing to delete a room and everything associated with it
1600 * (This one is synchronous and should only get called by THE DREADED
1601 * AUTO-PURGER in serv_expire.c. All user-facing code should call
1602 * the asynchronous schedule_room_for_deletion() instead.)
1604 void delete_room(struct ctdlroom *qrbuf)
1608 /* TODO: filename magic? does this realy work? */
1610 lprintf(CTDL_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1612 /* Delete the info file */
1613 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_info_dir);
1616 /* Delete the image file */
1617 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_image_dir);
1620 /* Delete the room's network config file */
1621 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1624 /* Delete the messages in the room
1625 * (Careful: this opens an S_ROOMS critical section!)
1627 CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
1629 /* Flag the room record as not in use */
1630 lgetroom(qrbuf, qrbuf->QRname);
1634 /* then decrement the reference count for the floor */
1635 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1636 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1637 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1639 /* Delete the room record from the database! */
1640 b_deleteroom(qrbuf->QRname);
1646 * Check access control for deleting a room
1648 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1650 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1654 if (is_noneditable(qr)) {
1659 * For mailboxes, check stuff
1661 if (qr->QRflags & QR_MAILBOX) {
1663 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1665 if (atol(qr->QRname) != CC->user.usernum) {
1666 return(0); /* not my room */
1669 /* Can't delete your Mail> room */
1670 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1672 /* Otherwise it's ok */
1677 * For normal rooms, just check for aide or room aide status.
1679 return(is_room_aide());
1683 * aide command: kill the current room
1685 void cmd_kill(char *argbuf)
1687 char deleted_room_name[ROOMNAMELEN];
1691 kill_ok = extract_int(argbuf, 0);
1693 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
1694 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1698 if (CC->room.QRflags & QR_MAILBOX) {
1699 safestrncpy(deleted_room_name, &CC->room.QRname[11], sizeof deleted_room_name);
1702 safestrncpy(deleted_room_name, CC->room.QRname, sizeof deleted_room_name);
1705 /* Do the dirty work */
1706 schedule_room_for_deletion(&CC->room);
1708 /* Return to the Lobby */
1709 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1711 /* tell the world what we did */
1712 snprintf(msg, sizeof msg, "The room \"%s\" has been deleted by %s.\n",
1713 deleted_room_name, CC->curr_user);
1714 aide_message(msg, "Room Purger Message");
1715 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1717 cprintf("%d ok to delete.\n", CIT_OK);
1723 * Internal code to create a new room (returns room flags)
1725 * Room types: 0=public, 1=guessname, 2=passworded, 3=inv-only,
1726 * 4=mailbox, 5=mailbox, but caller supplies namespace
1728 unsigned create_room(char *new_room_name,
1730 char *new_room_pass,
1737 struct ctdlroom qrbuf;
1741 lprintf(CTDL_DEBUG, "create_room(name=%s, type=%d, view=%d)\n",
1742 new_room_name, new_room_type, new_room_view);
1744 if (getroom(&qrbuf, new_room_name) == 0) {
1745 lprintf(CTDL_DEBUG, "%s already exists.\n", new_room_name);
1749 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1750 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1751 qrbuf.QRflags = QR_INUSE;
1752 if (new_room_type > 0)
1753 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1754 if (new_room_type == 1)
1755 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1756 if (new_room_type == 2)
1757 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1758 if ( (new_room_type == 4) || (new_room_type == 5) ) {
1759 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1760 /* qrbuf.QRflags2 |= QR2_SUBJECTREQ; */
1763 /* If the user is requesting a personal room, set up the room
1764 * name accordingly (prepend the user number)
1766 if (new_room_type == 4) {
1767 MailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1770 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1773 /* If the room is private, and the system administrator has elected
1774 * to automatically grant room aide privileges, do so now; otherwise,
1775 * set the room aide to undefined.
1777 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1778 qrbuf.QRroomaide = CC->user.usernum;
1780 qrbuf.QRroomaide = (-1L);
1784 * If the caller is only interested in testing whether this will work,
1785 * return now without creating the room.
1787 if (!really_create) return (qrbuf.QRflags);
1789 qrbuf.QRnumber = get_new_room_number();
1790 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1791 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1792 qrbuf.QRfloor = new_room_floor;
1793 qrbuf.QRdefaultview = new_room_view;
1795 /* save what we just did... */
1798 /* bump the reference count on whatever floor the room is on */
1799 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1800 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1801 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1803 /* Grant the creator access to the room unless the avoid_access
1804 * parameter was specified.
1806 if ( (CC->logged_in) && (avoid_access == 0) ) {
1807 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1808 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1809 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1810 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1813 /* resume our happy day */
1814 return (qrbuf.QRflags);
1821 void cmd_cre8(char *args)
1824 char new_room_name[ROOMNAMELEN];
1826 char new_room_pass[32];
1829 char *notification_message = NULL;
1832 int avoid_access = 0;
1834 cre8_ok = extract_int(args, 0);
1835 extract_token(new_room_name, args, 1, '|', sizeof new_room_name);
1836 new_room_name[ROOMNAMELEN - 1] = 0;
1837 new_room_type = extract_int(args, 2);
1838 extract_token(new_room_pass, args, 3, '|', sizeof new_room_pass);
1839 avoid_access = extract_int(args, 5);
1840 new_room_view = extract_int(args, 6);
1841 new_room_pass[9] = 0;
1844 if ((strlen(new_room_name) == 0) && (cre8_ok == 1)) {
1845 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
1849 if (!strcasecmp(new_room_name, MAILROOM)) {
1850 cprintf("%d '%s' already exists.\n",
1851 ERROR + ALREADY_EXISTS, new_room_name);
1855 if (num_parms(args) >= 5) {
1856 fl = cgetfloor(extract_int(args, 4));
1858 cprintf("%d Invalid floor number.\n",
1859 ERROR + INVALID_FLOOR_OPERATION);
1862 else if ((fl->f_flags & F_INUSE) == 0) {
1863 cprintf("%d Invalid floor number.\n",
1864 ERROR + INVALID_FLOOR_OPERATION);
1867 new_room_floor = extract_int(args, 4);
1871 if (CtdlAccessCheck(ac_logged_in)) return;
1873 if (CC->user.axlevel < config.c_createax || CC->internal_pgm) {
1874 cprintf("%d You need higher access to create rooms.\n",
1875 ERROR + HIGHER_ACCESS_REQUIRED);
1879 if ((strlen(new_room_name) == 0) && (cre8_ok == 0)) {
1880 cprintf("%d Ok to create rooms.\n", CIT_OK);
1884 if ((new_room_type < 0) || (new_room_type > 5)) {
1885 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
1889 if (new_room_type == 5) {
1890 if (CC->user.axlevel < 6) {
1891 cprintf("%d Higher access required\n",
1892 ERROR + HIGHER_ACCESS_REQUIRED);
1897 /* Check to make sure the requested room name doesn't already exist */
1898 newflags = create_room(new_room_name,
1899 new_room_type, new_room_pass, new_room_floor,
1900 0, avoid_access, new_room_view);
1901 if (newflags == 0) {
1902 cprintf("%d '%s' already exists.\n",
1903 ERROR + ALREADY_EXISTS, new_room_name);
1908 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
1912 /* If we reach this point, the room needs to be created. */
1914 newflags = create_room(new_room_name,
1915 new_room_type, new_room_pass, new_room_floor, 1, 0,
1918 /* post a message in Aide> describing the new room */
1919 notification_message = malloc(1024);
1920 snprintf(notification_message, 1024,
1921 "A new room called \"%s\" has been created by %s%s%s%s%s%s\n",
1924 ((newflags & QR_MAILBOX) ? " [personal]" : ""),
1925 ((newflags & QR_PRIVATE) ? " [private]" : ""),
1926 ((newflags & QR_GUESSNAME) ? " [hidden]" : ""),
1927 ((newflags & QR_PASSWORDED) ? " Password: " : ""),
1928 ((newflags & QR_PASSWORDED) ? new_room_pass : "")
1930 aide_message(notification_message, "Room Creation Message");
1931 free(notification_message);
1933 cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
1938 void cmd_einf(char *ok)
1939 { /* enter info file for current room */
1941 char infofilename[SIZ];
1946 if (CtdlAccessCheck(ac_room_aide)) return;
1948 if (atoi(ok) == 0) {
1949 cprintf("%d Ok.\n", CIT_OK);
1952 assoc_file_name(infofilename, sizeof infofilename, &CC->room, ctdl_info_dir);
1953 lprintf(CTDL_DEBUG, "opening\n");
1954 fp = fopen(infofilename, "w");
1955 lprintf(CTDL_DEBUG, "checking\n");
1957 cprintf("%d Cannot open %s: %s\n",
1958 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1961 cprintf("%d Send info...\n", SEND_LISTING);
1964 client_getln(buf, sizeof buf);
1965 if (strcmp(buf, "000"))
1966 fprintf(fp, "%s\n", buf);
1967 } while (strcmp(buf, "000"));
1970 /* now update the room index so people will see our new info */
1971 lgetroom(&CC->room, CC->room.QRname); /* lock so no one steps on us */
1972 CC->room.QRinfo = CC->room.QRhighest + 1L;
1973 lputroom(&CC->room);
1978 * cmd_lflr() - List all known floors
1985 if (CtdlAccessCheck(ac_logged_in)) return;
1987 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1989 for (a = 0; a < MAXFLOORS; ++a) {
1990 getfloor(&flbuf, a);
1991 if (flbuf.f_flags & F_INUSE) {
1992 cprintf("%d|%s|%d\n",
2004 * create a new floor
2006 void cmd_cflr(char *argbuf)
2008 char new_floor_name[256];
2011 int free_slot = (-1);
2014 extract_token(new_floor_name, argbuf, 0, '|', sizeof new_floor_name);
2015 cflr_ok = extract_int(argbuf, 1);
2017 if (CtdlAccessCheck(ac_aide)) return;
2019 if (strlen(new_floor_name) == 0) {
2020 cprintf("%d Blank floor name not allowed.\n",
2021 ERROR + ILLEGAL_VALUE);
2025 for (a = 0; a < MAXFLOORS; ++a) {
2026 getfloor(&flbuf, a);
2028 /* note any free slots while we're scanning... */
2029 if (((flbuf.f_flags & F_INUSE) == 0)
2033 /* check to see if it already exists */
2034 if ((!strcasecmp(flbuf.f_name, new_floor_name))
2035 && (flbuf.f_flags & F_INUSE)) {
2036 cprintf("%d Floor '%s' already exists.\n",
2037 ERROR + ALREADY_EXISTS,
2043 if (free_slot < 0) {
2044 cprintf("%d There is no space available for a new floor.\n",
2045 ERROR + INVALID_FLOOR_OPERATION);
2049 cprintf("%d ok to create...\n", CIT_OK);
2052 lgetfloor(&flbuf, free_slot);
2053 flbuf.f_flags = F_INUSE;
2054 flbuf.f_ref_count = 0;
2055 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
2056 lputfloor(&flbuf, free_slot);
2057 cprintf("%d %d\n", CIT_OK, free_slot);
2065 void cmd_kflr(char *argbuf)
2068 int floor_to_delete;
2072 floor_to_delete = extract_int(argbuf, 0);
2073 kflr_ok = extract_int(argbuf, 1);
2075 if (CtdlAccessCheck(ac_aide)) return;
2077 lgetfloor(&flbuf, floor_to_delete);
2080 if ((flbuf.f_flags & F_INUSE) == 0) {
2081 cprintf("%d Floor %d not in use.\n",
2082 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
2085 if (flbuf.f_ref_count != 0) {
2086 cprintf("%d Cannot delete; floor contains %d rooms.\n",
2087 ERROR + INVALID_FLOOR_OPERATION,
2092 cprintf("%d Ok\n", CIT_OK);
2094 cprintf("%d Ok to delete...\n", CIT_OK);
2101 if ((delete_ok == 1) && (kflr_ok == 1))
2103 lputfloor(&flbuf, floor_to_delete);
2109 void cmd_eflr(char *argbuf)
2115 np = num_parms(argbuf);
2117 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
2121 if (CtdlAccessCheck(ac_aide)) return;
2123 floor_num = extract_int(argbuf, 0);
2124 lgetfloor(&flbuf, floor_num);
2125 if ((flbuf.f_flags & F_INUSE) == 0) {
2126 lputfloor(&flbuf, floor_num);
2127 cprintf("%d Floor %d is not in use.\n",
2128 ERROR + INVALID_FLOOR_OPERATION, floor_num);
2132 extract_token(flbuf.f_name, argbuf, 1, '|', sizeof flbuf.f_name);
2133 lputfloor(&flbuf, floor_num);
2135 cprintf("%d Ok\n", CIT_OK);