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)
927 int new_messages = 0;
928 int old_messages = 0;
929 int total_messages = 0;
933 int newmailcount = 0;
935 char truncated_roomname[ROOMNAMELEN];
936 struct cdbdata *cdbfr;
937 long *msglist = NULL;
939 unsigned int original_v_flags;
942 char setstr[128], lostr[64], histr[64];
946 /* If the supplied room name is NULL, the caller wants us to know that
947 * it has already copied the room record into CC->room, so
948 * we can skip the extra database fetch.
951 safestrncpy(CC->room.QRname, where, sizeof CC->room.QRname);
952 CtdlGetRoom(&CC->room, where);
955 /* Take care of all the formalities. */
957 begin_critical_section(S_USERS);
958 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
959 original_v_flags = vbuf.v_flags;
961 /* Know the room ... but not if it's the page log room, or if the
962 * caller specified that we're only entering this room transiently.
964 if ((strcasecmp(CC->room.QRname, config.c_logpages))
965 && (transiently == 0) ) {
966 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
967 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
970 /* Only rewrite the database record if we changed something */
971 if (vbuf.v_flags != original_v_flags) {
972 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
974 end_critical_section(S_USERS);
976 /* Check for new mail */
977 newmailcount = NewMailCount();
979 /* set info to 1 if the user needs to read the room's info file */
980 if (CC->room.QRinfo > vbuf.v_lastseen) {
984 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
986 msglist = (long *) cdbfr->ptr;
987 cdbfr->ptr = NULL; /* CtdlUserGoto() now owns this memory */
988 num_msgs = cdbfr->len / sizeof(long);
993 for (a=0; a<num_msgs; ++a) {
994 if (msglist[a] > 0L) ++total_messages;
996 new_messages = num_msgs;
997 num_sets = num_tokens(vbuf.v_seen, ',');
998 for (s=0; s<num_sets; ++s) {
999 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
1001 extract_token(lostr, setstr, 0, ':', sizeof lostr);
1002 if (num_tokens(setstr, ':') >= 2) {
1003 extract_token(histr, setstr, 1, ':', sizeof histr);
1004 if (!strcmp(histr, "*")) {
1005 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
1009 strcpy(histr, lostr);
1014 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
1015 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
1021 new_messages = total_messages - old_messages;
1023 if (msglist != NULL) free(msglist);
1025 if (CC->room.QRflags & QR_MAILBOX)
1030 if ((CC->room.QRroomaide == CC->user.usernum)
1031 || (CC->user.axlevel >= AxAideU))
1036 safestrncpy(truncated_roomname, CC->room.QRname, sizeof truncated_roomname);
1037 if ( (CC->room.QRflags & QR_MAILBOX)
1038 && (atol(CC->room.QRname) == CC->user.usernum) ) {
1039 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
1042 if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
1046 if (retmsgs != NULL) *retmsgs = total_messages;
1047 if (retnew != NULL) *retnew = new_messages;
1048 syslog(LOG_DEBUG, "<%s> %d new of %d total messages\n",
1050 new_messages, total_messages
1053 CC->curr_view = (int)vbuf.v_view;
1055 if (display_result) {
1056 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|%d|\n",
1057 CIT_OK, CtdlCheckExpress(),
1060 (int)total_messages,
1062 (int)CC->room.QRflags,
1063 (long)CC->room.QRhighest,
1064 (long)vbuf.v_lastseen,
1068 (int)CC->room.QRfloor,
1070 (int)CC->room.QRdefaultview,
1072 (int)CC->room.QRflags2
1079 * Handle some of the macro named rooms
1081 void convert_room_name_macros(char *towhere, size_t maxlen) {
1082 if (!strcasecmp(towhere, "_BASEROOM_")) {
1083 safestrncpy(towhere, config.c_baseroom, maxlen);
1085 else if (!strcasecmp(towhere, "_MAIL_")) {
1086 safestrncpy(towhere, MAILROOM, maxlen);
1088 else if (!strcasecmp(towhere, "_TRASH_")) {
1089 safestrncpy(towhere, USERTRASHROOM, maxlen);
1091 else if (!strcasecmp(towhere, "_DRAFTS_")) {
1092 safestrncpy(towhere, USERDRAFTROOM, maxlen);
1094 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
1095 safestrncpy(towhere, config.c_twitroom, maxlen);
1097 else if (!strcasecmp(towhere, "_CALENDAR_")) {
1098 safestrncpy(towhere, USERCALENDARROOM, maxlen);
1100 else if (!strcasecmp(towhere, "_TASKS_")) {
1101 safestrncpy(towhere, USERTASKSROOM, maxlen);
1103 else if (!strcasecmp(towhere, "_CONTACTS_")) {
1104 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
1106 else if (!strcasecmp(towhere, "_NOTES_")) {
1107 safestrncpy(towhere, USERNOTESROOM, maxlen);
1113 * cmd_goto() - goto a new room
1115 void cmd_goto(char *gargs)
1117 struct ctdlroom QRscratch;
1121 char augmented_roomname[ROOMNAMELEN];
1122 char towhere[ROOMNAMELEN];
1124 int transiently = 0;
1126 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
1128 extract_token(towhere, gargs, 0, '|', sizeof towhere);
1129 extract_token(password, gargs, 1, '|', sizeof password);
1130 transiently = extract_int(gargs, 2);
1132 CtdlGetUser(&CC->user, CC->curr_user);
1135 * Handle some of the macro named rooms
1137 convert_room_name_macros(towhere, sizeof towhere);
1139 /* First try a regular match */
1140 c = CtdlGetRoom(&QRscratch, towhere);
1142 /* Then try a mailbox name match */
1144 CtdlMailboxName(augmented_roomname, sizeof augmented_roomname,
1145 &CC->user, towhere);
1146 c = CtdlGetRoom(&QRscratch, augmented_roomname);
1148 safestrncpy(towhere, augmented_roomname, sizeof towhere);
1151 /* And if the room was found... */
1154 /* Let internal programs go directly to any room. */
1155 if (CC->internal_pgm) {
1156 memcpy(&CC->room, &QRscratch,
1157 sizeof(struct ctdlroom));
1158 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1162 /* See if there is an existing user/room relationship */
1163 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
1165 /* normal clients have to pass through security */
1166 if (ra & UA_GOTOALLOWED) {
1171 if ((QRscratch.QRflags & QR_MAILBOX) &&
1172 ((ra & UA_GOTOALLOWED))) {
1173 memcpy(&CC->room, &QRscratch,
1174 sizeof(struct ctdlroom));
1175 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1177 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
1178 ((ra & UA_KNOWN) == 0) &&
1179 (strcasecmp(QRscratch.QRpasswd, password)) &&
1180 (CC->user.axlevel < AxAideU)
1182 cprintf("%d wrong or missing passwd\n",
1183 ERROR + PASSWORD_REQUIRED);
1185 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
1186 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
1187 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
1188 ((ra & UA_KNOWN) == 0) &&
1189 (CC->user.axlevel < AxAideU)
1191 syslog(LOG_DEBUG, "Failed to acquire private room\n");
1193 memcpy(&CC->room, &QRscratch,
1194 sizeof(struct ctdlroom));
1195 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1201 cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
1205 void cmd_whok(char *cmdbuf)
1207 struct ctdluser temp;
1208 struct cdbdata *cdbus;
1211 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
1212 cdb_rewind(CDB_USERS);
1213 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1214 memset(&temp, 0, sizeof temp);
1215 memcpy(&temp, cdbus->ptr, sizeof temp);
1218 CtdlRoomAccess(&CC->room, &temp, &ra, NULL);
1219 if ((!IsEmptyStr(temp.fullname)) &&
1220 (CC->room.QRflags & QR_INUSE) &&
1223 cprintf("%s\n", temp.fullname);
1230 * RDIR command for room directory
1232 void cmd_rdir(char *cmdbuf)
1237 struct stat statbuf;
1238 DIR *filedir = NULL;
1239 struct dirent *filedir_entry;
1245 if (CtdlAccessCheck(ac_logged_in)) return;
1247 CtdlGetRoom(&CC->room, CC->room.QRname);
1248 CtdlGetUser(&CC->user, CC->curr_user);
1250 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
1251 cprintf("%d not here.\n", ERROR + NOT_HERE);
1254 if (((CC->room.QRflags & QR_VISDIR) == 0)
1255 && (CC->user.axlevel < AxAideU)
1256 && (CC->user.usernum != CC->room.QRroomaide)) {
1257 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1261 snprintf(buf, sizeof buf, "%s/%s", ctdl_file_dir, CC->room.QRdirname);
1262 filedir = opendir (buf);
1264 if (filedir == NULL) {
1265 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1268 cprintf("%d %s|%s/%s\n", LISTING_FOLLOWS, config.c_fqdn, ctdl_file_dir, CC->room.QRdirname);
1270 snprintf(buf, sizeof buf, "%s/%s/filedir", ctdl_file_dir, CC->room.QRdirname);
1271 fd = fopen(buf, "r");
1273 fd = fopen("/dev/null", "r");
1274 while ((filedir_entry = readdir(filedir)))
1276 if (strcasecmp(filedir_entry->d_name, "filedir") && filedir_entry->d_name[0] != '.')
1278 #ifdef _DIRENT_HAVE_D_NAMELEN
1279 d_namelen = filedir_entry->d_namelen;
1281 d_namelen = strlen(filedir_entry->d_name);
1283 snprintf(buf, sizeof buf, "%s/%s/%s", ctdl_file_dir, CC->room.QRdirname, filedir_entry->d_name);
1284 stat(buf, &statbuf); /* stat the file */
1285 if (!(statbuf.st_mode & S_IFREG))
1287 snprintf(buf2, sizeof buf2,
1288 "\"%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",
1289 buf, CC->room.QRname
1291 CtdlAideMessage(buf2, "Unusable data found in room directory");
1292 continue; /* not a useable file type so don't show it */
1294 safestrncpy(comment, "", sizeof comment);
1295 fseek(fd, 0L, 0); /* rewind descriptions file */
1296 /* Get the description from the descriptions file */
1297 while ((fgets(buf, sizeof buf, fd) != NULL) && (IsEmptyStr(comment)))
1299 buf[strlen(buf) - 1] = 0;
1300 if ((!strncasecmp(buf, filedir_entry->d_name, d_namelen)) && (buf[d_namelen] == ' '))
1301 safestrncpy(comment, &buf[d_namelen + 1], sizeof comment);
1303 len = extract_token (mimebuf, comment, 0,' ', 64);
1304 if ((len <0) || strchr(mimebuf, '/') == NULL)
1306 snprintf (mimebuf, 64, "application/octetstream");
1309 cprintf("%s|%ld|%s|%s\n",
1310 filedir_entry->d_name,
1311 (long)statbuf.st_size,
1323 * get room parameters (aide or room aide command)
1325 void cmd_getr(char *cmdbuf)
1327 if (CtdlAccessCheck(ac_room_aide)) return;
1329 CtdlGetRoom(&CC->room, CC->room.QRname);
1330 cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1334 ((CC->room.QRflags & QR_MAILBOX) ?
1335 &CC->room.QRname[11] : CC->room.QRname),
1337 ((CC->room.QRflags & QR_PASSWORDED) ?
1338 CC->room.QRpasswd : ""),
1340 ((CC->room.QRflags & QR_DIRECTORY) ?
1341 CC->room.QRdirname : ""),
1344 (int) CC->room.QRfloor,
1345 (int) CC->room.QRorder,
1347 CC->room.QRdefaultview,
1354 * Back end function to rename a room.
1355 * You can also specify which floor to move the room to, or specify -1 to
1356 * keep the room on the same floor it was on.
1358 * If you are renaming a mailbox room, you must supply the namespace prefix
1359 * in *at least* the old name!
1361 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1363 struct ctdlroom qrbuf;
1364 struct ctdlroom qrtmp;
1369 char actual_old_name[ROOMNAMELEN];
1371 syslog(LOG_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1372 old_name, new_name, new_floor);
1374 if (new_floor >= 0) {
1375 fl = CtdlGetCachedFloor(new_floor);
1376 if ((fl->f_flags & F_INUSE) == 0) {
1377 return(crr_invalid_floor);
1381 begin_critical_section(S_ROOMS);
1383 if ( (CtdlGetRoom(&qrtmp, new_name) == 0)
1384 && (strcasecmp(new_name, old_name)) ) {
1385 ret = crr_already_exists;
1388 else if (CtdlGetRoom(&qrbuf, old_name) != 0) {
1389 ret = crr_room_not_found;
1392 else if ( (CC->user.axlevel < AxAideU) && (!CC->internal_pgm)
1393 && (CC->user.usernum != qrbuf.QRroomaide)
1394 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) ) {
1395 ret = crr_access_denied;
1398 else if (CtdlIsNonEditable(&qrbuf)) {
1399 ret = crr_noneditable;
1404 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1405 if (qrbuf.QRflags & QR_MAILBOX) {
1406 owner = atol(qrbuf.QRname);
1408 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1409 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1410 "%010ld.%s", owner, new_name);
1413 safestrncpy(qrbuf.QRname, new_name,
1414 sizeof(qrbuf.QRname));
1417 /* Reject change of floor for baseroom/aideroom */
1418 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1419 !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1423 /* Take care of floor stuff */
1424 old_floor = qrbuf.QRfloor;
1425 if (new_floor < 0) {
1426 new_floor = old_floor;
1428 qrbuf.QRfloor = new_floor;
1429 CtdlPutRoom(&qrbuf);
1431 begin_critical_section(S_CONFIG);
1433 /* If baseroom/aideroom name changes, update config */
1434 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1435 safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1438 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1439 safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1443 end_critical_section(S_CONFIG);
1445 /* If the room name changed, then there are now two room
1446 * records, so we have to delete the old one.
1448 if (strcasecmp(new_name, old_name)) {
1449 b_deleteroom(actual_old_name);
1455 end_critical_section(S_ROOMS);
1457 /* Adjust the floor reference counts if necessary */
1458 if (new_floor != old_floor) {
1459 lgetfloor(&flbuf, old_floor);
1460 --flbuf.f_ref_count;
1461 lputfloor(&flbuf, old_floor);
1462 syslog(LOG_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1463 lgetfloor(&flbuf, new_floor);
1464 ++flbuf.f_ref_count;
1465 lputfloor(&flbuf, new_floor);
1466 syslog(LOG_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1469 /* ...and everybody say "YATTA!" */
1475 * set room parameters (aide or room aide command)
1477 void cmd_setr(char *args)
1483 char new_name[ROOMNAMELEN];
1485 if (CtdlAccessCheck(ac_logged_in)) return;
1487 if (num_parms(args) >= 6) {
1488 new_floor = extract_int(args, 5);
1490 new_floor = (-1); /* don't change the floor */
1493 /* When is a new name more than just a new name? When the old name
1494 * has a namespace prefix.
1496 if (CC->room.QRflags & QR_MAILBOX) {
1497 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
1499 safestrncpy(new_name, "", sizeof new_name);
1501 extract_token(&new_name[strlen(new_name)], args, 0, '|', (sizeof new_name - strlen(new_name)));
1503 r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
1505 if (r == crr_room_not_found) {
1506 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
1507 } else if (r == crr_already_exists) {
1508 cprintf("%d '%s' already exists.\n",
1509 ERROR + ALREADY_EXISTS, new_name);
1510 } else if (r == crr_noneditable) {
1511 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
1512 } else if (r == crr_invalid_floor) {
1513 cprintf("%d Target floor does not exist.\n",
1514 ERROR + INVALID_FLOOR_OPERATION);
1515 } else if (r == crr_access_denied) {
1516 cprintf("%d You do not have permission to edit '%s'\n",
1517 ERROR + HIGHER_ACCESS_REQUIRED,
1519 } else if (r != crr_ok) {
1520 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1521 ERROR + INTERNAL_ERROR, r);
1528 CtdlGetRoom(&CC->room, new_name);
1530 /* Now we have to do a bunch of other stuff */
1532 if (num_parms(args) >= 7) {
1533 new_order = extract_int(args, 6);
1536 if (new_order > 127)
1540 CtdlGetRoomLock(&CC->room, CC->room.QRname);
1542 /* Directory room */
1543 extract_token(buf, args, 2, '|', sizeof buf);
1545 safestrncpy(CC->room.QRdirname, buf,
1546 sizeof CC->room.QRdirname);
1549 if (num_parms(args) >= 8) {
1550 CC->room.QRdefaultview = extract_int(args, 7);
1553 /* Second set of flags */
1554 if (num_parms(args) >= 9) {
1555 CC->room.QRflags2 = extract_int(args, 8);
1559 CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
1560 /* Clean up a client boo-boo: if the client set the room to
1561 * guess-name or passworded, ensure that the private flag is
1564 if ((CC->room.QRflags & QR_GUESSNAME)
1565 || (CC->room.QRflags & QR_PASSWORDED))
1566 CC->room.QRflags |= QR_PRIVATE;
1568 /* Some changes can't apply to BASEROOM */
1569 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1571 CC->room.QRorder = 0;
1572 CC->room.QRpasswd[0] = '\0';
1573 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1574 QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1575 CC->room.QRflags |= QR_PERMANENT;
1577 /* March order (doesn't apply to AIDEROOM) */
1578 if (num_parms(args) >= 7)
1579 CC->room.QRorder = (char) new_order;
1581 extract_token(buf, args, 1, '|', sizeof buf);
1583 safestrncpy(CC->room.QRpasswd, buf,
1584 sizeof CC->room.QRpasswd);
1585 /* Kick everyone out if the client requested it
1586 * (by changing the room's generation number)
1588 if (extract_int(args, 4)) {
1589 time(&CC->room.QRgen);
1592 /* Some changes can't apply to AIDEROOM */
1593 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1595 CC->room.QRorder = 0;
1596 CC->room.QRflags &= ~QR_MAILBOX;
1597 CC->room.QRflags |= QR_PERMANENT;
1600 /* Write the room record back to disk */
1601 CtdlPutRoomLock(&CC->room);
1603 /* Create a room directory if necessary */
1604 if (CC->room.QRflags & QR_DIRECTORY) {
1605 snprintf(buf, sizeof buf,"%s/%s",
1607 CC->room.QRdirname);
1610 snprintf(buf, sizeof buf, "The room \"%s\" has been edited by %s.\n",
1611 CC->room.QRname, CC->curr_user);
1612 CtdlAideMessage(buf, "Room modification Message");
1613 cprintf("%d Ok\n", CIT_OK);
1619 * get the name of the room aide for this room
1621 void cmd_geta(char *cmdbuf)
1623 struct ctdluser usbuf;
1625 if (CtdlAccessCheck(ac_logged_in)) return;
1627 if (CtdlGetUserByNumber(&usbuf, CC->room.QRroomaide) == 0) {
1628 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1630 cprintf("%d \n", CIT_OK);
1636 * set the room aide for this room
1638 void cmd_seta(char *new_ra)
1640 struct ctdluser usbuf;
1645 if (CtdlAccessCheck(ac_room_aide)) return;
1647 if (CtdlGetUser(&usbuf, new_ra) != 0) {
1650 newu = usbuf.usernum;
1653 CtdlGetRoomLock(&CC->room, CC->room.QRname);
1655 if (CC->room.QRroomaide != newu) {
1658 CC->room.QRroomaide = newu;
1659 CtdlPutRoomLock(&CC->room);
1662 * We have to post the change notice _after_ writing changes to
1663 * the room table, otherwise it would deadlock!
1665 if (post_notice == 1) {
1666 if (!IsEmptyStr(usbuf.fullname))
1667 snprintf(buf, sizeof buf,
1668 "%s is now the room aide for \"%s\".\n",
1669 usbuf.fullname, CC->room.QRname);
1671 snprintf(buf, sizeof buf,
1672 "There is now no room aide for \"%s\".\n",
1674 CtdlAideMessage(buf, "Aide Room Modification");
1676 cprintf("%d Ok\n", CIT_OK);
1680 * retrieve info file for this room
1682 void cmd_rinf(char *gargs)
1688 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_info_dir);
1689 info_fp = fopen(filename, "r");
1691 if (info_fp == NULL) {
1692 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
1695 cprintf("%d Info:\n", LISTING_FOLLOWS);
1696 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1697 if (!IsEmptyStr(buf))
1698 buf[strlen(buf) - 1] = 0;
1699 cprintf("%s\n", buf);
1706 * Asynchronously schedule a room for deletion. The room will appear
1707 * deleted to the user(s), but it won't actually get purged from the
1708 * database until THE DREADED AUTO-PURGER makes its next run.
1710 void CtdlScheduleRoomForDeletion(struct ctdlroom *qrbuf)
1712 char old_name[ROOMNAMELEN];
1715 syslog(LOG_NOTICE, "Scheduling room <%s> for deletion\n",
1718 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1720 CtdlGetRoom(qrbuf, qrbuf->QRname);
1722 /* Turn the room into a private mailbox owned by a user who doesn't
1723 * exist. This will immediately make the room invisible to everyone,
1724 * and qualify the room for purging.
1726 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1731 qrbuf->QRflags |= QR_MAILBOX;
1732 time(&qrbuf->QRgen); /* Use a timestamp as the new generation number */
1736 b_deleteroom(old_name);
1742 * Back end processing to delete a room and everything associated with it
1743 * (This one is synchronous and should only get called by THE DREADED
1744 * AUTO-PURGER in serv_expire.c. All user-facing code should call
1745 * the asynchronous schedule_room_for_deletion() instead.)
1747 void CtdlDeleteRoom(struct ctdlroom *qrbuf)
1751 /* TODO: filename magic? does this realy work? */
1753 syslog(LOG_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1755 /* Delete the info file */
1756 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_info_dir);
1759 /* Delete the image file */
1760 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_image_dir);
1763 /* Delete the room's network config file */
1764 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1767 /* Delete the messages in the room
1768 * (Careful: this opens an S_ROOMS critical section!)
1770 CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
1772 /* Flag the room record as not in use */
1773 CtdlGetRoomLock(qrbuf, qrbuf->QRname);
1775 CtdlPutRoomLock(qrbuf);
1777 /* then decrement the reference count for the floor */
1778 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1779 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1780 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1782 /* Delete the room record from the database! */
1783 b_deleteroom(qrbuf->QRname);
1789 * Check access control for deleting a room
1791 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1793 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1797 if (CtdlIsNonEditable(qr)) {
1802 * For mailboxes, check stuff
1804 if (qr->QRflags & QR_MAILBOX) {
1806 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1808 if (atol(qr->QRname) != CC->user.usernum) {
1809 return(0); /* not my room */
1812 /* Can't delete your Mail> room */
1813 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1815 /* Otherwise it's ok */
1820 * For normal rooms, just check for aide or room aide status.
1822 return(is_room_aide());
1826 * aide command: kill the current room
1828 void cmd_kill(char *argbuf)
1830 char deleted_room_name[ROOMNAMELEN];
1834 kill_ok = extract_int(argbuf, 0);
1836 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
1837 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1841 if (CC->room.QRflags & QR_MAILBOX) {
1842 safestrncpy(deleted_room_name, &CC->room.QRname[11], sizeof deleted_room_name);
1845 safestrncpy(deleted_room_name, CC->room.QRname, sizeof deleted_room_name);
1848 /* Do the dirty work */
1849 CtdlScheduleRoomForDeletion(&CC->room);
1851 /* Return to the Lobby */
1852 CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
1854 /* tell the world what we did */
1855 snprintf(msg, sizeof msg, "The room \"%s\" has been deleted by %s.\n",
1856 deleted_room_name, CC->curr_user);
1857 CtdlAideMessage(msg, "Room Purger Message");
1858 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1860 cprintf("%d ok to delete.\n", CIT_OK);
1866 * Internal code to create a new room (returns room flags)
1868 * Room types: 0=public, 1=hidden, 2=passworded, 3=invitation-only,
1869 * 4=mailbox, 5=mailbox, but caller supplies namespace
1871 unsigned CtdlCreateRoom(char *new_room_name,
1873 char *new_room_pass,
1880 struct ctdlroom qrbuf;
1884 syslog(LOG_DEBUG, "CtdlCreateRoom(name=%s, type=%d, view=%d)\n",
1885 new_room_name, new_room_type, new_room_view);
1887 if (CtdlGetRoom(&qrbuf, new_room_name) == 0) {
1888 syslog(LOG_DEBUG, "%s already exists.\n", new_room_name);
1892 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1893 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1894 qrbuf.QRflags = QR_INUSE;
1895 if (new_room_type > 0)
1896 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1897 if (new_room_type == 1)
1898 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1899 if (new_room_type == 2)
1900 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1901 if ( (new_room_type == 4) || (new_room_type == 5) ) {
1902 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1903 /* qrbuf.QRflags2 |= QR2_SUBJECTREQ; */
1906 /* If the user is requesting a personal room, set up the room
1907 * name accordingly (prepend the user number)
1909 if (new_room_type == 4) {
1910 CtdlMailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1913 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1916 /* If the room is private, and the system administrator has elected
1917 * to automatically grant room aide privileges, do so now.
1919 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1920 qrbuf.QRroomaide = CC->user.usernum;
1922 /* Blog owners automatically become room aides of their blogs.
1923 * (In the future we will offer a site-wide configuration setting to suppress this behavior.)
1925 else if (new_room_view == VIEW_BLOG) {
1926 qrbuf.QRroomaide = CC->user.usernum;
1928 /* Otherwise, set the room aide to undefined.
1931 qrbuf.QRroomaide = (-1L);
1935 * If the caller is only interested in testing whether this will work,
1936 * return now without creating the room.
1938 if (!really_create) return (qrbuf.QRflags);
1940 qrbuf.QRnumber = get_new_room_number();
1941 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1942 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1943 qrbuf.QRfloor = new_room_floor;
1944 qrbuf.QRdefaultview = new_room_view;
1946 /* save what we just did... */
1947 CtdlPutRoom(&qrbuf);
1949 /* bump the reference count on whatever floor the room is on */
1950 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1951 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1952 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1954 /* Grant the creator access to the room unless the avoid_access
1955 * parameter was specified.
1957 if ( (CC->logged_in) && (avoid_access == 0) ) {
1958 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1959 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1960 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1961 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1964 /* resume our happy day */
1965 return (qrbuf.QRflags);
1972 void cmd_cre8(char *args)
1975 char new_room_name[ROOMNAMELEN];
1977 char new_room_pass[32];
1980 char *notification_message = NULL;
1983 int avoid_access = 0;
1985 cre8_ok = extract_int(args, 0);
1986 extract_token(new_room_name, args, 1, '|', sizeof new_room_name);
1987 new_room_name[ROOMNAMELEN - 1] = 0;
1988 new_room_type = extract_int(args, 2);
1989 extract_token(new_room_pass, args, 3, '|', sizeof new_room_pass);
1990 avoid_access = extract_int(args, 5);
1991 new_room_view = extract_int(args, 6);
1992 new_room_pass[9] = 0;
1995 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 1)) {
1996 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
2000 if (!strcasecmp(new_room_name, MAILROOM)) {
2001 cprintf("%d '%s' already exists.\n",
2002 ERROR + ALREADY_EXISTS, new_room_name);
2006 if (num_parms(args) >= 5) {
2007 fl = CtdlGetCachedFloor(extract_int(args, 4));
2009 cprintf("%d Invalid floor number.\n",
2010 ERROR + INVALID_FLOOR_OPERATION);
2013 else if ((fl->f_flags & F_INUSE) == 0) {
2014 cprintf("%d Invalid floor number.\n",
2015 ERROR + INVALID_FLOOR_OPERATION);
2018 new_room_floor = extract_int(args, 4);
2022 if (CtdlAccessCheck(ac_logged_in)) return;
2024 if (CC->user.axlevel < config.c_createax && !CC->internal_pgm) {
2025 cprintf("%d You need higher access to create rooms.\n",
2026 ERROR + HIGHER_ACCESS_REQUIRED);
2030 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 0)) {
2031 cprintf("%d Ok to create rooms.\n", CIT_OK);
2035 if ((new_room_type < 0) || (new_room_type > 5)) {
2036 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
2040 if (new_room_type == 5) {
2041 if (CC->user.axlevel < AxAideU) {
2042 cprintf("%d Higher access required\n",
2043 ERROR + HIGHER_ACCESS_REQUIRED);
2048 /* Check to make sure the requested room name doesn't already exist */
2049 newflags = CtdlCreateRoom(new_room_name,
2050 new_room_type, new_room_pass, new_room_floor,
2051 0, avoid_access, new_room_view);
2052 if (newflags == 0) {
2053 cprintf("%d '%s' already exists.\n",
2054 ERROR + ALREADY_EXISTS, new_room_name);
2059 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
2063 /* If we reach this point, the room needs to be created. */
2065 newflags = CtdlCreateRoom(new_room_name,
2066 new_room_type, new_room_pass, new_room_floor, 1, 0,
2069 /* post a message in Aide> describing the new room */
2070 notification_message = malloc(1024);
2071 snprintf(notification_message, 1024,
2072 "A new room called \"%s\" has been created by %s%s%s%s%s%s\n",
2075 ((newflags & QR_MAILBOX) ? " [personal]" : ""),
2076 ((newflags & QR_PRIVATE) ? " [private]" : ""),
2077 ((newflags & QR_GUESSNAME) ? " [hidden]" : ""),
2078 ((newflags & QR_PASSWORDED) ? " Password: " : ""),
2079 ((newflags & QR_PASSWORDED) ? new_room_pass : "")
2081 CtdlAideMessage(notification_message, "Room Creation Message");
2082 free(notification_message);
2084 cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
2089 void cmd_einf(char *ok)
2090 { /* enter info file for current room */
2092 char infofilename[SIZ];
2097 if (CtdlAccessCheck(ac_room_aide)) return;
2099 if (atoi(ok) == 0) {
2100 cprintf("%d Ok.\n", CIT_OK);
2103 assoc_file_name(infofilename, sizeof infofilename, &CC->room, ctdl_info_dir);
2104 syslog(LOG_DEBUG, "opening\n");
2105 fp = fopen(infofilename, "w");
2106 syslog(LOG_DEBUG, "checking\n");
2108 cprintf("%d Cannot open %s: %s\n",
2109 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
2112 cprintf("%d Send info...\n", SEND_LISTING);
2115 client_getln(buf, sizeof buf);
2116 if (strcmp(buf, "000"))
2117 fprintf(fp, "%s\n", buf);
2118 } while (strcmp(buf, "000"));
2121 /* now update the room index so people will see our new info */
2122 CtdlGetRoomLock(&CC->room, CC->room.QRname); /* lock so no one steps on us */
2123 CC->room.QRinfo = CC->room.QRhighest + 1L;
2124 CtdlPutRoomLock(&CC->room);
2129 * cmd_lflr() - List all known floors
2131 void cmd_lflr(char *gargs)
2136 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
2138 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
2140 for (a = 0; a < MAXFLOORS; ++a) {
2141 CtdlGetFloor(&flbuf, a);
2142 if (flbuf.f_flags & F_INUSE) {
2143 cprintf("%d|%s|%d\n",
2155 * create a new floor
2157 void cmd_cflr(char *argbuf)
2159 char new_floor_name[256];
2162 int free_slot = (-1);
2165 extract_token(new_floor_name, argbuf, 0, '|', sizeof new_floor_name);
2166 cflr_ok = extract_int(argbuf, 1);
2168 if (CtdlAccessCheck(ac_aide)) return;
2170 if (IsEmptyStr(new_floor_name)) {
2171 cprintf("%d Blank floor name not allowed.\n",
2172 ERROR + ILLEGAL_VALUE);
2176 for (a = 0; a < MAXFLOORS; ++a) {
2177 CtdlGetFloor(&flbuf, a);
2179 /* note any free slots while we're scanning... */
2180 if (((flbuf.f_flags & F_INUSE) == 0)
2184 /* check to see if it already exists */
2185 if ((!strcasecmp(flbuf.f_name, new_floor_name))
2186 && (flbuf.f_flags & F_INUSE)) {
2187 cprintf("%d Floor '%s' already exists.\n",
2188 ERROR + ALREADY_EXISTS,
2194 if (free_slot < 0) {
2195 cprintf("%d There is no space available for a new floor.\n",
2196 ERROR + INVALID_FLOOR_OPERATION);
2200 cprintf("%d ok to create...\n", CIT_OK);
2203 lgetfloor(&flbuf, free_slot);
2204 flbuf.f_flags = F_INUSE;
2205 flbuf.f_ref_count = 0;
2206 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
2207 lputfloor(&flbuf, free_slot);
2208 cprintf("%d %d\n", CIT_OK, free_slot);
2216 void cmd_kflr(char *argbuf)
2219 int floor_to_delete;
2223 floor_to_delete = extract_int(argbuf, 0);
2224 kflr_ok = extract_int(argbuf, 1);
2226 if (CtdlAccessCheck(ac_aide)) return;
2228 lgetfloor(&flbuf, floor_to_delete);
2231 if ((flbuf.f_flags & F_INUSE) == 0) {
2232 cprintf("%d Floor %d not in use.\n",
2233 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
2236 if (flbuf.f_ref_count != 0) {
2237 cprintf("%d Cannot delete; floor contains %d rooms.\n",
2238 ERROR + INVALID_FLOOR_OPERATION,
2243 cprintf("%d Ok\n", CIT_OK);
2245 cprintf("%d Ok to delete...\n", CIT_OK);
2252 if ((delete_ok == 1) && (kflr_ok == 1))
2254 lputfloor(&flbuf, floor_to_delete);
2260 void cmd_eflr(char *argbuf)
2266 np = num_parms(argbuf);
2268 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
2272 if (CtdlAccessCheck(ac_aide)) return;
2274 floor_num = extract_int(argbuf, 0);
2275 lgetfloor(&flbuf, floor_num);
2276 if ((flbuf.f_flags & F_INUSE) == 0) {
2277 lputfloor(&flbuf, floor_num);
2278 cprintf("%d Floor %d is not in use.\n",
2279 ERROR + INVALID_FLOOR_OPERATION, floor_num);
2283 extract_token(flbuf.f_name, argbuf, 1, '|', sizeof flbuf.f_name);
2284 lputfloor(&flbuf, floor_num);
2286 cprintf("%d Ok\n", CIT_OK);
2290 /*****************************************************************************/
2291 /* MODULE INITIALIZATION STUFF */
2292 /*****************************************************************************/
2294 CTDL_MODULE_INIT(room_ops)
2297 CtdlRegisterProtoHook(cmd_lrms, "LRMS", "List rooms");
2298 CtdlRegisterProtoHook(cmd_lkra, "LKRA", "List all known rooms");
2299 CtdlRegisterProtoHook(cmd_lkrn, "LKRN", "List known rooms with new messages");
2300 CtdlRegisterProtoHook(cmd_lkro, "LKRO", "List known rooms without new messages");
2301 CtdlRegisterProtoHook(cmd_lzrm, "LZRM", "List zapped rooms");
2302 CtdlRegisterProtoHook(cmd_lprm, "LPRM", "List public rooms");
2303 CtdlRegisterProtoHook(cmd_goto, "GOTO", "Goto a named room");
2304 CtdlRegisterProtoHook(cmd_whok, "WHOK", "List users who know this room");
2305 CtdlRegisterProtoHook(cmd_rdir, "RDIR", "List files in room directory");
2306 CtdlRegisterProtoHook(cmd_getr, "GETR", "Get room parameters");
2307 CtdlRegisterProtoHook(cmd_setr, "SETR", "Set room parameters");
2308 CtdlRegisterProtoHook(cmd_geta, "GETA", "Get the room aide name");
2309 CtdlRegisterProtoHook(cmd_seta, "SETA", "Set the room aide for this room");
2310 CtdlRegisterProtoHook(cmd_rinf, "RINF", "Fetch room info file");
2311 CtdlRegisterProtoHook(cmd_kill, "KILL", "Kill (delete) the current room");
2312 CtdlRegisterProtoHook(cmd_cre8, "CRE8", "Create a new room");
2313 CtdlRegisterProtoHook(cmd_einf, "EINF", "Enter info file for the current room");
2314 CtdlRegisterProtoHook(cmd_lflr, "LFLR", "List all known floors");
2315 CtdlRegisterProtoHook(cmd_cflr, "CFLR", "Create a new floor");
2316 CtdlRegisterProtoHook(cmd_kflr, "KFLR", "Kill a floor");
2317 CtdlRegisterProtoHook(cmd_eflr, "EFLR", "Edit a floor");
2319 /* return our Subversion id for the Log */