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