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>
31 #include "serv_extensions.h"
35 #include "sysdep_decls.h"
39 #include "citserver.h"
42 #include "citadel_dirs.h"
44 struct floor *floorcache[MAXFLOORS];
47 * Generic routine for determining user access to rooms
49 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf,
50 int *result, int *view)
55 /* for internal programs, always do everything */
56 if (((CC->internal_pgm)) && (roombuf->QRflags & QR_INUSE)) {
57 retval = (UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED);
62 /* Locate any applicable user/room relationships */
63 CtdlGetRelationship(&vbuf, userbuf, roombuf);
65 /* Force the properties of the Aide room */
66 if (!strcasecmp(roombuf->QRname, config.c_aideroom)) {
67 if (userbuf->axlevel >= 6) {
68 retval = UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
75 /* If this is a public room, it's accessible... */
76 if ( ((roombuf->QRflags & QR_PRIVATE) == 0)
77 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
78 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
81 /* If this is a preferred users only room, check access level */
82 if (roombuf->QRflags & QR_PREFONLY) {
83 if (userbuf->axlevel < 5) {
84 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
88 /* For private rooms, check the generation number matchups */
89 if ( (roombuf->QRflags & QR_PRIVATE)
90 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
92 /* An explicit match means the user belongs in this room */
93 if (vbuf.v_flags & V_ACCESS) {
94 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
96 /* Otherwise, check if this is a guess-name or passworded
97 * room. If it is, a goto may at least be attempted
99 else if ((roombuf->QRflags & QR_PRIVATE)
100 || (roombuf->QRflags & QR_PASSWORDED)) {
101 retval = retval & ~UA_KNOWN;
102 retval = retval | UA_GOTOALLOWED;
106 /* For mailbox rooms, also check the namespace */
107 /* Also, mailbox owners can delete their messages */
108 if (roombuf->QRflags & QR_MAILBOX) {
109 if (userbuf->usernum == atol(roombuf->QRname)) {
110 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
112 /* An explicit match means the user belongs in this room */
113 if (vbuf.v_flags & V_ACCESS) {
114 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
118 /* For non-mailbox rooms... */
121 /* User is allowed to post in the room unless:
122 * - User is not validated
123 * - User has no net privileges and it is a shared network room
124 * - It is a read-only room
126 int post_allowed = 1;
127 if (CC->user.axlevel < 2) post_allowed = 0;
128 if ((CC->user.axlevel < 4) && (CC->room.QRflags & QR_NETWORK)) post_allowed = 0;
129 if (roombuf->QRflags & QR_READONLY) post_allowed = 0;
131 retval = retval | UA_POSTALLOWED;
136 /* Check to see if the user has forgotten this room */
137 if (vbuf.v_flags & V_FORGET) {
138 retval = retval & ~UA_KNOWN;
139 if ( ( ((roombuf->QRflags & QR_PRIVATE) == 0)
140 && ((roombuf->QRflags & QR_MAILBOX) == 0) )
141 || ( (roombuf->QRflags & QR_MAILBOX)
142 && (atol(roombuf->QRname) == CC->user.usernum))) {
143 retval = retval | UA_ZAPPED;
146 /* If user is explicitly locked out of this room, deny everything */
147 if (vbuf.v_flags & V_LOCKOUT) {
148 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED & ~UA_POSTALLOWED;
151 /* Aides get access to all private rooms */
152 if ( (userbuf->axlevel >= 6)
153 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
154 if (vbuf.v_flags & V_FORGET) {
155 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED;
158 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED;
162 /* Aides can gain access to mailboxes as well, but they don't show
165 if ( (userbuf->axlevel >= 6)
166 && (roombuf->QRflags & QR_MAILBOX) ) {
167 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED;
170 /* Aides and Room Aides have admin privileges */
171 if ( (userbuf->axlevel >= 6)
172 || (userbuf->usernum == roombuf->QRroomaide)
174 retval = retval | UA_ADMINALLOWED | UA_DELETEALLOWED;
177 NEWMSG: /* By the way, we also check for the presence of new messages */
178 if (is_msg_in_sequence_set(vbuf.v_seen, roombuf->QRhighest) == 0) {
179 retval = retval | UA_HASNEWMSGS;
182 /* System rooms never show up in the list. */
183 if (roombuf->QRflags2 & QR2_SYSTEM) {
184 retval = retval & ~UA_KNOWN;
188 /* Now give the caller the information it wants. */
189 if (result != NULL) *result = retval;
190 if (view != NULL) *view = vbuf.v_view;
194 * Self-checking stuff for a room record read into memory
196 void room_sanity_check(struct ctdlroom *qrbuf)
198 /* Mailbox rooms are always on the lowest floor */
199 if (qrbuf->QRflags & QR_MAILBOX) {
202 /* Listing order of 0 is illegal except for base rooms */
203 if (qrbuf->QRorder == 0)
204 if (!(qrbuf->QRflags & QR_MAILBOX) &&
205 strncasecmp(qrbuf->QRname, config.c_baseroom, ROOMNAMELEN)
207 strncasecmp(qrbuf->QRname, config.c_aideroom, ROOMNAMELEN))
213 * getroom() - retrieve room data from disk
215 int getroom(struct ctdlroom *qrbuf, char *room_name)
217 struct cdbdata *cdbqr;
218 char lowercase_name[ROOMNAMELEN];
219 char personal_lowercase_name[ROOMNAMELEN];
222 for (a = 0; room_name[a] && a < sizeof lowercase_name - 1; ++a) {
223 lowercase_name[a] = tolower(room_name[a]);
225 lowercase_name[a] = 0;
227 memset(qrbuf, 0, sizeof(struct ctdlroom));
229 /* First, try the public namespace */
230 cdbqr = cdb_fetch(CDB_ROOMS,
231 lowercase_name, strlen(lowercase_name));
233 /* If that didn't work, try the user's personal namespace */
235 snprintf(personal_lowercase_name,
236 sizeof personal_lowercase_name, "%010ld.%s",
237 CC->user.usernum, lowercase_name);
238 cdbqr = cdb_fetch(CDB_ROOMS,
239 personal_lowercase_name,
240 strlen(personal_lowercase_name));
243 memcpy(qrbuf, cdbqr->ptr,
244 ((cdbqr->len > sizeof(struct ctdlroom)) ?
245 sizeof(struct ctdlroom) : cdbqr->len));
248 room_sanity_check(qrbuf);
257 * lgetroom() - same as getroom() but locks the record (if supported)
259 int lgetroom(struct ctdlroom *qrbuf, char *room_name)
262 retval = getroom(qrbuf, room_name);
263 if (retval == 0) begin_critical_section(S_ROOMS);
269 * b_putroom() - back end to putroom() and b_deleteroom()
270 * (if the supplied buffer is NULL, delete the room record)
272 void b_putroom(struct ctdlroom *qrbuf, char *room_name)
274 char lowercase_name[ROOMNAMELEN];
277 for (a = 0; a <= strlen(room_name); ++a) {
278 lowercase_name[a] = tolower(room_name[a]);
282 cdb_delete(CDB_ROOMS,
283 lowercase_name, strlen(lowercase_name));
285 time(&qrbuf->QRmtime);
287 lowercase_name, strlen(lowercase_name),
288 qrbuf, sizeof(struct ctdlroom));
294 * putroom() - store room data to disk
296 void putroom(struct ctdlroom *qrbuf) {
297 b_putroom(qrbuf, qrbuf->QRname);
302 * b_deleteroom() - delete a room record from disk
304 void b_deleteroom(char *room_name) {
305 b_putroom(NULL, room_name);
311 * lputroom() - same as putroom() but unlocks the record (if supported)
313 void lputroom(struct ctdlroom *qrbuf)
317 end_critical_section(S_ROOMS);
321 /****************************************************************************/
324 * getfloor() - retrieve floor data from disk
326 void getfloor(struct floor *flbuf, int floor_num)
328 struct cdbdata *cdbfl;
330 memset(flbuf, 0, sizeof(struct floor));
331 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
333 memcpy(flbuf, cdbfl->ptr,
334 ((cdbfl->len > sizeof(struct floor)) ?
335 sizeof(struct floor) : cdbfl->len));
338 if (floor_num == 0) {
339 safestrncpy(flbuf->f_name, "Main Floor",
340 sizeof flbuf->f_name);
341 flbuf->f_flags = F_INUSE;
342 flbuf->f_ref_count = 3;
349 * lgetfloor() - same as getfloor() but locks the record (if supported)
351 void lgetfloor(struct floor *flbuf, int floor_num)
354 begin_critical_section(S_FLOORTAB);
355 getfloor(flbuf, floor_num);
360 * cgetfloor() - Get floor record from *cache* (loads from disk if needed)
362 * This is strictly a performance hack.
364 struct floor *cgetfloor(int floor_num) {
365 static int initialized = 0;
368 struct floor *fl = NULL;
370 begin_critical_section(S_FLOORCACHE);
371 if (initialized == 0) {
372 for (i=0; i<MAXFLOORS; ++i) {
373 floorcache[floor_num] = NULL;
377 if (floorcache[floor_num] == NULL) {
380 end_critical_section(S_FLOORCACHE);
383 fl = malloc(sizeof(struct floor));
384 getfloor(fl, floor_num);
385 begin_critical_section(S_FLOORCACHE);
386 if (floorcache[floor_num] != NULL) {
387 free(floorcache[floor_num]);
389 floorcache[floor_num] = fl;
390 end_critical_section(S_FLOORCACHE);
393 return(floorcache[floor_num]);
399 * putfloor() - store floor data on disk
401 void putfloor(struct floor *flbuf, int floor_num)
403 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
404 begin_critical_section(S_FLOORCACHE);
405 if (floorcache[floor_num] != NULL) {
406 free(floorcache[floor_num]);
407 floorcache[floor_num] = malloc(sizeof(struct floor));
408 memcpy(floorcache[floor_num], flbuf, sizeof(struct floor));
410 end_critical_section(S_FLOORCACHE);
412 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
413 flbuf, sizeof(struct floor));
418 * lputfloor() - same as putfloor() but unlocks the record (if supported)
420 void lputfloor(struct floor *flbuf, int floor_num)
423 putfloor(flbuf, floor_num);
424 end_critical_section(S_FLOORTAB);
430 * Traverse the room file...
432 void ForEachRoom(void (*CallBack) (struct ctdlroom *EachRoom, void *out_data),
435 struct ctdlroom qrbuf;
436 struct cdbdata *cdbqr;
438 cdb_rewind(CDB_ROOMS);
440 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
441 memset(&qrbuf, 0, sizeof(struct ctdlroom));
442 memcpy(&qrbuf, cdbqr->ptr,
443 ((cdbqr->len > sizeof(struct ctdlroom)) ?
444 sizeof(struct ctdlroom) : cdbqr->len));
446 room_sanity_check(&qrbuf);
447 if (qrbuf.QRflags & QR_INUSE)
448 (*CallBack)(&qrbuf, in_data);
454 * delete_msglist() - delete room message pointers
456 void delete_msglist(struct ctdlroom *whichroom)
458 struct cdbdata *cdbml;
460 /* Make sure the msglist we're deleting actually exists, otherwise
461 * gdbm will complain when we try to delete an invalid record
463 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
467 /* Go ahead and delete it */
468 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
476 * sort message pointers
477 * (returns new msg count)
479 int sort_msglist(long listptrs[], int oldcount)
490 for (a = numitems - 2; a >= 0; --a) {
491 for (b = 0; b <= a; ++b) {
492 if (listptrs[b] > (listptrs[b + 1])) {
494 hold2 = listptrs[b + 1];
496 listptrs[b + 1] = hold1;
501 /* and yank any nulls */
502 while ((numitems > 0) && (listptrs[0] == 0L)) {
503 memcpy(&listptrs[0], &listptrs[1],
504 (sizeof(long) * (numitems - 1)));
513 * Determine whether a given room is non-editable.
515 int is_noneditable(struct ctdlroom *qrbuf)
518 /* Mail> rooms are non-editable */
519 if ( (qrbuf->QRflags & QR_MAILBOX)
520 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
523 /* Everything else is editable */
530 * Back-back-end for all room listing commands
532 void list_roomname(struct ctdlroom *qrbuf, int ra, int current_view, int default_view)
534 char truncated_roomname[ROOMNAMELEN];
536 /* For my own mailbox rooms, chop off the owner prefix */
537 if ( (qrbuf->QRflags & QR_MAILBOX)
538 && (atol(qrbuf->QRname) == CC->user.usernum) ) {
539 safestrncpy(truncated_roomname, qrbuf->QRname, sizeof truncated_roomname);
540 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
541 cprintf("%s", truncated_roomname);
543 /* For all other rooms, just display the name in its entirety */
545 cprintf("%s", qrbuf->QRname);
548 /* ...and now the other parameters */
549 cprintf("|%u|%d|%d|%d|%d|%d|%d|%ld|\n",
551 (int) qrbuf->QRfloor,
552 (int) qrbuf->QRorder,
553 (int) qrbuf->QRflags2,
563 * cmd_lrms() - List all accessible rooms, known or forgotten
565 void cmd_lrms_backend(struct ctdlroom *qrbuf, void *data)
567 int FloorBeingSearched = (-1);
571 FloorBeingSearched = *(int *)data;
572 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
574 if ((( ra & (UA_KNOWN | UA_ZAPPED)))
575 && ((qrbuf->QRfloor == (FloorBeingSearched))
576 || ((FloorBeingSearched) < 0)))
577 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
580 void cmd_lrms(char *argbuf)
582 int FloorBeingSearched = (-1);
583 if (strlen(argbuf) > 0)
584 FloorBeingSearched = extract_int(argbuf, 0);
586 if (CtdlAccessCheck(ac_logged_in)) return;
588 if (getuser(&CC->user, CC->curr_user)) {
589 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
592 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
594 ForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
601 * cmd_lkra() - List all known rooms
603 void cmd_lkra_backend(struct ctdlroom *qrbuf, void *data)
605 int FloorBeingSearched = (-1);
609 FloorBeingSearched = *(int *)data;
610 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
612 if ((( ra & (UA_KNOWN)))
613 && ((qrbuf->QRfloor == (FloorBeingSearched))
614 || ((FloorBeingSearched) < 0)))
615 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
618 void cmd_lkra(char *argbuf)
620 int FloorBeingSearched = (-1);
621 if (strlen(argbuf) > 0)
622 FloorBeingSearched = extract_int(argbuf, 0);
624 if (CtdlAccessCheck(ac_logged_in)) return;
626 if (getuser(&CC->user, CC->curr_user)) {
627 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
630 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
632 ForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
638 void cmd_lprm_backend(struct ctdlroom *qrbuf, void *data)
640 int FloorBeingSearched = (-1);
644 FloorBeingSearched = *(int *)data;
645 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
647 if ( ((qrbuf->QRflags & QR_PRIVATE) == 0)
648 && ((qrbuf->QRflags & QR_MAILBOX) == 0)
649 && ((qrbuf->QRfloor == (FloorBeingSearched))
650 || ((FloorBeingSearched) < 0)))
651 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
654 void cmd_lprm(char *argbuf)
656 int FloorBeingSearched = (-1);
657 if (strlen(argbuf) > 0)
658 FloorBeingSearched = extract_int(argbuf, 0);
660 cprintf("%d Publiic rooms:\n", LISTING_FOLLOWS);
662 ForEachRoom(cmd_lprm_backend, &FloorBeingSearched);
669 * cmd_lkrn() - List all known rooms with new messages
671 void cmd_lkrn_backend(struct ctdlroom *qrbuf, void *data)
673 int FloorBeingSearched = (-1);
677 FloorBeingSearched = *(int *)data;
678 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
681 && (ra & UA_HASNEWMSGS)
682 && ((qrbuf->QRfloor == (FloorBeingSearched))
683 || ((FloorBeingSearched) < 0)))
684 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
687 void cmd_lkrn(char *argbuf)
689 int FloorBeingSearched = (-1);
690 if (strlen(argbuf) > 0)
691 FloorBeingSearched = extract_int(argbuf, 0);
693 if (CtdlAccessCheck(ac_logged_in)) return;
695 if (getuser(&CC->user, CC->curr_user)) {
696 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
699 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
701 ForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
708 * cmd_lkro() - List all known rooms
710 void cmd_lkro_backend(struct ctdlroom *qrbuf, void *data)
712 int FloorBeingSearched = (-1);
716 FloorBeingSearched = *(int *)data;
717 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
720 && ((ra & UA_HASNEWMSGS) == 0)
721 && ((qrbuf->QRfloor == (FloorBeingSearched))
722 || ((FloorBeingSearched) < 0)))
723 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
726 void cmd_lkro(char *argbuf)
728 int FloorBeingSearched = (-1);
729 if (strlen(argbuf) > 0)
730 FloorBeingSearched = extract_int(argbuf, 0);
732 if (CtdlAccessCheck(ac_logged_in)) return;
734 if (getuser(&CC->user, CC->curr_user)) {
735 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
738 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
740 ForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
747 * cmd_lzrm() - List all forgotten rooms
749 void cmd_lzrm_backend(struct ctdlroom *qrbuf, void *data)
751 int FloorBeingSearched = (-1);
755 FloorBeingSearched = *(int *)data;
756 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
758 if ((ra & UA_GOTOALLOWED)
760 && ((qrbuf->QRfloor == (FloorBeingSearched))
761 || ((FloorBeingSearched) < 0)))
762 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
765 void cmd_lzrm(char *argbuf)
767 int FloorBeingSearched = (-1);
768 if (strlen(argbuf) > 0)
769 FloorBeingSearched = extract_int(argbuf, 0);
771 if (CtdlAccessCheck(ac_logged_in)) return;
773 if (getuser(&CC->user, CC->curr_user)) {
774 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
777 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
779 ForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
785 * Make the specified room the current room for this session. No validation
786 * or access control is done here -- the caller should make sure that the
787 * specified room exists and is ok to access.
789 void usergoto(char *where, int display_result, int transiently,
790 int *retmsgs, int *retnew)
793 int new_messages = 0;
794 int old_messages = 0;
795 int total_messages = 0;
799 int newmailcount = 0;
801 char truncated_roomname[ROOMNAMELEN];
802 struct cdbdata *cdbfr;
803 long *msglist = NULL;
805 unsigned int original_v_flags;
808 char setstr[128], lostr[64], histr[64];
812 /* If the supplied room name is NULL, the caller wants us to know that
813 * it has already copied the room record into CC->room, so
814 * we can skip the extra database fetch.
817 safestrncpy(CC->room.QRname, where, sizeof CC->room.QRname);
818 getroom(&CC->room, where);
821 /* Take care of all the formalities. */
823 begin_critical_section(S_USERS);
824 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
825 original_v_flags = vbuf.v_flags;
827 /* Know the room ... but not if it's the page log room, or if the
828 * caller specified that we're only entering this room transiently.
830 if ((strcasecmp(CC->room.QRname, config.c_logpages))
831 && (transiently == 0) ) {
832 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
833 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
836 /* Only rewrite the database record if we changed something */
837 if (vbuf.v_flags != original_v_flags) {
838 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
840 end_critical_section(S_USERS);
842 /* Check for new mail */
843 newmailcount = NewMailCount();
845 /* set info to 1 if the user needs to read the room's info file */
846 if (CC->room.QRinfo > vbuf.v_lastseen) {
851 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
853 msglist = (long *) cdbfr->ptr;
854 cdbfr->ptr = NULL; /* usergoto() now owns this memory */
855 num_msgs = cdbfr->len / sizeof(long);
860 for (a=0; a<num_msgs; ++a) {
861 if (msglist[a] > 0L) ++total_messages;
863 new_messages = num_msgs;
864 num_sets = num_tokens(vbuf.v_seen, ',');
865 for (s=0; s<num_sets; ++s) {
866 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
868 extract_token(lostr, setstr, 0, ':', sizeof lostr);
869 if (num_tokens(setstr, ':') >= 2) {
870 extract_token(histr, setstr, 1, ':', sizeof histr);
871 if (!strcmp(histr, "*")) {
872 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
876 strcpy(histr, lostr);
881 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
882 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
888 new_messages = total_messages - old_messages;
890 if (msglist != NULL) free(msglist);
892 if (CC->room.QRflags & QR_MAILBOX)
897 if ((CC->room.QRroomaide == CC->user.usernum)
898 || (CC->user.axlevel >= 6))
903 safestrncpy(truncated_roomname, CC->room.QRname, sizeof truncated_roomname);
904 if ( (CC->room.QRflags & QR_MAILBOX)
905 && (atol(CC->room.QRname) == CC->user.usernum) ) {
906 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
909 if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
913 if (retmsgs != NULL) *retmsgs = total_messages;
914 if (retnew != NULL) *retnew = new_messages;
915 lprintf(CTDL_DEBUG, "<%s> %d new of %d total messages\n",
917 new_messages, total_messages
920 CC->curr_view = (int)vbuf.v_view;
922 if (display_result) {
923 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|\n",
924 CIT_OK, CtdlCheckExpress(),
929 (int)CC->room.QRflags,
930 (long)CC->room.QRhighest,
931 (long)vbuf.v_lastseen,
935 (int)CC->room.QRfloor,
937 (int)CC->room.QRdefaultview,
945 * Handle some of the macro named rooms
947 void convert_room_name_macros(char *towhere, size_t maxlen) {
948 if (!strcasecmp(towhere, "_BASEROOM_")) {
949 safestrncpy(towhere, config.c_baseroom, maxlen);
951 else if (!strcasecmp(towhere, "_MAIL_")) {
952 safestrncpy(towhere, MAILROOM, maxlen);
954 else if (!strcasecmp(towhere, "_TRASH_")) {
955 safestrncpy(towhere, USERTRASHROOM, maxlen);
957 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
958 safestrncpy(towhere, config.c_twitroom, maxlen);
960 else if (!strcasecmp(towhere, "_CALENDAR_")) {
961 safestrncpy(towhere, USERCALENDARROOM, maxlen);
963 else if (!strcasecmp(towhere, "_TASKS_")) {
964 safestrncpy(towhere, USERTASKSROOM, maxlen);
966 else if (!strcasecmp(towhere, "_CONTACTS_")) {
967 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
969 else if (!strcasecmp(towhere, "_NOTES_")) {
970 safestrncpy(towhere, USERNOTESROOM, maxlen);
976 * cmd_goto() - goto a new room
978 void cmd_goto(char *gargs)
980 struct ctdlroom QRscratch;
984 char augmented_roomname[ROOMNAMELEN];
985 char towhere[ROOMNAMELEN];
989 if (CtdlAccessCheck(ac_logged_in)) return;
991 extract_token(towhere, gargs, 0, '|', sizeof towhere);
992 extract_token(password, gargs, 1, '|', sizeof password);
993 transiently = extract_int(gargs, 2);
995 getuser(&CC->user, CC->curr_user);
998 * Handle some of the macro named rooms
1000 convert_room_name_macros(towhere, sizeof towhere);
1002 /* First try a regular match */
1003 c = getroom(&QRscratch, towhere);
1005 /* Then try a mailbox name match */
1007 MailboxName(augmented_roomname, sizeof augmented_roomname,
1008 &CC->user, towhere);
1009 c = getroom(&QRscratch, augmented_roomname);
1011 safestrncpy(towhere, augmented_roomname, sizeof towhere);
1014 /* And if the room was found... */
1017 /* Let internal programs go directly to any room. */
1018 if (CC->internal_pgm) {
1019 memcpy(&CC->room, &QRscratch,
1020 sizeof(struct ctdlroom));
1021 usergoto(NULL, 1, transiently, NULL, NULL);
1025 /* See if there is an existing user/room relationship */
1026 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
1028 /* normal clients have to pass through security */
1029 if (ra & UA_GOTOALLOWED) {
1034 if ((QRscratch.QRflags & QR_MAILBOX) &&
1035 ((ra & UA_GOTOALLOWED))) {
1036 memcpy(&CC->room, &QRscratch,
1037 sizeof(struct ctdlroom));
1038 usergoto(NULL, 1, transiently, NULL, NULL);
1040 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
1041 ((ra & UA_KNOWN) == 0) &&
1042 (strcasecmp(QRscratch.QRpasswd, password)) &&
1043 (CC->user.axlevel < 6)
1045 cprintf("%d wrong or missing passwd\n",
1046 ERROR + PASSWORD_REQUIRED);
1048 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
1049 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
1050 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
1051 ((ra & UA_KNOWN) == 0) &&
1052 (CC->user.axlevel < 6)
1054 lprintf(CTDL_DEBUG, "Failed to acquire private room\n");
1056 memcpy(&CC->room, &QRscratch,
1057 sizeof(struct ctdlroom));
1058 usergoto(NULL, 1, transiently, NULL, NULL);
1064 cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
1070 struct ctdluser temp;
1071 struct cdbdata *cdbus;
1074 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
1075 cdb_rewind(CDB_USERS);
1076 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1077 memset(&temp, 0, sizeof temp);
1078 memcpy(&temp, cdbus->ptr, sizeof temp);
1081 CtdlRoomAccess(&CC->room, &temp, &ra, NULL);
1082 if ((CC->room.QRflags & QR_INUSE)
1085 cprintf("%s\n", temp.fullname);
1092 * RDIR command for room directory
1100 struct stat statbuf;
1101 char tempfilename[PATH_MAX];
1103 if (CtdlAccessCheck(ac_logged_in)) return;
1104 CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
1106 getroom(&CC->room, CC->room.QRname);
1107 getuser(&CC->user, CC->curr_user);
1109 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
1110 cprintf("%d not here.\n", ERROR + NOT_HERE);
1113 if (((CC->room.QRflags & QR_VISDIR) == 0)
1114 && (CC->user.axlevel < 6)
1115 && (CC->user.usernum != CC->room.QRroomaide)) {
1116 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1119 cprintf("%d %s|%s/%s\n",
1123 CC->room.QRdirname);
1125 snprintf(buf, sizeof buf,
1126 "ls %s/%s >%s 2>/dev/null",
1132 snprintf(buf, sizeof buf,
1135 CC->room.QRdirname);
1136 fd = fopen(buf, "r");
1138 fd = fopen("/dev/null", "r");
1140 ls = fopen(tempfilename, "r");
1141 while (fgets(flnm, sizeof flnm, ls) != NULL) {
1142 flnm[strlen(flnm) - 1] = 0;
1143 if (strcasecmp(flnm, "filedir")) {
1144 snprintf(buf, sizeof buf,
1149 stat(buf, &statbuf);
1150 safestrncpy(comment, "", sizeof comment);
1152 while ((fgets(buf, sizeof buf, fd) != NULL)
1153 && (strlen(comment) == 0)) {
1154 buf[strlen(buf) - 1] = 0;
1155 if ((!strncasecmp(buf, flnm, strlen(flnm)))
1156 && (buf[strlen(flnm)] == ' '))
1157 safestrncpy(comment,
1158 &buf[strlen(flnm) + 1],
1161 cprintf("%s|%ld|%s\n", flnm, (long)statbuf.st_size, comment);
1166 unlink(tempfilename);
1172 * get room parameters (aide or room aide command)
1176 if (CtdlAccessCheck(ac_room_aide)) return;
1178 getroom(&CC->room, CC->room.QRname);
1179 cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1183 ((CC->room.QRflags & QR_MAILBOX) ?
1184 &CC->room.QRname[11] : CC->room.QRname),
1186 ((CC->room.QRflags & QR_PASSWORDED) ?
1187 CC->room.QRpasswd : ""),
1189 ((CC->room.QRflags & QR_DIRECTORY) ?
1190 CC->room.QRdirname : ""),
1193 (int) CC->room.QRfloor,
1194 (int) CC->room.QRorder,
1196 CC->room.QRdefaultview,
1203 * Back end function to rename a room.
1204 * You can also specify which floor to move the room to, or specify -1 to
1205 * keep the room on the same floor it was on.
1207 * If you are renaming a mailbox room, you must supply the namespace prefix
1208 * in *at least* the old name!
1210 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1212 struct ctdlroom qrbuf;
1213 struct ctdlroom qrtmp;
1218 char actual_old_name[ROOMNAMELEN];
1220 lprintf(CTDL_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1221 old_name, new_name, new_floor);
1223 if (new_floor >= 0) {
1224 fl = cgetfloor(new_floor);
1225 if ((fl->f_flags & F_INUSE) == 0) {
1226 return(crr_invalid_floor);
1230 begin_critical_section(S_ROOMS);
1232 if ( (getroom(&qrtmp, new_name) == 0)
1233 && (strcasecmp(new_name, old_name)) ) {
1234 ret = crr_already_exists;
1237 else if (getroom(&qrbuf, old_name) != 0) {
1238 ret = crr_room_not_found;
1241 else if ( (CC->user.axlevel < 6) && (!CC->internal_pgm)
1242 && (CC->user.usernum != qrbuf.QRroomaide)
1243 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) ) {
1244 ret = crr_access_denied;
1247 else if (is_noneditable(&qrbuf)) {
1248 ret = crr_noneditable;
1253 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1254 if (qrbuf.QRflags & QR_MAILBOX) {
1255 owner = atol(qrbuf.QRname);
1257 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1258 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1259 "%010ld.%s", owner, new_name);
1262 safestrncpy(qrbuf.QRname, new_name,
1263 sizeof(qrbuf.QRname));
1266 /* Reject change of floor for baseroom/aideroom */
1267 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1268 !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1272 /* Take care of floor stuff */
1273 old_floor = qrbuf.QRfloor;
1274 if (new_floor < 0) {
1275 new_floor = old_floor;
1277 qrbuf.QRfloor = new_floor;
1280 begin_critical_section(S_CONFIG);
1282 /* If baseroom/aideroom name changes, update config */
1283 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1284 safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1287 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1288 safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1292 end_critical_section(S_CONFIG);
1294 /* If the room name changed, then there are now two room
1295 * records, so we have to delete the old one.
1297 if (strcasecmp(new_name, old_name)) {
1298 b_deleteroom(actual_old_name);
1304 end_critical_section(S_ROOMS);
1306 /* Adjust the floor reference counts if necessary */
1307 if (new_floor != old_floor) {
1308 lgetfloor(&flbuf, old_floor);
1309 --flbuf.f_ref_count;
1310 lputfloor(&flbuf, old_floor);
1311 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1312 lgetfloor(&flbuf, new_floor);
1313 ++flbuf.f_ref_count;
1314 lputfloor(&flbuf, new_floor);
1315 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1318 /* ...and everybody say "YATTA!" */
1324 * set room parameters (aide or room aide command)
1326 void cmd_setr(char *args)
1332 char new_name[ROOMNAMELEN];
1334 if (CtdlAccessCheck(ac_logged_in)) return;
1336 if (num_parms(args) >= 6) {
1337 new_floor = extract_int(args, 5);
1339 new_floor = (-1); /* don't change the floor */
1342 /* When is a new name more than just a new name? When the old name
1343 * has a namespace prefix.
1345 if (CC->room.QRflags & QR_MAILBOX) {
1346 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
1348 safestrncpy(new_name, "", sizeof new_name);
1350 extract_token(&new_name[strlen(new_name)], args, 0, '|', (sizeof new_name - strlen(new_name)));
1352 r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
1354 if (r == crr_room_not_found) {
1355 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
1356 } else if (r == crr_already_exists) {
1357 cprintf("%d '%s' already exists.\n",
1358 ERROR + ALREADY_EXISTS, new_name);
1359 } else if (r == crr_noneditable) {
1360 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
1361 } else if (r == crr_invalid_floor) {
1362 cprintf("%d Target floor does not exist.\n",
1363 ERROR + INVALID_FLOOR_OPERATION);
1364 } else if (r == crr_access_denied) {
1365 cprintf("%d You do not have permission to edit '%s'\n",
1366 ERROR + HIGHER_ACCESS_REQUIRED,
1368 } else if (r != crr_ok) {
1369 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1370 ERROR + INTERNAL_ERROR, r);
1377 getroom(&CC->room, new_name);
1379 /* Now we have to do a bunch of other stuff */
1381 if (num_parms(args) >= 7) {
1382 new_order = extract_int(args, 6);
1385 if (new_order > 127)
1389 lgetroom(&CC->room, CC->room.QRname);
1391 /* Directory room */
1392 extract_token(buf, args, 2, '|', sizeof buf);
1394 safestrncpy(CC->room.QRdirname, buf,
1395 sizeof CC->room.QRdirname);
1398 if (num_parms(args) >= 8) {
1399 CC->room.QRdefaultview = extract_int(args, 7);
1402 /* Second set of flags */
1403 if (num_parms(args) >= 9) {
1404 CC->room.QRflags2 = extract_int(args, 8);
1408 CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
1409 /* Clean up a client boo-boo: if the client set the room to
1410 * guess-name or passworded, ensure that the private flag is
1413 if ((CC->room.QRflags & QR_GUESSNAME)
1414 || (CC->room.QRflags & QR_PASSWORDED))
1415 CC->room.QRflags |= QR_PRIVATE;
1417 /* Some changes can't apply to BASEROOM */
1418 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1420 CC->room.QRorder = 0;
1421 CC->room.QRpasswd[0] = '\0';
1422 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1423 QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1424 CC->room.QRflags |= QR_PERMANENT;
1426 /* March order (doesn't apply to AIDEROOM) */
1427 if (num_parms(args) >= 7)
1428 CC->room.QRorder = (char) new_order;
1430 extract_token(buf, args, 1, '|', sizeof buf);
1432 safestrncpy(CC->room.QRpasswd, buf,
1433 sizeof CC->room.QRpasswd);
1434 /* Kick everyone out if the client requested it
1435 * (by changing the room's generation number)
1437 if (extract_int(args, 4)) {
1438 time(&CC->room.QRgen);
1441 /* Some changes can't apply to AIDEROOM */
1442 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1444 CC->room.QRorder = 0;
1445 CC->room.QRflags &= ~QR_MAILBOX;
1446 CC->room.QRflags |= QR_PERMANENT;
1449 /* Write the room record back to disk */
1450 lputroom(&CC->room);
1452 /* Create a room directory if necessary */
1453 if (CC->room.QRflags & QR_DIRECTORY) {
1454 snprintf(buf, sizeof buf,"%s/%s",
1456 CC->room.QRdirname);
1459 snprintf(buf, sizeof buf, "The room \"%s\" has been edited by %s.\n",
1460 CC->room.QRname, CC->curr_user);
1461 aide_message(buf, "Room modification Message");
1462 cprintf("%d Ok\n", CIT_OK);
1468 * get the name of the room aide for this room
1472 struct ctdluser usbuf;
1474 if (CtdlAccessCheck(ac_logged_in)) return;
1476 if (getuserbynumber(&usbuf, CC->room.QRroomaide) == 0) {
1477 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1479 cprintf("%d \n", CIT_OK);
1485 * set the room aide for this room
1487 void cmd_seta(char *new_ra)
1489 struct ctdluser usbuf;
1494 if (CtdlAccessCheck(ac_room_aide)) return;
1496 if (getuser(&usbuf, new_ra) != 0) {
1499 newu = usbuf.usernum;
1502 lgetroom(&CC->room, CC->room.QRname);
1504 if (CC->room.QRroomaide != newu) {
1507 CC->room.QRroomaide = newu;
1508 lputroom(&CC->room);
1511 * We have to post the change notice _after_ writing changes to
1512 * the room table, otherwise it would deadlock!
1514 if (post_notice == 1) {
1515 if (strlen(usbuf.fullname) > 0)
1516 snprintf(buf, sizeof buf,
1517 "%s is now the room aide for \"%s\".\n",
1518 usbuf.fullname, CC->room.QRname);
1520 snprintf(buf, sizeof buf,
1521 "There is now no room aide for \"%s\".\n",
1523 aide_message(buf, "Aide Room Modification");
1525 cprintf("%d Ok\n", CIT_OK);
1529 * retrieve info file for this room
1537 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_info_dir);
1538 info_fp = fopen(filename, "r");
1540 if (info_fp == NULL) {
1541 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
1544 cprintf("%d Info:\n", LISTING_FOLLOWS);
1545 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1546 if (strlen(buf) > 0)
1547 buf[strlen(buf) - 1] = 0;
1548 cprintf("%s\n", buf);
1555 * Asynchronously schedule a room for deletion. The room will appear
1556 * deleted to the user(s), but it won't actually get purged from the
1557 * database until THE DREADED AUTO-PURGER makes its next run.
1559 void schedule_room_for_deletion(struct ctdlroom *qrbuf)
1561 char old_name[ROOMNAMELEN];
1564 lprintf(CTDL_NOTICE, "Scheduling room <%s> for deletion\n",
1567 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1569 getroom(qrbuf, qrbuf->QRname);
1571 /* Turn the room into a private mailbox owned by a user who doesn't
1572 * exist. This will immediately make the room invisible to everyone,
1573 * and qualify the room for purging.
1575 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1580 qrbuf->QRflags |= QR_MAILBOX;
1581 time(&qrbuf->QRgen); /* Use a timestamp as the new generation number */
1585 b_deleteroom(old_name);
1591 * Back end processing to delete a room and everything associated with it
1592 * (This one is synchronous and should only get called by THE DREADED
1593 * AUTO-PURGER in serv_expire.c. All user-facing code should call
1594 * the asynchronous schedule_room_for_deletion() instead.)
1596 void delete_room(struct ctdlroom *qrbuf)
1600 /* TODO: filename magic? does this realy work? */
1602 lprintf(CTDL_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1604 /* Delete the info file */
1605 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_info_dir);
1608 /* Delete the image file */
1609 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_image_dir);
1612 /* Delete the room's network config file */
1613 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1616 /* Delete the messages in the room
1617 * (Careful: this opens an S_ROOMS critical section!)
1619 CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
1621 /* Flag the room record as not in use */
1622 lgetroom(qrbuf, qrbuf->QRname);
1626 /* then decrement the reference count for the floor */
1627 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1628 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1629 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1631 /* Delete the room record from the database! */
1632 b_deleteroom(qrbuf->QRname);
1638 * Check access control for deleting a room
1640 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1642 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1646 if (is_noneditable(qr)) {
1651 * For mailboxes, check stuff
1653 if (qr->QRflags & QR_MAILBOX) {
1655 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1657 if (atol(qr->QRname) != CC->user.usernum) {
1658 return(0); /* not my room */
1661 /* Can't delete your Mail> room */
1662 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1664 /* Otherwise it's ok */
1669 * For normal rooms, just check for aide or room aide status.
1671 return(is_room_aide());
1675 * aide command: kill the current room
1677 void cmd_kill(char *argbuf)
1679 char deleted_room_name[ROOMNAMELEN];
1683 kill_ok = extract_int(argbuf, 0);
1685 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
1686 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1690 if (CC->room.QRflags & QR_MAILBOX) {
1691 safestrncpy(deleted_room_name, &CC->room.QRname[11], sizeof deleted_room_name);
1694 safestrncpy(deleted_room_name, CC->room.QRname, sizeof deleted_room_name);
1697 /* Do the dirty work */
1698 schedule_room_for_deletion(&CC->room);
1700 /* Return to the Lobby */
1701 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1703 /* tell the world what we did */
1704 snprintf(msg, sizeof msg, "The room \"%s\" has been deleted by %s.\n",
1705 deleted_room_name, CC->curr_user);
1706 aide_message(msg, "Room Purger Message");
1707 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1709 cprintf("%d ok to delete.\n", CIT_OK);
1715 * Internal code to create a new room (returns room flags)
1717 * Room types: 0=public, 1=guessname, 2=passworded, 3=inv-only,
1718 * 4=mailbox, 5=mailbox, but caller supplies namespace
1720 unsigned create_room(char *new_room_name,
1722 char *new_room_pass,
1729 struct ctdlroom qrbuf;
1733 lprintf(CTDL_DEBUG, "create_room(name=%s, type=%d, view=%d)\n",
1734 new_room_name, new_room_type, new_room_view);
1736 if (getroom(&qrbuf, new_room_name) == 0) {
1737 lprintf(CTDL_DEBUG, "%s already exists.\n", new_room_name);
1741 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1742 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1743 qrbuf.QRflags = QR_INUSE;
1744 if (new_room_type > 0)
1745 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1746 if (new_room_type == 1)
1747 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1748 if (new_room_type == 2)
1749 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1750 if ( (new_room_type == 4) || (new_room_type == 5) )
1751 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1753 /* If the user is requesting a personal room, set up the room
1754 * name accordingly (prepend the user number)
1756 if (new_room_type == 4) {
1757 MailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1760 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1763 /* If the room is private, and the system administrator has elected
1764 * to automatically grant room aide privileges, do so now; otherwise,
1765 * set the room aide to undefined.
1767 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1768 qrbuf.QRroomaide = CC->user.usernum;
1770 qrbuf.QRroomaide = (-1L);
1774 * If the caller is only interested in testing whether this will work,
1775 * return now without creating the room.
1777 if (!really_create) return (qrbuf.QRflags);
1779 qrbuf.QRnumber = get_new_room_number();
1780 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1781 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1782 qrbuf.QRfloor = new_room_floor;
1783 qrbuf.QRdefaultview = new_room_view;
1785 /* save what we just did... */
1788 /* bump the reference count on whatever floor the room is on */
1789 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1790 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1791 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1793 /* Grant the creator access to the room unless the avoid_access
1794 * parameter was specified.
1796 if ( (CC->logged_in) && (avoid_access == 0) ) {
1797 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1798 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1799 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1800 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1803 /* resume our happy day */
1804 return (qrbuf.QRflags);
1811 void cmd_cre8(char *args)
1814 char new_room_name[ROOMNAMELEN];
1816 char new_room_pass[32];
1819 char *notification_message = NULL;
1822 int avoid_access = 0;
1824 cre8_ok = extract_int(args, 0);
1825 extract_token(new_room_name, args, 1, '|', sizeof new_room_name);
1826 new_room_name[ROOMNAMELEN - 1] = 0;
1827 new_room_type = extract_int(args, 2);
1828 extract_token(new_room_pass, args, 3, '|', sizeof new_room_pass);
1829 avoid_access = extract_int(args, 5);
1830 new_room_view = extract_int(args, 6);
1831 new_room_pass[9] = 0;
1834 if ((strlen(new_room_name) == 0) && (cre8_ok == 1)) {
1835 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
1839 if (!strcasecmp(new_room_name, MAILROOM)) {
1840 cprintf("%d '%s' already exists.\n",
1841 ERROR + ALREADY_EXISTS, new_room_name);
1845 if (num_parms(args) >= 5) {
1846 fl = cgetfloor(extract_int(args, 4));
1848 cprintf("%d Invalid floor number.\n",
1849 ERROR + INVALID_FLOOR_OPERATION);
1852 else if ((fl->f_flags & F_INUSE) == 0) {
1853 cprintf("%d Invalid floor number.\n",
1854 ERROR + INVALID_FLOOR_OPERATION);
1857 new_room_floor = extract_int(args, 4);
1861 if (CtdlAccessCheck(ac_logged_in)) return;
1863 if (CC->user.axlevel < config.c_createax || CC->internal_pgm) {
1864 cprintf("%d You need higher access to create rooms.\n",
1865 ERROR + HIGHER_ACCESS_REQUIRED);
1869 if ((strlen(new_room_name) == 0) && (cre8_ok == 0)) {
1870 cprintf("%d Ok to create rooms.\n", CIT_OK);
1874 if ((new_room_type < 0) || (new_room_type > 5)) {
1875 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
1879 if (new_room_type == 5) {
1880 if (CC->user.axlevel < 6) {
1881 cprintf("%d Higher access required\n",
1882 ERROR + HIGHER_ACCESS_REQUIRED);
1887 /* Check to make sure the requested room name doesn't already exist */
1888 newflags = create_room(new_room_name,
1889 new_room_type, new_room_pass, new_room_floor,
1890 0, avoid_access, new_room_view);
1891 if (newflags == 0) {
1892 cprintf("%d '%s' already exists.\n",
1893 ERROR + ALREADY_EXISTS, new_room_name);
1898 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
1902 /* If we reach this point, the room needs to be created. */
1904 newflags = create_room(new_room_name,
1905 new_room_type, new_room_pass, new_room_floor, 1, 0,
1908 /* post a message in Aide> describing the new room */
1909 notification_message = malloc(1024);
1910 snprintf(notification_message, 1024,
1911 "A new room called \"%s\" has been created by %s%s%s%s%s%s\n",
1914 ((newflags & QR_MAILBOX) ? " [personal]" : ""),
1915 ((newflags & QR_PRIVATE) ? " [private]" : ""),
1916 ((newflags & QR_GUESSNAME) ? " [hidden]" : ""),
1917 ((newflags & QR_PASSWORDED) ? " Password: " : ""),
1918 ((newflags & QR_PASSWORDED) ? new_room_pass : "")
1920 aide_message(notification_message, "Room Creation Message");
1921 free(notification_message);
1923 cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
1928 void cmd_einf(char *ok)
1929 { /* enter info file for current room */
1931 char infofilename[SIZ];
1936 if (CtdlAccessCheck(ac_room_aide)) return;
1938 if (atoi(ok) == 0) {
1939 cprintf("%d Ok.\n", CIT_OK);
1942 assoc_file_name(infofilename, sizeof infofilename, &CC->room, ctdl_info_dir);
1943 lprintf(CTDL_DEBUG, "opening\n");
1944 fp = fopen(infofilename, "w");
1945 lprintf(CTDL_DEBUG, "checking\n");
1947 cprintf("%d Cannot open %s: %s\n",
1948 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1951 cprintf("%d Send info...\n", SEND_LISTING);
1954 client_getln(buf, sizeof buf);
1955 if (strcmp(buf, "000"))
1956 fprintf(fp, "%s\n", buf);
1957 } while (strcmp(buf, "000"));
1960 /* now update the room index so people will see our new info */
1961 lgetroom(&CC->room, CC->room.QRname); /* lock so no one steps on us */
1962 CC->room.QRinfo = CC->room.QRhighest + 1L;
1963 lputroom(&CC->room);
1968 * cmd_lflr() - List all known floors
1975 if (CtdlAccessCheck(ac_logged_in)) return;
1977 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1979 for (a = 0; a < MAXFLOORS; ++a) {
1980 getfloor(&flbuf, a);
1981 if (flbuf.f_flags & F_INUSE) {
1982 cprintf("%d|%s|%d\n",
1994 * create a new floor
1996 void cmd_cflr(char *argbuf)
1998 char new_floor_name[256];
2001 int free_slot = (-1);
2004 extract_token(new_floor_name, argbuf, 0, '|', sizeof new_floor_name);
2005 cflr_ok = extract_int(argbuf, 1);
2007 if (CtdlAccessCheck(ac_aide)) return;
2009 if (strlen(new_floor_name) == 0) {
2010 cprintf("%d Blank floor name not allowed.\n",
2011 ERROR + ILLEGAL_VALUE);
2015 for (a = 0; a < MAXFLOORS; ++a) {
2016 getfloor(&flbuf, a);
2018 /* note any free slots while we're scanning... */
2019 if (((flbuf.f_flags & F_INUSE) == 0)
2023 /* check to see if it already exists */
2024 if ((!strcasecmp(flbuf.f_name, new_floor_name))
2025 && (flbuf.f_flags & F_INUSE)) {
2026 cprintf("%d Floor '%s' already exists.\n",
2027 ERROR + ALREADY_EXISTS,
2033 if (free_slot < 0) {
2034 cprintf("%d There is no space available for a new floor.\n",
2035 ERROR + INVALID_FLOOR_OPERATION);
2039 cprintf("%d ok to create...\n", CIT_OK);
2042 lgetfloor(&flbuf, free_slot);
2043 flbuf.f_flags = F_INUSE;
2044 flbuf.f_ref_count = 0;
2045 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
2046 lputfloor(&flbuf, free_slot);
2047 cprintf("%d %d\n", CIT_OK, free_slot);
2055 void cmd_kflr(char *argbuf)
2058 int floor_to_delete;
2062 floor_to_delete = extract_int(argbuf, 0);
2063 kflr_ok = extract_int(argbuf, 1);
2065 if (CtdlAccessCheck(ac_aide)) return;
2067 lgetfloor(&flbuf, floor_to_delete);
2070 if ((flbuf.f_flags & F_INUSE) == 0) {
2071 cprintf("%d Floor %d not in use.\n",
2072 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
2075 if (flbuf.f_ref_count != 0) {
2076 cprintf("%d Cannot delete; floor contains %d rooms.\n",
2077 ERROR + INVALID_FLOOR_OPERATION,
2082 cprintf("%d Ok\n", CIT_OK);
2084 cprintf("%d Ok to delete...\n", CIT_OK);
2091 if ((delete_ok == 1) && (kflr_ok == 1))
2093 lputfloor(&flbuf, floor_to_delete);
2099 void cmd_eflr(char *argbuf)
2105 np = num_parms(argbuf);
2107 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
2111 if (CtdlAccessCheck(ac_aide)) return;
2113 floor_num = extract_int(argbuf, 0);
2114 lgetfloor(&flbuf, floor_num);
2115 if ((flbuf.f_flags & F_INUSE) == 0) {
2116 lputfloor(&flbuf, floor_num);
2117 cprintf("%d Floor %d is not in use.\n",
2118 ERROR + INVALID_FLOOR_OPERATION, floor_num);
2122 extract_token(flbuf.f_name, argbuf, 1, '|', sizeof flbuf.f_name);
2123 lputfloor(&flbuf, floor_num);
2125 cprintf("%d Ok\n", CIT_OK);