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