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(void (*CallBack) (struct ctdlroom *EachRoom, void *out_data),
571 struct ctdlroom qrbuf;
572 struct cdbdata *cdbqr;
574 cdb_rewind(CDB_ROOMS);
576 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
577 memset(&qrbuf, 0, sizeof(struct ctdlroom));
578 memcpy(&qrbuf, cdbqr->ptr,
579 ((cdbqr->len > sizeof(struct ctdlroom)) ?
580 sizeof(struct ctdlroom) : cdbqr->len)
583 room_sanity_check(&qrbuf);
584 if (qrbuf.QRflags & QR_INUSE) {
585 (*CallBack)(&qrbuf, in_data);
592 * delete_msglist() - delete room message pointers
594 void delete_msglist(struct ctdlroom *whichroom)
596 struct cdbdata *cdbml;
598 /* Make sure the msglist we're deleting actually exists, otherwise
599 * libdb will complain when we try to delete an invalid record
601 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
605 /* Go ahead and delete it */
606 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
612 * Message pointer compare function for sort_msglist()
614 int sort_msglist_cmp(const void *m1, const void *m2) {
615 if ((*(const long *)m1) > (*(const long *)m2)) return(1);
616 if ((*(const long *)m1) < (*(const long *)m2)) return(-1);
622 * sort message pointers
623 * (returns new msg count)
625 int sort_msglist(long listptrs[], int oldcount)
635 qsort(listptrs, numitems, sizeof(long), sort_msglist_cmp);
637 /* and yank any nulls */
638 while ((numitems > 0) && (listptrs[0] == 0L)) {
639 memmove(&listptrs[0], &listptrs[1], (sizeof(long) * (numitems - 1)));
648 * Determine whether a given room is non-editable.
650 int CtdlIsNonEditable(struct ctdlroom *qrbuf)
653 /* Mail> rooms are non-editable */
654 if ( (qrbuf->QRflags & QR_MAILBOX)
655 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
658 /* Everything else is editable */
664 * Back-back-end for all room listing commands
666 void list_roomname(struct ctdlroom *qrbuf, int ra, int current_view, int default_view)
668 char truncated_roomname[ROOMNAMELEN];
670 /* For my own mailbox rooms, chop off the owner prefix */
671 if ( (qrbuf->QRflags & QR_MAILBOX)
672 && (atol(qrbuf->QRname) == CC->user.usernum) ) {
673 safestrncpy(truncated_roomname, qrbuf->QRname, sizeof truncated_roomname);
674 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
675 cprintf("%s", truncated_roomname);
677 /* For all other rooms, just display the name in its entirety */
679 cprintf("%s", qrbuf->QRname);
682 /* ...and now the other parameters */
683 cprintf("|%u|%d|%d|%d|%d|%d|%d|%ld|\n",
685 (int) qrbuf->QRfloor,
686 (int) qrbuf->QRorder,
687 (int) qrbuf->QRflags2,
697 * cmd_lrms() - List all accessible rooms, known or forgotten
699 void cmd_lrms_backend(struct ctdlroom *qrbuf, void *data)
701 int FloorBeingSearched = (-1);
705 FloorBeingSearched = *(int *)data;
706 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
708 if ((( ra & (UA_KNOWN | UA_ZAPPED)))
709 && ((qrbuf->QRfloor == (FloorBeingSearched))
710 || ((FloorBeingSearched) < 0)))
711 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
714 void cmd_lrms(char *argbuf)
716 int FloorBeingSearched = (-1);
717 if (!IsEmptyStr(argbuf))
718 FloorBeingSearched = extract_int(argbuf, 0);
720 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
722 CtdlGetUser(&CC->user, CC->curr_user);
723 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
725 CtdlForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
732 * cmd_lkra() - List all known rooms
734 void cmd_lkra_backend(struct ctdlroom *qrbuf, void *data)
736 int FloorBeingSearched = (-1);
740 FloorBeingSearched = *(int *)data;
741 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
743 if ((( ra & (UA_KNOWN)))
744 && ((qrbuf->QRfloor == (FloorBeingSearched))
745 || ((FloorBeingSearched) < 0)))
746 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
749 void cmd_lkra(char *argbuf)
751 int FloorBeingSearched = (-1);
752 if (!IsEmptyStr(argbuf))
753 FloorBeingSearched = extract_int(argbuf, 0);
755 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
757 CtdlGetUser(&CC->user, CC->curr_user);
758 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
760 CtdlForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
766 void cmd_lprm_backend(struct ctdlroom *qrbuf, void *data)
768 int FloorBeingSearched = (-1);
772 FloorBeingSearched = *(int *)data;
773 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
775 if ( ((qrbuf->QRflags & QR_PRIVATE) == 0)
776 && ((qrbuf->QRflags & QR_MAILBOX) == 0)
777 && ((qrbuf->QRfloor == (FloorBeingSearched))
778 || ((FloorBeingSearched) < 0)))
779 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
782 void cmd_lprm(char *argbuf)
784 int FloorBeingSearched = (-1);
785 if (!IsEmptyStr(argbuf))
786 FloorBeingSearched = extract_int(argbuf, 0);
788 cprintf("%d Public rooms:\n", LISTING_FOLLOWS);
790 CtdlForEachRoom(cmd_lprm_backend, &FloorBeingSearched);
797 * cmd_lkrn() - List all known rooms with new messages
799 void cmd_lkrn_backend(struct ctdlroom *qrbuf, void *data)
801 int FloorBeingSearched = (-1);
805 FloorBeingSearched = *(int *)data;
806 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
809 && (ra & UA_HASNEWMSGS)
810 && ((qrbuf->QRfloor == (FloorBeingSearched))
811 || ((FloorBeingSearched) < 0)))
812 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
815 void cmd_lkrn(char *argbuf)
817 int FloorBeingSearched = (-1);
818 if (!IsEmptyStr(argbuf))
819 FloorBeingSearched = extract_int(argbuf, 0);
821 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
823 CtdlGetUser(&CC->user, CC->curr_user);
824 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
826 CtdlForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
833 * cmd_lkro() - List all known rooms
835 void cmd_lkro_backend(struct ctdlroom *qrbuf, void *data)
837 int FloorBeingSearched = (-1);
841 FloorBeingSearched = *(int *)data;
842 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
845 && ((ra & UA_HASNEWMSGS) == 0)
846 && ((qrbuf->QRfloor == (FloorBeingSearched))
847 || ((FloorBeingSearched) < 0)))
848 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
851 void cmd_lkro(char *argbuf)
853 int FloorBeingSearched = (-1);
854 if (!IsEmptyStr(argbuf))
855 FloorBeingSearched = extract_int(argbuf, 0);
857 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
859 CtdlGetUser(&CC->user, CC->curr_user);
860 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
862 CtdlForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
869 * cmd_lzrm() - List all forgotten rooms
871 void cmd_lzrm_backend(struct ctdlroom *qrbuf, void *data)
873 int FloorBeingSearched = (-1);
877 FloorBeingSearched = *(int *)data;
878 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
880 if ((ra & UA_GOTOALLOWED)
882 && ((qrbuf->QRfloor == (FloorBeingSearched))
883 || ((FloorBeingSearched) < 0)))
884 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
887 void cmd_lzrm(char *argbuf)
889 int FloorBeingSearched = (-1);
890 if (!IsEmptyStr(argbuf))
891 FloorBeingSearched = extract_int(argbuf, 0);
893 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
895 CtdlGetUser(&CC->user, CC->curr_user);
896 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
898 CtdlForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
904 * Make the specified room the current room for this session. No validation
905 * or access control is done here -- the caller should make sure that the
906 * specified room exists and is ok to access.
908 void CtdlUserGoto(char *where, int display_result, int transiently,
909 int *retmsgs, int *retnew)
911 struct CitContext *CCC = CC;
913 int new_messages = 0;
914 int old_messages = 0;
915 int total_messages = 0;
919 int newmailcount = 0;
921 char truncated_roomname[ROOMNAMELEN];
922 struct cdbdata *cdbfr;
923 long *msglist = NULL;
925 unsigned int original_v_flags;
928 char setstr[128], lostr[64], histr[64];
932 /* If the supplied room name is NULL, the caller wants us to know that
933 * it has already copied the room record into CC->room, so
934 * we can skip the extra database fetch.
937 safestrncpy(CCC->room.QRname, where, sizeof CCC->room.QRname);
938 CtdlGetRoom(&CCC->room, where);
941 /* Take care of all the formalities. */
943 begin_critical_section(S_USERS);
944 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
945 original_v_flags = vbuf.v_flags;
947 /* Know the room ... but not if it's the page log room, or if the
948 * caller specified that we're only entering this room transiently.
950 if ((strcasecmp(CCC->room.QRname, config.c_logpages))
951 && (transiently == 0) ) {
952 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
953 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
956 /* Only rewrite the database record if we changed something */
957 if (vbuf.v_flags != original_v_flags) {
958 CtdlSetRelationship(&vbuf, &CCC->user, &CCC->room);
960 end_critical_section(S_USERS);
962 /* Check for new mail */
963 newmailcount = NewMailCount();
965 /* set info to 1 if the user needs to read the room's info file */
966 if (CCC->room.QRinfo > vbuf.v_lastseen) {
970 cdbfr = cdb_fetch(CDB_MSGLISTS, &CCC->room.QRnumber, sizeof(long));
972 msglist = (long *) cdbfr->ptr;
973 cdbfr->ptr = NULL; /* CtdlUserGoto() now owns this memory */
974 num_msgs = cdbfr->len / sizeof(long);
979 for (a=0; a<num_msgs; ++a) {
980 if (msglist[a] > 0L) ++total_messages;
983 num_sets = num_tokens(vbuf.v_seen, ',');
984 for (s=0; s<num_sets; ++s) {
985 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
987 extract_token(lostr, setstr, 0, ':', sizeof lostr);
988 if (num_tokens(setstr, ':') >= 2) {
989 extract_token(histr, setstr, 1, ':', sizeof histr);
990 if (!strcmp(histr, "*")) {
991 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
995 strcpy(histr, lostr);
1000 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
1001 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
1007 new_messages = total_messages - old_messages;
1009 if (msglist != NULL) free(msglist);
1011 if (CCC->room.QRflags & QR_MAILBOX)
1016 if ((CCC->room.QRroomaide == CCC->user.usernum)
1017 || (CCC->user.axlevel >= AxAideU))
1022 safestrncpy(truncated_roomname, CCC->room.QRname, sizeof truncated_roomname);
1023 if ( (CCC->room.QRflags & QR_MAILBOX)
1024 && (atol(CCC->room.QRname) == CCC->user.usernum) ) {
1025 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
1028 if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
1032 if (retmsgs != NULL) *retmsgs = total_messages;
1033 if (retnew != NULL) *retnew = new_messages;
1034 MSG_syslog(LOG_INFO, "<%s> %d new of %d total messages\n",
1036 new_messages, total_messages
1039 CCC->curr_view = (int)vbuf.v_view;
1041 if (display_result) {
1042 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|%d|%ld|\n",
1043 CIT_OK, CtdlCheckExpress(),
1046 (int)total_messages,
1048 (int)CCC->room.QRflags,
1049 (long)CCC->room.QRhighest,
1050 (long)vbuf.v_lastseen,
1054 (int)CCC->room.QRfloor,
1056 (int)CCC->room.QRdefaultview,
1058 (int)CCC->room.QRflags2,
1059 (long)CCC->room.QRmtime
1066 * Handle some of the macro named rooms
1068 void convert_room_name_macros(char *towhere, size_t maxlen) {
1069 if (!strcasecmp(towhere, "_BASEROOM_")) {
1070 safestrncpy(towhere, config.c_baseroom, maxlen);
1072 else if (!strcasecmp(towhere, "_MAIL_")) {
1073 safestrncpy(towhere, MAILROOM, maxlen);
1075 else if (!strcasecmp(towhere, "_TRASH_")) {
1076 safestrncpy(towhere, USERTRASHROOM, maxlen);
1078 else if (!strcasecmp(towhere, "_DRAFTS_")) {
1079 safestrncpy(towhere, USERDRAFTROOM, maxlen);
1081 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
1082 safestrncpy(towhere, config.c_twitroom, maxlen);
1084 else if (!strcasecmp(towhere, "_CALENDAR_")) {
1085 safestrncpy(towhere, USERCALENDARROOM, maxlen);
1087 else if (!strcasecmp(towhere, "_TASKS_")) {
1088 safestrncpy(towhere, USERTASKSROOM, maxlen);
1090 else if (!strcasecmp(towhere, "_CONTACTS_")) {
1091 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
1093 else if (!strcasecmp(towhere, "_NOTES_")) {
1094 safestrncpy(towhere, USERNOTESROOM, maxlen);
1100 * cmd_goto() - goto a new room
1102 void cmd_goto(char *gargs)
1104 struct ctdlroom QRscratch;
1108 char augmented_roomname[ROOMNAMELEN];
1109 char towhere[ROOMNAMELEN];
1111 int transiently = 0;
1113 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
1115 extract_token(towhere, gargs, 0, '|', sizeof towhere);
1116 extract_token(password, gargs, 1, '|', sizeof password);
1117 transiently = extract_int(gargs, 2);
1119 CtdlGetUser(&CC->user, CC->curr_user);
1122 * Handle some of the macro named rooms
1124 convert_room_name_macros(towhere, sizeof towhere);
1126 /* First try a regular match */
1127 c = CtdlGetRoom(&QRscratch, towhere);
1129 /* Then try a mailbox name match */
1131 CtdlMailboxName(augmented_roomname, sizeof augmented_roomname,
1132 &CC->user, towhere);
1133 c = CtdlGetRoom(&QRscratch, augmented_roomname);
1135 safestrncpy(towhere, augmented_roomname, sizeof towhere);
1138 /* And if the room was found... */
1141 /* Let internal programs go directly to any room. */
1142 if (CC->internal_pgm) {
1143 memcpy(&CC->room, &QRscratch,
1144 sizeof(struct ctdlroom));
1145 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1149 /* See if there is an existing user/room relationship */
1150 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
1152 /* normal clients have to pass through security */
1153 if (ra & UA_GOTOALLOWED) {
1158 if ((QRscratch.QRflags & QR_MAILBOX) &&
1159 ((ra & UA_GOTOALLOWED))) {
1160 memcpy(&CC->room, &QRscratch,
1161 sizeof(struct ctdlroom));
1162 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1164 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
1165 ((ra & UA_KNOWN) == 0) &&
1166 (strcasecmp(QRscratch.QRpasswd, password)) &&
1167 (CC->user.axlevel < AxAideU)
1169 cprintf("%d wrong or missing passwd\n",
1170 ERROR + PASSWORD_REQUIRED);
1172 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
1173 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
1174 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
1175 ((ra & UA_KNOWN) == 0) &&
1176 (CC->user.axlevel < AxAideU)
1178 syslog(LOG_DEBUG, "Failed to acquire private room\n");
1180 memcpy(&CC->room, &QRscratch,
1181 sizeof(struct ctdlroom));
1182 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1188 cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
1192 void cmd_whok(char *cmdbuf)
1194 struct ctdluser temp;
1195 struct cdbdata *cdbus;
1198 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
1199 cdb_rewind(CDB_USERS);
1200 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1201 memset(&temp, 0, sizeof temp);
1202 memcpy(&temp, cdbus->ptr, sizeof temp);
1205 CtdlRoomAccess(&CC->room, &temp, &ra, NULL);
1206 if ((!IsEmptyStr(temp.fullname)) &&
1207 (CC->room.QRflags & QR_INUSE) &&
1210 cprintf("%s\n", temp.fullname);
1217 * RDIR command for room directory
1219 void cmd_rdir(char *cmdbuf)
1224 struct stat statbuf;
1225 DIR *filedir = NULL;
1226 struct dirent *filedir_entry;
1232 if (CtdlAccessCheck(ac_logged_in)) return;
1234 CtdlGetRoom(&CC->room, CC->room.QRname);
1235 CtdlGetUser(&CC->user, CC->curr_user);
1237 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
1238 cprintf("%d not here.\n", ERROR + NOT_HERE);
1241 if (((CC->room.QRflags & QR_VISDIR) == 0)
1242 && (CC->user.axlevel < AxAideU)
1243 && (CC->user.usernum != CC->room.QRroomaide)) {
1244 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1248 snprintf(buf, sizeof buf, "%s/%s", ctdl_file_dir, CC->room.QRdirname);
1249 filedir = opendir (buf);
1251 if (filedir == NULL) {
1252 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1255 cprintf("%d %s|%s/%s\n", LISTING_FOLLOWS, config.c_fqdn, ctdl_file_dir, CC->room.QRdirname);
1257 snprintf(buf, sizeof buf, "%s/%s/filedir", ctdl_file_dir, CC->room.QRdirname);
1258 fd = fopen(buf, "r");
1260 fd = fopen("/dev/null", "r");
1261 while ((filedir_entry = readdir(filedir)))
1263 if (strcasecmp(filedir_entry->d_name, "filedir") && filedir_entry->d_name[0] != '.')
1265 #ifdef _DIRENT_HAVE_D_NAMELEN
1266 d_namelen = filedir_entry->d_namelen;
1268 d_namelen = strlen(filedir_entry->d_name);
1270 snprintf(buf, sizeof buf, "%s/%s/%s", ctdl_file_dir, CC->room.QRdirname, filedir_entry->d_name);
1271 stat(buf, &statbuf); /* stat the file */
1272 if (!(statbuf.st_mode & S_IFREG))
1274 snprintf(buf2, sizeof buf2,
1275 "\"%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",
1276 buf, CC->room.QRname
1278 CtdlAideMessage(buf2, "Unusable data found in room directory");
1279 continue; /* not a useable file type so don't show it */
1281 safestrncpy(comment, "", sizeof comment);
1282 fseek(fd, 0L, 0); /* rewind descriptions file */
1283 /* Get the description from the descriptions file */
1284 while ((fgets(buf, sizeof buf, fd) != NULL) && (IsEmptyStr(comment)))
1286 buf[strlen(buf) - 1] = 0;
1287 if ((!strncasecmp(buf, filedir_entry->d_name, d_namelen)) && (buf[d_namelen] == ' '))
1288 safestrncpy(comment, &buf[d_namelen + 1], sizeof comment);
1290 len = extract_token (mimebuf, comment, 0,' ', 64);
1291 if ((len <0) || strchr(mimebuf, '/') == NULL)
1293 snprintf (mimebuf, 64, "application/octetstream");
1296 cprintf("%s|%ld|%s|%s\n",
1297 filedir_entry->d_name,
1298 (long)statbuf.st_size,
1310 * get room parameters (aide or room aide command)
1312 void cmd_getr(char *cmdbuf)
1314 if (CtdlAccessCheck(ac_room_aide)) return;
1316 CtdlGetRoom(&CC->room, CC->room.QRname);
1317 cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1321 ((CC->room.QRflags & QR_MAILBOX) ?
1322 &CC->room.QRname[11] : CC->room.QRname),
1324 ((CC->room.QRflags & QR_PASSWORDED) ?
1325 CC->room.QRpasswd : ""),
1327 ((CC->room.QRflags & QR_DIRECTORY) ?
1328 CC->room.QRdirname : ""),
1331 (int) CC->room.QRfloor,
1332 (int) CC->room.QRorder,
1334 CC->room.QRdefaultview,
1341 * Back end function to rename a room.
1342 * You can also specify which floor to move the room to, or specify -1 to
1343 * keep the room on the same floor it was on.
1345 * If you are renaming a mailbox room, you must supply the namespace prefix
1346 * in *at least* the old name!
1348 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1350 struct ctdlroom qrbuf;
1351 struct ctdlroom qrtmp;
1356 char actual_old_name[ROOMNAMELEN];
1358 syslog(LOG_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1359 old_name, new_name, new_floor);
1361 if (new_floor >= 0) {
1362 fl = CtdlGetCachedFloor(new_floor);
1363 if ((fl->f_flags & F_INUSE) == 0) {
1364 return(crr_invalid_floor);
1368 begin_critical_section(S_ROOMS);
1370 if ( (CtdlGetRoom(&qrtmp, new_name) == 0)
1371 && (strcasecmp(new_name, old_name)) ) {
1372 ret = crr_already_exists;
1375 else if (CtdlGetRoom(&qrbuf, old_name) != 0) {
1376 ret = crr_room_not_found;
1379 else if ( (CC->user.axlevel < AxAideU) && (!CC->internal_pgm)
1380 && (CC->user.usernum != qrbuf.QRroomaide)
1381 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) ) {
1382 ret = crr_access_denied;
1385 else if (CtdlIsNonEditable(&qrbuf)) {
1386 ret = crr_noneditable;
1391 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1392 if (qrbuf.QRflags & QR_MAILBOX) {
1393 owner = atol(qrbuf.QRname);
1395 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1396 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1397 "%010ld.%s", owner, new_name);
1400 safestrncpy(qrbuf.QRname, new_name,
1401 sizeof(qrbuf.QRname));
1404 /* Reject change of floor for baseroom/aideroom */
1405 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1406 !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1410 /* Take care of floor stuff */
1411 old_floor = qrbuf.QRfloor;
1412 if (new_floor < 0) {
1413 new_floor = old_floor;
1415 qrbuf.QRfloor = new_floor;
1416 CtdlPutRoom(&qrbuf);
1418 begin_critical_section(S_CONFIG);
1420 /* If baseroom/aideroom name changes, update config */
1421 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1422 safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1425 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1426 safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1430 end_critical_section(S_CONFIG);
1432 /* If the room name changed, then there are now two room
1433 * records, so we have to delete the old one.
1435 if (strcasecmp(new_name, old_name)) {
1436 b_deleteroom(actual_old_name);
1442 end_critical_section(S_ROOMS);
1444 /* Adjust the floor reference counts if necessary */
1445 if (new_floor != old_floor) {
1446 lgetfloor(&flbuf, old_floor);
1447 --flbuf.f_ref_count;
1448 lputfloor(&flbuf, old_floor);
1449 syslog(LOG_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1450 lgetfloor(&flbuf, new_floor);
1451 ++flbuf.f_ref_count;
1452 lputfloor(&flbuf, new_floor);
1453 syslog(LOG_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1456 /* ...and everybody say "YATTA!" */
1462 * set room parameters (aide or room aide command)
1464 void cmd_setr(char *args)
1470 char new_name[ROOMNAMELEN];
1472 if (CtdlAccessCheck(ac_logged_in)) return;
1474 if (num_parms(args) >= 6) {
1475 new_floor = extract_int(args, 5);
1477 new_floor = (-1); /* don't change the floor */
1480 /* When is a new name more than just a new name? When the old name
1481 * has a namespace prefix.
1483 if (CC->room.QRflags & QR_MAILBOX) {
1484 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
1486 safestrncpy(new_name, "", sizeof new_name);
1488 extract_token(&new_name[strlen(new_name)], args, 0, '|', (sizeof new_name - strlen(new_name)));
1490 r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
1492 if (r == crr_room_not_found) {
1493 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
1494 } else if (r == crr_already_exists) {
1495 cprintf("%d '%s' already exists.\n",
1496 ERROR + ALREADY_EXISTS, new_name);
1497 } else if (r == crr_noneditable) {
1498 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
1499 } else if (r == crr_invalid_floor) {
1500 cprintf("%d Target floor does not exist.\n",
1501 ERROR + INVALID_FLOOR_OPERATION);
1502 } else if (r == crr_access_denied) {
1503 cprintf("%d You do not have permission to edit '%s'\n",
1504 ERROR + HIGHER_ACCESS_REQUIRED,
1506 } else if (r != crr_ok) {
1507 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1508 ERROR + INTERNAL_ERROR, r);
1515 CtdlGetRoom(&CC->room, new_name);
1517 /* Now we have to do a bunch of other stuff */
1519 if (num_parms(args) >= 7) {
1520 new_order = extract_int(args, 6);
1523 if (new_order > 127)
1527 CtdlGetRoomLock(&CC->room, CC->room.QRname);
1529 /* Directory room */
1530 extract_token(buf, args, 2, '|', sizeof buf);
1532 safestrncpy(CC->room.QRdirname, buf,
1533 sizeof CC->room.QRdirname);
1536 if (num_parms(args) >= 8) {
1537 CC->room.QRdefaultview = extract_int(args, 7);
1540 /* Second set of flags */
1541 if (num_parms(args) >= 9) {
1542 CC->room.QRflags2 = extract_int(args, 8);
1546 CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
1547 /* Clean up a client boo-boo: if the client set the room to
1548 * guess-name or passworded, ensure that the private flag is
1551 if ((CC->room.QRflags & QR_GUESSNAME)
1552 || (CC->room.QRflags & QR_PASSWORDED))
1553 CC->room.QRflags |= QR_PRIVATE;
1555 /* Some changes can't apply to BASEROOM */
1556 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1558 CC->room.QRorder = 0;
1559 CC->room.QRpasswd[0] = '\0';
1560 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1561 QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1562 CC->room.QRflags |= QR_PERMANENT;
1564 /* March order (doesn't apply to AIDEROOM) */
1565 if (num_parms(args) >= 7)
1566 CC->room.QRorder = (char) new_order;
1568 extract_token(buf, args, 1, '|', sizeof buf);
1570 safestrncpy(CC->room.QRpasswd, buf,
1571 sizeof CC->room.QRpasswd);
1572 /* Kick everyone out if the client requested it
1573 * (by changing the room's generation number)
1575 if (extract_int(args, 4)) {
1576 time(&CC->room.QRgen);
1579 /* Some changes can't apply to AIDEROOM */
1580 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1582 CC->room.QRorder = 0;
1583 CC->room.QRflags &= ~QR_MAILBOX;
1584 CC->room.QRflags |= QR_PERMANENT;
1587 /* Write the room record back to disk */
1588 CtdlPutRoomLock(&CC->room);
1590 /* Create a room directory if necessary */
1591 if (CC->room.QRflags & QR_DIRECTORY) {
1592 snprintf(buf, sizeof buf,"%s/%s",
1594 CC->room.QRdirname);
1597 snprintf(buf, sizeof buf, "The room \"%s\" has been edited by %s.\n",
1599 (CC->logged_in ? CC->curr_user : "an administrator")
1601 CtdlAideMessage(buf, "Room modification Message");
1602 cprintf("%d Ok\n", CIT_OK);
1608 * get the name of the room aide for this room
1610 void cmd_geta(char *cmdbuf)
1612 struct ctdluser usbuf;
1614 if (CtdlAccessCheck(ac_logged_in)) return;
1616 if (CtdlGetUserByNumber(&usbuf, CC->room.QRroomaide) == 0) {
1617 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1619 cprintf("%d \n", CIT_OK);
1625 * set the room aide for this room
1627 void cmd_seta(char *new_ra)
1629 struct ctdluser usbuf;
1634 if (CtdlAccessCheck(ac_room_aide)) return;
1636 if (CtdlGetUser(&usbuf, new_ra) != 0) {
1639 newu = usbuf.usernum;
1642 CtdlGetRoomLock(&CC->room, CC->room.QRname);
1644 if (CC->room.QRroomaide != newu) {
1647 CC->room.QRroomaide = newu;
1648 CtdlPutRoomLock(&CC->room);
1651 * We have to post the change notice _after_ writing changes to
1652 * the room table, otherwise it would deadlock!
1654 if (post_notice == 1) {
1655 if (!IsEmptyStr(usbuf.fullname))
1656 snprintf(buf, sizeof buf,
1657 "%s is now the room aide for \"%s\".\n",
1658 usbuf.fullname, CC->room.QRname);
1660 snprintf(buf, sizeof buf,
1661 "There is now no room aide for \"%s\".\n",
1663 CtdlAideMessage(buf, "Aide Room Modification");
1665 cprintf("%d Ok\n", CIT_OK);
1669 * retrieve info file for this room
1671 void cmd_rinf(char *gargs)
1677 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_info_dir);
1678 info_fp = fopen(filename, "r");
1680 if (info_fp == NULL) {
1681 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
1684 cprintf("%d Info:\n", LISTING_FOLLOWS);
1685 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1686 if (!IsEmptyStr(buf))
1687 buf[strlen(buf) - 1] = 0;
1688 cprintf("%s\n", buf);
1695 * Asynchronously schedule a room for deletion. The room will appear
1696 * deleted to the user(s), but it won't actually get purged from the
1697 * database until THE DREADED AUTO-PURGER makes its next run.
1699 void CtdlScheduleRoomForDeletion(struct ctdlroom *qrbuf)
1701 char old_name[ROOMNAMELEN];
1704 syslog(LOG_NOTICE, "Scheduling room <%s> for deletion\n",
1707 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1709 CtdlGetRoom(qrbuf, qrbuf->QRname);
1711 /* Turn the room into a private mailbox owned by a user who doesn't
1712 * exist. This will immediately make the room invisible to everyone,
1713 * and qualify the room for purging.
1715 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1720 qrbuf->QRflags |= QR_MAILBOX;
1721 time(&qrbuf->QRgen); /* Use a timestamp as the new generation number */
1725 b_deleteroom(old_name);
1731 * Back end processing to delete a room and everything associated with it
1732 * (This one is synchronous and should only get called by THE DREADED
1733 * AUTO-PURGER in serv_expire.c. All user-facing code should call
1734 * the asynchronous schedule_room_for_deletion() instead.)
1736 void CtdlDeleteRoom(struct ctdlroom *qrbuf)
1740 /* TODO: filename magic? does this realy work? */
1742 syslog(LOG_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1744 /* Delete the info file */
1745 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_info_dir);
1748 /* Delete the image file */
1749 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_image_dir);
1752 /* Delete the room's network config file */
1753 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1756 /* Delete the messages in the room
1757 * (Careful: this opens an S_ROOMS critical section!)
1759 CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
1761 /* Flag the room record as not in use */
1762 CtdlGetRoomLock(qrbuf, qrbuf->QRname);
1764 CtdlPutRoomLock(qrbuf);
1766 /* then decrement the reference count for the floor */
1767 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1768 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1769 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1771 /* Delete the room record from the database! */
1772 b_deleteroom(qrbuf->QRname);
1778 * Check access control for deleting a room
1780 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1782 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1786 if (CtdlIsNonEditable(qr)) {
1791 * For mailboxes, check stuff
1793 if (qr->QRflags & QR_MAILBOX) {
1795 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1797 if (atol(qr->QRname) != CC->user.usernum) {
1798 return(0); /* not my room */
1801 /* Can't delete your Mail> room */
1802 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1804 /* Otherwise it's ok */
1809 * For normal rooms, just check for aide or room aide status.
1811 return(is_room_aide());
1815 * aide command: kill the current room
1817 void cmd_kill(char *argbuf)
1819 char deleted_room_name[ROOMNAMELEN];
1823 kill_ok = extract_int(argbuf, 0);
1825 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
1826 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1830 if (CC->room.QRflags & QR_MAILBOX) {
1831 safestrncpy(deleted_room_name, &CC->room.QRname[11], sizeof deleted_room_name);
1834 safestrncpy(deleted_room_name, CC->room.QRname, sizeof deleted_room_name);
1837 /* Do the dirty work */
1838 CtdlScheduleRoomForDeletion(&CC->room);
1840 /* Return to the Lobby */
1841 CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
1843 /* tell the world what we did */
1844 snprintf(msg, sizeof msg, "The room \"%s\" has been deleted by %s.\n",
1846 (CC->logged_in ? CC->curr_user : "an administrator")
1848 CtdlAideMessage(msg, "Room Purger Message");
1849 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1851 cprintf("%d ok to delete.\n", CIT_OK);
1857 * Internal code to create a new room (returns room flags)
1859 * Room types: 0=public, 1=hidden, 2=passworded, 3=invitation-only,
1860 * 4=mailbox, 5=mailbox, but caller supplies namespace
1862 unsigned CtdlCreateRoom(char *new_room_name,
1864 char *new_room_pass,
1871 struct ctdlroom qrbuf;
1875 syslog(LOG_DEBUG, "CtdlCreateRoom(name=%s, type=%d, view=%d)\n",
1876 new_room_name, new_room_type, new_room_view);
1878 if (CtdlGetRoom(&qrbuf, new_room_name) == 0) {
1879 syslog(LOG_DEBUG, "%s already exists.\n", new_room_name);
1883 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1884 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1885 qrbuf.QRflags = QR_INUSE;
1886 if (new_room_type > 0)
1887 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1888 if (new_room_type == 1)
1889 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1890 if (new_room_type == 2)
1891 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1892 if ( (new_room_type == 4) || (new_room_type == 5) ) {
1893 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1894 /* qrbuf.QRflags2 |= QR2_SUBJECTREQ; */
1897 /* If the user is requesting a personal room, set up the room
1898 * name accordingly (prepend the user number)
1900 if (new_room_type == 4) {
1901 CtdlMailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1904 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1907 /* If the room is private, and the system administrator has elected
1908 * to automatically grant room aide privileges, do so now.
1910 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1911 qrbuf.QRroomaide = CC->user.usernum;
1913 /* Blog owners automatically become room aides of their blogs.
1914 * (In the future we will offer a site-wide configuration setting to suppress this behavior.)
1916 else if (new_room_view == VIEW_BLOG) {
1917 qrbuf.QRroomaide = CC->user.usernum;
1919 /* Otherwise, set the room aide to undefined.
1922 qrbuf.QRroomaide = (-1L);
1926 * If the caller is only interested in testing whether this will work,
1927 * return now without creating the room.
1929 if (!really_create) return (qrbuf.QRflags);
1931 qrbuf.QRnumber = get_new_room_number();
1932 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1933 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1934 qrbuf.QRfloor = new_room_floor;
1935 qrbuf.QRdefaultview = new_room_view;
1937 /* save what we just did... */
1938 CtdlPutRoom(&qrbuf);
1940 /* bump the reference count on whatever floor the room is on */
1941 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1942 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1943 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1945 /* Grant the creator access to the room unless the avoid_access
1946 * parameter was specified.
1948 if ( (CC->logged_in) && (avoid_access == 0) ) {
1949 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1950 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1951 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1952 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1955 /* resume our happy day */
1956 return (qrbuf.QRflags);
1963 void cmd_cre8(char *args)
1966 char new_room_name[ROOMNAMELEN];
1968 char new_room_pass[32];
1971 char *notification_message = NULL;
1974 int avoid_access = 0;
1976 cre8_ok = extract_int(args, 0);
1977 extract_token(new_room_name, args, 1, '|', sizeof new_room_name);
1978 new_room_name[ROOMNAMELEN - 1] = 0;
1979 new_room_type = extract_int(args, 2);
1980 extract_token(new_room_pass, args, 3, '|', sizeof new_room_pass);
1981 avoid_access = extract_int(args, 5);
1982 new_room_view = extract_int(args, 6);
1983 new_room_pass[9] = 0;
1986 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 1)) {
1987 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
1991 if (!strcasecmp(new_room_name, MAILROOM)) {
1992 cprintf("%d '%s' already exists.\n",
1993 ERROR + ALREADY_EXISTS, new_room_name);
1997 if (num_parms(args) >= 5) {
1998 fl = CtdlGetCachedFloor(extract_int(args, 4));
2000 cprintf("%d Invalid floor number.\n",
2001 ERROR + INVALID_FLOOR_OPERATION);
2004 else if ((fl->f_flags & F_INUSE) == 0) {
2005 cprintf("%d Invalid floor number.\n",
2006 ERROR + INVALID_FLOOR_OPERATION);
2009 new_room_floor = extract_int(args, 4);
2013 if (CtdlAccessCheck(ac_logged_in)) return;
2015 if (CC->user.axlevel < config.c_createax && !CC->internal_pgm) {
2016 cprintf("%d You need higher access to create rooms.\n",
2017 ERROR + HIGHER_ACCESS_REQUIRED);
2021 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 0)) {
2022 cprintf("%d Ok to create rooms.\n", CIT_OK);
2026 if ((new_room_type < 0) || (new_room_type > 5)) {
2027 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
2031 if (new_room_type == 5) {
2032 if (CC->user.axlevel < AxAideU) {
2033 cprintf("%d Higher access required\n",
2034 ERROR + HIGHER_ACCESS_REQUIRED);
2039 /* Check to make sure the requested room name doesn't already exist */
2040 newflags = CtdlCreateRoom(new_room_name,
2041 new_room_type, new_room_pass, new_room_floor,
2042 0, avoid_access, new_room_view);
2043 if (newflags == 0) {
2044 cprintf("%d '%s' already exists.\n",
2045 ERROR + ALREADY_EXISTS, new_room_name);
2050 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
2054 /* If we reach this point, the room needs to be created. */
2056 newflags = CtdlCreateRoom(new_room_name,
2057 new_room_type, new_room_pass, new_room_floor, 1, 0,
2060 /* post a message in Aide> describing the new room */
2061 notification_message = malloc(1024);
2062 snprintf(notification_message, 1024,
2063 "A new room called \"%s\" has been created by %s%s%s%s%s%s\n",
2065 (CC->logged_in ? CC->curr_user : "an administrator"),
2066 ((newflags & QR_MAILBOX) ? " [personal]" : ""),
2067 ((newflags & QR_PRIVATE) ? " [private]" : ""),
2068 ((newflags & QR_GUESSNAME) ? " [hidden]" : ""),
2069 ((newflags & QR_PASSWORDED) ? " Password: " : ""),
2070 ((newflags & QR_PASSWORDED) ? new_room_pass : "")
2072 CtdlAideMessage(notification_message, "Room Creation Message");
2073 free(notification_message);
2075 cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
2080 void cmd_einf(char *ok)
2081 { /* enter info file for current room */
2083 char infofilename[SIZ];
2088 if (CtdlAccessCheck(ac_room_aide)) return;
2090 if (atoi(ok) == 0) {
2091 cprintf("%d Ok.\n", CIT_OK);
2094 assoc_file_name(infofilename, sizeof infofilename, &CC->room, ctdl_info_dir);
2095 syslog(LOG_DEBUG, "opening\n");
2096 fp = fopen(infofilename, "w");
2097 syslog(LOG_DEBUG, "checking\n");
2099 cprintf("%d Cannot open %s: %s\n",
2100 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
2103 cprintf("%d Send info...\n", SEND_LISTING);
2106 client_getln(buf, sizeof buf);
2107 if (strcmp(buf, "000"))
2108 fprintf(fp, "%s\n", buf);
2109 } while (strcmp(buf, "000"));
2112 /* now update the room index so people will see our new info */
2113 CtdlGetRoomLock(&CC->room, CC->room.QRname); /* lock so no one steps on us */
2114 CC->room.QRinfo = CC->room.QRhighest + 1L;
2115 CtdlPutRoomLock(&CC->room);
2120 * cmd_lflr() - List all known floors
2122 void cmd_lflr(char *gargs)
2127 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
2129 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
2131 for (a = 0; a < MAXFLOORS; ++a) {
2132 CtdlGetFloor(&flbuf, a);
2133 if (flbuf.f_flags & F_INUSE) {
2134 cprintf("%d|%s|%d\n",
2146 * create a new floor
2148 void cmd_cflr(char *argbuf)
2150 char new_floor_name[256];
2153 int free_slot = (-1);
2156 extract_token(new_floor_name, argbuf, 0, '|', sizeof new_floor_name);
2157 cflr_ok = extract_int(argbuf, 1);
2159 if (CtdlAccessCheck(ac_aide)) return;
2161 if (IsEmptyStr(new_floor_name)) {
2162 cprintf("%d Blank floor name not allowed.\n",
2163 ERROR + ILLEGAL_VALUE);
2167 for (a = 0; a < MAXFLOORS; ++a) {
2168 CtdlGetFloor(&flbuf, a);
2170 /* note any free slots while we're scanning... */
2171 if (((flbuf.f_flags & F_INUSE) == 0)
2175 /* check to see if it already exists */
2176 if ((!strcasecmp(flbuf.f_name, new_floor_name))
2177 && (flbuf.f_flags & F_INUSE)) {
2178 cprintf("%d Floor '%s' already exists.\n",
2179 ERROR + ALREADY_EXISTS,
2185 if (free_slot < 0) {
2186 cprintf("%d There is no space available for a new floor.\n",
2187 ERROR + INVALID_FLOOR_OPERATION);
2191 cprintf("%d ok to create...\n", CIT_OK);
2194 lgetfloor(&flbuf, free_slot);
2195 flbuf.f_flags = F_INUSE;
2196 flbuf.f_ref_count = 0;
2197 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
2198 lputfloor(&flbuf, free_slot);
2199 cprintf("%d %d\n", CIT_OK, free_slot);
2207 void cmd_kflr(char *argbuf)
2210 int floor_to_delete;
2214 floor_to_delete = extract_int(argbuf, 0);
2215 kflr_ok = extract_int(argbuf, 1);
2217 if (CtdlAccessCheck(ac_aide)) return;
2219 lgetfloor(&flbuf, floor_to_delete);
2222 if ((flbuf.f_flags & F_INUSE) == 0) {
2223 cprintf("%d Floor %d not in use.\n",
2224 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
2227 if (flbuf.f_ref_count != 0) {
2228 cprintf("%d Cannot delete; floor contains %d rooms.\n",
2229 ERROR + INVALID_FLOOR_OPERATION,
2234 cprintf("%d Ok\n", CIT_OK);
2236 cprintf("%d Ok to delete...\n", CIT_OK);
2243 if ((delete_ok == 1) && (kflr_ok == 1))
2245 lputfloor(&flbuf, floor_to_delete);
2251 void cmd_eflr(char *argbuf)
2257 np = num_parms(argbuf);
2259 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
2263 if (CtdlAccessCheck(ac_aide)) return;
2265 floor_num = extract_int(argbuf, 0);
2266 lgetfloor(&flbuf, floor_num);
2267 if ((flbuf.f_flags & F_INUSE) == 0) {
2268 lputfloor(&flbuf, floor_num);
2269 cprintf("%d Floor %d is not in use.\n",
2270 ERROR + INVALID_FLOOR_OPERATION, floor_num);
2274 extract_token(flbuf.f_name, argbuf, 1, '|', sizeof flbuf.f_name);
2275 lputfloor(&flbuf, floor_num);
2277 cprintf("%d Ok\n", CIT_OK);
2283 * cmd_stat() - return the modification time of the current room (maybe other things in the future)
2285 void cmd_stat(char *gargs)
2287 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
2288 CtdlGetRoom(&CC->room, CC->room.QRname);
2289 cprintf("%d %s|%ld|\n", CIT_OK, CC->room.QRname, CC->room.QRmtime);
2294 /*****************************************************************************/
2295 /* MODULE INITIALIZATION STUFF */
2296 /*****************************************************************************/
2298 CTDL_MODULE_INIT(room_ops)
2301 CtdlRegisterProtoHook(cmd_lrms, "LRMS", "List rooms");
2302 CtdlRegisterProtoHook(cmd_lkra, "LKRA", "List all known rooms");
2303 CtdlRegisterProtoHook(cmd_lkrn, "LKRN", "List known rooms with new messages");
2304 CtdlRegisterProtoHook(cmd_lkro, "LKRO", "List known rooms without new messages");
2305 CtdlRegisterProtoHook(cmd_lzrm, "LZRM", "List zapped rooms");
2306 CtdlRegisterProtoHook(cmd_lprm, "LPRM", "List public rooms");
2307 CtdlRegisterProtoHook(cmd_goto, "GOTO", "Goto a named room");
2308 CtdlRegisterProtoHook(cmd_stat, "STAT", "Get mtime of the current room");
2309 CtdlRegisterProtoHook(cmd_whok, "WHOK", "List users who know this room");
2310 CtdlRegisterProtoHook(cmd_rdir, "RDIR", "List files in room directory");
2311 CtdlRegisterProtoHook(cmd_getr, "GETR", "Get room parameters");
2312 CtdlRegisterProtoHook(cmd_setr, "SETR", "Set room parameters");
2313 CtdlRegisterProtoHook(cmd_geta, "GETA", "Get the room aide name");
2314 CtdlRegisterProtoHook(cmd_seta, "SETA", "Set the room aide for this room");
2315 CtdlRegisterProtoHook(cmd_rinf, "RINF", "Fetch room info file");
2316 CtdlRegisterProtoHook(cmd_kill, "KILL", "Kill (delete) the current room");
2317 CtdlRegisterProtoHook(cmd_cre8, "CRE8", "Create a new room");
2318 CtdlRegisterProtoHook(cmd_einf, "EINF", "Enter info file for the current room");
2319 CtdlRegisterProtoHook(cmd_lflr, "LFLR", "List all known floors");
2320 CtdlRegisterProtoHook(cmd_cflr, "CFLR", "Create a new floor");
2321 CtdlRegisterProtoHook(cmd_kflr, "KFLR", "Kill a floor");
2322 CtdlRegisterProtoHook(cmd_eflr, "EFLR", "Edit a floor");
2324 /* return our Subversion id for the Log */