2 * Server functions which perform operations on room objects.
4 * Copyright (c) 1987-2021 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include <libcitadel.h>
18 #include "citserver.h"
19 #include "ctdl_module.h"
25 struct floor *floorcache[MAXFLOORS];
28 * Determine whether the currently logged in session has permission to read
29 * messages in the current room.
31 int CtdlDoIHavePermissionToReadMessagesInThisRoom(void) {
32 if ( (!(CC->logged_in))
33 && (!(CC->internal_pgm))
34 && (!CtdlGetConfigInt("c_guest_logins"))
36 return(om_not_logged_in);
43 * Check to see whether we have permission to post a message in the current
44 * room. Returns a *CITADEL ERROR CODE* and puts a message in errmsgbuf, or
45 * returns 0 on success.
47 int CtdlDoIHavePermissionToPostInThisRoom(
50 const char* RemoteIdentifier,
56 if (!(CC->logged_in) && (PostPublic == POST_LOGGED_IN)) {
57 snprintf(errmsgbuf, n, "Not logged in.");
58 return (ERROR + NOT_LOGGED_IN);
60 else if (PostPublic == CHECK_EXIST) {
61 return (0); // evaluate whether a recipient exists
63 else if (!(CC->logged_in)) {
64 if ((CC->room.QRflags & QR_READONLY)) {
65 snprintf(errmsgbuf, n, "Not logged in.");
66 return (ERROR + NOT_LOGGED_IN);
71 if ((CC->user.axlevel < AxProbU) && ((CC->room.QRflags & QR_MAILBOX) == 0)) {
72 snprintf(errmsgbuf, n, "Need to be validated to enter (except in %s> to sysop)", MAILROOM);
73 return (ERROR + HIGHER_ACCESS_REQUIRED);
76 CtdlRoomAccess(&CC->room, &CC->user, &ra, NULL);
78 if (ra & UA_POSTALLOWED) {
79 strcpy(errmsgbuf, "OK to post or reply here");
83 if ( (ra & UA_REPLYALLOWED) && (is_reply) ) {
85 * To be thorough, we ought to check to see if the message they are
86 * replying to is actually a valid one in this room, but unless this
87 * actually becomes a problem we'll go with high performance instead.
89 strcpy(errmsgbuf, "OK to reply here");
93 if ( (ra & UA_REPLYALLOWED) && (!is_reply) ) {
94 /* Clarify what happened with a better error message */
95 snprintf(errmsgbuf, n, "You may only reply to existing messages here.");
96 return (ERROR + HIGHER_ACCESS_REQUIRED);
99 snprintf(errmsgbuf, n, "Higher access is required to post in this room.");
100 return (ERROR + HIGHER_ACCESS_REQUIRED);
106 * Check whether the current user has permission to delete messages from
107 * the current room (returns 1 for yes, 0 for no)
109 int CtdlDoIHavePermissionToDeleteMessagesFromThisRoom(void) {
111 CtdlRoomAccess(&CC->room, &CC->user, &ra, NULL);
112 if (ra & UA_DELETEALLOWED) return(1);
118 * Retrieve access control information for any user/room pair.
119 * Yes, it has a couple of gotos. If you don't like that, go die in a car fire.
121 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf, int *result, int *view) {
127 if (userbuf == &CC->user) {
131 if ((is_me) && (CtdlGetConfigInt("c_guest_logins")) && (!CC->logged_in)) {
135 /* for internal programs, always do everything */
136 if (((CC->internal_pgm)) && (roombuf->QRflags & QR_INUSE)) {
137 retval = (UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED);
139 goto SKIP_EVERYTHING;
142 /* If guest mode is enabled, always grant access to the Lobby */
143 if ((is_guest) && (!strcasecmp(roombuf->QRname, BASEROOM))) {
144 retval = (UA_KNOWN | UA_GOTOALLOWED);
146 goto SKIP_EVERYTHING;
149 /* Locate any applicable user/room relationships */
151 memset(&vbuf, 0, sizeof vbuf);
154 CtdlGetRelationship(&vbuf, userbuf, roombuf);
157 /* Force the properties of the Aide room */
158 if (!strcasecmp(roombuf->QRname, CtdlGetConfigStr("c_aideroom"))) {
159 if (userbuf->axlevel >= AxAideU) {
160 retval = UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED;
167 /* If this is a public room, it's accessible... */
168 if ( ((roombuf->QRflags & QR_PRIVATE) == 0)
169 && ((roombuf->QRflags & QR_MAILBOX) == 0)
171 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
174 /* If this is a preferred users only room, check access level */
175 if (roombuf->QRflags & QR_PREFONLY) {
176 if (userbuf->axlevel < AxPrefU) {
177 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
181 /* For private rooms, check the generation number matchups */
182 if ( (roombuf->QRflags & QR_PRIVATE)
183 && ((roombuf->QRflags & QR_MAILBOX) == 0)
186 /* An explicit match means the user belongs in this room */
187 if (vbuf.v_flags & V_ACCESS) {
188 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
190 /* Otherwise, check if this is a guess-name or passworded
191 * room. If it is, a goto may at least be attempted
193 else if ( (roombuf->QRflags & QR_PRIVATE)
194 || (roombuf->QRflags & QR_PASSWORDED)
196 retval = retval & ~UA_KNOWN;
197 retval = retval | UA_GOTOALLOWED;
201 /* For mailbox rooms, also check the namespace */
202 /* Also, mailbox owners can delete their messages */
203 if ( (roombuf->QRflags & QR_MAILBOX) && (atol(roombuf->QRname) != 0)) {
204 if (userbuf->usernum == atol(roombuf->QRname)) {
205 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED;
207 /* An explicit match means the user belongs in this room */
208 if (vbuf.v_flags & V_ACCESS) {
209 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED;
213 /* For non-mailbox rooms... */
216 /* User is allowed to post in the room unless:
217 * - User is not validated
218 * - User has no net privileges and it is a shared network room
219 * - It is a read-only room
220 * - It is a blog room (in which case we only allow replies to existing messages)
222 int post_allowed = 1;
223 int reply_allowed = 1;
224 if (userbuf->axlevel < AxProbU) {
228 if ((userbuf->axlevel < AxNetU) && (roombuf->QRflags & QR_NETWORK)) {
232 if (roombuf->QRflags & QR_READONLY) {
236 if (roombuf->QRdefaultview == VIEW_BLOG) {
240 retval = retval | UA_POSTALLOWED | UA_REPLYALLOWED;
243 retval = retval | UA_REPLYALLOWED;
246 /* If "collaborative deletion" is active for this room, any user who can post
247 * is also allowed to delete
249 if (roombuf->QRflags2 & QR2_COLLABDEL) {
250 if (retval & UA_POSTALLOWED) {
251 retval = retval | UA_DELETEALLOWED;
257 /* Check to see if the user has forgotten this room */
258 if (vbuf.v_flags & V_FORGET) {
259 retval = retval & ~UA_KNOWN;
260 if ( ( ((roombuf->QRflags & QR_PRIVATE) == 0)
261 && ((roombuf->QRflags & QR_MAILBOX) == 0)
262 ) || ( (roombuf->QRflags & QR_MAILBOX)
263 && (atol(roombuf->QRname) == CC->user.usernum))
265 retval = retval | UA_ZAPPED;
269 /* If user is explicitly locked out of this room, deny everything */
270 if (vbuf.v_flags & V_LOCKOUT) {
271 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED & ~UA_POSTALLOWED & ~UA_REPLYALLOWED;
274 /* Aides get access to all private rooms */
275 if ( (userbuf->axlevel >= AxAideU)
276 && ((roombuf->QRflags & QR_MAILBOX) == 0)
278 if (vbuf.v_flags & V_FORGET) {
279 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
282 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
286 /* Aides can gain access to mailboxes as well, but they don't show
289 if ( (userbuf->axlevel >= AxAideU)
290 && (roombuf->QRflags & QR_MAILBOX)
292 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
295 /* Aides and Room Aides have admin privileges */
296 if ( (userbuf->axlevel >= AxAideU)
297 || (userbuf->usernum == roombuf->QRroomaide)
299 retval = retval | UA_ADMINALLOWED | UA_DELETEALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
302 NEWMSG: /* By the way, we also check for the presence of new messages */
303 if (is_msg_in_sequence_set(vbuf.v_seen, roombuf->QRhighest) == 0) {
304 retval = retval | UA_HASNEWMSGS;
307 /* System rooms never show up in the list. */
308 if (roombuf->QRflags2 & QR2_SYSTEM) {
309 retval = retval & ~UA_KNOWN;
313 /* Now give the caller the information it wants. */
314 if (result != NULL) *result = retval;
315 if (view != NULL) *view = vbuf.v_view;
320 * Self-checking stuff for a room record read into memory
322 void room_sanity_check(struct ctdlroom *qrbuf) {
323 /* Mailbox rooms are always on the lowest floor */
324 if (qrbuf->QRflags & QR_MAILBOX) {
327 /* Listing order of 0 is illegal except for base rooms */
328 if (qrbuf->QRorder == 0) {
329 if ( !(qrbuf->QRflags & QR_MAILBOX)
330 && strncasecmp(qrbuf->QRname, CtdlGetConfigStr("c_baseroom"), ROOMNAMELEN)
331 && strncasecmp(qrbuf->QRname, CtdlGetConfigStr("c_aideroom"), ROOMNAMELEN)
340 * CtdlGetRoom() - retrieve room data from disk
342 int CtdlGetRoom(struct ctdlroom *qrbuf, const char *room_name) {
343 struct cdbdata *cdbqr;
344 char lowercase_name[ROOMNAMELEN];
345 char personal_lowercase_name[ROOMNAMELEN];
349 dptr = lowercase_name;
351 eptr = (dptr + (sizeof lowercase_name - 1));
352 while (!IsEmptyStr(sptr) && (dptr < eptr)) {
353 *dptr = tolower(*sptr);
358 memset(qrbuf, 0, sizeof(struct ctdlroom));
360 if (IsEmptyStr(lowercase_name)) {
361 return(1); // empty room name , not valid
364 /* First, try the public namespace */
365 cdbqr = cdb_fetch(CDB_ROOMS, lowercase_name, strlen(lowercase_name));
367 /* If that didn't work, try the user's personal namespace */
369 snprintf(personal_lowercase_name, sizeof personal_lowercase_name, "%010ld.%s", CC->user.usernum, lowercase_name);
370 cdbqr = cdb_fetch(CDB_ROOMS, personal_lowercase_name, strlen(personal_lowercase_name));
373 memcpy(qrbuf, cdbqr->ptr, ((cdbqr->len > sizeof(struct ctdlroom)) ? sizeof(struct ctdlroom) : cdbqr->len));
375 room_sanity_check(qrbuf);
385 * CtdlGetRoomLock() - same as getroom() but locks the record (if supported)
387 int CtdlGetRoomLock(struct ctdlroom *qrbuf, const char *room_name) {
389 retval = CtdlGetRoom(qrbuf, room_name);
390 if (retval == 0) begin_critical_section(S_ROOMS);
396 * b_putroom() - back end to putroom() and b_deleteroom()
397 * (if the supplied buffer is NULL, delete the room record)
399 void b_putroom(struct ctdlroom *qrbuf, char *room_name)
401 char lowercase_name[ROOMNAMELEN];
406 bptr = lowercase_name;
407 while (!IsEmptyStr(aptr)) {
408 *bptr = tolower(*aptr);
414 len = bptr - lowercase_name;
416 cdb_delete(CDB_ROOMS, lowercase_name, len);
419 time(&qrbuf->QRmtime);
420 cdb_store(CDB_ROOMS, lowercase_name, len, qrbuf, sizeof(struct ctdlroom));
426 * CtdlPutRoom() - store room data to disk
428 void CtdlPutRoom(struct ctdlroom *qrbuf) {
429 b_putroom(qrbuf, qrbuf->QRname);
434 * b_deleteroom() - delete a room record from disk
436 void b_deleteroom(char *room_name) {
437 b_putroom(NULL, room_name);
442 * CtdlPutRoomLock() - same as CtdlPutRoom() but unlocks the record (if supported)
444 void CtdlPutRoomLock(struct ctdlroom *qrbuf) {
446 end_critical_section(S_ROOMS);
451 * CtdlGetFloorByName() - retrieve the number of the named floor
452 * return < 0 if not found else return floor number
454 int CtdlGetFloorByName(const char *floor_name) {
456 struct floor *flbuf = NULL;
458 for (a = 0; a < MAXFLOORS; ++a) {
459 flbuf = CtdlGetCachedFloor(a);
461 /* check to see if it already exists */
462 if ((!strcasecmp(flbuf->f_name, floor_name)) && (flbuf->f_flags & F_INUSE)) {
471 * CtdlGetFloorByNameLock() - retrieve floor number for given floor and lock the floor list.
473 int CtdlGetFloorByNameLock(const char *floor_name)
475 begin_critical_section(S_FLOORTAB);
476 return CtdlGetFloorByName(floor_name);
481 * CtdlGetAvailableFloor() - Return number of first unused floor
482 * return < 0 if none available
484 int CtdlGetAvailableFloor(void) {
486 struct floor *flbuf = NULL;
488 for (a = 0; a < MAXFLOORS; a++) {
489 flbuf = CtdlGetCachedFloor(a);
491 /* check to see if it already exists */
492 if ((flbuf->f_flags & F_INUSE) == 0) {
501 * CtdlGetFloor() - retrieve floor data from disk
503 void CtdlGetFloor(struct floor *flbuf, int floor_num) {
504 struct cdbdata *cdbfl;
506 memset(flbuf, 0, sizeof(struct floor));
507 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
509 memcpy(flbuf, cdbfl->ptr, ((cdbfl->len > sizeof(struct floor)) ? sizeof(struct floor) : cdbfl->len));
512 if (floor_num == 0) {
513 safestrncpy(flbuf->f_name, "Main Floor", sizeof flbuf->f_name);
514 flbuf->f_flags = F_INUSE;
515 flbuf->f_ref_count = 3;
522 * lgetfloor() - same as CtdlGetFloor() but locks the record (if supported)
524 void lgetfloor(struct floor *flbuf, int floor_num) {
525 begin_critical_section(S_FLOORTAB);
526 CtdlGetFloor(flbuf, floor_num);
531 * CtdlGetCachedFloor() - Get floor record from *cache* (loads from disk if needed)
533 * This is strictly a performance hack.
535 struct floor *CtdlGetCachedFloor(int floor_num) {
536 static int initialized = 0;
539 struct floor *fl = NULL;
541 begin_critical_section(S_FLOORCACHE);
542 if (initialized == 0) {
543 for (i=0; i<MAXFLOORS; ++i) {
544 floorcache[floor_num] = NULL;
548 if (floorcache[floor_num] == NULL) {
551 end_critical_section(S_FLOORCACHE);
554 fl = malloc(sizeof(struct floor));
555 CtdlGetFloor(fl, floor_num);
556 begin_critical_section(S_FLOORCACHE);
557 if (floorcache[floor_num] != NULL) {
558 free(floorcache[floor_num]);
560 floorcache[floor_num] = fl;
561 end_critical_section(S_FLOORCACHE);
564 return(floorcache[floor_num]);
569 * CtdlPutFloor() - store floor data on disk
571 void CtdlPutFloor(struct floor *flbuf, int floor_num) {
572 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
573 begin_critical_section(S_FLOORCACHE);
574 if (floorcache[floor_num] != NULL) {
575 free(floorcache[floor_num]);
576 floorcache[floor_num] = malloc(sizeof(struct floor));
577 memcpy(floorcache[floor_num], flbuf, sizeof(struct floor));
579 end_critical_section(S_FLOORCACHE);
581 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
582 flbuf, sizeof(struct floor));
587 * CtdlPutFloorLock() - same as CtdlPutFloor() but unlocks the record (if supported)
589 void CtdlPutFloorLock(struct floor *flbuf, int floor_num) {
590 CtdlPutFloor(flbuf, floor_num);
591 end_critical_section(S_FLOORTAB);
597 * lputfloor() - same as CtdlPutFloor() but unlocks the record (if supported)
599 void lputfloor(struct floor *flbuf, int floor_num) {
600 CtdlPutFloorLock(flbuf, floor_num);
604 * Iterate through the room table, performing a callback for each room.
606 void CtdlForEachRoom(ForEachRoomCallBack callback_func, void *in_data) {
607 struct ctdlroom qrbuf;
608 struct cdbdata *cdbqr;
610 cdb_rewind(CDB_ROOMS);
612 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
613 memset(&qrbuf, 0, sizeof(struct ctdlroom));
614 memcpy(&qrbuf, cdbqr->ptr, ((cdbqr->len > sizeof(struct ctdlroom)) ? sizeof(struct ctdlroom) : cdbqr->len) );
616 room_sanity_check(&qrbuf);
617 if (qrbuf.QRflags & QR_INUSE) {
618 callback_func(&qrbuf, in_data);
625 * delete_msglist() - delete room message pointers
627 void delete_msglist(struct ctdlroom *whichroom) {
628 struct cdbdata *cdbml;
630 /* Make sure the msglist we're deleting actually exists, otherwise
631 * libdb will complain when we try to delete an invalid record
633 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
637 /* Go ahead and delete it */
638 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
644 * Message pointer compare function for sort_msglist()
646 int sort_msglist_cmp(const void *m1, const void *m2) {
647 if ((*(const long *)m1) > (*(const long *)m2)) return(1);
648 if ((*(const long *)m1) < (*(const long *)m2)) return(-1);
654 * sort message pointers
655 * (returns new msg count)
657 int sort_msglist(long listptrs[], int oldcount) {
667 qsort(listptrs, numitems, sizeof(long), sort_msglist_cmp);
669 /* and yank any nulls */
670 while ((i < numitems) && (listptrs[i] == 0L)) i++;
673 memmove(&listptrs[0], &listptrs[i], (sizeof(long) * (numitems - i)));
682 * Determine whether a given room is non-editable.
684 int CtdlIsNonEditable(struct ctdlroom *qrbuf) {
686 /* Mail> rooms are non-editable */
687 if ( (qrbuf->QRflags & QR_MAILBOX) && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) ) {
691 /* Everything else is editable */
698 * Make the specified room the current room for this session. No validation
699 * or access control is done here -- the caller should make sure that the
700 * specified room exists and is ok to access.
702 void CtdlUserGoto(char *where, int display_result, int transiently,
703 int *retmsgs, int *retnew, long *retoldest, long *retnewest)
706 int new_messages = 0;
707 int old_messages = 0;
708 int total_messages = 0;
709 long oldest_message = 0;
710 long newest_message = 0;
714 int newmailcount = 0;
716 char truncated_roomname[ROOMNAMELEN];
717 struct cdbdata *cdbfr;
718 long *msglist = NULL;
720 unsigned int original_v_flags;
723 char setstr[128], lostr[64], histr[64];
727 /* If the supplied room name is NULL, the caller wants us to know that
728 * it has already copied the room record into CC->room, so
729 * we can skip the extra database fetch.
732 safestrncpy(CC->room.QRname, where, sizeof CC->room.QRname);
733 CtdlGetRoom(&CC->room, where);
736 /* Take care of all the formalities. */
738 begin_critical_section(S_USERS);
739 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
740 original_v_flags = vbuf.v_flags;
742 /* Know the room ... but not if it's the page log room, or if the
743 * caller specified that we're only entering this room transiently.
745 int add_room_to_known_list = 1;
746 if (transiently == 1) {
747 add_room_to_known_list = 0;
749 char *c_logpages = CtdlGetConfigStr("c_logpages");
750 if ( (c_logpages != NULL) && (!strcasecmp(CC->room.QRname, c_logpages)) ) {
751 add_room_to_known_list = 0;
753 if (add_room_to_known_list) {
754 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
755 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
758 /* Only rewrite the database record if we changed something */
759 if (vbuf.v_flags != original_v_flags) {
760 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
762 end_critical_section(S_USERS);
764 /* Check for new mail */
765 newmailcount = NewMailCount();
767 /* Set info to 1 if the room banner is new since our last visit.
768 * Some clients only want to display it when it changes.
770 if (CC->room.msgnum_info > vbuf.v_lastseen) {
774 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
776 msglist = (long *) cdbfr->ptr;
777 cdbfr->ptr = NULL; /* CtdlUserGoto() now owns this memory */
778 num_msgs = cdbfr->len / sizeof(long);
783 for (a=0; a<num_msgs; ++a) {
784 if (msglist[a] > 0L) ++total_messages;
787 if (total_messages > 0) {
788 oldest_message = msglist[0];
789 newest_message = msglist[num_msgs - 1];
792 num_sets = num_tokens(vbuf.v_seen, ',');
793 for (s=0; s<num_sets; ++s) {
794 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
796 extract_token(lostr, setstr, 0, ':', sizeof lostr);
797 if (num_tokens(setstr, ':') >= 2) {
798 extract_token(histr, setstr, 1, ':', sizeof histr);
799 if (!strcmp(histr, "*")) {
800 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
804 strcpy(histr, lostr);
809 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
810 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
816 new_messages = total_messages - old_messages;
818 if (msglist != NULL) free(msglist);
820 if (CC->room.QRflags & QR_MAILBOX)
825 if ((CC->room.QRroomaide == CC->user.usernum) || (CC->user.axlevel >= AxAideU))
830 safestrncpy(truncated_roomname, CC->room.QRname, sizeof truncated_roomname);
831 if ( (CC->room.QRflags & QR_MAILBOX) && (atol(CC->room.QRname) == CC->user.usernum) ) {
832 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
835 if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
839 if (retmsgs != NULL) *retmsgs = total_messages;
840 if (retnew != NULL) *retnew = new_messages;
841 if (retoldest != NULL) *retoldest = oldest_message;
842 if (retnewest != NULL) *retnewest = newest_message;
843 syslog(LOG_DEBUG, "room_ops: %s : %d new of %d total messages, oldest=%ld, newest=%ld",
844 CC->room.QRname, new_messages, total_messages, oldest_message, newest_message
847 CC->curr_view = (int)vbuf.v_view;
849 if (display_result) {
850 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|%d|%ld|\n",
851 CIT_OK, CtdlCheckExpress(),
856 (int)CC->room.QRflags,
857 (long)CC->room.QRhighest,
858 (long)vbuf.v_lastseen,
862 (int)CC->room.QRfloor,
864 (int)CC->room.QRdefaultview,
866 (int)CC->room.QRflags2,
867 (long)CC->room.QRmtime
874 * Handle some of the macro named rooms
876 void convert_room_name_macros(char *towhere, size_t maxlen) {
877 if (!strcasecmp(towhere, "_BASEROOM_")) {
878 safestrncpy(towhere, CtdlGetConfigStr("c_baseroom"), maxlen);
880 else if (!strcasecmp(towhere, "_MAIL_")) {
881 safestrncpy(towhere, MAILROOM, maxlen);
883 else if (!strcasecmp(towhere, "_TRASH_")) {
884 safestrncpy(towhere, USERTRASHROOM, maxlen);
886 else if (!strcasecmp(towhere, "_DRAFTS_")) {
887 safestrncpy(towhere, USERDRAFTROOM, maxlen);
889 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
890 safestrncpy(towhere, CtdlGetConfigStr("c_twitroom"), maxlen);
892 else if (!strcasecmp(towhere, "_CALENDAR_")) {
893 safestrncpy(towhere, USERCALENDARROOM, maxlen);
895 else if (!strcasecmp(towhere, "_TASKS_")) {
896 safestrncpy(towhere, USERTASKSROOM, maxlen);
898 else if (!strcasecmp(towhere, "_CONTACTS_")) {
899 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
901 else if (!strcasecmp(towhere, "_NOTES_")) {
902 safestrncpy(towhere, USERNOTESROOM, maxlen);
908 * Back end function to rename a room.
909 * You can also specify which floor to move the room to, or specify -1 to
910 * keep the room on the same floor it was on.
912 * If you are renaming a mailbox room, you must supply the namespace prefix
913 * in *at least* the old name!
915 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
917 struct ctdlroom qrbuf;
918 struct ctdlroom qrtmp;
923 char actual_old_name[ROOMNAMELEN];
925 syslog(LOG_DEBUG, "room_ops: CtdlRenameRoom(%s, %s, %d)", old_name, new_name, new_floor);
927 if (new_floor >= 0) {
928 fl = CtdlGetCachedFloor(new_floor);
929 if ((fl->f_flags & F_INUSE) == 0) {
930 return(crr_invalid_floor);
934 begin_critical_section(S_ROOMS);
936 if ( (CtdlGetRoom(&qrtmp, new_name) == 0) && (strcasecmp(new_name, old_name)) ) {
937 ret = crr_already_exists;
940 else if (CtdlGetRoom(&qrbuf, old_name) != 0) {
941 ret = crr_room_not_found;
944 else if ( (CC->user.axlevel < AxAideU) && (!CC->internal_pgm)
945 && (CC->user.usernum != qrbuf.QRroomaide)
946 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) ) {
947 ret = crr_access_denied;
950 else if (CtdlIsNonEditable(&qrbuf)) {
951 ret = crr_noneditable;
956 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
957 if (qrbuf.QRflags & QR_MAILBOX) {
958 owner = atol(qrbuf.QRname);
960 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
961 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
962 "%010ld.%s", owner, new_name);
965 safestrncpy(qrbuf.QRname, new_name,
966 sizeof(qrbuf.QRname));
969 /* Reject change of floor for baseroom/aideroom */
970 if (!strncasecmp(old_name, CtdlGetConfigStr("c_baseroom"), ROOMNAMELEN) ||
971 !strncasecmp(old_name, CtdlGetConfigStr("c_aideroom"), ROOMNAMELEN))
976 /* Take care of floor stuff */
977 old_floor = qrbuf.QRfloor;
979 new_floor = old_floor;
981 qrbuf.QRfloor = new_floor;
984 begin_critical_section(S_CONFIG);
986 /* If baseroom/aideroom name changes, update config */
987 if (!strncasecmp(old_name, CtdlGetConfigStr("c_baseroom"), ROOMNAMELEN)) {
988 CtdlSetConfigStr("c_baseroom", new_name);
990 if (!strncasecmp(old_name, CtdlGetConfigStr("c_aideroom"), ROOMNAMELEN)) {
991 CtdlSetConfigStr("c_aideroom", new_name);
994 end_critical_section(S_CONFIG);
996 /* If the room name changed, then there are now two room
997 * records, so we have to delete the old one.
999 if (strcasecmp(new_name, old_name)) {
1000 b_deleteroom(actual_old_name);
1006 end_critical_section(S_ROOMS);
1008 /* Adjust the floor reference counts if necessary */
1009 if (new_floor != old_floor) {
1010 lgetfloor(&flbuf, old_floor);
1011 --flbuf.f_ref_count;
1012 lputfloor(&flbuf, old_floor);
1013 syslog(LOG_DEBUG, "room_ops: reference count for floor %d is now %d", old_floor, flbuf.f_ref_count);
1014 lgetfloor(&flbuf, new_floor);
1015 ++flbuf.f_ref_count;
1016 lputfloor(&flbuf, new_floor);
1017 syslog(LOG_DEBUG, "room_ops: reference count for floor %d is now %d", new_floor, flbuf.f_ref_count);
1020 /* ...and everybody say "YATTA!" */
1026 * Asynchronously schedule a room for deletion. By placing the room into an invalid private namespace,
1027 * the room will appear deleted to the user(s), but the session doesn't need to block while waiting for
1028 * database operations to complete. Instead, the room gets purged when THE DREADED AUTO-PURGER makes
1029 * its next run. Aren't we so clever?!!
1031 void CtdlScheduleRoomForDeletion(struct ctdlroom *qrbuf) {
1032 char old_name[ROOMNAMELEN];
1035 syslog(LOG_NOTICE, "room_ops: scheduling room <%s> for deletion", qrbuf->QRname);
1037 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1038 CtdlGetRoom(qrbuf, qrbuf->QRname);
1040 /* Turn the room into a private mailbox owned by a user who doesn't
1041 * exist. This will immediately make the room invisible to everyone,
1042 * and qualify the room for purging.
1044 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1049 qrbuf->QRflags |= QR_MAILBOX;
1050 time(&qrbuf->QRgen); /* Use a timestamp as the new generation number */
1052 b_deleteroom(old_name);
1057 * Back end processing to delete a room and everything associated with it
1058 * (This one is synchronous and should only get called by THE DREADED
1059 * AUTO-PURGER in serv_expire.c. All user-facing code should call
1060 * the asynchronous schedule_room_for_deletion() instead.)
1062 void CtdlDeleteRoom(struct ctdlroom *qrbuf) {
1064 char configdbkeyname[25];
1066 syslog(LOG_NOTICE, "room_ops: deleting room <%s>", qrbuf->QRname);
1068 /* Delete the room's network configdb entry */
1069 netcfg_keyname(configdbkeyname, qrbuf->QRnumber);
1070 CtdlDelConfig(configdbkeyname);
1072 /* Delete the messages in the room
1073 * (Careful: this opens an S_ROOMS critical section!)
1075 CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
1077 /* Flag the room record as not in use */
1078 CtdlGetRoomLock(qrbuf, qrbuf->QRname);
1080 CtdlPutRoomLock(qrbuf);
1082 /* then decrement the reference count for the floor */
1083 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1084 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1085 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1087 /* Delete the room record from the database! */
1088 b_deleteroom(qrbuf->QRname);
1093 * Check access control for deleting a room
1095 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1097 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1101 if (CtdlIsNonEditable(qr)) {
1106 * For mailboxes, check stuff
1108 if (qr->QRflags & QR_MAILBOX) {
1110 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1112 if (atol(qr->QRname) != CC->user.usernum) {
1113 return(0); /* not my room */
1116 /* Can't delete your Mail> room */
1117 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1119 /* Otherwise it's ok */
1124 * For normal rooms, just check for admin or room admin status.
1126 return(is_room_aide());
1131 * Internal code to create a new room (returns room flags)
1133 * Room types: 0=public, 1=hidden, 2=passworded, 3=invitation-only,
1134 * 4=mailbox, 5=mailbox, but caller supplies namespace
1136 unsigned CtdlCreateRoom(char *new_room_name,
1138 char *new_room_pass,
1144 struct ctdlroom qrbuf;
1148 syslog(LOG_DEBUG, "room_ops: CtdlCreateRoom(name=%s, type=%d, view=%d)", new_room_name, new_room_type, new_room_view);
1150 if (CtdlGetRoom(&qrbuf, new_room_name) == 0) {
1151 syslog(LOG_DEBUG, "room_ops: cannot create room <%s> - already exists", new_room_name);
1155 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1156 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1157 qrbuf.QRflags = QR_INUSE;
1158 if (new_room_type > 0)
1159 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1160 if (new_room_type == 1)
1161 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1162 if (new_room_type == 2)
1163 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1164 if ( (new_room_type == 4) || (new_room_type == 5) ) {
1165 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1166 /* qrbuf.QRflags2 |= QR2_SUBJECTREQ; */
1169 /* If the user is requesting a personal room, set up the room
1170 * name accordingly (prepend the user number)
1172 if (new_room_type == 4) {
1173 CtdlMailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1176 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1179 /* If the room is private, and the system administrator has elected
1180 * to automatically grant room admin privileges, do so now.
1182 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1183 qrbuf.QRroomaide = CC->user.usernum;
1185 /* Blog owners automatically become room admins of their blogs.
1186 * (In the future we will offer a site-wide configuration setting to suppress this behavior.)
1188 else if (new_room_view == VIEW_BLOG) {
1189 qrbuf.QRroomaide = CC->user.usernum;
1191 /* Otherwise, set the room admin to undefined.
1194 qrbuf.QRroomaide = (-1L);
1198 * If the caller is only interested in testing whether this will work,
1199 * return now without creating the room.
1201 if (!really_create) return (qrbuf.QRflags);
1203 qrbuf.QRnumber = get_new_room_number();
1204 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1205 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1206 qrbuf.QRfloor = new_room_floor;
1207 qrbuf.QRdefaultview = new_room_view;
1209 /* save what we just did... */
1210 CtdlPutRoom(&qrbuf);
1212 /* bump the reference count on whatever floor the room is on */
1213 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1214 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1215 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1217 /* Grant the creator access to the room unless the avoid_access
1218 * parameter was specified.
1220 if ( (CC->logged_in) && (avoid_access == 0) ) {
1221 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1222 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1223 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1224 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1227 /* resume our happy day */
1228 return (qrbuf.QRflags);