2 * Server functions which perform operations on room objects.
4 * Copyright (c) 1987-2017 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) &&
57 (PostPublic == POST_LOGGED_IN)) {
58 snprintf(errmsgbuf, n, "Not logged in.");
59 return (ERROR + NOT_LOGGED_IN);
61 else if (PostPublic == CHECK_EXISTANCE) {
62 return (0); // We're Evaling whether a recipient exists
64 else if (!(CC->logged_in)) {
66 if ((CC->room.QRflags & QR_READONLY)) {
67 snprintf(errmsgbuf, n, "Not logged in.");
68 return (ERROR + NOT_LOGGED_IN);
70 if (CC->room.QRflags2 & QR2_MODERATED) {
71 snprintf(errmsgbuf, n, "Not logged in Moderation feature not yet implemented!");
72 return (ERROR + NOT_LOGGED_IN);
74 if ((PostPublic!=POST_LMTP) &&(CC->room.QRflags2 & QR2_SMTP_PUBLIC) == 0) {
76 return CtdlNetconfigCheckRoomaccess(errmsgbuf, n, RemoteIdentifier);
82 if ((CC->user.axlevel < AxProbU) && ((CC->room.QRflags & QR_MAILBOX) == 0))
84 snprintf(errmsgbuf, n, "Need to be validated to enter (except in %s> to sysop)", MAILROOM);
85 return (ERROR + HIGHER_ACCESS_REQUIRED);
88 CtdlRoomAccess(&CC->room, &CC->user, &ra, NULL);
90 if (ra & UA_POSTALLOWED) {
91 strcpy(errmsgbuf, "OK to post or reply here");
95 if ( (ra & UA_REPLYALLOWED) && (is_reply) ) {
97 * To be thorough, we ought to check to see if the message they are
98 * replying to is actually a valid one in this room, but unless this
99 * actually becomes a problem we'll go with high performance instead.
101 strcpy(errmsgbuf, "OK to reply here");
105 if ( (ra & UA_REPLYALLOWED) && (!is_reply) ) {
106 /* Clarify what happened with a better error message */
107 snprintf(errmsgbuf, n, "You may only reply to existing messages here.");
108 return (ERROR + HIGHER_ACCESS_REQUIRED);
111 snprintf(errmsgbuf, n, "Higher access is required to post in this room.");
112 return (ERROR + HIGHER_ACCESS_REQUIRED);
118 * Check whether the current user has permission to delete messages from
119 * the current room (returns 1 for yes, 0 for no)
121 int CtdlDoIHavePermissionToDeleteMessagesFromThisRoom(void) {
123 CtdlRoomAccess(&CC->room, &CC->user, &ra, NULL);
124 if (ra & UA_DELETEALLOWED) return(1);
130 * Retrieve access control information for any user/room pair.
131 * Yes, it has a couple of gotos. If you don't like that, go die in a car fire.
133 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf,
134 int *result, int *view)
141 if (userbuf == &CC->user) {
145 if ((is_me) && (CtdlGetConfigInt("c_guest_logins")) && (!CC->logged_in)) {
149 /* for internal programs, always do everything */
150 if (((CC->internal_pgm)) && (roombuf->QRflags & QR_INUSE)) {
151 retval = (UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED);
153 goto SKIP_EVERYTHING;
156 /* If guest mode is enabled, always grant access to the Lobby */
157 if ((is_guest) && (!strcasecmp(roombuf->QRname, BASEROOM))) {
158 retval = (UA_KNOWN | UA_GOTOALLOWED);
160 goto SKIP_EVERYTHING;
163 /* Locate any applicable user/room relationships */
165 memset(&vbuf, 0, sizeof vbuf);
168 CtdlGetRelationship(&vbuf, userbuf, roombuf);
171 /* Force the properties of the Aide room */
172 if (!strcasecmp(roombuf->QRname, CtdlGetConfigStr("c_aideroom"))) {
173 if (userbuf->axlevel >= AxAideU) {
174 retval = UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED;
181 /* If this is a public room, it's accessible... */
182 if ( ((roombuf->QRflags & QR_PRIVATE) == 0)
183 && ((roombuf->QRflags & QR_MAILBOX) == 0)
185 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
188 /* If this is a preferred users only room, check access level */
189 if (roombuf->QRflags & QR_PREFONLY) {
190 if (userbuf->axlevel < AxPrefU) {
191 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
195 /* For private rooms, check the generation number matchups */
196 if ( (roombuf->QRflags & QR_PRIVATE)
197 && ((roombuf->QRflags & QR_MAILBOX) == 0)
200 /* An explicit match means the user belongs in this room */
201 if (vbuf.v_flags & V_ACCESS) {
202 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
204 /* Otherwise, check if this is a guess-name or passworded
205 * room. If it is, a goto may at least be attempted
207 else if ( (roombuf->QRflags & QR_PRIVATE)
208 || (roombuf->QRflags & QR_PASSWORDED)
210 retval = retval & ~UA_KNOWN;
211 retval = retval | UA_GOTOALLOWED;
215 /* For mailbox rooms, also check the namespace */
216 /* Also, mailbox owners can delete their messages */
217 if ( (roombuf->QRflags & QR_MAILBOX) && (atol(roombuf->QRname) != 0)) {
218 if (userbuf->usernum == atol(roombuf->QRname)) {
219 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED;
221 /* An explicit match means the user belongs in this room */
222 if (vbuf.v_flags & V_ACCESS) {
223 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED;
227 /* For non-mailbox rooms... */
230 /* User is allowed to post in the room unless:
231 * - User is not validated
232 * - User has no net privileges and it is a shared network room
233 * - It is a read-only room
234 * - It is a blog room (in which case we only allow replies to existing messages)
236 int post_allowed = 1;
237 int reply_allowed = 1;
238 if (userbuf->axlevel < AxProbU) {
242 if ((userbuf->axlevel < AxNetU) && (roombuf->QRflags & QR_NETWORK)) {
246 if (roombuf->QRflags & QR_READONLY) {
250 if (roombuf->QRdefaultview == VIEW_BLOG) {
254 retval = retval | UA_POSTALLOWED | UA_REPLYALLOWED;
257 retval = retval | UA_REPLYALLOWED;
260 /* If "collaborative deletion" is active for this room, any user who can post
261 * is also allowed to delete
263 if (roombuf->QRflags2 & QR2_COLLABDEL) {
264 if (retval & UA_POSTALLOWED) {
265 retval = retval | UA_DELETEALLOWED;
271 /* Check to see if the user has forgotten this room */
272 if (vbuf.v_flags & V_FORGET) {
273 retval = retval & ~UA_KNOWN;
274 if ( ( ((roombuf->QRflags & QR_PRIVATE) == 0)
275 && ((roombuf->QRflags & QR_MAILBOX) == 0)
276 ) || ( (roombuf->QRflags & QR_MAILBOX)
277 && (atol(roombuf->QRname) == CC->user.usernum))
279 retval = retval | UA_ZAPPED;
283 /* If user is explicitly locked out of this room, deny everything */
284 if (vbuf.v_flags & V_LOCKOUT) {
285 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED & ~UA_POSTALLOWED & ~UA_REPLYALLOWED;
288 /* Aides get access to all private rooms */
289 if ( (userbuf->axlevel >= AxAideU)
290 && ((roombuf->QRflags & QR_MAILBOX) == 0)
292 if (vbuf.v_flags & V_FORGET) {
293 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
296 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
300 /* Aides can gain access to mailboxes as well, but they don't show
303 if ( (userbuf->axlevel >= AxAideU)
304 && (roombuf->QRflags & QR_MAILBOX)
306 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
309 /* Aides and Room Aides have admin privileges */
310 if ( (userbuf->axlevel >= AxAideU)
311 || (userbuf->usernum == roombuf->QRroomaide)
313 retval = retval | UA_ADMINALLOWED | UA_DELETEALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
316 NEWMSG: /* By the way, we also check for the presence of new messages */
317 if (is_msg_in_sequence_set(vbuf.v_seen, roombuf->QRhighest) == 0) {
318 retval = retval | UA_HASNEWMSGS;
321 /* System rooms never show up in the list. */
322 if (roombuf->QRflags2 & QR2_SYSTEM) {
323 retval = retval & ~UA_KNOWN;
327 /* Now give the caller the information it wants. */
328 if (result != NULL) *result = retval;
329 if (view != NULL) *view = vbuf.v_view;
334 * Self-checking stuff for a room record read into memory
336 void room_sanity_check(struct ctdlroom *qrbuf)
338 /* Mailbox rooms are always on the lowest floor */
339 if (qrbuf->QRflags & QR_MAILBOX) {
342 /* Listing order of 0 is illegal except for base rooms */
343 if (qrbuf->QRorder == 0)
344 if ( !(qrbuf->QRflags & QR_MAILBOX)
345 && strncasecmp(qrbuf->QRname, CtdlGetConfigStr("c_baseroom"), ROOMNAMELEN)
346 && strncasecmp(qrbuf->QRname, CtdlGetConfigStr("c_aideroom"), ROOMNAMELEN)
354 * CtdlGetRoom() - retrieve room data from disk
356 int CtdlGetRoom(struct ctdlroom *qrbuf, const char *room_name)
358 struct cdbdata *cdbqr;
359 char lowercase_name[ROOMNAMELEN];
360 char personal_lowercase_name[ROOMNAMELEN];
364 dptr = lowercase_name;
366 eptr = (dptr + (sizeof lowercase_name - 1));
367 while (!IsEmptyStr(sptr) && (dptr < eptr)){
368 *dptr = tolower(*sptr);
373 memset(qrbuf, 0, sizeof(struct ctdlroom));
375 /* First, try the public namespace */
376 cdbqr = cdb_fetch(CDB_ROOMS,
377 lowercase_name, strlen(lowercase_name));
379 /* If that didn't work, try the user's personal namespace */
381 snprintf(personal_lowercase_name,
382 sizeof personal_lowercase_name, "%010ld.%s",
383 CC->user.usernum, lowercase_name);
384 cdbqr = cdb_fetch(CDB_ROOMS,
385 personal_lowercase_name,
386 strlen(personal_lowercase_name));
389 memcpy(qrbuf, cdbqr->ptr,
390 ((cdbqr->len > sizeof(struct ctdlroom)) ?
391 sizeof(struct ctdlroom) : cdbqr->len));
394 room_sanity_check(qrbuf);
404 * CtdlGetRoomLock() - same as getroom() but locks the record (if supported)
406 int CtdlGetRoomLock(struct ctdlroom *qrbuf, const char *room_name)
409 retval = CtdlGetRoom(qrbuf, room_name);
410 if (retval == 0) begin_critical_section(S_ROOMS);
416 * b_putroom() - back end to putroom() and b_deleteroom()
417 * (if the supplied buffer is NULL, delete the room record)
419 void b_putroom(struct ctdlroom *qrbuf, char *room_name)
421 char lowercase_name[ROOMNAMELEN];
426 bptr = lowercase_name;
427 while (!IsEmptyStr(aptr))
429 *bptr = tolower(*aptr);
435 len = bptr - lowercase_name;
437 cdb_delete(CDB_ROOMS, lowercase_name, len);
439 time(&qrbuf->QRmtime);
440 cdb_store(CDB_ROOMS, lowercase_name, len, qrbuf, sizeof(struct ctdlroom));
446 * CtdlPutRoom() - store room data to disk
448 void CtdlPutRoom(struct ctdlroom *qrbuf) {
449 b_putroom(qrbuf, qrbuf->QRname);
454 * b_deleteroom() - delete a room record from disk
456 void b_deleteroom(char *room_name) {
457 b_putroom(NULL, room_name);
462 * CtdlPutRoomLock() - same as CtdlPutRoom() but unlocks the record (if supported)
464 void CtdlPutRoomLock(struct ctdlroom *qrbuf)
467 end_critical_section(S_ROOMS);
472 * CtdlGetFloorByName() - retrieve the number of the named floor
473 * return < 0 if not found else return floor number
475 int CtdlGetFloorByName(const char *floor_name)
478 struct floor *flbuf = NULL;
480 for (a = 0; a < MAXFLOORS; ++a) {
481 flbuf = CtdlGetCachedFloor(a);
483 /* check to see if it already exists */
484 if ((!strcasecmp(flbuf->f_name, floor_name))
485 && (flbuf->f_flags & F_INUSE)) {
494 * CtdlGetFloorByNameLock() - retrieve floor number for given floor and lock the floor list.
496 int CtdlGetFloorByNameLock(const char *floor_name)
498 begin_critical_section(S_FLOORTAB);
499 return CtdlGetFloorByName(floor_name);
505 * CtdlGetAvailableFloor() - Return number of first unused floor
506 * return < 0 if none available
508 int CtdlGetAvailableFloor(void)
511 struct floor *flbuf = NULL;
513 for (a = 0; a < MAXFLOORS; a++) {
514 flbuf = CtdlGetCachedFloor(a);
516 /* check to see if it already exists */
517 if ((flbuf->f_flags & F_INUSE) == 0) {
526 * CtdlGetFloor() - retrieve floor data from disk
528 void CtdlGetFloor(struct floor *flbuf, int floor_num)
530 struct cdbdata *cdbfl;
532 memset(flbuf, 0, sizeof(struct floor));
533 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
535 memcpy(flbuf, cdbfl->ptr,
536 ((cdbfl->len > sizeof(struct floor)) ?
537 sizeof(struct floor) : cdbfl->len));
540 if (floor_num == 0) {
541 safestrncpy(flbuf->f_name, "Main Floor",
542 sizeof flbuf->f_name);
543 flbuf->f_flags = F_INUSE;
544 flbuf->f_ref_count = 3;
552 * lgetfloor() - same as CtdlGetFloor() but locks the record (if supported)
554 void lgetfloor(struct floor *flbuf, int floor_num)
556 begin_critical_section(S_FLOORTAB);
557 CtdlGetFloor(flbuf, floor_num);
562 * CtdlGetCachedFloor() - Get floor record from *cache* (loads from disk if needed)
564 * This is strictly a performance hack.
566 struct floor *CtdlGetCachedFloor(int floor_num) {
567 static int initialized = 0;
570 struct floor *fl = NULL;
572 begin_critical_section(S_FLOORCACHE);
573 if (initialized == 0) {
574 for (i=0; i<MAXFLOORS; ++i) {
575 floorcache[floor_num] = NULL;
579 if (floorcache[floor_num] == NULL) {
582 end_critical_section(S_FLOORCACHE);
585 fl = malloc(sizeof(struct floor));
586 CtdlGetFloor(fl, floor_num);
587 begin_critical_section(S_FLOORCACHE);
588 if (floorcache[floor_num] != NULL) {
589 free(floorcache[floor_num]);
591 floorcache[floor_num] = fl;
592 end_critical_section(S_FLOORCACHE);
595 return(floorcache[floor_num]);
600 * CtdlPutFloor() - store floor data on disk
602 void CtdlPutFloor(struct floor *flbuf, int floor_num)
604 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
605 begin_critical_section(S_FLOORCACHE);
606 if (floorcache[floor_num] != NULL) {
607 free(floorcache[floor_num]);
608 floorcache[floor_num] = malloc(sizeof(struct floor));
609 memcpy(floorcache[floor_num], flbuf, sizeof(struct floor));
611 end_critical_section(S_FLOORCACHE);
613 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
614 flbuf, sizeof(struct floor));
619 * CtdlPutFloorLock() - same as CtdlPutFloor() but unlocks the record (if supported)
621 void CtdlPutFloorLock(struct floor *flbuf, int floor_num)
623 CtdlPutFloor(flbuf, floor_num);
624 end_critical_section(S_FLOORTAB);
630 * lputfloor() - same as CtdlPutFloor() but unlocks the record (if supported)
632 void lputfloor(struct floor *flbuf, int floor_num)
634 CtdlPutFloorLock(flbuf, floor_num);
639 * Iterate through the room table, performing a callback for each room.
641 void CtdlForEachRoom(ForEachRoomCallBack CB, void *in_data)
643 struct ctdlroom qrbuf;
644 struct cdbdata *cdbqr;
646 cdb_rewind(CDB_ROOMS);
648 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
649 memset(&qrbuf, 0, sizeof(struct ctdlroom));
650 memcpy(&qrbuf, cdbqr->ptr,
651 ((cdbqr->len > sizeof(struct ctdlroom)) ?
652 sizeof(struct ctdlroom) : cdbqr->len)
655 room_sanity_check(&qrbuf);
656 if (qrbuf.QRflags & QR_INUSE) {
663 * Iterate through the room table, performing a callback for each room that has a netconfig entry.
665 void CtdlForEachNetCfgRoom(ForEachRoomNetCfgCallBack CB, void *in_data)
667 struct ctdlroom qrbuf;
668 struct cdbdata *cdbqr;
670 cdb_rewind(CDB_ROOMS);
672 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
673 memset(&qrbuf, 0, sizeof(struct ctdlroom));
674 memcpy(&qrbuf, cdbqr->ptr, ((cdbqr->len > sizeof(struct ctdlroom)) ? sizeof(struct ctdlroom) : cdbqr->len));
676 room_sanity_check(&qrbuf);
677 if (qrbuf.QRflags & QR_INUSE)
679 OneRoomNetCfg *RNCfg;
680 RNCfg = CtdlGetNetCfgForRoom(qrbuf.QRnumber);
683 CB(&qrbuf, in_data, RNCfg);
684 FreeRoomNetworkStruct(&RNCfg);
692 * delete_msglist() - delete room message pointers
694 void delete_msglist(struct ctdlroom *whichroom)
696 struct cdbdata *cdbml;
698 /* Make sure the msglist we're deleting actually exists, otherwise
699 * libdb will complain when we try to delete an invalid record
701 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
705 /* Go ahead and delete it */
706 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
712 * Message pointer compare function for sort_msglist()
714 int sort_msglist_cmp(const void *m1, const void *m2) {
715 if ((*(const long *)m1) > (*(const long *)m2)) return(1);
716 if ((*(const long *)m1) < (*(const long *)m2)) return(-1);
722 * sort message pointers
723 * (returns new msg count)
725 int sort_msglist(long listptrs[], int oldcount)
736 qsort(listptrs, numitems, sizeof(long), sort_msglist_cmp);
738 /* and yank any nulls */
739 while ((i < numitems) && (listptrs[i] == 0L)) i++;
743 memmove(&listptrs[0], &listptrs[i], (sizeof(long) * (numitems - i)));
752 * Determine whether a given room is non-editable.
754 int CtdlIsNonEditable(struct ctdlroom *qrbuf)
757 /* Mail> rooms are non-editable */
758 if ( (qrbuf->QRflags & QR_MAILBOX)
759 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
762 /* Everything else is editable */
769 * Make the specified room the current room for this session. No validation
770 * or access control is done here -- the caller should make sure that the
771 * specified room exists and is ok to access.
773 void CtdlUserGoto(char *where, int display_result, int transiently,
774 int *retmsgs, int *retnew, long *retoldest, long *retnewest)
776 struct CitContext *CCC = CC;
778 int new_messages = 0;
779 int old_messages = 0;
780 int total_messages = 0;
781 long oldest_message = 0;
782 long newest_message = 0;
786 int newmailcount = 0;
788 char truncated_roomname[ROOMNAMELEN];
789 struct cdbdata *cdbfr;
790 long *msglist = NULL;
792 unsigned int original_v_flags;
795 char setstr[128], lostr[64], histr[64];
799 /* If the supplied room name is NULL, the caller wants us to know that
800 * it has already copied the room record into CC->room, so
801 * we can skip the extra database fetch.
804 safestrncpy(CCC->room.QRname, where, sizeof CCC->room.QRname);
805 CtdlGetRoom(&CCC->room, where);
808 /* Take care of all the formalities. */
810 begin_critical_section(S_USERS);
811 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
812 original_v_flags = vbuf.v_flags;
814 /* Know the room ... but not if it's the page log room, or if the
815 * caller specified that we're only entering this room transiently.
817 int add_room_to_known_list = 1;
818 if (transiently == 1) {
819 add_room_to_known_list = 0;
821 char *c_logpages = CtdlGetConfigStr("c_logpages");
822 if ( (c_logpages != NULL) && (!strcasecmp(CCC->room.QRname, c_logpages)) ) {
823 add_room_to_known_list = 0;
825 if (add_room_to_known_list) {
826 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
827 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
830 /* Only rewrite the database record if we changed something */
831 if (vbuf.v_flags != original_v_flags) {
832 CtdlSetRelationship(&vbuf, &CCC->user, &CCC->room);
834 end_critical_section(S_USERS);
836 /* Check for new mail */
837 newmailcount = NewMailCount();
839 /* Set info to 1 if the room banner is new since our last visit.
840 * Some clients only want to display it when it changes.
842 if (CCC->room.msgnum_info > vbuf.v_lastseen) {
846 cdbfr = cdb_fetch(CDB_MSGLISTS, &CCC->room.QRnumber, sizeof(long));
848 msglist = (long *) cdbfr->ptr;
849 cdbfr->ptr = NULL; /* CtdlUserGoto() now owns this memory */
850 num_msgs = cdbfr->len / sizeof(long);
855 for (a=0; a<num_msgs; ++a) {
856 if (msglist[a] > 0L) ++total_messages;
859 if (total_messages > 0) {
860 oldest_message = msglist[0];
861 newest_message = msglist[num_msgs - 1];
864 num_sets = num_tokens(vbuf.v_seen, ',');
865 for (s=0; s<num_sets; ++s) {
866 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
868 extract_token(lostr, setstr, 0, ':', sizeof lostr);
869 if (num_tokens(setstr, ':') >= 2) {
870 extract_token(histr, setstr, 1, ':', sizeof histr);
871 if (!strcmp(histr, "*")) {
872 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
876 strcpy(histr, lostr);
881 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
882 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
888 new_messages = total_messages - old_messages;
890 if (msglist != NULL) free(msglist);
892 if (CCC->room.QRflags & QR_MAILBOX)
897 if ((CCC->room.QRroomaide == CCC->user.usernum)
898 || (CCC->user.axlevel >= AxAideU))
903 safestrncpy(truncated_roomname, CCC->room.QRname, sizeof truncated_roomname);
904 if ( (CCC->room.QRflags & QR_MAILBOX)
905 && (atol(CCC->room.QRname) == CCC->user.usernum) ) {
906 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
909 if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
913 if (retmsgs != NULL) *retmsgs = total_messages;
914 if (retnew != NULL) *retnew = new_messages;
915 if (retoldest != NULL) *retoldest = oldest_message;
916 if (retnewest != NULL) *retnewest = newest_message;
917 syslog(LOG_DEBUG, "room_ops: %s : %d new of %d total messages, oldest=%ld, newest=%ld",
918 CCC->room.QRname, new_messages, total_messages, oldest_message, newest_message
921 CCC->curr_view = (int)vbuf.v_view;
923 if (display_result) {
924 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|%d|%ld|\n",
925 CIT_OK, CtdlCheckExpress(),
930 (int)CCC->room.QRflags,
931 (long)CCC->room.QRhighest,
932 (long)vbuf.v_lastseen,
936 (int)CCC->room.QRfloor,
938 (int)CCC->room.QRdefaultview,
940 (int)CCC->room.QRflags2,
941 (long)CCC->room.QRmtime
948 * Handle some of the macro named rooms
950 void convert_room_name_macros(char *towhere, size_t maxlen) {
951 if (!strcasecmp(towhere, "_BASEROOM_")) {
952 safestrncpy(towhere, CtdlGetConfigStr("c_baseroom"), maxlen);
954 else if (!strcasecmp(towhere, "_MAIL_")) {
955 safestrncpy(towhere, MAILROOM, maxlen);
957 else if (!strcasecmp(towhere, "_TRASH_")) {
958 safestrncpy(towhere, USERTRASHROOM, maxlen);
960 else if (!strcasecmp(towhere, "_DRAFTS_")) {
961 safestrncpy(towhere, USERDRAFTROOM, maxlen);
963 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
964 safestrncpy(towhere, CtdlGetConfigStr("c_twitroom"), maxlen);
966 else if (!strcasecmp(towhere, "_CALENDAR_")) {
967 safestrncpy(towhere, USERCALENDARROOM, maxlen);
969 else if (!strcasecmp(towhere, "_TASKS_")) {
970 safestrncpy(towhere, USERTASKSROOM, maxlen);
972 else if (!strcasecmp(towhere, "_CONTACTS_")) {
973 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
975 else if (!strcasecmp(towhere, "_NOTES_")) {
976 safestrncpy(towhere, USERNOTESROOM, maxlen);
982 * Back end function to rename a room.
983 * You can also specify which floor to move the room to, or specify -1 to
984 * keep the room on the same floor it was on.
986 * If you are renaming a mailbox room, you must supply the namespace prefix
987 * in *at least* the old name!
989 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
990 struct CitContext *CCC = CC;
992 struct ctdlroom qrbuf;
993 struct ctdlroom qrtmp;
998 char actual_old_name[ROOMNAMELEN];
1000 syslog(LOG_DEBUG, "room_ops: CtdlRenameRoom(%s, %s, %d)", old_name, new_name, new_floor);
1002 if (new_floor >= 0) {
1003 fl = CtdlGetCachedFloor(new_floor);
1004 if ((fl->f_flags & F_INUSE) == 0) {
1005 return(crr_invalid_floor);
1009 begin_critical_section(S_ROOMS);
1011 if ( (CtdlGetRoom(&qrtmp, new_name) == 0)
1012 && (strcasecmp(new_name, old_name)) ) {
1013 ret = crr_already_exists;
1016 else if (CtdlGetRoom(&qrbuf, old_name) != 0) {
1017 ret = crr_room_not_found;
1020 else if ( (CCC->user.axlevel < AxAideU) && (!CCC->internal_pgm)
1021 && (CCC->user.usernum != qrbuf.QRroomaide)
1022 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CCC->user.usernum))) ) {
1023 ret = crr_access_denied;
1026 else if (CtdlIsNonEditable(&qrbuf)) {
1027 ret = crr_noneditable;
1032 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1033 if (qrbuf.QRflags & QR_MAILBOX) {
1034 owner = atol(qrbuf.QRname);
1036 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1037 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1038 "%010ld.%s", owner, new_name);
1041 safestrncpy(qrbuf.QRname, new_name,
1042 sizeof(qrbuf.QRname));
1045 /* Reject change of floor for baseroom/aideroom */
1046 if (!strncasecmp(old_name, CtdlGetConfigStr("c_baseroom"), ROOMNAMELEN) ||
1047 !strncasecmp(old_name, CtdlGetConfigStr("c_aideroom"), ROOMNAMELEN))
1052 /* Take care of floor stuff */
1053 old_floor = qrbuf.QRfloor;
1054 if (new_floor < 0) {
1055 new_floor = old_floor;
1057 qrbuf.QRfloor = new_floor;
1058 CtdlPutRoom(&qrbuf);
1060 begin_critical_section(S_CONFIG);
1062 /* If baseroom/aideroom name changes, update config */
1063 if (!strncasecmp(old_name, CtdlGetConfigStr("c_baseroom"), ROOMNAMELEN)) {
1064 CtdlSetConfigStr("c_baseroom", new_name);
1066 if (!strncasecmp(old_name, CtdlGetConfigStr("c_aideroom"), ROOMNAMELEN)) {
1067 CtdlSetConfigStr("c_aideroom", new_name);
1070 end_critical_section(S_CONFIG);
1072 /* If the room name changed, then there are now two room
1073 * records, so we have to delete the old one.
1075 if (strcasecmp(new_name, old_name)) {
1076 b_deleteroom(actual_old_name);
1082 end_critical_section(S_ROOMS);
1084 /* Adjust the floor reference counts if necessary */
1085 if (new_floor != old_floor) {
1086 lgetfloor(&flbuf, old_floor);
1087 --flbuf.f_ref_count;
1088 lputfloor(&flbuf, old_floor);
1089 syslog(LOG_DEBUG, "room_ops: reference count for floor %d is now %d", old_floor, flbuf.f_ref_count);
1090 lgetfloor(&flbuf, new_floor);
1091 ++flbuf.f_ref_count;
1092 lputfloor(&flbuf, new_floor);
1093 syslog(LOG_DEBUG, "room_ops: reference count for floor %d is now %d", new_floor, flbuf.f_ref_count);
1096 /* ...and everybody say "YATTA!" */
1102 * Asynchronously schedule a room for deletion. By placing the room into an invalid private namespace,
1103 * the room will appear deleted to the user(s), but the session doesn't need to block while waiting for
1104 * database operations to complete. Instead, the room gets purged when THE DREADED AUTO-PURGER makes
1105 * its next run. Aren't we so clever?!!
1107 void CtdlScheduleRoomForDeletion(struct ctdlroom *qrbuf)
1109 char old_name[ROOMNAMELEN];
1112 syslog(LOG_NOTICE, "room_ops: scheduling room <%s> for deletion", qrbuf->QRname);
1114 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1115 CtdlGetRoom(qrbuf, qrbuf->QRname);
1117 /* Turn the room into a private mailbox owned by a user who doesn't
1118 * exist. This will immediately make the room invisible to everyone,
1119 * and qualify the room for purging.
1121 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1126 qrbuf->QRflags |= QR_MAILBOX;
1127 time(&qrbuf->QRgen); /* Use a timestamp as the new generation number */
1129 b_deleteroom(old_name);
1135 * Back end processing to delete a room and everything associated with it
1136 * (This one is synchronous and should only get called by THE DREADED
1137 * AUTO-PURGER in serv_expire.c. All user-facing code should call
1138 * the asynchronous schedule_room_for_deletion() instead.)
1140 void CtdlDeleteRoom(struct ctdlroom *qrbuf)
1143 char configdbkeyname[25];
1145 syslog(LOG_NOTICE, "room_ops: deleting room <%s>", qrbuf->QRname);
1147 /* Delete the room's network configdb entry */
1148 netcfg_keyname(configdbkeyname, qrbuf->QRnumber);
1149 CtdlDelConfig(configdbkeyname);
1151 /* Delete the messages in the room
1152 * (Careful: this opens an S_ROOMS critical section!)
1154 CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
1156 /* Flag the room record as not in use */
1157 CtdlGetRoomLock(qrbuf, qrbuf->QRname);
1159 CtdlPutRoomLock(qrbuf);
1161 /* then decrement the reference count for the floor */
1162 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1163 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1164 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1166 /* Delete the room record from the database! */
1167 b_deleteroom(qrbuf->QRname);
1172 * Check access control for deleting a room
1174 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1176 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1180 if (CtdlIsNonEditable(qr)) {
1185 * For mailboxes, check stuff
1187 if (qr->QRflags & QR_MAILBOX) {
1189 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1191 if (atol(qr->QRname) != CC->user.usernum) {
1192 return(0); /* not my room */
1195 /* Can't delete your Mail> room */
1196 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1198 /* Otherwise it's ok */
1203 * For normal rooms, just check for admin or room admin status.
1205 return(is_room_aide());
1211 * Internal code to create a new room (returns room flags)
1213 * Room types: 0=public, 1=hidden, 2=passworded, 3=invitation-only,
1214 * 4=mailbox, 5=mailbox, but caller supplies namespace
1216 unsigned CtdlCreateRoom(char *new_room_name,
1218 char *new_room_pass,
1225 struct ctdlroom qrbuf;
1229 syslog(LOG_DEBUG, "room_ops: CtdlCreateRoom(name=%s, type=%d, view=%d)", new_room_name, new_room_type, new_room_view);
1231 if (CtdlGetRoom(&qrbuf, new_room_name) == 0) {
1232 syslog(LOG_DEBUG, "room_ops: cannot create room <%s> - already exists", new_room_name);
1236 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1237 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1238 qrbuf.QRflags = QR_INUSE;
1239 if (new_room_type > 0)
1240 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1241 if (new_room_type == 1)
1242 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1243 if (new_room_type == 2)
1244 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1245 if ( (new_room_type == 4) || (new_room_type == 5) ) {
1246 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1247 /* qrbuf.QRflags2 |= QR2_SUBJECTREQ; */
1250 /* If the user is requesting a personal room, set up the room
1251 * name accordingly (prepend the user number)
1253 if (new_room_type == 4) {
1254 CtdlMailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1257 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1260 /* If the room is private, and the system administrator has elected
1261 * to automatically grant room admin privileges, do so now.
1263 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1264 qrbuf.QRroomaide = CC->user.usernum;
1266 /* Blog owners automatically become room admins of their blogs.
1267 * (In the future we will offer a site-wide configuration setting to suppress this behavior.)
1269 else if (new_room_view == VIEW_BLOG) {
1270 qrbuf.QRroomaide = CC->user.usernum;
1272 /* Otherwise, set the room admin to undefined.
1275 qrbuf.QRroomaide = (-1L);
1279 * If the caller is only interested in testing whether this will work,
1280 * return now without creating the room.
1282 if (!really_create) return (qrbuf.QRflags);
1284 qrbuf.QRnumber = get_new_room_number();
1285 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1286 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1287 qrbuf.QRfloor = new_room_floor;
1288 qrbuf.QRdefaultview = new_room_view;
1290 /* save what we just did... */
1291 CtdlPutRoom(&qrbuf);
1293 /* bump the reference count on whatever floor the room is on */
1294 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1295 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1296 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1298 /* Grant the creator access to the room unless the avoid_access
1299 * parameter was specified.
1301 if ( (CC->logged_in) && (avoid_access == 0) ) {
1302 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1303 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1304 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1305 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1308 /* resume our happy day */
1309 return (qrbuf.QRflags);