]> code.citadel.org Git - citadel.git/blob - citadel/room_ops.c
* Extend GETR/SETR to allow twiddling of bits in the QRflags2 bucket
[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|%d\n",
501                 qrbuf->QRflags,
502                 (int) qrbuf->QRfloor,
503                 (int) qrbuf->QRorder,
504                 (int) qrbuf->QRflags2
505         );
506 }
507
508
509 /* 
510  * cmd_lrms()   -  List all accessible rooms, known or forgotten
511  */
512 void cmd_lrms_backend(struct quickroom *qrbuf, void *data)
513 {
514         int FloorBeingSearched = (-1);
515         FloorBeingSearched = *(int *)data;
516
517         if (((CtdlRoomAccess(qrbuf, &CC->usersupp)
518               & (UA_KNOWN | UA_ZAPPED)))
519             && ((qrbuf->QRfloor == (FloorBeingSearched))
520                 || ((FloorBeingSearched) < 0)))
521                 list_roomname(qrbuf);
522 }
523
524 void cmd_lrms(char *argbuf)
525 {
526         int FloorBeingSearched = (-1);
527         if (strlen(argbuf) > 0)
528                 FloorBeingSearched = extract_int(argbuf, 0);
529
530         if (CtdlAccessCheck(ac_logged_in)) return;
531
532         if (getuser(&CC->usersupp, CC->curr_user)) {
533                 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
534                 return;
535         }
536         cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
537
538         ForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
539         cprintf("000\n");
540 }
541
542
543
544 /* 
545  * cmd_lkra()   -  List all known rooms
546  */
547 void cmd_lkra_backend(struct quickroom *qrbuf, void *data)
548 {
549         int FloorBeingSearched = (-1);
550         FloorBeingSearched = *(int *)data;
551
552         if (((CtdlRoomAccess(qrbuf, &CC->usersupp)
553               & (UA_KNOWN)))
554             && ((qrbuf->QRfloor == (FloorBeingSearched))
555                 || ((FloorBeingSearched) < 0)))
556                 list_roomname(qrbuf);
557 }
558
559 void cmd_lkra(char *argbuf)
560 {
561         int FloorBeingSearched = (-1);
562         if (strlen(argbuf) > 0)
563                 FloorBeingSearched = extract_int(argbuf, 0);
564
565         if (CtdlAccessCheck(ac_logged_in)) return;
566         
567         if (getuser(&CC->usersupp, CC->curr_user)) {
568                 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
569                 return;
570         }
571         cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
572
573         ForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
574         cprintf("000\n");
575 }
576
577
578
579 /* 
580  * cmd_lkrn()   -  List all known rooms with new messages
581  */
582 void cmd_lkrn_backend(struct quickroom *qrbuf, void *data)
583 {
584         int ra;
585         int FloorBeingSearched = (-1);
586         FloorBeingSearched = *(int *)data;
587
588         ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
589         if ((ra & UA_KNOWN)
590             && (ra & UA_HASNEWMSGS)
591             && ((qrbuf->QRfloor == (FloorBeingSearched))
592                 || ((FloorBeingSearched) < 0)))
593                 list_roomname(qrbuf);
594 }
595
596 void cmd_lkrn(char *argbuf)
597 {
598         int FloorBeingSearched = (-1);
599         if (strlen(argbuf) > 0)
600                 FloorBeingSearched = extract_int(argbuf, 0);
601
602         if (CtdlAccessCheck(ac_logged_in)) return;
603         
604         if (getuser(&CC->usersupp, CC->curr_user)) {
605                 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
606                 return;
607         }
608         cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
609
610         ForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
611         cprintf("000\n");
612 }
613
614
615
616 /* 
617  * cmd_lkro()   -  List all known rooms
618  */
619 void cmd_lkro_backend(struct quickroom *qrbuf, void *data)
620 {
621         int ra;
622         int FloorBeingSearched = (-1);
623         FloorBeingSearched = *(int *)data;
624
625         ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
626         if ((ra & UA_KNOWN)
627             && ((ra & UA_HASNEWMSGS) == 0)
628             && ((qrbuf->QRfloor == (FloorBeingSearched))
629                 || ((FloorBeingSearched) < 0)))
630                 list_roomname(qrbuf);
631 }
632
633 void cmd_lkro(char *argbuf)
634 {
635         int FloorBeingSearched = (-1);
636         if (strlen(argbuf) > 0)
637                 FloorBeingSearched = extract_int(argbuf, 0);
638
639         if (CtdlAccessCheck(ac_logged_in)) return;
640         
641         if (getuser(&CC->usersupp, CC->curr_user)) {
642                 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
643                 return;
644         }
645         cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
646
647         ForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
648         cprintf("000\n");
649 }
650
651
652
653 /* 
654  * cmd_lzrm()   -  List all forgotten rooms
655  */
656 void cmd_lzrm_backend(struct quickroom *qrbuf, void *data)
657 {
658         int ra;
659         int FloorBeingSearched = (-1);
660         FloorBeingSearched = *(int *)data;
661
662         ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
663         if ((ra & UA_GOTOALLOWED)
664             && (ra & UA_ZAPPED)
665             && ((qrbuf->QRfloor == (FloorBeingSearched))
666                 || ((FloorBeingSearched) < 0)))
667                 list_roomname(qrbuf);
668 }
669
670 void cmd_lzrm(char *argbuf)
671 {
672         int FloorBeingSearched = (-1);
673         if (strlen(argbuf) > 0)
674                 FloorBeingSearched = extract_int(argbuf, 0);
675
676         if (CtdlAccessCheck(ac_logged_in)) return;
677         
678         if (getuser(&CC->usersupp, CC->curr_user)) {
679                 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
680                 return;
681         }
682         cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
683
684         ForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
685         cprintf("000\n");
686 }
687
688
689 /*
690  * Make the specified room the current room for this session.  No validation
691  * or access control is done here -- the caller should make sure that the
692  * specified room exists and is ok to access.
693  */
694 void usergoto(char *where, int display_result, int transiently,
695                 int *retmsgs, int *retnew)
696 {
697         int a;
698         int new_messages = 0;
699         int total_messages = 0;
700         int info = 0;
701         int rmailflag;
702         int raideflag;
703         int newmailcount = 0;
704         struct visit vbuf;
705         char truncated_roomname[ROOMNAMELEN];
706         struct cdbdata *cdbfr;
707         long *msglist = NULL;
708         int num_msgs = 0;
709
710         /* If the supplied room name is NULL, the caller wants us to know that
711          * it has already copied the quickroom record into CC->quickroom, so
712          * we can skip the extra database fetch.
713          */
714         if (where != NULL) {
715                 strcpy(CC->quickroom.QRname, where);
716                 getroom(&CC->quickroom, where);
717         }
718
719         /* Take care of all the formalities. */
720
721         begin_critical_section(S_USERSUPP);
722         CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
723
724         /* Know the room ... but not if it's the page log room, or if the
725          * caller specified that we're only entering this room transiently.
726          */
727         if ((strcasecmp(CC->quickroom.QRname, config.c_logpages))
728            && (transiently == 0) ) {
729                 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
730                 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
731         }
732         CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
733         end_critical_section(S_USERSUPP);
734
735         /* Check for new mail */
736         newmailcount = NewMailCount();
737
738         /* set info to 1 if the user needs to read the room's info file */
739         if (CC->quickroom.QRinfo > vbuf.v_lastseen) {
740                 info = 1;
741         }
742
743         get_mm();
744         cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->quickroom.QRnumber, sizeof(long));
745         if (cdbfr != NULL) {
746                 msglist = mallok(cdbfr->len);
747                 memcpy(msglist, cdbfr->ptr, cdbfr->len);
748                 num_msgs = cdbfr->len / sizeof(long);
749                 cdb_free(cdbfr);
750         }
751
752         if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
753                 if (msglist[a] > 0L) {
754                         ++total_messages;
755                         if (is_msg_in_mset(vbuf.v_seen, msglist[a]) == 0) {
756                                 ++new_messages;
757                         }
758                 }
759         }
760
761         if (msglist != NULL) phree(msglist);
762
763         if (CC->quickroom.QRflags & QR_MAILBOX)
764                 rmailflag = 1;
765         else
766                 rmailflag = 0;
767
768         if ((CC->quickroom.QRroomaide == CC->usersupp.usernum)
769             || (CC->usersupp.axlevel >= 6))
770                 raideflag = 1;
771         else
772                 raideflag = 0;
773
774         strcpy(truncated_roomname, CC->quickroom.QRname);
775         if ( (CC->quickroom.QRflags & QR_MAILBOX)
776            && (atol(CC->quickroom.QRname) == CC->usersupp.usernum) ) {
777                 strcpy(truncated_roomname, &truncated_roomname[11]);
778         }
779
780         if (retmsgs != NULL) *retmsgs = total_messages;
781         if (retnew != NULL) *retnew = new_messages;
782         lprintf(9, "<%s> %d new of %d total messages\n",
783                 CC->quickroom.QRname, new_messages, total_messages);
784
785         if (display_result)
786                 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d\n",
787                         CIT_OK, CtdlCheckExpress(),
788                         truncated_roomname,
789                         new_messages, total_messages,
790                         info, CC->quickroom.QRflags,
791                         CC->quickroom.QRhighest,
792                         vbuf.v_lastseen,
793                         rmailflag, raideflag, newmailcount,
794                         CC->quickroom.QRfloor,
795                         vbuf.v_view);
796 }
797
798
799 /* 
800  * cmd_goto()  -  goto a new room
801  */
802 void cmd_goto(char *gargs)
803 {
804         struct quickroom QRscratch;
805         int c;
806         int ok = 0;
807         int ra;
808         char augmented_roomname[SIZ];
809         char towhere[SIZ];
810         char password[SIZ];
811         int transiently = 0;
812
813         if (CtdlAccessCheck(ac_logged_in)) return;
814
815         extract(towhere, gargs, 0);
816         extract(password, gargs, 1);
817         transiently = extract_int(gargs, 2);
818
819         getuser(&CC->usersupp, CC->curr_user);
820
821         if (!strcasecmp(towhere, "_BASEROOM_"))
822                 strcpy(towhere, config.c_baseroom);
823
824         if (!strcasecmp(towhere, "_MAIL_"))
825                 strcpy(towhere, MAILROOM);
826
827         if (!strcasecmp(towhere, "_BITBUCKET_"))
828                 strcpy(towhere, config.c_twitroom);
829
830
831         /* First try a regular match */
832         c = getroom(&QRscratch, towhere);
833
834         /* Then try a mailbox name match */
835         if (c != 0) {
836                 MailboxName(augmented_roomname, sizeof augmented_roomname,
837                             &CC->usersupp, towhere);
838                 c = getroom(&QRscratch, augmented_roomname);
839                 if (c == 0)
840                         strcpy(towhere, augmented_roomname);
841         }
842
843         /* And if the room was found... */
844         if (c == 0) {
845
846                 /* Let internal programs go directly to any room. */
847                 if (CC->internal_pgm) {
848                         memcpy(&CC->quickroom, &QRscratch,
849                                 sizeof(struct quickroom));
850                         usergoto(NULL, 1, transiently, NULL, NULL);
851                         return;
852                 }
853
854                 /* See if there is an existing user/room relationship */
855                 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
856
857                 /* normal clients have to pass through security */
858                 if (ra & UA_GOTOALLOWED) {
859                         ok = 1;
860                 }
861
862                 if (ok == 1) {
863                         if ((QRscratch.QRflags & QR_MAILBOX) &&
864                             ((ra & UA_GOTOALLOWED))) {
865                                 memcpy(&CC->quickroom, &QRscratch,
866                                         sizeof(struct quickroom));
867                                 usergoto(NULL, 1, transiently, NULL, NULL);
868                                 return;
869                         } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
870                             ((ra & UA_KNOWN) == 0) &&
871                             (strcasecmp(QRscratch.QRpasswd, password)) &&
872                             (CC->usersupp.axlevel < 6)
873                             ) {
874                                 cprintf("%d wrong or missing passwd\n",
875                                         ERROR + PASSWORD_REQUIRED);
876                                 return;
877                         } else if ((QRscratch.QRflags & QR_PRIVATE) &&
878                                    ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
879                                    ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
880                                    ((ra & UA_KNOWN) == 0) &&
881                                    (CC->usersupp.axlevel < 6)
882                                   ) {
883                                 lprintf(9, "Failed to acquire private room\n");
884                                 goto NOPE;
885                         } else {
886                                 memcpy(&CC->quickroom, &QRscratch,
887                                         sizeof(struct quickroom));
888                                 usergoto(NULL, 1, transiently, NULL, NULL);
889                                 return;
890                         }
891                 }
892         }
893
894 NOPE:   cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
895 }
896
897
898 void cmd_whok(void)
899 {
900         struct usersupp temp;
901         struct cdbdata *cdbus;
902
903         getuser(&CC->usersupp, CC->curr_user);
904
905         /*
906          * This command is only allowed by aides, room aides,
907          * and room namespace owners
908          */
909         if (is_room_aide()
910            || (atol(CC->quickroom.QRname) == CC->usersupp.usernum) ) {
911                 /* access granted */
912         }
913         else {
914                 /* access denied */
915                 cprintf("%d Higher access or room ownership required.\n",
916                         ERROR + HIGHER_ACCESS_REQUIRED);
917                 return;
918         }
919
920         cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
921         cdb_rewind(CDB_USERSUPP);
922         while (cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
923                 memset(&temp, 0, sizeof temp);
924                 memcpy(&temp, cdbus->ptr, sizeof temp);
925                 cdb_free(cdbus);
926
927                 if ((CC->quickroom.QRflags & QR_INUSE)
928                     && (CtdlRoomAccess(&CC->quickroom, &temp) & UA_KNOWN)
929                     )
930                         cprintf("%s\n", temp.fullname);
931         }
932         cprintf("000\n");
933 }
934
935
936 /*
937  * RDIR command for room directory
938  */
939 void cmd_rdir(void)
940 {
941         char buf[SIZ];
942         char flnm[SIZ];
943         char comment[SIZ];
944         FILE *ls, *fd;
945         struct stat statbuf;
946
947         if (CtdlAccessCheck(ac_logged_in)) return;
948         
949         getroom(&CC->quickroom, CC->quickroom.QRname);
950         getuser(&CC->usersupp, CC->curr_user);
951
952         if ((CC->quickroom.QRflags & QR_DIRECTORY) == 0) {
953                 cprintf("%d not here.\n", ERROR + NOT_HERE);
954                 return;
955         }
956         if (((CC->quickroom.QRflags & QR_VISDIR) == 0)
957             && (CC->usersupp.axlevel < 6)
958             && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
959                 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
960                 return;
961         }
962         cprintf("%d %s|%s/files/%s\n",
963         LISTING_FOLLOWS, config.c_fqdn, BBSDIR, CC->quickroom.QRdirname);
964
965         snprintf(buf, sizeof buf, "ls %s/files/%s  >%s 2> /dev/null",
966                 BBSDIR, CC->quickroom.QRdirname, CC->temp);
967         system(buf);
968
969         snprintf(buf, sizeof buf, "%s/files/%s/filedir", BBSDIR, CC->quickroom.QRdirname);
970         fd = fopen(buf, "r");
971         if (fd == NULL)
972                 fd = fopen("/dev/null", "r");
973
974         ls = fopen(CC->temp, "r");
975         while (fgets(flnm, sizeof flnm, ls) != NULL) {
976                 flnm[strlen(flnm) - 1] = 0;
977                 if (strcasecmp(flnm, "filedir")) {
978                         snprintf(buf, sizeof buf, "%s/files/%s/%s",
979                                 BBSDIR, CC->quickroom.QRdirname, flnm);
980                         stat(buf, &statbuf);
981                         strcpy(comment, "");
982                         fseek(fd, 0L, 0);
983                         while ((fgets(buf, sizeof buf, fd) != NULL)
984                                && (strlen(comment) == 0)) {
985                                 buf[strlen(buf) - 1] = 0;
986                                 if ((!strncasecmp(buf, flnm, strlen(flnm)))
987                                     && (buf[strlen(flnm)] == ' '))
988                                         safestrncpy(comment,
989                                             &buf[strlen(flnm) + 1],
990                                             sizeof comment);
991                         }
992                         cprintf("%s|%ld|%s\n", flnm, (long)statbuf.st_size, comment);
993                 }
994         }
995         fclose(ls);
996         fclose(fd);
997         unlink(CC->temp);
998
999         cprintf("000\n");
1000 }
1001
1002 /*
1003  * get room parameters (aide or room aide command)
1004  */
1005 void cmd_getr(void)
1006 {
1007         if (CtdlAccessCheck(ac_room_aide)) return;
1008
1009         getroom(&CC->quickroom, CC->quickroom.QRname);
1010         cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1011                 CIT_OK,
1012                 CtdlCheckExpress(),
1013
1014                 ((CC->quickroom.QRflags & QR_MAILBOX) ?
1015                         &CC->quickroom.QRname[11] : CC->quickroom.QRname),
1016
1017                 ((CC->quickroom.QRflags & QR_PASSWORDED) ?
1018                         CC->quickroom.QRpasswd : ""),
1019
1020                 ((CC->quickroom.QRflags & QR_DIRECTORY) ?
1021                         CC->quickroom.QRdirname : ""),
1022
1023                 CC->quickroom.QRflags,
1024                 (int) CC->quickroom.QRfloor,
1025                 (int) CC->quickroom.QRorder,
1026
1027                 CC->quickroom.QRdefaultview,
1028                 CC->quickroom.QRflags2
1029                 );
1030 }
1031
1032
1033 /*
1034  * Back end function to rename a room.
1035  * You can also specify which floor to move the room to, or specify -1 to
1036  * keep the room on the same floor it was on.
1037  *
1038  * If you are renaming a mailbox room, you must supply the namespace prefix
1039  * in *at least* the old name!
1040  */
1041 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1042         int old_floor = 0;
1043         struct quickroom qrbuf;
1044         struct quickroom qrtmp;
1045         int ret = 0;
1046         struct floor *fl;
1047         struct floor flbuf;
1048         long owner = 0L;
1049
1050         lprintf(9, "CtdlRenameRoom(%s, %s, %d)\n",
1051                 old_name, new_name, new_floor);
1052
1053         if (new_floor >= 0) {
1054                 fl = cgetfloor(new_floor);
1055                 if ((fl->f_flags & F_INUSE) == 0) {
1056                         return(crr_invalid_floor);
1057                 }
1058         }
1059
1060         begin_critical_section(S_QUICKROOM);
1061
1062         if ( (getroom(&qrtmp, new_name) == 0) 
1063            && (strcasecmp(new_name, old_name)) ) {
1064                 ret = crr_already_exists;
1065         }
1066
1067         else if (getroom(&qrbuf, old_name) != 0) {
1068                 ret = crr_room_not_found;
1069         }
1070
1071         else if ( (CC->usersupp.axlevel < 6)
1072                   && (CC->usersupp.usernum != qrbuf.QRroomaide)
1073                   && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->usersupp.usernum))) )  {
1074                 ret = crr_access_denied;
1075         }
1076
1077         else if (is_noneditable(&qrbuf)) {
1078                 ret = crr_noneditable;
1079         }
1080
1081         else {
1082                 /* Rename it */
1083                 if (qrbuf.QRflags & QR_MAILBOX) {
1084                         owner = atol(qrbuf.QRname);
1085                 }
1086                 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1087                         snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1088                                         "%010ld.%s", owner, new_name);
1089                 }
1090                 else {
1091                         safestrncpy(qrbuf.QRname, new_name,
1092                                                 sizeof(qrbuf.QRname));
1093                 }
1094
1095                 /* Reject change of floor for baseroom/aideroom */
1096                 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1097                     !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1098                         new_floor = 0;
1099                 }
1100
1101                 /* Take care of floor stuff */
1102                 old_floor = qrbuf.QRfloor;
1103                 if (new_floor < 0) {
1104                         new_floor = old_floor;
1105                 }
1106                 qrbuf.QRfloor = new_floor;
1107                 putroom(&qrbuf);
1108
1109                 begin_critical_section(S_CONFIG);
1110         
1111                 /* If baseroom/aideroom name changes, update config */
1112                 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1113                         safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1114                         put_config();
1115                 }
1116                 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1117                         safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1118                         put_config();
1119                 }
1120         
1121                 end_critical_section(S_CONFIG);
1122         
1123                 /* If the room name changed, then there are now two room
1124                  * records, so we have to delete the old one.
1125                  */
1126                 if (strcasecmp(new_name, old_name)) {
1127                         b_deleteroom(old_name);
1128                 }
1129
1130                 ret = crr_ok;
1131         }
1132
1133         end_critical_section(S_QUICKROOM);
1134
1135         /* Adjust the floor reference counts if necessary */
1136         if (new_floor != old_floor) {
1137                 lgetfloor(&flbuf, old_floor);
1138                 --flbuf.f_ref_count;
1139                 lputfloor(&flbuf, old_floor);
1140                 lgetfloor(&flbuf, CC->quickroom.QRfloor);
1141                 ++flbuf.f_ref_count;
1142                 lputfloor(&flbuf, CC->quickroom.QRfloor);
1143         }
1144
1145         /* ...and everybody say "YATTA!" */     
1146         return(ret);
1147 }
1148
1149
1150 /*
1151  * set room parameters (aide or room aide command)
1152  */
1153 void cmd_setr(char *args)
1154 {
1155         char buf[SIZ];
1156         int new_order = 0;
1157         int r;
1158         int new_floor;
1159         char new_name[ROOMNAMELEN];
1160
1161         if (CtdlAccessCheck(ac_logged_in)) return;
1162
1163         if (num_parms(args) >= 6) {
1164                 new_floor = extract_int(args, 5);
1165         } else {
1166                 new_floor = (-1);       /* don't change the floor */
1167         }
1168
1169         /* When is a new name more than just a new name?  When the old name
1170          * has a namespace prefix.
1171          */
1172         if (CC->quickroom.QRflags & QR_MAILBOX) {
1173                 sprintf(new_name, "%010ld.", atol(CC->quickroom.QRname) );
1174         } else {
1175                 strcpy(new_name, "");
1176         }
1177         extract(&new_name[strlen(new_name)], args, 0);
1178
1179         r = CtdlRenameRoom(CC->quickroom.QRname, new_name, new_floor);
1180
1181         if (r == crr_room_not_found) {
1182                 cprintf("%d Internal error - room not found?\n", ERROR);
1183         } else if (r == crr_already_exists) {
1184                 cprintf("%d '%s' already exists.\n",
1185                         ERROR + ALREADY_EXISTS, new_name);
1186         } else if (r == crr_noneditable) {
1187                 cprintf("%d Cannot edit this room.\n", ERROR);
1188         } else if (r == crr_invalid_floor) {
1189                 cprintf("%d Target floor does not exist.\n",
1190                         ERROR + INVALID_FLOOR_OPERATION);
1191         } else if (r == crr_access_denied) {
1192                 cprintf("%d You do not have permission to edit '%s'\n",
1193                         ERROR + HIGHER_ACCESS_REQUIRED,
1194                         CC->quickroom.QRname);
1195         } else if (r != crr_ok) {
1196                 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1197                         ERROR, r);
1198         }
1199
1200         if (r != crr_ok) {
1201                 return;
1202         }
1203
1204         getroom(&CC->quickroom, new_name);
1205
1206         /* Now we have to do a bunch of other stuff */
1207
1208         if (num_parms(args) >= 7) {
1209                 new_order = extract_int(args, 6);
1210                 if (new_order < 1)
1211                         new_order = 1;
1212                 if (new_order > 127)
1213                         new_order = 127;
1214         }
1215
1216         lgetroom(&CC->quickroom, CC->quickroom.QRname);
1217
1218         /* Directory room */
1219         extract(buf, args, 2);
1220         buf[15] = 0;
1221         safestrncpy(CC->quickroom.QRdirname, buf,
1222                 sizeof CC->quickroom.QRdirname);
1223
1224         /* Default view */
1225         if (num_parms(args) >= 8) {
1226                 CC->quickroom.QRdefaultview = extract_int(args, 7);
1227         }
1228
1229         /* Second set of flags */
1230         if (num_parms(args) >= 9) {
1231                 CC->quickroom.QRflags2 = extract_int(args, 8);
1232         }
1233
1234         /* Misc. flags */
1235         CC->quickroom.QRflags = (extract_int(args, 3) | QR_INUSE);
1236         /* Clean up a client boo-boo: if the client set the room to
1237          * guess-name or passworded, ensure that the private flag is
1238          * also set.
1239          */
1240         if ((CC->quickroom.QRflags & QR_GUESSNAME)
1241             || (CC->quickroom.QRflags & QR_PASSWORDED))
1242                 CC->quickroom.QRflags |= QR_PRIVATE;
1243
1244         /* Some changes can't apply to BASEROOM */
1245         if (!strncasecmp(CC->quickroom.QRname, config.c_baseroom,
1246                          ROOMNAMELEN)) {
1247                 CC->quickroom.QRorder = 0;
1248                 CC->quickroom.QRpasswd[0] = '\0';
1249                 CC->quickroom.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1250                         QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1251                 CC->quickroom.QRflags |= QR_PERMANENT;
1252         } else {        
1253                 /* March order (doesn't apply to AIDEROOM) */
1254                 if (num_parms(args) >= 7)
1255                         CC->quickroom.QRorder = (char) new_order;
1256                 /* Room password */
1257                 extract(buf, args, 1);
1258                 buf[10] = 0;
1259                 safestrncpy(CC->quickroom.QRpasswd, buf,
1260                             sizeof CC->quickroom.QRpasswd);
1261                 /* Kick everyone out if the client requested it
1262                  * (by changing the room's generation number)
1263                  */
1264                 if (extract_int(args, 4)) {
1265                         time(&CC->quickroom.QRgen);
1266                 }
1267         }
1268         /* Some changes can't apply to AIDEROOM */
1269         if (!strncasecmp(CC->quickroom.QRname, config.c_baseroom,
1270                          ROOMNAMELEN)) {
1271                 CC->quickroom.QRorder = 0;
1272                 CC->quickroom.QRflags &= ~QR_MAILBOX;
1273                 CC->quickroom.QRflags |= QR_PERMANENT;
1274         }
1275
1276         /* Write the room record back to disk */
1277         lputroom(&CC->quickroom);
1278
1279         /* Create a room directory if necessary */
1280         if (CC->quickroom.QRflags & QR_DIRECTORY) {
1281                 snprintf(buf, sizeof buf,
1282                     "mkdir ./files/%s </dev/null >/dev/null 2>/dev/null",
1283                         CC->quickroom.QRdirname);
1284                 system(buf);
1285         }
1286         snprintf(buf, sizeof buf, "%s> edited by %s\n", CC->quickroom.QRname, CC->curr_user);
1287         aide_message(buf);
1288         cprintf("%d Ok\n", CIT_OK);
1289 }
1290
1291
1292
1293 /* 
1294  * get the name of the room aide for this room
1295  */
1296 void cmd_geta(void)
1297 {
1298         struct usersupp usbuf;
1299
1300         if (CtdlAccessCheck(ac_logged_in)) return;
1301
1302         if (getuserbynumber(&usbuf, CC->quickroom.QRroomaide) == 0) {
1303                 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1304         } else {
1305                 cprintf("%d \n", CIT_OK);
1306         }
1307 }
1308
1309
1310 /* 
1311  * set the room aide for this room
1312  */
1313 void cmd_seta(char *new_ra)
1314 {
1315         struct usersupp usbuf;
1316         long newu;
1317         char buf[SIZ];
1318         int post_notice;
1319
1320         if (CtdlAccessCheck(ac_room_aide)) return;
1321
1322         if (getuser(&usbuf, new_ra) != 0) {
1323                 newu = (-1L);
1324         } else {
1325                 newu = usbuf.usernum;
1326         }
1327
1328         lgetroom(&CC->quickroom, CC->quickroom.QRname);
1329         post_notice = 0;
1330         if (CC->quickroom.QRroomaide != newu) {
1331                 post_notice = 1;
1332         }
1333         CC->quickroom.QRroomaide = newu;
1334         lputroom(&CC->quickroom);
1335
1336         /*
1337          * We have to post the change notice _after_ writing changes to 
1338          * the room table, otherwise it would deadlock!
1339          */
1340         if (post_notice == 1) {
1341                 if (strlen(usbuf.fullname) > 0)
1342                         snprintf(buf, sizeof buf,
1343                                 "%s is now room aide for %s>\n",
1344                                 usbuf.fullname, CC->quickroom.QRname);
1345                 else
1346                         snprintf(buf, sizeof buf,
1347                                 "There is now no room aide for %s>\n",
1348                                 CC->quickroom.QRname);
1349                 aide_message(buf);
1350         }
1351         cprintf("%d Ok\n", CIT_OK);
1352 }
1353
1354 /*
1355  * Generate an associated file name for a room
1356  */
1357 void assoc_file_name(char *buf, size_t n,
1358                      struct quickroom *qrbuf, const char *prefix)
1359 {
1360         snprintf(buf, n, "./%s/%ld", prefix, qrbuf->QRnumber);
1361 }
1362
1363 /* 
1364  * retrieve info file for this room
1365  */
1366 void cmd_rinf(void)
1367 {
1368         char filename[128];
1369         char buf[SIZ];
1370         FILE *info_fp;
1371
1372         assoc_file_name(filename, sizeof filename, &CC->quickroom, "info");
1373         info_fp = fopen(filename, "r");
1374
1375         if (info_fp == NULL) {
1376                 cprintf("%d No info file.\n", ERROR);
1377                 return;
1378         }
1379         cprintf("%d Info:\n", LISTING_FOLLOWS);
1380         while (fgets(buf, sizeof buf, info_fp) != NULL) {
1381                 if (strlen(buf) > 0)
1382                         buf[strlen(buf) - 1] = 0;
1383                 cprintf("%s\n", buf);
1384         }
1385         cprintf("000\n");
1386         fclose(info_fp);
1387 }
1388
1389 /*
1390  * Back end processing to delete a room and everything associated with it
1391  */
1392 void delete_room(struct quickroom *qrbuf)
1393 {
1394         struct floor flbuf;
1395         char filename[100];
1396
1397         lprintf(9, "Deleting room <%s>\n", qrbuf->QRname);
1398
1399         /* Delete the info file */
1400         assoc_file_name(filename, sizeof filename, qrbuf, "info");
1401         unlink(filename);
1402
1403         /* Delete the image file */
1404         assoc_file_name(filename, sizeof filename, qrbuf, "images");
1405         unlink(filename);
1406
1407         /* Delete the room's network config file */
1408         assoc_file_name(filename, sizeof filename, qrbuf, "netconfigs");
1409         unlink(filename);
1410
1411         /* Delete the messages in the room
1412          * (Careful: this opens an S_QUICKROOM critical section!)
1413          */
1414         CtdlDeleteMessages(qrbuf->QRname, 0L, "");
1415
1416         /* Flag the room record as not in use */
1417         lgetroom(qrbuf, qrbuf->QRname);
1418         qrbuf->QRflags = 0;
1419         lputroom(qrbuf);
1420
1421         /* then decrement the reference count for the floor */
1422         lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1423         flbuf.f_ref_count = flbuf.f_ref_count - 1;
1424         lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1425
1426         /* Delete the room record from the database! */
1427         b_deleteroom(qrbuf->QRname);
1428 }
1429
1430
1431
1432 /*
1433  * Check access control for deleting a room
1434  */
1435 int CtdlDoIHavePermissionToDeleteThisRoom(struct quickroom *qr) {
1436
1437         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1438                 return(0);
1439         }
1440
1441         if (is_noneditable(qr)) {
1442                 return(0);
1443         }
1444
1445         /*
1446          * For mailboxes, check stuff
1447          */
1448         if (qr->QRflags & QR_MAILBOX) {
1449
1450                 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1451
1452                 if (atol(qr->QRname) != CC->usersupp.usernum) {
1453                         return(0);      /* not my room */
1454                 }
1455
1456                 /* Can't delete your Mail> room */
1457                 if (!strcasecmp(&qr->QRname[12], MAILROOM)) return(0);
1458
1459                 /* Otherwise it's ok */
1460                 return(1);
1461         }
1462
1463         /*
1464          * For normal rooms, just check for aide or room aide status.
1465          */
1466         return(is_room_aide());
1467 }
1468
1469 /*
1470  * aide command: kill the current room
1471  */
1472 void cmd_kill(char *argbuf)
1473 {
1474         char aaa[100];
1475         char deleted_room_name[ROOMNAMELEN];
1476         int kill_ok;
1477
1478         kill_ok = extract_int(argbuf, 0);
1479
1480         if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->quickroom) == 0) {
1481                 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1482                 return;
1483         }
1484         if (kill_ok) {
1485                 strcpy(deleted_room_name, CC->quickroom.QRname);
1486                 delete_room(&CC->quickroom);    /* Do the dirty work */
1487                 usergoto(config.c_baseroom, 0, 0, NULL, NULL); /* Return to the Lobby */
1488
1489                 /* tell the world what we did */
1490                 snprintf(aaa, sizeof aaa, "%s> killed by %s\n",
1491                          deleted_room_name, CC->curr_user);
1492                 aide_message(aaa);
1493                 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1494         } else {
1495                 cprintf("%d ok to delete.\n", CIT_OK);
1496         }
1497 }
1498
1499
1500 /*
1501  * Internal code to create a new room (returns room flags)
1502  *
1503  * Room types:  0=public, 1=guessname, 2=passworded, 3=inv-only,
1504  *              4=mailbox, 5=mailbox, but caller supplies namespace
1505  */
1506 unsigned create_room(char *new_room_name,
1507                      int new_room_type,
1508                      char *new_room_pass,
1509                      int new_room_floor,
1510                      int really_create,
1511                      int avoid_access)
1512 {
1513
1514         struct quickroom qrbuf;
1515         struct floor flbuf;
1516         struct visit vbuf;
1517
1518         lprintf(9, "create_room(%s)\n", new_room_name);
1519         if (getroom(&qrbuf, new_room_name) == 0) {
1520                 lprintf(9, "%s already exists.\n", new_room_name);
1521                 return (0);     /* already exists */
1522         }
1523
1524
1525         memset(&qrbuf, 0, sizeof(struct quickroom));
1526         safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1527         qrbuf.QRflags = QR_INUSE;
1528         if (new_room_type > 0)
1529                 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1530         if (new_room_type == 1)
1531                 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1532         if (new_room_type == 2)
1533                 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1534         if ( (new_room_type == 4) || (new_room_type == 5) )
1535                 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1536
1537         /* If the user is requesting a personal room, set up the room
1538          * name accordingly (prepend the user number)
1539          */
1540         if (new_room_type == 4) {
1541                 MailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->usersupp, new_room_name);
1542         }
1543         else {
1544                 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1545         }
1546
1547         /* If the room is private, and the system administrator has elected
1548          * to automatically grant room aide privileges, do so now; otherwise,
1549          * set the room aide to undefined.
1550          */
1551         if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1552                 qrbuf.QRroomaide = CC->usersupp.usernum;
1553         } else {
1554                 qrbuf.QRroomaide = (-1L);
1555         }
1556
1557         /* 
1558          * If the caller is only interested in testing whether this will work,
1559          * return now without creating the room.
1560          */
1561         if (!really_create) return (qrbuf.QRflags);
1562
1563         qrbuf.QRnumber = get_new_room_number();
1564         qrbuf.QRhighest = 0L;   /* No messages in this room yet */
1565         time(&qrbuf.QRgen);     /* Use a timestamp as the generation number */
1566         qrbuf.QRfloor = new_room_floor;
1567
1568         /* save what we just did... */
1569         putroom(&qrbuf);
1570
1571         /* bump the reference count on whatever floor the room is on */
1572         lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1573         flbuf.f_ref_count = flbuf.f_ref_count + 1;
1574         lputfloor(&flbuf, (int) qrbuf.QRfloor);
1575
1576         /* Grant the creator access to the room unless the avoid_access
1577          * parameter was specified.
1578          */
1579         if (avoid_access == 0) {
1580                 lgetuser(&CC->usersupp, CC->curr_user);
1581                 CtdlGetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1582                 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1583                 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1584                 CtdlSetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1585                 lputuser(&CC->usersupp);
1586         }
1587
1588         /* resume our happy day */
1589         return (qrbuf.QRflags);
1590 }
1591
1592
1593 /*
1594  * create a new room
1595  */
1596 void cmd_cre8(char *args)
1597 {
1598         int cre8_ok;
1599         char new_room_name[SIZ];
1600         int new_room_type;
1601         char new_room_pass[SIZ];
1602         int new_room_floor;
1603         char aaa[SIZ];
1604         unsigned newflags;
1605         struct floor *fl;
1606         int avoid_access = 0;
1607
1608         cre8_ok = extract_int(args, 0);
1609         extract(new_room_name, args, 1);
1610         new_room_name[ROOMNAMELEN - 1] = 0;
1611         new_room_type = extract_int(args, 2);
1612         extract(new_room_pass, args, 3);
1613         avoid_access = extract_int(args, 5);
1614         new_room_pass[9] = 0;
1615         new_room_floor = 0;
1616
1617         if ((strlen(new_room_name) == 0) && (cre8_ok == 1)) {
1618                 cprintf("%d Invalid room name.\n", ERROR);
1619                 return;
1620         }
1621
1622         if (!strcasecmp(new_room_name, MAILROOM)) {
1623                 cprintf("%d '%s' already exists.\n",
1624                         ERROR + ALREADY_EXISTS, new_room_name);
1625                 return;
1626         }
1627
1628         if (num_parms(args) >= 5) {
1629                 fl = cgetfloor(extract_int(args, 4));
1630                 if ((fl->f_flags & F_INUSE) == 0) {
1631                         cprintf("%d Invalid floor number.\n",
1632                                 ERROR + INVALID_FLOOR_OPERATION);
1633                         return;
1634                 } else {
1635                         new_room_floor = extract_int(args, 4);
1636                 }
1637         }
1638
1639         if (CtdlAccessCheck(ac_logged_in)) return;
1640
1641         if (CC->usersupp.axlevel < config.c_createax) {
1642                 cprintf("%d You need higher access to create rooms.\n",
1643                         ERROR + HIGHER_ACCESS_REQUIRED);
1644                 return;
1645         }
1646
1647         if ((strlen(new_room_name) == 0) && (cre8_ok == 0)) {
1648                 cprintf("%d Ok to create rooms.\n", CIT_OK);
1649                 return;
1650         }
1651
1652         if ((new_room_type < 0) || (new_room_type > 5)) {
1653                 cprintf("%d Invalid room type.\n", ERROR);
1654                 return;
1655         }
1656
1657         if (new_room_type == 5) {
1658                 if ((config.c_aide_mailboxes == 0)
1659                    || (CC->usersupp.axlevel < 6)) {
1660                         cprintf("%d Higher access required\n", 
1661                                 ERROR+HIGHER_ACCESS_REQUIRED);
1662                         return;
1663                 }
1664         }
1665
1666         /* Check to make sure the requested room name doesn't already exist */
1667         newflags = create_room(new_room_name,
1668                                 new_room_type, new_room_pass, new_room_floor,
1669                                 0, avoid_access);
1670         if (newflags == 0) {
1671                 cprintf("%d '%s' already exists.\n",
1672                         ERROR + ALREADY_EXISTS, new_room_name);
1673                 return;
1674         }
1675
1676         if (cre8_ok == 0) {
1677                 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
1678                 return;
1679         }
1680
1681         /* If we reach this point, the room needs to be created. */
1682
1683         newflags = create_room(new_room_name,
1684                            new_room_type, new_room_pass, new_room_floor, 1, 0);
1685
1686         /* post a message in Aide> describing the new room */
1687         safestrncpy(aaa, new_room_name, sizeof aaa);
1688         strcat(aaa, "> created by ");
1689         strcat(aaa, CC->usersupp.fullname);
1690         if (newflags & QR_MAILBOX)
1691                 strcat(aaa, " [personal]");
1692         else if (newflags & QR_PRIVATE)
1693                 strcat(aaa, " [private]");
1694         if (newflags & QR_GUESSNAME)
1695                 strcat(aaa, "[guessname] ");
1696         if (newflags & QR_PASSWORDED) {
1697                 strcat(aaa, "\n Password: ");
1698                 strcat(aaa, new_room_pass);
1699         }
1700         strcat(aaa, "\n");
1701         aide_message(aaa);
1702
1703         cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
1704 }
1705
1706
1707
1708 void cmd_einf(char *ok)
1709 {                               /* enter info file for current room */
1710         FILE *fp;
1711         char infofilename[SIZ];
1712         char buf[SIZ];
1713
1714         if (CtdlAccessCheck(ac_room_aide)) return;
1715
1716         if (atoi(ok) == 0) {
1717                 cprintf("%d Ok.\n", CIT_OK);
1718                 return;
1719         }
1720         assoc_file_name(infofilename, sizeof infofilename, &CC->quickroom, "info");
1721         lprintf(9, "opening\n");
1722         fp = fopen(infofilename, "w");
1723         lprintf(9, "checking\n");
1724         if (fp == NULL) {
1725                 cprintf("%d Cannot open %s: %s\n",
1726                   ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1727                 return;
1728         }
1729         cprintf("%d Send info...\n", SEND_LISTING);
1730
1731         do {
1732                 client_gets(buf);
1733                 if (strcmp(buf, "000"))
1734                         fprintf(fp, "%s\n", buf);
1735         } while (strcmp(buf, "000"));
1736         fclose(fp);
1737
1738         /* now update the room index so people will see our new info */
1739         lgetroom(&CC->quickroom, CC->quickroom.QRname);         /* lock so no one steps on us */
1740         CC->quickroom.QRinfo = CC->quickroom.QRhighest + 1L;
1741         lputroom(&CC->quickroom);
1742 }
1743
1744
1745 /* 
1746  * cmd_lflr()   -  List all known floors
1747  */
1748 void cmd_lflr(void)
1749 {
1750         int a;
1751         struct floor flbuf;
1752
1753         if (CtdlAccessCheck(ac_logged_in)) return;
1754
1755         cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1756
1757         for (a = 0; a < MAXFLOORS; ++a) {
1758                 getfloor(&flbuf, a);
1759                 if (flbuf.f_flags & F_INUSE) {
1760                         cprintf("%d|%s|%d\n",
1761                                 a,
1762                                 flbuf.f_name,
1763                                 flbuf.f_ref_count);
1764                 }
1765         }
1766         cprintf("000\n");
1767 }
1768
1769
1770
1771 /*
1772  * create a new floor
1773  */
1774 void cmd_cflr(char *argbuf)
1775 {
1776         char new_floor_name[SIZ];
1777         struct floor flbuf;
1778         int cflr_ok;
1779         int free_slot = (-1);
1780         int a;
1781
1782         extract(new_floor_name, argbuf, 0);
1783         cflr_ok = extract_int(argbuf, 1);
1784
1785
1786         if (CtdlAccessCheck(ac_aide)) return;
1787
1788         for (a = 0; a < MAXFLOORS; ++a) {
1789                 getfloor(&flbuf, a);
1790
1791                 /* note any free slots while we're scanning... */
1792                 if (((flbuf.f_flags & F_INUSE) == 0)
1793                     && (free_slot < 0))
1794                         free_slot = a;
1795
1796                 /* check to see if it already exists */
1797                 if ((!strcasecmp(flbuf.f_name, new_floor_name))
1798                     && (flbuf.f_flags & F_INUSE)) {
1799                         cprintf("%d Floor '%s' already exists.\n",
1800                                 ERROR + ALREADY_EXISTS,
1801                                 flbuf.f_name);
1802                         return;
1803                 }
1804         }
1805
1806         if (free_slot < 0) {
1807                 cprintf("%d There is no space available for a new floor.\n",
1808                         ERROR + INVALID_FLOOR_OPERATION);
1809                 return;
1810         }
1811         if (cflr_ok == 0) {
1812                 cprintf("%d ok to create...\n", CIT_OK);
1813                 return;
1814         }
1815         lgetfloor(&flbuf, free_slot);
1816         flbuf.f_flags = F_INUSE;
1817         flbuf.f_ref_count = 0;
1818         safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
1819         lputfloor(&flbuf, free_slot);
1820         cprintf("%d %d\n", CIT_OK, free_slot);
1821 }
1822
1823
1824
1825 /*
1826  * delete a floor
1827  */
1828 void cmd_kflr(char *argbuf)
1829 {
1830         struct floor flbuf;
1831         int floor_to_delete;
1832         int kflr_ok;
1833         int delete_ok;
1834
1835         floor_to_delete = extract_int(argbuf, 0);
1836         kflr_ok = extract_int(argbuf, 1);
1837
1838         if (CtdlAccessCheck(ac_aide)) return;
1839
1840         lgetfloor(&flbuf, floor_to_delete);
1841
1842         delete_ok = 1;
1843         if ((flbuf.f_flags & F_INUSE) == 0) {
1844                 cprintf("%d Floor %d not in use.\n",
1845                         ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
1846                 delete_ok = 0;
1847         } else {
1848                 if (flbuf.f_ref_count != 0) {
1849                         cprintf("%d Cannot delete; floor contains %d rooms.\n",
1850                                 ERROR + INVALID_FLOOR_OPERATION,
1851                                 flbuf.f_ref_count);
1852                         delete_ok = 0;
1853                 } else {
1854                         if (kflr_ok == 1) {
1855                                 cprintf("%d Ok\n", CIT_OK);
1856                         } else {
1857                                 cprintf("%d Ok to delete...\n", CIT_OK);
1858                         }
1859
1860                 }
1861
1862         }
1863
1864         if ((delete_ok == 1) && (kflr_ok == 1))
1865                 flbuf.f_flags = 0;
1866         lputfloor(&flbuf, floor_to_delete);
1867 }
1868
1869 /*
1870  * edit a floor
1871  */
1872 void cmd_eflr(char *argbuf)
1873 {
1874         struct floor flbuf;
1875         int floor_num;
1876         int np;
1877
1878         np = num_parms(argbuf);
1879         if (np < 1) {
1880                 cprintf("%d Usage error.\n", ERROR);
1881                 return;
1882         }
1883
1884         if (CtdlAccessCheck(ac_aide)) return;
1885
1886         floor_num = extract_int(argbuf, 0);
1887         lgetfloor(&flbuf, floor_num);
1888         if ((flbuf.f_flags & F_INUSE) == 0) {
1889                 lputfloor(&flbuf, floor_num);
1890                 cprintf("%d Floor %d is not in use.\n",
1891                         ERROR + INVALID_FLOOR_OPERATION, floor_num);
1892                 return;
1893         }
1894         if (np >= 2)
1895                 extract(flbuf.f_name, argbuf, 1);
1896         lputfloor(&flbuf, floor_num);
1897
1898         cprintf("%d Ok\n", CIT_OK);
1899 }