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