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