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