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