]> code.citadel.org Git - citadel.git/blob - citadel/room_ops.c
* Added support for "transient goto" which allows entry into a private and/or
[citadel.git] / citadel / room_ops.c
1 /* 
2  * $Id$
3  * 
4  * Server functions which perform operations on room objects.
5  *
6  */
7
8 #ifdef DLL_EXPORT
9 #define IN_LIBCIT
10 #endif
11
12 #include "sysdep.h"
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <stdio.h>
16 #include <sys/stat.h>
17 #include <string.h>
18
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
21 # include <time.h>
22 #else
23 # if HAVE_SYS_TIME_H
24 #  include <sys/time.h>
25 # else
26 #  include <time.h>
27 # endif
28 #endif
29
30 #include <limits.h>
31 #include <errno.h>
32 #include "citadel.h"
33 #include "server.h"
34 #include "dynloader.h"
35 #include "database.h"
36 #include "config.h"
37 #include "room_ops.h"
38 #include "sysdep_decls.h"
39 #include "support.h"
40 #include "user_ops.h"
41 #include "msgbase.h"
42 #include "citserver.h"
43 #include "control.h"
44 #include "tools.h"
45
46 struct floor *floorcache[MAXFLOORS];
47
48 /*
49  * Generic routine for determining user access to rooms
50  */
51 int CtdlRoomAccess(struct quickroom *roombuf, struct usersupp *userbuf)
52 {
53         int retval = 0;
54         struct visit vbuf;
55
56         /* for internal programs, always do everything */
57         if (((CC->internal_pgm)) && (roombuf->QRflags & QR_INUSE)) {
58                 return (UA_KNOWN | UA_GOTOALLOWED);
59         }
60
61         /* Locate any applicable user/room relationships */
62         CtdlGetRelationship(&vbuf, userbuf, roombuf);
63
64         /* Force the properties of the Aide room */
65         if (!strcasecmp(roombuf->QRname, config.c_aideroom)) {
66                 if (userbuf->axlevel >= 6) {
67                         retval = UA_KNOWN | UA_GOTOALLOWED;
68                 } else {
69                         retval = 0;
70                 }
71                 goto NEWMSG;
72         }
73
74         /* If this is a public room, it's accessible... */
75         if ( ((roombuf->QRflags & QR_PRIVATE) == 0) 
76            && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
77                 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
78         }
79
80         /* If this is a preferred users only room, check access level */
81         if (roombuf->QRflags & QR_PREFONLY) {
82                 if (userbuf->axlevel < 5) {
83                         retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
84                 }
85         }
86
87         /* For private rooms, check the generation number matchups */
88         if ( (roombuf->QRflags & QR_PRIVATE) 
89            && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
90
91                 /* An explicit match means the user belongs in this room */
92                 if (vbuf.v_flags & V_ACCESS) {
93                         retval = retval | UA_KNOWN | UA_GOTOALLOWED;
94                 }
95                 /* Otherwise, check if this is a guess-name or passworded
96                  * room.  If it is, a goto may at least be attempted
97                  */
98                 else if ((roombuf->QRflags & QR_PRIVATE)
99                          || (roombuf->QRflags & QR_PASSWORDED)) {
100                         retval = retval & ~UA_KNOWN;
101                         retval = retval | UA_GOTOALLOWED;
102                 }
103         }
104
105         /* For mailbox rooms, also check the generation number matchups */
106         if (roombuf->QRflags & QR_MAILBOX) {
107                 if (userbuf->usernum == atol(roombuf->QRname)) {
108                         retval = retval | UA_KNOWN | UA_GOTOALLOWED;
109                 }
110                 /* An explicit match means the user belongs in this room */
111                 if (vbuf.v_flags & V_ACCESS) {
112                         retval = retval | UA_KNOWN | UA_GOTOALLOWED;
113                 }
114         }
115
116         /* Check to see if the user has forgotten this room */
117         if (vbuf.v_flags & V_FORGET) {
118                 retval = retval & ~UA_KNOWN;
119                 if ( ( ((roombuf->QRflags & QR_PRIVATE) == 0) 
120                       && ((roombuf->QRflags & QR_MAILBOX) == 0) )
121                    || ( (roombuf->QRflags & QR_MAILBOX) 
122                       && (atol(roombuf->QRname) == CC->usersupp.usernum))) {
123                         retval = retval | UA_ZAPPED;
124                 }
125         }
126         /* If user is explicitly locked out of this room, deny everything */
127         if (vbuf.v_flags & V_LOCKOUT) {
128                 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
129         }
130
131         /* Aides get access to all private rooms */
132         if ( (userbuf->axlevel >= 6)
133            && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
134                 if (vbuf.v_flags & V_FORGET) {
135                         retval = retval | UA_GOTOALLOWED;
136                 }
137                 else {
138                         retval = retval | UA_KNOWN | UA_GOTOALLOWED;
139                 }
140         }
141
142         /* On some systems, Aides can gain access to mailboxes as well */
143         if ( (config.c_aide_mailboxes)
144            && (userbuf->axlevel >= 6)
145            && (roombuf->QRflags & QR_MAILBOX) ) {
146                 retval = retval | UA_GOTOALLOWED;
147         }
148
149 NEWMSG: /* By the way, we also check for the presence of new messages */
150         if (is_msg_in_mset(vbuf.v_seen, roombuf->QRhighest) == 0) {
151                 retval = retval | UA_HASNEWMSGS;
152         }
153
154         /* System rooms never show up in the list. */
155         if (roombuf->QRflags2 & QR2_SYSTEM) {
156                 retval = retval & ~UA_KNOWN;
157         }
158         return (retval);
159 }
160
161 /*
162  * Self-checking stuff for a room record read into memory
163  */
164 void room_sanity_check(struct quickroom *qrbuf)
165 {
166         /* Mailbox rooms are always on the lowest floor */
167         if (qrbuf->QRflags & QR_MAILBOX) {
168                 qrbuf->QRfloor = 0;
169         }
170         /* Listing order of 0 is illegal except for base rooms */
171         if (qrbuf->QRorder == 0)
172                 if (!(qrbuf->QRflags & QR_MAILBOX) &&
173                     strncasecmp(qrbuf->QRname, config.c_baseroom, ROOMNAMELEN)
174                     &&
175                     strncasecmp(qrbuf->QRname, config.c_aideroom, ROOMNAMELEN))
176                         qrbuf->QRorder = 64;
177 }
178
179
180 /*
181  * getroom()  -  retrieve room data from disk
182  */
183 int getroom(struct quickroom *qrbuf, char *room_name)
184 {
185         struct cdbdata *cdbqr;
186         char lowercase_name[ROOMNAMELEN];
187         char personal_lowercase_name[ROOMNAMELEN];
188         int a;
189
190         for (a = 0; room_name[a] && a < sizeof lowercase_name - 1; ++a) {
191                 lowercase_name[a] = tolower(room_name[a]);
192         }
193         lowercase_name[a] = 0;
194
195         memset(qrbuf, 0, sizeof(struct quickroom));
196
197         /* First, try the public namespace */
198         cdbqr = cdb_fetch(CDB_QUICKROOM,
199                           lowercase_name, strlen(lowercase_name));
200
201         /* If that didn't work, try the user's personal namespace */
202         if (cdbqr == NULL) {
203                 snprintf(personal_lowercase_name,
204                          sizeof personal_lowercase_name, "%010ld.%s",
205                          CC->usersupp.usernum, lowercase_name);
206                 cdbqr = cdb_fetch(CDB_QUICKROOM,
207                                   personal_lowercase_name,
208                                   strlen(personal_lowercase_name));
209         }
210         if (cdbqr != NULL) {
211                 memcpy(qrbuf, cdbqr->ptr,
212                        ((cdbqr->len > sizeof(struct quickroom)) ?
213                         sizeof(struct quickroom) : cdbqr->len));
214                 cdb_free(cdbqr);
215
216                 room_sanity_check(qrbuf);
217
218                 return (0);
219         } else {
220                 return (1);
221         }
222 }
223
224 /*
225  * lgetroom()  -  same as getroom() but locks the record (if supported)
226  */
227 int lgetroom(struct quickroom *qrbuf, char *room_name)
228 {
229         register int retval;
230         retval = getroom(qrbuf, room_name);
231         if (retval == 0) begin_critical_section(S_QUICKROOM);
232         return(retval);
233 }
234
235
236 /*
237  * b_putroom()  -  back end to putroom() and b_deleteroom()
238  *              (if the supplied buffer is NULL, delete the room record)
239  */
240 void b_putroom(struct quickroom *qrbuf, char *room_name)
241 {
242         char lowercase_name[ROOMNAMELEN];
243         int a;
244
245         for (a = 0; a <= strlen(room_name); ++a) {
246                 lowercase_name[a] = tolower(room_name[a]);
247         }
248
249         if (qrbuf == NULL) {
250                 cdb_delete(CDB_QUICKROOM,
251                            lowercase_name, strlen(lowercase_name));
252         } else {
253                 time(&qrbuf->QRmtime);
254                 cdb_store(CDB_QUICKROOM,
255                           lowercase_name, strlen(lowercase_name),
256                           qrbuf, sizeof(struct quickroom));
257         }
258 }
259
260
261 /* 
262  * putroom()  -  store room data to disk
263  */
264 void putroom(struct quickroom *qrbuf) {
265         b_putroom(qrbuf, qrbuf->QRname);
266 }
267
268
269 /*
270  * b_deleteroom()  -  delete a room record from disk
271  */
272 void b_deleteroom(char *room_name) {
273         b_putroom(NULL, room_name);
274 }
275
276
277
278 /*
279  * lputroom()  -  same as putroom() but unlocks the record (if supported)
280  */
281 void lputroom(struct quickroom *qrbuf)
282 {
283
284         putroom(qrbuf);
285         end_critical_section(S_QUICKROOM);
286
287 }
288
289 /****************************************************************************/
290
291 /*
292  * getfloor()  -  retrieve floor data from disk
293  */
294 void getfloor(struct floor *flbuf, int floor_num)
295 {
296         struct cdbdata *cdbfl;
297
298         memset(flbuf, 0, sizeof(struct floor));
299         cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
300         if (cdbfl != NULL) {
301                 memcpy(flbuf, cdbfl->ptr,
302                        ((cdbfl->len > sizeof(struct floor)) ?
303                         sizeof(struct floor) : cdbfl->len));
304                 cdb_free(cdbfl);
305         } else {
306                 if (floor_num == 0) {
307                         strcpy(flbuf->f_name, "Main Floor");
308                         flbuf->f_flags = F_INUSE;
309                         flbuf->f_ref_count = 3;
310                 }
311         }
312
313 }
314
315 /*
316  * lgetfloor()  -  same as getfloor() but locks the record (if supported)
317  */
318 void lgetfloor(struct floor *flbuf, int floor_num)
319 {
320
321         begin_critical_section(S_FLOORTAB);
322         getfloor(flbuf, floor_num);
323 }
324
325
326 /*
327  * cgetfloor()  -  Get floor record from *cache* (loads from disk if needed)
328  *    
329  * This is strictly a performance hack.
330  */
331 struct floor *cgetfloor(int floor_num) {
332         static int initialized = 0;
333         int i;
334
335         if (initialized == 0) {
336                 for (i=0; i<MAXFLOORS; ++i) {
337                         floorcache[floor_num] = NULL;
338                 }
339         initialized = 1;
340         }
341         
342         if (floorcache[floor_num] == NULL) {
343                 floorcache[floor_num] = mallok(sizeof(struct floor));
344                 getfloor(floorcache[floor_num], floor_num);
345         }
346
347         return(floorcache[floor_num]);
348 }
349
350
351
352 /*
353  * putfloor()  -  store floor data on disk
354  */
355 void putfloor(struct floor *flbuf, int floor_num)
356 {
357         cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
358                   flbuf, sizeof(struct floor));
359
360         /* If we've cached this, clear it out, 'cuz it's WRONG now! */
361         if (floorcache[floor_num] != NULL) {
362                 phree(floorcache[floor_num]);
363                 floorcache[floor_num] = NULL;
364         }
365 }
366
367
368 /*
369  * lputfloor()  -  same as putfloor() but unlocks the record (if supported)
370  */
371 void lputfloor(struct floor *flbuf, int floor_num)
372 {
373
374         putfloor(flbuf, floor_num);
375         end_critical_section(S_FLOORTAB);
376
377 }
378
379
380 /* 
381  *  Traverse the room file...
382  */
383 void ForEachRoom(void (*CallBack) (struct quickroom *EachRoom, void *out_data),
384                 void *in_data)
385 {
386         struct quickroom qrbuf;
387         struct cdbdata *cdbqr;
388
389         cdb_rewind(CDB_QUICKROOM);
390
391         while (cdbqr = cdb_next_item(CDB_QUICKROOM), cdbqr != NULL) {
392                 memset(&qrbuf, 0, sizeof(struct quickroom));
393                 memcpy(&qrbuf, cdbqr->ptr,
394                        ((cdbqr->len > sizeof(struct quickroom)) ?
395                         sizeof(struct quickroom) : cdbqr->len));
396                 cdb_free(cdbqr);
397                 room_sanity_check(&qrbuf);
398                 if (qrbuf.QRflags & QR_INUSE)
399                         (*CallBack)(&qrbuf, in_data);
400         }
401 }
402
403
404 /*
405  * delete_msglist()  -  delete room message pointers
406  */
407 void delete_msglist(struct quickroom *whichroom)
408 {
409         struct cdbdata *cdbml;
410
411         /* Make sure the msglist we're deleting actually exists, otherwise
412          * gdbm will complain when we try to delete an invalid record
413          */
414         cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
415         if (cdbml != NULL) {
416                 cdb_free(cdbml);
417
418                 /* Go ahead and delete it */
419                 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
420         }
421 }
422
423
424
425
426 /*
427  * sort message pointers
428  * (returns new msg count)
429  */
430 int sort_msglist(long listptrs[], int oldcount)
431 {
432         int a, b;
433         long hold1, hold2;
434         int numitems;
435
436         numitems = oldcount;
437         if (numitems < 2)
438                 return (oldcount);
439
440         /* do the sort */
441         for (a = numitems - 2; a >= 0; --a) {
442                 for (b = 0; b <= a; ++b) {
443                         if (listptrs[b] > (listptrs[b + 1])) {
444                                 hold1 = listptrs[b];
445                                 hold2 = listptrs[b + 1];
446                                 listptrs[b] = hold2;
447                                 listptrs[b + 1] = hold1;
448                         }
449                 }
450         }
451
452         /* and yank any nulls */
453         while ((numitems > 0) && (listptrs[0] == 0L)) {
454                 memcpy(&listptrs[0], &listptrs[1],
455                        (sizeof(long) * (numitems - 1)));
456                 --numitems;
457         }
458
459         return (numitems);
460 }
461
462
463 /*
464  * Determine whether a given room is non-editable.
465  */
466 int is_noneditable(struct quickroom *qrbuf)
467 {
468
469         /* Mail> rooms are non-editable */
470         if ( (qrbuf->QRflags & QR_MAILBOX)
471              && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
472                 return (1);
473
474         /* Everything else is editable */
475         return (0);
476 }
477
478
479
480 /*
481  * Back-back-end for all room listing commands
482  */
483 void list_roomname(struct quickroom *qrbuf)
484 {
485         char truncated_roomname[ROOMNAMELEN];
486
487         /* For my own mailbox rooms, chop off the owner prefix */
488         if ( (qrbuf->QRflags & QR_MAILBOX)
489              && (atol(qrbuf->QRname) == CC->usersupp.usernum) ) {
490                 strcpy(truncated_roomname, qrbuf->QRname);
491                 strcpy(truncated_roomname, &truncated_roomname[11]);
492                 cprintf("%s", truncated_roomname);
493         }
494         /* For all other rooms, just display the name in its entirety */
495         else {
496                 cprintf("%s", qrbuf->QRname);
497         }
498
499         /* ...and now the other parameters */
500         cprintf("|%u|%d|%d\n",
501                 qrbuf->QRflags,
502                 (int) qrbuf->QRfloor,
503                 (int) qrbuf->QRorder);
504 }
505
506
507 /* 
508  * cmd_lrms()   -  List all accessible rooms, known or forgotten
509  */
510 void cmd_lrms_backend(struct quickroom *qrbuf, void *data)
511 {
512         int FloorBeingSearched = (-1);
513         FloorBeingSearched = *(int *)data;
514
515         if (((CtdlRoomAccess(qrbuf, &CC->usersupp)
516               & (UA_KNOWN | UA_ZAPPED)))
517             && ((qrbuf->QRfloor == (FloorBeingSearched))
518                 || ((FloorBeingSearched) < 0)))
519                 list_roomname(qrbuf);
520 }
521
522 void cmd_lrms(char *argbuf)
523 {
524         int FloorBeingSearched = (-1);
525         if (strlen(argbuf) > 0)
526                 FloorBeingSearched = extract_int(argbuf, 0);
527
528         if (CtdlAccessCheck(ac_logged_in)) return;
529
530         if (getuser(&CC->usersupp, CC->curr_user)) {
531                 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
532                 return;
533         }
534         cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
535
536         ForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
537         cprintf("000\n");
538 }
539
540
541
542 /* 
543  * cmd_lkra()   -  List all known rooms
544  */
545 void cmd_lkra_backend(struct quickroom *qrbuf, void *data)
546 {
547         int FloorBeingSearched = (-1);
548         FloorBeingSearched = *(int *)data;
549
550         if (((CtdlRoomAccess(qrbuf, &CC->usersupp)
551               & (UA_KNOWN)))
552             && ((qrbuf->QRfloor == (FloorBeingSearched))
553                 || ((FloorBeingSearched) < 0)))
554                 list_roomname(qrbuf);
555 }
556
557 void cmd_lkra(char *argbuf)
558 {
559         int FloorBeingSearched = (-1);
560         if (strlen(argbuf) > 0)
561                 FloorBeingSearched = extract_int(argbuf, 0);
562
563         if (CtdlAccessCheck(ac_logged_in)) return;
564         
565         if (getuser(&CC->usersupp, CC->curr_user)) {
566                 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
567                 return;
568         }
569         cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
570
571         ForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
572         cprintf("000\n");
573 }
574
575
576
577 /* 
578  * cmd_lkrn()   -  List all known rooms with new messages
579  */
580 void cmd_lkrn_backend(struct quickroom *qrbuf, void *data)
581 {
582         int ra;
583         int FloorBeingSearched = (-1);
584         FloorBeingSearched = *(int *)data;
585
586         ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
587         if ((ra & UA_KNOWN)
588             && (ra & UA_HASNEWMSGS)
589             && ((qrbuf->QRfloor == (FloorBeingSearched))
590                 || ((FloorBeingSearched) < 0)))
591                 list_roomname(qrbuf);
592 }
593
594 void cmd_lkrn(char *argbuf)
595 {
596         int FloorBeingSearched = (-1);
597         if (strlen(argbuf) > 0)
598                 FloorBeingSearched = extract_int(argbuf, 0);
599
600         if (CtdlAccessCheck(ac_logged_in)) return;
601         
602         if (getuser(&CC->usersupp, CC->curr_user)) {
603                 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
604                 return;
605         }
606         cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
607
608         ForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
609         cprintf("000\n");
610 }
611
612
613
614 /* 
615  * cmd_lkro()   -  List all known rooms
616  */
617 void cmd_lkro_backend(struct quickroom *qrbuf, void *data)
618 {
619         int ra;
620         int FloorBeingSearched = (-1);
621         FloorBeingSearched = *(int *)data;
622
623         ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
624         if ((ra & UA_KNOWN)
625             && ((ra & UA_HASNEWMSGS) == 0)
626             && ((qrbuf->QRfloor == (FloorBeingSearched))
627                 || ((FloorBeingSearched) < 0)))
628                 list_roomname(qrbuf);
629 }
630
631 void cmd_lkro(char *argbuf)
632 {
633         int FloorBeingSearched = (-1);
634         if (strlen(argbuf) > 0)
635                 FloorBeingSearched = extract_int(argbuf, 0);
636
637         if (CtdlAccessCheck(ac_logged_in)) return;
638         
639         if (getuser(&CC->usersupp, CC->curr_user)) {
640                 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
641                 return;
642         }
643         cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
644
645         ForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
646         cprintf("000\n");
647 }
648
649
650
651 /* 
652  * cmd_lzrm()   -  List all forgotten rooms
653  */
654 void cmd_lzrm_backend(struct quickroom *qrbuf, void *data)
655 {
656         int ra;
657         int FloorBeingSearched = (-1);
658         FloorBeingSearched = *(int *)data;
659
660         ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
661         if ((ra & UA_GOTOALLOWED)
662             && (ra & UA_ZAPPED)
663             && ((qrbuf->QRfloor == (FloorBeingSearched))
664                 || ((FloorBeingSearched) < 0)))
665                 list_roomname(qrbuf);
666 }
667
668 void cmd_lzrm(char *argbuf)
669 {
670         int FloorBeingSearched = (-1);
671         if (strlen(argbuf) > 0)
672                 FloorBeingSearched = extract_int(argbuf, 0);
673
674         if (CtdlAccessCheck(ac_logged_in)) return;
675         
676         if (getuser(&CC->usersupp, CC->curr_user)) {
677                 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
678                 return;
679         }
680         cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
681
682         ForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
683         cprintf("000\n");
684 }
685
686
687 /*
688  * Make the specified room the current room for this session.  No validation
689  * or access control is done here -- the caller should make sure that the
690  * specified room exists and is ok to access.
691  */
692 void usergoto(char *where, int display_result, int transiently,
693                 int *retmsgs, int *retnew)
694 {
695         int a;
696         int new_messages = 0;
697         int total_messages = 0;
698         int info = 0;
699         int rmailflag;
700         int raideflag;
701         int newmailcount = 0;
702         struct visit vbuf;
703         char truncated_roomname[ROOMNAMELEN];
704         struct cdbdata *cdbfr;
705         long *msglist = NULL;
706         int num_msgs = 0;
707
708         /* If the supplied room name is NULL, the caller wants us to know that
709          * it has already copied the quickroom record into CC->quickroom, so
710          * we can skip the extra database fetch.
711          */
712         if (where != NULL) {
713                 strcpy(CC->quickroom.QRname, where);
714                 getroom(&CC->quickroom, where);
715         }
716
717         /* Take care of all the formalities. */
718
719         begin_critical_section(S_USERSUPP);
720         CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
721
722         /* Know the room ... but not if it's the page log room, or if the
723          * caller specified that we're only entering this room transiently.
724          */
725         if ((strcasecmp(CC->quickroom.QRname, config.c_logpages))
726            && (transiently == 0) ) {
727                 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
728                 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
729         }
730         CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
731         end_critical_section(S_USERSUPP);
732
733         /* Check for new mail */
734         newmailcount = NewMailCount();
735
736         /* set info to 1 if the user needs to read the room's info file */
737         if (CC->quickroom.QRinfo > vbuf.v_lastseen) {
738                 info = 1;
739         }
740
741         get_mm();
742         cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->quickroom.QRnumber, sizeof(long));
743         if (cdbfr != NULL) {
744                 msglist = mallok(cdbfr->len);
745                 memcpy(msglist, cdbfr->ptr, cdbfr->len);
746                 num_msgs = cdbfr->len / sizeof(long);
747                 cdb_free(cdbfr);
748         }
749
750         if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
751                 if (msglist[a] > 0L) {
752                         ++total_messages;
753                         if (is_msg_in_mset(vbuf.v_seen, msglist[a]) == 0) {
754                                 ++new_messages;
755                         }
756                 }
757         }
758
759         if (msglist != NULL) phree(msglist);
760
761         if (CC->quickroom.QRflags & QR_MAILBOX)
762                 rmailflag = 1;
763         else
764                 rmailflag = 0;
765
766         if ((CC->quickroom.QRroomaide == CC->usersupp.usernum)
767             || (CC->usersupp.axlevel >= 6))
768                 raideflag = 1;
769         else
770                 raideflag = 0;
771
772         strcpy(truncated_roomname, CC->quickroom.QRname);
773         if ( (CC->quickroom.QRflags & QR_MAILBOX)
774            && (atol(CC->quickroom.QRname) == CC->usersupp.usernum) ) {
775                 strcpy(truncated_roomname, &truncated_roomname[11]);
776         }
777
778         if (retmsgs != NULL) *retmsgs = total_messages;
779         if (retnew != NULL) *retnew = new_messages;
780         lprintf(9, "<%s> %d new of %d total messages\n",
781                 CC->quickroom.QRname, new_messages, total_messages);
782
783         if (display_result)
784                 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d\n",
785                         CIT_OK, CtdlCheckExpress(),
786                         truncated_roomname,
787                         new_messages, total_messages,
788                         info, CC->quickroom.QRflags,
789                         CC->quickroom.QRhighest,
790                         vbuf.v_lastseen,
791                         rmailflag, raideflag, newmailcount,
792                         CC->quickroom.QRfloor,
793                         vbuf.v_view);
794 }
795
796
797 /* 
798  * cmd_goto()  -  goto a new room
799  */
800 void cmd_goto(char *gargs)
801 {
802         struct quickroom QRscratch;
803         int c;
804         int ok = 0;
805         int ra;
806         char augmented_roomname[SIZ];
807         char towhere[SIZ];
808         char password[SIZ];
809         int transiently = 0;
810
811         if (CtdlAccessCheck(ac_logged_in)) return;
812
813         extract(towhere, gargs, 0);
814         extract(password, gargs, 1);
815         transiently = extract_int(gargs, 2);
816
817         getuser(&CC->usersupp, CC->curr_user);
818
819         if (!strcasecmp(towhere, "_BASEROOM_"))
820                 strcpy(towhere, config.c_baseroom);
821
822         if (!strcasecmp(towhere, "_MAIL_"))
823                 strcpy(towhere, MAILROOM);
824
825         if (!strcasecmp(towhere, "_BITBUCKET_"))
826                 strcpy(towhere, config.c_twitroom);
827
828
829         /* First try a regular match */
830         c = getroom(&QRscratch, towhere);
831
832         /* Then try a mailbox name match */
833         if (c != 0) {
834                 MailboxName(augmented_roomname, sizeof augmented_roomname,
835                             &CC->usersupp, towhere);
836                 c = getroom(&QRscratch, augmented_roomname);
837                 if (c == 0)
838                         strcpy(towhere, augmented_roomname);
839         }
840
841         /* And if the room was found... */
842         if (c == 0) {
843
844                 /* Let internal programs go directly to any room. */
845                 if (CC->internal_pgm) {
846                         memcpy(&CC->quickroom, &QRscratch,
847                                 sizeof(struct quickroom));
848                         usergoto(NULL, 1, transiently, NULL, NULL);
849                         return;
850                 }
851
852                 /* See if there is an existing user/room relationship */
853                 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
854
855                 /* normal clients have to pass through security */
856                 if (ra & UA_GOTOALLOWED) {
857                         ok = 1;
858                 }
859
860                 if (ok == 1) {
861                         if ((QRscratch.QRflags & QR_MAILBOX) &&
862                             ((ra & UA_GOTOALLOWED))) {
863                                 memcpy(&CC->quickroom, &QRscratch,
864                                         sizeof(struct quickroom));
865                                 usergoto(NULL, 1, transiently, NULL, NULL);
866                                 return;
867                         } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
868                             ((ra & UA_KNOWN) == 0) &&
869                             (strcasecmp(QRscratch.QRpasswd, password)) &&
870                             (CC->usersupp.axlevel < 6)
871                             ) {
872                                 cprintf("%d wrong or missing passwd\n",
873                                         ERROR + PASSWORD_REQUIRED);
874                                 return;
875                         } else if ((QRscratch.QRflags & QR_PRIVATE) &&
876                                    ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
877                                    ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
878                                    ((ra & UA_KNOWN) == 0) &&
879                                    (CC->usersupp.axlevel < 6)
880                                   ) {
881                                 lprintf(9, "Failed to acquire private room\n");
882                                 goto NOPE;
883                         } else {
884                                 memcpy(&CC->quickroom, &QRscratch,
885                                         sizeof(struct quickroom));
886                                 usergoto(NULL, 1, transiently, NULL, NULL);
887                                 return;
888                         }
889                 }
890         }
891
892 NOPE:   cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
893 }
894
895
896 void cmd_whok(void)
897 {
898         struct usersupp temp;
899         struct cdbdata *cdbus;
900
901         getuser(&CC->usersupp, CC->curr_user);
902
903         /*
904          * This command is only allowed by aides, room aides,
905          * and room namespace owners
906          */
907         if (is_room_aide()
908            || (atol(CC->quickroom.QRname) == CC->usersupp.usernum) ) {
909                 /* access granted */
910         }
911         else {
912                 /* access denied */
913                 cprintf("%d Higher access or room ownership required.\n",
914                         ERROR + HIGHER_ACCESS_REQUIRED);
915                 return;
916         }
917
918         cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
919         cdb_rewind(CDB_USERSUPP);
920         while (cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
921                 memset(&temp, 0, sizeof temp);
922                 memcpy(&temp, cdbus->ptr, sizeof temp);
923                 cdb_free(cdbus);
924
925                 if ((CC->quickroom.QRflags & QR_INUSE)
926                     && (CtdlRoomAccess(&CC->quickroom, &temp) & UA_KNOWN)
927                     )
928                         cprintf("%s\n", temp.fullname);
929         }
930         cprintf("000\n");
931 }
932
933
934 /*
935  * RDIR command for room directory
936  */
937 void cmd_rdir(void)
938 {
939         char buf[SIZ];
940         char flnm[SIZ];
941         char comment[SIZ];
942         FILE *ls, *fd;
943         struct stat statbuf;
944
945         if (CtdlAccessCheck(ac_logged_in)) return;
946         
947         getroom(&CC->quickroom, CC->quickroom.QRname);
948         getuser(&CC->usersupp, CC->curr_user);
949
950         if ((CC->quickroom.QRflags & QR_DIRECTORY) == 0) {
951                 cprintf("%d not here.\n", ERROR + NOT_HERE);
952                 return;
953         }
954         if (((CC->quickroom.QRflags & QR_VISDIR) == 0)
955             && (CC->usersupp.axlevel < 6)
956             && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
957                 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
958                 return;
959         }
960         cprintf("%d %s|%s/files/%s\n",
961         LISTING_FOLLOWS, config.c_fqdn, BBSDIR, CC->quickroom.QRdirname);
962
963         snprintf(buf, sizeof buf, "ls %s/files/%s  >%s 2> /dev/null",
964                 BBSDIR, CC->quickroom.QRdirname, CC->temp);
965         system(buf);
966
967         snprintf(buf, sizeof buf, "%s/files/%s/filedir", BBSDIR, CC->quickroom.QRdirname);
968         fd = fopen(buf, "r");
969         if (fd == NULL)
970                 fd = fopen("/dev/null", "r");
971
972         ls = fopen(CC->temp, "r");
973         while (fgets(flnm, sizeof flnm, ls) != NULL) {
974                 flnm[strlen(flnm) - 1] = 0;
975                 if (strcasecmp(flnm, "filedir")) {
976                         snprintf(buf, sizeof buf, "%s/files/%s/%s",
977                                 BBSDIR, CC->quickroom.QRdirname, flnm);
978                         stat(buf, &statbuf);
979                         strcpy(comment, "");
980                         fseek(fd, 0L, 0);
981                         while ((fgets(buf, sizeof buf, fd) != NULL)
982                                && (strlen(comment) == 0)) {
983                                 buf[strlen(buf) - 1] = 0;
984                                 if ((!strncasecmp(buf, flnm, strlen(flnm)))
985                                     && (buf[strlen(flnm)] == ' '))
986                                         safestrncpy(comment,
987                                             &buf[strlen(flnm) + 1],
988                                             sizeof comment);
989                         }
990                         cprintf("%s|%ld|%s\n", flnm, (long)statbuf.st_size, comment);
991                 }
992         }
993         fclose(ls);
994         fclose(fd);
995         unlink(CC->temp);
996
997         cprintf("000\n");
998 }
999
1000 /*
1001  * get room parameters (aide or room aide command)
1002  */
1003 void cmd_getr(void)
1004 {
1005         if (CtdlAccessCheck(ac_room_aide)) return;
1006
1007         getroom(&CC->quickroom, CC->quickroom.QRname);
1008         cprintf("%d%c%s|%s|%s|%d|%d|%d|%d\n",
1009                 CIT_OK,
1010                 CtdlCheckExpress(),
1011
1012                 ((CC->quickroom.QRflags & QR_MAILBOX) ?
1013                         &CC->quickroom.QRname[11] : CC->quickroom.QRname),
1014
1015                 ((CC->quickroom.QRflags & QR_PASSWORDED) ?
1016                         CC->quickroom.QRpasswd : ""),
1017
1018                 ((CC->quickroom.QRflags & QR_DIRECTORY) ?
1019                         CC->quickroom.QRdirname : ""),
1020
1021                 CC->quickroom.QRflags,
1022                 (int) CC->quickroom.QRfloor,
1023                 (int) CC->quickroom.QRorder,
1024
1025                 CC->quickroom.QRdefaultview);
1026 }
1027
1028
1029 /*
1030  * Back end function to rename a room.
1031  * You can also specify which floor to move the room to, or specify -1 to
1032  * keep the room on the same floor it was on.
1033  *
1034  * If you are renaming a mailbox room, you must supply the namespace prefix
1035  * in *at least* the old name!
1036  */
1037 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1038         int old_floor = 0;
1039         struct quickroom qrbuf;
1040         struct quickroom qrtmp;
1041         int ret = 0;
1042         struct floor *fl;
1043         struct floor flbuf;
1044         long owner = 0L;
1045
1046         lprintf(9, "CtdlRenameRoom(%s, %s, %d)\n",
1047                 old_name, new_name, new_floor);
1048
1049         if (new_floor >= 0) {
1050                 fl = cgetfloor(new_floor);
1051                 if ((fl->f_flags & F_INUSE) == 0) {
1052                         return(crr_invalid_floor);
1053                 }
1054         }
1055
1056         begin_critical_section(S_QUICKROOM);
1057
1058         if ( (getroom(&qrtmp, new_name) == 0) 
1059            && (strcasecmp(new_name, old_name)) ) {
1060                 ret = crr_already_exists;
1061         }
1062
1063         else if (getroom(&qrbuf, old_name) != 0) {
1064                 ret = crr_room_not_found;
1065         }
1066
1067         else if ( (CC->usersupp.axlevel < 6)
1068                   && (CC->usersupp.usernum != qrbuf.QRroomaide)
1069                   && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->usersupp.usernum))) )  {
1070                 ret = crr_access_denied;
1071         }
1072
1073         else if (is_noneditable(&qrbuf)) {
1074                 ret = crr_noneditable;
1075         }
1076
1077         else {
1078                 /* Rename it */
1079                 if (qrbuf.QRflags & QR_MAILBOX) {
1080                         owner = atol(qrbuf.QRname);
1081                 }
1082                 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1083                         snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1084                                         "%010ld.%s", owner, new_name);
1085                 }
1086                 else {
1087                         safestrncpy(qrbuf.QRname, new_name,
1088                                                 sizeof(qrbuf.QRname));
1089                 }
1090
1091                 /* Reject change of floor for baseroom/aideroom */
1092                 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1093                     !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1094                         new_floor = 0;
1095                 }
1096
1097                 /* Take care of floor stuff */
1098                 old_floor = qrbuf.QRfloor;
1099                 if (new_floor < 0) {
1100                         new_floor = old_floor;
1101                 }
1102                 qrbuf.QRfloor = new_floor;
1103                 putroom(&qrbuf);
1104
1105                 begin_critical_section(S_CONFIG);
1106         
1107                 /* If baseroom/aideroom name changes, update config */
1108                 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1109                         safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1110                         put_config();
1111                 }
1112                 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1113                         safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1114                         put_config();
1115                 }
1116         
1117                 end_critical_section(S_CONFIG);
1118         
1119                 /* If the room name changed, then there are now two room
1120                  * records, so we have to delete the old one.
1121                  */
1122                 if (strcasecmp(new_name, old_name)) {
1123                         b_deleteroom(old_name);
1124                 }
1125
1126                 ret = crr_ok;
1127         }
1128
1129         end_critical_section(S_QUICKROOM);
1130
1131         /* Adjust the floor reference counts if necessary */
1132         if (new_floor != old_floor) {
1133                 lgetfloor(&flbuf, old_floor);
1134                 --flbuf.f_ref_count;
1135                 lputfloor(&flbuf, old_floor);
1136                 lgetfloor(&flbuf, CC->quickroom.QRfloor);
1137                 ++flbuf.f_ref_count;
1138                 lputfloor(&flbuf, CC->quickroom.QRfloor);
1139         }
1140
1141         /* ...and everybody say "YATTA!" */     
1142         return(ret);
1143 }
1144
1145
1146 /*
1147  * set room parameters (aide or room aide command)
1148  */
1149 void cmd_setr(char *args)
1150 {
1151         char buf[SIZ];
1152         int new_order = 0;
1153         int r;
1154         int new_floor;
1155         char new_name[ROOMNAMELEN];
1156
1157         if (CtdlAccessCheck(ac_logged_in)) return;
1158
1159         if (num_parms(args) >= 6) {
1160                 new_floor = extract_int(args, 5);
1161         } else {
1162                 new_floor = (-1);       /* don't change the floor */
1163         }
1164
1165         /* When is a new name more than just a new name?  When the old name
1166          * has a namespace prefix.
1167          */
1168         if (CC->quickroom.QRflags & QR_MAILBOX) {
1169                 sprintf(new_name, "%010ld.", atol(CC->quickroom.QRname) );
1170         } else {
1171                 strcpy(new_name, "");
1172         }
1173         extract(&new_name[strlen(new_name)], args, 0);
1174
1175         r = CtdlRenameRoom(CC->quickroom.QRname, new_name, new_floor);
1176
1177         if (r == crr_room_not_found) {
1178                 cprintf("%d Internal error - room not found?\n", ERROR);
1179         } else if (r == crr_already_exists) {
1180                 cprintf("%d '%s' already exists.\n",
1181                         ERROR + ALREADY_EXISTS, new_name);
1182         } else if (r == crr_noneditable) {
1183                 cprintf("%d Cannot edit this room.\n", ERROR);
1184         } else if (r == crr_invalid_floor) {
1185                 cprintf("%d Target floor does not exist.\n",
1186                         ERROR + INVALID_FLOOR_OPERATION);
1187         } else if (r == crr_access_denied) {
1188                 cprintf("%d You do not have permission to edit '%s'\n",
1189                         ERROR + HIGHER_ACCESS_REQUIRED,
1190                         CC->quickroom.QRname);
1191         } else if (r != crr_ok) {
1192                 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1193                         ERROR, r);
1194         }
1195
1196         if (r != crr_ok) {
1197                 return;
1198         }
1199
1200         getroom(&CC->quickroom, new_name);
1201
1202         /* Now we have to do a bunch of other stuff */
1203
1204         if (num_parms(args) >= 7) {
1205                 new_order = extract_int(args, 6);
1206                 if (new_order < 1)
1207                         new_order = 1;
1208                 if (new_order > 127)
1209                         new_order = 127;
1210         }
1211
1212         lgetroom(&CC->quickroom, CC->quickroom.QRname);
1213
1214         /* Directory room */
1215         extract(buf, args, 2);
1216         buf[15] = 0;
1217         safestrncpy(CC->quickroom.QRdirname, buf,
1218                 sizeof CC->quickroom.QRdirname);
1219
1220         /* Default view */
1221         if (num_parms(args) >= 8) {
1222                 CC->quickroom.QRdefaultview = extract_int(args, 7);
1223         }
1224
1225         /* Misc. flags */
1226         CC->quickroom.QRflags = (extract_int(args, 3) | QR_INUSE);
1227         /* Clean up a client boo-boo: if the client set the room to
1228          * guess-name or passworded, ensure that the private flag is
1229          * also set.
1230          */
1231         if ((CC->quickroom.QRflags & QR_GUESSNAME)
1232             || (CC->quickroom.QRflags & QR_PASSWORDED))
1233                 CC->quickroom.QRflags |= QR_PRIVATE;
1234
1235         /* Some changes can't apply to BASEROOM */
1236         if (!strncasecmp(CC->quickroom.QRname, config.c_baseroom,
1237                          ROOMNAMELEN)) {
1238                 CC->quickroom.QRorder = 0;
1239                 CC->quickroom.QRpasswd[0] = '\0';
1240                 CC->quickroom.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1241                         QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1242                 CC->quickroom.QRflags |= QR_PERMANENT;
1243         } else {        
1244                 /* March order (doesn't apply to AIDEROOM) */
1245                 if (num_parms(args) >= 7)
1246                         CC->quickroom.QRorder = (char) new_order;
1247                 /* Room password */
1248                 extract(buf, args, 1);
1249                 buf[10] = 0;
1250                 safestrncpy(CC->quickroom.QRpasswd, buf,
1251                             sizeof CC->quickroom.QRpasswd);
1252                 /* Kick everyone out if the client requested it
1253                  * (by changing the room's generation number)
1254                  */
1255                 if (extract_int(args, 4)) {
1256                         time(&CC->quickroom.QRgen);
1257                 }
1258         }
1259         /* Some changes can't apply to AIDEROOM */
1260         if (!strncasecmp(CC->quickroom.QRname, config.c_baseroom,
1261                          ROOMNAMELEN)) {
1262                 CC->quickroom.QRorder = 0;
1263                 CC->quickroom.QRflags &= ~QR_MAILBOX;
1264                 CC->quickroom.QRflags |= QR_PERMANENT;
1265         }
1266
1267         /* Write the room record back to disk */
1268         lputroom(&CC->quickroom);
1269
1270         /* Create a room directory if necessary */
1271         if (CC->quickroom.QRflags & QR_DIRECTORY) {
1272                 snprintf(buf, sizeof buf,
1273                     "mkdir ./files/%s </dev/null >/dev/null 2>/dev/null",
1274                         CC->quickroom.QRdirname);
1275                 system(buf);
1276         }
1277         snprintf(buf, sizeof buf, "%s> edited by %s\n", CC->quickroom.QRname, CC->curr_user);
1278         aide_message(buf);
1279         cprintf("%d Ok\n", CIT_OK);
1280 }
1281
1282
1283
1284 /* 
1285  * get the name of the room aide for this room
1286  */
1287 void cmd_geta(void)
1288 {
1289         struct usersupp usbuf;
1290
1291         if (CtdlAccessCheck(ac_logged_in)) return;
1292
1293         if (getuserbynumber(&usbuf, CC->quickroom.QRroomaide) == 0) {
1294                 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1295         } else {
1296                 cprintf("%d \n", CIT_OK);
1297         }
1298 }
1299
1300
1301 /* 
1302  * set the room aide for this room
1303  */
1304 void cmd_seta(char *new_ra)
1305 {
1306         struct usersupp usbuf;
1307         long newu;
1308         char buf[SIZ];
1309         int post_notice;
1310
1311         if (CtdlAccessCheck(ac_room_aide)) return;
1312
1313         if (getuser(&usbuf, new_ra) != 0) {
1314                 newu = (-1L);
1315         } else {
1316                 newu = usbuf.usernum;
1317         }
1318
1319         lgetroom(&CC->quickroom, CC->quickroom.QRname);
1320         post_notice = 0;
1321         if (CC->quickroom.QRroomaide != newu) {
1322                 post_notice = 1;
1323         }
1324         CC->quickroom.QRroomaide = newu;
1325         lputroom(&CC->quickroom);
1326
1327         /*
1328          * We have to post the change notice _after_ writing changes to 
1329          * the room table, otherwise it would deadlock!
1330          */
1331         if (post_notice == 1) {
1332                 if (strlen(usbuf.fullname) > 0)
1333                         snprintf(buf, sizeof buf,
1334                                 "%s is now room aide for %s>\n",
1335                                 usbuf.fullname, CC->quickroom.QRname);
1336                 else
1337                         snprintf(buf, sizeof buf,
1338                                 "There is now no room aide for %s>\n",
1339                                 CC->quickroom.QRname);
1340                 aide_message(buf);
1341         }
1342         cprintf("%d Ok\n", CIT_OK);
1343 }
1344
1345 /*
1346  * Generate an associated file name for a room
1347  */
1348 void assoc_file_name(char *buf, size_t n,
1349                      struct quickroom *qrbuf, const char *prefix)
1350 {
1351         snprintf(buf, n, "./%s/%ld", prefix, qrbuf->QRnumber);
1352 }
1353
1354 /* 
1355  * retrieve info file for this room
1356  */
1357 void cmd_rinf(void)
1358 {
1359         char filename[128];
1360         char buf[SIZ];
1361         FILE *info_fp;
1362
1363         assoc_file_name(filename, sizeof filename, &CC->quickroom, "info");
1364         info_fp = fopen(filename, "r");
1365
1366         if (info_fp == NULL) {
1367                 cprintf("%d No info file.\n", ERROR);
1368                 return;
1369         }
1370         cprintf("%d Info:\n", LISTING_FOLLOWS);
1371         while (fgets(buf, sizeof buf, info_fp) != NULL) {
1372                 if (strlen(buf) > 0)
1373                         buf[strlen(buf) - 1] = 0;
1374                 cprintf("%s\n", buf);
1375         }
1376         cprintf("000\n");
1377         fclose(info_fp);
1378 }
1379
1380 /*
1381  * Back end processing to delete a room and everything associated with it
1382  */
1383 void delete_room(struct quickroom *qrbuf)
1384 {
1385         struct floor flbuf;
1386         char filename[100];
1387
1388         lprintf(9, "Deleting room <%s>\n", qrbuf->QRname);
1389
1390         /* Delete the info file */
1391         assoc_file_name(filename, sizeof filename, qrbuf, "info");
1392         unlink(filename);
1393
1394         /* Delete the image file */
1395         assoc_file_name(filename, sizeof filename, qrbuf, "images");
1396         unlink(filename);
1397
1398         /* Delete the room's network config file */
1399         assoc_file_name(filename, sizeof filename, qrbuf, "netconfigs");
1400         unlink(filename);
1401
1402         /* Delete the messages in the room
1403          * (Careful: this opens an S_QUICKROOM critical section!)
1404          */
1405         CtdlDeleteMessages(qrbuf->QRname, 0L, "");
1406
1407         /* Flag the room record as not in use */
1408         lgetroom(qrbuf, qrbuf->QRname);
1409         qrbuf->QRflags = 0;
1410         lputroom(qrbuf);
1411
1412         /* then decrement the reference count for the floor */
1413         lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1414         flbuf.f_ref_count = flbuf.f_ref_count - 1;
1415         lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1416
1417         /* Delete the room record from the database! */
1418         b_deleteroom(qrbuf->QRname);
1419 }
1420
1421
1422
1423 /*
1424  * Check access control for deleting a room
1425  */
1426 int CtdlDoIHavePermissionToDeleteThisRoom(struct quickroom *qr) {
1427
1428         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1429                 return(0);
1430         }
1431
1432         if (is_noneditable(qr)) {
1433                 return(0);
1434         }
1435
1436         /*
1437          * For mailboxes, check stuff
1438          */
1439         if (qr->QRflags & QR_MAILBOX) {
1440
1441                 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1442
1443                 if (atol(qr->QRname) != CC->usersupp.usernum) {
1444                         return(0);      /* not my room */
1445                 }
1446
1447                 /* Can't delete your Mail> room */
1448                 if (!strcasecmp(&qr->QRname[12], MAILROOM)) return(0);
1449
1450                 /* Otherwise it's ok */
1451                 return(1);
1452         }
1453
1454         /*
1455          * For normal rooms, just check for aide or room aide status.
1456          */
1457         return(is_room_aide());
1458 }
1459
1460 /*
1461  * aide command: kill the current room
1462  */
1463 void cmd_kill(char *argbuf)
1464 {
1465         char aaa[100];
1466         char deleted_room_name[ROOMNAMELEN];
1467         int kill_ok;
1468
1469         kill_ok = extract_int(argbuf, 0);
1470
1471         if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->quickroom) == 0) {
1472                 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1473                 return;
1474         }
1475         if (kill_ok) {
1476                 strcpy(deleted_room_name, CC->quickroom.QRname);
1477                 delete_room(&CC->quickroom);    /* Do the dirty work */
1478                 usergoto(config.c_baseroom, 0, 0, NULL, NULL); /* Return to the Lobby */
1479
1480                 /* tell the world what we did */
1481                 snprintf(aaa, sizeof aaa, "%s> killed by %s\n",
1482                          deleted_room_name, CC->curr_user);
1483                 aide_message(aaa);
1484                 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1485         } else {
1486                 cprintf("%d ok to delete.\n", CIT_OK);
1487         }
1488 }
1489
1490
1491 /*
1492  * Internal code to create a new room (returns room flags)
1493  *
1494  * Room types:  0=public, 1=guessname, 2=passworded, 3=inv-only,
1495  *              4=mailbox, 5=mailbox, but caller supplies namespace
1496  */
1497 unsigned create_room(char *new_room_name,
1498                      int new_room_type,
1499                      char *new_room_pass,
1500                      int new_room_floor,
1501                      int really_create,
1502                      int avoid_access)
1503 {
1504
1505         struct quickroom qrbuf;
1506         struct floor flbuf;
1507         struct visit vbuf;
1508
1509         lprintf(9, "create_room(%s)\n", new_room_name);
1510         if (getroom(&qrbuf, new_room_name) == 0) {
1511                 lprintf(9, "%s already exists.\n", new_room_name);
1512                 return (0);     /* already exists */
1513         }
1514
1515
1516         memset(&qrbuf, 0, sizeof(struct quickroom));
1517         safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1518         qrbuf.QRflags = QR_INUSE;
1519         if (new_room_type > 0)
1520                 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1521         if (new_room_type == 1)
1522                 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1523         if (new_room_type == 2)
1524                 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1525         if ( (new_room_type == 4) || (new_room_type == 5) )
1526                 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1527
1528         /* If the user is requesting a personal room, set up the room
1529          * name accordingly (prepend the user number)
1530          */
1531         if (new_room_type == 4) {
1532                 MailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->usersupp, new_room_name);
1533         }
1534         else {
1535                 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1536         }
1537
1538         /* If the room is private, and the system administrator has elected
1539          * to automatically grant room aide privileges, do so now; otherwise,
1540          * set the room aide to undefined.
1541          */
1542         if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1543                 qrbuf.QRroomaide = CC->usersupp.usernum;
1544         } else {
1545                 qrbuf.QRroomaide = (-1L);
1546         }
1547
1548         /* 
1549          * If the caller is only interested in testing whether this will work,
1550          * return now without creating the room.
1551          */
1552         if (!really_create) return (qrbuf.QRflags);
1553
1554         qrbuf.QRnumber = get_new_room_number();
1555         qrbuf.QRhighest = 0L;   /* No messages in this room yet */
1556         time(&qrbuf.QRgen);     /* Use a timestamp as the generation number */
1557         qrbuf.QRfloor = new_room_floor;
1558
1559         /* save what we just did... */
1560         putroom(&qrbuf);
1561
1562         /* bump the reference count on whatever floor the room is on */
1563         lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1564         flbuf.f_ref_count = flbuf.f_ref_count + 1;
1565         lputfloor(&flbuf, (int) qrbuf.QRfloor);
1566
1567         /* Grant the creator access to the room unless the avoid_access
1568          * parameter was specified.
1569          */
1570         if (avoid_access == 0) {
1571                 lgetuser(&CC->usersupp, CC->curr_user);
1572                 CtdlGetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1573                 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1574                 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1575                 CtdlSetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1576                 lputuser(&CC->usersupp);
1577         }
1578
1579         /* resume our happy day */
1580         return (qrbuf.QRflags);
1581 }
1582
1583
1584 /*
1585  * create a new room
1586  */
1587 void cmd_cre8(char *args)
1588 {
1589         int cre8_ok;
1590         char new_room_name[SIZ];
1591         int new_room_type;
1592         char new_room_pass[SIZ];
1593         int new_room_floor;
1594         char aaa[SIZ];
1595         unsigned newflags;
1596         struct floor *fl;
1597
1598         cre8_ok = extract_int(args, 0);
1599         extract(new_room_name, args, 1);
1600         new_room_name[ROOMNAMELEN - 1] = 0;
1601         new_room_type = extract_int(args, 2);
1602         extract(new_room_pass, args, 3);
1603         new_room_pass[9] = 0;
1604         new_room_floor = 0;
1605
1606         if ((strlen(new_room_name) == 0) && (cre8_ok == 1)) {
1607                 cprintf("%d Invalid room name.\n", ERROR);
1608                 return;
1609         }
1610
1611         if (!strcasecmp(new_room_name, MAILROOM)) {
1612                 cprintf("%d '%s' already exists.\n",
1613                         ERROR + ALREADY_EXISTS, new_room_name);
1614                 return;
1615         }
1616
1617         if (num_parms(args) >= 5) {
1618                 fl = cgetfloor(extract_int(args, 4));
1619                 if ((fl->f_flags & F_INUSE) == 0) {
1620                         cprintf("%d Invalid floor number.\n",
1621                                 ERROR + INVALID_FLOOR_OPERATION);
1622                         return;
1623                 } else {
1624                         new_room_floor = extract_int(args, 4);
1625                 }
1626         }
1627
1628         if (CtdlAccessCheck(ac_logged_in)) return;
1629
1630         if (CC->usersupp.axlevel < config.c_createax) {
1631                 cprintf("%d You need higher access to create rooms.\n",
1632                         ERROR + HIGHER_ACCESS_REQUIRED);
1633                 return;
1634         }
1635
1636         if ((strlen(new_room_name) == 0) && (cre8_ok == 0)) {
1637                 cprintf("%d Ok to create rooms.\n", CIT_OK);
1638                 return;
1639         }
1640
1641         if ((new_room_type < 0) || (new_room_type > 5)) {
1642                 cprintf("%d Invalid room type.\n", ERROR);
1643                 return;
1644         }
1645
1646         if (new_room_type == 5) {
1647                 if ((config.c_aide_mailboxes == 0)
1648                    || (CC->usersupp.axlevel < 6)) {
1649                         cprintf("%d Higher access required\n", 
1650                                 ERROR+HIGHER_ACCESS_REQUIRED);
1651                         return;
1652                 }
1653         }
1654
1655         /* Check to make sure the requested room name doesn't already exist */
1656         newflags = create_room(new_room_name,
1657                            new_room_type, new_room_pass, new_room_floor, 0, 0);
1658         if (newflags == 0) {
1659                 cprintf("%d '%s' already exists.\n",
1660                         ERROR + ALREADY_EXISTS, new_room_name);
1661                 return;
1662         }
1663
1664         if (cre8_ok == 0) {
1665                 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
1666                 return;
1667         }
1668
1669         /* If we reach this point, the room needs to be created. */
1670
1671         newflags = create_room(new_room_name,
1672                            new_room_type, new_room_pass, new_room_floor, 1, 0);
1673
1674         /* post a message in Aide> describing the new room */
1675         safestrncpy(aaa, new_room_name, sizeof aaa);
1676         strcat(aaa, "> created by ");
1677         strcat(aaa, CC->usersupp.fullname);
1678         if (newflags & QR_MAILBOX)
1679                 strcat(aaa, " [personal]");
1680         else if (newflags & QR_PRIVATE)
1681                 strcat(aaa, " [private]");
1682         if (newflags & QR_GUESSNAME)
1683                 strcat(aaa, "[guessname] ");
1684         if (newflags & QR_PASSWORDED) {
1685                 strcat(aaa, "\n Password: ");
1686                 strcat(aaa, new_room_pass);
1687         }
1688         strcat(aaa, "\n");
1689         aide_message(aaa);
1690
1691         cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
1692 }
1693
1694
1695
1696 void cmd_einf(char *ok)
1697 {                               /* enter info file for current room */
1698         FILE *fp;
1699         char infofilename[SIZ];
1700         char buf[SIZ];
1701
1702         if (CtdlAccessCheck(ac_room_aide)) return;
1703
1704         if (atoi(ok) == 0) {
1705                 cprintf("%d Ok.\n", CIT_OK);
1706                 return;
1707         }
1708         assoc_file_name(infofilename, sizeof infofilename, &CC->quickroom, "info");
1709         lprintf(9, "opening\n");
1710         fp = fopen(infofilename, "w");
1711         lprintf(9, "checking\n");
1712         if (fp == NULL) {
1713                 cprintf("%d Cannot open %s: %s\n",
1714                   ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1715                 return;
1716         }
1717         cprintf("%d Send info...\n", SEND_LISTING);
1718
1719         do {
1720                 client_gets(buf);
1721                 if (strcmp(buf, "000"))
1722                         fprintf(fp, "%s\n", buf);
1723         } while (strcmp(buf, "000"));
1724         fclose(fp);
1725
1726         /* now update the room index so people will see our new info */
1727         lgetroom(&CC->quickroom, CC->quickroom.QRname);         /* lock so no one steps on us */
1728         CC->quickroom.QRinfo = CC->quickroom.QRhighest + 1L;
1729         lputroom(&CC->quickroom);
1730 }
1731
1732
1733 /* 
1734  * cmd_lflr()   -  List all known floors
1735  */
1736 void cmd_lflr(void)
1737 {
1738         int a;
1739         struct floor flbuf;
1740
1741         if (CtdlAccessCheck(ac_logged_in)) return;
1742
1743         cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1744
1745         for (a = 0; a < MAXFLOORS; ++a) {
1746                 getfloor(&flbuf, a);
1747                 if (flbuf.f_flags & F_INUSE) {
1748                         cprintf("%d|%s|%d\n",
1749                                 a,
1750                                 flbuf.f_name,
1751                                 flbuf.f_ref_count);
1752                 }
1753         }
1754         cprintf("000\n");
1755 }
1756
1757
1758
1759 /*
1760  * create a new floor
1761  */
1762 void cmd_cflr(char *argbuf)
1763 {
1764         char new_floor_name[SIZ];
1765         struct floor flbuf;
1766         int cflr_ok;
1767         int free_slot = (-1);
1768         int a;
1769
1770         extract(new_floor_name, argbuf, 0);
1771         cflr_ok = extract_int(argbuf, 1);
1772
1773
1774         if (CtdlAccessCheck(ac_aide)) return;
1775
1776         for (a = 0; a < MAXFLOORS; ++a) {
1777                 getfloor(&flbuf, a);
1778
1779                 /* note any free slots while we're scanning... */
1780                 if (((flbuf.f_flags & F_INUSE) == 0)
1781                     && (free_slot < 0))
1782                         free_slot = a;
1783
1784                 /* check to see if it already exists */
1785                 if ((!strcasecmp(flbuf.f_name, new_floor_name))
1786                     && (flbuf.f_flags & F_INUSE)) {
1787                         cprintf("%d Floor '%s' already exists.\n",
1788                                 ERROR + ALREADY_EXISTS,
1789                                 flbuf.f_name);
1790                         return;
1791                 }
1792         }
1793
1794         if (free_slot < 0) {
1795                 cprintf("%d There is no space available for a new floor.\n",
1796                         ERROR + INVALID_FLOOR_OPERATION);
1797                 return;
1798         }
1799         if (cflr_ok == 0) {
1800                 cprintf("%d ok to create...\n", CIT_OK);
1801                 return;
1802         }
1803         lgetfloor(&flbuf, free_slot);
1804         flbuf.f_flags = F_INUSE;
1805         flbuf.f_ref_count = 0;
1806         safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
1807         lputfloor(&flbuf, free_slot);
1808         cprintf("%d %d\n", CIT_OK, free_slot);
1809 }
1810
1811
1812
1813 /*
1814  * delete a floor
1815  */
1816 void cmd_kflr(char *argbuf)
1817 {
1818         struct floor flbuf;
1819         int floor_to_delete;
1820         int kflr_ok;
1821         int delete_ok;
1822
1823         floor_to_delete = extract_int(argbuf, 0);
1824         kflr_ok = extract_int(argbuf, 1);
1825
1826         if (CtdlAccessCheck(ac_aide)) return;
1827
1828         lgetfloor(&flbuf, floor_to_delete);
1829
1830         delete_ok = 1;
1831         if ((flbuf.f_flags & F_INUSE) == 0) {
1832                 cprintf("%d Floor %d not in use.\n",
1833                         ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
1834                 delete_ok = 0;
1835         } else {
1836                 if (flbuf.f_ref_count != 0) {
1837                         cprintf("%d Cannot delete; floor contains %d rooms.\n",
1838                                 ERROR + INVALID_FLOOR_OPERATION,
1839                                 flbuf.f_ref_count);
1840                         delete_ok = 0;
1841                 } else {
1842                         if (kflr_ok == 1) {
1843                                 cprintf("%d Ok\n", CIT_OK);
1844                         } else {
1845                                 cprintf("%d Ok to delete...\n", CIT_OK);
1846                         }
1847
1848                 }
1849
1850         }
1851
1852         if ((delete_ok == 1) && (kflr_ok == 1))
1853                 flbuf.f_flags = 0;
1854         lputfloor(&flbuf, floor_to_delete);
1855 }
1856
1857 /*
1858  * edit a floor
1859  */
1860 void cmd_eflr(char *argbuf)
1861 {
1862         struct floor flbuf;
1863         int floor_num;
1864         int np;
1865
1866         np = num_parms(argbuf);
1867         if (np < 1) {
1868                 cprintf("%d Usage error.\n", ERROR);
1869                 return;
1870         }
1871
1872         if (CtdlAccessCheck(ac_aide)) return;
1873
1874         floor_num = extract_int(argbuf, 0);
1875         lgetfloor(&flbuf, floor_num);
1876         if ((flbuf.f_flags & F_INUSE) == 0) {
1877                 lputfloor(&flbuf, floor_num);
1878                 cprintf("%d Floor %d is not in use.\n",
1879                         ERROR + INVALID_FLOOR_OPERATION, floor_num);
1880                 return;
1881         }
1882         if (np >= 2)
1883                 extract(flbuf.f_name, argbuf, 1);
1884         lputfloor(&flbuf, floor_num);
1885
1886         cprintf("%d Ok\n", CIT_OK);
1887 }