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, const char *room_name)
283 struct cdbdata *cdbqr;
284 char lowercase_name[ROOMNAMELEN];
285 char personal_lowercase_name[ROOMNAMELEN];
289 dptr = lowercase_name;
291 eptr = (dptr + (sizeof lowercase_name - 1));
292 while (!IsEmptyStr(sptr) && (dptr < eptr)){
293 *dptr = tolower(*sptr);
298 memset(qrbuf, 0, sizeof(struct ctdlroom));
300 /* First, try the public namespace */
301 cdbqr = cdb_fetch(CDB_ROOMS,
302 lowercase_name, strlen(lowercase_name));
304 /* If that didn't work, try the user's personal namespace */
306 snprintf(personal_lowercase_name,
307 sizeof personal_lowercase_name, "%010ld.%s",
308 CC->user.usernum, lowercase_name);
309 cdbqr = cdb_fetch(CDB_ROOMS,
310 personal_lowercase_name,
311 strlen(personal_lowercase_name));
314 memcpy(qrbuf, cdbqr->ptr,
315 ((cdbqr->len > sizeof(struct ctdlroom)) ?
316 sizeof(struct ctdlroom) : cdbqr->len));
319 room_sanity_check(qrbuf);
328 * CtdlGetRoomLock() - same as getroom() but locks the record (if supported)
330 int CtdlGetRoomLock(struct ctdlroom *qrbuf, char *room_name)
333 retval = CtdlGetRoom(qrbuf, room_name);
334 if (retval == 0) begin_critical_section(S_ROOMS);
340 * b_putroom() - back end to putroom() and b_deleteroom()
341 * (if the supplied buffer is NULL, delete the room record)
343 void b_putroom(struct ctdlroom *qrbuf, char *room_name)
345 char lowercase_name[ROOMNAMELEN];
350 bptr = lowercase_name;
351 while (!IsEmptyStr(aptr))
353 *bptr = tolower(*aptr);
359 len = bptr - lowercase_name;
361 cdb_delete(CDB_ROOMS, lowercase_name, len);
363 time(&qrbuf->QRmtime);
364 cdb_store(CDB_ROOMS, lowercase_name, len, qrbuf, sizeof(struct ctdlroom));
370 * CtdlPutRoom() - store room data to disk
372 void CtdlPutRoom(struct ctdlroom *qrbuf) {
373 b_putroom(qrbuf, qrbuf->QRname);
378 * b_deleteroom() - delete a room record from disk
380 void b_deleteroom(char *room_name) {
381 b_putroom(NULL, room_name);
386 * CtdlPutRoomLock() - same as CtdlPutRoom() but unlocks the record (if supported)
388 void CtdlPutRoomLock(struct ctdlroom *qrbuf)
392 end_critical_section(S_ROOMS);
398 * CtdlGetFloorByName() - retrieve the number of the named floor
399 * return < 0 if not found else return floor number
401 int CtdlGetFloorByName(const char *floor_name)
404 struct floor *flbuf = NULL;
406 for (a = 0; a < MAXFLOORS; ++a) {
407 flbuf = CtdlGetCachedFloor(a);
409 /* check to see if it already exists */
410 if ((!strcasecmp(flbuf->f_name, floor_name))
411 && (flbuf->f_flags & F_INUSE)) {
420 * CtdlGetFloorByNameLock() - retrieve floor number for given floor and lock the floor list.
422 int CtdlGetFloorByNameLock(const char *floor_name)
424 begin_critical_section(S_FLOORTAB);
425 return CtdlGetFloorByName(floor_name);
431 * CtdlGetAvailableFloor() - Return number of first unused floor
432 * return < 0 if none available
434 int CtdlGetAvailableFloor(void)
437 struct floor *flbuf = NULL;
439 for (a = 0; a < MAXFLOORS; a++) {
440 flbuf = CtdlGetCachedFloor(a);
442 /* check to see if it already exists */
443 if ((flbuf->f_flags & F_INUSE) == 0) {
452 * CtdlGetFloor() - retrieve floor data from disk
454 void CtdlGetFloor(struct floor *flbuf, int floor_num)
456 struct cdbdata *cdbfl;
458 memset(flbuf, 0, sizeof(struct floor));
459 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
461 memcpy(flbuf, cdbfl->ptr,
462 ((cdbfl->len > sizeof(struct floor)) ?
463 sizeof(struct floor) : cdbfl->len));
466 if (floor_num == 0) {
467 safestrncpy(flbuf->f_name, "Main Floor",
468 sizeof flbuf->f_name);
469 flbuf->f_flags = F_INUSE;
470 flbuf->f_ref_count = 3;
478 * lgetfloor() - same as CtdlGetFloor() but locks the record (if supported)
480 void lgetfloor(struct floor *flbuf, int floor_num)
483 begin_critical_section(S_FLOORTAB);
484 CtdlGetFloor(flbuf, floor_num);
489 * CtdlGetCachedFloor() - Get floor record from *cache* (loads from disk if needed)
491 * This is strictly a performance hack.
493 struct floor *CtdlGetCachedFloor(int floor_num) {
494 static int initialized = 0;
497 struct floor *fl = NULL;
499 begin_critical_section(S_FLOORCACHE);
500 if (initialized == 0) {
501 for (i=0; i<MAXFLOORS; ++i) {
502 floorcache[floor_num] = NULL;
506 if (floorcache[floor_num] == NULL) {
509 end_critical_section(S_FLOORCACHE);
512 fl = malloc(sizeof(struct floor));
513 CtdlGetFloor(fl, floor_num);
514 begin_critical_section(S_FLOORCACHE);
515 if (floorcache[floor_num] != NULL) {
516 free(floorcache[floor_num]);
518 floorcache[floor_num] = fl;
519 end_critical_section(S_FLOORCACHE);
522 return(floorcache[floor_num]);
527 * CtdlPutFloor() - store floor data on disk
529 void CtdlPutFloor(struct floor *flbuf, int floor_num)
531 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
532 begin_critical_section(S_FLOORCACHE);
533 if (floorcache[floor_num] != NULL) {
534 free(floorcache[floor_num]);
535 floorcache[floor_num] = malloc(sizeof(struct floor));
536 memcpy(floorcache[floor_num], flbuf, sizeof(struct floor));
538 end_critical_section(S_FLOORCACHE);
540 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
541 flbuf, sizeof(struct floor));
546 * CtdlPutFloorLock() - same as CtdlPutFloor() but unlocks the record (if supported)
548 void CtdlPutFloorLock(struct floor *flbuf, int floor_num)
551 CtdlPutFloor(flbuf, floor_num);
552 end_critical_section(S_FLOORTAB);
558 * lputfloor() - same as CtdlPutFloor() but unlocks the record (if supported)
560 void lputfloor(struct floor *flbuf, int floor_num)
562 CtdlPutFloorLock(flbuf, floor_num);
567 * Traverse the room file...
569 void CtdlForEachRoom(ForEachRoomCallBack CB, void *in_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) {
591 * Traverse the room file...
593 void CtdlForEachNetCfgRoom(ForEachRoomNetCfgCallBack CB,
597 struct ctdlroom qrbuf;
598 struct cdbdata *cdbqr;
600 cdb_rewind(CDB_ROOMS);
602 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
603 memset(&qrbuf, 0, sizeof(struct ctdlroom));
604 memcpy(&qrbuf, cdbqr->ptr,
605 ((cdbqr->len > sizeof(struct ctdlroom)) ?
606 sizeof(struct ctdlroom) : cdbqr->len)
609 room_sanity_check(&qrbuf);
610 if (qrbuf.QRflags & QR_INUSE)
612 OneRoomNetCfg* RNCfg;
613 RNCfg = CtdlGetNetCfgForRoom(qrbuf.QRnumber);
614 if ((RNCfg != NULL) &&
615 ((filter == maxRoomNetCfg) ||
616 (RNCfg->NetConfigs[filter] != NULL)))
618 CB(&qrbuf, in_data, RNCfg);
626 * delete_msglist() - delete room message pointers
628 void delete_msglist(struct ctdlroom *whichroom)
630 struct cdbdata *cdbml;
632 /* Make sure the msglist we're deleting actually exists, otherwise
633 * libdb will complain when we try to delete an invalid record
635 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
639 /* Go ahead and delete it */
640 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
646 * Message pointer compare function for sort_msglist()
648 int sort_msglist_cmp(const void *m1, const void *m2) {
649 if ((*(const long *)m1) > (*(const long *)m2)) return(1);
650 if ((*(const long *)m1) < (*(const long *)m2)) return(-1);
656 * sort message pointers
657 * (returns new msg count)
659 int sort_msglist(long listptrs[], int oldcount)
670 qsort(listptrs, numitems, sizeof(long), sort_msglist_cmp);
672 /* and yank any nulls */
673 while ((i < numitems) && (listptrs[i] == 0L)) i++;
677 memmove(&listptrs[0], &listptrs[i], (sizeof(long) * (numitems - i)));
686 * Determine whether a given room is non-editable.
688 int CtdlIsNonEditable(struct ctdlroom *qrbuf)
691 /* Mail> rooms are non-editable */
692 if ( (qrbuf->QRflags & QR_MAILBOX)
693 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
696 /* Everything else is editable */
702 * Back-back-end for all room listing commands
704 void list_roomname(struct ctdlroom *qrbuf, int ra, int current_view, int default_view)
706 char truncated_roomname[ROOMNAMELEN];
708 /* For my own mailbox rooms, chop off the owner prefix */
709 if ( (qrbuf->QRflags & QR_MAILBOX)
710 && (atol(qrbuf->QRname) == CC->user.usernum) ) {
711 safestrncpy(truncated_roomname, qrbuf->QRname, sizeof truncated_roomname);
712 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
713 cprintf("%s", truncated_roomname);
715 /* For all other rooms, just display the name in its entirety */
717 cprintf("%s", qrbuf->QRname);
720 /* ...and now the other parameters */
721 cprintf("|%u|%d|%d|%d|%d|%d|%d|%ld|\n",
723 (int) qrbuf->QRfloor,
724 (int) qrbuf->QRorder,
725 (int) qrbuf->QRflags2,
735 * cmd_lrms() - List all accessible rooms, known or forgotten
737 void cmd_lrms_backend(struct ctdlroom *qrbuf, void *data)
739 int FloorBeingSearched = (-1);
743 FloorBeingSearched = *(int *)data;
744 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
746 if ((( ra & (UA_KNOWN | UA_ZAPPED)))
747 && ((qrbuf->QRfloor == (FloorBeingSearched))
748 || ((FloorBeingSearched) < 0)))
749 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
752 void cmd_lrms(char *argbuf)
754 int FloorBeingSearched = (-1);
755 if (!IsEmptyStr(argbuf))
756 FloorBeingSearched = extract_int(argbuf, 0);
758 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
760 CtdlGetUser(&CC->user, CC->curr_user);
761 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
763 CtdlForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
770 * cmd_lkra() - List all known rooms
772 void cmd_lkra_backend(struct ctdlroom *qrbuf, void *data)
774 int FloorBeingSearched = (-1);
778 FloorBeingSearched = *(int *)data;
779 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
781 if ((( ra & (UA_KNOWN)))
782 && ((qrbuf->QRfloor == (FloorBeingSearched))
783 || ((FloorBeingSearched) < 0)))
784 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
787 void cmd_lkra(char *argbuf)
789 int FloorBeingSearched = (-1);
790 if (!IsEmptyStr(argbuf))
791 FloorBeingSearched = extract_int(argbuf, 0);
793 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
795 CtdlGetUser(&CC->user, CC->curr_user);
796 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
798 CtdlForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
804 void cmd_lprm_backend(struct ctdlroom *qrbuf, void *data)
806 int FloorBeingSearched = (-1);
810 FloorBeingSearched = *(int *)data;
811 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
813 if ( ((qrbuf->QRflags & QR_PRIVATE) == 0)
814 && ((qrbuf->QRflags & QR_MAILBOX) == 0)
815 && ((qrbuf->QRfloor == (FloorBeingSearched))
816 || ((FloorBeingSearched) < 0)))
817 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
820 void cmd_lprm(char *argbuf)
822 int FloorBeingSearched = (-1);
823 if (!IsEmptyStr(argbuf))
824 FloorBeingSearched = extract_int(argbuf, 0);
826 cprintf("%d Public rooms:\n", LISTING_FOLLOWS);
828 CtdlForEachRoom(cmd_lprm_backend, &FloorBeingSearched);
835 * cmd_lkrn() - List all known rooms with new messages
837 void cmd_lkrn_backend(struct ctdlroom *qrbuf, void *data)
839 int FloorBeingSearched = (-1);
843 FloorBeingSearched = *(int *)data;
844 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
847 && (ra & UA_HASNEWMSGS)
848 && ((qrbuf->QRfloor == (FloorBeingSearched))
849 || ((FloorBeingSearched) < 0)))
850 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
853 void cmd_lkrn(char *argbuf)
855 int FloorBeingSearched = (-1);
856 if (!IsEmptyStr(argbuf))
857 FloorBeingSearched = extract_int(argbuf, 0);
859 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
861 CtdlGetUser(&CC->user, CC->curr_user);
862 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
864 CtdlForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
871 * cmd_lkro() - List all known rooms
873 void cmd_lkro_backend(struct ctdlroom *qrbuf, void *data)
875 int FloorBeingSearched = (-1);
879 FloorBeingSearched = *(int *)data;
880 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
883 && ((ra & UA_HASNEWMSGS) == 0)
884 && ((qrbuf->QRfloor == (FloorBeingSearched))
885 || ((FloorBeingSearched) < 0)))
886 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
889 void cmd_lkro(char *argbuf)
891 int FloorBeingSearched = (-1);
892 if (!IsEmptyStr(argbuf))
893 FloorBeingSearched = extract_int(argbuf, 0);
895 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
897 CtdlGetUser(&CC->user, CC->curr_user);
898 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
900 CtdlForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
907 * cmd_lzrm() - List all forgotten rooms
909 void cmd_lzrm_backend(struct ctdlroom *qrbuf, void *data)
911 int FloorBeingSearched = (-1);
915 FloorBeingSearched = *(int *)data;
916 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
918 if ((ra & UA_GOTOALLOWED)
920 && ((qrbuf->QRfloor == (FloorBeingSearched))
921 || ((FloorBeingSearched) < 0)))
922 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
925 void cmd_lzrm(char *argbuf)
927 int FloorBeingSearched = (-1);
928 if (!IsEmptyStr(argbuf))
929 FloorBeingSearched = extract_int(argbuf, 0);
931 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
933 CtdlGetUser(&CC->user, CC->curr_user);
934 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
936 CtdlForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
942 * Make the specified room the current room for this session. No validation
943 * or access control is done here -- the caller should make sure that the
944 * specified room exists and is ok to access.
946 void CtdlUserGoto(char *where, int display_result, int transiently,
947 int *retmsgs, int *retnew)
949 struct CitContext *CCC = CC;
951 int new_messages = 0;
952 int old_messages = 0;
953 int total_messages = 0;
957 int newmailcount = 0;
959 char truncated_roomname[ROOMNAMELEN];
960 struct cdbdata *cdbfr;
961 long *msglist = NULL;
963 unsigned int original_v_flags;
966 char setstr[128], lostr[64], histr[64];
970 /* If the supplied room name is NULL, the caller wants us to know that
971 * it has already copied the room record into CC->room, so
972 * we can skip the extra database fetch.
975 safestrncpy(CCC->room.QRname, where, sizeof CCC->room.QRname);
976 CtdlGetRoom(&CCC->room, where);
979 /* Take care of all the formalities. */
981 begin_critical_section(S_USERS);
982 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
983 original_v_flags = vbuf.v_flags;
985 /* Know the room ... but not if it's the page log room, or if the
986 * caller specified that we're only entering this room transiently.
988 if ((strcasecmp(CCC->room.QRname, config.c_logpages))
989 && (transiently == 0) ) {
990 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
991 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
994 /* Only rewrite the database record if we changed something */
995 if (vbuf.v_flags != original_v_flags) {
996 CtdlSetRelationship(&vbuf, &CCC->user, &CCC->room);
998 end_critical_section(S_USERS);
1000 /* Check for new mail */
1001 newmailcount = NewMailCount();
1003 /* set info to 1 if the user needs to read the room's info file */
1004 if (CCC->room.QRinfo > vbuf.v_lastseen) {
1008 cdbfr = cdb_fetch(CDB_MSGLISTS, &CCC->room.QRnumber, sizeof(long));
1009 if (cdbfr != NULL) {
1010 msglist = (long *) cdbfr->ptr;
1011 cdbfr->ptr = NULL; /* CtdlUserGoto() now owns this memory */
1012 num_msgs = cdbfr->len / sizeof(long);
1017 for (a=0; a<num_msgs; ++a) {
1018 if (msglist[a] > 0L) ++total_messages;
1021 num_sets = num_tokens(vbuf.v_seen, ',');
1022 for (s=0; s<num_sets; ++s) {
1023 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
1025 extract_token(lostr, setstr, 0, ':', sizeof lostr);
1026 if (num_tokens(setstr, ':') >= 2) {
1027 extract_token(histr, setstr, 1, ':', sizeof histr);
1028 if (!strcmp(histr, "*")) {
1029 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
1033 strcpy(histr, lostr);
1038 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
1039 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
1045 new_messages = total_messages - old_messages;
1047 if (msglist != NULL) free(msglist);
1049 if (CCC->room.QRflags & QR_MAILBOX)
1054 if ((CCC->room.QRroomaide == CCC->user.usernum)
1055 || (CCC->user.axlevel >= AxAideU))
1060 safestrncpy(truncated_roomname, CCC->room.QRname, sizeof truncated_roomname);
1061 if ( (CCC->room.QRflags & QR_MAILBOX)
1062 && (atol(CCC->room.QRname) == CCC->user.usernum) ) {
1063 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
1066 if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
1070 if (retmsgs != NULL) *retmsgs = total_messages;
1071 if (retnew != NULL) *retnew = new_messages;
1072 MSG_syslog(LOG_INFO, "<%s> %d new of %d total messages\n",
1074 new_messages, total_messages
1077 CCC->curr_view = (int)vbuf.v_view;
1079 if (display_result) {
1080 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|%d|%ld|\n",
1081 CIT_OK, CtdlCheckExpress(),
1084 (int)total_messages,
1086 (int)CCC->room.QRflags,
1087 (long)CCC->room.QRhighest,
1088 (long)vbuf.v_lastseen,
1092 (int)CCC->room.QRfloor,
1094 (int)CCC->room.QRdefaultview,
1096 (int)CCC->room.QRflags2,
1097 (long)CCC->room.QRmtime
1104 * Handle some of the macro named rooms
1106 void convert_room_name_macros(char *towhere, size_t maxlen) {
1107 if (!strcasecmp(towhere, "_BASEROOM_")) {
1108 safestrncpy(towhere, config.c_baseroom, maxlen);
1110 else if (!strcasecmp(towhere, "_MAIL_")) {
1111 safestrncpy(towhere, MAILROOM, maxlen);
1113 else if (!strcasecmp(towhere, "_TRASH_")) {
1114 safestrncpy(towhere, USERTRASHROOM, maxlen);
1116 else if (!strcasecmp(towhere, "_DRAFTS_")) {
1117 safestrncpy(towhere, USERDRAFTROOM, maxlen);
1119 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
1120 safestrncpy(towhere, config.c_twitroom, maxlen);
1122 else if (!strcasecmp(towhere, "_CALENDAR_")) {
1123 safestrncpy(towhere, USERCALENDARROOM, maxlen);
1125 else if (!strcasecmp(towhere, "_TASKS_")) {
1126 safestrncpy(towhere, USERTASKSROOM, maxlen);
1128 else if (!strcasecmp(towhere, "_CONTACTS_")) {
1129 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
1131 else if (!strcasecmp(towhere, "_NOTES_")) {
1132 safestrncpy(towhere, USERNOTESROOM, maxlen);
1138 * cmd_goto() - goto a new room
1140 void cmd_goto(char *gargs)
1142 struct ctdlroom QRscratch;
1146 char augmented_roomname[ROOMNAMELEN];
1147 char towhere[ROOMNAMELEN];
1149 int transiently = 0;
1151 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
1153 extract_token(towhere, gargs, 0, '|', sizeof towhere);
1154 extract_token(password, gargs, 1, '|', sizeof password);
1155 transiently = extract_int(gargs, 2);
1157 CtdlGetUser(&CC->user, CC->curr_user);
1160 * Handle some of the macro named rooms
1162 convert_room_name_macros(towhere, sizeof towhere);
1164 /* First try a regular match */
1165 c = CtdlGetRoom(&QRscratch, towhere);
1167 /* Then try a mailbox name match */
1169 CtdlMailboxName(augmented_roomname, sizeof augmented_roomname,
1170 &CC->user, towhere);
1171 c = CtdlGetRoom(&QRscratch, augmented_roomname);
1173 safestrncpy(towhere, augmented_roomname, sizeof towhere);
1176 /* And if the room was found... */
1179 /* Let internal programs go directly to any room. */
1180 if (CC->internal_pgm) {
1181 memcpy(&CC->room, &QRscratch,
1182 sizeof(struct ctdlroom));
1183 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1187 /* See if there is an existing user/room relationship */
1188 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
1190 /* normal clients have to pass through security */
1191 if (ra & UA_GOTOALLOWED) {
1196 if ((QRscratch.QRflags & QR_MAILBOX) &&
1197 ((ra & UA_GOTOALLOWED))) {
1198 memcpy(&CC->room, &QRscratch,
1199 sizeof(struct ctdlroom));
1200 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1202 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
1203 ((ra & UA_KNOWN) == 0) &&
1204 (strcasecmp(QRscratch.QRpasswd, password)) &&
1205 (CC->user.axlevel < AxAideU)
1207 cprintf("%d wrong or missing passwd\n",
1208 ERROR + PASSWORD_REQUIRED);
1210 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
1211 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
1212 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
1213 ((ra & UA_KNOWN) == 0) &&
1214 (CC->user.axlevel < AxAideU)
1216 syslog(LOG_DEBUG, "Failed to acquire private room\n");
1218 memcpy(&CC->room, &QRscratch,
1219 sizeof(struct ctdlroom));
1220 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1226 cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
1230 void cmd_whok(char *cmdbuf)
1232 struct ctdluser temp;
1233 struct cdbdata *cdbus;
1236 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
1237 cdb_rewind(CDB_USERS);
1238 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1239 memset(&temp, 0, sizeof temp);
1240 memcpy(&temp, cdbus->ptr, sizeof temp);
1243 CtdlRoomAccess(&CC->room, &temp, &ra, NULL);
1244 if ((!IsEmptyStr(temp.fullname)) &&
1245 (CC->room.QRflags & QR_INUSE) &&
1248 cprintf("%s\n", temp.fullname);
1255 * RDIR command for room directory
1257 void cmd_rdir(char *cmdbuf)
1262 struct stat statbuf;
1263 DIR *filedir = NULL;
1264 struct dirent *filedir_entry;
1270 if (CtdlAccessCheck(ac_logged_in)) return;
1272 CtdlGetRoom(&CC->room, CC->room.QRname);
1273 CtdlGetUser(&CC->user, CC->curr_user);
1275 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
1276 cprintf("%d not here.\n", ERROR + NOT_HERE);
1279 if (((CC->room.QRflags & QR_VISDIR) == 0)
1280 && (CC->user.axlevel < AxAideU)
1281 && (CC->user.usernum != CC->room.QRroomaide)) {
1282 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1286 snprintf(buf, sizeof buf, "%s/%s", ctdl_file_dir, CC->room.QRdirname);
1287 filedir = opendir (buf);
1289 if (filedir == NULL) {
1290 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1293 cprintf("%d %s|%s/%s\n", LISTING_FOLLOWS, config.c_fqdn, ctdl_file_dir, CC->room.QRdirname);
1295 snprintf(buf, sizeof buf, "%s/%s/filedir", ctdl_file_dir, CC->room.QRdirname);
1296 fd = fopen(buf, "r");
1298 fd = fopen("/dev/null", "r");
1299 while ((filedir_entry = readdir(filedir)))
1301 if (strcasecmp(filedir_entry->d_name, "filedir") && filedir_entry->d_name[0] != '.')
1303 #ifdef _DIRENT_HAVE_D_NAMELEN
1304 d_namelen = filedir_entry->d_namelen;
1306 d_namelen = strlen(filedir_entry->d_name);
1308 snprintf(buf, sizeof buf, "%s/%s/%s", ctdl_file_dir, CC->room.QRdirname, filedir_entry->d_name);
1309 stat(buf, &statbuf); /* stat the file */
1310 if (!(statbuf.st_mode & S_IFREG))
1312 snprintf(buf2, sizeof buf2,
1313 "\"%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",
1314 buf, CC->room.QRname
1316 CtdlAideMessage(buf2, "Unusable data found in room directory");
1317 continue; /* not a useable file type so don't show it */
1319 safestrncpy(comment, "", sizeof comment);
1320 fseek(fd, 0L, 0); /* rewind descriptions file */
1321 /* Get the description from the descriptions file */
1322 while ((fgets(buf, sizeof buf, fd) != NULL) && (IsEmptyStr(comment)))
1324 buf[strlen(buf) - 1] = 0;
1325 if ((!strncasecmp(buf, filedir_entry->d_name, d_namelen)) && (buf[d_namelen] == ' '))
1326 safestrncpy(comment, &buf[d_namelen + 1], sizeof comment);
1328 len = extract_token (mimebuf, comment, 0,' ', 64);
1329 if ((len <0) || strchr(mimebuf, '/') == NULL)
1331 snprintf (mimebuf, 64, "application/octetstream");
1334 cprintf("%s|%ld|%s|%s\n",
1335 filedir_entry->d_name,
1336 (long)statbuf.st_size,
1348 * get room parameters (admin or room admin command)
1350 void cmd_getr(char *cmdbuf)
1352 if (CtdlAccessCheck(ac_room_aide)) return;
1354 CtdlGetRoom(&CC->room, CC->room.QRname);
1355 cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1359 ((CC->room.QRflags & QR_MAILBOX) ?
1360 &CC->room.QRname[11] : CC->room.QRname),
1362 ((CC->room.QRflags & QR_PASSWORDED) ?
1363 CC->room.QRpasswd : ""),
1365 ((CC->room.QRflags & QR_DIRECTORY) ?
1366 CC->room.QRdirname : ""),
1369 (int) CC->room.QRfloor,
1370 (int) CC->room.QRorder,
1372 CC->room.QRdefaultview,
1379 * Back end function to rename a room.
1380 * You can also specify which floor to move the room to, or specify -1 to
1381 * keep the room on the same floor it was on.
1383 * If you are renaming a mailbox room, you must supply the namespace prefix
1384 * in *at least* the old name!
1386 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1388 struct ctdlroom qrbuf;
1389 struct ctdlroom qrtmp;
1394 char actual_old_name[ROOMNAMELEN];
1396 syslog(LOG_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1397 old_name, new_name, new_floor);
1399 if (new_floor >= 0) {
1400 fl = CtdlGetCachedFloor(new_floor);
1401 if ((fl->f_flags & F_INUSE) == 0) {
1402 return(crr_invalid_floor);
1406 begin_critical_section(S_ROOMS);
1408 if ( (CtdlGetRoom(&qrtmp, new_name) == 0)
1409 && (strcasecmp(new_name, old_name)) ) {
1410 ret = crr_already_exists;
1413 else if (CtdlGetRoom(&qrbuf, old_name) != 0) {
1414 ret = crr_room_not_found;
1417 else if ( (CC->user.axlevel < AxAideU) && (!CC->internal_pgm)
1418 && (CC->user.usernum != qrbuf.QRroomaide)
1419 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) ) {
1420 ret = crr_access_denied;
1423 else if (CtdlIsNonEditable(&qrbuf)) {
1424 ret = crr_noneditable;
1429 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1430 if (qrbuf.QRflags & QR_MAILBOX) {
1431 owner = atol(qrbuf.QRname);
1433 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1434 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1435 "%010ld.%s", owner, new_name);
1438 safestrncpy(qrbuf.QRname, new_name,
1439 sizeof(qrbuf.QRname));
1442 /* Reject change of floor for baseroom/aideroom */
1443 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1444 !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1448 /* Take care of floor stuff */
1449 old_floor = qrbuf.QRfloor;
1450 if (new_floor < 0) {
1451 new_floor = old_floor;
1453 qrbuf.QRfloor = new_floor;
1454 CtdlPutRoom(&qrbuf);
1456 begin_critical_section(S_CONFIG);
1458 /* If baseroom/aideroom name changes, update config */
1459 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1460 safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1463 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1464 safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1468 end_critical_section(S_CONFIG);
1470 /* If the room name changed, then there are now two room
1471 * records, so we have to delete the old one.
1473 if (strcasecmp(new_name, old_name)) {
1474 b_deleteroom(actual_old_name);
1480 end_critical_section(S_ROOMS);
1482 /* Adjust the floor reference counts if necessary */
1483 if (new_floor != old_floor) {
1484 lgetfloor(&flbuf, old_floor);
1485 --flbuf.f_ref_count;
1486 lputfloor(&flbuf, old_floor);
1487 syslog(LOG_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1488 lgetfloor(&flbuf, new_floor);
1489 ++flbuf.f_ref_count;
1490 lputfloor(&flbuf, new_floor);
1491 syslog(LOG_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1494 /* ...and everybody say "YATTA!" */
1500 * set room parameters (admin or room admin command)
1502 void cmd_setr(char *args)
1508 char new_name[ROOMNAMELEN];
1510 if (CtdlAccessCheck(ac_logged_in)) return;
1512 if (num_parms(args) >= 6) {
1513 new_floor = extract_int(args, 5);
1515 new_floor = (-1); /* don't change the floor */
1518 /* When is a new name more than just a new name? When the old name
1519 * has a namespace prefix.
1521 if (CC->room.QRflags & QR_MAILBOX) {
1522 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
1524 safestrncpy(new_name, "", sizeof new_name);
1526 extract_token(&new_name[strlen(new_name)], args, 0, '|', (sizeof new_name - strlen(new_name)));
1528 r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
1530 if (r == crr_room_not_found) {
1531 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
1532 } else if (r == crr_already_exists) {
1533 cprintf("%d '%s' already exists.\n",
1534 ERROR + ALREADY_EXISTS, new_name);
1535 } else if (r == crr_noneditable) {
1536 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
1537 } else if (r == crr_invalid_floor) {
1538 cprintf("%d Target floor does not exist.\n",
1539 ERROR + INVALID_FLOOR_OPERATION);
1540 } else if (r == crr_access_denied) {
1541 cprintf("%d You do not have permission to edit '%s'\n",
1542 ERROR + HIGHER_ACCESS_REQUIRED,
1544 } else if (r != crr_ok) {
1545 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1546 ERROR + INTERNAL_ERROR, r);
1553 CtdlGetRoom(&CC->room, new_name);
1555 /* Now we have to do a bunch of other stuff */
1557 if (num_parms(args) >= 7) {
1558 new_order = extract_int(args, 6);
1561 if (new_order > 127)
1565 CtdlGetRoomLock(&CC->room, CC->room.QRname);
1567 /* Directory room */
1568 extract_token(buf, args, 2, '|', sizeof buf);
1570 safestrncpy(CC->room.QRdirname, buf,
1571 sizeof CC->room.QRdirname);
1574 if (num_parms(args) >= 8) {
1575 CC->room.QRdefaultview = extract_int(args, 7);
1578 /* Second set of flags */
1579 if (num_parms(args) >= 9) {
1580 CC->room.QRflags2 = extract_int(args, 8);
1584 CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
1585 /* Clean up a client boo-boo: if the client set the room to
1586 * guess-name or passworded, ensure that the private flag is
1589 if ((CC->room.QRflags & QR_GUESSNAME)
1590 || (CC->room.QRflags & QR_PASSWORDED))
1591 CC->room.QRflags |= QR_PRIVATE;
1593 /* Some changes can't apply to BASEROOM */
1594 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1596 CC->room.QRorder = 0;
1597 CC->room.QRpasswd[0] = '\0';
1598 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1599 QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1600 CC->room.QRflags |= QR_PERMANENT;
1602 /* March order (doesn't apply to AIDEROOM) */
1603 if (num_parms(args) >= 7)
1604 CC->room.QRorder = (char) new_order;
1606 extract_token(buf, args, 1, '|', sizeof buf);
1608 safestrncpy(CC->room.QRpasswd, buf,
1609 sizeof CC->room.QRpasswd);
1610 /* Kick everyone out if the client requested it
1611 * (by changing the room's generation number)
1613 if (extract_int(args, 4)) {
1614 time(&CC->room.QRgen);
1617 /* Some changes can't apply to AIDEROOM */
1618 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1620 CC->room.QRorder = 0;
1621 CC->room.QRflags &= ~QR_MAILBOX;
1622 CC->room.QRflags |= QR_PERMANENT;
1625 /* Write the room record back to disk */
1626 CtdlPutRoomLock(&CC->room);
1628 /* Create a room directory if necessary */
1629 if (CC->room.QRflags & QR_DIRECTORY) {
1630 snprintf(buf, sizeof buf,"%s/%s",
1632 CC->room.QRdirname);
1635 snprintf(buf, sizeof buf, "The room \"%s\" has been edited by %s.\n",
1637 (CC->logged_in ? CC->curr_user : "an administrator")
1639 CtdlAideMessage(buf, "Room modification Message");
1640 cprintf("%d Ok\n", CIT_OK);
1646 * get the name of the room admin for this room
1648 void cmd_geta(char *cmdbuf)
1650 struct ctdluser usbuf;
1652 if (CtdlAccessCheck(ac_logged_in)) return;
1654 if (CtdlGetUserByNumber(&usbuf, CC->room.QRroomaide) == 0) {
1655 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1657 cprintf("%d \n", CIT_OK);
1663 * set the room admin for this room
1665 void cmd_seta(char *new_ra)
1667 struct ctdluser usbuf;
1672 if (CtdlAccessCheck(ac_room_aide)) return;
1674 if (CtdlGetUser(&usbuf, new_ra) != 0) {
1677 newu = usbuf.usernum;
1680 CtdlGetRoomLock(&CC->room, CC->room.QRname);
1682 if (CC->room.QRroomaide != newu) {
1685 CC->room.QRroomaide = newu;
1686 CtdlPutRoomLock(&CC->room);
1689 * We have to post the change notice _after_ writing changes to
1690 * the room table, otherwise it would deadlock!
1692 if (post_notice == 1) {
1693 if (!IsEmptyStr(usbuf.fullname))
1694 snprintf(buf, sizeof buf,
1695 "%s is now the room admin for \"%s\".\n",
1696 usbuf.fullname, CC->room.QRname);
1698 snprintf(buf, sizeof buf,
1699 "There is now no room admin for \"%s\".\n",
1701 CtdlAideMessage(buf, "Admin Room Modification");
1703 cprintf("%d Ok\n", CIT_OK);
1707 * retrieve info file for this room
1709 void cmd_rinf(char *gargs)
1711 char filename[PATH_MAX];
1715 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_info_dir);
1716 info_fp = fopen(filename, "r");
1718 if (info_fp == NULL) {
1719 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
1722 cprintf("%d Info:\n", LISTING_FOLLOWS);
1723 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1724 if (!IsEmptyStr(buf))
1725 buf[strlen(buf) - 1] = 0;
1726 cprintf("%s\n", buf);
1733 * Asynchronously schedule a room for deletion. The room will appear
1734 * deleted to the user(s), but it won't actually get purged from the
1735 * database until THE DREADED AUTO-PURGER makes its next run.
1737 void CtdlScheduleRoomForDeletion(struct ctdlroom *qrbuf)
1739 char old_name[ROOMNAMELEN];
1742 syslog(LOG_NOTICE, "Scheduling room <%s> for deletion\n",
1745 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1747 CtdlGetRoom(qrbuf, qrbuf->QRname);
1749 /* Turn the room into a private mailbox owned by a user who doesn't
1750 * exist. This will immediately make the room invisible to everyone,
1751 * and qualify the room for purging.
1753 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1758 qrbuf->QRflags |= QR_MAILBOX;
1759 time(&qrbuf->QRgen); /* Use a timestamp as the new generation number */
1763 b_deleteroom(old_name);
1769 * Back end processing to delete a room and everything associated with it
1770 * (This one is synchronous and should only get called by THE DREADED
1771 * AUTO-PURGER in serv_expire.c. All user-facing code should call
1772 * the asynchronous schedule_room_for_deletion() instead.)
1774 void CtdlDeleteRoom(struct ctdlroom *qrbuf)
1778 /* TODO: filename magic? does this realy work? */
1780 syslog(LOG_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1782 /* Delete the info file */
1783 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_info_dir);
1786 /* Delete the image file */
1787 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_image_dir);
1790 /* Delete the room's network config file */
1791 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1794 /* Delete the messages in the room
1795 * (Careful: this opens an S_ROOMS critical section!)
1797 CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
1799 /* Flag the room record as not in use */
1800 CtdlGetRoomLock(qrbuf, qrbuf->QRname);
1802 CtdlPutRoomLock(qrbuf);
1804 /* then decrement the reference count for the floor */
1805 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1806 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1807 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1809 /* Delete the room record from the database! */
1810 b_deleteroom(qrbuf->QRname);
1816 * Check access control for deleting a room
1818 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1820 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1824 if (CtdlIsNonEditable(qr)) {
1829 * For mailboxes, check stuff
1831 if (qr->QRflags & QR_MAILBOX) {
1833 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1835 if (atol(qr->QRname) != CC->user.usernum) {
1836 return(0); /* not my room */
1839 /* Can't delete your Mail> room */
1840 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1842 /* Otherwise it's ok */
1847 * For normal rooms, just check for admin or room admin status.
1849 return(is_room_aide());
1853 * admin command: kill the current room
1855 void cmd_kill(char *argbuf)
1857 char deleted_room_name[ROOMNAMELEN];
1861 kill_ok = extract_int(argbuf, 0);
1863 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
1864 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1868 if (CC->room.QRflags & QR_MAILBOX) {
1869 safestrncpy(deleted_room_name, &CC->room.QRname[11], sizeof deleted_room_name);
1872 safestrncpy(deleted_room_name, CC->room.QRname, sizeof deleted_room_name);
1875 /* Do the dirty work */
1876 CtdlScheduleRoomForDeletion(&CC->room);
1878 /* Return to the Lobby */
1879 CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
1881 /* tell the world what we did */
1882 snprintf(msg, sizeof msg, "The room \"%s\" has been deleted by %s.\n",
1884 (CC->logged_in ? CC->curr_user : "an administrator")
1886 CtdlAideMessage(msg, "Room Purger Message");
1887 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1889 cprintf("%d ok to delete.\n", CIT_OK);
1895 * Internal code to create a new room (returns room flags)
1897 * Room types: 0=public, 1=hidden, 2=passworded, 3=invitation-only,
1898 * 4=mailbox, 5=mailbox, but caller supplies namespace
1900 unsigned CtdlCreateRoom(char *new_room_name,
1902 char *new_room_pass,
1909 struct ctdlroom qrbuf;
1913 syslog(LOG_DEBUG, "CtdlCreateRoom(name=%s, type=%d, view=%d)\n",
1914 new_room_name, new_room_type, new_room_view);
1916 if (CtdlGetRoom(&qrbuf, new_room_name) == 0) {
1917 syslog(LOG_DEBUG, "%s already exists.\n", new_room_name);
1921 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1922 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1923 qrbuf.QRflags = QR_INUSE;
1924 if (new_room_type > 0)
1925 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1926 if (new_room_type == 1)
1927 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1928 if (new_room_type == 2)
1929 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1930 if ( (new_room_type == 4) || (new_room_type == 5) ) {
1931 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1932 /* qrbuf.QRflags2 |= QR2_SUBJECTREQ; */
1935 /* If the user is requesting a personal room, set up the room
1936 * name accordingly (prepend the user number)
1938 if (new_room_type == 4) {
1939 CtdlMailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1942 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1945 /* If the room is private, and the system administrator has elected
1946 * to automatically grant room admin privileges, do so now.
1948 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1949 qrbuf.QRroomaide = CC->user.usernum;
1951 /* Blog owners automatically become room admins of their blogs.
1952 * (In the future we will offer a site-wide configuration setting to suppress this behavior.)
1954 else if (new_room_view == VIEW_BLOG) {
1955 qrbuf.QRroomaide = CC->user.usernum;
1957 /* Otherwise, set the room admin to undefined.
1960 qrbuf.QRroomaide = (-1L);
1964 * If the caller is only interested in testing whether this will work,
1965 * return now without creating the room.
1967 if (!really_create) return (qrbuf.QRflags);
1969 qrbuf.QRnumber = get_new_room_number();
1970 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1971 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1972 qrbuf.QRfloor = new_room_floor;
1973 qrbuf.QRdefaultview = new_room_view;
1975 /* save what we just did... */
1976 CtdlPutRoom(&qrbuf);
1978 /* bump the reference count on whatever floor the room is on */
1979 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1980 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1981 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1983 /* Grant the creator access to the room unless the avoid_access
1984 * parameter was specified.
1986 if ( (CC->logged_in) && (avoid_access == 0) ) {
1987 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1988 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1989 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1990 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1993 /* resume our happy day */
1994 return (qrbuf.QRflags);
2001 void cmd_cre8(char *args)
2004 char new_room_name[ROOMNAMELEN];
2006 char new_room_pass[32];
2009 char *notification_message = NULL;
2012 int avoid_access = 0;
2014 cre8_ok = extract_int(args, 0);
2015 extract_token(new_room_name, args, 1, '|', sizeof new_room_name);
2016 new_room_name[ROOMNAMELEN - 1] = 0;
2017 new_room_type = extract_int(args, 2);
2018 extract_token(new_room_pass, args, 3, '|', sizeof new_room_pass);
2019 avoid_access = extract_int(args, 5);
2020 new_room_view = extract_int(args, 6);
2021 new_room_pass[9] = 0;
2024 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 1)) {
2025 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
2029 if (!strcasecmp(new_room_name, MAILROOM)) {
2030 cprintf("%d '%s' already exists.\n",
2031 ERROR + ALREADY_EXISTS, new_room_name);
2035 if (num_parms(args) >= 5) {
2036 fl = CtdlGetCachedFloor(extract_int(args, 4));
2038 cprintf("%d Invalid floor number.\n",
2039 ERROR + INVALID_FLOOR_OPERATION);
2042 else if ((fl->f_flags & F_INUSE) == 0) {
2043 cprintf("%d Invalid floor number.\n",
2044 ERROR + INVALID_FLOOR_OPERATION);
2047 new_room_floor = extract_int(args, 4);
2051 if (CtdlAccessCheck(ac_logged_in)) return;
2053 if (CC->user.axlevel < config.c_createax && !CC->internal_pgm) {
2054 cprintf("%d You need higher access to create rooms.\n",
2055 ERROR + HIGHER_ACCESS_REQUIRED);
2059 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 0)) {
2060 cprintf("%d Ok to create rooms.\n", CIT_OK);
2064 if ((new_room_type < 0) || (new_room_type > 5)) {
2065 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
2069 if (new_room_type == 5) {
2070 if (CC->user.axlevel < AxAideU) {
2071 cprintf("%d Higher access required\n",
2072 ERROR + HIGHER_ACCESS_REQUIRED);
2077 /* Check to make sure the requested room name doesn't already exist */
2078 newflags = CtdlCreateRoom(new_room_name,
2079 new_room_type, new_room_pass, new_room_floor,
2080 0, avoid_access, new_room_view);
2081 if (newflags == 0) {
2082 cprintf("%d '%s' already exists.\n",
2083 ERROR + ALREADY_EXISTS, new_room_name);
2088 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
2092 /* If we reach this point, the room needs to be created. */
2094 newflags = CtdlCreateRoom(new_room_name,
2095 new_room_type, new_room_pass, new_room_floor, 1, 0,
2098 /* post a message in Aide> describing the new room */
2099 notification_message = malloc(1024);
2100 snprintf(notification_message, 1024,
2101 "A new room called \"%s\" has been created by %s%s%s%s%s%s\n",
2103 (CC->logged_in ? CC->curr_user : "an administrator"),
2104 ((newflags & QR_MAILBOX) ? " [personal]" : ""),
2105 ((newflags & QR_PRIVATE) ? " [private]" : ""),
2106 ((newflags & QR_GUESSNAME) ? " [hidden]" : ""),
2107 ((newflags & QR_PASSWORDED) ? " Password: " : ""),
2108 ((newflags & QR_PASSWORDED) ? new_room_pass : "")
2110 CtdlAideMessage(notification_message, "Room Creation Message");
2111 free(notification_message);
2113 cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
2118 void cmd_einf(char *ok)
2119 { /* enter info file for current room */
2121 char infofilename[SIZ];
2126 if (CtdlAccessCheck(ac_room_aide)) return;
2128 if (atoi(ok) == 0) {
2129 cprintf("%d Ok.\n", CIT_OK);
2132 assoc_file_name(infofilename, sizeof infofilename, &CC->room, ctdl_info_dir);
2133 syslog(LOG_DEBUG, "opening\n");
2134 fp = fopen(infofilename, "w");
2135 syslog(LOG_DEBUG, "checking\n");
2137 cprintf("%d Cannot open %s: %s\n",
2138 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
2141 cprintf("%d Send info...\n", SEND_LISTING);
2144 client_getln(buf, sizeof buf);
2145 if (strcmp(buf, "000"))
2146 fprintf(fp, "%s\n", buf);
2147 } while (strcmp(buf, "000"));
2150 /* now update the room index so people will see our new info */
2151 CtdlGetRoomLock(&CC->room, CC->room.QRname); /* lock so no one steps on us */
2152 CC->room.QRinfo = CC->room.QRhighest + 1L;
2153 CtdlPutRoomLock(&CC->room);
2158 * cmd_lflr() - List all known floors
2160 void cmd_lflr(char *gargs)
2165 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
2167 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
2169 for (a = 0; a < MAXFLOORS; ++a) {
2170 CtdlGetFloor(&flbuf, a);
2171 if (flbuf.f_flags & F_INUSE) {
2172 cprintf("%d|%s|%d\n",
2184 * create a new floor
2186 void cmd_cflr(char *argbuf)
2188 char new_floor_name[256];
2191 int free_slot = (-1);
2194 extract_token(new_floor_name, argbuf, 0, '|', sizeof new_floor_name);
2195 cflr_ok = extract_int(argbuf, 1);
2197 if (CtdlAccessCheck(ac_aide)) return;
2199 if (IsEmptyStr(new_floor_name)) {
2200 cprintf("%d Blank floor name not allowed.\n",
2201 ERROR + ILLEGAL_VALUE);
2205 for (a = 0; a < MAXFLOORS; ++a) {
2206 CtdlGetFloor(&flbuf, a);
2208 /* note any free slots while we're scanning... */
2209 if (((flbuf.f_flags & F_INUSE) == 0)
2213 /* check to see if it already exists */
2214 if ((!strcasecmp(flbuf.f_name, new_floor_name))
2215 && (flbuf.f_flags & F_INUSE)) {
2216 cprintf("%d Floor '%s' already exists.\n",
2217 ERROR + ALREADY_EXISTS,
2223 if (free_slot < 0) {
2224 cprintf("%d There is no space available for a new floor.\n",
2225 ERROR + INVALID_FLOOR_OPERATION);
2229 cprintf("%d ok to create...\n", CIT_OK);
2232 lgetfloor(&flbuf, free_slot);
2233 flbuf.f_flags = F_INUSE;
2234 flbuf.f_ref_count = 0;
2235 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
2236 lputfloor(&flbuf, free_slot);
2237 cprintf("%d %d\n", CIT_OK, free_slot);
2245 void cmd_kflr(char *argbuf)
2248 int floor_to_delete;
2252 floor_to_delete = extract_int(argbuf, 0);
2253 kflr_ok = extract_int(argbuf, 1);
2255 if (CtdlAccessCheck(ac_aide)) return;
2257 lgetfloor(&flbuf, floor_to_delete);
2260 if ((flbuf.f_flags & F_INUSE) == 0) {
2261 cprintf("%d Floor %d not in use.\n",
2262 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
2265 if (flbuf.f_ref_count != 0) {
2266 cprintf("%d Cannot delete; floor contains %d rooms.\n",
2267 ERROR + INVALID_FLOOR_OPERATION,
2272 cprintf("%d Ok\n", CIT_OK);
2274 cprintf("%d Ok to delete...\n", CIT_OK);
2281 if ((delete_ok == 1) && (kflr_ok == 1))
2283 lputfloor(&flbuf, floor_to_delete);
2289 void cmd_eflr(char *argbuf)
2295 np = num_parms(argbuf);
2297 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
2301 if (CtdlAccessCheck(ac_aide)) return;
2303 floor_num = extract_int(argbuf, 0);
2304 lgetfloor(&flbuf, floor_num);
2305 if ((flbuf.f_flags & F_INUSE) == 0) {
2306 lputfloor(&flbuf, floor_num);
2307 cprintf("%d Floor %d is not in use.\n",
2308 ERROR + INVALID_FLOOR_OPERATION, floor_num);
2312 extract_token(flbuf.f_name, argbuf, 1, '|', sizeof flbuf.f_name);
2313 lputfloor(&flbuf, floor_num);
2315 cprintf("%d Ok\n", CIT_OK);
2321 * cmd_stat() - return the modification time of the current room (maybe other things in the future)
2323 void cmd_stat(char *gargs)
2325 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
2326 CtdlGetRoom(&CC->room, CC->room.QRname);
2327 cprintf("%d %s|%ld|\n", CIT_OK, CC->room.QRname, CC->room.QRmtime);
2332 /*****************************************************************************/
2333 /* MODULE INITIALIZATION STUFF */
2334 /*****************************************************************************/
2336 CTDL_MODULE_INIT(room_ops)
2339 CtdlRegisterProtoHook(cmd_lrms, "LRMS", "List rooms");
2340 CtdlRegisterProtoHook(cmd_lkra, "LKRA", "List all known rooms");
2341 CtdlRegisterProtoHook(cmd_lkrn, "LKRN", "List known rooms with new messages");
2342 CtdlRegisterProtoHook(cmd_lkro, "LKRO", "List known rooms without new messages");
2343 CtdlRegisterProtoHook(cmd_lzrm, "LZRM", "List zapped rooms");
2344 CtdlRegisterProtoHook(cmd_lprm, "LPRM", "List public rooms");
2345 CtdlRegisterProtoHook(cmd_goto, "GOTO", "Goto a named room");
2346 CtdlRegisterProtoHook(cmd_stat, "STAT", "Get mtime of the current room");
2347 CtdlRegisterProtoHook(cmd_whok, "WHOK", "List users who know this room");
2348 CtdlRegisterProtoHook(cmd_rdir, "RDIR", "List files in room directory");
2349 CtdlRegisterProtoHook(cmd_getr, "GETR", "Get room parameters");
2350 CtdlRegisterProtoHook(cmd_setr, "SETR", "Set room parameters");
2351 CtdlRegisterProtoHook(cmd_geta, "GETA", "Get the room admin name");
2352 CtdlRegisterProtoHook(cmd_seta, "SETA", "Set the room admin for this room");
2353 CtdlRegisterProtoHook(cmd_rinf, "RINF", "Fetch room info file");
2354 CtdlRegisterProtoHook(cmd_kill, "KILL", "Kill (delete) the current room");
2355 CtdlRegisterProtoHook(cmd_cre8, "CRE8", "Create a new room");
2356 CtdlRegisterProtoHook(cmd_einf, "EINF", "Enter info file for the current room");
2357 CtdlRegisterProtoHook(cmd_lflr, "LFLR", "List all known floors");
2358 CtdlRegisterProtoHook(cmd_cflr, "CFLR", "Create a new floor");
2359 CtdlRegisterProtoHook(cmd_kflr, "KFLR", "Kill a floor");
2360 CtdlRegisterProtoHook(cmd_eflr, "EFLR", "Edit a floor");
2362 /* return our Subversion id for the Log */