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