]> code.citadel.org Git - citadel.git/blob - citadel/room_ops.c
* room_ops.c: modified usergoto() to look at the new data structures
[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;
547         int new_messages = 0;
548         int total_messages = 0;
549         int info = 0;
550         int rmailflag;
551         int raideflag;
552         int newmailcount = 0;
553         struct visit vbuf;
554
555         CC->curr_rm=where;
556         getroom(&CC->quickroom,CC->curr_rm);
557         lgetuser(&CC->usersupp,CC->curr_user);
558         CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
559
560         /* old method - remove when we're ready */
561         CC->usersupp.forget[CC->curr_rm]=(-1);
562         CC->usersupp.generation[CC->curr_rm]=CC->quickroom.QRgen;
563
564         /* new method */
565         vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
566         vbuf.v_flags = vbuf.v_flags | V_ACCESS;
567
568         CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
569         lputuser(&CC->usersupp,CC->curr_user);
570
571         /* check for new mail */
572         newmailcount = NewMailCount();
573
574         /* set info to 1 if the user needs to read the room's info file */
575         if (CC->quickroom.QRinfo > vbuf.v_lastseen) info = 1;
576
577         get_mm();
578         get_msglist(CC->curr_rm);
579         for (a=0; a<CC->num_msgs; ++a) {
580                 if (MessageFromList(a)>0L) {
581                         ++total_messages;
582                         if (MessageFromList(a) > vbuf.v_lastseen) {
583                                 ++new_messages;
584                                 }
585                         }
586                 }
587
588
589         if (CC->curr_rm == 1) rmailflag = 1;
590         else rmailflag = 0;
591
592         if ( (CC->quickroom.QRroomaide == CC->usersupp.usernum)
593            || (CC->usersupp.axlevel>=6) )  raideflag = 1;
594         else raideflag = 0;
595
596         if (display_result) cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d\n",
597                 OK,check_express(),
598                 CC->quickroom.QRname,
599                 new_messages, total_messages,
600                 info,CC->quickroom.QRflags,
601                 CC->quickroom.QRhighest,
602                 vbuf.v_lastseen,
603                 rmailflag,raideflag,newmailcount,CC->quickroom.QRfloor);
604
605         if (CC->quickroom.QRflags & QR_PRIVATE) {
606                 set_wtmpsupp("<private room>");
607                 }
608         else {
609                 set_wtmpsupp(CC->quickroom.QRname);
610                 }
611         }
612
613
614 /* 
615  * cmd_goto()  -  goto a new room
616  */
617 void cmd_goto(char *gargs)
618 {
619         struct quickroom QRscratch;
620         int a,c;
621         int ok;
622         char bbb[20],towhere[32],password[20];
623
624         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
625                 cprintf("%d not logged in\n",ERROR+NOT_LOGGED_IN);
626                 return;
627                 }
628
629         extract(towhere,gargs,0);
630         extract(password,gargs,1);
631
632         c=0;
633         getuser(&CC->usersupp,CC->curr_user);
634
635         /* FIX  This is the primary bit of code that needs to be rewritten
636          * during the cutover.  Search for rooms by name rather than by
637          * position in the file.  A total rewrite may be necessary.
638          */
639         for (a=0; a<MAXROOMS; ++a) {
640                 getroom(&QRscratch,a);
641                 if ((a==0)&&(!strcasecmp(towhere,"_BASEROOM_"))) {
642                         strncpy(towhere,QRscratch.QRname,31);
643                         }
644                 if ((a==1)&&(!strcasecmp(towhere,"_MAIL_"))) {
645                         strncpy(towhere,QRscratch.QRname,31);
646                         }
647                 if ((!strcasecmp(QRscratch.QRname,config.c_twitroom))
648                    &&(!strcasecmp(towhere,"_BITBUCKET_"))) {
649                         strncpy(towhere,QRscratch.QRname,31);
650                         }
651                 strcpy(bbb,QRscratch.QRname);
652                 ok = 0;
653
654                 /* let internal programs go directly to any room */
655                 if (((CC->internal_pgm))&&(!strcasecmp(bbb,towhere))) {
656                         usergoto(a,1);
657                         return;
658                         }
659
660                 /* normal clients have to pass through security */
661                 if ( 
662                         (strcasecmp(bbb,towhere)==0)
663                         &&      ((QRscratch.QRflags&QR_INUSE)!=0)
664
665                         && (    ((QRscratch.QRflags&QR_PREFONLY)==0)
666                         ||      (CC->usersupp.axlevel>=5)
667                         )
668
669                         && (    (a!=2) || (CC->usersupp.axlevel>=6) )
670
671                         && (    ((QRscratch.QRflags&QR_PRIVATE)==0)
672                         || (QRscratch.QRflags&QR_GUESSNAME)
673                         || (CC->usersupp.axlevel>=6)
674                         || (QRscratch.QRflags&QR_PASSWORDED)
675                         ||      (QRscratch.QRgen==CC->usersupp.generation[a])
676                         )
677         
678                         ) ok = 1;
679
680
681                 if (ok==1) {
682
683                         if (  (QRscratch.QRflags&QR_PASSWORDED) &&
684                                 (CC->usersupp.axlevel<6) &&
685                                 (QRscratch.QRgen!=CC->usersupp.generation[a]) &&
686                                 (strcasecmp(QRscratch.QRpasswd,password))
687                                 ) {
688                                         cprintf("%d wrong or missing passwd\n",
689                                                 ERROR+PASSWORD_REQUIRED);
690                                         return;
691                                         }
692
693                         usergoto(a,1);
694                         return;
695                         }
696
697                 }
698         cprintf("%d room '%s' not found\n",ERROR+ROOM_NOT_FOUND,towhere);
699         }
700
701
702 void cmd_whok(void) {
703         struct usersupp temp;
704         struct cdbdata *cdbus;
705
706         if ((!(CC->logged_in))&&(!(CC->internal_pgm))) {
707                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
708                 return;
709                 }
710         getuser(&CC->usersupp,CC->curr_user);
711
712         if ((!is_room_aide()) && (!(CC->internal_pgm)) ) {
713                 cprintf("%d Higher access required.\n",
714                         ERROR+HIGHER_ACCESS_REQUIRED);
715                 return;
716                 }
717
718         cprintf("%d Who knows room:\n",LISTING_FOLLOWS);
719         cdb_rewind(CDB_USERSUPP);
720         while(cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
721                 bzero(&temp, sizeof(struct usersupp));
722                 memcpy(&temp, cdbus->ptr, cdbus->len);
723                 cdb_free(cdbus);
724                 if ((CC->quickroom.QRflags & QR_INUSE)
725                         && ( (CC->curr_rm!=2) || (temp.axlevel>=6) )
726                         && (CC->quickroom.QRgen != (temp.forget[CC->curr_rm]) )
727
728                         && (    ((CC->quickroom.QRflags&QR_PREFONLY)==0)
729                         ||      (temp.axlevel>=5)
730                         )
731
732                         && (    ((CC->quickroom.QRflags&QR_PRIVATE)==0)
733                         ||      (temp.axlevel>=6)
734                         ||      (CC->quickroom.QRgen==(temp.generation[CC->curr_rm]))
735                         )
736
737                         && (strncmp(temp.fullname,"000",3))
738
739                 ) cprintf("%s\n",temp.fullname);
740                 }
741         cprintf("000\n");
742         }
743
744
745 /*
746  * RDIR command for room directory
747  */
748 void cmd_rdir(void) {
749         char buf[256];
750         char flnm[256];
751         char comment[256];
752         FILE *ls,*fd;
753         struct stat statbuf;
754
755         if (!(CC->logged_in)) {
756                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
757                 return;
758                 }
759
760         getroom(&CC->quickroom,CC->curr_rm);
761         getuser(&CC->usersupp,CC->curr_user);
762
763         if ((CC->quickroom.QRflags & QR_DIRECTORY) == 0) {
764                 cprintf("%d not here.\n",ERROR+NOT_HERE);
765                 return;
766                 }
767
768         if (((CC->quickroom.QRflags & QR_VISDIR) == 0)
769            && (CC->usersupp.axlevel<6)
770            && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
771                 cprintf("%d not here.\n",ERROR+HIGHER_ACCESS_REQUIRED);
772                 return;
773                 }
774
775         cprintf("%d %s|%s/files/%s\n",
776                 LISTING_FOLLOWS,config.c_fqdn,BBSDIR,CC->quickroom.QRdirname);
777
778         sprintf(buf,"cd %s/files/%s; ls >%s 2>/dev/null",
779                 BBSDIR,CC->quickroom.QRdirname,CC->temp);
780         system(buf);
781
782         sprintf(buf,"%s/files/%s/filedir",BBSDIR,CC->quickroom.QRdirname);
783         fd = fopen(buf,"r");
784         if (fd==NULL) fd=fopen("/dev/null","r");
785
786         ls = fopen(CC->temp,"r");
787         while (fgets(flnm,256,ls)!=NULL) {
788                 flnm[strlen(flnm)-1]=0;
789                 if (strcasecmp(flnm,"filedir")) {
790                         sprintf(buf,"%s/files/%s/%s",
791                                 BBSDIR,CC->quickroom.QRdirname,flnm);
792                         stat(buf,&statbuf);
793                         strcpy(comment,"");
794                         fseek(fd,0L,0);
795                         while ((fgets(buf,256,fd)!=NULL)
796                             &&(strlen(comment)==0)) {
797                                 buf[strlen(buf)-1] = 0;
798                                 if ((!strncasecmp(buf,flnm,strlen(flnm)))
799                                    && (buf[strlen(flnm)]==' ')) 
800                                         strncpy(comment,
801                                                 &buf[strlen(flnm)+1],255);
802                                 }
803                         cprintf("%s|%ld|%s\n",flnm,statbuf.st_size,comment);
804                         }
805                 }
806         fclose(ls);
807         fclose(fd);
808         unlink(CC->temp);
809
810         cprintf("000\n");
811         }
812
813 /*
814  * get room parameters (aide or room aide command)
815  */
816 void cmd_getr(void) {
817         if ((!(CC->logged_in))&&(!(CC->internal_pgm))) {
818                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
819                 return;
820                 }
821
822         if ( (!is_room_aide()) && (!(CC->internal_pgm)) ) {
823                 cprintf("%d Higher access required.\n",
824                         ERROR+HIGHER_ACCESS_REQUIRED);
825                 return;
826                 }
827
828         if (CC->curr_rm < 3) {
829                 cprintf("%d Can't edit this room.\n",ERROR+NOT_HERE);
830                 return;
831                 }
832
833         getroom(&CC->quickroom,CC->curr_rm);
834         cprintf("%d%c%s|%s|%s|%d|%d\n",
835                 OK,check_express(),
836                 CC->quickroom.QRname,
837                 ((CC->quickroom.QRflags & QR_PASSWORDED) ? CC->quickroom.QRpasswd : ""),
838                 ((CC->quickroom.QRflags & QR_DIRECTORY) ? CC->quickroom.QRdirname : ""),
839                 CC->quickroom.QRflags,
840                 (int)CC->quickroom.QRfloor);
841         }
842
843
844 /*
845  * set room parameters (aide or room aide command)
846  */
847 void cmd_setr(char *args) {
848         char buf[256];
849         struct floor flbuf;
850         int old_floor;
851
852         if (!(CC->logged_in)) {
853                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
854                 return;
855                 }
856
857         if (!is_room_aide()) {
858                 cprintf("%d Higher access required.\n",
859                         ERROR+HIGHER_ACCESS_REQUIRED);
860                 return;
861                 }
862
863         if (CC->curr_rm < 3) {
864                 cprintf("%d Can't edit this room.\n",ERROR+NOT_HERE);
865                 return;
866                 }
867
868         if (num_parms(args)>=6) {
869                 getfloor(&flbuf,extract_int(args,5));
870                 if ((flbuf.f_flags & F_INUSE) == 0) {
871                         cprintf("%d Invalid floor number.\n",
872                                 ERROR+INVALID_FLOOR_OPERATION);
873                         return;
874                         }
875                 }
876
877         lgetroom(&CC->quickroom,CC->curr_rm);
878         extract(buf,args,0); buf[20]=0;
879         strncpy(CC->quickroom.QRname,buf,19);
880         extract(buf,args,1); buf[10]=0;
881         strncpy(CC->quickroom.QRpasswd,buf,9);
882         extract(buf,args,2); buf[15]=0;
883         strncpy(CC->quickroom.QRdirname,buf,19);
884         CC->quickroom.QRflags = ( extract_int(args,3) | QR_INUSE);
885
886         /* Clean up a client boo-boo: if the client set the room to
887          * guess-name or passworded, ensure that the private flag is
888          * also set.
889          */
890         if ((CC->quickroom.QRflags & QR_GUESSNAME)
891            ||(CC->quickroom.QRflags & QR_PASSWORDED))
892                 CC->quickroom.QRflags |= QR_PRIVATE;
893
894         /* Kick everyone out if the client requested it (by changing the
895          * room's generation number)
896          */
897         if (extract_int(args,4)) {
898                 time(&CC->quickroom.QRgen);
899                 }
900
901         old_floor = CC->quickroom.QRfloor;
902         if (num_parms(args)>=6) {
903                 CC->quickroom.QRfloor = extract_int(args,5);
904                 }
905
906         lputroom(&CC->quickroom,CC->curr_rm);
907
908         /* adjust the floor reference counts */
909         lgetfloor(&flbuf,old_floor);
910         --flbuf.f_ref_count;
911         lputfloor(&flbuf,old_floor);
912         lgetfloor(&flbuf,CC->quickroom.QRfloor);
913         ++flbuf.f_ref_count;
914         lputfloor(&flbuf,CC->quickroom.QRfloor);
915
916         /* create a room directory if necessary */
917         if (CC->quickroom.QRflags & QR_DIRECTORY) {
918                 sprintf(buf,
919                         "mkdir ./files/%s </dev/null >/dev/null 2>/dev/null",
920                 CC->quickroom.QRdirname);
921                 system(buf);
922                 }
923
924         sprintf(buf,"%s> edited by %s",CC->quickroom.QRname,CC->curr_user);
925         aide_message(buf);
926         cprintf("%d Ok\n",OK);
927         }
928
929
930
931 /* 
932  * get the name of the room aide for this room
933  */
934 void cmd_geta(void) {
935         struct usersupp usbuf;
936
937         if ((!(CC->logged_in))&&(!(CC->internal_pgm))) {
938                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
939                 return;
940                 }
941
942         if (CC->curr_rm < 0) {
943                 cprintf("%d No current room.\n",ERROR);
944                 return;
945                 }
946
947         if (getuserbynumber(&usbuf,CC->quickroom.QRroomaide)==0) {
948                 cprintf("%d %s\n",OK,usbuf.fullname);
949                 }
950         else {
951                 cprintf("%d \n",OK);
952                 }
953         }
954
955
956 /* 
957  * set the room aide for this room
958  */
959 void cmd_seta(char *new_ra)
960 {
961         struct usersupp usbuf;
962         long newu;
963         char buf[256];
964         int post_notice;
965         
966         if (!(CC->logged_in)) {
967                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
968                 return;
969                 }
970
971         if (!is_room_aide()) {
972                 cprintf("%d Higher access required.\n",
973                         ERROR+HIGHER_ACCESS_REQUIRED);
974                 return;
975                 }
976
977         if (CC->curr_rm < 3) {
978                 cprintf("%d Can't edit this room.\n",ERROR+NOT_HERE);
979                 return;
980                 }
981
982         if (getuser(&usbuf,new_ra)!=0) {
983                 newu = (-1L);
984                 }
985         else {
986                 newu = usbuf.usernum;
987                 }
988
989         lgetroom(&CC->quickroom,CC->curr_rm);
990         post_notice = 0;
991         if (CC->quickroom.QRroomaide != newu) {
992                 post_notice = 1;
993                 }
994         CC->quickroom.QRroomaide = newu;
995         lputroom(&CC->quickroom,CC->curr_rm);
996
997         /*
998          * We have to post the change notice _after_ writing changes to 
999          * the room table, otherwise it would deadlock!
1000          */
1001         if (post_notice == 1) {
1002                 sprintf(buf,"%s is now room aide for %s>",
1003                         usbuf.fullname,CC->quickroom.QRname);
1004                 aide_message(buf);
1005                 }
1006         cprintf("%d Ok\n",OK);
1007         }
1008
1009
1010 /* 
1011  * retrieve info file for this room
1012  */
1013 void cmd_rinf(void) {
1014         char filename[64];
1015         char buf[256];
1016         FILE *info_fp;
1017         
1018         sprintf(filename,"./info/%d",CC->curr_rm);
1019         info_fp = fopen(filename,"r");
1020
1021         if (info_fp==NULL) {
1022                 cprintf("%d No info file.\n",ERROR);
1023                 return;
1024                 }
1025
1026         cprintf("%d Info:\n",LISTING_FOLLOWS);  
1027         while (fgets(buf, 256, info_fp) != NULL) {
1028                 if (strlen(buf) > 0) buf[strlen(buf)-1] = 0;
1029                 cprintf("%s\n", buf);
1030                 }
1031         cprintf("000\n");
1032         fclose(info_fp);
1033         }
1034
1035 /*
1036  * aide command: kill the current room
1037  */
1038 void cmd_kill(char *argbuf)
1039 {
1040         char aaa[100];
1041         int a;
1042         int kill_ok;
1043         struct floor flbuf;
1044         long MsgToDelete;
1045         
1046         kill_ok = extract_int(argbuf,0);
1047
1048         if (!(CC->logged_in)) {
1049                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
1050                 return;
1051                 }
1052
1053         if (!is_room_aide()) {
1054                 cprintf("%d Higher access required.\n",
1055                         ERROR+HIGHER_ACCESS_REQUIRED);
1056                 return;
1057                 }
1058
1059         if (CC->curr_rm < 3) {
1060                 cprintf("%d Can't kill this room.\n",ERROR+NOT_HERE);
1061                 return;
1062                 }
1063
1064         if (kill_ok) {
1065
1066                 /* first flag the room record as not in use */
1067                 lgetroom(&CC->quickroom,CC->curr_rm);
1068                 CC->quickroom.QRflags=0;
1069
1070                 /* then delete the messages in the room */
1071                 get_msglist(CC->curr_rm);
1072                 if (CC->num_msgs > 0) for (a=0; a < CC->num_msgs; ++a) {
1073                         MsgToDelete = MessageFromList(a);
1074                         cdb_delete(CDB_MSGMAIN, &MsgToDelete, sizeof(long));
1075                         }
1076                 put_msglist(CC->curr_rm);
1077                 free(CC->msglist);
1078                 CC->num_msgs = 0;
1079                 cdb_delete(CDB_MSGLISTS, &CC->curr_rm, sizeof(int));
1080
1081                 lputroom(&CC->quickroom,CC->curr_rm);
1082
1083
1084                 /* then decrement the reference count for the floor */
1085                 lgetfloor(&flbuf,(int)CC->quickroom.QRfloor);
1086                 flbuf.f_ref_count = flbuf.f_ref_count - 1;
1087                 lputfloor(&flbuf,(int)CC->quickroom.QRfloor);
1088
1089                 /* tell the world what we did */
1090                 sprintf(aaa,"%s> killed by %s",CC->quickroom.QRname,CC->curr_user);
1091                 aide_message(aaa);
1092                 CC->curr_rm=(-1);
1093                 cprintf("%d '%s' deleted.\n",OK,CC->quickroom.QRname);
1094                 }
1095         else {
1096                 cprintf("%d ok to delete.\n",OK);
1097                 }
1098         }
1099
1100
1101 /*
1102  * Find a free slot to create a new room in, or return -1 for error.
1103  * search_dir is the direction to search in.  1 causes this function to
1104  * return the first available slot, -1 gets the last available slot.
1105  *
1106  * FIX   This function can be eliminated during the cutover.
1107  */
1108 int get_free_room_slot(int search_dir)
1109 {
1110         int a,st;
1111         struct quickroom qrbuf;
1112
1113         st = ((search_dir>0) ? 3 : (MAXROOMS-1));
1114
1115         for (a=st; ((a<MAXROOMS)&&(a>=3)); a=a+search_dir) {
1116                 getroom(&qrbuf,a);
1117                 if ((qrbuf.QRflags & QR_INUSE)==0) return(a);
1118                 }
1119         return(-1);
1120         }
1121
1122
1123 /*
1124  * internal code to create a new room (returns room flags)
1125  */
1126 unsigned create_room(int free_slot, char *new_room_name, int new_room_type, char *new_room_pass, int new_room_floor)
1127 {
1128         struct quickroom qrbuf;
1129         struct floor flbuf;
1130         struct visit vbuf;
1131
1132         lgetroom(&qrbuf,free_slot);
1133         strncpy(qrbuf.QRname,new_room_name,19);
1134         strncpy(qrbuf.QRpasswd,new_room_pass,9);
1135         qrbuf.QRflags = QR_INUSE;
1136         if (new_room_type > 0) qrbuf.QRflags=(qrbuf.QRflags|QR_PRIVATE);
1137         if (new_room_type == 1) qrbuf.QRflags=(qrbuf.QRflags|QR_GUESSNAME);
1138         if (new_room_type == 2) qrbuf.QRflags=(qrbuf.QRflags|QR_PASSWORDED);
1139         qrbuf.QRroomaide = (-1L);
1140         if ((new_room_type > 0)&&(CREATAIDE==1))
1141                 qrbuf.QRroomaide=CC->usersupp.usernum;
1142         qrbuf.QRhighest = 0L;
1143         time(&qrbuf.QRgen);
1144         qrbuf.QRfloor = new_room_floor;
1145
1146         /* save what we just did... */
1147         lputroom(&qrbuf,free_slot);
1148
1149         /* bump the reference count on whatever floor the room is on */
1150         lgetfloor(&flbuf,(int)qrbuf.QRfloor);
1151         flbuf.f_ref_count = flbuf.f_ref_count + 1;
1152         lputfloor(&flbuf,(int)qrbuf.QRfloor);
1153
1154         /* be sure not to kick the creator out of the room! */
1155         lgetuser(&CC->usersupp,CC->curr_user);
1156         CtdlGetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1157         /* (old method) */
1158         CC->usersupp.generation[free_slot] = qrbuf.QRgen;
1159         CC->usersupp.forget[free_slot] = (-1);
1160         /* (new method) */
1161         vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1162         vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1163         CtdlSetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1164         lputuser(&CC->usersupp,CC->curr_user);
1165
1166         /* resume our happy day */
1167         return(qrbuf.QRflags);
1168         }
1169
1170
1171 /*
1172  * create a new room        FIX  Rework this to use name indexing
1173  */
1174 void cmd_cre8(char *args)
1175 {
1176         int cre8_ok;
1177         int free_slot;
1178         int a;
1179         char new_room_name[256];
1180         int new_room_type;
1181         char new_room_pass[256];
1182         int new_room_floor;
1183         char aaa[256];
1184         unsigned newflags;
1185         struct quickroom qrbuf;
1186         struct floor flbuf;
1187
1188         cre8_ok = extract_int(args,0);
1189         extract(new_room_name,args,1);
1190         new_room_name[19] = 0;
1191         new_room_type = extract_int(args,2);
1192         extract(new_room_pass,args,3);
1193         new_room_pass[9] = 0;
1194         new_room_floor = 0;
1195
1196         if ((strlen(new_room_name)==0) && (cre8_ok==1)) {
1197                 cprintf("%d Invalid room name.\n",ERROR);
1198                 return;
1199                 }
1200
1201         if (num_parms(args)>=5) {
1202                 getfloor(&flbuf,extract_int(args,4));
1203                 if ((flbuf.f_flags & F_INUSE) == 0) {
1204                         cprintf("%d Invalid floor number.\n",
1205                                 ERROR+INVALID_FLOOR_OPERATION);
1206                         return;
1207                         }
1208                 else {
1209                         new_room_floor = extract_int(args,4);
1210                         }
1211                 }
1212
1213         if (!(CC->logged_in)) {
1214                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
1215                 return;
1216                 }
1217
1218         if (CC->usersupp.axlevel<3) {
1219                 cprintf("%d You need higher access to create rooms.\n",
1220                         ERROR+HIGHER_ACCESS_REQUIRED);
1221                 return;
1222                 }
1223
1224         free_slot = get_free_room_slot(1);
1225         if (free_slot<0) {
1226                 cprintf("%d There is no space available for a new room.\n",
1227                         ERROR);
1228                 return;
1229                 }
1230
1231         if (cre8_ok==0) {
1232                 cprintf("%d ok to create...\n",OK);
1233                 return;
1234                 }
1235
1236         for (a=0; a<MAXROOMS; ++a) {
1237                 getroom(&qrbuf,a);
1238                 if ( (!strcasecmp(qrbuf.QRname,new_room_name))
1239                    && (qrbuf.QRflags & QR_INUSE) ) {
1240                         cprintf("%d '%s' already exists.\n",
1241                                 ERROR,qrbuf.QRname);
1242                         return;
1243                         }
1244                 }
1245
1246         if ((new_room_type < 0) || (new_room_type > 3)) {
1247                 cprintf("%d Invalid room type.\n",ERROR);
1248                 return;
1249                 }
1250
1251         newflags = create_room(free_slot,new_room_name,
1252                         new_room_type,new_room_pass,new_room_floor);
1253
1254         /* post a message in Aide> describing the new room */
1255         strncpy(aaa,new_room_name,255);
1256         strcat(aaa,"> created by ");
1257         strcat(aaa,CC->usersupp.fullname);
1258         if (newflags&QR_PRIVATE) strcat(aaa," [private]");
1259         if (newflags&QR_GUESSNAME) strcat(aaa,"[guessname] ");
1260         if (newflags&QR_PASSWORDED) {
1261                 strcat(aaa,"\n Password: ");
1262                 strcat(aaa,new_room_pass);
1263                 }
1264         aide_message(aaa); 
1265
1266         sprintf(aaa,"./info/%d",free_slot);     /* delete old info file */
1267         unlink(aaa);    
1268         sprintf(aaa,"./images/room.%d.gif",free_slot);  /* and picture */
1269         unlink(aaa);    
1270
1271         cprintf("%d '%s' has been created.\n",OK,qrbuf.QRname);
1272         }
1273
1274
1275
1276 void cmd_einf(char *ok)
1277 {       /* enter info file for current room */
1278         FILE *fp;
1279         char infofilename[32];
1280         char buf[256];
1281
1282         if (!(CC->logged_in)) {
1283                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
1284                 return;
1285                 }
1286
1287         if (!is_room_aide()) {
1288                 cprintf("%d Higher access required.\n",
1289                         ERROR+HIGHER_ACCESS_REQUIRED);
1290                 return;
1291                 }
1292
1293         if (atoi(ok)==0) {
1294                 cprintf("%d Ok.\n",OK);
1295                 return;
1296                 }
1297
1298         cprintf("%d Send info...\n",SEND_LISTING);
1299
1300         sprintf(infofilename,"./info/%d",CC->curr_rm);
1301
1302         fp=fopen(infofilename,"w");
1303         do {
1304                 client_gets(buf);
1305                 if (strcmp(buf,"000")) fprintf(fp,"%s\n",buf);
1306                 } while(strcmp(buf,"000"));
1307         fclose(fp);
1308
1309         /* now update the room index so people will see our new info */
1310         lgetroom(&CC->quickroom,CC->curr_rm);   /* lock so no one steps on us */
1311         CC->quickroom.QRinfo = CC->quickroom.QRhighest + 1L;
1312         lputroom(&CC->quickroom,CC->curr_rm);
1313         }
1314
1315
1316 /* 
1317  * cmd_lflr()   -  List all known floors
1318  */
1319 void cmd_lflr(void) {
1320         int a;
1321         struct floor flbuf;
1322
1323         if (!(CC->logged_in)) {
1324                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
1325                 return;
1326                 }
1327
1328         /* if (getuser(&CC->usersupp,CC->curr_user)) {
1329                 cprintf("%d Can't locate user!\n",ERROR+INTERNAL_ERROR);
1330                 return;
1331                 }
1332         */
1333
1334         cprintf("%d Known floors:\n",LISTING_FOLLOWS);
1335         
1336         for (a=0; a<MAXFLOORS; ++a) {
1337                 getfloor(&flbuf,a);
1338                 if (flbuf.f_flags & F_INUSE) {
1339                         cprintf("%d|%s|%d\n",
1340                                 a,
1341                                 flbuf.f_name,
1342                                 flbuf.f_ref_count);
1343                         }
1344                 }
1345         cprintf("000\n");
1346         }
1347
1348
1349
1350 /*
1351  * create a new floor
1352  */
1353 void cmd_cflr(char *argbuf)
1354 {
1355         char new_floor_name[256];
1356         struct floor flbuf;
1357         int cflr_ok;
1358         int free_slot = (-1);
1359         int a;
1360
1361         extract(new_floor_name,argbuf,0);
1362         cflr_ok = extract_int(argbuf,1);
1363
1364         
1365         if (!(CC->logged_in)) {
1366                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
1367                 return;
1368                 }
1369
1370         if (CC->usersupp.axlevel<6) {
1371                 cprintf("%d You need higher access to create rooms.\n",
1372                         ERROR+HIGHER_ACCESS_REQUIRED);
1373                 return;
1374                 }
1375
1376         for (a=0; a<MAXFLOORS; ++a) {
1377                 getfloor(&flbuf,a);
1378
1379                 /* note any free slots while we're scanning... */
1380                 if ( ((flbuf.f_flags & F_INUSE)==0) 
1381                      && (free_slot < 0) )  free_slot = a;
1382
1383                 /* check to see if it already exists */
1384                 if ( (!strcasecmp(flbuf.f_name,new_floor_name))
1385                      && (flbuf.f_flags & F_INUSE) ) {
1386                         cprintf("%d Floor '%s' already exists.\n",
1387                                 ERROR+ALREADY_EXISTS,
1388                                 flbuf.f_name);
1389                         return;
1390                         }
1391
1392                 }
1393
1394         if (free_slot<0) {
1395                 cprintf("%d There is no space available for a new floor.\n",
1396                         ERROR+INVALID_FLOOR_OPERATION);
1397                 return;
1398                 }
1399
1400         if (cflr_ok==0) {
1401                 cprintf("%d ok to create...\n",OK);
1402                 return;
1403                 }
1404
1405         lgetfloor(&flbuf,free_slot);
1406         flbuf.f_flags = F_INUSE;
1407         flbuf.f_ref_count = 0;
1408         strncpy(flbuf.f_name,new_floor_name,255);
1409         lputfloor(&flbuf,free_slot);
1410         cprintf("%d %d\n",OK,free_slot);
1411         }
1412
1413
1414
1415 /*
1416  * delete a floor
1417  */
1418 void cmd_kflr(char *argbuf)
1419 {
1420         struct floor flbuf;
1421         int floor_to_delete;
1422         int kflr_ok;
1423         int delete_ok;
1424
1425         floor_to_delete = extract_int(argbuf,0);
1426         kflr_ok = extract_int(argbuf,1);
1427
1428         
1429         if (!(CC->logged_in)) {
1430                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
1431                 return;
1432                 }
1433
1434         if (CC->usersupp.axlevel<6) {
1435                 cprintf("%d You need higher access to delete floors.\n",
1436                         ERROR+HIGHER_ACCESS_REQUIRED);
1437                 return;
1438                 }
1439
1440         lgetfloor(&flbuf,floor_to_delete);
1441
1442         delete_ok = 1;  
1443         if ((flbuf.f_flags & F_INUSE) == 0) {
1444                 cprintf("%d Floor %d not in use.\n",
1445                         ERROR+INVALID_FLOOR_OPERATION,floor_to_delete);
1446                 delete_ok = 0;
1447                 }
1448
1449         else {
1450                 if (flbuf.f_ref_count != 0) {
1451                         cprintf("%d Cannot delete; floor contains %d rooms.\n",
1452                                 ERROR+INVALID_FLOOR_OPERATION,
1453                                 flbuf.f_ref_count);
1454                         delete_ok = 0;
1455                         }
1456
1457                 else {
1458                         if (kflr_ok == 1) {
1459                                 cprintf("%d Ok\n",OK);
1460                                 }
1461                         else {
1462                                 cprintf("%d Ok to delete...\n",OK);
1463                                 }
1464
1465                         }
1466
1467                 }
1468
1469         if ( (delete_ok == 1) && (kflr_ok == 1) ) flbuf.f_flags = 0;
1470         lputfloor(&flbuf,floor_to_delete);
1471         }
1472
1473 /*
1474  * edit a floor
1475  */
1476 void cmd_eflr(char *argbuf)
1477 {
1478         struct floor flbuf;
1479         int floor_num;
1480         int np;
1481
1482         np = num_parms(argbuf);
1483         if (np < 1) {
1484                 cprintf("%d Usage error.\n",ERROR);
1485                 return;
1486                 }
1487         
1488         if (!(CC->logged_in)) {
1489                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
1490                 return;
1491                 }
1492
1493         if (CC->usersupp.axlevel<6) {
1494                 cprintf("%d You need higher access to edit floors.\n",
1495                         ERROR+HIGHER_ACCESS_REQUIRED);
1496                 return;
1497                 }
1498
1499         floor_num = extract_int(argbuf,0);
1500         lgetfloor(&flbuf,floor_num);
1501         if ( (flbuf.f_flags & F_INUSE) == 0) {
1502                 lputfloor(&flbuf,floor_num);
1503                 cprintf("%d Floor %d is not in use.\n",
1504                         ERROR+INVALID_FLOOR_OPERATION,floor_num);
1505                 return;
1506                 }
1507         if (np >= 2) extract(flbuf.f_name,argbuf,1);
1508         lputfloor(&flbuf,floor_num);
1509         
1510         cprintf("%d Ok\n",OK);
1511         }