4 * Server functions which perform operations on room objects.
15 #include <dirent.h> /* for cmd_rdir to read contents of the directory */
17 #if TIME_WITH_SYS_TIME
18 # include <sys/time.h>
22 # include <sys/time.h>
31 #include <libcitadel.h>
36 #include "sysdep_decls.h"
40 #include "citserver.h"
42 #include "citadel_dirs.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)
56 /* for internal programs, always do everything */
57 if (((CC->internal_pgm)) && (roombuf->QRflags & QR_INUSE)) {
58 retval = (UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED);
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;
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... */
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;
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;
168 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED;
172 /* Aides can gain access to mailboxes as well, but they don't show
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 | UA_POSTALLOWED;
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;
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) {
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))
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;
234 eptr = (dptr + (sizeof lowercase_name - 1));
235 while (!IsEmptyStr(sptr) && (dptr < eptr)){
236 *dptr = tolower(*sptr);
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 */
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));
257 memcpy(qrbuf, cdbqr->ptr,
258 ((cdbqr->len > sizeof(struct ctdlroom)) ?
259 sizeof(struct ctdlroom) : cdbqr->len));
262 room_sanity_check(qrbuf);
271 * lgetroom() - same as getroom() but locks the record (if supported)
273 int lgetroom(struct ctdlroom *qrbuf, char *room_name)
276 retval = getroom(qrbuf, room_name);
277 if (retval == 0) begin_critical_section(S_ROOMS);
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];
293 bptr = lowercase_name;
294 while (!IsEmptyStr(aptr))
296 *bptr = tolower(*aptr);
302 len = bptr - lowercase_name;
304 cdb_delete(CDB_ROOMS,
305 lowercase_name, len);
307 time(&qrbuf->QRmtime);
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)
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));
355 memcpy(flbuf, cdbfl->ptr,
356 ((cdbfl->len > sizeof(struct floor)) ?
357 sizeof(struct floor) : cdbfl->len));
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;
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;
399 if (floorcache[floor_num] == NULL) {
402 end_critical_section(S_FLOORCACHE);
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),
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));
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));
489 /* Go ahead and delete it */
490 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
497 * Message pointer compare function for sort_msglist()
499 int sort_msglist_cmp(const void *m1, const void *m2) {
500 if ((*(const long *)m1) > (*(const long *)m2)) return(1);
501 if ((*(const long *)m1) < (*(const long *)m2)) return(-1);
507 * sort message pointers
508 * (returns new msg count)
510 int sort_msglist(long listptrs[], int oldcount)
520 qsort(listptrs, numitems, sizeof(long), sort_msglist_cmp);
522 /* and yank any nulls */
523 while ((numitems > 0) && (listptrs[0] == 0L)) {
524 memmove(&listptrs[0], &listptrs[1],
525 (sizeof(long) * (numitems - 1)));
534 * Determine whether a given room is non-editable.
536 int is_noneditable(struct ctdlroom *qrbuf)
539 /* Mail> rooms are non-editable */
540 if ( (qrbuf->QRflags & QR_MAILBOX)
541 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
544 /* Everything else is editable */
551 * Back-back-end for all room listing commands
553 void list_roomname(struct ctdlroom *qrbuf, int ra, int current_view, int default_view)
555 char truncated_roomname[ROOMNAMELEN];
557 /* For my own mailbox rooms, chop off the owner prefix */
558 if ( (qrbuf->QRflags & QR_MAILBOX)
559 && (atol(qrbuf->QRname) == CC->user.usernum) ) {
560 safestrncpy(truncated_roomname, qrbuf->QRname, sizeof truncated_roomname);
561 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
562 cprintf("%s", truncated_roomname);
564 /* For all other rooms, just display the name in its entirety */
566 cprintf("%s", qrbuf->QRname);
569 /* ...and now the other parameters */
570 cprintf("|%u|%d|%d|%d|%d|%d|%d|%ld|\n",
572 (int) qrbuf->QRfloor,
573 (int) qrbuf->QRorder,
574 (int) qrbuf->QRflags2,
584 * cmd_lrms() - List all accessible rooms, known or forgotten
586 void cmd_lrms_backend(struct ctdlroom *qrbuf, void *data)
588 int FloorBeingSearched = (-1);
592 FloorBeingSearched = *(int *)data;
593 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
595 if ((( ra & (UA_KNOWN | UA_ZAPPED)))
596 && ((qrbuf->QRfloor == (FloorBeingSearched))
597 || ((FloorBeingSearched) < 0)))
598 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
601 void cmd_lrms(char *argbuf)
603 int FloorBeingSearched = (-1);
604 if (!IsEmptyStr(argbuf))
605 FloorBeingSearched = extract_int(argbuf, 0);
607 if (CtdlAccessCheck(ac_logged_in)) return;
609 if (getuser(&CC->user, CC->curr_user)) {
610 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
613 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
615 ForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
622 * cmd_lkra() - List all known rooms
624 void cmd_lkra_backend(struct ctdlroom *qrbuf, void *data)
626 int FloorBeingSearched = (-1);
630 FloorBeingSearched = *(int *)data;
631 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
633 if ((( ra & (UA_KNOWN)))
634 && ((qrbuf->QRfloor == (FloorBeingSearched))
635 || ((FloorBeingSearched) < 0)))
636 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
639 void cmd_lkra(char *argbuf)
641 int FloorBeingSearched = (-1);
642 if (!IsEmptyStr(argbuf))
643 FloorBeingSearched = extract_int(argbuf, 0);
645 if (CtdlAccessCheck(ac_logged_in)) return;
647 if (getuser(&CC->user, CC->curr_user)) {
648 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
651 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
653 ForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
659 void cmd_lprm_backend(struct ctdlroom *qrbuf, void *data)
661 int FloorBeingSearched = (-1);
665 FloorBeingSearched = *(int *)data;
666 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
668 if ( ((qrbuf->QRflags & QR_PRIVATE) == 0)
669 && ((qrbuf->QRflags & QR_MAILBOX) == 0)
670 && ((qrbuf->QRfloor == (FloorBeingSearched))
671 || ((FloorBeingSearched) < 0)))
672 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
675 void cmd_lprm(char *argbuf)
677 int FloorBeingSearched = (-1);
678 if (!IsEmptyStr(argbuf))
679 FloorBeingSearched = extract_int(argbuf, 0);
681 cprintf("%d Publiic rooms:\n", LISTING_FOLLOWS);
683 ForEachRoom(cmd_lprm_backend, &FloorBeingSearched);
690 * cmd_lkrn() - List all known rooms with new messages
692 void cmd_lkrn_backend(struct ctdlroom *qrbuf, void *data)
694 int FloorBeingSearched = (-1);
698 FloorBeingSearched = *(int *)data;
699 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
702 && (ra & UA_HASNEWMSGS)
703 && ((qrbuf->QRfloor == (FloorBeingSearched))
704 || ((FloorBeingSearched) < 0)))
705 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
708 void cmd_lkrn(char *argbuf)
710 int FloorBeingSearched = (-1);
711 if (!IsEmptyStr(argbuf))
712 FloorBeingSearched = extract_int(argbuf, 0);
714 if (CtdlAccessCheck(ac_logged_in)) return;
716 if (getuser(&CC->user, CC->curr_user)) {
717 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
720 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
722 ForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
729 * cmd_lkro() - List all known rooms
731 void cmd_lkro_backend(struct ctdlroom *qrbuf, void *data)
733 int FloorBeingSearched = (-1);
737 FloorBeingSearched = *(int *)data;
738 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
741 && ((ra & UA_HASNEWMSGS) == 0)
742 && ((qrbuf->QRfloor == (FloorBeingSearched))
743 || ((FloorBeingSearched) < 0)))
744 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
747 void cmd_lkro(char *argbuf)
749 int FloorBeingSearched = (-1);
750 if (!IsEmptyStr(argbuf))
751 FloorBeingSearched = extract_int(argbuf, 0);
753 if (CtdlAccessCheck(ac_logged_in)) return;
755 if (getuser(&CC->user, CC->curr_user)) {
756 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
759 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
761 ForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
768 * cmd_lzrm() - List all forgotten rooms
770 void cmd_lzrm_backend(struct ctdlroom *qrbuf, void *data)
772 int FloorBeingSearched = (-1);
776 FloorBeingSearched = *(int *)data;
777 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
779 if ((ra & UA_GOTOALLOWED)
781 && ((qrbuf->QRfloor == (FloorBeingSearched))
782 || ((FloorBeingSearched) < 0)))
783 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
786 void cmd_lzrm(char *argbuf)
788 int FloorBeingSearched = (-1);
789 if (!IsEmptyStr(argbuf))
790 FloorBeingSearched = extract_int(argbuf, 0);
792 if (CtdlAccessCheck(ac_logged_in)) return;
794 if (getuser(&CC->user, CC->curr_user)) {
795 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
798 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
800 ForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
806 * Make the specified room the current room for this session. No validation
807 * or access control is done here -- the caller should make sure that the
808 * specified room exists and is ok to access.
810 void usergoto(char *where, int display_result, int transiently,
811 int *retmsgs, int *retnew)
814 int new_messages = 0;
815 int old_messages = 0;
816 int total_messages = 0;
820 int newmailcount = 0;
822 char truncated_roomname[ROOMNAMELEN];
823 struct cdbdata *cdbfr;
824 long *msglist = NULL;
826 unsigned int original_v_flags;
829 char setstr[128], lostr[64], histr[64];
833 /* If the supplied room name is NULL, the caller wants us to know that
834 * it has already copied the room record into CC->room, so
835 * we can skip the extra database fetch.
838 safestrncpy(CC->room.QRname, where, sizeof CC->room.QRname);
839 getroom(&CC->room, where);
842 /* Take care of all the formalities. */
844 begin_critical_section(S_USERS);
845 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
846 original_v_flags = vbuf.v_flags;
848 /* Know the room ... but not if it's the page log room, or if the
849 * caller specified that we're only entering this room transiently.
851 if ((strcasecmp(CC->room.QRname, config.c_logpages))
852 && (transiently == 0) ) {
853 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
854 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
857 /* Only rewrite the database record if we changed something */
858 if (vbuf.v_flags != original_v_flags) {
859 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
861 end_critical_section(S_USERS);
863 /* Check for new mail */
864 newmailcount = NewMailCount();
866 /* set info to 1 if the user needs to read the room's info file */
867 if (CC->room.QRinfo > vbuf.v_lastseen) {
871 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
873 msglist = (long *) cdbfr->ptr;
874 cdbfr->ptr = NULL; /* usergoto() now owns this memory */
875 num_msgs = cdbfr->len / sizeof(long);
880 for (a=0; a<num_msgs; ++a) {
881 if (msglist[a] > 0L) ++total_messages;
883 new_messages = num_msgs;
884 num_sets = num_tokens(vbuf.v_seen, ',');
885 for (s=0; s<num_sets; ++s) {
886 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
888 extract_token(lostr, setstr, 0, ':', sizeof lostr);
889 if (num_tokens(setstr, ':') >= 2) {
890 extract_token(histr, setstr, 1, ':', sizeof histr);
891 if (!strcmp(histr, "*")) {
892 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
896 strcpy(histr, lostr);
901 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
902 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
908 new_messages = total_messages - old_messages;
910 if (msglist != NULL) free(msglist);
912 if (CC->room.QRflags & QR_MAILBOX)
917 if ((CC->room.QRroomaide == CC->user.usernum)
918 || (CC->user.axlevel >= 6))
923 safestrncpy(truncated_roomname, CC->room.QRname, sizeof truncated_roomname);
924 if ( (CC->room.QRflags & QR_MAILBOX)
925 && (atol(CC->room.QRname) == CC->user.usernum) ) {
926 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
929 if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
933 if (retmsgs != NULL) *retmsgs = total_messages;
934 if (retnew != NULL) *retnew = new_messages;
935 CtdlLogPrintf(CTDL_DEBUG, "<%s> %d new of %d total messages\n",
937 new_messages, total_messages
940 CC->curr_view = (int)vbuf.v_view;
942 if (display_result) {
943 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|%d|\n",
944 CIT_OK, CtdlCheckExpress(),
949 (int)CC->room.QRflags,
950 (long)CC->room.QRhighest,
951 (long)vbuf.v_lastseen,
955 (int)CC->room.QRfloor,
957 (int)CC->room.QRdefaultview,
959 (int)CC->room.QRflags2
966 * Handle some of the macro named rooms
968 void convert_room_name_macros(char *towhere, size_t maxlen) {
969 if (!strcasecmp(towhere, "_BASEROOM_")) {
970 safestrncpy(towhere, config.c_baseroom, maxlen);
972 else if (!strcasecmp(towhere, "_MAIL_")) {
973 safestrncpy(towhere, MAILROOM, maxlen);
975 else if (!strcasecmp(towhere, "_TRASH_")) {
976 safestrncpy(towhere, USERTRASHROOM, maxlen);
978 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
979 safestrncpy(towhere, config.c_twitroom, maxlen);
981 else if (!strcasecmp(towhere, "_CALENDAR_")) {
982 safestrncpy(towhere, USERCALENDARROOM, maxlen);
984 else if (!strcasecmp(towhere, "_TASKS_")) {
985 safestrncpy(towhere, USERTASKSROOM, maxlen);
987 else if (!strcasecmp(towhere, "_CONTACTS_")) {
988 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
990 else if (!strcasecmp(towhere, "_NOTES_")) {
991 safestrncpy(towhere, USERNOTESROOM, maxlen);
997 * cmd_goto() - goto a new room
999 void cmd_goto(char *gargs)
1001 struct ctdlroom QRscratch;
1005 char augmented_roomname[ROOMNAMELEN];
1006 char towhere[ROOMNAMELEN];
1008 int transiently = 0;
1010 if (CtdlAccessCheck(ac_logged_in)) return;
1012 extract_token(towhere, gargs, 0, '|', sizeof towhere);
1013 extract_token(password, gargs, 1, '|', sizeof password);
1014 transiently = extract_int(gargs, 2);
1016 getuser(&CC->user, CC->curr_user);
1019 * Handle some of the macro named rooms
1021 convert_room_name_macros(towhere, sizeof towhere);
1023 /* First try a regular match */
1024 c = getroom(&QRscratch, towhere);
1026 /* Then try a mailbox name match */
1028 MailboxName(augmented_roomname, sizeof augmented_roomname,
1029 &CC->user, towhere);
1030 c = getroom(&QRscratch, augmented_roomname);
1032 safestrncpy(towhere, augmented_roomname, sizeof towhere);
1035 /* And if the room was found... */
1038 /* Let internal programs go directly to any room. */
1039 if (CC->internal_pgm) {
1040 memcpy(&CC->room, &QRscratch,
1041 sizeof(struct ctdlroom));
1042 usergoto(NULL, 1, transiently, NULL, NULL);
1046 /* See if there is an existing user/room relationship */
1047 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
1049 /* normal clients have to pass through security */
1050 if (ra & UA_GOTOALLOWED) {
1055 if ((QRscratch.QRflags & QR_MAILBOX) &&
1056 ((ra & UA_GOTOALLOWED))) {
1057 memcpy(&CC->room, &QRscratch,
1058 sizeof(struct ctdlroom));
1059 usergoto(NULL, 1, transiently, NULL, NULL);
1061 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
1062 ((ra & UA_KNOWN) == 0) &&
1063 (strcasecmp(QRscratch.QRpasswd, password)) &&
1064 (CC->user.axlevel < 6)
1066 cprintf("%d wrong or missing passwd\n",
1067 ERROR + PASSWORD_REQUIRED);
1069 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
1070 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
1071 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
1072 ((ra & UA_KNOWN) == 0) &&
1073 (CC->user.axlevel < 6)
1075 CtdlLogPrintf(CTDL_DEBUG, "Failed to acquire private room\n");
1077 memcpy(&CC->room, &QRscratch,
1078 sizeof(struct ctdlroom));
1079 usergoto(NULL, 1, transiently, NULL, NULL);
1085 cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
1089 void cmd_whok(char *cmdbuf)
1091 struct ctdluser temp;
1092 struct cdbdata *cdbus;
1095 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
1096 cdb_rewind(CDB_USERS);
1097 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1098 memset(&temp, 0, sizeof temp);
1099 memcpy(&temp, cdbus->ptr, sizeof temp);
1102 CtdlRoomAccess(&CC->room, &temp, &ra, NULL);
1103 if ((CC->room.QRflags & QR_INUSE)
1106 cprintf("%s\n", temp.fullname);
1113 * RDIR command for room directory
1115 void cmd_rdir(char *cmdbuf)
1120 struct stat statbuf;
1121 DIR *filedir = NULL;
1122 struct dirent *filedir_entry;
1128 if (CtdlAccessCheck(ac_logged_in)) return;
1130 getroom(&CC->room, CC->room.QRname);
1131 getuser(&CC->user, CC->curr_user);
1133 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
1134 cprintf("%d not here.\n", ERROR + NOT_HERE);
1137 if (((CC->room.QRflags & QR_VISDIR) == 0)
1138 && (CC->user.axlevel < 6)
1139 && (CC->user.usernum != CC->room.QRroomaide)) {
1140 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1144 snprintf(buf, sizeof buf, "%s/%s", ctdl_file_dir, CC->room.QRdirname);
1145 filedir = opendir (buf);
1147 if (filedir == NULL) {
1148 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1151 cprintf("%d %s|%s/%s\n", LISTING_FOLLOWS, config.c_fqdn, ctdl_file_dir, CC->room.QRdirname);
1153 snprintf(buf, sizeof buf, "%s/%s/filedir", ctdl_file_dir, CC->room.QRdirname);
1154 fd = fopen(buf, "r");
1156 fd = fopen("/dev/null", "r");
1157 while ((filedir_entry = readdir(filedir)))
1159 if (strcasecmp(filedir_entry->d_name, "filedir") && filedir_entry->d_name[0] != '.')
1161 #ifdef _DIRENT_HAVE_D_NAMELEN
1162 d_namelen = filedir_entry->d_namelen;
1164 d_namelen = strlen(filedir_entry->d_name);
1166 snprintf(buf, sizeof buf, "%s/%s/%s", ctdl_file_dir, CC->room.QRdirname, filedir_entry->d_name);
1167 stat(buf, &statbuf); /* stat the file */
1168 if (!(statbuf.st_mode & S_IFREG))
1170 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);
1171 aide_message(buf2, "RDIR found bad file");
1172 continue; /* not a useable file type so don't show it */
1174 safestrncpy(comment, "", sizeof comment);
1175 fseek(fd, 0L, 0); /* rewind descriptions file */
1176 /* Get the description from the descriptions file */
1177 while ((fgets(buf, sizeof buf, fd) != NULL) && (IsEmptyStr(comment)))
1179 buf[strlen(buf) - 1] = 0;
1180 if ((!strncasecmp(buf, filedir_entry->d_name, d_namelen)) && (buf[d_namelen] == ' '))
1181 safestrncpy(comment, &buf[d_namelen + 1], sizeof comment);
1183 len = extract_token (mimebuf, comment, 0,' ', 64);
1184 if ((len <0) || strchr(mimebuf, '/') == NULL)
1186 snprintf (mimebuf, 64, "application/octetstream");
1189 cprintf("%s|%ld|%s|%s\n",
1190 filedir_entry->d_name,
1191 (long)statbuf.st_size,
1203 * get room parameters (aide or room aide command)
1205 void cmd_getr(char *cmdbuf)
1207 if (CtdlAccessCheck(ac_room_aide)) return;
1209 getroom(&CC->room, CC->room.QRname);
1210 cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1214 ((CC->room.QRflags & QR_MAILBOX) ?
1215 &CC->room.QRname[11] : CC->room.QRname),
1217 ((CC->room.QRflags & QR_PASSWORDED) ?
1218 CC->room.QRpasswd : ""),
1220 ((CC->room.QRflags & QR_DIRECTORY) ?
1221 CC->room.QRdirname : ""),
1224 (int) CC->room.QRfloor,
1225 (int) CC->room.QRorder,
1227 CC->room.QRdefaultview,
1234 * Back end function to rename a room.
1235 * You can also specify which floor to move the room to, or specify -1 to
1236 * keep the room on the same floor it was on.
1238 * If you are renaming a mailbox room, you must supply the namespace prefix
1239 * in *at least* the old name!
1241 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1243 struct ctdlroom qrbuf;
1244 struct ctdlroom qrtmp;
1249 char actual_old_name[ROOMNAMELEN];
1251 CtdlLogPrintf(CTDL_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1252 old_name, new_name, new_floor);
1254 if (new_floor >= 0) {
1255 fl = cgetfloor(new_floor);
1256 if ((fl->f_flags & F_INUSE) == 0) {
1257 return(crr_invalid_floor);
1261 begin_critical_section(S_ROOMS);
1263 if ( (getroom(&qrtmp, new_name) == 0)
1264 && (strcasecmp(new_name, old_name)) ) {
1265 ret = crr_already_exists;
1268 else if (getroom(&qrbuf, old_name) != 0) {
1269 ret = crr_room_not_found;
1272 else if ( (CC->user.axlevel < 6) && (!CC->internal_pgm)
1273 && (CC->user.usernum != qrbuf.QRroomaide)
1274 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) ) {
1275 ret = crr_access_denied;
1278 else if (is_noneditable(&qrbuf)) {
1279 ret = crr_noneditable;
1284 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1285 if (qrbuf.QRflags & QR_MAILBOX) {
1286 owner = atol(qrbuf.QRname);
1288 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1289 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1290 "%010ld.%s", owner, new_name);
1293 safestrncpy(qrbuf.QRname, new_name,
1294 sizeof(qrbuf.QRname));
1297 /* Reject change of floor for baseroom/aideroom */
1298 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1299 !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1303 /* Take care of floor stuff */
1304 old_floor = qrbuf.QRfloor;
1305 if (new_floor < 0) {
1306 new_floor = old_floor;
1308 qrbuf.QRfloor = new_floor;
1311 begin_critical_section(S_CONFIG);
1313 /* If baseroom/aideroom name changes, update config */
1314 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1315 safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1318 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1319 safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1323 end_critical_section(S_CONFIG);
1325 /* If the room name changed, then there are now two room
1326 * records, so we have to delete the old one.
1328 if (strcasecmp(new_name, old_name)) {
1329 b_deleteroom(actual_old_name);
1335 end_critical_section(S_ROOMS);
1337 /* Adjust the floor reference counts if necessary */
1338 if (new_floor != old_floor) {
1339 lgetfloor(&flbuf, old_floor);
1340 --flbuf.f_ref_count;
1341 lputfloor(&flbuf, old_floor);
1342 CtdlLogPrintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1343 lgetfloor(&flbuf, new_floor);
1344 ++flbuf.f_ref_count;
1345 lputfloor(&flbuf, new_floor);
1346 CtdlLogPrintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1349 /* ...and everybody say "YATTA!" */
1355 * set room parameters (aide or room aide command)
1357 void cmd_setr(char *args)
1363 char new_name[ROOMNAMELEN];
1365 if (CtdlAccessCheck(ac_logged_in)) return;
1367 if (num_parms(args) >= 6) {
1368 new_floor = extract_int(args, 5);
1370 new_floor = (-1); /* don't change the floor */
1373 /* When is a new name more than just a new name? When the old name
1374 * has a namespace prefix.
1376 if (CC->room.QRflags & QR_MAILBOX) {
1377 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
1379 safestrncpy(new_name, "", sizeof new_name);
1381 extract_token(&new_name[strlen(new_name)], args, 0, '|', (sizeof new_name - strlen(new_name)));
1383 r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
1385 if (r == crr_room_not_found) {
1386 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
1387 } else if (r == crr_already_exists) {
1388 cprintf("%d '%s' already exists.\n",
1389 ERROR + ALREADY_EXISTS, new_name);
1390 } else if (r == crr_noneditable) {
1391 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
1392 } else if (r == crr_invalid_floor) {
1393 cprintf("%d Target floor does not exist.\n",
1394 ERROR + INVALID_FLOOR_OPERATION);
1395 } else if (r == crr_access_denied) {
1396 cprintf("%d You do not have permission to edit '%s'\n",
1397 ERROR + HIGHER_ACCESS_REQUIRED,
1399 } else if (r != crr_ok) {
1400 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1401 ERROR + INTERNAL_ERROR, r);
1408 getroom(&CC->room, new_name);
1410 /* Now we have to do a bunch of other stuff */
1412 if (num_parms(args) >= 7) {
1413 new_order = extract_int(args, 6);
1416 if (new_order > 127)
1420 lgetroom(&CC->room, CC->room.QRname);
1422 /* Directory room */
1423 extract_token(buf, args, 2, '|', sizeof buf);
1425 safestrncpy(CC->room.QRdirname, buf,
1426 sizeof CC->room.QRdirname);
1429 if (num_parms(args) >= 8) {
1430 CC->room.QRdefaultview = extract_int(args, 7);
1433 /* Second set of flags */
1434 if (num_parms(args) >= 9) {
1435 CC->room.QRflags2 = extract_int(args, 8);
1439 CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
1440 /* Clean up a client boo-boo: if the client set the room to
1441 * guess-name or passworded, ensure that the private flag is
1444 if ((CC->room.QRflags & QR_GUESSNAME)
1445 || (CC->room.QRflags & QR_PASSWORDED))
1446 CC->room.QRflags |= QR_PRIVATE;
1448 /* Some changes can't apply to BASEROOM */
1449 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1451 CC->room.QRorder = 0;
1452 CC->room.QRpasswd[0] = '\0';
1453 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1454 QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1455 CC->room.QRflags |= QR_PERMANENT;
1457 /* March order (doesn't apply to AIDEROOM) */
1458 if (num_parms(args) >= 7)
1459 CC->room.QRorder = (char) new_order;
1461 extract_token(buf, args, 1, '|', sizeof buf);
1463 safestrncpy(CC->room.QRpasswd, buf,
1464 sizeof CC->room.QRpasswd);
1465 /* Kick everyone out if the client requested it
1466 * (by changing the room's generation number)
1468 if (extract_int(args, 4)) {
1469 time(&CC->room.QRgen);
1472 /* Some changes can't apply to AIDEROOM */
1473 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1475 CC->room.QRorder = 0;
1476 CC->room.QRflags &= ~QR_MAILBOX;
1477 CC->room.QRflags |= QR_PERMANENT;
1480 /* Write the room record back to disk */
1481 lputroom(&CC->room);
1483 /* Create a room directory if necessary */
1484 if (CC->room.QRflags & QR_DIRECTORY) {
1485 snprintf(buf, sizeof buf,"%s/%s",
1487 CC->room.QRdirname);
1490 snprintf(buf, sizeof buf, "The room \"%s\" has been edited by %s.\n",
1491 CC->room.QRname, CC->curr_user);
1492 aide_message(buf, "Room modification Message");
1493 cprintf("%d Ok\n", CIT_OK);
1499 * get the name of the room aide for this room
1501 void cmd_geta(char *cmdbuf)
1503 struct ctdluser usbuf;
1505 if (CtdlAccessCheck(ac_logged_in)) return;
1507 if (getuserbynumber(&usbuf, CC->room.QRroomaide) == 0) {
1508 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1510 cprintf("%d \n", CIT_OK);
1516 * set the room aide for this room
1518 void cmd_seta(char *new_ra)
1520 struct ctdluser usbuf;
1525 if (CtdlAccessCheck(ac_room_aide)) return;
1527 if (getuser(&usbuf, new_ra) != 0) {
1530 newu = usbuf.usernum;
1533 lgetroom(&CC->room, CC->room.QRname);
1535 if (CC->room.QRroomaide != newu) {
1538 CC->room.QRroomaide = newu;
1539 lputroom(&CC->room);
1542 * We have to post the change notice _after_ writing changes to
1543 * the room table, otherwise it would deadlock!
1545 if (post_notice == 1) {
1546 if (!IsEmptyStr(usbuf.fullname))
1547 snprintf(buf, sizeof buf,
1548 "%s is now the room aide for \"%s\".\n",
1549 usbuf.fullname, CC->room.QRname);
1551 snprintf(buf, sizeof buf,
1552 "There is now no room aide for \"%s\".\n",
1554 aide_message(buf, "Aide Room Modification");
1556 cprintf("%d Ok\n", CIT_OK);
1560 * retrieve info file for this room
1568 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_info_dir);
1569 info_fp = fopen(filename, "r");
1571 if (info_fp == NULL) {
1572 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
1575 cprintf("%d Info:\n", LISTING_FOLLOWS);
1576 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1577 if (!IsEmptyStr(buf))
1578 buf[strlen(buf) - 1] = 0;
1579 cprintf("%s\n", buf);
1586 * Asynchronously schedule a room for deletion. The room will appear
1587 * deleted to the user(s), but it won't actually get purged from the
1588 * database until THE DREADED AUTO-PURGER makes its next run.
1590 void schedule_room_for_deletion(struct ctdlroom *qrbuf)
1592 char old_name[ROOMNAMELEN];
1595 CtdlLogPrintf(CTDL_NOTICE, "Scheduling room <%s> for deletion\n",
1598 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1600 getroom(qrbuf, qrbuf->QRname);
1602 /* Turn the room into a private mailbox owned by a user who doesn't
1603 * exist. This will immediately make the room invisible to everyone,
1604 * and qualify the room for purging.
1606 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1611 qrbuf->QRflags |= QR_MAILBOX;
1612 time(&qrbuf->QRgen); /* Use a timestamp as the new generation number */
1616 b_deleteroom(old_name);
1622 * Back end processing to delete a room and everything associated with it
1623 * (This one is synchronous and should only get called by THE DREADED
1624 * AUTO-PURGER in serv_expire.c. All user-facing code should call
1625 * the asynchronous schedule_room_for_deletion() instead.)
1627 void delete_room(struct ctdlroom *qrbuf)
1631 /* TODO: filename magic? does this realy work? */
1633 CtdlLogPrintf(CTDL_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1635 /* Delete the info file */
1636 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_info_dir);
1639 /* Delete the image file */
1640 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_image_dir);
1643 /* Delete the room's network config file */
1644 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1647 /* Delete the messages in the room
1648 * (Careful: this opens an S_ROOMS critical section!)
1650 CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
1652 /* Flag the room record as not in use */
1653 lgetroom(qrbuf, qrbuf->QRname);
1657 /* then decrement the reference count for the floor */
1658 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1659 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1660 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1662 /* Delete the room record from the database! */
1663 b_deleteroom(qrbuf->QRname);
1669 * Check access control for deleting a room
1671 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1673 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1677 if (is_noneditable(qr)) {
1682 * For mailboxes, check stuff
1684 if (qr->QRflags & QR_MAILBOX) {
1686 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1688 if (atol(qr->QRname) != CC->user.usernum) {
1689 return(0); /* not my room */
1692 /* Can't delete your Mail> room */
1693 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1695 /* Otherwise it's ok */
1700 * For normal rooms, just check for aide or room aide status.
1702 return(is_room_aide());
1706 * aide command: kill the current room
1708 void cmd_kill(char *argbuf)
1710 char deleted_room_name[ROOMNAMELEN];
1714 kill_ok = extract_int(argbuf, 0);
1716 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
1717 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1721 if (CC->room.QRflags & QR_MAILBOX) {
1722 safestrncpy(deleted_room_name, &CC->room.QRname[11], sizeof deleted_room_name);
1725 safestrncpy(deleted_room_name, CC->room.QRname, sizeof deleted_room_name);
1728 /* Do the dirty work */
1729 schedule_room_for_deletion(&CC->room);
1731 /* Return to the Lobby */
1732 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1734 /* tell the world what we did */
1735 snprintf(msg, sizeof msg, "The room \"%s\" has been deleted by %s.\n",
1736 deleted_room_name, CC->curr_user);
1737 aide_message(msg, "Room Purger Message");
1738 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1740 cprintf("%d ok to delete.\n", CIT_OK);
1746 * Internal code to create a new room (returns room flags)
1748 * Room types: 0=public, 1=guessname, 2=passworded, 3=inv-only,
1749 * 4=mailbox, 5=mailbox, but caller supplies namespace
1751 unsigned create_room(char *new_room_name,
1753 char *new_room_pass,
1760 struct ctdlroom qrbuf;
1764 CtdlLogPrintf(CTDL_DEBUG, "create_room(name=%s, type=%d, view=%d)\n",
1765 new_room_name, new_room_type, new_room_view);
1767 if (getroom(&qrbuf, new_room_name) == 0) {
1768 CtdlLogPrintf(CTDL_DEBUG, "%s already exists.\n", new_room_name);
1772 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1773 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1774 qrbuf.QRflags = QR_INUSE;
1775 if (new_room_type > 0)
1776 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1777 if (new_room_type == 1)
1778 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1779 if (new_room_type == 2)
1780 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1781 if ( (new_room_type == 4) || (new_room_type == 5) ) {
1782 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1783 /* qrbuf.QRflags2 |= QR2_SUBJECTREQ; */
1786 /* If the user is requesting a personal room, set up the room
1787 * name accordingly (prepend the user number)
1789 if (new_room_type == 4) {
1790 MailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1793 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1796 /* If the room is private, and the system administrator has elected
1797 * to automatically grant room aide privileges, do so now; otherwise,
1798 * set the room aide to undefined.
1800 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1801 qrbuf.QRroomaide = CC->user.usernum;
1803 qrbuf.QRroomaide = (-1L);
1807 * If the caller is only interested in testing whether this will work,
1808 * return now without creating the room.
1810 if (!really_create) return (qrbuf.QRflags);
1812 qrbuf.QRnumber = get_new_room_number();
1813 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1814 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1815 qrbuf.QRfloor = new_room_floor;
1816 qrbuf.QRdefaultview = new_room_view;
1818 /* save what we just did... */
1821 /* bump the reference count on whatever floor the room is on */
1822 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1823 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1824 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1826 /* Grant the creator access to the room unless the avoid_access
1827 * parameter was specified.
1829 if ( (CC->logged_in) && (avoid_access == 0) ) {
1830 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1831 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1832 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1833 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1836 /* resume our happy day */
1837 return (qrbuf.QRflags);
1844 void cmd_cre8(char *args)
1847 char new_room_name[ROOMNAMELEN];
1849 char new_room_pass[32];
1852 char *notification_message = NULL;
1855 int avoid_access = 0;
1857 cre8_ok = extract_int(args, 0);
1858 extract_token(new_room_name, args, 1, '|', sizeof new_room_name);
1859 new_room_name[ROOMNAMELEN - 1] = 0;
1860 new_room_type = extract_int(args, 2);
1861 extract_token(new_room_pass, args, 3, '|', sizeof new_room_pass);
1862 avoid_access = extract_int(args, 5);
1863 new_room_view = extract_int(args, 6);
1864 new_room_pass[9] = 0;
1867 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 1)) {
1868 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
1872 if (!strcasecmp(new_room_name, MAILROOM)) {
1873 cprintf("%d '%s' already exists.\n",
1874 ERROR + ALREADY_EXISTS, new_room_name);
1878 if (num_parms(args) >= 5) {
1879 fl = cgetfloor(extract_int(args, 4));
1881 cprintf("%d Invalid floor number.\n",
1882 ERROR + INVALID_FLOOR_OPERATION);
1885 else if ((fl->f_flags & F_INUSE) == 0) {
1886 cprintf("%d Invalid floor number.\n",
1887 ERROR + INVALID_FLOOR_OPERATION);
1890 new_room_floor = extract_int(args, 4);
1894 if (CtdlAccessCheck(ac_logged_in)) return;
1896 if (CC->user.axlevel < config.c_createax && !CC->internal_pgm) {
1897 cprintf("%d You need higher access to create rooms.\n",
1898 ERROR + HIGHER_ACCESS_REQUIRED);
1902 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 0)) {
1903 cprintf("%d Ok to create rooms.\n", CIT_OK);
1907 if ((new_room_type < 0) || (new_room_type > 5)) {
1908 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
1912 if (new_room_type == 5) {
1913 if (CC->user.axlevel < 6) {
1914 cprintf("%d Higher access required\n",
1915 ERROR + HIGHER_ACCESS_REQUIRED);
1920 /* Check to make sure the requested room name doesn't already exist */
1921 newflags = create_room(new_room_name,
1922 new_room_type, new_room_pass, new_room_floor,
1923 0, avoid_access, new_room_view);
1924 if (newflags == 0) {
1925 cprintf("%d '%s' already exists.\n",
1926 ERROR + ALREADY_EXISTS, new_room_name);
1931 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
1935 /* If we reach this point, the room needs to be created. */
1937 newflags = create_room(new_room_name,
1938 new_room_type, new_room_pass, new_room_floor, 1, 0,
1941 /* post a message in Aide> describing the new room */
1942 notification_message = malloc(1024);
1943 snprintf(notification_message, 1024,
1944 "A new room called \"%s\" has been created by %s%s%s%s%s%s\n",
1947 ((newflags & QR_MAILBOX) ? " [personal]" : ""),
1948 ((newflags & QR_PRIVATE) ? " [private]" : ""),
1949 ((newflags & QR_GUESSNAME) ? " [hidden]" : ""),
1950 ((newflags & QR_PASSWORDED) ? " Password: " : ""),
1951 ((newflags & QR_PASSWORDED) ? new_room_pass : "")
1953 aide_message(notification_message, "Room Creation Message");
1954 free(notification_message);
1956 cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
1961 void cmd_einf(char *ok)
1962 { /* enter info file for current room */
1964 char infofilename[SIZ];
1969 if (CtdlAccessCheck(ac_room_aide)) return;
1971 if (atoi(ok) == 0) {
1972 cprintf("%d Ok.\n", CIT_OK);
1975 assoc_file_name(infofilename, sizeof infofilename, &CC->room, ctdl_info_dir);
1976 CtdlLogPrintf(CTDL_DEBUG, "opening\n");
1977 fp = fopen(infofilename, "w");
1978 CtdlLogPrintf(CTDL_DEBUG, "checking\n");
1980 cprintf("%d Cannot open %s: %s\n",
1981 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1984 cprintf("%d Send info...\n", SEND_LISTING);
1987 client_getln(buf, sizeof buf);
1988 if (strcmp(buf, "000"))
1989 fprintf(fp, "%s\n", buf);
1990 } while (strcmp(buf, "000"));
1993 /* now update the room index so people will see our new info */
1994 lgetroom(&CC->room, CC->room.QRname); /* lock so no one steps on us */
1995 CC->room.QRinfo = CC->room.QRhighest + 1L;
1996 lputroom(&CC->room);
2001 * cmd_lflr() - List all known floors
2008 if (CtdlAccessCheck(ac_logged_in)) return;
2010 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
2012 for (a = 0; a < MAXFLOORS; ++a) {
2013 getfloor(&flbuf, a);
2014 if (flbuf.f_flags & F_INUSE) {
2015 cprintf("%d|%s|%d\n",
2027 * create a new floor
2029 void cmd_cflr(char *argbuf)
2031 char new_floor_name[256];
2034 int free_slot = (-1);
2037 extract_token(new_floor_name, argbuf, 0, '|', sizeof new_floor_name);
2038 cflr_ok = extract_int(argbuf, 1);
2040 if (CtdlAccessCheck(ac_aide)) return;
2042 if (IsEmptyStr(new_floor_name)) {
2043 cprintf("%d Blank floor name not allowed.\n",
2044 ERROR + ILLEGAL_VALUE);
2048 for (a = 0; a < MAXFLOORS; ++a) {
2049 getfloor(&flbuf, a);
2051 /* note any free slots while we're scanning... */
2052 if (((flbuf.f_flags & F_INUSE) == 0)
2056 /* check to see if it already exists */
2057 if ((!strcasecmp(flbuf.f_name, new_floor_name))
2058 && (flbuf.f_flags & F_INUSE)) {
2059 cprintf("%d Floor '%s' already exists.\n",
2060 ERROR + ALREADY_EXISTS,
2066 if (free_slot < 0) {
2067 cprintf("%d There is no space available for a new floor.\n",
2068 ERROR + INVALID_FLOOR_OPERATION);
2072 cprintf("%d ok to create...\n", CIT_OK);
2075 lgetfloor(&flbuf, free_slot);
2076 flbuf.f_flags = F_INUSE;
2077 flbuf.f_ref_count = 0;
2078 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
2079 lputfloor(&flbuf, free_slot);
2080 cprintf("%d %d\n", CIT_OK, free_slot);
2088 void cmd_kflr(char *argbuf)
2091 int floor_to_delete;
2095 floor_to_delete = extract_int(argbuf, 0);
2096 kflr_ok = extract_int(argbuf, 1);
2098 if (CtdlAccessCheck(ac_aide)) return;
2100 lgetfloor(&flbuf, floor_to_delete);
2103 if ((flbuf.f_flags & F_INUSE) == 0) {
2104 cprintf("%d Floor %d not in use.\n",
2105 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
2108 if (flbuf.f_ref_count != 0) {
2109 cprintf("%d Cannot delete; floor contains %d rooms.\n",
2110 ERROR + INVALID_FLOOR_OPERATION,
2115 cprintf("%d Ok\n", CIT_OK);
2117 cprintf("%d Ok to delete...\n", CIT_OK);
2124 if ((delete_ok == 1) && (kflr_ok == 1))
2126 lputfloor(&flbuf, floor_to_delete);
2132 void cmd_eflr(char *argbuf)
2138 np = num_parms(argbuf);
2140 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
2144 if (CtdlAccessCheck(ac_aide)) return;
2146 floor_num = extract_int(argbuf, 0);
2147 lgetfloor(&flbuf, floor_num);
2148 if ((flbuf.f_flags & F_INUSE) == 0) {
2149 lputfloor(&flbuf, floor_num);
2150 cprintf("%d Floor %d is not in use.\n",
2151 ERROR + INVALID_FLOOR_OPERATION, floor_num);
2155 extract_token(flbuf.f_name, argbuf, 1, '|', sizeof flbuf.f_name);
2156 lputfloor(&flbuf, floor_num);
2158 cprintf("%d Ok\n", CIT_OK);