2 * Server functions which perform operations on room objects.
4 * Copyright (c) 1987-2011 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <dirent.h> /* for cmd_rdir to read contents of the directory */
30 #if TIME_WITH_SYS_TIME
31 # include <sys/time.h>
35 # include <sys/time.h>
44 #include <libcitadel.h>
49 #include "sysdep_decls.h"
52 #include "citserver.h"
54 #include "citadel_dirs.h"
57 #include "ctdl_module.h"
60 struct floor *floorcache[MAXFLOORS];
63 * Retrieve access control information for any user/room pair
65 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf,
66 int *result, int *view)
73 if (userbuf == &CC->user) {
77 if ((is_me) && (config.c_guest_logins) && (!CC->logged_in)) {
81 /* for internal programs, always do everything */
82 if (((CC->internal_pgm)) && (roombuf->QRflags & QR_INUSE)) {
83 retval = (UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED);
88 /* If guest mode is enabled, always grant access to the Lobby */
89 if ((is_guest) && (!strcasecmp(roombuf->QRname, BASEROOM))) {
90 retval = (UA_KNOWN | UA_GOTOALLOWED);
95 /* Locate any applicable user/room relationships */
97 memset(&vbuf, 0, sizeof vbuf);
100 CtdlGetRelationship(&vbuf, userbuf, roombuf);
103 /* Force the properties of the Aide room */
104 if (!strcasecmp(roombuf->QRname, config.c_aideroom)) {
105 if (userbuf->axlevel >= AxAideU) {
106 retval = UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED;
113 /* If this is a public room, it's accessible... */
114 if ( ((roombuf->QRflags & QR_PRIVATE) == 0)
115 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
116 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
119 /* If this is a preferred users only room, check access level */
120 if (roombuf->QRflags & QR_PREFONLY) {
121 if (userbuf->axlevel < AxPrefU) {
122 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
126 /* For private rooms, check the generation number matchups */
127 if ( (roombuf->QRflags & QR_PRIVATE)
128 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
130 /* An explicit match means the user belongs in this room */
131 if (vbuf.v_flags & V_ACCESS) {
132 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
134 /* Otherwise, check if this is a guess-name or passworded
135 * room. If it is, a goto may at least be attempted
137 else if ((roombuf->QRflags & QR_PRIVATE)
138 || (roombuf->QRflags & QR_PASSWORDED)) {
139 retval = retval & ~UA_KNOWN;
140 retval = retval | UA_GOTOALLOWED;
144 /* For mailbox rooms, also check the namespace */
145 /* Also, mailbox owners can delete their messages */
146 if (roombuf->QRflags & QR_MAILBOX) {
147 if (userbuf->usernum == atol(roombuf->QRname)) {
148 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED;
150 /* An explicit match means the user belongs in this room */
151 if (vbuf.v_flags & V_ACCESS) {
152 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED;
156 /* For non-mailbox rooms... */
159 /* User is allowed to post in the room unless:
160 * - User is not validated
161 * - User has no net privileges and it is a shared network room
162 * - It is a read-only room
164 int post_allowed = 1;
165 int reply_allowed = 1;
166 if (userbuf->axlevel < AxProbU) {
170 if ((userbuf->axlevel < AxNetU) && (roombuf->QRflags & QR_NETWORK)) {
174 if (roombuf->QRflags & QR_READONLY) {
179 retval = retval | UA_POSTALLOWED | UA_REPLYALLOWED;
182 retval = retval | UA_REPLYALLOWED;
185 /* If "collaborative deletion" is active for this room, any user who can post
186 * is also allowed to delete
188 if (roombuf->QRflags2 & QR2_COLLABDEL) {
189 if (retval & UA_POSTALLOWED) {
190 retval = retval | UA_DELETEALLOWED;
196 /* Check to see if the user has forgotten this room */
197 if (vbuf.v_flags & V_FORGET) {
198 retval = retval & ~UA_KNOWN;
199 if ( ( ((roombuf->QRflags & QR_PRIVATE) == 0)
200 && ((roombuf->QRflags & QR_MAILBOX) == 0) )
201 || ( (roombuf->QRflags & QR_MAILBOX)
202 && (atol(roombuf->QRname) == CC->user.usernum))) {
203 retval = retval | UA_ZAPPED;
206 /* If user is explicitly locked out of this room, deny everything */
207 if (vbuf.v_flags & V_LOCKOUT) {
208 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED & ~UA_POSTALLOWED & ~UA_REPLYALLOWED;
211 /* Aides get access to all private rooms */
212 if ( (userbuf->axlevel >= AxAideU)
213 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
214 if (vbuf.v_flags & V_FORGET) {
215 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
218 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
222 /* Aides can gain access to mailboxes as well, but they don't show
225 if ( (userbuf->axlevel >= AxAideU)
226 && (roombuf->QRflags & QR_MAILBOX) ) {
227 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
230 /* Aides and Room Aides have admin privileges */
231 if ( (userbuf->axlevel >= AxAideU)
232 || (userbuf->usernum == roombuf->QRroomaide)
234 retval = retval | UA_ADMINALLOWED | UA_DELETEALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
237 NEWMSG: /* By the way, we also check for the presence of new messages */
238 if (is_msg_in_sequence_set(vbuf.v_seen, roombuf->QRhighest) == 0) {
239 retval = retval | UA_HASNEWMSGS;
242 /* System rooms never show up in the list. */
243 if (roombuf->QRflags2 & QR2_SYSTEM) {
244 retval = retval & ~UA_KNOWN;
248 /* Now give the caller the information it wants. */
249 if (result != NULL) *result = retval;
250 if (view != NULL) *view = vbuf.v_view;
254 * Self-checking stuff for a room record read into memory
256 void room_sanity_check(struct ctdlroom *qrbuf)
258 /* Mailbox rooms are always on the lowest floor */
259 if (qrbuf->QRflags & QR_MAILBOX) {
262 /* Listing order of 0 is illegal except for base rooms */
263 if (qrbuf->QRorder == 0)
264 if (!(qrbuf->QRflags & QR_MAILBOX) &&
265 strncasecmp(qrbuf->QRname, config.c_baseroom, ROOMNAMELEN)
267 strncasecmp(qrbuf->QRname, config.c_aideroom, ROOMNAMELEN))
273 * CtdlGetRoom() - retrieve room data from disk
275 int CtdlGetRoom(struct ctdlroom *qrbuf, char *room_name)
277 struct cdbdata *cdbqr;
278 char lowercase_name[ROOMNAMELEN];
279 char personal_lowercase_name[ROOMNAMELEN];
280 char *dptr, *sptr, *eptr;
282 dptr = lowercase_name;
284 eptr = (dptr + (sizeof lowercase_name - 1));
285 while (!IsEmptyStr(sptr) && (dptr < eptr)){
286 *dptr = tolower(*sptr);
291 memset(qrbuf, 0, sizeof(struct ctdlroom));
293 /* First, try the public namespace */
294 cdbqr = cdb_fetch(CDB_ROOMS,
295 lowercase_name, strlen(lowercase_name));
297 /* If that didn't work, try the user's personal namespace */
299 snprintf(personal_lowercase_name,
300 sizeof personal_lowercase_name, "%010ld.%s",
301 CC->user.usernum, lowercase_name);
302 cdbqr = cdb_fetch(CDB_ROOMS,
303 personal_lowercase_name,
304 strlen(personal_lowercase_name));
307 memcpy(qrbuf, cdbqr->ptr,
308 ((cdbqr->len > sizeof(struct ctdlroom)) ?
309 sizeof(struct ctdlroom) : cdbqr->len));
312 room_sanity_check(qrbuf);
321 * CtdlGetRoomLock() - same as getroom() but locks the record (if supported)
323 int CtdlGetRoomLock(struct ctdlroom *qrbuf, char *room_name)
326 retval = CtdlGetRoom(qrbuf, room_name);
327 if (retval == 0) begin_critical_section(S_ROOMS);
333 * b_putroom() - back end to putroom() and b_deleteroom()
334 * (if the supplied buffer is NULL, delete the room record)
336 void b_putroom(struct ctdlroom *qrbuf, char *room_name)
338 char lowercase_name[ROOMNAMELEN];
343 bptr = lowercase_name;
344 while (!IsEmptyStr(aptr))
346 *bptr = tolower(*aptr);
352 len = bptr - lowercase_name;
354 cdb_delete(CDB_ROOMS,
355 lowercase_name, len);
357 time(&qrbuf->QRmtime);
360 qrbuf, sizeof(struct ctdlroom));
366 * CtdlPutRoom() - store room data to disk
368 void CtdlPutRoom(struct ctdlroom *qrbuf) {
369 b_putroom(qrbuf, qrbuf->QRname);
374 * b_deleteroom() - delete a room record from disk
376 void b_deleteroom(char *room_name) {
377 b_putroom(NULL, room_name);
383 * CtdlPutRoomLock() - same as CtdlPutRoom() but unlocks the record (if supported)
385 void CtdlPutRoomLock(struct ctdlroom *qrbuf)
389 end_critical_section(S_ROOMS);
393 /****************************************************************************/
397 * CtdlGetFloorByName() - retrieve the number of the named floor
398 * return < 0 if not found else return floor number
400 int CtdlGetFloorByName(const char *floor_name)
403 struct floor *flbuf = NULL;
405 for (a = 0; a < MAXFLOORS; ++a) {
406 flbuf = CtdlGetCachedFloor(a);
408 /* check to see if it already exists */
409 if ((!strcasecmp(flbuf->f_name, floor_name))
410 && (flbuf->f_flags & F_INUSE)) {
420 * CtdlGetFloorByNameLock() - retrieve floor number for given floor and lock the floor list.
422 int CtdlGetFloorByNameLock(const char *floor_name)
424 begin_critical_section(S_FLOORTAB);
425 return CtdlGetFloorByName(floor_name);
431 * CtdlGetAvailableFloor() - Return number of first unused floor
432 * return < 0 if none available
434 int CtdlGetAvailableFloor(void)
437 struct floor *flbuf = NULL;
439 for (a = 0; a < MAXFLOORS; a++) {
440 flbuf = CtdlGetCachedFloor(a);
442 /* check to see if it already exists */
443 if ((flbuf->f_flags & F_INUSE) == 0) {
452 * CtdlGetFloor() - retrieve floor data from disk
454 void CtdlGetFloor(struct floor *flbuf, int floor_num)
456 struct cdbdata *cdbfl;
458 memset(flbuf, 0, sizeof(struct floor));
459 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
461 memcpy(flbuf, cdbfl->ptr,
462 ((cdbfl->len > sizeof(struct floor)) ?
463 sizeof(struct floor) : cdbfl->len));
466 if (floor_num == 0) {
467 safestrncpy(flbuf->f_name, "Main Floor",
468 sizeof flbuf->f_name);
469 flbuf->f_flags = F_INUSE;
470 flbuf->f_ref_count = 3;
477 * lgetfloor() - same as CtdlGetFloor() but locks the record (if supported)
479 void lgetfloor(struct floor *flbuf, int floor_num)
482 begin_critical_section(S_FLOORTAB);
483 CtdlGetFloor(flbuf, floor_num);
488 * CtdlGetCachedFloor() - Get floor record from *cache* (loads from disk if needed)
490 * This is strictly a performance hack.
492 struct floor *CtdlGetCachedFloor(int floor_num) {
493 static int initialized = 0;
496 struct floor *fl = NULL;
498 begin_critical_section(S_FLOORCACHE);
499 if (initialized == 0) {
500 for (i=0; i<MAXFLOORS; ++i) {
501 floorcache[floor_num] = NULL;
505 if (floorcache[floor_num] == NULL) {
508 end_critical_section(S_FLOORCACHE);
511 fl = malloc(sizeof(struct floor));
512 CtdlGetFloor(fl, floor_num);
513 begin_critical_section(S_FLOORCACHE);
514 if (floorcache[floor_num] != NULL) {
515 free(floorcache[floor_num]);
517 floorcache[floor_num] = fl;
518 end_critical_section(S_FLOORCACHE);
521 return(floorcache[floor_num]);
527 * CtdlPutFloor() - store floor data on disk
529 void CtdlPutFloor(struct floor *flbuf, int floor_num)
531 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
532 begin_critical_section(S_FLOORCACHE);
533 if (floorcache[floor_num] != NULL) {
534 free(floorcache[floor_num]);
535 floorcache[floor_num] = malloc(sizeof(struct floor));
536 memcpy(floorcache[floor_num], flbuf, sizeof(struct floor));
538 end_critical_section(S_FLOORCACHE);
540 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
541 flbuf, sizeof(struct floor));
547 * CtdlPutFloorLock() - same as CtdlPutFloor() but unlocks the record (if supported)
549 void CtdlPutFloorLock(struct floor *flbuf, int floor_num)
552 CtdlPutFloor(flbuf, floor_num);
553 end_critical_section(S_FLOORTAB);
560 * lputfloor() - same as CtdlPutFloor() but unlocks the record (if supported)
562 void lputfloor(struct floor *flbuf, int floor_num)
564 CtdlPutFloorLock(flbuf, floor_num);
569 * Traverse the room file...
571 void CtdlForEachRoom(void (*CallBack) (struct ctdlroom *EachRoom, void *out_data),
574 struct ctdlroom qrbuf;
575 struct cdbdata *cdbqr;
577 cdb_rewind(CDB_ROOMS);
579 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
580 memset(&qrbuf, 0, sizeof(struct ctdlroom));
581 memcpy(&qrbuf, cdbqr->ptr,
582 ((cdbqr->len > sizeof(struct ctdlroom)) ?
583 sizeof(struct ctdlroom) : cdbqr->len));
585 room_sanity_check(&qrbuf);
586 if (qrbuf.QRflags & QR_INUSE)
587 (*CallBack)(&qrbuf, in_data);
593 * delete_msglist() - delete room message pointers
595 void delete_msglist(struct ctdlroom *whichroom)
597 struct cdbdata *cdbml;
599 /* Make sure the msglist we're deleting actually exists, otherwise
600 * gdbm will complain when we try to delete an invalid record
602 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
606 /* Go ahead and delete it */
607 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
614 * Message pointer compare function for sort_msglist()
616 int sort_msglist_cmp(const void *m1, const void *m2) {
617 if ((*(const long *)m1) > (*(const long *)m2)) return(1);
618 if ((*(const long *)m1) < (*(const long *)m2)) return(-1);
624 * sort message pointers
625 * (returns new msg count)
627 int sort_msglist(long listptrs[], int oldcount)
637 qsort(listptrs, numitems, sizeof(long), sort_msglist_cmp);
639 /* and yank any nulls */
640 while ((numitems > 0) && (listptrs[0] == 0L)) {
641 memmove(&listptrs[0], &listptrs[1],
642 (sizeof(long) * (numitems - 1)));
651 * Determine whether a given room is non-editable.
653 int CtdlIsNonEditable(struct ctdlroom *qrbuf)
656 /* Mail> rooms are non-editable */
657 if ( (qrbuf->QRflags & QR_MAILBOX)
658 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
661 /* Everything else is editable */
668 * Back-back-end for all room listing commands
670 void list_roomname(struct ctdlroom *qrbuf, int ra, int current_view, int default_view)
672 char truncated_roomname[ROOMNAMELEN];
674 /* For my own mailbox rooms, chop off the owner prefix */
675 if ( (qrbuf->QRflags & QR_MAILBOX)
676 && (atol(qrbuf->QRname) == CC->user.usernum) ) {
677 safestrncpy(truncated_roomname, qrbuf->QRname, sizeof truncated_roomname);
678 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
679 cprintf("%s", truncated_roomname);
681 /* For all other rooms, just display the name in its entirety */
683 cprintf("%s", qrbuf->QRname);
686 /* ...and now the other parameters */
687 cprintf("|%u|%d|%d|%d|%d|%d|%d|%ld|\n",
689 (int) qrbuf->QRfloor,
690 (int) qrbuf->QRorder,
691 (int) qrbuf->QRflags2,
701 * cmd_lrms() - List all accessible rooms, known or forgotten
703 void cmd_lrms_backend(struct ctdlroom *qrbuf, void *data)
705 int FloorBeingSearched = (-1);
709 FloorBeingSearched = *(int *)data;
710 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
712 if ((( ra & (UA_KNOWN | UA_ZAPPED)))
713 && ((qrbuf->QRfloor == (FloorBeingSearched))
714 || ((FloorBeingSearched) < 0)))
715 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
718 void cmd_lrms(char *argbuf)
720 int FloorBeingSearched = (-1);
721 if (!IsEmptyStr(argbuf))
722 FloorBeingSearched = extract_int(argbuf, 0);
724 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
726 CtdlGetUser(&CC->user, CC->curr_user);
727 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
729 CtdlForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
736 * cmd_lkra() - List all known rooms
738 void cmd_lkra_backend(struct ctdlroom *qrbuf, void *data)
740 int FloorBeingSearched = (-1);
744 FloorBeingSearched = *(int *)data;
745 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
747 if ((( ra & (UA_KNOWN)))
748 && ((qrbuf->QRfloor == (FloorBeingSearched))
749 || ((FloorBeingSearched) < 0)))
750 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
753 void cmd_lkra(char *argbuf)
755 int FloorBeingSearched = (-1);
756 if (!IsEmptyStr(argbuf))
757 FloorBeingSearched = extract_int(argbuf, 0);
759 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
761 CtdlGetUser(&CC->user, CC->curr_user);
762 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
764 CtdlForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
770 void cmd_lprm_backend(struct ctdlroom *qrbuf, void *data)
772 int FloorBeingSearched = (-1);
776 FloorBeingSearched = *(int *)data;
777 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
779 if ( ((qrbuf->QRflags & QR_PRIVATE) == 0)
780 && ((qrbuf->QRflags & QR_MAILBOX) == 0)
781 && ((qrbuf->QRfloor == (FloorBeingSearched))
782 || ((FloorBeingSearched) < 0)))
783 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
786 void cmd_lprm(char *argbuf)
788 int FloorBeingSearched = (-1);
789 if (!IsEmptyStr(argbuf))
790 FloorBeingSearched = extract_int(argbuf, 0);
792 cprintf("%d Public rooms:\n", LISTING_FOLLOWS);
794 CtdlForEachRoom(cmd_lprm_backend, &FloorBeingSearched);
801 * cmd_lkrn() - List all known rooms with new messages
803 void cmd_lkrn_backend(struct ctdlroom *qrbuf, void *data)
805 int FloorBeingSearched = (-1);
809 FloorBeingSearched = *(int *)data;
810 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
813 && (ra & UA_HASNEWMSGS)
814 && ((qrbuf->QRfloor == (FloorBeingSearched))
815 || ((FloorBeingSearched) < 0)))
816 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
819 void cmd_lkrn(char *argbuf)
821 int FloorBeingSearched = (-1);
822 if (!IsEmptyStr(argbuf))
823 FloorBeingSearched = extract_int(argbuf, 0);
825 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
827 CtdlGetUser(&CC->user, CC->curr_user);
828 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
830 CtdlForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
837 * cmd_lkro() - List all known rooms
839 void cmd_lkro_backend(struct ctdlroom *qrbuf, void *data)
841 int FloorBeingSearched = (-1);
845 FloorBeingSearched = *(int *)data;
846 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
849 && ((ra & UA_HASNEWMSGS) == 0)
850 && ((qrbuf->QRfloor == (FloorBeingSearched))
851 || ((FloorBeingSearched) < 0)))
852 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
855 void cmd_lkro(char *argbuf)
857 int FloorBeingSearched = (-1);
858 if (!IsEmptyStr(argbuf))
859 FloorBeingSearched = extract_int(argbuf, 0);
861 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
863 CtdlGetUser(&CC->user, CC->curr_user);
864 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
866 CtdlForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
873 * cmd_lzrm() - List all forgotten rooms
875 void cmd_lzrm_backend(struct ctdlroom *qrbuf, void *data)
877 int FloorBeingSearched = (-1);
881 FloorBeingSearched = *(int *)data;
882 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
884 if ((ra & UA_GOTOALLOWED)
886 && ((qrbuf->QRfloor == (FloorBeingSearched))
887 || ((FloorBeingSearched) < 0)))
888 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
891 void cmd_lzrm(char *argbuf)
893 int FloorBeingSearched = (-1);
894 if (!IsEmptyStr(argbuf))
895 FloorBeingSearched = extract_int(argbuf, 0);
897 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
899 CtdlGetUser(&CC->user, CC->curr_user);
900 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
902 CtdlForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
908 * Make the specified room the current room for this session. No validation
909 * or access control is done here -- the caller should make sure that the
910 * specified room exists and is ok to access.
912 void CtdlUserGoto(char *where, int display_result, int transiently,
913 int *retmsgs, int *retnew)
916 int new_messages = 0;
917 int old_messages = 0;
918 int total_messages = 0;
922 int newmailcount = 0;
924 char truncated_roomname[ROOMNAMELEN];
925 struct cdbdata *cdbfr;
926 long *msglist = NULL;
928 unsigned int original_v_flags;
931 char setstr[128], lostr[64], histr[64];
935 /* If the supplied room name is NULL, the caller wants us to know that
936 * it has already copied the room record into CC->room, so
937 * we can skip the extra database fetch.
940 safestrncpy(CC->room.QRname, where, sizeof CC->room.QRname);
941 CtdlGetRoom(&CC->room, where);
944 /* Take care of all the formalities. */
946 begin_critical_section(S_USERS);
947 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
948 original_v_flags = vbuf.v_flags;
950 /* Know the room ... but not if it's the page log room, or if the
951 * caller specified that we're only entering this room transiently.
953 if ((strcasecmp(CC->room.QRname, config.c_logpages))
954 && (transiently == 0) ) {
955 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
956 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
959 /* Only rewrite the database record if we changed something */
960 if (vbuf.v_flags != original_v_flags) {
961 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
963 end_critical_section(S_USERS);
965 /* Check for new mail */
966 newmailcount = NewMailCount();
968 /* set info to 1 if the user needs to read the room's info file */
969 if (CC->room.QRinfo > vbuf.v_lastseen) {
973 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
975 msglist = (long *) cdbfr->ptr;
976 cdbfr->ptr = NULL; /* CtdlUserGoto() now owns this memory */
977 num_msgs = cdbfr->len / sizeof(long);
982 for (a=0; a<num_msgs; ++a) {
983 if (msglist[a] > 0L) ++total_messages;
985 new_messages = num_msgs;
986 num_sets = num_tokens(vbuf.v_seen, ',');
987 for (s=0; s<num_sets; ++s) {
988 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
990 extract_token(lostr, setstr, 0, ':', sizeof lostr);
991 if (num_tokens(setstr, ':') >= 2) {
992 extract_token(histr, setstr, 1, ':', sizeof histr);
993 if (!strcmp(histr, "*")) {
994 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
998 strcpy(histr, lostr);
1003 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
1004 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
1010 new_messages = total_messages - old_messages;
1012 if (msglist != NULL) free(msglist);
1014 if (CC->room.QRflags & QR_MAILBOX)
1019 if ((CC->room.QRroomaide == CC->user.usernum)
1020 || (CC->user.axlevel >= AxAideU))
1025 safestrncpy(truncated_roomname, CC->room.QRname, sizeof truncated_roomname);
1026 if ( (CC->room.QRflags & QR_MAILBOX)
1027 && (atol(CC->room.QRname) == CC->user.usernum) ) {
1028 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
1031 if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
1035 if (retmsgs != NULL) *retmsgs = total_messages;
1036 if (retnew != NULL) *retnew = new_messages;
1037 syslog(LOG_DEBUG, "<%s> %d new of %d total messages\n",
1039 new_messages, total_messages
1042 CC->curr_view = (int)vbuf.v_view;
1044 if (display_result) {
1045 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|%d|\n",
1046 CIT_OK, CtdlCheckExpress(),
1049 (int)total_messages,
1051 (int)CC->room.QRflags,
1052 (long)CC->room.QRhighest,
1053 (long)vbuf.v_lastseen,
1057 (int)CC->room.QRfloor,
1059 (int)CC->room.QRdefaultview,
1061 (int)CC->room.QRflags2
1068 * Handle some of the macro named rooms
1070 void convert_room_name_macros(char *towhere, size_t maxlen) {
1071 if (!strcasecmp(towhere, "_BASEROOM_")) {
1072 safestrncpy(towhere, config.c_baseroom, maxlen);
1074 else if (!strcasecmp(towhere, "_MAIL_")) {
1075 safestrncpy(towhere, MAILROOM, maxlen);
1077 else if (!strcasecmp(towhere, "_TRASH_")) {
1078 safestrncpy(towhere, USERTRASHROOM, maxlen);
1080 else if (!strcasecmp(towhere, "_DRAFTS_")) {
1081 safestrncpy(towhere, USERDRAFTROOM, maxlen);
1083 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
1084 safestrncpy(towhere, config.c_twitroom, maxlen);
1086 else if (!strcasecmp(towhere, "_CALENDAR_")) {
1087 safestrncpy(towhere, USERCALENDARROOM, maxlen);
1089 else if (!strcasecmp(towhere, "_TASKS_")) {
1090 safestrncpy(towhere, USERTASKSROOM, maxlen);
1092 else if (!strcasecmp(towhere, "_CONTACTS_")) {
1093 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
1095 else if (!strcasecmp(towhere, "_NOTES_")) {
1096 safestrncpy(towhere, USERNOTESROOM, maxlen);
1102 * cmd_goto() - goto a new room
1104 void cmd_goto(char *gargs)
1106 struct ctdlroom QRscratch;
1110 char augmented_roomname[ROOMNAMELEN];
1111 char towhere[ROOMNAMELEN];
1113 int transiently = 0;
1115 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
1117 extract_token(towhere, gargs, 0, '|', sizeof towhere);
1118 extract_token(password, gargs, 1, '|', sizeof password);
1119 transiently = extract_int(gargs, 2);
1121 CtdlGetUser(&CC->user, CC->curr_user);
1124 * Handle some of the macro named rooms
1126 convert_room_name_macros(towhere, sizeof towhere);
1128 /* First try a regular match */
1129 c = CtdlGetRoom(&QRscratch, towhere);
1131 /* Then try a mailbox name match */
1133 CtdlMailboxName(augmented_roomname, sizeof augmented_roomname,
1134 &CC->user, towhere);
1135 c = CtdlGetRoom(&QRscratch, augmented_roomname);
1137 safestrncpy(towhere, augmented_roomname, sizeof towhere);
1140 /* And if the room was found... */
1143 /* Let internal programs go directly to any room. */
1144 if (CC->internal_pgm) {
1145 memcpy(&CC->room, &QRscratch,
1146 sizeof(struct ctdlroom));
1147 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1151 /* See if there is an existing user/room relationship */
1152 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
1154 /* normal clients have to pass through security */
1155 if (ra & UA_GOTOALLOWED) {
1160 if ((QRscratch.QRflags & QR_MAILBOX) &&
1161 ((ra & UA_GOTOALLOWED))) {
1162 memcpy(&CC->room, &QRscratch,
1163 sizeof(struct ctdlroom));
1164 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1166 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
1167 ((ra & UA_KNOWN) == 0) &&
1168 (strcasecmp(QRscratch.QRpasswd, password)) &&
1169 (CC->user.axlevel < AxAideU)
1171 cprintf("%d wrong or missing passwd\n",
1172 ERROR + PASSWORD_REQUIRED);
1174 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
1175 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
1176 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
1177 ((ra & UA_KNOWN) == 0) &&
1178 (CC->user.axlevel < AxAideU)
1180 syslog(LOG_DEBUG, "Failed to acquire private room\n");
1182 memcpy(&CC->room, &QRscratch,
1183 sizeof(struct ctdlroom));
1184 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1190 cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
1194 void cmd_whok(char *cmdbuf)
1196 struct ctdluser temp;
1197 struct cdbdata *cdbus;
1200 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
1201 cdb_rewind(CDB_USERS);
1202 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1203 memset(&temp, 0, sizeof temp);
1204 memcpy(&temp, cdbus->ptr, sizeof temp);
1207 CtdlRoomAccess(&CC->room, &temp, &ra, NULL);
1208 if ((!IsEmptyStr(temp.fullname)) &&
1209 (CC->room.QRflags & QR_INUSE) &&
1212 cprintf("%s\n", temp.fullname);
1219 * RDIR command for room directory
1221 void cmd_rdir(char *cmdbuf)
1226 struct stat statbuf;
1227 DIR *filedir = NULL;
1228 struct dirent *filedir_entry;
1234 if (CtdlAccessCheck(ac_logged_in)) return;
1236 CtdlGetRoom(&CC->room, CC->room.QRname);
1237 CtdlGetUser(&CC->user, CC->curr_user);
1239 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
1240 cprintf("%d not here.\n", ERROR + NOT_HERE);
1243 if (((CC->room.QRflags & QR_VISDIR) == 0)
1244 && (CC->user.axlevel < AxAideU)
1245 && (CC->user.usernum != CC->room.QRroomaide)) {
1246 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1250 snprintf(buf, sizeof buf, "%s/%s", ctdl_file_dir, CC->room.QRdirname);
1251 filedir = opendir (buf);
1253 if (filedir == NULL) {
1254 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1257 cprintf("%d %s|%s/%s\n", LISTING_FOLLOWS, config.c_fqdn, ctdl_file_dir, CC->room.QRdirname);
1259 snprintf(buf, sizeof buf, "%s/%s/filedir", ctdl_file_dir, CC->room.QRdirname);
1260 fd = fopen(buf, "r");
1262 fd = fopen("/dev/null", "r");
1263 while ((filedir_entry = readdir(filedir)))
1265 if (strcasecmp(filedir_entry->d_name, "filedir") && filedir_entry->d_name[0] != '.')
1267 #ifdef _DIRENT_HAVE_D_NAMELEN
1268 d_namelen = filedir_entry->d_namelen;
1270 d_namelen = strlen(filedir_entry->d_name);
1272 snprintf(buf, sizeof buf, "%s/%s/%s", ctdl_file_dir, CC->room.QRdirname, filedir_entry->d_name);
1273 stat(buf, &statbuf); /* stat the file */
1274 if (!(statbuf.st_mode & S_IFREG))
1276 snprintf(buf2, sizeof buf2,
1277 "\"%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",
1278 buf, CC->room.QRname
1280 CtdlAideMessage(buf2, "Unusable data found in room directory");
1281 continue; /* not a useable file type so don't show it */
1283 safestrncpy(comment, "", sizeof comment);
1284 fseek(fd, 0L, 0); /* rewind descriptions file */
1285 /* Get the description from the descriptions file */
1286 while ((fgets(buf, sizeof buf, fd) != NULL) && (IsEmptyStr(comment)))
1288 buf[strlen(buf) - 1] = 0;
1289 if ((!strncasecmp(buf, filedir_entry->d_name, d_namelen)) && (buf[d_namelen] == ' '))
1290 safestrncpy(comment, &buf[d_namelen + 1], sizeof comment);
1292 len = extract_token (mimebuf, comment, 0,' ', 64);
1293 if ((len <0) || strchr(mimebuf, '/') == NULL)
1295 snprintf (mimebuf, 64, "application/octetstream");
1298 cprintf("%s|%ld|%s|%s\n",
1299 filedir_entry->d_name,
1300 (long)statbuf.st_size,
1312 * get room parameters (aide or room aide command)
1314 void cmd_getr(char *cmdbuf)
1316 if (CtdlAccessCheck(ac_room_aide)) return;
1318 CtdlGetRoom(&CC->room, CC->room.QRname);
1319 cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1323 ((CC->room.QRflags & QR_MAILBOX) ?
1324 &CC->room.QRname[11] : CC->room.QRname),
1326 ((CC->room.QRflags & QR_PASSWORDED) ?
1327 CC->room.QRpasswd : ""),
1329 ((CC->room.QRflags & QR_DIRECTORY) ?
1330 CC->room.QRdirname : ""),
1333 (int) CC->room.QRfloor,
1334 (int) CC->room.QRorder,
1336 CC->room.QRdefaultview,
1343 * Back end function to rename a room.
1344 * You can also specify which floor to move the room to, or specify -1 to
1345 * keep the room on the same floor it was on.
1347 * If you are renaming a mailbox room, you must supply the namespace prefix
1348 * in *at least* the old name!
1350 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1352 struct ctdlroom qrbuf;
1353 struct ctdlroom qrtmp;
1358 char actual_old_name[ROOMNAMELEN];
1360 syslog(LOG_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1361 old_name, new_name, new_floor);
1363 if (new_floor >= 0) {
1364 fl = CtdlGetCachedFloor(new_floor);
1365 if ((fl->f_flags & F_INUSE) == 0) {
1366 return(crr_invalid_floor);
1370 begin_critical_section(S_ROOMS);
1372 if ( (CtdlGetRoom(&qrtmp, new_name) == 0)
1373 && (strcasecmp(new_name, old_name)) ) {
1374 ret = crr_already_exists;
1377 else if (CtdlGetRoom(&qrbuf, old_name) != 0) {
1378 ret = crr_room_not_found;
1381 else if ( (CC->user.axlevel < AxAideU) && (!CC->internal_pgm)
1382 && (CC->user.usernum != qrbuf.QRroomaide)
1383 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) ) {
1384 ret = crr_access_denied;
1387 else if (CtdlIsNonEditable(&qrbuf)) {
1388 ret = crr_noneditable;
1393 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1394 if (qrbuf.QRflags & QR_MAILBOX) {
1395 owner = atol(qrbuf.QRname);
1397 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1398 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1399 "%010ld.%s", owner, new_name);
1402 safestrncpy(qrbuf.QRname, new_name,
1403 sizeof(qrbuf.QRname));
1406 /* Reject change of floor for baseroom/aideroom */
1407 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1408 !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1412 /* Take care of floor stuff */
1413 old_floor = qrbuf.QRfloor;
1414 if (new_floor < 0) {
1415 new_floor = old_floor;
1417 qrbuf.QRfloor = new_floor;
1418 CtdlPutRoom(&qrbuf);
1420 begin_critical_section(S_CONFIG);
1422 /* If baseroom/aideroom name changes, update config */
1423 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1424 safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1427 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1428 safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1432 end_critical_section(S_CONFIG);
1434 /* If the room name changed, then there are now two room
1435 * records, so we have to delete the old one.
1437 if (strcasecmp(new_name, old_name)) {
1438 b_deleteroom(actual_old_name);
1444 end_critical_section(S_ROOMS);
1446 /* Adjust the floor reference counts if necessary */
1447 if (new_floor != old_floor) {
1448 lgetfloor(&flbuf, old_floor);
1449 --flbuf.f_ref_count;
1450 lputfloor(&flbuf, old_floor);
1451 syslog(LOG_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1452 lgetfloor(&flbuf, new_floor);
1453 ++flbuf.f_ref_count;
1454 lputfloor(&flbuf, new_floor);
1455 syslog(LOG_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1458 /* ...and everybody say "YATTA!" */
1464 * set room parameters (aide or room aide command)
1466 void cmd_setr(char *args)
1472 char new_name[ROOMNAMELEN];
1474 if (CtdlAccessCheck(ac_logged_in)) return;
1476 if (num_parms(args) >= 6) {
1477 new_floor = extract_int(args, 5);
1479 new_floor = (-1); /* don't change the floor */
1482 /* When is a new name more than just a new name? When the old name
1483 * has a namespace prefix.
1485 if (CC->room.QRflags & QR_MAILBOX) {
1486 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
1488 safestrncpy(new_name, "", sizeof new_name);
1490 extract_token(&new_name[strlen(new_name)], args, 0, '|', (sizeof new_name - strlen(new_name)));
1492 r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
1494 if (r == crr_room_not_found) {
1495 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
1496 } else if (r == crr_already_exists) {
1497 cprintf("%d '%s' already exists.\n",
1498 ERROR + ALREADY_EXISTS, new_name);
1499 } else if (r == crr_noneditable) {
1500 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
1501 } else if (r == crr_invalid_floor) {
1502 cprintf("%d Target floor does not exist.\n",
1503 ERROR + INVALID_FLOOR_OPERATION);
1504 } else if (r == crr_access_denied) {
1505 cprintf("%d You do not have permission to edit '%s'\n",
1506 ERROR + HIGHER_ACCESS_REQUIRED,
1508 } else if (r != crr_ok) {
1509 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1510 ERROR + INTERNAL_ERROR, r);
1517 CtdlGetRoom(&CC->room, new_name);
1519 /* Now we have to do a bunch of other stuff */
1521 if (num_parms(args) >= 7) {
1522 new_order = extract_int(args, 6);
1525 if (new_order > 127)
1529 CtdlGetRoomLock(&CC->room, CC->room.QRname);
1531 /* Directory room */
1532 extract_token(buf, args, 2, '|', sizeof buf);
1534 safestrncpy(CC->room.QRdirname, buf,
1535 sizeof CC->room.QRdirname);
1538 if (num_parms(args) >= 8) {
1539 CC->room.QRdefaultview = extract_int(args, 7);
1542 /* Second set of flags */
1543 if (num_parms(args) >= 9) {
1544 CC->room.QRflags2 = extract_int(args, 8);
1548 CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
1549 /* Clean up a client boo-boo: if the client set the room to
1550 * guess-name or passworded, ensure that the private flag is
1553 if ((CC->room.QRflags & QR_GUESSNAME)
1554 || (CC->room.QRflags & QR_PASSWORDED))
1555 CC->room.QRflags |= QR_PRIVATE;
1557 /* Some changes can't apply to BASEROOM */
1558 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1560 CC->room.QRorder = 0;
1561 CC->room.QRpasswd[0] = '\0';
1562 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1563 QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1564 CC->room.QRflags |= QR_PERMANENT;
1566 /* March order (doesn't apply to AIDEROOM) */
1567 if (num_parms(args) >= 7)
1568 CC->room.QRorder = (char) new_order;
1570 extract_token(buf, args, 1, '|', sizeof buf);
1572 safestrncpy(CC->room.QRpasswd, buf,
1573 sizeof CC->room.QRpasswd);
1574 /* Kick everyone out if the client requested it
1575 * (by changing the room's generation number)
1577 if (extract_int(args, 4)) {
1578 time(&CC->room.QRgen);
1581 /* Some changes can't apply to AIDEROOM */
1582 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1584 CC->room.QRorder = 0;
1585 CC->room.QRflags &= ~QR_MAILBOX;
1586 CC->room.QRflags |= QR_PERMANENT;
1589 /* Write the room record back to disk */
1590 CtdlPutRoomLock(&CC->room);
1592 /* Create a room directory if necessary */
1593 if (CC->room.QRflags & QR_DIRECTORY) {
1594 snprintf(buf, sizeof buf,"%s/%s",
1596 CC->room.QRdirname);
1599 snprintf(buf, sizeof buf, "The room \"%s\" has been edited by %s.\n",
1600 CC->room.QRname, CC->curr_user);
1601 CtdlAideMessage(buf, "Room modification Message");
1602 cprintf("%d Ok\n", CIT_OK);
1608 * get the name of the room aide for this room
1610 void cmd_geta(char *cmdbuf)
1612 struct ctdluser usbuf;
1614 if (CtdlAccessCheck(ac_logged_in)) return;
1616 if (CtdlGetUserByNumber(&usbuf, CC->room.QRroomaide) == 0) {
1617 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1619 cprintf("%d \n", CIT_OK);
1625 * set the room aide for this room
1627 void cmd_seta(char *new_ra)
1629 struct ctdluser usbuf;
1634 if (CtdlAccessCheck(ac_room_aide)) return;
1636 if (CtdlGetUser(&usbuf, new_ra) != 0) {
1639 newu = usbuf.usernum;
1642 CtdlGetRoomLock(&CC->room, CC->room.QRname);
1644 if (CC->room.QRroomaide != newu) {
1647 CC->room.QRroomaide = newu;
1648 CtdlPutRoomLock(&CC->room);
1651 * We have to post the change notice _after_ writing changes to
1652 * the room table, otherwise it would deadlock!
1654 if (post_notice == 1) {
1655 if (!IsEmptyStr(usbuf.fullname))
1656 snprintf(buf, sizeof buf,
1657 "%s is now the room aide for \"%s\".\n",
1658 usbuf.fullname, CC->room.QRname);
1660 snprintf(buf, sizeof buf,
1661 "There is now no room aide for \"%s\".\n",
1663 CtdlAideMessage(buf, "Aide Room Modification");
1665 cprintf("%d Ok\n", CIT_OK);
1669 * retrieve info file for this room
1671 void cmd_rinf(char *gargs)
1677 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_info_dir);
1678 info_fp = fopen(filename, "r");
1680 if (info_fp == NULL) {
1681 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
1684 cprintf("%d Info:\n", LISTING_FOLLOWS);
1685 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1686 if (!IsEmptyStr(buf))
1687 buf[strlen(buf) - 1] = 0;
1688 cprintf("%s\n", buf);
1695 * Asynchronously schedule a room for deletion. The room will appear
1696 * deleted to the user(s), but it won't actually get purged from the
1697 * database until THE DREADED AUTO-PURGER makes its next run.
1699 void CtdlScheduleRoomForDeletion(struct ctdlroom *qrbuf)
1701 char old_name[ROOMNAMELEN];
1704 syslog(LOG_NOTICE, "Scheduling room <%s> for deletion\n",
1707 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1709 CtdlGetRoom(qrbuf, qrbuf->QRname);
1711 /* Turn the room into a private mailbox owned by a user who doesn't
1712 * exist. This will immediately make the room invisible to everyone,
1713 * and qualify the room for purging.
1715 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1720 qrbuf->QRflags |= QR_MAILBOX;
1721 time(&qrbuf->QRgen); /* Use a timestamp as the new generation number */
1725 b_deleteroom(old_name);
1731 * Back end processing to delete a room and everything associated with it
1732 * (This one is synchronous and should only get called by THE DREADED
1733 * AUTO-PURGER in serv_expire.c. All user-facing code should call
1734 * the asynchronous schedule_room_for_deletion() instead.)
1736 void CtdlDeleteRoom(struct ctdlroom *qrbuf)
1740 /* TODO: filename magic? does this realy work? */
1742 syslog(LOG_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1744 /* Delete the info file */
1745 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_info_dir);
1748 /* Delete the image file */
1749 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_image_dir);
1752 /* Delete the room's network config file */
1753 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1756 /* Delete the messages in the room
1757 * (Careful: this opens an S_ROOMS critical section!)
1759 CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
1761 /* Flag the room record as not in use */
1762 CtdlGetRoomLock(qrbuf, qrbuf->QRname);
1764 CtdlPutRoomLock(qrbuf);
1766 /* then decrement the reference count for the floor */
1767 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1768 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1769 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1771 /* Delete the room record from the database! */
1772 b_deleteroom(qrbuf->QRname);
1778 * Check access control for deleting a room
1780 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1782 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1786 if (CtdlIsNonEditable(qr)) {
1791 * For mailboxes, check stuff
1793 if (qr->QRflags & QR_MAILBOX) {
1795 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1797 if (atol(qr->QRname) != CC->user.usernum) {
1798 return(0); /* not my room */
1801 /* Can't delete your Mail> room */
1802 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1804 /* Otherwise it's ok */
1809 * For normal rooms, just check for aide or room aide status.
1811 return(is_room_aide());
1815 * aide command: kill the current room
1817 void cmd_kill(char *argbuf)
1819 char deleted_room_name[ROOMNAMELEN];
1823 kill_ok = extract_int(argbuf, 0);
1825 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
1826 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1830 if (CC->room.QRflags & QR_MAILBOX) {
1831 safestrncpy(deleted_room_name, &CC->room.QRname[11], sizeof deleted_room_name);
1834 safestrncpy(deleted_room_name, CC->room.QRname, sizeof deleted_room_name);
1837 /* Do the dirty work */
1838 CtdlScheduleRoomForDeletion(&CC->room);
1840 /* Return to the Lobby */
1841 CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
1843 /* tell the world what we did */
1844 snprintf(msg, sizeof msg, "The room \"%s\" has been deleted by %s.\n",
1845 deleted_room_name, CC->curr_user);
1846 CtdlAideMessage(msg, "Room Purger Message");
1847 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1849 cprintf("%d ok to delete.\n", CIT_OK);
1855 * Internal code to create a new room (returns room flags)
1857 * Room types: 0=public, 1=hidden, 2=passworded, 3=invitation-only,
1858 * 4=mailbox, 5=mailbox, but caller supplies namespace
1860 unsigned CtdlCreateRoom(char *new_room_name,
1862 char *new_room_pass,
1869 struct ctdlroom qrbuf;
1873 syslog(LOG_DEBUG, "CtdlCreateRoom(name=%s, type=%d, view=%d)\n",
1874 new_room_name, new_room_type, new_room_view);
1876 if (CtdlGetRoom(&qrbuf, new_room_name) == 0) {
1877 syslog(LOG_DEBUG, "%s already exists.\n", new_room_name);
1881 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1882 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1883 qrbuf.QRflags = QR_INUSE;
1884 if (new_room_type > 0)
1885 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1886 if (new_room_type == 1)
1887 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1888 if (new_room_type == 2)
1889 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1890 if ( (new_room_type == 4) || (new_room_type == 5) ) {
1891 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1892 /* qrbuf.QRflags2 |= QR2_SUBJECTREQ; */
1895 /* If the user is requesting a personal room, set up the room
1896 * name accordingly (prepend the user number)
1898 if (new_room_type == 4) {
1899 CtdlMailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1902 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1905 /* If the room is private, and the system administrator has elected
1906 * to automatically grant room aide privileges, do so now; otherwise,
1907 * set the room aide to undefined.
1909 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1910 qrbuf.QRroomaide = CC->user.usernum;
1912 qrbuf.QRroomaide = (-1L);
1916 * If the caller is only interested in testing whether this will work,
1917 * return now without creating the room.
1919 if (!really_create) return (qrbuf.QRflags);
1921 qrbuf.QRnumber = get_new_room_number();
1922 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1923 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1924 qrbuf.QRfloor = new_room_floor;
1925 qrbuf.QRdefaultview = new_room_view;
1927 /* save what we just did... */
1928 CtdlPutRoom(&qrbuf);
1930 /* bump the reference count on whatever floor the room is on */
1931 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1932 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1933 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1935 /* Grant the creator access to the room unless the avoid_access
1936 * parameter was specified.
1938 if ( (CC->logged_in) && (avoid_access == 0) ) {
1939 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1940 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1941 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1942 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1945 /* resume our happy day */
1946 return (qrbuf.QRflags);
1953 void cmd_cre8(char *args)
1956 char new_room_name[ROOMNAMELEN];
1958 char new_room_pass[32];
1961 char *notification_message = NULL;
1964 int avoid_access = 0;
1966 cre8_ok = extract_int(args, 0);
1967 extract_token(new_room_name, args, 1, '|', sizeof new_room_name);
1968 new_room_name[ROOMNAMELEN - 1] = 0;
1969 new_room_type = extract_int(args, 2);
1970 extract_token(new_room_pass, args, 3, '|', sizeof new_room_pass);
1971 avoid_access = extract_int(args, 5);
1972 new_room_view = extract_int(args, 6);
1973 new_room_pass[9] = 0;
1976 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 1)) {
1977 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
1981 if (!strcasecmp(new_room_name, MAILROOM)) {
1982 cprintf("%d '%s' already exists.\n",
1983 ERROR + ALREADY_EXISTS, new_room_name);
1987 if (num_parms(args) >= 5) {
1988 fl = CtdlGetCachedFloor(extract_int(args, 4));
1990 cprintf("%d Invalid floor number.\n",
1991 ERROR + INVALID_FLOOR_OPERATION);
1994 else if ((fl->f_flags & F_INUSE) == 0) {
1995 cprintf("%d Invalid floor number.\n",
1996 ERROR + INVALID_FLOOR_OPERATION);
1999 new_room_floor = extract_int(args, 4);
2003 if (CtdlAccessCheck(ac_logged_in)) return;
2005 if (CC->user.axlevel < config.c_createax && !CC->internal_pgm) {
2006 cprintf("%d You need higher access to create rooms.\n",
2007 ERROR + HIGHER_ACCESS_REQUIRED);
2011 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 0)) {
2012 cprintf("%d Ok to create rooms.\n", CIT_OK);
2016 if ((new_room_type < 0) || (new_room_type > 5)) {
2017 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
2021 if (new_room_type == 5) {
2022 if (CC->user.axlevel < AxAideU) {
2023 cprintf("%d Higher access required\n",
2024 ERROR + HIGHER_ACCESS_REQUIRED);
2029 /* Check to make sure the requested room name doesn't already exist */
2030 newflags = CtdlCreateRoom(new_room_name,
2031 new_room_type, new_room_pass, new_room_floor,
2032 0, avoid_access, new_room_view);
2033 if (newflags == 0) {
2034 cprintf("%d '%s' already exists.\n",
2035 ERROR + ALREADY_EXISTS, new_room_name);
2040 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
2044 /* If we reach this point, the room needs to be created. */
2046 newflags = CtdlCreateRoom(new_room_name,
2047 new_room_type, new_room_pass, new_room_floor, 1, 0,
2050 /* post a message in Aide> describing the new room */
2051 notification_message = malloc(1024);
2052 snprintf(notification_message, 1024,
2053 "A new room called \"%s\" has been created by %s%s%s%s%s%s\n",
2056 ((newflags & QR_MAILBOX) ? " [personal]" : ""),
2057 ((newflags & QR_PRIVATE) ? " [private]" : ""),
2058 ((newflags & QR_GUESSNAME) ? " [hidden]" : ""),
2059 ((newflags & QR_PASSWORDED) ? " Password: " : ""),
2060 ((newflags & QR_PASSWORDED) ? new_room_pass : "")
2062 CtdlAideMessage(notification_message, "Room Creation Message");
2063 free(notification_message);
2065 cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
2070 void cmd_einf(char *ok)
2071 { /* enter info file for current room */
2073 char infofilename[SIZ];
2078 if (CtdlAccessCheck(ac_room_aide)) return;
2080 if (atoi(ok) == 0) {
2081 cprintf("%d Ok.\n", CIT_OK);
2084 assoc_file_name(infofilename, sizeof infofilename, &CC->room, ctdl_info_dir);
2085 syslog(LOG_DEBUG, "opening\n");
2086 fp = fopen(infofilename, "w");
2087 syslog(LOG_DEBUG, "checking\n");
2089 cprintf("%d Cannot open %s: %s\n",
2090 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
2093 cprintf("%d Send info...\n", SEND_LISTING);
2096 client_getln(buf, sizeof buf);
2097 if (strcmp(buf, "000"))
2098 fprintf(fp, "%s\n", buf);
2099 } while (strcmp(buf, "000"));
2102 /* now update the room index so people will see our new info */
2103 CtdlGetRoomLock(&CC->room, CC->room.QRname); /* lock so no one steps on us */
2104 CC->room.QRinfo = CC->room.QRhighest + 1L;
2105 CtdlPutRoomLock(&CC->room);
2110 * cmd_lflr() - List all known floors
2112 void cmd_lflr(char *gargs)
2117 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
2119 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
2121 for (a = 0; a < MAXFLOORS; ++a) {
2122 CtdlGetFloor(&flbuf, a);
2123 if (flbuf.f_flags & F_INUSE) {
2124 cprintf("%d|%s|%d\n",
2136 * create a new floor
2138 void cmd_cflr(char *argbuf)
2140 char new_floor_name[256];
2143 int free_slot = (-1);
2146 extract_token(new_floor_name, argbuf, 0, '|', sizeof new_floor_name);
2147 cflr_ok = extract_int(argbuf, 1);
2149 if (CtdlAccessCheck(ac_aide)) return;
2151 if (IsEmptyStr(new_floor_name)) {
2152 cprintf("%d Blank floor name not allowed.\n",
2153 ERROR + ILLEGAL_VALUE);
2157 for (a = 0; a < MAXFLOORS; ++a) {
2158 CtdlGetFloor(&flbuf, a);
2160 /* note any free slots while we're scanning... */
2161 if (((flbuf.f_flags & F_INUSE) == 0)
2165 /* check to see if it already exists */
2166 if ((!strcasecmp(flbuf.f_name, new_floor_name))
2167 && (flbuf.f_flags & F_INUSE)) {
2168 cprintf("%d Floor '%s' already exists.\n",
2169 ERROR + ALREADY_EXISTS,
2175 if (free_slot < 0) {
2176 cprintf("%d There is no space available for a new floor.\n",
2177 ERROR + INVALID_FLOOR_OPERATION);
2181 cprintf("%d ok to create...\n", CIT_OK);
2184 lgetfloor(&flbuf, free_slot);
2185 flbuf.f_flags = F_INUSE;
2186 flbuf.f_ref_count = 0;
2187 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
2188 lputfloor(&flbuf, free_slot);
2189 cprintf("%d %d\n", CIT_OK, free_slot);
2197 void cmd_kflr(char *argbuf)
2200 int floor_to_delete;
2204 floor_to_delete = extract_int(argbuf, 0);
2205 kflr_ok = extract_int(argbuf, 1);
2207 if (CtdlAccessCheck(ac_aide)) return;
2209 lgetfloor(&flbuf, floor_to_delete);
2212 if ((flbuf.f_flags & F_INUSE) == 0) {
2213 cprintf("%d Floor %d not in use.\n",
2214 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
2217 if (flbuf.f_ref_count != 0) {
2218 cprintf("%d Cannot delete; floor contains %d rooms.\n",
2219 ERROR + INVALID_FLOOR_OPERATION,
2224 cprintf("%d Ok\n", CIT_OK);
2226 cprintf("%d Ok to delete...\n", CIT_OK);
2233 if ((delete_ok == 1) && (kflr_ok == 1))
2235 lputfloor(&flbuf, floor_to_delete);
2241 void cmd_eflr(char *argbuf)
2247 np = num_parms(argbuf);
2249 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
2253 if (CtdlAccessCheck(ac_aide)) return;
2255 floor_num = extract_int(argbuf, 0);
2256 lgetfloor(&flbuf, floor_num);
2257 if ((flbuf.f_flags & F_INUSE) == 0) {
2258 lputfloor(&flbuf, floor_num);
2259 cprintf("%d Floor %d is not in use.\n",
2260 ERROR + INVALID_FLOOR_OPERATION, floor_num);
2264 extract_token(flbuf.f_name, argbuf, 1, '|', sizeof flbuf.f_name);
2265 lputfloor(&flbuf, floor_num);
2267 cprintf("%d Ok\n", CIT_OK);
2271 /*****************************************************************************/
2272 /* MODULE INITIALIZATION STUFF */
2273 /*****************************************************************************/
2275 CTDL_MODULE_INIT(room_ops)
2278 CtdlRegisterProtoHook(cmd_lrms, "LRMS", "List rooms");
2279 CtdlRegisterProtoHook(cmd_lkra, "LKRA", "List all known rooms");
2280 CtdlRegisterProtoHook(cmd_lkrn, "LKRN", "List known rooms with new messages");
2281 CtdlRegisterProtoHook(cmd_lkro, "LKRO", "List known rooms without new messages");
2282 CtdlRegisterProtoHook(cmd_lzrm, "LZRM", "List zapped rooms");
2283 CtdlRegisterProtoHook(cmd_lprm, "LPRM", "List public rooms");
2284 CtdlRegisterProtoHook(cmd_goto, "GOTO", "Goto a named room");
2285 CtdlRegisterProtoHook(cmd_whok, "WHOK", "List users who know this room");
2286 CtdlRegisterProtoHook(cmd_rdir, "RDIR", "List files in room directory");
2287 CtdlRegisterProtoHook(cmd_getr, "GETR", "Get room parameters");
2288 CtdlRegisterProtoHook(cmd_setr, "SETR", "Set room parameters");
2289 CtdlRegisterProtoHook(cmd_geta, "GETA", "Get the room aide name");
2290 CtdlRegisterProtoHook(cmd_seta, "SETA", "Set the room aide for this room");
2291 CtdlRegisterProtoHook(cmd_rinf, "RINF", "Fetch room info file");
2292 CtdlRegisterProtoHook(cmd_kill, "KILL", "Kill (delete) the current room");
2293 CtdlRegisterProtoHook(cmd_cre8, "CRE8", "Create a new room");
2294 CtdlRegisterProtoHook(cmd_einf, "EINF", "Enter info file for the current room");
2295 CtdlRegisterProtoHook(cmd_lflr, "LFLR", "List all known floors");
2296 CtdlRegisterProtoHook(cmd_cflr, "CFLR", "Create a new floor");
2297 CtdlRegisterProtoHook(cmd_kflr, "KFLR", "Kill a floor");
2298 CtdlRegisterProtoHook(cmd_eflr, "EFLR", "Edit a floor");
2300 /* return our Subversion id for the Log */