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