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