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
166 * - It is a blog room (in which case we only allow replies to existing messages)
168 int post_allowed = 1;
169 int reply_allowed = 1;
170 if (userbuf->axlevel < AxProbU) {
174 if ((userbuf->axlevel < AxNetU) && (roombuf->QRflags & QR_NETWORK)) {
178 if (roombuf->QRflags & QR_READONLY) {
182 if (roombuf->QRdefaultview == VIEW_BLOG) {
186 retval = retval | UA_POSTALLOWED | UA_REPLYALLOWED;
189 retval = retval | UA_REPLYALLOWED;
192 /* If "collaborative deletion" is active for this room, any user who can post
193 * is also allowed to delete
195 if (roombuf->QRflags2 & QR2_COLLABDEL) {
196 if (retval & UA_POSTALLOWED) {
197 retval = retval | UA_DELETEALLOWED;
203 /* Check to see if the user has forgotten this room */
204 if (vbuf.v_flags & V_FORGET) {
205 retval = retval & ~UA_KNOWN;
206 if ( ( ((roombuf->QRflags & QR_PRIVATE) == 0)
207 && ((roombuf->QRflags & QR_MAILBOX) == 0)
208 ) || ( (roombuf->QRflags & QR_MAILBOX)
209 && (atol(roombuf->QRname) == CC->user.usernum))
211 retval = retval | UA_ZAPPED;
215 /* If user is explicitly locked out of this room, deny everything */
216 if (vbuf.v_flags & V_LOCKOUT) {
217 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED & ~UA_POSTALLOWED & ~UA_REPLYALLOWED;
220 /* Aides get access to all private rooms */
221 if ( (userbuf->axlevel >= AxAideU)
222 && ((roombuf->QRflags & QR_MAILBOX) == 0)
224 if (vbuf.v_flags & V_FORGET) {
225 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
228 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
232 /* Aides can gain access to mailboxes as well, but they don't show
235 if ( (userbuf->axlevel >= AxAideU)
236 && (roombuf->QRflags & QR_MAILBOX)
238 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
241 /* Aides and Room Aides have admin privileges */
242 if ( (userbuf->axlevel >= AxAideU)
243 || (userbuf->usernum == roombuf->QRroomaide)
245 retval = retval | UA_ADMINALLOWED | UA_DELETEALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
248 NEWMSG: /* By the way, we also check for the presence of new messages */
249 if (is_msg_in_sequence_set(vbuf.v_seen, roombuf->QRhighest) == 0) {
250 retval = retval | UA_HASNEWMSGS;
253 /* System rooms never show up in the list. */
254 if (roombuf->QRflags2 & QR2_SYSTEM) {
255 retval = retval & ~UA_KNOWN;
259 /* Now give the caller the information it wants. */
260 if (result != NULL) *result = retval;
261 if (view != NULL) *view = vbuf.v_view;
265 * Self-checking stuff for a room record read into memory
267 void room_sanity_check(struct ctdlroom *qrbuf)
269 /* Mailbox rooms are always on the lowest floor */
270 if (qrbuf->QRflags & QR_MAILBOX) {
273 /* Listing order of 0 is illegal except for base rooms */
274 if (qrbuf->QRorder == 0)
275 if (!(qrbuf->QRflags & QR_MAILBOX) &&
276 strncasecmp(qrbuf->QRname, config.c_baseroom, ROOMNAMELEN)
278 strncasecmp(qrbuf->QRname, config.c_aideroom, ROOMNAMELEN))
284 * CtdlGetRoom() - retrieve room data from disk
286 int CtdlGetRoom(struct ctdlroom *qrbuf, char *room_name)
288 struct cdbdata *cdbqr;
289 char lowercase_name[ROOMNAMELEN];
290 char personal_lowercase_name[ROOMNAMELEN];
291 char *dptr, *sptr, *eptr;
293 dptr = lowercase_name;
295 eptr = (dptr + (sizeof lowercase_name - 1));
296 while (!IsEmptyStr(sptr) && (dptr < eptr)){
297 *dptr = tolower(*sptr);
302 memset(qrbuf, 0, sizeof(struct ctdlroom));
304 /* First, try the public namespace */
305 cdbqr = cdb_fetch(CDB_ROOMS,
306 lowercase_name, strlen(lowercase_name));
308 /* If that didn't work, try the user's personal namespace */
310 snprintf(personal_lowercase_name,
311 sizeof personal_lowercase_name, "%010ld.%s",
312 CC->user.usernum, lowercase_name);
313 cdbqr = cdb_fetch(CDB_ROOMS,
314 personal_lowercase_name,
315 strlen(personal_lowercase_name));
318 memcpy(qrbuf, cdbqr->ptr,
319 ((cdbqr->len > sizeof(struct ctdlroom)) ?
320 sizeof(struct ctdlroom) : cdbqr->len));
323 room_sanity_check(qrbuf);
332 * CtdlGetRoomLock() - same as getroom() but locks the record (if supported)
334 int CtdlGetRoomLock(struct ctdlroom *qrbuf, char *room_name)
337 retval = CtdlGetRoom(qrbuf, room_name);
338 if (retval == 0) begin_critical_section(S_ROOMS);
344 * b_putroom() - back end to putroom() and b_deleteroom()
345 * (if the supplied buffer is NULL, delete the room record)
347 void b_putroom(struct ctdlroom *qrbuf, char *room_name)
349 char lowercase_name[ROOMNAMELEN];
354 bptr = lowercase_name;
355 while (!IsEmptyStr(aptr))
357 *bptr = tolower(*aptr);
363 len = bptr - lowercase_name;
365 cdb_delete(CDB_ROOMS,
366 lowercase_name, len);
368 time(&qrbuf->QRmtime);
371 qrbuf, sizeof(struct ctdlroom));
377 * CtdlPutRoom() - store room data to disk
379 void CtdlPutRoom(struct ctdlroom *qrbuf) {
380 b_putroom(qrbuf, qrbuf->QRname);
385 * b_deleteroom() - delete a room record from disk
387 void b_deleteroom(char *room_name) {
388 b_putroom(NULL, room_name);
394 * CtdlPutRoomLock() - same as CtdlPutRoom() but unlocks the record (if supported)
396 void CtdlPutRoomLock(struct ctdlroom *qrbuf)
400 end_critical_section(S_ROOMS);
404 /****************************************************************************/
408 * CtdlGetFloorByName() - retrieve the number of the named floor
409 * return < 0 if not found else return floor number
411 int CtdlGetFloorByName(const char *floor_name)
414 struct floor *flbuf = NULL;
416 for (a = 0; a < MAXFLOORS; ++a) {
417 flbuf = CtdlGetCachedFloor(a);
419 /* check to see if it already exists */
420 if ((!strcasecmp(flbuf->f_name, floor_name))
421 && (flbuf->f_flags & F_INUSE)) {
431 * CtdlGetFloorByNameLock() - retrieve floor number for given floor and lock the floor list.
433 int CtdlGetFloorByNameLock(const char *floor_name)
435 begin_critical_section(S_FLOORTAB);
436 return CtdlGetFloorByName(floor_name);
442 * CtdlGetAvailableFloor() - Return number of first unused floor
443 * return < 0 if none available
445 int CtdlGetAvailableFloor(void)
448 struct floor *flbuf = NULL;
450 for (a = 0; a < MAXFLOORS; a++) {
451 flbuf = CtdlGetCachedFloor(a);
453 /* check to see if it already exists */
454 if ((flbuf->f_flags & F_INUSE) == 0) {
463 * CtdlGetFloor() - retrieve floor data from disk
465 void CtdlGetFloor(struct floor *flbuf, int floor_num)
467 struct cdbdata *cdbfl;
469 memset(flbuf, 0, sizeof(struct floor));
470 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
472 memcpy(flbuf, cdbfl->ptr,
473 ((cdbfl->len > sizeof(struct floor)) ?
474 sizeof(struct floor) : cdbfl->len));
477 if (floor_num == 0) {
478 safestrncpy(flbuf->f_name, "Main Floor",
479 sizeof flbuf->f_name);
480 flbuf->f_flags = F_INUSE;
481 flbuf->f_ref_count = 3;
488 * lgetfloor() - same as CtdlGetFloor() but locks the record (if supported)
490 void lgetfloor(struct floor *flbuf, int floor_num)
493 begin_critical_section(S_FLOORTAB);
494 CtdlGetFloor(flbuf, floor_num);
499 * CtdlGetCachedFloor() - Get floor record from *cache* (loads from disk if needed)
501 * This is strictly a performance hack.
503 struct floor *CtdlGetCachedFloor(int floor_num) {
504 static int initialized = 0;
507 struct floor *fl = NULL;
509 begin_critical_section(S_FLOORCACHE);
510 if (initialized == 0) {
511 for (i=0; i<MAXFLOORS; ++i) {
512 floorcache[floor_num] = NULL;
516 if (floorcache[floor_num] == NULL) {
519 end_critical_section(S_FLOORCACHE);
522 fl = malloc(sizeof(struct floor));
523 CtdlGetFloor(fl, floor_num);
524 begin_critical_section(S_FLOORCACHE);
525 if (floorcache[floor_num] != NULL) {
526 free(floorcache[floor_num]);
528 floorcache[floor_num] = fl;
529 end_critical_section(S_FLOORCACHE);
532 return(floorcache[floor_num]);
538 * CtdlPutFloor() - store floor data on disk
540 void CtdlPutFloor(struct floor *flbuf, int floor_num)
542 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
543 begin_critical_section(S_FLOORCACHE);
544 if (floorcache[floor_num] != NULL) {
545 free(floorcache[floor_num]);
546 floorcache[floor_num] = malloc(sizeof(struct floor));
547 memcpy(floorcache[floor_num], flbuf, sizeof(struct floor));
549 end_critical_section(S_FLOORCACHE);
551 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
552 flbuf, sizeof(struct floor));
558 * CtdlPutFloorLock() - same as CtdlPutFloor() but unlocks the record (if supported)
560 void CtdlPutFloorLock(struct floor *flbuf, int floor_num)
563 CtdlPutFloor(flbuf, floor_num);
564 end_critical_section(S_FLOORTAB);
571 * lputfloor() - same as CtdlPutFloor() but unlocks the record (if supported)
573 void lputfloor(struct floor *flbuf, int floor_num)
575 CtdlPutFloorLock(flbuf, floor_num);
580 * Traverse the room file...
582 void CtdlForEachRoom(void (*CallBack) (struct ctdlroom *EachRoom, void *out_data),
585 struct ctdlroom qrbuf;
586 struct cdbdata *cdbqr;
588 cdb_rewind(CDB_ROOMS);
590 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
591 memset(&qrbuf, 0, sizeof(struct ctdlroom));
592 memcpy(&qrbuf, cdbqr->ptr,
593 ((cdbqr->len > sizeof(struct ctdlroom)) ?
594 sizeof(struct ctdlroom) : cdbqr->len));
596 room_sanity_check(&qrbuf);
597 if (qrbuf.QRflags & QR_INUSE)
598 (*CallBack)(&qrbuf, in_data);
604 * delete_msglist() - delete room message pointers
606 void delete_msglist(struct ctdlroom *whichroom)
608 struct cdbdata *cdbml;
610 /* Make sure the msglist we're deleting actually exists, otherwise
611 * gdbm will complain when we try to delete an invalid record
613 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
617 /* Go ahead and delete it */
618 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
625 * Message pointer compare function for sort_msglist()
627 int sort_msglist_cmp(const void *m1, const void *m2) {
628 if ((*(const long *)m1) > (*(const long *)m2)) return(1);
629 if ((*(const long *)m1) < (*(const long *)m2)) return(-1);
635 * sort message pointers
636 * (returns new msg count)
638 int sort_msglist(long listptrs[], int oldcount)
648 qsort(listptrs, numitems, sizeof(long), sort_msglist_cmp);
650 /* and yank any nulls */
651 while ((numitems > 0) && (listptrs[0] == 0L)) {
652 memmove(&listptrs[0], &listptrs[1],
653 (sizeof(long) * (numitems - 1)));
662 * Determine whether a given room is non-editable.
664 int CtdlIsNonEditable(struct ctdlroom *qrbuf)
667 /* Mail> rooms are non-editable */
668 if ( (qrbuf->QRflags & QR_MAILBOX)
669 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
672 /* Everything else is editable */
679 * Back-back-end for all room listing commands
681 void list_roomname(struct ctdlroom *qrbuf, int ra, int current_view, int default_view)
683 char truncated_roomname[ROOMNAMELEN];
685 /* For my own mailbox rooms, chop off the owner prefix */
686 if ( (qrbuf->QRflags & QR_MAILBOX)
687 && (atol(qrbuf->QRname) == CC->user.usernum) ) {
688 safestrncpy(truncated_roomname, qrbuf->QRname, sizeof truncated_roomname);
689 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
690 cprintf("%s", truncated_roomname);
692 /* For all other rooms, just display the name in its entirety */
694 cprintf("%s", qrbuf->QRname);
697 /* ...and now the other parameters */
698 cprintf("|%u|%d|%d|%d|%d|%d|%d|%ld|\n",
700 (int) qrbuf->QRfloor,
701 (int) qrbuf->QRorder,
702 (int) qrbuf->QRflags2,
712 * cmd_lrms() - List all accessible rooms, known or forgotten
714 void cmd_lrms_backend(struct ctdlroom *qrbuf, void *data)
716 int FloorBeingSearched = (-1);
720 FloorBeingSearched = *(int *)data;
721 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
723 if ((( ra & (UA_KNOWN | UA_ZAPPED)))
724 && ((qrbuf->QRfloor == (FloorBeingSearched))
725 || ((FloorBeingSearched) < 0)))
726 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
729 void cmd_lrms(char *argbuf)
731 int FloorBeingSearched = (-1);
732 if (!IsEmptyStr(argbuf))
733 FloorBeingSearched = extract_int(argbuf, 0);
735 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
737 CtdlGetUser(&CC->user, CC->curr_user);
738 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
740 CtdlForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
747 * cmd_lkra() - List all known rooms
749 void cmd_lkra_backend(struct ctdlroom *qrbuf, void *data)
751 int FloorBeingSearched = (-1);
755 FloorBeingSearched = *(int *)data;
756 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
758 if ((( ra & (UA_KNOWN)))
759 && ((qrbuf->QRfloor == (FloorBeingSearched))
760 || ((FloorBeingSearched) < 0)))
761 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
764 void cmd_lkra(char *argbuf)
766 int FloorBeingSearched = (-1);
767 if (!IsEmptyStr(argbuf))
768 FloorBeingSearched = extract_int(argbuf, 0);
770 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
772 CtdlGetUser(&CC->user, CC->curr_user);
773 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
775 CtdlForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
781 void cmd_lprm_backend(struct ctdlroom *qrbuf, void *data)
783 int FloorBeingSearched = (-1);
787 FloorBeingSearched = *(int *)data;
788 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
790 if ( ((qrbuf->QRflags & QR_PRIVATE) == 0)
791 && ((qrbuf->QRflags & QR_MAILBOX) == 0)
792 && ((qrbuf->QRfloor == (FloorBeingSearched))
793 || ((FloorBeingSearched) < 0)))
794 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
797 void cmd_lprm(char *argbuf)
799 int FloorBeingSearched = (-1);
800 if (!IsEmptyStr(argbuf))
801 FloorBeingSearched = extract_int(argbuf, 0);
803 cprintf("%d Public rooms:\n", LISTING_FOLLOWS);
805 CtdlForEachRoom(cmd_lprm_backend, &FloorBeingSearched);
812 * cmd_lkrn() - List all known rooms with new messages
814 void cmd_lkrn_backend(struct ctdlroom *qrbuf, void *data)
816 int FloorBeingSearched = (-1);
820 FloorBeingSearched = *(int *)data;
821 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
824 && (ra & UA_HASNEWMSGS)
825 && ((qrbuf->QRfloor == (FloorBeingSearched))
826 || ((FloorBeingSearched) < 0)))
827 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
830 void cmd_lkrn(char *argbuf)
832 int FloorBeingSearched = (-1);
833 if (!IsEmptyStr(argbuf))
834 FloorBeingSearched = extract_int(argbuf, 0);
836 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
838 CtdlGetUser(&CC->user, CC->curr_user);
839 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
841 CtdlForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
848 * cmd_lkro() - List all known rooms
850 void cmd_lkro_backend(struct ctdlroom *qrbuf, void *data)
852 int FloorBeingSearched = (-1);
856 FloorBeingSearched = *(int *)data;
857 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
860 && ((ra & UA_HASNEWMSGS) == 0)
861 && ((qrbuf->QRfloor == (FloorBeingSearched))
862 || ((FloorBeingSearched) < 0)))
863 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
866 void cmd_lkro(char *argbuf)
868 int FloorBeingSearched = (-1);
869 if (!IsEmptyStr(argbuf))
870 FloorBeingSearched = extract_int(argbuf, 0);
872 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
874 CtdlGetUser(&CC->user, CC->curr_user);
875 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
877 CtdlForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
884 * cmd_lzrm() - List all forgotten rooms
886 void cmd_lzrm_backend(struct ctdlroom *qrbuf, void *data)
888 int FloorBeingSearched = (-1);
892 FloorBeingSearched = *(int *)data;
893 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
895 if ((ra & UA_GOTOALLOWED)
897 && ((qrbuf->QRfloor == (FloorBeingSearched))
898 || ((FloorBeingSearched) < 0)))
899 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
902 void cmd_lzrm(char *argbuf)
904 int FloorBeingSearched = (-1);
905 if (!IsEmptyStr(argbuf))
906 FloorBeingSearched = extract_int(argbuf, 0);
908 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
910 CtdlGetUser(&CC->user, CC->curr_user);
911 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
913 CtdlForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
919 * Make the specified room the current room for this session. No validation
920 * or access control is done here -- the caller should make sure that the
921 * specified room exists and is ok to access.
923 void CtdlUserGoto(char *where, int display_result, int transiently,
924 int *retmsgs, int *retnew)
926 struct CitContext *CCC = CC;
928 int new_messages = 0;
929 int old_messages = 0;
930 int total_messages = 0;
934 int newmailcount = 0;
936 char truncated_roomname[ROOMNAMELEN];
937 struct cdbdata *cdbfr;
938 long *msglist = NULL;
940 unsigned int original_v_flags;
943 char setstr[128], lostr[64], histr[64];
947 /* If the supplied room name is NULL, the caller wants us to know that
948 * it has already copied the room record into CC->room, so
949 * we can skip the extra database fetch.
952 safestrncpy(CCC->room.QRname, where, sizeof CCC->room.QRname);
953 CtdlGetRoom(&CCC->room, where);
956 /* Take care of all the formalities. */
958 begin_critical_section(S_USERS);
959 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
960 original_v_flags = vbuf.v_flags;
962 /* Know the room ... but not if it's the page log room, or if the
963 * caller specified that we're only entering this room transiently.
965 if ((strcasecmp(CCC->room.QRname, config.c_logpages))
966 && (transiently == 0) ) {
967 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
968 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
971 /* Only rewrite the database record if we changed something */
972 if (vbuf.v_flags != original_v_flags) {
973 CtdlSetRelationship(&vbuf, &CCC->user, &CCC->room);
975 end_critical_section(S_USERS);
977 /* Check for new mail */
978 newmailcount = NewMailCount();
980 /* set info to 1 if the user needs to read the room's info file */
981 if (CCC->room.QRinfo > vbuf.v_lastseen) {
985 cdbfr = cdb_fetch(CDB_MSGLISTS, &CCC->room.QRnumber, sizeof(long));
987 msglist = (long *) cdbfr->ptr;
988 cdbfr->ptr = NULL; /* CtdlUserGoto() now owns this memory */
989 num_msgs = cdbfr->len / sizeof(long);
994 for (a=0; a<num_msgs; ++a) {
995 if (msglist[a] > 0L) ++total_messages;
998 num_sets = num_tokens(vbuf.v_seen, ',');
999 for (s=0; s<num_sets; ++s) {
1000 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
1002 extract_token(lostr, setstr, 0, ':', sizeof lostr);
1003 if (num_tokens(setstr, ':') >= 2) {
1004 extract_token(histr, setstr, 1, ':', sizeof histr);
1005 if (!strcmp(histr, "*")) {
1006 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
1010 strcpy(histr, lostr);
1015 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
1016 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
1022 new_messages = total_messages - old_messages;
1024 if (msglist != NULL) free(msglist);
1026 if (CCC->room.QRflags & QR_MAILBOX)
1031 if ((CCC->room.QRroomaide == CCC->user.usernum)
1032 || (CCC->user.axlevel >= AxAideU))
1037 safestrncpy(truncated_roomname, CCC->room.QRname, sizeof truncated_roomname);
1038 if ( (CCC->room.QRflags & QR_MAILBOX)
1039 && (atol(CCC->room.QRname) == CCC->user.usernum) ) {
1040 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
1043 if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
1047 if (retmsgs != NULL) *retmsgs = total_messages;
1048 if (retnew != NULL) *retnew = new_messages;
1049 MSG_syslog(LOG_INFO, "<%s> %d new of %d total messages\n",
1051 new_messages, total_messages
1054 CCC->curr_view = (int)vbuf.v_view;
1056 if (display_result) {
1057 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|%d|\n",
1058 CIT_OK, CtdlCheckExpress(),
1061 (int)total_messages,
1063 (int)CCC->room.QRflags,
1064 (long)CCC->room.QRhighest,
1065 (long)vbuf.v_lastseen,
1069 (int)CCC->room.QRfloor,
1071 (int)CCC->room.QRdefaultview,
1073 (int)CCC->room.QRflags2
1080 * Handle some of the macro named rooms
1082 void convert_room_name_macros(char *towhere, size_t maxlen) {
1083 if (!strcasecmp(towhere, "_BASEROOM_")) {
1084 safestrncpy(towhere, config.c_baseroom, maxlen);
1086 else if (!strcasecmp(towhere, "_MAIL_")) {
1087 safestrncpy(towhere, MAILROOM, maxlen);
1089 else if (!strcasecmp(towhere, "_TRASH_")) {
1090 safestrncpy(towhere, USERTRASHROOM, maxlen);
1092 else if (!strcasecmp(towhere, "_DRAFTS_")) {
1093 safestrncpy(towhere, USERDRAFTROOM, maxlen);
1095 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
1096 safestrncpy(towhere, config.c_twitroom, maxlen);
1098 else if (!strcasecmp(towhere, "_CALENDAR_")) {
1099 safestrncpy(towhere, USERCALENDARROOM, maxlen);
1101 else if (!strcasecmp(towhere, "_TASKS_")) {
1102 safestrncpy(towhere, USERTASKSROOM, maxlen);
1104 else if (!strcasecmp(towhere, "_CONTACTS_")) {
1105 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
1107 else if (!strcasecmp(towhere, "_NOTES_")) {
1108 safestrncpy(towhere, USERNOTESROOM, maxlen);
1114 * cmd_goto() - goto a new room
1116 void cmd_goto(char *gargs)
1118 struct ctdlroom QRscratch;
1122 char augmented_roomname[ROOMNAMELEN];
1123 char towhere[ROOMNAMELEN];
1125 int transiently = 0;
1127 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
1129 extract_token(towhere, gargs, 0, '|', sizeof towhere);
1130 extract_token(password, gargs, 1, '|', sizeof password);
1131 transiently = extract_int(gargs, 2);
1133 CtdlGetUser(&CC->user, CC->curr_user);
1136 * Handle some of the macro named rooms
1138 convert_room_name_macros(towhere, sizeof towhere);
1140 /* First try a regular match */
1141 c = CtdlGetRoom(&QRscratch, towhere);
1143 /* Then try a mailbox name match */
1145 CtdlMailboxName(augmented_roomname, sizeof augmented_roomname,
1146 &CC->user, towhere);
1147 c = CtdlGetRoom(&QRscratch, augmented_roomname);
1149 safestrncpy(towhere, augmented_roomname, sizeof towhere);
1152 /* And if the room was found... */
1155 /* Let internal programs go directly to any room. */
1156 if (CC->internal_pgm) {
1157 memcpy(&CC->room, &QRscratch,
1158 sizeof(struct ctdlroom));
1159 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1163 /* See if there is an existing user/room relationship */
1164 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
1166 /* normal clients have to pass through security */
1167 if (ra & UA_GOTOALLOWED) {
1172 if ((QRscratch.QRflags & QR_MAILBOX) &&
1173 ((ra & UA_GOTOALLOWED))) {
1174 memcpy(&CC->room, &QRscratch,
1175 sizeof(struct ctdlroom));
1176 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1178 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
1179 ((ra & UA_KNOWN) == 0) &&
1180 (strcasecmp(QRscratch.QRpasswd, password)) &&
1181 (CC->user.axlevel < AxAideU)
1183 cprintf("%d wrong or missing passwd\n",
1184 ERROR + PASSWORD_REQUIRED);
1186 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
1187 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
1188 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
1189 ((ra & UA_KNOWN) == 0) &&
1190 (CC->user.axlevel < AxAideU)
1192 syslog(LOG_DEBUG, "Failed to acquire private room\n");
1194 memcpy(&CC->room, &QRscratch,
1195 sizeof(struct ctdlroom));
1196 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1202 cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
1206 void cmd_whok(char *cmdbuf)
1208 struct ctdluser temp;
1209 struct cdbdata *cdbus;
1212 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
1213 cdb_rewind(CDB_USERS);
1214 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1215 memset(&temp, 0, sizeof temp);
1216 memcpy(&temp, cdbus->ptr, sizeof temp);
1219 CtdlRoomAccess(&CC->room, &temp, &ra, NULL);
1220 if ((!IsEmptyStr(temp.fullname)) &&
1221 (CC->room.QRflags & QR_INUSE) &&
1224 cprintf("%s\n", temp.fullname);
1231 * RDIR command for room directory
1233 void cmd_rdir(char *cmdbuf)
1238 struct stat statbuf;
1239 DIR *filedir = NULL;
1240 struct dirent *filedir_entry;
1246 if (CtdlAccessCheck(ac_logged_in)) return;
1248 CtdlGetRoom(&CC->room, CC->room.QRname);
1249 CtdlGetUser(&CC->user, CC->curr_user);
1251 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
1252 cprintf("%d not here.\n", ERROR + NOT_HERE);
1255 if (((CC->room.QRflags & QR_VISDIR) == 0)
1256 && (CC->user.axlevel < AxAideU)
1257 && (CC->user.usernum != CC->room.QRroomaide)) {
1258 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1262 snprintf(buf, sizeof buf, "%s/%s", ctdl_file_dir, CC->room.QRdirname);
1263 filedir = opendir (buf);
1265 if (filedir == NULL) {
1266 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1269 cprintf("%d %s|%s/%s\n", LISTING_FOLLOWS, config.c_fqdn, ctdl_file_dir, CC->room.QRdirname);
1271 snprintf(buf, sizeof buf, "%s/%s/filedir", ctdl_file_dir, CC->room.QRdirname);
1272 fd = fopen(buf, "r");
1274 fd = fopen("/dev/null", "r");
1275 while ((filedir_entry = readdir(filedir)))
1277 if (strcasecmp(filedir_entry->d_name, "filedir") && filedir_entry->d_name[0] != '.')
1279 #ifdef _DIRENT_HAVE_D_NAMELEN
1280 d_namelen = filedir_entry->d_namelen;
1282 d_namelen = strlen(filedir_entry->d_name);
1284 snprintf(buf, sizeof buf, "%s/%s/%s", ctdl_file_dir, CC->room.QRdirname, filedir_entry->d_name);
1285 stat(buf, &statbuf); /* stat the file */
1286 if (!(statbuf.st_mode & S_IFREG))
1288 snprintf(buf2, sizeof buf2,
1289 "\"%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",
1290 buf, CC->room.QRname
1292 CtdlAideMessage(buf2, "Unusable data found in room directory");
1293 continue; /* not a useable file type so don't show it */
1295 safestrncpy(comment, "", sizeof comment);
1296 fseek(fd, 0L, 0); /* rewind descriptions file */
1297 /* Get the description from the descriptions file */
1298 while ((fgets(buf, sizeof buf, fd) != NULL) && (IsEmptyStr(comment)))
1300 buf[strlen(buf) - 1] = 0;
1301 if ((!strncasecmp(buf, filedir_entry->d_name, d_namelen)) && (buf[d_namelen] == ' '))
1302 safestrncpy(comment, &buf[d_namelen + 1], sizeof comment);
1304 len = extract_token (mimebuf, comment, 0,' ', 64);
1305 if ((len <0) || strchr(mimebuf, '/') == NULL)
1307 snprintf (mimebuf, 64, "application/octetstream");
1310 cprintf("%s|%ld|%s|%s\n",
1311 filedir_entry->d_name,
1312 (long)statbuf.st_size,
1324 * get room parameters (aide or room aide command)
1326 void cmd_getr(char *cmdbuf)
1328 if (CtdlAccessCheck(ac_room_aide)) return;
1330 CtdlGetRoom(&CC->room, CC->room.QRname);
1331 cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1335 ((CC->room.QRflags & QR_MAILBOX) ?
1336 &CC->room.QRname[11] : CC->room.QRname),
1338 ((CC->room.QRflags & QR_PASSWORDED) ?
1339 CC->room.QRpasswd : ""),
1341 ((CC->room.QRflags & QR_DIRECTORY) ?
1342 CC->room.QRdirname : ""),
1345 (int) CC->room.QRfloor,
1346 (int) CC->room.QRorder,
1348 CC->room.QRdefaultview,
1355 * Back end function to rename a room.
1356 * You can also specify which floor to move the room to, or specify -1 to
1357 * keep the room on the same floor it was on.
1359 * If you are renaming a mailbox room, you must supply the namespace prefix
1360 * in *at least* the old name!
1362 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1364 struct ctdlroom qrbuf;
1365 struct ctdlroom qrtmp;
1370 char actual_old_name[ROOMNAMELEN];
1372 syslog(LOG_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1373 old_name, new_name, new_floor);
1375 if (new_floor >= 0) {
1376 fl = CtdlGetCachedFloor(new_floor);
1377 if ((fl->f_flags & F_INUSE) == 0) {
1378 return(crr_invalid_floor);
1382 begin_critical_section(S_ROOMS);
1384 if ( (CtdlGetRoom(&qrtmp, new_name) == 0)
1385 && (strcasecmp(new_name, old_name)) ) {
1386 ret = crr_already_exists;
1389 else if (CtdlGetRoom(&qrbuf, old_name) != 0) {
1390 ret = crr_room_not_found;
1393 else if ( (CC->user.axlevel < AxAideU) && (!CC->internal_pgm)
1394 && (CC->user.usernum != qrbuf.QRroomaide)
1395 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) ) {
1396 ret = crr_access_denied;
1399 else if (CtdlIsNonEditable(&qrbuf)) {
1400 ret = crr_noneditable;
1405 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1406 if (qrbuf.QRflags & QR_MAILBOX) {
1407 owner = atol(qrbuf.QRname);
1409 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1410 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1411 "%010ld.%s", owner, new_name);
1414 safestrncpy(qrbuf.QRname, new_name,
1415 sizeof(qrbuf.QRname));
1418 /* Reject change of floor for baseroom/aideroom */
1419 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1420 !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1424 /* Take care of floor stuff */
1425 old_floor = qrbuf.QRfloor;
1426 if (new_floor < 0) {
1427 new_floor = old_floor;
1429 qrbuf.QRfloor = new_floor;
1430 CtdlPutRoom(&qrbuf);
1432 begin_critical_section(S_CONFIG);
1434 /* If baseroom/aideroom name changes, update config */
1435 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1436 safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1439 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1440 safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1444 end_critical_section(S_CONFIG);
1446 /* If the room name changed, then there are now two room
1447 * records, so we have to delete the old one.
1449 if (strcasecmp(new_name, old_name)) {
1450 b_deleteroom(actual_old_name);
1456 end_critical_section(S_ROOMS);
1458 /* Adjust the floor reference counts if necessary */
1459 if (new_floor != old_floor) {
1460 lgetfloor(&flbuf, old_floor);
1461 --flbuf.f_ref_count;
1462 lputfloor(&flbuf, old_floor);
1463 syslog(LOG_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1464 lgetfloor(&flbuf, new_floor);
1465 ++flbuf.f_ref_count;
1466 lputfloor(&flbuf, new_floor);
1467 syslog(LOG_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1470 /* ...and everybody say "YATTA!" */
1476 * set room parameters (aide or room aide command)
1478 void cmd_setr(char *args)
1484 char new_name[ROOMNAMELEN];
1486 if (CtdlAccessCheck(ac_logged_in)) return;
1488 if (num_parms(args) >= 6) {
1489 new_floor = extract_int(args, 5);
1491 new_floor = (-1); /* don't change the floor */
1494 /* When is a new name more than just a new name? When the old name
1495 * has a namespace prefix.
1497 if (CC->room.QRflags & QR_MAILBOX) {
1498 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
1500 safestrncpy(new_name, "", sizeof new_name);
1502 extract_token(&new_name[strlen(new_name)], args, 0, '|', (sizeof new_name - strlen(new_name)));
1504 r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
1506 if (r == crr_room_not_found) {
1507 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
1508 } else if (r == crr_already_exists) {
1509 cprintf("%d '%s' already exists.\n",
1510 ERROR + ALREADY_EXISTS, new_name);
1511 } else if (r == crr_noneditable) {
1512 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
1513 } else if (r == crr_invalid_floor) {
1514 cprintf("%d Target floor does not exist.\n",
1515 ERROR + INVALID_FLOOR_OPERATION);
1516 } else if (r == crr_access_denied) {
1517 cprintf("%d You do not have permission to edit '%s'\n",
1518 ERROR + HIGHER_ACCESS_REQUIRED,
1520 } else if (r != crr_ok) {
1521 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1522 ERROR + INTERNAL_ERROR, r);
1529 CtdlGetRoom(&CC->room, new_name);
1531 /* Now we have to do a bunch of other stuff */
1533 if (num_parms(args) >= 7) {
1534 new_order = extract_int(args, 6);
1537 if (new_order > 127)
1541 CtdlGetRoomLock(&CC->room, CC->room.QRname);
1543 /* Directory room */
1544 extract_token(buf, args, 2, '|', sizeof buf);
1546 safestrncpy(CC->room.QRdirname, buf,
1547 sizeof CC->room.QRdirname);
1550 if (num_parms(args) >= 8) {
1551 CC->room.QRdefaultview = extract_int(args, 7);
1554 /* Second set of flags */
1555 if (num_parms(args) >= 9) {
1556 CC->room.QRflags2 = extract_int(args, 8);
1560 CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
1561 /* Clean up a client boo-boo: if the client set the room to
1562 * guess-name or passworded, ensure that the private flag is
1565 if ((CC->room.QRflags & QR_GUESSNAME)
1566 || (CC->room.QRflags & QR_PASSWORDED))
1567 CC->room.QRflags |= QR_PRIVATE;
1569 /* Some changes can't apply to BASEROOM */
1570 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1572 CC->room.QRorder = 0;
1573 CC->room.QRpasswd[0] = '\0';
1574 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1575 QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1576 CC->room.QRflags |= QR_PERMANENT;
1578 /* March order (doesn't apply to AIDEROOM) */
1579 if (num_parms(args) >= 7)
1580 CC->room.QRorder = (char) new_order;
1582 extract_token(buf, args, 1, '|', sizeof buf);
1584 safestrncpy(CC->room.QRpasswd, buf,
1585 sizeof CC->room.QRpasswd);
1586 /* Kick everyone out if the client requested it
1587 * (by changing the room's generation number)
1589 if (extract_int(args, 4)) {
1590 time(&CC->room.QRgen);
1593 /* Some changes can't apply to AIDEROOM */
1594 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1596 CC->room.QRorder = 0;
1597 CC->room.QRflags &= ~QR_MAILBOX;
1598 CC->room.QRflags |= QR_PERMANENT;
1601 /* Write the room record back to disk */
1602 CtdlPutRoomLock(&CC->room);
1604 /* Create a room directory if necessary */
1605 if (CC->room.QRflags & QR_DIRECTORY) {
1606 snprintf(buf, sizeof buf,"%s/%s",
1608 CC->room.QRdirname);
1611 snprintf(buf, sizeof buf, "The room \"%s\" has been edited by %s.\n",
1613 (CC->logged_in ? CC->curr_user : "an administrator")
1615 CtdlAideMessage(buf, "Room modification Message");
1616 cprintf("%d Ok\n", CIT_OK);
1622 * get the name of the room aide for this room
1624 void cmd_geta(char *cmdbuf)
1626 struct ctdluser usbuf;
1628 if (CtdlAccessCheck(ac_logged_in)) return;
1630 if (CtdlGetUserByNumber(&usbuf, CC->room.QRroomaide) == 0) {
1631 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1633 cprintf("%d \n", CIT_OK);
1639 * set the room aide for this room
1641 void cmd_seta(char *new_ra)
1643 struct ctdluser usbuf;
1648 if (CtdlAccessCheck(ac_room_aide)) return;
1650 if (CtdlGetUser(&usbuf, new_ra) != 0) {
1653 newu = usbuf.usernum;
1656 CtdlGetRoomLock(&CC->room, CC->room.QRname);
1658 if (CC->room.QRroomaide != newu) {
1661 CC->room.QRroomaide = newu;
1662 CtdlPutRoomLock(&CC->room);
1665 * We have to post the change notice _after_ writing changes to
1666 * the room table, otherwise it would deadlock!
1668 if (post_notice == 1) {
1669 if (!IsEmptyStr(usbuf.fullname))
1670 snprintf(buf, sizeof buf,
1671 "%s is now the room aide for \"%s\".\n",
1672 usbuf.fullname, CC->room.QRname);
1674 snprintf(buf, sizeof buf,
1675 "There is now no room aide for \"%s\".\n",
1677 CtdlAideMessage(buf, "Aide Room Modification");
1679 cprintf("%d Ok\n", CIT_OK);
1683 * retrieve info file for this room
1685 void cmd_rinf(char *gargs)
1691 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_info_dir);
1692 info_fp = fopen(filename, "r");
1694 if (info_fp == NULL) {
1695 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
1698 cprintf("%d Info:\n", LISTING_FOLLOWS);
1699 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1700 if (!IsEmptyStr(buf))
1701 buf[strlen(buf) - 1] = 0;
1702 cprintf("%s\n", buf);
1709 * Asynchronously schedule a room for deletion. The room will appear
1710 * deleted to the user(s), but it won't actually get purged from the
1711 * database until THE DREADED AUTO-PURGER makes its next run.
1713 void CtdlScheduleRoomForDeletion(struct ctdlroom *qrbuf)
1715 char old_name[ROOMNAMELEN];
1718 syslog(LOG_NOTICE, "Scheduling room <%s> for deletion\n",
1721 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1723 CtdlGetRoom(qrbuf, qrbuf->QRname);
1725 /* Turn the room into a private mailbox owned by a user who doesn't
1726 * exist. This will immediately make the room invisible to everyone,
1727 * and qualify the room for purging.
1729 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1734 qrbuf->QRflags |= QR_MAILBOX;
1735 time(&qrbuf->QRgen); /* Use a timestamp as the new generation number */
1739 b_deleteroom(old_name);
1745 * Back end processing to delete a room and everything associated with it
1746 * (This one is synchronous and should only get called by THE DREADED
1747 * AUTO-PURGER in serv_expire.c. All user-facing code should call
1748 * the asynchronous schedule_room_for_deletion() instead.)
1750 void CtdlDeleteRoom(struct ctdlroom *qrbuf)
1754 /* TODO: filename magic? does this realy work? */
1756 syslog(LOG_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1758 /* Delete the info file */
1759 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_info_dir);
1762 /* Delete the image file */
1763 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_image_dir);
1766 /* Delete the room's network config file */
1767 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1770 /* Delete the messages in the room
1771 * (Careful: this opens an S_ROOMS critical section!)
1773 CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
1775 /* Flag the room record as not in use */
1776 CtdlGetRoomLock(qrbuf, qrbuf->QRname);
1778 CtdlPutRoomLock(qrbuf);
1780 /* then decrement the reference count for the floor */
1781 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1782 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1783 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1785 /* Delete the room record from the database! */
1786 b_deleteroom(qrbuf->QRname);
1792 * Check access control for deleting a room
1794 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1796 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1800 if (CtdlIsNonEditable(qr)) {
1805 * For mailboxes, check stuff
1807 if (qr->QRflags & QR_MAILBOX) {
1809 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1811 if (atol(qr->QRname) != CC->user.usernum) {
1812 return(0); /* not my room */
1815 /* Can't delete your Mail> room */
1816 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1818 /* Otherwise it's ok */
1823 * For normal rooms, just check for aide or room aide status.
1825 return(is_room_aide());
1829 * aide command: kill the current room
1831 void cmd_kill(char *argbuf)
1833 char deleted_room_name[ROOMNAMELEN];
1837 kill_ok = extract_int(argbuf, 0);
1839 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
1840 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1844 if (CC->room.QRflags & QR_MAILBOX) {
1845 safestrncpy(deleted_room_name, &CC->room.QRname[11], sizeof deleted_room_name);
1848 safestrncpy(deleted_room_name, CC->room.QRname, sizeof deleted_room_name);
1851 /* Do the dirty work */
1852 CtdlScheduleRoomForDeletion(&CC->room);
1854 /* Return to the Lobby */
1855 CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
1857 /* tell the world what we did */
1858 snprintf(msg, sizeof msg, "The room \"%s\" has been deleted by %s.\n",
1860 (CC->logged_in ? CC->curr_user : "an administrator")
1862 CtdlAideMessage(msg, "Room Purger Message");
1863 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1865 cprintf("%d ok to delete.\n", CIT_OK);
1871 * Internal code to create a new room (returns room flags)
1873 * Room types: 0=public, 1=hidden, 2=passworded, 3=invitation-only,
1874 * 4=mailbox, 5=mailbox, but caller supplies namespace
1876 unsigned CtdlCreateRoom(char *new_room_name,
1878 char *new_room_pass,
1885 struct ctdlroom qrbuf;
1889 syslog(LOG_DEBUG, "CtdlCreateRoom(name=%s, type=%d, view=%d)\n",
1890 new_room_name, new_room_type, new_room_view);
1892 if (CtdlGetRoom(&qrbuf, new_room_name) == 0) {
1893 syslog(LOG_DEBUG, "%s already exists.\n", new_room_name);
1897 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1898 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1899 qrbuf.QRflags = QR_INUSE;
1900 if (new_room_type > 0)
1901 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1902 if (new_room_type == 1)
1903 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1904 if (new_room_type == 2)
1905 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1906 if ( (new_room_type == 4) || (new_room_type == 5) ) {
1907 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1908 /* qrbuf.QRflags2 |= QR2_SUBJECTREQ; */
1911 /* If the user is requesting a personal room, set up the room
1912 * name accordingly (prepend the user number)
1914 if (new_room_type == 4) {
1915 CtdlMailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1918 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1921 /* If the room is private, and the system administrator has elected
1922 * to automatically grant room aide privileges, do so now.
1924 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1925 qrbuf.QRroomaide = CC->user.usernum;
1927 /* Blog owners automatically become room aides of their blogs.
1928 * (In the future we will offer a site-wide configuration setting to suppress this behavior.)
1930 else if (new_room_view == VIEW_BLOG) {
1931 qrbuf.QRroomaide = CC->user.usernum;
1933 /* Otherwise, set the room aide to undefined.
1936 qrbuf.QRroomaide = (-1L);
1940 * If the caller is only interested in testing whether this will work,
1941 * return now without creating the room.
1943 if (!really_create) return (qrbuf.QRflags);
1945 qrbuf.QRnumber = get_new_room_number();
1946 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1947 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1948 qrbuf.QRfloor = new_room_floor;
1949 qrbuf.QRdefaultview = new_room_view;
1951 /* save what we just did... */
1952 CtdlPutRoom(&qrbuf);
1954 /* bump the reference count on whatever floor the room is on */
1955 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1956 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1957 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1959 /* Grant the creator access to the room unless the avoid_access
1960 * parameter was specified.
1962 if ( (CC->logged_in) && (avoid_access == 0) ) {
1963 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1964 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1965 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1966 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1969 /* resume our happy day */
1970 return (qrbuf.QRflags);
1977 void cmd_cre8(char *args)
1980 char new_room_name[ROOMNAMELEN];
1982 char new_room_pass[32];
1985 char *notification_message = NULL;
1988 int avoid_access = 0;
1990 cre8_ok = extract_int(args, 0);
1991 extract_token(new_room_name, args, 1, '|', sizeof new_room_name);
1992 new_room_name[ROOMNAMELEN - 1] = 0;
1993 new_room_type = extract_int(args, 2);
1994 extract_token(new_room_pass, args, 3, '|', sizeof new_room_pass);
1995 avoid_access = extract_int(args, 5);
1996 new_room_view = extract_int(args, 6);
1997 new_room_pass[9] = 0;
2000 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 1)) {
2001 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
2005 if (!strcasecmp(new_room_name, MAILROOM)) {
2006 cprintf("%d '%s' already exists.\n",
2007 ERROR + ALREADY_EXISTS, new_room_name);
2011 if (num_parms(args) >= 5) {
2012 fl = CtdlGetCachedFloor(extract_int(args, 4));
2014 cprintf("%d Invalid floor number.\n",
2015 ERROR + INVALID_FLOOR_OPERATION);
2018 else if ((fl->f_flags & F_INUSE) == 0) {
2019 cprintf("%d Invalid floor number.\n",
2020 ERROR + INVALID_FLOOR_OPERATION);
2023 new_room_floor = extract_int(args, 4);
2027 if (CtdlAccessCheck(ac_logged_in)) return;
2029 if (CC->user.axlevel < config.c_createax && !CC->internal_pgm) {
2030 cprintf("%d You need higher access to create rooms.\n",
2031 ERROR + HIGHER_ACCESS_REQUIRED);
2035 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 0)) {
2036 cprintf("%d Ok to create rooms.\n", CIT_OK);
2040 if ((new_room_type < 0) || (new_room_type > 5)) {
2041 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
2045 if (new_room_type == 5) {
2046 if (CC->user.axlevel < AxAideU) {
2047 cprintf("%d Higher access required\n",
2048 ERROR + HIGHER_ACCESS_REQUIRED);
2053 /* Check to make sure the requested room name doesn't already exist */
2054 newflags = CtdlCreateRoom(new_room_name,
2055 new_room_type, new_room_pass, new_room_floor,
2056 0, avoid_access, new_room_view);
2057 if (newflags == 0) {
2058 cprintf("%d '%s' already exists.\n",
2059 ERROR + ALREADY_EXISTS, new_room_name);
2064 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
2068 /* If we reach this point, the room needs to be created. */
2070 newflags = CtdlCreateRoom(new_room_name,
2071 new_room_type, new_room_pass, new_room_floor, 1, 0,
2074 /* post a message in Aide> describing the new room */
2075 notification_message = malloc(1024);
2076 snprintf(notification_message, 1024,
2077 "A new room called \"%s\" has been created by %s%s%s%s%s%s\n",
2079 (CC->logged_in ? CC->curr_user : "an administrator"),
2080 ((newflags & QR_MAILBOX) ? " [personal]" : ""),
2081 ((newflags & QR_PRIVATE) ? " [private]" : ""),
2082 ((newflags & QR_GUESSNAME) ? " [hidden]" : ""),
2083 ((newflags & QR_PASSWORDED) ? " Password: " : ""),
2084 ((newflags & QR_PASSWORDED) ? new_room_pass : "")
2086 CtdlAideMessage(notification_message, "Room Creation Message");
2087 free(notification_message);
2089 cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
2094 void cmd_einf(char *ok)
2095 { /* enter info file for current room */
2097 char infofilename[SIZ];
2102 if (CtdlAccessCheck(ac_room_aide)) return;
2104 if (atoi(ok) == 0) {
2105 cprintf("%d Ok.\n", CIT_OK);
2108 assoc_file_name(infofilename, sizeof infofilename, &CC->room, ctdl_info_dir);
2109 syslog(LOG_DEBUG, "opening\n");
2110 fp = fopen(infofilename, "w");
2111 syslog(LOG_DEBUG, "checking\n");
2113 cprintf("%d Cannot open %s: %s\n",
2114 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
2117 cprintf("%d Send info...\n", SEND_LISTING);
2120 client_getln(buf, sizeof buf);
2121 if (strcmp(buf, "000"))
2122 fprintf(fp, "%s\n", buf);
2123 } while (strcmp(buf, "000"));
2126 /* now update the room index so people will see our new info */
2127 CtdlGetRoomLock(&CC->room, CC->room.QRname); /* lock so no one steps on us */
2128 CC->room.QRinfo = CC->room.QRhighest + 1L;
2129 CtdlPutRoomLock(&CC->room);
2134 * cmd_lflr() - List all known floors
2136 void cmd_lflr(char *gargs)
2141 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
2143 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
2145 for (a = 0; a < MAXFLOORS; ++a) {
2146 CtdlGetFloor(&flbuf, a);
2147 if (flbuf.f_flags & F_INUSE) {
2148 cprintf("%d|%s|%d\n",
2160 * create a new floor
2162 void cmd_cflr(char *argbuf)
2164 char new_floor_name[256];
2167 int free_slot = (-1);
2170 extract_token(new_floor_name, argbuf, 0, '|', sizeof new_floor_name);
2171 cflr_ok = extract_int(argbuf, 1);
2173 if (CtdlAccessCheck(ac_aide)) return;
2175 if (IsEmptyStr(new_floor_name)) {
2176 cprintf("%d Blank floor name not allowed.\n",
2177 ERROR + ILLEGAL_VALUE);
2181 for (a = 0; a < MAXFLOORS; ++a) {
2182 CtdlGetFloor(&flbuf, a);
2184 /* note any free slots while we're scanning... */
2185 if (((flbuf.f_flags & F_INUSE) == 0)
2189 /* check to see if it already exists */
2190 if ((!strcasecmp(flbuf.f_name, new_floor_name))
2191 && (flbuf.f_flags & F_INUSE)) {
2192 cprintf("%d Floor '%s' already exists.\n",
2193 ERROR + ALREADY_EXISTS,
2199 if (free_slot < 0) {
2200 cprintf("%d There is no space available for a new floor.\n",
2201 ERROR + INVALID_FLOOR_OPERATION);
2205 cprintf("%d ok to create...\n", CIT_OK);
2208 lgetfloor(&flbuf, free_slot);
2209 flbuf.f_flags = F_INUSE;
2210 flbuf.f_ref_count = 0;
2211 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
2212 lputfloor(&flbuf, free_slot);
2213 cprintf("%d %d\n", CIT_OK, free_slot);
2221 void cmd_kflr(char *argbuf)
2224 int floor_to_delete;
2228 floor_to_delete = extract_int(argbuf, 0);
2229 kflr_ok = extract_int(argbuf, 1);
2231 if (CtdlAccessCheck(ac_aide)) return;
2233 lgetfloor(&flbuf, floor_to_delete);
2236 if ((flbuf.f_flags & F_INUSE) == 0) {
2237 cprintf("%d Floor %d not in use.\n",
2238 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
2241 if (flbuf.f_ref_count != 0) {
2242 cprintf("%d Cannot delete; floor contains %d rooms.\n",
2243 ERROR + INVALID_FLOOR_OPERATION,
2248 cprintf("%d Ok\n", CIT_OK);
2250 cprintf("%d Ok to delete...\n", CIT_OK);
2257 if ((delete_ok == 1) && (kflr_ok == 1))
2259 lputfloor(&flbuf, floor_to_delete);
2265 void cmd_eflr(char *argbuf)
2271 np = num_parms(argbuf);
2273 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
2277 if (CtdlAccessCheck(ac_aide)) return;
2279 floor_num = extract_int(argbuf, 0);
2280 lgetfloor(&flbuf, floor_num);
2281 if ((flbuf.f_flags & F_INUSE) == 0) {
2282 lputfloor(&flbuf, floor_num);
2283 cprintf("%d Floor %d is not in use.\n",
2284 ERROR + INVALID_FLOOR_OPERATION, floor_num);
2288 extract_token(flbuf.f_name, argbuf, 1, '|', sizeof flbuf.f_name);
2289 lputfloor(&flbuf, floor_num);
2291 cprintf("%d Ok\n", CIT_OK);
2295 /*****************************************************************************/
2296 /* MODULE INITIALIZATION STUFF */
2297 /*****************************************************************************/
2299 CTDL_MODULE_INIT(room_ops)
2302 CtdlRegisterProtoHook(cmd_lrms, "LRMS", "List rooms");
2303 CtdlRegisterProtoHook(cmd_lkra, "LKRA", "List all known rooms");
2304 CtdlRegisterProtoHook(cmd_lkrn, "LKRN", "List known rooms with new messages");
2305 CtdlRegisterProtoHook(cmd_lkro, "LKRO", "List known rooms without new messages");
2306 CtdlRegisterProtoHook(cmd_lzrm, "LZRM", "List zapped rooms");
2307 CtdlRegisterProtoHook(cmd_lprm, "LPRM", "List public rooms");
2308 CtdlRegisterProtoHook(cmd_goto, "GOTO", "Goto a named room");
2309 CtdlRegisterProtoHook(cmd_whok, "WHOK", "List users who know this room");
2310 CtdlRegisterProtoHook(cmd_rdir, "RDIR", "List files in room directory");
2311 CtdlRegisterProtoHook(cmd_getr, "GETR", "Get room parameters");
2312 CtdlRegisterProtoHook(cmd_setr, "SETR", "Set room parameters");
2313 CtdlRegisterProtoHook(cmd_geta, "GETA", "Get the room aide name");
2314 CtdlRegisterProtoHook(cmd_seta, "SETA", "Set the room aide for this room");
2315 CtdlRegisterProtoHook(cmd_rinf, "RINF", "Fetch room info file");
2316 CtdlRegisterProtoHook(cmd_kill, "KILL", "Kill (delete) the current room");
2317 CtdlRegisterProtoHook(cmd_cre8, "CRE8", "Create a new room");
2318 CtdlRegisterProtoHook(cmd_einf, "EINF", "Enter info file for the current room");
2319 CtdlRegisterProtoHook(cmd_lflr, "LFLR", "List all known floors");
2320 CtdlRegisterProtoHook(cmd_cflr, "CFLR", "Create a new floor");
2321 CtdlRegisterProtoHook(cmd_kflr, "KFLR", "Kill a floor");
2322 CtdlRegisterProtoHook(cmd_eflr, "EFLR", "Edit a floor");
2324 /* return our Subversion id for the Log */