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