4 * Server functions which perform operations on room objects.
15 #include <dirent.h> /* for cmd_rdir to read contents of the directory */
17 #if TIME_WITH_SYS_TIME
18 # include <sys/time.h>
22 # include <sys/time.h>
31 #include <libcitadel.h>
36 #include "sysdep_decls.h"
40 #include "citserver.h"
42 #include "citadel_dirs.h"
45 #include "ctdl_module.h"
47 struct floor *floorcache[MAXFLOORS];
50 * Retrieve access control information for any user/room pair
52 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf,
53 int *result, int *view)
58 /* for internal programs, always do everything */
59 if (((CC->internal_pgm)) && (roombuf->QRflags & QR_INUSE)) {
60 retval = (UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED);
65 /* Locate any applicable user/room relationships */
66 CtdlGetRelationship(&vbuf, userbuf, roombuf);
68 /* Force the properties of the Aide room */
69 if (!strcasecmp(roombuf->QRname, config.c_aideroom)) {
70 if (userbuf->axlevel >= 6) {
71 retval = UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
78 /* If this is a public room, it's accessible... */
79 if ( ((roombuf->QRflags & QR_PRIVATE) == 0)
80 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
81 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
84 /* If this is a preferred users only room, check access level */
85 if (roombuf->QRflags & QR_PREFONLY) {
86 if (userbuf->axlevel < 5) {
87 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
91 /* For private rooms, check the generation number matchups */
92 if ( (roombuf->QRflags & QR_PRIVATE)
93 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
95 /* An explicit match means the user belongs in this room */
96 if (vbuf.v_flags & V_ACCESS) {
97 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
99 /* Otherwise, check if this is a guess-name or passworded
100 * room. If it is, a goto may at least be attempted
102 else if ((roombuf->QRflags & QR_PRIVATE)
103 || (roombuf->QRflags & QR_PASSWORDED)) {
104 retval = retval & ~UA_KNOWN;
105 retval = retval | UA_GOTOALLOWED;
109 /* For mailbox rooms, also check the namespace */
110 /* Also, mailbox owners can delete their messages */
111 if (roombuf->QRflags & QR_MAILBOX) {
112 if (userbuf->usernum == atol(roombuf->QRname)) {
113 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
115 /* An explicit match means the user belongs in this room */
116 if (vbuf.v_flags & V_ACCESS) {
117 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
121 /* For non-mailbox rooms... */
124 /* User is allowed to post in the room unless:
125 * - User is not validated
126 * - User has no net privileges and it is a shared network room
127 * - It is a read-only room
129 int post_allowed = 1;
130 if (CC->user.axlevel < 2) post_allowed = 0;
131 if ((CC->user.axlevel < 4) && (CC->room.QRflags & QR_NETWORK)) post_allowed = 0;
132 if (roombuf->QRflags & QR_READONLY) post_allowed = 0;
134 retval = retval | UA_POSTALLOWED;
137 /* If "collaborative deletion" is active for this room, any user who can post
138 * is also allowed to delete
140 if (CC->room.QRflags2 & QR2_COLLABDEL) {
141 if (retval & UA_POSTALLOWED) {
142 retval = retval | UA_DELETEALLOWED;
148 /* Check to see if the user has forgotten this room */
149 if (vbuf.v_flags & V_FORGET) {
150 retval = retval & ~UA_KNOWN;
151 if ( ( ((roombuf->QRflags & QR_PRIVATE) == 0)
152 && ((roombuf->QRflags & QR_MAILBOX) == 0) )
153 || ( (roombuf->QRflags & QR_MAILBOX)
154 && (atol(roombuf->QRname) == CC->user.usernum))) {
155 retval = retval | UA_ZAPPED;
158 /* If user is explicitly locked out of this room, deny everything */
159 if (vbuf.v_flags & V_LOCKOUT) {
160 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED & ~UA_POSTALLOWED;
163 /* Aides get access to all private rooms */
164 if ( (userbuf->axlevel >= 6)
165 && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
166 if (vbuf.v_flags & V_FORGET) {
167 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED;
170 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED;
174 /* Aides can gain access to mailboxes as well, but they don't show
177 if ( (userbuf->axlevel >= 6)
178 && (roombuf->QRflags & QR_MAILBOX) ) {
179 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED;
182 /* Aides and Room Aides have admin privileges */
183 if ( (userbuf->axlevel >= 6)
184 || (userbuf->usernum == roombuf->QRroomaide)
186 retval = retval | UA_ADMINALLOWED | UA_DELETEALLOWED | UA_POSTALLOWED;
189 NEWMSG: /* By the way, we also check for the presence of new messages */
190 if (is_msg_in_sequence_set(vbuf.v_seen, roombuf->QRhighest) == 0) {
191 retval = retval | UA_HASNEWMSGS;
194 /* System rooms never show up in the list. */
195 if (roombuf->QRflags2 & QR2_SYSTEM) {
196 retval = retval & ~UA_KNOWN;
200 /* Now give the caller the information it wants. */
201 if (result != NULL) *result = retval;
202 if (view != NULL) *view = vbuf.v_view;
206 * Self-checking stuff for a room record read into memory
208 void room_sanity_check(struct ctdlroom *qrbuf)
210 /* Mailbox rooms are always on the lowest floor */
211 if (qrbuf->QRflags & QR_MAILBOX) {
214 /* Listing order of 0 is illegal except for base rooms */
215 if (qrbuf->QRorder == 0)
216 if (!(qrbuf->QRflags & QR_MAILBOX) &&
217 strncasecmp(qrbuf->QRname, config.c_baseroom, ROOMNAMELEN)
219 strncasecmp(qrbuf->QRname, config.c_aideroom, ROOMNAMELEN))
225 * getroom() - retrieve room data from disk
227 int getroom(struct ctdlroom *qrbuf, char *room_name)
229 struct cdbdata *cdbqr;
230 char lowercase_name[ROOMNAMELEN];
231 char personal_lowercase_name[ROOMNAMELEN];
232 char *dptr, *sptr, *eptr;
234 dptr = lowercase_name;
236 eptr = (dptr + (sizeof lowercase_name - 1));
237 while (!IsEmptyStr(sptr) && (dptr < eptr)){
238 *dptr = tolower(*sptr);
243 memset(qrbuf, 0, sizeof(struct ctdlroom));
245 /* First, try the public namespace */
246 cdbqr = cdb_fetch(CDB_ROOMS,
247 lowercase_name, strlen(lowercase_name));
249 /* If that didn't work, try the user's personal namespace */
251 snprintf(personal_lowercase_name,
252 sizeof personal_lowercase_name, "%010ld.%s",
253 CC->user.usernum, lowercase_name);
254 cdbqr = cdb_fetch(CDB_ROOMS,
255 personal_lowercase_name,
256 strlen(personal_lowercase_name));
259 memcpy(qrbuf, cdbqr->ptr,
260 ((cdbqr->len > sizeof(struct ctdlroom)) ?
261 sizeof(struct ctdlroom) : cdbqr->len));
264 room_sanity_check(qrbuf);
273 * lgetroom() - same as getroom() but locks the record (if supported)
275 int lgetroom(struct ctdlroom *qrbuf, char *room_name)
278 retval = getroom(qrbuf, room_name);
279 if (retval == 0) begin_critical_section(S_ROOMS);
285 * b_putroom() - back end to putroom() and b_deleteroom()
286 * (if the supplied buffer is NULL, delete the room record)
288 void b_putroom(struct ctdlroom *qrbuf, char *room_name)
290 char lowercase_name[ROOMNAMELEN];
295 bptr = lowercase_name;
296 while (!IsEmptyStr(aptr))
298 *bptr = tolower(*aptr);
304 len = bptr - lowercase_name;
306 cdb_delete(CDB_ROOMS,
307 lowercase_name, len);
309 time(&qrbuf->QRmtime);
312 qrbuf, sizeof(struct ctdlroom));
318 * putroom() - store room data to disk
320 void putroom(struct ctdlroom *qrbuf) {
321 b_putroom(qrbuf, qrbuf->QRname);
326 * b_deleteroom() - delete a room record from disk
328 void b_deleteroom(char *room_name) {
329 b_putroom(NULL, room_name);
335 * lputroom() - same as putroom() but unlocks the record (if supported)
337 void lputroom(struct ctdlroom *qrbuf)
341 end_critical_section(S_ROOMS);
345 /****************************************************************************/
348 * getfloor() - retrieve floor data from disk
350 void getfloor(struct floor *flbuf, int floor_num)
352 struct cdbdata *cdbfl;
354 memset(flbuf, 0, sizeof(struct floor));
355 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
357 memcpy(flbuf, cdbfl->ptr,
358 ((cdbfl->len > sizeof(struct floor)) ?
359 sizeof(struct floor) : cdbfl->len));
362 if (floor_num == 0) {
363 safestrncpy(flbuf->f_name, "Main Floor",
364 sizeof flbuf->f_name);
365 flbuf->f_flags = F_INUSE;
366 flbuf->f_ref_count = 3;
373 * lgetfloor() - same as getfloor() but locks the record (if supported)
375 void lgetfloor(struct floor *flbuf, int floor_num)
378 begin_critical_section(S_FLOORTAB);
379 getfloor(flbuf, floor_num);
384 * cgetfloor() - Get floor record from *cache* (loads from disk if needed)
386 * This is strictly a performance hack.
388 struct floor *cgetfloor(int floor_num) {
389 static int initialized = 0;
392 struct floor *fl = NULL;
394 begin_critical_section(S_FLOORCACHE);
395 if (initialized == 0) {
396 for (i=0; i<MAXFLOORS; ++i) {
397 floorcache[floor_num] = NULL;
401 if (floorcache[floor_num] == NULL) {
404 end_critical_section(S_FLOORCACHE);
407 fl = malloc(sizeof(struct floor));
408 getfloor(fl, floor_num);
409 begin_critical_section(S_FLOORCACHE);
410 if (floorcache[floor_num] != NULL) {
411 free(floorcache[floor_num]);
413 floorcache[floor_num] = fl;
414 end_critical_section(S_FLOORCACHE);
417 return(floorcache[floor_num]);
423 * putfloor() - store floor data on disk
425 void putfloor(struct floor *flbuf, int floor_num)
427 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
428 begin_critical_section(S_FLOORCACHE);
429 if (floorcache[floor_num] != NULL) {
430 free(floorcache[floor_num]);
431 floorcache[floor_num] = malloc(sizeof(struct floor));
432 memcpy(floorcache[floor_num], flbuf, sizeof(struct floor));
434 end_critical_section(S_FLOORCACHE);
436 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
437 flbuf, sizeof(struct floor));
442 * lputfloor() - same as putfloor() but unlocks the record (if supported)
444 void lputfloor(struct floor *flbuf, int floor_num)
447 putfloor(flbuf, floor_num);
448 end_critical_section(S_FLOORTAB);
454 * Traverse the room file...
456 void ForEachRoom(void (*CallBack) (struct ctdlroom *EachRoom, void *out_data),
459 struct ctdlroom qrbuf;
460 struct cdbdata *cdbqr;
462 cdb_rewind(CDB_ROOMS);
464 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
465 memset(&qrbuf, 0, sizeof(struct ctdlroom));
466 memcpy(&qrbuf, cdbqr->ptr,
467 ((cdbqr->len > sizeof(struct ctdlroom)) ?
468 sizeof(struct ctdlroom) : cdbqr->len));
470 room_sanity_check(&qrbuf);
471 if (qrbuf.QRflags & QR_INUSE)
472 (*CallBack)(&qrbuf, in_data);
478 * delete_msglist() - delete room message pointers
480 void delete_msglist(struct ctdlroom *whichroom)
482 struct cdbdata *cdbml;
484 /* Make sure the msglist we're deleting actually exists, otherwise
485 * gdbm will complain when we try to delete an invalid record
487 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
491 /* Go ahead and delete it */
492 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
499 * Message pointer compare function for sort_msglist()
501 int sort_msglist_cmp(const void *m1, const void *m2) {
502 if ((*(const long *)m1) > (*(const long *)m2)) return(1);
503 if ((*(const long *)m1) < (*(const long *)m2)) return(-1);
509 * sort message pointers
510 * (returns new msg count)
512 int sort_msglist(long listptrs[], int oldcount)
522 qsort(listptrs, numitems, sizeof(long), sort_msglist_cmp);
524 /* and yank any nulls */
525 while ((numitems > 0) && (listptrs[0] == 0L)) {
526 memmove(&listptrs[0], &listptrs[1],
527 (sizeof(long) * (numitems - 1)));
536 * Determine whether a given room is non-editable.
538 int is_noneditable(struct ctdlroom *qrbuf)
541 /* Mail> rooms are non-editable */
542 if ( (qrbuf->QRflags & QR_MAILBOX)
543 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
546 /* Everything else is editable */
553 * Back-back-end for all room listing commands
555 void list_roomname(struct ctdlroom *qrbuf, int ra, int current_view, int default_view)
557 char truncated_roomname[ROOMNAMELEN];
559 /* For my own mailbox rooms, chop off the owner prefix */
560 if ( (qrbuf->QRflags & QR_MAILBOX)
561 && (atol(qrbuf->QRname) == CC->user.usernum) ) {
562 safestrncpy(truncated_roomname, qrbuf->QRname, sizeof truncated_roomname);
563 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
564 cprintf("%s", truncated_roomname);
566 /* For all other rooms, just display the name in its entirety */
568 cprintf("%s", qrbuf->QRname);
571 /* ...and now the other parameters */
572 cprintf("|%u|%d|%d|%d|%d|%d|%d|%ld|\n",
574 (int) qrbuf->QRfloor,
575 (int) qrbuf->QRorder,
576 (int) qrbuf->QRflags2,
586 * cmd_lrms() - List all accessible rooms, known or forgotten
588 void cmd_lrms_backend(struct ctdlroom *qrbuf, void *data)
590 int FloorBeingSearched = (-1);
594 FloorBeingSearched = *(int *)data;
595 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
597 if ((( ra & (UA_KNOWN | UA_ZAPPED)))
598 && ((qrbuf->QRfloor == (FloorBeingSearched))
599 || ((FloorBeingSearched) < 0)))
600 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
603 void cmd_lrms(char *argbuf)
605 int FloorBeingSearched = (-1);
606 if (!IsEmptyStr(argbuf))
607 FloorBeingSearched = extract_int(argbuf, 0);
609 if (CtdlAccessCheck(ac_logged_in)) return;
611 if (getuser(&CC->user, CC->curr_user)) {
612 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
615 cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
617 ForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
624 * cmd_lkra() - List all known rooms
626 void cmd_lkra_backend(struct ctdlroom *qrbuf, void *data)
628 int FloorBeingSearched = (-1);
632 FloorBeingSearched = *(int *)data;
633 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
635 if ((( ra & (UA_KNOWN)))
636 && ((qrbuf->QRfloor == (FloorBeingSearched))
637 || ((FloorBeingSearched) < 0)))
638 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
641 void cmd_lkra(char *argbuf)
643 int FloorBeingSearched = (-1);
644 if (!IsEmptyStr(argbuf))
645 FloorBeingSearched = extract_int(argbuf, 0);
647 if (CtdlAccessCheck(ac_logged_in)) return;
649 if (getuser(&CC->user, CC->curr_user)) {
650 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
653 cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
655 ForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
661 void cmd_lprm_backend(struct ctdlroom *qrbuf, void *data)
663 int FloorBeingSearched = (-1);
667 FloorBeingSearched = *(int *)data;
668 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
670 if ( ((qrbuf->QRflags & QR_PRIVATE) == 0)
671 && ((qrbuf->QRflags & QR_MAILBOX) == 0)
672 && ((qrbuf->QRfloor == (FloorBeingSearched))
673 || ((FloorBeingSearched) < 0)))
674 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
677 void cmd_lprm(char *argbuf)
679 int FloorBeingSearched = (-1);
680 if (!IsEmptyStr(argbuf))
681 FloorBeingSearched = extract_int(argbuf, 0);
683 cprintf("%d Publiic rooms:\n", LISTING_FOLLOWS);
685 ForEachRoom(cmd_lprm_backend, &FloorBeingSearched);
692 * cmd_lkrn() - List all known rooms with new messages
694 void cmd_lkrn_backend(struct ctdlroom *qrbuf, void *data)
696 int FloorBeingSearched = (-1);
700 FloorBeingSearched = *(int *)data;
701 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
704 && (ra & UA_HASNEWMSGS)
705 && ((qrbuf->QRfloor == (FloorBeingSearched))
706 || ((FloorBeingSearched) < 0)))
707 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
710 void cmd_lkrn(char *argbuf)
712 int FloorBeingSearched = (-1);
713 if (!IsEmptyStr(argbuf))
714 FloorBeingSearched = extract_int(argbuf, 0);
716 if (CtdlAccessCheck(ac_logged_in)) return;
718 if (getuser(&CC->user, CC->curr_user)) {
719 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
722 cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
724 ForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
731 * cmd_lkro() - List all known rooms
733 void cmd_lkro_backend(struct ctdlroom *qrbuf, void *data)
735 int FloorBeingSearched = (-1);
739 FloorBeingSearched = *(int *)data;
740 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
743 && ((ra & UA_HASNEWMSGS) == 0)
744 && ((qrbuf->QRfloor == (FloorBeingSearched))
745 || ((FloorBeingSearched) < 0)))
746 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
749 void cmd_lkro(char *argbuf)
751 int FloorBeingSearched = (-1);
752 if (!IsEmptyStr(argbuf))
753 FloorBeingSearched = extract_int(argbuf, 0);
755 if (CtdlAccessCheck(ac_logged_in)) return;
757 if (getuser(&CC->user, CC->curr_user)) {
758 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
761 cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
763 ForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
770 * cmd_lzrm() - List all forgotten rooms
772 void cmd_lzrm_backend(struct ctdlroom *qrbuf, void *data)
774 int FloorBeingSearched = (-1);
778 FloorBeingSearched = *(int *)data;
779 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
781 if ((ra & UA_GOTOALLOWED)
783 && ((qrbuf->QRfloor == (FloorBeingSearched))
784 || ((FloorBeingSearched) < 0)))
785 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
788 void cmd_lzrm(char *argbuf)
790 int FloorBeingSearched = (-1);
791 if (!IsEmptyStr(argbuf))
792 FloorBeingSearched = extract_int(argbuf, 0);
794 if (CtdlAccessCheck(ac_logged_in)) return;
796 if (getuser(&CC->user, CC->curr_user)) {
797 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
800 cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
802 ForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
808 * Make the specified room the current room for this session. No validation
809 * or access control is done here -- the caller should make sure that the
810 * specified room exists and is ok to access.
812 void usergoto(char *where, int display_result, int transiently,
813 int *retmsgs, int *retnew)
816 int new_messages = 0;
817 int old_messages = 0;
818 int total_messages = 0;
822 int newmailcount = 0;
824 char truncated_roomname[ROOMNAMELEN];
825 struct cdbdata *cdbfr;
826 long *msglist = NULL;
828 unsigned int original_v_flags;
831 char setstr[128], lostr[64], histr[64];
835 /* If the supplied room name is NULL, the caller wants us to know that
836 * it has already copied the room record into CC->room, so
837 * we can skip the extra database fetch.
840 safestrncpy(CC->room.QRname, where, sizeof CC->room.QRname);
841 getroom(&CC->room, where);
844 /* Take care of all the formalities. */
846 begin_critical_section(S_USERS);
847 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
848 original_v_flags = vbuf.v_flags;
850 /* Know the room ... but not if it's the page log room, or if the
851 * caller specified that we're only entering this room transiently.
853 if ((strcasecmp(CC->room.QRname, config.c_logpages))
854 && (transiently == 0) ) {
855 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
856 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
859 /* Only rewrite the database record if we changed something */
860 if (vbuf.v_flags != original_v_flags) {
861 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
863 end_critical_section(S_USERS);
865 /* Check for new mail */
866 newmailcount = NewMailCount();
868 /* set info to 1 if the user needs to read the room's info file */
869 if (CC->room.QRinfo > vbuf.v_lastseen) {
873 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
875 msglist = (long *) cdbfr->ptr;
876 cdbfr->ptr = NULL; /* usergoto() now owns this memory */
877 num_msgs = cdbfr->len / sizeof(long);
882 for (a=0; a<num_msgs; ++a) {
883 if (msglist[a] > 0L) ++total_messages;
885 new_messages = num_msgs;
886 num_sets = num_tokens(vbuf.v_seen, ',');
887 for (s=0; s<num_sets; ++s) {
888 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
890 extract_token(lostr, setstr, 0, ':', sizeof lostr);
891 if (num_tokens(setstr, ':') >= 2) {
892 extract_token(histr, setstr, 1, ':', sizeof histr);
893 if (!strcmp(histr, "*")) {
894 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
898 strcpy(histr, lostr);
903 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
904 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
910 new_messages = total_messages - old_messages;
912 if (msglist != NULL) free(msglist);
914 if (CC->room.QRflags & QR_MAILBOX)
919 if ((CC->room.QRroomaide == CC->user.usernum)
920 || (CC->user.axlevel >= 6))
925 safestrncpy(truncated_roomname, CC->room.QRname, sizeof truncated_roomname);
926 if ( (CC->room.QRflags & QR_MAILBOX)
927 && (atol(CC->room.QRname) == CC->user.usernum) ) {
928 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
931 if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
935 if (retmsgs != NULL) *retmsgs = total_messages;
936 if (retnew != NULL) *retnew = new_messages;
937 CtdlLogPrintf(CTDL_DEBUG, "<%s> %d new of %d total messages\n",
939 new_messages, total_messages
942 CC->curr_view = (int)vbuf.v_view;
944 if (display_result) {
945 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|%d|\n",
946 CIT_OK, CtdlCheckExpress(),
951 (int)CC->room.QRflags,
952 (long)CC->room.QRhighest,
953 (long)vbuf.v_lastseen,
957 (int)CC->room.QRfloor,
959 (int)CC->room.QRdefaultview,
961 (int)CC->room.QRflags2
968 * Handle some of the macro named rooms
970 void convert_room_name_macros(char *towhere, size_t maxlen) {
971 if (!strcasecmp(towhere, "_BASEROOM_")) {
972 safestrncpy(towhere, config.c_baseroom, maxlen);
974 else if (!strcasecmp(towhere, "_MAIL_")) {
975 safestrncpy(towhere, MAILROOM, maxlen);
977 else if (!strcasecmp(towhere, "_TRASH_")) {
978 safestrncpy(towhere, USERTRASHROOM, maxlen);
980 else if (!strcasecmp(towhere, "_DRAFTS_")) {
981 safestrncpy(towhere, USERDRAFTROOM, maxlen);
983 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
984 safestrncpy(towhere, config.c_twitroom, maxlen);
986 else if (!strcasecmp(towhere, "_CALENDAR_")) {
987 safestrncpy(towhere, USERCALENDARROOM, maxlen);
989 else if (!strcasecmp(towhere, "_TASKS_")) {
990 safestrncpy(towhere, USERTASKSROOM, maxlen);
992 else if (!strcasecmp(towhere, "_CONTACTS_")) {
993 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
995 else if (!strcasecmp(towhere, "_NOTES_")) {
996 safestrncpy(towhere, USERNOTESROOM, maxlen);
1002 * cmd_goto() - goto a new room
1004 void cmd_goto(char *gargs)
1006 struct ctdlroom QRscratch;
1010 char augmented_roomname[ROOMNAMELEN];
1011 char towhere[ROOMNAMELEN];
1013 int transiently = 0;
1015 if (CtdlAccessCheck(ac_logged_in)) return;
1017 extract_token(towhere, gargs, 0, '|', sizeof towhere);
1018 extract_token(password, gargs, 1, '|', sizeof password);
1019 transiently = extract_int(gargs, 2);
1021 getuser(&CC->user, CC->curr_user);
1024 * Handle some of the macro named rooms
1026 convert_room_name_macros(towhere, sizeof towhere);
1028 /* First try a regular match */
1029 c = getroom(&QRscratch, towhere);
1031 /* Then try a mailbox name match */
1033 MailboxName(augmented_roomname, sizeof augmented_roomname,
1034 &CC->user, towhere);
1035 c = getroom(&QRscratch, augmented_roomname);
1037 safestrncpy(towhere, augmented_roomname, sizeof towhere);
1040 /* And if the room was found... */
1043 /* Let internal programs go directly to any room. */
1044 if (CC->internal_pgm) {
1045 memcpy(&CC->room, &QRscratch,
1046 sizeof(struct ctdlroom));
1047 usergoto(NULL, 1, transiently, NULL, NULL);
1051 /* See if there is an existing user/room relationship */
1052 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
1054 /* normal clients have to pass through security */
1055 if (ra & UA_GOTOALLOWED) {
1060 if ((QRscratch.QRflags & QR_MAILBOX) &&
1061 ((ra & UA_GOTOALLOWED))) {
1062 memcpy(&CC->room, &QRscratch,
1063 sizeof(struct ctdlroom));
1064 usergoto(NULL, 1, transiently, NULL, NULL);
1066 } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
1067 ((ra & UA_KNOWN) == 0) &&
1068 (strcasecmp(QRscratch.QRpasswd, password)) &&
1069 (CC->user.axlevel < 6)
1071 cprintf("%d wrong or missing passwd\n",
1072 ERROR + PASSWORD_REQUIRED);
1074 } else if ((QRscratch.QRflags & QR_PRIVATE) &&
1075 ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
1076 ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
1077 ((ra & UA_KNOWN) == 0) &&
1078 (CC->user.axlevel < 6)
1080 CtdlLogPrintf(CTDL_DEBUG, "Failed to acquire private room\n");
1082 memcpy(&CC->room, &QRscratch,
1083 sizeof(struct ctdlroom));
1084 usergoto(NULL, 1, transiently, NULL, NULL);
1090 cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
1094 void cmd_whok(char *cmdbuf)
1096 struct ctdluser temp;
1097 struct cdbdata *cdbus;
1100 cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
1101 cdb_rewind(CDB_USERS);
1102 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1103 memset(&temp, 0, sizeof temp);
1104 memcpy(&temp, cdbus->ptr, sizeof temp);
1107 CtdlRoomAccess(&CC->room, &temp, &ra, NULL);
1108 if ((CC->room.QRflags & QR_INUSE)
1111 cprintf("%s\n", temp.fullname);
1118 * RDIR command for room directory
1120 void cmd_rdir(char *cmdbuf)
1125 struct stat statbuf;
1126 DIR *filedir = NULL;
1127 struct dirent *filedir_entry;
1133 if (CtdlAccessCheck(ac_logged_in)) return;
1135 getroom(&CC->room, CC->room.QRname);
1136 getuser(&CC->user, CC->curr_user);
1138 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
1139 cprintf("%d not here.\n", ERROR + NOT_HERE);
1142 if (((CC->room.QRflags & QR_VISDIR) == 0)
1143 && (CC->user.axlevel < 6)
1144 && (CC->user.usernum != CC->room.QRroomaide)) {
1145 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1149 snprintf(buf, sizeof buf, "%s/%s", ctdl_file_dir, CC->room.QRdirname);
1150 filedir = opendir (buf);
1152 if (filedir == NULL) {
1153 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1156 cprintf("%d %s|%s/%s\n", LISTING_FOLLOWS, config.c_fqdn, ctdl_file_dir, CC->room.QRdirname);
1158 snprintf(buf, sizeof buf, "%s/%s/filedir", ctdl_file_dir, CC->room.QRdirname);
1159 fd = fopen(buf, "r");
1161 fd = fopen("/dev/null", "r");
1162 while ((filedir_entry = readdir(filedir)))
1164 if (strcasecmp(filedir_entry->d_name, "filedir") && filedir_entry->d_name[0] != '.')
1166 #ifdef _DIRENT_HAVE_D_NAMELEN
1167 d_namelen = filedir_entry->d_namelen;
1169 d_namelen = strlen(filedir_entry->d_name);
1171 snprintf(buf, sizeof buf, "%s/%s/%s", ctdl_file_dir, CC->room.QRdirname, filedir_entry->d_name);
1172 stat(buf, &statbuf); /* stat the file */
1173 if (!(statbuf.st_mode & S_IFREG))
1175 snprintf(buf2, sizeof buf2, "Command RDIR found something that is not a useable file. It should be cleaned up.\n RDIR found this non regular file:\n%s\n", buf);
1176 aide_message(buf2, "RDIR found bad file");
1177 continue; /* not a useable file type so don't show it */
1179 safestrncpy(comment, "", sizeof comment);
1180 fseek(fd, 0L, 0); /* rewind descriptions file */
1181 /* Get the description from the descriptions file */
1182 while ((fgets(buf, sizeof buf, fd) != NULL) && (IsEmptyStr(comment)))
1184 buf[strlen(buf) - 1] = 0;
1185 if ((!strncasecmp(buf, filedir_entry->d_name, d_namelen)) && (buf[d_namelen] == ' '))
1186 safestrncpy(comment, &buf[d_namelen + 1], sizeof comment);
1188 len = extract_token (mimebuf, comment, 0,' ', 64);
1189 if ((len <0) || strchr(mimebuf, '/') == NULL)
1191 snprintf (mimebuf, 64, "application/octetstream");
1194 cprintf("%s|%ld|%s|%s\n",
1195 filedir_entry->d_name,
1196 (long)statbuf.st_size,
1208 * get room parameters (aide or room aide command)
1210 void cmd_getr(char *cmdbuf)
1212 if (CtdlAccessCheck(ac_room_aide)) return;
1214 getroom(&CC->room, CC->room.QRname);
1215 cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1219 ((CC->room.QRflags & QR_MAILBOX) ?
1220 &CC->room.QRname[11] : CC->room.QRname),
1222 ((CC->room.QRflags & QR_PASSWORDED) ?
1223 CC->room.QRpasswd : ""),
1225 ((CC->room.QRflags & QR_DIRECTORY) ?
1226 CC->room.QRdirname : ""),
1229 (int) CC->room.QRfloor,
1230 (int) CC->room.QRorder,
1232 CC->room.QRdefaultview,
1239 * Back end function to rename a room.
1240 * You can also specify which floor to move the room to, or specify -1 to
1241 * keep the room on the same floor it was on.
1243 * If you are renaming a mailbox room, you must supply the namespace prefix
1244 * in *at least* the old name!
1246 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1248 struct ctdlroom qrbuf;
1249 struct ctdlroom qrtmp;
1254 char actual_old_name[ROOMNAMELEN];
1256 CtdlLogPrintf(CTDL_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1257 old_name, new_name, new_floor);
1259 if (new_floor >= 0) {
1260 fl = cgetfloor(new_floor);
1261 if ((fl->f_flags & F_INUSE) == 0) {
1262 return(crr_invalid_floor);
1266 begin_critical_section(S_ROOMS);
1268 if ( (getroom(&qrtmp, new_name) == 0)
1269 && (strcasecmp(new_name, old_name)) ) {
1270 ret = crr_already_exists;
1273 else if (getroom(&qrbuf, old_name) != 0) {
1274 ret = crr_room_not_found;
1277 else if ( (CC->user.axlevel < 6) && (!CC->internal_pgm)
1278 && (CC->user.usernum != qrbuf.QRroomaide)
1279 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) ) {
1280 ret = crr_access_denied;
1283 else if (is_noneditable(&qrbuf)) {
1284 ret = crr_noneditable;
1289 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1290 if (qrbuf.QRflags & QR_MAILBOX) {
1291 owner = atol(qrbuf.QRname);
1293 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1294 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1295 "%010ld.%s", owner, new_name);
1298 safestrncpy(qrbuf.QRname, new_name,
1299 sizeof(qrbuf.QRname));
1302 /* Reject change of floor for baseroom/aideroom */
1303 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1304 !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1308 /* Take care of floor stuff */
1309 old_floor = qrbuf.QRfloor;
1310 if (new_floor < 0) {
1311 new_floor = old_floor;
1313 qrbuf.QRfloor = new_floor;
1316 begin_critical_section(S_CONFIG);
1318 /* If baseroom/aideroom name changes, update config */
1319 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1320 safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1323 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1324 safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1328 end_critical_section(S_CONFIG);
1330 /* If the room name changed, then there are now two room
1331 * records, so we have to delete the old one.
1333 if (strcasecmp(new_name, old_name)) {
1334 b_deleteroom(actual_old_name);
1340 end_critical_section(S_ROOMS);
1342 /* Adjust the floor reference counts if necessary */
1343 if (new_floor != old_floor) {
1344 lgetfloor(&flbuf, old_floor);
1345 --flbuf.f_ref_count;
1346 lputfloor(&flbuf, old_floor);
1347 CtdlLogPrintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1348 lgetfloor(&flbuf, new_floor);
1349 ++flbuf.f_ref_count;
1350 lputfloor(&flbuf, new_floor);
1351 CtdlLogPrintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1354 /* ...and everybody say "YATTA!" */
1360 * set room parameters (aide or room aide command)
1362 void cmd_setr(char *args)
1368 char new_name[ROOMNAMELEN];
1370 if (CtdlAccessCheck(ac_logged_in)) return;
1372 if (num_parms(args) >= 6) {
1373 new_floor = extract_int(args, 5);
1375 new_floor = (-1); /* don't change the floor */
1378 /* When is a new name more than just a new name? When the old name
1379 * has a namespace prefix.
1381 if (CC->room.QRflags & QR_MAILBOX) {
1382 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
1384 safestrncpy(new_name, "", sizeof new_name);
1386 extract_token(&new_name[strlen(new_name)], args, 0, '|', (sizeof new_name - strlen(new_name)));
1388 r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
1390 if (r == crr_room_not_found) {
1391 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
1392 } else if (r == crr_already_exists) {
1393 cprintf("%d '%s' already exists.\n",
1394 ERROR + ALREADY_EXISTS, new_name);
1395 } else if (r == crr_noneditable) {
1396 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
1397 } else if (r == crr_invalid_floor) {
1398 cprintf("%d Target floor does not exist.\n",
1399 ERROR + INVALID_FLOOR_OPERATION);
1400 } else if (r == crr_access_denied) {
1401 cprintf("%d You do not have permission to edit '%s'\n",
1402 ERROR + HIGHER_ACCESS_REQUIRED,
1404 } else if (r != crr_ok) {
1405 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1406 ERROR + INTERNAL_ERROR, r);
1413 getroom(&CC->room, new_name);
1415 /* Now we have to do a bunch of other stuff */
1417 if (num_parms(args) >= 7) {
1418 new_order = extract_int(args, 6);
1421 if (new_order > 127)
1425 lgetroom(&CC->room, CC->room.QRname);
1427 /* Directory room */
1428 extract_token(buf, args, 2, '|', sizeof buf);
1430 safestrncpy(CC->room.QRdirname, buf,
1431 sizeof CC->room.QRdirname);
1434 if (num_parms(args) >= 8) {
1435 CC->room.QRdefaultview = extract_int(args, 7);
1438 /* Second set of flags */
1439 if (num_parms(args) >= 9) {
1440 CC->room.QRflags2 = extract_int(args, 8);
1444 CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
1445 /* Clean up a client boo-boo: if the client set the room to
1446 * guess-name or passworded, ensure that the private flag is
1449 if ((CC->room.QRflags & QR_GUESSNAME)
1450 || (CC->room.QRflags & QR_PASSWORDED))
1451 CC->room.QRflags |= QR_PRIVATE;
1453 /* Some changes can't apply to BASEROOM */
1454 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1456 CC->room.QRorder = 0;
1457 CC->room.QRpasswd[0] = '\0';
1458 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1459 QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1460 CC->room.QRflags |= QR_PERMANENT;
1462 /* March order (doesn't apply to AIDEROOM) */
1463 if (num_parms(args) >= 7)
1464 CC->room.QRorder = (char) new_order;
1466 extract_token(buf, args, 1, '|', sizeof buf);
1468 safestrncpy(CC->room.QRpasswd, buf,
1469 sizeof CC->room.QRpasswd);
1470 /* Kick everyone out if the client requested it
1471 * (by changing the room's generation number)
1473 if (extract_int(args, 4)) {
1474 time(&CC->room.QRgen);
1477 /* Some changes can't apply to AIDEROOM */
1478 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1480 CC->room.QRorder = 0;
1481 CC->room.QRflags &= ~QR_MAILBOX;
1482 CC->room.QRflags |= QR_PERMANENT;
1485 /* Write the room record back to disk */
1486 lputroom(&CC->room);
1488 /* Create a room directory if necessary */
1489 if (CC->room.QRflags & QR_DIRECTORY) {
1490 snprintf(buf, sizeof buf,"%s/%s",
1492 CC->room.QRdirname);
1495 snprintf(buf, sizeof buf, "The room \"%s\" has been edited by %s.\n",
1496 CC->room.QRname, CC->curr_user);
1497 aide_message(buf, "Room modification Message");
1498 cprintf("%d Ok\n", CIT_OK);
1504 * get the name of the room aide for this room
1506 void cmd_geta(char *cmdbuf)
1508 struct ctdluser usbuf;
1510 if (CtdlAccessCheck(ac_logged_in)) return;
1512 if (getuserbynumber(&usbuf, CC->room.QRroomaide) == 0) {
1513 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1515 cprintf("%d \n", CIT_OK);
1521 * set the room aide for this room
1523 void cmd_seta(char *new_ra)
1525 struct ctdluser usbuf;
1530 if (CtdlAccessCheck(ac_room_aide)) return;
1532 if (getuser(&usbuf, new_ra) != 0) {
1535 newu = usbuf.usernum;
1538 lgetroom(&CC->room, CC->room.QRname);
1540 if (CC->room.QRroomaide != newu) {
1543 CC->room.QRroomaide = newu;
1544 lputroom(&CC->room);
1547 * We have to post the change notice _after_ writing changes to
1548 * the room table, otherwise it would deadlock!
1550 if (post_notice == 1) {
1551 if (!IsEmptyStr(usbuf.fullname))
1552 snprintf(buf, sizeof buf,
1553 "%s is now the room aide for \"%s\".\n",
1554 usbuf.fullname, CC->room.QRname);
1556 snprintf(buf, sizeof buf,
1557 "There is now no room aide for \"%s\".\n",
1559 aide_message(buf, "Aide Room Modification");
1561 cprintf("%d Ok\n", CIT_OK);
1565 * retrieve info file for this room
1567 void cmd_rinf(char *gargs)
1573 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_info_dir);
1574 info_fp = fopen(filename, "r");
1576 if (info_fp == NULL) {
1577 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
1580 cprintf("%d Info:\n", LISTING_FOLLOWS);
1581 while (fgets(buf, sizeof buf, info_fp) != NULL) {
1582 if (!IsEmptyStr(buf))
1583 buf[strlen(buf) - 1] = 0;
1584 cprintf("%s\n", buf);
1591 * Asynchronously schedule a room for deletion. The room will appear
1592 * deleted to the user(s), but it won't actually get purged from the
1593 * database until THE DREADED AUTO-PURGER makes its next run.
1595 void schedule_room_for_deletion(struct ctdlroom *qrbuf)
1597 char old_name[ROOMNAMELEN];
1600 CtdlLogPrintf(CTDL_NOTICE, "Scheduling room <%s> for deletion\n",
1603 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1605 getroom(qrbuf, qrbuf->QRname);
1607 /* Turn the room into a private mailbox owned by a user who doesn't
1608 * exist. This will immediately make the room invisible to everyone,
1609 * and qualify the room for purging.
1611 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1616 qrbuf->QRflags |= QR_MAILBOX;
1617 time(&qrbuf->QRgen); /* Use a timestamp as the new generation number */
1621 b_deleteroom(old_name);
1627 * Back end processing to delete a room and everything associated with it
1628 * (This one is synchronous and should only get called by THE DREADED
1629 * AUTO-PURGER in serv_expire.c. All user-facing code should call
1630 * the asynchronous schedule_room_for_deletion() instead.)
1632 void delete_room(struct ctdlroom *qrbuf)
1636 /* TODO: filename magic? does this realy work? */
1638 CtdlLogPrintf(CTDL_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1640 /* Delete the info file */
1641 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_info_dir);
1644 /* Delete the image file */
1645 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_image_dir);
1648 /* Delete the room's network config file */
1649 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1652 /* Delete the messages in the room
1653 * (Careful: this opens an S_ROOMS critical section!)
1655 CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
1657 /* Flag the room record as not in use */
1658 lgetroom(qrbuf, qrbuf->QRname);
1662 /* then decrement the reference count for the floor */
1663 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1664 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1665 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1667 /* Delete the room record from the database! */
1668 b_deleteroom(qrbuf->QRname);
1674 * Check access control for deleting a room
1676 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1678 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1682 if (is_noneditable(qr)) {
1687 * For mailboxes, check stuff
1689 if (qr->QRflags & QR_MAILBOX) {
1691 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1693 if (atol(qr->QRname) != CC->user.usernum) {
1694 return(0); /* not my room */
1697 /* Can't delete your Mail> room */
1698 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1700 /* Otherwise it's ok */
1705 * For normal rooms, just check for aide or room aide status.
1707 return(is_room_aide());
1711 * aide command: kill the current room
1713 void cmd_kill(char *argbuf)
1715 char deleted_room_name[ROOMNAMELEN];
1719 kill_ok = extract_int(argbuf, 0);
1721 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
1722 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1726 if (CC->room.QRflags & QR_MAILBOX) {
1727 safestrncpy(deleted_room_name, &CC->room.QRname[11], sizeof deleted_room_name);
1730 safestrncpy(deleted_room_name, CC->room.QRname, sizeof deleted_room_name);
1733 /* Do the dirty work */
1734 schedule_room_for_deletion(&CC->room);
1736 /* Return to the Lobby */
1737 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1739 /* tell the world what we did */
1740 snprintf(msg, sizeof msg, "The room \"%s\" has been deleted by %s.\n",
1741 deleted_room_name, CC->curr_user);
1742 aide_message(msg, "Room Purger Message");
1743 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1745 cprintf("%d ok to delete.\n", CIT_OK);
1751 * Internal code to create a new room (returns room flags)
1753 * Room types: 0=public, 1=guessname, 2=passworded, 3=inv-only,
1754 * 4=mailbox, 5=mailbox, but caller supplies namespace
1756 unsigned create_room(char *new_room_name,
1758 char *new_room_pass,
1765 struct ctdlroom qrbuf;
1769 CtdlLogPrintf(CTDL_DEBUG, "create_room(name=%s, type=%d, view=%d)\n",
1770 new_room_name, new_room_type, new_room_view);
1772 if (getroom(&qrbuf, new_room_name) == 0) {
1773 CtdlLogPrintf(CTDL_DEBUG, "%s already exists.\n", new_room_name);
1777 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1778 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1779 qrbuf.QRflags = QR_INUSE;
1780 if (new_room_type > 0)
1781 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1782 if (new_room_type == 1)
1783 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1784 if (new_room_type == 2)
1785 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1786 if ( (new_room_type == 4) || (new_room_type == 5) ) {
1787 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1788 /* qrbuf.QRflags2 |= QR2_SUBJECTREQ; */
1791 /* If the user is requesting a personal room, set up the room
1792 * name accordingly (prepend the user number)
1794 if (new_room_type == 4) {
1795 MailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1798 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1801 /* If the room is private, and the system administrator has elected
1802 * to automatically grant room aide privileges, do so now; otherwise,
1803 * set the room aide to undefined.
1805 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1806 qrbuf.QRroomaide = CC->user.usernum;
1808 qrbuf.QRroomaide = (-1L);
1812 * If the caller is only interested in testing whether this will work,
1813 * return now without creating the room.
1815 if (!really_create) return (qrbuf.QRflags);
1817 qrbuf.QRnumber = get_new_room_number();
1818 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1819 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1820 qrbuf.QRfloor = new_room_floor;
1821 qrbuf.QRdefaultview = new_room_view;
1823 /* save what we just did... */
1826 /* bump the reference count on whatever floor the room is on */
1827 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1828 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1829 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1831 /* Grant the creator access to the room unless the avoid_access
1832 * parameter was specified.
1834 if ( (CC->logged_in) && (avoid_access == 0) ) {
1835 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1836 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1837 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1838 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1841 /* resume our happy day */
1842 return (qrbuf.QRflags);
1849 void cmd_cre8(char *args)
1852 char new_room_name[ROOMNAMELEN];
1854 char new_room_pass[32];
1857 char *notification_message = NULL;
1860 int avoid_access = 0;
1862 cre8_ok = extract_int(args, 0);
1863 extract_token(new_room_name, args, 1, '|', sizeof new_room_name);
1864 new_room_name[ROOMNAMELEN - 1] = 0;
1865 new_room_type = extract_int(args, 2);
1866 extract_token(new_room_pass, args, 3, '|', sizeof new_room_pass);
1867 avoid_access = extract_int(args, 5);
1868 new_room_view = extract_int(args, 6);
1869 new_room_pass[9] = 0;
1872 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 1)) {
1873 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
1877 if (!strcasecmp(new_room_name, MAILROOM)) {
1878 cprintf("%d '%s' already exists.\n",
1879 ERROR + ALREADY_EXISTS, new_room_name);
1883 if (num_parms(args) >= 5) {
1884 fl = cgetfloor(extract_int(args, 4));
1886 cprintf("%d Invalid floor number.\n",
1887 ERROR + INVALID_FLOOR_OPERATION);
1890 else if ((fl->f_flags & F_INUSE) == 0) {
1891 cprintf("%d Invalid floor number.\n",
1892 ERROR + INVALID_FLOOR_OPERATION);
1895 new_room_floor = extract_int(args, 4);
1899 if (CtdlAccessCheck(ac_logged_in)) return;
1901 if (CC->user.axlevel < config.c_createax && !CC->internal_pgm) {
1902 cprintf("%d You need higher access to create rooms.\n",
1903 ERROR + HIGHER_ACCESS_REQUIRED);
1907 if ((IsEmptyStr(new_room_name)) && (cre8_ok == 0)) {
1908 cprintf("%d Ok to create rooms.\n", CIT_OK);
1912 if ((new_room_type < 0) || (new_room_type > 5)) {
1913 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
1917 if (new_room_type == 5) {
1918 if (CC->user.axlevel < 6) {
1919 cprintf("%d Higher access required\n",
1920 ERROR + HIGHER_ACCESS_REQUIRED);
1925 /* Check to make sure the requested room name doesn't already exist */
1926 newflags = create_room(new_room_name,
1927 new_room_type, new_room_pass, new_room_floor,
1928 0, avoid_access, new_room_view);
1929 if (newflags == 0) {
1930 cprintf("%d '%s' already exists.\n",
1931 ERROR + ALREADY_EXISTS, new_room_name);
1936 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
1940 /* If we reach this point, the room needs to be created. */
1942 newflags = create_room(new_room_name,
1943 new_room_type, new_room_pass, new_room_floor, 1, 0,
1946 /* post a message in Aide> describing the new room */
1947 notification_message = malloc(1024);
1948 snprintf(notification_message, 1024,
1949 "A new room called \"%s\" has been created by %s%s%s%s%s%s\n",
1952 ((newflags & QR_MAILBOX) ? " [personal]" : ""),
1953 ((newflags & QR_PRIVATE) ? " [private]" : ""),
1954 ((newflags & QR_GUESSNAME) ? " [hidden]" : ""),
1955 ((newflags & QR_PASSWORDED) ? " Password: " : ""),
1956 ((newflags & QR_PASSWORDED) ? new_room_pass : "")
1958 aide_message(notification_message, "Room Creation Message");
1959 free(notification_message);
1961 cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
1966 void cmd_einf(char *ok)
1967 { /* enter info file for current room */
1969 char infofilename[SIZ];
1974 if (CtdlAccessCheck(ac_room_aide)) return;
1976 if (atoi(ok) == 0) {
1977 cprintf("%d Ok.\n", CIT_OK);
1980 assoc_file_name(infofilename, sizeof infofilename, &CC->room, ctdl_info_dir);
1981 CtdlLogPrintf(CTDL_DEBUG, "opening\n");
1982 fp = fopen(infofilename, "w");
1983 CtdlLogPrintf(CTDL_DEBUG, "checking\n");
1985 cprintf("%d Cannot open %s: %s\n",
1986 ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1989 cprintf("%d Send info...\n", SEND_LISTING);
1992 client_getln(buf, sizeof buf);
1993 if (strcmp(buf, "000"))
1994 fprintf(fp, "%s\n", buf);
1995 } while (strcmp(buf, "000"));
1998 /* now update the room index so people will see our new info */
1999 lgetroom(&CC->room, CC->room.QRname); /* lock so no one steps on us */
2000 CC->room.QRinfo = CC->room.QRhighest + 1L;
2001 lputroom(&CC->room);
2006 * cmd_lflr() - List all known floors
2008 void cmd_lflr(char *gargs)
2013 if (CtdlAccessCheck(ac_logged_in)) return;
2015 cprintf("%d Known floors:\n", LISTING_FOLLOWS);
2017 for (a = 0; a < MAXFLOORS; ++a) {
2018 getfloor(&flbuf, a);
2019 if (flbuf.f_flags & F_INUSE) {
2020 cprintf("%d|%s|%d\n",
2032 * create a new floor
2034 void cmd_cflr(char *argbuf)
2036 char new_floor_name[256];
2039 int free_slot = (-1);
2042 extract_token(new_floor_name, argbuf, 0, '|', sizeof new_floor_name);
2043 cflr_ok = extract_int(argbuf, 1);
2045 if (CtdlAccessCheck(ac_aide)) return;
2047 if (IsEmptyStr(new_floor_name)) {
2048 cprintf("%d Blank floor name not allowed.\n",
2049 ERROR + ILLEGAL_VALUE);
2053 for (a = 0; a < MAXFLOORS; ++a) {
2054 getfloor(&flbuf, a);
2056 /* note any free slots while we're scanning... */
2057 if (((flbuf.f_flags & F_INUSE) == 0)
2061 /* check to see if it already exists */
2062 if ((!strcasecmp(flbuf.f_name, new_floor_name))
2063 && (flbuf.f_flags & F_INUSE)) {
2064 cprintf("%d Floor '%s' already exists.\n",
2065 ERROR + ALREADY_EXISTS,
2071 if (free_slot < 0) {
2072 cprintf("%d There is no space available for a new floor.\n",
2073 ERROR + INVALID_FLOOR_OPERATION);
2077 cprintf("%d ok to create...\n", CIT_OK);
2080 lgetfloor(&flbuf, free_slot);
2081 flbuf.f_flags = F_INUSE;
2082 flbuf.f_ref_count = 0;
2083 safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
2084 lputfloor(&flbuf, free_slot);
2085 cprintf("%d %d\n", CIT_OK, free_slot);
2093 void cmd_kflr(char *argbuf)
2096 int floor_to_delete;
2100 floor_to_delete = extract_int(argbuf, 0);
2101 kflr_ok = extract_int(argbuf, 1);
2103 if (CtdlAccessCheck(ac_aide)) return;
2105 lgetfloor(&flbuf, floor_to_delete);
2108 if ((flbuf.f_flags & F_INUSE) == 0) {
2109 cprintf("%d Floor %d not in use.\n",
2110 ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
2113 if (flbuf.f_ref_count != 0) {
2114 cprintf("%d Cannot delete; floor contains %d rooms.\n",
2115 ERROR + INVALID_FLOOR_OPERATION,
2120 cprintf("%d Ok\n", CIT_OK);
2122 cprintf("%d Ok to delete...\n", CIT_OK);
2129 if ((delete_ok == 1) && (kflr_ok == 1))
2131 lputfloor(&flbuf, floor_to_delete);
2137 void cmd_eflr(char *argbuf)
2143 np = num_parms(argbuf);
2145 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
2149 if (CtdlAccessCheck(ac_aide)) return;
2151 floor_num = extract_int(argbuf, 0);
2152 lgetfloor(&flbuf, floor_num);
2153 if ((flbuf.f_flags & F_INUSE) == 0) {
2154 lputfloor(&flbuf, floor_num);
2155 cprintf("%d Floor %d is not in use.\n",
2156 ERROR + INVALID_FLOOR_OPERATION, floor_num);
2160 extract_token(flbuf.f_name, argbuf, 1, '|', sizeof flbuf.f_name);
2161 lputfloor(&flbuf, floor_num);
2163 cprintf("%d Ok\n", CIT_OK);
2167 /*****************************************************************************/
2168 /* MODULE INITIALIZATION STUFF */
2169 /*****************************************************************************/
2171 CTDL_MODULE_INIT(room_ops)
2173 CtdlRegisterProtoHook(cmd_lrms, "LRMS", "Autoconverted. TODO: document me.");
2174 CtdlRegisterProtoHook(cmd_lkra, "LKRA", "Autoconverted. TODO: document me.");
2175 CtdlRegisterProtoHook(cmd_lkrn, "LKRN", "Autoconverted. TODO: document me.");
2176 CtdlRegisterProtoHook(cmd_lkro, "LKRO", "Autoconverted. TODO: document me.");
2177 CtdlRegisterProtoHook(cmd_lzrm, "LZRM", "Autoconverted. TODO: document me.");
2178 CtdlRegisterProtoHook(cmd_lprm, "LPRM", "Autoconverted. TODO: document me.");
2179 CtdlRegisterProtoHook(cmd_goto, "GOTO", "Autoconverted. TODO: document me.");
2180 CtdlRegisterProtoHook(cmd_whok, "WHOK", "Autoconverted. TODO: document me.");
2181 CtdlRegisterProtoHook(cmd_rdir, "RDIR", "Autoconverted. TODO: document me.");
2182 CtdlRegisterProtoHook(cmd_getr, "GETR", "Autoconverted. TODO: document me.");
2183 CtdlRegisterProtoHook(cmd_setr, "SETR", "Autoconverted. TODO: document me.");
2184 CtdlRegisterProtoHook(cmd_geta, "GETA", "Autoconverted. TODO: document me.");
2185 CtdlRegisterProtoHook(cmd_seta, "SETA", "Autoconverted. TODO: document me.");
2186 CtdlRegisterProtoHook(cmd_rinf, "RINF", "Autoconverted. TODO: document me.");
2187 CtdlRegisterProtoHook(cmd_kill, "KILL", "Autoconverted. TODO: document me.");
2188 CtdlRegisterProtoHook(cmd_cre8, "CRE8", "Autoconverted. TODO: document me.");
2189 CtdlRegisterProtoHook(cmd_einf, "EINF", "Autoconverted. TODO: document me.");
2190 CtdlRegisterProtoHook(cmd_lflr, "LFLR", "Autoconverted. TODO: document me.");
2191 CtdlRegisterProtoHook(cmd_cflr, "CFLR", "Autoconverted. TODO: document me.");
2192 CtdlRegisterProtoHook(cmd_kflr, "KFLR", "Autoconverted. TODO: document me.");
2193 CtdlRegisterProtoHook(cmd_eflr, "EFLR", "Autoconverted. TODO: document me.");
2194 /* return our Subversion id for the Log */