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>
34 #include "sysdep_decls.h"
38 #include "citserver.h"
41 #include "citadel_dirs.h"
43 struct floor *floorcache[MAXFLOORS];
46 * Retrieve access control information for any user/room pair
48 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf,
49 int *result, int *view)
54 /* for internal programs, always do everything */
55 if (((CC->internal_pgm)) && (roombuf->QRflags & QR_INUSE)) {
56 retval = (UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED);
61 /* Locate any applicable user/room relationships */
62 CtdlGetRelationship(&vbuf, userbuf, roombuf);
64 /* Force the properties of the Aide room */
65 if (!strcasecmp(roombuf->QRname, config.c_aideroom)) {
66 if (userbuf->axlevel >= 6) {
67 retval = UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
74 /* If this is a public room, it's accessible... */
75 if ( ((roombuf->QRflags & QR_PRIVATE) == 0)
76 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
77 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
80 /* If this is a preferred users only room, check access level */
81 if (roombuf->QRflags & QR_PREFONLY) {
82 if (userbuf->axlevel < 5) {
83 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
87 /* For private rooms, check the generation number matchups */
88 if ( (roombuf->QRflags & QR_PRIVATE)
89 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
91 /* An explicit match means the user belongs in this room */
92 if (vbuf.v_flags & V_ACCESS) {
93 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
95 /* Otherwise, check if this is a guess-name or passworded
96 * room. If it is, a goto may at least be attempted
98 else if ((roombuf->QRflags & QR_PRIVATE)
99 || (roombuf->QRflags & QR_PASSWORDED)) {
100 retval = retval & ~UA_KNOWN;
101 retval = retval | UA_GOTOALLOWED;
105 /* For mailbox rooms, also check the namespace */
106 /* Also, mailbox owners can delete their messages */
107 if (roombuf->QRflags & QR_MAILBOX) {
108 if (userbuf->usernum == atol(roombuf->QRname)) {
109 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
111 /* An explicit match means the user belongs in this room */
112 if (vbuf.v_flags & V_ACCESS) {
113 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
117 /* For non-mailbox rooms... */
120 /* User is allowed to post in the room unless:
121 * - User is not validated
122 * - User has no net privileges and it is a shared network room
123 * - It is a read-only room
125 int post_allowed = 1;
126 if (CC->user.axlevel < 2) post_allowed = 0;
127 if ((CC->user.axlevel < 4) && (CC->room.QRflags & QR_NETWORK)) post_allowed = 0;
128 if (roombuf->QRflags & QR_READONLY) post_allowed = 0;
130 retval = retval | UA_POSTALLOWED;
133 /* If "collaborative deletion" is active for this room, any user who can post
134 * is also allowed to delete
136 if (CC->room.QRflags2 & QR2_COLLABDEL) {
137 if (retval & UA_POSTALLOWED) {
138 retval = retval | UA_DELETEALLOWED;
144 /* Check to see if the user has forgotten this room */
145 if (vbuf.v_flags & V_FORGET) {
146 retval = retval & ~UA_KNOWN;
147 if ( ( ((roombuf->QRflags & QR_PRIVATE) == 0)
148 && ((roombuf->QRflags & QR_MAILBOX) == 0) )
149 || ( (roombuf->QRflags & QR_MAILBOX)
150 && (atol(roombuf->QRname) == CC->user.usernum))) {
151 retval = retval | UA_ZAPPED;
154 /* If user is explicitly locked out of this room, deny everything */
155 if (vbuf.v_flags & V_LOCKOUT) {
156 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED & ~UA_POSTALLOWED;
159 /* Aides get access to all private rooms */
160 if ( (userbuf->axlevel >= 6)
161 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
162 if (vbuf.v_flags & V_FORGET) {
163 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED;
166 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED;
170 /* Aides can gain access to mailboxes as well, but they don't show
173 if ( (userbuf->axlevel >= 6)
174 && (roombuf->QRflags & QR_MAILBOX) ) {
175 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED;
178 /* Aides and Room Aides have admin privileges */
179 if ( (userbuf->axlevel >= 6)
180 || (userbuf->usernum == roombuf->QRroomaide)
182 retval = retval | UA_ADMINALLOWED | UA_DELETEALLOWED;
185 NEWMSG: /* By the way, we also check for the presence of new messages */
186 if (is_msg_in_sequence_set(vbuf.v_seen, roombuf->QRhighest) == 0) {
187 retval = retval | UA_HASNEWMSGS;
190 /* System rooms never show up in the list. */
191 if (roombuf->QRflags2 & QR2_SYSTEM) {
192 retval = retval & ~UA_KNOWN;
196 /* Now give the caller the information it wants. */
197 if (result != NULL) *result = retval;
198 if (view != NULL) *view = vbuf.v_view;
202 * Self-checking stuff for a room record read into memory
204 void room_sanity_check(struct ctdlroom *qrbuf)
206 /* Mailbox rooms are always on the lowest floor */
207 if (qrbuf->QRflags & QR_MAILBOX) {
210 /* Listing order of 0 is illegal except for base rooms */
211 if (qrbuf->QRorder == 0)
212 if (!(qrbuf->QRflags & QR_MAILBOX) &&
213 strncasecmp(qrbuf->QRname, config.c_baseroom, ROOMNAMELEN)
215 strncasecmp(qrbuf->QRname, config.c_aideroom, ROOMNAMELEN))
221 * getroom() - retrieve room data from disk
223 int getroom(struct ctdlroom *qrbuf, char *room_name)
225 struct cdbdata *cdbqr;
226 char lowercase_name[ROOMNAMELEN];
227 char personal_lowercase_name[ROOMNAMELEN];
228 char *dptr, *sptr, *eptr;
230 dptr = lowercase_name;
232 eptr = (dptr + (sizeof lowercase_name - 1));
233 while (!IsEmptyStr(sptr) && (dptr < eptr)){
234 *dptr = tolower(*sptr);
239 memset(qrbuf, 0, sizeof(struct ctdlroom));
241 /* First, try the public namespace */
242 cdbqr = cdb_fetch(CDB_ROOMS,
243 lowercase_name, strlen(lowercase_name));
245 /* If that didn't work, try the user's personal namespace */
247 snprintf(personal_lowercase_name,
248 sizeof personal_lowercase_name, "%010ld.%s",
249 CC->user.usernum, lowercase_name);
250 cdbqr = cdb_fetch(CDB_ROOMS,
251 personal_lowercase_name,
252 strlen(personal_lowercase_name));
255 memcpy(qrbuf, cdbqr->ptr,
256 ((cdbqr->len > sizeof(struct ctdlroom)) ?
257 sizeof(struct ctdlroom) : cdbqr->len));
260 room_sanity_check(qrbuf);
269 * lgetroom() - same as getroom() but locks the record (if supported)
271 int lgetroom(struct ctdlroom *qrbuf, char *room_name)
274 retval = getroom(qrbuf, room_name);
275 if (retval == 0) begin_critical_section(S_ROOMS);
281 * b_putroom() - back end to putroom() and b_deleteroom()
282 * (if the supplied buffer is NULL, delete the room record)
284 void b_putroom(struct ctdlroom *qrbuf, char *room_name)
286 char lowercase_name[ROOMNAMELEN];
291 bptr = lowercase_name;
292 while (!IsEmptyStr(aptr))
294 *bptr = tolower(*aptr);
300 len = bptr - lowercase_name;
302 cdb_delete(CDB_ROOMS,
303 lowercase_name, len);
305 time(&qrbuf->QRmtime);
308 qrbuf, sizeof(struct ctdlroom));
314 * putroom() - store room data to disk
316 void putroom(struct ctdlroom *qrbuf) {
317 b_putroom(qrbuf, qrbuf->QRname);
322 * b_deleteroom() - delete a room record from disk
324 void b_deleteroom(char *room_name) {
325 b_putroom(NULL, room_name);
331 * lputroom() - same as putroom() but unlocks the record (if supported)
333 void lputroom(struct ctdlroom *qrbuf)
337 end_critical_section(S_ROOMS);
341 /****************************************************************************/
344 * getfloor() - retrieve floor data from disk
346 void getfloor(struct floor *flbuf, int floor_num)
348 struct cdbdata *cdbfl;
350 memset(flbuf, 0, sizeof(struct floor));
351 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
353 memcpy(flbuf, cdbfl->ptr,
354 ((cdbfl->len > sizeof(struct floor)) ?
355 sizeof(struct floor) : cdbfl->len));
358 if (floor_num == 0) {
359 safestrncpy(flbuf->f_name, "Main Floor",
360 sizeof flbuf->f_name);
361 flbuf->f_flags = F_INUSE;
362 flbuf->f_ref_count = 3;
369 * lgetfloor() - same as getfloor() but locks the record (if supported)
371 void lgetfloor(struct floor *flbuf, int floor_num)
374 begin_critical_section(S_FLOORTAB);
375 getfloor(flbuf, floor_num);
380 * cgetfloor() - Get floor record from *cache* (loads from disk if needed)
382 * This is strictly a performance hack.
384 struct floor *cgetfloor(int floor_num) {
385 static int initialized = 0;
388 struct floor *fl = NULL;
390 begin_critical_section(S_FLOORCACHE);
391 if (initialized == 0) {
392 for (i=0; i<MAXFLOORS; ++i) {
393 floorcache[floor_num] = NULL;
397 if (floorcache[floor_num] == NULL) {
400 end_critical_section(S_FLOORCACHE);
403 fl = malloc(sizeof(struct floor));
404 getfloor(fl, floor_num);
405 begin_critical_section(S_FLOORCACHE);
406 if (floorcache[floor_num] != NULL) {
407 free(floorcache[floor_num]);
409 floorcache[floor_num] = fl;
410 end_critical_section(S_FLOORCACHE);
413 return(floorcache[floor_num]);
419 * putfloor() - store floor data on disk
421 void putfloor(struct floor *flbuf, int floor_num)
423 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
424 begin_critical_section(S_FLOORCACHE);
425 if (floorcache[floor_num] != NULL) {
426 free(floorcache[floor_num]);
427 floorcache[floor_num] = malloc(sizeof(struct floor));
428 memcpy(floorcache[floor_num], flbuf, sizeof(struct floor));
430 end_critical_section(S_FLOORCACHE);
432 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
433 flbuf, sizeof(struct floor));
438 * lputfloor() - same as putfloor() but unlocks the record (if supported)
440 void lputfloor(struct floor *flbuf, int floor_num)
443 putfloor(flbuf, floor_num);
444 end_critical_section(S_FLOORTAB);
450 * Traverse the room file...
452 void ForEachRoom(void (*CallBack) (struct ctdlroom *EachRoom, void *out_data),
455 struct ctdlroom qrbuf;
456 struct cdbdata *cdbqr;
458 cdb_rewind(CDB_ROOMS);
460 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
461 memset(&qrbuf, 0, sizeof(struct ctdlroom));
462 memcpy(&qrbuf, cdbqr->ptr,
463 ((cdbqr->len > sizeof(struct ctdlroom)) ?
464 sizeof(struct ctdlroom) : cdbqr->len));
466 room_sanity_check(&qrbuf);
467 if (qrbuf.QRflags & QR_INUSE)
468 (*CallBack)(&qrbuf, in_data);
474 * delete_msglist() - delete room message pointers
476 void delete_msglist(struct ctdlroom *whichroom)
478 struct cdbdata *cdbml;
480 /* Make sure the msglist we're deleting actually exists, otherwise
481 * gdbm will complain when we try to delete an invalid record
483 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
487 /* Go ahead and delete it */
488 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
496 * sort message pointers
497 * (returns new msg count)
499 int sort_msglist(long listptrs[], int oldcount)
510 for (a = numitems - 2; a >= 0; --a) {
511 for (b = 0; b <= a; ++b) {
512 if (listptrs[b] > (listptrs[b + 1])) {
514 hold2 = listptrs[b + 1];
516 listptrs[b + 1] = hold1;
521 /* and yank any nulls */
522 while ((numitems > 0) && (listptrs[0] == 0L)) {
523 memmove(&listptrs[0], &listptrs[1],
524 (sizeof(long) * (numitems - 1)));
533 * Determine whether a given room is non-editable.
535 int is_noneditable(struct ctdlroom *qrbuf)
538 /* Mail> rooms are non-editable */
539 if ( (qrbuf->QRflags & QR_MAILBOX)
540 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
543 /* Everything else is editable */
550 * Back-back-end for all room listing commands
552 void list_roomname(struct ctdlroom *qrbuf, int ra, int current_view, int default_view)
554 char truncated_roomname[ROOMNAMELEN];
556 /* For my own mailbox rooms, chop off the owner prefix */
557 if ( (qrbuf->QRflags & QR_MAILBOX)
558 && (atol(qrbuf->QRname) == CC->user.usernum) ) {
559 safestrncpy(truncated_roomname, qrbuf->QRname, sizeof truncated_roomname);
560 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
561 cprintf("%s", truncated_roomname);
563 /* For all other rooms, just display the name in its entirety */
565 cprintf("%s", qrbuf->QRname);
568 /* ...and now the other parameters */
569 cprintf("|%u|%d|%d|%d|%d|%d|%d|%ld|\n",
571 (int) qrbuf->QRfloor,
572 (int) qrbuf->QRorder,
573 (int) qrbuf->QRflags2,
583 * cmd_lrms() - List all accessible rooms, known or forgotten
585 void cmd_lrms_backend(struct ctdlroom *qrbuf, void *data)
587 int FloorBeingSearched = (-1);
591 FloorBeingSearched = *(int *)data;
592 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
594 if ((( ra & (UA_KNOWN | UA_ZAPPED)))
595 && ((qrbuf->QRfloor == (FloorBeingSearched))
596 || ((FloorBeingSearched) < 0)))
597 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
600 void cmd_lrms(char *argbuf)
602 int FloorBeingSearched = (-1);
603 if (!IsEmptyStr(argbuf))
604 FloorBeingSearched = extract_int(argbuf, 0);
606 if (CtdlAccessCheck(ac_logged_in)) return;
608 if (getuser(&CC->user, CC->curr_user)) {
609 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
612 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
614 ForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
621 * cmd_lkra() - List all known rooms
623 void cmd_lkra_backend(struct ctdlroom *qrbuf, void *data)
625 int FloorBeingSearched = (-1);
629 FloorBeingSearched = *(int *)data;
630 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
632 if ((( ra & (UA_KNOWN)))
633 && ((qrbuf->QRfloor == (FloorBeingSearched))
634 || ((FloorBeingSearched) < 0)))
635 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
638 void cmd_lkra(char *argbuf)
640 int FloorBeingSearched = (-1);
641 if (!IsEmptyStr(argbuf))
642 FloorBeingSearched = extract_int(argbuf, 0);
644 if (CtdlAccessCheck(ac_logged_in)) return;
646 if (getuser(&CC->user, CC->curr_user)) {
647 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
650 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
652 ForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
658 void cmd_lprm_backend(struct ctdlroom *qrbuf, void *data)
660 int FloorBeingSearched = (-1);
664 FloorBeingSearched = *(int *)data;
665 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
667 if ( ((qrbuf->QRflags & QR_PRIVATE) == 0)
668 && ((qrbuf->QRflags & QR_MAILBOX) == 0)
669 && ((qrbuf->QRfloor == (FloorBeingSearched))
670 || ((FloorBeingSearched) < 0)))
671 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
674 void cmd_lprm(char *argbuf)
676 int FloorBeingSearched = (-1);
677 if (!IsEmptyStr(argbuf))
678 FloorBeingSearched = extract_int(argbuf, 0);
680 cprintf("%d Publiic rooms:\n", LISTING_FOLLOWS);
682 ForEachRoom(cmd_lprm_backend, &FloorBeingSearched);
689 * cmd_lkrn() - List all known rooms with new messages
691 void cmd_lkrn_backend(struct ctdlroom *qrbuf, void *data)
693 int FloorBeingSearched = (-1);
697 FloorBeingSearched = *(int *)data;
698 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
701 && (ra & UA_HASNEWMSGS)
702 && ((qrbuf->QRfloor == (FloorBeingSearched))
703 || ((FloorBeingSearched) < 0)))
704 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
707 void cmd_lkrn(char *argbuf)
709 int FloorBeingSearched = (-1);
710 if (!IsEmptyStr(argbuf))
711 FloorBeingSearched = extract_int(argbuf, 0);
713 if (CtdlAccessCheck(ac_logged_in)) return;
715 if (getuser(&CC->user, CC->curr_user)) {
716 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
719 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
721 ForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
728 * cmd_lkro() - List all known rooms
730 void cmd_lkro_backend(struct ctdlroom *qrbuf, void *data)
732 int FloorBeingSearched = (-1);
736 FloorBeingSearched = *(int *)data;
737 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
740 && ((ra & UA_HASNEWMSGS) == 0)
741 && ((qrbuf->QRfloor == (FloorBeingSearched))
742 || ((FloorBeingSearched) < 0)))
743 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
746 void cmd_lkro(char *argbuf)
748 int FloorBeingSearched = (-1);
749 if (!IsEmptyStr(argbuf))
750 FloorBeingSearched = extract_int(argbuf, 0);
752 if (CtdlAccessCheck(ac_logged_in)) return;
754 if (getuser(&CC->user, CC->curr_user)) {
755 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
758 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
760 ForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
767 * cmd_lzrm() - List all forgotten rooms
769 void cmd_lzrm_backend(struct ctdlroom *qrbuf, void *data)
771 int FloorBeingSearched = (-1);
775 FloorBeingSearched = *(int *)data;
776 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
778 if ((ra & UA_GOTOALLOWED)
780 && ((qrbuf->QRfloor == (FloorBeingSearched))
781 || ((FloorBeingSearched) < 0)))
782 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
785 void cmd_lzrm(char *argbuf)
787 int FloorBeingSearched = (-1);
788 if (!IsEmptyStr(argbuf))
789 FloorBeingSearched = extract_int(argbuf, 0);
791 if (CtdlAccessCheck(ac_logged_in)) return;
793 if (getuser(&CC->user, CC->curr_user)) {
794 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
797 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
799 ForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
805 * Make the specified room the current room for this session. No validation
806 * or access control is done here -- the caller should make sure that the
807 * specified room exists and is ok to access.
809 void usergoto(char *where, int display_result, int transiently,
810 int *retmsgs, int *retnew)
813 int new_messages = 0;
814 int old_messages = 0;
815 int total_messages = 0;
819 int newmailcount = 0;
821 char truncated_roomname[ROOMNAMELEN];
822 struct cdbdata *cdbfr;
823 long *msglist = NULL;
825 unsigned int original_v_flags;
828 char setstr[128], lostr[64], histr[64];
832 /* If the supplied room name is NULL, the caller wants us to know that
833 * it has already copied the room record into CC->room, so
834 * we can skip the extra database fetch.
837 safestrncpy(CC->room.QRname, where, sizeof CC->room.QRname);
838 getroom(&CC->room, where);
841 /* Take care of all the formalities. */
843 begin_critical_section(S_USERS);
844 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
845 original_v_flags = vbuf.v_flags;
847 /* Know the room ... but not if it's the page log room, or if the
848 * caller specified that we're only entering this room transiently.
850 if ((strcasecmp(CC->room.QRname, config.c_logpages))
851 && (transiently == 0) ) {
852 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
853 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
856 /* Only rewrite the database record if we changed something */
857 if (vbuf.v_flags != original_v_flags) {
858 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
860 end_critical_section(S_USERS);
862 /* Check for new mail */
863 newmailcount = NewMailCount();
865 /* set info to 1 if the user needs to read the room's info file */
866 if (CC->room.QRinfo > vbuf.v_lastseen) {
870 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
872 msglist = (long *) cdbfr->ptr;
873 cdbfr->ptr = NULL; /* usergoto() now owns this memory */
874 num_msgs = cdbfr->len / sizeof(long);
879 for (a=0; a<num_msgs; ++a) {
880 if (msglist[a] > 0L) ++total_messages;
882 new_messages = num_msgs;
883 num_sets = num_tokens(vbuf.v_seen, ',');
884 for (s=0; s<num_sets; ++s) {
885 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
887 extract_token(lostr, setstr, 0, ':', sizeof lostr);
888 if (num_tokens(setstr, ':') >= 2) {
889 extract_token(histr, setstr, 1, ':', sizeof histr);
890 if (!strcmp(histr, "*")) {
891 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
895 strcpy(histr, lostr);
900 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
901 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
907 new_messages = total_messages - old_messages;
909 if (msglist != NULL) free(msglist);
911 if (CC->room.QRflags & QR_MAILBOX)
916 if ((CC->room.QRroomaide == CC->user.usernum)
917 || (CC->user.axlevel >= 6))
922 safestrncpy(truncated_roomname, CC->room.QRname, sizeof truncated_roomname);
923 if ( (CC->room.QRflags & QR_MAILBOX)
924 && (atol(CC->room.QRname) == CC->user.usernum) ) {
925 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
928 if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
932 if (retmsgs != NULL) *retmsgs = total_messages;
933 if (retnew != NULL) *retnew = new_messages;
934 lprintf(CTDL_DEBUG, "<%s> %d new of %d total messages\n",
936 new_messages, total_messages
939 CC->curr_view = (int)vbuf.v_view;
941 if (display_result) {
942 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|%d|\n",
943 CIT_OK, CtdlCheckExpress(),
948 (int)CC->room.QRflags,
949 (long)CC->room.QRhighest,
950 (long)vbuf.v_lastseen,
954 (int)CC->room.QRfloor,
956 (int)CC->room.QRdefaultview,
958 (int)CC->room.QRflags2
965 * Handle some of the macro named rooms
967 void convert_room_name_macros(char *towhere, size_t maxlen) {
968 if (!strcasecmp(towhere, "_BASEROOM_")) {
969 safestrncpy(towhere, config.c_baseroom, maxlen);
971 else if (!strcasecmp(towhere, "_MAIL_")) {
972 safestrncpy(towhere, MAILROOM, maxlen);
974 else if (!strcasecmp(towhere, "_TRASH_")) {
975 safestrncpy(towhere, USERTRASHROOM, maxlen);
977 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
978 safestrncpy(towhere, config.c_twitroom, maxlen);
980 else if (!strcasecmp(towhere, "_CALENDAR_")) {
981 safestrncpy(towhere, USERCALENDARROOM, maxlen);
983 else if (!strcasecmp(towhere, "_TASKS_")) {
984 safestrncpy(towhere, USERTASKSROOM, maxlen);
986 else if (!strcasecmp(towhere, "_CONTACTS_")) {
987 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
989 else if (!strcasecmp(towhere, "_NOTES_")) {
990 safestrncpy(towhere, USERNOTESROOM, maxlen);
996 * cmd_goto() - goto a new room
998 void cmd_goto(char *gargs)
1000 struct ctdlroom QRscratch;
1004 char augmented_roomname[ROOMNAMELEN];
1005 char towhere[ROOMNAMELEN];
1007 int transiently = 0;
1009 if (CtdlAccessCheck(ac_logged_in)) return;
1011 extract_token(towhere, gargs, 0, '|', sizeof towhere);
1012 extract_token(password, gargs, 1, '|', sizeof password);
1013 transiently = extract_int(gargs, 2);
1015 getuser(&CC->user, CC->curr_user);
1018 * Handle some of the macro named rooms
1020 convert_room_name_macros(towhere, sizeof towhere);
1022 /* First try a regular match */
1023 c = getroom(&QRscratch, towhere);
1025 /* Then try a mailbox name match */
1027 MailboxName(augmented_roomname, sizeof augmented_roomname,
1028 &CC->user, towhere);
1029 c = getroom(&QRscratch, augmented_roomname);
1031 safestrncpy(towhere, augmented_roomname, sizeof towhere);
1034 /* And if the room was found... */
1037 /* Let internal programs go directly to any room. */
1038 if (CC->internal_pgm) {
1039 memcpy(&CC->room, &QRscratch,
1040 sizeof(struct ctdlroom));
1041 usergoto(NULL, 1, transiently, NULL, NULL);
1045 /* See if there is an existing user/room relationship */
1046 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
1048 /* normal clients have to pass through security */
1049 if (ra & UA_GOTOALLOWED) {
1054 if ((QRscratch.QRflags & QR_MAILBOX) &&
1055 ((ra & UA_GOTOALLOWED))) {
1056 memcpy(&CC->room, &QRscratch,
1057 sizeof(struct ctdlroom));
1058 usergoto(NULL, 1, transiently, NULL, NULL);
1060 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
1061 ((ra & UA_KNOWN) == 0) &&
1062 (strcasecmp(QRscratch.QRpasswd, password)) &&
1063 (CC->user.axlevel < 6)
1065 cprintf("%d wrong or missing passwd\n",
1066 ERROR + PASSWORD_REQUIRED);
1068 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
1069 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
1070 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
1071 ((ra & UA_KNOWN) == 0) &&
1072 (CC->user.axlevel < 6)
1074 lprintf(CTDL_DEBUG, "Failed to acquire private room\n");
1076 memcpy(&CC->room, &QRscratch,
1077 sizeof(struct ctdlroom));
1078 usergoto(NULL, 1, transiently, NULL, NULL);
1084 cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
1090 struct ctdluser temp;
1091 struct cdbdata *cdbus;
1094 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
1095 cdb_rewind(CDB_USERS);
1096 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1097 memset(&temp, 0, sizeof temp);
1098 memcpy(&temp, cdbus->ptr, sizeof temp);
1101 CtdlRoomAccess(&CC->room, &temp, &ra, NULL);
1102 if ((CC->room.QRflags & QR_INUSE)
1105 cprintf("%s\n", temp.fullname);
1112 * RDIR command for room directory
1120 struct stat statbuf;
1121 char tempfilename[PATH_MAX];
1123 if (CtdlAccessCheck(ac_logged_in)) return;
1124 CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
1126 getroom(&CC->room, CC->room.QRname);
1127 getuser(&CC->user, CC->curr_user);
1129 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
1130 cprintf("%d not here.\n", ERROR + NOT_HERE);
1133 if (((CC->room.QRflags & QR_VISDIR) == 0)
1134 && (CC->user.axlevel < 6)
1135 && (CC->user.usernum != CC->room.QRroomaide)) {
1136 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1139 cprintf("%d %s|%s/%s\n",
1143 CC->room.QRdirname);
1145 snprintf(buf, sizeof buf,
1146 "ls %s/%s >%s 2>/dev/null",
1152 snprintf(buf, sizeof buf,
1155 CC->room.QRdirname);
1156 fd = fopen(buf, "r");
1158 fd = fopen("/dev/null", "r");
1160 ls = fopen(tempfilename, "r");
1161 while (fgets(flnm, sizeof flnm, ls) != NULL) {
1162 flnm[strlen(flnm) - 1] = 0;
1163 if (strcasecmp(flnm, "filedir")) {
1164 snprintf(buf, sizeof buf,
1169 stat(buf, &statbuf);
1170 safestrncpy(comment, "", sizeof comment);
1172 while ((fgets(buf, sizeof buf, fd) != NULL)
1173 && (IsEmptyStr(comment))) {
1174 buf[strlen(buf) - 1] = 0;
1175 if ((!strncasecmp(buf, flnm, strlen(flnm)))
1176 && (buf[strlen(flnm)] == ' '))
1177 safestrncpy(comment,
1178 &buf[strlen(flnm) + 1],
1181 cprintf("%s|%ld|%s\n", flnm, (long)statbuf.st_size, comment);
1186 unlink(tempfilename);
1192 * get room parameters (aide or room aide command)
1196 if (CtdlAccessCheck(ac_room_aide)) return;
1198 getroom(&CC->room, CC->room.QRname);
1199 cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1203 ((CC->room.QRflags & QR_MAILBOX) ?
1204 &CC->room.QRname[11] : CC->room.QRname),
1206 ((CC->room.QRflags & QR_PASSWORDED) ?
1207 CC->room.QRpasswd : ""),
1209 ((CC->room.QRflags & QR_DIRECTORY) ?
1210 CC->room.QRdirname : ""),
1213 (int) CC->room.QRfloor,
1214 (int) CC->room.QRorder,
1216 CC->room.QRdefaultview,
1223 * Back end function to rename a room.
1224 * You can also specify which floor to move the room to, or specify -1 to
1225 * keep the room on the same floor it was on.
1227 * If you are renaming a mailbox room, you must supply the namespace prefix
1228 * in *at least* the old name!
1230 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1232 struct ctdlroom qrbuf;
1233 struct ctdlroom qrtmp;
1238 char actual_old_name[ROOMNAMELEN];
1240 lprintf(CTDL_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1241 old_name, new_name, new_floor);
1243 if (new_floor >= 0) {
1244 fl = cgetfloor(new_floor);
1245 if ((fl->f_flags & F_INUSE) == 0) {
1246 return(crr_invalid_floor);
1250 begin_critical_section(S_ROOMS);
1252 if ( (getroom(&qrtmp, new_name) == 0)
1253 && (strcasecmp(new_name, old_name)) ) {
1254 ret = crr_already_exists;
1257 else if (getroom(&qrbuf, old_name) != 0) {
1258 ret = crr_room_not_found;
1261 else if ( (CC->user.axlevel < 6) && (!CC->internal_pgm)
1262 && (CC->user.usernum != qrbuf.QRroomaide)
1263 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) ) {
1264 ret = crr_access_denied;
1267 else if (is_noneditable(&qrbuf)) {
1268 ret = crr_noneditable;
1273 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1274 if (qrbuf.QRflags & QR_MAILBOX) {
1275 owner = atol(qrbuf.QRname);
1277 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1278 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1279 "%010ld.%s", owner, new_name);
1282 safestrncpy(qrbuf.QRname, new_name,
1283 sizeof(qrbuf.QRname));
1286 /* Reject change of floor for baseroom/aideroom */
1287 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1288 !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1292 /* Take care of floor stuff */
1293 old_floor = qrbuf.QRfloor;
1294 if (new_floor < 0) {
1295 new_floor = old_floor;
1297 qrbuf.QRfloor = new_floor;
1300 begin_critical_section(S_CONFIG);
1302 /* If baseroom/aideroom name changes, update config */
1303 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1304 safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1307 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1308 safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1312 end_critical_section(S_CONFIG);
1314 /* If the room name changed, then there are now two room
1315 * records, so we have to delete the old one.
1317 if (strcasecmp(new_name, old_name)) {
1318 b_deleteroom(actual_old_name);
1324 end_critical_section(S_ROOMS);
1326 /* Adjust the floor reference counts if necessary */
1327 if (new_floor != old_floor) {
1328 lgetfloor(&flbuf, old_floor);
1329 --flbuf.f_ref_count;
1330 lputfloor(&flbuf, old_floor);
1331 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1332 lgetfloor(&flbuf, new_floor);
1333 ++flbuf.f_ref_count;
1334 lputfloor(&flbuf, new_floor);
1335 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1338 /* ...and everybody say "YATTA!" */
1344 * set room parameters (aide or room aide command)
1346 void cmd_setr(char *args)
1352 char new_name[ROOMNAMELEN];
1354 if (CtdlAccessCheck(ac_logged_in)) return;
1356 if (num_parms(args) >= 6) {
1357 new_floor = extract_int(args, 5);
1359 new_floor = (-1); /* don't change the floor */
1362 /* When is a new name more than just a new name? When the old name
1363 * has a namespace prefix.
1365 if (CC->room.QRflags & QR_MAILBOX) {
1366 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
1368 safestrncpy(new_name, "", sizeof new_name);
1370 extract_token(&new_name[strlen(new_name)], args, 0, '|', (sizeof new_name - strlen(new_name)));
1372 r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
1374 if (r == crr_room_not_found) {
1375 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
1376 } else if (r == crr_already_exists) {
1377 cprintf("%d '%s' already exists.\n",
1378 ERROR + ALREADY_EXISTS, new_name);
1379 } else if (r == crr_noneditable) {
1380 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
1381 } else if (r == crr_invalid_floor) {
1382 cprintf("%d Target floor does not exist.\n",
1383 ERROR + INVALID_FLOOR_OPERATION);
1384 } else if (r == crr_access_denied) {
1385 cprintf("%d You do not have permission to edit '%s'\n",
1386 ERROR + HIGHER_ACCESS_REQUIRED,
1388 } else if (r != crr_ok) {
1389 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1390 ERROR + INTERNAL_ERROR, r);
1397 getroom(&CC->room, new_name);
1399 /* Now we have to do a bunch of other stuff */
1401 if (num_parms(args) >= 7) {
1402 new_order = extract_int(args, 6);
1405 if (new_order > 127)
1409 lgetroom(&CC->room, CC->room.QRname);
1411 /* Directory room */
1412 extract_token(buf, args, 2, '|', sizeof buf);
1414 safestrncpy(CC->room.QRdirname, buf,
1415 sizeof CC->room.QRdirname);
1418 if (num_parms(args) >= 8) {
1419 CC->room.QRdefaultview = extract_int(args, 7);
1422 /* Second set of flags */
1423 if (num_parms(args) >= 9) {
1424 CC->room.QRflags2 = extract_int(args, 8);
1428 CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
1429 /* Clean up a client boo-boo: if the client set the room to
1430 * guess-name or passworded, ensure that the private flag is
1433 if ((CC->room.QRflags & QR_GUESSNAME)
1434 || (CC->room.QRflags & QR_PASSWORDED))
1435 CC->room.QRflags |= QR_PRIVATE;
1437 /* Some changes can't apply to BASEROOM */
1438 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1440 CC->room.QRorder = 0;
1441 CC->room.QRpasswd[0] = '\0';
1442 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1443 QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1444 CC->room.QRflags |= QR_PERMANENT;
1446 /* March order (doesn't apply to AIDEROOM) */
1447 if (num_parms(args) >= 7)
1448 CC->room.QRorder = (char) new_order;
1450 extract_token(buf, args, 1, '|', sizeof buf);
1452 safestrncpy(CC->room.QRpasswd, buf,
1453 sizeof CC->room.QRpasswd);
1454 /* Kick everyone out if the client requested it
1455 * (by changing the room's generation number)
1457 if (extract_int(args, 4)) {
1458 time(&CC->room.QRgen);
1461 /* Some changes can't apply to AIDEROOM */
1462 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1464 CC->room.QRorder = 0;
1465 CC->room.QRflags &= ~QR_MAILBOX;
1466 CC->room.QRflags |= QR_PERMANENT;
1469 /* Write the room record back to disk */
1470 lputroom(&CC->room);
1472 /* Create a room directory if necessary */
1473 if (CC->room.QRflags & QR_DIRECTORY) {
1474 snprintf(buf, sizeof buf,"%s/%s",
1476 CC->room.QRdirname);
1479 snprintf(buf, sizeof buf, "The room \"%s\" has been edited by %s.\n",
1480 CC->room.QRname, CC->curr_user);
1481 aide_message(buf, "Room modification Message");
1482 cprintf("%d Ok\n", CIT_OK);
1488 * get the name of the room aide for this room
1492 struct ctdluser usbuf;
1494 if (CtdlAccessCheck(ac_logged_in)) return;
1496 if (getuserbynumber(&usbuf, CC->room.QRroomaide) == 0) {
1497 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1499 cprintf("%d \n", CIT_OK);
1505 * set the room aide for this room
1507 void cmd_seta(char *new_ra)
1509 struct ctdluser usbuf;
1514 if (CtdlAccessCheck(ac_room_aide)) return;
1516 if (getuser(&usbuf, new_ra) != 0) {
1519 newu = usbuf.usernum;
1522 lgetroom(&CC->room, CC->room.QRname);
1524 if (CC->room.QRroomaide != newu) {
1527 CC->room.QRroomaide = newu;
1528 lputroom(&CC->room);
1531 * We have to post the change notice _after_ writing changes to
1532 * the room table, otherwise it would deadlock!
1534 if (post_notice == 1) {
1535 if (!IsEmptyStr(usbuf.fullname))
1536 snprintf(buf, sizeof buf,
1537 "%s is now the room aide for \"%s\".\n",
1538 usbuf.fullname, CC->room.QRname);
1540 snprintf(buf, sizeof buf,
1541 "There is now no room aide for \"%s\".\n",
1543 aide_message(buf, "Aide Room Modification");
1545 cprintf("%d Ok\n", CIT_OK);
1549 * retrieve info file for this room
1557 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_info_dir);
1558 info_fp = fopen(filename, "r");
1560 if (info_fp == NULL) {
1561 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
1564 cprintf("%d Info:\n", LISTING_FOLLOWS);
1565 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1566 if (!IsEmptyStr(buf))
1567 buf[strlen(buf) - 1] = 0;
1568 cprintf("%s\n", buf);
1575 * Asynchronously schedule a room for deletion. The room will appear
1576 * deleted to the user(s), but it won't actually get purged from the
1577 * database until THE DREADED AUTO-PURGER makes its next run.
1579 void schedule_room_for_deletion(struct ctdlroom *qrbuf)
1581 char old_name[ROOMNAMELEN];
1584 lprintf(CTDL_NOTICE, "Scheduling room <%s> for deletion\n",
1587 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1589 getroom(qrbuf, qrbuf->QRname);
1591 /* Turn the room into a private mailbox owned by a user who doesn't
1592 * exist. This will immediately make the room invisible to everyone,
1593 * and qualify the room for purging.
1595 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1600 qrbuf->QRflags |= QR_MAILBOX;
1601 time(&qrbuf->QRgen); /* Use a timestamp as the new generation number */
1605 b_deleteroom(old_name);
1611 * Back end processing to delete a room and everything associated with it
1612 * (This one is synchronous and should only get called by THE DREADED
1613 * AUTO-PURGER in serv_expire.c. All user-facing code should call
1614 * the asynchronous schedule_room_for_deletion() instead.)
1616 void delete_room(struct ctdlroom *qrbuf)
1620 /* TODO: filename magic? does this realy work? */
1622 lprintf(CTDL_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1624 /* Delete the info file */
1625 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_info_dir);
1628 /* Delete the image file */
1629 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_image_dir);
1632 /* Delete the room's network config file */
1633 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1636 /* Delete the messages in the room
1637 * (Careful: this opens an S_ROOMS critical section!)
1639 CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
1641 /* Flag the room record as not in use */
1642 lgetroom(qrbuf, qrbuf->QRname);
1646 /* then decrement the reference count for the floor */
1647 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1648 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1649 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1651 /* Delete the room record from the database! */
1652 b_deleteroom(qrbuf->QRname);
1658 * Check access control for deleting a room
1660 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1662 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1666 if (is_noneditable(qr)) {
1671 * For mailboxes, check stuff
1673 if (qr->QRflags & QR_MAILBOX) {
1675 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1677 if (atol(qr->QRname) != CC->user.usernum) {
1678 return(0); /* not my room */
1681 /* Can't delete your Mail> room */
1682 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1684 /* Otherwise it's ok */
1689 * For normal rooms, just check for aide or room aide status.
1691 return(is_room_aide());
1695 * aide command: kill the current room
1697 void cmd_kill(char *argbuf)
1699 char deleted_room_name[ROOMNAMELEN];
1703 kill_ok = extract_int(argbuf, 0);
1705 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
1706 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1710 if (CC->room.QRflags & QR_MAILBOX) {
1711 safestrncpy(deleted_room_name, &CC->room.QRname[11], sizeof deleted_room_name);
1714 safestrncpy(deleted_room_name, CC->room.QRname, sizeof deleted_room_name);
1717 /* Do the dirty work */
1718 schedule_room_for_deletion(&CC->room);
1720 /* Return to the Lobby */
1721 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1723 /* tell the world what we did */
1724 snprintf(msg, sizeof msg, "The room \"%s\" has been deleted by %s.\n",
1725 deleted_room_name, CC->curr_user);
1726 aide_message(msg, "Room Purger Message");
1727 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1729 cprintf("%d ok to delete.\n", CIT_OK);
1735 * Internal code to create a new room (returns room flags)
1737 * Room types: 0=public, 1=guessname, 2=passworded, 3=inv-only,
1738 * 4=mailbox, 5=mailbox, but caller supplies namespace
1740 unsigned create_room(char *new_room_name,
1742 char *new_room_pass,
1749 struct ctdlroom qrbuf;
1753 lprintf(CTDL_DEBUG, "create_room(name=%s, type=%d, view=%d)\n",
1754 new_room_name, new_room_type, new_room_view);
1756 if (getroom(&qrbuf, new_room_name) == 0) {
1757 lprintf(CTDL_DEBUG, "%s already exists.\n", new_room_name);
1761 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1762 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1763 qrbuf.QRflags = QR_INUSE;
1764 if (new_room_type > 0)
1765 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1766 if (new_room_type == 1)
1767 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1768 if (new_room_type == 2)
1769 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1770 if ( (new_room_type == 4) || (new_room_type == 5) ) {
1771 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1772 /* qrbuf.QRflags2 |= QR2_SUBJECTREQ; */
1775 /* If the user is requesting a personal room, set up the room
1776 * name accordingly (prepend the user number)
1778 if (new_room_type == 4) {
1779 MailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1782 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1785 /* If the room is private, and the system administrator has elected
1786 * to automatically grant room aide privileges, do so now; otherwise,
1787 * set the room aide to undefined.
1789 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1790 qrbuf.QRroomaide = CC->user.usernum;
1792 qrbuf.QRroomaide = (-1L);
1796 * If the caller is only interested in testing whether this will work,
1797 * return now without creating the room.
1799 if (!really_create) return (qrbuf.QRflags);
1801 qrbuf.QRnumber = get_new_room_number();
1802 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1803 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1804 qrbuf.QRfloor = new_room_floor;
1805 qrbuf.QRdefaultview = new_room_view;
1807 /* save what we just did... */
1810 /* bump the reference count on whatever floor the room is on */
1811 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1812 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1813 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1815 /* Grant the creator access to the room unless the avoid_access
1816 * parameter was specified.
1818 if ( (CC->logged_in) && (avoid_access == 0) ) {
1819 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1820 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1821 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1822 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1825 /* resume our happy day */
1826 return (qrbuf.QRflags);
1833 void cmd_cre8(char *args)
1836 char new_room_name[ROOMNAMELEN];
1838 char new_room_pass[32];
1841 char *notification_message = NULL;
1844 int avoid_access = 0;
1846 cre8_ok = extract_int(args, 0);
1847 extract_token(new_room_name, args, 1, '|', sizeof new_room_name);
1848 new_room_name[ROOMNAMELEN - 1] = 0;
1849 new_room_type = extract_int(args, 2);
1850 extract_token(new_room_pass, args, 3, '|', sizeof new_room_pass);
1851 avoid_access = extract_int(args, 5);
1852 new_room_view = extract_int(args, 6);
1853 new_room_pass[9] = 0;
1856 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 1)) {
1857 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
1861 if (!strcasecmp(new_room_name, MAILROOM)) {
1862 cprintf("%d '%s' already exists.\n",
1863 ERROR + ALREADY_EXISTS, new_room_name);
1867 if (num_parms(args) >= 5) {
1868 fl = cgetfloor(extract_int(args, 4));
1870 cprintf("%d Invalid floor number.\n",
1871 ERROR + INVALID_FLOOR_OPERATION);
1874 else if ((fl->f_flags & F_INUSE) == 0) {
1875 cprintf("%d Invalid floor number.\n",
1876 ERROR + INVALID_FLOOR_OPERATION);
1879 new_room_floor = extract_int(args, 4);
1883 if (CtdlAccessCheck(ac_logged_in)) return;
1885 if (CC->user.axlevel < config.c_createax && !CC->internal_pgm) {
1886 cprintf("%d You need higher access to create rooms.\n",
1887 ERROR + HIGHER_ACCESS_REQUIRED);
1891 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 0)) {
1892 cprintf("%d Ok to create rooms.\n", CIT_OK);
1896 if ((new_room_type < 0) || (new_room_type > 5)) {
1897 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
1901 if (new_room_type == 5) {
1902 if (CC->user.axlevel < 6) {
1903 cprintf("%d Higher access required\n",
1904 ERROR + HIGHER_ACCESS_REQUIRED);
1909 /* Check to make sure the requested room name doesn't already exist */
1910 newflags = create_room(new_room_name,
1911 new_room_type, new_room_pass, new_room_floor,
1912 0, avoid_access, new_room_view);
1913 if (newflags == 0) {
1914 cprintf("%d '%s' already exists.\n",
1915 ERROR + ALREADY_EXISTS, new_room_name);
1920 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
1924 /* If we reach this point, the room needs to be created. */
1926 newflags = create_room(new_room_name,
1927 new_room_type, new_room_pass, new_room_floor, 1, 0,
1930 /* post a message in Aide> describing the new room */
1931 notification_message = malloc(1024);
1932 snprintf(notification_message, 1024,
1933 "A new room called \"%s\" has been created by %s%s%s%s%s%s\n",
1936 ((newflags & QR_MAILBOX) ? " [personal]" : ""),
1937 ((newflags & QR_PRIVATE) ? " [private]" : ""),
1938 ((newflags & QR_GUESSNAME) ? " [hidden]" : ""),
1939 ((newflags & QR_PASSWORDED) ? " Password: " : ""),
1940 ((newflags & QR_PASSWORDED) ? new_room_pass : "")
1942 aide_message(notification_message, "Room Creation Message");
1943 free(notification_message);
1945 cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
1950 void cmd_einf(char *ok)
1951 { /* enter info file for current room */
1953 char infofilename[SIZ];
1958 if (CtdlAccessCheck(ac_room_aide)) return;
1960 if (atoi(ok) == 0) {
1961 cprintf("%d Ok.\n", CIT_OK);
1964 assoc_file_name(infofilename, sizeof infofilename, &CC->room, ctdl_info_dir);
1965 lprintf(CTDL_DEBUG, "opening\n");
1966 fp = fopen(infofilename, "w");
1967 lprintf(CTDL_DEBUG, "checking\n");
1969 cprintf("%d Cannot open %s: %s\n",
1970 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1973 cprintf("%d Send info...\n", SEND_LISTING);
1976 client_getln(buf, sizeof buf);
1977 if (strcmp(buf, "000"))
1978 fprintf(fp, "%s\n", buf);
1979 } while (strcmp(buf, "000"));
1982 /* now update the room index so people will see our new info */
1983 lgetroom(&CC->room, CC->room.QRname); /* lock so no one steps on us */
1984 CC->room.QRinfo = CC->room.QRhighest + 1L;
1985 lputroom(&CC->room);
1990 * cmd_lflr() - List all known floors
1997 if (CtdlAccessCheck(ac_logged_in)) return;
1999 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
2001 for (a = 0; a < MAXFLOORS; ++a) {
2002 getfloor(&flbuf, a);
2003 if (flbuf.f_flags & F_INUSE) {
2004 cprintf("%d|%s|%d\n",
2016 * create a new floor
2018 void cmd_cflr(char *argbuf)
2020 char new_floor_name[256];
2023 int free_slot = (-1);
2026 extract_token(new_floor_name, argbuf, 0, '|', sizeof new_floor_name);
2027 cflr_ok = extract_int(argbuf, 1);
2029 if (CtdlAccessCheck(ac_aide)) return;
2031 if (IsEmptyStr(new_floor_name)) {
2032 cprintf("%d Blank floor name not allowed.\n",
2033 ERROR + ILLEGAL_VALUE);
2037 for (a = 0; a < MAXFLOORS; ++a) {
2038 getfloor(&flbuf, a);
2040 /* note any free slots while we're scanning... */
2041 if (((flbuf.f_flags & F_INUSE) == 0)
2045 /* check to see if it already exists */
2046 if ((!strcasecmp(flbuf.f_name, new_floor_name))
2047 && (flbuf.f_flags & F_INUSE)) {
2048 cprintf("%d Floor '%s' already exists.\n",
2049 ERROR + ALREADY_EXISTS,
2055 if (free_slot < 0) {
2056 cprintf("%d There is no space available for a new floor.\n",
2057 ERROR + INVALID_FLOOR_OPERATION);
2061 cprintf("%d ok to create...\n", CIT_OK);
2064 lgetfloor(&flbuf, free_slot);
2065 flbuf.f_flags = F_INUSE;
2066 flbuf.f_ref_count = 0;
2067 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
2068 lputfloor(&flbuf, free_slot);
2069 cprintf("%d %d\n", CIT_OK, free_slot);
2077 void cmd_kflr(char *argbuf)
2080 int floor_to_delete;
2084 floor_to_delete = extract_int(argbuf, 0);
2085 kflr_ok = extract_int(argbuf, 1);
2087 if (CtdlAccessCheck(ac_aide)) return;
2089 lgetfloor(&flbuf, floor_to_delete);
2092 if ((flbuf.f_flags & F_INUSE) == 0) {
2093 cprintf("%d Floor %d not in use.\n",
2094 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
2097 if (flbuf.f_ref_count != 0) {
2098 cprintf("%d Cannot delete; floor contains %d rooms.\n",
2099 ERROR + INVALID_FLOOR_OPERATION,
2104 cprintf("%d Ok\n", CIT_OK);
2106 cprintf("%d Ok to delete...\n", CIT_OK);
2113 if ((delete_ok == 1) && (kflr_ok == 1))
2115 lputfloor(&flbuf, floor_to_delete);
2121 void cmd_eflr(char *argbuf)
2127 np = num_parms(argbuf);
2129 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
2133 if (CtdlAccessCheck(ac_aide)) return;
2135 floor_num = extract_int(argbuf, 0);
2136 lgetfloor(&flbuf, floor_num);
2137 if ((flbuf.f_flags & F_INUSE) == 0) {
2138 lputfloor(&flbuf, floor_num);
2139 cprintf("%d Floor %d is not in use.\n",
2140 ERROR + INVALID_FLOOR_OPERATION, floor_num);
2144 extract_token(flbuf.f_name, argbuf, 1, '|', sizeof flbuf.f_name);
2145 lputfloor(&flbuf, floor_num);
2147 cprintf("%d Ok\n", CIT_OK);