4 * Server functions which perform operations on room objects.
16 #if TIME_WITH_SYS_TIME
17 # include <sys/time.h>
21 # include <sys/time.h>
31 #include "serv_extensions.h"
35 #include "sysdep_decls.h"
39 #include "citserver.h"
42 #include "citadel_dirs.h"
44 struct floor *floorcache[MAXFLOORS];
47 * Retrieve access control information for any user/room pair
49 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf,
50 int *result, int *view)
55 /* for internal programs, always do everything */
56 if (((CC->internal_pgm)) && (roombuf->QRflags & QR_INUSE)) {
57 retval = (UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED);
62 /* Locate any applicable user/room relationships */
63 CtdlGetRelationship(&vbuf, userbuf, roombuf);
65 /* Force the properties of the Aide room */
66 if (!strcasecmp(roombuf->QRname, config.c_aideroom)) {
67 if (userbuf->axlevel >= 6) {
68 retval = UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
75 /* If this is a public room, it's accessible... */
76 if ( ((roombuf->QRflags & QR_PRIVATE) == 0)
77 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
78 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
81 /* If this is a preferred users only room, check access level */
82 if (roombuf->QRflags & QR_PREFONLY) {
83 if (userbuf->axlevel < 5) {
84 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
88 /* For private rooms, check the generation number matchups */
89 if ( (roombuf->QRflags & QR_PRIVATE)
90 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
92 /* An explicit match means the user belongs in this room */
93 if (vbuf.v_flags & V_ACCESS) {
94 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
96 /* Otherwise, check if this is a guess-name or passworded
97 * room. If it is, a goto may at least be attempted
99 else if ((roombuf->QRflags & QR_PRIVATE)
100 || (roombuf->QRflags & QR_PASSWORDED)) {
101 retval = retval & ~UA_KNOWN;
102 retval = retval | UA_GOTOALLOWED;
106 /* For mailbox rooms, also check the namespace */
107 /* Also, mailbox owners can delete their messages */
108 if (roombuf->QRflags & QR_MAILBOX) {
109 if (userbuf->usernum == atol(roombuf->QRname)) {
110 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
112 /* An explicit match means the user belongs in this room */
113 if (vbuf.v_flags & V_ACCESS) {
114 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
118 /* For non-mailbox rooms... */
121 /* User is allowed to post in the room unless:
122 * - User is not validated
123 * - User has no net privileges and it is a shared network room
124 * - It is a read-only room
126 int post_allowed = 1;
127 if (CC->user.axlevel < 2) post_allowed = 0;
128 if ((CC->user.axlevel < 4) && (CC->room.QRflags & QR_NETWORK)) post_allowed = 0;
129 if (roombuf->QRflags & QR_READONLY) post_allowed = 0;
131 retval = retval | UA_POSTALLOWED;
134 /* If "collaborative deletion" is active for this room, any user who can post
135 * is also allowed to delete
137 if (CC->room.QRflags2 & QR2_COLLABDEL) {
138 if (retval & UA_POSTALLOWED) {
139 retval = retval | UA_DELETEALLOWED;
145 /* Check to see if the user has forgotten this room */
146 if (vbuf.v_flags & V_FORGET) {
147 retval = retval & ~UA_KNOWN;
148 if ( ( ((roombuf->QRflags & QR_PRIVATE) == 0)
149 && ((roombuf->QRflags & QR_MAILBOX) == 0) )
150 || ( (roombuf->QRflags & QR_MAILBOX)
151 && (atol(roombuf->QRname) == CC->user.usernum))) {
152 retval = retval | UA_ZAPPED;
155 /* If user is explicitly locked out of this room, deny everything */
156 if (vbuf.v_flags & V_LOCKOUT) {
157 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED & ~UA_POSTALLOWED;
160 /* Aides get access to all private rooms */
161 if ( (userbuf->axlevel >= 6)
162 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
163 if (vbuf.v_flags & V_FORGET) {
164 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED;
167 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED;
171 /* Aides can gain access to mailboxes as well, but they don't show
174 if ( (userbuf->axlevel >= 6)
175 && (roombuf->QRflags & QR_MAILBOX) ) {
176 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED;
179 /* Aides and Room Aides have admin privileges */
180 if ( (userbuf->axlevel >= 6)
181 || (userbuf->usernum == roombuf->QRroomaide)
183 retval = retval | UA_ADMINALLOWED | UA_DELETEALLOWED;
186 NEWMSG: /* By the way, we also check for the presence of new messages */
187 if (is_msg_in_sequence_set(vbuf.v_seen, roombuf->QRhighest) == 0) {
188 retval = retval | UA_HASNEWMSGS;
191 /* System rooms never show up in the list. */
192 if (roombuf->QRflags2 & QR2_SYSTEM) {
193 retval = retval & ~UA_KNOWN;
197 /* Now give the caller the information it wants. */
198 if (result != NULL) *result = retval;
199 if (view != NULL) *view = vbuf.v_view;
203 * Self-checking stuff for a room record read into memory
205 void room_sanity_check(struct ctdlroom *qrbuf)
207 /* Mailbox rooms are always on the lowest floor */
208 if (qrbuf->QRflags & QR_MAILBOX) {
211 /* Listing order of 0 is illegal except for base rooms */
212 if (qrbuf->QRorder == 0)
213 if (!(qrbuf->QRflags & QR_MAILBOX) &&
214 strncasecmp(qrbuf->QRname, config.c_baseroom, ROOMNAMELEN)
216 strncasecmp(qrbuf->QRname, config.c_aideroom, ROOMNAMELEN))
222 * getroom() - retrieve room data from disk
224 int getroom(struct ctdlroom *qrbuf, char *room_name)
226 struct cdbdata *cdbqr;
227 char lowercase_name[ROOMNAMELEN];
228 char personal_lowercase_name[ROOMNAMELEN];
231 for (a = 0; room_name[a] && a < sizeof lowercase_name - 1; ++a) {
232 lowercase_name[a] = tolower(room_name[a]);
234 lowercase_name[a] = 0;
236 memset(qrbuf, 0, sizeof(struct ctdlroom));
238 /* First, try the public namespace */
239 cdbqr = cdb_fetch(CDB_ROOMS,
240 lowercase_name, strlen(lowercase_name));
242 /* If that didn't work, try the user's personal namespace */
244 snprintf(personal_lowercase_name,
245 sizeof personal_lowercase_name, "%010ld.%s",
246 CC->user.usernum, lowercase_name);
247 cdbqr = cdb_fetch(CDB_ROOMS,
248 personal_lowercase_name,
249 strlen(personal_lowercase_name));
252 memcpy(qrbuf, cdbqr->ptr,
253 ((cdbqr->len > sizeof(struct ctdlroom)) ?
254 sizeof(struct ctdlroom) : cdbqr->len));
257 room_sanity_check(qrbuf);
266 * lgetroom() - same as getroom() but locks the record (if supported)
268 int lgetroom(struct ctdlroom *qrbuf, char *room_name)
271 retval = getroom(qrbuf, room_name);
272 if (retval == 0) begin_critical_section(S_ROOMS);
278 * b_putroom() - back end to putroom() and b_deleteroom()
279 * (if the supplied buffer is NULL, delete the room record)
281 void b_putroom(struct ctdlroom *qrbuf, char *room_name)
283 char lowercase_name[ROOMNAMELEN];
286 for (a = 0; a <= strlen(room_name); ++a) {
287 lowercase_name[a] = tolower(room_name[a]);
291 cdb_delete(CDB_ROOMS,
292 lowercase_name, strlen(lowercase_name));
294 time(&qrbuf->QRmtime);
296 lowercase_name, strlen(lowercase_name),
297 qrbuf, sizeof(struct ctdlroom));
303 * putroom() - store room data to disk
305 void putroom(struct ctdlroom *qrbuf) {
306 b_putroom(qrbuf, qrbuf->QRname);
311 * b_deleteroom() - delete a room record from disk
313 void b_deleteroom(char *room_name) {
314 b_putroom(NULL, room_name);
320 * lputroom() - same as putroom() but unlocks the record (if supported)
322 void lputroom(struct ctdlroom *qrbuf)
326 end_critical_section(S_ROOMS);
330 /****************************************************************************/
333 * getfloor() - retrieve floor data from disk
335 void getfloor(struct floor *flbuf, int floor_num)
337 struct cdbdata *cdbfl;
339 memset(flbuf, 0, sizeof(struct floor));
340 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
342 memcpy(flbuf, cdbfl->ptr,
343 ((cdbfl->len > sizeof(struct floor)) ?
344 sizeof(struct floor) : cdbfl->len));
347 if (floor_num == 0) {
348 safestrncpy(flbuf->f_name, "Main Floor",
349 sizeof flbuf->f_name);
350 flbuf->f_flags = F_INUSE;
351 flbuf->f_ref_count = 3;
358 * lgetfloor() - same as getfloor() but locks the record (if supported)
360 void lgetfloor(struct floor *flbuf, int floor_num)
363 begin_critical_section(S_FLOORTAB);
364 getfloor(flbuf, floor_num);
369 * cgetfloor() - Get floor record from *cache* (loads from disk if needed)
371 * This is strictly a performance hack.
373 struct floor *cgetfloor(int floor_num) {
374 static int initialized = 0;
377 struct floor *fl = NULL;
379 begin_critical_section(S_FLOORCACHE);
380 if (initialized == 0) {
381 for (i=0; i<MAXFLOORS; ++i) {
382 floorcache[floor_num] = NULL;
386 if (floorcache[floor_num] == NULL) {
389 end_critical_section(S_FLOORCACHE);
392 fl = malloc(sizeof(struct floor));
393 getfloor(fl, floor_num);
394 begin_critical_section(S_FLOORCACHE);
395 if (floorcache[floor_num] != NULL) {
396 free(floorcache[floor_num]);
398 floorcache[floor_num] = fl;
399 end_critical_section(S_FLOORCACHE);
402 return(floorcache[floor_num]);
408 * putfloor() - store floor data on disk
410 void putfloor(struct floor *flbuf, int floor_num)
412 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
413 begin_critical_section(S_FLOORCACHE);
414 if (floorcache[floor_num] != NULL) {
415 free(floorcache[floor_num]);
416 floorcache[floor_num] = malloc(sizeof(struct floor));
417 memcpy(floorcache[floor_num], flbuf, sizeof(struct floor));
419 end_critical_section(S_FLOORCACHE);
421 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
422 flbuf, sizeof(struct floor));
427 * lputfloor() - same as putfloor() but unlocks the record (if supported)
429 void lputfloor(struct floor *flbuf, int floor_num)
432 putfloor(flbuf, floor_num);
433 end_critical_section(S_FLOORTAB);
439 * Traverse the room file...
441 void ForEachRoom(void (*CallBack) (struct ctdlroom *EachRoom, void *out_data),
444 struct ctdlroom qrbuf;
445 struct cdbdata *cdbqr;
447 cdb_rewind(CDB_ROOMS);
449 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
450 memset(&qrbuf, 0, sizeof(struct ctdlroom));
451 memcpy(&qrbuf, cdbqr->ptr,
452 ((cdbqr->len > sizeof(struct ctdlroom)) ?
453 sizeof(struct ctdlroom) : cdbqr->len));
455 room_sanity_check(&qrbuf);
456 if (qrbuf.QRflags & QR_INUSE)
457 (*CallBack)(&qrbuf, in_data);
463 * delete_msglist() - delete room message pointers
465 void delete_msglist(struct ctdlroom *whichroom)
467 struct cdbdata *cdbml;
469 /* Make sure the msglist we're deleting actually exists, otherwise
470 * gdbm will complain when we try to delete an invalid record
472 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
476 /* Go ahead and delete it */
477 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
485 * sort message pointers
486 * (returns new msg count)
488 int sort_msglist(long listptrs[], int oldcount)
499 for (a = numitems - 2; a >= 0; --a) {
500 for (b = 0; b <= a; ++b) {
501 if (listptrs[b] > (listptrs[b + 1])) {
503 hold2 = listptrs[b + 1];
505 listptrs[b + 1] = hold1;
510 /* and yank any nulls */
511 while ((numitems > 0) && (listptrs[0] == 0L)) {
512 memcpy(&listptrs[0], &listptrs[1],
513 (sizeof(long) * (numitems - 1)));
522 * Determine whether a given room is non-editable.
524 int is_noneditable(struct ctdlroom *qrbuf)
527 /* Mail> rooms are non-editable */
528 if ( (qrbuf->QRflags & QR_MAILBOX)
529 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
532 /* Everything else is editable */
539 * Back-back-end for all room listing commands
541 void list_roomname(struct ctdlroom *qrbuf, int ra, int current_view, int default_view)
543 char truncated_roomname[ROOMNAMELEN];
545 /* For my own mailbox rooms, chop off the owner prefix */
546 if ( (qrbuf->QRflags & QR_MAILBOX)
547 && (atol(qrbuf->QRname) == CC->user.usernum) ) {
548 safestrncpy(truncated_roomname, qrbuf->QRname, sizeof truncated_roomname);
549 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
550 cprintf("%s", truncated_roomname);
552 /* For all other rooms, just display the name in its entirety */
554 cprintf("%s", qrbuf->QRname);
557 /* ...and now the other parameters */
558 cprintf("|%u|%d|%d|%d|%d|%d|%d|%ld|\n",
560 (int) qrbuf->QRfloor,
561 (int) qrbuf->QRorder,
562 (int) qrbuf->QRflags2,
572 * cmd_lrms() - List all accessible rooms, known or forgotten
574 void cmd_lrms_backend(struct ctdlroom *qrbuf, void *data)
576 int FloorBeingSearched = (-1);
580 FloorBeingSearched = *(int *)data;
581 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
583 if ((( ra & (UA_KNOWN | UA_ZAPPED)))
584 && ((qrbuf->QRfloor == (FloorBeingSearched))
585 || ((FloorBeingSearched) < 0)))
586 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
589 void cmd_lrms(char *argbuf)
591 int FloorBeingSearched = (-1);
592 if (strlen(argbuf) > 0)
593 FloorBeingSearched = extract_int(argbuf, 0);
595 if (CtdlAccessCheck(ac_logged_in)) return;
597 if (getuser(&CC->user, CC->curr_user)) {
598 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
601 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
603 ForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
610 * cmd_lkra() - List all known rooms
612 void cmd_lkra_backend(struct ctdlroom *qrbuf, void *data)
614 int FloorBeingSearched = (-1);
618 FloorBeingSearched = *(int *)data;
619 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
621 if ((( ra & (UA_KNOWN)))
622 && ((qrbuf->QRfloor == (FloorBeingSearched))
623 || ((FloorBeingSearched) < 0)))
624 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
627 void cmd_lkra(char *argbuf)
629 int FloorBeingSearched = (-1);
630 if (strlen(argbuf) > 0)
631 FloorBeingSearched = extract_int(argbuf, 0);
633 if (CtdlAccessCheck(ac_logged_in)) return;
635 if (getuser(&CC->user, CC->curr_user)) {
636 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
639 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
641 ForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
647 void cmd_lprm_backend(struct ctdlroom *qrbuf, void *data)
649 int FloorBeingSearched = (-1);
653 FloorBeingSearched = *(int *)data;
654 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
656 if ( ((qrbuf->QRflags & QR_PRIVATE) == 0)
657 && ((qrbuf->QRflags & QR_MAILBOX) == 0)
658 && ((qrbuf->QRfloor == (FloorBeingSearched))
659 || ((FloorBeingSearched) < 0)))
660 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
663 void cmd_lprm(char *argbuf)
665 int FloorBeingSearched = (-1);
666 if (strlen(argbuf) > 0)
667 FloorBeingSearched = extract_int(argbuf, 0);
669 cprintf("%d Publiic rooms:\n", LISTING_FOLLOWS);
671 ForEachRoom(cmd_lprm_backend, &FloorBeingSearched);
678 * cmd_lkrn() - List all known rooms with new messages
680 void cmd_lkrn_backend(struct ctdlroom *qrbuf, void *data)
682 int FloorBeingSearched = (-1);
686 FloorBeingSearched = *(int *)data;
687 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
690 && (ra & UA_HASNEWMSGS)
691 && ((qrbuf->QRfloor == (FloorBeingSearched))
692 || ((FloorBeingSearched) < 0)))
693 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
696 void cmd_lkrn(char *argbuf)
698 int FloorBeingSearched = (-1);
699 if (strlen(argbuf) > 0)
700 FloorBeingSearched = extract_int(argbuf, 0);
702 if (CtdlAccessCheck(ac_logged_in)) return;
704 if (getuser(&CC->user, CC->curr_user)) {
705 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
708 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
710 ForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
717 * cmd_lkro() - List all known rooms
719 void cmd_lkro_backend(struct ctdlroom *qrbuf, void *data)
721 int FloorBeingSearched = (-1);
725 FloorBeingSearched = *(int *)data;
726 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
729 && ((ra & UA_HASNEWMSGS) == 0)
730 && ((qrbuf->QRfloor == (FloorBeingSearched))
731 || ((FloorBeingSearched) < 0)))
732 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
735 void cmd_lkro(char *argbuf)
737 int FloorBeingSearched = (-1);
738 if (strlen(argbuf) > 0)
739 FloorBeingSearched = extract_int(argbuf, 0);
741 if (CtdlAccessCheck(ac_logged_in)) return;
743 if (getuser(&CC->user, CC->curr_user)) {
744 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
747 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
749 ForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
756 * cmd_lzrm() - List all forgotten rooms
758 void cmd_lzrm_backend(struct ctdlroom *qrbuf, void *data)
760 int FloorBeingSearched = (-1);
764 FloorBeingSearched = *(int *)data;
765 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
767 if ((ra & UA_GOTOALLOWED)
769 && ((qrbuf->QRfloor == (FloorBeingSearched))
770 || ((FloorBeingSearched) < 0)))
771 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
774 void cmd_lzrm(char *argbuf)
776 int FloorBeingSearched = (-1);
777 if (strlen(argbuf) > 0)
778 FloorBeingSearched = extract_int(argbuf, 0);
780 if (CtdlAccessCheck(ac_logged_in)) return;
782 if (getuser(&CC->user, CC->curr_user)) {
783 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
786 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
788 ForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
794 * Make the specified room the current room for this session. No validation
795 * or access control is done here -- the caller should make sure that the
796 * specified room exists and is ok to access.
798 void usergoto(char *where, int display_result, int transiently,
799 int *retmsgs, int *retnew)
802 int new_messages = 0;
803 int old_messages = 0;
804 int total_messages = 0;
808 int newmailcount = 0;
810 char truncated_roomname[ROOMNAMELEN];
811 struct cdbdata *cdbfr;
812 long *msglist = NULL;
814 unsigned int original_v_flags;
817 char setstr[128], lostr[64], histr[64];
821 /* If the supplied room name is NULL, the caller wants us to know that
822 * it has already copied the room record into CC->room, so
823 * we can skip the extra database fetch.
826 safestrncpy(CC->room.QRname, where, sizeof CC->room.QRname);
827 getroom(&CC->room, where);
830 /* Take care of all the formalities. */
832 begin_critical_section(S_USERS);
833 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
834 original_v_flags = vbuf.v_flags;
836 /* Know the room ... but not if it's the page log room, or if the
837 * caller specified that we're only entering this room transiently.
839 if ((strcasecmp(CC->room.QRname, config.c_logpages))
840 && (transiently == 0) ) {
841 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
842 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
845 /* Only rewrite the database record if we changed something */
846 if (vbuf.v_flags != original_v_flags) {
847 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
849 end_critical_section(S_USERS);
851 /* Check for new mail */
852 newmailcount = NewMailCount();
854 /* set info to 1 if the user needs to read the room's info file */
855 if (CC->room.QRinfo > vbuf.v_lastseen) {
859 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
861 msglist = (long *) cdbfr->ptr;
862 cdbfr->ptr = NULL; /* usergoto() now owns this memory */
863 num_msgs = cdbfr->len / sizeof(long);
868 for (a=0; a<num_msgs; ++a) {
869 if (msglist[a] > 0L) ++total_messages;
871 new_messages = num_msgs;
872 num_sets = num_tokens(vbuf.v_seen, ',');
873 for (s=0; s<num_sets; ++s) {
874 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
876 extract_token(lostr, setstr, 0, ':', sizeof lostr);
877 if (num_tokens(setstr, ':') >= 2) {
878 extract_token(histr, setstr, 1, ':', sizeof histr);
879 if (!strcmp(histr, "*")) {
880 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
884 strcpy(histr, lostr);
889 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
890 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
896 new_messages = total_messages - old_messages;
898 if (msglist != NULL) free(msglist);
900 if (CC->room.QRflags & QR_MAILBOX)
905 if ((CC->room.QRroomaide == CC->user.usernum)
906 || (CC->user.axlevel >= 6))
911 safestrncpy(truncated_roomname, CC->room.QRname, sizeof truncated_roomname);
912 if ( (CC->room.QRflags & QR_MAILBOX)
913 && (atol(CC->room.QRname) == CC->user.usernum) ) {
914 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
917 if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
921 if (retmsgs != NULL) *retmsgs = total_messages;
922 if (retnew != NULL) *retnew = new_messages;
923 lprintf(CTDL_DEBUG, "<%s> %d new of %d total messages\n",
925 new_messages, total_messages
928 CC->curr_view = (int)vbuf.v_view;
930 if (display_result) {
931 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|%d|\n",
932 CIT_OK, CtdlCheckExpress(),
937 (int)CC->room.QRflags,
938 (long)CC->room.QRhighest,
939 (long)vbuf.v_lastseen,
943 (int)CC->room.QRfloor,
945 (int)CC->room.QRdefaultview,
947 (int)CC->room.QRflags2
954 * Handle some of the macro named rooms
956 void convert_room_name_macros(char *towhere, size_t maxlen) {
957 if (!strcasecmp(towhere, "_BASEROOM_")) {
958 safestrncpy(towhere, config.c_baseroom, maxlen);
960 else if (!strcasecmp(towhere, "_MAIL_")) {
961 safestrncpy(towhere, MAILROOM, maxlen);
963 else if (!strcasecmp(towhere, "_TRASH_")) {
964 safestrncpy(towhere, USERTRASHROOM, maxlen);
966 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
967 safestrncpy(towhere, config.c_twitroom, maxlen);
969 else if (!strcasecmp(towhere, "_CALENDAR_")) {
970 safestrncpy(towhere, USERCALENDARROOM, maxlen);
972 else if (!strcasecmp(towhere, "_TASKS_")) {
973 safestrncpy(towhere, USERTASKSROOM, maxlen);
975 else if (!strcasecmp(towhere, "_CONTACTS_")) {
976 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
978 else if (!strcasecmp(towhere, "_NOTES_")) {
979 safestrncpy(towhere, USERNOTESROOM, maxlen);
985 * cmd_goto() - goto a new room
987 void cmd_goto(char *gargs)
989 struct ctdlroom QRscratch;
993 char augmented_roomname[ROOMNAMELEN];
994 char towhere[ROOMNAMELEN];
998 if (CtdlAccessCheck(ac_logged_in)) return;
1000 extract_token(towhere, gargs, 0, '|', sizeof towhere);
1001 extract_token(password, gargs, 1, '|', sizeof password);
1002 transiently = extract_int(gargs, 2);
1004 getuser(&CC->user, CC->curr_user);
1007 * Handle some of the macro named rooms
1009 convert_room_name_macros(towhere, sizeof towhere);
1011 /* First try a regular match */
1012 c = getroom(&QRscratch, towhere);
1014 /* Then try a mailbox name match */
1016 MailboxName(augmented_roomname, sizeof augmented_roomname,
1017 &CC->user, towhere);
1018 c = getroom(&QRscratch, augmented_roomname);
1020 safestrncpy(towhere, augmented_roomname, sizeof towhere);
1023 /* And if the room was found... */
1026 /* Let internal programs go directly to any room. */
1027 if (CC->internal_pgm) {
1028 memcpy(&CC->room, &QRscratch,
1029 sizeof(struct ctdlroom));
1030 usergoto(NULL, 1, transiently, NULL, NULL);
1034 /* See if there is an existing user/room relationship */
1035 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
1037 /* normal clients have to pass through security */
1038 if (ra & UA_GOTOALLOWED) {
1043 if ((QRscratch.QRflags & QR_MAILBOX) &&
1044 ((ra & UA_GOTOALLOWED))) {
1045 memcpy(&CC->room, &QRscratch,
1046 sizeof(struct ctdlroom));
1047 usergoto(NULL, 1, transiently, NULL, NULL);
1049 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
1050 ((ra & UA_KNOWN) == 0) &&
1051 (strcasecmp(QRscratch.QRpasswd, password)) &&
1052 (CC->user.axlevel < 6)
1054 cprintf("%d wrong or missing passwd\n",
1055 ERROR + PASSWORD_REQUIRED);
1057 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
1058 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
1059 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
1060 ((ra & UA_KNOWN) == 0) &&
1061 (CC->user.axlevel < 6)
1063 lprintf(CTDL_DEBUG, "Failed to acquire private room\n");
1065 memcpy(&CC->room, &QRscratch,
1066 sizeof(struct ctdlroom));
1067 usergoto(NULL, 1, transiently, NULL, NULL);
1073 cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
1079 struct ctdluser temp;
1080 struct cdbdata *cdbus;
1083 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
1084 cdb_rewind(CDB_USERS);
1085 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1086 memset(&temp, 0, sizeof temp);
1087 memcpy(&temp, cdbus->ptr, sizeof temp);
1090 CtdlRoomAccess(&CC->room, &temp, &ra, NULL);
1091 if ((CC->room.QRflags & QR_INUSE)
1094 cprintf("%s\n", temp.fullname);
1101 * RDIR command for room directory
1109 struct stat statbuf;
1110 char tempfilename[PATH_MAX];
1112 if (CtdlAccessCheck(ac_logged_in)) return;
1113 CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
1115 getroom(&CC->room, CC->room.QRname);
1116 getuser(&CC->user, CC->curr_user);
1118 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
1119 cprintf("%d not here.\n", ERROR + NOT_HERE);
1122 if (((CC->room.QRflags & QR_VISDIR) == 0)
1123 && (CC->user.axlevel < 6)
1124 && (CC->user.usernum != CC->room.QRroomaide)) {
1125 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1128 cprintf("%d %s|%s/%s\n",
1132 CC->room.QRdirname);
1134 snprintf(buf, sizeof buf,
1135 "ls %s/%s >%s 2>/dev/null",
1141 snprintf(buf, sizeof buf,
1144 CC->room.QRdirname);
1145 fd = fopen(buf, "r");
1147 fd = fopen("/dev/null", "r");
1149 ls = fopen(tempfilename, "r");
1150 while (fgets(flnm, sizeof flnm, ls) != NULL) {
1151 flnm[strlen(flnm) - 1] = 0;
1152 if (strcasecmp(flnm, "filedir")) {
1153 snprintf(buf, sizeof buf,
1158 stat(buf, &statbuf);
1159 safestrncpy(comment, "", sizeof comment);
1161 while ((fgets(buf, sizeof buf, fd) != NULL)
1162 && (strlen(comment) == 0)) {
1163 buf[strlen(buf) - 1] = 0;
1164 if ((!strncasecmp(buf, flnm, strlen(flnm)))
1165 && (buf[strlen(flnm)] == ' '))
1166 safestrncpy(comment,
1167 &buf[strlen(flnm) + 1],
1170 cprintf("%s|%ld|%s\n", flnm, (long)statbuf.st_size, comment);
1175 unlink(tempfilename);
1181 * get room parameters (aide or room aide command)
1185 if (CtdlAccessCheck(ac_room_aide)) return;
1187 getroom(&CC->room, CC->room.QRname);
1188 cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1192 ((CC->room.QRflags & QR_MAILBOX) ?
1193 &CC->room.QRname[11] : CC->room.QRname),
1195 ((CC->room.QRflags & QR_PASSWORDED) ?
1196 CC->room.QRpasswd : ""),
1198 ((CC->room.QRflags & QR_DIRECTORY) ?
1199 CC->room.QRdirname : ""),
1202 (int) CC->room.QRfloor,
1203 (int) CC->room.QRorder,
1205 CC->room.QRdefaultview,
1212 * Back end function to rename a room.
1213 * You can also specify which floor to move the room to, or specify -1 to
1214 * keep the room on the same floor it was on.
1216 * If you are renaming a mailbox room, you must supply the namespace prefix
1217 * in *at least* the old name!
1219 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1221 struct ctdlroom qrbuf;
1222 struct ctdlroom qrtmp;
1227 char actual_old_name[ROOMNAMELEN];
1229 lprintf(CTDL_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1230 old_name, new_name, new_floor);
1232 if (new_floor >= 0) {
1233 fl = cgetfloor(new_floor);
1234 if ((fl->f_flags & F_INUSE) == 0) {
1235 return(crr_invalid_floor);
1239 begin_critical_section(S_ROOMS);
1241 if ( (getroom(&qrtmp, new_name) == 0)
1242 && (strcasecmp(new_name, old_name)) ) {
1243 ret = crr_already_exists;
1246 else if (getroom(&qrbuf, old_name) != 0) {
1247 ret = crr_room_not_found;
1250 else if ( (CC->user.axlevel < 6) && (!CC->internal_pgm)
1251 && (CC->user.usernum != qrbuf.QRroomaide)
1252 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) ) {
1253 ret = crr_access_denied;
1256 else if (is_noneditable(&qrbuf)) {
1257 ret = crr_noneditable;
1262 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1263 if (qrbuf.QRflags & QR_MAILBOX) {
1264 owner = atol(qrbuf.QRname);
1266 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1267 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1268 "%010ld.%s", owner, new_name);
1271 safestrncpy(qrbuf.QRname, new_name,
1272 sizeof(qrbuf.QRname));
1275 /* Reject change of floor for baseroom/aideroom */
1276 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1277 !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1281 /* Take care of floor stuff */
1282 old_floor = qrbuf.QRfloor;
1283 if (new_floor < 0) {
1284 new_floor = old_floor;
1286 qrbuf.QRfloor = new_floor;
1289 begin_critical_section(S_CONFIG);
1291 /* If baseroom/aideroom name changes, update config */
1292 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1293 safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1296 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1297 safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1301 end_critical_section(S_CONFIG);
1303 /* If the room name changed, then there are now two room
1304 * records, so we have to delete the old one.
1306 if (strcasecmp(new_name, old_name)) {
1307 b_deleteroom(actual_old_name);
1313 end_critical_section(S_ROOMS);
1315 /* Adjust the floor reference counts if necessary */
1316 if (new_floor != old_floor) {
1317 lgetfloor(&flbuf, old_floor);
1318 --flbuf.f_ref_count;
1319 lputfloor(&flbuf, old_floor);
1320 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1321 lgetfloor(&flbuf, new_floor);
1322 ++flbuf.f_ref_count;
1323 lputfloor(&flbuf, new_floor);
1324 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1327 /* ...and everybody say "YATTA!" */
1333 * set room parameters (aide or room aide command)
1335 void cmd_setr(char *args)
1341 char new_name[ROOMNAMELEN];
1343 if (CtdlAccessCheck(ac_logged_in)) return;
1345 if (num_parms(args) >= 6) {
1346 new_floor = extract_int(args, 5);
1348 new_floor = (-1); /* don't change the floor */
1351 /* When is a new name more than just a new name? When the old name
1352 * has a namespace prefix.
1354 if (CC->room.QRflags & QR_MAILBOX) {
1355 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
1357 safestrncpy(new_name, "", sizeof new_name);
1359 extract_token(&new_name[strlen(new_name)], args, 0, '|', (sizeof new_name - strlen(new_name)));
1361 r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
1363 if (r == crr_room_not_found) {
1364 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
1365 } else if (r == crr_already_exists) {
1366 cprintf("%d '%s' already exists.\n",
1367 ERROR + ALREADY_EXISTS, new_name);
1368 } else if (r == crr_noneditable) {
1369 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
1370 } else if (r == crr_invalid_floor) {
1371 cprintf("%d Target floor does not exist.\n",
1372 ERROR + INVALID_FLOOR_OPERATION);
1373 } else if (r == crr_access_denied) {
1374 cprintf("%d You do not have permission to edit '%s'\n",
1375 ERROR + HIGHER_ACCESS_REQUIRED,
1377 } else if (r != crr_ok) {
1378 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1379 ERROR + INTERNAL_ERROR, r);
1386 getroom(&CC->room, new_name);
1388 /* Now we have to do a bunch of other stuff */
1390 if (num_parms(args) >= 7) {
1391 new_order = extract_int(args, 6);
1394 if (new_order > 127)
1398 lgetroom(&CC->room, CC->room.QRname);
1400 /* Directory room */
1401 extract_token(buf, args, 2, '|', sizeof buf);
1403 safestrncpy(CC->room.QRdirname, buf,
1404 sizeof CC->room.QRdirname);
1407 if (num_parms(args) >= 8) {
1408 CC->room.QRdefaultview = extract_int(args, 7);
1411 /* Second set of flags */
1412 if (num_parms(args) >= 9) {
1413 CC->room.QRflags2 = extract_int(args, 8);
1417 CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
1418 /* Clean up a client boo-boo: if the client set the room to
1419 * guess-name or passworded, ensure that the private flag is
1422 if ((CC->room.QRflags & QR_GUESSNAME)
1423 || (CC->room.QRflags & QR_PASSWORDED))
1424 CC->room.QRflags |= QR_PRIVATE;
1426 /* Some changes can't apply to BASEROOM */
1427 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1429 CC->room.QRorder = 0;
1430 CC->room.QRpasswd[0] = '\0';
1431 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1432 QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1433 CC->room.QRflags |= QR_PERMANENT;
1435 /* March order (doesn't apply to AIDEROOM) */
1436 if (num_parms(args) >= 7)
1437 CC->room.QRorder = (char) new_order;
1439 extract_token(buf, args, 1, '|', sizeof buf);
1441 safestrncpy(CC->room.QRpasswd, buf,
1442 sizeof CC->room.QRpasswd);
1443 /* Kick everyone out if the client requested it
1444 * (by changing the room's generation number)
1446 if (extract_int(args, 4)) {
1447 time(&CC->room.QRgen);
1450 /* Some changes can't apply to AIDEROOM */
1451 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1453 CC->room.QRorder = 0;
1454 CC->room.QRflags &= ~QR_MAILBOX;
1455 CC->room.QRflags |= QR_PERMANENT;
1458 /* Write the room record back to disk */
1459 lputroom(&CC->room);
1461 /* Create a room directory if necessary */
1462 if (CC->room.QRflags & QR_DIRECTORY) {
1463 snprintf(buf, sizeof buf,"%s/%s",
1465 CC->room.QRdirname);
1468 snprintf(buf, sizeof buf, "The room \"%s\" has been edited by %s.\n",
1469 CC->room.QRname, CC->curr_user);
1470 aide_message(buf, "Room modification Message");
1471 cprintf("%d Ok\n", CIT_OK);
1477 * get the name of the room aide for this room
1481 struct ctdluser usbuf;
1483 if (CtdlAccessCheck(ac_logged_in)) return;
1485 if (getuserbynumber(&usbuf, CC->room.QRroomaide) == 0) {
1486 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1488 cprintf("%d \n", CIT_OK);
1494 * set the room aide for this room
1496 void cmd_seta(char *new_ra)
1498 struct ctdluser usbuf;
1503 if (CtdlAccessCheck(ac_room_aide)) return;
1505 if (getuser(&usbuf, new_ra) != 0) {
1508 newu = usbuf.usernum;
1511 lgetroom(&CC->room, CC->room.QRname);
1513 if (CC->room.QRroomaide != newu) {
1516 CC->room.QRroomaide = newu;
1517 lputroom(&CC->room);
1520 * We have to post the change notice _after_ writing changes to
1521 * the room table, otherwise it would deadlock!
1523 if (post_notice == 1) {
1524 if (strlen(usbuf.fullname) > 0)
1525 snprintf(buf, sizeof buf,
1526 "%s is now the room aide for \"%s\".\n",
1527 usbuf.fullname, CC->room.QRname);
1529 snprintf(buf, sizeof buf,
1530 "There is now no room aide for \"%s\".\n",
1532 aide_message(buf, "Aide Room Modification");
1534 cprintf("%d Ok\n", CIT_OK);
1538 * retrieve info file for this room
1546 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_info_dir);
1547 info_fp = fopen(filename, "r");
1549 if (info_fp == NULL) {
1550 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
1553 cprintf("%d Info:\n", LISTING_FOLLOWS);
1554 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1555 if (strlen(buf) > 0)
1556 buf[strlen(buf) - 1] = 0;
1557 cprintf("%s\n", buf);
1564 * Asynchronously schedule a room for deletion. The room will appear
1565 * deleted to the user(s), but it won't actually get purged from the
1566 * database until THE DREADED AUTO-PURGER makes its next run.
1568 void schedule_room_for_deletion(struct ctdlroom *qrbuf)
1570 char old_name[ROOMNAMELEN];
1573 lprintf(CTDL_NOTICE, "Scheduling room <%s> for deletion\n",
1576 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1578 getroom(qrbuf, qrbuf->QRname);
1580 /* Turn the room into a private mailbox owned by a user who doesn't
1581 * exist. This will immediately make the room invisible to everyone,
1582 * and qualify the room for purging.
1584 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1589 qrbuf->QRflags |= QR_MAILBOX;
1590 time(&qrbuf->QRgen); /* Use a timestamp as the new generation number */
1594 b_deleteroom(old_name);
1600 * Back end processing to delete a room and everything associated with it
1601 * (This one is synchronous and should only get called by THE DREADED
1602 * AUTO-PURGER in serv_expire.c. All user-facing code should call
1603 * the asynchronous schedule_room_for_deletion() instead.)
1605 void delete_room(struct ctdlroom *qrbuf)
1609 /* TODO: filename magic? does this realy work? */
1611 lprintf(CTDL_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1613 /* Delete the info file */
1614 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_info_dir);
1617 /* Delete the image file */
1618 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_image_dir);
1621 /* Delete the room's network config file */
1622 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1625 /* Delete the messages in the room
1626 * (Careful: this opens an S_ROOMS critical section!)
1628 CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
1630 /* Flag the room record as not in use */
1631 lgetroom(qrbuf, qrbuf->QRname);
1635 /* then decrement the reference count for the floor */
1636 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1637 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1638 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1640 /* Delete the room record from the database! */
1641 b_deleteroom(qrbuf->QRname);
1647 * Check access control for deleting a room
1649 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1651 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1655 if (is_noneditable(qr)) {
1660 * For mailboxes, check stuff
1662 if (qr->QRflags & QR_MAILBOX) {
1664 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1666 if (atol(qr->QRname) != CC->user.usernum) {
1667 return(0); /* not my room */
1670 /* Can't delete your Mail> room */
1671 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1673 /* Otherwise it's ok */
1678 * For normal rooms, just check for aide or room aide status.
1680 return(is_room_aide());
1684 * aide command: kill the current room
1686 void cmd_kill(char *argbuf)
1688 char deleted_room_name[ROOMNAMELEN];
1692 kill_ok = extract_int(argbuf, 0);
1694 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
1695 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1699 if (CC->room.QRflags & QR_MAILBOX) {
1700 safestrncpy(deleted_room_name, &CC->room.QRname[11], sizeof deleted_room_name);
1703 safestrncpy(deleted_room_name, CC->room.QRname, sizeof deleted_room_name);
1706 /* Do the dirty work */
1707 schedule_room_for_deletion(&CC->room);
1709 /* Return to the Lobby */
1710 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1712 /* tell the world what we did */
1713 snprintf(msg, sizeof msg, "The room \"%s\" has been deleted by %s.\n",
1714 deleted_room_name, CC->curr_user);
1715 aide_message(msg, "Room Purger Message");
1716 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1718 cprintf("%d ok to delete.\n", CIT_OK);
1724 * Internal code to create a new room (returns room flags)
1726 * Room types: 0=public, 1=guessname, 2=passworded, 3=inv-only,
1727 * 4=mailbox, 5=mailbox, but caller supplies namespace
1729 unsigned create_room(char *new_room_name,
1731 char *new_room_pass,
1738 struct ctdlroom qrbuf;
1742 lprintf(CTDL_DEBUG, "create_room(name=%s, type=%d, view=%d)\n",
1743 new_room_name, new_room_type, new_room_view);
1745 if (getroom(&qrbuf, new_room_name) == 0) {
1746 lprintf(CTDL_DEBUG, "%s already exists.\n", new_room_name);
1750 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1751 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1752 qrbuf.QRflags = QR_INUSE;
1753 if (new_room_type > 0)
1754 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1755 if (new_room_type == 1)
1756 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1757 if (new_room_type == 2)
1758 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1759 if ( (new_room_type == 4) || (new_room_type == 5) ) {
1760 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1761 /* qrbuf.QRflags2 |= QR2_SUBJECTREQ; */
1764 /* If the user is requesting a personal room, set up the room
1765 * name accordingly (prepend the user number)
1767 if (new_room_type == 4) {
1768 MailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1771 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1774 /* If the room is private, and the system administrator has elected
1775 * to automatically grant room aide privileges, do so now; otherwise,
1776 * set the room aide to undefined.
1778 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1779 qrbuf.QRroomaide = CC->user.usernum;
1781 qrbuf.QRroomaide = (-1L);
1785 * If the caller is only interested in testing whether this will work,
1786 * return now without creating the room.
1788 if (!really_create) return (qrbuf.QRflags);
1790 qrbuf.QRnumber = get_new_room_number();
1791 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1792 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1793 qrbuf.QRfloor = new_room_floor;
1794 qrbuf.QRdefaultview = new_room_view;
1796 /* save what we just did... */
1799 /* bump the reference count on whatever floor the room is on */
1800 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1801 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1802 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1804 /* Grant the creator access to the room unless the avoid_access
1805 * parameter was specified.
1807 if ( (CC->logged_in) && (avoid_access == 0) ) {
1808 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1809 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1810 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1811 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1814 /* resume our happy day */
1815 return (qrbuf.QRflags);
1822 void cmd_cre8(char *args)
1825 char new_room_name[ROOMNAMELEN];
1827 char new_room_pass[32];
1830 char *notification_message = NULL;
1833 int avoid_access = 0;
1835 cre8_ok = extract_int(args, 0);
1836 extract_token(new_room_name, args, 1, '|', sizeof new_room_name);
1837 new_room_name[ROOMNAMELEN - 1] = 0;
1838 new_room_type = extract_int(args, 2);
1839 extract_token(new_room_pass, args, 3, '|', sizeof new_room_pass);
1840 avoid_access = extract_int(args, 5);
1841 new_room_view = extract_int(args, 6);
1842 new_room_pass[9] = 0;
1845 if ((strlen(new_room_name) == 0) && (cre8_ok == 1)) {
1846 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
1850 if (!strcasecmp(new_room_name, MAILROOM)) {
1851 cprintf("%d '%s' already exists.\n",
1852 ERROR + ALREADY_EXISTS, new_room_name);
1856 if (num_parms(args) >= 5) {
1857 fl = cgetfloor(extract_int(args, 4));
1859 cprintf("%d Invalid floor number.\n",
1860 ERROR + INVALID_FLOOR_OPERATION);
1863 else if ((fl->f_flags & F_INUSE) == 0) {
1864 cprintf("%d Invalid floor number.\n",
1865 ERROR + INVALID_FLOOR_OPERATION);
1868 new_room_floor = extract_int(args, 4);
1872 if (CtdlAccessCheck(ac_logged_in)) return;
1874 if (CC->user.axlevel < config.c_createax || CC->internal_pgm) {
1875 cprintf("%d You need higher access to create rooms.\n",
1876 ERROR + HIGHER_ACCESS_REQUIRED);
1880 if ((strlen(new_room_name) == 0) && (cre8_ok == 0)) {
1881 cprintf("%d Ok to create rooms.\n", CIT_OK);
1885 if ((new_room_type < 0) || (new_room_type > 5)) {
1886 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
1890 if (new_room_type == 5) {
1891 if (CC->user.axlevel < 6) {
1892 cprintf("%d Higher access required\n",
1893 ERROR + HIGHER_ACCESS_REQUIRED);
1898 /* Check to make sure the requested room name doesn't already exist */
1899 newflags = create_room(new_room_name,
1900 new_room_type, new_room_pass, new_room_floor,
1901 0, avoid_access, new_room_view);
1902 if (newflags == 0) {
1903 cprintf("%d '%s' already exists.\n",
1904 ERROR + ALREADY_EXISTS, new_room_name);
1909 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
1913 /* If we reach this point, the room needs to be created. */
1915 newflags = create_room(new_room_name,
1916 new_room_type, new_room_pass, new_room_floor, 1, 0,
1919 /* post a message in Aide> describing the new room */
1920 notification_message = malloc(1024);
1921 snprintf(notification_message, 1024,
1922 "A new room called \"%s\" has been created by %s%s%s%s%s%s\n",
1925 ((newflags & QR_MAILBOX) ? " [personal]" : ""),
1926 ((newflags & QR_PRIVATE) ? " [private]" : ""),
1927 ((newflags & QR_GUESSNAME) ? " [hidden]" : ""),
1928 ((newflags & QR_PASSWORDED) ? " Password: " : ""),
1929 ((newflags & QR_PASSWORDED) ? new_room_pass : "")
1931 aide_message(notification_message, "Room Creation Message");
1932 free(notification_message);
1934 cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
1939 void cmd_einf(char *ok)
1940 { /* enter info file for current room */
1942 char infofilename[SIZ];
1947 if (CtdlAccessCheck(ac_room_aide)) return;
1949 if (atoi(ok) == 0) {
1950 cprintf("%d Ok.\n", CIT_OK);
1953 assoc_file_name(infofilename, sizeof infofilename, &CC->room, ctdl_info_dir);
1954 lprintf(CTDL_DEBUG, "opening\n");
1955 fp = fopen(infofilename, "w");
1956 lprintf(CTDL_DEBUG, "checking\n");
1958 cprintf("%d Cannot open %s: %s\n",
1959 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1962 cprintf("%d Send info...\n", SEND_LISTING);
1965 client_getln(buf, sizeof buf);
1966 if (strcmp(buf, "000"))
1967 fprintf(fp, "%s\n", buf);
1968 } while (strcmp(buf, "000"));
1971 /* now update the room index so people will see our new info */
1972 lgetroom(&CC->room, CC->room.QRname); /* lock so no one steps on us */
1973 CC->room.QRinfo = CC->room.QRhighest + 1L;
1974 lputroom(&CC->room);
1979 * cmd_lflr() - List all known floors
1986 if (CtdlAccessCheck(ac_logged_in)) return;
1988 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1990 for (a = 0; a < MAXFLOORS; ++a) {
1991 getfloor(&flbuf, a);
1992 if (flbuf.f_flags & F_INUSE) {
1993 cprintf("%d|%s|%d\n",
2005 * create a new floor
2007 void cmd_cflr(char *argbuf)
2009 char new_floor_name[256];
2012 int free_slot = (-1);
2015 extract_token(new_floor_name, argbuf, 0, '|', sizeof new_floor_name);
2016 cflr_ok = extract_int(argbuf, 1);
2018 if (CtdlAccessCheck(ac_aide)) return;
2020 if (strlen(new_floor_name) == 0) {
2021 cprintf("%d Blank floor name not allowed.\n",
2022 ERROR + ILLEGAL_VALUE);
2026 for (a = 0; a < MAXFLOORS; ++a) {
2027 getfloor(&flbuf, a);
2029 /* note any free slots while we're scanning... */
2030 if (((flbuf.f_flags & F_INUSE) == 0)
2034 /* check to see if it already exists */
2035 if ((!strcasecmp(flbuf.f_name, new_floor_name))
2036 && (flbuf.f_flags & F_INUSE)) {
2037 cprintf("%d Floor '%s' already exists.\n",
2038 ERROR + ALREADY_EXISTS,
2044 if (free_slot < 0) {
2045 cprintf("%d There is no space available for a new floor.\n",
2046 ERROR + INVALID_FLOOR_OPERATION);
2050 cprintf("%d ok to create...\n", CIT_OK);
2053 lgetfloor(&flbuf, free_slot);
2054 flbuf.f_flags = F_INUSE;
2055 flbuf.f_ref_count = 0;
2056 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
2057 lputfloor(&flbuf, free_slot);
2058 cprintf("%d %d\n", CIT_OK, free_slot);
2066 void cmd_kflr(char *argbuf)
2069 int floor_to_delete;
2073 floor_to_delete = extract_int(argbuf, 0);
2074 kflr_ok = extract_int(argbuf, 1);
2076 if (CtdlAccessCheck(ac_aide)) return;
2078 lgetfloor(&flbuf, floor_to_delete);
2081 if ((flbuf.f_flags & F_INUSE) == 0) {
2082 cprintf("%d Floor %d not in use.\n",
2083 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
2086 if (flbuf.f_ref_count != 0) {
2087 cprintf("%d Cannot delete; floor contains %d rooms.\n",
2088 ERROR + INVALID_FLOOR_OPERATION,
2093 cprintf("%d Ok\n", CIT_OK);
2095 cprintf("%d Ok to delete...\n", CIT_OK);
2102 if ((delete_ok == 1) && (kflr_ok == 1))
2104 lputfloor(&flbuf, floor_to_delete);
2110 void cmd_eflr(char *argbuf)
2116 np = num_parms(argbuf);
2118 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
2122 if (CtdlAccessCheck(ac_aide)) return;
2124 floor_num = extract_int(argbuf, 0);
2125 lgetfloor(&flbuf, floor_num);
2126 if ((flbuf.f_flags & F_INUSE) == 0) {
2127 lputfloor(&flbuf, floor_num);
2128 cprintf("%d Floor %d is not in use.\n",
2129 ERROR + INVALID_FLOOR_OPERATION, floor_num);
2133 extract_token(flbuf.f_name, argbuf, 1, '|', sizeof flbuf.f_name);
2134 lputfloor(&flbuf, floor_num);
2136 cprintf("%d Ok\n", CIT_OK);