4 * Server functions which perform operations on room objects.
15 #include <dirent.h> /* for cmd_rdir to read contents of the directory */
17 #if TIME_WITH_SYS_TIME
18 # include <sys/time.h>
22 # include <sys/time.h>
31 #include <libcitadel.h>
36 #include "sysdep_decls.h"
40 #include "citserver.h"
42 #include "citadel_dirs.h"
44 struct floor *floorcache[MAXFLOORS];
47 * Retrieve access control information for any user/room pair
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;
134 /* If "collaborative deletion" is active for this room, any user who can post
135 * is also allowed to delete
137 if (CC->room.QRflags2 & QR2_COLLABDEL) {
138 if (retval & UA_POSTALLOWED) {
139 retval = retval | UA_DELETEALLOWED;
145 /* Check to see if the user has forgotten this room */
146 if (vbuf.v_flags & V_FORGET) {
147 retval = retval & ~UA_KNOWN;
148 if ( ( ((roombuf->QRflags & QR_PRIVATE) == 0)
149 && ((roombuf->QRflags & QR_MAILBOX) == 0) )
150 || ( (roombuf->QRflags & QR_MAILBOX)
151 && (atol(roombuf->QRname) == CC->user.usernum))) {
152 retval = retval | UA_ZAPPED;
155 /* If user is explicitly locked out of this room, deny everything */
156 if (vbuf.v_flags & V_LOCKOUT) {
157 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED & ~UA_POSTALLOWED;
160 /* Aides get access to all private rooms */
161 if ( (userbuf->axlevel >= 6)
162 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
163 if (vbuf.v_flags & V_FORGET) {
164 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED;
167 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED;
171 /* Aides can gain access to mailboxes as well, but they don't show
174 if ( (userbuf->axlevel >= 6)
175 && (roombuf->QRflags & QR_MAILBOX) ) {
176 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED;
179 /* Aides and Room Aides have admin privileges */
180 if ( (userbuf->axlevel >= 6)
181 || (userbuf->usernum == roombuf->QRroomaide)
183 retval = retval | UA_ADMINALLOWED | UA_DELETEALLOWED;
186 NEWMSG: /* By the way, we also check for the presence of new messages */
187 if (is_msg_in_sequence_set(vbuf.v_seen, roombuf->QRhighest) == 0) {
188 retval = retval | UA_HASNEWMSGS;
191 /* System rooms never show up in the list. */
192 if (roombuf->QRflags2 & QR2_SYSTEM) {
193 retval = retval & ~UA_KNOWN;
197 /* Now give the caller the information it wants. */
198 if (result != NULL) *result = retval;
199 if (view != NULL) *view = vbuf.v_view;
203 * Self-checking stuff for a room record read into memory
205 void room_sanity_check(struct ctdlroom *qrbuf)
207 /* Mailbox rooms are always on the lowest floor */
208 if (qrbuf->QRflags & QR_MAILBOX) {
211 /* Listing order of 0 is illegal except for base rooms */
212 if (qrbuf->QRorder == 0)
213 if (!(qrbuf->QRflags & QR_MAILBOX) &&
214 strncasecmp(qrbuf->QRname, config.c_baseroom, ROOMNAMELEN)
216 strncasecmp(qrbuf->QRname, config.c_aideroom, ROOMNAMELEN))
222 * getroom() - retrieve room data from disk
224 int getroom(struct ctdlroom *qrbuf, char *room_name)
226 struct cdbdata *cdbqr;
227 char lowercase_name[ROOMNAMELEN];
228 char personal_lowercase_name[ROOMNAMELEN];
229 char *dptr, *sptr, *eptr;
231 dptr = lowercase_name;
233 eptr = (dptr + (sizeof lowercase_name - 1));
234 while (!IsEmptyStr(sptr) && (dptr < eptr)){
235 *dptr = tolower(*sptr);
240 memset(qrbuf, 0, sizeof(struct ctdlroom));
242 /* First, try the public namespace */
243 cdbqr = cdb_fetch(CDB_ROOMS,
244 lowercase_name, strlen(lowercase_name));
246 /* If that didn't work, try the user's personal namespace */
248 snprintf(personal_lowercase_name,
249 sizeof personal_lowercase_name, "%010ld.%s",
250 CC->user.usernum, lowercase_name);
251 cdbqr = cdb_fetch(CDB_ROOMS,
252 personal_lowercase_name,
253 strlen(personal_lowercase_name));
256 memcpy(qrbuf, cdbqr->ptr,
257 ((cdbqr->len > sizeof(struct ctdlroom)) ?
258 sizeof(struct ctdlroom) : cdbqr->len));
261 room_sanity_check(qrbuf);
270 * lgetroom() - same as getroom() but locks the record (if supported)
272 int lgetroom(struct ctdlroom *qrbuf, char *room_name)
275 retval = getroom(qrbuf, room_name);
276 if (retval == 0) begin_critical_section(S_ROOMS);
282 * b_putroom() - back end to putroom() and b_deleteroom()
283 * (if the supplied buffer is NULL, delete the room record)
285 void b_putroom(struct ctdlroom *qrbuf, char *room_name)
287 char lowercase_name[ROOMNAMELEN];
292 bptr = lowercase_name;
293 while (!IsEmptyStr(aptr))
295 *bptr = tolower(*aptr);
301 len = bptr - lowercase_name;
303 cdb_delete(CDB_ROOMS,
304 lowercase_name, len);
306 time(&qrbuf->QRmtime);
309 qrbuf, sizeof(struct ctdlroom));
315 * putroom() - store room data to disk
317 void putroom(struct ctdlroom *qrbuf) {
318 b_putroom(qrbuf, qrbuf->QRname);
323 * b_deleteroom() - delete a room record from disk
325 void b_deleteroom(char *room_name) {
326 b_putroom(NULL, room_name);
332 * lputroom() - same as putroom() but unlocks the record (if supported)
334 void lputroom(struct ctdlroom *qrbuf)
338 end_critical_section(S_ROOMS);
342 /****************************************************************************/
345 * getfloor() - retrieve floor data from disk
347 void getfloor(struct floor *flbuf, int floor_num)
349 struct cdbdata *cdbfl;
351 memset(flbuf, 0, sizeof(struct floor));
352 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
354 memcpy(flbuf, cdbfl->ptr,
355 ((cdbfl->len > sizeof(struct floor)) ?
356 sizeof(struct floor) : cdbfl->len));
359 if (floor_num == 0) {
360 safestrncpy(flbuf->f_name, "Main Floor",
361 sizeof flbuf->f_name);
362 flbuf->f_flags = F_INUSE;
363 flbuf->f_ref_count = 3;
370 * lgetfloor() - same as getfloor() but locks the record (if supported)
372 void lgetfloor(struct floor *flbuf, int floor_num)
375 begin_critical_section(S_FLOORTAB);
376 getfloor(flbuf, floor_num);
381 * cgetfloor() - Get floor record from *cache* (loads from disk if needed)
383 * This is strictly a performance hack.
385 struct floor *cgetfloor(int floor_num) {
386 static int initialized = 0;
389 struct floor *fl = NULL;
391 begin_critical_section(S_FLOORCACHE);
392 if (initialized == 0) {
393 for (i=0; i<MAXFLOORS; ++i) {
394 floorcache[floor_num] = NULL;
398 if (floorcache[floor_num] == NULL) {
401 end_critical_section(S_FLOORCACHE);
404 fl = malloc(sizeof(struct floor));
405 getfloor(fl, floor_num);
406 begin_critical_section(S_FLOORCACHE);
407 if (floorcache[floor_num] != NULL) {
408 free(floorcache[floor_num]);
410 floorcache[floor_num] = fl;
411 end_critical_section(S_FLOORCACHE);
414 return(floorcache[floor_num]);
420 * putfloor() - store floor data on disk
422 void putfloor(struct floor *flbuf, int floor_num)
424 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
425 begin_critical_section(S_FLOORCACHE);
426 if (floorcache[floor_num] != NULL) {
427 free(floorcache[floor_num]);
428 floorcache[floor_num] = malloc(sizeof(struct floor));
429 memcpy(floorcache[floor_num], flbuf, sizeof(struct floor));
431 end_critical_section(S_FLOORCACHE);
433 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
434 flbuf, sizeof(struct floor));
439 * lputfloor() - same as putfloor() but unlocks the record (if supported)
441 void lputfloor(struct floor *flbuf, int floor_num)
444 putfloor(flbuf, floor_num);
445 end_critical_section(S_FLOORTAB);
451 * Traverse the room file...
453 void ForEachRoom(void (*CallBack) (struct ctdlroom *EachRoom, void *out_data),
456 struct ctdlroom qrbuf;
457 struct cdbdata *cdbqr;
459 cdb_rewind(CDB_ROOMS);
461 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
462 memset(&qrbuf, 0, sizeof(struct ctdlroom));
463 memcpy(&qrbuf, cdbqr->ptr,
464 ((cdbqr->len > sizeof(struct ctdlroom)) ?
465 sizeof(struct ctdlroom) : cdbqr->len));
467 room_sanity_check(&qrbuf);
468 if (qrbuf.QRflags & QR_INUSE)
469 (*CallBack)(&qrbuf, in_data);
475 * delete_msglist() - delete room message pointers
477 void delete_msglist(struct ctdlroom *whichroom)
479 struct cdbdata *cdbml;
481 /* Make sure the msglist we're deleting actually exists, otherwise
482 * gdbm will complain when we try to delete an invalid record
484 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
488 /* Go ahead and delete it */
489 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
497 * sort message pointers
498 * (returns new msg count)
500 int sort_msglist(long listptrs[], int oldcount)
511 for (a = numitems - 2; a >= 0; --a) {
512 for (b = 0; b <= a; ++b) {
513 if (listptrs[b] > (listptrs[b + 1])) {
515 hold2 = listptrs[b + 1];
517 listptrs[b + 1] = hold1;
522 /* and yank any nulls */
523 while ((numitems > 0) && (listptrs[0] == 0L)) {
524 memmove(&listptrs[0], &listptrs[1],
525 (sizeof(long) * (numitems - 1)));
534 * Determine whether a given room is non-editable.
536 int is_noneditable(struct ctdlroom *qrbuf)
539 /* Mail> rooms are non-editable */
540 if ( (qrbuf->QRflags & QR_MAILBOX)
541 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
544 /* Everything else is editable */
551 * Back-back-end for all room listing commands
553 void list_roomname(struct ctdlroom *qrbuf, int ra, int current_view, int default_view)
555 char truncated_roomname[ROOMNAMELEN];
557 /* For my own mailbox rooms, chop off the owner prefix */
558 if ( (qrbuf->QRflags & QR_MAILBOX)
559 && (atol(qrbuf->QRname) == CC->user.usernum) ) {
560 safestrncpy(truncated_roomname, qrbuf->QRname, sizeof truncated_roomname);
561 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
562 cprintf("%s", truncated_roomname);
564 /* For all other rooms, just display the name in its entirety */
566 cprintf("%s", qrbuf->QRname);
569 /* ...and now the other parameters */
570 cprintf("|%u|%d|%d|%d|%d|%d|%d|%ld|\n",
572 (int) qrbuf->QRfloor,
573 (int) qrbuf->QRorder,
574 (int) qrbuf->QRflags2,
584 * cmd_lrms() - List all accessible rooms, known or forgotten
586 void cmd_lrms_backend(struct ctdlroom *qrbuf, void *data)
588 int FloorBeingSearched = (-1);
592 FloorBeingSearched = *(int *)data;
593 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
595 if ((( ra & (UA_KNOWN | UA_ZAPPED)))
596 && ((qrbuf->QRfloor == (FloorBeingSearched))
597 || ((FloorBeingSearched) < 0)))
598 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
601 void cmd_lrms(char *argbuf)
603 int FloorBeingSearched = (-1);
604 if (!IsEmptyStr(argbuf))
605 FloorBeingSearched = extract_int(argbuf, 0);
607 if (CtdlAccessCheck(ac_logged_in)) return;
609 if (getuser(&CC->user, CC->curr_user)) {
610 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
613 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
615 ForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
622 * cmd_lkra() - List all known rooms
624 void cmd_lkra_backend(struct ctdlroom *qrbuf, void *data)
626 int FloorBeingSearched = (-1);
630 FloorBeingSearched = *(int *)data;
631 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
633 if ((( ra & (UA_KNOWN)))
634 && ((qrbuf->QRfloor == (FloorBeingSearched))
635 || ((FloorBeingSearched) < 0)))
636 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
639 void cmd_lkra(char *argbuf)
641 int FloorBeingSearched = (-1);
642 if (!IsEmptyStr(argbuf))
643 FloorBeingSearched = extract_int(argbuf, 0);
645 if (CtdlAccessCheck(ac_logged_in)) return;
647 if (getuser(&CC->user, CC->curr_user)) {
648 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
651 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
653 ForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
659 void cmd_lprm_backend(struct ctdlroom *qrbuf, void *data)
661 int FloorBeingSearched = (-1);
665 FloorBeingSearched = *(int *)data;
666 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
668 if ( ((qrbuf->QRflags & QR_PRIVATE) == 0)
669 && ((qrbuf->QRflags & QR_MAILBOX) == 0)
670 && ((qrbuf->QRfloor == (FloorBeingSearched))
671 || ((FloorBeingSearched) < 0)))
672 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
675 void cmd_lprm(char *argbuf)
677 int FloorBeingSearched = (-1);
678 if (!IsEmptyStr(argbuf))
679 FloorBeingSearched = extract_int(argbuf, 0);
681 cprintf("%d Publiic rooms:\n", LISTING_FOLLOWS);
683 ForEachRoom(cmd_lprm_backend, &FloorBeingSearched);
690 * cmd_lkrn() - List all known rooms with new messages
692 void cmd_lkrn_backend(struct ctdlroom *qrbuf, void *data)
694 int FloorBeingSearched = (-1);
698 FloorBeingSearched = *(int *)data;
699 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
702 && (ra & UA_HASNEWMSGS)
703 && ((qrbuf->QRfloor == (FloorBeingSearched))
704 || ((FloorBeingSearched) < 0)))
705 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
708 void cmd_lkrn(char *argbuf)
710 int FloorBeingSearched = (-1);
711 if (!IsEmptyStr(argbuf))
712 FloorBeingSearched = extract_int(argbuf, 0);
714 if (CtdlAccessCheck(ac_logged_in)) return;
716 if (getuser(&CC->user, CC->curr_user)) {
717 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
720 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
722 ForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
729 * cmd_lkro() - List all known rooms
731 void cmd_lkro_backend(struct ctdlroom *qrbuf, void *data)
733 int FloorBeingSearched = (-1);
737 FloorBeingSearched = *(int *)data;
738 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
741 && ((ra & UA_HASNEWMSGS) == 0)
742 && ((qrbuf->QRfloor == (FloorBeingSearched))
743 || ((FloorBeingSearched) < 0)))
744 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
747 void cmd_lkro(char *argbuf)
749 int FloorBeingSearched = (-1);
750 if (!IsEmptyStr(argbuf))
751 FloorBeingSearched = extract_int(argbuf, 0);
753 if (CtdlAccessCheck(ac_logged_in)) return;
755 if (getuser(&CC->user, CC->curr_user)) {
756 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
759 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
761 ForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
768 * cmd_lzrm() - List all forgotten rooms
770 void cmd_lzrm_backend(struct ctdlroom *qrbuf, void *data)
772 int FloorBeingSearched = (-1);
776 FloorBeingSearched = *(int *)data;
777 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
779 if ((ra & UA_GOTOALLOWED)
781 && ((qrbuf->QRfloor == (FloorBeingSearched))
782 || ((FloorBeingSearched) < 0)))
783 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
786 void cmd_lzrm(char *argbuf)
788 int FloorBeingSearched = (-1);
789 if (!IsEmptyStr(argbuf))
790 FloorBeingSearched = extract_int(argbuf, 0);
792 if (CtdlAccessCheck(ac_logged_in)) return;
794 if (getuser(&CC->user, CC->curr_user)) {
795 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
798 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
800 ForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
806 * Make the specified room the current room for this session. No validation
807 * or access control is done here -- the caller should make sure that the
808 * specified room exists and is ok to access.
810 void usergoto(char *where, int display_result, int transiently,
811 int *retmsgs, int *retnew)
814 int new_messages = 0;
815 int old_messages = 0;
816 int total_messages = 0;
820 int newmailcount = 0;
822 char truncated_roomname[ROOMNAMELEN];
823 struct cdbdata *cdbfr;
824 long *msglist = NULL;
826 unsigned int original_v_flags;
829 char setstr[128], lostr[64], histr[64];
833 /* If the supplied room name is NULL, the caller wants us to know that
834 * it has already copied the room record into CC->room, so
835 * we can skip the extra database fetch.
838 safestrncpy(CC->room.QRname, where, sizeof CC->room.QRname);
839 getroom(&CC->room, where);
842 /* Take care of all the formalities. */
844 begin_critical_section(S_USERS);
845 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
846 original_v_flags = vbuf.v_flags;
848 /* Know the room ... but not if it's the page log room, or if the
849 * caller specified that we're only entering this room transiently.
851 if ((strcasecmp(CC->room.QRname, config.c_logpages))
852 && (transiently == 0) ) {
853 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
854 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
857 /* Only rewrite the database record if we changed something */
858 if (vbuf.v_flags != original_v_flags) {
859 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
861 end_critical_section(S_USERS);
863 /* Check for new mail */
864 newmailcount = NewMailCount();
866 /* set info to 1 if the user needs to read the room's info file */
867 if (CC->room.QRinfo > vbuf.v_lastseen) {
871 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
873 msglist = (long *) cdbfr->ptr;
874 cdbfr->ptr = NULL; /* usergoto() now owns this memory */
875 num_msgs = cdbfr->len / sizeof(long);
880 for (a=0; a<num_msgs; ++a) {
881 if (msglist[a] > 0L) ++total_messages;
883 new_messages = num_msgs;
884 num_sets = num_tokens(vbuf.v_seen, ',');
885 for (s=0; s<num_sets; ++s) {
886 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
888 extract_token(lostr, setstr, 0, ':', sizeof lostr);
889 if (num_tokens(setstr, ':') >= 2) {
890 extract_token(histr, setstr, 1, ':', sizeof histr);
891 if (!strcmp(histr, "*")) {
892 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
896 strcpy(histr, lostr);
901 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
902 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
908 new_messages = total_messages - old_messages;
910 if (msglist != NULL) free(msglist);
912 if (CC->room.QRflags & QR_MAILBOX)
917 if ((CC->room.QRroomaide == CC->user.usernum)
918 || (CC->user.axlevel >= 6))
923 safestrncpy(truncated_roomname, CC->room.QRname, sizeof truncated_roomname);
924 if ( (CC->room.QRflags & QR_MAILBOX)
925 && (atol(CC->room.QRname) == CC->user.usernum) ) {
926 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
929 if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
933 if (retmsgs != NULL) *retmsgs = total_messages;
934 if (retnew != NULL) *retnew = new_messages;
935 lprintf(CTDL_DEBUG, "<%s> %d new of %d total messages\n",
937 new_messages, total_messages
940 CC->curr_view = (int)vbuf.v_view;
942 if (display_result) {
943 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|%d|\n",
944 CIT_OK, CtdlCheckExpress(),
949 (int)CC->room.QRflags,
950 (long)CC->room.QRhighest,
951 (long)vbuf.v_lastseen,
955 (int)CC->room.QRfloor,
957 (int)CC->room.QRdefaultview,
959 (int)CC->room.QRflags2
966 * Handle some of the macro named rooms
968 void convert_room_name_macros(char *towhere, size_t maxlen) {
969 if (!strcasecmp(towhere, "_BASEROOM_")) {
970 safestrncpy(towhere, config.c_baseroom, maxlen);
972 else if (!strcasecmp(towhere, "_MAIL_")) {
973 safestrncpy(towhere, MAILROOM, maxlen);
975 else if (!strcasecmp(towhere, "_TRASH_")) {
976 safestrncpy(towhere, USERTRASHROOM, maxlen);
978 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
979 safestrncpy(towhere, config.c_twitroom, maxlen);
981 else if (!strcasecmp(towhere, "_CALENDAR_")) {
982 safestrncpy(towhere, USERCALENDARROOM, maxlen);
984 else if (!strcasecmp(towhere, "_TASKS_")) {
985 safestrncpy(towhere, USERTASKSROOM, maxlen);
987 else if (!strcasecmp(towhere, "_CONTACTS_")) {
988 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
990 else if (!strcasecmp(towhere, "_NOTES_")) {
991 safestrncpy(towhere, USERNOTESROOM, maxlen);
997 * cmd_goto() - goto a new room
999 void cmd_goto(char *gargs)
1001 struct ctdlroom QRscratch;
1005 char augmented_roomname[ROOMNAMELEN];
1006 char towhere[ROOMNAMELEN];
1008 int transiently = 0;
1010 if (CtdlAccessCheck(ac_logged_in)) return;
1012 extract_token(towhere, gargs, 0, '|', sizeof towhere);
1013 extract_token(password, gargs, 1, '|', sizeof password);
1014 transiently = extract_int(gargs, 2);
1016 getuser(&CC->user, CC->curr_user);
1019 * Handle some of the macro named rooms
1021 convert_room_name_macros(towhere, sizeof towhere);
1023 /* First try a regular match */
1024 c = getroom(&QRscratch, towhere);
1026 /* Then try a mailbox name match */
1028 MailboxName(augmented_roomname, sizeof augmented_roomname,
1029 &CC->user, towhere);
1030 c = getroom(&QRscratch, augmented_roomname);
1032 safestrncpy(towhere, augmented_roomname, sizeof towhere);
1035 /* And if the room was found... */
1038 /* Let internal programs go directly to any room. */
1039 if (CC->internal_pgm) {
1040 memcpy(&CC->room, &QRscratch,
1041 sizeof(struct ctdlroom));
1042 usergoto(NULL, 1, transiently, NULL, NULL);
1046 /* See if there is an existing user/room relationship */
1047 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
1049 /* normal clients have to pass through security */
1050 if (ra & UA_GOTOALLOWED) {
1055 if ((QRscratch.QRflags & QR_MAILBOX) &&
1056 ((ra & UA_GOTOALLOWED))) {
1057 memcpy(&CC->room, &QRscratch,
1058 sizeof(struct ctdlroom));
1059 usergoto(NULL, 1, transiently, NULL, NULL);
1061 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
1062 ((ra & UA_KNOWN) == 0) &&
1063 (strcasecmp(QRscratch.QRpasswd, password)) &&
1064 (CC->user.axlevel < 6)
1066 cprintf("%d wrong or missing passwd\n",
1067 ERROR + PASSWORD_REQUIRED);
1069 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
1070 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
1071 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
1072 ((ra & UA_KNOWN) == 0) &&
1073 (CC->user.axlevel < 6)
1075 lprintf(CTDL_DEBUG, "Failed to acquire private room\n");
1077 memcpy(&CC->room, &QRscratch,
1078 sizeof(struct ctdlroom));
1079 usergoto(NULL, 1, transiently, NULL, NULL);
1085 cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
1091 struct ctdluser temp;
1092 struct cdbdata *cdbus;
1095 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
1096 cdb_rewind(CDB_USERS);
1097 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1098 memset(&temp, 0, sizeof temp);
1099 memcpy(&temp, cdbus->ptr, sizeof temp);
1102 CtdlRoomAccess(&CC->room, &temp, &ra, NULL);
1103 if ((CC->room.QRflags & QR_INUSE)
1106 cprintf("%s\n", temp.fullname);
1113 * RDIR command for room directory
1120 struct stat statbuf;
1121 DIR *filedir = NULL;
1122 struct dirent *filedir_entry;
1126 if (CtdlAccessCheck(ac_logged_in)) return;
1128 getroom(&CC->room, CC->room.QRname);
1129 getuser(&CC->user, CC->curr_user);
1131 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
1132 cprintf("%d not here.\n", ERROR + NOT_HERE);
1135 if (((CC->room.QRflags & QR_VISDIR) == 0)
1136 && (CC->user.axlevel < 6)
1137 && (CC->user.usernum != CC->room.QRroomaide)) {
1138 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1141 cprintf("%d %s|%s/%s\n", LISTING_FOLLOWS, config.c_fqdn, ctdl_file_dir, CC->room.QRdirname);
1143 snprintf(buf, sizeof buf, "%s/%s", ctdl_file_dir, CC->room.QRdirname);
1144 filedir = opendir (buf);
1147 snprintf(buf, sizeof buf, "%s/%s/filedir", ctdl_file_dir, CC->room.QRdirname);
1148 fd = fopen(buf, "r");
1150 fd = fopen("/dev/null", "r");
1151 while ((filedir_entry = readdir(filedir)))
1153 if (strcasecmp(filedir_entry->d_name, "filedir") && filedir_entry->d_name[0] != '.')
1155 #ifdef _DIRENT_HAVE_D_NAMELEN
1156 d_namelen = filedir_entry->d_namelen;
1158 d_namelen = strlen(filedir_entry->d_name);
1160 snprintf(buf, sizeof buf, "%s/%s/%s", ctdl_file_dir, CC->room.QRdirname, filedir_entry->d_name);
1161 stat(buf, &statbuf); /* stat the file */
1162 if (!(statbuf.st_mode & S_IFREG))
1164 snprintf(buf2, sizeof buf2, "Command RDIR found something that is not a useable file. It should be cleaned up.\n RDIR found this non regular file:\n%s\n", buf);
1165 aide_message(buf2, "RDIR found bad file");
1166 continue; /* not a useable file type so don't show it */
1168 safestrncpy(comment, "", sizeof comment);
1169 fseek(fd, 0L, 0); /* rewind descriptions file */
1170 /* Get the description from the descriptions file */
1171 while ((fgets(buf, sizeof buf, fd) != NULL) && (IsEmptyStr(comment)))
1173 buf[strlen(buf) - 1] = 0;
1174 if ((!strncasecmp(buf, filedir_entry->d_name, d_namelen)) && (buf[d_namelen] == ' '))
1175 safestrncpy(comment, &buf[d_namelen + 1], sizeof comment);
1177 cprintf("%s|%ld|%s\n", filedir_entry->d_name, (long)statbuf.st_size, comment);
1187 * get room parameters (aide or room aide command)
1191 if (CtdlAccessCheck(ac_room_aide)) return;
1193 getroom(&CC->room, CC->room.QRname);
1194 cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1198 ((CC->room.QRflags & QR_MAILBOX) ?
1199 &CC->room.QRname[11] : CC->room.QRname),
1201 ((CC->room.QRflags & QR_PASSWORDED) ?
1202 CC->room.QRpasswd : ""),
1204 ((CC->room.QRflags & QR_DIRECTORY) ?
1205 CC->room.QRdirname : ""),
1208 (int) CC->room.QRfloor,
1209 (int) CC->room.QRorder,
1211 CC->room.QRdefaultview,
1218 * Back end function to rename a room.
1219 * You can also specify which floor to move the room to, or specify -1 to
1220 * keep the room on the same floor it was on.
1222 * If you are renaming a mailbox room, you must supply the namespace prefix
1223 * in *at least* the old name!
1225 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1227 struct ctdlroom qrbuf;
1228 struct ctdlroom qrtmp;
1233 char actual_old_name[ROOMNAMELEN];
1235 lprintf(CTDL_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1236 old_name, new_name, new_floor);
1238 if (new_floor >= 0) {
1239 fl = cgetfloor(new_floor);
1240 if ((fl->f_flags & F_INUSE) == 0) {
1241 return(crr_invalid_floor);
1245 begin_critical_section(S_ROOMS);
1247 if ( (getroom(&qrtmp, new_name) == 0)
1248 && (strcasecmp(new_name, old_name)) ) {
1249 ret = crr_already_exists;
1252 else if (getroom(&qrbuf, old_name) != 0) {
1253 ret = crr_room_not_found;
1256 else if ( (CC->user.axlevel < 6) && (!CC->internal_pgm)
1257 && (CC->user.usernum != qrbuf.QRroomaide)
1258 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) ) {
1259 ret = crr_access_denied;
1262 else if (is_noneditable(&qrbuf)) {
1263 ret = crr_noneditable;
1268 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1269 if (qrbuf.QRflags & QR_MAILBOX) {
1270 owner = atol(qrbuf.QRname);
1272 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1273 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1274 "%010ld.%s", owner, new_name);
1277 safestrncpy(qrbuf.QRname, new_name,
1278 sizeof(qrbuf.QRname));
1281 /* Reject change of floor for baseroom/aideroom */
1282 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1283 !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1287 /* Take care of floor stuff */
1288 old_floor = qrbuf.QRfloor;
1289 if (new_floor < 0) {
1290 new_floor = old_floor;
1292 qrbuf.QRfloor = new_floor;
1295 begin_critical_section(S_CONFIG);
1297 /* If baseroom/aideroom name changes, update config */
1298 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1299 safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1302 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1303 safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1307 end_critical_section(S_CONFIG);
1309 /* If the room name changed, then there are now two room
1310 * records, so we have to delete the old one.
1312 if (strcasecmp(new_name, old_name)) {
1313 b_deleteroom(actual_old_name);
1319 end_critical_section(S_ROOMS);
1321 /* Adjust the floor reference counts if necessary */
1322 if (new_floor != old_floor) {
1323 lgetfloor(&flbuf, old_floor);
1324 --flbuf.f_ref_count;
1325 lputfloor(&flbuf, old_floor);
1326 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1327 lgetfloor(&flbuf, new_floor);
1328 ++flbuf.f_ref_count;
1329 lputfloor(&flbuf, new_floor);
1330 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1333 /* ...and everybody say "YATTA!" */
1339 * set room parameters (aide or room aide command)
1341 void cmd_setr(char *args)
1347 char new_name[ROOMNAMELEN];
1349 if (CtdlAccessCheck(ac_logged_in)) return;
1351 if (num_parms(args) >= 6) {
1352 new_floor = extract_int(args, 5);
1354 new_floor = (-1); /* don't change the floor */
1357 /* When is a new name more than just a new name? When the old name
1358 * has a namespace prefix.
1360 if (CC->room.QRflags & QR_MAILBOX) {
1361 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
1363 safestrncpy(new_name, "", sizeof new_name);
1365 extract_token(&new_name[strlen(new_name)], args, 0, '|', (sizeof new_name - strlen(new_name)));
1367 r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
1369 if (r == crr_room_not_found) {
1370 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
1371 } else if (r == crr_already_exists) {
1372 cprintf("%d '%s' already exists.\n",
1373 ERROR + ALREADY_EXISTS, new_name);
1374 } else if (r == crr_noneditable) {
1375 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
1376 } else if (r == crr_invalid_floor) {
1377 cprintf("%d Target floor does not exist.\n",
1378 ERROR + INVALID_FLOOR_OPERATION);
1379 } else if (r == crr_access_denied) {
1380 cprintf("%d You do not have permission to edit '%s'\n",
1381 ERROR + HIGHER_ACCESS_REQUIRED,
1383 } else if (r != crr_ok) {
1384 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1385 ERROR + INTERNAL_ERROR, r);
1392 getroom(&CC->room, new_name);
1394 /* Now we have to do a bunch of other stuff */
1396 if (num_parms(args) >= 7) {
1397 new_order = extract_int(args, 6);
1400 if (new_order > 127)
1404 lgetroom(&CC->room, CC->room.QRname);
1406 /* Directory room */
1407 extract_token(buf, args, 2, '|', sizeof buf);
1409 safestrncpy(CC->room.QRdirname, buf,
1410 sizeof CC->room.QRdirname);
1413 if (num_parms(args) >= 8) {
1414 CC->room.QRdefaultview = extract_int(args, 7);
1417 /* Second set of flags */
1418 if (num_parms(args) >= 9) {
1419 CC->room.QRflags2 = extract_int(args, 8);
1423 CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
1424 /* Clean up a client boo-boo: if the client set the room to
1425 * guess-name or passworded, ensure that the private flag is
1428 if ((CC->room.QRflags & QR_GUESSNAME)
1429 || (CC->room.QRflags & QR_PASSWORDED))
1430 CC->room.QRflags |= QR_PRIVATE;
1432 /* Some changes can't apply to BASEROOM */
1433 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1435 CC->room.QRorder = 0;
1436 CC->room.QRpasswd[0] = '\0';
1437 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1438 QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1439 CC->room.QRflags |= QR_PERMANENT;
1441 /* March order (doesn't apply to AIDEROOM) */
1442 if (num_parms(args) >= 7)
1443 CC->room.QRorder = (char) new_order;
1445 extract_token(buf, args, 1, '|', sizeof buf);
1447 safestrncpy(CC->room.QRpasswd, buf,
1448 sizeof CC->room.QRpasswd);
1449 /* Kick everyone out if the client requested it
1450 * (by changing the room's generation number)
1452 if (extract_int(args, 4)) {
1453 time(&CC->room.QRgen);
1456 /* Some changes can't apply to AIDEROOM */
1457 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1459 CC->room.QRorder = 0;
1460 CC->room.QRflags &= ~QR_MAILBOX;
1461 CC->room.QRflags |= QR_PERMANENT;
1464 /* Write the room record back to disk */
1465 lputroom(&CC->room);
1467 /* Create a room directory if necessary */
1468 if (CC->room.QRflags & QR_DIRECTORY) {
1469 snprintf(buf, sizeof buf,"%s/%s",
1471 CC->room.QRdirname);
1474 snprintf(buf, sizeof buf, "The room \"%s\" has been edited by %s.\n",
1475 CC->room.QRname, CC->curr_user);
1476 aide_message(buf, "Room modification Message");
1477 cprintf("%d Ok\n", CIT_OK);
1483 * get the name of the room aide for this room
1487 struct ctdluser usbuf;
1489 if (CtdlAccessCheck(ac_logged_in)) return;
1491 if (getuserbynumber(&usbuf, CC->room.QRroomaide) == 0) {
1492 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1494 cprintf("%d \n", CIT_OK);
1500 * set the room aide for this room
1502 void cmd_seta(char *new_ra)
1504 struct ctdluser usbuf;
1509 if (CtdlAccessCheck(ac_room_aide)) return;
1511 if (getuser(&usbuf, new_ra) != 0) {
1514 newu = usbuf.usernum;
1517 lgetroom(&CC->room, CC->room.QRname);
1519 if (CC->room.QRroomaide != newu) {
1522 CC->room.QRroomaide = newu;
1523 lputroom(&CC->room);
1526 * We have to post the change notice _after_ writing changes to
1527 * the room table, otherwise it would deadlock!
1529 if (post_notice == 1) {
1530 if (!IsEmptyStr(usbuf.fullname))
1531 snprintf(buf, sizeof buf,
1532 "%s is now the room aide for \"%s\".\n",
1533 usbuf.fullname, CC->room.QRname);
1535 snprintf(buf, sizeof buf,
1536 "There is now no room aide for \"%s\".\n",
1538 aide_message(buf, "Aide Room Modification");
1540 cprintf("%d Ok\n", CIT_OK);
1544 * retrieve info file for this room
1552 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_info_dir);
1553 info_fp = fopen(filename, "r");
1555 if (info_fp == NULL) {
1556 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
1559 cprintf("%d Info:\n", LISTING_FOLLOWS);
1560 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1561 if (!IsEmptyStr(buf))
1562 buf[strlen(buf) - 1] = 0;
1563 cprintf("%s\n", buf);
1570 * Asynchronously schedule a room for deletion. The room will appear
1571 * deleted to the user(s), but it won't actually get purged from the
1572 * database until THE DREADED AUTO-PURGER makes its next run.
1574 void schedule_room_for_deletion(struct ctdlroom *qrbuf)
1576 char old_name[ROOMNAMELEN];
1579 lprintf(CTDL_NOTICE, "Scheduling room <%s> for deletion\n",
1582 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1584 getroom(qrbuf, qrbuf->QRname);
1586 /* Turn the room into a private mailbox owned by a user who doesn't
1587 * exist. This will immediately make the room invisible to everyone,
1588 * and qualify the room for purging.
1590 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1595 qrbuf->QRflags |= QR_MAILBOX;
1596 time(&qrbuf->QRgen); /* Use a timestamp as the new generation number */
1600 b_deleteroom(old_name);
1606 * Back end processing to delete a room and everything associated with it
1607 * (This one is synchronous and should only get called by THE DREADED
1608 * AUTO-PURGER in serv_expire.c. All user-facing code should call
1609 * the asynchronous schedule_room_for_deletion() instead.)
1611 void delete_room(struct ctdlroom *qrbuf)
1615 /* TODO: filename magic? does this realy work? */
1617 lprintf(CTDL_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1619 /* Delete the info file */
1620 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_info_dir);
1623 /* Delete the image file */
1624 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_image_dir);
1627 /* Delete the room's network config file */
1628 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1631 /* Delete the messages in the room
1632 * (Careful: this opens an S_ROOMS critical section!)
1634 CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
1636 /* Flag the room record as not in use */
1637 lgetroom(qrbuf, qrbuf->QRname);
1641 /* then decrement the reference count for the floor */
1642 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1643 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1644 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1646 /* Delete the room record from the database! */
1647 b_deleteroom(qrbuf->QRname);
1653 * Check access control for deleting a room
1655 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1657 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1661 if (is_noneditable(qr)) {
1666 * For mailboxes, check stuff
1668 if (qr->QRflags & QR_MAILBOX) {
1670 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1672 if (atol(qr->QRname) != CC->user.usernum) {
1673 return(0); /* not my room */
1676 /* Can't delete your Mail> room */
1677 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1679 /* Otherwise it's ok */
1684 * For normal rooms, just check for aide or room aide status.
1686 return(is_room_aide());
1690 * aide command: kill the current room
1692 void cmd_kill(char *argbuf)
1694 char deleted_room_name[ROOMNAMELEN];
1698 kill_ok = extract_int(argbuf, 0);
1700 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
1701 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1705 if (CC->room.QRflags & QR_MAILBOX) {
1706 safestrncpy(deleted_room_name, &CC->room.QRname[11], sizeof deleted_room_name);
1709 safestrncpy(deleted_room_name, CC->room.QRname, sizeof deleted_room_name);
1712 /* Do the dirty work */
1713 schedule_room_for_deletion(&CC->room);
1715 /* Return to the Lobby */
1716 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1718 /* tell the world what we did */
1719 snprintf(msg, sizeof msg, "The room \"%s\" has been deleted by %s.\n",
1720 deleted_room_name, CC->curr_user);
1721 aide_message(msg, "Room Purger Message");
1722 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1724 cprintf("%d ok to delete.\n", CIT_OK);
1730 * Internal code to create a new room (returns room flags)
1732 * Room types: 0=public, 1=guessname, 2=passworded, 3=inv-only,
1733 * 4=mailbox, 5=mailbox, but caller supplies namespace
1735 unsigned create_room(char *new_room_name,
1737 char *new_room_pass,
1744 struct ctdlroom qrbuf;
1748 lprintf(CTDL_DEBUG, "create_room(name=%s, type=%d, view=%d)\n",
1749 new_room_name, new_room_type, new_room_view);
1751 if (getroom(&qrbuf, new_room_name) == 0) {
1752 lprintf(CTDL_DEBUG, "%s already exists.\n", new_room_name);
1756 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1757 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1758 qrbuf.QRflags = QR_INUSE;
1759 if (new_room_type > 0)
1760 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1761 if (new_room_type == 1)
1762 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1763 if (new_room_type == 2)
1764 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1765 if ( (new_room_type == 4) || (new_room_type == 5) ) {
1766 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1767 /* qrbuf.QRflags2 |= QR2_SUBJECTREQ; */
1770 /* If the user is requesting a personal room, set up the room
1771 * name accordingly (prepend the user number)
1773 if (new_room_type == 4) {
1774 MailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1777 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1780 /* If the room is private, and the system administrator has elected
1781 * to automatically grant room aide privileges, do so now; otherwise,
1782 * set the room aide to undefined.
1784 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1785 qrbuf.QRroomaide = CC->user.usernum;
1787 qrbuf.QRroomaide = (-1L);
1791 * If the caller is only interested in testing whether this will work,
1792 * return now without creating the room.
1794 if (!really_create) return (qrbuf.QRflags);
1796 qrbuf.QRnumber = get_new_room_number();
1797 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1798 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1799 qrbuf.QRfloor = new_room_floor;
1800 qrbuf.QRdefaultview = new_room_view;
1802 /* save what we just did... */
1805 /* bump the reference count on whatever floor the room is on */
1806 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1807 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1808 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1810 /* Grant the creator access to the room unless the avoid_access
1811 * parameter was specified.
1813 if ( (CC->logged_in) && (avoid_access == 0) ) {
1814 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1815 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1816 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1817 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1820 /* resume our happy day */
1821 return (qrbuf.QRflags);
1828 void cmd_cre8(char *args)
1831 char new_room_name[ROOMNAMELEN];
1833 char new_room_pass[32];
1836 char *notification_message = NULL;
1839 int avoid_access = 0;
1841 cre8_ok = extract_int(args, 0);
1842 extract_token(new_room_name, args, 1, '|', sizeof new_room_name);
1843 new_room_name[ROOMNAMELEN - 1] = 0;
1844 new_room_type = extract_int(args, 2);
1845 extract_token(new_room_pass, args, 3, '|', sizeof new_room_pass);
1846 avoid_access = extract_int(args, 5);
1847 new_room_view = extract_int(args, 6);
1848 new_room_pass[9] = 0;
1851 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 1)) {
1852 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
1856 if (!strcasecmp(new_room_name, MAILROOM)) {
1857 cprintf("%d '%s' already exists.\n",
1858 ERROR + ALREADY_EXISTS, new_room_name);
1862 if (num_parms(args) >= 5) {
1863 fl = cgetfloor(extract_int(args, 4));
1865 cprintf("%d Invalid floor number.\n",
1866 ERROR + INVALID_FLOOR_OPERATION);
1869 else if ((fl->f_flags & F_INUSE) == 0) {
1870 cprintf("%d Invalid floor number.\n",
1871 ERROR + INVALID_FLOOR_OPERATION);
1874 new_room_floor = extract_int(args, 4);
1878 if (CtdlAccessCheck(ac_logged_in)) return;
1880 if (CC->user.axlevel < config.c_createax && !CC->internal_pgm) {
1881 cprintf("%d You need higher access to create rooms.\n",
1882 ERROR + HIGHER_ACCESS_REQUIRED);
1886 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 0)) {
1887 cprintf("%d Ok to create rooms.\n", CIT_OK);
1891 if ((new_room_type < 0) || (new_room_type > 5)) {
1892 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
1896 if (new_room_type == 5) {
1897 if (CC->user.axlevel < 6) {
1898 cprintf("%d Higher access required\n",
1899 ERROR + HIGHER_ACCESS_REQUIRED);
1904 /* Check to make sure the requested room name doesn't already exist */
1905 newflags = create_room(new_room_name,
1906 new_room_type, new_room_pass, new_room_floor,
1907 0, avoid_access, new_room_view);
1908 if (newflags == 0) {
1909 cprintf("%d '%s' already exists.\n",
1910 ERROR + ALREADY_EXISTS, new_room_name);
1915 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
1919 /* If we reach this point, the room needs to be created. */
1921 newflags = create_room(new_room_name,
1922 new_room_type, new_room_pass, new_room_floor, 1, 0,
1925 /* post a message in Aide> describing the new room */
1926 notification_message = malloc(1024);
1927 snprintf(notification_message, 1024,
1928 "A new room called \"%s\" has been created by %s%s%s%s%s%s\n",
1931 ((newflags & QR_MAILBOX) ? " [personal]" : ""),
1932 ((newflags & QR_PRIVATE) ? " [private]" : ""),
1933 ((newflags & QR_GUESSNAME) ? " [hidden]" : ""),
1934 ((newflags & QR_PASSWORDED) ? " Password: " : ""),
1935 ((newflags & QR_PASSWORDED) ? new_room_pass : "")
1937 aide_message(notification_message, "Room Creation Message");
1938 free(notification_message);
1940 cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
1945 void cmd_einf(char *ok)
1946 { /* enter info file for current room */
1948 char infofilename[SIZ];
1953 if (CtdlAccessCheck(ac_room_aide)) return;
1955 if (atoi(ok) == 0) {
1956 cprintf("%d Ok.\n", CIT_OK);
1959 assoc_file_name(infofilename, sizeof infofilename, &CC->room, ctdl_info_dir);
1960 lprintf(CTDL_DEBUG, "opening\n");
1961 fp = fopen(infofilename, "w");
1962 lprintf(CTDL_DEBUG, "checking\n");
1964 cprintf("%d Cannot open %s: %s\n",
1965 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1968 cprintf("%d Send info...\n", SEND_LISTING);
1971 client_getln(buf, sizeof buf);
1972 if (strcmp(buf, "000"))
1973 fprintf(fp, "%s\n", buf);
1974 } while (strcmp(buf, "000"));
1977 /* now update the room index so people will see our new info */
1978 lgetroom(&CC->room, CC->room.QRname); /* lock so no one steps on us */
1979 CC->room.QRinfo = CC->room.QRhighest + 1L;
1980 lputroom(&CC->room);
1985 * cmd_lflr() - List all known floors
1992 if (CtdlAccessCheck(ac_logged_in)) return;
1994 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1996 for (a = 0; a < MAXFLOORS; ++a) {
1997 getfloor(&flbuf, a);
1998 if (flbuf.f_flags & F_INUSE) {
1999 cprintf("%d|%s|%d\n",
2011 * create a new floor
2013 void cmd_cflr(char *argbuf)
2015 char new_floor_name[256];
2018 int free_slot = (-1);
2021 extract_token(new_floor_name, argbuf, 0, '|', sizeof new_floor_name);
2022 cflr_ok = extract_int(argbuf, 1);
2024 if (CtdlAccessCheck(ac_aide)) return;
2026 if (IsEmptyStr(new_floor_name)) {
2027 cprintf("%d Blank floor name not allowed.\n",
2028 ERROR + ILLEGAL_VALUE);
2032 for (a = 0; a < MAXFLOORS; ++a) {
2033 getfloor(&flbuf, a);
2035 /* note any free slots while we're scanning... */
2036 if (((flbuf.f_flags & F_INUSE) == 0)
2040 /* check to see if it already exists */
2041 if ((!strcasecmp(flbuf.f_name, new_floor_name))
2042 && (flbuf.f_flags & F_INUSE)) {
2043 cprintf("%d Floor '%s' already exists.\n",
2044 ERROR + ALREADY_EXISTS,
2050 if (free_slot < 0) {
2051 cprintf("%d There is no space available for a new floor.\n",
2052 ERROR + INVALID_FLOOR_OPERATION);
2056 cprintf("%d ok to create...\n", CIT_OK);
2059 lgetfloor(&flbuf, free_slot);
2060 flbuf.f_flags = F_INUSE;
2061 flbuf.f_ref_count = 0;
2062 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
2063 lputfloor(&flbuf, free_slot);
2064 cprintf("%d %d\n", CIT_OK, free_slot);
2072 void cmd_kflr(char *argbuf)
2075 int floor_to_delete;
2079 floor_to_delete = extract_int(argbuf, 0);
2080 kflr_ok = extract_int(argbuf, 1);
2082 if (CtdlAccessCheck(ac_aide)) return;
2084 lgetfloor(&flbuf, floor_to_delete);
2087 if ((flbuf.f_flags & F_INUSE) == 0) {
2088 cprintf("%d Floor %d not in use.\n",
2089 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
2092 if (flbuf.f_ref_count != 0) {
2093 cprintf("%d Cannot delete; floor contains %d rooms.\n",
2094 ERROR + INVALID_FLOOR_OPERATION,
2099 cprintf("%d Ok\n", CIT_OK);
2101 cprintf("%d Ok to delete...\n", CIT_OK);
2108 if ((delete_ok == 1) && (kflr_ok == 1))
2110 lputfloor(&flbuf, floor_to_delete);
2116 void cmd_eflr(char *argbuf)
2122 np = num_parms(argbuf);
2124 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
2128 if (CtdlAccessCheck(ac_aide)) return;
2130 floor_num = extract_int(argbuf, 0);
2131 lgetfloor(&flbuf, floor_num);
2132 if ((flbuf.f_flags & F_INUSE) == 0) {
2133 lputfloor(&flbuf, floor_num);
2134 cprintf("%d Floor %d is not in use.\n",
2135 ERROR + INVALID_FLOOR_OPERATION, floor_num);
2139 extract_token(flbuf.f_name, argbuf, 1, '|', sizeof flbuf.f_name);
2140 lputfloor(&flbuf, floor_num);
2142 cprintf("%d Ok\n", CIT_OK);