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