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