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