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