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