2 * Server functions which perform operations on room objects.
4 * Copyright (c) 1987-2016 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);
42 * Check to see whether we have permission to post a message in the current
43 * room. Returns a *CITADEL ERROR CODE* and puts a message in errmsgbuf, or
44 * returns 0 on success.
46 int CtdlDoIHavePermissionToPostInThisRoom(
49 const char* RemoteIdentifier,
55 if (!(CC->logged_in) &&
56 (PostPublic == POST_LOGGED_IN)) {
57 snprintf(errmsgbuf, n, "Not logged in.");
58 return (ERROR + NOT_LOGGED_IN);
60 else if (PostPublic == CHECK_EXISTANCE) {
61 return (0); // We're Evaling whether a recipient exists
63 else if (!(CC->logged_in)) {
65 if ((CC->room.QRflags & QR_READONLY)) {
66 snprintf(errmsgbuf, n, "Not logged in.");
67 return (ERROR + NOT_LOGGED_IN);
69 if (CC->room.QRflags2 & QR2_MODERATED) {
70 snprintf(errmsgbuf, n, "Not logged in Moderation feature not yet implemented!");
71 return (ERROR + NOT_LOGGED_IN);
73 if ((PostPublic!=POST_LMTP) &&(CC->room.QRflags2 & QR2_SMTP_PUBLIC) == 0) {
75 return CtdlNetconfigCheckRoomaccess(errmsgbuf, n, RemoteIdentifier);
81 if ((CC->user.axlevel < AxProbU) && ((CC->room.QRflags & QR_MAILBOX) == 0))
83 snprintf(errmsgbuf, n, "Need to be validated to enter (except in %s> to sysop)", MAILROOM);
84 return (ERROR + HIGHER_ACCESS_REQUIRED);
87 CtdlRoomAccess(&CC->room, &CC->user, &ra, NULL);
89 if (ra & UA_POSTALLOWED) {
90 strcpy(errmsgbuf, "OK to post or reply here");
94 if ( (ra & UA_REPLYALLOWED) && (is_reply) ) {
96 * To be thorough, we ought to check to see if the message they are
97 * replying to is actually a valid one in this room, but unless this
98 * actually becomes a problem we'll go with high performance instead.
100 strcpy(errmsgbuf, "OK to reply here");
104 if ( (ra & UA_REPLYALLOWED) && (!is_reply) ) {
105 /* Clarify what happened with a better error message */
106 snprintf(errmsgbuf, n, "You may only reply to existing messages here.");
107 return (ERROR + HIGHER_ACCESS_REQUIRED);
110 snprintf(errmsgbuf, n, "Higher access is required to post in this room.");
111 return (ERROR + HIGHER_ACCESS_REQUIRED);
116 * Check whether the current user has permission to delete messages from
117 * the current room (returns 1 for yes, 0 for no)
119 int CtdlDoIHavePermissionToDeleteMessagesFromThisRoom(void) {
121 CtdlRoomAccess(&CC->room, &CC->user, &ra, NULL);
122 if (ra & UA_DELETEALLOWED) return(1);
127 * Retrieve access control information for any user/room pair.
128 * Yes, it has a couple of gotos. If you don't like that, go die in a car fire.
130 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf,
131 int *result, int *view)
138 if (userbuf == &CC->user) {
142 if ((is_me) && (CtdlGetConfigInt("c_guest_logins")) && (!CC->logged_in)) {
146 /* for internal programs, always do everything */
147 if (((CC->internal_pgm)) && (roombuf->QRflags & QR_INUSE)) {
148 retval = (UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED);
150 goto SKIP_EVERYTHING;
153 /* If guest mode is enabled, always grant access to the Lobby */
154 if ((is_guest) && (!strcasecmp(roombuf->QRname, BASEROOM))) {
155 retval = (UA_KNOWN | UA_GOTOALLOWED);
157 goto SKIP_EVERYTHING;
160 /* Locate any applicable user/room relationships */
162 memset(&vbuf, 0, sizeof vbuf);
165 CtdlGetRelationship(&vbuf, userbuf, roombuf);
168 /* Force the properties of the Aide room */
169 if (!strcasecmp(roombuf->QRname, CtdlGetConfigStr("c_aideroom"))) {
170 if (userbuf->axlevel >= AxAideU) {
171 retval = UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED;
178 /* If this is a public room, it's accessible... */
179 if ( ((roombuf->QRflags & QR_PRIVATE) == 0)
180 && ((roombuf->QRflags & QR_MAILBOX) == 0)
182 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
185 /* If this is a preferred users only room, check access level */
186 if (roombuf->QRflags & QR_PREFONLY) {
187 if (userbuf->axlevel < AxPrefU) {
188 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
192 /* For private rooms, check the generation number matchups */
193 if ( (roombuf->QRflags & QR_PRIVATE)
194 && ((roombuf->QRflags & QR_MAILBOX) == 0)
197 /* An explicit match means the user belongs in this room */
198 if (vbuf.v_flags & V_ACCESS) {
199 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
201 /* Otherwise, check if this is a guess-name or passworded
202 * room. If it is, a goto may at least be attempted
204 else if ( (roombuf->QRflags & QR_PRIVATE)
205 || (roombuf->QRflags & QR_PASSWORDED)
207 retval = retval & ~UA_KNOWN;
208 retval = retval | UA_GOTOALLOWED;
212 /* For mailbox rooms, also check the namespace */
213 /* Also, mailbox owners can delete their messages */
214 if ( (roombuf->QRflags & QR_MAILBOX) && (atol(roombuf->QRname) != 0)) {
215 if (userbuf->usernum == atol(roombuf->QRname)) {
216 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED;
218 /* An explicit match means the user belongs in this room */
219 if (vbuf.v_flags & V_ACCESS) {
220 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED;
224 /* For non-mailbox rooms... */
227 /* User is allowed to post in the room unless:
228 * - User is not validated
229 * - User has no net privileges and it is a shared network room
230 * - It is a read-only room
231 * - It is a blog room (in which case we only allow replies to existing messages)
233 int post_allowed = 1;
234 int reply_allowed = 1;
235 if (userbuf->axlevel < AxProbU) {
239 if ((userbuf->axlevel < AxNetU) && (roombuf->QRflags & QR_NETWORK)) {
243 if (roombuf->QRflags & QR_READONLY) {
247 if (roombuf->QRdefaultview == VIEW_BLOG) {
251 retval = retval | UA_POSTALLOWED | UA_REPLYALLOWED;
254 retval = retval | UA_REPLYALLOWED;
257 /* If "collaborative deletion" is active for this room, any user who can post
258 * is also allowed to delete
260 if (roombuf->QRflags2 & QR2_COLLABDEL) {
261 if (retval & UA_POSTALLOWED) {
262 retval = retval | UA_DELETEALLOWED;
268 /* Check to see if the user has forgotten this room */
269 if (vbuf.v_flags & V_FORGET) {
270 retval = retval & ~UA_KNOWN;
271 if ( ( ((roombuf->QRflags & QR_PRIVATE) == 0)
272 && ((roombuf->QRflags & QR_MAILBOX) == 0)
273 ) || ( (roombuf->QRflags & QR_MAILBOX)
274 && (atol(roombuf->QRname) == CC->user.usernum))
276 retval = retval | UA_ZAPPED;
280 /* If user is explicitly locked out of this room, deny everything */
281 if (vbuf.v_flags & V_LOCKOUT) {
282 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED & ~UA_POSTALLOWED & ~UA_REPLYALLOWED;
285 /* Aides get access to all private rooms */
286 if ( (userbuf->axlevel >= AxAideU)
287 && ((roombuf->QRflags & QR_MAILBOX) == 0)
289 if (vbuf.v_flags & V_FORGET) {
290 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
293 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
297 /* Aides can gain access to mailboxes as well, but they don't show
300 if ( (userbuf->axlevel >= AxAideU)
301 && (roombuf->QRflags & QR_MAILBOX)
303 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
306 /* Aides and Room Aides have admin privileges */
307 if ( (userbuf->axlevel >= AxAideU)
308 || (userbuf->usernum == roombuf->QRroomaide)
310 retval = retval | UA_ADMINALLOWED | UA_DELETEALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
313 NEWMSG: /* By the way, we also check for the presence of new messages */
314 if (is_msg_in_sequence_set(vbuf.v_seen, roombuf->QRhighest) == 0) {
315 retval = retval | UA_HASNEWMSGS;
318 /* System rooms never show up in the list. */
319 if (roombuf->QRflags2 & QR2_SYSTEM) {
320 retval = retval & ~UA_KNOWN;
324 /* Now give the caller the information it wants. */
325 if (result != NULL) *result = retval;
326 if (view != NULL) *view = vbuf.v_view;
331 * Self-checking stuff for a room record read into memory
333 void room_sanity_check(struct ctdlroom *qrbuf)
335 /* Mailbox rooms are always on the lowest floor */
336 if (qrbuf->QRflags & QR_MAILBOX) {
339 /* Listing order of 0 is illegal except for base rooms */
340 if (qrbuf->QRorder == 0)
341 if ( !(qrbuf->QRflags & QR_MAILBOX)
342 && strncasecmp(qrbuf->QRname, CtdlGetConfigStr("c_baseroom"), ROOMNAMELEN)
343 && strncasecmp(qrbuf->QRname, CtdlGetConfigStr("c_aideroom"), ROOMNAMELEN)
351 * CtdlGetRoom() - retrieve room data from disk
353 int CtdlGetRoom(struct ctdlroom *qrbuf, const char *room_name)
355 struct cdbdata *cdbqr;
356 char lowercase_name[ROOMNAMELEN];
357 char personal_lowercase_name[ROOMNAMELEN];
361 dptr = lowercase_name;
363 eptr = (dptr + (sizeof lowercase_name - 1));
364 while (!IsEmptyStr(sptr) && (dptr < eptr)){
365 *dptr = tolower(*sptr);
370 memset(qrbuf, 0, sizeof(struct ctdlroom));
372 /* First, try the public namespace */
373 cdbqr = cdb_fetch(CDB_ROOMS,
374 lowercase_name, strlen(lowercase_name));
376 /* If that didn't work, try the user's personal namespace */
378 snprintf(personal_lowercase_name,
379 sizeof personal_lowercase_name, "%010ld.%s",
380 CC->user.usernum, lowercase_name);
381 cdbqr = cdb_fetch(CDB_ROOMS,
382 personal_lowercase_name,
383 strlen(personal_lowercase_name));
386 memcpy(qrbuf, cdbqr->ptr,
387 ((cdbqr->len > sizeof(struct ctdlroom)) ?
388 sizeof(struct ctdlroom) : cdbqr->len));
391 room_sanity_check(qrbuf);
401 * CtdlGetRoomLock() - same as getroom() but locks the record (if supported)
403 int CtdlGetRoomLock(struct ctdlroom *qrbuf, const char *room_name)
406 retval = CtdlGetRoom(qrbuf, room_name);
407 if (retval == 0) begin_critical_section(S_ROOMS);
413 * b_putroom() - back end to putroom() and b_deleteroom()
414 * (if the supplied buffer is NULL, delete the room record)
416 void b_putroom(struct ctdlroom *qrbuf, char *room_name)
418 char lowercase_name[ROOMNAMELEN];
423 bptr = lowercase_name;
424 while (!IsEmptyStr(aptr))
426 *bptr = tolower(*aptr);
432 len = bptr - lowercase_name;
434 cdb_delete(CDB_ROOMS, lowercase_name, len);
436 time(&qrbuf->QRmtime);
437 cdb_store(CDB_ROOMS, lowercase_name, len, qrbuf, sizeof(struct ctdlroom));
443 * CtdlPutRoom() - store room data to disk
445 void CtdlPutRoom(struct ctdlroom *qrbuf) {
446 b_putroom(qrbuf, qrbuf->QRname);
451 * b_deleteroom() - delete a room record from disk
453 void b_deleteroom(char *room_name) {
454 b_putroom(NULL, room_name);
459 * CtdlPutRoomLock() - same as CtdlPutRoom() but unlocks the record (if supported)
461 void CtdlPutRoomLock(struct ctdlroom *qrbuf)
464 end_critical_section(S_ROOMS);
469 * CtdlGetFloorByName() - retrieve the number of the named floor
470 * return < 0 if not found else return floor number
472 int CtdlGetFloorByName(const char *floor_name)
475 struct floor *flbuf = NULL;
477 for (a = 0; a < MAXFLOORS; ++a) {
478 flbuf = CtdlGetCachedFloor(a);
480 /* check to see if it already exists */
481 if ((!strcasecmp(flbuf->f_name, floor_name))
482 && (flbuf->f_flags & F_INUSE)) {
491 * CtdlGetFloorByNameLock() - retrieve floor number for given floor and lock the floor list.
493 int CtdlGetFloorByNameLock(const char *floor_name)
495 begin_critical_section(S_FLOORTAB);
496 return CtdlGetFloorByName(floor_name);
502 * CtdlGetAvailableFloor() - Return number of first unused floor
503 * return < 0 if none available
505 int CtdlGetAvailableFloor(void)
508 struct floor *flbuf = NULL;
510 for (a = 0; a < MAXFLOORS; a++) {
511 flbuf = CtdlGetCachedFloor(a);
513 /* check to see if it already exists */
514 if ((flbuf->f_flags & F_INUSE) == 0) {
523 * CtdlGetFloor() - retrieve floor data from disk
525 void CtdlGetFloor(struct floor *flbuf, int floor_num)
527 struct cdbdata *cdbfl;
529 memset(flbuf, 0, sizeof(struct floor));
530 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
532 memcpy(flbuf, cdbfl->ptr,
533 ((cdbfl->len > sizeof(struct floor)) ?
534 sizeof(struct floor) : cdbfl->len));
537 if (floor_num == 0) {
538 safestrncpy(flbuf->f_name, "Main Floor",
539 sizeof flbuf->f_name);
540 flbuf->f_flags = F_INUSE;
541 flbuf->f_ref_count = 3;
549 * lgetfloor() - same as CtdlGetFloor() but locks the record (if supported)
551 void lgetfloor(struct floor *flbuf, int floor_num)
553 begin_critical_section(S_FLOORTAB);
554 CtdlGetFloor(flbuf, floor_num);
559 * CtdlGetCachedFloor() - Get floor record from *cache* (loads from disk if needed)
561 * This is strictly a performance hack.
563 struct floor *CtdlGetCachedFloor(int floor_num) {
564 static int initialized = 0;
567 struct floor *fl = NULL;
569 begin_critical_section(S_FLOORCACHE);
570 if (initialized == 0) {
571 for (i=0; i<MAXFLOORS; ++i) {
572 floorcache[floor_num] = NULL;
576 if (floorcache[floor_num] == NULL) {
579 end_critical_section(S_FLOORCACHE);
582 fl = malloc(sizeof(struct floor));
583 CtdlGetFloor(fl, floor_num);
584 begin_critical_section(S_FLOORCACHE);
585 if (floorcache[floor_num] != NULL) {
586 free(floorcache[floor_num]);
588 floorcache[floor_num] = fl;
589 end_critical_section(S_FLOORCACHE);
592 return(floorcache[floor_num]);
597 * CtdlPutFloor() - store floor data on disk
599 void CtdlPutFloor(struct floor *flbuf, int floor_num)
601 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
602 begin_critical_section(S_FLOORCACHE);
603 if (floorcache[floor_num] != NULL) {
604 free(floorcache[floor_num]);
605 floorcache[floor_num] = malloc(sizeof(struct floor));
606 memcpy(floorcache[floor_num], flbuf, sizeof(struct floor));
608 end_critical_section(S_FLOORCACHE);
610 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
611 flbuf, sizeof(struct floor));
616 * CtdlPutFloorLock() - same as CtdlPutFloor() but unlocks the record (if supported)
618 void CtdlPutFloorLock(struct floor *flbuf, int floor_num)
620 CtdlPutFloor(flbuf, floor_num);
621 end_critical_section(S_FLOORTAB);
627 * lputfloor() - same as CtdlPutFloor() but unlocks the record (if supported)
629 void lputfloor(struct floor *flbuf, int floor_num)
631 CtdlPutFloorLock(flbuf, floor_num);
636 * Iterate through the room table, performing a callback for each room.
638 void CtdlForEachRoom(ForEachRoomCallBack CB, void *in_data)
640 struct ctdlroom qrbuf;
641 struct cdbdata *cdbqr;
643 cdb_rewind(CDB_ROOMS);
645 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
646 memset(&qrbuf, 0, sizeof(struct ctdlroom));
647 memcpy(&qrbuf, cdbqr->ptr,
648 ((cdbqr->len > sizeof(struct ctdlroom)) ?
649 sizeof(struct ctdlroom) : cdbqr->len)
652 room_sanity_check(&qrbuf);
653 if (qrbuf.QRflags & QR_INUSE) {
660 * Iterate through the room table, performing a callback for each room that has a netconfig entry.
662 void CtdlForEachNetCfgRoom(ForEachRoomNetCfgCallBack CB, void *in_data)
664 struct ctdlroom qrbuf;
665 struct cdbdata *cdbqr;
667 cdb_rewind(CDB_ROOMS);
669 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
670 memset(&qrbuf, 0, sizeof(struct ctdlroom));
671 memcpy(&qrbuf, cdbqr->ptr, ((cdbqr->len > sizeof(struct ctdlroom)) ? sizeof(struct ctdlroom) : cdbqr->len));
673 room_sanity_check(&qrbuf);
674 if (qrbuf.QRflags & QR_INUSE)
676 OneRoomNetCfg *RNCfg;
677 RNCfg = CtdlGetNetCfgForRoom(qrbuf.QRnumber);
681 CB(&qrbuf, in_data, RNCfg);
682 FreeRoomNetworkStruct(&RNCfg);
694 * delete_msglist() - delete room message pointers
696 void delete_msglist(struct ctdlroom *whichroom)
698 struct cdbdata *cdbml;
700 /* Make sure the msglist we're deleting actually exists, otherwise
701 * libdb will complain when we try to delete an invalid record
703 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
707 /* Go ahead and delete it */
708 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
714 * Message pointer compare function for sort_msglist()
716 int sort_msglist_cmp(const void *m1, const void *m2) {
717 if ((*(const long *)m1) > (*(const long *)m2)) return(1);
718 if ((*(const long *)m1) < (*(const long *)m2)) return(-1);
724 * sort message pointers
725 * (returns new msg count)
727 int sort_msglist(long listptrs[], int oldcount)
738 qsort(listptrs, numitems, sizeof(long), sort_msglist_cmp);
740 /* and yank any nulls */
741 while ((i < numitems) && (listptrs[i] == 0L)) i++;
745 memmove(&listptrs[0], &listptrs[i], (sizeof(long) * (numitems - i)));
754 * Determine whether a given room is non-editable.
756 int CtdlIsNonEditable(struct ctdlroom *qrbuf)
759 /* Mail> rooms are non-editable */
760 if ( (qrbuf->QRflags & QR_MAILBOX)
761 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
764 /* Everything else is editable */
771 * Make the specified room the current room for this session. No validation
772 * or access control is done here -- the caller should make sure that the
773 * specified room exists and is ok to access.
775 void CtdlUserGoto(char *where, int display_result, int transiently,
776 int *retmsgs, int *retnew, long *retoldest, long *retnewest)
778 struct CitContext *CCC = CC;
780 int new_messages = 0;
781 int old_messages = 0;
782 int total_messages = 0;
783 long oldest_message = 0;
784 long newest_message = 0;
788 int newmailcount = 0;
790 char truncated_roomname[ROOMNAMELEN];
791 struct cdbdata *cdbfr;
792 long *msglist = NULL;
794 unsigned int original_v_flags;
797 char setstr[128], lostr[64], histr[64];
801 /* If the supplied room name is NULL, the caller wants us to know that
802 * it has already copied the room record into CC->room, so
803 * we can skip the extra database fetch.
806 safestrncpy(CCC->room.QRname, where, sizeof CCC->room.QRname);
807 CtdlGetRoom(&CCC->room, where);
810 /* Take care of all the formalities. */
812 begin_critical_section(S_USERS);
813 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
814 original_v_flags = vbuf.v_flags;
816 /* Know the room ... but not if it's the page log room, or if the
817 * caller specified that we're only entering this room transiently.
819 if ((strcasecmp(CCC->room.QRname, CtdlGetConfigStr("c_logpages"))) && (transiently == 0))
821 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
822 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
825 /* Only rewrite the database record if we changed something */
826 if (vbuf.v_flags != original_v_flags) {
827 CtdlSetRelationship(&vbuf, &CCC->user, &CCC->room);
829 end_critical_section(S_USERS);
831 /* Check for new mail */
832 newmailcount = NewMailCount();
834 /* set info to 1 if the user needs to read the room's info file */
835 if (CCC->room.QRinfo > vbuf.v_lastseen) {
839 cdbfr = cdb_fetch(CDB_MSGLISTS, &CCC->room.QRnumber, sizeof(long));
841 msglist = (long *) cdbfr->ptr;
842 cdbfr->ptr = NULL; /* CtdlUserGoto() now owns this memory */
843 num_msgs = cdbfr->len / sizeof(long);
848 for (a=0; a<num_msgs; ++a) {
849 if (msglist[a] > 0L) ++total_messages;
852 if (total_messages > 0) {
853 oldest_message = msglist[0];
854 newest_message = msglist[num_msgs - 1];
857 num_sets = num_tokens(vbuf.v_seen, ',');
858 for (s=0; s<num_sets; ++s) {
859 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
861 extract_token(lostr, setstr, 0, ':', sizeof lostr);
862 if (num_tokens(setstr, ':') >= 2) {
863 extract_token(histr, setstr, 1, ':', sizeof histr);
864 if (!strcmp(histr, "*")) {
865 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
869 strcpy(histr, lostr);
874 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
875 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
881 new_messages = total_messages - old_messages;
883 if (msglist != NULL) free(msglist);
885 if (CCC->room.QRflags & QR_MAILBOX)
890 if ((CCC->room.QRroomaide == CCC->user.usernum)
891 || (CCC->user.axlevel >= AxAideU))
896 safestrncpy(truncated_roomname, CCC->room.QRname, sizeof truncated_roomname);
897 if ( (CCC->room.QRflags & QR_MAILBOX)
898 && (atol(CCC->room.QRname) == CCC->user.usernum) ) {
899 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
902 if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
906 if (retmsgs != NULL) *retmsgs = total_messages;
907 if (retnew != NULL) *retnew = new_messages;
908 if (retoldest != NULL) *retoldest = oldest_message;
909 if (retnewest != NULL) *retnewest = newest_message;
910 MSG_syslog(LOG_DEBUG, "<%s> %d new of %d total messages, oldest=%ld, newest=%ld",
911 CCC->room.QRname, new_messages, total_messages, oldest_message, newest_message
914 CCC->curr_view = (int)vbuf.v_view;
916 if (display_result) {
917 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|%d|%ld|\n",
918 CIT_OK, CtdlCheckExpress(),
923 (int)CCC->room.QRflags,
924 (long)CCC->room.QRhighest,
925 (long)vbuf.v_lastseen,
929 (int)CCC->room.QRfloor,
931 (int)CCC->room.QRdefaultview,
933 (int)CCC->room.QRflags2,
934 (long)CCC->room.QRmtime
941 * Handle some of the macro named rooms
943 void convert_room_name_macros(char *towhere, size_t maxlen) {
944 if (!strcasecmp(towhere, "_BASEROOM_")) {
945 safestrncpy(towhere, CtdlGetConfigStr("c_baseroom"), maxlen);
947 else if (!strcasecmp(towhere, "_MAIL_")) {
948 safestrncpy(towhere, MAILROOM, maxlen);
950 else if (!strcasecmp(towhere, "_TRASH_")) {
951 safestrncpy(towhere, USERTRASHROOM, maxlen);
953 else if (!strcasecmp(towhere, "_DRAFTS_")) {
954 safestrncpy(towhere, USERDRAFTROOM, maxlen);
956 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
957 safestrncpy(towhere, CtdlGetConfigStr("c_twitroom"), maxlen);
959 else if (!strcasecmp(towhere, "_CALENDAR_")) {
960 safestrncpy(towhere, USERCALENDARROOM, maxlen);
962 else if (!strcasecmp(towhere, "_TASKS_")) {
963 safestrncpy(towhere, USERTASKSROOM, maxlen);
965 else if (!strcasecmp(towhere, "_CONTACTS_")) {
966 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
968 else if (!strcasecmp(towhere, "_NOTES_")) {
969 safestrncpy(towhere, USERNOTESROOM, maxlen);
975 * Back end function to rename a room.
976 * You can also specify which floor to move the room to, or specify -1 to
977 * keep the room on the same floor it was on.
979 * If you are renaming a mailbox room, you must supply the namespace prefix
980 * in *at least* the old name!
982 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
983 struct CitContext *CCC = CC;
985 struct ctdlroom qrbuf;
986 struct ctdlroom qrtmp;
991 char actual_old_name[ROOMNAMELEN];
993 MSG_syslog(LOG_DEBUG, "CtdlRenameRoom(%s, %s, %d)", old_name, new_name, new_floor);
995 if (new_floor >= 0) {
996 fl = CtdlGetCachedFloor(new_floor);
997 if ((fl->f_flags & F_INUSE) == 0) {
998 return(crr_invalid_floor);
1002 begin_critical_section(S_ROOMS);
1004 if ( (CtdlGetRoom(&qrtmp, new_name) == 0)
1005 && (strcasecmp(new_name, old_name)) ) {
1006 ret = crr_already_exists;
1009 else if (CtdlGetRoom(&qrbuf, old_name) != 0) {
1010 ret = crr_room_not_found;
1013 else if ( (CCC->user.axlevel < AxAideU) && (!CCC->internal_pgm)
1014 && (CCC->user.usernum != qrbuf.QRroomaide)
1015 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CCC->user.usernum))) ) {
1016 ret = crr_access_denied;
1019 else if (CtdlIsNonEditable(&qrbuf)) {
1020 ret = crr_noneditable;
1025 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1026 if (qrbuf.QRflags & QR_MAILBOX) {
1027 owner = atol(qrbuf.QRname);
1029 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1030 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1031 "%010ld.%s", owner, new_name);
1034 safestrncpy(qrbuf.QRname, new_name,
1035 sizeof(qrbuf.QRname));
1038 /* Reject change of floor for baseroom/aideroom */
1039 if (!strncasecmp(old_name, CtdlGetConfigStr("c_baseroom"), ROOMNAMELEN) ||
1040 !strncasecmp(old_name, CtdlGetConfigStr("c_aideroom"), ROOMNAMELEN))
1045 /* Take care of floor stuff */
1046 old_floor = qrbuf.QRfloor;
1047 if (new_floor < 0) {
1048 new_floor = old_floor;
1050 qrbuf.QRfloor = new_floor;
1051 CtdlPutRoom(&qrbuf);
1053 begin_critical_section(S_CONFIG);
1055 /* If baseroom/aideroom name changes, update config */
1056 if (!strncasecmp(old_name, CtdlGetConfigStr("c_baseroom"), ROOMNAMELEN)) {
1057 CtdlSetConfigStr("c_baseroom", new_name);
1059 if (!strncasecmp(old_name, CtdlGetConfigStr("c_aideroom"), ROOMNAMELEN)) {
1060 CtdlSetConfigStr("c_aideroom", new_name);
1063 end_critical_section(S_CONFIG);
1065 /* If the room name changed, then there are now two room
1066 * records, so we have to delete the old one.
1068 if (strcasecmp(new_name, old_name)) {
1069 b_deleteroom(actual_old_name);
1075 end_critical_section(S_ROOMS);
1077 /* Adjust the floor reference counts if necessary */
1078 if (new_floor != old_floor) {
1079 lgetfloor(&flbuf, old_floor);
1080 --flbuf.f_ref_count;
1081 lputfloor(&flbuf, old_floor);
1082 MSG_syslog(LOG_DEBUG, "Reference count for floor %d is now %d", old_floor, flbuf.f_ref_count);
1083 lgetfloor(&flbuf, new_floor);
1084 ++flbuf.f_ref_count;
1085 lputfloor(&flbuf, new_floor);
1086 MSG_syslog(LOG_DEBUG, "Reference count for floor %d is now %d", new_floor, flbuf.f_ref_count);
1089 /* ...and everybody say "YATTA!" */
1095 * Asynchronously schedule a room for deletion. By placing the room into an invalid private namespace,
1096 * the room will appear deleted to the user(s), but the session doesn't need to block while waiting for
1097 * database operations to complete. Instead, the room gets purged when THE DREADED AUTO-PURGER makes
1098 * its next run. Aren't we so clever?!!
1100 void CtdlScheduleRoomForDeletion(struct ctdlroom *qrbuf)
1102 char old_name[ROOMNAMELEN];
1105 syslog(LOG_NOTICE, "Scheduling room <%s> for deletion", qrbuf->QRname);
1107 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1108 CtdlGetRoom(qrbuf, qrbuf->QRname);
1110 /* Turn the room into a private mailbox owned by a user who doesn't
1111 * exist. This will immediately make the room invisible to everyone,
1112 * and qualify the room for purging.
1114 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1119 qrbuf->QRflags |= QR_MAILBOX;
1120 time(&qrbuf->QRgen); /* Use a timestamp as the new generation number */
1122 b_deleteroom(old_name);
1128 * Back end processing to delete a room and everything associated with it
1129 * (This one is synchronous and should only get called by THE DREADED
1130 * AUTO-PURGER in serv_expire.c. All user-facing code should call
1131 * the asynchronous schedule_room_for_deletion() instead.)
1133 void CtdlDeleteRoom(struct ctdlroom *qrbuf)
1136 char filename[PATH_MAX];
1137 char configdbkeyname[25];
1139 syslog(LOG_NOTICE, "Deleting room <%s>", qrbuf->QRname);
1141 /* Delete the info file */
1142 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_info_dir);
1145 /* Delete the image file */
1146 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_image_dir);
1149 /* Delete the room's network configdb entry */
1150 netcfg_keyname(configdbkeyname, qrbuf->QRnumber);
1151 CtdlDelConfig(configdbkeyname);
1153 /* Delete the messages in the room
1154 * (Careful: this opens an S_ROOMS critical section!)
1156 CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
1158 /* Flag the room record as not in use */
1159 CtdlGetRoomLock(qrbuf, qrbuf->QRname);
1161 CtdlPutRoomLock(qrbuf);
1163 /* then decrement the reference count for the floor */
1164 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1165 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1166 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1168 /* Delete the room record from the database! */
1169 b_deleteroom(qrbuf->QRname);
1174 * Check access control for deleting a room
1176 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1178 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1182 if (CtdlIsNonEditable(qr)) {
1187 * For mailboxes, check stuff
1189 if (qr->QRflags & QR_MAILBOX) {
1191 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1193 if (atol(qr->QRname) != CC->user.usernum) {
1194 return(0); /* not my room */
1197 /* Can't delete your Mail> room */
1198 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1200 /* Otherwise it's ok */
1205 * For normal rooms, just check for admin or room admin status.
1207 return(is_room_aide());
1213 * Internal code to create a new room (returns room flags)
1215 * Room types: 0=public, 1=hidden, 2=passworded, 3=invitation-only,
1216 * 4=mailbox, 5=mailbox, but caller supplies namespace
1218 unsigned CtdlCreateRoom(char *new_room_name,
1220 char *new_room_pass,
1227 struct ctdlroom qrbuf;
1231 MARK_syslog(LOG_DEBUG, "CtdlCreateRoom(name=%s, type=%d, view=%d)", new_room_name, new_room_type, new_room_view);
1233 if (CtdlGetRoom(&qrbuf, new_room_name) == 0) {
1234 MARK_syslog(LOG_DEBUG, "Cannot create room <%s> - already exists", new_room_name);
1238 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1239 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1240 qrbuf.QRflags = QR_INUSE;
1241 if (new_room_type > 0)
1242 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1243 if (new_room_type == 1)
1244 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1245 if (new_room_type == 2)
1246 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1247 if ( (new_room_type == 4) || (new_room_type == 5) ) {
1248 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1249 /* qrbuf.QRflags2 |= QR2_SUBJECTREQ; */
1252 /* If the user is requesting a personal room, set up the room
1253 * name accordingly (prepend the user number)
1255 if (new_room_type == 4) {
1256 CtdlMailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1259 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1262 /* If the room is private, and the system administrator has elected
1263 * to automatically grant room admin privileges, do so now.
1265 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1266 qrbuf.QRroomaide = CC->user.usernum;
1268 /* Blog owners automatically become room admins of their blogs.
1269 * (In the future we will offer a site-wide configuration setting to suppress this behavior.)
1271 else if (new_room_view == VIEW_BLOG) {
1272 qrbuf.QRroomaide = CC->user.usernum;
1274 /* Otherwise, set the room admin to undefined.
1277 qrbuf.QRroomaide = (-1L);
1281 * If the caller is only interested in testing whether this will work,
1282 * return now without creating the room.
1284 if (!really_create) return (qrbuf.QRflags);
1286 qrbuf.QRnumber = get_new_room_number();
1287 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1288 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1289 qrbuf.QRfloor = new_room_floor;
1290 qrbuf.QRdefaultview = new_room_view;
1292 /* save what we just did... */
1293 CtdlPutRoom(&qrbuf);
1295 /* bump the reference count on whatever floor the room is on */
1296 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1297 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1298 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1300 /* Grant the creator access to the room unless the avoid_access
1301 * parameter was specified.
1303 if ( (CC->logged_in) && (avoid_access == 0) ) {
1304 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1305 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1306 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1307 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1310 /* resume our happy day */
1311 return (qrbuf.QRflags);