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