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