2 * Server functions which perform operations on room objects.
13 #include <dirent.h> /* for cmd_rdir to read contents of the directory */
15 #if TIME_WITH_SYS_TIME
16 # include <sys/time.h>
20 # include <sys/time.h>
29 #include <libcitadel.h>
34 #include "sysdep_decls.h"
37 #include "citserver.h"
39 #include "citadel_dirs.h"
42 #include "ctdl_module.h"
45 struct floor *floorcache[MAXFLOORS];
48 * Retrieve access control information for any user/room pair
50 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf,
51 int *result, int *view)
58 if (userbuf == &CC->user) {
62 if ((is_me) && (config.c_guest_logins) && (!CC->logged_in)) {
66 /* for internal programs, always do everything */
67 if (((CC->internal_pgm)) && (roombuf->QRflags & QR_INUSE)) {
68 retval = (UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED);
73 /* If guest mode is enabled, always grant access to the Lobby */
74 if ((is_guest) && (!strcasecmp(roombuf->QRname, BASEROOM))) {
75 retval = (UA_KNOWN | UA_GOTOALLOWED);
80 /* Locate any applicable user/room relationships */
82 memset(&vbuf, 0, sizeof vbuf);
85 CtdlGetRelationship(&vbuf, userbuf, roombuf);
88 /* Force the properties of the Aide room */
89 if (!strcasecmp(roombuf->QRname, config.c_aideroom)) {
90 if (userbuf->axlevel >= AxAideU) {
91 retval = UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
98 /* If this is a public room, it's accessible... */
99 if ( ((roombuf->QRflags & QR_PRIVATE) == 0)
100 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
101 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
104 /* If this is a preferred users only room, check access level */
105 if (roombuf->QRflags & QR_PREFONLY) {
106 if (userbuf->axlevel < AxPrefU) {
107 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
111 /* For private rooms, check the generation number matchups */
112 if ( (roombuf->QRflags & QR_PRIVATE)
113 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
115 /* An explicit match means the user belongs in this room */
116 if (vbuf.v_flags & V_ACCESS) {
117 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
119 /* Otherwise, check if this is a guess-name or passworded
120 * room. If it is, a goto may at least be attempted
122 else if ((roombuf->QRflags & QR_PRIVATE)
123 || (roombuf->QRflags & QR_PASSWORDED)) {
124 retval = retval & ~UA_KNOWN;
125 retval = retval | UA_GOTOALLOWED;
129 /* For mailbox rooms, also check the namespace */
130 /* Also, mailbox owners can delete their messages */
131 if (roombuf->QRflags & QR_MAILBOX) {
132 if (userbuf->usernum == atol(roombuf->QRname)) {
133 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
135 /* An explicit match means the user belongs in this room */
136 if (vbuf.v_flags & V_ACCESS) {
137 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
141 /* For non-mailbox rooms... */
144 /* User is allowed to post in the room unless:
145 * - User is not validated
146 * - User has no net privileges and it is a shared network room
147 * - It is a read-only room
149 int post_allowed = 1;
150 if (userbuf->axlevel < AxProbU) post_allowed = 0;
151 if ((userbuf->axlevel < AxNetU) && (roombuf->QRflags & QR_NETWORK)) post_allowed = 0;
152 if (roombuf->QRflags & QR_READONLY) post_allowed = 0;
154 retval = retval | UA_POSTALLOWED;
157 /* If "collaborative deletion" is active for this room, any user who can post
158 * is also allowed to delete
160 if (roombuf->QRflags2 & QR2_COLLABDEL) {
161 if (retval & UA_POSTALLOWED) {
162 retval = retval | UA_DELETEALLOWED;
168 /* Check to see if the user has forgotten this room */
169 if (vbuf.v_flags & V_FORGET) {
170 retval = retval & ~UA_KNOWN;
171 if ( ( ((roombuf->QRflags & QR_PRIVATE) == 0)
172 && ((roombuf->QRflags & QR_MAILBOX) == 0) )
173 || ( (roombuf->QRflags & QR_MAILBOX)
174 && (atol(roombuf->QRname) == CC->user.usernum))) {
175 retval = retval | UA_ZAPPED;
178 /* If user is explicitly locked out of this room, deny everything */
179 if (vbuf.v_flags & V_LOCKOUT) {
180 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED & ~UA_POSTALLOWED;
183 /* Aides get access to all private rooms */
184 if ( (userbuf->axlevel >= AxAideU)
185 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
186 if (vbuf.v_flags & V_FORGET) {
187 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED;
190 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED;
194 /* Aides can gain access to mailboxes as well, but they don't show
197 if ( (userbuf->axlevel >= AxAideU)
198 && (roombuf->QRflags & QR_MAILBOX) ) {
199 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED;
202 /* Aides and Room Aides have admin privileges */
203 if ( (userbuf->axlevel >= AxAideU)
204 || (userbuf->usernum == roombuf->QRroomaide)
206 retval = retval | UA_ADMINALLOWED | UA_DELETEALLOWED | UA_POSTALLOWED;
209 NEWMSG: /* By the way, we also check for the presence of new messages */
210 if (is_msg_in_sequence_set(vbuf.v_seen, roombuf->QRhighest) == 0) {
211 retval = retval | UA_HASNEWMSGS;
214 /* System rooms never show up in the list. */
215 if (roombuf->QRflags2 & QR2_SYSTEM) {
216 retval = retval & ~UA_KNOWN;
220 /* Now give the caller the information it wants. */
221 if (result != NULL) *result = retval;
222 if (view != NULL) *view = vbuf.v_view;
226 * Self-checking stuff for a room record read into memory
228 void room_sanity_check(struct ctdlroom *qrbuf)
230 /* Mailbox rooms are always on the lowest floor */
231 if (qrbuf->QRflags & QR_MAILBOX) {
234 /* Listing order of 0 is illegal except for base rooms */
235 if (qrbuf->QRorder == 0)
236 if (!(qrbuf->QRflags & QR_MAILBOX) &&
237 strncasecmp(qrbuf->QRname, config.c_baseroom, ROOMNAMELEN)
239 strncasecmp(qrbuf->QRname, config.c_aideroom, ROOMNAMELEN))
245 * CtdlGetRoom() - retrieve room data from disk
247 int CtdlGetRoom(struct ctdlroom *qrbuf, char *room_name)
249 struct cdbdata *cdbqr;
250 char lowercase_name[ROOMNAMELEN];
251 char personal_lowercase_name[ROOMNAMELEN];
252 char *dptr, *sptr, *eptr;
254 dptr = lowercase_name;
256 eptr = (dptr + (sizeof lowercase_name - 1));
257 while (!IsEmptyStr(sptr) && (dptr < eptr)){
258 *dptr = tolower(*sptr);
263 memset(qrbuf, 0, sizeof(struct ctdlroom));
265 /* First, try the public namespace */
266 cdbqr = cdb_fetch(CDB_ROOMS,
267 lowercase_name, strlen(lowercase_name));
269 /* If that didn't work, try the user's personal namespace */
271 snprintf(personal_lowercase_name,
272 sizeof personal_lowercase_name, "%010ld.%s",
273 CC->user.usernum, lowercase_name);
274 cdbqr = cdb_fetch(CDB_ROOMS,
275 personal_lowercase_name,
276 strlen(personal_lowercase_name));
279 memcpy(qrbuf, cdbqr->ptr,
280 ((cdbqr->len > sizeof(struct ctdlroom)) ?
281 sizeof(struct ctdlroom) : cdbqr->len));
284 room_sanity_check(qrbuf);
293 * CtdlGetRoomLock() - same as getroom() but locks the record (if supported)
295 int CtdlGetRoomLock(struct ctdlroom *qrbuf, char *room_name)
298 retval = CtdlGetRoom(qrbuf, room_name);
299 if (retval == 0) begin_critical_section(S_ROOMS);
305 * b_putroom() - back end to putroom() and b_deleteroom()
306 * (if the supplied buffer is NULL, delete the room record)
308 void b_putroom(struct ctdlroom *qrbuf, char *room_name)
310 char lowercase_name[ROOMNAMELEN];
315 bptr = lowercase_name;
316 while (!IsEmptyStr(aptr))
318 *bptr = tolower(*aptr);
324 len = bptr - lowercase_name;
326 cdb_delete(CDB_ROOMS,
327 lowercase_name, len);
329 time(&qrbuf->QRmtime);
332 qrbuf, sizeof(struct ctdlroom));
338 * CtdlPutRoom() - store room data to disk
340 void CtdlPutRoom(struct ctdlroom *qrbuf) {
341 b_putroom(qrbuf, qrbuf->QRname);
346 * b_deleteroom() - delete a room record from disk
348 void b_deleteroom(char *room_name) {
349 b_putroom(NULL, room_name);
355 * CtdlPutRoomLock() - same as CtdlPutRoom() but unlocks the record (if supported)
357 void CtdlPutRoomLock(struct ctdlroom *qrbuf)
361 end_critical_section(S_ROOMS);
365 /****************************************************************************/
369 * CtdlGetFloorByName() - retrieve the number of the named floor
370 * return < 0 if not found else return floor number
372 int CtdlGetFloorByName(const char *floor_name)
375 struct floor *flbuf = NULL;
377 for (a = 0; a < MAXFLOORS; ++a) {
378 flbuf = CtdlGetCachedFloor(a);
380 /* check to see if it already exists */
381 if ((!strcasecmp(flbuf->f_name, floor_name))
382 && (flbuf->f_flags & F_INUSE)) {
392 * CtdlGetFloorByNameLock() - retrieve floor number for given floor and lock the floor list.
394 int CtdlGetFloorByNameLock(const char *floor_name)
396 begin_critical_section(S_FLOORTAB);
397 return CtdlGetFloorByName(floor_name);
403 * CtdlGetAvailableFloor() - Return number of first unused floor
404 * return < 0 if none available
406 int CtdlGetAvailableFloor(void)
409 struct floor *flbuf = NULL;
411 for (a = 0; a < MAXFLOORS; a++) {
412 flbuf = CtdlGetCachedFloor(a);
414 /* check to see if it already exists */
415 if ((flbuf->f_flags & F_INUSE) == 0) {
424 * CtdlGetFloor() - retrieve floor data from disk
426 void CtdlGetFloor(struct floor *flbuf, int floor_num)
428 struct cdbdata *cdbfl;
430 memset(flbuf, 0, sizeof(struct floor));
431 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
433 memcpy(flbuf, cdbfl->ptr,
434 ((cdbfl->len > sizeof(struct floor)) ?
435 sizeof(struct floor) : cdbfl->len));
438 if (floor_num == 0) {
439 safestrncpy(flbuf->f_name, "Main Floor",
440 sizeof flbuf->f_name);
441 flbuf->f_flags = F_INUSE;
442 flbuf->f_ref_count = 3;
449 * lgetfloor() - same as CtdlGetFloor() but locks the record (if supported)
451 void lgetfloor(struct floor *flbuf, int floor_num)
454 begin_critical_section(S_FLOORTAB);
455 CtdlGetFloor(flbuf, floor_num);
460 * CtdlGetCachedFloor() - Get floor record from *cache* (loads from disk if needed)
462 * This is strictly a performance hack.
464 struct floor *CtdlGetCachedFloor(int floor_num) {
465 static int initialized = 0;
468 struct floor *fl = NULL;
470 begin_critical_section(S_FLOORCACHE);
471 if (initialized == 0) {
472 for (i=0; i<MAXFLOORS; ++i) {
473 floorcache[floor_num] = NULL;
477 if (floorcache[floor_num] == NULL) {
480 end_critical_section(S_FLOORCACHE);
483 fl = malloc(sizeof(struct floor));
484 CtdlGetFloor(fl, floor_num);
485 begin_critical_section(S_FLOORCACHE);
486 if (floorcache[floor_num] != NULL) {
487 free(floorcache[floor_num]);
489 floorcache[floor_num] = fl;
490 end_critical_section(S_FLOORCACHE);
493 return(floorcache[floor_num]);
499 * CtdlPutFloor() - store floor data on disk
501 void CtdlPutFloor(struct floor *flbuf, int floor_num)
503 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
504 begin_critical_section(S_FLOORCACHE);
505 if (floorcache[floor_num] != NULL) {
506 free(floorcache[floor_num]);
507 floorcache[floor_num] = malloc(sizeof(struct floor));
508 memcpy(floorcache[floor_num], flbuf, sizeof(struct floor));
510 end_critical_section(S_FLOORCACHE);
512 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
513 flbuf, sizeof(struct floor));
519 * CtdlPutFloorLock() - same as CtdlPutFloor() but unlocks the record (if supported)
521 void CtdlPutFloorLock(struct floor *flbuf, int floor_num)
524 CtdlPutFloor(flbuf, floor_num);
525 end_critical_section(S_FLOORTAB);
532 * lputfloor() - same as CtdlPutFloor() but unlocks the record (if supported)
534 void lputfloor(struct floor *flbuf, int floor_num)
536 CtdlPutFloorLock(flbuf, floor_num);
541 * Traverse the room file...
543 void CtdlForEachRoom(void (*CallBack) (struct ctdlroom *EachRoom, void *out_data),
546 struct ctdlroom qrbuf;
547 struct cdbdata *cdbqr;
549 cdb_rewind(CDB_ROOMS);
551 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
552 memset(&qrbuf, 0, sizeof(struct ctdlroom));
553 memcpy(&qrbuf, cdbqr->ptr,
554 ((cdbqr->len > sizeof(struct ctdlroom)) ?
555 sizeof(struct ctdlroom) : cdbqr->len));
557 room_sanity_check(&qrbuf);
558 if (qrbuf.QRflags & QR_INUSE)
559 (*CallBack)(&qrbuf, in_data);
565 * delete_msglist() - delete room message pointers
567 void delete_msglist(struct ctdlroom *whichroom)
569 struct cdbdata *cdbml;
571 /* Make sure the msglist we're deleting actually exists, otherwise
572 * gdbm will complain when we try to delete an invalid record
574 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
578 /* Go ahead and delete it */
579 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
586 * Message pointer compare function for sort_msglist()
588 int sort_msglist_cmp(const void *m1, const void *m2) {
589 if ((*(const long *)m1) > (*(const long *)m2)) return(1);
590 if ((*(const long *)m1) < (*(const long *)m2)) return(-1);
596 * sort message pointers
597 * (returns new msg count)
599 int sort_msglist(long listptrs[], int oldcount)
609 qsort(listptrs, numitems, sizeof(long), sort_msglist_cmp);
611 /* and yank any nulls */
612 while ((numitems > 0) && (listptrs[0] == 0L)) {
613 memmove(&listptrs[0], &listptrs[1],
614 (sizeof(long) * (numitems - 1)));
623 * Determine whether a given room is non-editable.
625 int CtdlIsNonEditable(struct ctdlroom *qrbuf)
628 /* Mail> rooms are non-editable */
629 if ( (qrbuf->QRflags & QR_MAILBOX)
630 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
633 /* Everything else is editable */
640 * Back-back-end for all room listing commands
642 void list_roomname(struct ctdlroom *qrbuf, int ra, int current_view, int default_view)
644 char truncated_roomname[ROOMNAMELEN];
646 /* For my own mailbox rooms, chop off the owner prefix */
647 if ( (qrbuf->QRflags & QR_MAILBOX)
648 && (atol(qrbuf->QRname) == CC->user.usernum) ) {
649 safestrncpy(truncated_roomname, qrbuf->QRname, sizeof truncated_roomname);
650 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
651 cprintf("%s", truncated_roomname);
653 /* For all other rooms, just display the name in its entirety */
655 cprintf("%s", qrbuf->QRname);
658 /* ...and now the other parameters */
659 cprintf("|%u|%d|%d|%d|%d|%d|%d|%ld|\n",
661 (int) qrbuf->QRfloor,
662 (int) qrbuf->QRorder,
663 (int) qrbuf->QRflags2,
673 * cmd_lrms() - List all accessible rooms, known or forgotten
675 void cmd_lrms_backend(struct ctdlroom *qrbuf, void *data)
677 int FloorBeingSearched = (-1);
681 FloorBeingSearched = *(int *)data;
682 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
684 if ((( ra & (UA_KNOWN | UA_ZAPPED)))
685 && ((qrbuf->QRfloor == (FloorBeingSearched))
686 || ((FloorBeingSearched) < 0)))
687 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
690 void cmd_lrms(char *argbuf)
692 int FloorBeingSearched = (-1);
693 if (!IsEmptyStr(argbuf))
694 FloorBeingSearched = extract_int(argbuf, 0);
696 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
698 CtdlGetUser(&CC->user, CC->curr_user);
699 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
701 CtdlForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
708 * cmd_lkra() - List all known rooms
710 void cmd_lkra_backend(struct ctdlroom *qrbuf, void *data)
712 int FloorBeingSearched = (-1);
716 FloorBeingSearched = *(int *)data;
717 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
719 if ((( ra & (UA_KNOWN)))
720 && ((qrbuf->QRfloor == (FloorBeingSearched))
721 || ((FloorBeingSearched) < 0)))
722 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
725 void cmd_lkra(char *argbuf)
727 int FloorBeingSearched = (-1);
728 if (!IsEmptyStr(argbuf))
729 FloorBeingSearched = extract_int(argbuf, 0);
731 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
733 CtdlGetUser(&CC->user, CC->curr_user);
734 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
736 CtdlForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
742 void cmd_lprm_backend(struct ctdlroom *qrbuf, void *data)
744 int FloorBeingSearched = (-1);
748 FloorBeingSearched = *(int *)data;
749 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
751 if ( ((qrbuf->QRflags & QR_PRIVATE) == 0)
752 && ((qrbuf->QRflags & QR_MAILBOX) == 0)
753 && ((qrbuf->QRfloor == (FloorBeingSearched))
754 || ((FloorBeingSearched) < 0)))
755 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
758 void cmd_lprm(char *argbuf)
760 int FloorBeingSearched = (-1);
761 if (!IsEmptyStr(argbuf))
762 FloorBeingSearched = extract_int(argbuf, 0);
764 cprintf("%d Publiic rooms:\n", LISTING_FOLLOWS);
766 CtdlForEachRoom(cmd_lprm_backend, &FloorBeingSearched);
773 * cmd_lkrn() - List all known rooms with new messages
775 void cmd_lkrn_backend(struct ctdlroom *qrbuf, void *data)
777 int FloorBeingSearched = (-1);
781 FloorBeingSearched = *(int *)data;
782 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
785 && (ra & UA_HASNEWMSGS)
786 && ((qrbuf->QRfloor == (FloorBeingSearched))
787 || ((FloorBeingSearched) < 0)))
788 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
791 void cmd_lkrn(char *argbuf)
793 int FloorBeingSearched = (-1);
794 if (!IsEmptyStr(argbuf))
795 FloorBeingSearched = extract_int(argbuf, 0);
797 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
799 CtdlGetUser(&CC->user, CC->curr_user);
800 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
802 CtdlForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
809 * cmd_lkro() - List all known rooms
811 void cmd_lkro_backend(struct ctdlroom *qrbuf, void *data)
813 int FloorBeingSearched = (-1);
817 FloorBeingSearched = *(int *)data;
818 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
821 && ((ra & UA_HASNEWMSGS) == 0)
822 && ((qrbuf->QRfloor == (FloorBeingSearched))
823 || ((FloorBeingSearched) < 0)))
824 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
827 void cmd_lkro(char *argbuf)
829 int FloorBeingSearched = (-1);
830 if (!IsEmptyStr(argbuf))
831 FloorBeingSearched = extract_int(argbuf, 0);
833 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
835 CtdlGetUser(&CC->user, CC->curr_user);
836 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
838 CtdlForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
845 * cmd_lzrm() - List all forgotten rooms
847 void cmd_lzrm_backend(struct ctdlroom *qrbuf, void *data)
849 int FloorBeingSearched = (-1);
853 FloorBeingSearched = *(int *)data;
854 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
856 if ((ra & UA_GOTOALLOWED)
858 && ((qrbuf->QRfloor == (FloorBeingSearched))
859 || ((FloorBeingSearched) < 0)))
860 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
863 void cmd_lzrm(char *argbuf)
865 int FloorBeingSearched = (-1);
866 if (!IsEmptyStr(argbuf))
867 FloorBeingSearched = extract_int(argbuf, 0);
869 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
871 CtdlGetUser(&CC->user, CC->curr_user);
872 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
874 CtdlForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
880 * Make the specified room the current room for this session. No validation
881 * or access control is done here -- the caller should make sure that the
882 * specified room exists and is ok to access.
884 void CtdlUserGoto(char *where, int display_result, int transiently,
885 int *retmsgs, int *retnew)
888 int new_messages = 0;
889 int old_messages = 0;
890 int total_messages = 0;
894 int newmailcount = 0;
896 char truncated_roomname[ROOMNAMELEN];
897 struct cdbdata *cdbfr;
898 long *msglist = NULL;
900 unsigned int original_v_flags;
903 char setstr[128], lostr[64], histr[64];
907 /* If the supplied room name is NULL, the caller wants us to know that
908 * it has already copied the room record into CC->room, so
909 * we can skip the extra database fetch.
912 safestrncpy(CC->room.QRname, where, sizeof CC->room.QRname);
913 CtdlGetRoom(&CC->room, where);
916 /* Take care of all the formalities. */
918 begin_critical_section(S_USERS);
919 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
920 original_v_flags = vbuf.v_flags;
922 /* Know the room ... but not if it's the page log room, or if the
923 * caller specified that we're only entering this room transiently.
925 if ((strcasecmp(CC->room.QRname, config.c_logpages))
926 && (transiently == 0) ) {
927 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
928 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
931 /* Only rewrite the database record if we changed something */
932 if (vbuf.v_flags != original_v_flags) {
933 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
935 end_critical_section(S_USERS);
937 /* Check for new mail */
938 newmailcount = NewMailCount();
940 /* set info to 1 if the user needs to read the room's info file */
941 if (CC->room.QRinfo > vbuf.v_lastseen) {
945 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
947 msglist = (long *) cdbfr->ptr;
948 cdbfr->ptr = NULL; /* CtdlUserGoto() now owns this memory */
949 num_msgs = cdbfr->len / sizeof(long);
954 for (a=0; a<num_msgs; ++a) {
955 if (msglist[a] > 0L) ++total_messages;
957 new_messages = num_msgs;
958 num_sets = num_tokens(vbuf.v_seen, ',');
959 for (s=0; s<num_sets; ++s) {
960 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
962 extract_token(lostr, setstr, 0, ':', sizeof lostr);
963 if (num_tokens(setstr, ':') >= 2) {
964 extract_token(histr, setstr, 1, ':', sizeof histr);
965 if (!strcmp(histr, "*")) {
966 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
970 strcpy(histr, lostr);
975 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
976 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
982 new_messages = total_messages - old_messages;
984 if (msglist != NULL) free(msglist);
986 if (CC->room.QRflags & QR_MAILBOX)
991 if ((CC->room.QRroomaide == CC->user.usernum)
992 || (CC->user.axlevel >= AxAideU))
997 safestrncpy(truncated_roomname, CC->room.QRname, sizeof truncated_roomname);
998 if ( (CC->room.QRflags & QR_MAILBOX)
999 && (atol(CC->room.QRname) == CC->user.usernum) ) {
1000 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
1003 if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
1007 if (retmsgs != NULL) *retmsgs = total_messages;
1008 if (retnew != NULL) *retnew = new_messages;
1009 CtdlLogPrintf(CTDL_DEBUG, "<%s> %d new of %d total messages\n",
1011 new_messages, total_messages
1014 CC->curr_view = (int)vbuf.v_view;
1016 if (display_result) {
1017 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|%d|\n",
1018 CIT_OK, CtdlCheckExpress(),
1021 (int)total_messages,
1023 (int)CC->room.QRflags,
1024 (long)CC->room.QRhighest,
1025 (long)vbuf.v_lastseen,
1029 (int)CC->room.QRfloor,
1031 (int)CC->room.QRdefaultview,
1033 (int)CC->room.QRflags2
1040 * Handle some of the macro named rooms
1042 void convert_room_name_macros(char *towhere, size_t maxlen) {
1043 if (!strcasecmp(towhere, "_BASEROOM_")) {
1044 safestrncpy(towhere, config.c_baseroom, maxlen);
1046 else if (!strcasecmp(towhere, "_MAIL_")) {
1047 safestrncpy(towhere, MAILROOM, maxlen);
1049 else if (!strcasecmp(towhere, "_TRASH_")) {
1050 safestrncpy(towhere, USERTRASHROOM, maxlen);
1052 else if (!strcasecmp(towhere, "_DRAFTS_")) {
1053 safestrncpy(towhere, USERDRAFTROOM, maxlen);
1055 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
1056 safestrncpy(towhere, config.c_twitroom, maxlen);
1058 else if (!strcasecmp(towhere, "_CALENDAR_")) {
1059 safestrncpy(towhere, USERCALENDARROOM, maxlen);
1061 else if (!strcasecmp(towhere, "_TASKS_")) {
1062 safestrncpy(towhere, USERTASKSROOM, maxlen);
1064 else if (!strcasecmp(towhere, "_CONTACTS_")) {
1065 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
1067 else if (!strcasecmp(towhere, "_NOTES_")) {
1068 safestrncpy(towhere, USERNOTESROOM, maxlen);
1074 * cmd_goto() - goto a new room
1076 void cmd_goto(char *gargs)
1078 struct ctdlroom QRscratch;
1082 char augmented_roomname[ROOMNAMELEN];
1083 char towhere[ROOMNAMELEN];
1085 int transiently = 0;
1087 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
1089 extract_token(towhere, gargs, 0, '|', sizeof towhere);
1090 extract_token(password, gargs, 1, '|', sizeof password);
1091 transiently = extract_int(gargs, 2);
1093 CtdlGetUser(&CC->user, CC->curr_user);
1096 * Handle some of the macro named rooms
1098 convert_room_name_macros(towhere, sizeof towhere);
1100 /* First try a regular match */
1101 c = CtdlGetRoom(&QRscratch, towhere);
1103 /* Then try a mailbox name match */
1105 CtdlMailboxName(augmented_roomname, sizeof augmented_roomname,
1106 &CC->user, towhere);
1107 c = CtdlGetRoom(&QRscratch, augmented_roomname);
1109 safestrncpy(towhere, augmented_roomname, sizeof towhere);
1112 /* And if the room was found... */
1115 /* Let internal programs go directly to any room. */
1116 if (CC->internal_pgm) {
1117 memcpy(&CC->room, &QRscratch,
1118 sizeof(struct ctdlroom));
1119 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1123 /* See if there is an existing user/room relationship */
1124 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
1126 /* normal clients have to pass through security */
1127 if (ra & UA_GOTOALLOWED) {
1132 if ((QRscratch.QRflags & QR_MAILBOX) &&
1133 ((ra & UA_GOTOALLOWED))) {
1134 memcpy(&CC->room, &QRscratch,
1135 sizeof(struct ctdlroom));
1136 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1138 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
1139 ((ra & UA_KNOWN) == 0) &&
1140 (strcasecmp(QRscratch.QRpasswd, password)) &&
1141 (CC->user.axlevel < AxAideU)
1143 cprintf("%d wrong or missing passwd\n",
1144 ERROR + PASSWORD_REQUIRED);
1146 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
1147 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
1148 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
1149 ((ra & UA_KNOWN) == 0) &&
1150 (CC->user.axlevel < AxAideU)
1152 CtdlLogPrintf(CTDL_DEBUG, "Failed to acquire private room\n");
1154 memcpy(&CC->room, &QRscratch,
1155 sizeof(struct ctdlroom));
1156 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1162 cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
1166 void cmd_whok(char *cmdbuf)
1168 struct ctdluser temp;
1169 struct cdbdata *cdbus;
1172 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
1173 cdb_rewind(CDB_USERS);
1174 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1175 memset(&temp, 0, sizeof temp);
1176 memcpy(&temp, cdbus->ptr, sizeof temp);
1179 CtdlRoomAccess(&CC->room, &temp, &ra, NULL);
1180 if ((CC->room.QRflags & QR_INUSE)
1183 cprintf("%s\n", temp.fullname);
1190 * RDIR command for room directory
1192 void cmd_rdir(char *cmdbuf)
1197 struct stat statbuf;
1198 DIR *filedir = NULL;
1199 struct dirent *filedir_entry;
1205 if (CtdlAccessCheck(ac_logged_in)) return;
1207 CtdlGetRoom(&CC->room, CC->room.QRname);
1208 CtdlGetUser(&CC->user, CC->curr_user);
1210 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
1211 cprintf("%d not here.\n", ERROR + NOT_HERE);
1214 if (((CC->room.QRflags & QR_VISDIR) == 0)
1215 && (CC->user.axlevel < AxAideU)
1216 && (CC->user.usernum != CC->room.QRroomaide)) {
1217 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1221 snprintf(buf, sizeof buf, "%s/%s", ctdl_file_dir, CC->room.QRdirname);
1222 filedir = opendir (buf);
1224 if (filedir == NULL) {
1225 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1228 cprintf("%d %s|%s/%s\n", LISTING_FOLLOWS, config.c_fqdn, ctdl_file_dir, CC->room.QRdirname);
1230 snprintf(buf, sizeof buf, "%s/%s/filedir", ctdl_file_dir, CC->room.QRdirname);
1231 fd = fopen(buf, "r");
1233 fd = fopen("/dev/null", "r");
1234 while ((filedir_entry = readdir(filedir)))
1236 if (strcasecmp(filedir_entry->d_name, "filedir") && filedir_entry->d_name[0] != '.')
1238 #ifdef _DIRENT_HAVE_D_NAMELEN
1239 d_namelen = filedir_entry->d_namelen;
1241 d_namelen = strlen(filedir_entry->d_name);
1243 snprintf(buf, sizeof buf, "%s/%s/%s", ctdl_file_dir, CC->room.QRdirname, filedir_entry->d_name);
1244 stat(buf, &statbuf); /* stat the file */
1245 if (!(statbuf.st_mode & S_IFREG))
1247 snprintf(buf2, sizeof buf2,
1248 "\"%s\" appears in the file directory for room \"%s\" but is not a regular file. Directories, named pipes, sockets, etc. are not usable in Citadel room directories.\n",
1249 buf, CC->room.QRname
1251 CtdlAideMessage(buf2, "Unusable data found in room directory");
1252 continue; /* not a useable file type so don't show it */
1254 safestrncpy(comment, "", sizeof comment);
1255 fseek(fd, 0L, 0); /* rewind descriptions file */
1256 /* Get the description from the descriptions file */
1257 while ((fgets(buf, sizeof buf, fd) != NULL) && (IsEmptyStr(comment)))
1259 buf[strlen(buf) - 1] = 0;
1260 if ((!strncasecmp(buf, filedir_entry->d_name, d_namelen)) && (buf[d_namelen] == ' '))
1261 safestrncpy(comment, &buf[d_namelen + 1], sizeof comment);
1263 len = extract_token (mimebuf, comment, 0,' ', 64);
1264 if ((len <0) || strchr(mimebuf, '/') == NULL)
1266 snprintf (mimebuf, 64, "application/octetstream");
1269 cprintf("%s|%ld|%s|%s\n",
1270 filedir_entry->d_name,
1271 (long)statbuf.st_size,
1283 * get room parameters (aide or room aide command)
1285 void cmd_getr(char *cmdbuf)
1287 if (CtdlAccessCheck(ac_room_aide)) return;
1289 CtdlGetRoom(&CC->room, CC->room.QRname);
1290 cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1294 ((CC->room.QRflags & QR_MAILBOX) ?
1295 &CC->room.QRname[11] : CC->room.QRname),
1297 ((CC->room.QRflags & QR_PASSWORDED) ?
1298 CC->room.QRpasswd : ""),
1300 ((CC->room.QRflags & QR_DIRECTORY) ?
1301 CC->room.QRdirname : ""),
1304 (int) CC->room.QRfloor,
1305 (int) CC->room.QRorder,
1307 CC->room.QRdefaultview,
1314 * Back end function to rename a room.
1315 * You can also specify which floor to move the room to, or specify -1 to
1316 * keep the room on the same floor it was on.
1318 * If you are renaming a mailbox room, you must supply the namespace prefix
1319 * in *at least* the old name!
1321 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1323 struct ctdlroom qrbuf;
1324 struct ctdlroom qrtmp;
1329 char actual_old_name[ROOMNAMELEN];
1331 CtdlLogPrintf(CTDL_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1332 old_name, new_name, new_floor);
1334 if (new_floor >= 0) {
1335 fl = CtdlGetCachedFloor(new_floor);
1336 if ((fl->f_flags & F_INUSE) == 0) {
1337 return(crr_invalid_floor);
1341 begin_critical_section(S_ROOMS);
1343 if ( (CtdlGetRoom(&qrtmp, new_name) == 0)
1344 && (strcasecmp(new_name, old_name)) ) {
1345 ret = crr_already_exists;
1348 else if (CtdlGetRoom(&qrbuf, old_name) != 0) {
1349 ret = crr_room_not_found;
1352 else if ( (CC->user.axlevel < AxAideU) && (!CC->internal_pgm)
1353 && (CC->user.usernum != qrbuf.QRroomaide)
1354 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) ) {
1355 ret = crr_access_denied;
1358 else if (CtdlIsNonEditable(&qrbuf)) {
1359 ret = crr_noneditable;
1364 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1365 if (qrbuf.QRflags & QR_MAILBOX) {
1366 owner = atol(qrbuf.QRname);
1368 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1369 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1370 "%010ld.%s", owner, new_name);
1373 safestrncpy(qrbuf.QRname, new_name,
1374 sizeof(qrbuf.QRname));
1377 /* Reject change of floor for baseroom/aideroom */
1378 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1379 !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1383 /* Take care of floor stuff */
1384 old_floor = qrbuf.QRfloor;
1385 if (new_floor < 0) {
1386 new_floor = old_floor;
1388 qrbuf.QRfloor = new_floor;
1389 CtdlPutRoom(&qrbuf);
1391 begin_critical_section(S_CONFIG);
1393 /* If baseroom/aideroom name changes, update config */
1394 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1395 safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1398 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1399 safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1403 end_critical_section(S_CONFIG);
1405 /* If the room name changed, then there are now two room
1406 * records, so we have to delete the old one.
1408 if (strcasecmp(new_name, old_name)) {
1409 b_deleteroom(actual_old_name);
1415 end_critical_section(S_ROOMS);
1417 /* Adjust the floor reference counts if necessary */
1418 if (new_floor != old_floor) {
1419 lgetfloor(&flbuf, old_floor);
1420 --flbuf.f_ref_count;
1421 lputfloor(&flbuf, old_floor);
1422 CtdlLogPrintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1423 lgetfloor(&flbuf, new_floor);
1424 ++flbuf.f_ref_count;
1425 lputfloor(&flbuf, new_floor);
1426 CtdlLogPrintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1429 /* ...and everybody say "YATTA!" */
1435 * set room parameters (aide or room aide command)
1437 void cmd_setr(char *args)
1443 char new_name[ROOMNAMELEN];
1445 if (CtdlAccessCheck(ac_logged_in)) return;
1447 if (num_parms(args) >= 6) {
1448 new_floor = extract_int(args, 5);
1450 new_floor = (-1); /* don't change the floor */
1453 /* When is a new name more than just a new name? When the old name
1454 * has a namespace prefix.
1456 if (CC->room.QRflags & QR_MAILBOX) {
1457 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
1459 safestrncpy(new_name, "", sizeof new_name);
1461 extract_token(&new_name[strlen(new_name)], args, 0, '|', (sizeof new_name - strlen(new_name)));
1463 r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
1465 if (r == crr_room_not_found) {
1466 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
1467 } else if (r == crr_already_exists) {
1468 cprintf("%d '%s' already exists.\n",
1469 ERROR + ALREADY_EXISTS, new_name);
1470 } else if (r == crr_noneditable) {
1471 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
1472 } else if (r == crr_invalid_floor) {
1473 cprintf("%d Target floor does not exist.\n",
1474 ERROR + INVALID_FLOOR_OPERATION);
1475 } else if (r == crr_access_denied) {
1476 cprintf("%d You do not have permission to edit '%s'\n",
1477 ERROR + HIGHER_ACCESS_REQUIRED,
1479 } else if (r != crr_ok) {
1480 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1481 ERROR + INTERNAL_ERROR, r);
1488 CtdlGetRoom(&CC->room, new_name);
1490 /* Now we have to do a bunch of other stuff */
1492 if (num_parms(args) >= 7) {
1493 new_order = extract_int(args, 6);
1496 if (new_order > 127)
1500 CtdlGetRoomLock(&CC->room, CC->room.QRname);
1502 /* Directory room */
1503 extract_token(buf, args, 2, '|', sizeof buf);
1505 safestrncpy(CC->room.QRdirname, buf,
1506 sizeof CC->room.QRdirname);
1509 if (num_parms(args) >= 8) {
1510 CC->room.QRdefaultview = extract_int(args, 7);
1513 /* Second set of flags */
1514 if (num_parms(args) >= 9) {
1515 CC->room.QRflags2 = extract_int(args, 8);
1519 CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
1520 /* Clean up a client boo-boo: if the client set the room to
1521 * guess-name or passworded, ensure that the private flag is
1524 if ((CC->room.QRflags & QR_GUESSNAME)
1525 || (CC->room.QRflags & QR_PASSWORDED))
1526 CC->room.QRflags |= QR_PRIVATE;
1528 /* Some changes can't apply to BASEROOM */
1529 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1531 CC->room.QRorder = 0;
1532 CC->room.QRpasswd[0] = '\0';
1533 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1534 QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1535 CC->room.QRflags |= QR_PERMANENT;
1537 /* March order (doesn't apply to AIDEROOM) */
1538 if (num_parms(args) >= 7)
1539 CC->room.QRorder = (char) new_order;
1541 extract_token(buf, args, 1, '|', sizeof buf);
1543 safestrncpy(CC->room.QRpasswd, buf,
1544 sizeof CC->room.QRpasswd);
1545 /* Kick everyone out if the client requested it
1546 * (by changing the room's generation number)
1548 if (extract_int(args, 4)) {
1549 time(&CC->room.QRgen);
1552 /* Some changes can't apply to AIDEROOM */
1553 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1555 CC->room.QRorder = 0;
1556 CC->room.QRflags &= ~QR_MAILBOX;
1557 CC->room.QRflags |= QR_PERMANENT;
1560 /* Write the room record back to disk */
1561 CtdlPutRoomLock(&CC->room);
1563 /* Create a room directory if necessary */
1564 if (CC->room.QRflags & QR_DIRECTORY) {
1565 snprintf(buf, sizeof buf,"%s/%s",
1567 CC->room.QRdirname);
1570 snprintf(buf, sizeof buf, "The room \"%s\" has been edited by %s.\n",
1571 CC->room.QRname, CC->curr_user);
1572 CtdlAideMessage(buf, "Room modification Message");
1573 cprintf("%d Ok\n", CIT_OK);
1579 * get the name of the room aide for this room
1581 void cmd_geta(char *cmdbuf)
1583 struct ctdluser usbuf;
1585 if (CtdlAccessCheck(ac_logged_in)) return;
1587 if (CtdlGetUserByNumber(&usbuf, CC->room.QRroomaide) == 0) {
1588 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1590 cprintf("%d \n", CIT_OK);
1596 * set the room aide for this room
1598 void cmd_seta(char *new_ra)
1600 struct ctdluser usbuf;
1605 if (CtdlAccessCheck(ac_room_aide)) return;
1607 if (CtdlGetUser(&usbuf, new_ra) != 0) {
1610 newu = usbuf.usernum;
1613 CtdlGetRoomLock(&CC->room, CC->room.QRname);
1615 if (CC->room.QRroomaide != newu) {
1618 CC->room.QRroomaide = newu;
1619 CtdlPutRoomLock(&CC->room);
1622 * We have to post the change notice _after_ writing changes to
1623 * the room table, otherwise it would deadlock!
1625 if (post_notice == 1) {
1626 if (!IsEmptyStr(usbuf.fullname))
1627 snprintf(buf, sizeof buf,
1628 "%s is now the room aide for \"%s\".\n",
1629 usbuf.fullname, CC->room.QRname);
1631 snprintf(buf, sizeof buf,
1632 "There is now no room aide for \"%s\".\n",
1634 CtdlAideMessage(buf, "Aide Room Modification");
1636 cprintf("%d Ok\n", CIT_OK);
1640 * retrieve info file for this room
1642 void cmd_rinf(char *gargs)
1648 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_info_dir);
1649 info_fp = fopen(filename, "r");
1651 if (info_fp == NULL) {
1652 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
1655 cprintf("%d Info:\n", LISTING_FOLLOWS);
1656 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1657 if (!IsEmptyStr(buf))
1658 buf[strlen(buf) - 1] = 0;
1659 cprintf("%s\n", buf);
1666 * Asynchronously schedule a room for deletion. The room will appear
1667 * deleted to the user(s), but it won't actually get purged from the
1668 * database until THE DREADED AUTO-PURGER makes its next run.
1670 void CtdlScheduleRoomForDeletion(struct ctdlroom *qrbuf)
1672 char old_name[ROOMNAMELEN];
1675 CtdlLogPrintf(CTDL_NOTICE, "Scheduling room <%s> for deletion\n",
1678 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1680 CtdlGetRoom(qrbuf, qrbuf->QRname);
1682 /* Turn the room into a private mailbox owned by a user who doesn't
1683 * exist. This will immediately make the room invisible to everyone,
1684 * and qualify the room for purging.
1686 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1691 qrbuf->QRflags |= QR_MAILBOX;
1692 time(&qrbuf->QRgen); /* Use a timestamp as the new generation number */
1696 b_deleteroom(old_name);
1702 * Back end processing to delete a room and everything associated with it
1703 * (This one is synchronous and should only get called by THE DREADED
1704 * AUTO-PURGER in serv_expire.c. All user-facing code should call
1705 * the asynchronous schedule_room_for_deletion() instead.)
1707 void CtdlDeleteRoom(struct ctdlroom *qrbuf)
1711 /* TODO: filename magic? does this realy work? */
1713 CtdlLogPrintf(CTDL_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1715 /* Delete the info file */
1716 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_info_dir);
1719 /* Delete the image file */
1720 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_image_dir);
1723 /* Delete the room's network config file */
1724 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1727 /* Delete the messages in the room
1728 * (Careful: this opens an S_ROOMS critical section!)
1730 CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
1732 /* Flag the room record as not in use */
1733 CtdlGetRoomLock(qrbuf, qrbuf->QRname);
1735 CtdlPutRoomLock(qrbuf);
1737 /* then decrement the reference count for the floor */
1738 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1739 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1740 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1742 /* Delete the room record from the database! */
1743 b_deleteroom(qrbuf->QRname);
1749 * Check access control for deleting a room
1751 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1753 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1757 if (CtdlIsNonEditable(qr)) {
1762 * For mailboxes, check stuff
1764 if (qr->QRflags & QR_MAILBOX) {
1766 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1768 if (atol(qr->QRname) != CC->user.usernum) {
1769 return(0); /* not my room */
1772 /* Can't delete your Mail> room */
1773 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1775 /* Otherwise it's ok */
1780 * For normal rooms, just check for aide or room aide status.
1782 return(is_room_aide());
1786 * aide command: kill the current room
1788 void cmd_kill(char *argbuf)
1790 char deleted_room_name[ROOMNAMELEN];
1794 kill_ok = extract_int(argbuf, 0);
1796 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
1797 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1801 if (CC->room.QRflags & QR_MAILBOX) {
1802 safestrncpy(deleted_room_name, &CC->room.QRname[11], sizeof deleted_room_name);
1805 safestrncpy(deleted_room_name, CC->room.QRname, sizeof deleted_room_name);
1808 /* Do the dirty work */
1809 CtdlScheduleRoomForDeletion(&CC->room);
1811 /* Return to the Lobby */
1812 CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
1814 /* tell the world what we did */
1815 snprintf(msg, sizeof msg, "The room \"%s\" has been deleted by %s.\n",
1816 deleted_room_name, CC->curr_user);
1817 CtdlAideMessage(msg, "Room Purger Message");
1818 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1820 cprintf("%d ok to delete.\n", CIT_OK);
1826 * Internal code to create a new room (returns room flags)
1828 * Room types: 0=public, 1=hidden, 2=passworded, 3=invitation-only,
1829 * 4=mailbox, 5=mailbox, but caller supplies namespace
1831 unsigned CtdlCreateRoom(char *new_room_name,
1833 char *new_room_pass,
1840 struct ctdlroom qrbuf;
1844 CtdlLogPrintf(CTDL_DEBUG, "CtdlCreateRoom(name=%s, type=%d, view=%d)\n",
1845 new_room_name, new_room_type, new_room_view);
1847 if (CtdlGetRoom(&qrbuf, new_room_name) == 0) {
1848 CtdlLogPrintf(CTDL_DEBUG, "%s already exists.\n", new_room_name);
1852 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1853 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1854 qrbuf.QRflags = QR_INUSE;
1855 if (new_room_type > 0)
1856 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1857 if (new_room_type == 1)
1858 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1859 if (new_room_type == 2)
1860 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1861 if ( (new_room_type == 4) || (new_room_type == 5) ) {
1862 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1863 /* qrbuf.QRflags2 |= QR2_SUBJECTREQ; */
1866 /* If the user is requesting a personal room, set up the room
1867 * name accordingly (prepend the user number)
1869 if (new_room_type == 4) {
1870 CtdlMailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1873 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1876 /* If the room is private, and the system administrator has elected
1877 * to automatically grant room aide privileges, do so now; otherwise,
1878 * set the room aide to undefined.
1880 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1881 qrbuf.QRroomaide = CC->user.usernum;
1883 qrbuf.QRroomaide = (-1L);
1887 * If the caller is only interested in testing whether this will work,
1888 * return now without creating the room.
1890 if (!really_create) return (qrbuf.QRflags);
1892 qrbuf.QRnumber = get_new_room_number();
1893 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1894 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1895 qrbuf.QRfloor = new_room_floor;
1896 qrbuf.QRdefaultview = new_room_view;
1898 /* save what we just did... */
1899 CtdlPutRoom(&qrbuf);
1901 /* bump the reference count on whatever floor the room is on */
1902 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1903 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1904 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1906 /* Grant the creator access to the room unless the avoid_access
1907 * parameter was specified.
1909 if ( (CC->logged_in) && (avoid_access == 0) ) {
1910 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1911 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1912 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1913 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1916 /* resume our happy day */
1917 return (qrbuf.QRflags);
1924 void cmd_cre8(char *args)
1927 char new_room_name[ROOMNAMELEN];
1929 char new_room_pass[32];
1932 char *notification_message = NULL;
1935 int avoid_access = 0;
1937 cre8_ok = extract_int(args, 0);
1938 extract_token(new_room_name, args, 1, '|', sizeof new_room_name);
1939 new_room_name[ROOMNAMELEN - 1] = 0;
1940 new_room_type = extract_int(args, 2);
1941 extract_token(new_room_pass, args, 3, '|', sizeof new_room_pass);
1942 avoid_access = extract_int(args, 5);
1943 new_room_view = extract_int(args, 6);
1944 new_room_pass[9] = 0;
1947 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 1)) {
1948 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
1952 if (!strcasecmp(new_room_name, MAILROOM)) {
1953 cprintf("%d '%s' already exists.\n",
1954 ERROR + ALREADY_EXISTS, new_room_name);
1958 if (num_parms(args) >= 5) {
1959 fl = CtdlGetCachedFloor(extract_int(args, 4));
1961 cprintf("%d Invalid floor number.\n",
1962 ERROR + INVALID_FLOOR_OPERATION);
1965 else if ((fl->f_flags & F_INUSE) == 0) {
1966 cprintf("%d Invalid floor number.\n",
1967 ERROR + INVALID_FLOOR_OPERATION);
1970 new_room_floor = extract_int(args, 4);
1974 if (CtdlAccessCheck(ac_logged_in)) return;
1976 if (CC->user.axlevel < config.c_createax && !CC->internal_pgm) {
1977 cprintf("%d You need higher access to create rooms.\n",
1978 ERROR + HIGHER_ACCESS_REQUIRED);
1982 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 0)) {
1983 cprintf("%d Ok to create rooms.\n", CIT_OK);
1987 if ((new_room_type < 0) || (new_room_type > 5)) {
1988 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
1992 if (new_room_type == 5) {
1993 if (CC->user.axlevel < AxAideU) {
1994 cprintf("%d Higher access required\n",
1995 ERROR + HIGHER_ACCESS_REQUIRED);
2000 /* Check to make sure the requested room name doesn't already exist */
2001 newflags = CtdlCreateRoom(new_room_name,
2002 new_room_type, new_room_pass, new_room_floor,
2003 0, avoid_access, new_room_view);
2004 if (newflags == 0) {
2005 cprintf("%d '%s' already exists.\n",
2006 ERROR + ALREADY_EXISTS, new_room_name);
2011 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
2015 /* If we reach this point, the room needs to be created. */
2017 newflags = CtdlCreateRoom(new_room_name,
2018 new_room_type, new_room_pass, new_room_floor, 1, 0,
2021 /* post a message in Aide> describing the new room */
2022 notification_message = malloc(1024);
2023 snprintf(notification_message, 1024,
2024 "A new room called \"%s\" has been created by %s%s%s%s%s%s\n",
2027 ((newflags & QR_MAILBOX) ? " [personal]" : ""),
2028 ((newflags & QR_PRIVATE) ? " [private]" : ""),
2029 ((newflags & QR_GUESSNAME) ? " [hidden]" : ""),
2030 ((newflags & QR_PASSWORDED) ? " Password: " : ""),
2031 ((newflags & QR_PASSWORDED) ? new_room_pass : "")
2033 CtdlAideMessage(notification_message, "Room Creation Message");
2034 free(notification_message);
2036 cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
2041 void cmd_einf(char *ok)
2042 { /* enter info file for current room */
2044 char infofilename[SIZ];
2049 if (CtdlAccessCheck(ac_room_aide)) return;
2051 if (atoi(ok) == 0) {
2052 cprintf("%d Ok.\n", CIT_OK);
2055 assoc_file_name(infofilename, sizeof infofilename, &CC->room, ctdl_info_dir);
2056 CtdlLogPrintf(CTDL_DEBUG, "opening\n");
2057 fp = fopen(infofilename, "w");
2058 CtdlLogPrintf(CTDL_DEBUG, "checking\n");
2060 cprintf("%d Cannot open %s: %s\n",
2061 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
2064 cprintf("%d Send info...\n", SEND_LISTING);
2067 client_getln(buf, sizeof buf);
2068 if (strcmp(buf, "000"))
2069 fprintf(fp, "%s\n", buf);
2070 } while (strcmp(buf, "000"));
2073 /* now update the room index so people will see our new info */
2074 CtdlGetRoomLock(&CC->room, CC->room.QRname); /* lock so no one steps on us */
2075 CC->room.QRinfo = CC->room.QRhighest + 1L;
2076 CtdlPutRoomLock(&CC->room);
2081 * cmd_lflr() - List all known floors
2083 void cmd_lflr(char *gargs)
2088 if (CtdlAccessCheck(ac_logged_in)) return;
2090 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
2092 for (a = 0; a < MAXFLOORS; ++a) {
2093 CtdlGetFloor(&flbuf, a);
2094 if (flbuf.f_flags & F_INUSE) {
2095 cprintf("%d|%s|%d\n",
2107 * create a new floor
2109 void cmd_cflr(char *argbuf)
2111 char new_floor_name[256];
2114 int free_slot = (-1);
2117 extract_token(new_floor_name, argbuf, 0, '|', sizeof new_floor_name);
2118 cflr_ok = extract_int(argbuf, 1);
2120 if (CtdlAccessCheck(ac_aide)) return;
2122 if (IsEmptyStr(new_floor_name)) {
2123 cprintf("%d Blank floor name not allowed.\n",
2124 ERROR + ILLEGAL_VALUE);
2128 for (a = 0; a < MAXFLOORS; ++a) {
2129 CtdlGetFloor(&flbuf, a);
2131 /* note any free slots while we're scanning... */
2132 if (((flbuf.f_flags & F_INUSE) == 0)
2136 /* check to see if it already exists */
2137 if ((!strcasecmp(flbuf.f_name, new_floor_name))
2138 && (flbuf.f_flags & F_INUSE)) {
2139 cprintf("%d Floor '%s' already exists.\n",
2140 ERROR + ALREADY_EXISTS,
2146 if (free_slot < 0) {
2147 cprintf("%d There is no space available for a new floor.\n",
2148 ERROR + INVALID_FLOOR_OPERATION);
2152 cprintf("%d ok to create...\n", CIT_OK);
2155 lgetfloor(&flbuf, free_slot);
2156 flbuf.f_flags = F_INUSE;
2157 flbuf.f_ref_count = 0;
2158 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
2159 lputfloor(&flbuf, free_slot);
2160 cprintf("%d %d\n", CIT_OK, free_slot);
2168 void cmd_kflr(char *argbuf)
2171 int floor_to_delete;
2175 floor_to_delete = extract_int(argbuf, 0);
2176 kflr_ok = extract_int(argbuf, 1);
2178 if (CtdlAccessCheck(ac_aide)) return;
2180 lgetfloor(&flbuf, floor_to_delete);
2183 if ((flbuf.f_flags & F_INUSE) == 0) {
2184 cprintf("%d Floor %d not in use.\n",
2185 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
2188 if (flbuf.f_ref_count != 0) {
2189 cprintf("%d Cannot delete; floor contains %d rooms.\n",
2190 ERROR + INVALID_FLOOR_OPERATION,
2195 cprintf("%d Ok\n", CIT_OK);
2197 cprintf("%d Ok to delete...\n", CIT_OK);
2204 if ((delete_ok == 1) && (kflr_ok == 1))
2206 lputfloor(&flbuf, floor_to_delete);
2212 void cmd_eflr(char *argbuf)
2218 np = num_parms(argbuf);
2220 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
2224 if (CtdlAccessCheck(ac_aide)) return;
2226 floor_num = extract_int(argbuf, 0);
2227 lgetfloor(&flbuf, floor_num);
2228 if ((flbuf.f_flags & F_INUSE) == 0) {
2229 lputfloor(&flbuf, floor_num);
2230 cprintf("%d Floor %d is not in use.\n",
2231 ERROR + INVALID_FLOOR_OPERATION, floor_num);
2235 extract_token(flbuf.f_name, argbuf, 1, '|', sizeof flbuf.f_name);
2236 lputfloor(&flbuf, floor_num);
2238 cprintf("%d Ok\n", CIT_OK);
2242 /*****************************************************************************/
2243 /* MODULE INITIALIZATION STUFF */
2244 /*****************************************************************************/
2246 CTDL_MODULE_INIT(room_ops)
2249 CtdlRegisterProtoHook(cmd_lrms, "LRMS", "Autoconverted. TODO: document me.");
2250 CtdlRegisterProtoHook(cmd_lkra, "LKRA", "Autoconverted. TODO: document me.");
2251 CtdlRegisterProtoHook(cmd_lkrn, "LKRN", "Autoconverted. TODO: document me.");
2252 CtdlRegisterProtoHook(cmd_lkro, "LKRO", "Autoconverted. TODO: document me.");
2253 CtdlRegisterProtoHook(cmd_lzrm, "LZRM", "Autoconverted. TODO: document me.");
2254 CtdlRegisterProtoHook(cmd_lprm, "LPRM", "Autoconverted. TODO: document me.");
2255 CtdlRegisterProtoHook(cmd_goto, "GOTO", "Autoconverted. TODO: document me.");
2256 CtdlRegisterProtoHook(cmd_whok, "WHOK", "Autoconverted. TODO: document me.");
2257 CtdlRegisterProtoHook(cmd_rdir, "RDIR", "Autoconverted. TODO: document me.");
2258 CtdlRegisterProtoHook(cmd_getr, "GETR", "Autoconverted. TODO: document me.");
2259 CtdlRegisterProtoHook(cmd_setr, "SETR", "Autoconverted. TODO: document me.");
2260 CtdlRegisterProtoHook(cmd_geta, "GETA", "Autoconverted. TODO: document me.");
2261 CtdlRegisterProtoHook(cmd_seta, "SETA", "Autoconverted. TODO: document me.");
2262 CtdlRegisterProtoHook(cmd_rinf, "RINF", "Autoconverted. TODO: document me.");
2263 CtdlRegisterProtoHook(cmd_kill, "KILL", "Autoconverted. TODO: document me.");
2264 CtdlRegisterProtoHook(cmd_cre8, "CRE8", "Autoconverted. TODO: document me.");
2265 CtdlRegisterProtoHook(cmd_einf, "EINF", "Autoconverted. TODO: document me.");
2266 CtdlRegisterProtoHook(cmd_lflr, "LFLR", "Autoconverted. TODO: document me.");
2267 CtdlRegisterProtoHook(cmd_cflr, "CFLR", "Autoconverted. TODO: document me.");
2268 CtdlRegisterProtoHook(cmd_kflr, "KFLR", "Autoconverted. TODO: document me.");
2269 CtdlRegisterProtoHook(cmd_eflr, "EFLR", "Autoconverted. TODO: document me.");
2271 /* return our Subversion id for the Log */