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