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