calculate the directories in a central manner.
[citadel.git] / citadel / room_ops.c
1 /* 
2  * $Id$
3  * 
4  * Server functions which perform operations on room objects.
5  *
6  */
7
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <sys/stat.h>
13 #include <ctype.h>
14 #include <string.h>
15
16 #if TIME_WITH_SYS_TIME
17 # include <sys/time.h>
18 # include <time.h>
19 #else
20 # if HAVE_SYS_TIME_H
21 #  include <sys/time.h>
22 # else
23 #  include <time.h>
24 # endif
25 #endif
26
27 #include <limits.h>
28 #include <errno.h>
29 #include "citadel.h"
30 #include "server.h"
31 #include "serv_extensions.h"
32 #include "database.h"
33 #include "config.h"
34 #include "room_ops.h"
35 #include "sysdep_decls.h"
36 #include "support.h"
37 #include "user_ops.h"
38 #include "msgbase.h"
39 #include "citserver.h"
40 #include "control.h"
41 #include "tools.h"
42
43 struct floor *floorcache[MAXFLOORS];
44
45 /*
46  * Generic routine for determining user access to rooms
47  */
48 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf,
49                 int *result, int *view)
50 {
51         int retval = 0;
52         struct visit vbuf;
53
54         /* for internal programs, always do everything */
55         if (((CC->internal_pgm)) && (roombuf->QRflags & QR_INUSE)) {
56                 retval = (UA_KNOWN | UA_GOTOALLOWED);
57                 vbuf.v_view = 0;
58                 goto SKIP_EVERYTHING;
59         }
60
61         /* Locate any applicable user/room relationships */
62         CtdlGetRelationship(&vbuf, userbuf, roombuf);
63
64         /* Force the properties of the Aide room */
65         if (!strcasecmp(roombuf->QRname, config.c_aideroom)) {
66                 if (userbuf->axlevel >= 6) {
67                         retval = UA_KNOWN | UA_GOTOALLOWED;
68                 } else {
69                         retval = 0;
70                 }
71                 goto NEWMSG;
72         }
73
74         /* If this is a public room, it's accessible... */
75         if ( ((roombuf->QRflags & QR_PRIVATE) == 0) 
76            && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
77                 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
78         }
79
80         /* If this is a preferred users only room, check access level */
81         if (roombuf->QRflags & QR_PREFONLY) {
82                 if (userbuf->axlevel < 5) {
83                         retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
84                 }
85         }
86
87         /* For private rooms, check the generation number matchups */
88         if ( (roombuf->QRflags & QR_PRIVATE) 
89            && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
90
91                 /* An explicit match means the user belongs in this room */
92                 if (vbuf.v_flags & V_ACCESS) {
93                         retval = retval | UA_KNOWN | UA_GOTOALLOWED;
94                 }
95                 /* Otherwise, check if this is a guess-name or passworded
96                  * room.  If it is, a goto may at least be attempted
97                  */
98                 else if ((roombuf->QRflags & QR_PRIVATE)
99                          || (roombuf->QRflags & QR_PASSWORDED)) {
100                         retval = retval & ~UA_KNOWN;
101                         retval = retval | UA_GOTOALLOWED;
102                 }
103         }
104
105         /* For mailbox rooms, also check the generation number matchups */
106         if (roombuf->QRflags & QR_MAILBOX) {
107                 if (userbuf->usernum == atol(roombuf->QRname)) {
108                         retval = retval | UA_KNOWN | UA_GOTOALLOWED;
109                 }
110                 /* An explicit match means the user belongs in this room */
111                 if (vbuf.v_flags & V_ACCESS) {
112                         retval = retval | UA_KNOWN | UA_GOTOALLOWED;
113                 }
114         }
115
116         /* Check to see if the user has forgotten this room */
117         if (vbuf.v_flags & V_FORGET) {
118                 retval = retval & ~UA_KNOWN;
119                 if ( ( ((roombuf->QRflags & QR_PRIVATE) == 0) 
120                       && ((roombuf->QRflags & QR_MAILBOX) == 0) )
121                    || ( (roombuf->QRflags & QR_MAILBOX) 
122                       && (atol(roombuf->QRname) == CC->user.usernum))) {
123                         retval = retval | UA_ZAPPED;
124                 }
125         }
126         /* If user is explicitly locked out of this room, deny everything */
127         if (vbuf.v_flags & V_LOCKOUT) {
128                 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
129         }
130
131         /* Aides get access to all private rooms */
132         if ( (userbuf->axlevel >= 6)
133            && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
134                 if (vbuf.v_flags & V_FORGET) {
135                         retval = retval | UA_GOTOALLOWED;
136                 }
137                 else {
138                         retval = retval | UA_KNOWN | UA_GOTOALLOWED;
139                 }
140         }
141
142         /* Aides can gain access to mailboxes as well, but they don't show
143          * by default.
144          */
145         if ( (userbuf->axlevel >= 6)
146            && (roombuf->QRflags & QR_MAILBOX) ) {
147                 retval = retval | UA_GOTOALLOWED;
148         }
149
150 NEWMSG: /* By the way, we also check for the presence of new messages */
151         if (is_msg_in_sequence_set(vbuf.v_seen, roombuf->QRhighest) == 0) {
152                 retval = retval | UA_HASNEWMSGS;
153         }
154
155         /* System rooms never show up in the list. */
156         if (roombuf->QRflags2 & QR2_SYSTEM) {
157                 retval = retval & ~UA_KNOWN;
158         }
159
160 SKIP_EVERYTHING:
161         /* Now give the caller the information it wants. */
162         if (result != NULL) *result = retval;
163         if (view != NULL) *view = vbuf.v_view;
164 }
165
166 /*
167  * Self-checking stuff for a room record read into memory
168  */
169 void room_sanity_check(struct ctdlroom *qrbuf)
170 {
171         /* Mailbox rooms are always on the lowest floor */
172         if (qrbuf->QRflags & QR_MAILBOX) {
173                 qrbuf->QRfloor = 0;
174         }
175         /* Listing order of 0 is illegal except for base rooms */
176         if (qrbuf->QRorder == 0)
177                 if (!(qrbuf->QRflags & QR_MAILBOX) &&
178                     strncasecmp(qrbuf->QRname, config.c_baseroom, ROOMNAMELEN)
179                     &&
180                     strncasecmp(qrbuf->QRname, config.c_aideroom, ROOMNAMELEN))
181                         qrbuf->QRorder = 64;
182 }
183
184
185 /*
186  * getroom()  -  retrieve room data from disk
187  */
188 int getroom(struct ctdlroom *qrbuf, char *room_name)
189 {
190         struct cdbdata *cdbqr;
191         char lowercase_name[ROOMNAMELEN];
192         char personal_lowercase_name[ROOMNAMELEN];
193         int a;
194
195         for (a = 0; room_name[a] && a < sizeof lowercase_name - 1; ++a) {
196                 lowercase_name[a] = tolower(room_name[a]);
197         }
198         lowercase_name[a] = 0;
199
200         memset(qrbuf, 0, sizeof(struct ctdlroom));
201
202         /* First, try the public namespace */
203         cdbqr = cdb_fetch(CDB_ROOMS,
204                           lowercase_name, strlen(lowercase_name));
205
206         /* If that didn't work, try the user's personal namespace */
207         if (cdbqr == NULL) {
208                 snprintf(personal_lowercase_name,
209                          sizeof personal_lowercase_name, "%010ld.%s",
210                          CC->user.usernum, lowercase_name);
211                 cdbqr = cdb_fetch(CDB_ROOMS,
212                                   personal_lowercase_name,
213                                   strlen(personal_lowercase_name));
214         }
215         if (cdbqr != NULL) {
216                 memcpy(qrbuf, cdbqr->ptr,
217                        ((cdbqr->len > sizeof(struct ctdlroom)) ?
218                         sizeof(struct ctdlroom) : cdbqr->len));
219                 cdb_free(cdbqr);
220
221                 room_sanity_check(qrbuf);
222
223                 return (0);
224         } else {
225                 return (1);
226         }
227 }
228
229 /*
230  * lgetroom()  -  same as getroom() but locks the record (if supported)
231  */
232 int lgetroom(struct ctdlroom *qrbuf, char *room_name)
233 {
234         register int retval;
235         retval = getroom(qrbuf, room_name);
236         if (retval == 0) begin_critical_section(S_ROOMS);
237         return(retval);
238 }
239
240
241 /*
242  * b_putroom()  -  back end to putroom() and b_deleteroom()
243  *              (if the supplied buffer is NULL, delete the room record)
244  */
245 void b_putroom(struct ctdlroom *qrbuf, char *room_name)
246 {
247         char lowercase_name[ROOMNAMELEN];
248         int a;
249
250         for (a = 0; a <= strlen(room_name); ++a) {
251                 lowercase_name[a] = tolower(room_name[a]);
252         }
253
254         if (qrbuf == NULL) {
255                 cdb_delete(CDB_ROOMS,
256                            lowercase_name, strlen(lowercase_name));
257         } else {
258                 time(&qrbuf->QRmtime);
259                 cdb_store(CDB_ROOMS,
260                           lowercase_name, strlen(lowercase_name),
261                           qrbuf, sizeof(struct ctdlroom));
262         }
263 }
264
265
266 /* 
267  * putroom()  -  store room data to disk
268  */
269 void putroom(struct ctdlroom *qrbuf) {
270         b_putroom(qrbuf, qrbuf->QRname);
271 }
272
273
274 /*
275  * b_deleteroom()  -  delete a room record from disk
276  */
277 void b_deleteroom(char *room_name) {
278         b_putroom(NULL, room_name);
279 }
280
281
282
283 /*
284  * lputroom()  -  same as putroom() but unlocks the record (if supported)
285  */
286 void lputroom(struct ctdlroom *qrbuf)
287 {
288
289         putroom(qrbuf);
290         end_critical_section(S_ROOMS);
291
292 }
293
294 /****************************************************************************/
295
296 /*
297  * getfloor()  -  retrieve floor data from disk
298  */
299 void getfloor(struct floor *flbuf, int floor_num)
300 {
301         struct cdbdata *cdbfl;
302
303         memset(flbuf, 0, sizeof(struct floor));
304         cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
305         if (cdbfl != NULL) {
306                 memcpy(flbuf, cdbfl->ptr,
307                        ((cdbfl->len > sizeof(struct floor)) ?
308                         sizeof(struct floor) : cdbfl->len));
309                 cdb_free(cdbfl);
310         } else {
311                 if (floor_num == 0) {
312                         safestrncpy(flbuf->f_name, "Main Floor", 
313                                 sizeof flbuf->f_name);
314                         flbuf->f_flags = F_INUSE;
315                         flbuf->f_ref_count = 3;
316                 }
317         }
318
319 }
320
321 /*
322  * lgetfloor()  -  same as getfloor() but locks the record (if supported)
323  */
324 void lgetfloor(struct floor *flbuf, int floor_num)
325 {
326
327         begin_critical_section(S_FLOORTAB);
328         getfloor(flbuf, floor_num);
329 }
330
331
332 /*
333  * cgetfloor()  -  Get floor record from *cache* (loads from disk if needed)
334  *    
335  * This is strictly a performance hack.
336  */
337 struct floor *cgetfloor(int floor_num) {
338         static int initialized = 0;
339         int i;
340         int fetch_new = 0;
341         struct floor *fl = NULL;
342
343         begin_critical_section(S_FLOORCACHE);
344         if (initialized == 0) {
345                 for (i=0; i<MAXFLOORS; ++i) {
346                         floorcache[floor_num] = NULL;
347                 }
348         initialized = 1;
349         }
350         if (floorcache[floor_num] == NULL) {
351                 fetch_new = 1;
352         }
353         end_critical_section(S_FLOORCACHE);
354
355         if (fetch_new) {
356                 fl = malloc(sizeof(struct floor));
357                 getfloor(fl, floor_num);
358                 begin_critical_section(S_FLOORCACHE);
359                 if (floorcache[floor_num] != NULL) {
360                         free(floorcache[floor_num]);
361                 }
362                 floorcache[floor_num] = fl;
363                 end_critical_section(S_FLOORCACHE);
364         }
365
366         return(floorcache[floor_num]);
367 }
368
369
370
371 /*
372  * putfloor()  -  store floor data on disk
373  */
374 void putfloor(struct floor *flbuf, int floor_num)
375 {
376         /* If we've cached this, clear it out, 'cuz it's WRONG now! */
377         begin_critical_section(S_FLOORCACHE);
378         if (floorcache[floor_num] != NULL) {
379                 free(floorcache[floor_num]);
380                 floorcache[floor_num] = malloc(sizeof(struct floor));
381                 memcpy(floorcache[floor_num], flbuf, sizeof(struct floor));
382         }
383         end_critical_section(S_FLOORCACHE);
384
385         cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
386                   flbuf, sizeof(struct floor));
387 }
388
389
390 /*
391  * lputfloor()  -  same as putfloor() but unlocks the record (if supported)
392  */
393 void lputfloor(struct floor *flbuf, int floor_num)
394 {
395
396         putfloor(flbuf, floor_num);
397         end_critical_section(S_FLOORTAB);
398
399 }
400
401
402 /* 
403  *  Traverse the room file...
404  */
405 void ForEachRoom(void (*CallBack) (struct ctdlroom *EachRoom, void *out_data),
406                 void *in_data)
407 {
408         struct ctdlroom qrbuf;
409         struct cdbdata *cdbqr;
410
411         cdb_rewind(CDB_ROOMS);
412
413         while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
414                 memset(&qrbuf, 0, sizeof(struct ctdlroom));
415                 memcpy(&qrbuf, cdbqr->ptr,
416                        ((cdbqr->len > sizeof(struct ctdlroom)) ?
417                         sizeof(struct ctdlroom) : cdbqr->len));
418                 cdb_free(cdbqr);
419                 room_sanity_check(&qrbuf);
420                 if (qrbuf.QRflags & QR_INUSE)
421                         (*CallBack)(&qrbuf, in_data);
422         }
423 }
424
425
426 /*
427  * delete_msglist()  -  delete room message pointers
428  */
429 void delete_msglist(struct ctdlroom *whichroom)
430 {
431         struct cdbdata *cdbml;
432
433         /* Make sure the msglist we're deleting actually exists, otherwise
434          * gdbm will complain when we try to delete an invalid record
435          */
436         cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
437         if (cdbml != NULL) {
438                 cdb_free(cdbml);
439
440                 /* Go ahead and delete it */
441                 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
442         }
443 }
444
445
446
447
448 /*
449  * sort message pointers
450  * (returns new msg count)
451  */
452 int sort_msglist(long listptrs[], int oldcount)
453 {
454         int a, b;
455         long hold1, hold2;
456         int numitems;
457
458         numitems = oldcount;
459         if (numitems < 2)
460                 return (oldcount);
461
462         /* do the sort */
463         for (a = numitems - 2; a >= 0; --a) {
464                 for (b = 0; b <= a; ++b) {
465                         if (listptrs[b] > (listptrs[b + 1])) {
466                                 hold1 = listptrs[b];
467                                 hold2 = listptrs[b + 1];
468                                 listptrs[b] = hold2;
469                                 listptrs[b + 1] = hold1;
470                         }
471                 }
472         }
473
474         /* and yank any nulls */
475         while ((numitems > 0) && (listptrs[0] == 0L)) {
476                 memcpy(&listptrs[0], &listptrs[1],
477                        (sizeof(long) * (numitems - 1)));
478                 --numitems;
479         }
480
481         return (numitems);
482 }
483
484
485 /*
486  * Determine whether a given room is non-editable.
487  */
488 int is_noneditable(struct ctdlroom *qrbuf)
489 {
490
491         /* Mail> rooms are non-editable */
492         if ( (qrbuf->QRflags & QR_MAILBOX)
493              && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
494                 return (1);
495
496         /* Everything else is editable */
497         return (0);
498 }
499
500
501
502 /*
503  * Back-back-end for all room listing commands
504  */
505 void list_roomname(struct ctdlroom *qrbuf, int ra, int view)
506 {
507         char truncated_roomname[ROOMNAMELEN];
508
509         /* For my own mailbox rooms, chop off the owner prefix */
510         if ( (qrbuf->QRflags & QR_MAILBOX)
511              && (atol(qrbuf->QRname) == CC->user.usernum) ) {
512                 safestrncpy(truncated_roomname, qrbuf->QRname, sizeof truncated_roomname);
513                 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
514                 cprintf("%s", truncated_roomname);
515         }
516         /* For all other rooms, just display the name in its entirety */
517         else {
518                 cprintf("%s", qrbuf->QRname);
519         }
520
521         /* ...and now the other parameters */
522         cprintf("|%u|%d|%d|%d|%d|%d|\n",
523                 qrbuf->QRflags,
524                 (int) qrbuf->QRfloor,
525                 (int) qrbuf->QRorder,
526                 (int) qrbuf->QRflags2,
527                 ra,
528                 view
529         );
530 }
531
532
533 /* 
534  * cmd_lrms()   -  List all accessible rooms, known or forgotten
535  */
536 void cmd_lrms_backend(struct ctdlroom *qrbuf, void *data)
537 {
538         int FloorBeingSearched = (-1);
539         int ra;
540         int view;
541
542         FloorBeingSearched = *(int *)data;
543         CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
544
545         if ((( ra & (UA_KNOWN | UA_ZAPPED)))
546             && ((qrbuf->QRfloor == (FloorBeingSearched))
547                 || ((FloorBeingSearched) < 0)))
548                 list_roomname(qrbuf, ra, view);
549 }
550
551 void cmd_lrms(char *argbuf)
552 {
553         int FloorBeingSearched = (-1);
554         if (strlen(argbuf) > 0)
555                 FloorBeingSearched = extract_int(argbuf, 0);
556
557         if (CtdlAccessCheck(ac_logged_in)) return;
558
559         if (getuser(&CC->user, CC->curr_user)) {
560                 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
561                 return;
562         }
563         cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
564
565         ForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
566         cprintf("000\n");
567 }
568
569
570
571 /* 
572  * cmd_lkra()   -  List all known rooms
573  */
574 void cmd_lkra_backend(struct ctdlroom *qrbuf, void *data)
575 {
576         int FloorBeingSearched = (-1);
577         int ra;
578         int view;
579
580         FloorBeingSearched = *(int *)data;
581         CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
582
583         if ((( ra & (UA_KNOWN)))
584             && ((qrbuf->QRfloor == (FloorBeingSearched))
585                 || ((FloorBeingSearched) < 0)))
586                 list_roomname(qrbuf, ra, view);
587 }
588
589 void cmd_lkra(char *argbuf)
590 {
591         int FloorBeingSearched = (-1);
592         if (strlen(argbuf) > 0)
593                 FloorBeingSearched = extract_int(argbuf, 0);
594
595         if (CtdlAccessCheck(ac_logged_in)) return;
596         
597         if (getuser(&CC->user, CC->curr_user)) {
598                 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
599                 return;
600         }
601         cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
602
603         ForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
604         cprintf("000\n");
605 }
606
607
608
609 void cmd_lprm_backend(struct ctdlroom *qrbuf, void *data)
610 {
611         int FloorBeingSearched = (-1);
612         int ra;
613         int view;
614
615         FloorBeingSearched = *(int *)data;
616         CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
617
618         if (   ((qrbuf->QRflags & QR_PRIVATE) == 0)
619                 && ((qrbuf->QRflags & QR_MAILBOX) == 0)
620             && ((qrbuf->QRfloor == (FloorBeingSearched))
621                 || ((FloorBeingSearched) < 0)))
622                 list_roomname(qrbuf, ra, view);
623 }
624
625 void cmd_lprm(char *argbuf)
626 {
627         int FloorBeingSearched = (-1);
628         if (strlen(argbuf) > 0)
629                 FloorBeingSearched = extract_int(argbuf, 0);
630
631         cprintf("%d Publiic rooms:\n", LISTING_FOLLOWS);
632
633         ForEachRoom(cmd_lprm_backend, &FloorBeingSearched);
634         cprintf("000\n");
635 }
636
637
638
639 /* 
640  * cmd_lkrn()   -  List all known rooms with new messages
641  */
642 void cmd_lkrn_backend(struct ctdlroom *qrbuf, void *data)
643 {
644         int FloorBeingSearched = (-1);
645         int ra;
646         int view;
647
648         FloorBeingSearched = *(int *)data;
649         CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
650
651         if ((ra & UA_KNOWN)
652             && (ra & UA_HASNEWMSGS)
653             && ((qrbuf->QRfloor == (FloorBeingSearched))
654                 || ((FloorBeingSearched) < 0)))
655                 list_roomname(qrbuf, ra, view);
656 }
657
658 void cmd_lkrn(char *argbuf)
659 {
660         int FloorBeingSearched = (-1);
661         if (strlen(argbuf) > 0)
662                 FloorBeingSearched = extract_int(argbuf, 0);
663
664         if (CtdlAccessCheck(ac_logged_in)) return;
665         
666         if (getuser(&CC->user, CC->curr_user)) {
667                 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
668                 return;
669         }
670         cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
671
672         ForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
673         cprintf("000\n");
674 }
675
676
677
678 /* 
679  * cmd_lkro()   -  List all known rooms
680  */
681 void cmd_lkro_backend(struct ctdlroom *qrbuf, void *data)
682 {
683         int FloorBeingSearched = (-1);
684         int ra;
685         int view;
686
687         FloorBeingSearched = *(int *)data;
688         CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
689
690         if ((ra & UA_KNOWN)
691             && ((ra & UA_HASNEWMSGS) == 0)
692             && ((qrbuf->QRfloor == (FloorBeingSearched))
693                 || ((FloorBeingSearched) < 0)))
694                 list_roomname(qrbuf, ra, view);
695 }
696
697 void cmd_lkro(char *argbuf)
698 {
699         int FloorBeingSearched = (-1);
700         if (strlen(argbuf) > 0)
701                 FloorBeingSearched = extract_int(argbuf, 0);
702
703         if (CtdlAccessCheck(ac_logged_in)) return;
704         
705         if (getuser(&CC->user, CC->curr_user)) {
706                 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
707                 return;
708         }
709         cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
710
711         ForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
712         cprintf("000\n");
713 }
714
715
716
717 /* 
718  * cmd_lzrm()   -  List all forgotten rooms
719  */
720 void cmd_lzrm_backend(struct ctdlroom *qrbuf, void *data)
721 {
722         int FloorBeingSearched = (-1);
723         int ra;
724         int view;
725
726         FloorBeingSearched = *(int *)data;
727         CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
728
729         if ((ra & UA_GOTOALLOWED)
730             && (ra & UA_ZAPPED)
731             && ((qrbuf->QRfloor == (FloorBeingSearched))
732                 || ((FloorBeingSearched) < 0)))
733                 list_roomname(qrbuf, ra, view);
734 }
735
736 void cmd_lzrm(char *argbuf)
737 {
738         int FloorBeingSearched = (-1);
739         if (strlen(argbuf) > 0)
740                 FloorBeingSearched = extract_int(argbuf, 0);
741
742         if (CtdlAccessCheck(ac_logged_in)) return;
743         
744         if (getuser(&CC->user, CC->curr_user)) {
745                 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
746                 return;
747         }
748         cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
749
750         ForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
751         cprintf("000\n");
752 }
753
754
755 /*
756  * Make the specified room the current room for this session.  No validation
757  * or access control is done here -- the caller should make sure that the
758  * specified room exists and is ok to access.
759  */
760 void usergoto(char *where, int display_result, int transiently,
761                 int *retmsgs, int *retnew)
762 {
763         int a;
764         int new_messages = 0;
765         int old_messages = 0;
766         int total_messages = 0;
767         int info = 0;
768         int rmailflag;
769         int raideflag;
770         int newmailcount = 0;
771         struct visit vbuf;
772         char truncated_roomname[ROOMNAMELEN];
773         struct cdbdata *cdbfr;
774         long *msglist = NULL;
775         int num_msgs = 0;
776         unsigned int original_v_flags;
777         int num_sets;
778         int s;
779         char setstr[128], lostr[64], histr[64];
780         long lo, hi;
781         int is_trash = 0;
782
783         /* If the supplied room name is NULL, the caller wants us to know that
784          * it has already copied the room record into CC->room, so
785          * we can skip the extra database fetch.
786          */
787         if (where != NULL) {
788                 safestrncpy(CC->room.QRname, where, sizeof CC->room.QRname);
789                 getroom(&CC->room, where);
790         }
791
792         /* Take care of all the formalities. */
793
794         begin_critical_section(S_USERS);
795         CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
796         original_v_flags = vbuf.v_flags;
797
798         /* Know the room ... but not if it's the page log room, or if the
799          * caller specified that we're only entering this room transiently.
800          */
801         if ((strcasecmp(CC->room.QRname, config.c_logpages))
802            && (transiently == 0) ) {
803                 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
804                 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
805         }
806         
807         /* Only rewrite the database record if we changed something */
808         if (vbuf.v_flags != original_v_flags) {
809                 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
810         }
811         end_critical_section(S_USERS);
812
813         /* Check for new mail */
814         newmailcount = NewMailCount();
815
816         /* set info to 1 if the user needs to read the room's info file */
817         if (CC->room.QRinfo > vbuf.v_lastseen) {
818                 info = 1;
819         }
820
821         get_mm();
822         cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
823         if (cdbfr != NULL) {
824                 msglist = (long *) cdbfr->ptr;
825                 cdbfr->ptr = NULL;      /* usergoto() now owns this memory */
826                 num_msgs = cdbfr->len / sizeof(long);
827                 cdb_free(cdbfr);
828         }
829
830         total_messages = 0;
831         for (a=0; a<num_msgs; ++a) {
832                 if (msglist[a] > 0L) ++total_messages;
833         }
834         new_messages = num_msgs;
835         num_sets = num_tokens(vbuf.v_seen, ',');
836         for (s=0; s<num_sets; ++s) {
837                 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
838
839                 extract_token(lostr, setstr, 0, ':', sizeof lostr);
840                 if (num_tokens(setstr, ':') >= 2) {
841                         extract_token(histr, setstr, 1, ':', sizeof histr);
842                         if (!strcmp(histr, "*")) {
843                                 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
844                         }
845                 } 
846                 else {
847                         strcpy(histr, lostr);
848                 }
849                 lo = atol(lostr);
850                 hi = atol(histr);
851
852                 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
853                         if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
854                                 ++old_messages;
855                                 msglist[a] = 0L;
856                         }
857                 }
858         }
859         new_messages = total_messages - old_messages;
860
861         if (msglist != NULL) free(msglist);
862
863         if (CC->room.QRflags & QR_MAILBOX)
864                 rmailflag = 1;
865         else
866                 rmailflag = 0;
867
868         if ((CC->room.QRroomaide == CC->user.usernum)
869             || (CC->user.axlevel >= 6))
870                 raideflag = 1;
871         else
872                 raideflag = 0;
873
874         safestrncpy(truncated_roomname, CC->room.QRname, sizeof truncated_roomname);
875         if ( (CC->room.QRflags & QR_MAILBOX)
876            && (atol(CC->room.QRname) == CC->user.usernum) ) {
877                 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
878         }
879
880         if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
881                 is_trash = 1;
882         }
883
884         if (retmsgs != NULL) *retmsgs = total_messages;
885         if (retnew != NULL) *retnew = new_messages;
886         lprintf(CTDL_DEBUG, "<%s> %d new of %d total messages\n",
887                 CC->room.QRname,
888                 new_messages, total_messages
889         );
890
891         CC->curr_view = (int)vbuf.v_view;
892
893         if (display_result) {
894                 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|\n",
895                         CIT_OK, CtdlCheckExpress(),
896                         truncated_roomname,
897                         (int)new_messages,
898                         (int)total_messages,
899                         (int)info,
900                         (int)CC->room.QRflags,
901                         (long)CC->room.QRhighest,
902                         (long)vbuf.v_lastseen,
903                         (int)rmailflag,
904                         (int)raideflag,
905                         (int)newmailcount,
906                         (int)CC->room.QRfloor,
907                         (int)vbuf.v_view,
908                         (int)CC->room.QRdefaultview,
909                         (int)is_trash
910                 );
911         }
912 }
913
914
915 /*
916  * Handle some of the macro named rooms
917  */
918 void convert_room_name_macros(char *towhere, size_t maxlen) {
919         if (!strcasecmp(towhere, "_BASEROOM_")) {
920                 safestrncpy(towhere, config.c_baseroom, maxlen);
921         }
922         else if (!strcasecmp(towhere, "_MAIL_")) {
923                 safestrncpy(towhere, MAILROOM, maxlen);
924         }
925         else if (!strcasecmp(towhere, "_TRASH_")) {
926                 safestrncpy(towhere, USERTRASHROOM, maxlen);
927         }
928         else if (!strcasecmp(towhere, "_BITBUCKET_")) {
929                 safestrncpy(towhere, config.c_twitroom, maxlen);
930         }
931         else if (!strcasecmp(towhere, "_CALENDAR_")) {
932                 safestrncpy(towhere, USERCALENDARROOM, maxlen);
933         }
934         else if (!strcasecmp(towhere, "_TASKS_")) {
935                 safestrncpy(towhere, USERTASKSROOM, maxlen);
936         }
937         else if (!strcasecmp(towhere, "_CONTACTS_")) {
938                 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
939         }
940         else if (!strcasecmp(towhere, "_NOTES_")) {
941                 safestrncpy(towhere, USERNOTESROOM, maxlen);
942         }
943 }
944
945
946 /* 
947  * cmd_goto()  -  goto a new room
948  */
949 void cmd_goto(char *gargs)
950 {
951         struct ctdlroom QRscratch;
952         int c;
953         int ok = 0;
954         int ra;
955         char augmented_roomname[ROOMNAMELEN];
956         char towhere[ROOMNAMELEN];
957         char password[32];
958         int transiently = 0;
959
960         if (CtdlAccessCheck(ac_logged_in)) return;
961
962         extract_token(towhere, gargs, 0, '|', sizeof towhere);
963         extract_token(password, gargs, 1, '|', sizeof password);
964         transiently = extract_int(gargs, 2);
965
966         getuser(&CC->user, CC->curr_user);
967
968         /*
969          * Handle some of the macro named rooms
970          */
971         convert_room_name_macros(towhere, sizeof towhere);
972
973         /* First try a regular match */
974         c = getroom(&QRscratch, towhere);
975
976         /* Then try a mailbox name match */
977         if (c != 0) {
978                 MailboxName(augmented_roomname, sizeof augmented_roomname,
979                             &CC->user, towhere);
980                 c = getroom(&QRscratch, augmented_roomname);
981                 if (c == 0)
982                         safestrncpy(towhere, augmented_roomname, sizeof towhere);
983         }
984
985         /* And if the room was found... */
986         if (c == 0) {
987
988                 /* Let internal programs go directly to any room. */
989                 if (CC->internal_pgm) {
990                         memcpy(&CC->room, &QRscratch,
991                                 sizeof(struct ctdlroom));
992                         usergoto(NULL, 1, transiently, NULL, NULL);
993                         return;
994                 }
995
996                 /* See if there is an existing user/room relationship */
997                 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
998
999                 /* normal clients have to pass through security */
1000                 if (ra & UA_GOTOALLOWED) {
1001                         ok = 1;
1002                 }
1003
1004                 if (ok == 1) {
1005                         if ((QRscratch.QRflags & QR_MAILBOX) &&
1006                             ((ra & UA_GOTOALLOWED))) {
1007                                 memcpy(&CC->room, &QRscratch,
1008                                         sizeof(struct ctdlroom));
1009                                 usergoto(NULL, 1, transiently, NULL, NULL);
1010                                 return;
1011                         } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
1012                             ((ra & UA_KNOWN) == 0) &&
1013                             (strcasecmp(QRscratch.QRpasswd, password)) &&
1014                             (CC->user.axlevel < 6)
1015                             ) {
1016                                 cprintf("%d wrong or missing passwd\n",
1017                                         ERROR + PASSWORD_REQUIRED);
1018                                 return;
1019                         } else if ((QRscratch.QRflags & QR_PRIVATE) &&
1020                                    ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
1021                                    ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
1022                                    ((ra & UA_KNOWN) == 0) &&
1023                                    (CC->user.axlevel < 6)
1024                                   ) {
1025                                 lprintf(CTDL_DEBUG, "Failed to acquire private room\n");
1026                         } else {
1027                                 memcpy(&CC->room, &QRscratch,
1028                                         sizeof(struct ctdlroom));
1029                                 usergoto(NULL, 1, transiently, NULL, NULL);
1030                                 return;
1031                         }
1032                 }
1033         }
1034
1035         cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
1036 }
1037
1038
1039 void cmd_whok(void)
1040 {
1041         struct ctdluser temp;
1042         struct cdbdata *cdbus;
1043         int ra;
1044
1045         getuser(&CC->user, CC->curr_user);
1046
1047         /*
1048          * This command is only allowed by aides, room aides,
1049          * and room namespace owners
1050          */
1051         if (is_room_aide()
1052            || (atol(CC->room.QRname) == CC->user.usernum) ) {
1053                 /* access granted */
1054         }
1055         else {
1056                 /* access denied */
1057                 cprintf("%d Higher access or room ownership required.\n",
1058                         ERROR + HIGHER_ACCESS_REQUIRED);
1059                 return;
1060         }
1061
1062         cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
1063         cdb_rewind(CDB_USERS);
1064         while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1065                 memset(&temp, 0, sizeof temp);
1066                 memcpy(&temp, cdbus->ptr, sizeof temp);
1067                 cdb_free(cdbus);
1068
1069                 CtdlRoomAccess(&CC->room, &temp, &ra, NULL);
1070                 if ((CC->room.QRflags & QR_INUSE)
1071                     && (ra & UA_KNOWN)
1072                     )
1073                         cprintf("%s\n", temp.fullname);
1074         }
1075         cprintf("000\n");
1076 }
1077
1078
1079 /*
1080  * RDIR command for room directory
1081  */
1082 void cmd_rdir(void)
1083 {
1084         char buf[256];
1085         char flnm[256];
1086         char comment[256];
1087         FILE *ls, *fd;
1088         struct stat statbuf;
1089         char tempfilename[PATH_MAX];
1090
1091         if (CtdlAccessCheck(ac_logged_in)) return;
1092         CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
1093         
1094         getroom(&CC->room, CC->room.QRname);
1095         getuser(&CC->user, CC->curr_user);
1096
1097         if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
1098                 cprintf("%d not here.\n", ERROR + NOT_HERE);
1099                 return;
1100         }
1101         if (((CC->room.QRflags & QR_VISDIR) == 0)
1102             && (CC->user.axlevel < 6)
1103             && (CC->user.usernum != CC->room.QRroomaide)) {
1104                 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1105                 return;
1106         }
1107         cprintf("%d %s|%s/%s\n",
1108                         LISTING_FOLLOWS, 
1109                         config.c_fqdn,
1110                         ctdl_file_dir, 
1111                         CC->room.QRdirname);
1112
1113         snprintf(buf, sizeof buf, 
1114                          "ls %s/%s >%s 2>/dev/null",
1115                          ctdl_file_dir,
1116                          CC->room.QRdirname, 
1117                          tempfilename);
1118         system(buf);
1119
1120         snprintf(buf, sizeof buf, 
1121                          "%s/%s/filedir",
1122                          ctdl_file_dir,
1123                          CC->room.QRdirname);
1124         fd = fopen(buf, "r");
1125         if (fd == NULL)
1126                 fd = fopen("/dev/null", "r");
1127
1128         ls = fopen(tempfilename, "r");
1129         while (fgets(flnm, sizeof flnm, ls) != NULL) {
1130                 flnm[strlen(flnm) - 1] = 0;
1131                 if (strcasecmp(flnm, "filedir")) {
1132                         snprintf(buf, sizeof buf, 
1133                                          "%s/%s/%s",
1134                                          ctdl_file_dir,
1135                                          CC->room.QRdirname,
1136                                          flnm);
1137                         stat(buf, &statbuf);
1138                         safestrncpy(comment, "", sizeof comment);
1139                         fseek(fd, 0L, 0);
1140                         while ((fgets(buf, sizeof buf, fd) != NULL)
1141                                && (strlen(comment) == 0)) {
1142                                 buf[strlen(buf) - 1] = 0;
1143                                 if ((!strncasecmp(buf, flnm, strlen(flnm)))
1144                                     && (buf[strlen(flnm)] == ' '))
1145                                         safestrncpy(comment,
1146                                             &buf[strlen(flnm) + 1],
1147                                             sizeof comment);
1148                         }
1149                         cprintf("%s|%ld|%s\n", flnm, (long)statbuf.st_size, comment);
1150                 }
1151         }
1152         fclose(ls);
1153         fclose(fd);
1154         unlink(tempfilename);
1155
1156         cprintf("000\n");
1157 }
1158
1159 /*
1160  * get room parameters (aide or room aide command)
1161  */
1162 void cmd_getr(void)
1163 {
1164         if (CtdlAccessCheck(ac_room_aide)) return;
1165
1166         getroom(&CC->room, CC->room.QRname);
1167         cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1168                 CIT_OK,
1169                 CtdlCheckExpress(),
1170
1171                 ((CC->room.QRflags & QR_MAILBOX) ?
1172                         &CC->room.QRname[11] : CC->room.QRname),
1173
1174                 ((CC->room.QRflags & QR_PASSWORDED) ?
1175                         CC->room.QRpasswd : ""),
1176
1177                 ((CC->room.QRflags & QR_DIRECTORY) ?
1178                         CC->room.QRdirname : ""),
1179
1180                 CC->room.QRflags,
1181                 (int) CC->room.QRfloor,
1182                 (int) CC->room.QRorder,
1183
1184                 CC->room.QRdefaultview,
1185                 CC->room.QRflags2
1186                 );
1187 }
1188
1189
1190 /*
1191  * Back end function to rename a room.
1192  * You can also specify which floor to move the room to, or specify -1 to
1193  * keep the room on the same floor it was on.
1194  *
1195  * If you are renaming a mailbox room, you must supply the namespace prefix
1196  * in *at least* the old name!
1197  */
1198 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1199         int old_floor = 0;
1200         struct ctdlroom qrbuf;
1201         struct ctdlroom qrtmp;
1202         int ret = 0;
1203         struct floor *fl;
1204         struct floor flbuf;
1205         long owner = 0L;
1206         char actual_old_name[ROOMNAMELEN];
1207
1208         lprintf(CTDL_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1209                 old_name, new_name, new_floor);
1210
1211         if (new_floor >= 0) {
1212                 fl = cgetfloor(new_floor);
1213                 if ((fl->f_flags & F_INUSE) == 0) {
1214                         return(crr_invalid_floor);
1215                 }
1216         }
1217
1218         begin_critical_section(S_ROOMS);
1219
1220         if ( (getroom(&qrtmp, new_name) == 0) 
1221            && (strcasecmp(new_name, old_name)) ) {
1222                 ret = crr_already_exists;
1223         }
1224
1225         else if (getroom(&qrbuf, old_name) != 0) {
1226                 ret = crr_room_not_found;
1227         }
1228
1229         else if ( (CC->user.axlevel < 6)
1230                   && (CC->user.usernum != qrbuf.QRroomaide)
1231                   && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) )  {
1232                 ret = crr_access_denied;
1233         }
1234
1235         else if (is_noneditable(&qrbuf)) {
1236                 ret = crr_noneditable;
1237         }
1238
1239         else {
1240                 /* Rename it */
1241                 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1242                 if (qrbuf.QRflags & QR_MAILBOX) {
1243                         owner = atol(qrbuf.QRname);
1244                 }
1245                 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1246                         snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1247                                         "%010ld.%s", owner, new_name);
1248                 }
1249                 else {
1250                         safestrncpy(qrbuf.QRname, new_name,
1251                                                 sizeof(qrbuf.QRname));
1252                 }
1253
1254                 /* Reject change of floor for baseroom/aideroom */
1255                 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1256                     !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1257                         new_floor = 0;
1258                 }
1259
1260                 /* Take care of floor stuff */
1261                 old_floor = qrbuf.QRfloor;
1262                 if (new_floor < 0) {
1263                         new_floor = old_floor;
1264                 }
1265                 qrbuf.QRfloor = new_floor;
1266                 putroom(&qrbuf);
1267
1268                 begin_critical_section(S_CONFIG);
1269         
1270                 /* If baseroom/aideroom name changes, update config */
1271                 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1272                         safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1273                         put_config();
1274                 }
1275                 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1276                         safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1277                         put_config();
1278                 }
1279         
1280                 end_critical_section(S_CONFIG);
1281         
1282                 /* If the room name changed, then there are now two room
1283                  * records, so we have to delete the old one.
1284                  */
1285                 if (strcasecmp(new_name, old_name)) {
1286                         b_deleteroom(actual_old_name);
1287                 }
1288
1289                 ret = crr_ok;
1290         }
1291
1292         end_critical_section(S_ROOMS);
1293
1294         /* Adjust the floor reference counts if necessary */
1295         if (new_floor != old_floor) {
1296                 lgetfloor(&flbuf, old_floor);
1297                 --flbuf.f_ref_count;
1298                 lputfloor(&flbuf, old_floor);
1299                 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1300                 lgetfloor(&flbuf, new_floor);
1301                 ++flbuf.f_ref_count;
1302                 lputfloor(&flbuf, new_floor);
1303                 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1304         }
1305
1306         /* ...and everybody say "YATTA!" */     
1307         return(ret);
1308 }
1309
1310
1311 /*
1312  * set room parameters (aide or room aide command)
1313  */
1314 void cmd_setr(char *args)
1315 {
1316         char buf[256];
1317         int new_order = 0;
1318         int r;
1319         int new_floor;
1320         char new_name[ROOMNAMELEN];
1321
1322         if (CtdlAccessCheck(ac_logged_in)) return;
1323
1324         if (num_parms(args) >= 6) {
1325                 new_floor = extract_int(args, 5);
1326         } else {
1327                 new_floor = (-1);       /* don't change the floor */
1328         }
1329
1330         /* When is a new name more than just a new name?  When the old name
1331          * has a namespace prefix.
1332          */
1333         if (CC->room.QRflags & QR_MAILBOX) {
1334                 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
1335         } else {
1336                 safestrncpy(new_name, "", sizeof new_name);
1337         }
1338         extract_token(&new_name[strlen(new_name)], args, 0, '|', (sizeof new_name - strlen(new_name)));
1339
1340         r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
1341
1342         if (r == crr_room_not_found) {
1343                 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
1344         } else if (r == crr_already_exists) {
1345                 cprintf("%d '%s' already exists.\n",
1346                         ERROR + ALREADY_EXISTS, new_name);
1347         } else if (r == crr_noneditable) {
1348                 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
1349         } else if (r == crr_invalid_floor) {
1350                 cprintf("%d Target floor does not exist.\n",
1351                         ERROR + INVALID_FLOOR_OPERATION);
1352         } else if (r == crr_access_denied) {
1353                 cprintf("%d You do not have permission to edit '%s'\n",
1354                         ERROR + HIGHER_ACCESS_REQUIRED,
1355                         CC->room.QRname);
1356         } else if (r != crr_ok) {
1357                 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1358                         ERROR + INTERNAL_ERROR, r);
1359         }
1360
1361         if (r != crr_ok) {
1362                 return;
1363         }
1364
1365         getroom(&CC->room, new_name);
1366
1367         /* Now we have to do a bunch of other stuff */
1368
1369         if (num_parms(args) >= 7) {
1370                 new_order = extract_int(args, 6);
1371                 if (new_order < 1)
1372                         new_order = 1;
1373                 if (new_order > 127)
1374                         new_order = 127;
1375         }
1376
1377         lgetroom(&CC->room, CC->room.QRname);
1378
1379         /* Directory room */
1380         extract_token(buf, args, 2, '|', sizeof buf);
1381         buf[15] = 0;
1382         safestrncpy(CC->room.QRdirname, buf,
1383                 sizeof CC->room.QRdirname);
1384
1385         /* Default view */
1386         if (num_parms(args) >= 8) {
1387                 CC->room.QRdefaultview = extract_int(args, 7);
1388         }
1389
1390         /* Second set of flags */
1391         if (num_parms(args) >= 9) {
1392                 CC->room.QRflags2 = extract_int(args, 8);
1393         }
1394
1395         /* Misc. flags */
1396         CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
1397         /* Clean up a client boo-boo: if the client set the room to
1398          * guess-name or passworded, ensure that the private flag is
1399          * also set.
1400          */
1401         if ((CC->room.QRflags & QR_GUESSNAME)
1402             || (CC->room.QRflags & QR_PASSWORDED))
1403                 CC->room.QRflags |= QR_PRIVATE;
1404
1405         /* Some changes can't apply to BASEROOM */
1406         if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1407                          ROOMNAMELEN)) {
1408                 CC->room.QRorder = 0;
1409                 CC->room.QRpasswd[0] = '\0';
1410                 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1411                         QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1412                 CC->room.QRflags |= QR_PERMANENT;
1413         } else {        
1414                 /* March order (doesn't apply to AIDEROOM) */
1415                 if (num_parms(args) >= 7)
1416                         CC->room.QRorder = (char) new_order;
1417                 /* Room password */
1418                 extract_token(buf, args, 1, '|', sizeof buf);
1419                 buf[10] = 0;
1420                 safestrncpy(CC->room.QRpasswd, buf,
1421                             sizeof CC->room.QRpasswd);
1422                 /* Kick everyone out if the client requested it
1423                  * (by changing the room's generation number)
1424                  */
1425                 if (extract_int(args, 4)) {
1426                         time(&CC->room.QRgen);
1427                 }
1428         }
1429         /* Some changes can't apply to AIDEROOM */
1430         if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1431                          ROOMNAMELEN)) {
1432                 CC->room.QRorder = 0;
1433                 CC->room.QRflags &= ~QR_MAILBOX;
1434                 CC->room.QRflags |= QR_PERMANENT;
1435         }
1436
1437         /* Write the room record back to disk */
1438         lputroom(&CC->room);
1439
1440         /* Create a room directory if necessary */
1441         if (CC->room.QRflags & QR_DIRECTORY) {
1442                 snprintf(buf, sizeof buf,"%s/%s",
1443                                  ctdl_file_dir,
1444                                  CC->room.QRdirname);
1445                 mkdir(buf, 0755);
1446         }
1447         snprintf(buf, sizeof buf, "The room \"%s\" has been edited by %s.\n",
1448                 CC->room.QRname, CC->curr_user);
1449         aide_message(buf);
1450         cprintf("%d Ok\n", CIT_OK);
1451 }
1452
1453
1454
1455 /* 
1456  * get the name of the room aide for this room
1457  */
1458 void cmd_geta(void)
1459 {
1460         struct ctdluser usbuf;
1461
1462         if (CtdlAccessCheck(ac_logged_in)) return;
1463
1464         if (getuserbynumber(&usbuf, CC->room.QRroomaide) == 0) {
1465                 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1466         } else {
1467                 cprintf("%d \n", CIT_OK);
1468         }
1469 }
1470
1471
1472 /* 
1473  * set the room aide for this room
1474  */
1475 void cmd_seta(char *new_ra)
1476 {
1477         struct ctdluser usbuf;
1478         long newu;
1479         char buf[SIZ];
1480         int post_notice;
1481
1482         if (CtdlAccessCheck(ac_room_aide)) return;
1483
1484         if (getuser(&usbuf, new_ra) != 0) {
1485                 newu = (-1L);
1486         } else {
1487                 newu = usbuf.usernum;
1488         }
1489
1490         lgetroom(&CC->room, CC->room.QRname);
1491         post_notice = 0;
1492         if (CC->room.QRroomaide != newu) {
1493                 post_notice = 1;
1494         }
1495         CC->room.QRroomaide = newu;
1496         lputroom(&CC->room);
1497
1498         /*
1499          * We have to post the change notice _after_ writing changes to 
1500          * the room table, otherwise it would deadlock!
1501          */
1502         if (post_notice == 1) {
1503                 if (strlen(usbuf.fullname) > 0)
1504                         snprintf(buf, sizeof buf,
1505                                 "%s is now the room aide for \"%s\".\n",
1506                                 usbuf.fullname, CC->room.QRname);
1507                 else
1508                         snprintf(buf, sizeof buf,
1509                                 "There is now no room aide for \"%s\".\n",
1510                                 CC->room.QRname);
1511                 aide_message(buf);
1512         }
1513         cprintf("%d Ok\n", CIT_OK);
1514 }
1515
1516 /*
1517  * Generate an associated file name for a room
1518  */
1519 void assoc_file_name(char *buf, size_t n,
1520                      struct ctdlroom *qrbuf, const char *prefix)
1521 {
1522         snprintf(buf, n, "./%s/%ld", prefix, qrbuf->QRnumber);
1523 }
1524
1525 /* 
1526  * retrieve info file for this room
1527  */
1528 void cmd_rinf(void)
1529 {
1530         char filename[128];
1531         char buf[SIZ];
1532         FILE *info_fp;
1533
1534         assoc_file_name(filename, sizeof filename, &CC->room, "info");
1535         info_fp = fopen(filename, "r");
1536
1537         if (info_fp == NULL) {
1538                 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
1539                 return;
1540         }
1541         cprintf("%d Info:\n", LISTING_FOLLOWS);
1542         while (fgets(buf, sizeof buf, info_fp) != NULL) {
1543                 if (strlen(buf) > 0)
1544                         buf[strlen(buf) - 1] = 0;
1545                 cprintf("%s\n", buf);
1546         }
1547         cprintf("000\n");
1548         fclose(info_fp);
1549 }
1550
1551 /*
1552  * Asynchronously schedule a room for deletion.  The room will appear
1553  * deleted to the user(s), but it won't actually get purged from the
1554  * database until THE DREADED AUTO-PURGER makes its next run.
1555  */
1556 void schedule_room_for_deletion(struct ctdlroom *qrbuf)
1557 {
1558         char old_name[ROOMNAMELEN];
1559         static int seq = 0;
1560
1561         lprintf(CTDL_NOTICE, "Scheduling room <%s> for deletion\n",
1562                 qrbuf->QRname);
1563
1564         safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1565
1566         getroom(qrbuf, qrbuf->QRname);
1567
1568         /* Turn the room into a private mailbox owned by a user who doesn't
1569          * exist.  This will immediately make the room invisible to everyone,
1570          * and qualify the room for purging.
1571          */
1572         snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1573                 time(NULL),
1574                 ++seq,
1575                 old_name
1576         );
1577         qrbuf->QRflags |= QR_MAILBOX;
1578         time(&qrbuf->QRgen);    /* Use a timestamp as the new generation number  */
1579
1580         putroom(qrbuf);
1581
1582         b_deleteroom(old_name);
1583 }
1584
1585
1586
1587 /*
1588  * Back end processing to delete a room and everything associated with it
1589  * (This one is synchronous and should only get called by THE DREADED
1590  * AUTO-PURGER in serv_expire.c.  All user-facing code should call
1591  * the asynchronous schedule_room_for_deletion() instead.)
1592  */
1593 void delete_room(struct ctdlroom *qrbuf)
1594 {
1595         struct floor flbuf;
1596         char filename[100];
1597
1598         lprintf(CTDL_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1599
1600         /* Delete the info file */
1601         assoc_file_name(filename, sizeof filename, qrbuf, "info");
1602         unlink(filename);
1603
1604         /* Delete the image file */
1605         assoc_file_name(filename, sizeof filename, qrbuf, "images");
1606         unlink(filename);
1607
1608         /* Delete the room's network config file */
1609         assoc_file_name(filename, sizeof filename, qrbuf, "netconfigs");
1610         unlink(filename);
1611
1612         /* Delete the messages in the room
1613          * (Careful: this opens an S_ROOMS critical section!)
1614          */
1615         CtdlDeleteMessages(qrbuf->QRname, 0L, "", 0);
1616
1617         /* Flag the room record as not in use */
1618         lgetroom(qrbuf, qrbuf->QRname);
1619         qrbuf->QRflags = 0;
1620         lputroom(qrbuf);
1621
1622         /* then decrement the reference count for the floor */
1623         lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1624         flbuf.f_ref_count = flbuf.f_ref_count - 1;
1625         lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1626
1627         /* Delete the room record from the database! */
1628         b_deleteroom(qrbuf->QRname);
1629 }
1630
1631
1632
1633 /*
1634  * Check access control for deleting a room
1635  */
1636 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1637
1638         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1639                 return(0);
1640         }
1641
1642         if (is_noneditable(qr)) {
1643                 return(0);
1644         }
1645
1646         /*
1647          * For mailboxes, check stuff
1648          */
1649         if (qr->QRflags & QR_MAILBOX) {
1650
1651                 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1652
1653                 if (atol(qr->QRname) != CC->user.usernum) {
1654                         return(0);      /* not my room */
1655                 }
1656
1657                 /* Can't delete your Mail> room */
1658                 if (!strcasecmp(&qr->QRname[12], MAILROOM)) return(0);
1659
1660                 /* Otherwise it's ok */
1661                 return(1);
1662         }
1663
1664         /*
1665          * For normal rooms, just check for aide or room aide status.
1666          */
1667         return(is_room_aide());
1668 }
1669
1670 /*
1671  * aide command: kill the current room
1672  */
1673 void cmd_kill(char *argbuf)
1674 {
1675         char deleted_room_name[ROOMNAMELEN];
1676         char msg[SIZ];
1677         int kill_ok;
1678
1679         kill_ok = extract_int(argbuf, 0);
1680
1681         if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
1682                 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1683                 return;
1684         }
1685         if (kill_ok) {
1686                 if (CC->room.QRflags & QR_MAILBOX) {
1687                         safestrncpy(deleted_room_name, &CC->room.QRname[11], sizeof deleted_room_name);
1688                 }
1689                 else {
1690                         safestrncpy(deleted_room_name, CC->room.QRname, sizeof deleted_room_name);
1691                 }
1692
1693                 /* Do the dirty work */
1694                 schedule_room_for_deletion(&CC->room);
1695
1696                 /* Return to the Lobby */
1697                 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1698
1699                 /* tell the world what we did */
1700                 snprintf(msg, sizeof msg, "The room \"%s\" has been deleted by %s.\n",
1701                          deleted_room_name, CC->curr_user);
1702                 aide_message(msg);
1703                 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1704         } else {
1705                 cprintf("%d ok to delete.\n", CIT_OK);
1706         }
1707 }
1708
1709
1710 /*
1711  * Internal code to create a new room (returns room flags)
1712  *
1713  * Room types:  0=public, 1=guessname, 2=passworded, 3=inv-only,
1714  *              4=mailbox, 5=mailbox, but caller supplies namespace
1715  */
1716 unsigned create_room(char *new_room_name,
1717                      int new_room_type,
1718                      char *new_room_pass,
1719                      int new_room_floor,
1720                      int really_create,
1721                      int avoid_access,
1722                      int new_room_view)
1723 {
1724
1725         struct ctdlroom qrbuf;
1726         struct floor flbuf;
1727         struct visit vbuf;
1728
1729         lprintf(CTDL_DEBUG, "create_room(name=%s, type=%d, view=%d)\n",
1730                 new_room_name, new_room_type, new_room_view);
1731
1732         if (getroom(&qrbuf, new_room_name) == 0) {
1733                 lprintf(CTDL_DEBUG, "%s already exists.\n", new_room_name);
1734                 return(0);
1735         }
1736
1737         memset(&qrbuf, 0, sizeof(struct ctdlroom));
1738         safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1739         qrbuf.QRflags = QR_INUSE;
1740         if (new_room_type > 0)
1741                 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1742         if (new_room_type == 1)
1743                 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1744         if (new_room_type == 2)
1745                 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1746         if ( (new_room_type == 4) || (new_room_type == 5) )
1747                 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1748
1749         /* If the user is requesting a personal room, set up the room
1750          * name accordingly (prepend the user number)
1751          */
1752         if (new_room_type == 4) {
1753                 MailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1754         }
1755         else {
1756                 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1757         }
1758
1759         /* If the room is private, and the system administrator has elected
1760          * to automatically grant room aide privileges, do so now; otherwise,
1761          * set the room aide to undefined.
1762          */
1763         if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1764                 qrbuf.QRroomaide = CC->user.usernum;
1765         } else {
1766                 qrbuf.QRroomaide = (-1L);
1767         }
1768
1769         /* 
1770          * If the caller is only interested in testing whether this will work,
1771          * return now without creating the room.
1772          */
1773         if (!really_create) return (qrbuf.QRflags);
1774
1775         qrbuf.QRnumber = get_new_room_number();
1776         qrbuf.QRhighest = 0L;   /* No messages in this room yet */
1777         time(&qrbuf.QRgen);     /* Use a timestamp as the generation number */
1778         qrbuf.QRfloor = new_room_floor;
1779         qrbuf.QRdefaultview = new_room_view;
1780
1781         /* save what we just did... */
1782         putroom(&qrbuf);
1783
1784         /* bump the reference count on whatever floor the room is on */
1785         lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1786         flbuf.f_ref_count = flbuf.f_ref_count + 1;
1787         lputfloor(&flbuf, (int) qrbuf.QRfloor);
1788
1789         /* Grant the creator access to the room unless the avoid_access
1790          * parameter was specified.
1791          */
1792         if ( (CC->logged_in) && (avoid_access == 0) ) {
1793                 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1794                 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1795                 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1796                 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1797         }
1798
1799         /* resume our happy day */
1800         return (qrbuf.QRflags);
1801 }
1802
1803
1804 /*
1805  * create a new room
1806  */
1807 void cmd_cre8(char *args)
1808 {
1809         int cre8_ok;
1810         char new_room_name[ROOMNAMELEN];
1811         int new_room_type;
1812         char new_room_pass[32];
1813         int new_room_floor;
1814         int new_room_view;
1815         char *notification_message = NULL;
1816         unsigned newflags;
1817         struct floor *fl;
1818         int avoid_access = 0;
1819
1820         cre8_ok = extract_int(args, 0);
1821         extract_token(new_room_name, args, 1, '|', sizeof new_room_name);
1822         new_room_name[ROOMNAMELEN - 1] = 0;
1823         new_room_type = extract_int(args, 2);
1824         extract_token(new_room_pass, args, 3, '|', sizeof new_room_pass);
1825         avoid_access = extract_int(args, 5);
1826         new_room_view = extract_int(args, 6);
1827         new_room_pass[9] = 0;
1828         new_room_floor = 0;
1829
1830         if ((strlen(new_room_name) == 0) && (cre8_ok == 1)) {
1831                 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
1832                 return;
1833         }
1834
1835         if (!strcasecmp(new_room_name, MAILROOM)) {
1836                 cprintf("%d '%s' already exists.\n",
1837                         ERROR + ALREADY_EXISTS, new_room_name);
1838                 return;
1839         }
1840
1841         if (num_parms(args) >= 5) {
1842                 fl = cgetfloor(extract_int(args, 4));
1843                 if (fl == NULL) {
1844                         cprintf("%d Invalid floor number.\n",
1845                                 ERROR + INVALID_FLOOR_OPERATION);
1846                         return;
1847                 }
1848                 else if ((fl->f_flags & F_INUSE) == 0) {
1849                         cprintf("%d Invalid floor number.\n",
1850                                 ERROR + INVALID_FLOOR_OPERATION);
1851                         return;
1852                 } else {
1853                         new_room_floor = extract_int(args, 4);
1854                 }
1855         }
1856
1857         if (CtdlAccessCheck(ac_logged_in)) return;
1858
1859         if (CC->user.axlevel < config.c_createax) {
1860                 cprintf("%d You need higher access to create rooms.\n",
1861                         ERROR + HIGHER_ACCESS_REQUIRED);
1862                 return;
1863         }
1864
1865         if ((strlen(new_room_name) == 0) && (cre8_ok == 0)) {
1866                 cprintf("%d Ok to create rooms.\n", CIT_OK);
1867                 return;
1868         }
1869
1870         if ((new_room_type < 0) || (new_room_type > 5)) {
1871                 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
1872                 return;
1873         }
1874
1875         if (new_room_type == 5) {
1876                 if (CC->user.axlevel < 6) {
1877                         cprintf("%d Higher access required\n", 
1878                                 ERROR + HIGHER_ACCESS_REQUIRED);
1879                         return;
1880                 }
1881         }
1882
1883         /* Check to make sure the requested room name doesn't already exist */
1884         newflags = create_room(new_room_name,
1885                                 new_room_type, new_room_pass, new_room_floor,
1886                                 0, avoid_access, new_room_view);
1887         if (newflags == 0) {
1888                 cprintf("%d '%s' already exists.\n",
1889                         ERROR + ALREADY_EXISTS, new_room_name);
1890                 return;
1891         }
1892
1893         if (cre8_ok == 0) {
1894                 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
1895                 return;
1896         }
1897
1898         /* If we reach this point, the room needs to be created. */
1899
1900         newflags = create_room(new_room_name,
1901                            new_room_type, new_room_pass, new_room_floor, 1, 0,
1902                            new_room_view);
1903
1904         /* post a message in Aide> describing the new room */
1905         notification_message = malloc(1024);
1906         snprintf(notification_message, 1024,
1907                 "A new room called \"%s\" has been created by %s%s%s%s%s%s\n",
1908                 new_room_name,
1909                 CC->user.fullname,
1910                 ((newflags & QR_MAILBOX) ? " [personal]" : ""),
1911                 ((newflags & QR_PRIVATE) ? " [private]" : ""),
1912                 ((newflags & QR_GUESSNAME) ? " [hidden]" : ""),
1913                 ((newflags & QR_PASSWORDED) ? " Password: " : ""),
1914                 ((newflags & QR_PASSWORDED) ? new_room_pass : "")
1915         );
1916         aide_message(notification_message);
1917         free(notification_message);
1918
1919         cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
1920 }
1921
1922
1923
1924 void cmd_einf(char *ok)
1925 {                               /* enter info file for current room */
1926         FILE *fp;
1927         char infofilename[SIZ];
1928         char buf[SIZ];
1929
1930         unbuffer_output();
1931
1932         if (CtdlAccessCheck(ac_room_aide)) return;
1933
1934         if (atoi(ok) == 0) {
1935                 cprintf("%d Ok.\n", CIT_OK);
1936                 return;
1937         }
1938         assoc_file_name(infofilename, sizeof infofilename, &CC->room, "info");
1939         lprintf(CTDL_DEBUG, "opening\n");
1940         fp = fopen(infofilename, "w");
1941         lprintf(CTDL_DEBUG, "checking\n");
1942         if (fp == NULL) {
1943                 cprintf("%d Cannot open %s: %s\n",
1944                   ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1945                 return;
1946         }
1947         cprintf("%d Send info...\n", SEND_LISTING);
1948
1949         do {
1950                 client_getln(buf, sizeof buf);
1951                 if (strcmp(buf, "000"))
1952                         fprintf(fp, "%s\n", buf);
1953         } while (strcmp(buf, "000"));
1954         fclose(fp);
1955
1956         /* now update the room index so people will see our new info */
1957         lgetroom(&CC->room, CC->room.QRname);           /* lock so no one steps on us */
1958         CC->room.QRinfo = CC->room.QRhighest + 1L;
1959         lputroom(&CC->room);
1960 }
1961
1962
1963 /* 
1964  * cmd_lflr()   -  List all known floors
1965  */
1966 void cmd_lflr(void)
1967 {
1968         int a;
1969         struct floor flbuf;
1970
1971         if (CtdlAccessCheck(ac_logged_in)) return;
1972
1973         cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1974
1975         for (a = 0; a < MAXFLOORS; ++a) {
1976                 getfloor(&flbuf, a);
1977                 if (flbuf.f_flags & F_INUSE) {
1978                         cprintf("%d|%s|%d\n",
1979                                 a,
1980                                 flbuf.f_name,
1981                                 flbuf.f_ref_count);
1982                 }
1983         }
1984         cprintf("000\n");
1985 }
1986
1987
1988
1989 /*
1990  * create a new floor
1991  */
1992 void cmd_cflr(char *argbuf)
1993 {
1994         char new_floor_name[256];
1995         struct floor flbuf;
1996         int cflr_ok;
1997         int free_slot = (-1);
1998         int a;
1999
2000         extract_token(new_floor_name, argbuf, 0, '|', sizeof new_floor_name);
2001         cflr_ok = extract_int(argbuf, 1);
2002
2003         if (CtdlAccessCheck(ac_aide)) return;
2004
2005         if (strlen(new_floor_name) == 0) {
2006                 cprintf("%d Blank floor name not allowed.\n",
2007                         ERROR + ILLEGAL_VALUE);
2008                 return;
2009         }
2010
2011         for (a = 0; a < MAXFLOORS; ++a) {
2012                 getfloor(&flbuf, a);
2013
2014                 /* note any free slots while we're scanning... */
2015                 if (((flbuf.f_flags & F_INUSE) == 0)
2016                     && (free_slot < 0))
2017                         free_slot = a;
2018
2019                 /* check to see if it already exists */
2020                 if ((!strcasecmp(flbuf.f_name, new_floor_name))
2021                     && (flbuf.f_flags & F_INUSE)) {
2022                         cprintf("%d Floor '%s' already exists.\n",
2023                                 ERROR + ALREADY_EXISTS,
2024                                 flbuf.f_name);
2025                         return;
2026                 }
2027         }
2028
2029         if (free_slot < 0) {
2030                 cprintf("%d There is no space available for a new floor.\n",
2031                         ERROR + INVALID_FLOOR_OPERATION);
2032                 return;
2033         }
2034         if (cflr_ok == 0) {
2035                 cprintf("%d ok to create...\n", CIT_OK);
2036                 return;
2037         }
2038         lgetfloor(&flbuf, free_slot);
2039         flbuf.f_flags = F_INUSE;
2040         flbuf.f_ref_count = 0;
2041         safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
2042         lputfloor(&flbuf, free_slot);
2043         cprintf("%d %d\n", CIT_OK, free_slot);
2044 }
2045
2046
2047
2048 /*
2049  * delete a floor
2050  */
2051 void cmd_kflr(char *argbuf)
2052 {
2053         struct floor flbuf;
2054         int floor_to_delete;
2055         int kflr_ok;
2056         int delete_ok;
2057
2058         floor_to_delete = extract_int(argbuf, 0);
2059         kflr_ok = extract_int(argbuf, 1);
2060
2061         if (CtdlAccessCheck(ac_aide)) return;
2062
2063         lgetfloor(&flbuf, floor_to_delete);
2064
2065         delete_ok = 1;
2066         if ((flbuf.f_flags & F_INUSE) == 0) {
2067                 cprintf("%d Floor %d not in use.\n",
2068                         ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
2069                 delete_ok = 0;
2070         } else {
2071                 if (flbuf.f_ref_count != 0) {
2072                         cprintf("%d Cannot delete; floor contains %d rooms.\n",
2073                                 ERROR + INVALID_FLOOR_OPERATION,
2074                                 flbuf.f_ref_count);
2075                         delete_ok = 0;
2076                 } else {
2077                         if (kflr_ok == 1) {
2078                                 cprintf("%d Ok\n", CIT_OK);
2079                         } else {
2080                                 cprintf("%d Ok to delete...\n", CIT_OK);
2081                         }
2082
2083                 }
2084
2085         }
2086
2087         if ((delete_ok == 1) && (kflr_ok == 1))
2088                 flbuf.f_flags = 0;
2089         lputfloor(&flbuf, floor_to_delete);
2090 }
2091
2092 /*
2093  * edit a floor
2094  */
2095 void cmd_eflr(char *argbuf)
2096 {
2097         struct floor flbuf;
2098         int floor_num;
2099         int np;
2100
2101         np = num_parms(argbuf);
2102         if (np < 1) {
2103                 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
2104                 return;
2105         }
2106
2107         if (CtdlAccessCheck(ac_aide)) return;
2108
2109         floor_num = extract_int(argbuf, 0);
2110         lgetfloor(&flbuf, floor_num);
2111         if ((flbuf.f_flags & F_INUSE) == 0) {
2112                 lputfloor(&flbuf, floor_num);
2113                 cprintf("%d Floor %d is not in use.\n",
2114                         ERROR + INVALID_FLOOR_OPERATION, floor_num);
2115                 return;
2116         }
2117         if (np >= 2)
2118                 extract_token(flbuf.f_name, argbuf, 1, '|', sizeof flbuf.f_name);
2119         lputfloor(&flbuf, floor_num);
2120
2121         cprintf("%d Ok\n", CIT_OK);
2122 }