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