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.
16 #include <libcitadel.h>
18 #include "citserver.h"
20 #include "ctdl_module.h"
26 struct floor *floorcache[MAXFLOORS];
29 * Determine whether the currently logged in session has permission to read
30 * messages in the current room.
32 int CtdlDoIHavePermissionToReadMessagesInThisRoom(void) {
33 if ( (!(CC->logged_in))
34 && (!(CC->internal_pgm))
35 && (!config.c_guest_logins)
37 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)
83 && ((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);
117 * Check whether the current user has permission to delete messages from
118 * the current room (returns 1 for yes, 0 for no)
120 int CtdlDoIHavePermissionToDeleteMessagesFromThisRoom(void) {
122 CtdlRoomAccess(&CC->room, &CC->user, &ra, NULL);
123 if (ra & UA_DELETEALLOWED) return(1);
128 * Retrieve access control information for any user/room pair
130 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf,
131 int *result, int *view)
138 if (userbuf == &CC->user) {
142 if ((is_me) && (config.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, config.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, config.c_baseroom, ROOMNAMELEN)
344 strncasecmp(qrbuf->QRname, config.c_aideroom, ROOMNAMELEN))
350 * CtdlGetRoom() - retrieve room data from disk
352 int CtdlGetRoom(struct ctdlroom *qrbuf, const char *room_name)
354 struct cdbdata *cdbqr;
355 char lowercase_name[ROOMNAMELEN];
356 char personal_lowercase_name[ROOMNAMELEN];
360 dptr = lowercase_name;
362 eptr = (dptr + (sizeof lowercase_name - 1));
363 while (!IsEmptyStr(sptr) && (dptr < eptr)){
364 *dptr = tolower(*sptr);
369 memset(qrbuf, 0, sizeof(struct ctdlroom));
371 /* First, try the public namespace */
372 cdbqr = cdb_fetch(CDB_ROOMS,
373 lowercase_name, strlen(lowercase_name));
375 /* If that didn't work, try the user's personal namespace */
377 snprintf(personal_lowercase_name,
378 sizeof personal_lowercase_name, "%010ld.%s",
379 CC->user.usernum, lowercase_name);
380 cdbqr = cdb_fetch(CDB_ROOMS,
381 personal_lowercase_name,
382 strlen(personal_lowercase_name));
385 memcpy(qrbuf, cdbqr->ptr,
386 ((cdbqr->len > sizeof(struct ctdlroom)) ?
387 sizeof(struct ctdlroom) : cdbqr->len));
390 room_sanity_check(qrbuf);
399 * CtdlGetRoomLock() - same as getroom() but locks the record (if supported)
401 int CtdlGetRoomLock(struct ctdlroom *qrbuf, char *room_name)
404 retval = CtdlGetRoom(qrbuf, room_name);
405 if (retval == 0) begin_critical_section(S_ROOMS);
411 * b_putroom() - back end to putroom() and b_deleteroom()
412 * (if the supplied buffer is NULL, delete the room record)
414 void b_putroom(struct ctdlroom *qrbuf, char *room_name)
416 char lowercase_name[ROOMNAMELEN];
421 bptr = lowercase_name;
422 while (!IsEmptyStr(aptr))
424 *bptr = tolower(*aptr);
430 len = bptr - lowercase_name;
432 cdb_delete(CDB_ROOMS, lowercase_name, len);
434 time(&qrbuf->QRmtime);
435 cdb_store(CDB_ROOMS, lowercase_name, len, qrbuf, sizeof(struct ctdlroom));
441 * CtdlPutRoom() - store room data to disk
443 void CtdlPutRoom(struct ctdlroom *qrbuf) {
444 b_putroom(qrbuf, qrbuf->QRname);
449 * b_deleteroom() - delete a room record from disk
451 void b_deleteroom(char *room_name) {
452 b_putroom(NULL, room_name);
457 * CtdlPutRoomLock() - same as CtdlPutRoom() but unlocks the record (if supported)
459 void CtdlPutRoomLock(struct ctdlroom *qrbuf)
463 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)
554 begin_critical_section(S_FLOORTAB);
555 CtdlGetFloor(flbuf, floor_num);
560 * CtdlGetCachedFloor() - Get floor record from *cache* (loads from disk if needed)
562 * This is strictly a performance hack.
564 struct floor *CtdlGetCachedFloor(int floor_num) {
565 static int initialized = 0;
568 struct floor *fl = NULL;
570 begin_critical_section(S_FLOORCACHE);
571 if (initialized == 0) {
572 for (i=0; i<MAXFLOORS; ++i) {
573 floorcache[floor_num] = NULL;
577 if (floorcache[floor_num] == NULL) {
580 end_critical_section(S_FLOORCACHE);
583 fl = malloc(sizeof(struct floor));
584 CtdlGetFloor(fl, floor_num);
585 begin_critical_section(S_FLOORCACHE);
586 if (floorcache[floor_num] != NULL) {
587 free(floorcache[floor_num]);
589 floorcache[floor_num] = fl;
590 end_critical_section(S_FLOORCACHE);
593 return(floorcache[floor_num]);
598 * CtdlPutFloor() - store floor data on disk
600 void CtdlPutFloor(struct floor *flbuf, int floor_num)
602 /* If we've cached this, clear it out, 'cuz it's WRONG now! */
603 begin_critical_section(S_FLOORCACHE);
604 if (floorcache[floor_num] != NULL) {
605 free(floorcache[floor_num]);
606 floorcache[floor_num] = malloc(sizeof(struct floor));
607 memcpy(floorcache[floor_num], flbuf, sizeof(struct floor));
609 end_critical_section(S_FLOORCACHE);
611 cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
612 flbuf, sizeof(struct floor));
617 * CtdlPutFloorLock() - same as CtdlPutFloor() but unlocks the record (if supported)
619 void CtdlPutFloorLock(struct floor *flbuf, int floor_num)
622 CtdlPutFloor(flbuf, floor_num);
623 end_critical_section(S_FLOORTAB);
629 * lputfloor() - same as CtdlPutFloor() but unlocks the record (if supported)
631 void lputfloor(struct floor *flbuf, int floor_num)
633 CtdlPutFloorLock(flbuf, floor_num);
638 * Traverse the room file...
640 void CtdlForEachRoom(ForEachRoomCallBack CB, void *in_data)
642 struct ctdlroom qrbuf;
643 struct cdbdata *cdbqr;
645 cdb_rewind(CDB_ROOMS);
647 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
648 memset(&qrbuf, 0, sizeof(struct ctdlroom));
649 memcpy(&qrbuf, cdbqr->ptr,
650 ((cdbqr->len > sizeof(struct ctdlroom)) ?
651 sizeof(struct ctdlroom) : cdbqr->len)
654 room_sanity_check(&qrbuf);
655 if (qrbuf.QRflags & QR_INUSE) {
662 * Traverse the room file...
664 void CtdlForEachNetCfgRoom(ForEachRoomNetCfgCallBack CB,
668 struct ctdlroom qrbuf;
669 struct cdbdata *cdbqr;
671 cdb_rewind(CDB_ROOMS);
673 while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
674 memset(&qrbuf, 0, sizeof(struct ctdlroom));
675 memcpy(&qrbuf, cdbqr->ptr,
676 ((cdbqr->len > sizeof(struct ctdlroom)) ?
677 sizeof(struct ctdlroom) : cdbqr->len)
680 room_sanity_check(&qrbuf);
681 if (qrbuf.QRflags & QR_INUSE)
683 OneRoomNetCfg* RNCfg;
684 RNCfg = CtdlGetNetCfgForRoom(qrbuf.QRnumber);
685 if ((RNCfg != NULL) &&
686 ((filter == maxRoomNetCfg) ||
687 (RNCfg->NetConfigs[filter] != NULL)))
689 CB(&qrbuf, in_data, RNCfg);
697 * delete_msglist() - delete room message pointers
699 void delete_msglist(struct ctdlroom *whichroom)
701 struct cdbdata *cdbml;
703 /* Make sure the msglist we're deleting actually exists, otherwise
704 * libdb will complain when we try to delete an invalid record
706 cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
710 /* Go ahead and delete it */
711 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
717 * Message pointer compare function for sort_msglist()
719 int sort_msglist_cmp(const void *m1, const void *m2) {
720 if ((*(const long *)m1) > (*(const long *)m2)) return(1);
721 if ((*(const long *)m1) < (*(const long *)m2)) return(-1);
727 * sort message pointers
728 * (returns new msg count)
730 int sort_msglist(long listptrs[], int oldcount)
741 qsort(listptrs, numitems, sizeof(long), sort_msglist_cmp);
743 /* and yank any nulls */
744 while ((i < numitems) && (listptrs[i] == 0L)) i++;
748 memmove(&listptrs[0], &listptrs[i], (sizeof(long) * (numitems - i)));
757 * Determine whether a given room is non-editable.
759 int CtdlIsNonEditable(struct ctdlroom *qrbuf)
762 /* Mail> rooms are non-editable */
763 if ( (qrbuf->QRflags & QR_MAILBOX)
764 && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
767 /* Everything else is editable */
774 * Make the specified room the current room for this session. No validation
775 * or access control is done here -- the caller should make sure that the
776 * specified room exists and is ok to access.
778 void CtdlUserGoto(char *where, int display_result, int transiently,
779 int *retmsgs, int *retnew)
781 struct CitContext *CCC = CC;
783 int new_messages = 0;
784 int old_messages = 0;
785 int total_messages = 0;
789 int newmailcount = 0;
791 char truncated_roomname[ROOMNAMELEN];
792 struct cdbdata *cdbfr;
793 long *msglist = NULL;
795 unsigned int original_v_flags;
798 char setstr[128], lostr[64], histr[64];
802 /* If the supplied room name is NULL, the caller wants us to know that
803 * it has already copied the room record into CC->room, so
804 * we can skip the extra database fetch.
807 safestrncpy(CCC->room.QRname, where, sizeof CCC->room.QRname);
808 CtdlGetRoom(&CCC->room, where);
811 /* Take care of all the formalities. */
813 begin_critical_section(S_USERS);
814 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
815 original_v_flags = vbuf.v_flags;
817 /* Know the room ... but not if it's the page log room, or if the
818 * caller specified that we're only entering this room transiently.
820 if ((strcasecmp(CCC->room.QRname, config.c_logpages))
821 && (transiently == 0) ) {
822 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
823 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
826 /* Only rewrite the database record if we changed something */
827 if (vbuf.v_flags != original_v_flags) {
828 CtdlSetRelationship(&vbuf, &CCC->user, &CCC->room);
830 end_critical_section(S_USERS);
832 /* Check for new mail */
833 newmailcount = NewMailCount();
835 /* set info to 1 if the user needs to read the room's info file */
836 if (CCC->room.QRinfo > vbuf.v_lastseen) {
840 cdbfr = cdb_fetch(CDB_MSGLISTS, &CCC->room.QRnumber, sizeof(long));
842 msglist = (long *) cdbfr->ptr;
843 cdbfr->ptr = NULL; /* CtdlUserGoto() now owns this memory */
844 num_msgs = cdbfr->len / sizeof(long);
849 for (a=0; a<num_msgs; ++a) {
850 if (msglist[a] > 0L) ++total_messages;
853 num_sets = num_tokens(vbuf.v_seen, ',');
854 for (s=0; s<num_sets; ++s) {
855 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
857 extract_token(lostr, setstr, 0, ':', sizeof lostr);
858 if (num_tokens(setstr, ':') >= 2) {
859 extract_token(histr, setstr, 1, ':', sizeof histr);
860 if (!strcmp(histr, "*")) {
861 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
865 strcpy(histr, lostr);
870 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
871 if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
877 new_messages = total_messages - old_messages;
879 if (msglist != NULL) free(msglist);
881 if (CCC->room.QRflags & QR_MAILBOX)
886 if ((CCC->room.QRroomaide == CCC->user.usernum)
887 || (CCC->user.axlevel >= AxAideU))
892 safestrncpy(truncated_roomname, CCC->room.QRname, sizeof truncated_roomname);
893 if ( (CCC->room.QRflags & QR_MAILBOX)
894 && (atol(CCC->room.QRname) == CCC->user.usernum) ) {
895 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
898 if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
902 if (retmsgs != NULL) *retmsgs = total_messages;
903 if (retnew != NULL) *retnew = new_messages;
904 MSG_syslog(LOG_INFO, "<%s> %d new of %d total messages\n",
906 new_messages, total_messages
909 CCC->curr_view = (int)vbuf.v_view;
911 if (display_result) {
912 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|%d|%ld|\n",
913 CIT_OK, CtdlCheckExpress(),
918 (int)CCC->room.QRflags,
919 (long)CCC->room.QRhighest,
920 (long)vbuf.v_lastseen,
924 (int)CCC->room.QRfloor,
926 (int)CCC->room.QRdefaultview,
928 (int)CCC->room.QRflags2,
929 (long)CCC->room.QRmtime
936 * Handle some of the macro named rooms
938 void convert_room_name_macros(char *towhere, size_t maxlen) {
939 if (!strcasecmp(towhere, "_BASEROOM_")) {
940 safestrncpy(towhere, config.c_baseroom, maxlen);
942 else if (!strcasecmp(towhere, "_MAIL_")) {
943 safestrncpy(towhere, MAILROOM, maxlen);
945 else if (!strcasecmp(towhere, "_TRASH_")) {
946 safestrncpy(towhere, USERTRASHROOM, maxlen);
948 else if (!strcasecmp(towhere, "_DRAFTS_")) {
949 safestrncpy(towhere, USERDRAFTROOM, maxlen);
951 else if (!strcasecmp(towhere, "_BITBUCKET_")) {
952 safestrncpy(towhere, config.c_twitroom, maxlen);
954 else if (!strcasecmp(towhere, "_CALENDAR_")) {
955 safestrncpy(towhere, USERCALENDARROOM, maxlen);
957 else if (!strcasecmp(towhere, "_TASKS_")) {
958 safestrncpy(towhere, USERTASKSROOM, maxlen);
960 else if (!strcasecmp(towhere, "_CONTACTS_")) {
961 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
963 else if (!strcasecmp(towhere, "_NOTES_")) {
964 safestrncpy(towhere, USERNOTESROOM, maxlen);
970 * Back end function to rename a room.
971 * You can also specify which floor to move the room to, or specify -1 to
972 * keep the room on the same floor it was on.
974 * If you are renaming a mailbox room, you must supply the namespace prefix
975 * in *at least* the old name!
977 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
979 struct ctdlroom qrbuf;
980 struct ctdlroom qrtmp;
985 char actual_old_name[ROOMNAMELEN];
987 syslog(LOG_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
988 old_name, new_name, new_floor);
990 if (new_floor >= 0) {
991 fl = CtdlGetCachedFloor(new_floor);
992 if ((fl->f_flags & F_INUSE) == 0) {
993 return(crr_invalid_floor);
997 begin_critical_section(S_ROOMS);
999 if ( (CtdlGetRoom(&qrtmp, new_name) == 0)
1000 && (strcasecmp(new_name, old_name)) ) {
1001 ret = crr_already_exists;
1004 else if (CtdlGetRoom(&qrbuf, old_name) != 0) {
1005 ret = crr_room_not_found;
1008 else if ( (CC->user.axlevel < AxAideU) && (!CC->internal_pgm)
1009 && (CC->user.usernum != qrbuf.QRroomaide)
1010 && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) ) {
1011 ret = crr_access_denied;
1014 else if (CtdlIsNonEditable(&qrbuf)) {
1015 ret = crr_noneditable;
1020 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1021 if (qrbuf.QRflags & QR_MAILBOX) {
1022 owner = atol(qrbuf.QRname);
1024 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1025 snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1026 "%010ld.%s", owner, new_name);
1029 safestrncpy(qrbuf.QRname, new_name,
1030 sizeof(qrbuf.QRname));
1033 /* Reject change of floor for baseroom/aideroom */
1034 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1035 !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1039 /* Take care of floor stuff */
1040 old_floor = qrbuf.QRfloor;
1041 if (new_floor < 0) {
1042 new_floor = old_floor;
1044 qrbuf.QRfloor = new_floor;
1045 CtdlPutRoom(&qrbuf);
1047 begin_critical_section(S_CONFIG);
1049 /* If baseroom/aideroom name changes, update config */
1050 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1051 safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1054 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1055 safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1059 end_critical_section(S_CONFIG);
1061 /* If the room name changed, then there are now two room
1062 * records, so we have to delete the old one.
1064 if (strcasecmp(new_name, old_name)) {
1065 b_deleteroom(actual_old_name);
1071 end_critical_section(S_ROOMS);
1073 /* Adjust the floor reference counts if necessary */
1074 if (new_floor != old_floor) {
1075 lgetfloor(&flbuf, old_floor);
1076 --flbuf.f_ref_count;
1077 lputfloor(&flbuf, old_floor);
1078 syslog(LOG_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1079 lgetfloor(&flbuf, new_floor);
1080 ++flbuf.f_ref_count;
1081 lputfloor(&flbuf, new_floor);
1082 syslog(LOG_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1085 /* ...and everybody say "YATTA!" */
1092 * Asynchronously schedule a room for deletion. The room will appear
1093 * deleted to the user(s), but it won't actually get purged from the
1094 * database until THE DREADED AUTO-PURGER makes its next run.
1096 void CtdlScheduleRoomForDeletion(struct ctdlroom *qrbuf)
1098 char old_name[ROOMNAMELEN];
1101 syslog(LOG_NOTICE, "Scheduling room <%s> for deletion\n",
1104 safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1106 CtdlGetRoom(qrbuf, qrbuf->QRname);
1108 /* Turn the room into a private mailbox owned by a user who doesn't
1109 * exist. This will immediately make the room invisible to everyone,
1110 * and qualify the room for purging.
1112 snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1117 qrbuf->QRflags |= QR_MAILBOX;
1118 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)
1137 /* TODO: filename magic? does this realy work? */
1139 syslog(LOG_NOTICE, "Deleting room <%s>\n", 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 config file */
1150 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
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);
1175 * Check access control for deleting a room
1177 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1179 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1183 if (CtdlIsNonEditable(qr)) {
1188 * For mailboxes, check stuff
1190 if (qr->QRflags & QR_MAILBOX) {
1192 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1194 if (atol(qr->QRname) != CC->user.usernum) {
1195 return(0); /* not my room */
1198 /* Can't delete your Mail> room */
1199 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1201 /* Otherwise it's ok */
1206 * For normal rooms, just check for admin or room admin status.
1208 return(is_room_aide());
1214 * Internal code to create a new room (returns room flags)
1216 * Room types: 0=public, 1=hidden, 2=passworded, 3=invitation-only,
1217 * 4=mailbox, 5=mailbox, but caller supplies namespace
1219 unsigned CtdlCreateRoom(char *new_room_name,
1221 char *new_room_pass,
1228 struct ctdlroom qrbuf;
1232 syslog(LOG_DEBUG, "CtdlCreateRoom(name=%s, type=%d, view=%d)\n",
1233 new_room_name, new_room_type, new_room_view);
1235 if (CtdlGetRoom(&qrbuf, new_room_name) == 0) {
1236 syslog(LOG_DEBUG, "%s already exists.\n", new_room_name);
1240 memset(&qrbuf, 0, sizeof(struct ctdlroom));
1241 safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1242 qrbuf.QRflags = QR_INUSE;
1243 if (new_room_type > 0)
1244 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1245 if (new_room_type == 1)
1246 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1247 if (new_room_type == 2)
1248 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1249 if ( (new_room_type == 4) || (new_room_type == 5) ) {
1250 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1251 /* qrbuf.QRflags2 |= QR2_SUBJECTREQ; */
1254 /* If the user is requesting a personal room, set up the room
1255 * name accordingly (prepend the user number)
1257 if (new_room_type == 4) {
1258 CtdlMailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1261 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1264 /* If the room is private, and the system administrator has elected
1265 * to automatically grant room admin privileges, do so now.
1267 if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1268 qrbuf.QRroomaide = CC->user.usernum;
1270 /* Blog owners automatically become room admins of their blogs.
1271 * (In the future we will offer a site-wide configuration setting to suppress this behavior.)
1273 else if (new_room_view == VIEW_BLOG) {
1274 qrbuf.QRroomaide = CC->user.usernum;
1276 /* Otherwise, set the room admin to undefined.
1279 qrbuf.QRroomaide = (-1L);
1283 * If the caller is only interested in testing whether this will work,
1284 * return now without creating the room.
1286 if (!really_create) return (qrbuf.QRflags);
1288 qrbuf.QRnumber = get_new_room_number();
1289 qrbuf.QRhighest = 0L; /* No messages in this room yet */
1290 time(&qrbuf.QRgen); /* Use a timestamp as the generation number */
1291 qrbuf.QRfloor = new_room_floor;
1292 qrbuf.QRdefaultview = new_room_view;
1294 /* save what we just did... */
1295 CtdlPutRoom(&qrbuf);
1297 /* bump the reference count on whatever floor the room is on */
1298 lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1299 flbuf.f_ref_count = flbuf.f_ref_count + 1;
1300 lputfloor(&flbuf, (int) qrbuf.QRfloor);
1302 /* Grant the creator access to the room unless the avoid_access
1303 * parameter was specified.
1305 if ( (CC->logged_in) && (avoid_access == 0) ) {
1306 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1307 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1308 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1309 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1312 /* resume our happy day */
1313 return (qrbuf.QRflags);