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