2 * Server functions which perform operations on room objects.
4 * Copyright (c) 1987-2012 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, version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
22 #include <dirent.h> /* for cmd_rdir to read contents of the directory */
24 #if TIME_WITH_SYS_TIME
25 # include <sys/time.h>
29 # include <sys/time.h>
38 #include <libcitadel.h>
43 #include "sysdep_decls.h"
46 #include "citserver.h"
48 #include "citadel_dirs.h"
51 #include "ctdl_module.h"
54 struct floor *floorcache[MAXFLOORS];
57 * Retrieve access control information for any user/room pair
59 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf,
60 int *result, int *view)
67 if (userbuf == &CC->user) {
71 if ((is_me) && (config.c_guest_logins) && (!CC->logged_in)) {
75 /* for internal programs, always do everything */
76 if (((CC->internal_pgm)) && (roombuf->QRflags & QR_INUSE)) {
77 retval = (UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED);
82 /* If guest mode is enabled, always grant access to the Lobby */
83 if ((is_guest) && (!strcasecmp(roombuf->QRname, BASEROOM))) {
84 retval = (UA_KNOWN | UA_GOTOALLOWED);
89 /* Locate any applicable user/room relationships */
91 memset(&vbuf, 0, sizeof vbuf);
94 CtdlGetRelationship(&vbuf, userbuf, roombuf);
97 /* Force the properties of the Aide room */
98 if (!strcasecmp(roombuf->QRname, config.c_aideroom)) {
99 if (userbuf->axlevel >= AxAideU) {
100 retval = UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED;
107 /* If this is a public room, it's accessible... */
108 if ( ((roombuf->QRflags & QR_PRIVATE) == 0)
109 && ((roombuf->QRflags & QR_MAILBOX) == 0)
111 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
114 /* If this is a preferred users only room, check access level */
115 if (roombuf->QRflags & QR_PREFONLY) {
116 if (userbuf->axlevel < AxPrefU) {
117 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
121 /* For private rooms, check the generation number matchups */
122 if ( (roombuf->QRflags & QR_PRIVATE)
123 && ((roombuf->QRflags & QR_MAILBOX) == 0)
126 /* An explicit match means the user belongs in this room */
127 if (vbuf.v_flags & V_ACCESS) {
128 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
130 /* Otherwise, check if this is a guess-name or passworded
131 * room. If it is, a goto may at least be attempted
133 else if ( (roombuf->QRflags & QR_PRIVATE)
134 || (roombuf->QRflags & QR_PASSWORDED)
136 retval = retval & ~UA_KNOWN;
137 retval = retval | UA_GOTOALLOWED;
141 /* For mailbox rooms, also check the namespace */
142 /* Also, mailbox owners can delete their messages */
143 if ( (roombuf->QRflags & QR_MAILBOX) && (atol(roombuf->QRname) != 0)) {
144 if (userbuf->usernum == atol(roombuf->QRname)) {
145 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED;
147 /* An explicit match means the user belongs in this room */
148 if (vbuf.v_flags & V_ACCESS) {
149 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED;
153 /* For non-mailbox rooms... */
156 /* User is allowed to post in the room unless:
157 * - User is not validated
158 * - User has no net privileges and it is a shared network room
159 * - It is a read-only room
160 * - It is a blog room (in which case we only allow replies to existing messages)
162 int post_allowed = 1;
163 int reply_allowed = 1;
164 if (userbuf->axlevel < AxProbU) {
168 if ((userbuf->axlevel < AxNetU) && (roombuf->QRflags & QR_NETWORK)) {
172 if (roombuf->QRflags & QR_READONLY) {
176 if (roombuf->QRdefaultview == VIEW_BLOG) {
180 retval = retval | UA_POSTALLOWED | UA_REPLYALLOWED;
183 retval = retval | UA_REPLYALLOWED;
186 /* If "collaborative deletion" is active for this room, any user who can post
187 * is also allowed to delete
189 if (roombuf->QRflags2 & QR2_COLLABDEL) {
190 if (retval & UA_POSTALLOWED) {
191 retval = retval | UA_DELETEALLOWED;
197 /* Check to see if the user has forgotten this room */
198 if (vbuf.v_flags & V_FORGET) {
199 retval = retval & ~UA_KNOWN;
200 if ( ( ((roombuf->QRflags & QR_PRIVATE) == 0)
201 && ((roombuf->QRflags & QR_MAILBOX) == 0)
202 ) || ( (roombuf->QRflags & QR_MAILBOX)
203 && (atol(roombuf->QRname) == CC->user.usernum))
205 retval = retval | UA_ZAPPED;
209 /* If user is explicitly locked out of this room, deny everything */
210 if (vbuf.v_flags & V_LOCKOUT) {
211 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED & ~UA_POSTALLOWED & ~UA_REPLYALLOWED;
214 /* Aides get access to all private rooms */
215 if ( (userbuf->axlevel >= AxAideU)
216 && ((roombuf->QRflags & QR_MAILBOX) == 0)
218 if (vbuf.v_flags & V_FORGET) {
219 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
222 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
226 /* Aides can gain access to mailboxes as well, but they don't show
229 if ( (userbuf->axlevel >= AxAideU)
230 && (roombuf->QRflags & QR_MAILBOX)
232 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
235 /* Aides and Room Aides have admin privileges */
236 if ( (userbuf->axlevel >= AxAideU)
237 || (userbuf->usernum == roombuf->QRroomaide)
239 retval = retval | UA_ADMINALLOWED | UA_DELETEALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
242 NEWMSG: /* By the way, we also check for the presence of new messages */
243 if (is_msg_in_sequence_set(vbuf.v_seen, roombuf->QRhighest) == 0) {
244 retval = retval | UA_HASNEWMSGS;
247 /* System rooms never show up in the list. */
248 if (roombuf->QRflags2 & QR2_SYSTEM) {
249 retval = retval & ~UA_KNOWN;
253 /* Now give the caller the information it wants. */
254 if (result != NULL) *result = retval;
255 if (view != NULL) *view = vbuf.v_view;
260 * Self-checking stuff for a room record read into memory
262 void room_sanity_check(struct ctdlroom *qrbuf)
264 /* Mailbox rooms are always on the lowest floor */
265 if (qrbuf->QRflags & QR_MAILBOX) {
268 /* Listing order of 0 is illegal except for base rooms */
269 if (qrbuf->QRorder == 0)
270 if (!(qrbuf->QRflags & QR_MAILBOX) &&
271 strncasecmp(qrbuf->QRname, config.c_baseroom, ROOMNAMELEN)
273 strncasecmp(qrbuf->QRname, config.c_aideroom, ROOMNAMELEN))
279 * CtdlGetRoom() - retrieve room data from disk
281 int CtdlGetRoom(struct ctdlroom *qrbuf, char *room_name)
283 struct cdbdata *cdbqr;
284 char lowercase_name[ROOMNAMELEN];
285 char personal_lowercase_name[ROOMNAMELEN];
286 char *dptr, *sptr, *eptr;
288 dptr = lowercase_name;
290 eptr = (dptr + (sizeof lowercase_name - 1));
291 while (!IsEmptyStr(sptr) && (dptr < eptr)){
292 *dptr = tolower(*sptr);
297 memset(qrbuf, 0, sizeof(struct ctdlroom));
299 /* First, try the public namespace */
300 cdbqr = cdb_fetch(CDB_ROOMS,
301 lowercase_name, strlen(lowercase_name));
303 /* If that didn't work, try the user's personal namespace */
305 snprintf(personal_lowercase_name,
306 sizeof personal_lowercase_name, "%010ld.%s",
307 CC->user.usernum, lowercase_name);
308 cdbqr = cdb_fetch(CDB_ROOMS,
309 personal_lowercase_name,
310 strlen(personal_lowercase_name));
313 memcpy(qrbuf, cdbqr->ptr,
314 ((cdbqr->len > sizeof(struct ctdlroom)) ?
315 sizeof(struct ctdlroom) : cdbqr->len));
318 room_sanity_check(qrbuf);
327 * CtdlGetRoomLock() - same as getroom() but locks the record (if supported)
329 int CtdlGetRoomLock(struct ctdlroom *qrbuf, char *room_name)
332 retval = CtdlGetRoom(qrbuf, room_name);
333 if (retval == 0) begin_critical_section(S_ROOMS);
339 * b_putroom() - back end to putroom() and b_deleteroom()
340 * (if the supplied buffer is NULL, delete the room record)
342 void b_putroom(struct ctdlroom *qrbuf, char *room_name)
344 char lowercase_name[ROOMNAMELEN];
349 bptr = lowercase_name;
350 while (!IsEmptyStr(aptr))
352 *bptr = tolower(*aptr);
358 len = bptr - lowercase_name;
360 cdb_delete(CDB_ROOMS, lowercase_name, len);
362 time(&qrbuf->QRmtime);
363 cdb_store(CDB_ROOMS, lowercase_name, len, qrbuf, sizeof(struct ctdlroom));
369 * CtdlPutRoom() - store room data to disk
371 void CtdlPutRoom(struct ctdlroom *qrbuf) {
372 b_putroom(qrbuf, qrbuf->QRname);
377 * b_deleteroom() - delete a room record from disk
379 void b_deleteroom(char *room_name) {
380 b_putroom(NULL, room_name);
385 * CtdlPutRoomLock() - same as CtdlPutRoom() but unlocks the record (if supported)
387 void CtdlPutRoomLock(struct ctdlroom *qrbuf)
391 end_critical_section(S_ROOMS);
397 * CtdlGetFloorByName() - retrieve the number of the named floor
398 * return < 0 if not found else return floor number
400 int CtdlGetFloorByName(const char *floor_name)
403 struct floor *flbuf = NULL;
405 for (a = 0; a < MAXFLOORS; ++a) {
406 flbuf = CtdlGetCachedFloor(a);
408 /* check to see if it already exists */
409 if ((!strcasecmp(flbuf->f_name, floor_name))
410 && (flbuf->f_flags & F_INUSE)) {
419 * CtdlGetFloorByNameLock() - retrieve floor number for given floor and lock the floor list.
421 int CtdlGetFloorByNameLock(const char *floor_name)
423 begin_critical_section(S_FLOORTAB);
424 return CtdlGetFloorByName(floor_name);
430 * CtdlGetAvailableFloor() - Return number of first unused floor
431 * return < 0 if none available
433 int CtdlGetAvailableFloor(void)
436 struct floor *flbuf = NULL;
438 for (a = 0; a < MAXFLOORS; a++) {
439 flbuf = CtdlGetCachedFloor(a);
441 /* check to see if it already exists */
442 if ((flbuf->f_flags & F_INUSE) == 0) {
451 * CtdlGetFloor() - retrieve floor data from disk
453 void CtdlGetFloor(struct floor *flbuf, int floor_num)
455 struct cdbdata *cdbfl;
457 memset(flbuf, 0, sizeof(struct floor));
458 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
460 memcpy(flbuf, cdbfl->ptr,
461 ((cdbfl->len > sizeof(struct floor)) ?
462 sizeof(struct floor) : cdbfl->len));
465 if (floor_num == 0) {
466 safestrncpy(flbuf->f_name, "Main Floor",
467 sizeof flbuf->f_name);
468 flbuf->f_flags = F_INUSE;
469 flbuf->f_ref_count = 3;
477 * lgetfloor() - same as CtdlGetFloor() but locks the record (if supported)
479 void lgetfloor(struct floor *flbuf, int floor_num)
482 begin_critical_section(S_FLOORTAB);
483 CtdlGetFloor(flbuf, floor_num);
488 * CtdlGetCachedFloor() - Get floor record from *cache* (loads from disk if needed)
490 * This is strictly a performance hack.
492 struct floor *CtdlGetCachedFloor(int floor_num) {
493 static int initialized = 0;
496 struct floor *fl = NULL;
498 begin_critical_section(S_FLOORCACHE);
499 if (initialized == 0) {
500 for (i=0; i<MAXFLOORS; ++i) {
501 floorcache[floor_num] = NULL;
505 if (floorcache[floor_num] == NULL) {
508 end_critical_section(S_FLOORCACHE);
511 fl = malloc(sizeof(struct floor));
512 CtdlGetFloor(fl, floor_num);
513 begin_critical_section(S_FLOORCACHE);
514 if (floorcache[floor_num] != NULL) {
515 free(floorcache[floor_num]);
517 floorcache[floor_num] = fl;
518 end_critical_section(S_FLOORCACHE);
521 return(floorcache[floor_num]);
526 * CtdlPutFloor() - store floor data on disk
528 void CtdlPutFloor(struct floor *flbuf, int floor_num)
530 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
531 begin_critical_section(S_FLOORCACHE);
532 if (floorcache[floor_num] != NULL) {
533 free(floorcache[floor_num]);
534 floorcache[floor_num] = malloc(sizeof(struct floor));
535 memcpy(floorcache[floor_num], flbuf, sizeof(struct floor));
537 end_critical_section(S_FLOORCACHE);
539 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
540 flbuf, sizeof(struct floor));
545 * CtdlPutFloorLock() - same as CtdlPutFloor() but unlocks the record (if supported)
547 void CtdlPutFloorLock(struct floor *flbuf, int floor_num)
550 CtdlPutFloor(flbuf, floor_num);
551 end_critical_section(S_FLOORTAB);
557 * lputfloor() - same as CtdlPutFloor() but unlocks the record (if supported)
559 void lputfloor(struct floor *flbuf, int floor_num)
561 CtdlPutFloorLock(flbuf, floor_num);
566 * Traverse the room file...
568 void CtdlForEachRoom(ForEachRoomCallBack CB, void *in_data)
570 struct ctdlroom qrbuf;
571 struct cdbdata *cdbqr;
573 cdb_rewind(CDB_ROOMS);
575 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
576 memset(&qrbuf, 0, sizeof(struct ctdlroom));
577 memcpy(&qrbuf, cdbqr->ptr,
578 ((cdbqr->len > sizeof(struct ctdlroom)) ?
579 sizeof(struct ctdlroom) : cdbqr->len)
582 room_sanity_check(&qrbuf);
583 if (qrbuf.QRflags & QR_INUSE) {
591 * delete_msglist() - delete room message pointers
593 void delete_msglist(struct ctdlroom *whichroom)
595 struct cdbdata *cdbml;
597 /* Make sure the msglist we're deleting actually exists, otherwise
598 * libdb will complain when we try to delete an invalid record
600 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
604 /* Go ahead and delete it */
605 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
611 * Message pointer compare function for sort_msglist()
613 int sort_msglist_cmp(const void *m1, const void *m2) {
614 if ((*(const long *)m1) > (*(const long *)m2)) return(1);
615 if ((*(const long *)m1) < (*(const long *)m2)) return(-1);
621 * sort message pointers
622 * (returns new msg count)
624 int sort_msglist(long listptrs[], int oldcount)
634 qsort(listptrs, numitems, sizeof(long), sort_msglist_cmp);
636 /* and yank any nulls */
637 while ((numitems > 0) && (listptrs[0] == 0L)) {
638 memmove(&listptrs[0], &listptrs[1], (sizeof(long) * (numitems - 1)));
647 * Determine whether a given room is non-editable.
649 int CtdlIsNonEditable(struct ctdlroom *qrbuf)
652 /* Mail> rooms are non-editable */
653 if ( (qrbuf->QRflags & QR_MAILBOX)
654 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
657 /* Everything else is editable */
663 * Back-back-end for all room listing commands
665 void list_roomname(struct ctdlroom *qrbuf, int ra, int current_view, int default_view)
667 char truncated_roomname[ROOMNAMELEN];
669 /* For my own mailbox rooms, chop off the owner prefix */
670 if ( (qrbuf->QRflags & QR_MAILBOX)
671 && (atol(qrbuf->QRname) == CC->user.usernum) ) {
672 safestrncpy(truncated_roomname, qrbuf->QRname, sizeof truncated_roomname);
673 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
674 cprintf("%s", truncated_roomname);
676 /* For all other rooms, just display the name in its entirety */
678 cprintf("%s", qrbuf->QRname);
681 /* ...and now the other parameters */
682 cprintf("|%u|%d|%d|%d|%d|%d|%d|%ld|\n",
684 (int) qrbuf->QRfloor,
685 (int) qrbuf->QRorder,
686 (int) qrbuf->QRflags2,
696 * cmd_lrms() - List all accessible rooms, known or forgotten
698 void cmd_lrms_backend(struct ctdlroom *qrbuf, void *data)
700 int FloorBeingSearched = (-1);
704 FloorBeingSearched = *(int *)data;
705 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
707 if ((( ra & (UA_KNOWN | UA_ZAPPED)))
708 && ((qrbuf->QRfloor == (FloorBeingSearched))
709 || ((FloorBeingSearched) < 0)))
710 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
713 void cmd_lrms(char *argbuf)
715 int FloorBeingSearched = (-1);
716 if (!IsEmptyStr(argbuf))
717 FloorBeingSearched = extract_int(argbuf, 0);
719 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
721 CtdlGetUser(&CC->user, CC->curr_user);
722 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
724 CtdlForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
731 * cmd_lkra() - List all known rooms
733 void cmd_lkra_backend(struct ctdlroom *qrbuf, void *data)
735 int FloorBeingSearched = (-1);
739 FloorBeingSearched = *(int *)data;
740 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
742 if ((( ra & (UA_KNOWN)))
743 && ((qrbuf->QRfloor == (FloorBeingSearched))
744 || ((FloorBeingSearched) < 0)))
745 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
748 void cmd_lkra(char *argbuf)
750 int FloorBeingSearched = (-1);
751 if (!IsEmptyStr(argbuf))
752 FloorBeingSearched = extract_int(argbuf, 0);
754 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
756 CtdlGetUser(&CC->user, CC->curr_user);
757 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
759 CtdlForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
765 void cmd_lprm_backend(struct ctdlroom *qrbuf, void *data)
767 int FloorBeingSearched = (-1);
771 FloorBeingSearched = *(int *)data;
772 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
774 if ( ((qrbuf->QRflags & QR_PRIVATE) == 0)
775 && ((qrbuf->QRflags & QR_MAILBOX) == 0)
776 && ((qrbuf->QRfloor == (FloorBeingSearched))
777 || ((FloorBeingSearched) < 0)))
778 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
781 void cmd_lprm(char *argbuf)
783 int FloorBeingSearched = (-1);
784 if (!IsEmptyStr(argbuf))
785 FloorBeingSearched = extract_int(argbuf, 0);
787 cprintf("%d Public rooms:\n", LISTING_FOLLOWS);
789 CtdlForEachRoom(cmd_lprm_backend, &FloorBeingSearched);
796 * cmd_lkrn() - List all known rooms with new messages
798 void cmd_lkrn_backend(struct ctdlroom *qrbuf, void *data)
800 int FloorBeingSearched = (-1);
804 FloorBeingSearched = *(int *)data;
805 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
808 && (ra & UA_HASNEWMSGS)
809 && ((qrbuf->QRfloor == (FloorBeingSearched))
810 || ((FloorBeingSearched) < 0)))
811 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
814 void cmd_lkrn(char *argbuf)
816 int FloorBeingSearched = (-1);
817 if (!IsEmptyStr(argbuf))
818 FloorBeingSearched = extract_int(argbuf, 0);
820 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
822 CtdlGetUser(&CC->user, CC->curr_user);
823 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
825 CtdlForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
832 * cmd_lkro() - List all known rooms
834 void cmd_lkro_backend(struct ctdlroom *qrbuf, void *data)
836 int FloorBeingSearched = (-1);
840 FloorBeingSearched = *(int *)data;
841 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
844 && ((ra & UA_HASNEWMSGS) == 0)
845 && ((qrbuf->QRfloor == (FloorBeingSearched))
846 || ((FloorBeingSearched) < 0)))
847 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
850 void cmd_lkro(char *argbuf)
852 int FloorBeingSearched = (-1);
853 if (!IsEmptyStr(argbuf))
854 FloorBeingSearched = extract_int(argbuf, 0);
856 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
858 CtdlGetUser(&CC->user, CC->curr_user);
859 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
861 CtdlForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
868 * cmd_lzrm() - List all forgotten rooms
870 void cmd_lzrm_backend(struct ctdlroom *qrbuf, void *data)
872 int FloorBeingSearched = (-1);
876 FloorBeingSearched = *(int *)data;
877 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
879 if ((ra & UA_GOTOALLOWED)
881 && ((qrbuf->QRfloor == (FloorBeingSearched))
882 || ((FloorBeingSearched) < 0)))
883 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
886 void cmd_lzrm(char *argbuf)
888 int FloorBeingSearched = (-1);
889 if (!IsEmptyStr(argbuf))
890 FloorBeingSearched = extract_int(argbuf, 0);
892 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
894 CtdlGetUser(&CC->user, CC->curr_user);
895 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
897 CtdlForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
903 * Make the specified room the current room for this session. No validation
904 * or access control is done here -- the caller should make sure that the
905 * specified room exists and is ok to access.
907 void CtdlUserGoto(char *where, int display_result, int transiently,
908 int *retmsgs, int *retnew)
910 struct CitContext *CCC = CC;
912 int new_messages = 0;
913 int old_messages = 0;
914 int total_messages = 0;
918 int newmailcount = 0;
920 char truncated_roomname[ROOMNAMELEN];
921 struct cdbdata *cdbfr;
922 long *msglist = NULL;
924 unsigned int original_v_flags;
927 char setstr[128], lostr[64], histr[64];
931 /* If the supplied room name is NULL, the caller wants us to know that
932 * it has already copied the room record into CC->room, so
933 * we can skip the extra database fetch.
936 safestrncpy(CCC->room.QRname, where, sizeof CCC->room.QRname);
937 CtdlGetRoom(&CCC->room, where);
940 /* Take care of all the formalities. */
942 begin_critical_section(S_USERS);
943 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
944 original_v_flags = vbuf.v_flags;
946 /* Know the room ... but not if it's the page log room, or if the
947 * caller specified that we're only entering this room transiently.
949 if ((strcasecmp(CCC->room.QRname, config.c_logpages))
950 && (transiently == 0) ) {
951 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
952 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
955 /* Only rewrite the database record if we changed something */
956 if (vbuf.v_flags != original_v_flags) {
957 CtdlSetRelationship(&vbuf, &CCC->user, &CCC->room);
959 end_critical_section(S_USERS);
961 /* Check for new mail */
962 newmailcount = NewMailCount();
964 /* set info to 1 if the user needs to read the room's info file */
965 if (CCC->room.QRinfo > vbuf.v_lastseen) {
969 cdbfr = cdb_fetch(CDB_MSGLISTS, &CCC->room.QRnumber, sizeof(long));
971 msglist = (long *) cdbfr->ptr;
972 cdbfr->ptr = NULL; /* CtdlUserGoto() now owns this memory */
973 num_msgs = cdbfr->len / sizeof(long);
978 for (a=0; a<num_msgs; ++a) {
979 if (msglist[a] > 0L) ++total_messages;
982 num_sets = num_tokens(vbuf.v_seen, ',');
983 for (s=0; s<num_sets; ++s) {
984 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
986 extract_token(lostr, setstr, 0, ':', sizeof lostr);
987 if (num_tokens(setstr, ':') >= 2) {
988 extract_token(histr, setstr, 1, ':', sizeof histr);
989 if (!strcmp(histr, "*")) {
990 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
994 strcpy(histr, lostr);
999 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
1000 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
1006 new_messages = total_messages - old_messages;
1008 if (msglist != NULL) free(msglist);
1010 if (CCC->room.QRflags & QR_MAILBOX)
1015 if ((CCC->room.QRroomaide == CCC->user.usernum)
1016 || (CCC->user.axlevel >= AxAideU))
1021 safestrncpy(truncated_roomname, CCC->room.QRname, sizeof truncated_roomname);
1022 if ( (CCC->room.QRflags & QR_MAILBOX)
1023 && (atol(CCC->room.QRname) == CCC->user.usernum) ) {
1024 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
1027 if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
1031 if (retmsgs != NULL) *retmsgs = total_messages;
1032 if (retnew != NULL) *retnew = new_messages;
1033 MSG_syslog(LOG_INFO, "<%s> %d new of %d total messages\n",
1035 new_messages, total_messages
1038 CCC->curr_view = (int)vbuf.v_view;
1040 if (display_result) {
1041 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|%d|%ld|\n",
1042 CIT_OK, CtdlCheckExpress(),
1045 (int)total_messages,
1047 (int)CCC->room.QRflags,
1048 (long)CCC->room.QRhighest,
1049 (long)vbuf.v_lastseen,
1053 (int)CCC->room.QRfloor,
1055 (int)CCC->room.QRdefaultview,
1057 (int)CCC->room.QRflags2,
1058 (long)CCC->room.QRmtime
1065 * Handle some of the macro named rooms
1067 void convert_room_name_macros(char *towhere, size_t maxlen) {
1068 if (!strcasecmp(towhere, "_BASEROOM_")) {
1069 safestrncpy(towhere, config.c_baseroom, maxlen);
1071 else if (!strcasecmp(towhere, "_MAIL_")) {
1072 safestrncpy(towhere, MAILROOM, maxlen);
1074 else if (!strcasecmp(towhere, "_TRASH_")) {
1075 safestrncpy(towhere, USERTRASHROOM, maxlen);
1077 else if (!strcasecmp(towhere, "_DRAFTS_")) {
1078 safestrncpy(towhere, USERDRAFTROOM, maxlen);
1080 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
1081 safestrncpy(towhere, config.c_twitroom, maxlen);
1083 else if (!strcasecmp(towhere, "_CALENDAR_")) {
1084 safestrncpy(towhere, USERCALENDARROOM, maxlen);
1086 else if (!strcasecmp(towhere, "_TASKS_")) {
1087 safestrncpy(towhere, USERTASKSROOM, maxlen);
1089 else if (!strcasecmp(towhere, "_CONTACTS_")) {
1090 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
1092 else if (!strcasecmp(towhere, "_NOTES_")) {
1093 safestrncpy(towhere, USERNOTESROOM, maxlen);
1099 * cmd_goto() - goto a new room
1101 void cmd_goto(char *gargs)
1103 struct ctdlroom QRscratch;
1107 char augmented_roomname[ROOMNAMELEN];
1108 char towhere[ROOMNAMELEN];
1110 int transiently = 0;
1112 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
1114 extract_token(towhere, gargs, 0, '|', sizeof towhere);
1115 extract_token(password, gargs, 1, '|', sizeof password);
1116 transiently = extract_int(gargs, 2);
1118 CtdlGetUser(&CC->user, CC->curr_user);
1121 * Handle some of the macro named rooms
1123 convert_room_name_macros(towhere, sizeof towhere);
1125 /* First try a regular match */
1126 c = CtdlGetRoom(&QRscratch, towhere);
1128 /* Then try a mailbox name match */
1130 CtdlMailboxName(augmented_roomname, sizeof augmented_roomname,
1131 &CC->user, towhere);
1132 c = CtdlGetRoom(&QRscratch, augmented_roomname);
1134 safestrncpy(towhere, augmented_roomname, sizeof towhere);
1137 /* And if the room was found... */
1140 /* Let internal programs go directly to any room. */
1141 if (CC->internal_pgm) {
1142 memcpy(&CC->room, &QRscratch,
1143 sizeof(struct ctdlroom));
1144 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1148 /* See if there is an existing user/room relationship */
1149 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
1151 /* normal clients have to pass through security */
1152 if (ra & UA_GOTOALLOWED) {
1157 if ((QRscratch.QRflags & QR_MAILBOX) &&
1158 ((ra & UA_GOTOALLOWED))) {
1159 memcpy(&CC->room, &QRscratch,
1160 sizeof(struct ctdlroom));
1161 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1163 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
1164 ((ra & UA_KNOWN) == 0) &&
1165 (strcasecmp(QRscratch.QRpasswd, password)) &&
1166 (CC->user.axlevel < AxAideU)
1168 cprintf("%d wrong or missing passwd\n",
1169 ERROR + PASSWORD_REQUIRED);
1171 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
1172 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
1173 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
1174 ((ra & UA_KNOWN) == 0) &&
1175 (CC->user.axlevel < AxAideU)
1177 syslog(LOG_DEBUG, "Failed to acquire private room\n");
1179 memcpy(&CC->room, &QRscratch,
1180 sizeof(struct ctdlroom));
1181 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1187 cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
1191 void cmd_whok(char *cmdbuf)
1193 struct ctdluser temp;
1194 struct cdbdata *cdbus;
1197 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
1198 cdb_rewind(CDB_USERS);
1199 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1200 memset(&temp, 0, sizeof temp);
1201 memcpy(&temp, cdbus->ptr, sizeof temp);
1204 CtdlRoomAccess(&CC->room, &temp, &ra, NULL);
1205 if ((!IsEmptyStr(temp.fullname)) &&
1206 (CC->room.QRflags & QR_INUSE) &&
1209 cprintf("%s\n", temp.fullname);
1216 * RDIR command for room directory
1218 void cmd_rdir(char *cmdbuf)
1223 struct stat statbuf;
1224 DIR *filedir = NULL;
1225 struct dirent *filedir_entry;
1231 if (CtdlAccessCheck(ac_logged_in)) return;
1233 CtdlGetRoom(&CC->room, CC->room.QRname);
1234 CtdlGetUser(&CC->user, CC->curr_user);
1236 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
1237 cprintf("%d not here.\n", ERROR + NOT_HERE);
1240 if (((CC->room.QRflags & QR_VISDIR) == 0)
1241 && (CC->user.axlevel < AxAideU)
1242 && (CC->user.usernum != CC->room.QRroomaide)) {
1243 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1247 snprintf(buf, sizeof buf, "%s/%s", ctdl_file_dir, CC->room.QRdirname);
1248 filedir = opendir (buf);
1250 if (filedir == NULL) {
1251 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1254 cprintf("%d %s|%s/%s\n", LISTING_FOLLOWS, config.c_fqdn, ctdl_file_dir, CC->room.QRdirname);
1256 snprintf(buf, sizeof buf, "%s/%s/filedir", ctdl_file_dir, CC->room.QRdirname);
1257 fd = fopen(buf, "r");
1259 fd = fopen("/dev/null", "r");
1260 while ((filedir_entry = readdir(filedir)))
1262 if (strcasecmp(filedir_entry->d_name, "filedir") && filedir_entry->d_name[0] != '.')
1264 #ifdef _DIRENT_HAVE_D_NAMELEN
1265 d_namelen = filedir_entry->d_namelen;
1267 d_namelen = strlen(filedir_entry->d_name);
1269 snprintf(buf, sizeof buf, "%s/%s/%s", ctdl_file_dir, CC->room.QRdirname, filedir_entry->d_name);
1270 stat(buf, &statbuf); /* stat the file */
1271 if (!(statbuf.st_mode & S_IFREG))
1273 snprintf(buf2, sizeof buf2,
1274 "\"%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",
1275 buf, CC->room.QRname
1277 CtdlAideMessage(buf2, "Unusable data found in room directory");
1278 continue; /* not a useable file type so don't show it */
1280 safestrncpy(comment, "", sizeof comment);
1281 fseek(fd, 0L, 0); /* rewind descriptions file */
1282 /* Get the description from the descriptions file */
1283 while ((fgets(buf, sizeof buf, fd) != NULL) && (IsEmptyStr(comment)))
1285 buf[strlen(buf) - 1] = 0;
1286 if ((!strncasecmp(buf, filedir_entry->d_name, d_namelen)) && (buf[d_namelen] == ' '))
1287 safestrncpy(comment, &buf[d_namelen + 1], sizeof comment);
1289 len = extract_token (mimebuf, comment, 0,' ', 64);
1290 if ((len <0) || strchr(mimebuf, '/') == NULL)
1292 snprintf (mimebuf, 64, "application/octetstream");
1295 cprintf("%s|%ld|%s|%s\n",
1296 filedir_entry->d_name,
1297 (long)statbuf.st_size,
1309 * get room parameters (admin or room admin command)
1311 void cmd_getr(char *cmdbuf)
1313 if (CtdlAccessCheck(ac_room_aide)) return;
1315 CtdlGetRoom(&CC->room, CC->room.QRname);
1316 cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1320 ((CC->room.QRflags & QR_MAILBOX) ?
1321 &CC->room.QRname[11] : CC->room.QRname),
1323 ((CC->room.QRflags & QR_PASSWORDED) ?
1324 CC->room.QRpasswd : ""),
1326 ((CC->room.QRflags & QR_DIRECTORY) ?
1327 CC->room.QRdirname : ""),
1330 (int) CC->room.QRfloor,
1331 (int) CC->room.QRorder,
1333 CC->room.QRdefaultview,
1340 * Back end function to rename a room.
1341 * You can also specify which floor to move the room to, or specify -1 to
1342 * keep the room on the same floor it was on.
1344 * If you are renaming a mailbox room, you must supply the namespace prefix
1345 * in *at least* the old name!
1347 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1349 struct ctdlroom qrbuf;
1350 struct ctdlroom qrtmp;
1355 char actual_old_name[ROOMNAMELEN];
1357 syslog(LOG_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1358 old_name, new_name, new_floor);
1360 if (new_floor >= 0) {
1361 fl = CtdlGetCachedFloor(new_floor);
1362 if ((fl->f_flags & F_INUSE) == 0) {
1363 return(crr_invalid_floor);
1367 begin_critical_section(S_ROOMS);
1369 if ( (CtdlGetRoom(&qrtmp, new_name) == 0)
1370 && (strcasecmp(new_name, old_name)) ) {
1371 ret = crr_already_exists;
1374 else if (CtdlGetRoom(&qrbuf, old_name) != 0) {
1375 ret = crr_room_not_found;
1378 else if ( (CC->user.axlevel < AxAideU) && (!CC->internal_pgm)
1379 && (CC->user.usernum != qrbuf.QRroomaide)
1380 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) ) {
1381 ret = crr_access_denied;
1384 else if (CtdlIsNonEditable(&qrbuf)) {
1385 ret = crr_noneditable;
1390 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1391 if (qrbuf.QRflags & QR_MAILBOX) {
1392 owner = atol(qrbuf.QRname);
1394 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1395 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1396 "%010ld.%s", owner, new_name);
1399 safestrncpy(qrbuf.QRname, new_name,
1400 sizeof(qrbuf.QRname));
1403 /* Reject change of floor for baseroom/aideroom */
1404 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1405 !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1409 /* Take care of floor stuff */
1410 old_floor = qrbuf.QRfloor;
1411 if (new_floor < 0) {
1412 new_floor = old_floor;
1414 qrbuf.QRfloor = new_floor;
1415 CtdlPutRoom(&qrbuf);
1417 begin_critical_section(S_CONFIG);
1419 /* If baseroom/aideroom name changes, update config */
1420 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1421 safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1424 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1425 safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1429 end_critical_section(S_CONFIG);
1431 /* If the room name changed, then there are now two room
1432 * records, so we have to delete the old one.
1434 if (strcasecmp(new_name, old_name)) {
1435 b_deleteroom(actual_old_name);
1441 end_critical_section(S_ROOMS);
1443 /* Adjust the floor reference counts if necessary */
1444 if (new_floor != old_floor) {
1445 lgetfloor(&flbuf, old_floor);
1446 --flbuf.f_ref_count;
1447 lputfloor(&flbuf, old_floor);
1448 syslog(LOG_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1449 lgetfloor(&flbuf, new_floor);
1450 ++flbuf.f_ref_count;
1451 lputfloor(&flbuf, new_floor);
1452 syslog(LOG_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1455 /* ...and everybody say "YATTA!" */
1461 * set room parameters (admin or room admin command)
1463 void cmd_setr(char *args)
1469 char new_name[ROOMNAMELEN];
1471 if (CtdlAccessCheck(ac_logged_in)) return;
1473 if (num_parms(args) >= 6) {
1474 new_floor = extract_int(args, 5);
1476 new_floor = (-1); /* don't change the floor */
1479 /* When is a new name more than just a new name? When the old name
1480 * has a namespace prefix.
1482 if (CC->room.QRflags & QR_MAILBOX) {
1483 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
1485 safestrncpy(new_name, "", sizeof new_name);
1487 extract_token(&new_name[strlen(new_name)], args, 0, '|', (sizeof new_name - strlen(new_name)));
1489 r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
1491 if (r == crr_room_not_found) {
1492 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
1493 } else if (r == crr_already_exists) {
1494 cprintf("%d '%s' already exists.\n",
1495 ERROR + ALREADY_EXISTS, new_name);
1496 } else if (r == crr_noneditable) {
1497 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
1498 } else if (r == crr_invalid_floor) {
1499 cprintf("%d Target floor does not exist.\n",
1500 ERROR + INVALID_FLOOR_OPERATION);
1501 } else if (r == crr_access_denied) {
1502 cprintf("%d You do not have permission to edit '%s'\n",
1503 ERROR + HIGHER_ACCESS_REQUIRED,
1505 } else if (r != crr_ok) {
1506 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1507 ERROR + INTERNAL_ERROR, r);
1514 CtdlGetRoom(&CC->room, new_name);
1516 /* Now we have to do a bunch of other stuff */
1518 if (num_parms(args) >= 7) {
1519 new_order = extract_int(args, 6);
1522 if (new_order > 127)
1526 CtdlGetRoomLock(&CC->room, CC->room.QRname);
1528 /* Directory room */
1529 extract_token(buf, args, 2, '|', sizeof buf);
1531 safestrncpy(CC->room.QRdirname, buf,
1532 sizeof CC->room.QRdirname);
1535 if (num_parms(args) >= 8) {
1536 CC->room.QRdefaultview = extract_int(args, 7);
1539 /* Second set of flags */
1540 if (num_parms(args) >= 9) {
1541 CC->room.QRflags2 = extract_int(args, 8);
1545 CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
1546 /* Clean up a client boo-boo: if the client set the room to
1547 * guess-name or passworded, ensure that the private flag is
1550 if ((CC->room.QRflags & QR_GUESSNAME)
1551 || (CC->room.QRflags & QR_PASSWORDED))
1552 CC->room.QRflags |= QR_PRIVATE;
1554 /* Some changes can't apply to BASEROOM */
1555 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1557 CC->room.QRorder = 0;
1558 CC->room.QRpasswd[0] = '\0';
1559 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1560 QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1561 CC->room.QRflags |= QR_PERMANENT;
1563 /* March order (doesn't apply to AIDEROOM) */
1564 if (num_parms(args) >= 7)
1565 CC->room.QRorder = (char) new_order;
1567 extract_token(buf, args, 1, '|', sizeof buf);
1569 safestrncpy(CC->room.QRpasswd, buf,
1570 sizeof CC->room.QRpasswd);
1571 /* Kick everyone out if the client requested it
1572 * (by changing the room's generation number)
1574 if (extract_int(args, 4)) {
1575 time(&CC->room.QRgen);
1578 /* Some changes can't apply to AIDEROOM */
1579 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1581 CC->room.QRorder = 0;
1582 CC->room.QRflags &= ~QR_MAILBOX;
1583 CC->room.QRflags |= QR_PERMANENT;
1586 /* Write the room record back to disk */
1587 CtdlPutRoomLock(&CC->room);
1589 /* Create a room directory if necessary */
1590 if (CC->room.QRflags & QR_DIRECTORY) {
1591 snprintf(buf, sizeof buf,"%s/%s",
1593 CC->room.QRdirname);
1596 snprintf(buf, sizeof buf, "The room \"%s\" has been edited by %s.\n",
1598 (CC->logged_in ? CC->curr_user : "an administrator")
1600 CtdlAideMessage(buf, "Room modification Message");
1601 cprintf("%d Ok\n", CIT_OK);
1607 * get the name of the room admin for this room
1609 void cmd_geta(char *cmdbuf)
1611 struct ctdluser usbuf;
1613 if (CtdlAccessCheck(ac_logged_in)) return;
1615 if (CtdlGetUserByNumber(&usbuf, CC->room.QRroomaide) == 0) {
1616 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1618 cprintf("%d \n", CIT_OK);
1624 * set the room admin for this room
1626 void cmd_seta(char *new_ra)
1628 struct ctdluser usbuf;
1633 if (CtdlAccessCheck(ac_room_aide)) return;
1635 if (CtdlGetUser(&usbuf, new_ra) != 0) {
1638 newu = usbuf.usernum;
1641 CtdlGetRoomLock(&CC->room, CC->room.QRname);
1643 if (CC->room.QRroomaide != newu) {
1646 CC->room.QRroomaide = newu;
1647 CtdlPutRoomLock(&CC->room);
1650 * We have to post the change notice _after_ writing changes to
1651 * the room table, otherwise it would deadlock!
1653 if (post_notice == 1) {
1654 if (!IsEmptyStr(usbuf.fullname))
1655 snprintf(buf, sizeof buf,
1656 "%s is now the room admin for \"%s\".\n",
1657 usbuf.fullname, CC->room.QRname);
1659 snprintf(buf, sizeof buf,
1660 "There is now no room admin for \"%s\".\n",
1662 CtdlAideMessage(buf, "Admin Room Modification");
1664 cprintf("%d Ok\n", CIT_OK);
1668 * retrieve info file for this room
1670 void cmd_rinf(char *gargs)
1676 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_info_dir);
1677 info_fp = fopen(filename, "r");
1679 if (info_fp == NULL) {
1680 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
1683 cprintf("%d Info:\n", LISTING_FOLLOWS);
1684 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1685 if (!IsEmptyStr(buf))
1686 buf[strlen(buf) - 1] = 0;
1687 cprintf("%s\n", buf);
1694 * Asynchronously schedule a room for deletion. The room will appear
1695 * deleted to the user(s), but it won't actually get purged from the
1696 * database until THE DREADED AUTO-PURGER makes its next run.
1698 void CtdlScheduleRoomForDeletion(struct ctdlroom *qrbuf)
1700 char old_name[ROOMNAMELEN];
1703 syslog(LOG_NOTICE, "Scheduling room <%s> for deletion\n",
1706 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1708 CtdlGetRoom(qrbuf, qrbuf->QRname);
1710 /* Turn the room into a private mailbox owned by a user who doesn't
1711 * exist. This will immediately make the room invisible to everyone,
1712 * and qualify the room for purging.
1714 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1719 qrbuf->QRflags |= QR_MAILBOX;
1720 time(&qrbuf->QRgen); /* Use a timestamp as the new generation number */
1724 b_deleteroom(old_name);
1730 * Back end processing to delete a room and everything associated with it
1731 * (This one is synchronous and should only get called by THE DREADED
1732 * AUTO-PURGER in serv_expire.c. All user-facing code should call
1733 * the asynchronous schedule_room_for_deletion() instead.)
1735 void CtdlDeleteRoom(struct ctdlroom *qrbuf)
1739 /* TODO: filename magic? does this realy work? */
1741 syslog(LOG_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1743 /* Delete the info file */
1744 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_info_dir);
1747 /* Delete the image file */
1748 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_image_dir);
1751 /* Delete the room's network config file */
1752 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1755 /* Delete the messages in the room
1756 * (Careful: this opens an S_ROOMS critical section!)
1758 CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
1760 /* Flag the room record as not in use */
1761 CtdlGetRoomLock(qrbuf, qrbuf->QRname);
1763 CtdlPutRoomLock(qrbuf);
1765 /* then decrement the reference count for the floor */
1766 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1767 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1768 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1770 /* Delete the room record from the database! */
1771 b_deleteroom(qrbuf->QRname);
1777 * Check access control for deleting a room
1779 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1781 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1785 if (CtdlIsNonEditable(qr)) {
1790 * For mailboxes, check stuff
1792 if (qr->QRflags & QR_MAILBOX) {
1794 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1796 if (atol(qr->QRname) != CC->user.usernum) {
1797 return(0); /* not my room */
1800 /* Can't delete your Mail> room */
1801 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1803 /* Otherwise it's ok */
1808 * For normal rooms, just check for admin or room admin status.
1810 return(is_room_aide());
1814 * admin command: kill the current room
1816 void cmd_kill(char *argbuf)
1818 char deleted_room_name[ROOMNAMELEN];
1822 kill_ok = extract_int(argbuf, 0);
1824 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
1825 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1829 if (CC->room.QRflags & QR_MAILBOX) {
1830 safestrncpy(deleted_room_name, &CC->room.QRname[11], sizeof deleted_room_name);
1833 safestrncpy(deleted_room_name, CC->room.QRname, sizeof deleted_room_name);
1836 /* Do the dirty work */
1837 CtdlScheduleRoomForDeletion(&CC->room);
1839 /* Return to the Lobby */
1840 CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
1842 /* tell the world what we did */
1843 snprintf(msg, sizeof msg, "The room \"%s\" has been deleted by %s.\n",
1845 (CC->logged_in ? CC->curr_user : "an administrator")
1847 CtdlAideMessage(msg, "Room Purger Message");
1848 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1850 cprintf("%d ok to delete.\n", CIT_OK);
1856 * Internal code to create a new room (returns room flags)
1858 * Room types: 0=public, 1=hidden, 2=passworded, 3=invitation-only,
1859 * 4=mailbox, 5=mailbox, but caller supplies namespace
1861 unsigned CtdlCreateRoom(char *new_room_name,
1863 char *new_room_pass,
1870 struct ctdlroom qrbuf;
1874 syslog(LOG_DEBUG, "CtdlCreateRoom(name=%s, type=%d, view=%d)\n",
1875 new_room_name, new_room_type, new_room_view);
1877 if (CtdlGetRoom(&qrbuf, new_room_name) == 0) {
1878 syslog(LOG_DEBUG, "%s already exists.\n", new_room_name);
1882 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1883 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1884 qrbuf.QRflags = QR_INUSE;
1885 if (new_room_type > 0)
1886 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1887 if (new_room_type == 1)
1888 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1889 if (new_room_type == 2)
1890 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1891 if ( (new_room_type == 4) || (new_room_type == 5) ) {
1892 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1893 /* qrbuf.QRflags2 |= QR2_SUBJECTREQ; */
1896 /* If the user is requesting a personal room, set up the room
1897 * name accordingly (prepend the user number)
1899 if (new_room_type == 4) {
1900 CtdlMailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1903 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1906 /* If the room is private, and the system administrator has elected
1907 * to automatically grant room admin privileges, do so now.
1909 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1910 qrbuf.QRroomaide = CC->user.usernum;
1912 /* Blog owners automatically become room admins of their blogs.
1913 * (In the future we will offer a site-wide configuration setting to suppress this behavior.)
1915 else if (new_room_view == VIEW_BLOG) {
1916 qrbuf.QRroomaide = CC->user.usernum;
1918 /* Otherwise, set the room admin to undefined.
1921 qrbuf.QRroomaide = (-1L);
1925 * If the caller is only interested in testing whether this will work,
1926 * return now without creating the room.
1928 if (!really_create) return (qrbuf.QRflags);
1930 qrbuf.QRnumber = get_new_room_number();
1931 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1932 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1933 qrbuf.QRfloor = new_room_floor;
1934 qrbuf.QRdefaultview = new_room_view;
1936 /* save what we just did... */
1937 CtdlPutRoom(&qrbuf);
1939 /* bump the reference count on whatever floor the room is on */
1940 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1941 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1942 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1944 /* Grant the creator access to the room unless the avoid_access
1945 * parameter was specified.
1947 if ( (CC->logged_in) && (avoid_access == 0) ) {
1948 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1949 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1950 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1951 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1954 /* resume our happy day */
1955 return (qrbuf.QRflags);
1962 void cmd_cre8(char *args)
1965 char new_room_name[ROOMNAMELEN];
1967 char new_room_pass[32];
1970 char *notification_message = NULL;
1973 int avoid_access = 0;
1975 cre8_ok = extract_int(args, 0);
1976 extract_token(new_room_name, args, 1, '|', sizeof new_room_name);
1977 new_room_name[ROOMNAMELEN - 1] = 0;
1978 new_room_type = extract_int(args, 2);
1979 extract_token(new_room_pass, args, 3, '|', sizeof new_room_pass);
1980 avoid_access = extract_int(args, 5);
1981 new_room_view = extract_int(args, 6);
1982 new_room_pass[9] = 0;
1985 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 1)) {
1986 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
1990 if (!strcasecmp(new_room_name, MAILROOM)) {
1991 cprintf("%d '%s' already exists.\n",
1992 ERROR + ALREADY_EXISTS, new_room_name);
1996 if (num_parms(args) >= 5) {
1997 fl = CtdlGetCachedFloor(extract_int(args, 4));
1999 cprintf("%d Invalid floor number.\n",
2000 ERROR + INVALID_FLOOR_OPERATION);
2003 else if ((fl->f_flags & F_INUSE) == 0) {
2004 cprintf("%d Invalid floor number.\n",
2005 ERROR + INVALID_FLOOR_OPERATION);
2008 new_room_floor = extract_int(args, 4);
2012 if (CtdlAccessCheck(ac_logged_in)) return;
2014 if (CC->user.axlevel < config.c_createax && !CC->internal_pgm) {
2015 cprintf("%d You need higher access to create rooms.\n",
2016 ERROR + HIGHER_ACCESS_REQUIRED);
2020 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 0)) {
2021 cprintf("%d Ok to create rooms.\n", CIT_OK);
2025 if ((new_room_type < 0) || (new_room_type > 5)) {
2026 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
2030 if (new_room_type == 5) {
2031 if (CC->user.axlevel < AxAideU) {
2032 cprintf("%d Higher access required\n",
2033 ERROR + HIGHER_ACCESS_REQUIRED);
2038 /* Check to make sure the requested room name doesn't already exist */
2039 newflags = CtdlCreateRoom(new_room_name,
2040 new_room_type, new_room_pass, new_room_floor,
2041 0, avoid_access, new_room_view);
2042 if (newflags == 0) {
2043 cprintf("%d '%s' already exists.\n",
2044 ERROR + ALREADY_EXISTS, new_room_name);
2049 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
2053 /* If we reach this point, the room needs to be created. */
2055 newflags = CtdlCreateRoom(new_room_name,
2056 new_room_type, new_room_pass, new_room_floor, 1, 0,
2059 /* post a message in Aide> describing the new room */
2060 notification_message = malloc(1024);
2061 snprintf(notification_message, 1024,
2062 "A new room called \"%s\" has been created by %s%s%s%s%s%s\n",
2064 (CC->logged_in ? CC->curr_user : "an administrator"),
2065 ((newflags & QR_MAILBOX) ? " [personal]" : ""),
2066 ((newflags & QR_PRIVATE) ? " [private]" : ""),
2067 ((newflags & QR_GUESSNAME) ? " [hidden]" : ""),
2068 ((newflags & QR_PASSWORDED) ? " Password: " : ""),
2069 ((newflags & QR_PASSWORDED) ? new_room_pass : "")
2071 CtdlAideMessage(notification_message, "Room Creation Message");
2072 free(notification_message);
2074 cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
2079 void cmd_einf(char *ok)
2080 { /* enter info file for current room */
2082 char infofilename[SIZ];
2087 if (CtdlAccessCheck(ac_room_aide)) return;
2089 if (atoi(ok) == 0) {
2090 cprintf("%d Ok.\n", CIT_OK);
2093 assoc_file_name(infofilename, sizeof infofilename, &CC->room, ctdl_info_dir);
2094 syslog(LOG_DEBUG, "opening\n");
2095 fp = fopen(infofilename, "w");
2096 syslog(LOG_DEBUG, "checking\n");
2098 cprintf("%d Cannot open %s: %s\n",
2099 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
2102 cprintf("%d Send info...\n", SEND_LISTING);
2105 client_getln(buf, sizeof buf);
2106 if (strcmp(buf, "000"))
2107 fprintf(fp, "%s\n", buf);
2108 } while (strcmp(buf, "000"));
2111 /* now update the room index so people will see our new info */
2112 CtdlGetRoomLock(&CC->room, CC->room.QRname); /* lock so no one steps on us */
2113 CC->room.QRinfo = CC->room.QRhighest + 1L;
2114 CtdlPutRoomLock(&CC->room);
2119 * cmd_lflr() - List all known floors
2121 void cmd_lflr(char *gargs)
2126 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
2128 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
2130 for (a = 0; a < MAXFLOORS; ++a) {
2131 CtdlGetFloor(&flbuf, a);
2132 if (flbuf.f_flags & F_INUSE) {
2133 cprintf("%d|%s|%d\n",
2145 * create a new floor
2147 void cmd_cflr(char *argbuf)
2149 char new_floor_name[256];
2152 int free_slot = (-1);
2155 extract_token(new_floor_name, argbuf, 0, '|', sizeof new_floor_name);
2156 cflr_ok = extract_int(argbuf, 1);
2158 if (CtdlAccessCheck(ac_aide)) return;
2160 if (IsEmptyStr(new_floor_name)) {
2161 cprintf("%d Blank floor name not allowed.\n",
2162 ERROR + ILLEGAL_VALUE);
2166 for (a = 0; a < MAXFLOORS; ++a) {
2167 CtdlGetFloor(&flbuf, a);
2169 /* note any free slots while we're scanning... */
2170 if (((flbuf.f_flags & F_INUSE) == 0)
2174 /* check to see if it already exists */
2175 if ((!strcasecmp(flbuf.f_name, new_floor_name))
2176 && (flbuf.f_flags & F_INUSE)) {
2177 cprintf("%d Floor '%s' already exists.\n",
2178 ERROR + ALREADY_EXISTS,
2184 if (free_slot < 0) {
2185 cprintf("%d There is no space available for a new floor.\n",
2186 ERROR + INVALID_FLOOR_OPERATION);
2190 cprintf("%d ok to create...\n", CIT_OK);
2193 lgetfloor(&flbuf, free_slot);
2194 flbuf.f_flags = F_INUSE;
2195 flbuf.f_ref_count = 0;
2196 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
2197 lputfloor(&flbuf, free_slot);
2198 cprintf("%d %d\n", CIT_OK, free_slot);
2206 void cmd_kflr(char *argbuf)
2209 int floor_to_delete;
2213 floor_to_delete = extract_int(argbuf, 0);
2214 kflr_ok = extract_int(argbuf, 1);
2216 if (CtdlAccessCheck(ac_aide)) return;
2218 lgetfloor(&flbuf, floor_to_delete);
2221 if ((flbuf.f_flags & F_INUSE) == 0) {
2222 cprintf("%d Floor %d not in use.\n",
2223 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
2226 if (flbuf.f_ref_count != 0) {
2227 cprintf("%d Cannot delete; floor contains %d rooms.\n",
2228 ERROR + INVALID_FLOOR_OPERATION,
2233 cprintf("%d Ok\n", CIT_OK);
2235 cprintf("%d Ok to delete...\n", CIT_OK);
2242 if ((delete_ok == 1) && (kflr_ok == 1))
2244 lputfloor(&flbuf, floor_to_delete);
2250 void cmd_eflr(char *argbuf)
2256 np = num_parms(argbuf);
2258 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
2262 if (CtdlAccessCheck(ac_aide)) return;
2264 floor_num = extract_int(argbuf, 0);
2265 lgetfloor(&flbuf, floor_num);
2266 if ((flbuf.f_flags & F_INUSE) == 0) {
2267 lputfloor(&flbuf, floor_num);
2268 cprintf("%d Floor %d is not in use.\n",
2269 ERROR + INVALID_FLOOR_OPERATION, floor_num);
2273 extract_token(flbuf.f_name, argbuf, 1, '|', sizeof flbuf.f_name);
2274 lputfloor(&flbuf, floor_num);
2276 cprintf("%d Ok\n", CIT_OK);
2282 * cmd_stat() - return the modification time of the current room (maybe other things in the future)
2284 void cmd_stat(char *gargs)
2286 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
2287 CtdlGetRoom(&CC->room, CC->room.QRname);
2288 cprintf("%d %s|%ld|\n", CIT_OK, CC->room.QRname, CC->room.QRmtime);
2293 /*****************************************************************************/
2294 /* MODULE INITIALIZATION STUFF */
2295 /*****************************************************************************/
2297 CTDL_MODULE_INIT(room_ops)
2300 CtdlRegisterProtoHook(cmd_lrms, "LRMS", "List rooms");
2301 CtdlRegisterProtoHook(cmd_lkra, "LKRA", "List all known rooms");
2302 CtdlRegisterProtoHook(cmd_lkrn, "LKRN", "List known rooms with new messages");
2303 CtdlRegisterProtoHook(cmd_lkro, "LKRO", "List known rooms without new messages");
2304 CtdlRegisterProtoHook(cmd_lzrm, "LZRM", "List zapped rooms");
2305 CtdlRegisterProtoHook(cmd_lprm, "LPRM", "List public rooms");
2306 CtdlRegisterProtoHook(cmd_goto, "GOTO", "Goto a named room");
2307 CtdlRegisterProtoHook(cmd_stat, "STAT", "Get mtime of the current room");
2308 CtdlRegisterProtoHook(cmd_whok, "WHOK", "List users who know this room");
2309 CtdlRegisterProtoHook(cmd_rdir, "RDIR", "List files in room directory");
2310 CtdlRegisterProtoHook(cmd_getr, "GETR", "Get room parameters");
2311 CtdlRegisterProtoHook(cmd_setr, "SETR", "Set room parameters");
2312 CtdlRegisterProtoHook(cmd_geta, "GETA", "Get the room admin name");
2313 CtdlRegisterProtoHook(cmd_seta, "SETA", "Set the room admin for this room");
2314 CtdlRegisterProtoHook(cmd_rinf, "RINF", "Fetch room info file");
2315 CtdlRegisterProtoHook(cmd_kill, "KILL", "Kill (delete) the current room");
2316 CtdlRegisterProtoHook(cmd_cre8, "CRE8", "Create a new room");
2317 CtdlRegisterProtoHook(cmd_einf, "EINF", "Enter info file for the current room");
2318 CtdlRegisterProtoHook(cmd_lflr, "LFLR", "List all known floors");
2319 CtdlRegisterProtoHook(cmd_cflr, "CFLR", "Create a new floor");
2320 CtdlRegisterProtoHook(cmd_kflr, "KFLR", "Kill a floor");
2321 CtdlRegisterProtoHook(cmd_eflr, "EFLR", "Edit a floor");
2323 /* return our Subversion id for the Log */