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