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) {
850 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
852 msglist = (long *) cdbfr->ptr;
853 cdbfr->ptr = NULL; /* usergoto() now owns this memory */
854 num_msgs = cdbfr->len / sizeof(long);
859 for (a=0; a<num_msgs; ++a) {
860 if (msglist[a] > 0L) ++total_messages;
862 new_messages = num_msgs;
863 num_sets = num_tokens(vbuf.v_seen, ',');
864 for (s=0; s<num_sets; ++s) {
865 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
867 extract_token(lostr, setstr, 0, ':', sizeof lostr);
868 if (num_tokens(setstr, ':') >= 2) {
869 extract_token(histr, setstr, 1, ':', sizeof histr);
870 if (!strcmp(histr, "*")) {
871 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
875 strcpy(histr, lostr);
880 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
881 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
887 new_messages = total_messages - old_messages;
889 if (msglist != NULL) free(msglist);
891 if (CC->room.QRflags & QR_MAILBOX)
896 if ((CC->room.QRroomaide == CC->user.usernum)
897 || (CC->user.axlevel >= 6))
902 safestrncpy(truncated_roomname, CC->room.QRname, sizeof truncated_roomname);
903 if ( (CC->room.QRflags & QR_MAILBOX)
904 && (atol(CC->room.QRname) == CC->user.usernum) ) {
905 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
908 if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
912 if (retmsgs != NULL) *retmsgs = total_messages;
913 if (retnew != NULL) *retnew = new_messages;
914 lprintf(CTDL_DEBUG, "<%s> %d new of %d total messages\n",
916 new_messages, total_messages
919 CC->curr_view = (int)vbuf.v_view;
921 if (display_result) {
922 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|\n",
923 CIT_OK, CtdlCheckExpress(),
928 (int)CC->room.QRflags,
929 (long)CC->room.QRhighest,
930 (long)vbuf.v_lastseen,
934 (int)CC->room.QRfloor,
936 (int)CC->room.QRdefaultview,
944 * Handle some of the macro named rooms
946 void convert_room_name_macros(char *towhere, size_t maxlen) {
947 if (!strcasecmp(towhere, "_BASEROOM_")) {
948 safestrncpy(towhere, config.c_baseroom, maxlen);
950 else if (!strcasecmp(towhere, "_MAIL_")) {
951 safestrncpy(towhere, MAILROOM, maxlen);
953 else if (!strcasecmp(towhere, "_TRASH_")) {
954 safestrncpy(towhere, USERTRASHROOM, maxlen);
956 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
957 safestrncpy(towhere, config.c_twitroom, maxlen);
959 else if (!strcasecmp(towhere, "_CALENDAR_")) {
960 safestrncpy(towhere, USERCALENDARROOM, maxlen);
962 else if (!strcasecmp(towhere, "_TASKS_")) {
963 safestrncpy(towhere, USERTASKSROOM, maxlen);
965 else if (!strcasecmp(towhere, "_CONTACTS_")) {
966 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
968 else if (!strcasecmp(towhere, "_NOTES_")) {
969 safestrncpy(towhere, USERNOTESROOM, maxlen);
975 * cmd_goto() - goto a new room
977 void cmd_goto(char *gargs)
979 struct ctdlroom QRscratch;
983 char augmented_roomname[ROOMNAMELEN];
984 char towhere[ROOMNAMELEN];
988 if (CtdlAccessCheck(ac_logged_in)) return;
990 extract_token(towhere, gargs, 0, '|', sizeof towhere);
991 extract_token(password, gargs, 1, '|', sizeof password);
992 transiently = extract_int(gargs, 2);
994 getuser(&CC->user, CC->curr_user);
997 * Handle some of the macro named rooms
999 convert_room_name_macros(towhere, sizeof towhere);
1001 /* First try a regular match */
1002 c = getroom(&QRscratch, towhere);
1004 /* Then try a mailbox name match */
1006 MailboxName(augmented_roomname, sizeof augmented_roomname,
1007 &CC->user, towhere);
1008 c = getroom(&QRscratch, augmented_roomname);
1010 safestrncpy(towhere, augmented_roomname, sizeof towhere);
1013 /* And if the room was found... */
1016 /* Let internal programs go directly to any room. */
1017 if (CC->internal_pgm) {
1018 memcpy(&CC->room, &QRscratch,
1019 sizeof(struct ctdlroom));
1020 usergoto(NULL, 1, transiently, NULL, NULL);
1024 /* See if there is an existing user/room relationship */
1025 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
1027 /* normal clients have to pass through security */
1028 if (ra & UA_GOTOALLOWED) {
1033 if ((QRscratch.QRflags & QR_MAILBOX) &&
1034 ((ra & UA_GOTOALLOWED))) {
1035 memcpy(&CC->room, &QRscratch,
1036 sizeof(struct ctdlroom));
1037 usergoto(NULL, 1, transiently, NULL, NULL);
1039 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
1040 ((ra & UA_KNOWN) == 0) &&
1041 (strcasecmp(QRscratch.QRpasswd, password)) &&
1042 (CC->user.axlevel < 6)
1044 cprintf("%d wrong or missing passwd\n",
1045 ERROR + PASSWORD_REQUIRED);
1047 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
1048 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
1049 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
1050 ((ra & UA_KNOWN) == 0) &&
1051 (CC->user.axlevel < 6)
1053 lprintf(CTDL_DEBUG, "Failed to acquire private room\n");
1055 memcpy(&CC->room, &QRscratch,
1056 sizeof(struct ctdlroom));
1057 usergoto(NULL, 1, transiently, NULL, NULL);
1063 cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
1069 struct ctdluser temp;
1070 struct cdbdata *cdbus;
1073 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
1074 cdb_rewind(CDB_USERS);
1075 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1076 memset(&temp, 0, sizeof temp);
1077 memcpy(&temp, cdbus->ptr, sizeof temp);
1080 CtdlRoomAccess(&CC->room, &temp, &ra, NULL);
1081 if ((CC->room.QRflags & QR_INUSE)
1084 cprintf("%s\n", temp.fullname);
1091 * RDIR command for room directory
1099 struct stat statbuf;
1100 char tempfilename[PATH_MAX];
1102 if (CtdlAccessCheck(ac_logged_in)) return;
1103 CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
1105 getroom(&CC->room, CC->room.QRname);
1106 getuser(&CC->user, CC->curr_user);
1108 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
1109 cprintf("%d not here.\n", ERROR + NOT_HERE);
1112 if (((CC->room.QRflags & QR_VISDIR) == 0)
1113 && (CC->user.axlevel < 6)
1114 && (CC->user.usernum != CC->room.QRroomaide)) {
1115 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1118 cprintf("%d %s|%s/%s\n",
1122 CC->room.QRdirname);
1124 snprintf(buf, sizeof buf,
1125 "ls %s/%s >%s 2>/dev/null",
1131 snprintf(buf, sizeof buf,
1134 CC->room.QRdirname);
1135 fd = fopen(buf, "r");
1137 fd = fopen("/dev/null", "r");
1139 ls = fopen(tempfilename, "r");
1140 while (fgets(flnm, sizeof flnm, ls) != NULL) {
1141 flnm[strlen(flnm) - 1] = 0;
1142 if (strcasecmp(flnm, "filedir")) {
1143 snprintf(buf, sizeof buf,
1148 stat(buf, &statbuf);
1149 safestrncpy(comment, "", sizeof comment);
1151 while ((fgets(buf, sizeof buf, fd) != NULL)
1152 && (strlen(comment) == 0)) {
1153 buf[strlen(buf) - 1] = 0;
1154 if ((!strncasecmp(buf, flnm, strlen(flnm)))
1155 && (buf[strlen(flnm)] == ' '))
1156 safestrncpy(comment,
1157 &buf[strlen(flnm) + 1],
1160 cprintf("%s|%ld|%s\n", flnm, (long)statbuf.st_size, comment);
1165 unlink(tempfilename);
1171 * get room parameters (aide or room aide command)
1175 if (CtdlAccessCheck(ac_room_aide)) return;
1177 getroom(&CC->room, CC->room.QRname);
1178 cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1182 ((CC->room.QRflags & QR_MAILBOX) ?
1183 &CC->room.QRname[11] : CC->room.QRname),
1185 ((CC->room.QRflags & QR_PASSWORDED) ?
1186 CC->room.QRpasswd : ""),
1188 ((CC->room.QRflags & QR_DIRECTORY) ?
1189 CC->room.QRdirname : ""),
1192 (int) CC->room.QRfloor,
1193 (int) CC->room.QRorder,
1195 CC->room.QRdefaultview,
1202 * Back end function to rename a room.
1203 * You can also specify which floor to move the room to, or specify -1 to
1204 * keep the room on the same floor it was on.
1206 * If you are renaming a mailbox room, you must supply the namespace prefix
1207 * in *at least* the old name!
1209 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1211 struct ctdlroom qrbuf;
1212 struct ctdlroom qrtmp;
1217 char actual_old_name[ROOMNAMELEN];
1219 lprintf(CTDL_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1220 old_name, new_name, new_floor);
1222 if (new_floor >= 0) {
1223 fl = cgetfloor(new_floor);
1224 if ((fl->f_flags & F_INUSE) == 0) {
1225 return(crr_invalid_floor);
1229 begin_critical_section(S_ROOMS);
1231 if ( (getroom(&qrtmp, new_name) == 0)
1232 && (strcasecmp(new_name, old_name)) ) {
1233 ret = crr_already_exists;
1236 else if (getroom(&qrbuf, old_name) != 0) {
1237 ret = crr_room_not_found;
1240 else if ( (CC->user.axlevel < 6) && (!CC->internal_pgm)
1241 && (CC->user.usernum != qrbuf.QRroomaide)
1242 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) ) {
1243 ret = crr_access_denied;
1246 else if (is_noneditable(&qrbuf)) {
1247 ret = crr_noneditable;
1252 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1253 if (qrbuf.QRflags & QR_MAILBOX) {
1254 owner = atol(qrbuf.QRname);
1256 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1257 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1258 "%010ld.%s", owner, new_name);
1261 safestrncpy(qrbuf.QRname, new_name,
1262 sizeof(qrbuf.QRname));
1265 /* Reject change of floor for baseroom/aideroom */
1266 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1267 !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1271 /* Take care of floor stuff */
1272 old_floor = qrbuf.QRfloor;
1273 if (new_floor < 0) {
1274 new_floor = old_floor;
1276 qrbuf.QRfloor = new_floor;
1279 begin_critical_section(S_CONFIG);
1281 /* If baseroom/aideroom name changes, update config */
1282 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1283 safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1286 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1287 safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1291 end_critical_section(S_CONFIG);
1293 /* If the room name changed, then there are now two room
1294 * records, so we have to delete the old one.
1296 if (strcasecmp(new_name, old_name)) {
1297 b_deleteroom(actual_old_name);
1303 end_critical_section(S_ROOMS);
1305 /* Adjust the floor reference counts if necessary */
1306 if (new_floor != old_floor) {
1307 lgetfloor(&flbuf, old_floor);
1308 --flbuf.f_ref_count;
1309 lputfloor(&flbuf, old_floor);
1310 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1311 lgetfloor(&flbuf, new_floor);
1312 ++flbuf.f_ref_count;
1313 lputfloor(&flbuf, new_floor);
1314 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1317 /* ...and everybody say "YATTA!" */
1323 * set room parameters (aide or room aide command)
1325 void cmd_setr(char *args)
1331 char new_name[ROOMNAMELEN];
1333 if (CtdlAccessCheck(ac_logged_in)) return;
1335 if (num_parms(args) >= 6) {
1336 new_floor = extract_int(args, 5);
1338 new_floor = (-1); /* don't change the floor */
1341 /* When is a new name more than just a new name? When the old name
1342 * has a namespace prefix.
1344 if (CC->room.QRflags & QR_MAILBOX) {
1345 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
1347 safestrncpy(new_name, "", sizeof new_name);
1349 extract_token(&new_name[strlen(new_name)], args, 0, '|', (sizeof new_name - strlen(new_name)));
1351 r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
1353 if (r == crr_room_not_found) {
1354 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
1355 } else if (r == crr_already_exists) {
1356 cprintf("%d '%s' already exists.\n",
1357 ERROR + ALREADY_EXISTS, new_name);
1358 } else if (r == crr_noneditable) {
1359 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
1360 } else if (r == crr_invalid_floor) {
1361 cprintf("%d Target floor does not exist.\n",
1362 ERROR + INVALID_FLOOR_OPERATION);
1363 } else if (r == crr_access_denied) {
1364 cprintf("%d You do not have permission to edit '%s'\n",
1365 ERROR + HIGHER_ACCESS_REQUIRED,
1367 } else if (r != crr_ok) {
1368 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1369 ERROR + INTERNAL_ERROR, r);
1376 getroom(&CC->room, new_name);
1378 /* Now we have to do a bunch of other stuff */
1380 if (num_parms(args) >= 7) {
1381 new_order = extract_int(args, 6);
1384 if (new_order > 127)
1388 lgetroom(&CC->room, CC->room.QRname);
1390 /* Directory room */
1391 extract_token(buf, args, 2, '|', sizeof buf);
1393 safestrncpy(CC->room.QRdirname, buf,
1394 sizeof CC->room.QRdirname);
1397 if (num_parms(args) >= 8) {
1398 CC->room.QRdefaultview = extract_int(args, 7);
1401 /* Second set of flags */
1402 if (num_parms(args) >= 9) {
1403 CC->room.QRflags2 = extract_int(args, 8);
1407 CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
1408 /* Clean up a client boo-boo: if the client set the room to
1409 * guess-name or passworded, ensure that the private flag is
1412 if ((CC->room.QRflags & QR_GUESSNAME)
1413 || (CC->room.QRflags & QR_PASSWORDED))
1414 CC->room.QRflags |= QR_PRIVATE;
1416 /* Some changes can't apply to BASEROOM */
1417 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1419 CC->room.QRorder = 0;
1420 CC->room.QRpasswd[0] = '\0';
1421 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1422 QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1423 CC->room.QRflags |= QR_PERMANENT;
1425 /* March order (doesn't apply to AIDEROOM) */
1426 if (num_parms(args) >= 7)
1427 CC->room.QRorder = (char) new_order;
1429 extract_token(buf, args, 1, '|', sizeof buf);
1431 safestrncpy(CC->room.QRpasswd, buf,
1432 sizeof CC->room.QRpasswd);
1433 /* Kick everyone out if the client requested it
1434 * (by changing the room's generation number)
1436 if (extract_int(args, 4)) {
1437 time(&CC->room.QRgen);
1440 /* Some changes can't apply to AIDEROOM */
1441 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1443 CC->room.QRorder = 0;
1444 CC->room.QRflags &= ~QR_MAILBOX;
1445 CC->room.QRflags |= QR_PERMANENT;
1448 /* Write the room record back to disk */
1449 lputroom(&CC->room);
1451 /* Create a room directory if necessary */
1452 if (CC->room.QRflags & QR_DIRECTORY) {
1453 snprintf(buf, sizeof buf,"%s/%s",
1455 CC->room.QRdirname);
1458 snprintf(buf, sizeof buf, "The room \"%s\" has been edited by %s.\n",
1459 CC->room.QRname, CC->curr_user);
1460 aide_message(buf, "Room modification Message");
1461 cprintf("%d Ok\n", CIT_OK);
1467 * get the name of the room aide for this room
1471 struct ctdluser usbuf;
1473 if (CtdlAccessCheck(ac_logged_in)) return;
1475 if (getuserbynumber(&usbuf, CC->room.QRroomaide) == 0) {
1476 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1478 cprintf("%d \n", CIT_OK);
1484 * set the room aide for this room
1486 void cmd_seta(char *new_ra)
1488 struct ctdluser usbuf;
1493 if (CtdlAccessCheck(ac_room_aide)) return;
1495 if (getuser(&usbuf, new_ra) != 0) {
1498 newu = usbuf.usernum;
1501 lgetroom(&CC->room, CC->room.QRname);
1503 if (CC->room.QRroomaide != newu) {
1506 CC->room.QRroomaide = newu;
1507 lputroom(&CC->room);
1510 * We have to post the change notice _after_ writing changes to
1511 * the room table, otherwise it would deadlock!
1513 if (post_notice == 1) {
1514 if (strlen(usbuf.fullname) > 0)
1515 snprintf(buf, sizeof buf,
1516 "%s is now the room aide for \"%s\".\n",
1517 usbuf.fullname, CC->room.QRname);
1519 snprintf(buf, sizeof buf,
1520 "There is now no room aide for \"%s\".\n",
1522 aide_message(buf, "Aide Room Modification");
1524 cprintf("%d Ok\n", CIT_OK);
1528 * retrieve info file for this room
1536 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_info_dir);
1537 info_fp = fopen(filename, "r");
1539 if (info_fp == NULL) {
1540 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
1543 cprintf("%d Info:\n", LISTING_FOLLOWS);
1544 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1545 if (strlen(buf) > 0)
1546 buf[strlen(buf) - 1] = 0;
1547 cprintf("%s\n", buf);
1554 * Asynchronously schedule a room for deletion. The room will appear
1555 * deleted to the user(s), but it won't actually get purged from the
1556 * database until THE DREADED AUTO-PURGER makes its next run.
1558 void schedule_room_for_deletion(struct ctdlroom *qrbuf)
1560 char old_name[ROOMNAMELEN];
1563 lprintf(CTDL_NOTICE, "Scheduling room <%s> for deletion\n",
1566 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1568 getroom(qrbuf, qrbuf->QRname);
1570 /* Turn the room into a private mailbox owned by a user who doesn't
1571 * exist. This will immediately make the room invisible to everyone,
1572 * and qualify the room for purging.
1574 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1579 qrbuf->QRflags |= QR_MAILBOX;
1580 time(&qrbuf->QRgen); /* Use a timestamp as the new generation number */
1584 b_deleteroom(old_name);
1590 * Back end processing to delete a room and everything associated with it
1591 * (This one is synchronous and should only get called by THE DREADED
1592 * AUTO-PURGER in serv_expire.c. All user-facing code should call
1593 * the asynchronous schedule_room_for_deletion() instead.)
1595 void delete_room(struct ctdlroom *qrbuf)
1599 /* TODO: filename magic? does this realy work? */
1601 lprintf(CTDL_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1603 /* Delete the info file */
1604 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_info_dir);
1607 /* Delete the image file */
1608 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_image_dir);
1611 /* Delete the room's network config file */
1612 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1615 /* Delete the messages in the room
1616 * (Careful: this opens an S_ROOMS critical section!)
1618 CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
1620 /* Flag the room record as not in use */
1621 lgetroom(qrbuf, qrbuf->QRname);
1625 /* then decrement the reference count for the floor */
1626 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1627 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1628 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1630 /* Delete the room record from the database! */
1631 b_deleteroom(qrbuf->QRname);
1637 * Check access control for deleting a room
1639 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1641 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1645 if (is_noneditable(qr)) {
1650 * For mailboxes, check stuff
1652 if (qr->QRflags & QR_MAILBOX) {
1654 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1656 if (atol(qr->QRname) != CC->user.usernum) {
1657 return(0); /* not my room */
1660 /* Can't delete your Mail> room */
1661 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1663 /* Otherwise it's ok */
1668 * For normal rooms, just check for aide or room aide status.
1670 return(is_room_aide());
1674 * aide command: kill the current room
1676 void cmd_kill(char *argbuf)
1678 char deleted_room_name[ROOMNAMELEN];
1682 kill_ok = extract_int(argbuf, 0);
1684 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
1685 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1689 if (CC->room.QRflags & QR_MAILBOX) {
1690 safestrncpy(deleted_room_name, &CC->room.QRname[11], sizeof deleted_room_name);
1693 safestrncpy(deleted_room_name, CC->room.QRname, sizeof deleted_room_name);
1696 /* Do the dirty work */
1697 schedule_room_for_deletion(&CC->room);
1699 /* Return to the Lobby */
1700 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1702 /* tell the world what we did */
1703 snprintf(msg, sizeof msg, "The room \"%s\" has been deleted by %s.\n",
1704 deleted_room_name, CC->curr_user);
1705 aide_message(msg, "Room Purger Message");
1706 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1708 cprintf("%d ok to delete.\n", CIT_OK);
1714 * Internal code to create a new room (returns room flags)
1716 * Room types: 0=public, 1=guessname, 2=passworded, 3=inv-only,
1717 * 4=mailbox, 5=mailbox, but caller supplies namespace
1719 unsigned create_room(char *new_room_name,
1721 char *new_room_pass,
1728 struct ctdlroom qrbuf;
1732 lprintf(CTDL_DEBUG, "create_room(name=%s, type=%d, view=%d)\n",
1733 new_room_name, new_room_type, new_room_view);
1735 if (getroom(&qrbuf, new_room_name) == 0) {
1736 lprintf(CTDL_DEBUG, "%s already exists.\n", new_room_name);
1740 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1741 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1742 qrbuf.QRflags = QR_INUSE;
1743 if (new_room_type > 0)
1744 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1745 if (new_room_type == 1)
1746 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1747 if (new_room_type == 2)
1748 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1749 if ( (new_room_type == 4) || (new_room_type == 5) )
1750 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1752 /* If the user is requesting a personal room, set up the room
1753 * name accordingly (prepend the user number)
1755 if (new_room_type == 4) {
1756 MailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1759 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1762 /* If the room is private, and the system administrator has elected
1763 * to automatically grant room aide privileges, do so now; otherwise,
1764 * set the room aide to undefined.
1766 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1767 qrbuf.QRroomaide = CC->user.usernum;
1769 qrbuf.QRroomaide = (-1L);
1773 * If the caller is only interested in testing whether this will work,
1774 * return now without creating the room.
1776 if (!really_create) return (qrbuf.QRflags);
1778 qrbuf.QRnumber = get_new_room_number();
1779 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1780 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1781 qrbuf.QRfloor = new_room_floor;
1782 qrbuf.QRdefaultview = new_room_view;
1784 /* save what we just did... */
1787 /* bump the reference count on whatever floor the room is on */
1788 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1789 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1790 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1792 /* Grant the creator access to the room unless the avoid_access
1793 * parameter was specified.
1795 if ( (CC->logged_in) && (avoid_access == 0) ) {
1796 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1797 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1798 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1799 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1802 /* resume our happy day */
1803 return (qrbuf.QRflags);
1810 void cmd_cre8(char *args)
1813 char new_room_name[ROOMNAMELEN];
1815 char new_room_pass[32];
1818 char *notification_message = NULL;
1821 int avoid_access = 0;
1823 cre8_ok = extract_int(args, 0);
1824 extract_token(new_room_name, args, 1, '|', sizeof new_room_name);
1825 new_room_name[ROOMNAMELEN - 1] = 0;
1826 new_room_type = extract_int(args, 2);
1827 extract_token(new_room_pass, args, 3, '|', sizeof new_room_pass);
1828 avoid_access = extract_int(args, 5);
1829 new_room_view = extract_int(args, 6);
1830 new_room_pass[9] = 0;
1833 if ((strlen(new_room_name) == 0) && (cre8_ok == 1)) {
1834 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
1838 if (!strcasecmp(new_room_name, MAILROOM)) {
1839 cprintf("%d '%s' already exists.\n",
1840 ERROR + ALREADY_EXISTS, new_room_name);
1844 if (num_parms(args) >= 5) {
1845 fl = cgetfloor(extract_int(args, 4));
1847 cprintf("%d Invalid floor number.\n",
1848 ERROR + INVALID_FLOOR_OPERATION);
1851 else if ((fl->f_flags & F_INUSE) == 0) {
1852 cprintf("%d Invalid floor number.\n",
1853 ERROR + INVALID_FLOOR_OPERATION);
1856 new_room_floor = extract_int(args, 4);
1860 if (CtdlAccessCheck(ac_logged_in)) return;
1862 if (CC->user.axlevel < config.c_createax || CC->internal_pgm) {
1863 cprintf("%d You need higher access to create rooms.\n",
1864 ERROR + HIGHER_ACCESS_REQUIRED);
1868 if ((strlen(new_room_name) == 0) && (cre8_ok == 0)) {
1869 cprintf("%d Ok to create rooms.\n", CIT_OK);
1873 if ((new_room_type < 0) || (new_room_type > 5)) {
1874 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
1878 if (new_room_type == 5) {
1879 if (CC->user.axlevel < 6) {
1880 cprintf("%d Higher access required\n",
1881 ERROR + HIGHER_ACCESS_REQUIRED);
1886 /* Check to make sure the requested room name doesn't already exist */
1887 newflags = create_room(new_room_name,
1888 new_room_type, new_room_pass, new_room_floor,
1889 0, avoid_access, new_room_view);
1890 if (newflags == 0) {
1891 cprintf("%d '%s' already exists.\n",
1892 ERROR + ALREADY_EXISTS, new_room_name);
1897 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
1901 /* If we reach this point, the room needs to be created. */
1903 newflags = create_room(new_room_name,
1904 new_room_type, new_room_pass, new_room_floor, 1, 0,
1907 /* post a message in Aide> describing the new room */
1908 notification_message = malloc(1024);
1909 snprintf(notification_message, 1024,
1910 "A new room called \"%s\" has been created by %s%s%s%s%s%s\n",
1913 ((newflags & QR_MAILBOX) ? " [personal]" : ""),
1914 ((newflags & QR_PRIVATE) ? " [private]" : ""),
1915 ((newflags & QR_GUESSNAME) ? " [hidden]" : ""),
1916 ((newflags & QR_PASSWORDED) ? " Password: " : ""),
1917 ((newflags & QR_PASSWORDED) ? new_room_pass : "")
1919 aide_message(notification_message, "Room Creation Message");
1920 free(notification_message);
1922 cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
1927 void cmd_einf(char *ok)
1928 { /* enter info file for current room */
1930 char infofilename[SIZ];
1935 if (CtdlAccessCheck(ac_room_aide)) return;
1937 if (atoi(ok) == 0) {
1938 cprintf("%d Ok.\n", CIT_OK);
1941 assoc_file_name(infofilename, sizeof infofilename, &CC->room, ctdl_info_dir);
1942 lprintf(CTDL_DEBUG, "opening\n");
1943 fp = fopen(infofilename, "w");
1944 lprintf(CTDL_DEBUG, "checking\n");
1946 cprintf("%d Cannot open %s: %s\n",
1947 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1950 cprintf("%d Send info...\n", SEND_LISTING);
1953 client_getln(buf, sizeof buf);
1954 if (strcmp(buf, "000"))
1955 fprintf(fp, "%s\n", buf);
1956 } while (strcmp(buf, "000"));
1959 /* now update the room index so people will see our new info */
1960 lgetroom(&CC->room, CC->room.QRname); /* lock so no one steps on us */
1961 CC->room.QRinfo = CC->room.QRhighest + 1L;
1962 lputroom(&CC->room);
1967 * cmd_lflr() - List all known floors
1974 if (CtdlAccessCheck(ac_logged_in)) return;
1976 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1978 for (a = 0; a < MAXFLOORS; ++a) {
1979 getfloor(&flbuf, a);
1980 if (flbuf.f_flags & F_INUSE) {
1981 cprintf("%d|%s|%d\n",
1993 * create a new floor
1995 void cmd_cflr(char *argbuf)
1997 char new_floor_name[256];
2000 int free_slot = (-1);
2003 extract_token(new_floor_name, argbuf, 0, '|', sizeof new_floor_name);
2004 cflr_ok = extract_int(argbuf, 1);
2006 if (CtdlAccessCheck(ac_aide)) return;
2008 if (strlen(new_floor_name) == 0) {
2009 cprintf("%d Blank floor name not allowed.\n",
2010 ERROR + ILLEGAL_VALUE);
2014 for (a = 0; a < MAXFLOORS; ++a) {
2015 getfloor(&flbuf, a);
2017 /* note any free slots while we're scanning... */
2018 if (((flbuf.f_flags & F_INUSE) == 0)
2022 /* check to see if it already exists */
2023 if ((!strcasecmp(flbuf.f_name, new_floor_name))
2024 && (flbuf.f_flags & F_INUSE)) {
2025 cprintf("%d Floor '%s' already exists.\n",
2026 ERROR + ALREADY_EXISTS,
2032 if (free_slot < 0) {
2033 cprintf("%d There is no space available for a new floor.\n",
2034 ERROR + INVALID_FLOOR_OPERATION);
2038 cprintf("%d ok to create...\n", CIT_OK);
2041 lgetfloor(&flbuf, free_slot);
2042 flbuf.f_flags = F_INUSE;
2043 flbuf.f_ref_count = 0;
2044 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
2045 lputfloor(&flbuf, free_slot);
2046 cprintf("%d %d\n", CIT_OK, free_slot);
2054 void cmd_kflr(char *argbuf)
2057 int floor_to_delete;
2061 floor_to_delete = extract_int(argbuf, 0);
2062 kflr_ok = extract_int(argbuf, 1);
2064 if (CtdlAccessCheck(ac_aide)) return;
2066 lgetfloor(&flbuf, floor_to_delete);
2069 if ((flbuf.f_flags & F_INUSE) == 0) {
2070 cprintf("%d Floor %d not in use.\n",
2071 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
2074 if (flbuf.f_ref_count != 0) {
2075 cprintf("%d Cannot delete; floor contains %d rooms.\n",
2076 ERROR + INVALID_FLOOR_OPERATION,
2081 cprintf("%d Ok\n", CIT_OK);
2083 cprintf("%d Ok to delete...\n", CIT_OK);
2090 if ((delete_ok == 1) && (kflr_ok == 1))
2092 lputfloor(&flbuf, floor_to_delete);
2098 void cmd_eflr(char *argbuf)
2104 np = num_parms(argbuf);
2106 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
2110 if (CtdlAccessCheck(ac_aide)) return;
2112 floor_num = extract_int(argbuf, 0);
2113 lgetfloor(&flbuf, floor_num);
2114 if ((flbuf.f_flags & F_INUSE) == 0) {
2115 lputfloor(&flbuf, floor_num);
2116 cprintf("%d Floor %d is not in use.\n",
2117 ERROR + INVALID_FLOOR_OPERATION, floor_num);
2121 extract_token(flbuf.f_name, argbuf, 1, '|', sizeof flbuf.f_name);
2122 lputfloor(&flbuf, floor_num);
2124 cprintf("%d Ok\n", CIT_OK);