* Full text indexer is now switchable on/off
[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 = malloc(cdbfr->len);
828                 memcpy(msglist, cdbfr->ptr, cdbfr->len);
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         if (!strcasecmp(towhere, "_BASEROOM_"))
936                 safestrncpy(towhere, config.c_baseroom, sizeof towhere);
937
938         if (!strcasecmp(towhere, "_MAIL_"))
939                 safestrncpy(towhere, MAILROOM, sizeof towhere);
940
941         if (!strcasecmp(towhere, "_BITBUCKET_"))
942                 safestrncpy(towhere, config.c_twitroom, sizeof towhere);
943
944
945         /* First try a regular match */
946         c = getroom(&QRscratch, towhere);
947
948         /* Then try a mailbox name match */
949         if (c != 0) {
950                 MailboxName(augmented_roomname, sizeof augmented_roomname,
951                             &CC->user, towhere);
952                 c = getroom(&QRscratch, augmented_roomname);
953                 if (c == 0)
954                         safestrncpy(towhere, augmented_roomname, sizeof towhere);
955         }
956
957         /* And if the room was found... */
958         if (c == 0) {
959
960                 /* Let internal programs go directly to any room. */
961                 if (CC->internal_pgm) {
962                         memcpy(&CC->room, &QRscratch,
963                                 sizeof(struct ctdlroom));
964                         usergoto(NULL, 1, transiently, NULL, NULL);
965                         return;
966                 }
967
968                 /* See if there is an existing user/room relationship */
969                 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
970
971                 /* normal clients have to pass through security */
972                 if (ra & UA_GOTOALLOWED) {
973                         ok = 1;
974                 }
975
976                 if (ok == 1) {
977                         if ((QRscratch.QRflags & QR_MAILBOX) &&
978                             ((ra & UA_GOTOALLOWED))) {
979                                 memcpy(&CC->room, &QRscratch,
980                                         sizeof(struct ctdlroom));
981                                 usergoto(NULL, 1, transiently, NULL, NULL);
982                                 return;
983                         } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
984                             ((ra & UA_KNOWN) == 0) &&
985                             (strcasecmp(QRscratch.QRpasswd, password)) &&
986                             (CC->user.axlevel < 6)
987                             ) {
988                                 cprintf("%d wrong or missing passwd\n",
989                                         ERROR + PASSWORD_REQUIRED);
990                                 return;
991                         } else if ((QRscratch.QRflags & QR_PRIVATE) &&
992                                    ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
993                                    ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
994                                    ((ra & UA_KNOWN) == 0) &&
995                                    (CC->user.axlevel < 6)
996                                   ) {
997                                 lprintf(CTDL_DEBUG, "Failed to acquire private room\n");
998                         } else {
999                                 memcpy(&CC->room, &QRscratch,
1000                                         sizeof(struct ctdlroom));
1001                                 usergoto(NULL, 1, transiently, NULL, NULL);
1002                                 return;
1003                         }
1004                 }
1005         }
1006
1007         cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
1008 }
1009
1010
1011 void cmd_whok(void)
1012 {
1013         struct ctdluser temp;
1014         struct cdbdata *cdbus;
1015         int ra;
1016
1017         getuser(&CC->user, CC->curr_user);
1018
1019         /*
1020          * This command is only allowed by aides, room aides,
1021          * and room namespace owners
1022          */
1023         if (is_room_aide()
1024            || (atol(CC->room.QRname) == CC->user.usernum) ) {
1025                 /* access granted */
1026         }
1027         else {
1028                 /* access denied */
1029                 cprintf("%d Higher access or room ownership required.\n",
1030                         ERROR + HIGHER_ACCESS_REQUIRED);
1031                 return;
1032         }
1033
1034         cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
1035         cdb_rewind(CDB_USERS);
1036         while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1037                 memset(&temp, 0, sizeof temp);
1038                 memcpy(&temp, cdbus->ptr, sizeof temp);
1039                 cdb_free(cdbus);
1040
1041                 CtdlRoomAccess(&CC->room, &temp, &ra, NULL);
1042                 if ((CC->room.QRflags & QR_INUSE)
1043                     && (ra & UA_KNOWN)
1044                     )
1045                         cprintf("%s\n", temp.fullname);
1046         }
1047         cprintf("000\n");
1048 }
1049
1050
1051 /*
1052  * RDIR command for room directory
1053  */
1054 void cmd_rdir(void)
1055 {
1056         char buf[256];
1057         char flnm[256];
1058         char comment[256];
1059         FILE *ls, *fd;
1060         struct stat statbuf;
1061         char tempfilename[PATH_MAX];
1062
1063         if (CtdlAccessCheck(ac_logged_in)) return;
1064         tmpnam(tempfilename);
1065         
1066         getroom(&CC->room, CC->room.QRname);
1067         getuser(&CC->user, CC->curr_user);
1068
1069         if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
1070                 cprintf("%d not here.\n", ERROR + NOT_HERE);
1071                 return;
1072         }
1073         if (((CC->room.QRflags & QR_VISDIR) == 0)
1074             && (CC->user.axlevel < 6)
1075             && (CC->user.usernum != CC->room.QRroomaide)) {
1076                 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1077                 return;
1078         }
1079         cprintf("%d %s|%s/files/%s\n",
1080         LISTING_FOLLOWS, config.c_fqdn, CTDLDIR, CC->room.QRdirname);
1081
1082         snprintf(buf, sizeof buf, "ls %s/files/%s  >%s 2> /dev/null",
1083                 CTDLDIR, CC->room.QRdirname, tempfilename);
1084         system(buf);
1085
1086         snprintf(buf, sizeof buf, "%s/files/%s/filedir", CTDLDIR, CC->room.QRdirname);
1087         fd = fopen(buf, "r");
1088         if (fd == NULL)
1089                 fd = fopen("/dev/null", "r");
1090
1091         ls = fopen(tempfilename, "r");
1092         while (fgets(flnm, sizeof flnm, ls) != NULL) {
1093                 flnm[strlen(flnm) - 1] = 0;
1094                 if (strcasecmp(flnm, "filedir")) {
1095                         snprintf(buf, sizeof buf, "%s/files/%s/%s",
1096                                 CTDLDIR, CC->room.QRdirname, flnm);
1097                         stat(buf, &statbuf);
1098                         safestrncpy(comment, "", sizeof comment);
1099                         fseek(fd, 0L, 0);
1100                         while ((fgets(buf, sizeof buf, fd) != NULL)
1101                                && (strlen(comment) == 0)) {
1102                                 buf[strlen(buf) - 1] = 0;
1103                                 if ((!strncasecmp(buf, flnm, strlen(flnm)))
1104                                     && (buf[strlen(flnm)] == ' '))
1105                                         safestrncpy(comment,
1106                                             &buf[strlen(flnm) + 1],
1107                                             sizeof comment);
1108                         }
1109                         cprintf("%s|%ld|%s\n", flnm, (long)statbuf.st_size, comment);
1110                 }
1111         }
1112         fclose(ls);
1113         fclose(fd);
1114         unlink(tempfilename);
1115
1116         cprintf("000\n");
1117 }
1118
1119 /*
1120  * get room parameters (aide or room aide command)
1121  */
1122 void cmd_getr(void)
1123 {
1124         if (CtdlAccessCheck(ac_room_aide)) return;
1125
1126         getroom(&CC->room, CC->room.QRname);
1127         cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1128                 CIT_OK,
1129                 CtdlCheckExpress(),
1130
1131                 ((CC->room.QRflags & QR_MAILBOX) ?
1132                         &CC->room.QRname[11] : CC->room.QRname),
1133
1134                 ((CC->room.QRflags & QR_PASSWORDED) ?
1135                         CC->room.QRpasswd : ""),
1136
1137                 ((CC->room.QRflags & QR_DIRECTORY) ?
1138                         CC->room.QRdirname : ""),
1139
1140                 CC->room.QRflags,
1141                 (int) CC->room.QRfloor,
1142                 (int) CC->room.QRorder,
1143
1144                 CC->room.QRdefaultview,
1145                 CC->room.QRflags2
1146                 );
1147 }
1148
1149
1150 /*
1151  * Back end function to rename a room.
1152  * You can also specify which floor to move the room to, or specify -1 to
1153  * keep the room on the same floor it was on.
1154  *
1155  * If you are renaming a mailbox room, you must supply the namespace prefix
1156  * in *at least* the old name!
1157  */
1158 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1159         int old_floor = 0;
1160         struct ctdlroom qrbuf;
1161         struct ctdlroom qrtmp;
1162         int ret = 0;
1163         struct floor *fl;
1164         struct floor flbuf;
1165         long owner = 0L;
1166         char actual_old_name[ROOMNAMELEN];
1167
1168         lprintf(CTDL_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1169                 old_name, new_name, new_floor);
1170
1171         if (new_floor >= 0) {
1172                 fl = cgetfloor(new_floor);
1173                 if ((fl->f_flags & F_INUSE) == 0) {
1174                         return(crr_invalid_floor);
1175                 }
1176         }
1177
1178         begin_critical_section(S_ROOMS);
1179
1180         if ( (getroom(&qrtmp, new_name) == 0) 
1181            && (strcasecmp(new_name, old_name)) ) {
1182                 ret = crr_already_exists;
1183         }
1184
1185         else if (getroom(&qrbuf, old_name) != 0) {
1186                 ret = crr_room_not_found;
1187         }
1188
1189         else if ( (CC->user.axlevel < 6)
1190                   && (CC->user.usernum != qrbuf.QRroomaide)
1191                   && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) )  {
1192                 ret = crr_access_denied;
1193         }
1194
1195         else if (is_noneditable(&qrbuf)) {
1196                 ret = crr_noneditable;
1197         }
1198
1199         else {
1200                 /* Rename it */
1201                 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1202                 if (qrbuf.QRflags & QR_MAILBOX) {
1203                         owner = atol(qrbuf.QRname);
1204                 }
1205                 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1206                         snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1207                                         "%010ld.%s", owner, new_name);
1208                 }
1209                 else {
1210                         safestrncpy(qrbuf.QRname, new_name,
1211                                                 sizeof(qrbuf.QRname));
1212                 }
1213
1214                 /* Reject change of floor for baseroom/aideroom */
1215                 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1216                     !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1217                         new_floor = 0;
1218                 }
1219
1220                 /* Take care of floor stuff */
1221                 old_floor = qrbuf.QRfloor;
1222                 if (new_floor < 0) {
1223                         new_floor = old_floor;
1224                 }
1225                 qrbuf.QRfloor = new_floor;
1226                 putroom(&qrbuf);
1227
1228                 begin_critical_section(S_CONFIG);
1229         
1230                 /* If baseroom/aideroom name changes, update config */
1231                 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1232                         safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1233                         put_config();
1234                 }
1235                 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1236                         safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1237                         put_config();
1238                 }
1239         
1240                 end_critical_section(S_CONFIG);
1241         
1242                 /* If the room name changed, then there are now two room
1243                  * records, so we have to delete the old one.
1244                  */
1245                 if (strcasecmp(new_name, old_name)) {
1246                         b_deleteroom(actual_old_name);
1247                 }
1248
1249                 ret = crr_ok;
1250         }
1251
1252         end_critical_section(S_ROOMS);
1253
1254         /* Adjust the floor reference counts if necessary */
1255         if (new_floor != old_floor) {
1256                 lgetfloor(&flbuf, old_floor);
1257                 --flbuf.f_ref_count;
1258                 lputfloor(&flbuf, old_floor);
1259                 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1260                 lgetfloor(&flbuf, new_floor);
1261                 ++flbuf.f_ref_count;
1262                 lputfloor(&flbuf, new_floor);
1263                 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1264         }
1265
1266         /* ...and everybody say "YATTA!" */     
1267         return(ret);
1268 }
1269
1270
1271 /*
1272  * set room parameters (aide or room aide command)
1273  */
1274 void cmd_setr(char *args)
1275 {
1276         char buf[256];
1277         int new_order = 0;
1278         int r;
1279         int new_floor;
1280         char new_name[ROOMNAMELEN];
1281
1282         if (CtdlAccessCheck(ac_logged_in)) return;
1283
1284         if (num_parms(args) >= 6) {
1285                 new_floor = extract_int(args, 5);
1286         } else {
1287                 new_floor = (-1);       /* don't change the floor */
1288         }
1289
1290         /* When is a new name more than just a new name?  When the old name
1291          * has a namespace prefix.
1292          */
1293         if (CC->room.QRflags & QR_MAILBOX) {
1294                 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
1295         } else {
1296                 safestrncpy(new_name, "", sizeof new_name);
1297         }
1298         extract_token(&new_name[strlen(new_name)], args, 0, '|', (sizeof new_name - strlen(new_name)));
1299
1300         r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
1301
1302         if (r == crr_room_not_found) {
1303                 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
1304         } else if (r == crr_already_exists) {
1305                 cprintf("%d '%s' already exists.\n",
1306                         ERROR + ALREADY_EXISTS, new_name);
1307         } else if (r == crr_noneditable) {
1308                 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
1309         } else if (r == crr_invalid_floor) {
1310                 cprintf("%d Target floor does not exist.\n",
1311                         ERROR + INVALID_FLOOR_OPERATION);
1312         } else if (r == crr_access_denied) {
1313                 cprintf("%d You do not have permission to edit '%s'\n",
1314                         ERROR + HIGHER_ACCESS_REQUIRED,
1315                         CC->room.QRname);
1316         } else if (r != crr_ok) {
1317                 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1318                         ERROR + INTERNAL_ERROR, r);
1319         }
1320
1321         if (r != crr_ok) {
1322                 return;
1323         }
1324
1325         getroom(&CC->room, new_name);
1326
1327         /* Now we have to do a bunch of other stuff */
1328
1329         if (num_parms(args) >= 7) {
1330                 new_order = extract_int(args, 6);
1331                 if (new_order < 1)
1332                         new_order = 1;
1333                 if (new_order > 127)
1334                         new_order = 127;
1335         }
1336
1337         lgetroom(&CC->room, CC->room.QRname);
1338
1339         /* Directory room */
1340         extract_token(buf, args, 2, '|', sizeof buf);
1341         buf[15] = 0;
1342         safestrncpy(CC->room.QRdirname, buf,
1343                 sizeof CC->room.QRdirname);
1344
1345         /* Default view */
1346         if (num_parms(args) >= 8) {
1347                 CC->room.QRdefaultview = extract_int(args, 7);
1348         }
1349
1350         /* Second set of flags */
1351         if (num_parms(args) >= 9) {
1352                 CC->room.QRflags2 = extract_int(args, 8);
1353         }
1354
1355         /* Misc. flags */
1356         CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
1357         /* Clean up a client boo-boo: if the client set the room to
1358          * guess-name or passworded, ensure that the private flag is
1359          * also set.
1360          */
1361         if ((CC->room.QRflags & QR_GUESSNAME)
1362             || (CC->room.QRflags & QR_PASSWORDED))
1363                 CC->room.QRflags |= QR_PRIVATE;
1364
1365         /* Some changes can't apply to BASEROOM */
1366         if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1367                          ROOMNAMELEN)) {
1368                 CC->room.QRorder = 0;
1369                 CC->room.QRpasswd[0] = '\0';
1370                 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1371                         QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1372                 CC->room.QRflags |= QR_PERMANENT;
1373         } else {        
1374                 /* March order (doesn't apply to AIDEROOM) */
1375                 if (num_parms(args) >= 7)
1376                         CC->room.QRorder = (char) new_order;
1377                 /* Room password */
1378                 extract_token(buf, args, 1, '|', sizeof buf);
1379                 buf[10] = 0;
1380                 safestrncpy(CC->room.QRpasswd, buf,
1381                             sizeof CC->room.QRpasswd);
1382                 /* Kick everyone out if the client requested it
1383                  * (by changing the room's generation number)
1384                  */
1385                 if (extract_int(args, 4)) {
1386                         time(&CC->room.QRgen);
1387                 }
1388         }
1389         /* Some changes can't apply to AIDEROOM */
1390         if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1391                          ROOMNAMELEN)) {
1392                 CC->room.QRorder = 0;
1393                 CC->room.QRflags &= ~QR_MAILBOX;
1394                 CC->room.QRflags |= QR_PERMANENT;
1395         }
1396
1397         /* Write the room record back to disk */
1398         lputroom(&CC->room);
1399
1400         /* Create a room directory if necessary */
1401         if (CC->room.QRflags & QR_DIRECTORY) {
1402                 snprintf(buf, sizeof buf, "./files/%s", CC->room.QRdirname);
1403                 mkdir(buf, 0755);
1404         }
1405         snprintf(buf, sizeof buf, "%s> edited by %s\n", CC->room.QRname, CC->curr_user);
1406         aide_message(buf);
1407         cprintf("%d Ok\n", CIT_OK);
1408 }
1409
1410
1411
1412 /* 
1413  * get the name of the room aide for this room
1414  */
1415 void cmd_geta(void)
1416 {
1417         struct ctdluser usbuf;
1418
1419         if (CtdlAccessCheck(ac_logged_in)) return;
1420
1421         if (getuserbynumber(&usbuf, CC->room.QRroomaide) == 0) {
1422                 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1423         } else {
1424                 cprintf("%d \n", CIT_OK);
1425         }
1426 }
1427
1428
1429 /* 
1430  * set the room aide for this room
1431  */
1432 void cmd_seta(char *new_ra)
1433 {
1434         struct ctdluser usbuf;
1435         long newu;
1436         char buf[SIZ];
1437         int post_notice;
1438
1439         if (CtdlAccessCheck(ac_room_aide)) return;
1440
1441         if (getuser(&usbuf, new_ra) != 0) {
1442                 newu = (-1L);
1443         } else {
1444                 newu = usbuf.usernum;
1445         }
1446
1447         lgetroom(&CC->room, CC->room.QRname);
1448         post_notice = 0;
1449         if (CC->room.QRroomaide != newu) {
1450                 post_notice = 1;
1451         }
1452         CC->room.QRroomaide = newu;
1453         lputroom(&CC->room);
1454
1455         /*
1456          * We have to post the change notice _after_ writing changes to 
1457          * the room table, otherwise it would deadlock!
1458          */
1459         if (post_notice == 1) {
1460                 if (strlen(usbuf.fullname) > 0)
1461                         snprintf(buf, sizeof buf,
1462                                 "%s is now room aide for %s>\n",
1463                                 usbuf.fullname, CC->room.QRname);
1464                 else
1465                         snprintf(buf, sizeof buf,
1466                                 "There is now no room aide for %s>\n",
1467                                 CC->room.QRname);
1468                 aide_message(buf);
1469         }
1470         cprintf("%d Ok\n", CIT_OK);
1471 }
1472
1473 /*
1474  * Generate an associated file name for a room
1475  */
1476 void assoc_file_name(char *buf, size_t n,
1477                      struct ctdlroom *qrbuf, const char *prefix)
1478 {
1479         snprintf(buf, n, "./%s/%ld", prefix, qrbuf->QRnumber);
1480 }
1481
1482 /* 
1483  * retrieve info file for this room
1484  */
1485 void cmd_rinf(void)
1486 {
1487         char filename[128];
1488         char buf[SIZ];
1489         FILE *info_fp;
1490
1491         assoc_file_name(filename, sizeof filename, &CC->room, "info");
1492         info_fp = fopen(filename, "r");
1493
1494         if (info_fp == NULL) {
1495                 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
1496                 return;
1497         }
1498         cprintf("%d Info:\n", LISTING_FOLLOWS);
1499         while (fgets(buf, sizeof buf, info_fp) != NULL) {
1500                 if (strlen(buf) > 0)
1501                         buf[strlen(buf) - 1] = 0;
1502                 cprintf("%s\n", buf);
1503         }
1504         cprintf("000\n");
1505         fclose(info_fp);
1506 }
1507
1508 /*
1509  * Asynchronously schedule a room for deletion.  The room will appear
1510  * deleted to the user(s), but it won't actually get purged from the
1511  * database until THE DREADED AUTO-PURGER makes its next run.
1512  */
1513 void schedule_room_for_deletion(struct ctdlroom *qrbuf)
1514 {
1515         char old_name[ROOMNAMELEN];
1516         static int seq = 0;
1517
1518         lprintf(CTDL_NOTICE, "Scheduling room <%s> for deletion\n",
1519                 qrbuf->QRname);
1520
1521         safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1522
1523         getroom(qrbuf, qrbuf->QRname);
1524
1525         /* Turn the room into a private mailbox owned by a user who doesn't
1526          * exist.  This will immediately make the room invisible to everyone,
1527          * and qualify the room for purging.
1528          */
1529         snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1530                 time(NULL),
1531                 ++seq,
1532                 old_name
1533         );
1534         qrbuf->QRflags |= QR_MAILBOX;
1535         time(&qrbuf->QRgen);    /* Use a timestamp as the new generation number  */
1536
1537         putroom(qrbuf);
1538
1539         b_deleteroom(old_name);
1540 }
1541
1542
1543
1544 /*
1545  * Back end processing to delete a room and everything associated with it
1546  * (This one is synchronous and should only get called by THE DREADED
1547  * AUTO-PURGER in serv_expire.c.  All user-facing code should call
1548  * the asynchronous schedule_room_for_deletion() instead.)
1549  */
1550 void delete_room(struct ctdlroom *qrbuf)
1551 {
1552         struct floor flbuf;
1553         char filename[100];
1554
1555         lprintf(CTDL_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1556
1557         /* Delete the info file */
1558         assoc_file_name(filename, sizeof filename, qrbuf, "info");
1559         unlink(filename);
1560
1561         /* Delete the image file */
1562         assoc_file_name(filename, sizeof filename, qrbuf, "images");
1563         unlink(filename);
1564
1565         /* Delete the room's network config file */
1566         assoc_file_name(filename, sizeof filename, qrbuf, "netconfigs");
1567         unlink(filename);
1568
1569         /* Delete the messages in the room
1570          * (Careful: this opens an S_ROOMS critical section!)
1571          */
1572         CtdlDeleteMessages(qrbuf->QRname, 0L, "");
1573
1574         /* Flag the room record as not in use */
1575         lgetroom(qrbuf, qrbuf->QRname);
1576         qrbuf->QRflags = 0;
1577         lputroom(qrbuf);
1578
1579         /* then decrement the reference count for the floor */
1580         lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1581         flbuf.f_ref_count = flbuf.f_ref_count - 1;
1582         lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1583
1584         /* Delete the room record from the database! */
1585         b_deleteroom(qrbuf->QRname);
1586 }
1587
1588
1589
1590 /*
1591  * Check access control for deleting a room
1592  */
1593 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1594
1595         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1596                 return(0);
1597         }
1598
1599         if (is_noneditable(qr)) {
1600                 return(0);
1601         }
1602
1603         /*
1604          * For mailboxes, check stuff
1605          */
1606         if (qr->QRflags & QR_MAILBOX) {
1607
1608                 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1609
1610                 if (atol(qr->QRname) != CC->user.usernum) {
1611                         return(0);      /* not my room */
1612                 }
1613
1614                 /* Can't delete your Mail> room */
1615                 if (!strcasecmp(&qr->QRname[12], MAILROOM)) return(0);
1616
1617                 /* Otherwise it's ok */
1618                 return(1);
1619         }
1620
1621         /*
1622          * For normal rooms, just check for aide or room aide status.
1623          */
1624         return(is_room_aide());
1625 }
1626
1627 /*
1628  * aide command: kill the current room
1629  */
1630 void cmd_kill(char *argbuf)
1631 {
1632         char deleted_room_name[ROOMNAMELEN];
1633         char msg[SIZ];
1634         int kill_ok;
1635
1636         kill_ok = extract_int(argbuf, 0);
1637
1638         if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
1639                 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1640                 return;
1641         }
1642         if (kill_ok) {
1643                 if (CC->room.QRflags & QR_MAILBOX) {
1644                         safestrncpy(deleted_room_name, &CC->room.QRname[11], sizeof deleted_room_name);
1645                 }
1646                 else {
1647                         safestrncpy(deleted_room_name, CC->room.QRname, sizeof deleted_room_name);
1648                 }
1649
1650                 /* Do the dirty work */
1651                 schedule_room_for_deletion(&CC->room);
1652
1653                 /* Return to the Lobby */
1654                 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1655
1656                 /* tell the world what we did */
1657                 snprintf(msg, sizeof msg, "%s> killed by %s\n",
1658                          deleted_room_name, CC->curr_user);
1659                 aide_message(msg);
1660                 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1661         } else {
1662                 cprintf("%d ok to delete.\n", CIT_OK);
1663         }
1664 }
1665
1666
1667 /*
1668  * Internal code to create a new room (returns room flags)
1669  *
1670  * Room types:  0=public, 1=guessname, 2=passworded, 3=inv-only,
1671  *              4=mailbox, 5=mailbox, but caller supplies namespace
1672  */
1673 unsigned create_room(char *new_room_name,
1674                      int new_room_type,
1675                      char *new_room_pass,
1676                      int new_room_floor,
1677                      int really_create,
1678                      int avoid_access,
1679                      int new_room_view)
1680 {
1681
1682         struct ctdlroom qrbuf;
1683         struct floor flbuf;
1684         struct visit vbuf;
1685
1686         lprintf(CTDL_DEBUG, "create_room(name=%s, type=%d, view=%d)\n",
1687                 new_room_name, new_room_type, new_room_view);
1688
1689         if (getroom(&qrbuf, new_room_name) == 0) {
1690                 lprintf(CTDL_DEBUG, "%s already exists.\n", new_room_name);
1691                 return(0);
1692         }
1693
1694         memset(&qrbuf, 0, sizeof(struct ctdlroom));
1695         safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1696         qrbuf.QRflags = QR_INUSE;
1697         if (new_room_type > 0)
1698                 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1699         if (new_room_type == 1)
1700                 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1701         if (new_room_type == 2)
1702                 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1703         if ( (new_room_type == 4) || (new_room_type == 5) )
1704                 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1705
1706         /* If the user is requesting a personal room, set up the room
1707          * name accordingly (prepend the user number)
1708          */
1709         if (new_room_type == 4) {
1710                 MailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1711         }
1712         else {
1713                 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1714         }
1715
1716         /* If the room is private, and the system administrator has elected
1717          * to automatically grant room aide privileges, do so now; otherwise,
1718          * set the room aide to undefined.
1719          */
1720         if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1721                 qrbuf.QRroomaide = CC->user.usernum;
1722         } else {
1723                 qrbuf.QRroomaide = (-1L);
1724         }
1725
1726         /* 
1727          * If the caller is only interested in testing whether this will work,
1728          * return now without creating the room.
1729          */
1730         if (!really_create) return (qrbuf.QRflags);
1731
1732         qrbuf.QRnumber = get_new_room_number();
1733         qrbuf.QRhighest = 0L;   /* No messages in this room yet */
1734         time(&qrbuf.QRgen);     /* Use a timestamp as the generation number */
1735         qrbuf.QRfloor = new_room_floor;
1736         qrbuf.QRdefaultview = new_room_view;
1737
1738         /* save what we just did... */
1739         putroom(&qrbuf);
1740
1741         /* bump the reference count on whatever floor the room is on */
1742         lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1743         flbuf.f_ref_count = flbuf.f_ref_count + 1;
1744         lputfloor(&flbuf, (int) qrbuf.QRfloor);
1745
1746         /* Grant the creator access to the room unless the avoid_access
1747          * parameter was specified.
1748          */
1749         if (avoid_access == 0) {
1750                 lgetuser(&CC->user, CC->curr_user);
1751                 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1752                 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1753                 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1754                 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1755                 lputuser(&CC->user);
1756         }
1757
1758         /* resume our happy day */
1759         return (qrbuf.QRflags);
1760 }
1761
1762
1763 /*
1764  * create a new room
1765  */
1766 void cmd_cre8(char *args)
1767 {
1768         int cre8_ok;
1769         char new_room_name[ROOMNAMELEN];
1770         int new_room_type;
1771         char new_room_pass[32];
1772         int new_room_floor;
1773         int new_room_view;
1774         char *notification_message = NULL;
1775         unsigned newflags;
1776         struct floor *fl;
1777         int avoid_access = 0;
1778
1779         cre8_ok = extract_int(args, 0);
1780         extract_token(new_room_name, args, 1, '|', sizeof new_room_name);
1781         new_room_name[ROOMNAMELEN - 1] = 0;
1782         new_room_type = extract_int(args, 2);
1783         extract_token(new_room_pass, args, 3, '|', sizeof new_room_pass);
1784         avoid_access = extract_int(args, 5);
1785         new_room_view = extract_int(args, 6);
1786         new_room_pass[9] = 0;
1787         new_room_floor = 0;
1788
1789         if ((strlen(new_room_name) == 0) && (cre8_ok == 1)) {
1790                 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
1791                 return;
1792         }
1793
1794         if (!strcasecmp(new_room_name, MAILROOM)) {
1795                 cprintf("%d '%s' already exists.\n",
1796                         ERROR + ALREADY_EXISTS, new_room_name);
1797                 return;
1798         }
1799
1800         if (num_parms(args) >= 5) {
1801                 fl = cgetfloor(extract_int(args, 4));
1802                 if (fl == NULL) {
1803                         cprintf("%d Invalid floor number.\n",
1804                                 ERROR + INVALID_FLOOR_OPERATION);
1805                         return;
1806                 }
1807                 else if ((fl->f_flags & F_INUSE) == 0) {
1808                         cprintf("%d Invalid floor number.\n",
1809                                 ERROR + INVALID_FLOOR_OPERATION);
1810                         return;
1811                 } else {
1812                         new_room_floor = extract_int(args, 4);
1813                 }
1814         }
1815
1816         if (CtdlAccessCheck(ac_logged_in)) return;
1817
1818         if (CC->user.axlevel < config.c_createax) {
1819                 cprintf("%d You need higher access to create rooms.\n",
1820                         ERROR + HIGHER_ACCESS_REQUIRED);
1821                 return;
1822         }
1823
1824         if ((strlen(new_room_name) == 0) && (cre8_ok == 0)) {
1825                 cprintf("%d Ok to create rooms.\n", CIT_OK);
1826                 return;
1827         }
1828
1829         if ((new_room_type < 0) || (new_room_type > 5)) {
1830                 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
1831                 return;
1832         }
1833
1834         if (new_room_type == 5) {
1835                 if (CC->user.axlevel < 6) {
1836                         cprintf("%d Higher access required\n", 
1837                                 ERROR + HIGHER_ACCESS_REQUIRED);
1838                         return;
1839                 }
1840         }
1841
1842         /* Check to make sure the requested room name doesn't already exist */
1843         newflags = create_room(new_room_name,
1844                                 new_room_type, new_room_pass, new_room_floor,
1845                                 0, avoid_access, new_room_view);
1846         if (newflags == 0) {
1847                 cprintf("%d '%s' already exists.\n",
1848                         ERROR + ALREADY_EXISTS, new_room_name);
1849                 return;
1850         }
1851
1852         if (cre8_ok == 0) {
1853                 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
1854                 return;
1855         }
1856
1857         /* If we reach this point, the room needs to be created. */
1858
1859         newflags = create_room(new_room_name,
1860                            new_room_type, new_room_pass, new_room_floor, 1, 0,
1861                            new_room_view);
1862
1863         /* post a message in Aide> describing the new room */
1864         notification_message = malloc(1024);
1865         snprintf(notification_message, 1024,
1866                 "%s> created by %s%s%s%s%s%s\n",
1867                 new_room_name,
1868                 CC->user.fullname,
1869                 ((newflags & QR_MAILBOX) ? " [personal]" : ""),
1870                 ((newflags & QR_PRIVATE) ? " [private]" : ""),
1871                 ((newflags & QR_GUESSNAME) ? " [hidden]" : ""),
1872                 ((newflags & QR_PASSWORDED) ? " Password: " : ""),
1873                 ((newflags & QR_PASSWORDED) ? new_room_pass : "")
1874         );
1875         aide_message(notification_message);
1876         free(notification_message);
1877
1878         cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
1879 }
1880
1881
1882
1883 void cmd_einf(char *ok)
1884 {                               /* enter info file for current room */
1885         FILE *fp;
1886         char infofilename[SIZ];
1887         char buf[SIZ];
1888
1889         unbuffer_output();
1890
1891         if (CtdlAccessCheck(ac_room_aide)) return;
1892
1893         if (atoi(ok) == 0) {
1894                 cprintf("%d Ok.\n", CIT_OK);
1895                 return;
1896         }
1897         assoc_file_name(infofilename, sizeof infofilename, &CC->room, "info");
1898         lprintf(CTDL_DEBUG, "opening\n");
1899         fp = fopen(infofilename, "w");
1900         lprintf(CTDL_DEBUG, "checking\n");
1901         if (fp == NULL) {
1902                 cprintf("%d Cannot open %s: %s\n",
1903                   ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1904                 return;
1905         }
1906         cprintf("%d Send info...\n", SEND_LISTING);
1907
1908         do {
1909                 client_getln(buf, sizeof buf);
1910                 if (strcmp(buf, "000"))
1911                         fprintf(fp, "%s\n", buf);
1912         } while (strcmp(buf, "000"));
1913         fclose(fp);
1914
1915         /* now update the room index so people will see our new info */
1916         lgetroom(&CC->room, CC->room.QRname);           /* lock so no one steps on us */
1917         CC->room.QRinfo = CC->room.QRhighest + 1L;
1918         lputroom(&CC->room);
1919 }
1920
1921
1922 /* 
1923  * cmd_lflr()   -  List all known floors
1924  */
1925 void cmd_lflr(void)
1926 {
1927         int a;
1928         struct floor flbuf;
1929
1930         if (CtdlAccessCheck(ac_logged_in)) return;
1931
1932         cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1933
1934         for (a = 0; a < MAXFLOORS; ++a) {
1935                 getfloor(&flbuf, a);
1936                 if (flbuf.f_flags & F_INUSE) {
1937                         cprintf("%d|%s|%d\n",
1938                                 a,
1939                                 flbuf.f_name,
1940                                 flbuf.f_ref_count);
1941                 }
1942         }
1943         cprintf("000\n");
1944 }
1945
1946
1947
1948 /*
1949  * create a new floor
1950  */
1951 void cmd_cflr(char *argbuf)
1952 {
1953         char new_floor_name[256];
1954         struct floor flbuf;
1955         int cflr_ok;
1956         int free_slot = (-1);
1957         int a;
1958
1959         extract_token(new_floor_name, argbuf, 0, '|', sizeof new_floor_name);
1960         cflr_ok = extract_int(argbuf, 1);
1961
1962         if (CtdlAccessCheck(ac_aide)) return;
1963
1964         if (strlen(new_floor_name) == 0) {
1965                 cprintf("%d Blank floor name not allowed.\n",
1966                         ERROR + ILLEGAL_VALUE);
1967                 return;
1968         }
1969
1970         for (a = 0; a < MAXFLOORS; ++a) {
1971                 getfloor(&flbuf, a);
1972
1973                 /* note any free slots while we're scanning... */
1974                 if (((flbuf.f_flags & F_INUSE) == 0)
1975                     && (free_slot < 0))
1976                         free_slot = a;
1977
1978                 /* check to see if it already exists */
1979                 if ((!strcasecmp(flbuf.f_name, new_floor_name))
1980                     && (flbuf.f_flags & F_INUSE)) {
1981                         cprintf("%d Floor '%s' already exists.\n",
1982                                 ERROR + ALREADY_EXISTS,
1983                                 flbuf.f_name);
1984                         return;
1985                 }
1986         }
1987
1988         if (free_slot < 0) {
1989                 cprintf("%d There is no space available for a new floor.\n",
1990                         ERROR + INVALID_FLOOR_OPERATION);
1991                 return;
1992         }
1993         if (cflr_ok == 0) {
1994                 cprintf("%d ok to create...\n", CIT_OK);
1995                 return;
1996         }
1997         lgetfloor(&flbuf, free_slot);
1998         flbuf.f_flags = F_INUSE;
1999         flbuf.f_ref_count = 0;
2000         safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
2001         lputfloor(&flbuf, free_slot);
2002         cprintf("%d %d\n", CIT_OK, free_slot);
2003 }
2004
2005
2006
2007 /*
2008  * delete a floor
2009  */
2010 void cmd_kflr(char *argbuf)
2011 {
2012         struct floor flbuf;
2013         int floor_to_delete;
2014         int kflr_ok;
2015         int delete_ok;
2016
2017         floor_to_delete = extract_int(argbuf, 0);
2018         kflr_ok = extract_int(argbuf, 1);
2019
2020         if (CtdlAccessCheck(ac_aide)) return;
2021
2022         lgetfloor(&flbuf, floor_to_delete);
2023
2024         delete_ok = 1;
2025         if ((flbuf.f_flags & F_INUSE) == 0) {
2026                 cprintf("%d Floor %d not in use.\n",
2027                         ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
2028                 delete_ok = 0;
2029         } else {
2030                 if (flbuf.f_ref_count != 0) {
2031                         cprintf("%d Cannot delete; floor contains %d rooms.\n",
2032                                 ERROR + INVALID_FLOOR_OPERATION,
2033                                 flbuf.f_ref_count);
2034                         delete_ok = 0;
2035                 } else {
2036                         if (kflr_ok == 1) {
2037                                 cprintf("%d Ok\n", CIT_OK);
2038                         } else {
2039                                 cprintf("%d Ok to delete...\n", CIT_OK);
2040                         }
2041
2042                 }
2043
2044         }
2045
2046         if ((delete_ok == 1) && (kflr_ok == 1))
2047                 flbuf.f_flags = 0;
2048         lputfloor(&flbuf, floor_to_delete);
2049 }
2050
2051 /*
2052  * edit a floor
2053  */
2054 void cmd_eflr(char *argbuf)
2055 {
2056         struct floor flbuf;
2057         int floor_num;
2058         int np;
2059
2060         np = num_parms(argbuf);
2061         if (np < 1) {
2062                 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
2063                 return;
2064         }
2065
2066         if (CtdlAccessCheck(ac_aide)) return;
2067
2068         floor_num = extract_int(argbuf, 0);
2069         lgetfloor(&flbuf, floor_num);
2070         if ((flbuf.f_flags & F_INUSE) == 0) {
2071                 lputfloor(&flbuf, floor_num);
2072                 cprintf("%d Floor %d is not in use.\n",
2073                         ERROR + INVALID_FLOOR_OPERATION, floor_num);
2074                 return;
2075         }
2076         if (np >= 2)
2077                 extract_token(flbuf.f_name, argbuf, 1, '|', sizeof flbuf.f_name);
2078         lputfloor(&flbuf, floor_num);
2079
2080         cprintf("%d Ok\n", CIT_OK);
2081 }