2 * Server functions which perform operations on room objects.
4 * Copyright (c) 1987-2012 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.
22 #include <dirent.h> /* for cmd_rdir to read contents of the directory */
24 #if TIME_WITH_SYS_TIME
25 # include <sys/time.h>
29 # include <sys/time.h>
38 #include <libcitadel.h>
43 #include "sysdep_decls.h"
46 #include "citserver.h"
48 #include "citadel_dirs.h"
51 #include "ctdl_module.h"
54 struct floor *floorcache[MAXFLOORS];
57 * Determine whether the currently logged in session has permission to read
58 * messages in the current room.
60 int CtdlDoIHavePermissionToReadMessagesInThisRoom(void) {
61 if ( (!(CC->logged_in))
62 && (!(CC->internal_pgm))
63 && (!config.c_guest_logins)
65 return(om_not_logged_in);
71 * Check to see whether we have permission to post a message in the current
72 * room. Returns a *CITADEL ERROR CODE* and puts a message in errmsgbuf, or
73 * returns 0 on success.
75 int CtdlDoIHavePermissionToPostInThisRoom(
78 const char* RemoteIdentifier,
84 if (!(CC->logged_in) &&
85 (PostPublic == POST_LOGGED_IN)) {
86 snprintf(errmsgbuf, n, "Not logged in.");
87 return (ERROR + NOT_LOGGED_IN);
89 else if (PostPublic == CHECK_EXISTANCE) {
90 return (0); // We're Evaling whether a recipient exists
92 else if (!(CC->logged_in)) {
94 if ((CC->room.QRflags & QR_READONLY)) {
95 snprintf(errmsgbuf, n, "Not logged in.");
96 return (ERROR + NOT_LOGGED_IN);
98 if (CC->room.QRflags2 & QR2_MODERATED) {
99 snprintf(errmsgbuf, n, "Not logged in Moderation feature not yet implemented!");
100 return (ERROR + NOT_LOGGED_IN);
102 if ((PostPublic!=POST_LMTP) &&(CC->room.QRflags2 & QR2_SMTP_PUBLIC) == 0) {
104 return CtdlNetconfigCheckRoomaccess(errmsgbuf, n, RemoteIdentifier);
110 if ((CC->user.axlevel < AxProbU)
111 && ((CC->room.QRflags & QR_MAILBOX) == 0)) {
112 snprintf(errmsgbuf, n, "Need to be validated to enter (except in %s> to sysop)", MAILROOM);
113 return (ERROR + HIGHER_ACCESS_REQUIRED);
116 CtdlRoomAccess(&CC->room, &CC->user, &ra, NULL);
118 if (ra & UA_POSTALLOWED) {
119 strcpy(errmsgbuf, "OK to post or reply here");
123 if ( (ra & UA_REPLYALLOWED) && (is_reply) ) {
125 * To be thorough, we ought to check to see if the message they are
126 * replying to is actually a valid one in this room, but unless this
127 * actually becomes a problem we'll go with high performance instead.
129 strcpy(errmsgbuf, "OK to reply here");
133 if ( (ra & UA_REPLYALLOWED) && (!is_reply) ) {
134 /* Clarify what happened with a better error message */
135 snprintf(errmsgbuf, n, "You may only reply to existing messages here.");
136 return (ERROR + HIGHER_ACCESS_REQUIRED);
139 snprintf(errmsgbuf, n, "Higher access is required to post in this room.");
140 return (ERROR + HIGHER_ACCESS_REQUIRED);
145 * Check whether the current user has permission to delete messages from
146 * the current room (returns 1 for yes, 0 for no)
148 int CtdlDoIHavePermissionToDeleteMessagesFromThisRoom(void) {
150 CtdlRoomAccess(&CC->room, &CC->user, &ra, NULL);
151 if (ra & UA_DELETEALLOWED) return(1);
156 * Retrieve access control information for any user/room pair
158 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf,
159 int *result, int *view)
166 if (userbuf == &CC->user) {
170 if ((is_me) && (config.c_guest_logins) && (!CC->logged_in)) {
174 /* for internal programs, always do everything */
175 if (((CC->internal_pgm)) && (roombuf->QRflags & QR_INUSE)) {
176 retval = (UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED);
178 goto SKIP_EVERYTHING;
181 /* If guest mode is enabled, always grant access to the Lobby */
182 if ((is_guest) && (!strcasecmp(roombuf->QRname, BASEROOM))) {
183 retval = (UA_KNOWN | UA_GOTOALLOWED);
185 goto SKIP_EVERYTHING;
188 /* Locate any applicable user/room relationships */
190 memset(&vbuf, 0, sizeof vbuf);
193 CtdlGetRelationship(&vbuf, userbuf, roombuf);
196 /* Force the properties of the Aide room */
197 if (!strcasecmp(roombuf->QRname, config.c_aideroom)) {
198 if (userbuf->axlevel >= AxAideU) {
199 retval = UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED;
206 /* If this is a public room, it's accessible... */
207 if ( ((roombuf->QRflags & QR_PRIVATE) == 0)
208 && ((roombuf->QRflags & QR_MAILBOX) == 0)
210 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
213 /* If this is a preferred users only room, check access level */
214 if (roombuf->QRflags & QR_PREFONLY) {
215 if (userbuf->axlevel < AxPrefU) {
216 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
220 /* For private rooms, check the generation number matchups */
221 if ( (roombuf->QRflags & QR_PRIVATE)
222 && ((roombuf->QRflags & QR_MAILBOX) == 0)
225 /* An explicit match means the user belongs in this room */
226 if (vbuf.v_flags & V_ACCESS) {
227 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
229 /* Otherwise, check if this is a guess-name or passworded
230 * room. If it is, a goto may at least be attempted
232 else if ( (roombuf->QRflags & QR_PRIVATE)
233 || (roombuf->QRflags & QR_PASSWORDED)
235 retval = retval & ~UA_KNOWN;
236 retval = retval | UA_GOTOALLOWED;
240 /* For mailbox rooms, also check the namespace */
241 /* Also, mailbox owners can delete their messages */
242 if ( (roombuf->QRflags & QR_MAILBOX) && (atol(roombuf->QRname) != 0)) {
243 if (userbuf->usernum == atol(roombuf->QRname)) {
244 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED;
246 /* An explicit match means the user belongs in this room */
247 if (vbuf.v_flags & V_ACCESS) {
248 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED | UA_REPLYALLOWED;
252 /* For non-mailbox rooms... */
255 /* User is allowed to post in the room unless:
256 * - User is not validated
257 * - User has no net privileges and it is a shared network room
258 * - It is a read-only room
259 * - It is a blog room (in which case we only allow replies to existing messages)
261 int post_allowed = 1;
262 int reply_allowed = 1;
263 if (userbuf->axlevel < AxProbU) {
267 if ((userbuf->axlevel < AxNetU) && (roombuf->QRflags & QR_NETWORK)) {
271 if (roombuf->QRflags & QR_READONLY) {
275 if (roombuf->QRdefaultview == VIEW_BLOG) {
279 retval = retval | UA_POSTALLOWED | UA_REPLYALLOWED;
282 retval = retval | UA_REPLYALLOWED;
285 /* If "collaborative deletion" is active for this room, any user who can post
286 * is also allowed to delete
288 if (roombuf->QRflags2 & QR2_COLLABDEL) {
289 if (retval & UA_POSTALLOWED) {
290 retval = retval | UA_DELETEALLOWED;
296 /* Check to see if the user has forgotten this room */
297 if (vbuf.v_flags & V_FORGET) {
298 retval = retval & ~UA_KNOWN;
299 if ( ( ((roombuf->QRflags & QR_PRIVATE) == 0)
300 && ((roombuf->QRflags & QR_MAILBOX) == 0)
301 ) || ( (roombuf->QRflags & QR_MAILBOX)
302 && (atol(roombuf->QRname) == CC->user.usernum))
304 retval = retval | UA_ZAPPED;
308 /* If user is explicitly locked out of this room, deny everything */
309 if (vbuf.v_flags & V_LOCKOUT) {
310 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED & ~UA_POSTALLOWED & ~UA_REPLYALLOWED;
313 /* Aides get access to all private rooms */
314 if ( (userbuf->axlevel >= AxAideU)
315 && ((roombuf->QRflags & QR_MAILBOX) == 0)
317 if (vbuf.v_flags & V_FORGET) {
318 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
321 retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
325 /* Aides can gain access to mailboxes as well, but they don't show
328 if ( (userbuf->axlevel >= AxAideU)
329 && (roombuf->QRflags & QR_MAILBOX)
331 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
334 /* Aides and Room Aides have admin privileges */
335 if ( (userbuf->axlevel >= AxAideU)
336 || (userbuf->usernum == roombuf->QRroomaide)
338 retval = retval | UA_ADMINALLOWED | UA_DELETEALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
341 NEWMSG: /* By the way, we also check for the presence of new messages */
342 if (is_msg_in_sequence_set(vbuf.v_seen, roombuf->QRhighest) == 0) {
343 retval = retval | UA_HASNEWMSGS;
346 /* System rooms never show up in the list. */
347 if (roombuf->QRflags2 & QR2_SYSTEM) {
348 retval = retval & ~UA_KNOWN;
352 /* Now give the caller the information it wants. */
353 if (result != NULL) *result = retval;
354 if (view != NULL) *view = vbuf.v_view;
359 * Self-checking stuff for a room record read into memory
361 void room_sanity_check(struct ctdlroom *qrbuf)
363 /* Mailbox rooms are always on the lowest floor */
364 if (qrbuf->QRflags & QR_MAILBOX) {
367 /* Listing order of 0 is illegal except for base rooms */
368 if (qrbuf->QRorder == 0)
369 if (!(qrbuf->QRflags & QR_MAILBOX) &&
370 strncasecmp(qrbuf->QRname, config.c_baseroom, ROOMNAMELEN)
372 strncasecmp(qrbuf->QRname, config.c_aideroom, ROOMNAMELEN))
378 * CtdlGetRoom() - retrieve room data from disk
380 int CtdlGetRoom(struct ctdlroom *qrbuf, const char *room_name)
382 struct cdbdata *cdbqr;
383 char lowercase_name[ROOMNAMELEN];
384 char personal_lowercase_name[ROOMNAMELEN];
388 dptr = lowercase_name;
390 eptr = (dptr + (sizeof lowercase_name - 1));
391 while (!IsEmptyStr(sptr) && (dptr < eptr)){
392 *dptr = tolower(*sptr);
397 memset(qrbuf, 0, sizeof(struct ctdlroom));
399 /* First, try the public namespace */
400 cdbqr = cdb_fetch(CDB_ROOMS,
401 lowercase_name, strlen(lowercase_name));
403 /* If that didn't work, try the user's personal namespace */
405 snprintf(personal_lowercase_name,
406 sizeof personal_lowercase_name, "%010ld.%s",
407 CC->user.usernum, lowercase_name);
408 cdbqr = cdb_fetch(CDB_ROOMS,
409 personal_lowercase_name,
410 strlen(personal_lowercase_name));
413 memcpy(qrbuf, cdbqr->ptr,
414 ((cdbqr->len > sizeof(struct ctdlroom)) ?
415 sizeof(struct ctdlroom) : cdbqr->len));
418 room_sanity_check(qrbuf);
427 * CtdlGetRoomLock() - same as getroom() but locks the record (if supported)
429 int CtdlGetRoomLock(struct ctdlroom *qrbuf, char *room_name)
432 retval = CtdlGetRoom(qrbuf, room_name);
433 if (retval == 0) begin_critical_section(S_ROOMS);
439 * b_putroom() - back end to putroom() and b_deleteroom()
440 * (if the supplied buffer is NULL, delete the room record)
442 void b_putroom(struct ctdlroom *qrbuf, char *room_name)
444 char lowercase_name[ROOMNAMELEN];
449 bptr = lowercase_name;
450 while (!IsEmptyStr(aptr))
452 *bptr = tolower(*aptr);
458 len = bptr - lowercase_name;
460 cdb_delete(CDB_ROOMS, lowercase_name, len);
462 time(&qrbuf->QRmtime);
463 cdb_store(CDB_ROOMS, lowercase_name, len, qrbuf, sizeof(struct ctdlroom));
469 * CtdlPutRoom() - store room data to disk
471 void CtdlPutRoom(struct ctdlroom *qrbuf) {
472 b_putroom(qrbuf, qrbuf->QRname);
477 * b_deleteroom() - delete a room record from disk
479 void b_deleteroom(char *room_name) {
480 b_putroom(NULL, room_name);
485 * CtdlPutRoomLock() - same as CtdlPutRoom() but unlocks the record (if supported)
487 void CtdlPutRoomLock(struct ctdlroom *qrbuf)
491 end_critical_section(S_ROOMS);
497 * CtdlGetFloorByName() - retrieve the number of the named floor
498 * return < 0 if not found else return floor number
500 int CtdlGetFloorByName(const char *floor_name)
503 struct floor *flbuf = NULL;
505 for (a = 0; a < MAXFLOORS; ++a) {
506 flbuf = CtdlGetCachedFloor(a);
508 /* check to see if it already exists */
509 if ((!strcasecmp(flbuf->f_name, floor_name))
510 && (flbuf->f_flags & F_INUSE)) {
519 * CtdlGetFloorByNameLock() - retrieve floor number for given floor and lock the floor list.
521 int CtdlGetFloorByNameLock(const char *floor_name)
523 begin_critical_section(S_FLOORTAB);
524 return CtdlGetFloorByName(floor_name);
530 * CtdlGetAvailableFloor() - Return number of first unused floor
531 * return < 0 if none available
533 int CtdlGetAvailableFloor(void)
536 struct floor *flbuf = NULL;
538 for (a = 0; a < MAXFLOORS; a++) {
539 flbuf = CtdlGetCachedFloor(a);
541 /* check to see if it already exists */
542 if ((flbuf->f_flags & F_INUSE) == 0) {
551 * CtdlGetFloor() - retrieve floor data from disk
553 void CtdlGetFloor(struct floor *flbuf, int floor_num)
555 struct cdbdata *cdbfl;
557 memset(flbuf, 0, sizeof(struct floor));
558 cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
560 memcpy(flbuf, cdbfl->ptr,
561 ((cdbfl->len > sizeof(struct floor)) ?
562 sizeof(struct floor) : cdbfl->len));
565 if (floor_num == 0) {
566 safestrncpy(flbuf->f_name, "Main Floor",
567 sizeof flbuf->f_name);
568 flbuf->f_flags = F_INUSE;
569 flbuf->f_ref_count = 3;
577 * lgetfloor() - same as CtdlGetFloor() but locks the record (if supported)
579 void lgetfloor(struct floor *flbuf, int floor_num)
582 begin_critical_section(S_FLOORTAB);
583 CtdlGetFloor(flbuf, floor_num);
588 * CtdlGetCachedFloor() - Get floor record from *cache* (loads from disk if needed)
590 * This is strictly a performance hack.
592 struct floor *CtdlGetCachedFloor(int floor_num) {
593 static int initialized = 0;
596 struct floor *fl = NULL;
598 begin_critical_section(S_FLOORCACHE);
599 if (initialized == 0) {
600 for (i=0; i<MAXFLOORS; ++i) {
601 floorcache[floor_num] = NULL;
605 if (floorcache[floor_num] == NULL) {
608 end_critical_section(S_FLOORCACHE);
611 fl = malloc(sizeof(struct floor));
612 CtdlGetFloor(fl, floor_num);
613 begin_critical_section(S_FLOORCACHE);
614 if (floorcache[floor_num] != NULL) {
615 free(floorcache[floor_num]);
617 floorcache[floor_num] = fl;
618 end_critical_section(S_FLOORCACHE);
621 return(floorcache[floor_num]);
626 * CtdlPutFloor() - store floor data on disk
628 void CtdlPutFloor(struct floor *flbuf, int floor_num)
630 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
631 begin_critical_section(S_FLOORCACHE);
632 if (floorcache[floor_num] != NULL) {
633 free(floorcache[floor_num]);
634 floorcache[floor_num] = malloc(sizeof(struct floor));
635 memcpy(floorcache[floor_num], flbuf, sizeof(struct floor));
637 end_critical_section(S_FLOORCACHE);
639 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
640 flbuf, sizeof(struct floor));
645 * CtdlPutFloorLock() - same as CtdlPutFloor() but unlocks the record (if supported)
647 void CtdlPutFloorLock(struct floor *flbuf, int floor_num)
650 CtdlPutFloor(flbuf, floor_num);
651 end_critical_section(S_FLOORTAB);
657 * lputfloor() - same as CtdlPutFloor() but unlocks the record (if supported)
659 void lputfloor(struct floor *flbuf, int floor_num)
661 CtdlPutFloorLock(flbuf, floor_num);
666 * Traverse the room file...
668 void CtdlForEachRoom(ForEachRoomCallBack CB, void *in_data)
670 struct ctdlroom qrbuf;
671 struct cdbdata *cdbqr;
673 cdb_rewind(CDB_ROOMS);
675 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
676 memset(&qrbuf, 0, sizeof(struct ctdlroom));
677 memcpy(&qrbuf, cdbqr->ptr,
678 ((cdbqr->len > sizeof(struct ctdlroom)) ?
679 sizeof(struct ctdlroom) : cdbqr->len)
682 room_sanity_check(&qrbuf);
683 if (qrbuf.QRflags & QR_INUSE) {
690 * Traverse the room file...
692 void CtdlForEachNetCfgRoom(ForEachRoomNetCfgCallBack CB,
696 struct ctdlroom qrbuf;
697 struct cdbdata *cdbqr;
699 cdb_rewind(CDB_ROOMS);
701 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
702 memset(&qrbuf, 0, sizeof(struct ctdlroom));
703 memcpy(&qrbuf, cdbqr->ptr,
704 ((cdbqr->len > sizeof(struct ctdlroom)) ?
705 sizeof(struct ctdlroom) : cdbqr->len)
708 room_sanity_check(&qrbuf);
709 if (qrbuf.QRflags & QR_INUSE)
711 OneRoomNetCfg* RNCfg;
712 RNCfg = CtdlGetNetCfgForRoom(qrbuf.QRnumber);
713 if ((RNCfg != NULL) &&
714 ((filter == maxRoomNetCfg) ||
715 (RNCfg->NetConfigs[filter] != NULL)))
717 CB(&qrbuf, in_data, RNCfg);
725 * delete_msglist() - delete room message pointers
727 void delete_msglist(struct ctdlroom *whichroom)
729 struct cdbdata *cdbml;
731 /* Make sure the msglist we're deleting actually exists, otherwise
732 * libdb will complain when we try to delete an invalid record
734 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
738 /* Go ahead and delete it */
739 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
745 * Message pointer compare function for sort_msglist()
747 int sort_msglist_cmp(const void *m1, const void *m2) {
748 if ((*(const long *)m1) > (*(const long *)m2)) return(1);
749 if ((*(const long *)m1) < (*(const long *)m2)) return(-1);
755 * sort message pointers
756 * (returns new msg count)
758 int sort_msglist(long listptrs[], int oldcount)
769 qsort(listptrs, numitems, sizeof(long), sort_msglist_cmp);
771 /* and yank any nulls */
772 while ((i < numitems) && (listptrs[i] == 0L)) i++;
776 memmove(&listptrs[0], &listptrs[i], (sizeof(long) * (numitems - i)));
785 * Determine whether a given room is non-editable.
787 int CtdlIsNonEditable(struct ctdlroom *qrbuf)
790 /* Mail> rooms are non-editable */
791 if ( (qrbuf->QRflags & QR_MAILBOX)
792 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
795 /* Everything else is editable */
802 * Make the specified room the current room for this session. No validation
803 * or access control is done here -- the caller should make sure that the
804 * specified room exists and is ok to access.
806 void CtdlUserGoto(char *where, int display_result, int transiently,
807 int *retmsgs, int *retnew)
809 struct CitContext *CCC = CC;
811 int new_messages = 0;
812 int old_messages = 0;
813 int total_messages = 0;
817 int newmailcount = 0;
819 char truncated_roomname[ROOMNAMELEN];
820 struct cdbdata *cdbfr;
821 long *msglist = NULL;
823 unsigned int original_v_flags;
826 char setstr[128], lostr[64], histr[64];
830 /* If the supplied room name is NULL, the caller wants us to know that
831 * it has already copied the room record into CC->room, so
832 * we can skip the extra database fetch.
835 safestrncpy(CCC->room.QRname, where, sizeof CCC->room.QRname);
836 CtdlGetRoom(&CCC->room, where);
839 /* Take care of all the formalities. */
841 begin_critical_section(S_USERS);
842 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
843 original_v_flags = vbuf.v_flags;
845 /* Know the room ... but not if it's the page log room, or if the
846 * caller specified that we're only entering this room transiently.
848 if ((strcasecmp(CCC->room.QRname, config.c_logpages))
849 && (transiently == 0) ) {
850 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
851 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
854 /* Only rewrite the database record if we changed something */
855 if (vbuf.v_flags != original_v_flags) {
856 CtdlSetRelationship(&vbuf, &CCC->user, &CCC->room);
858 end_critical_section(S_USERS);
860 /* Check for new mail */
861 newmailcount = NewMailCount();
863 /* set info to 1 if the user needs to read the room's info file */
864 if (CCC->room.QRinfo > vbuf.v_lastseen) {
868 cdbfr = cdb_fetch(CDB_MSGLISTS, &CCC->room.QRnumber, sizeof(long));
870 msglist = (long *) cdbfr->ptr;
871 cdbfr->ptr = NULL; /* CtdlUserGoto() now owns this memory */
872 num_msgs = cdbfr->len / sizeof(long);
877 for (a=0; a<num_msgs; ++a) {
878 if (msglist[a] > 0L) ++total_messages;
881 num_sets = num_tokens(vbuf.v_seen, ',');
882 for (s=0; s<num_sets; ++s) {
883 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
885 extract_token(lostr, setstr, 0, ':', sizeof lostr);
886 if (num_tokens(setstr, ':') >= 2) {
887 extract_token(histr, setstr, 1, ':', sizeof histr);
888 if (!strcmp(histr, "*")) {
889 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
893 strcpy(histr, lostr);
898 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
899 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
905 new_messages = total_messages - old_messages;
907 if (msglist != NULL) free(msglist);
909 if (CCC->room.QRflags & QR_MAILBOX)
914 if ((CCC->room.QRroomaide == CCC->user.usernum)
915 || (CCC->user.axlevel >= AxAideU))
920 safestrncpy(truncated_roomname, CCC->room.QRname, sizeof truncated_roomname);
921 if ( (CCC->room.QRflags & QR_MAILBOX)
922 && (atol(CCC->room.QRname) == CCC->user.usernum) ) {
923 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
926 if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
930 if (retmsgs != NULL) *retmsgs = total_messages;
931 if (retnew != NULL) *retnew = new_messages;
932 MSG_syslog(LOG_INFO, "<%s> %d new of %d total messages\n",
934 new_messages, total_messages
937 CCC->curr_view = (int)vbuf.v_view;
939 if (display_result) {
940 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|%d|%ld|\n",
941 CIT_OK, CtdlCheckExpress(),
946 (int)CCC->room.QRflags,
947 (long)CCC->room.QRhighest,
948 (long)vbuf.v_lastseen,
952 (int)CCC->room.QRfloor,
954 (int)CCC->room.QRdefaultview,
956 (int)CCC->room.QRflags2,
957 (long)CCC->room.QRmtime
964 * Handle some of the macro named rooms
966 void convert_room_name_macros(char *towhere, size_t maxlen) {
967 if (!strcasecmp(towhere, "_BASEROOM_")) {
968 safestrncpy(towhere, config.c_baseroom, maxlen);
970 else if (!strcasecmp(towhere, "_MAIL_")) {
971 safestrncpy(towhere, MAILROOM, maxlen);
973 else if (!strcasecmp(towhere, "_TRASH_")) {
974 safestrncpy(towhere, USERTRASHROOM, maxlen);
976 else if (!strcasecmp(towhere, "_DRAFTS_")) {
977 safestrncpy(towhere, USERDRAFTROOM, maxlen);
979 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
980 safestrncpy(towhere, config.c_twitroom, maxlen);
982 else if (!strcasecmp(towhere, "_CALENDAR_")) {
983 safestrncpy(towhere, USERCALENDARROOM, maxlen);
985 else if (!strcasecmp(towhere, "_TASKS_")) {
986 safestrncpy(towhere, USERTASKSROOM, maxlen);
988 else if (!strcasecmp(towhere, "_CONTACTS_")) {
989 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
991 else if (!strcasecmp(towhere, "_NOTES_")) {
992 safestrncpy(towhere, USERNOTESROOM, maxlen);
998 * Back end function to rename a room.
999 * You can also specify which floor to move the room to, or specify -1 to
1000 * keep the room on the same floor it was on.
1002 * If you are renaming a mailbox room, you must supply the namespace prefix
1003 * in *at least* the old name!
1005 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1007 struct ctdlroom qrbuf;
1008 struct ctdlroom qrtmp;
1013 char actual_old_name[ROOMNAMELEN];
1015 syslog(LOG_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1016 old_name, new_name, new_floor);
1018 if (new_floor >= 0) {
1019 fl = CtdlGetCachedFloor(new_floor);
1020 if ((fl->f_flags & F_INUSE) == 0) {
1021 return(crr_invalid_floor);
1025 begin_critical_section(S_ROOMS);
1027 if ( (CtdlGetRoom(&qrtmp, new_name) == 0)
1028 && (strcasecmp(new_name, old_name)) ) {
1029 ret = crr_already_exists;
1032 else if (CtdlGetRoom(&qrbuf, old_name) != 0) {
1033 ret = crr_room_not_found;
1036 else if ( (CC->user.axlevel < AxAideU) && (!CC->internal_pgm)
1037 && (CC->user.usernum != qrbuf.QRroomaide)
1038 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) ) {
1039 ret = crr_access_denied;
1042 else if (CtdlIsNonEditable(&qrbuf)) {
1043 ret = crr_noneditable;
1048 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1049 if (qrbuf.QRflags & QR_MAILBOX) {
1050 owner = atol(qrbuf.QRname);
1052 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1053 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1054 "%010ld.%s", owner, new_name);
1057 safestrncpy(qrbuf.QRname, new_name,
1058 sizeof(qrbuf.QRname));
1061 /* Reject change of floor for baseroom/aideroom */
1062 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1063 !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1067 /* Take care of floor stuff */
1068 old_floor = qrbuf.QRfloor;
1069 if (new_floor < 0) {
1070 new_floor = old_floor;
1072 qrbuf.QRfloor = new_floor;
1073 CtdlPutRoom(&qrbuf);
1075 begin_critical_section(S_CONFIG);
1077 /* If baseroom/aideroom name changes, update config */
1078 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1079 safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1082 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1083 safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1087 end_critical_section(S_CONFIG);
1089 /* If the room name changed, then there are now two room
1090 * records, so we have to delete the old one.
1092 if (strcasecmp(new_name, old_name)) {
1093 b_deleteroom(actual_old_name);
1099 end_critical_section(S_ROOMS);
1101 /* Adjust the floor reference counts if necessary */
1102 if (new_floor != old_floor) {
1103 lgetfloor(&flbuf, old_floor);
1104 --flbuf.f_ref_count;
1105 lputfloor(&flbuf, old_floor);
1106 syslog(LOG_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1107 lgetfloor(&flbuf, new_floor);
1108 ++flbuf.f_ref_count;
1109 lputfloor(&flbuf, new_floor);
1110 syslog(LOG_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1113 /* ...and everybody say "YATTA!" */
1120 * Asynchronously schedule a room for deletion. The room will appear
1121 * deleted to the user(s), but it won't actually get purged from the
1122 * database until THE DREADED AUTO-PURGER makes its next run.
1124 void CtdlScheduleRoomForDeletion(struct ctdlroom *qrbuf)
1126 char old_name[ROOMNAMELEN];
1129 syslog(LOG_NOTICE, "Scheduling room <%s> for deletion\n",
1132 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1134 CtdlGetRoom(qrbuf, qrbuf->QRname);
1136 /* Turn the room into a private mailbox owned by a user who doesn't
1137 * exist. This will immediately make the room invisible to everyone,
1138 * and qualify the room for purging.
1140 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1145 qrbuf->QRflags |= QR_MAILBOX;
1146 time(&qrbuf->QRgen); /* Use a timestamp as the new generation number */
1150 b_deleteroom(old_name);
1156 * Back end processing to delete a room and everything associated with it
1157 * (This one is synchronous and should only get called by THE DREADED
1158 * AUTO-PURGER in serv_expire.c. All user-facing code should call
1159 * the asynchronous schedule_room_for_deletion() instead.)
1161 void CtdlDeleteRoom(struct ctdlroom *qrbuf)
1165 /* TODO: filename magic? does this realy work? */
1167 syslog(LOG_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1169 /* Delete the info file */
1170 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_info_dir);
1173 /* Delete the image file */
1174 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_image_dir);
1177 /* Delete the room's network config file */
1178 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1181 /* Delete the messages in the room
1182 * (Careful: this opens an S_ROOMS critical section!)
1184 CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
1186 /* Flag the room record as not in use */
1187 CtdlGetRoomLock(qrbuf, qrbuf->QRname);
1189 CtdlPutRoomLock(qrbuf);
1191 /* then decrement the reference count for the floor */
1192 lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1193 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1194 lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1196 /* Delete the room record from the database! */
1197 b_deleteroom(qrbuf->QRname);
1203 * Check access control for deleting a room
1205 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1207 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1211 if (CtdlIsNonEditable(qr)) {
1216 * For mailboxes, check stuff
1218 if (qr->QRflags & QR_MAILBOX) {
1220 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1222 if (atol(qr->QRname) != CC->user.usernum) {
1223 return(0); /* not my room */
1226 /* Can't delete your Mail> room */
1227 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1229 /* Otherwise it's ok */
1234 * For normal rooms, just check for admin or room admin status.
1236 return(is_room_aide());
1242 * Internal code to create a new room (returns room flags)
1244 * Room types: 0=public, 1=hidden, 2=passworded, 3=invitation-only,
1245 * 4=mailbox, 5=mailbox, but caller supplies namespace
1247 unsigned CtdlCreateRoom(char *new_room_name,
1249 char *new_room_pass,
1256 struct ctdlroom qrbuf;
1260 syslog(LOG_DEBUG, "CtdlCreateRoom(name=%s, type=%d, view=%d)\n",
1261 new_room_name, new_room_type, new_room_view);
1263 if (CtdlGetRoom(&qrbuf, new_room_name) == 0) {
1264 syslog(LOG_DEBUG, "%s already exists.\n", new_room_name);
1268 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1269 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1270 qrbuf.QRflags = QR_INUSE;
1271 if (new_room_type > 0)
1272 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1273 if (new_room_type == 1)
1274 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1275 if (new_room_type == 2)
1276 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1277 if ( (new_room_type == 4) || (new_room_type == 5) ) {
1278 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1279 /* qrbuf.QRflags2 |= QR2_SUBJECTREQ; */
1282 /* If the user is requesting a personal room, set up the room
1283 * name accordingly (prepend the user number)
1285 if (new_room_type == 4) {
1286 CtdlMailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1289 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1292 /* If the room is private, and the system administrator has elected
1293 * to automatically grant room admin privileges, do so now.
1295 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1296 qrbuf.QRroomaide = CC->user.usernum;
1298 /* Blog owners automatically become room admins of their blogs.
1299 * (In the future we will offer a site-wide configuration setting to suppress this behavior.)
1301 else if (new_room_view == VIEW_BLOG) {
1302 qrbuf.QRroomaide = CC->user.usernum;
1304 /* Otherwise, set the room admin to undefined.
1307 qrbuf.QRroomaide = (-1L);
1311 * If the caller is only interested in testing whether this will work,
1312 * return now without creating the room.
1314 if (!really_create) return (qrbuf.QRflags);
1316 qrbuf.QRnumber = get_new_room_number();
1317 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1318 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1319 qrbuf.QRfloor = new_room_floor;
1320 qrbuf.QRdefaultview = new_room_view;
1322 /* save what we just did... */
1323 CtdlPutRoom(&qrbuf);
1325 /* bump the reference count on whatever floor the room is on */
1326 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1327 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1328 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1330 /* Grant the creator access to the room unless the avoid_access
1331 * parameter was specified.
1333 if ( (CC->logged_in) && (avoid_access == 0) ) {
1334 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1335 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1336 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1337 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1340 /* resume our happy day */
1341 return (qrbuf.QRflags);