0d90bc3b300ea0eb7bb73dce6565f95e8fd56bdc
[citadel.git] / citadel / room_ops.c
1 /* 
2  * Server functions which perform operations on room objects.
3  *
4  * Copyright (c) 1987-2012 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 "sysdep.h"
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <stdio.h>
19 #include <sys/stat.h>
20 #include <ctype.h>
21 #include <string.h>
22 #include <dirent.h>     /* for cmd_rdir to read contents of the directory */
23
24 #if TIME_WITH_SYS_TIME
25 # include <sys/time.h>
26 # include <time.h>
27 #else
28 # if HAVE_SYS_TIME_H
29 #  include <sys/time.h>
30 # else
31 #  include <time.h>
32 # endif
33 #endif
34
35 #include <limits.h>
36 #include <errno.h>
37 #include "citadel.h"
38 #include <libcitadel.h>
39 #include "server.h"
40 #include "database.h"
41 #include "config.h"
42 #include "room_ops.h"
43 #include "sysdep_decls.h"
44 #include "support.h"
45 #include "msgbase.h"
46 #include "citserver.h"
47 #include "control.h"
48 #include "citadel_dirs.h"
49 #include "threads.h"
50
51 #include "ctdl_module.h"
52 #include "user_ops.h"
53
54 struct floor *floorcache[MAXFLOORS];
55
56 /* 
57  * Determine whether the currently logged in session has permission to read
58  * messages in the current room.
59  */
60 int CtdlDoIHavePermissionToReadMessagesInThisRoom(void) {
61         if (    (!(CC->logged_in))
62                 && (!(CC->internal_pgm))
63                 && (!config.c_guest_logins)
64         ) {
65                 return(om_not_logged_in);
66         }
67         return(om_ok);
68 }
69
70 /*
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.
74  */
75 int CtdlDoIHavePermissionToPostInThisRoom(
76         char *errmsgbuf, 
77         size_t n, 
78         const char* RemoteIdentifier,
79         PostType PostPublic,
80         int is_reply
81         ) {
82         int ra;
83
84         if (!(CC->logged_in) && 
85             (PostPublic == POST_LOGGED_IN)) {
86                 snprintf(errmsgbuf, n, "Not logged in.");
87                 return (ERROR + NOT_LOGGED_IN);
88         }
89         else if (PostPublic == CHECK_EXISTANCE) {
90                 return (0); // We're Evaling whether a recipient exists
91         }
92         else if (!(CC->logged_in)) {
93                 
94                 if ((CC->room.QRflags & QR_READONLY)) {
95                         snprintf(errmsgbuf, n, "Not logged in.");
96                         return (ERROR + NOT_LOGGED_IN);
97                 }
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);
101                 }
102                 if ((PostPublic!=POST_LMTP) &&(CC->room.QRflags2 & QR2_SMTP_PUBLIC) == 0) {
103
104                         return CtdlNetconfigCheckRoomaccess(errmsgbuf, n, RemoteIdentifier);
105                 }
106                 return (0);
107
108         }
109
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);
114         }
115
116         CtdlRoomAccess(&CC->room, &CC->user, &ra, NULL);
117
118         if (ra & UA_POSTALLOWED) {
119                 strcpy(errmsgbuf, "OK to post or reply here");
120                 return(0);
121         }
122
123         if ( (ra & UA_REPLYALLOWED) && (is_reply) ) {
124                 /*
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.
128                  */
129                 strcpy(errmsgbuf, "OK to reply here");
130                 return(0);
131         }
132
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);
137         }
138
139         snprintf(errmsgbuf, n, "Higher access is required to post in this room.");
140         return (ERROR + HIGHER_ACCESS_REQUIRED);
141
142 }
143
144 /*
145  * Check whether the current user has permission to delete messages from
146  * the current room (returns 1 for yes, 0 for no)
147  */
148 int CtdlDoIHavePermissionToDeleteMessagesFromThisRoom(void) {
149         int ra;
150         CtdlRoomAccess(&CC->room, &CC->user, &ra, NULL);
151         if (ra & UA_DELETEALLOWED) return(1);
152         return(0);
153 }
154
155 /*
156  * Retrieve access control information for any user/room pair
157  */
158 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf,
159                 int *result, int *view)
160 {
161         int retval = 0;
162         visit vbuf;
163         int is_me = 0;
164         int is_guest = 0;
165
166         if (userbuf == &CC->user) {
167                 is_me = 1;
168         }
169
170         if ((is_me) && (config.c_guest_logins) && (!CC->logged_in)) {
171                 is_guest = 1;
172         }
173
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);
177                 vbuf.v_view = 0;
178                 goto SKIP_EVERYTHING;
179         }
180
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);
184                 vbuf.v_view = 0;
185                 goto SKIP_EVERYTHING;
186         }
187
188         /* Locate any applicable user/room relationships */
189         if (is_guest) {
190                 memset(&vbuf, 0, sizeof vbuf);
191         }
192         else {
193                 CtdlGetRelationship(&vbuf, userbuf, roombuf);
194         }
195
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;
200                 } else {
201                         retval = 0;
202                 }
203                 goto NEWMSG;
204         }
205
206         /* If this is a public room, it's accessible... */
207         if (    ((roombuf->QRflags & QR_PRIVATE) == 0) 
208                 && ((roombuf->QRflags & QR_MAILBOX) == 0)
209         ) {
210                 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
211         }
212
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;
217                 }
218         }
219
220         /* For private rooms, check the generation number matchups */
221         if (    (roombuf->QRflags & QR_PRIVATE) 
222                 && ((roombuf->QRflags & QR_MAILBOX) == 0)
223         ) {
224
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;
228                 }
229                 /* Otherwise, check if this is a guess-name or passworded
230                  * room.  If it is, a goto may at least be attempted
231                  */
232                 else if (       (roombuf->QRflags & QR_PRIVATE)
233                                 || (roombuf->QRflags & QR_PASSWORDED)
234                 ) {
235                         retval = retval & ~UA_KNOWN;
236                         retval = retval | UA_GOTOALLOWED;
237                 }
238         }
239
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;
245                 }
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;
249                 }
250         }
251
252         /* For non-mailbox rooms... */
253         else {
254
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)
260                  */
261                 int post_allowed = 1;
262                 int reply_allowed = 1;
263                 if (userbuf->axlevel < AxProbU) {
264                         post_allowed = 0;
265                         reply_allowed = 0;
266                 }
267                 if ((userbuf->axlevel < AxNetU) && (roombuf->QRflags & QR_NETWORK)) {
268                         post_allowed = 0;
269                         reply_allowed = 0;
270                 }
271                 if (roombuf->QRflags & QR_READONLY) {
272                         post_allowed = 0;
273                         reply_allowed = 0;
274                 }
275                 if (roombuf->QRdefaultview == VIEW_BLOG) {
276                         post_allowed = 0;
277                 }
278                 if (post_allowed) {
279                         retval = retval | UA_POSTALLOWED | UA_REPLYALLOWED;
280                 }
281                 if (reply_allowed) {
282                         retval = retval | UA_REPLYALLOWED;
283                 }
284
285                 /* If "collaborative deletion" is active for this room, any user who can post
286                  * is also allowed to delete
287                  */
288                 if (roombuf->QRflags2 & QR2_COLLABDEL) {
289                         if (retval & UA_POSTALLOWED) {
290                                 retval = retval | UA_DELETEALLOWED;
291                         }
292                 }
293
294         }
295
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))
303                 ) {
304                         retval = retval | UA_ZAPPED;
305                 }
306         }
307
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;
311         }
312
313         /* Aides get access to all private rooms */
314         if (    (userbuf->axlevel >= AxAideU)
315                 && ((roombuf->QRflags & QR_MAILBOX) == 0)
316         ) {
317                 if (vbuf.v_flags & V_FORGET) {
318                         retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
319                 }
320                 else {
321                         retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
322                 }
323         }
324
325         /* Aides can gain access to mailboxes as well, but they don't show
326          * by default.
327          */
328         if (    (userbuf->axlevel >= AxAideU)
329                 && (roombuf->QRflags & QR_MAILBOX)
330         ) {
331                 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
332         }
333
334         /* Aides and Room Aides have admin privileges */
335         if (    (userbuf->axlevel >= AxAideU)
336                 || (userbuf->usernum == roombuf->QRroomaide)
337         ) {
338                 retval = retval | UA_ADMINALLOWED | UA_DELETEALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
339         }
340
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;
344         }
345
346         /* System rooms never show up in the list. */
347         if (roombuf->QRflags2 & QR2_SYSTEM) {
348                 retval = retval & ~UA_KNOWN;
349         }
350
351 SKIP_EVERYTHING:
352         /* Now give the caller the information it wants. */
353         if (result != NULL) *result = retval;
354         if (view != NULL) *view = vbuf.v_view;
355 }
356
357
358 /*
359  * Self-checking stuff for a room record read into memory
360  */
361 void room_sanity_check(struct ctdlroom *qrbuf)
362 {
363         /* Mailbox rooms are always on the lowest floor */
364         if (qrbuf->QRflags & QR_MAILBOX) {
365                 qrbuf->QRfloor = 0;
366         }
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)
371                     &&
372                     strncasecmp(qrbuf->QRname, config.c_aideroom, ROOMNAMELEN))
373                         qrbuf->QRorder = 64;
374 }
375
376
377 /*
378  * CtdlGetRoom()  -  retrieve room data from disk
379  */
380 int CtdlGetRoom(struct ctdlroom *qrbuf, const char *room_name)
381 {
382         struct cdbdata *cdbqr;
383         char lowercase_name[ROOMNAMELEN];
384         char personal_lowercase_name[ROOMNAMELEN];
385         const char *sptr;
386         char *dptr, *eptr;
387
388         dptr = lowercase_name;
389         sptr = room_name;
390         eptr = (dptr + (sizeof lowercase_name - 1));
391         while (!IsEmptyStr(sptr) && (dptr < eptr)){
392                 *dptr = tolower(*sptr);
393                 sptr++; dptr++;
394         }
395         *dptr = '\0';
396
397         memset(qrbuf, 0, sizeof(struct ctdlroom));
398
399         /* First, try the public namespace */
400         cdbqr = cdb_fetch(CDB_ROOMS,
401                           lowercase_name, strlen(lowercase_name));
402
403         /* If that didn't work, try the user's personal namespace */
404         if (cdbqr == NULL) {
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));
411         }
412         if (cdbqr != NULL) {
413                 memcpy(qrbuf, cdbqr->ptr,
414                        ((cdbqr->len > sizeof(struct ctdlroom)) ?
415                         sizeof(struct ctdlroom) : cdbqr->len));
416                 cdb_free(cdbqr);
417
418                 room_sanity_check(qrbuf);
419
420                 return (0);
421         } else {
422                 return (1);
423         }
424 }
425
426 /*
427  * CtdlGetRoomLock()  -  same as getroom() but locks the record (if supported)
428  */
429 int CtdlGetRoomLock(struct ctdlroom *qrbuf, char *room_name)
430 {
431         register int retval;
432         retval = CtdlGetRoom(qrbuf, room_name);
433         if (retval == 0) begin_critical_section(S_ROOMS);
434         return(retval);
435 }
436
437
438 /*
439  * b_putroom()  -  back end to putroom() and b_deleteroom()
440  *              (if the supplied buffer is NULL, delete the room record)
441  */
442 void b_putroom(struct ctdlroom *qrbuf, char *room_name)
443 {
444         char lowercase_name[ROOMNAMELEN];
445         char *aptr, *bptr;
446         long len;
447
448         aptr = room_name;
449         bptr = lowercase_name;
450         while (!IsEmptyStr(aptr))
451         {
452                 *bptr = tolower(*aptr);
453                 aptr++;
454                 bptr++;
455         }
456         *bptr='\0';
457
458         len = bptr - lowercase_name;
459         if (qrbuf == NULL) {
460                 cdb_delete(CDB_ROOMS, lowercase_name, len);
461         } else {
462                 time(&qrbuf->QRmtime);
463                 cdb_store(CDB_ROOMS, lowercase_name, len, qrbuf, sizeof(struct ctdlroom));
464         }
465 }
466
467
468 /* 
469  * CtdlPutRoom()  -  store room data to disk
470  */
471 void CtdlPutRoom(struct ctdlroom *qrbuf) {
472         b_putroom(qrbuf, qrbuf->QRname);
473 }
474
475
476 /*
477  * b_deleteroom()  -  delete a room record from disk
478  */
479 void b_deleteroom(char *room_name) {
480         b_putroom(NULL, room_name);
481 }
482
483
484 /*
485  * CtdlPutRoomLock()  -  same as CtdlPutRoom() but unlocks the record (if supported)
486  */
487 void CtdlPutRoomLock(struct ctdlroom *qrbuf)
488 {
489
490         CtdlPutRoom(qrbuf);
491         end_critical_section(S_ROOMS);
492
493 }
494
495
496 /*
497  * CtdlGetFloorByName()  -  retrieve the number of the named floor
498  * return < 0 if not found else return floor number
499  */
500 int CtdlGetFloorByName(const char *floor_name)
501 {
502         int a;
503         struct floor *flbuf = NULL;
504
505         for (a = 0; a < MAXFLOORS; ++a) {
506                 flbuf = CtdlGetCachedFloor(a);
507
508                 /* check to see if it already exists */
509                 if ((!strcasecmp(flbuf->f_name, floor_name))
510                     && (flbuf->f_flags & F_INUSE)) {
511                         return a;
512                 }
513         }
514         return -1;
515 }
516
517
518 /*
519  * CtdlGetFloorByNameLock()  -  retrieve floor number for given floor and lock the floor list.
520  */
521 int CtdlGetFloorByNameLock(const char *floor_name)
522 {
523         begin_critical_section(S_FLOORTAB);
524         return CtdlGetFloorByName(floor_name);
525 }
526
527
528
529 /*
530  * CtdlGetAvailableFloor()  -  Return number of first unused floor
531  * return < 0 if none available
532  */
533 int CtdlGetAvailableFloor(void)
534 {
535         int a;
536         struct floor *flbuf = NULL;
537
538         for (a = 0; a < MAXFLOORS; a++) {
539                 flbuf = CtdlGetCachedFloor(a);
540
541                 /* check to see if it already exists */
542                 if ((flbuf->f_flags & F_INUSE) == 0) {
543                         return a;
544                 }
545         }
546         return -1;
547 }
548
549
550 /*
551  * CtdlGetFloor()  -  retrieve floor data from disk
552  */
553 void CtdlGetFloor(struct floor *flbuf, int floor_num)
554 {
555         struct cdbdata *cdbfl;
556
557         memset(flbuf, 0, sizeof(struct floor));
558         cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
559         if (cdbfl != NULL) {
560                 memcpy(flbuf, cdbfl->ptr,
561                        ((cdbfl->len > sizeof(struct floor)) ?
562                         sizeof(struct floor) : cdbfl->len));
563                 cdb_free(cdbfl);
564         } else {
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;
570                 }
571         }
572
573 }
574
575
576 /*
577  * lgetfloor()  -  same as CtdlGetFloor() but locks the record (if supported)
578  */
579 void lgetfloor(struct floor *flbuf, int floor_num)
580 {
581
582         begin_critical_section(S_FLOORTAB);
583         CtdlGetFloor(flbuf, floor_num);
584 }
585
586
587 /*
588  * CtdlGetCachedFloor()  -  Get floor record from *cache* (loads from disk if needed)
589  *    
590  * This is strictly a performance hack.
591  */
592 struct floor *CtdlGetCachedFloor(int floor_num) {
593         static int initialized = 0;
594         int i;
595         int fetch_new = 0;
596         struct floor *fl = NULL;
597
598         begin_critical_section(S_FLOORCACHE);
599         if (initialized == 0) {
600                 for (i=0; i<MAXFLOORS; ++i) {
601                         floorcache[floor_num] = NULL;
602                 }
603         initialized = 1;
604         }
605         if (floorcache[floor_num] == NULL) {
606                 fetch_new = 1;
607         }
608         end_critical_section(S_FLOORCACHE);
609
610         if (fetch_new) {
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]);
616                 }
617                 floorcache[floor_num] = fl;
618                 end_critical_section(S_FLOORCACHE);
619         }
620
621         return(floorcache[floor_num]);
622 }
623
624
625 /*
626  * CtdlPutFloor()  -  store floor data on disk
627  */
628 void CtdlPutFloor(struct floor *flbuf, int floor_num)
629 {
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));
636         }
637         end_critical_section(S_FLOORCACHE);
638
639         cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
640                   flbuf, sizeof(struct floor));
641 }
642
643
644 /*
645  * CtdlPutFloorLock()  -  same as CtdlPutFloor() but unlocks the record (if supported)
646  */
647 void CtdlPutFloorLock(struct floor *flbuf, int floor_num)
648 {
649
650         CtdlPutFloor(flbuf, floor_num);
651         end_critical_section(S_FLOORTAB);
652
653 }
654
655
656 /*
657  * lputfloor()  -  same as CtdlPutFloor() but unlocks the record (if supported)
658  */
659 void lputfloor(struct floor *flbuf, int floor_num)
660 {
661         CtdlPutFloorLock(flbuf, floor_num);
662 }
663
664
665 /* 
666  *  Traverse the room file...
667  */
668 void CtdlForEachRoom(ForEachRoomCallBack CB, void *in_data)
669 {
670         struct ctdlroom qrbuf;
671         struct cdbdata *cdbqr;
672
673         cdb_rewind(CDB_ROOMS);
674
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)
680                 );
681                 cdb_free(cdbqr);
682                 room_sanity_check(&qrbuf);
683                 if (qrbuf.QRflags & QR_INUSE) {
684                         CB(&qrbuf, in_data);
685                 }
686         }
687 }
688
689 /* 
690  *  Traverse the room file...
691  */
692 void CtdlForEachNetCfgRoom(ForEachRoomNetCfgCallBack CB,
693                            void *in_data,
694                            RoomNetCfg filter)
695 {
696         struct ctdlroom qrbuf;
697         struct cdbdata *cdbqr;
698
699         cdb_rewind(CDB_ROOMS);
700
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)
706                 );
707                 cdb_free(cdbqr);
708                 room_sanity_check(&qrbuf);
709                 if (qrbuf.QRflags & QR_INUSE)
710                 {
711                         OneRoomNetCfg* RNCfg;
712                         RNCfg = CtdlGetNetCfgForRoom(qrbuf.QRnumber);
713                         if ((RNCfg != NULL) &&
714                             ((filter == maxRoomNetCfg) ||
715                              (RNCfg->NetConfigs[filter] != NULL)))
716                         {
717                                 CB(&qrbuf, in_data, RNCfg);
718                         }
719                 }
720         }
721 }
722
723
724 /*
725  * delete_msglist()  -  delete room message pointers
726  */
727 void delete_msglist(struct ctdlroom *whichroom)
728 {
729         struct cdbdata *cdbml;
730
731         /* Make sure the msglist we're deleting actually exists, otherwise
732          * libdb will complain when we try to delete an invalid record
733          */
734         cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
735         if (cdbml != NULL) {
736                 cdb_free(cdbml);
737
738                 /* Go ahead and delete it */
739                 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
740         }
741 }
742
743
744 /*
745  * Message pointer compare function for sort_msglist()
746  */
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);
750         return(0);
751 }
752
753
754 /*
755  * sort message pointers
756  * (returns new msg count)
757  */
758 int sort_msglist(long listptrs[], int oldcount)
759 {
760         int numitems;
761         int i = 0;
762
763         numitems = oldcount;
764         if (numitems < 2) {
765                 return (oldcount);
766         }
767
768         /* do the sort */
769         qsort(listptrs, numitems, sizeof(long), sort_msglist_cmp);
770
771         /* and yank any nulls */
772         while ((i < numitems) && (listptrs[i] == 0L)) i++;
773
774         if (i > 0)
775         {
776                 memmove(&listptrs[0], &listptrs[i], (sizeof(long) * (numitems - i)));
777                 numitems-=i;
778         }
779
780         return (numitems);
781 }
782
783
784 /*
785  * Determine whether a given room is non-editable.
786  */
787 int CtdlIsNonEditable(struct ctdlroom *qrbuf)
788 {
789
790         /* Mail> rooms are non-editable */
791         if ( (qrbuf->QRflags & QR_MAILBOX)
792              && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
793                 return (1);
794
795         /* Everything else is editable */
796         return (0);
797 }
798
799
800
801 /*
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.
805  */
806 void CtdlUserGoto(char *where, int display_result, int transiently,
807                 int *retmsgs, int *retnew)
808 {
809         struct CitContext *CCC = CC;
810         int a;
811         int new_messages = 0;
812         int old_messages = 0;
813         int total_messages = 0;
814         int info = 0;
815         int rmailflag;
816         int raideflag;
817         int newmailcount = 0;
818         visit vbuf;
819         char truncated_roomname[ROOMNAMELEN];
820         struct cdbdata *cdbfr;
821         long *msglist = NULL;
822         int num_msgs = 0;
823         unsigned int original_v_flags;
824         int num_sets;
825         int s;
826         char setstr[128], lostr[64], histr[64];
827         long lo, hi;
828         int is_trash = 0;
829
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.
833          */
834         if (where != NULL) {
835                 safestrncpy(CCC->room.QRname, where, sizeof CCC->room.QRname);
836                 CtdlGetRoom(&CCC->room, where);
837         }
838
839         /* Take care of all the formalities. */
840
841         begin_critical_section(S_USERS);
842         CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
843         original_v_flags = vbuf.v_flags;
844
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.
847          */
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;
852         }
853         
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);
857         }
858         end_critical_section(S_USERS);
859
860         /* Check for new mail */
861         newmailcount = NewMailCount();
862
863         /* set info to 1 if the user needs to read the room's info file */
864         if (CCC->room.QRinfo > vbuf.v_lastseen) {
865                 info = 1;
866         }
867
868         cdbfr = cdb_fetch(CDB_MSGLISTS, &CCC->room.QRnumber, sizeof(long));
869         if (cdbfr != NULL) {
870                 msglist = (long *) cdbfr->ptr;
871                 cdbfr->ptr = NULL;      /* CtdlUserGoto() now owns this memory */
872                 num_msgs = cdbfr->len / sizeof(long);
873                 cdb_free(cdbfr);
874         }
875
876         total_messages = 0;
877         for (a=0; a<num_msgs; ++a) {
878                 if (msglist[a] > 0L) ++total_messages;
879         }
880
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);
884
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);
890                         }
891                 } 
892                 else {
893                         strcpy(histr, lostr);
894                 }
895                 lo = atol(lostr);
896                 hi = atol(histr);
897
898                 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
899                         if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
900                                 ++old_messages;
901                                 msglist[a] = 0L;
902                         }
903                 }
904         }
905         new_messages = total_messages - old_messages;
906
907         if (msglist != NULL) free(msglist);
908
909         if (CCC->room.QRflags & QR_MAILBOX)
910                 rmailflag = 1;
911         else
912                 rmailflag = 0;
913
914         if ((CCC->room.QRroomaide == CCC->user.usernum)
915             || (CCC->user.axlevel >= AxAideU))
916                 raideflag = 1;
917         else
918                 raideflag = 0;
919
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);
924         }
925
926         if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
927                 is_trash = 1;
928         }
929
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",
933                    CCC->room.QRname,
934                    new_messages, total_messages
935                 );
936
937         CCC->curr_view = (int)vbuf.v_view;
938
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(),
942                         truncated_roomname,
943                         (int)new_messages,
944                         (int)total_messages,
945                         (int)info,
946                         (int)CCC->room.QRflags,
947                         (long)CCC->room.QRhighest,
948                         (long)vbuf.v_lastseen,
949                         (int)rmailflag,
950                         (int)raideflag,
951                         (int)newmailcount,
952                         (int)CCC->room.QRfloor,
953                         (int)vbuf.v_view,
954                         (int)CCC->room.QRdefaultview,
955                         (int)is_trash,
956                         (int)CCC->room.QRflags2,
957                         (long)CCC->room.QRmtime
958                 );
959         }
960 }
961
962
963 /*
964  * Handle some of the macro named rooms
965  */
966 void convert_room_name_macros(char *towhere, size_t maxlen) {
967         if (!strcasecmp(towhere, "_BASEROOM_")) {
968                 safestrncpy(towhere, config.c_baseroom, maxlen);
969         }
970         else if (!strcasecmp(towhere, "_MAIL_")) {
971                 safestrncpy(towhere, MAILROOM, maxlen);
972         }
973         else if (!strcasecmp(towhere, "_TRASH_")) {
974                 safestrncpy(towhere, USERTRASHROOM, maxlen);
975         }
976         else if (!strcasecmp(towhere, "_DRAFTS_")) {
977                 safestrncpy(towhere, USERDRAFTROOM, maxlen);
978         }
979         else if (!strcasecmp(towhere, "_BITBUCKET_")) {
980                 safestrncpy(towhere, config.c_twitroom, maxlen);
981         }
982         else if (!strcasecmp(towhere, "_CALENDAR_")) {
983                 safestrncpy(towhere, USERCALENDARROOM, maxlen);
984         }
985         else if (!strcasecmp(towhere, "_TASKS_")) {
986                 safestrncpy(towhere, USERTASKSROOM, maxlen);
987         }
988         else if (!strcasecmp(towhere, "_CONTACTS_")) {
989                 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
990         }
991         else if (!strcasecmp(towhere, "_NOTES_")) {
992                 safestrncpy(towhere, USERNOTESROOM, maxlen);
993         }
994 }
995
996
997 /*
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.
1001  *
1002  * If you are renaming a mailbox room, you must supply the namespace prefix
1003  * in *at least* the old name!
1004  */
1005 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1006         int old_floor = 0;
1007         struct ctdlroom qrbuf;
1008         struct ctdlroom qrtmp;
1009         int ret = 0;
1010         struct floor *fl;
1011         struct floor flbuf;
1012         long owner = 0L;
1013         char actual_old_name[ROOMNAMELEN];
1014
1015         syslog(LOG_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1016                 old_name, new_name, new_floor);
1017
1018         if (new_floor >= 0) {
1019                 fl = CtdlGetCachedFloor(new_floor);
1020                 if ((fl->f_flags & F_INUSE) == 0) {
1021                         return(crr_invalid_floor);
1022                 }
1023         }
1024
1025         begin_critical_section(S_ROOMS);
1026
1027         if ( (CtdlGetRoom(&qrtmp, new_name) == 0) 
1028            && (strcasecmp(new_name, old_name)) ) {
1029                 ret = crr_already_exists;
1030         }
1031
1032         else if (CtdlGetRoom(&qrbuf, old_name) != 0) {
1033                 ret = crr_room_not_found;
1034         }
1035
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;
1040         }
1041
1042         else if (CtdlIsNonEditable(&qrbuf)) {
1043                 ret = crr_noneditable;
1044         }
1045
1046         else {
1047                 /* Rename it */
1048                 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1049                 if (qrbuf.QRflags & QR_MAILBOX) {
1050                         owner = atol(qrbuf.QRname);
1051                 }
1052                 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1053                         snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1054                                         "%010ld.%s", owner, new_name);
1055                 }
1056                 else {
1057                         safestrncpy(qrbuf.QRname, new_name,
1058                                                 sizeof(qrbuf.QRname));
1059                 }
1060
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)) {
1064                         new_floor = 0;
1065                 }
1066
1067                 /* Take care of floor stuff */
1068                 old_floor = qrbuf.QRfloor;
1069                 if (new_floor < 0) {
1070                         new_floor = old_floor;
1071                 }
1072                 qrbuf.QRfloor = new_floor;
1073                 CtdlPutRoom(&qrbuf);
1074
1075                 begin_critical_section(S_CONFIG);
1076         
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);
1080                         put_config();
1081                 }
1082                 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1083                         safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1084                         put_config();
1085                 }
1086         
1087                 end_critical_section(S_CONFIG);
1088         
1089                 /* If the room name changed, then there are now two room
1090                  * records, so we have to delete the old one.
1091                  */
1092                 if (strcasecmp(new_name, old_name)) {
1093                         b_deleteroom(actual_old_name);
1094                 }
1095
1096                 ret = crr_ok;
1097         }
1098
1099         end_critical_section(S_ROOMS);
1100
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);
1111         }
1112
1113         /* ...and everybody say "YATTA!" */     
1114         return(ret);
1115 }
1116
1117
1118
1119 /*
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.
1123  */
1124 void CtdlScheduleRoomForDeletion(struct ctdlroom *qrbuf)
1125 {
1126         char old_name[ROOMNAMELEN];
1127         static int seq = 0;
1128
1129         syslog(LOG_NOTICE, "Scheduling room <%s> for deletion\n",
1130                 qrbuf->QRname);
1131
1132         safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1133
1134         CtdlGetRoom(qrbuf, qrbuf->QRname);
1135
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.
1139          */
1140         snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1141                 time(NULL),
1142                 ++seq,
1143                 old_name
1144         );
1145         qrbuf->QRflags |= QR_MAILBOX;
1146         time(&qrbuf->QRgen);    /* Use a timestamp as the new generation number  */
1147
1148         CtdlPutRoom(qrbuf);
1149
1150         b_deleteroom(old_name);
1151 }
1152
1153
1154
1155 /*
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.)
1160  */
1161 void CtdlDeleteRoom(struct ctdlroom *qrbuf)
1162 {
1163         struct floor flbuf;
1164         char filename[100];
1165         /* TODO: filename magic? does this realy work? */
1166
1167         syslog(LOG_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1168
1169         /* Delete the info file */
1170         assoc_file_name(filename, sizeof filename, qrbuf, ctdl_info_dir);
1171         unlink(filename);
1172
1173         /* Delete the image file */
1174         assoc_file_name(filename, sizeof filename, qrbuf, ctdl_image_dir);
1175         unlink(filename);
1176
1177         /* Delete the room's network config file */
1178         assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1179         unlink(filename);
1180
1181         /* Delete the messages in the room
1182          * (Careful: this opens an S_ROOMS critical section!)
1183          */
1184         CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
1185
1186         /* Flag the room record as not in use */
1187         CtdlGetRoomLock(qrbuf, qrbuf->QRname);
1188         qrbuf->QRflags = 0;
1189         CtdlPutRoomLock(qrbuf);
1190
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));
1195
1196         /* Delete the room record from the database! */
1197         b_deleteroom(qrbuf->QRname);
1198 }
1199
1200
1201
1202 /*
1203  * Check access control for deleting a room
1204  */
1205 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1206
1207         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1208                 return(0);
1209         }
1210
1211         if (CtdlIsNonEditable(qr)) {
1212                 return(0);
1213         }
1214
1215         /*
1216          * For mailboxes, check stuff
1217          */
1218         if (qr->QRflags & QR_MAILBOX) {
1219
1220                 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1221
1222                 if (atol(qr->QRname) != CC->user.usernum) {
1223                         return(0);      /* not my room */
1224                 }
1225
1226                 /* Can't delete your Mail> room */
1227                 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1228
1229                 /* Otherwise it's ok */
1230                 return(1);
1231         }
1232
1233         /*
1234          * For normal rooms, just check for admin or room admin status.
1235          */
1236         return(is_room_aide());
1237 }
1238
1239
1240
1241 /*
1242  * Internal code to create a new room (returns room flags)
1243  *
1244  * Room types:  0=public, 1=hidden, 2=passworded, 3=invitation-only,
1245  *              4=mailbox, 5=mailbox, but caller supplies namespace
1246  */
1247 unsigned CtdlCreateRoom(char *new_room_name,
1248                      int new_room_type,
1249                      char *new_room_pass,
1250                      int new_room_floor,
1251                      int really_create,
1252                      int avoid_access,
1253                      int new_room_view)
1254 {
1255
1256         struct ctdlroom qrbuf;
1257         struct floor flbuf;
1258         visit vbuf;
1259
1260         syslog(LOG_DEBUG, "CtdlCreateRoom(name=%s, type=%d, view=%d)\n",
1261                 new_room_name, new_room_type, new_room_view);
1262
1263         if (CtdlGetRoom(&qrbuf, new_room_name) == 0) {
1264                 syslog(LOG_DEBUG, "%s already exists.\n", new_room_name);
1265                 return(0);
1266         }
1267
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; */
1280         }
1281
1282         /* If the user is requesting a personal room, set up the room
1283          * name accordingly (prepend the user number)
1284          */
1285         if (new_room_type == 4) {
1286                 CtdlMailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1287         }
1288         else {
1289                 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1290         }
1291
1292         /* If the room is private, and the system administrator has elected
1293          * to automatically grant room admin privileges, do so now.
1294          */
1295         if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1296                 qrbuf.QRroomaide = CC->user.usernum;
1297         }
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.)
1300          */
1301         else if (new_room_view == VIEW_BLOG) {
1302                 qrbuf.QRroomaide = CC->user.usernum;
1303         }
1304         /* Otherwise, set the room admin to undefined.
1305          */
1306         else {
1307                 qrbuf.QRroomaide = (-1L);
1308         }
1309
1310         /* 
1311          * If the caller is only interested in testing whether this will work,
1312          * return now without creating the room.
1313          */
1314         if (!really_create) return (qrbuf.QRflags);
1315
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;
1321
1322         /* save what we just did... */
1323         CtdlPutRoom(&qrbuf);
1324
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);
1329
1330         /* Grant the creator access to the room unless the avoid_access
1331          * parameter was specified.
1332          */
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);
1338         }
1339
1340         /* resume our happy day */
1341         return (qrbuf.QRflags);
1342 }