* Removed some temporary diagnostics that had been left in serv_sieve.c
[citadel.git] / citadel / room_ops.c
blobe3720e3cc948490be0f52adf888886c71c0a2805
1 /*
2 * $Id$
3 *
4 * Server functions which perform operations on room objects.
6 */
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <sys/stat.h>
13 #include <ctype.h>
14 #include <string.h>
15 #include <dirent.h> /* for cmd_rdir to read contents of the directory */
17 #if TIME_WITH_SYS_TIME
18 # include <sys/time.h>
19 # include <time.h>
20 #else
21 # if HAVE_SYS_TIME_H
22 # include <sys/time.h>
23 # else
24 # include <time.h>
25 # endif
26 #endif
28 #include <limits.h>
29 #include <errno.h>
30 #include "citadel.h"
31 #include <libcitadel.h>
32 #include "server.h"
33 #include "database.h"
34 #include "config.h"
35 #include "room_ops.h"
36 #include "sysdep_decls.h"
37 #include "support.h"
38 #include "user_ops.h"
39 #include "msgbase.h"
40 #include "citserver.h"
41 #include "control.h"
42 #include "citadel_dirs.h"
43 #include "threads.h"
45 struct floor *floorcache[MAXFLOORS];
48 * Retrieve access control information for any user/room pair
50 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf,
51 int *result, int *view)
53 int retval = 0;
54 struct visit vbuf;
56 /* for internal programs, always do everything */
57 if (((CC->internal_pgm)) && (roombuf->QRflags & QR_INUSE)) {
58 retval = (UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED);
59 vbuf.v_view = 0;
60 goto SKIP_EVERYTHING;
63 /* Locate any applicable user/room relationships */
64 CtdlGetRelationship(&vbuf, userbuf, roombuf);
66 /* Force the properties of the Aide room */
67 if (!strcasecmp(roombuf->QRname, config.c_aideroom)) {
68 if (userbuf->axlevel >= 6) {
69 retval = UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
70 } else {
71 retval = 0;
73 goto NEWMSG;
76 /* If this is a public room, it's accessible... */
77 if ( ((roombuf->QRflags & QR_PRIVATE) == 0)
78 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
79 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
82 /* If this is a preferred users only room, check access level */
83 if (roombuf->QRflags & QR_PREFONLY) {
84 if (userbuf->axlevel < 5) {
85 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
89 /* For private rooms, check the generation number matchups */
90 if ( (roombuf->QRflags & QR_PRIVATE)
91 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
93 /* An explicit match means the user belongs in this room */
94 if (vbuf.v_flags & V_ACCESS) {
95 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
97 /* Otherwise, check if this is a guess-name or passworded
98 * room. If it is, a goto may at least be attempted
100 else if ((roombuf->QRflags & QR_PRIVATE)
101 || (roombuf->QRflags & QR_PASSWORDED)) {
102 retval = retval & ~UA_KNOWN;
103 retval = retval | UA_GOTOALLOWED;
107 /* For mailbox rooms, also check the namespace */
108 /* Also, mailbox owners can delete their messages */
109 if (roombuf->QRflags & QR_MAILBOX) {
110 if (userbuf->usernum == atol(roombuf->QRname)) {
111 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
113 /* An explicit match means the user belongs in this room */
114 if (vbuf.v_flags & V_ACCESS) {
115 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
119 /* For non-mailbox rooms... */
120 else {
122 /* User is allowed to post in the room unless:
123 * - User is not validated
124 * - User has no net privileges and it is a shared network room
125 * - It is a read-only room
127 int post_allowed = 1;
128 if (CC->user.axlevel < 2) post_allowed = 0;
129 if ((CC->user.axlevel < 4) && (CC->room.QRflags & QR_NETWORK)) post_allowed = 0;
130 if (roombuf->QRflags & QR_READONLY) post_allowed = 0;
131 if (post_allowed) {
132 retval = retval | UA_POSTALLOWED;
135 /* If "collaborative deletion" is active for this room, any user who can post
136 * is also allowed to delete
138 if (CC->room.QRflags2 & QR2_COLLABDEL) {
139 if (retval & UA_POSTALLOWED) {
140 retval = retval | UA_DELETEALLOWED;
146 /* Check to see if the user has forgotten this room */
147 if (vbuf.v_flags & V_FORGET) {
148 retval = retval & ~UA_KNOWN;
149 if ( ( ((roombuf->QRflags & QR_PRIVATE) == 0)
150 && ((roombuf->QRflags & QR_MAILBOX) == 0) )
151 || ( (roombuf->QRflags & QR_MAILBOX)
152 && (atol(roombuf->QRname) == CC->user.usernum))) {
153 retval = retval | UA_ZAPPED;
156 /* If user is explicitly locked out of this room, deny everything */
157 if (vbuf.v_flags & V_LOCKOUT) {
158 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED & ~UA_POSTALLOWED;
161 /* Aides get access to all private rooms */
162 if ( (userbuf->axlevel >= 6)
163 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
164 if (vbuf.v_flags & V_FORGET) {
165 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED;
167 else {
168 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED;
172 /* Aides can gain access to mailboxes as well, but they don't show
173 * by default.
175 if ( (userbuf->axlevel >= 6)
176 && (roombuf->QRflags & QR_MAILBOX) ) {
177 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED;
180 /* Aides and Room Aides have admin privileges */
181 if ( (userbuf->axlevel >= 6)
182 || (userbuf->usernum == roombuf->QRroomaide)
184 retval = retval | UA_ADMINALLOWED | UA_DELETEALLOWED;
187 NEWMSG: /* By the way, we also check for the presence of new messages */
188 if (is_msg_in_sequence_set(vbuf.v_seen, roombuf->QRhighest) == 0) {
189 retval = retval | UA_HASNEWMSGS;
192 /* System rooms never show up in the list. */
193 if (roombuf->QRflags2 & QR2_SYSTEM) {
194 retval = retval & ~UA_KNOWN;
197 SKIP_EVERYTHING:
198 /* Now give the caller the information it wants. */
199 if (result != NULL) *result = retval;
200 if (view != NULL) *view = vbuf.v_view;
204 * Self-checking stuff for a room record read into memory
206 void room_sanity_check(struct ctdlroom *qrbuf)
208 /* Mailbox rooms are always on the lowest floor */
209 if (qrbuf->QRflags & QR_MAILBOX) {
210 qrbuf->QRfloor = 0;
212 /* Listing order of 0 is illegal except for base rooms */
213 if (qrbuf->QRorder == 0)
214 if (!(qrbuf->QRflags & QR_MAILBOX) &&
215 strncasecmp(qrbuf->QRname, config.c_baseroom, ROOMNAMELEN)
217 strncasecmp(qrbuf->QRname, config.c_aideroom, ROOMNAMELEN))
218 qrbuf->QRorder = 64;
223 * getroom() - retrieve room data from disk
225 int getroom(struct ctdlroom *qrbuf, char *room_name)
227 struct cdbdata *cdbqr;
228 char lowercase_name[ROOMNAMELEN];
229 char personal_lowercase_name[ROOMNAMELEN];
230 char *dptr, *sptr, *eptr;
232 dptr = lowercase_name;
233 sptr = room_name;
234 eptr = (dptr + (sizeof lowercase_name - 1));
235 while (!IsEmptyStr(sptr) && (dptr < eptr)){
236 *dptr = tolower(*sptr);
237 sptr++; dptr++;
239 *dptr = '\0';
241 memset(qrbuf, 0, sizeof(struct ctdlroom));
243 /* First, try the public namespace */
244 cdbqr = cdb_fetch(CDB_ROOMS,
245 lowercase_name, strlen(lowercase_name));
247 /* If that didn't work, try the user's personal namespace */
248 if (cdbqr == NULL) {
249 snprintf(personal_lowercase_name,
250 sizeof personal_lowercase_name, "%010ld.%s",
251 CC->user.usernum, lowercase_name);
252 cdbqr = cdb_fetch(CDB_ROOMS,
253 personal_lowercase_name,
254 strlen(personal_lowercase_name));
256 if (cdbqr != NULL) {
257 memcpy(qrbuf, cdbqr->ptr,
258 ((cdbqr->len > sizeof(struct ctdlroom)) ?
259 sizeof(struct ctdlroom) : cdbqr->len));
260 cdb_free(cdbqr);
262 room_sanity_check(qrbuf);
264 return (0);
265 } else {
266 return (1);
271 * lgetroom() - same as getroom() but locks the record (if supported)
273 int lgetroom(struct ctdlroom *qrbuf, char *room_name)
275 register int retval;
276 retval = getroom(qrbuf, room_name);
277 if (retval == 0) begin_critical_section(S_ROOMS);
278 return(retval);
283 * b_putroom() - back end to putroom() and b_deleteroom()
284 * (if the supplied buffer is NULL, delete the room record)
286 void b_putroom(struct ctdlroom *qrbuf, char *room_name)
288 char lowercase_name[ROOMNAMELEN];
289 char *aptr, *bptr;
290 long len;
292 aptr = room_name;
293 bptr = lowercase_name;
294 while (!IsEmptyStr(aptr))
296 *bptr = tolower(*aptr);
297 aptr++;
298 bptr++;
300 *bptr='\0';
302 len = bptr - lowercase_name;
303 if (qrbuf == NULL) {
304 cdb_delete(CDB_ROOMS,
305 lowercase_name, len);
306 } else {
307 time(&qrbuf->QRmtime);
308 cdb_store(CDB_ROOMS,
309 lowercase_name, len,
310 qrbuf, sizeof(struct ctdlroom));
316 * putroom() - store room data to disk
318 void putroom(struct ctdlroom *qrbuf) {
319 b_putroom(qrbuf, qrbuf->QRname);
324 * b_deleteroom() - delete a room record from disk
326 void b_deleteroom(char *room_name) {
327 b_putroom(NULL, room_name);
333 * lputroom() - same as putroom() but unlocks the record (if supported)
335 void lputroom(struct ctdlroom *qrbuf)
338 putroom(qrbuf);
339 end_critical_section(S_ROOMS);
343 /****************************************************************************/
346 * getfloor() - retrieve floor data from disk
348 void getfloor(struct floor *flbuf, int floor_num)
350 struct cdbdata *cdbfl;
352 memset(flbuf, 0, sizeof(struct floor));
353 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
354 if (cdbfl != NULL) {
355 memcpy(flbuf, cdbfl->ptr,
356 ((cdbfl->len > sizeof(struct floor)) ?
357 sizeof(struct floor) : cdbfl->len));
358 cdb_free(cdbfl);
359 } else {
360 if (floor_num == 0) {
361 safestrncpy(flbuf->f_name, "Main Floor",
362 sizeof flbuf->f_name);
363 flbuf->f_flags = F_INUSE;
364 flbuf->f_ref_count = 3;
371 * lgetfloor() - same as getfloor() but locks the record (if supported)
373 void lgetfloor(struct floor *flbuf, int floor_num)
376 begin_critical_section(S_FLOORTAB);
377 getfloor(flbuf, floor_num);
382 * cgetfloor() - Get floor record from *cache* (loads from disk if needed)
384 * This is strictly a performance hack.
386 struct floor *cgetfloor(int floor_num) {
387 static int initialized = 0;
388 int i;
389 int fetch_new = 0;
390 struct floor *fl = NULL;
392 begin_critical_section(S_FLOORCACHE);
393 if (initialized == 0) {
394 for (i=0; i<MAXFLOORS; ++i) {
395 floorcache[floor_num] = NULL;
397 initialized = 1;
399 if (floorcache[floor_num] == NULL) {
400 fetch_new = 1;
402 end_critical_section(S_FLOORCACHE);
404 if (fetch_new) {
405 fl = malloc(sizeof(struct floor));
406 getfloor(fl, floor_num);
407 begin_critical_section(S_FLOORCACHE);
408 if (floorcache[floor_num] != NULL) {
409 free(floorcache[floor_num]);
411 floorcache[floor_num] = fl;
412 end_critical_section(S_FLOORCACHE);
415 return(floorcache[floor_num]);
421 * putfloor() - store floor data on disk
423 void putfloor(struct floor *flbuf, int floor_num)
425 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
426 begin_critical_section(S_FLOORCACHE);
427 if (floorcache[floor_num] != NULL) {
428 free(floorcache[floor_num]);
429 floorcache[floor_num] = malloc(sizeof(struct floor));
430 memcpy(floorcache[floor_num], flbuf, sizeof(struct floor));
432 end_critical_section(S_FLOORCACHE);
434 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
435 flbuf, sizeof(struct floor));
440 * lputfloor() - same as putfloor() but unlocks the record (if supported)
442 void lputfloor(struct floor *flbuf, int floor_num)
445 putfloor(flbuf, floor_num);
446 end_critical_section(S_FLOORTAB);
452 * Traverse the room file...
454 void ForEachRoom(void (*CallBack) (struct ctdlroom *EachRoom, void *out_data),
455 void *in_data)
457 struct ctdlroom qrbuf;
458 struct cdbdata *cdbqr;
460 cdb_rewind(CDB_ROOMS);
462 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
463 memset(&qrbuf, 0, sizeof(struct ctdlroom));
464 memcpy(&qrbuf, cdbqr->ptr,
465 ((cdbqr->len > sizeof(struct ctdlroom)) ?
466 sizeof(struct ctdlroom) : cdbqr->len));
467 cdb_free(cdbqr);
468 room_sanity_check(&qrbuf);
469 if (qrbuf.QRflags & QR_INUSE)
470 (*CallBack)(&qrbuf, in_data);
476 * delete_msglist() - delete room message pointers
478 void delete_msglist(struct ctdlroom *whichroom)
480 struct cdbdata *cdbml;
482 /* Make sure the msglist we're deleting actually exists, otherwise
483 * gdbm will complain when we try to delete an invalid record
485 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
486 if (cdbml != NULL) {
487 cdb_free(cdbml);
489 /* Go ahead and delete it */
490 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
498 * sort message pointers
499 * (returns new msg count)
501 int sort_msglist(long listptrs[], int oldcount)
503 int a, b;
504 long hold1, hold2;
505 int numitems;
507 numitems = oldcount;
508 if (numitems < 2)
509 return (oldcount);
511 /* do the sort */
512 for (a = numitems - 2; a >= 0; --a) {
513 for (b = 0; b <= a; ++b) {
514 if (listptrs[b] > (listptrs[b + 1])) {
515 hold1 = listptrs[b];
516 hold2 = listptrs[b + 1];
517 listptrs[b] = hold2;
518 listptrs[b + 1] = hold1;
523 /* and yank any nulls */
524 while ((numitems > 0) && (listptrs[0] == 0L)) {
525 memmove(&listptrs[0], &listptrs[1],
526 (sizeof(long) * (numitems - 1)));
527 --numitems;
530 return (numitems);
535 * Determine whether a given room is non-editable.
537 int is_noneditable(struct ctdlroom *qrbuf)
540 /* Mail> rooms are non-editable */
541 if ( (qrbuf->QRflags & QR_MAILBOX)
542 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
543 return (1);
545 /* Everything else is editable */
546 return (0);
552 * Back-back-end for all room listing commands
554 void list_roomname(struct ctdlroom *qrbuf, int ra, int current_view, int default_view)
556 char truncated_roomname[ROOMNAMELEN];
558 /* For my own mailbox rooms, chop off the owner prefix */
559 if ( (qrbuf->QRflags & QR_MAILBOX)
560 && (atol(qrbuf->QRname) == CC->user.usernum) ) {
561 safestrncpy(truncated_roomname, qrbuf->QRname, sizeof truncated_roomname);
562 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
563 cprintf("%s", truncated_roomname);
565 /* For all other rooms, just display the name in its entirety */
566 else {
567 cprintf("%s", qrbuf->QRname);
570 /* ...and now the other parameters */
571 cprintf("|%u|%d|%d|%d|%d|%d|%d|%ld|\n",
572 qrbuf->QRflags,
573 (int) qrbuf->QRfloor,
574 (int) qrbuf->QRorder,
575 (int) qrbuf->QRflags2,
577 current_view,
578 default_view,
579 qrbuf->QRmtime
585 * cmd_lrms() - List all accessible rooms, known or forgotten
587 void cmd_lrms_backend(struct ctdlroom *qrbuf, void *data)
589 int FloorBeingSearched = (-1);
590 int ra;
591 int view;
593 FloorBeingSearched = *(int *)data;
594 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
596 if ((( ra & (UA_KNOWN | UA_ZAPPED)))
597 && ((qrbuf->QRfloor == (FloorBeingSearched))
598 || ((FloorBeingSearched) < 0)))
599 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
602 void cmd_lrms(char *argbuf)
604 int FloorBeingSearched = (-1);
605 if (!IsEmptyStr(argbuf))
606 FloorBeingSearched = extract_int(argbuf, 0);
608 if (CtdlAccessCheck(ac_logged_in)) return;
610 if (getuser(&CC->user, CC->curr_user)) {
611 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
612 return;
614 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
616 ForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
617 cprintf("000\n");
623 * cmd_lkra() - List all known rooms
625 void cmd_lkra_backend(struct ctdlroom *qrbuf, void *data)
627 int FloorBeingSearched = (-1);
628 int ra;
629 int view;
631 FloorBeingSearched = *(int *)data;
632 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
634 if ((( ra & (UA_KNOWN)))
635 && ((qrbuf->QRfloor == (FloorBeingSearched))
636 || ((FloorBeingSearched) < 0)))
637 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
640 void cmd_lkra(char *argbuf)
642 int FloorBeingSearched = (-1);
643 if (!IsEmptyStr(argbuf))
644 FloorBeingSearched = extract_int(argbuf, 0);
646 if (CtdlAccessCheck(ac_logged_in)) return;
648 if (getuser(&CC->user, CC->curr_user)) {
649 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
650 return;
652 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
654 ForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
655 cprintf("000\n");
660 void cmd_lprm_backend(struct ctdlroom *qrbuf, void *data)
662 int FloorBeingSearched = (-1);
663 int ra;
664 int view;
666 FloorBeingSearched = *(int *)data;
667 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
669 if ( ((qrbuf->QRflags & QR_PRIVATE) == 0)
670 && ((qrbuf->QRflags & QR_MAILBOX) == 0)
671 && ((qrbuf->QRfloor == (FloorBeingSearched))
672 || ((FloorBeingSearched) < 0)))
673 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
676 void cmd_lprm(char *argbuf)
678 int FloorBeingSearched = (-1);
679 if (!IsEmptyStr(argbuf))
680 FloorBeingSearched = extract_int(argbuf, 0);
682 cprintf("%d Publiic rooms:\n", LISTING_FOLLOWS);
684 ForEachRoom(cmd_lprm_backend, &FloorBeingSearched);
685 cprintf("000\n");
691 * cmd_lkrn() - List all known rooms with new messages
693 void cmd_lkrn_backend(struct ctdlroom *qrbuf, void *data)
695 int FloorBeingSearched = (-1);
696 int ra;
697 int view;
699 FloorBeingSearched = *(int *)data;
700 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
702 if ((ra & UA_KNOWN)
703 && (ra & UA_HASNEWMSGS)
704 && ((qrbuf->QRfloor == (FloorBeingSearched))
705 || ((FloorBeingSearched) < 0)))
706 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
709 void cmd_lkrn(char *argbuf)
711 int FloorBeingSearched = (-1);
712 if (!IsEmptyStr(argbuf))
713 FloorBeingSearched = extract_int(argbuf, 0);
715 if (CtdlAccessCheck(ac_logged_in)) return;
717 if (getuser(&CC->user, CC->curr_user)) {
718 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
719 return;
721 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
723 ForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
724 cprintf("000\n");
730 * cmd_lkro() - List all known rooms
732 void cmd_lkro_backend(struct ctdlroom *qrbuf, void *data)
734 int FloorBeingSearched = (-1);
735 int ra;
736 int view;
738 FloorBeingSearched = *(int *)data;
739 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
741 if ((ra & UA_KNOWN)
742 && ((ra & UA_HASNEWMSGS) == 0)
743 && ((qrbuf->QRfloor == (FloorBeingSearched))
744 || ((FloorBeingSearched) < 0)))
745 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
748 void cmd_lkro(char *argbuf)
750 int FloorBeingSearched = (-1);
751 if (!IsEmptyStr(argbuf))
752 FloorBeingSearched = extract_int(argbuf, 0);
754 if (CtdlAccessCheck(ac_logged_in)) return;
756 if (getuser(&CC->user, CC->curr_user)) {
757 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
758 return;
760 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
762 ForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
763 cprintf("000\n");
769 * cmd_lzrm() - List all forgotten rooms
771 void cmd_lzrm_backend(struct ctdlroom *qrbuf, void *data)
773 int FloorBeingSearched = (-1);
774 int ra;
775 int view;
777 FloorBeingSearched = *(int *)data;
778 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
780 if ((ra & UA_GOTOALLOWED)
781 && (ra & UA_ZAPPED)
782 && ((qrbuf->QRfloor == (FloorBeingSearched))
783 || ((FloorBeingSearched) < 0)))
784 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
787 void cmd_lzrm(char *argbuf)
789 int FloorBeingSearched = (-1);
790 if (!IsEmptyStr(argbuf))
791 FloorBeingSearched = extract_int(argbuf, 0);
793 if (CtdlAccessCheck(ac_logged_in)) return;
795 if (getuser(&CC->user, CC->curr_user)) {
796 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
797 return;
799 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
801 ForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
802 cprintf("000\n");
807 * Make the specified room the current room for this session. No validation
808 * or access control is done here -- the caller should make sure that the
809 * specified room exists and is ok to access.
811 void usergoto(char *where, int display_result, int transiently,
812 int *retmsgs, int *retnew)
814 int a;
815 int new_messages = 0;
816 int old_messages = 0;
817 int total_messages = 0;
818 int info = 0;
819 int rmailflag;
820 int raideflag;
821 int newmailcount = 0;
822 struct visit vbuf;
823 char truncated_roomname[ROOMNAMELEN];
824 struct cdbdata *cdbfr;
825 long *msglist = NULL;
826 int num_msgs = 0;
827 unsigned int original_v_flags;
828 int num_sets;
829 int s;
830 char setstr[128], lostr[64], histr[64];
831 long lo, hi;
832 int is_trash = 0;
834 /* If the supplied room name is NULL, the caller wants us to know that
835 * it has already copied the room record into CC->room, so
836 * we can skip the extra database fetch.
838 if (where != NULL) {
839 safestrncpy(CC->room.QRname, where, sizeof CC->room.QRname);
840 getroom(&CC->room, where);
843 /* Take care of all the formalities. */
845 begin_critical_section(S_USERS);
846 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
847 original_v_flags = vbuf.v_flags;
849 /* Know the room ... but not if it's the page log room, or if the
850 * caller specified that we're only entering this room transiently.
852 if ((strcasecmp(CC->room.QRname, config.c_logpages))
853 && (transiently == 0) ) {
854 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
855 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
858 /* Only rewrite the database record if we changed something */
859 if (vbuf.v_flags != original_v_flags) {
860 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
862 end_critical_section(S_USERS);
864 /* Check for new mail */
865 newmailcount = NewMailCount();
867 /* set info to 1 if the user needs to read the room's info file */
868 if (CC->room.QRinfo > vbuf.v_lastseen) {
869 info = 1;
872 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
873 if (cdbfr != NULL) {
874 msglist = (long *) cdbfr->ptr;
875 cdbfr->ptr = NULL; /* usergoto() now owns this memory */
876 num_msgs = cdbfr->len / sizeof(long);
877 cdb_free(cdbfr);
880 total_messages = 0;
881 for (a=0; a<num_msgs; ++a) {
882 if (msglist[a] > 0L) ++total_messages;
884 new_messages = num_msgs;
885 num_sets = num_tokens(vbuf.v_seen, ',');
886 for (s=0; s<num_sets; ++s) {
887 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
889 extract_token(lostr, setstr, 0, ':', sizeof lostr);
890 if (num_tokens(setstr, ':') >= 2) {
891 extract_token(histr, setstr, 1, ':', sizeof histr);
892 if (!strcmp(histr, "*")) {
893 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
896 else {
897 strcpy(histr, lostr);
899 lo = atol(lostr);
900 hi = atol(histr);
902 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
903 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
904 ++old_messages;
905 msglist[a] = 0L;
909 new_messages = total_messages - old_messages;
911 if (msglist != NULL) free(msglist);
913 if (CC->room.QRflags & QR_MAILBOX)
914 rmailflag = 1;
915 else
916 rmailflag = 0;
918 if ((CC->room.QRroomaide == CC->user.usernum)
919 || (CC->user.axlevel >= 6))
920 raideflag = 1;
921 else
922 raideflag = 0;
924 safestrncpy(truncated_roomname, CC->room.QRname, sizeof truncated_roomname);
925 if ( (CC->room.QRflags & QR_MAILBOX)
926 && (atol(CC->room.QRname) == CC->user.usernum) ) {
927 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
930 if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
931 is_trash = 1;
934 if (retmsgs != NULL) *retmsgs = total_messages;
935 if (retnew != NULL) *retnew = new_messages;
936 CtdlLogPrintf(CTDL_DEBUG, "<%s> %d new of %d total messages\n",
937 CC->room.QRname,
938 new_messages, total_messages
941 CC->curr_view = (int)vbuf.v_view;
943 if (display_result) {
944 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|%d|\n",
945 CIT_OK, CtdlCheckExpress(),
946 truncated_roomname,
947 (int)new_messages,
948 (int)total_messages,
949 (int)info,
950 (int)CC->room.QRflags,
951 (long)CC->room.QRhighest,
952 (long)vbuf.v_lastseen,
953 (int)rmailflag,
954 (int)raideflag,
955 (int)newmailcount,
956 (int)CC->room.QRfloor,
957 (int)vbuf.v_view,
958 (int)CC->room.QRdefaultview,
959 (int)is_trash,
960 (int)CC->room.QRflags2
967 * Handle some of the macro named rooms
969 void convert_room_name_macros(char *towhere, size_t maxlen) {
970 if (!strcasecmp(towhere, "_BASEROOM_")) {
971 safestrncpy(towhere, config.c_baseroom, maxlen);
973 else if (!strcasecmp(towhere, "_MAIL_")) {
974 safestrncpy(towhere, MAILROOM, maxlen);
976 else if (!strcasecmp(towhere, "_TRASH_")) {
977 safestrncpy(towhere, USERTRASHROOM, maxlen);
979 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
980 safestrncpy(towhere, config.c_twitroom, maxlen);
982 else if (!strcasecmp(towhere, "_CALENDAR_")) {
983 safestrncpy(towhere, USERCALENDARROOM, maxlen);
985 else if (!strcasecmp(towhere, "_TASKS_")) {
986 safestrncpy(towhere, USERTASKSROOM, maxlen);
988 else if (!strcasecmp(towhere, "_CONTACTS_")) {
989 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
991 else if (!strcasecmp(towhere, "_NOTES_")) {
992 safestrncpy(towhere, USERNOTESROOM, maxlen);
998 * cmd_goto() - goto a new room
1000 void cmd_goto(char *gargs)
1002 struct ctdlroom QRscratch;
1003 int c;
1004 int ok = 0;
1005 int ra;
1006 char augmented_roomname[ROOMNAMELEN];
1007 char towhere[ROOMNAMELEN];
1008 char password[32];
1009 int transiently = 0;
1011 if (CtdlAccessCheck(ac_logged_in)) return;
1013 extract_token(towhere, gargs, 0, '|', sizeof towhere);
1014 extract_token(password, gargs, 1, '|', sizeof password);
1015 transiently = extract_int(gargs, 2);
1017 getuser(&CC->user, CC->curr_user);
1020 * Handle some of the macro named rooms
1022 convert_room_name_macros(towhere, sizeof towhere);
1024 /* First try a regular match */
1025 c = getroom(&QRscratch, towhere);
1027 /* Then try a mailbox name match */
1028 if (c != 0) {
1029 MailboxName(augmented_roomname, sizeof augmented_roomname,
1030 &CC->user, towhere);
1031 c = getroom(&QRscratch, augmented_roomname);
1032 if (c == 0)
1033 safestrncpy(towhere, augmented_roomname, sizeof towhere);
1036 /* And if the room was found... */
1037 if (c == 0) {
1039 /* Let internal programs go directly to any room. */
1040 if (CC->internal_pgm) {
1041 memcpy(&CC->room, &QRscratch,
1042 sizeof(struct ctdlroom));
1043 usergoto(NULL, 1, transiently, NULL, NULL);
1044 return;
1047 /* See if there is an existing user/room relationship */
1048 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
1050 /* normal clients have to pass through security */
1051 if (ra & UA_GOTOALLOWED) {
1052 ok = 1;
1055 if (ok == 1) {
1056 if ((QRscratch.QRflags & QR_MAILBOX) &&
1057 ((ra & UA_GOTOALLOWED))) {
1058 memcpy(&CC->room, &QRscratch,
1059 sizeof(struct ctdlroom));
1060 usergoto(NULL, 1, transiently, NULL, NULL);
1061 return;
1062 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
1063 ((ra & UA_KNOWN) == 0) &&
1064 (strcasecmp(QRscratch.QRpasswd, password)) &&
1065 (CC->user.axlevel < 6)
1067 cprintf("%d wrong or missing passwd\n",
1068 ERROR + PASSWORD_REQUIRED);
1069 return;
1070 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
1071 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
1072 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
1073 ((ra & UA_KNOWN) == 0) &&
1074 (CC->user.axlevel < 6)
1076 CtdlLogPrintf(CTDL_DEBUG, "Failed to acquire private room\n");
1077 } else {
1078 memcpy(&CC->room, &QRscratch,
1079 sizeof(struct ctdlroom));
1080 usergoto(NULL, 1, transiently, NULL, NULL);
1081 return;
1086 cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
1090 void cmd_whok(void)
1092 struct ctdluser temp;
1093 struct cdbdata *cdbus;
1094 int ra;
1096 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
1097 cdb_rewind(CDB_USERS);
1098 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1099 memset(&temp, 0, sizeof temp);
1100 memcpy(&temp, cdbus->ptr, sizeof temp);
1101 cdb_free(cdbus);
1103 CtdlRoomAccess(&CC->room, &temp, &ra, NULL);
1104 if ((CC->room.QRflags & QR_INUSE)
1105 && (ra & UA_KNOWN)
1107 cprintf("%s\n", temp.fullname);
1109 cprintf("000\n");
1114 * RDIR command for room directory
1116 void cmd_rdir(void)
1118 char buf[256];
1119 char comment[256];
1120 FILE *fd;
1121 struct stat statbuf;
1122 DIR *filedir = NULL;
1123 struct dirent *filedir_entry;
1124 int d_namelen;
1125 char buf2[SIZ];
1126 char mimebuf[64];
1127 long len;
1129 if (CtdlAccessCheck(ac_logged_in)) return;
1131 getroom(&CC->room, CC->room.QRname);
1132 getuser(&CC->user, CC->curr_user);
1134 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
1135 cprintf("%d not here.\n", ERROR + NOT_HERE);
1136 return;
1138 if (((CC->room.QRflags & QR_VISDIR) == 0)
1139 && (CC->user.axlevel < 6)
1140 && (CC->user.usernum != CC->room.QRroomaide)) {
1141 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1142 return;
1145 snprintf(buf, sizeof buf, "%s/%s", ctdl_file_dir, CC->room.QRdirname);
1146 filedir = opendir (buf);
1148 if (filedir == NULL) {
1149 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1150 return;
1152 cprintf("%d %s|%s/%s\n", LISTING_FOLLOWS, config.c_fqdn, ctdl_file_dir, CC->room.QRdirname);
1154 snprintf(buf, sizeof buf, "%s/%s/filedir", ctdl_file_dir, CC->room.QRdirname);
1155 fd = fopen(buf, "r");
1156 if (fd == NULL)
1157 fd = fopen("/dev/null", "r");
1158 while ((filedir_entry = readdir(filedir)))
1160 if (strcasecmp(filedir_entry->d_name, "filedir") && filedir_entry->d_name[0] != '.')
1162 #ifdef _DIRENT_HAVE_D_NAMELEN
1163 d_namelen = filedir_entry->d_namelen;
1164 #else
1165 d_namelen = strlen(filedir_entry->d_name);
1166 #endif
1167 snprintf(buf, sizeof buf, "%s/%s/%s", ctdl_file_dir, CC->room.QRdirname, filedir_entry->d_name);
1168 stat(buf, &statbuf); /* stat the file */
1169 if (!(statbuf.st_mode & S_IFREG))
1171 snprintf(buf2, sizeof buf2, "Command RDIR found something that is not a useable file. It should be cleaned up.\n RDIR found this non regular file:\n%s\n", buf);
1172 aide_message(buf2, "RDIR found bad file");
1173 continue; /* not a useable file type so don't show it */
1175 safestrncpy(comment, "", sizeof comment);
1176 fseek(fd, 0L, 0); /* rewind descriptions file */
1177 /* Get the description from the descriptions file */
1178 while ((fgets(buf, sizeof buf, fd) != NULL) && (IsEmptyStr(comment)))
1180 buf[strlen(buf) - 1] = 0;
1181 if ((!strncasecmp(buf, filedir_entry->d_name, d_namelen)) && (buf[d_namelen] == ' '))
1182 safestrncpy(comment, &buf[d_namelen + 1], sizeof comment);
1184 len = extract_token (mimebuf, comment, 0,' ', 64);
1185 if ((len <0) || strchr(mimebuf, '/') == NULL)
1187 snprintf (mimebuf, 64, "application/octetstream");
1188 len = 0;
1190 cprintf("%s|%ld|%s|%s\n",
1191 filedir_entry->d_name,
1192 (long)statbuf.st_size,
1193 mimebuf,
1194 &comment[len]);
1197 fclose(fd);
1198 closedir(filedir);
1200 cprintf("000\n");
1204 * get room parameters (aide or room aide command)
1206 void cmd_getr(void)
1208 if (CtdlAccessCheck(ac_room_aide)) return;
1210 getroom(&CC->room, CC->room.QRname);
1211 cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1212 CIT_OK,
1213 CtdlCheckExpress(),
1215 ((CC->room.QRflags & QR_MAILBOX) ?
1216 &CC->room.QRname[11] : CC->room.QRname),
1218 ((CC->room.QRflags & QR_PASSWORDED) ?
1219 CC->room.QRpasswd : ""),
1221 ((CC->room.QRflags & QR_DIRECTORY) ?
1222 CC->room.QRdirname : ""),
1224 CC->room.QRflags,
1225 (int) CC->room.QRfloor,
1226 (int) CC->room.QRorder,
1228 CC->room.QRdefaultview,
1229 CC->room.QRflags2
1235 * Back end function to rename a room.
1236 * You can also specify which floor to move the room to, or specify -1 to
1237 * keep the room on the same floor it was on.
1239 * If you are renaming a mailbox room, you must supply the namespace prefix
1240 * in *at least* the old name!
1242 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1243 int old_floor = 0;
1244 struct ctdlroom qrbuf;
1245 struct ctdlroom qrtmp;
1246 int ret = 0;
1247 struct floor *fl;
1248 struct floor flbuf;
1249 long owner = 0L;
1250 char actual_old_name[ROOMNAMELEN];
1252 CtdlLogPrintf(CTDL_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1253 old_name, new_name, new_floor);
1255 if (new_floor >= 0) {
1256 fl = cgetfloor(new_floor);
1257 if ((fl->f_flags & F_INUSE) == 0) {
1258 return(crr_invalid_floor);
1262 begin_critical_section(S_ROOMS);
1264 if ( (getroom(&qrtmp, new_name) == 0)
1265 && (strcasecmp(new_name, old_name)) ) {
1266 ret = crr_already_exists;
1269 else if (getroom(&qrbuf, old_name) != 0) {
1270 ret = crr_room_not_found;
1273 else if ( (CC->user.axlevel < 6) && (!CC->internal_pgm)
1274 && (CC->user.usernum != qrbuf.QRroomaide)
1275 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) ) {
1276 ret = crr_access_denied;
1279 else if (is_noneditable(&qrbuf)) {
1280 ret = crr_noneditable;
1283 else {
1284 /* Rename it */
1285 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1286 if (qrbuf.QRflags & QR_MAILBOX) {
1287 owner = atol(qrbuf.QRname);
1289 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1290 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1291 "%010ld.%s", owner, new_name);
1293 else {
1294 safestrncpy(qrbuf.QRname, new_name,
1295 sizeof(qrbuf.QRname));
1298 /* Reject change of floor for baseroom/aideroom */
1299 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1300 !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1301 new_floor = 0;
1304 /* Take care of floor stuff */
1305 old_floor = qrbuf.QRfloor;
1306 if (new_floor < 0) {
1307 new_floor = old_floor;
1309 qrbuf.QRfloor = new_floor;
1310 putroom(&qrbuf);
1312 begin_critical_section(S_CONFIG);
1314 /* If baseroom/aideroom name changes, update config */
1315 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1316 safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1317 put_config();
1319 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1320 safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1321 put_config();
1324 end_critical_section(S_CONFIG);
1326 /* If the room name changed, then there are now two room
1327 * records, so we have to delete the old one.
1329 if (strcasecmp(new_name, old_name)) {
1330 b_deleteroom(actual_old_name);
1333 ret = crr_ok;
1336 end_critical_section(S_ROOMS);
1338 /* Adjust the floor reference counts if necessary */
1339 if (new_floor != old_floor) {
1340 lgetfloor(&flbuf, old_floor);
1341 --flbuf.f_ref_count;
1342 lputfloor(&flbuf, old_floor);
1343 CtdlLogPrintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1344 lgetfloor(&flbuf, new_floor);
1345 ++flbuf.f_ref_count;
1346 lputfloor(&flbuf, new_floor);
1347 CtdlLogPrintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1350 /* ...and everybody say "YATTA!" */
1351 return(ret);
1356 * set room parameters (aide or room aide command)
1358 void cmd_setr(char *args)
1360 char buf[256];
1361 int new_order = 0;
1362 int r;
1363 int new_floor;
1364 char new_name[ROOMNAMELEN];
1366 if (CtdlAccessCheck(ac_logged_in)) return;
1368 if (num_parms(args) >= 6) {
1369 new_floor = extract_int(args, 5);
1370 } else {
1371 new_floor = (-1); /* don't change the floor */
1374 /* When is a new name more than just a new name? When the old name
1375 * has a namespace prefix.
1377 if (CC->room.QRflags & QR_MAILBOX) {
1378 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
1379 } else {
1380 safestrncpy(new_name, "", sizeof new_name);
1382 extract_token(&new_name[strlen(new_name)], args, 0, '|', (sizeof new_name - strlen(new_name)));
1384 r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
1386 if (r == crr_room_not_found) {
1387 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
1388 } else if (r == crr_already_exists) {
1389 cprintf("%d '%s' already exists.\n",
1390 ERROR + ALREADY_EXISTS, new_name);
1391 } else if (r == crr_noneditable) {
1392 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
1393 } else if (r == crr_invalid_floor) {
1394 cprintf("%d Target floor does not exist.\n",
1395 ERROR + INVALID_FLOOR_OPERATION);
1396 } else if (r == crr_access_denied) {
1397 cprintf("%d You do not have permission to edit '%s'\n",
1398 ERROR + HIGHER_ACCESS_REQUIRED,
1399 CC->room.QRname);
1400 } else if (r != crr_ok) {
1401 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1402 ERROR + INTERNAL_ERROR, r);
1405 if (r != crr_ok) {
1406 return;
1409 getroom(&CC->room, new_name);
1411 /* Now we have to do a bunch of other stuff */
1413 if (num_parms(args) >= 7) {
1414 new_order = extract_int(args, 6);
1415 if (new_order < 1)
1416 new_order = 1;
1417 if (new_order > 127)
1418 new_order = 127;
1421 lgetroom(&CC->room, CC->room.QRname);
1423 /* Directory room */
1424 extract_token(buf, args, 2, '|', sizeof buf);
1425 buf[15] = 0;
1426 safestrncpy(CC->room.QRdirname, buf,
1427 sizeof CC->room.QRdirname);
1429 /* Default view */
1430 if (num_parms(args) >= 8) {
1431 CC->room.QRdefaultview = extract_int(args, 7);
1434 /* Second set of flags */
1435 if (num_parms(args) >= 9) {
1436 CC->room.QRflags2 = extract_int(args, 8);
1439 /* Misc. flags */
1440 CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
1441 /* Clean up a client boo-boo: if the client set the room to
1442 * guess-name or passworded, ensure that the private flag is
1443 * also set.
1445 if ((CC->room.QRflags & QR_GUESSNAME)
1446 || (CC->room.QRflags & QR_PASSWORDED))
1447 CC->room.QRflags |= QR_PRIVATE;
1449 /* Some changes can't apply to BASEROOM */
1450 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1451 ROOMNAMELEN)) {
1452 CC->room.QRorder = 0;
1453 CC->room.QRpasswd[0] = '\0';
1454 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1455 QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1456 CC->room.QRflags |= QR_PERMANENT;
1457 } else {
1458 /* March order (doesn't apply to AIDEROOM) */
1459 if (num_parms(args) >= 7)
1460 CC->room.QRorder = (char) new_order;
1461 /* Room password */
1462 extract_token(buf, args, 1, '|', sizeof buf);
1463 buf[10] = 0;
1464 safestrncpy(CC->room.QRpasswd, buf,
1465 sizeof CC->room.QRpasswd);
1466 /* Kick everyone out if the client requested it
1467 * (by changing the room's generation number)
1469 if (extract_int(args, 4)) {
1470 time(&CC->room.QRgen);
1473 /* Some changes can't apply to AIDEROOM */
1474 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1475 ROOMNAMELEN)) {
1476 CC->room.QRorder = 0;
1477 CC->room.QRflags &= ~QR_MAILBOX;
1478 CC->room.QRflags |= QR_PERMANENT;
1481 /* Write the room record back to disk */
1482 lputroom(&CC->room);
1484 /* Create a room directory if necessary */
1485 if (CC->room.QRflags & QR_DIRECTORY) {
1486 snprintf(buf, sizeof buf,"%s/%s",
1487 ctdl_file_dir,
1488 CC->room.QRdirname);
1489 mkdir(buf, 0755);
1491 snprintf(buf, sizeof buf, "The room \"%s\" has been edited by %s.\n",
1492 CC->room.QRname, CC->curr_user);
1493 aide_message(buf, "Room modification Message");
1494 cprintf("%d Ok\n", CIT_OK);
1500 * get the name of the room aide for this room
1502 void cmd_geta(void)
1504 struct ctdluser usbuf;
1506 if (CtdlAccessCheck(ac_logged_in)) return;
1508 if (getuserbynumber(&usbuf, CC->room.QRroomaide) == 0) {
1509 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1510 } else {
1511 cprintf("%d \n", CIT_OK);
1517 * set the room aide for this room
1519 void cmd_seta(char *new_ra)
1521 struct ctdluser usbuf;
1522 long newu;
1523 char buf[SIZ];
1524 int post_notice;
1526 if (CtdlAccessCheck(ac_room_aide)) return;
1528 if (getuser(&usbuf, new_ra) != 0) {
1529 newu = (-1L);
1530 } else {
1531 newu = usbuf.usernum;
1534 lgetroom(&CC->room, CC->room.QRname);
1535 post_notice = 0;
1536 if (CC->room.QRroomaide != newu) {
1537 post_notice = 1;
1539 CC->room.QRroomaide = newu;
1540 lputroom(&CC->room);
1543 * We have to post the change notice _after_ writing changes to
1544 * the room table, otherwise it would deadlock!
1546 if (post_notice == 1) {
1547 if (!IsEmptyStr(usbuf.fullname))
1548 snprintf(buf, sizeof buf,
1549 "%s is now the room aide for \"%s\".\n",
1550 usbuf.fullname, CC->room.QRname);
1551 else
1552 snprintf(buf, sizeof buf,
1553 "There is now no room aide for \"%s\".\n",
1554 CC->room.QRname);
1555 aide_message(buf, "Aide Room Modification");
1557 cprintf("%d Ok\n", CIT_OK);
1561 * retrieve info file for this room
1563 void cmd_rinf(void)
1565 char filename[128];
1566 char buf[SIZ];
1567 FILE *info_fp;
1569 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_info_dir);
1570 info_fp = fopen(filename, "r");
1572 if (info_fp == NULL) {
1573 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
1574 return;
1576 cprintf("%d Info:\n", LISTING_FOLLOWS);
1577 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1578 if (!IsEmptyStr(buf))
1579 buf[strlen(buf) - 1] = 0;
1580 cprintf("%s\n", buf);
1582 cprintf("000\n");
1583 fclose(info_fp);
1587 * Asynchronously schedule a room for deletion. The room will appear
1588 * deleted to the user(s), but it won't actually get purged from the
1589 * database until THE DREADED AUTO-PURGER makes its next run.
1591 void schedule_room_for_deletion(struct ctdlroom *qrbuf)
1593 char old_name[ROOMNAMELEN];
1594 static int seq = 0;
1596 CtdlLogPrintf(CTDL_NOTICE, "Scheduling room <%s> for deletion\n",
1597 qrbuf->QRname);
1599 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1601 getroom(qrbuf, qrbuf->QRname);
1603 /* Turn the room into a private mailbox owned by a user who doesn't
1604 * exist. This will immediately make the room invisible to everyone,
1605 * and qualify the room for purging.
1607 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1608 time(NULL),
1609 ++seq,
1610 old_name
1612 qrbuf->QRflags |= QR_MAILBOX;
1613 time(&qrbuf->QRgen); /* Use a timestamp as the new generation number */
1615 putroom(qrbuf);
1617 b_deleteroom(old_name);
1623 * Back end processing to delete a room and everything associated with it
1624 * (This one is synchronous and should only get called by THE DREADED
1625 * AUTO-PURGER in serv_expire.c. All user-facing code should call
1626 * the asynchronous schedule_room_for_deletion() instead.)
1628 void delete_room(struct ctdlroom *qrbuf)
1630 struct floor flbuf;
1631 char filename[100];
1632 /* TODO: filename magic? does this realy work? */
1634 CtdlLogPrintf(CTDL_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1636 /* Delete the info file */
1637 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_info_dir);
1638 unlink(filename);
1640 /* Delete the image file */
1641 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_image_dir);
1642 unlink(filename);
1644 /* Delete the room's network config file */
1645 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1646 unlink(filename);
1648 /* Delete the messages in the room
1649 * (Careful: this opens an S_ROOMS critical section!)
1651 CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
1653 /* Flag the room record as not in use */
1654 lgetroom(qrbuf, qrbuf->QRname);
1655 qrbuf->QRflags = 0;
1656 lputroom(qrbuf);
1658 /* then decrement the reference count for the floor */
1659 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1660 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1661 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1663 /* Delete the room record from the database! */
1664 b_deleteroom(qrbuf->QRname);
1670 * Check access control for deleting a room
1672 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1674 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1675 return(0);
1678 if (is_noneditable(qr)) {
1679 return(0);
1683 * For mailboxes, check stuff
1685 if (qr->QRflags & QR_MAILBOX) {
1687 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1689 if (atol(qr->QRname) != CC->user.usernum) {
1690 return(0); /* not my room */
1693 /* Can't delete your Mail> room */
1694 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1696 /* Otherwise it's ok */
1697 return(1);
1701 * For normal rooms, just check for aide or room aide status.
1703 return(is_room_aide());
1707 * aide command: kill the current room
1709 void cmd_kill(char *argbuf)
1711 char deleted_room_name[ROOMNAMELEN];
1712 char msg[SIZ];
1713 int kill_ok;
1715 kill_ok = extract_int(argbuf, 0);
1717 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
1718 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1719 return;
1721 if (kill_ok) {
1722 if (CC->room.QRflags & QR_MAILBOX) {
1723 safestrncpy(deleted_room_name, &CC->room.QRname[11], sizeof deleted_room_name);
1725 else {
1726 safestrncpy(deleted_room_name, CC->room.QRname, sizeof deleted_room_name);
1729 /* Do the dirty work */
1730 schedule_room_for_deletion(&CC->room);
1732 /* Return to the Lobby */
1733 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1735 /* tell the world what we did */
1736 snprintf(msg, sizeof msg, "The room \"%s\" has been deleted by %s.\n",
1737 deleted_room_name, CC->curr_user);
1738 aide_message(msg, "Room Purger Message");
1739 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1740 } else {
1741 cprintf("%d ok to delete.\n", CIT_OK);
1747 * Internal code to create a new room (returns room flags)
1749 * Room types: 0=public, 1=guessname, 2=passworded, 3=inv-only,
1750 * 4=mailbox, 5=mailbox, but caller supplies namespace
1752 unsigned create_room(char *new_room_name,
1753 int new_room_type,
1754 char *new_room_pass,
1755 int new_room_floor,
1756 int really_create,
1757 int avoid_access,
1758 int new_room_view)
1761 struct ctdlroom qrbuf;
1762 struct floor flbuf;
1763 struct visit vbuf;
1765 CtdlLogPrintf(CTDL_DEBUG, "create_room(name=%s, type=%d, view=%d)\n",
1766 new_room_name, new_room_type, new_room_view);
1768 if (getroom(&qrbuf, new_room_name) == 0) {
1769 CtdlLogPrintf(CTDL_DEBUG, "%s already exists.\n", new_room_name);
1770 return(0);
1773 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1774 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1775 qrbuf.QRflags = QR_INUSE;
1776 if (new_room_type > 0)
1777 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1778 if (new_room_type == 1)
1779 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1780 if (new_room_type == 2)
1781 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1782 if ( (new_room_type == 4) || (new_room_type == 5) ) {
1783 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1784 /* qrbuf.QRflags2 |= QR2_SUBJECTREQ; */
1787 /* If the user is requesting a personal room, set up the room
1788 * name accordingly (prepend the user number)
1790 if (new_room_type == 4) {
1791 MailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1793 else {
1794 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1797 /* If the room is private, and the system administrator has elected
1798 * to automatically grant room aide privileges, do so now; otherwise,
1799 * set the room aide to undefined.
1801 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1802 qrbuf.QRroomaide = CC->user.usernum;
1803 } else {
1804 qrbuf.QRroomaide = (-1L);
1808 * If the caller is only interested in testing whether this will work,
1809 * return now without creating the room.
1811 if (!really_create) return (qrbuf.QRflags);
1813 qrbuf.QRnumber = get_new_room_number();
1814 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1815 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1816 qrbuf.QRfloor = new_room_floor;
1817 qrbuf.QRdefaultview = new_room_view;
1819 /* save what we just did... */
1820 putroom(&qrbuf);
1822 /* bump the reference count on whatever floor the room is on */
1823 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1824 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1825 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1827 /* Grant the creator access to the room unless the avoid_access
1828 * parameter was specified.
1830 if ( (CC->logged_in) && (avoid_access == 0) ) {
1831 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1832 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1833 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1834 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1837 /* resume our happy day */
1838 return (qrbuf.QRflags);
1843 * create a new room
1845 void cmd_cre8(char *args)
1847 int cre8_ok;
1848 char new_room_name[ROOMNAMELEN];
1849 int new_room_type;
1850 char new_room_pass[32];
1851 int new_room_floor;
1852 int new_room_view;
1853 char *notification_message = NULL;
1854 unsigned newflags;
1855 struct floor *fl;
1856 int avoid_access = 0;
1858 cre8_ok = extract_int(args, 0);
1859 extract_token(new_room_name, args, 1, '|', sizeof new_room_name);
1860 new_room_name[ROOMNAMELEN - 1] = 0;
1861 new_room_type = extract_int(args, 2);
1862 extract_token(new_room_pass, args, 3, '|', sizeof new_room_pass);
1863 avoid_access = extract_int(args, 5);
1864 new_room_view = extract_int(args, 6);
1865 new_room_pass[9] = 0;
1866 new_room_floor = 0;
1868 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 1)) {
1869 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
1870 return;
1873 if (!strcasecmp(new_room_name, MAILROOM)) {
1874 cprintf("%d '%s' already exists.\n",
1875 ERROR + ALREADY_EXISTS, new_room_name);
1876 return;
1879 if (num_parms(args) >= 5) {
1880 fl = cgetfloor(extract_int(args, 4));
1881 if (fl == NULL) {
1882 cprintf("%d Invalid floor number.\n",
1883 ERROR + INVALID_FLOOR_OPERATION);
1884 return;
1886 else if ((fl->f_flags & F_INUSE) == 0) {
1887 cprintf("%d Invalid floor number.\n",
1888 ERROR + INVALID_FLOOR_OPERATION);
1889 return;
1890 } else {
1891 new_room_floor = extract_int(args, 4);
1895 if (CtdlAccessCheck(ac_logged_in)) return;
1897 if (CC->user.axlevel < config.c_createax && !CC->internal_pgm) {
1898 cprintf("%d You need higher access to create rooms.\n",
1899 ERROR + HIGHER_ACCESS_REQUIRED);
1900 return;
1903 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 0)) {
1904 cprintf("%d Ok to create rooms.\n", CIT_OK);
1905 return;
1908 if ((new_room_type < 0) || (new_room_type > 5)) {
1909 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
1910 return;
1913 if (new_room_type == 5) {
1914 if (CC->user.axlevel < 6) {
1915 cprintf("%d Higher access required\n",
1916 ERROR + HIGHER_ACCESS_REQUIRED);
1917 return;
1921 /* Check to make sure the requested room name doesn't already exist */
1922 newflags = create_room(new_room_name,
1923 new_room_type, new_room_pass, new_room_floor,
1924 0, avoid_access, new_room_view);
1925 if (newflags == 0) {
1926 cprintf("%d '%s' already exists.\n",
1927 ERROR + ALREADY_EXISTS, new_room_name);
1928 return;
1931 if (cre8_ok == 0) {
1932 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
1933 return;
1936 /* If we reach this point, the room needs to be created. */
1938 newflags = create_room(new_room_name,
1939 new_room_type, new_room_pass, new_room_floor, 1, 0,
1940 new_room_view);
1942 /* post a message in Aide> describing the new room */
1943 notification_message = malloc(1024);
1944 snprintf(notification_message, 1024,
1945 "A new room called \"%s\" has been created by %s%s%s%s%s%s\n",
1946 new_room_name,
1947 CC->user.fullname,
1948 ((newflags & QR_MAILBOX) ? " [personal]" : ""),
1949 ((newflags & QR_PRIVATE) ? " [private]" : ""),
1950 ((newflags & QR_GUESSNAME) ? " [hidden]" : ""),
1951 ((newflags & QR_PASSWORDED) ? " Password: " : ""),
1952 ((newflags & QR_PASSWORDED) ? new_room_pass : "")
1954 aide_message(notification_message, "Room Creation Message");
1955 free(notification_message);
1957 cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
1962 void cmd_einf(char *ok)
1963 { /* enter info file for current room */
1964 FILE *fp;
1965 char infofilename[SIZ];
1966 char buf[SIZ];
1968 unbuffer_output();
1970 if (CtdlAccessCheck(ac_room_aide)) return;
1972 if (atoi(ok) == 0) {
1973 cprintf("%d Ok.\n", CIT_OK);
1974 return;
1976 assoc_file_name(infofilename, sizeof infofilename, &CC->room, ctdl_info_dir);
1977 CtdlLogPrintf(CTDL_DEBUG, "opening\n");
1978 fp = fopen(infofilename, "w");
1979 CtdlLogPrintf(CTDL_DEBUG, "checking\n");
1980 if (fp == NULL) {
1981 cprintf("%d Cannot open %s: %s\n",
1982 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1983 return;
1985 cprintf("%d Send info...\n", SEND_LISTING);
1987 do {
1988 client_getln(buf, sizeof buf);
1989 if (strcmp(buf, "000"))
1990 fprintf(fp, "%s\n", buf);
1991 } while (strcmp(buf, "000"));
1992 fclose(fp);
1994 /* now update the room index so people will see our new info */
1995 lgetroom(&CC->room, CC->room.QRname); /* lock so no one steps on us */
1996 CC->room.QRinfo = CC->room.QRhighest + 1L;
1997 lputroom(&CC->room);
2002 * cmd_lflr() - List all known floors
2004 void cmd_lflr(void)
2006 int a;
2007 struct floor flbuf;
2009 if (CtdlAccessCheck(ac_logged_in)) return;
2011 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
2013 for (a = 0; a < MAXFLOORS; ++a) {
2014 getfloor(&flbuf, a);
2015 if (flbuf.f_flags & F_INUSE) {
2016 cprintf("%d|%s|%d\n",
2018 flbuf.f_name,
2019 flbuf.f_ref_count);
2022 cprintf("000\n");
2028 * create a new floor
2030 void cmd_cflr(char *argbuf)
2032 char new_floor_name[256];
2033 struct floor flbuf;
2034 int cflr_ok;
2035 int free_slot = (-1);
2036 int a;
2038 extract_token(new_floor_name, argbuf, 0, '|', sizeof new_floor_name);
2039 cflr_ok = extract_int(argbuf, 1);
2041 if (CtdlAccessCheck(ac_aide)) return;
2043 if (IsEmptyStr(new_floor_name)) {
2044 cprintf("%d Blank floor name not allowed.\n",
2045 ERROR + ILLEGAL_VALUE);
2046 return;
2049 for (a = 0; a < MAXFLOORS; ++a) {
2050 getfloor(&flbuf, a);
2052 /* note any free slots while we're scanning... */
2053 if (((flbuf.f_flags & F_INUSE) == 0)
2054 && (free_slot < 0))
2055 free_slot = a;
2057 /* check to see if it already exists */
2058 if ((!strcasecmp(flbuf.f_name, new_floor_name))
2059 && (flbuf.f_flags & F_INUSE)) {
2060 cprintf("%d Floor '%s' already exists.\n",
2061 ERROR + ALREADY_EXISTS,
2062 flbuf.f_name);
2063 return;
2067 if (free_slot < 0) {
2068 cprintf("%d There is no space available for a new floor.\n",
2069 ERROR + INVALID_FLOOR_OPERATION);
2070 return;
2072 if (cflr_ok == 0) {
2073 cprintf("%d ok to create...\n", CIT_OK);
2074 return;
2076 lgetfloor(&flbuf, free_slot);
2077 flbuf.f_flags = F_INUSE;
2078 flbuf.f_ref_count = 0;
2079 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
2080 lputfloor(&flbuf, free_slot);
2081 cprintf("%d %d\n", CIT_OK, free_slot);
2087 * delete a floor
2089 void cmd_kflr(char *argbuf)
2091 struct floor flbuf;
2092 int floor_to_delete;
2093 int kflr_ok;
2094 int delete_ok;
2096 floor_to_delete = extract_int(argbuf, 0);
2097 kflr_ok = extract_int(argbuf, 1);
2099 if (CtdlAccessCheck(ac_aide)) return;
2101 lgetfloor(&flbuf, floor_to_delete);
2103 delete_ok = 1;
2104 if ((flbuf.f_flags & F_INUSE) == 0) {
2105 cprintf("%d Floor %d not in use.\n",
2106 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
2107 delete_ok = 0;
2108 } else {
2109 if (flbuf.f_ref_count != 0) {
2110 cprintf("%d Cannot delete; floor contains %d rooms.\n",
2111 ERROR + INVALID_FLOOR_OPERATION,
2112 flbuf.f_ref_count);
2113 delete_ok = 0;
2114 } else {
2115 if (kflr_ok == 1) {
2116 cprintf("%d Ok\n", CIT_OK);
2117 } else {
2118 cprintf("%d Ok to delete...\n", CIT_OK);
2125 if ((delete_ok == 1) && (kflr_ok == 1))
2126 flbuf.f_flags = 0;
2127 lputfloor(&flbuf, floor_to_delete);
2131 * edit a floor
2133 void cmd_eflr(char *argbuf)
2135 struct floor flbuf;
2136 int floor_num;
2137 int np;
2139 np = num_parms(argbuf);
2140 if (np < 1) {
2141 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
2142 return;
2145 if (CtdlAccessCheck(ac_aide)) return;
2147 floor_num = extract_int(argbuf, 0);
2148 lgetfloor(&flbuf, floor_num);
2149 if ((flbuf.f_flags & F_INUSE) == 0) {
2150 lputfloor(&flbuf, floor_num);
2151 cprintf("%d Floor %d is not in use.\n",
2152 ERROR + INVALID_FLOOR_OPERATION, floor_num);
2153 return;
2155 if (np >= 2)
2156 extract_token(flbuf.f_name, argbuf, 1, '|', sizeof flbuf.f_name);
2157 lputfloor(&flbuf, floor_num);
2159 cprintf("%d Ok\n", CIT_OK);