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