1 // Server functions which perform operations on room objects.
3 // Copyright (c) 1987-2022 by the citadel.org team
5 // This program is open source software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License, version 3.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
14 #include <libcitadel.h>
16 #include "citserver.h"
17 #include "ctdl_module.h"
23 struct floor *floorcache[MAXFLOORS];
25 // Determine whether the currently logged in session has permission to read
26 // messages in the current room.
27 int CtdlDoIHavePermissionToReadMessagesInThisRoom(void) {
28 if ( (!(CC->logged_in))
29 && (!(CC->internal_pgm))
30 && (!CtdlGetConfigInt("c_guest_logins"))
32 return(om_not_logged_in);
38 // Check to see whether we have permission to post a message in the current
39 // room. Returns a *CITADEL ERROR CODE* and puts a message in errmsgbuf, or
40 // returns 0 on success.
41 int CtdlDoIHavePermissionToPostInThisRoom(
49 if (!(CC->logged_in) && (PostPublic == POST_LOGGED_IN)) {
50 snprintf(errmsgbuf, n, "Not logged in.");
51 return (ERROR + NOT_LOGGED_IN);
53 else if (PostPublic == CHECK_EXIST) {
54 return (0); // evaluate whether a recipient exists
56 else if (!(CC->logged_in)) {
57 if ((CC->room.QRflags & QR_READONLY)) {
58 snprintf(errmsgbuf, n, "Not logged in.");
59 return (ERROR + NOT_LOGGED_IN);
64 if ((CC->user.axlevel < AxProbU) && ((CC->room.QRflags & QR_MAILBOX) == 0)) {
65 snprintf(errmsgbuf, n, "Need to be validated to enter (except in %s> to sysop)", MAILROOM);
66 return (ERROR + HIGHER_ACCESS_REQUIRED);
69 CtdlRoomAccess(&CC->room, &CC->user, &ra, NULL);
71 if (ra & UA_POSTALLOWED) {
72 strcpy(errmsgbuf, "OK to post or reply here");
76 if ( (ra & UA_REPLYALLOWED) && (is_reply) ) {
77 // To be thorough, we ought to check to see if the message they are
78 // replying to is actually a valid one in this room, but unless this
79 // actually becomes a problem we'll go with high performance instead.
80 strcpy(errmsgbuf, "OK to reply here");
84 if ( (ra & UA_REPLYALLOWED) && (!is_reply) ) {
85 // Clarify what happened with a better error message
86 snprintf(errmsgbuf, n, "You may only reply to existing messages here.");
87 return (ERROR + HIGHER_ACCESS_REQUIRED);
90 snprintf(errmsgbuf, n, "Higher access is required to post in this room.");
91 return (ERROR + HIGHER_ACCESS_REQUIRED);
96 // Check whether the current user has permission to delete messages from
97 // the current room (returns 1 for yes, 0 for no)
98 int CtdlDoIHavePermissionToDeleteMessagesFromThisRoom(void) {
100 CtdlRoomAccess(&CC->room, &CC->user, &ra, NULL);
101 if (ra & UA_DELETEALLOWED) return(1);
106 // This is the main access control function for any user/room pair.
107 // Yes, it has a couple of gotos. If you don't like that, go die in a car fire.
108 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf, int *result, int *view) {
114 if (userbuf == &CC->user) {
118 if ((is_me) && (CtdlGetConfigInt("c_guest_logins")) && (!CC->logged_in)) {
122 // for internal programs, always do everything
123 if (((CC->internal_pgm)) && (roombuf->QRflags & QR_INUSE)) {
124 retval = (UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED);
126 goto SKIP_EVERYTHING;
129 // If guest mode is enabled, always grant access to the Lobby
130 if ((is_guest) && (!strcasecmp(roombuf->QRname, BASEROOM))) {
131 retval = (UA_KNOWN | UA_GOTOALLOWED);
133 goto SKIP_EVERYTHING;
136 // Locate any applicable user/room relationships
138 memset(&vbuf, 0, sizeof vbuf);
141 CtdlGetRelationship(&vbuf, userbuf, roombuf);
144 // Force the properties of the Aide room
145 if (!strcasecmp(roombuf->QRname, CtdlGetConfigStr("c_aideroom"))) {
146 if (userbuf->axlevel >= AxAideU) {
147 retval = UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED;
155 // If this is a public room, it's accessible...
156 if ( ((roombuf->QRflags & QR_PRIVATE) == 0)
157 && ((roombuf->QRflags & QR_MAILBOX) == 0)
159 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
162 // If this is a preferred users only room, check access level
163 if (roombuf->QRflags & QR_PREFONLY) {
164 if (userbuf->axlevel < AxPrefU) {
165 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
169 // For private rooms, check the generation number matchups
170 if ( (roombuf->QRflags & QR_PRIVATE)
171 && ((roombuf->QRflags & QR_MAILBOX) == 0)
174 // An explicit match means the user belongs in this room
175 if (vbuf.v_flags & V_ACCESS) {
176 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
178 // Otherwise, check if this is a guess-name or passworded
179 // room. If it is, a goto may at least be attempted
180 else if ( (roombuf->QRflags & QR_PRIVATE)
181 || (roombuf->QRflags & QR_PASSWORDED)
183 retval = retval & ~UA_KNOWN;
184 retval = retval | UA_GOTOALLOWED;
188 // For mailbox rooms, also check the namespace. Also, mailbox owners can delete their messages
189 if ( (roombuf->QRflags & QR_MAILBOX) && (atol(roombuf->QRname) != 0)) {
190 if (userbuf->usernum == atol(roombuf->QRname)) {
191 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED;
193 // An explicit match means the user belongs in this room
194 if (vbuf.v_flags & V_ACCESS) {
195 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED;
199 // For non-mailbox rooms...
201 // User is allowed to post in the room unless:
202 // - User is not validated
203 // - It is a read-only room
204 // - It is a blog room (in which case we only allow replies to existing messages)
205 int post_allowed = 1;
206 int reply_allowed = 1;
207 if (userbuf->axlevel < AxProbU) {
211 if (roombuf->QRflags & QR_READONLY) {
215 if (roombuf->QRdefaultview == VIEW_BLOG) {
219 retval = retval | UA_POSTALLOWED | UA_REPLYALLOWED;
222 retval = retval | UA_REPLYALLOWED;
225 // If "collaborative deletion" is active for this room, any user who can post
226 // is also allowed to delete
227 if (roombuf->QRflags2 & QR2_COLLABDEL) {
228 if (retval & UA_POSTALLOWED) {
229 retval = retval | UA_DELETEALLOWED;
235 // Check to see if the user has forgotten this room
236 if (vbuf.v_flags & V_FORGET) {
237 retval = retval & ~UA_KNOWN;
238 if ( ( ((roombuf->QRflags & QR_PRIVATE) == 0)
239 && ((roombuf->QRflags & QR_MAILBOX) == 0)
240 ) || ( (roombuf->QRflags & QR_MAILBOX)
241 && (atol(roombuf->QRname) == CC->user.usernum))
243 retval = retval | UA_ZAPPED;
247 // If user is explicitly locked out of this room, deny everything
248 if (vbuf.v_flags & V_LOCKOUT) {
249 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED & ~UA_POSTALLOWED & ~UA_REPLYALLOWED;
252 // Aides get access to all private rooms
253 if ( (userbuf->axlevel >= AxAideU)
254 && ((roombuf->QRflags & QR_MAILBOX) == 0)
256 if (vbuf.v_flags & V_FORGET) {
257 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
260 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
264 // Aides can gain access to mailboxes as well, but they don't show by default.
265 if ( (userbuf->axlevel >= AxAideU)
266 && (roombuf->QRflags & QR_MAILBOX)
268 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
271 // Aides and Room Aides have admin privileges
272 if ( (userbuf->axlevel >= AxAideU)
273 || (userbuf->usernum == roombuf->QRroomaide)
275 retval = retval | UA_ADMINALLOWED | UA_DELETEALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
278 NEWMSG: // By the way, we also check for the presence of new messages
279 if (is_msg_in_sequence_set(vbuf.v_seen, roombuf->QRhighest) == 0) {
280 retval = retval | UA_HASNEWMSGS;
283 // System rooms never show up in the list.
284 if (roombuf->QRflags2 & QR2_SYSTEM) {
285 retval = retval & ~UA_KNOWN;
289 // Now give the caller the information it wants.
290 if (result != NULL) *result = retval;
291 if (view != NULL) *view = vbuf.v_view;
295 // Self-checking stuff for a room record read into memory
296 void room_sanity_check(struct ctdlroom *qrbuf) {
297 // Mailbox rooms are always on the lowest floor
298 if (qrbuf->QRflags & QR_MAILBOX) {
301 // Listing order of 0 is illegal except for base rooms
302 if (qrbuf->QRorder == 0) {
303 if ( !(qrbuf->QRflags & QR_MAILBOX)
304 && strncasecmp(qrbuf->QRname, CtdlGetConfigStr("c_baseroom"), ROOMNAMELEN)
305 && strncasecmp(qrbuf->QRname, CtdlGetConfigStr("c_aideroom"), ROOMNAMELEN)
313 // CtdlGetRoom() - retrieve room data from disk
314 int CtdlGetRoom(struct ctdlroom *qrbuf, const char *room_name) {
315 struct cdbdata *cdbqr;
316 char lowercase_name[ROOMNAMELEN];
317 char personal_lowercase_name[ROOMNAMELEN];
321 dptr = lowercase_name;
323 eptr = (dptr + (sizeof lowercase_name - 1));
324 while (!IsEmptyStr(sptr) && (dptr < eptr)) {
325 *dptr = tolower(*sptr);
330 memset(qrbuf, 0, sizeof(struct ctdlroom));
332 if (IsEmptyStr(lowercase_name)) {
333 return(1); // empty room name , not valid
336 // First, try the public namespace
337 cdbqr = cdb_fetch(CDB_ROOMS, lowercase_name, strlen(lowercase_name));
339 // If that didn't work, try the user's personal namespace
341 snprintf(personal_lowercase_name, sizeof personal_lowercase_name, "%010ld.%s", CC->user.usernum, lowercase_name);
342 cdbqr = cdb_fetch(CDB_ROOMS, personal_lowercase_name, strlen(personal_lowercase_name));
345 memcpy(qrbuf, cdbqr->ptr, ((cdbqr->len > sizeof(struct ctdlroom)) ? sizeof(struct ctdlroom) : cdbqr->len));
347 room_sanity_check(qrbuf);
356 // CtdlGetRoomLock() - same as getroom() but locks the record (if supported)
357 int CtdlGetRoomLock(struct ctdlroom *qrbuf, const char *room_name) {
359 retval = CtdlGetRoom(qrbuf, room_name);
360 if (retval == 0) begin_critical_section(S_ROOMS);
365 // b_putroom() - back end to putroom() and b_deleteroom()
366 // (if the supplied buffer is NULL, delete the room record)
367 void b_putroom(struct ctdlroom *qrbuf, char *room_name) {
368 char lowercase_name[ROOMNAMELEN];
373 bptr = lowercase_name;
374 while (!IsEmptyStr(aptr)) {
375 *bptr = tolower(*aptr);
381 len = bptr - lowercase_name;
383 cdb_delete(CDB_ROOMS, lowercase_name, len);
386 time(&qrbuf->QRmtime);
387 cdb_store(CDB_ROOMS, lowercase_name, len, qrbuf, sizeof(struct ctdlroom));
392 // CtdlPutRoom() - store room data to disk
393 void CtdlPutRoom(struct ctdlroom *qrbuf) {
394 b_putroom(qrbuf, qrbuf->QRname);
398 // b_deleteroom() - delete a room record from disk
399 void b_deleteroom(char *room_name) {
400 b_putroom(NULL, room_name);
404 // CtdlPutRoomLock() - same as CtdlPutRoom() but unlocks the record (if supported)
405 void CtdlPutRoomLock(struct ctdlroom *qrbuf) {
407 end_critical_section(S_ROOMS);
411 // CtdlGetFloorByName() - retrieve the number of the named floor
412 // return < 0 if not found else return floor number
413 int CtdlGetFloorByName(const char *floor_name) {
415 struct floor *flbuf = NULL;
417 for (a = 0; a < MAXFLOORS; ++a) {
418 flbuf = CtdlGetCachedFloor(a);
420 // check to see if it already exists
421 if ((!strcasecmp(flbuf->f_name, floor_name)) && (flbuf->f_flags & F_INUSE)) {
429 // CtdlGetFloorByNameLock() - retrieve floor number for given floor and lock the floor list.
430 int CtdlGetFloorByNameLock(const char *floor_name) {
431 begin_critical_section(S_FLOORTAB);
432 return CtdlGetFloorByName(floor_name);
436 // CtdlGetAvailableFloor() - Return number of first unused floor
437 // return < 0 if none available
438 int CtdlGetAvailableFloor(void) {
440 struct floor *flbuf = NULL;
442 for (a = 0; a < MAXFLOORS; a++) {
443 flbuf = CtdlGetCachedFloor(a);
445 // check to see if it already exists
446 if ((flbuf->f_flags & F_INUSE) == 0) {
454 // CtdlGetFloor() - retrieve floor data from disk
455 void CtdlGetFloor(struct floor *flbuf, int floor_num) {
456 struct cdbdata *cdbfl;
458 memset(flbuf, 0, sizeof(struct floor));
459 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
461 memcpy(flbuf, cdbfl->ptr, ((cdbfl->len > sizeof(struct floor)) ? sizeof(struct floor) : cdbfl->len));
465 if (floor_num == 0) {
466 safestrncpy(flbuf->f_name, "Main Floor", sizeof flbuf->f_name);
467 flbuf->f_flags = F_INUSE;
468 flbuf->f_ref_count = 3;
474 // lgetfloor() - same as CtdlGetFloor() but locks the record (if supported)
475 void lgetfloor(struct floor *flbuf, int floor_num) {
476 begin_critical_section(S_FLOORTAB);
477 CtdlGetFloor(flbuf, floor_num);
481 // CtdlGetCachedFloor() - Get floor record from *cache* (loads from disk if needed)
482 // This is strictly a performance hack.
483 struct floor *CtdlGetCachedFloor(int floor_num) {
484 static int initialized = 0;
487 struct floor *fl = NULL;
489 begin_critical_section(S_FLOORCACHE);
490 if (initialized == 0) {
491 for (i=0; i<MAXFLOORS; ++i) {
492 floorcache[floor_num] = NULL;
496 if (floorcache[floor_num] == NULL) {
499 end_critical_section(S_FLOORCACHE);
502 fl = malloc(sizeof(struct floor));
503 CtdlGetFloor(fl, floor_num);
504 begin_critical_section(S_FLOORCACHE);
505 if (floorcache[floor_num] != NULL) {
506 free(floorcache[floor_num]);
508 floorcache[floor_num] = fl;
509 end_critical_section(S_FLOORCACHE);
512 return(floorcache[floor_num]);
516 // CtdlPutFloor() - store floor data on disk
517 void CtdlPutFloor(struct floor *flbuf, int floor_num) {
518 // If we've cached this, clear it out, 'cuz it's WRONG now!
519 begin_critical_section(S_FLOORCACHE);
520 if (floorcache[floor_num] != NULL) {
521 free(floorcache[floor_num]);
522 floorcache[floor_num] = malloc(sizeof(struct floor));
523 memcpy(floorcache[floor_num], flbuf, sizeof(struct floor));
525 end_critical_section(S_FLOORCACHE);
527 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
528 flbuf, sizeof(struct floor));
532 // CtdlPutFloorLock() - same as CtdlPutFloor() but unlocks the record (if supported)
533 void CtdlPutFloorLock(struct floor *flbuf, int floor_num) {
534 CtdlPutFloor(flbuf, floor_num);
535 end_critical_section(S_FLOORTAB);
540 // lputfloor() - same as CtdlPutFloor() but unlocks the record (if supported)
541 void lputfloor(struct floor *flbuf, int floor_num) {
542 CtdlPutFloorLock(flbuf, floor_num);
546 // Iterate through the room table, performing a callback for each room.
547 void CtdlForEachRoom(ForEachRoomCallBack callback_func, void *in_data) {
548 struct ctdlroom qrbuf;
549 struct cdbdata *cdbqr;
551 cdb_rewind(CDB_ROOMS);
553 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
554 memset(&qrbuf, 0, sizeof(struct ctdlroom));
555 memcpy(&qrbuf, cdbqr->ptr, ((cdbqr->len > sizeof(struct ctdlroom)) ? sizeof(struct ctdlroom) : cdbqr->len) );
557 room_sanity_check(&qrbuf);
558 if (qrbuf.QRflags & QR_INUSE) {
559 callback_func(&qrbuf, in_data);
565 // delete_msglist() - delete room message pointers
566 void delete_msglist(struct ctdlroom *whichroom) {
567 struct cdbdata *cdbml;
569 // Make sure the msglist we're deleting actually exists, otherwise
570 // libdb will complain when we try to delete an invalid record
571 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
575 // Go ahead and delete it
576 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
581 // Message pointer compare function for sort_msglist()
582 int sort_msglist_cmp(const void *m1, const void *m2) {
583 if ((*(const long *)m1) > (*(const long *)m2)) return(1);
584 if ((*(const long *)m1) < (*(const long *)m2)) return(-1);
589 // sort message pointers
590 // (returns new msg count)
591 int sort_msglist(long listptrs[], int oldcount) {
596 if (numitems < 2) { // an empty or 1-item list needs no sorting
600 qsort(listptrs, numitems, sizeof(long), sort_msglist_cmp); // do the sort
602 while ((i < numitems) && (listptrs[i] == 0L)) i++; // yank out any nulls
604 memmove(&listptrs[0], &listptrs[i], (sizeof(long) * (numitems - i)));
612 // Determine whether a given room is non-editable.
613 int CtdlIsNonEditable(struct ctdlroom *qrbuf) {
615 // The inbox cannot be edited
616 if ( (qrbuf->QRflags & QR_MAILBOX) && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) ) {
620 // Everything else is editable
625 // Make the specified room the current room for this session. No validation
626 // or access control is done here -- the caller should make sure that the
627 // specified room exists and is ok to access.
628 void CtdlUserGoto(char *where, int display_result, int transiently,
629 int *retmsgs, int *retnew, long *retoldest, long *retnewest)
632 int new_messages = 0;
633 int old_messages = 0;
634 int total_messages = 0;
635 long oldest_message = 0;
636 long newest_message = 0;
640 int newmailcount = 0;
642 char truncated_roomname[ROOMNAMELEN];
643 struct cdbdata *cdbfr;
644 long *msglist = NULL;
646 unsigned int original_v_flags;
649 char setstr[128], lostr[64], histr[64];
653 // If the supplied room name is NULL, the caller wants us to know that
654 // it has already copied the room record into CC->room, so
655 // we can skip the extra database fetch.
657 safestrncpy(CC->room.QRname, where, sizeof CC->room.QRname);
658 CtdlGetRoom(&CC->room, where);
661 // Take care of all the formalities.
663 begin_critical_section(S_USERS);
664 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
665 original_v_flags = vbuf.v_flags;
667 // Know the room ... but not if it's the page log room, or if the
668 // caller specified that we're only entering this room transiently.
669 int add_room_to_known_list = 1;
670 if (transiently == 1) {
671 add_room_to_known_list = 0;
673 char *c_logpages = CtdlGetConfigStr("c_logpages");
674 if ( (c_logpages != NULL) && (!strcasecmp(CC->room.QRname, c_logpages)) ) {
675 add_room_to_known_list = 0;
677 if (add_room_to_known_list) {
678 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
679 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
682 // Only rewrite the database record if we changed something
683 if (vbuf.v_flags != original_v_flags) {
684 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
686 end_critical_section(S_USERS);
688 // Check for new mail
689 newmailcount = NewMailCount();
691 // Set info to 1 if the room banner is new since our last visit.
692 // Some clients only want to display it when it changes.
693 if (CC->room.msgnum_info > vbuf.v_lastseen) {
697 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
699 msglist = (long *) cdbfr->ptr;
700 cdbfr->ptr = NULL; // CtdlUserGoto() now owns this memory
701 num_msgs = cdbfr->len / sizeof(long);
706 for (a=0; a<num_msgs; ++a) {
707 if (msglist[a] > 0L) ++total_messages;
710 if (total_messages > 0) {
711 oldest_message = msglist[0];
712 newest_message = msglist[num_msgs - 1];
715 num_sets = num_tokens(vbuf.v_seen, ',');
716 for (s=0; s<num_sets; ++s) {
717 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
719 extract_token(lostr, setstr, 0, ':', sizeof lostr);
720 if (num_tokens(setstr, ':') >= 2) {
721 extract_token(histr, setstr, 1, ':', sizeof histr);
722 if (!strcmp(histr, "*")) {
723 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
727 strcpy(histr, lostr);
732 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
733 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
739 new_messages = total_messages - old_messages;
741 if (msglist != NULL) free(msglist);
743 if (CC->room.QRflags & QR_MAILBOX)
748 if ((CC->room.QRroomaide == CC->user.usernum) || (CC->user.axlevel >= AxAideU))
753 safestrncpy(truncated_roomname, CC->room.QRname, sizeof truncated_roomname);
754 if ( (CC->room.QRflags & QR_MAILBOX) && (atol(CC->room.QRname) == CC->user.usernum) ) {
755 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
758 if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
762 if (retmsgs != NULL) *retmsgs = total_messages;
763 if (retnew != NULL) *retnew = new_messages;
764 if (retoldest != NULL) *retoldest = oldest_message;
765 if (retnewest != NULL) *retnewest = newest_message;
766 syslog(LOG_DEBUG, "room_ops: %s : %d new of %d total messages, oldest=%ld, newest=%ld",
767 CC->room.QRname, new_messages, total_messages, oldest_message, newest_message
770 CC->curr_view = (int)vbuf.v_view;
772 if (display_result) {
773 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|%d|%ld|\n",
774 CIT_OK, CtdlCheckExpress(),
779 (int)CC->room.QRflags,
780 (long)CC->room.QRhighest,
781 (long)vbuf.v_lastseen,
785 (int)CC->room.QRfloor,
787 (int)CC->room.QRdefaultview,
789 (int)CC->room.QRflags2,
790 (long)CC->room.QRmtime
796 // Handle some of the macro named rooms
797 void convert_room_name_macros(char *towhere, size_t maxlen) {
798 if (!strcasecmp(towhere, "_BASEROOM_")) {
799 safestrncpy(towhere, CtdlGetConfigStr("c_baseroom"), maxlen);
801 else if (!strcasecmp(towhere, "_MAIL_")) {
802 safestrncpy(towhere, MAILROOM, maxlen);
804 else if (!strcasecmp(towhere, "_TRASH_")) {
805 safestrncpy(towhere, USERTRASHROOM, maxlen);
807 else if (!strcasecmp(towhere, "_DRAFTS_")) {
808 safestrncpy(towhere, USERDRAFTROOM, maxlen);
810 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
811 safestrncpy(towhere, CtdlGetConfigStr("c_twitroom"), maxlen);
813 else if (!strcasecmp(towhere, "_CALENDAR_")) {
814 safestrncpy(towhere, USERCALENDARROOM, maxlen);
816 else if (!strcasecmp(towhere, "_TASKS_")) {
817 safestrncpy(towhere, USERTASKSROOM, maxlen);
819 else if (!strcasecmp(towhere, "_CONTACTS_")) {
820 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
822 else if (!strcasecmp(towhere, "_NOTES_")) {
823 safestrncpy(towhere, USERNOTESROOM, maxlen);
828 // Back end function to rename a room.
829 // You can also specify which floor to move the room to, or specify -1 to
830 // keep the room on the same floor it was on.
832 // If you are renaming a mailbox room, you must supply the namespace prefix
833 // in *at least* the old name!
834 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
836 struct ctdlroom qrbuf;
837 struct ctdlroom qrtmp;
842 char actual_old_name[ROOMNAMELEN];
844 syslog(LOG_DEBUG, "room_ops: CtdlRenameRoom(%s, %s, %d)", old_name, new_name, new_floor);
846 if (new_floor >= 0) {
847 fl = CtdlGetCachedFloor(new_floor);
848 if ((fl->f_flags & F_INUSE) == 0) {
849 return(crr_invalid_floor);
853 begin_critical_section(S_ROOMS);
855 if ( (CtdlGetRoom(&qrtmp, new_name) == 0) && (strcasecmp(new_name, old_name)) ) {
856 ret = crr_already_exists;
859 else if (CtdlGetRoom(&qrbuf, old_name) != 0) {
860 ret = crr_room_not_found;
863 else if ( (CC->user.axlevel < AxAideU) && (!CC->internal_pgm)
864 && (CC->user.usernum != qrbuf.QRroomaide)
865 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) ) {
866 ret = crr_access_denied;
869 else if (CtdlIsNonEditable(&qrbuf)) {
870 ret = crr_noneditable;
875 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
876 if (qrbuf.QRflags & QR_MAILBOX) {
877 owner = atol(qrbuf.QRname);
879 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
880 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
881 "%010ld.%s", owner, new_name);
884 safestrncpy(qrbuf.QRname, new_name,
885 sizeof(qrbuf.QRname));
888 // Reject change of floor for baseroom/aideroom
889 if (!strncasecmp(old_name, CtdlGetConfigStr("c_baseroom"), ROOMNAMELEN) ||
890 !strncasecmp(old_name, CtdlGetConfigStr("c_aideroom"), ROOMNAMELEN))
895 // Take care of floor stuff
896 old_floor = qrbuf.QRfloor;
898 new_floor = old_floor;
900 qrbuf.QRfloor = new_floor;
903 begin_critical_section(S_CONFIG);
905 // If baseroom/aideroom name changes, update config
906 if (!strncasecmp(old_name, CtdlGetConfigStr("c_baseroom"), ROOMNAMELEN)) {
907 CtdlSetConfigStr("c_baseroom", new_name);
909 if (!strncasecmp(old_name, CtdlGetConfigStr("c_aideroom"), ROOMNAMELEN)) {
910 CtdlSetConfigStr("c_aideroom", new_name);
913 end_critical_section(S_CONFIG);
915 // If the room name changed, then there are now two room
916 // records, so we have to delete the old one.
917 if (strcasecmp(new_name, old_name)) {
918 b_deleteroom(actual_old_name);
924 end_critical_section(S_ROOMS);
926 // Adjust the floor reference counts if necessary
927 if (new_floor != old_floor) {
928 lgetfloor(&flbuf, old_floor);
930 lputfloor(&flbuf, old_floor);
931 syslog(LOG_DEBUG, "room_ops: reference count for floor %d is now %d", old_floor, flbuf.f_ref_count);
932 lgetfloor(&flbuf, new_floor);
934 lputfloor(&flbuf, new_floor);
935 syslog(LOG_DEBUG, "room_ops: reference count for floor %d is now %d", new_floor, flbuf.f_ref_count);
938 // ...and everybody say "YATTA!"
943 // Asynchronously schedule a room for deletion. By placing the room into an invalid private namespace,
944 // the room will appear deleted to the user(s), but the session doesn't need to block while waiting for
945 // database operations to complete. Instead, the room gets purged when THE DREADED AUTO-PURGER makes
946 // its next run. Aren't we so clever?!!
947 void CtdlScheduleRoomForDeletion(struct ctdlroom *qrbuf) {
948 char old_name[ROOMNAMELEN];
951 syslog(LOG_NOTICE, "room_ops: scheduling room <%s> for deletion", qrbuf->QRname);
953 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
954 CtdlGetRoom(qrbuf, qrbuf->QRname);
956 // Move the room into a hidden namespace owned by a non-existent user.
957 // This will make the room invisible to everyone.
958 // It will also qualify it for removal during the next purge cycle.
959 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
964 qrbuf->QRflags |= QR_MAILBOX;
965 time(&qrbuf->QRgen); // Use a timestamp as the new generation number
967 b_deleteroom(old_name);
971 // Back end processing to delete a room and everything associated with it
972 // (This one is synchronous and should only get called by THE DREADED
973 // AUTO-PURGER in serv_expire.c. All user-facing code should call
974 // the asynchronous schedule_room_for_deletion() instead.)
975 void CtdlDeleteRoom(struct ctdlroom *qrbuf) {
977 char configdbkeyname[25];
979 syslog(LOG_NOTICE, "room_ops: deleting room <%s>", qrbuf->QRname);
981 // Delete the room's network configdb entry
982 netcfg_keyname(configdbkeyname, qrbuf->QRnumber);
983 CtdlDelConfig(configdbkeyname);
985 // Delete the messages in the room
986 // (Careful: this opens an S_ROOMS critical section!)
987 CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
989 // Flag the room record as not in use
990 CtdlGetRoomLock(qrbuf, qrbuf->QRname);
992 CtdlPutRoomLock(qrbuf);
994 // then decrement the reference count for the floor
995 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
996 flbuf.f_ref_count = flbuf.f_ref_count - 1;
997 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
999 // Delete the room record from the database!
1000 b_deleteroom(qrbuf->QRname);
1004 // Check access control for deleting a room
1005 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1007 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1011 if (CtdlIsNonEditable(qr)) {
1015 // For mailboxes, check stuff
1016 if (qr->QRflags & QR_MAILBOX) {
1018 if (strlen(qr->QRname) < 12) return(0); // bad name
1020 if (atol(qr->QRname) != CC->user.usernum) {
1021 return(0); // not my room
1024 // Can't delete your own inbox
1025 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1027 // Otherwise it's ok
1031 // For normal rooms, just check for admin or room admin status.
1032 return(is_room_aide());
1036 // Internal code to create a new room (returns room flags)
1038 // Room types: 0=public, 1=hidden, 2=passworded, 3=invitation-only,
1039 // 4=mailbox, 5=mailbox, but caller supplies namespace
1040 unsigned CtdlCreateRoom(char *new_room_name,
1042 char *new_room_pass,
1048 struct ctdlroom qrbuf;
1052 syslog(LOG_DEBUG, "room_ops: CtdlCreateRoom(name=%s, type=%d, view=%d)", new_room_name, new_room_type, new_room_view);
1054 if (CtdlGetRoom(&qrbuf, new_room_name) == 0) {
1055 syslog(LOG_DEBUG, "room_ops: cannot create room <%s> - already exists", new_room_name);
1059 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1060 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1061 qrbuf.QRflags = QR_INUSE;
1062 if (new_room_type > 0)
1063 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1064 if (new_room_type == 1)
1065 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1066 if (new_room_type == 2)
1067 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1068 if ( (new_room_type == 4) || (new_room_type == 5) ) {
1069 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1070 // qrbuf.QRflags2 |= QR2_SUBJECTREQ;
1073 // If the user is requesting a personal room, set up the room
1074 // name accordingly (prepend the user number)
1075 if (new_room_type == 4) {
1076 CtdlMailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1079 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1082 // If the room is private, and the system administrator has elected
1083 // to automatically grant room admin privileges, do so now.
1084 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1085 qrbuf.QRroomaide = CC->user.usernum;
1088 // Blog owners automatically become room admins of their blogs.
1089 // (In the future we will offer a site-wide configuration setting to suppress this behavior.)
1090 else if (new_room_view == VIEW_BLOG) {
1091 qrbuf.QRroomaide = CC->user.usernum;
1094 // Otherwise, set the room admin to undefined.
1096 qrbuf.QRroomaide = (-1L);
1099 // If the caller is only interested in testing whether this will work,
1100 // return now without creating the room.
1101 if (!really_create) return (qrbuf.QRflags);
1103 qrbuf.QRnumber = get_new_room_number();
1104 qrbuf.QRhighest = 0L; // No messages in this room yet
1105 time(&qrbuf.QRgen); // Use a timestamp as the generation number
1106 qrbuf.QRfloor = new_room_floor;
1107 qrbuf.QRdefaultview = new_room_view;
1109 // save what we just did...
1110 CtdlPutRoom(&qrbuf);
1112 // bump the reference count on whatever floor the room is on
1113 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1114 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1115 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1117 // Grant the creator access to the room unless the avoid_access
1118 // parameter was specified.
1119 if ( (CC->logged_in) && (avoid_access == 0) ) {
1120 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1121 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1122 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1123 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1126 // resume our happy day
1127 return(qrbuf.QRflags);