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