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)
117 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
120 /* If this is a preferred users only room, check access level */
121 if (roombuf->QRflags & QR_PREFONLY) {
122 if (userbuf->axlevel < AxPrefU) {
123 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
127 /* For private rooms, check the generation number matchups */
128 if ( (roombuf->QRflags & QR_PRIVATE)
129 && ((roombuf->QRflags & QR_MAILBOX) == 0)
132 /* An explicit match means the user belongs in this room */
133 if (vbuf.v_flags & V_ACCESS) {
134 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
136 /* Otherwise, check if this is a guess-name or passworded
137 * room. If it is, a goto may at least be attempted
139 else if ( (roombuf->QRflags & QR_PRIVATE)
140 || (roombuf->QRflags & QR_PASSWORDED)
142 retval = retval & ~UA_KNOWN;
143 retval = retval | UA_GOTOALLOWED;
147 /* For mailbox rooms, also check the namespace */
148 /* Also, mailbox owners can delete their messages */
149 if ( (roombuf->QRflags & QR_MAILBOX) && (atol(roombuf->QRname) != 0)) {
150 if (userbuf->usernum == atol(roombuf->QRname)) {
151 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED;
153 /* An explicit match means the user belongs in this room */
154 if (vbuf.v_flags & V_ACCESS) {
155 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED;
159 /* For non-mailbox rooms... */
162 /* User is allowed to post in the room unless:
163 * - User is not validated
164 * - User has no net privileges and it is a shared network room
165 * - It is a read-only room
167 int post_allowed = 1;
168 int reply_allowed = 1;
169 if (userbuf->axlevel < AxProbU) {
173 if ((userbuf->axlevel < AxNetU) && (roombuf->QRflags & QR_NETWORK)) {
177 if (roombuf->QRflags & QR_READONLY) {
182 retval = retval | UA_POSTALLOWED | UA_REPLYALLOWED;
185 retval = retval | UA_REPLYALLOWED;
188 /* If "collaborative deletion" is active for this room, any user who can post
189 * is also allowed to delete
191 if (roombuf->QRflags2 & QR2_COLLABDEL) {
192 if (retval & UA_POSTALLOWED) {
193 retval = retval | UA_DELETEALLOWED;
199 /* Check to see if the user has forgotten this room */
200 if (vbuf.v_flags & V_FORGET) {
201 retval = retval & ~UA_KNOWN;
202 if ( ( ((roombuf->QRflags & QR_PRIVATE) == 0)
203 && ((roombuf->QRflags & QR_MAILBOX) == 0)
204 ) || ( (roombuf->QRflags & QR_MAILBOX)
205 && (atol(roombuf->QRname) == CC->user.usernum))
207 retval = retval | UA_ZAPPED;
211 /* If user is explicitly locked out of this room, deny everything */
212 if (vbuf.v_flags & V_LOCKOUT) {
213 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED & ~UA_POSTALLOWED & ~UA_REPLYALLOWED;
216 /* Aides get access to all private rooms */
217 if ( (userbuf->axlevel >= AxAideU)
218 && ((roombuf->QRflags & QR_MAILBOX) == 0)
220 if (vbuf.v_flags & V_FORGET) {
221 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
224 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
228 /* Aides can gain access to mailboxes as well, but they don't show
231 if ( (userbuf->axlevel >= AxAideU)
232 && (roombuf->QRflags & QR_MAILBOX)
234 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
237 /* Aides and Room Aides have admin privileges */
238 if ( (userbuf->axlevel >= AxAideU)
239 || (userbuf->usernum == roombuf->QRroomaide)
241 retval = retval | UA_ADMINALLOWED | UA_DELETEALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
244 NEWMSG: /* By the way, we also check for the presence of new messages */
245 if (is_msg_in_sequence_set(vbuf.v_seen, roombuf->QRhighest) == 0) {
246 retval = retval | UA_HASNEWMSGS;
249 /* System rooms never show up in the list. */
250 if (roombuf->QRflags2 & QR2_SYSTEM) {
251 retval = retval & ~UA_KNOWN;
255 /* Now give the caller the information it wants. */
256 if (result != NULL) *result = retval;
257 if (view != NULL) *view = vbuf.v_view;
261 * Self-checking stuff for a room record read into memory
263 void room_sanity_check(struct ctdlroom *qrbuf)
265 /* Mailbox rooms are always on the lowest floor */
266 if (qrbuf->QRflags & QR_MAILBOX) {
269 /* Listing order of 0 is illegal except for base rooms */
270 if (qrbuf->QRorder == 0)
271 if (!(qrbuf->QRflags & QR_MAILBOX) &&
272 strncasecmp(qrbuf->QRname, config.c_baseroom, ROOMNAMELEN)
274 strncasecmp(qrbuf->QRname, config.c_aideroom, ROOMNAMELEN))
280 * CtdlGetRoom() - retrieve room data from disk
282 int CtdlGetRoom(struct ctdlroom *qrbuf, char *room_name)
284 struct cdbdata *cdbqr;
285 char lowercase_name[ROOMNAMELEN];
286 char personal_lowercase_name[ROOMNAMELEN];
287 char *dptr, *sptr, *eptr;
289 dptr = lowercase_name;
291 eptr = (dptr + (sizeof lowercase_name - 1));
292 while (!IsEmptyStr(sptr) && (dptr < eptr)){
293 *dptr = tolower(*sptr);
298 memset(qrbuf, 0, sizeof(struct ctdlroom));
300 /* First, try the public namespace */
301 cdbqr = cdb_fetch(CDB_ROOMS,
302 lowercase_name, strlen(lowercase_name));
304 /* If that didn't work, try the user's personal namespace */
306 snprintf(personal_lowercase_name,
307 sizeof personal_lowercase_name, "%010ld.%s",
308 CC->user.usernum, lowercase_name);
309 cdbqr = cdb_fetch(CDB_ROOMS,
310 personal_lowercase_name,
311 strlen(personal_lowercase_name));
314 memcpy(qrbuf, cdbqr->ptr,
315 ((cdbqr->len > sizeof(struct ctdlroom)) ?
316 sizeof(struct ctdlroom) : cdbqr->len));
319 room_sanity_check(qrbuf);
328 * CtdlGetRoomLock() - same as getroom() but locks the record (if supported)
330 int CtdlGetRoomLock(struct ctdlroom *qrbuf, char *room_name)
333 retval = CtdlGetRoom(qrbuf, room_name);
334 if (retval == 0) begin_critical_section(S_ROOMS);
340 * b_putroom() - back end to putroom() and b_deleteroom()
341 * (if the supplied buffer is NULL, delete the room record)
343 void b_putroom(struct ctdlroom *qrbuf, char *room_name)
345 char lowercase_name[ROOMNAMELEN];
350 bptr = lowercase_name;
351 while (!IsEmptyStr(aptr))
353 *bptr = tolower(*aptr);
359 len = bptr - lowercase_name;
361 cdb_delete(CDB_ROOMS,
362 lowercase_name, len);
364 time(&qrbuf->QRmtime);
367 qrbuf, sizeof(struct ctdlroom));
373 * CtdlPutRoom() - store room data to disk
375 void CtdlPutRoom(struct ctdlroom *qrbuf) {
376 b_putroom(qrbuf, qrbuf->QRname);
381 * b_deleteroom() - delete a room record from disk
383 void b_deleteroom(char *room_name) {
384 b_putroom(NULL, room_name);
390 * CtdlPutRoomLock() - same as CtdlPutRoom() but unlocks the record (if supported)
392 void CtdlPutRoomLock(struct ctdlroom *qrbuf)
396 end_critical_section(S_ROOMS);
400 /****************************************************************************/
404 * CtdlGetFloorByName() - retrieve the number of the named floor
405 * return < 0 if not found else return floor number
407 int CtdlGetFloorByName(const char *floor_name)
410 struct floor *flbuf = NULL;
412 for (a = 0; a < MAXFLOORS; ++a) {
413 flbuf = CtdlGetCachedFloor(a);
415 /* check to see if it already exists */
416 if ((!strcasecmp(flbuf->f_name, floor_name))
417 && (flbuf->f_flags & F_INUSE)) {
427 * CtdlGetFloorByNameLock() - retrieve floor number for given floor and lock the floor list.
429 int CtdlGetFloorByNameLock(const char *floor_name)
431 begin_critical_section(S_FLOORTAB);
432 return CtdlGetFloorByName(floor_name);
438 * CtdlGetAvailableFloor() - Return number of first unused floor
439 * return < 0 if none available
441 int CtdlGetAvailableFloor(void)
444 struct floor *flbuf = NULL;
446 for (a = 0; a < MAXFLOORS; a++) {
447 flbuf = CtdlGetCachedFloor(a);
449 /* check to see if it already exists */
450 if ((flbuf->f_flags & F_INUSE) == 0) {
459 * CtdlGetFloor() - retrieve floor data from disk
461 void CtdlGetFloor(struct floor *flbuf, int floor_num)
463 struct cdbdata *cdbfl;
465 memset(flbuf, 0, sizeof(struct floor));
466 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
468 memcpy(flbuf, cdbfl->ptr,
469 ((cdbfl->len > sizeof(struct floor)) ?
470 sizeof(struct floor) : cdbfl->len));
473 if (floor_num == 0) {
474 safestrncpy(flbuf->f_name, "Main Floor",
475 sizeof flbuf->f_name);
476 flbuf->f_flags = F_INUSE;
477 flbuf->f_ref_count = 3;
484 * lgetfloor() - same as CtdlGetFloor() but locks the record (if supported)
486 void lgetfloor(struct floor *flbuf, int floor_num)
489 begin_critical_section(S_FLOORTAB);
490 CtdlGetFloor(flbuf, floor_num);
495 * CtdlGetCachedFloor() - Get floor record from *cache* (loads from disk if needed)
497 * This is strictly a performance hack.
499 struct floor *CtdlGetCachedFloor(int floor_num) {
500 static int initialized = 0;
503 struct floor *fl = NULL;
505 begin_critical_section(S_FLOORCACHE);
506 if (initialized == 0) {
507 for (i=0; i<MAXFLOORS; ++i) {
508 floorcache[floor_num] = NULL;
512 if (floorcache[floor_num] == NULL) {
515 end_critical_section(S_FLOORCACHE);
518 fl = malloc(sizeof(struct floor));
519 CtdlGetFloor(fl, floor_num);
520 begin_critical_section(S_FLOORCACHE);
521 if (floorcache[floor_num] != NULL) {
522 free(floorcache[floor_num]);
524 floorcache[floor_num] = fl;
525 end_critical_section(S_FLOORCACHE);
528 return(floorcache[floor_num]);
534 * CtdlPutFloor() - store floor data on disk
536 void CtdlPutFloor(struct floor *flbuf, int floor_num)
538 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
539 begin_critical_section(S_FLOORCACHE);
540 if (floorcache[floor_num] != NULL) {
541 free(floorcache[floor_num]);
542 floorcache[floor_num] = malloc(sizeof(struct floor));
543 memcpy(floorcache[floor_num], flbuf, sizeof(struct floor));
545 end_critical_section(S_FLOORCACHE);
547 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
548 flbuf, sizeof(struct floor));
554 * CtdlPutFloorLock() - same as CtdlPutFloor() but unlocks the record (if supported)
556 void CtdlPutFloorLock(struct floor *flbuf, int floor_num)
559 CtdlPutFloor(flbuf, floor_num);
560 end_critical_section(S_FLOORTAB);
567 * lputfloor() - same as CtdlPutFloor() but unlocks the record (if supported)
569 void lputfloor(struct floor *flbuf, int floor_num)
571 CtdlPutFloorLock(flbuf, floor_num);
576 * Traverse the room file...
578 void CtdlForEachRoom(void (*CallBack) (struct ctdlroom *EachRoom, void *out_data),
581 struct ctdlroom qrbuf;
582 struct cdbdata *cdbqr;
584 cdb_rewind(CDB_ROOMS);
586 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
587 memset(&qrbuf, 0, sizeof(struct ctdlroom));
588 memcpy(&qrbuf, cdbqr->ptr,
589 ((cdbqr->len > sizeof(struct ctdlroom)) ?
590 sizeof(struct ctdlroom) : cdbqr->len));
592 room_sanity_check(&qrbuf);
593 if (qrbuf.QRflags & QR_INUSE)
594 (*CallBack)(&qrbuf, in_data);
600 * delete_msglist() - delete room message pointers
602 void delete_msglist(struct ctdlroom *whichroom)
604 struct cdbdata *cdbml;
606 /* Make sure the msglist we're deleting actually exists, otherwise
607 * gdbm will complain when we try to delete an invalid record
609 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
613 /* Go ahead and delete it */
614 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
621 * Message pointer compare function for sort_msglist()
623 int sort_msglist_cmp(const void *m1, const void *m2) {
624 if ((*(const long *)m1) > (*(const long *)m2)) return(1);
625 if ((*(const long *)m1) < (*(const long *)m2)) return(-1);
631 * sort message pointers
632 * (returns new msg count)
634 int sort_msglist(long listptrs[], int oldcount)
644 qsort(listptrs, numitems, sizeof(long), sort_msglist_cmp);
646 /* and yank any nulls */
647 while ((numitems > 0) && (listptrs[0] == 0L)) {
648 memmove(&listptrs[0], &listptrs[1],
649 (sizeof(long) * (numitems - 1)));
658 * Determine whether a given room is non-editable.
660 int CtdlIsNonEditable(struct ctdlroom *qrbuf)
663 /* Mail> rooms are non-editable */
664 if ( (qrbuf->QRflags & QR_MAILBOX)
665 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
668 /* Everything else is editable */
675 * Back-back-end for all room listing commands
677 void list_roomname(struct ctdlroom *qrbuf, int ra, int current_view, int default_view)
679 char truncated_roomname[ROOMNAMELEN];
681 /* For my own mailbox rooms, chop off the owner prefix */
682 if ( (qrbuf->QRflags & QR_MAILBOX)
683 && (atol(qrbuf->QRname) == CC->user.usernum) ) {
684 safestrncpy(truncated_roomname, qrbuf->QRname, sizeof truncated_roomname);
685 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
686 cprintf("%s", truncated_roomname);
688 /* For all other rooms, just display the name in its entirety */
690 cprintf("%s", qrbuf->QRname);
693 /* ...and now the other parameters */
694 cprintf("|%u|%d|%d|%d|%d|%d|%d|%ld|\n",
696 (int) qrbuf->QRfloor,
697 (int) qrbuf->QRorder,
698 (int) qrbuf->QRflags2,
708 * cmd_lrms() - List all accessible rooms, known or forgotten
710 void cmd_lrms_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 | UA_ZAPPED)))
720 && ((qrbuf->QRfloor == (FloorBeingSearched))
721 || ((FloorBeingSearched) < 0)))
722 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
725 void cmd_lrms(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 Accessible rooms:\n", LISTING_FOLLOWS);
736 CtdlForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
743 * cmd_lkra() - List all known rooms
745 void cmd_lkra_backend(struct ctdlroom *qrbuf, void *data)
747 int FloorBeingSearched = (-1);
751 FloorBeingSearched = *(int *)data;
752 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
754 if ((( ra & (UA_KNOWN)))
755 && ((qrbuf->QRfloor == (FloorBeingSearched))
756 || ((FloorBeingSearched) < 0)))
757 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
760 void cmd_lkra(char *argbuf)
762 int FloorBeingSearched = (-1);
763 if (!IsEmptyStr(argbuf))
764 FloorBeingSearched = extract_int(argbuf, 0);
766 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
768 CtdlGetUser(&CC->user, CC->curr_user);
769 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
771 CtdlForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
777 void cmd_lprm_backend(struct ctdlroom *qrbuf, void *data)
779 int FloorBeingSearched = (-1);
783 FloorBeingSearched = *(int *)data;
784 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
786 if ( ((qrbuf->QRflags & QR_PRIVATE) == 0)
787 && ((qrbuf->QRflags & QR_MAILBOX) == 0)
788 && ((qrbuf->QRfloor == (FloorBeingSearched))
789 || ((FloorBeingSearched) < 0)))
790 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
793 void cmd_lprm(char *argbuf)
795 int FloorBeingSearched = (-1);
796 if (!IsEmptyStr(argbuf))
797 FloorBeingSearched = extract_int(argbuf, 0);
799 cprintf("%d Public rooms:\n", LISTING_FOLLOWS);
801 CtdlForEachRoom(cmd_lprm_backend, &FloorBeingSearched);
808 * cmd_lkrn() - List all known rooms with new messages
810 void cmd_lkrn_backend(struct ctdlroom *qrbuf, void *data)
812 int FloorBeingSearched = (-1);
816 FloorBeingSearched = *(int *)data;
817 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
820 && (ra & UA_HASNEWMSGS)
821 && ((qrbuf->QRfloor == (FloorBeingSearched))
822 || ((FloorBeingSearched) < 0)))
823 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
826 void cmd_lkrn(char *argbuf)
828 int FloorBeingSearched = (-1);
829 if (!IsEmptyStr(argbuf))
830 FloorBeingSearched = extract_int(argbuf, 0);
832 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
834 CtdlGetUser(&CC->user, CC->curr_user);
835 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
837 CtdlForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
844 * cmd_lkro() - List all known rooms
846 void cmd_lkro_backend(struct ctdlroom *qrbuf, void *data)
848 int FloorBeingSearched = (-1);
852 FloorBeingSearched = *(int *)data;
853 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
856 && ((ra & UA_HASNEWMSGS) == 0)
857 && ((qrbuf->QRfloor == (FloorBeingSearched))
858 || ((FloorBeingSearched) < 0)))
859 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
862 void cmd_lkro(char *argbuf)
864 int FloorBeingSearched = (-1);
865 if (!IsEmptyStr(argbuf))
866 FloorBeingSearched = extract_int(argbuf, 0);
868 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
870 CtdlGetUser(&CC->user, CC->curr_user);
871 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
873 CtdlForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
880 * cmd_lzrm() - List all forgotten rooms
882 void cmd_lzrm_backend(struct ctdlroom *qrbuf, void *data)
884 int FloorBeingSearched = (-1);
888 FloorBeingSearched = *(int *)data;
889 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
891 if ((ra & UA_GOTOALLOWED)
893 && ((qrbuf->QRfloor == (FloorBeingSearched))
894 || ((FloorBeingSearched) < 0)))
895 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
898 void cmd_lzrm(char *argbuf)
900 int FloorBeingSearched = (-1);
901 if (!IsEmptyStr(argbuf))
902 FloorBeingSearched = extract_int(argbuf, 0);
904 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
906 CtdlGetUser(&CC->user, CC->curr_user);
907 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
909 CtdlForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
915 * Make the specified room the current room for this session. No validation
916 * or access control is done here -- the caller should make sure that the
917 * specified room exists and is ok to access.
919 void CtdlUserGoto(char *where, int display_result, int transiently,
920 int *retmsgs, int *retnew)
923 int new_messages = 0;
924 int old_messages = 0;
925 int total_messages = 0;
929 int newmailcount = 0;
931 char truncated_roomname[ROOMNAMELEN];
932 struct cdbdata *cdbfr;
933 long *msglist = NULL;
935 unsigned int original_v_flags;
938 char setstr[128], lostr[64], histr[64];
942 /* If the supplied room name is NULL, the caller wants us to know that
943 * it has already copied the room record into CC->room, so
944 * we can skip the extra database fetch.
947 safestrncpy(CC->room.QRname, where, sizeof CC->room.QRname);
948 CtdlGetRoom(&CC->room, where);
951 /* Take care of all the formalities. */
953 begin_critical_section(S_USERS);
954 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
955 original_v_flags = vbuf.v_flags;
957 /* Know the room ... but not if it's the page log room, or if the
958 * caller specified that we're only entering this room transiently.
960 if ((strcasecmp(CC->room.QRname, config.c_logpages))
961 && (transiently == 0) ) {
962 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
963 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
966 /* Only rewrite the database record if we changed something */
967 if (vbuf.v_flags != original_v_flags) {
968 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
970 end_critical_section(S_USERS);
972 /* Check for new mail */
973 newmailcount = NewMailCount();
975 /* set info to 1 if the user needs to read the room's info file */
976 if (CC->room.QRinfo > vbuf.v_lastseen) {
980 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
982 msglist = (long *) cdbfr->ptr;
983 cdbfr->ptr = NULL; /* CtdlUserGoto() now owns this memory */
984 num_msgs = cdbfr->len / sizeof(long);
989 for (a=0; a<num_msgs; ++a) {
990 if (msglist[a] > 0L) ++total_messages;
992 new_messages = num_msgs;
993 num_sets = num_tokens(vbuf.v_seen, ',');
994 for (s=0; s<num_sets; ++s) {
995 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
997 extract_token(lostr, setstr, 0, ':', sizeof lostr);
998 if (num_tokens(setstr, ':') >= 2) {
999 extract_token(histr, setstr, 1, ':', sizeof histr);
1000 if (!strcmp(histr, "*")) {
1001 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
1005 strcpy(histr, lostr);
1010 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
1011 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
1017 new_messages = total_messages - old_messages;
1019 if (msglist != NULL) free(msglist);
1021 if (CC->room.QRflags & QR_MAILBOX)
1026 if ((CC->room.QRroomaide == CC->user.usernum)
1027 || (CC->user.axlevel >= AxAideU))
1032 safestrncpy(truncated_roomname, CC->room.QRname, sizeof truncated_roomname);
1033 if ( (CC->room.QRflags & QR_MAILBOX)
1034 && (atol(CC->room.QRname) == CC->user.usernum) ) {
1035 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
1038 if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
1042 if (retmsgs != NULL) *retmsgs = total_messages;
1043 if (retnew != NULL) *retnew = new_messages;
1044 syslog(LOG_DEBUG, "<%s> %d new of %d total messages\n",
1046 new_messages, total_messages
1049 CC->curr_view = (int)vbuf.v_view;
1051 if (display_result) {
1052 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|%d|\n",
1053 CIT_OK, CtdlCheckExpress(),
1056 (int)total_messages,
1058 (int)CC->room.QRflags,
1059 (long)CC->room.QRhighest,
1060 (long)vbuf.v_lastseen,
1064 (int)CC->room.QRfloor,
1066 (int)CC->room.QRdefaultview,
1068 (int)CC->room.QRflags2
1075 * Handle some of the macro named rooms
1077 void convert_room_name_macros(char *towhere, size_t maxlen) {
1078 if (!strcasecmp(towhere, "_BASEROOM_")) {
1079 safestrncpy(towhere, config.c_baseroom, maxlen);
1081 else if (!strcasecmp(towhere, "_MAIL_")) {
1082 safestrncpy(towhere, MAILROOM, maxlen);
1084 else if (!strcasecmp(towhere, "_TRASH_")) {
1085 safestrncpy(towhere, USERTRASHROOM, maxlen);
1087 else if (!strcasecmp(towhere, "_DRAFTS_")) {
1088 safestrncpy(towhere, USERDRAFTROOM, maxlen);
1090 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
1091 safestrncpy(towhere, config.c_twitroom, maxlen);
1093 else if (!strcasecmp(towhere, "_CALENDAR_")) {
1094 safestrncpy(towhere, USERCALENDARROOM, maxlen);
1096 else if (!strcasecmp(towhere, "_TASKS_")) {
1097 safestrncpy(towhere, USERTASKSROOM, maxlen);
1099 else if (!strcasecmp(towhere, "_CONTACTS_")) {
1100 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
1102 else if (!strcasecmp(towhere, "_NOTES_")) {
1103 safestrncpy(towhere, USERNOTESROOM, maxlen);
1109 * cmd_goto() - goto a new room
1111 void cmd_goto(char *gargs)
1113 struct ctdlroom QRscratch;
1117 char augmented_roomname[ROOMNAMELEN];
1118 char towhere[ROOMNAMELEN];
1120 int transiently = 0;
1122 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
1124 extract_token(towhere, gargs, 0, '|', sizeof towhere);
1125 extract_token(password, gargs, 1, '|', sizeof password);
1126 transiently = extract_int(gargs, 2);
1128 CtdlGetUser(&CC->user, CC->curr_user);
1131 * Handle some of the macro named rooms
1133 convert_room_name_macros(towhere, sizeof towhere);
1135 /* First try a regular match */
1136 c = CtdlGetRoom(&QRscratch, towhere);
1138 /* Then try a mailbox name match */
1140 CtdlMailboxName(augmented_roomname, sizeof augmented_roomname,
1141 &CC->user, towhere);
1142 c = CtdlGetRoom(&QRscratch, augmented_roomname);
1144 safestrncpy(towhere, augmented_roomname, sizeof towhere);
1147 /* And if the room was found... */
1150 /* Let internal programs go directly to any room. */
1151 if (CC->internal_pgm) {
1152 memcpy(&CC->room, &QRscratch,
1153 sizeof(struct ctdlroom));
1154 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1158 /* See if there is an existing user/room relationship */
1159 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
1161 /* normal clients have to pass through security */
1162 if (ra & UA_GOTOALLOWED) {
1167 if ((QRscratch.QRflags & QR_MAILBOX) &&
1168 ((ra & UA_GOTOALLOWED))) {
1169 memcpy(&CC->room, &QRscratch,
1170 sizeof(struct ctdlroom));
1171 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1173 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
1174 ((ra & UA_KNOWN) == 0) &&
1175 (strcasecmp(QRscratch.QRpasswd, password)) &&
1176 (CC->user.axlevel < AxAideU)
1178 cprintf("%d wrong or missing passwd\n",
1179 ERROR + PASSWORD_REQUIRED);
1181 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
1182 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
1183 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
1184 ((ra & UA_KNOWN) == 0) &&
1185 (CC->user.axlevel < AxAideU)
1187 syslog(LOG_DEBUG, "Failed to acquire private room\n");
1189 memcpy(&CC->room, &QRscratch,
1190 sizeof(struct ctdlroom));
1191 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1197 cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
1201 void cmd_whok(char *cmdbuf)
1203 struct ctdluser temp;
1204 struct cdbdata *cdbus;
1207 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
1208 cdb_rewind(CDB_USERS);
1209 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1210 memset(&temp, 0, sizeof temp);
1211 memcpy(&temp, cdbus->ptr, sizeof temp);
1214 CtdlRoomAccess(&CC->room, &temp, &ra, NULL);
1215 if ((!IsEmptyStr(temp.fullname)) &&
1216 (CC->room.QRflags & QR_INUSE) &&
1219 cprintf("%s\n", temp.fullname);
1226 * RDIR command for room directory
1228 void cmd_rdir(char *cmdbuf)
1233 struct stat statbuf;
1234 DIR *filedir = NULL;
1235 struct dirent *filedir_entry;
1241 if (CtdlAccessCheck(ac_logged_in)) return;
1243 CtdlGetRoom(&CC->room, CC->room.QRname);
1244 CtdlGetUser(&CC->user, CC->curr_user);
1246 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
1247 cprintf("%d not here.\n", ERROR + NOT_HERE);
1250 if (((CC->room.QRflags & QR_VISDIR) == 0)
1251 && (CC->user.axlevel < AxAideU)
1252 && (CC->user.usernum != CC->room.QRroomaide)) {
1253 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1257 snprintf(buf, sizeof buf, "%s/%s", ctdl_file_dir, CC->room.QRdirname);
1258 filedir = opendir (buf);
1260 if (filedir == NULL) {
1261 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1264 cprintf("%d %s|%s/%s\n", LISTING_FOLLOWS, config.c_fqdn, ctdl_file_dir, CC->room.QRdirname);
1266 snprintf(buf, sizeof buf, "%s/%s/filedir", ctdl_file_dir, CC->room.QRdirname);
1267 fd = fopen(buf, "r");
1269 fd = fopen("/dev/null", "r");
1270 while ((filedir_entry = readdir(filedir)))
1272 if (strcasecmp(filedir_entry->d_name, "filedir") && filedir_entry->d_name[0] != '.')
1274 #ifdef _DIRENT_HAVE_D_NAMELEN
1275 d_namelen = filedir_entry->d_namelen;
1277 d_namelen = strlen(filedir_entry->d_name);
1279 snprintf(buf, sizeof buf, "%s/%s/%s", ctdl_file_dir, CC->room.QRdirname, filedir_entry->d_name);
1280 stat(buf, &statbuf); /* stat the file */
1281 if (!(statbuf.st_mode & S_IFREG))
1283 snprintf(buf2, sizeof buf2,
1284 "\"%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",
1285 buf, CC->room.QRname
1287 CtdlAideMessage(buf2, "Unusable data found in room directory");
1288 continue; /* not a useable file type so don't show it */
1290 safestrncpy(comment, "", sizeof comment);
1291 fseek(fd, 0L, 0); /* rewind descriptions file */
1292 /* Get the description from the descriptions file */
1293 while ((fgets(buf, sizeof buf, fd) != NULL) && (IsEmptyStr(comment)))
1295 buf[strlen(buf) - 1] = 0;
1296 if ((!strncasecmp(buf, filedir_entry->d_name, d_namelen)) && (buf[d_namelen] == ' '))
1297 safestrncpy(comment, &buf[d_namelen + 1], sizeof comment);
1299 len = extract_token (mimebuf, comment, 0,' ', 64);
1300 if ((len <0) || strchr(mimebuf, '/') == NULL)
1302 snprintf (mimebuf, 64, "application/octetstream");
1305 cprintf("%s|%ld|%s|%s\n",
1306 filedir_entry->d_name,
1307 (long)statbuf.st_size,
1319 * get room parameters (aide or room aide command)
1321 void cmd_getr(char *cmdbuf)
1323 if (CtdlAccessCheck(ac_room_aide)) return;
1325 CtdlGetRoom(&CC->room, CC->room.QRname);
1326 cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1330 ((CC->room.QRflags & QR_MAILBOX) ?
1331 &CC->room.QRname[11] : CC->room.QRname),
1333 ((CC->room.QRflags & QR_PASSWORDED) ?
1334 CC->room.QRpasswd : ""),
1336 ((CC->room.QRflags & QR_DIRECTORY) ?
1337 CC->room.QRdirname : ""),
1340 (int) CC->room.QRfloor,
1341 (int) CC->room.QRorder,
1343 CC->room.QRdefaultview,
1350 * Back end function to rename a room.
1351 * You can also specify which floor to move the room to, or specify -1 to
1352 * keep the room on the same floor it was on.
1354 * If you are renaming a mailbox room, you must supply the namespace prefix
1355 * in *at least* the old name!
1357 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1359 struct ctdlroom qrbuf;
1360 struct ctdlroom qrtmp;
1365 char actual_old_name[ROOMNAMELEN];
1367 syslog(LOG_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1368 old_name, new_name, new_floor);
1370 if (new_floor >= 0) {
1371 fl = CtdlGetCachedFloor(new_floor);
1372 if ((fl->f_flags & F_INUSE) == 0) {
1373 return(crr_invalid_floor);
1377 begin_critical_section(S_ROOMS);
1379 if ( (CtdlGetRoom(&qrtmp, new_name) == 0)
1380 && (strcasecmp(new_name, old_name)) ) {
1381 ret = crr_already_exists;
1384 else if (CtdlGetRoom(&qrbuf, old_name) != 0) {
1385 ret = crr_room_not_found;
1388 else if ( (CC->user.axlevel < AxAideU) && (!CC->internal_pgm)
1389 && (CC->user.usernum != qrbuf.QRroomaide)
1390 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) ) {
1391 ret = crr_access_denied;
1394 else if (CtdlIsNonEditable(&qrbuf)) {
1395 ret = crr_noneditable;
1400 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1401 if (qrbuf.QRflags & QR_MAILBOX) {
1402 owner = atol(qrbuf.QRname);
1404 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1405 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1406 "%010ld.%s", owner, new_name);
1409 safestrncpy(qrbuf.QRname, new_name,
1410 sizeof(qrbuf.QRname));
1413 /* Reject change of floor for baseroom/aideroom */
1414 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1415 !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1419 /* Take care of floor stuff */
1420 old_floor = qrbuf.QRfloor;
1421 if (new_floor < 0) {
1422 new_floor = old_floor;
1424 qrbuf.QRfloor = new_floor;
1425 CtdlPutRoom(&qrbuf);
1427 begin_critical_section(S_CONFIG);
1429 /* If baseroom/aideroom name changes, update config */
1430 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1431 safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1434 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1435 safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1439 end_critical_section(S_CONFIG);
1441 /* If the room name changed, then there are now two room
1442 * records, so we have to delete the old one.
1444 if (strcasecmp(new_name, old_name)) {
1445 b_deleteroom(actual_old_name);
1451 end_critical_section(S_ROOMS);
1453 /* Adjust the floor reference counts if necessary */
1454 if (new_floor != old_floor) {
1455 lgetfloor(&flbuf, old_floor);
1456 --flbuf.f_ref_count;
1457 lputfloor(&flbuf, old_floor);
1458 syslog(LOG_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1459 lgetfloor(&flbuf, new_floor);
1460 ++flbuf.f_ref_count;
1461 lputfloor(&flbuf, new_floor);
1462 syslog(LOG_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1465 /* ...and everybody say "YATTA!" */
1471 * set room parameters (aide or room aide command)
1473 void cmd_setr(char *args)
1479 char new_name[ROOMNAMELEN];
1481 if (CtdlAccessCheck(ac_logged_in)) return;
1483 if (num_parms(args) >= 6) {
1484 new_floor = extract_int(args, 5);
1486 new_floor = (-1); /* don't change the floor */
1489 /* When is a new name more than just a new name? When the old name
1490 * has a namespace prefix.
1492 if (CC->room.QRflags & QR_MAILBOX) {
1493 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
1495 safestrncpy(new_name, "", sizeof new_name);
1497 extract_token(&new_name[strlen(new_name)], args, 0, '|', (sizeof new_name - strlen(new_name)));
1499 r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
1501 if (r == crr_room_not_found) {
1502 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
1503 } else if (r == crr_already_exists) {
1504 cprintf("%d '%s' already exists.\n",
1505 ERROR + ALREADY_EXISTS, new_name);
1506 } else if (r == crr_noneditable) {
1507 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
1508 } else if (r == crr_invalid_floor) {
1509 cprintf("%d Target floor does not exist.\n",
1510 ERROR + INVALID_FLOOR_OPERATION);
1511 } else if (r == crr_access_denied) {
1512 cprintf("%d You do not have permission to edit '%s'\n",
1513 ERROR + HIGHER_ACCESS_REQUIRED,
1515 } else if (r != crr_ok) {
1516 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1517 ERROR + INTERNAL_ERROR, r);
1524 CtdlGetRoom(&CC->room, new_name);
1526 /* Now we have to do a bunch of other stuff */
1528 if (num_parms(args) >= 7) {
1529 new_order = extract_int(args, 6);
1532 if (new_order > 127)
1536 CtdlGetRoomLock(&CC->room, CC->room.QRname);
1538 /* Directory room */
1539 extract_token(buf, args, 2, '|', sizeof buf);
1541 safestrncpy(CC->room.QRdirname, buf,
1542 sizeof CC->room.QRdirname);
1545 if (num_parms(args) >= 8) {
1546 CC->room.QRdefaultview = extract_int(args, 7);
1549 /* Second set of flags */
1550 if (num_parms(args) >= 9) {
1551 CC->room.QRflags2 = extract_int(args, 8);
1555 CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
1556 /* Clean up a client boo-boo: if the client set the room to
1557 * guess-name or passworded, ensure that the private flag is
1560 if ((CC->room.QRflags & QR_GUESSNAME)
1561 || (CC->room.QRflags & QR_PASSWORDED))
1562 CC->room.QRflags |= QR_PRIVATE;
1564 /* Some changes can't apply to BASEROOM */
1565 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1567 CC->room.QRorder = 0;
1568 CC->room.QRpasswd[0] = '\0';
1569 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1570 QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1571 CC->room.QRflags |= QR_PERMANENT;
1573 /* March order (doesn't apply to AIDEROOM) */
1574 if (num_parms(args) >= 7)
1575 CC->room.QRorder = (char) new_order;
1577 extract_token(buf, args, 1, '|', sizeof buf);
1579 safestrncpy(CC->room.QRpasswd, buf,
1580 sizeof CC->room.QRpasswd);
1581 /* Kick everyone out if the client requested it
1582 * (by changing the room's generation number)
1584 if (extract_int(args, 4)) {
1585 time(&CC->room.QRgen);
1588 /* Some changes can't apply to AIDEROOM */
1589 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1591 CC->room.QRorder = 0;
1592 CC->room.QRflags &= ~QR_MAILBOX;
1593 CC->room.QRflags |= QR_PERMANENT;
1596 /* Write the room record back to disk */
1597 CtdlPutRoomLock(&CC->room);
1599 /* Create a room directory if necessary */
1600 if (CC->room.QRflags & QR_DIRECTORY) {
1601 snprintf(buf, sizeof buf,"%s/%s",
1603 CC->room.QRdirname);
1606 snprintf(buf, sizeof buf, "The room \"%s\" has been edited by %s.\n",
1607 CC->room.QRname, CC->curr_user);
1608 CtdlAideMessage(buf, "Room modification Message");
1609 cprintf("%d Ok\n", CIT_OK);
1615 * get the name of the room aide for this room
1617 void cmd_geta(char *cmdbuf)
1619 struct ctdluser usbuf;
1621 if (CtdlAccessCheck(ac_logged_in)) return;
1623 if (CtdlGetUserByNumber(&usbuf, CC->room.QRroomaide) == 0) {
1624 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1626 cprintf("%d \n", CIT_OK);
1632 * set the room aide for this room
1634 void cmd_seta(char *new_ra)
1636 struct ctdluser usbuf;
1641 if (CtdlAccessCheck(ac_room_aide)) return;
1643 if (CtdlGetUser(&usbuf, new_ra) != 0) {
1646 newu = usbuf.usernum;
1649 CtdlGetRoomLock(&CC->room, CC->room.QRname);
1651 if (CC->room.QRroomaide != newu) {
1654 CC->room.QRroomaide = newu;
1655 CtdlPutRoomLock(&CC->room);
1658 * We have to post the change notice _after_ writing changes to
1659 * the room table, otherwise it would deadlock!
1661 if (post_notice == 1) {
1662 if (!IsEmptyStr(usbuf.fullname))
1663 snprintf(buf, sizeof buf,
1664 "%s is now the room aide for \"%s\".\n",
1665 usbuf.fullname, CC->room.QRname);
1667 snprintf(buf, sizeof buf,
1668 "There is now no room aide for \"%s\".\n",
1670 CtdlAideMessage(buf, "Aide Room Modification");
1672 cprintf("%d Ok\n", CIT_OK);
1676 * retrieve info file for this room
1678 void cmd_rinf(char *gargs)
1684 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_info_dir);
1685 info_fp = fopen(filename, "r");
1687 if (info_fp == NULL) {
1688 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
1691 cprintf("%d Info:\n", LISTING_FOLLOWS);
1692 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1693 if (!IsEmptyStr(buf))
1694 buf[strlen(buf) - 1] = 0;
1695 cprintf("%s\n", buf);
1702 * Asynchronously schedule a room for deletion. The room will appear
1703 * deleted to the user(s), but it won't actually get purged from the
1704 * database until THE DREADED AUTO-PURGER makes its next run.
1706 void CtdlScheduleRoomForDeletion(struct ctdlroom *qrbuf)
1708 char old_name[ROOMNAMELEN];
1711 syslog(LOG_NOTICE, "Scheduling room <%s> for deletion\n",
1714 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1716 CtdlGetRoom(qrbuf, qrbuf->QRname);
1718 /* Turn the room into a private mailbox owned by a user who doesn't
1719 * exist. This will immediately make the room invisible to everyone,
1720 * and qualify the room for purging.
1722 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1727 qrbuf->QRflags |= QR_MAILBOX;
1728 time(&qrbuf->QRgen); /* Use a timestamp as the new generation number */
1732 b_deleteroom(old_name);
1738 * Back end processing to delete a room and everything associated with it
1739 * (This one is synchronous and should only get called by THE DREADED
1740 * AUTO-PURGER in serv_expire.c. All user-facing code should call
1741 * the asynchronous schedule_room_for_deletion() instead.)
1743 void CtdlDeleteRoom(struct ctdlroom *qrbuf)
1747 /* TODO: filename magic? does this realy work? */
1749 syslog(LOG_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1751 /* Delete the info file */
1752 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_info_dir);
1755 /* Delete the image file */
1756 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_image_dir);
1759 /* Delete the room's network config file */
1760 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1763 /* Delete the messages in the room
1764 * (Careful: this opens an S_ROOMS critical section!)
1766 CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
1768 /* Flag the room record as not in use */
1769 CtdlGetRoomLock(qrbuf, qrbuf->QRname);
1771 CtdlPutRoomLock(qrbuf);
1773 /* then decrement the reference count for the floor */
1774 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1775 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1776 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1778 /* Delete the room record from the database! */
1779 b_deleteroom(qrbuf->QRname);
1785 * Check access control for deleting a room
1787 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1789 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1793 if (CtdlIsNonEditable(qr)) {
1798 * For mailboxes, check stuff
1800 if (qr->QRflags & QR_MAILBOX) {
1802 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1804 if (atol(qr->QRname) != CC->user.usernum) {
1805 return(0); /* not my room */
1808 /* Can't delete your Mail> room */
1809 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1811 /* Otherwise it's ok */
1816 * For normal rooms, just check for aide or room aide status.
1818 return(is_room_aide());
1822 * aide command: kill the current room
1824 void cmd_kill(char *argbuf)
1826 char deleted_room_name[ROOMNAMELEN];
1830 kill_ok = extract_int(argbuf, 0);
1832 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
1833 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1837 if (CC->room.QRflags & QR_MAILBOX) {
1838 safestrncpy(deleted_room_name, &CC->room.QRname[11], sizeof deleted_room_name);
1841 safestrncpy(deleted_room_name, CC->room.QRname, sizeof deleted_room_name);
1844 /* Do the dirty work */
1845 CtdlScheduleRoomForDeletion(&CC->room);
1847 /* Return to the Lobby */
1848 CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
1850 /* tell the world what we did */
1851 snprintf(msg, sizeof msg, "The room \"%s\" has been deleted by %s.\n",
1852 deleted_room_name, CC->curr_user);
1853 CtdlAideMessage(msg, "Room Purger Message");
1854 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1856 cprintf("%d ok to delete.\n", CIT_OK);
1862 * Internal code to create a new room (returns room flags)
1864 * Room types: 0=public, 1=hidden, 2=passworded, 3=invitation-only,
1865 * 4=mailbox, 5=mailbox, but caller supplies namespace
1867 unsigned CtdlCreateRoom(char *new_room_name,
1869 char *new_room_pass,
1876 struct ctdlroom qrbuf;
1880 syslog(LOG_DEBUG, "CtdlCreateRoom(name=%s, type=%d, view=%d)\n",
1881 new_room_name, new_room_type, new_room_view);
1883 if (CtdlGetRoom(&qrbuf, new_room_name) == 0) {
1884 syslog(LOG_DEBUG, "%s already exists.\n", new_room_name);
1888 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1889 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1890 qrbuf.QRflags = QR_INUSE;
1891 if (new_room_type > 0)
1892 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1893 if (new_room_type == 1)
1894 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1895 if (new_room_type == 2)
1896 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1897 if ( (new_room_type == 4) || (new_room_type == 5) ) {
1898 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1899 /* qrbuf.QRflags2 |= QR2_SUBJECTREQ; */
1902 /* If the user is requesting a personal room, set up the room
1903 * name accordingly (prepend the user number)
1905 if (new_room_type == 4) {
1906 CtdlMailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1909 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1912 /* If the room is private, and the system administrator has elected
1913 * to automatically grant room aide privileges, do so now; otherwise,
1914 * set the room aide to undefined.
1916 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1917 qrbuf.QRroomaide = CC->user.usernum;
1919 qrbuf.QRroomaide = (-1L);
1923 * If the caller is only interested in testing whether this will work,
1924 * return now without creating the room.
1926 if (!really_create) return (qrbuf.QRflags);
1928 qrbuf.QRnumber = get_new_room_number();
1929 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1930 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1931 qrbuf.QRfloor = new_room_floor;
1932 qrbuf.QRdefaultview = new_room_view;
1934 /* save what we just did... */
1935 CtdlPutRoom(&qrbuf);
1937 /* bump the reference count on whatever floor the room is on */
1938 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1939 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1940 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1942 /* Grant the creator access to the room unless the avoid_access
1943 * parameter was specified.
1945 if ( (CC->logged_in) && (avoid_access == 0) ) {
1946 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1947 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1948 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1949 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1952 /* resume our happy day */
1953 return (qrbuf.QRflags);
1960 void cmd_cre8(char *args)
1963 char new_room_name[ROOMNAMELEN];
1965 char new_room_pass[32];
1968 char *notification_message = NULL;
1971 int avoid_access = 0;
1973 cre8_ok = extract_int(args, 0);
1974 extract_token(new_room_name, args, 1, '|', sizeof new_room_name);
1975 new_room_name[ROOMNAMELEN - 1] = 0;
1976 new_room_type = extract_int(args, 2);
1977 extract_token(new_room_pass, args, 3, '|', sizeof new_room_pass);
1978 avoid_access = extract_int(args, 5);
1979 new_room_view = extract_int(args, 6);
1980 new_room_pass[9] = 0;
1983 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 1)) {
1984 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
1988 if (!strcasecmp(new_room_name, MAILROOM)) {
1989 cprintf("%d '%s' already exists.\n",
1990 ERROR + ALREADY_EXISTS, new_room_name);
1994 if (num_parms(args) >= 5) {
1995 fl = CtdlGetCachedFloor(extract_int(args, 4));
1997 cprintf("%d Invalid floor number.\n",
1998 ERROR + INVALID_FLOOR_OPERATION);
2001 else if ((fl->f_flags & F_INUSE) == 0) {
2002 cprintf("%d Invalid floor number.\n",
2003 ERROR + INVALID_FLOOR_OPERATION);
2006 new_room_floor = extract_int(args, 4);
2010 if (CtdlAccessCheck(ac_logged_in)) return;
2012 if (CC->user.axlevel < config.c_createax && !CC->internal_pgm) {
2013 cprintf("%d You need higher access to create rooms.\n",
2014 ERROR + HIGHER_ACCESS_REQUIRED);
2018 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 0)) {
2019 cprintf("%d Ok to create rooms.\n", CIT_OK);
2023 if ((new_room_type < 0) || (new_room_type > 5)) {
2024 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
2028 if (new_room_type == 5) {
2029 if (CC->user.axlevel < AxAideU) {
2030 cprintf("%d Higher access required\n",
2031 ERROR + HIGHER_ACCESS_REQUIRED);
2036 /* Check to make sure the requested room name doesn't already exist */
2037 newflags = CtdlCreateRoom(new_room_name,
2038 new_room_type, new_room_pass, new_room_floor,
2039 0, avoid_access, new_room_view);
2040 if (newflags == 0) {
2041 cprintf("%d '%s' already exists.\n",
2042 ERROR + ALREADY_EXISTS, new_room_name);
2047 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
2051 /* If we reach this point, the room needs to be created. */
2053 newflags = CtdlCreateRoom(new_room_name,
2054 new_room_type, new_room_pass, new_room_floor, 1, 0,
2057 /* post a message in Aide> describing the new room */
2058 notification_message = malloc(1024);
2059 snprintf(notification_message, 1024,
2060 "A new room called \"%s\" has been created by %s%s%s%s%s%s\n",
2063 ((newflags & QR_MAILBOX) ? " [personal]" : ""),
2064 ((newflags & QR_PRIVATE) ? " [private]" : ""),
2065 ((newflags & QR_GUESSNAME) ? " [hidden]" : ""),
2066 ((newflags & QR_PASSWORDED) ? " Password: " : ""),
2067 ((newflags & QR_PASSWORDED) ? new_room_pass : "")
2069 CtdlAideMessage(notification_message, "Room Creation Message");
2070 free(notification_message);
2072 cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
2077 void cmd_einf(char *ok)
2078 { /* enter info file for current room */
2080 char infofilename[SIZ];
2085 if (CtdlAccessCheck(ac_room_aide)) return;
2087 if (atoi(ok) == 0) {
2088 cprintf("%d Ok.\n", CIT_OK);
2091 assoc_file_name(infofilename, sizeof infofilename, &CC->room, ctdl_info_dir);
2092 syslog(LOG_DEBUG, "opening\n");
2093 fp = fopen(infofilename, "w");
2094 syslog(LOG_DEBUG, "checking\n");
2096 cprintf("%d Cannot open %s: %s\n",
2097 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
2100 cprintf("%d Send info...\n", SEND_LISTING);
2103 client_getln(buf, sizeof buf);
2104 if (strcmp(buf, "000"))
2105 fprintf(fp, "%s\n", buf);
2106 } while (strcmp(buf, "000"));
2109 /* now update the room index so people will see our new info */
2110 CtdlGetRoomLock(&CC->room, CC->room.QRname); /* lock so no one steps on us */
2111 CC->room.QRinfo = CC->room.QRhighest + 1L;
2112 CtdlPutRoomLock(&CC->room);
2117 * cmd_lflr() - List all known floors
2119 void cmd_lflr(char *gargs)
2124 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
2126 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
2128 for (a = 0; a < MAXFLOORS; ++a) {
2129 CtdlGetFloor(&flbuf, a);
2130 if (flbuf.f_flags & F_INUSE) {
2131 cprintf("%d|%s|%d\n",
2143 * create a new floor
2145 void cmd_cflr(char *argbuf)
2147 char new_floor_name[256];
2150 int free_slot = (-1);
2153 extract_token(new_floor_name, argbuf, 0, '|', sizeof new_floor_name);
2154 cflr_ok = extract_int(argbuf, 1);
2156 if (CtdlAccessCheck(ac_aide)) return;
2158 if (IsEmptyStr(new_floor_name)) {
2159 cprintf("%d Blank floor name not allowed.\n",
2160 ERROR + ILLEGAL_VALUE);
2164 for (a = 0; a < MAXFLOORS; ++a) {
2165 CtdlGetFloor(&flbuf, a);
2167 /* note any free slots while we're scanning... */
2168 if (((flbuf.f_flags & F_INUSE) == 0)
2172 /* check to see if it already exists */
2173 if ((!strcasecmp(flbuf.f_name, new_floor_name))
2174 && (flbuf.f_flags & F_INUSE)) {
2175 cprintf("%d Floor '%s' already exists.\n",
2176 ERROR + ALREADY_EXISTS,
2182 if (free_slot < 0) {
2183 cprintf("%d There is no space available for a new floor.\n",
2184 ERROR + INVALID_FLOOR_OPERATION);
2188 cprintf("%d ok to create...\n", CIT_OK);
2191 lgetfloor(&flbuf, free_slot);
2192 flbuf.f_flags = F_INUSE;
2193 flbuf.f_ref_count = 0;
2194 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
2195 lputfloor(&flbuf, free_slot);
2196 cprintf("%d %d\n", CIT_OK, free_slot);
2204 void cmd_kflr(char *argbuf)
2207 int floor_to_delete;
2211 floor_to_delete = extract_int(argbuf, 0);
2212 kflr_ok = extract_int(argbuf, 1);
2214 if (CtdlAccessCheck(ac_aide)) return;
2216 lgetfloor(&flbuf, floor_to_delete);
2219 if ((flbuf.f_flags & F_INUSE) == 0) {
2220 cprintf("%d Floor %d not in use.\n",
2221 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
2224 if (flbuf.f_ref_count != 0) {
2225 cprintf("%d Cannot delete; floor contains %d rooms.\n",
2226 ERROR + INVALID_FLOOR_OPERATION,
2231 cprintf("%d Ok\n", CIT_OK);
2233 cprintf("%d Ok to delete...\n", CIT_OK);
2240 if ((delete_ok == 1) && (kflr_ok == 1))
2242 lputfloor(&flbuf, floor_to_delete);
2248 void cmd_eflr(char *argbuf)
2254 np = num_parms(argbuf);
2256 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
2260 if (CtdlAccessCheck(ac_aide)) return;
2262 floor_num = extract_int(argbuf, 0);
2263 lgetfloor(&flbuf, floor_num);
2264 if ((flbuf.f_flags & F_INUSE) == 0) {
2265 lputfloor(&flbuf, floor_num);
2266 cprintf("%d Floor %d is not in use.\n",
2267 ERROR + INVALID_FLOOR_OPERATION, floor_num);
2271 extract_token(flbuf.f_name, argbuf, 1, '|', sizeof flbuf.f_name);
2272 lputfloor(&flbuf, floor_num);
2274 cprintf("%d Ok\n", CIT_OK);
2278 /*****************************************************************************/
2279 /* MODULE INITIALIZATION STUFF */
2280 /*****************************************************************************/
2282 CTDL_MODULE_INIT(room_ops)
2285 CtdlRegisterProtoHook(cmd_lrms, "LRMS", "List rooms");
2286 CtdlRegisterProtoHook(cmd_lkra, "LKRA", "List all known rooms");
2287 CtdlRegisterProtoHook(cmd_lkrn, "LKRN", "List known rooms with new messages");
2288 CtdlRegisterProtoHook(cmd_lkro, "LKRO", "List known rooms without new messages");
2289 CtdlRegisterProtoHook(cmd_lzrm, "LZRM", "List zapped rooms");
2290 CtdlRegisterProtoHook(cmd_lprm, "LPRM", "List public rooms");
2291 CtdlRegisterProtoHook(cmd_goto, "GOTO", "Goto a named room");
2292 CtdlRegisterProtoHook(cmd_whok, "WHOK", "List users who know this room");
2293 CtdlRegisterProtoHook(cmd_rdir, "RDIR", "List files in room directory");
2294 CtdlRegisterProtoHook(cmd_getr, "GETR", "Get room parameters");
2295 CtdlRegisterProtoHook(cmd_setr, "SETR", "Set room parameters");
2296 CtdlRegisterProtoHook(cmd_geta, "GETA", "Get the room aide name");
2297 CtdlRegisterProtoHook(cmd_seta, "SETA", "Set the room aide for this room");
2298 CtdlRegisterProtoHook(cmd_rinf, "RINF", "Fetch room info file");
2299 CtdlRegisterProtoHook(cmd_kill, "KILL", "Kill (delete) the current room");
2300 CtdlRegisterProtoHook(cmd_cre8, "CRE8", "Create a new room");
2301 CtdlRegisterProtoHook(cmd_einf, "EINF", "Enter info file for the current room");
2302 CtdlRegisterProtoHook(cmd_lflr, "LFLR", "List all known floors");
2303 CtdlRegisterProtoHook(cmd_cflr, "CFLR", "Create a new floor");
2304 CtdlRegisterProtoHook(cmd_kflr, "KFLR", "Kill a floor");
2305 CtdlRegisterProtoHook(cmd_eflr, "EFLR", "Edit a floor");
2307 /* return our Subversion id for the Log */