74adefb866690d56dce68c719e2d558f8c4df081
[citadel.git] / citadel / room_ops.c
1 /* 
2  * Server functions which perform operations on room objects.
3  *
4  * Copyright (c) 1987-2016 by the citadel.org team
5  *
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.
8  *
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.
13  */
14
15 #include <stdio.h>
16 #include <libcitadel.h>
17
18 #include "citserver.h"
19 #include "ctdl_module.h"
20 #include "config.h"
21 #include "control.h"
22 #include "user_ops.h"
23 #include "room_ops.h"
24
25 struct floor *floorcache[MAXFLOORS];
26
27 /* 
28  * Determine whether the currently logged in session has permission to read
29  * messages in the current room.
30  */
31 int CtdlDoIHavePermissionToReadMessagesInThisRoom(void) {
32         if (    (!(CC->logged_in))
33                 && (!(CC->internal_pgm))
34                 && (!CtdlGetConfigInt("c_guest_logins"))
35         ) {
36                 return(om_not_logged_in);
37         }
38         return(om_ok);
39 }
40
41 /*
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.
45  */
46 int CtdlDoIHavePermissionToPostInThisRoom(
47         char *errmsgbuf, 
48         size_t n, 
49         const char* RemoteIdentifier,
50         PostType PostPublic,
51         int is_reply
52         ) {
53         int ra;
54
55         if (!(CC->logged_in) && 
56             (PostPublic == POST_LOGGED_IN)) {
57                 snprintf(errmsgbuf, n, "Not logged in.");
58                 return (ERROR + NOT_LOGGED_IN);
59         }
60         else if (PostPublic == CHECK_EXISTANCE) {
61                 return (0); // We're Evaling whether a recipient exists
62         }
63         else if (!(CC->logged_in)) {
64                 
65                 if ((CC->room.QRflags & QR_READONLY)) {
66                         snprintf(errmsgbuf, n, "Not logged in.");
67                         return (ERROR + NOT_LOGGED_IN);
68                 }
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);
72                 }
73                 if ((PostPublic!=POST_LMTP) &&(CC->room.QRflags2 & QR2_SMTP_PUBLIC) == 0) {
74
75                         return CtdlNetconfigCheckRoomaccess(errmsgbuf, n, RemoteIdentifier);
76                 }
77                 return (0);
78
79         }
80
81         if ((CC->user.axlevel < AxProbU) && ((CC->room.QRflags & QR_MAILBOX) == 0))
82         {
83                 snprintf(errmsgbuf, n, "Need to be validated to enter (except in %s> to sysop)", MAILROOM);
84                 return (ERROR + HIGHER_ACCESS_REQUIRED);
85         }
86
87         CtdlRoomAccess(&CC->room, &CC->user, &ra, NULL);
88
89         if (ra & UA_POSTALLOWED) {
90                 strcpy(errmsgbuf, "OK to post or reply here");
91                 return(0);
92         }
93
94         if ( (ra & UA_REPLYALLOWED) && (is_reply) ) {
95                 /*
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.
99                  */
100                 strcpy(errmsgbuf, "OK to reply here");
101                 return(0);
102         }
103
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);
108         }
109
110         snprintf(errmsgbuf, n, "Higher access is required to post in this room.");
111         return (ERROR + HIGHER_ACCESS_REQUIRED);
112
113 }
114
115 /*
116  * Check whether the current user has permission to delete messages from
117  * the current room (returns 1 for yes, 0 for no)
118  */
119 int CtdlDoIHavePermissionToDeleteMessagesFromThisRoom(void) {
120         int ra;
121         CtdlRoomAccess(&CC->room, &CC->user, &ra, NULL);
122         if (ra & UA_DELETEALLOWED) return(1);
123         return(0);
124 }
125
126 /*
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.
129  */
130 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf,
131                 int *result, int *view)
132 {
133         int retval = 0;
134         visit vbuf;
135         int is_me = 0;
136         int is_guest = 0;
137
138         if (userbuf == &CC->user) {
139                 is_me = 1;
140         }
141
142         if ((is_me) && (CtdlGetConfigInt("c_guest_logins")) && (!CC->logged_in)) {
143                 is_guest = 1;
144         }
145
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);
149                 vbuf.v_view = 0;
150                 goto SKIP_EVERYTHING;
151         }
152
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);
156                 vbuf.v_view = 0;
157                 goto SKIP_EVERYTHING;
158         }
159
160         /* Locate any applicable user/room relationships */
161         if (is_guest) {
162                 memset(&vbuf, 0, sizeof vbuf);
163         }
164         else {
165                 CtdlGetRelationship(&vbuf, userbuf, roombuf);
166         }
167
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;
172                 } else {
173                         retval = 0;
174                 }
175                 goto NEWMSG;
176         }
177
178         /* If this is a public room, it's accessible... */
179         if (    ((roombuf->QRflags & QR_PRIVATE) == 0) 
180                 && ((roombuf->QRflags & QR_MAILBOX) == 0)
181         ) {
182                 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
183         }
184
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;
189                 }
190         }
191
192         /* For private rooms, check the generation number matchups */
193         if (    (roombuf->QRflags & QR_PRIVATE) 
194                 && ((roombuf->QRflags & QR_MAILBOX) == 0)
195         ) {
196
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;
200                 }
201                 /* Otherwise, check if this is a guess-name or passworded
202                  * room.  If it is, a goto may at least be attempted
203                  */
204                 else if (       (roombuf->QRflags & QR_PRIVATE)
205                                 || (roombuf->QRflags & QR_PASSWORDED)
206                 ) {
207                         retval = retval & ~UA_KNOWN;
208                         retval = retval | UA_GOTOALLOWED;
209                 }
210         }
211
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;
217                 }
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;
221                 }
222         }
223
224         /* For non-mailbox rooms... */
225         else {
226
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)
232                  */
233                 int post_allowed = 1;
234                 int reply_allowed = 1;
235                 if (userbuf->axlevel < AxProbU) {
236                         post_allowed = 0;
237                         reply_allowed = 0;
238                 }
239                 if ((userbuf->axlevel < AxNetU) && (roombuf->QRflags & QR_NETWORK)) {
240                         post_allowed = 0;
241                         reply_allowed = 0;
242                 }
243                 if (roombuf->QRflags & QR_READONLY) {
244                         post_allowed = 0;
245                         reply_allowed = 0;
246                 }
247                 if (roombuf->QRdefaultview == VIEW_BLOG) {
248                         post_allowed = 0;
249                 }
250                 if (post_allowed) {
251                         retval = retval | UA_POSTALLOWED | UA_REPLYALLOWED;
252                 }
253                 if (reply_allowed) {
254                         retval = retval | UA_REPLYALLOWED;
255                 }
256
257                 /* If "collaborative deletion" is active for this room, any user who can post
258                  * is also allowed to delete
259                  */
260                 if (roombuf->QRflags2 & QR2_COLLABDEL) {
261                         if (retval & UA_POSTALLOWED) {
262                                 retval = retval | UA_DELETEALLOWED;
263                         }
264                 }
265
266         }
267
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))
275                 ) {
276                         retval = retval | UA_ZAPPED;
277                 }
278         }
279
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;
283         }
284
285         /* Aides get access to all private rooms */
286         if (    (userbuf->axlevel >= AxAideU)
287                 && ((roombuf->QRflags & QR_MAILBOX) == 0)
288         ) {
289                 if (vbuf.v_flags & V_FORGET) {
290                         retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
291                 }
292                 else {
293                         retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
294                 }
295         }
296
297         /* Aides can gain access to mailboxes as well, but they don't show
298          * by default.
299          */
300         if (    (userbuf->axlevel >= AxAideU)
301                 && (roombuf->QRflags & QR_MAILBOX)
302         ) {
303                 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
304         }
305
306         /* Aides and Room Aides have admin privileges */
307         if (    (userbuf->axlevel >= AxAideU)
308                 || (userbuf->usernum == roombuf->QRroomaide)
309         ) {
310                 retval = retval | UA_ADMINALLOWED | UA_DELETEALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
311         }
312
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;
316         }
317
318         /* System rooms never show up in the list. */
319         if (roombuf->QRflags2 & QR2_SYSTEM) {
320                 retval = retval & ~UA_KNOWN;
321         }
322
323 SKIP_EVERYTHING:
324         /* Now give the caller the information it wants. */
325         if (result != NULL) *result = retval;
326         if (view != NULL) *view = vbuf.v_view;
327 }
328
329
330 /*
331  * Self-checking stuff for a room record read into memory
332  */
333 void room_sanity_check(struct ctdlroom *qrbuf)
334 {
335         /* Mailbox rooms are always on the lowest floor */
336         if (qrbuf->QRflags & QR_MAILBOX) {
337                 qrbuf->QRfloor = 0;
338         }
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)
344                 ) {
345                         qrbuf->QRorder = 64;
346                 }
347 }
348
349
350 /*
351  * CtdlGetRoom()  -  retrieve room data from disk
352  */
353 int CtdlGetRoom(struct ctdlroom *qrbuf, const char *room_name)
354 {
355         struct cdbdata *cdbqr;
356         char lowercase_name[ROOMNAMELEN];
357         char personal_lowercase_name[ROOMNAMELEN];
358         const char *sptr;
359         char *dptr, *eptr;
360
361         dptr = lowercase_name;
362         sptr = room_name;
363         eptr = (dptr + (sizeof lowercase_name - 1));
364         while (!IsEmptyStr(sptr) && (dptr < eptr)){
365                 *dptr = tolower(*sptr);
366                 sptr++; dptr++;
367         }
368         *dptr = '\0';
369
370         memset(qrbuf, 0, sizeof(struct ctdlroom));
371
372         /* First, try the public namespace */
373         cdbqr = cdb_fetch(CDB_ROOMS,
374                           lowercase_name, strlen(lowercase_name));
375
376         /* If that didn't work, try the user's personal namespace */
377         if (cdbqr == NULL) {
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));
384         }
385         if (cdbqr != NULL) {
386                 memcpy(qrbuf, cdbqr->ptr,
387                        ((cdbqr->len > sizeof(struct ctdlroom)) ?
388                         sizeof(struct ctdlroom) : cdbqr->len));
389                 cdb_free(cdbqr);
390
391                 room_sanity_check(qrbuf);
392
393                 return (0);
394         } else {
395                 return (1);
396         }
397 }
398
399
400 /*
401  * CtdlGetRoomLock()  -  same as getroom() but locks the record (if supported)
402  */
403 int CtdlGetRoomLock(struct ctdlroom *qrbuf, const char *room_name)
404 {
405         register int retval;
406         retval = CtdlGetRoom(qrbuf, room_name);
407         if (retval == 0) begin_critical_section(S_ROOMS);
408         return(retval);
409 }
410
411
412 /*
413  * b_putroom()  -  back end to putroom() and b_deleteroom()
414  *              (if the supplied buffer is NULL, delete the room record)
415  */
416 void b_putroom(struct ctdlroom *qrbuf, char *room_name)
417 {
418         char lowercase_name[ROOMNAMELEN];
419         char *aptr, *bptr;
420         long len;
421
422         aptr = room_name;
423         bptr = lowercase_name;
424         while (!IsEmptyStr(aptr))
425         {
426                 *bptr = tolower(*aptr);
427                 aptr++;
428                 bptr++;
429         }
430         *bptr='\0';
431
432         len = bptr - lowercase_name;
433         if (qrbuf == NULL) {
434                 cdb_delete(CDB_ROOMS, lowercase_name, len);
435         } else {
436                 time(&qrbuf->QRmtime);
437                 cdb_store(CDB_ROOMS, lowercase_name, len, qrbuf, sizeof(struct ctdlroom));
438         }
439 }
440
441
442 /* 
443  * CtdlPutRoom()  -  store room data to disk
444  */
445 void CtdlPutRoom(struct ctdlroom *qrbuf) {
446         b_putroom(qrbuf, qrbuf->QRname);
447 }
448
449
450 /*
451  * b_deleteroom()  -  delete a room record from disk
452  */
453 void b_deleteroom(char *room_name) {
454         b_putroom(NULL, room_name);
455 }
456
457
458 /*
459  * CtdlPutRoomLock()  -  same as CtdlPutRoom() but unlocks the record (if supported)
460  */
461 void CtdlPutRoomLock(struct ctdlroom *qrbuf)
462 {
463         CtdlPutRoom(qrbuf);
464         end_critical_section(S_ROOMS);
465 }
466
467
468 /*
469  * CtdlGetFloorByName()  -  retrieve the number of the named floor
470  * return < 0 if not found else return floor number
471  */
472 int CtdlGetFloorByName(const char *floor_name)
473 {
474         int a;
475         struct floor *flbuf = NULL;
476
477         for (a = 0; a < MAXFLOORS; ++a) {
478                 flbuf = CtdlGetCachedFloor(a);
479
480                 /* check to see if it already exists */
481                 if ((!strcasecmp(flbuf->f_name, floor_name))
482                     && (flbuf->f_flags & F_INUSE)) {
483                         return a;
484                 }
485         }
486         return -1;
487 }
488
489
490 /*
491  * CtdlGetFloorByNameLock()  -  retrieve floor number for given floor and lock the floor list.
492  */
493 int CtdlGetFloorByNameLock(const char *floor_name)
494 {
495         begin_critical_section(S_FLOORTAB);
496         return CtdlGetFloorByName(floor_name);
497 }
498
499
500
501 /*
502  * CtdlGetAvailableFloor()  -  Return number of first unused floor
503  * return < 0 if none available
504  */
505 int CtdlGetAvailableFloor(void)
506 {
507         int a;
508         struct floor *flbuf = NULL;
509
510         for (a = 0; a < MAXFLOORS; a++) {
511                 flbuf = CtdlGetCachedFloor(a);
512
513                 /* check to see if it already exists */
514                 if ((flbuf->f_flags & F_INUSE) == 0) {
515                         return a;
516                 }
517         }
518         return -1;
519 }
520
521
522 /*
523  * CtdlGetFloor()  -  retrieve floor data from disk
524  */
525 void CtdlGetFloor(struct floor *flbuf, int floor_num)
526 {
527         struct cdbdata *cdbfl;
528
529         memset(flbuf, 0, sizeof(struct floor));
530         cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
531         if (cdbfl != NULL) {
532                 memcpy(flbuf, cdbfl->ptr,
533                        ((cdbfl->len > sizeof(struct floor)) ?
534                         sizeof(struct floor) : cdbfl->len));
535                 cdb_free(cdbfl);
536         } else {
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;
542                 }
543         }
544
545 }
546
547
548 /*
549  * lgetfloor()  -  same as CtdlGetFloor() but locks the record (if supported)
550  */
551 void lgetfloor(struct floor *flbuf, int floor_num)
552 {
553         begin_critical_section(S_FLOORTAB);
554         CtdlGetFloor(flbuf, floor_num);
555 }
556
557
558 /*
559  * CtdlGetCachedFloor()  -  Get floor record from *cache* (loads from disk if needed)
560  *    
561  * This is strictly a performance hack.
562  */
563 struct floor *CtdlGetCachedFloor(int floor_num) {
564         static int initialized = 0;
565         int i;
566         int fetch_new = 0;
567         struct floor *fl = NULL;
568
569         begin_critical_section(S_FLOORCACHE);
570         if (initialized == 0) {
571                 for (i=0; i<MAXFLOORS; ++i) {
572                         floorcache[floor_num] = NULL;
573                 }
574         initialized = 1;
575         }
576         if (floorcache[floor_num] == NULL) {
577                 fetch_new = 1;
578         }
579         end_critical_section(S_FLOORCACHE);
580
581         if (fetch_new) {
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]);
587                 }
588                 floorcache[floor_num] = fl;
589                 end_critical_section(S_FLOORCACHE);
590         }
591
592         return(floorcache[floor_num]);
593 }
594
595
596 /*
597  * CtdlPutFloor()  -  store floor data on disk
598  */
599 void CtdlPutFloor(struct floor *flbuf, int floor_num)
600 {
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));
607         }
608         end_critical_section(S_FLOORCACHE);
609
610         cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
611                   flbuf, sizeof(struct floor));
612 }
613
614
615 /*
616  * CtdlPutFloorLock()  -  same as CtdlPutFloor() but unlocks the record (if supported)
617  */
618 void CtdlPutFloorLock(struct floor *flbuf, int floor_num)
619 {
620         CtdlPutFloor(flbuf, floor_num);
621         end_critical_section(S_FLOORTAB);
622
623 }
624
625
626 /*
627  * lputfloor()  -  same as CtdlPutFloor() but unlocks the record (if supported)
628  */
629 void lputfloor(struct floor *flbuf, int floor_num)
630 {
631         CtdlPutFloorLock(flbuf, floor_num);
632 }
633
634
635 /* 
636  * Iterate through the room table, performing a callback for each room.
637  */
638 void CtdlForEachRoom(ForEachRoomCallBack CB, void *in_data)
639 {
640         struct ctdlroom qrbuf;
641         struct cdbdata *cdbqr;
642
643         cdb_rewind(CDB_ROOMS);
644
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)
650                 );
651                 cdb_free(cdbqr);
652                 room_sanity_check(&qrbuf);
653                 if (qrbuf.QRflags & QR_INUSE) {
654                         CB(&qrbuf, in_data);
655                 }
656         }
657 }
658
659 /* 
660  * Iterate through the room table, performing a callback for each room that has a netconfig entry.
661  */
662 void CtdlForEachNetCfgRoom(ForEachRoomNetCfgCallBack CB, void *in_data)
663 {
664         struct ctdlroom qrbuf;
665         struct cdbdata *cdbqr;
666
667         cdb_rewind(CDB_ROOMS);
668
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));
672                 cdb_free(cdbqr);
673                 room_sanity_check(&qrbuf);
674                 if (qrbuf.QRflags & QR_INUSE)
675                 {
676                         OneRoomNetCfg *RNCfg;
677                         RNCfg = CtdlGetNetCfgForRoom(qrbuf.QRnumber);
678                         if (RNCfg != NULL)
679                         {
680                                 CB(&qrbuf, in_data, RNCfg);
681                                 FreeRoomNetworkStruct(&RNCfg);
682                         }
683                 }
684         }
685 }
686
687
688 /*
689  * delete_msglist()  -  delete room message pointers
690  */
691 void delete_msglist(struct ctdlroom *whichroom)
692 {
693         struct cdbdata *cdbml;
694
695         /* Make sure the msglist we're deleting actually exists, otherwise
696          * libdb will complain when we try to delete an invalid record
697          */
698         cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
699         if (cdbml != NULL) {
700                 cdb_free(cdbml);
701
702                 /* Go ahead and delete it */
703                 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
704         }
705 }
706
707
708 /*
709  * Message pointer compare function for sort_msglist()
710  */
711 int sort_msglist_cmp(const void *m1, const void *m2) {
712         if ((*(const long *)m1) > (*(const long *)m2)) return(1);
713         if ((*(const long *)m1) < (*(const long *)m2)) return(-1);
714         return(0);
715 }
716
717
718 /*
719  * sort message pointers
720  * (returns new msg count)
721  */
722 int sort_msglist(long listptrs[], int oldcount)
723 {
724         int numitems;
725         int i = 0;
726
727         numitems = oldcount;
728         if (numitems < 2) {
729                 return (oldcount);
730         }
731
732         /* do the sort */
733         qsort(listptrs, numitems, sizeof(long), sort_msglist_cmp);
734
735         /* and yank any nulls */
736         while ((i < numitems) && (listptrs[i] == 0L)) i++;
737
738         if (i > 0)
739         {
740                 memmove(&listptrs[0], &listptrs[i], (sizeof(long) * (numitems - i)));
741                 numitems-=i;
742         }
743
744         return (numitems);
745 }
746
747
748 /*
749  * Determine whether a given room is non-editable.
750  */
751 int CtdlIsNonEditable(struct ctdlroom *qrbuf)
752 {
753
754         /* Mail> rooms are non-editable */
755         if ( (qrbuf->QRflags & QR_MAILBOX)
756              && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
757                 return (1);
758
759         /* Everything else is editable */
760         return (0);
761 }
762
763
764
765 /*
766  * Make the specified room the current room for this session.  No validation
767  * or access control is done here -- the caller should make sure that the
768  * specified room exists and is ok to access.
769  */
770 void CtdlUserGoto(char *where, int display_result, int transiently,
771                 int *retmsgs, int *retnew, long *retoldest, long *retnewest)
772 {
773         struct CitContext *CCC = CC;
774         int a;
775         int new_messages = 0;
776         int old_messages = 0;
777         int total_messages = 0;
778         long oldest_message = 0;
779         long newest_message = 0;
780         int info = 0;
781         int rmailflag;
782         int raideflag;
783         int newmailcount = 0;
784         visit vbuf;
785         char truncated_roomname[ROOMNAMELEN];
786         struct cdbdata *cdbfr;
787         long *msglist = NULL;
788         int num_msgs = 0;
789         unsigned int original_v_flags;
790         int num_sets;
791         int s;
792         char setstr[128], lostr[64], histr[64];
793         long lo, hi;
794         int is_trash = 0;
795
796         /* If the supplied room name is NULL, the caller wants us to know that
797          * it has already copied the room record into CC->room, so
798          * we can skip the extra database fetch.
799          */
800         if (where != NULL) {
801                 safestrncpy(CCC->room.QRname, where, sizeof CCC->room.QRname);
802                 CtdlGetRoom(&CCC->room, where);
803         }
804
805         /* Take care of all the formalities. */
806
807         begin_critical_section(S_USERS);
808         CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
809         original_v_flags = vbuf.v_flags;
810
811         /* Know the room ... but not if it's the page log room, or if the
812          * caller specified that we're only entering this room transiently.
813          */
814         if ((strcasecmp(CCC->room.QRname, CtdlGetConfigStr("c_logpages"))) && (transiently == 0))
815         {
816                 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
817                 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
818         }
819         
820         /* Only rewrite the database record if we changed something */
821         if (vbuf.v_flags != original_v_flags) {
822                 CtdlSetRelationship(&vbuf, &CCC->user, &CCC->room);
823         }
824         end_critical_section(S_USERS);
825
826         /* Check for new mail */
827         newmailcount = NewMailCount();
828
829         /* Set info to 1 if the room banner is new since our last visit.
830          * Some clients only want to display it when it changes.
831          */
832         if (CCC->room.msgnum_info > vbuf.v_lastseen) {
833                 info = 1;
834         }
835
836         cdbfr = cdb_fetch(CDB_MSGLISTS, &CCC->room.QRnumber, sizeof(long));
837         if (cdbfr != NULL) {
838                 msglist = (long *) cdbfr->ptr;
839                 cdbfr->ptr = NULL;      /* CtdlUserGoto() now owns this memory */
840                 num_msgs = cdbfr->len / sizeof(long);
841                 cdb_free(cdbfr);
842         }
843
844         total_messages = 0;
845         for (a=0; a<num_msgs; ++a) {
846                 if (msglist[a] > 0L) ++total_messages;
847         }
848
849         if (total_messages > 0) {
850                 oldest_message = msglist[0];
851                 newest_message = msglist[num_msgs - 1];
852         }
853
854         num_sets = num_tokens(vbuf.v_seen, ',');
855         for (s=0; s<num_sets; ++s) {
856                 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
857
858                 extract_token(lostr, setstr, 0, ':', sizeof lostr);
859                 if (num_tokens(setstr, ':') >= 2) {
860                         extract_token(histr, setstr, 1, ':', sizeof histr);
861                         if (!strcmp(histr, "*")) {
862                                 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
863                         }
864                 } 
865                 else {
866                         strcpy(histr, lostr);
867                 }
868                 lo = atol(lostr);
869                 hi = atol(histr);
870
871                 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
872                         if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
873                                 ++old_messages;
874                                 msglist[a] = 0L;
875                         }
876                 }
877         }
878         new_messages = total_messages - old_messages;
879
880         if (msglist != NULL) free(msglist);
881
882         if (CCC->room.QRflags & QR_MAILBOX)
883                 rmailflag = 1;
884         else
885                 rmailflag = 0;
886
887         if ((CCC->room.QRroomaide == CCC->user.usernum)
888             || (CCC->user.axlevel >= AxAideU))
889                 raideflag = 1;
890         else
891                 raideflag = 0;
892
893         safestrncpy(truncated_roomname, CCC->room.QRname, sizeof truncated_roomname);
894         if ( (CCC->room.QRflags & QR_MAILBOX)
895            && (atol(CCC->room.QRname) == CCC->user.usernum) ) {
896                 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
897         }
898
899         if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
900                 is_trash = 1;
901         }
902
903         if (retmsgs != NULL) *retmsgs = total_messages;
904         if (retnew != NULL) *retnew = new_messages;
905         if (retoldest != NULL) *retoldest = oldest_message;
906         if (retnewest != NULL) *retnewest = newest_message;
907         MSG_syslog(LOG_DEBUG, "<%s> %d new of %d total messages, oldest=%ld, newest=%ld",
908                    CCC->room.QRname, new_messages, total_messages, oldest_message, newest_message
909         );
910
911         CCC->curr_view = (int)vbuf.v_view;
912
913         if (display_result) {
914                 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|%d|%ld|\n",
915                         CIT_OK, CtdlCheckExpress(),
916                         truncated_roomname,
917                         (int)new_messages,
918                         (int)total_messages,
919                         (int)info,
920                         (int)CCC->room.QRflags,
921                         (long)CCC->room.QRhighest,
922                         (long)vbuf.v_lastseen,
923                         (int)rmailflag,
924                         (int)raideflag,
925                         (int)newmailcount,
926                         (int)CCC->room.QRfloor,
927                         (int)vbuf.v_view,
928                         (int)CCC->room.QRdefaultview,
929                         (int)is_trash,
930                         (int)CCC->room.QRflags2,
931                         (long)CCC->room.QRmtime
932                 );
933         }
934 }
935
936
937 /*
938  * Handle some of the macro named rooms
939  */
940 void convert_room_name_macros(char *towhere, size_t maxlen) {
941         if (!strcasecmp(towhere, "_BASEROOM_")) {
942                 safestrncpy(towhere, CtdlGetConfigStr("c_baseroom"), maxlen);
943         }
944         else if (!strcasecmp(towhere, "_MAIL_")) {
945                 safestrncpy(towhere, MAILROOM, maxlen);
946         }
947         else if (!strcasecmp(towhere, "_TRASH_")) {
948                 safestrncpy(towhere, USERTRASHROOM, maxlen);
949         }
950         else if (!strcasecmp(towhere, "_DRAFTS_")) {
951                 safestrncpy(towhere, USERDRAFTROOM, maxlen);
952         }
953         else if (!strcasecmp(towhere, "_BITBUCKET_")) {
954                 safestrncpy(towhere, CtdlGetConfigStr("c_twitroom"), maxlen);
955         }
956         else if (!strcasecmp(towhere, "_CALENDAR_")) {
957                 safestrncpy(towhere, USERCALENDARROOM, maxlen);
958         }
959         else if (!strcasecmp(towhere, "_TASKS_")) {
960                 safestrncpy(towhere, USERTASKSROOM, maxlen);
961         }
962         else if (!strcasecmp(towhere, "_CONTACTS_")) {
963                 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
964         }
965         else if (!strcasecmp(towhere, "_NOTES_")) {
966                 safestrncpy(towhere, USERNOTESROOM, maxlen);
967         }
968 }
969
970
971 /*
972  * Back end function to rename a room.
973  * You can also specify which floor to move the room to, or specify -1 to
974  * keep the room on the same floor it was on.
975  *
976  * If you are renaming a mailbox room, you must supply the namespace prefix
977  * in *at least* the old name!
978  */
979 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
980         struct CitContext *CCC = CC;
981         int old_floor = 0;
982         struct ctdlroom qrbuf;
983         struct ctdlroom qrtmp;
984         int ret = 0;
985         struct floor *fl;
986         struct floor flbuf;
987         long owner = 0L;
988         char actual_old_name[ROOMNAMELEN];
989
990         MSG_syslog(LOG_DEBUG, "CtdlRenameRoom(%s, %s, %d)", old_name, new_name, new_floor);
991
992         if (new_floor >= 0) {
993                 fl = CtdlGetCachedFloor(new_floor);
994                 if ((fl->f_flags & F_INUSE) == 0) {
995                         return(crr_invalid_floor);
996                 }
997         }
998
999         begin_critical_section(S_ROOMS);
1000
1001         if ( (CtdlGetRoom(&qrtmp, new_name) == 0) 
1002            && (strcasecmp(new_name, old_name)) ) {
1003                 ret = crr_already_exists;
1004         }
1005
1006         else if (CtdlGetRoom(&qrbuf, old_name) != 0) {
1007                 ret = crr_room_not_found;
1008         }
1009
1010         else if ( (CCC->user.axlevel < AxAideU) && (!CCC->internal_pgm)
1011                   && (CCC->user.usernum != qrbuf.QRroomaide)
1012                   && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CCC->user.usernum))) )  {
1013                 ret = crr_access_denied;
1014         }
1015
1016         else if (CtdlIsNonEditable(&qrbuf)) {
1017                 ret = crr_noneditable;
1018         }
1019
1020         else {
1021                 /* Rename it */
1022                 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1023                 if (qrbuf.QRflags & QR_MAILBOX) {
1024                         owner = atol(qrbuf.QRname);
1025                 }
1026                 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1027                         snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1028                                         "%010ld.%s", owner, new_name);
1029                 }
1030                 else {
1031                         safestrncpy(qrbuf.QRname, new_name,
1032                                                 sizeof(qrbuf.QRname));
1033                 }
1034
1035                 /* Reject change of floor for baseroom/aideroom */
1036                 if (!strncasecmp(old_name, CtdlGetConfigStr("c_baseroom"), ROOMNAMELEN) ||
1037                     !strncasecmp(old_name, CtdlGetConfigStr("c_aideroom"), ROOMNAMELEN))
1038                 {
1039                         new_floor = 0;
1040                 }
1041
1042                 /* Take care of floor stuff */
1043                 old_floor = qrbuf.QRfloor;
1044                 if (new_floor < 0) {
1045                         new_floor = old_floor;
1046                 }
1047                 qrbuf.QRfloor = new_floor;
1048                 CtdlPutRoom(&qrbuf);
1049
1050                 begin_critical_section(S_CONFIG);
1051         
1052                 /* If baseroom/aideroom name changes, update config */
1053                 if (!strncasecmp(old_name, CtdlGetConfigStr("c_baseroom"), ROOMNAMELEN)) {
1054                         CtdlSetConfigStr("c_baseroom", new_name);
1055                 }
1056                 if (!strncasecmp(old_name, CtdlGetConfigStr("c_aideroom"), ROOMNAMELEN)) {
1057                         CtdlSetConfigStr("c_aideroom", new_name);
1058                 }
1059         
1060                 end_critical_section(S_CONFIG);
1061         
1062                 /* If the room name changed, then there are now two room
1063                  * records, so we have to delete the old one.
1064                  */
1065                 if (strcasecmp(new_name, old_name)) {
1066                         b_deleteroom(actual_old_name);
1067                 }
1068
1069                 ret = crr_ok;
1070         }
1071
1072         end_critical_section(S_ROOMS);
1073
1074         /* Adjust the floor reference counts if necessary */
1075         if (new_floor != old_floor) {
1076                 lgetfloor(&flbuf, old_floor);
1077                 --flbuf.f_ref_count;
1078                 lputfloor(&flbuf, old_floor);
1079                 MSG_syslog(LOG_DEBUG, "Reference count for floor %d is now %d", old_floor, flbuf.f_ref_count);
1080                 lgetfloor(&flbuf, new_floor);
1081                 ++flbuf.f_ref_count;
1082                 lputfloor(&flbuf, new_floor);
1083                 MSG_syslog(LOG_DEBUG, "Reference count for floor %d is now %d", new_floor, flbuf.f_ref_count);
1084         }
1085
1086         /* ...and everybody say "YATTA!" */     
1087         return(ret);
1088 }
1089
1090
1091 /*
1092  * Asynchronously schedule a room for deletion.  By placing the room into an invalid private namespace,
1093  * the room will appear deleted to the user(s), but the session doesn't need to block while waiting for
1094  * database operations to complete.  Instead, the room gets purged when THE DREADED AUTO-PURGER makes
1095  * its next run.  Aren't we so clever?!!
1096  */
1097 void CtdlScheduleRoomForDeletion(struct ctdlroom *qrbuf)
1098 {
1099         char old_name[ROOMNAMELEN];
1100         static int seq = 0;
1101
1102         syslog(LOG_NOTICE, "Scheduling room <%s> for deletion", qrbuf->QRname);
1103
1104         safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1105         CtdlGetRoom(qrbuf, qrbuf->QRname);
1106
1107         /* Turn the room into a private mailbox owned by a user who doesn't
1108          * exist.  This will immediately make the room invisible to everyone,
1109          * and qualify the room for purging.
1110          */
1111         snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1112                 time(NULL),
1113                 ++seq,
1114                 old_name
1115         );
1116         qrbuf->QRflags |= QR_MAILBOX;
1117         time(&qrbuf->QRgen);    /* Use a timestamp as the new generation number  */
1118         CtdlPutRoom(qrbuf);
1119         b_deleteroom(old_name);
1120 }
1121
1122
1123
1124 /*
1125  * Back end processing to delete a room and everything associated with it
1126  * (This one is synchronous and should only get called by THE DREADED
1127  * AUTO-PURGER in serv_expire.c.  All user-facing code should call
1128  * the asynchronous schedule_room_for_deletion() instead.)
1129  */
1130 void CtdlDeleteRoom(struct ctdlroom *qrbuf)
1131 {
1132         struct floor flbuf;
1133         char filename[PATH_MAX];
1134         char configdbkeyname[25];
1135
1136         syslog(LOG_NOTICE, "Deleting room <%s>", qrbuf->QRname);
1137
1138         /* Delete the info file */
1139         assoc_file_name(filename, sizeof filename, qrbuf, ctdl_info_dir);
1140         unlink(filename);
1141
1142         /* Delete the image file */
1143         assoc_file_name(filename, sizeof filename, qrbuf, ctdl_image_dir);
1144         unlink(filename);
1145
1146         /* Delete the room's network configdb entry */
1147         netcfg_keyname(configdbkeyname, qrbuf->QRnumber);
1148         CtdlDelConfig(configdbkeyname);
1149
1150         /* Delete the messages in the room
1151          * (Careful: this opens an S_ROOMS critical section!)
1152          */
1153         CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
1154
1155         /* Flag the room record as not in use */
1156         CtdlGetRoomLock(qrbuf, qrbuf->QRname);
1157         qrbuf->QRflags = 0;
1158         CtdlPutRoomLock(qrbuf);
1159
1160         /* then decrement the reference count for the floor */
1161         lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1162         flbuf.f_ref_count = flbuf.f_ref_count - 1;
1163         lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1164
1165         /* Delete the room record from the database! */
1166         b_deleteroom(qrbuf->QRname);
1167 }
1168
1169
1170 /*
1171  * Check access control for deleting a room
1172  */
1173 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1174
1175         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1176                 return(0);
1177         }
1178
1179         if (CtdlIsNonEditable(qr)) {
1180                 return(0);
1181         }
1182
1183         /*
1184          * For mailboxes, check stuff
1185          */
1186         if (qr->QRflags & QR_MAILBOX) {
1187
1188                 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1189
1190                 if (atol(qr->QRname) != CC->user.usernum) {
1191                         return(0);      /* not my room */
1192                 }
1193
1194                 /* Can't delete your Mail> room */
1195                 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1196
1197                 /* Otherwise it's ok */
1198                 return(1);
1199         }
1200
1201         /*
1202          * For normal rooms, just check for admin or room admin status.
1203          */
1204         return(is_room_aide());
1205 }
1206
1207
1208
1209 /*
1210  * Internal code to create a new room (returns room flags)
1211  *
1212  * Room types:  0=public, 1=hidden, 2=passworded, 3=invitation-only,
1213  *              4=mailbox, 5=mailbox, but caller supplies namespace
1214  */
1215 unsigned CtdlCreateRoom(char *new_room_name,
1216                      int new_room_type,
1217                      char *new_room_pass,
1218                      int new_room_floor,
1219                      int really_create,
1220                      int avoid_access,
1221                      int new_room_view)
1222 {
1223
1224         struct ctdlroom qrbuf;
1225         struct floor flbuf;
1226         visit vbuf;
1227
1228         MARK_syslog(LOG_DEBUG, "CtdlCreateRoom(name=%s, type=%d, view=%d)", new_room_name, new_room_type, new_room_view);
1229
1230         if (CtdlGetRoom(&qrbuf, new_room_name) == 0) {
1231                 MARK_syslog(LOG_DEBUG, "Cannot create room <%s> - already exists", new_room_name);
1232                 return(0);
1233         }
1234
1235         memset(&qrbuf, 0, sizeof(struct ctdlroom));
1236         safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1237         qrbuf.QRflags = QR_INUSE;
1238         if (new_room_type > 0)
1239                 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1240         if (new_room_type == 1)
1241                 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1242         if (new_room_type == 2)
1243                 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1244         if ( (new_room_type == 4) || (new_room_type == 5) ) {
1245                 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1246                 /* qrbuf.QRflags2 |= QR2_SUBJECTREQ; */
1247         }
1248
1249         /* If the user is requesting a personal room, set up the room
1250          * name accordingly (prepend the user number)
1251          */
1252         if (new_room_type == 4) {
1253                 CtdlMailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1254         }
1255         else {
1256                 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1257         }
1258
1259         /* If the room is private, and the system administrator has elected
1260          * to automatically grant room admin privileges, do so now.
1261          */
1262         if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1263                 qrbuf.QRroomaide = CC->user.usernum;
1264         }
1265         /* Blog owners automatically become room admins of their blogs.
1266          * (In the future we will offer a site-wide configuration setting to suppress this behavior.)
1267          */
1268         else if (new_room_view == VIEW_BLOG) {
1269                 qrbuf.QRroomaide = CC->user.usernum;
1270         }
1271         /* Otherwise, set the room admin to undefined.
1272          */
1273         else {
1274                 qrbuf.QRroomaide = (-1L);
1275         }
1276
1277         /* 
1278          * If the caller is only interested in testing whether this will work,
1279          * return now without creating the room.
1280          */
1281         if (!really_create) return (qrbuf.QRflags);
1282
1283         qrbuf.QRnumber = get_new_room_number();
1284         qrbuf.QRhighest = 0L;   /* No messages in this room yet */
1285         time(&qrbuf.QRgen);     /* Use a timestamp as the generation number */
1286         qrbuf.QRfloor = new_room_floor;
1287         qrbuf.QRdefaultview = new_room_view;
1288
1289         /* save what we just did... */
1290         CtdlPutRoom(&qrbuf);
1291
1292         /* bump the reference count on whatever floor the room is on */
1293         lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1294         flbuf.f_ref_count = flbuf.f_ref_count + 1;
1295         lputfloor(&flbuf, (int) qrbuf.QRfloor);
1296
1297         /* Grant the creator access to the room unless the avoid_access
1298          * parameter was specified.
1299          */
1300         if ( (CC->logged_in) && (avoid_access == 0) ) {
1301                 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1302                 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1303                 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1304                 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1305         }
1306
1307         /* resume our happy day */
1308         return (qrbuf.QRflags);
1309 }