af6cb2149babc141aafbc0394f8191aae5eaf43a
[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 "server.h"
32 #include "database.h"
33 #include "config.h"
34 #include "room_ops.h"
35 #include "sysdep_decls.h"
36 #include "support.h"
37 #include "user_ops.h"
38 #include "msgbase.h"
39 #include "citserver.h"
40 #include "control.h"
41 #include "tools.h"
42 #include "citadel_dirs.h"
43
44 struct floor *floorcache[MAXFLOORS];
45
46 /*
47  * Retrieve access control information for any user/room pair
48  */
49 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf,
50                 int *result, int *view)
51 {
52         int retval = 0;
53         struct visit vbuf;
54
55         /* for internal programs, always do everything */
56         if (((CC->internal_pgm)) && (roombuf->QRflags & QR_INUSE)) {
57                 retval = (UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED);
58                 vbuf.v_view = 0;
59                 goto SKIP_EVERYTHING;
60         }
61
62         /* Locate any applicable user/room relationships */
63         CtdlGetRelationship(&vbuf, userbuf, roombuf);
64
65         /* Force the properties of the Aide room */
66         if (!strcasecmp(roombuf->QRname, config.c_aideroom)) {
67                 if (userbuf->axlevel >= 6) {
68                         retval = UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
69                 } else {
70                         retval = 0;
71                 }
72                 goto NEWMSG;
73         }
74
75         /* If this is a public room, it's accessible... */
76         if ( ((roombuf->QRflags & QR_PRIVATE) == 0) 
77            && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
78                 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
79         }
80
81         /* If this is a preferred users only room, check access level */
82         if (roombuf->QRflags & QR_PREFONLY) {
83                 if (userbuf->axlevel < 5) {
84                         retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
85                 }
86         }
87
88         /* For private rooms, check the generation number matchups */
89         if ( (roombuf->QRflags & QR_PRIVATE) 
90            && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
91
92                 /* An explicit match means the user belongs in this room */
93                 if (vbuf.v_flags & V_ACCESS) {
94                         retval = retval | UA_KNOWN | UA_GOTOALLOWED;
95                 }
96                 /* Otherwise, check if this is a guess-name or passworded
97                  * room.  If it is, a goto may at least be attempted
98                  */
99                 else if ((roombuf->QRflags & QR_PRIVATE)
100                          || (roombuf->QRflags & QR_PASSWORDED)) {
101                         retval = retval & ~UA_KNOWN;
102                         retval = retval | UA_GOTOALLOWED;
103                 }
104         }
105
106         /* For mailbox rooms, also check the namespace */
107         /* Also, mailbox owners can delete their messages */
108         if (roombuf->QRflags & QR_MAILBOX) {
109                 if (userbuf->usernum == atol(roombuf->QRname)) {
110                         retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
111                 }
112                 /* An explicit match means the user belongs in this room */
113                 if (vbuf.v_flags & V_ACCESS) {
114                         retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_DELETEALLOWED;
115                 }
116         }
117
118         /* For non-mailbox rooms... */
119         else {
120
121                 /* User is allowed to post in the room unless:
122                  * - User is not validated
123                  * - User has no net privileges and it is a shared network room
124                  * - It is a read-only room
125                  */
126                 int post_allowed = 1;
127                 if (CC->user.axlevel < 2) post_allowed = 0;
128                 if ((CC->user.axlevel < 4) && (CC->room.QRflags & QR_NETWORK)) post_allowed = 0;
129                 if (roombuf->QRflags & QR_READONLY) post_allowed = 0;
130                 if (post_allowed) {
131                         retval = retval | UA_POSTALLOWED;
132                 }
133
134                 /* If "collaborative deletion" is active for this room, any user who can post
135                  * is also allowed to delete
136                  */
137                 if (CC->room.QRflags2 & QR2_COLLABDEL) {
138                         if (retval & UA_POSTALLOWED) {
139                                 retval = retval | UA_DELETEALLOWED;
140                         }
141                 }
142
143         }
144
145         /* Check to see if the user has forgotten this room */
146         if (vbuf.v_flags & V_FORGET) {
147                 retval = retval & ~UA_KNOWN;
148                 if ( ( ((roombuf->QRflags & QR_PRIVATE) == 0) 
149                       && ((roombuf->QRflags & QR_MAILBOX) == 0) )
150                    || ( (roombuf->QRflags & QR_MAILBOX) 
151                       && (atol(roombuf->QRname) == CC->user.usernum))) {
152                         retval = retval | UA_ZAPPED;
153                 }
154         }
155         /* If user is explicitly locked out of this room, deny everything */
156         if (vbuf.v_flags & V_LOCKOUT) {
157                 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED & ~UA_POSTALLOWED;
158         }
159
160         /* Aides get access to all private rooms */
161         if ( (userbuf->axlevel >= 6)
162            && ((roombuf->QRflags & QR_MAILBOX) == 0) ) {
163                 if (vbuf.v_flags & V_FORGET) {
164                         retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED;
165                 }
166                 else {
167                         retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED;
168                 }
169         }
170
171         /* Aides can gain access to mailboxes as well, but they don't show
172          * by default.
173          */
174         if ( (userbuf->axlevel >= 6)
175            && (roombuf->QRflags & QR_MAILBOX) ) {
176                 retval = retval | UA_GOTOALLOWED | UA_POSTALLOWED;
177         }
178
179         /* Aides and Room Aides have admin privileges */
180         if ( (userbuf->axlevel >= 6)
181            || (userbuf->usernum == roombuf->QRroomaide)
182            ) {
183                 retval = retval | UA_ADMINALLOWED | UA_DELETEALLOWED;
184         }
185
186 NEWMSG: /* By the way, we also check for the presence of new messages */
187         if (is_msg_in_sequence_set(vbuf.v_seen, roombuf->QRhighest) == 0) {
188                 retval = retval | UA_HASNEWMSGS;
189         }
190
191         /* System rooms never show up in the list. */
192         if (roombuf->QRflags2 & QR2_SYSTEM) {
193                 retval = retval & ~UA_KNOWN;
194         }
195
196 SKIP_EVERYTHING:
197         /* Now give the caller the information it wants. */
198         if (result != NULL) *result = retval;
199         if (view != NULL) *view = vbuf.v_view;
200 }
201
202 /*
203  * Self-checking stuff for a room record read into memory
204  */
205 void room_sanity_check(struct ctdlroom *qrbuf)
206 {
207         /* Mailbox rooms are always on the lowest floor */
208         if (qrbuf->QRflags & QR_MAILBOX) {
209                 qrbuf->QRfloor = 0;
210         }
211         /* Listing order of 0 is illegal except for base rooms */
212         if (qrbuf->QRorder == 0)
213                 if (!(qrbuf->QRflags & QR_MAILBOX) &&
214                     strncasecmp(qrbuf->QRname, config.c_baseroom, ROOMNAMELEN)
215                     &&
216                     strncasecmp(qrbuf->QRname, config.c_aideroom, ROOMNAMELEN))
217                         qrbuf->QRorder = 64;
218 }
219
220
221 /*
222  * getroom()  -  retrieve room data from disk
223  */
224 int getroom(struct ctdlroom *qrbuf, char *room_name)
225 {
226         struct cdbdata *cdbqr;
227         char lowercase_name[ROOMNAMELEN];
228         char personal_lowercase_name[ROOMNAMELEN];
229         char *dptr, *sptr, *eptr;
230
231         dptr = lowercase_name;
232         sptr = room_name;
233         eptr = (dptr + (sizeof lowercase_name - 1));
234         while (!IsEmptyStr(sptr) && (dptr < eptr)){
235                 *dptr = tolower(*sptr);
236                 sptr++; dptr++;
237         }
238         *dptr = '\0';
239
240         memset(qrbuf, 0, sizeof(struct ctdlroom));
241
242         /* First, try the public namespace */
243         cdbqr = cdb_fetch(CDB_ROOMS,
244                           lowercase_name, strlen(lowercase_name));
245
246         /* If that didn't work, try the user's personal namespace */
247         if (cdbqr == NULL) {
248                 snprintf(personal_lowercase_name,
249                          sizeof personal_lowercase_name, "%010ld.%s",
250                          CC->user.usernum, lowercase_name);
251                 cdbqr = cdb_fetch(CDB_ROOMS,
252                                   personal_lowercase_name,
253                                   strlen(personal_lowercase_name));
254         }
255         if (cdbqr != NULL) {
256                 memcpy(qrbuf, cdbqr->ptr,
257                        ((cdbqr->len > sizeof(struct ctdlroom)) ?
258                         sizeof(struct ctdlroom) : cdbqr->len));
259                 cdb_free(cdbqr);
260
261                 room_sanity_check(qrbuf);
262
263                 return (0);
264         } else {
265                 return (1);
266         }
267 }
268
269 /*
270  * lgetroom()  -  same as getroom() but locks the record (if supported)
271  */
272 int lgetroom(struct ctdlroom *qrbuf, char *room_name)
273 {
274         register int retval;
275         retval = getroom(qrbuf, room_name);
276         if (retval == 0) begin_critical_section(S_ROOMS);
277         return(retval);
278 }
279
280
281 /*
282  * b_putroom()  -  back end to putroom() and b_deleteroom()
283  *              (if the supplied buffer is NULL, delete the room record)
284  */
285 void b_putroom(struct ctdlroom *qrbuf, char *room_name)
286 {
287         char lowercase_name[ROOMNAMELEN];
288         char *aptr, *bptr;
289         long len;
290
291         aptr = room_name;
292         bptr = lowercase_name;
293         while (!IsEmptyStr(aptr))
294         {
295                 *bptr = tolower(*aptr);
296                 aptr++;
297                 bptr++;
298         }
299         *bptr='\0';
300
301         len = bptr - lowercase_name;
302         if (qrbuf == NULL) {
303                 cdb_delete(CDB_ROOMS,
304                            lowercase_name, len);
305         } else {
306                 time(&qrbuf->QRmtime);
307                 cdb_store(CDB_ROOMS,
308                           lowercase_name, len,
309                           qrbuf, sizeof(struct ctdlroom));
310         }
311 }
312
313
314 /* 
315  * putroom()  -  store room data to disk
316  */
317 void putroom(struct ctdlroom *qrbuf) {
318         b_putroom(qrbuf, qrbuf->QRname);
319 }
320
321
322 /*
323  * b_deleteroom()  -  delete a room record from disk
324  */
325 void b_deleteroom(char *room_name) {
326         b_putroom(NULL, room_name);
327 }
328
329
330
331 /*
332  * lputroom()  -  same as putroom() but unlocks the record (if supported)
333  */
334 void lputroom(struct ctdlroom *qrbuf)
335 {
336
337         putroom(qrbuf);
338         end_critical_section(S_ROOMS);
339
340 }
341
342 /****************************************************************************/
343
344 /*
345  * getfloor()  -  retrieve floor data from disk
346  */
347 void getfloor(struct floor *flbuf, int floor_num)
348 {
349         struct cdbdata *cdbfl;
350
351         memset(flbuf, 0, sizeof(struct floor));
352         cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
353         if (cdbfl != NULL) {
354                 memcpy(flbuf, cdbfl->ptr,
355                        ((cdbfl->len > sizeof(struct floor)) ?
356                         sizeof(struct floor) : cdbfl->len));
357                 cdb_free(cdbfl);
358         } else {
359                 if (floor_num == 0) {
360                         safestrncpy(flbuf->f_name, "Main Floor", 
361                                 sizeof flbuf->f_name);
362                         flbuf->f_flags = F_INUSE;
363                         flbuf->f_ref_count = 3;
364                 }
365         }
366
367 }
368
369 /*
370  * lgetfloor()  -  same as getfloor() but locks the record (if supported)
371  */
372 void lgetfloor(struct floor *flbuf, int floor_num)
373 {
374
375         begin_critical_section(S_FLOORTAB);
376         getfloor(flbuf, floor_num);
377 }
378
379
380 /*
381  * cgetfloor()  -  Get floor record from *cache* (loads from disk if needed)
382  *    
383  * This is strictly a performance hack.
384  */
385 struct floor *cgetfloor(int floor_num) {
386         static int initialized = 0;
387         int i;
388         int fetch_new = 0;
389         struct floor *fl = NULL;
390
391         begin_critical_section(S_FLOORCACHE);
392         if (initialized == 0) {
393                 for (i=0; i<MAXFLOORS; ++i) {
394                         floorcache[floor_num] = NULL;
395                 }
396         initialized = 1;
397         }
398         if (floorcache[floor_num] == NULL) {
399                 fetch_new = 1;
400         }
401         end_critical_section(S_FLOORCACHE);
402
403         if (fetch_new) {
404                 fl = malloc(sizeof(struct floor));
405                 getfloor(fl, floor_num);
406                 begin_critical_section(S_FLOORCACHE);
407                 if (floorcache[floor_num] != NULL) {
408                         free(floorcache[floor_num]);
409                 }
410                 floorcache[floor_num] = fl;
411                 end_critical_section(S_FLOORCACHE);
412         }
413
414         return(floorcache[floor_num]);
415 }
416
417
418
419 /*
420  * putfloor()  -  store floor data on disk
421  */
422 void putfloor(struct floor *flbuf, int floor_num)
423 {
424         /* If we've cached this, clear it out, 'cuz it's WRONG now! */
425         begin_critical_section(S_FLOORCACHE);
426         if (floorcache[floor_num] != NULL) {
427                 free(floorcache[floor_num]);
428                 floorcache[floor_num] = malloc(sizeof(struct floor));
429                 memcpy(floorcache[floor_num], flbuf, sizeof(struct floor));
430         }
431         end_critical_section(S_FLOORCACHE);
432
433         cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
434                   flbuf, sizeof(struct floor));
435 }
436
437
438 /*
439  * lputfloor()  -  same as putfloor() but unlocks the record (if supported)
440  */
441 void lputfloor(struct floor *flbuf, int floor_num)
442 {
443
444         putfloor(flbuf, floor_num);
445         end_critical_section(S_FLOORTAB);
446
447 }
448
449
450 /* 
451  *  Traverse the room file...
452  */
453 void ForEachRoom(void (*CallBack) (struct ctdlroom *EachRoom, void *out_data),
454                 void *in_data)
455 {
456         struct ctdlroom qrbuf;
457         struct cdbdata *cdbqr;
458
459         cdb_rewind(CDB_ROOMS);
460
461         while (cdbqr = cdb_next_item(CDB_ROOMS), cdbqr != NULL) {
462                 memset(&qrbuf, 0, sizeof(struct ctdlroom));
463                 memcpy(&qrbuf, cdbqr->ptr,
464                        ((cdbqr->len > sizeof(struct ctdlroom)) ?
465                         sizeof(struct ctdlroom) : cdbqr->len));
466                 cdb_free(cdbqr);
467                 room_sanity_check(&qrbuf);
468                 if (qrbuf.QRflags & QR_INUSE)
469                         (*CallBack)(&qrbuf, in_data);
470         }
471 }
472
473
474 /*
475  * delete_msglist()  -  delete room message pointers
476  */
477 void delete_msglist(struct ctdlroom *whichroom)
478 {
479         struct cdbdata *cdbml;
480
481         /* Make sure the msglist we're deleting actually exists, otherwise
482          * gdbm will complain when we try to delete an invalid record
483          */
484         cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
485         if (cdbml != NULL) {
486                 cdb_free(cdbml);
487
488                 /* Go ahead and delete it */
489                 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
490         }
491 }
492
493
494
495
496 /*
497  * sort message pointers
498  * (returns new msg count)
499  */
500 int sort_msglist(long listptrs[], int oldcount)
501 {
502         int a, b;
503         long hold1, hold2;
504         int numitems;
505
506         numitems = oldcount;
507         if (numitems < 2)
508                 return (oldcount);
509
510         /* do the sort */
511         for (a = numitems - 2; a >= 0; --a) {
512                 for (b = 0; b <= a; ++b) {
513                         if (listptrs[b] > (listptrs[b + 1])) {
514                                 hold1 = listptrs[b];
515                                 hold2 = listptrs[b + 1];
516                                 listptrs[b] = hold2;
517                                 listptrs[b + 1] = hold1;
518                         }
519                 }
520         }
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         lprintf(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                                 lprintf(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(void)
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(void)
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         
1126         if (CtdlAccessCheck(ac_logged_in)) return;
1127         
1128         getroom(&CC->room, CC->room.QRname);
1129         getuser(&CC->user, CC->curr_user);
1130
1131         if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
1132                 cprintf("%d not here.\n", ERROR + NOT_HERE);
1133                 return;
1134         }
1135         if (((CC->room.QRflags & QR_VISDIR) == 0)
1136             && (CC->user.axlevel < 6)
1137             && (CC->user.usernum != CC->room.QRroomaide)) {
1138                 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1139                 return;
1140         }
1141         cprintf("%d %s|%s/%s\n", LISTING_FOLLOWS, config.c_fqdn, ctdl_file_dir, CC->room.QRdirname);
1142         
1143         snprintf(buf, sizeof buf, "%s/%s", ctdl_file_dir, CC->room.QRdirname);
1144         filedir = opendir (buf);
1145         if (filedir)
1146         {
1147                 snprintf(buf, sizeof buf, "%s/%s/filedir", ctdl_file_dir, CC->room.QRdirname);
1148                 fd = fopen(buf, "r");
1149                 if (fd == NULL)
1150                         fd = fopen("/dev/null", "r");
1151                 while ((filedir_entry = readdir(filedir)))
1152                 {
1153                         if (strcasecmp(filedir_entry->d_name, "filedir") && filedir_entry->d_name[0] != '.')
1154                         {
1155 #ifdef _DIRENT_HAVE_D_NAMELEN
1156                                 d_namelen = filedir_entry->d_namelen;
1157 #else
1158                                 d_namelen = strlen(filedir_entry->d_name);
1159 #endif
1160                                 snprintf(buf, sizeof buf, "%s/%s/%s", ctdl_file_dir, CC->room.QRdirname, filedir_entry->d_name);
1161                                 stat(buf, &statbuf);    /* stat the file */
1162                                 if (!(statbuf.st_mode & S_IFREG))
1163                                 {
1164                                         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);
1165                                         aide_message(buf2, "RDIR found bad file");
1166                                         continue;       /* not a useable file type so don't show it */
1167                                 }
1168                                 safestrncpy(comment, "", sizeof comment);
1169                                 fseek(fd, 0L, 0);       /* rewind descriptions file */
1170                                 /* Get the description from the descriptions file */
1171                                 while ((fgets(buf, sizeof buf, fd) != NULL) && (IsEmptyStr(comment))) 
1172                                 {
1173                                         buf[strlen(buf) - 1] = 0;
1174                                         if ((!strncasecmp(buf, filedir_entry->d_name, d_namelen)) && (buf[d_namelen] == ' '))
1175                                                 safestrncpy(comment, &buf[d_namelen + 1], sizeof comment);
1176                                 }
1177                                 cprintf("%s|%ld|%s\n", filedir_entry->d_name, (long)statbuf.st_size, comment);
1178                         }
1179                 }
1180                 fclose(fd);
1181                 closedir(filedir);
1182         }
1183         cprintf("000\n");
1184 }
1185
1186 /*
1187  * get room parameters (aide or room aide command)
1188  */
1189 void cmd_getr(void)
1190 {
1191         if (CtdlAccessCheck(ac_room_aide)) return;
1192
1193         getroom(&CC->room, CC->room.QRname);
1194         cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1195                 CIT_OK,
1196                 CtdlCheckExpress(),
1197
1198                 ((CC->room.QRflags & QR_MAILBOX) ?
1199                         &CC->room.QRname[11] : CC->room.QRname),
1200
1201                 ((CC->room.QRflags & QR_PASSWORDED) ?
1202                         CC->room.QRpasswd : ""),
1203
1204                 ((CC->room.QRflags & QR_DIRECTORY) ?
1205                         CC->room.QRdirname : ""),
1206
1207                 CC->room.QRflags,
1208                 (int) CC->room.QRfloor,
1209                 (int) CC->room.QRorder,
1210
1211                 CC->room.QRdefaultview,
1212                 CC->room.QRflags2
1213                 );
1214 }
1215
1216
1217 /*
1218  * Back end function to rename a room.
1219  * You can also specify which floor to move the room to, or specify -1 to
1220  * keep the room on the same floor it was on.
1221  *
1222  * If you are renaming a mailbox room, you must supply the namespace prefix
1223  * in *at least* the old name!
1224  */
1225 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1226         int old_floor = 0;
1227         struct ctdlroom qrbuf;
1228         struct ctdlroom qrtmp;
1229         int ret = 0;
1230         struct floor *fl;
1231         struct floor flbuf;
1232         long owner = 0L;
1233         char actual_old_name[ROOMNAMELEN];
1234
1235         lprintf(CTDL_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1236                 old_name, new_name, new_floor);
1237
1238         if (new_floor >= 0) {
1239                 fl = cgetfloor(new_floor);
1240                 if ((fl->f_flags & F_INUSE) == 0) {
1241                         return(crr_invalid_floor);
1242                 }
1243         }
1244
1245         begin_critical_section(S_ROOMS);
1246
1247         if ( (getroom(&qrtmp, new_name) == 0) 
1248            && (strcasecmp(new_name, old_name)) ) {
1249                 ret = crr_already_exists;
1250         }
1251
1252         else if (getroom(&qrbuf, old_name) != 0) {
1253                 ret = crr_room_not_found;
1254         }
1255
1256         else if ( (CC->user.axlevel < 6) && (!CC->internal_pgm)
1257                   && (CC->user.usernum != qrbuf.QRroomaide)
1258                   && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) )  {
1259                 ret = crr_access_denied;
1260         }
1261
1262         else if (is_noneditable(&qrbuf)) {
1263                 ret = crr_noneditable;
1264         }
1265
1266         else {
1267                 /* Rename it */
1268                 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1269                 if (qrbuf.QRflags & QR_MAILBOX) {
1270                         owner = atol(qrbuf.QRname);
1271                 }
1272                 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1273                         snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1274                                         "%010ld.%s", owner, new_name);
1275                 }
1276                 else {
1277                         safestrncpy(qrbuf.QRname, new_name,
1278                                                 sizeof(qrbuf.QRname));
1279                 }
1280
1281                 /* Reject change of floor for baseroom/aideroom */
1282                 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1283                     !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1284                         new_floor = 0;
1285                 }
1286
1287                 /* Take care of floor stuff */
1288                 old_floor = qrbuf.QRfloor;
1289                 if (new_floor < 0) {
1290                         new_floor = old_floor;
1291                 }
1292                 qrbuf.QRfloor = new_floor;
1293                 putroom(&qrbuf);
1294
1295                 begin_critical_section(S_CONFIG);
1296         
1297                 /* If baseroom/aideroom name changes, update config */
1298                 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1299                         safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1300                         put_config();
1301                 }
1302                 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1303                         safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1304                         put_config();
1305                 }
1306         
1307                 end_critical_section(S_CONFIG);
1308         
1309                 /* If the room name changed, then there are now two room
1310                  * records, so we have to delete the old one.
1311                  */
1312                 if (strcasecmp(new_name, old_name)) {
1313                         b_deleteroom(actual_old_name);
1314                 }
1315
1316                 ret = crr_ok;
1317         }
1318
1319         end_critical_section(S_ROOMS);
1320
1321         /* Adjust the floor reference counts if necessary */
1322         if (new_floor != old_floor) {
1323                 lgetfloor(&flbuf, old_floor);
1324                 --flbuf.f_ref_count;
1325                 lputfloor(&flbuf, old_floor);
1326                 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1327                 lgetfloor(&flbuf, new_floor);
1328                 ++flbuf.f_ref_count;
1329                 lputfloor(&flbuf, new_floor);
1330                 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1331         }
1332
1333         /* ...and everybody say "YATTA!" */     
1334         return(ret);
1335 }
1336
1337
1338 /*
1339  * set room parameters (aide or room aide command)
1340  */
1341 void cmd_setr(char *args)
1342 {
1343         char buf[256];
1344         int new_order = 0;
1345         int r;
1346         int new_floor;
1347         char new_name[ROOMNAMELEN];
1348
1349         if (CtdlAccessCheck(ac_logged_in)) return;
1350
1351         if (num_parms(args) >= 6) {
1352                 new_floor = extract_int(args, 5);
1353         } else {
1354                 new_floor = (-1);       /* don't change the floor */
1355         }
1356
1357         /* When is a new name more than just a new name?  When the old name
1358          * has a namespace prefix.
1359          */
1360         if (CC->room.QRflags & QR_MAILBOX) {
1361                 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
1362         } else {
1363                 safestrncpy(new_name, "", sizeof new_name);
1364         }
1365         extract_token(&new_name[strlen(new_name)], args, 0, '|', (sizeof new_name - strlen(new_name)));
1366
1367         r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
1368
1369         if (r == crr_room_not_found) {
1370                 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
1371         } else if (r == crr_already_exists) {
1372                 cprintf("%d '%s' already exists.\n",
1373                         ERROR + ALREADY_EXISTS, new_name);
1374         } else if (r == crr_noneditable) {
1375                 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
1376         } else if (r == crr_invalid_floor) {
1377                 cprintf("%d Target floor does not exist.\n",
1378                         ERROR + INVALID_FLOOR_OPERATION);
1379         } else if (r == crr_access_denied) {
1380                 cprintf("%d You do not have permission to edit '%s'\n",
1381                         ERROR + HIGHER_ACCESS_REQUIRED,
1382                         CC->room.QRname);
1383         } else if (r != crr_ok) {
1384                 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1385                         ERROR + INTERNAL_ERROR, r);
1386         }
1387
1388         if (r != crr_ok) {
1389                 return;
1390         }
1391
1392         getroom(&CC->room, new_name);
1393
1394         /* Now we have to do a bunch of other stuff */
1395
1396         if (num_parms(args) >= 7) {
1397                 new_order = extract_int(args, 6);
1398                 if (new_order < 1)
1399                         new_order = 1;
1400                 if (new_order > 127)
1401                         new_order = 127;
1402         }
1403
1404         lgetroom(&CC->room, CC->room.QRname);
1405
1406         /* Directory room */
1407         extract_token(buf, args, 2, '|', sizeof buf);
1408         buf[15] = 0;
1409         safestrncpy(CC->room.QRdirname, buf,
1410                 sizeof CC->room.QRdirname);
1411
1412         /* Default view */
1413         if (num_parms(args) >= 8) {
1414                 CC->room.QRdefaultview = extract_int(args, 7);
1415         }
1416
1417         /* Second set of flags */
1418         if (num_parms(args) >= 9) {
1419                 CC->room.QRflags2 = extract_int(args, 8);
1420         }
1421
1422         /* Misc. flags */
1423         CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
1424         /* Clean up a client boo-boo: if the client set the room to
1425          * guess-name or passworded, ensure that the private flag is
1426          * also set.
1427          */
1428         if ((CC->room.QRflags & QR_GUESSNAME)
1429             || (CC->room.QRflags & QR_PASSWORDED))
1430                 CC->room.QRflags |= QR_PRIVATE;
1431
1432         /* Some changes can't apply to BASEROOM */
1433         if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1434                          ROOMNAMELEN)) {
1435                 CC->room.QRorder = 0;
1436                 CC->room.QRpasswd[0] = '\0';
1437                 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1438                         QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1439                 CC->room.QRflags |= QR_PERMANENT;
1440         } else {        
1441                 /* March order (doesn't apply to AIDEROOM) */
1442                 if (num_parms(args) >= 7)
1443                         CC->room.QRorder = (char) new_order;
1444                 /* Room password */
1445                 extract_token(buf, args, 1, '|', sizeof buf);
1446                 buf[10] = 0;
1447                 safestrncpy(CC->room.QRpasswd, buf,
1448                             sizeof CC->room.QRpasswd);
1449                 /* Kick everyone out if the client requested it
1450                  * (by changing the room's generation number)
1451                  */
1452                 if (extract_int(args, 4)) {
1453                         time(&CC->room.QRgen);
1454                 }
1455         }
1456         /* Some changes can't apply to AIDEROOM */
1457         if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1458                          ROOMNAMELEN)) {
1459                 CC->room.QRorder = 0;
1460                 CC->room.QRflags &= ~QR_MAILBOX;
1461                 CC->room.QRflags |= QR_PERMANENT;
1462         }
1463
1464         /* Write the room record back to disk */
1465         lputroom(&CC->room);
1466
1467         /* Create a room directory if necessary */
1468         if (CC->room.QRflags & QR_DIRECTORY) {
1469                 snprintf(buf, sizeof buf,"%s/%s",
1470                                  ctdl_file_dir,
1471                                  CC->room.QRdirname);
1472                 mkdir(buf, 0755);
1473         }
1474         snprintf(buf, sizeof buf, "The room \"%s\" has been edited by %s.\n",
1475                 CC->room.QRname, CC->curr_user);
1476         aide_message(buf, "Room modification Message");
1477         cprintf("%d Ok\n", CIT_OK);
1478 }
1479
1480
1481
1482 /* 
1483  * get the name of the room aide for this room
1484  */
1485 void cmd_geta(void)
1486 {
1487         struct ctdluser usbuf;
1488
1489         if (CtdlAccessCheck(ac_logged_in)) return;
1490
1491         if (getuserbynumber(&usbuf, CC->room.QRroomaide) == 0) {
1492                 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1493         } else {
1494                 cprintf("%d \n", CIT_OK);
1495         }
1496 }
1497
1498
1499 /* 
1500  * set the room aide for this room
1501  */
1502 void cmd_seta(char *new_ra)
1503 {
1504         struct ctdluser usbuf;
1505         long newu;
1506         char buf[SIZ];
1507         int post_notice;
1508
1509         if (CtdlAccessCheck(ac_room_aide)) return;
1510
1511         if (getuser(&usbuf, new_ra) != 0) {
1512                 newu = (-1L);
1513         } else {
1514                 newu = usbuf.usernum;
1515         }
1516
1517         lgetroom(&CC->room, CC->room.QRname);
1518         post_notice = 0;
1519         if (CC->room.QRroomaide != newu) {
1520                 post_notice = 1;
1521         }
1522         CC->room.QRroomaide = newu;
1523         lputroom(&CC->room);
1524
1525         /*
1526          * We have to post the change notice _after_ writing changes to 
1527          * the room table, otherwise it would deadlock!
1528          */
1529         if (post_notice == 1) {
1530                 if (!IsEmptyStr(usbuf.fullname))
1531                         snprintf(buf, sizeof buf,
1532                                 "%s is now the room aide for \"%s\".\n",
1533                                 usbuf.fullname, CC->room.QRname);
1534                 else
1535                         snprintf(buf, sizeof buf,
1536                                 "There is now no room aide for \"%s\".\n",
1537                                 CC->room.QRname);
1538                 aide_message(buf, "Aide Room Modification");
1539         }
1540         cprintf("%d Ok\n", CIT_OK);
1541 }
1542
1543 /* 
1544  * retrieve info file for this room
1545  */
1546 void cmd_rinf(void)
1547 {
1548         char filename[128];
1549         char buf[SIZ];
1550         FILE *info_fp;
1551
1552         assoc_file_name(filename, sizeof filename, &CC->room, ctdl_info_dir);
1553         info_fp = fopen(filename, "r");
1554
1555         if (info_fp == NULL) {
1556                 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
1557                 return;
1558         }
1559         cprintf("%d Info:\n", LISTING_FOLLOWS);
1560         while (fgets(buf, sizeof buf, info_fp) != NULL) {
1561                 if (!IsEmptyStr(buf))
1562                         buf[strlen(buf) - 1] = 0;
1563                 cprintf("%s\n", buf);
1564         }
1565         cprintf("000\n");
1566         fclose(info_fp);
1567 }
1568
1569 /*
1570  * Asynchronously schedule a room for deletion.  The room will appear
1571  * deleted to the user(s), but it won't actually get purged from the
1572  * database until THE DREADED AUTO-PURGER makes its next run.
1573  */
1574 void schedule_room_for_deletion(struct ctdlroom *qrbuf)
1575 {
1576         char old_name[ROOMNAMELEN];
1577         static int seq = 0;
1578
1579         lprintf(CTDL_NOTICE, "Scheduling room <%s> for deletion\n",
1580                 qrbuf->QRname);
1581
1582         safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1583
1584         getroom(qrbuf, qrbuf->QRname);
1585
1586         /* Turn the room into a private mailbox owned by a user who doesn't
1587          * exist.  This will immediately make the room invisible to everyone,
1588          * and qualify the room for purging.
1589          */
1590         snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1591                 time(NULL),
1592                 ++seq,
1593                 old_name
1594         );
1595         qrbuf->QRflags |= QR_MAILBOX;
1596         time(&qrbuf->QRgen);    /* Use a timestamp as the new generation number  */
1597
1598         putroom(qrbuf);
1599
1600         b_deleteroom(old_name);
1601 }
1602
1603
1604
1605 /*
1606  * Back end processing to delete a room and everything associated with it
1607  * (This one is synchronous and should only get called by THE DREADED
1608  * AUTO-PURGER in serv_expire.c.  All user-facing code should call
1609  * the asynchronous schedule_room_for_deletion() instead.)
1610  */
1611 void delete_room(struct ctdlroom *qrbuf)
1612 {
1613         struct floor flbuf;
1614         char filename[100];
1615         /* TODO: filename magic? does this realy work? */
1616
1617         lprintf(CTDL_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1618
1619         /* Delete the info file */
1620         assoc_file_name(filename, sizeof filename, qrbuf, ctdl_info_dir);
1621         unlink(filename);
1622
1623         /* Delete the image file */
1624         assoc_file_name(filename, sizeof filename, qrbuf, ctdl_image_dir);
1625         unlink(filename);
1626
1627         /* Delete the room's network config file */
1628         assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1629         unlink(filename);
1630
1631         /* Delete the messages in the room
1632          * (Careful: this opens an S_ROOMS critical section!)
1633          */
1634         CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
1635
1636         /* Flag the room record as not in use */
1637         lgetroom(qrbuf, qrbuf->QRname);
1638         qrbuf->QRflags = 0;
1639         lputroom(qrbuf);
1640
1641         /* then decrement the reference count for the floor */
1642         lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1643         flbuf.f_ref_count = flbuf.f_ref_count - 1;
1644         lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1645
1646         /* Delete the room record from the database! */
1647         b_deleteroom(qrbuf->QRname);
1648 }
1649
1650
1651
1652 /*
1653  * Check access control for deleting a room
1654  */
1655 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1656
1657         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1658                 return(0);
1659         }
1660
1661         if (is_noneditable(qr)) {
1662                 return(0);
1663         }
1664
1665         /*
1666          * For mailboxes, check stuff
1667          */
1668         if (qr->QRflags & QR_MAILBOX) {
1669
1670                 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1671
1672                 if (atol(qr->QRname) != CC->user.usernum) {
1673                         return(0);      /* not my room */
1674                 }
1675
1676                 /* Can't delete your Mail> room */
1677                 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1678
1679                 /* Otherwise it's ok */
1680                 return(1);
1681         }
1682
1683         /*
1684          * For normal rooms, just check for aide or room aide status.
1685          */
1686         return(is_room_aide());
1687 }
1688
1689 /*
1690  * aide command: kill the current room
1691  */
1692 void cmd_kill(char *argbuf)
1693 {
1694         char deleted_room_name[ROOMNAMELEN];
1695         char msg[SIZ];
1696         int kill_ok;
1697
1698         kill_ok = extract_int(argbuf, 0);
1699
1700         if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
1701                 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1702                 return;
1703         }
1704         if (kill_ok) {
1705                 if (CC->room.QRflags & QR_MAILBOX) {
1706                         safestrncpy(deleted_room_name, &CC->room.QRname[11], sizeof deleted_room_name);
1707                 }
1708                 else {
1709                         safestrncpy(deleted_room_name, CC->room.QRname, sizeof deleted_room_name);
1710                 }
1711
1712                 /* Do the dirty work */
1713                 schedule_room_for_deletion(&CC->room);
1714
1715                 /* Return to the Lobby */
1716                 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1717
1718                 /* tell the world what we did */
1719                 snprintf(msg, sizeof msg, "The room \"%s\" has been deleted by %s.\n",
1720                          deleted_room_name, CC->curr_user);
1721                 aide_message(msg, "Room Purger Message");
1722                 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1723         } else {
1724                 cprintf("%d ok to delete.\n", CIT_OK);
1725         }
1726 }
1727
1728
1729 /*
1730  * Internal code to create a new room (returns room flags)
1731  *
1732  * Room types:  0=public, 1=guessname, 2=passworded, 3=inv-only,
1733  *              4=mailbox, 5=mailbox, but caller supplies namespace
1734  */
1735 unsigned create_room(char *new_room_name,
1736                      int new_room_type,
1737                      char *new_room_pass,
1738                      int new_room_floor,
1739                      int really_create,
1740                      int avoid_access,
1741                      int new_room_view)
1742 {
1743
1744         struct ctdlroom qrbuf;
1745         struct floor flbuf;
1746         struct visit vbuf;
1747
1748         lprintf(CTDL_DEBUG, "create_room(name=%s, type=%d, view=%d)\n",
1749                 new_room_name, new_room_type, new_room_view);
1750
1751         if (getroom(&qrbuf, new_room_name) == 0) {
1752                 lprintf(CTDL_DEBUG, "%s already exists.\n", new_room_name);
1753                 return(0);
1754         }
1755
1756         memset(&qrbuf, 0, sizeof(struct ctdlroom));
1757         safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1758         qrbuf.QRflags = QR_INUSE;
1759         if (new_room_type > 0)
1760                 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1761         if (new_room_type == 1)
1762                 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1763         if (new_room_type == 2)
1764                 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1765         if ( (new_room_type == 4) || (new_room_type == 5) ) {
1766                 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1767                 /* qrbuf.QRflags2 |= QR2_SUBJECTREQ; */
1768         }
1769
1770         /* If the user is requesting a personal room, set up the room
1771          * name accordingly (prepend the user number)
1772          */
1773         if (new_room_type == 4) {
1774                 MailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1775         }
1776         else {
1777                 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1778         }
1779
1780         /* If the room is private, and the system administrator has elected
1781          * to automatically grant room aide privileges, do so now; otherwise,
1782          * set the room aide to undefined.
1783          */
1784         if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1785                 qrbuf.QRroomaide = CC->user.usernum;
1786         } else {
1787                 qrbuf.QRroomaide = (-1L);
1788         }
1789
1790         /* 
1791          * If the caller is only interested in testing whether this will work,
1792          * return now without creating the room.
1793          */
1794         if (!really_create) return (qrbuf.QRflags);
1795
1796         qrbuf.QRnumber = get_new_room_number();
1797         qrbuf.QRhighest = 0L;   /* No messages in this room yet */
1798         time(&qrbuf.QRgen);     /* Use a timestamp as the generation number */
1799         qrbuf.QRfloor = new_room_floor;
1800         qrbuf.QRdefaultview = new_room_view;
1801
1802         /* save what we just did... */
1803         putroom(&qrbuf);
1804
1805         /* bump the reference count on whatever floor the room is on */
1806         lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1807         flbuf.f_ref_count = flbuf.f_ref_count + 1;
1808         lputfloor(&flbuf, (int) qrbuf.QRfloor);
1809
1810         /* Grant the creator access to the room unless the avoid_access
1811          * parameter was specified.
1812          */
1813         if ( (CC->logged_in) && (avoid_access == 0) ) {
1814                 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1815                 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1816                 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1817                 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1818         }
1819
1820         /* resume our happy day */
1821         return (qrbuf.QRflags);
1822 }
1823
1824
1825 /*
1826  * create a new room
1827  */
1828 void cmd_cre8(char *args)
1829 {
1830         int cre8_ok;
1831         char new_room_name[ROOMNAMELEN];
1832         int new_room_type;
1833         char new_room_pass[32];
1834         int new_room_floor;
1835         int new_room_view;
1836         char *notification_message = NULL;
1837         unsigned newflags;
1838         struct floor *fl;
1839         int avoid_access = 0;
1840
1841         cre8_ok = extract_int(args, 0);
1842         extract_token(new_room_name, args, 1, '|', sizeof new_room_name);
1843         new_room_name[ROOMNAMELEN - 1] = 0;
1844         new_room_type = extract_int(args, 2);
1845         extract_token(new_room_pass, args, 3, '|', sizeof new_room_pass);
1846         avoid_access = extract_int(args, 5);
1847         new_room_view = extract_int(args, 6);
1848         new_room_pass[9] = 0;
1849         new_room_floor = 0;
1850
1851         if ((IsEmptyStr(new_room_name)) && (cre8_ok == 1)) {
1852                 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
1853                 return;
1854         }
1855
1856         if (!strcasecmp(new_room_name, MAILROOM)) {
1857                 cprintf("%d '%s' already exists.\n",
1858                         ERROR + ALREADY_EXISTS, new_room_name);
1859                 return;
1860         }
1861
1862         if (num_parms(args) >= 5) {
1863                 fl = cgetfloor(extract_int(args, 4));
1864                 if (fl == NULL) {
1865                         cprintf("%d Invalid floor number.\n",
1866                                 ERROR + INVALID_FLOOR_OPERATION);
1867                         return;
1868                 }
1869                 else if ((fl->f_flags & F_INUSE) == 0) {
1870                         cprintf("%d Invalid floor number.\n",
1871                                 ERROR + INVALID_FLOOR_OPERATION);
1872                         return;
1873                 } else {
1874                         new_room_floor = extract_int(args, 4);
1875                 }
1876         }
1877
1878         if (CtdlAccessCheck(ac_logged_in)) return;
1879
1880         if (CC->user.axlevel < config.c_createax && !CC->internal_pgm) {
1881                 cprintf("%d You need higher access to create rooms.\n",
1882                         ERROR + HIGHER_ACCESS_REQUIRED);
1883                 return;
1884         }
1885
1886         if ((IsEmptyStr(new_room_name)) && (cre8_ok == 0)) {
1887                 cprintf("%d Ok to create rooms.\n", CIT_OK);
1888                 return;
1889         }
1890
1891         if ((new_room_type < 0) || (new_room_type > 5)) {
1892                 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
1893                 return;
1894         }
1895
1896         if (new_room_type == 5) {
1897                 if (CC->user.axlevel < 6) {
1898                         cprintf("%d Higher access required\n", 
1899                                 ERROR + HIGHER_ACCESS_REQUIRED);
1900                         return;
1901                 }
1902         }
1903
1904         /* Check to make sure the requested room name doesn't already exist */
1905         newflags = create_room(new_room_name,
1906                                 new_room_type, new_room_pass, new_room_floor,
1907                                 0, avoid_access, new_room_view);
1908         if (newflags == 0) {
1909                 cprintf("%d '%s' already exists.\n",
1910                         ERROR + ALREADY_EXISTS, new_room_name);
1911                 return;
1912         }
1913
1914         if (cre8_ok == 0) {
1915                 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
1916                 return;
1917         }
1918
1919         /* If we reach this point, the room needs to be created. */
1920
1921         newflags = create_room(new_room_name,
1922                            new_room_type, new_room_pass, new_room_floor, 1, 0,
1923                            new_room_view);
1924
1925         /* post a message in Aide> describing the new room */
1926         notification_message = malloc(1024);
1927         snprintf(notification_message, 1024,
1928                 "A new room called \"%s\" has been created by %s%s%s%s%s%s\n",
1929                 new_room_name,
1930                 CC->user.fullname,
1931                 ((newflags & QR_MAILBOX) ? " [personal]" : ""),
1932                 ((newflags & QR_PRIVATE) ? " [private]" : ""),
1933                 ((newflags & QR_GUESSNAME) ? " [hidden]" : ""),
1934                 ((newflags & QR_PASSWORDED) ? " Password: " : ""),
1935                 ((newflags & QR_PASSWORDED) ? new_room_pass : "")
1936         );
1937         aide_message(notification_message, "Room Creation Message");
1938         free(notification_message);
1939
1940         cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
1941 }
1942
1943
1944
1945 void cmd_einf(char *ok)
1946 {                               /* enter info file for current room */
1947         FILE *fp;
1948         char infofilename[SIZ];
1949         char buf[SIZ];
1950
1951         unbuffer_output();
1952
1953         if (CtdlAccessCheck(ac_room_aide)) return;
1954
1955         if (atoi(ok) == 0) {
1956                 cprintf("%d Ok.\n", CIT_OK);
1957                 return;
1958         }
1959         assoc_file_name(infofilename, sizeof infofilename, &CC->room, ctdl_info_dir);
1960         lprintf(CTDL_DEBUG, "opening\n");
1961         fp = fopen(infofilename, "w");
1962         lprintf(CTDL_DEBUG, "checking\n");
1963         if (fp == NULL) {
1964                 cprintf("%d Cannot open %s: %s\n",
1965                   ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1966                 return;
1967         }
1968         cprintf("%d Send info...\n", SEND_LISTING);
1969
1970         do {
1971                 client_getln(buf, sizeof buf);
1972                 if (strcmp(buf, "000"))
1973                         fprintf(fp, "%s\n", buf);
1974         } while (strcmp(buf, "000"));
1975         fclose(fp);
1976
1977         /* now update the room index so people will see our new info */
1978         lgetroom(&CC->room, CC->room.QRname);           /* lock so no one steps on us */
1979         CC->room.QRinfo = CC->room.QRhighest + 1L;
1980         lputroom(&CC->room);
1981 }
1982
1983
1984 /* 
1985  * cmd_lflr()   -  List all known floors
1986  */
1987 void cmd_lflr(void)
1988 {
1989         int a;
1990         struct floor flbuf;
1991
1992         if (CtdlAccessCheck(ac_logged_in)) return;
1993
1994         cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1995
1996         for (a = 0; a < MAXFLOORS; ++a) {
1997                 getfloor(&flbuf, a);
1998                 if (flbuf.f_flags & F_INUSE) {
1999                         cprintf("%d|%s|%d\n",
2000                                 a,
2001                                 flbuf.f_name,
2002                                 flbuf.f_ref_count);
2003                 }
2004         }
2005         cprintf("000\n");
2006 }
2007
2008
2009
2010 /*
2011  * create a new floor
2012  */
2013 void cmd_cflr(char *argbuf)
2014 {
2015         char new_floor_name[256];
2016         struct floor flbuf;
2017         int cflr_ok;
2018         int free_slot = (-1);
2019         int a;
2020
2021         extract_token(new_floor_name, argbuf, 0, '|', sizeof new_floor_name);
2022         cflr_ok = extract_int(argbuf, 1);
2023
2024         if (CtdlAccessCheck(ac_aide)) return;
2025
2026         if (IsEmptyStr(new_floor_name)) {
2027                 cprintf("%d Blank floor name not allowed.\n",
2028                         ERROR + ILLEGAL_VALUE);
2029                 return;
2030         }
2031
2032         for (a = 0; a < MAXFLOORS; ++a) {
2033                 getfloor(&flbuf, a);
2034
2035                 /* note any free slots while we're scanning... */
2036                 if (((flbuf.f_flags & F_INUSE) == 0)
2037                     && (free_slot < 0))
2038                         free_slot = a;
2039
2040                 /* check to see if it already exists */
2041                 if ((!strcasecmp(flbuf.f_name, new_floor_name))
2042                     && (flbuf.f_flags & F_INUSE)) {
2043                         cprintf("%d Floor '%s' already exists.\n",
2044                                 ERROR + ALREADY_EXISTS,
2045                                 flbuf.f_name);
2046                         return;
2047                 }
2048         }
2049
2050         if (free_slot < 0) {
2051                 cprintf("%d There is no space available for a new floor.\n",
2052                         ERROR + INVALID_FLOOR_OPERATION);
2053                 return;
2054         }
2055         if (cflr_ok == 0) {
2056                 cprintf("%d ok to create...\n", CIT_OK);
2057                 return;
2058         }
2059         lgetfloor(&flbuf, free_slot);
2060         flbuf.f_flags = F_INUSE;
2061         flbuf.f_ref_count = 0;
2062         safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
2063         lputfloor(&flbuf, free_slot);
2064         cprintf("%d %d\n", CIT_OK, free_slot);
2065 }
2066
2067
2068
2069 /*
2070  * delete a floor
2071  */
2072 void cmd_kflr(char *argbuf)
2073 {
2074         struct floor flbuf;
2075         int floor_to_delete;
2076         int kflr_ok;
2077         int delete_ok;
2078
2079         floor_to_delete = extract_int(argbuf, 0);
2080         kflr_ok = extract_int(argbuf, 1);
2081
2082         if (CtdlAccessCheck(ac_aide)) return;
2083
2084         lgetfloor(&flbuf, floor_to_delete);
2085
2086         delete_ok = 1;
2087         if ((flbuf.f_flags & F_INUSE) == 0) {
2088                 cprintf("%d Floor %d not in use.\n",
2089                         ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
2090                 delete_ok = 0;
2091         } else {
2092                 if (flbuf.f_ref_count != 0) {
2093                         cprintf("%d Cannot delete; floor contains %d rooms.\n",
2094                                 ERROR + INVALID_FLOOR_OPERATION,
2095                                 flbuf.f_ref_count);
2096                         delete_ok = 0;
2097                 } else {
2098                         if (kflr_ok == 1) {
2099                                 cprintf("%d Ok\n", CIT_OK);
2100                         } else {
2101                                 cprintf("%d Ok to delete...\n", CIT_OK);
2102                         }
2103
2104                 }
2105
2106         }
2107
2108         if ((delete_ok == 1) && (kflr_ok == 1))
2109                 flbuf.f_flags = 0;
2110         lputfloor(&flbuf, floor_to_delete);
2111 }
2112
2113 /*
2114  * edit a floor
2115  */
2116 void cmd_eflr(char *argbuf)
2117 {
2118         struct floor flbuf;
2119         int floor_num;
2120         int np;
2121
2122         np = num_parms(argbuf);
2123         if (np < 1) {
2124                 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
2125                 return;
2126         }
2127
2128         if (CtdlAccessCheck(ac_aide)) return;
2129
2130         floor_num = extract_int(argbuf, 0);
2131         lgetfloor(&flbuf, floor_num);
2132         if ((flbuf.f_flags & F_INUSE) == 0) {
2133                 lputfloor(&flbuf, floor_num);
2134                 cprintf("%d Floor %d is not in use.\n",
2135                         ERROR + INVALID_FLOOR_OPERATION, floor_num);
2136                 return;
2137         }
2138         if (np >= 2)
2139                 extract_token(flbuf.f_name, argbuf, 1, '|', sizeof flbuf.f_name);
2140         lputfloor(&flbuf, floor_num);
2141
2142         cprintf("%d Ok\n", CIT_OK);
2143 }