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