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