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