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