* In the wholist, only show <private room> if the user viewing the list
[citadel.git] / citadel / room_ops.c
1 /* $Id$ */
2 #include "sysdep.h"
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <stdio.h>
6 #include <sys/stat.h>
7 #include <string.h>
8 #ifdef HAVE_PTHREAD_H
9 #include <pthread.h>
10 #endif
11 #include <time.h>
12 #include <limits.h>
13 #include <errno.h>
14 #include "citadel.h"
15 #include "server.h"
16 #include "database.h"
17 #include "config.h"
18 #include "room_ops.h"
19 #include "sysdep_decls.h"
20 #include "support.h"
21 #include "user_ops.h"
22 #include "msgbase.h"
23 #include "serv_chat.h"
24 #include "citserver.h"
25 #include "control.h"
26 #include "tools.h"
27
28 /*
29  * Generic routine for determining user access to rooms
30  */
31 int CtdlRoomAccess(struct quickroom *roombuf, struct usersupp *userbuf)
32 {
33         int retval = 0;
34         struct visit vbuf;
35
36         /* for internal programs, always do everything */
37         if (((CC->internal_pgm)) && (roombuf->QRflags & QR_INUSE)) {
38                 return (UA_KNOWN | UA_GOTOALLOWED);
39         }
40         /* For mailbox rooms, only allow access to the owner */
41         if (roombuf->QRflags & QR_MAILBOX) {
42                 if (userbuf->usernum != atol(roombuf->QRname)) {
43                         return (retval);
44                 }
45         }
46         /* Locate any applicable user/room relationships */
47         CtdlGetRelationship(&vbuf, userbuf, roombuf);
48
49         /* Force the properties of the Aide room */
50         if (!strcasecmp(roombuf->QRname, AIDEROOM)) {
51                 if (userbuf->axlevel >= 6) {
52                         retval = UA_KNOWN | UA_GOTOALLOWED;
53                 } else {
54                         retval = 0;
55                 }
56                 goto NEWMSG;
57         }
58         /* For mailboxes, we skip all the access stuff (and we've
59          * already checked by this point that the mailbox belongs
60          * to the user)
61          */
62         if (roombuf->QRflags & QR_MAILBOX) {
63                 retval = UA_KNOWN | UA_GOTOALLOWED;
64                 goto NEWMSG;
65         }
66         /* If this is a public room, it's accessible... */
67         if ((roombuf->QRflags & QR_PRIVATE) == 0) {
68                 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
69         }
70         /* If this is a preferred users only room, check access level */
71         if (roombuf->QRflags & QR_PREFONLY) {
72                 if (userbuf->axlevel < 5) {
73                         retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
74                 }
75         }
76         /* For private rooms, check the generation number matchups */
77         if (roombuf->QRflags & QR_PRIVATE) {
78
79                 /* An explicit match means the user belongs in this room */
80                 if (vbuf.v_flags & V_ACCESS) {
81                         retval = retval | UA_KNOWN | UA_GOTOALLOWED;
82                 }
83                 /* Otherwise, check if this is a guess-name or passworded
84                  * room.  If it is, a goto may at least be attempted
85                  */
86                 else if ((roombuf->QRflags & QR_PRIVATE)
87                          || (roombuf->QRflags & QR_PASSWORDED)) {
88                         retval = retval & ~UA_KNOWN;
89                         retval = retval | UA_GOTOALLOWED;
90                 }
91         }
92         /* Check to see if the user has forgotten this room */
93         if (vbuf.v_flags & V_FORGET) {
94                 retval = retval & ~UA_KNOWN;
95                 retval = retval | UA_ZAPPED;
96         }
97         /* If user is explicitly locked out of this room, deny everything */
98         if (vbuf.v_flags & V_LOCKOUT) {
99                 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
100         }
101         /* Aides get access to everything */
102         if (userbuf->axlevel >= 6) {
103                 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
104                 retval = retval & ~UA_ZAPPED;
105         }
106       NEWMSG:                   /* By the way, we also check for the presence of new messages */
107         if ((roombuf->QRhighest) > (vbuf.v_lastseen)) {
108                 retval = retval | UA_HASNEWMSGS;
109         }
110         return (retval);
111 }
112
113 /*
114  * Self-checking stuff for a room record read into memory
115  */
116 void room_sanity_check(struct quickroom *qrbuf)
117 {
118         /* Mailbox rooms are always on the lowest floor */
119         if (qrbuf->QRflags & QR_MAILBOX) {
120                 qrbuf->QRfloor = 0;
121         }
122         /* Listing order of 0 is illegal except for base rooms */
123         if (qrbuf->QRorder == 0)
124                 if (!is_noneditable(qrbuf))
125                         qrbuf->QRorder = 64;
126 }
127
128
129 /*
130  * getroom()  -  retrieve room data from disk
131  */
132 int getroom(struct quickroom *qrbuf, char *room_name)
133 {
134         struct cdbdata *cdbqr;
135         char lowercase_name[ROOMNAMELEN];
136         char personal_lowercase_name[ROOMNAMELEN];
137         int a;
138
139         for (a = 0; room_name[a] && a < sizeof lowercase_name - 1; ++a) {
140                 lowercase_name[a] = tolower(room_name[a]);
141         }
142         lowercase_name[a] = 0;
143
144         memset(qrbuf, 0, sizeof(struct quickroom));
145
146         /* First, try the public namespace */
147         cdbqr = cdb_fetch(CDB_QUICKROOM,
148                           lowercase_name, strlen(lowercase_name));
149
150         /* If that didn't work, try the user's personal namespace */
151         if (cdbqr == NULL) {
152                 sprintf(personal_lowercase_name, "%010ld.%s",
153                         CC->usersupp.usernum, lowercase_name);
154                 cdbqr = cdb_fetch(CDB_QUICKROOM,
155                                   personal_lowercase_name,
156                                   strlen(personal_lowercase_name));
157         }
158         if (cdbqr != NULL) {
159                 memcpy(qrbuf, cdbqr->ptr,
160                        ((cdbqr->len > sizeof(struct quickroom)) ?
161                         sizeof(struct quickroom) : cdbqr->len));
162                 cdb_free(cdbqr);
163
164                 room_sanity_check(qrbuf);
165
166                 return (0);
167         } else {
168                 return (1);
169         }
170 }
171
172 /*
173  * lgetroom()  -  same as getroom() but locks the record (if supported)
174  */
175 int lgetroom(struct quickroom *qrbuf, char *room_name)
176 {
177         begin_critical_section(S_QUICKROOM);
178         return (getroom(qrbuf, room_name));
179 }
180
181
182 /*
183  * b_putroom()  -  back end to putroom() and b_deleteroom()
184  *              (if the supplied buffer is NULL, delete the room record)
185  */
186 void b_putroom(struct quickroom *qrbuf, char *room_name)
187 {
188         char lowercase_name[ROOMNAMELEN];
189         int a;
190
191         for (a = 0; a <= strlen(room_name); ++a) {
192                 lowercase_name[a] = tolower(room_name[a]);
193         }
194
195         if (qrbuf == NULL) {
196                 cdb_delete(CDB_QUICKROOM,
197                            lowercase_name, strlen(lowercase_name));
198         } else {
199                 time(&qrbuf->QRmtime);
200                 cdb_store(CDB_QUICKROOM,
201                           lowercase_name, strlen(lowercase_name),
202                           qrbuf, sizeof(struct quickroom));
203         }
204 }
205
206
207 /* 
208  * putroom()  -  store room data to disk
209  */
210 void putroom(struct quickroom *qrbuf) {
211         b_putroom(qrbuf, qrbuf->QRname);
212 }
213
214
215 /*
216  * b_deleteroom()  -  delete a room record from disk
217  */
218 void b_deleteroom(char *room_name) {
219         b_putroom(NULL, room_name);
220 }
221
222
223
224 /*
225  * lputroom()  -  same as putroom() but unlocks the record (if supported)
226  */
227 void lputroom(struct quickroom *qrbuf)
228 {
229
230         putroom(qrbuf);
231         end_critical_section(S_QUICKROOM);
232
233 }
234
235 /****************************************************************************/
236
237 /*
238  * getfloor()  -  retrieve floor data from disk
239  */
240 void getfloor(struct floor *flbuf, int floor_num)
241 {
242         struct cdbdata *cdbfl;
243
244         memset(flbuf, 0, sizeof(struct floor));
245         cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
246         if (cdbfl != NULL) {
247                 memcpy(flbuf, cdbfl->ptr,
248                        ((cdbfl->len > sizeof(struct floor)) ?
249                         sizeof(struct floor) : cdbfl->len));
250                 cdb_free(cdbfl);
251         } else {
252                 if (floor_num == 0) {
253                         strcpy(flbuf->f_name, "Main Floor");
254                         flbuf->f_flags = F_INUSE;
255                         flbuf->f_ref_count = 3;
256                 }
257         }
258
259 }
260
261 /*
262  * lgetfloor()  -  same as getfloor() but locks the record (if supported)
263  */
264 void lgetfloor(struct floor *flbuf, int floor_num)
265 {
266
267         begin_critical_section(S_FLOORTAB);
268         getfloor(flbuf, floor_num);
269 }
270
271
272 /*
273  * putfloor()  -  store floor data on disk
274  */
275 void putfloor(struct floor *flbuf, int floor_num)
276 {
277         cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
278                   flbuf, sizeof(struct floor));
279 }
280
281
282 /*
283  * lputfloor()  -  same as putfloor() but unlocks the record (if supported)
284  */
285 void lputfloor(struct floor *flbuf, int floor_num)
286 {
287
288         putfloor(flbuf, floor_num);
289         end_critical_section(S_FLOORTAB);
290
291 }
292
293
294 /* 
295  *  Traverse the room file...
296  */
297 void ForEachRoom(void (*CallBack) (struct quickroom * EachRoom))
298 {
299         struct quickroom qrbuf;
300         struct cdbdata *cdbqr;
301
302         cdb_rewind(CDB_QUICKROOM);
303
304         while (cdbqr = cdb_next_item(CDB_QUICKROOM), cdbqr != NULL) {
305                 memset(&qrbuf, 0, sizeof(struct quickroom));
306                 memcpy(&qrbuf, cdbqr->ptr,
307                        ((cdbqr->len > sizeof(struct quickroom)) ?
308                         sizeof(struct quickroom) : cdbqr->len));
309                 cdb_free(cdbqr);
310                 room_sanity_check(&qrbuf);
311                 if (qrbuf.QRflags & QR_INUSE)
312                         (*CallBack) (&qrbuf);
313         }
314 }
315
316
317 /*
318  * delete_msglist()  -  delete room message pointers
319  * FIX - this really should check first to make sure there's actually a
320  *       msglist to delete.  As things stand now, calling this function on
321  *       a room which has never been posted in will result in a message
322  *       like "gdbm: illegal data" (no big deal, but could use fixing).
323  */
324 void delete_msglist(struct quickroom *whichroom)
325 {
326
327         cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
328 }
329
330
331
332
333 /*
334  * sort message pointers
335  * (returns new msg count)
336  */
337 int sort_msglist(long listptrs[], int oldcount)
338 {
339         int a, b;
340         long hold1, hold2;
341         int numitems;
342
343         numitems = oldcount;
344         if (numitems < 2)
345                 return (oldcount);
346
347         /* do the sort */
348         for (a = numitems - 2; a >= 0; --a) {
349                 for (b = 0; b <= a; ++b) {
350                         if (listptrs[b] > (listptrs[b + 1])) {
351                                 hold1 = listptrs[b];
352                                 hold2 = listptrs[b + 1];
353                                 listptrs[b] = hold2;
354                                 listptrs[b + 1] = hold1;
355                         }
356                 }
357         }
358
359         /* and yank any nulls */
360         while ((numitems > 0) && (listptrs[0] == 0L)) {
361                 memcpy(&listptrs[0], &listptrs[1],
362                        (sizeof(long) * (numitems - 1)));
363                 --numitems;
364         }
365
366         return (numitems);
367 }
368
369
370 /*
371  * Determine whether a given room is non-editable.
372  */
373 int is_noneditable(struct quickroom *qrbuf)
374 {
375
376         /* Lobby> and Aide> are non-editable */
377         if (!strcasecmp(qrbuf->QRname, BASEROOM))
378                 return (1);
379         else if (!strcasecmp(qrbuf->QRname, AIDEROOM))
380                 return (1);
381
382         /* Mail> rooms are also non-editable */
383         else if ( (qrbuf->QRflags & QR_MAILBOX)
384              && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
385                 return (1);
386
387         /* Everything else is editable */
388         else
389                 return (0);
390 }
391
392
393
394 /*
395  * Back-back-end for all room listing commands
396  */
397 void list_roomname(struct quickroom *qrbuf)
398 {
399         char truncated_roomname[ROOMNAMELEN];
400
401         /* For mailbox rooms, chop off the owner prefix */
402         if (qrbuf->QRflags & QR_MAILBOX) {
403                 strcpy(truncated_roomname, qrbuf->QRname);
404                 strcpy(truncated_roomname, &truncated_roomname[11]);
405                 cprintf("%s", truncated_roomname);
406         }
407         /* For all other rooms, just display the name in its entirety */
408         else {
409                 cprintf("%s", qrbuf->QRname);
410         }
411
412         /* ...and now the other parameters */
413         cprintf("|%u|%d|%d\n",
414                 qrbuf->QRflags,
415                 (int) qrbuf->QRfloor,
416                 (int) qrbuf->QRorder);
417 }
418
419
420 /* 
421  * cmd_lrms()   -  List all accessible rooms, known or forgotten
422  */
423 void cmd_lrms_backend(struct quickroom *qrbuf)
424 {
425         if (((CtdlRoomAccess(qrbuf, &CC->usersupp)
426               & (UA_KNOWN | UA_ZAPPED)))
427             && ((qrbuf->QRfloor == (CC->FloorBeingSearched))
428                 || ((CC->FloorBeingSearched) < 0)))
429                 list_roomname(qrbuf);
430 }
431
432 void cmd_lrms(char *argbuf)
433 {
434         CC->FloorBeingSearched = (-1);
435         if (strlen(argbuf) > 0)
436                 CC->FloorBeingSearched = extract_int(argbuf, 0);
437
438         if (!(CC->logged_in)) {
439                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
440                 return;
441         }
442         if (getuser(&CC->usersupp, CC->curr_user)) {
443                 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
444                 return;
445         }
446         cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
447
448         ForEachRoom(cmd_lrms_backend);
449         cprintf("000\n");
450 }
451
452
453
454 /* 
455  * cmd_lkra()   -  List all known rooms
456  */
457 void cmd_lkra_backend(struct quickroom *qrbuf)
458 {
459         if (((CtdlRoomAccess(qrbuf, &CC->usersupp)
460               & (UA_KNOWN)))
461             && ((qrbuf->QRfloor == (CC->FloorBeingSearched))
462                 || ((CC->FloorBeingSearched) < 0)))
463                 list_roomname(qrbuf);
464 }
465
466 void cmd_lkra(char *argbuf)
467 {
468         CC->FloorBeingSearched = (-1);
469         if (strlen(argbuf) > 0)
470                 CC->FloorBeingSearched = extract_int(argbuf, 0);
471
472         if (!(CC->logged_in)) {
473                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
474                 return;
475         }
476         if (getuser(&CC->usersupp, CC->curr_user)) {
477                 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
478                 return;
479         }
480         cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
481
482         ForEachRoom(cmd_lkra_backend);
483         cprintf("000\n");
484 }
485
486
487
488 /* 
489  * cmd_lkrn()   -  List all known rooms with new messages
490  */
491 void cmd_lkrn_backend(struct quickroom *qrbuf)
492 {
493         int ra;
494
495         ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
496         if ((ra & UA_KNOWN)
497             && (ra & UA_HASNEWMSGS)
498             && ((qrbuf->QRfloor == (CC->FloorBeingSearched))
499                 || ((CC->FloorBeingSearched) < 0)))
500                 list_roomname(qrbuf);
501 }
502
503 void cmd_lkrn(char *argbuf)
504 {
505         CC->FloorBeingSearched = (-1);
506         if (strlen(argbuf) > 0)
507                 CC->FloorBeingSearched = extract_int(argbuf, 0);
508
509         if (!(CC->logged_in)) {
510                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
511                 return;
512         }
513         if (getuser(&CC->usersupp, CC->curr_user)) {
514                 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
515                 return;
516         }
517         cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
518
519         ForEachRoom(cmd_lkrn_backend);
520         cprintf("000\n");
521 }
522
523
524
525 /* 
526  * cmd_lkro()   -  List all known rooms
527  */
528 void cmd_lkro_backend(struct quickroom *qrbuf)
529 {
530         int ra;
531
532         ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
533         if ((ra & UA_KNOWN)
534             && ((ra & UA_HASNEWMSGS) == 0)
535             && ((qrbuf->QRfloor == (CC->FloorBeingSearched))
536                 || ((CC->FloorBeingSearched) < 0)))
537                 list_roomname(qrbuf);
538 }
539
540 void cmd_lkro(char *argbuf)
541 {
542         CC->FloorBeingSearched = (-1);
543         if (strlen(argbuf) > 0)
544                 CC->FloorBeingSearched = extract_int(argbuf, 0);
545
546         if (!(CC->logged_in)) {
547                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
548                 return;
549         }
550         if (getuser(&CC->usersupp, CC->curr_user)) {
551                 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
552                 return;
553         }
554         cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
555
556         ForEachRoom(cmd_lkro_backend);
557         cprintf("000\n");
558 }
559
560
561
562 /* 
563  * cmd_lzrm()   -  List all forgotten rooms
564  */
565 void cmd_lzrm_backend(struct quickroom *qrbuf)
566 {
567         int ra;
568
569         ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
570         if ((ra & UA_GOTOALLOWED)
571             && (ra & UA_ZAPPED)
572             && ((qrbuf->QRfloor == (CC->FloorBeingSearched))
573                 || ((CC->FloorBeingSearched) < 0)))
574                 list_roomname(qrbuf);
575 }
576
577 void cmd_lzrm(char *argbuf)
578 {
579         CC->FloorBeingSearched = (-1);
580         if (strlen(argbuf) > 0)
581                 CC->FloorBeingSearched = extract_int(argbuf, 0);
582
583         if (!(CC->logged_in)) {
584                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
585                 return;
586         }
587         if (getuser(&CC->usersupp, CC->curr_user)) {
588                 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
589                 return;
590         }
591         cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
592
593         ForEachRoom(cmd_lzrm_backend);
594         cprintf("000\n");
595 }
596
597
598
599 void usergoto(char *where, int display_result)
600 {
601         int a;
602         int new_messages = 0;
603         int total_messages = 0;
604         int info = 0;
605         int rmailflag;
606         int raideflag;
607         int newmailcount = 0;
608         struct visit vbuf;
609         char truncated_roomname[ROOMNAMELEN];
610         struct cdbdata *cdbfr;
611         long *msglist = NULL;
612         int num_msgs = 0;
613
614         strcpy(CC->quickroom.QRname, where);
615         getroom(&CC->quickroom, where);
616
617         lgetuser(&CC->usersupp, CC->curr_user);
618         CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
619
620         /* Know the room ... but not if it's the page log room */
621         if (strcasecmp(where, config.c_logpages)) {
622                 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
623                 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
624         }
625         CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
626         lputuser(&CC->usersupp);
627
628         /* check for new mail */
629         newmailcount = NewMailCount();
630
631         /* set info to 1 if the user needs to read the room's info file */
632         if (CC->quickroom.QRinfo > vbuf.v_lastseen)
633                 info = 1;
634
635         get_mm();
636         cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->quickroom.QRnumber, sizeof(long));
637         if (cdbfr != NULL) {
638                 msglist = mallok(cdbfr->len);
639                 memcpy(msglist, cdbfr->ptr, cdbfr->len);
640                 num_msgs = cdbfr->len / sizeof(long);
641                 cdb_free(cdbfr);
642         }
643
644         if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
645                 if (msglist[a] > 0L) {
646                         ++total_messages;
647                         if (msglist[a] > vbuf.v_lastseen) {
648                                 ++new_messages;
649                         }
650                 }
651         }
652
653         if (msglist != NULL) phree(msglist);
654
655         if (CC->quickroom.QRflags & QR_MAILBOX)
656                 rmailflag = 1;
657         else
658                 rmailflag = 0;
659
660         if ((CC->quickroom.QRroomaide == CC->usersupp.usernum)
661             || (CC->usersupp.axlevel >= 6))
662                 raideflag = 1;
663         else
664                 raideflag = 0;
665
666         strcpy(truncated_roomname, CC->quickroom.QRname);
667         if (CC->quickroom.QRflags & QR_MAILBOX) {
668                 strcpy(truncated_roomname, &truncated_roomname[11]);
669         }
670
671         if (display_result)
672                 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d\n",
673                         OK, check_express(),
674                         truncated_roomname,
675                         new_messages, total_messages,
676                         info, CC->quickroom.QRflags,
677                         CC->quickroom.QRhighest,
678                         vbuf.v_lastseen,
679                         rmailflag, raideflag, newmailcount,
680                         CC->quickroom.QRfloor);
681
682 }
683
684
685 /* 
686  * cmd_goto()  -  goto a new room
687  */
688 void cmd_goto(char *gargs)
689 {
690         struct quickroom QRscratch;
691         int c;
692         int ok = 0;
693         int ra;
694         char augmented_roomname[256];
695         char towhere[256];
696         char password[256];
697
698         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
699                 cprintf("%d not logged in\n", ERROR + NOT_LOGGED_IN);
700                 return;
701         }
702
703         extract(towhere, gargs, 0);
704         extract(password, gargs, 1);
705
706         getuser(&CC->usersupp, CC->curr_user);
707
708         if (!strcasecmp(towhere, "_BASEROOM_"))
709                 strcpy(towhere, BASEROOM);
710
711         if (!strcasecmp(towhere, "_MAIL_"))
712                 strcpy(towhere, MAILROOM);
713
714         if (!strcasecmp(towhere, "_BITBUCKET_"))
715                 strcpy(towhere, config.c_twitroom);
716
717
718         /* First try a regular match */
719         c = getroom(&QRscratch, towhere);
720
721         /* Then try a mailbox name match */
722         if (c != 0) {
723                 MailboxName(augmented_roomname, &CC->usersupp, towhere);
724                 c = getroom(&QRscratch, augmented_roomname);
725                 if (c == 0)
726                         strcpy(towhere, augmented_roomname);
727         }
728
729         /* And if the room was found... */
730         if (c == 0) {
731
732                 /* let internal programs go directly to any room */
733                 if (CC->internal_pgm) {
734                         usergoto(towhere, 1);
735                         return;
736                 }
737
738                 /* See if there is an existing user/room relationship */
739                 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
740
741                 /* normal clients have to pass through security */
742                 if (ra & UA_GOTOALLOWED)
743                         ok = 1;
744
745                 if (ok == 1) {
746                         if ((QRscratch.QRflags & QR_PASSWORDED) &&
747                             ((ra & UA_KNOWN) == 0) &&
748                             (strcasecmp(QRscratch.QRpasswd, password))
749                             ) {
750                                 cprintf("%d wrong or missing passwd\n",
751                                         ERROR + PASSWORD_REQUIRED);
752                                 return;
753                         } else if ((QRscratch.QRflags & QR_PRIVATE) &&
754                                    ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
755                                    ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
756                                    ((ra & UA_KNOWN) == 0)) {
757                                 goto NOPE;
758                         } else {
759                                 usergoto(towhere, 1);
760                                 return;
761                         }
762                 }
763         }
764
765 NOPE:   cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
766 }
767
768
769 void cmd_whok(void)
770 {
771         struct usersupp temp;
772         struct cdbdata *cdbus;
773
774         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
775                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
776                 return;
777         }
778         getuser(&CC->usersupp, CC->curr_user);
779
780         if ((!is_room_aide()) && (!(CC->internal_pgm))) {
781                 cprintf("%d Higher access required.\n",
782                         ERROR + HIGHER_ACCESS_REQUIRED);
783                 return;
784         }
785         cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
786         cdb_rewind(CDB_USERSUPP);
787         while (cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
788                 memset(&temp, 0, sizeof temp);
789                 memcpy(&temp, cdbus->ptr, sizeof temp);
790                 cdb_free(cdbus);
791
792                 if ((CC->quickroom.QRflags & QR_INUSE)
793                     && (CtdlRoomAccess(&CC->quickroom, &temp) & UA_KNOWN)
794                     )
795                         cprintf("%s\n", temp.fullname);
796         }
797         cprintf("000\n");
798 }
799
800
801 /*
802  * RDIR command for room directory
803  */
804 void cmd_rdir(void)
805 {
806         char buf[256];
807         char flnm[256];
808         char comment[256];
809         FILE *ls, *fd;
810         struct stat statbuf;
811
812         if (!(CC->logged_in)) {
813                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
814                 return;
815         }
816         getroom(&CC->quickroom, CC->quickroom.QRname);
817         getuser(&CC->usersupp, CC->curr_user);
818
819         if ((CC->quickroom.QRflags & QR_DIRECTORY) == 0) {
820                 cprintf("%d not here.\n", ERROR + NOT_HERE);
821                 return;
822         }
823         if (((CC->quickroom.QRflags & QR_VISDIR) == 0)
824             && (CC->usersupp.axlevel < 6)
825             && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
826                 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
827                 return;
828         }
829         cprintf("%d %s|%s/files/%s\n",
830         LISTING_FOLLOWS, config.c_fqdn, BBSDIR, CC->quickroom.QRdirname);
831
832         sprintf(buf, "ls %s/files/%s  >%s 2> /dev/null",
833                 BBSDIR, CC->quickroom.QRdirname, CC->temp);
834         system(buf);
835
836         sprintf(buf, "%s/files/%s/filedir", BBSDIR, CC->quickroom.QRdirname);
837         fd = fopen(buf, "r");
838         if (fd == NULL)
839                 fd = fopen("/dev/null", "r");
840
841         ls = fopen(CC->temp, "r");
842         while (fgets(flnm, 256, ls) != NULL) {
843                 flnm[strlen(flnm) - 1] = 0;
844                 if (strcasecmp(flnm, "filedir")) {
845                         sprintf(buf, "%s/files/%s/%s",
846                                 BBSDIR, CC->quickroom.QRdirname, flnm);
847                         stat(buf, &statbuf);
848                         strcpy(comment, "");
849                         fseek(fd, 0L, 0);
850                         while ((fgets(buf, 256, fd) != NULL)
851                                && (strlen(comment) == 0)) {
852                                 buf[strlen(buf) - 1] = 0;
853                                 if ((!strncasecmp(buf, flnm, strlen(flnm)))
854                                     && (buf[strlen(flnm)] == ' '))
855                                         strncpy(comment,
856                                             &buf[strlen(flnm) + 1], 255);
857                         }
858                         cprintf("%s|%ld|%s\n", flnm, statbuf.st_size, comment);
859                 }
860         }
861         fclose(ls);
862         fclose(fd);
863         unlink(CC->temp);
864
865         cprintf("000\n");
866 }
867
868 /*
869  * get room parameters (aide or room aide command)
870  */
871 void cmd_getr(void)
872 {
873         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
874                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
875                 return;
876         }
877         if ((!is_room_aide()) && (!(CC->internal_pgm))) {
878                 cprintf("%d Higher access required.\n",
879                         ERROR + HIGHER_ACCESS_REQUIRED);
880                 return;
881         }
882         if (is_noneditable(&CC->quickroom)) {
883                 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
884                 return;
885         }
886         getroom(&CC->quickroom, CC->quickroom.QRname);
887         cprintf("%d%c%s|%s|%s|%d|%d|%d\n",
888                 OK, check_express(),
889                 CC->quickroom.QRname,
890                 ((CC->quickroom.QRflags & QR_PASSWORDED) ? CC->quickroom.QRpasswd : ""),
891                 ((CC->quickroom.QRflags & QR_DIRECTORY) ? CC->quickroom.QRdirname : ""),
892                 CC->quickroom.QRflags,
893                 (int) CC->quickroom.QRfloor,
894                 (int) CC->quickroom.QRorder);
895 }
896
897
898 /*
899  * set room parameters (aide or room aide command)
900  */
901 void cmd_setr(char *args)
902 {
903         char buf[256];
904         struct floor flbuf;
905         char old_name[ROOMNAMELEN];
906         int old_floor;
907         int new_order = 0;
908
909         if (!(CC->logged_in)) {
910                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
911                 return;
912         }
913         if (!is_room_aide()) {
914                 cprintf("%d Higher access required.\n",
915                         ERROR + HIGHER_ACCESS_REQUIRED);
916                 return;
917         }
918         if (is_noneditable(&CC->quickroom)) {
919                 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
920                 return;
921         }
922         if (num_parms(args) >= 6) {
923                 getfloor(&flbuf, extract_int(args, 5));
924                 if ((flbuf.f_flags & F_INUSE) == 0) {
925                         cprintf("%d Invalid floor number.\n",
926                                 ERROR + INVALID_FLOOR_OPERATION);
927                         return;
928                 }
929         }
930         if (num_parms(args) >= 7) {
931                 new_order = extract_int(args, 6);
932                 if (new_order < 1)
933                         new_order = 1;
934                 if (new_order > 127)
935                         new_order = 127;
936         }
937         lgetroom(&CC->quickroom, CC->quickroom.QRname);
938         strcpy(old_name, CC->quickroom.QRname);
939         extract(buf, args, 0);
940         buf[ROOMNAMELEN] = 0;
941         strncpy(CC->quickroom.QRname, buf, ROOMNAMELEN - 1);
942         extract(buf, args, 1);
943         buf[10] = 0;
944         strncpy(CC->quickroom.QRpasswd, buf, 9);
945         extract(buf, args, 2);
946         buf[15] = 0;
947         strncpy(CC->quickroom.QRdirname, buf, 19);
948         CC->quickroom.QRflags = (extract_int(args, 3) | QR_INUSE);
949         if (num_parms(args) >= 7)
950                 CC->quickroom.QRorder = (char) new_order;
951
952         /* Clean up a client boo-boo: if the client set the room to
953          * guess-name or passworded, ensure that the private flag is
954          * also set.
955          */
956         if ((CC->quickroom.QRflags & QR_GUESSNAME)
957             || (CC->quickroom.QRflags & QR_PASSWORDED))
958                 CC->quickroom.QRflags |= QR_PRIVATE;
959
960         /* Kick everyone out if the client requested it (by changing the
961          * room's generation number)
962          */
963         if (extract_int(args, 4)) {
964                 time(&CC->quickroom.QRgen);
965         }
966         old_floor = CC->quickroom.QRfloor;
967         if (num_parms(args) >= 6) {
968                 CC->quickroom.QRfloor = extract_int(args, 5);
969         }
970         /* Write the room record back to disk */
971         lputroom(&CC->quickroom);
972
973         /* If the room name changed, then there are now two room records,
974          * so we have to delete the old one.
975          */
976         if (strcasecmp(CC->quickroom.QRname, old_name)) {
977                 b_deleteroom(old_name);
978         }
979         /* adjust the floor reference counts */
980         lgetfloor(&flbuf, old_floor);
981         --flbuf.f_ref_count;
982         lputfloor(&flbuf, old_floor);
983         lgetfloor(&flbuf, CC->quickroom.QRfloor);
984         ++flbuf.f_ref_count;
985         lputfloor(&flbuf, CC->quickroom.QRfloor);
986
987         /* create a room directory if necessary */
988         if (CC->quickroom.QRflags & QR_DIRECTORY) {
989                 sprintf(buf,
990                     "mkdir ./files/%s </dev/null >/dev/null 2>/dev/null",
991                         CC->quickroom.QRdirname);
992                 system(buf);
993         }
994         sprintf(buf, "%s> edited by %s\n", CC->quickroom.QRname, CC->curr_user);
995         aide_message(buf);
996         cprintf("%d Ok\n", OK);
997 }
998
999
1000
1001 /* 
1002  * get the name of the room aide for this room
1003  */
1004 void cmd_geta(void)
1005 {
1006         struct usersupp usbuf;
1007
1008         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1009                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1010                 return;
1011         }
1012         if (is_noneditable(&CC->quickroom)) {
1013                 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
1014                 return;
1015         }
1016         if (getuserbynumber(&usbuf, CC->quickroom.QRroomaide) == 0) {
1017                 cprintf("%d %s\n", OK, usbuf.fullname);
1018         } else {
1019                 cprintf("%d \n", OK);
1020         }
1021 }
1022
1023
1024 /* 
1025  * set the room aide for this room
1026  */
1027 void cmd_seta(char *new_ra)
1028 {
1029         struct usersupp usbuf;
1030         long newu;
1031         char buf[256];
1032         int post_notice;
1033
1034         if (!(CC->logged_in)) {
1035                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1036                 return;
1037         }
1038         if (!is_room_aide()) {
1039                 cprintf("%d Higher access required.\n",
1040                         ERROR + HIGHER_ACCESS_REQUIRED);
1041                 return;
1042         }
1043         if (getuser(&usbuf, new_ra) != 0) {
1044                 newu = (-1L);
1045         } else {
1046                 newu = usbuf.usernum;
1047         }
1048
1049         lgetroom(&CC->quickroom, CC->quickroom.QRname);
1050         post_notice = 0;
1051         if (CC->quickroom.QRroomaide != newu) {
1052                 post_notice = 1;
1053         }
1054         CC->quickroom.QRroomaide = newu;
1055         lputroom(&CC->quickroom);
1056
1057         /*
1058          * We have to post the change notice _after_ writing changes to 
1059          * the room table, otherwise it would deadlock!
1060          */
1061         if (post_notice == 1) {
1062                 sprintf(buf, "%s is now room aide for %s>\n",
1063                         usbuf.fullname, CC->quickroom.QRname);
1064                 aide_message(buf);
1065         }
1066         cprintf("%d Ok\n", OK);
1067 }
1068
1069 /*
1070  * Generate an associated file name for a room
1071  */
1072 void assoc_file_name(char *buf, struct quickroom *qrbuf, char *prefix)
1073 {
1074         sprintf(buf, "./%s/%ld", prefix, qrbuf->QRnumber);
1075 }
1076
1077 /* 
1078  * retrieve info file for this room
1079  */
1080 void cmd_rinf(void)
1081 {
1082         char filename[128];
1083         char buf[256];
1084         FILE *info_fp;
1085
1086         assoc_file_name(filename, &CC->quickroom, "info");
1087         info_fp = fopen(filename, "r");
1088
1089         if (info_fp == NULL) {
1090                 cprintf("%d No info file.\n", ERROR);
1091                 return;
1092         }
1093         cprintf("%d Info:\n", LISTING_FOLLOWS);
1094         while (fgets(buf, 256, info_fp) != NULL) {
1095                 if (strlen(buf) > 0)
1096                         buf[strlen(buf) - 1] = 0;
1097                 cprintf("%s\n", buf);
1098         }
1099         cprintf("000\n");
1100         fclose(info_fp);
1101 }
1102
1103 /*
1104  * Back end processing to delete a room and everything associated with it
1105  */
1106 void delete_room(struct quickroom *qrbuf)
1107 {
1108         struct floor flbuf;
1109         char filename[100];
1110
1111         lprintf(9, "Deleting room <%s>\n", qrbuf->QRname);
1112
1113         /* Delete the info file */
1114         assoc_file_name(filename, qrbuf, "info");
1115         unlink(filename);
1116
1117         /* Delete the image file */
1118         assoc_file_name(filename, qrbuf, "images");
1119         unlink(filename);
1120
1121         /* Delete the messages in the room
1122          * (Careful: this opens an S_QUICKROOM critical section!)
1123          */
1124         CtdlDeleteMessages(qrbuf->QRname, 0L, NULL);
1125
1126         /* Flag the room record as not in use */
1127         lgetroom(qrbuf, qrbuf->QRname);
1128         qrbuf->QRflags = 0;
1129         lputroom(qrbuf);
1130
1131         /* then decrement the reference count for the floor */
1132         lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1133         flbuf.f_ref_count = flbuf.f_ref_count - 1;
1134         lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1135
1136         /* Delete the room record from the database! */
1137         b_deleteroom(qrbuf->QRname);
1138 }
1139
1140
1141 /*
1142  * aide command: kill the current room
1143  */
1144 void cmd_kill(char *argbuf)
1145 {
1146         char aaa[100];
1147         char deleted_room_name[ROOMNAMELEN];
1148         int kill_ok;
1149
1150         kill_ok = extract_int(argbuf, 0);
1151
1152         if (!(CC->logged_in)) {
1153                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1154                 return;
1155         }
1156         if (!is_room_aide()) {
1157                 cprintf("%d Higher access required.\n",
1158                         ERROR + HIGHER_ACCESS_REQUIRED);
1159                 return;
1160         }
1161         if (is_noneditable(&CC->quickroom)) {
1162                 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
1163                 return;
1164         }
1165         if (kill_ok) {
1166                 strcpy(deleted_room_name, CC->quickroom.QRname);
1167                 delete_room(&CC->quickroom);    /* Do the dirty work */
1168                 usergoto(BASEROOM, 0);  /* Return to the Lobby */
1169
1170                 /* tell the world what we did */
1171                 sprintf(aaa, "%s> killed by %s\n",
1172                         deleted_room_name, CC->curr_user);
1173                 aide_message(aaa);
1174                 cprintf("%d '%s' deleted.\n", OK, deleted_room_name);
1175         } else {
1176                 cprintf("%d ok to delete.\n", OK);
1177         }
1178 }
1179
1180
1181 /*
1182  * Internal code to create a new room (returns room flags)
1183  *
1184  * Room types:  0=public, 1=guessname, 2=passworded, 3=inv-only, 4=mailbox
1185  */
1186 unsigned create_room(char *new_room_name,
1187                      int new_room_type,
1188                      char *new_room_pass,
1189                      int new_room_floor)
1190 {
1191
1192         struct quickroom qrbuf;
1193         struct floor flbuf;
1194         struct visit vbuf;
1195
1196         if (getroom(&qrbuf, new_room_name) == 0)
1197                 return (0);     /* already exists */
1198
1199         memset(&qrbuf, 0, sizeof(struct quickroom));
1200         strncpy(qrbuf.QRname, new_room_name, ROOMNAMELEN);
1201         strncpy(qrbuf.QRpasswd, new_room_pass, 9);
1202         qrbuf.QRflags = QR_INUSE;
1203         qrbuf.QRnumber = get_new_room_number();
1204         if (new_room_type > 0)
1205                 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1206         if (new_room_type == 1)
1207                 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1208         if (new_room_type == 2)
1209                 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1210         if (new_room_type == 4)
1211                 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1212
1213         /* If the room is private, and the system administrator has elected
1214          * to automatically grant room aide privileges, do so now; otherwise,
1215          * set the room aide to undefined.
1216          */
1217         if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1218                 qrbuf.QRroomaide = CC->usersupp.usernum;
1219         } else {
1220                 qrbuf.QRroomaide = (-1L);
1221         }
1222
1223         qrbuf.QRhighest = 0L;   /* No messages in this room yet */
1224         time(&qrbuf.QRgen);     /* Use a timestamp as the generation number */
1225         qrbuf.QRfloor = new_room_floor;
1226
1227         /* save what we just did... */
1228         putroom(&qrbuf);
1229
1230         /* bump the reference count on whatever floor the room is on */
1231         lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1232         flbuf.f_ref_count = flbuf.f_ref_count + 1;
1233         lputfloor(&flbuf, (int) qrbuf.QRfloor);
1234
1235         /* be sure not to kick the creator out of the room! */
1236         lgetuser(&CC->usersupp, CC->curr_user);
1237         CtdlGetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1238         vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1239         vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1240         CtdlSetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1241         lputuser(&CC->usersupp);
1242
1243         /* resume our happy day */
1244         return (qrbuf.QRflags);
1245 }
1246
1247
1248 /*
1249  * create a new room
1250  */
1251 void cmd_cre8(char *args)
1252 {
1253         int cre8_ok;
1254         char new_room_name[256];
1255         int new_room_type;
1256         char new_room_pass[256];
1257         int new_room_floor;
1258         char aaa[256];
1259         unsigned newflags;
1260         struct quickroom qrbuf;
1261         struct floor flbuf;
1262
1263         cre8_ok = extract_int(args, 0);
1264         extract(new_room_name, args, 1);
1265         new_room_name[ROOMNAMELEN - 1] = 0;
1266         new_room_type = extract_int(args, 2);
1267         extract(new_room_pass, args, 3);
1268         new_room_pass[9] = 0;
1269         new_room_floor = 0;
1270
1271         if ((strlen(new_room_name) == 0) && (cre8_ok == 1)) {
1272                 cprintf("%d Invalid room name.\n", ERROR);
1273                 return;
1274         }
1275
1276         if (!strcasecmp(new_room_name, MAILROOM)) {
1277                 cprintf("%d '%s' already exists.\n",
1278                         ERROR + ALREADY_EXISTS, new_room_name);
1279                 return;
1280         }
1281
1282         if (num_parms(args) >= 5) {
1283                 getfloor(&flbuf, extract_int(args, 4));
1284                 if ((flbuf.f_flags & F_INUSE) == 0) {
1285                         cprintf("%d Invalid floor number.\n",
1286                                 ERROR + INVALID_FLOOR_OPERATION);
1287                         return;
1288                 } else {
1289                         new_room_floor = extract_int(args, 4);
1290                 }
1291         }
1292
1293         if (!(CC->logged_in)) {
1294                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1295                 return;
1296         }
1297
1298         if (CC->usersupp.axlevel < config.c_createax) {
1299                 cprintf("%d You need higher access to create rooms.\n",
1300                         ERROR + HIGHER_ACCESS_REQUIRED);
1301                 return;
1302         }
1303
1304         if ((strlen(new_room_name) == 0) && (cre8_ok == 0)) {
1305                 cprintf("%d Ok to create rooms.\n", OK);
1306                 return;
1307         }
1308
1309         if ((new_room_type < 0) || (new_room_type > 4)) {
1310                 cprintf("%d Invalid room type.\n", ERROR);
1311                 return;
1312         }
1313
1314         /* If the user is requesting a personal room, set up the room
1315          * name accordingly (prepend the user number)
1316          */
1317         if (new_room_type == 4) {
1318                 sprintf(aaa, "%010ld.%s",
1319                         CC->usersupp.usernum, new_room_name);
1320                 strcpy(new_room_name, aaa);
1321         }
1322
1323         /* Check to make sure the requested room name doesn't already exist */
1324         if (getroom(&qrbuf, new_room_name) == 0) {
1325                 cprintf("%d '%s' already exists.\n",
1326                         ERROR + ALREADY_EXISTS, qrbuf.QRname);
1327                 return;
1328         }
1329
1330         if (cre8_ok == 0) {
1331                 cprintf("%d OK to create '%s'\n", OK, new_room_name);
1332                 return;
1333         }
1334
1335         newflags = create_room(new_room_name,
1336                            new_room_type, new_room_pass, new_room_floor);
1337
1338         /* post a message in Aide> describing the new room */
1339         strncpy(aaa, new_room_name, 255);
1340         strcat(aaa, "> created by ");
1341         strcat(aaa, CC->usersupp.fullname);
1342         if (newflags & QR_MAILBOX)
1343                 strcat(aaa, " [personal]");
1344         else if (newflags & QR_PRIVATE)
1345                 strcat(aaa, " [private]");
1346         if (newflags & QR_GUESSNAME)
1347                 strcat(aaa, "[guessname] ");
1348         if (newflags & QR_PASSWORDED) {
1349                 strcat(aaa, "\n Password: ");
1350                 strcat(aaa, new_room_pass);
1351         }
1352         strcat(aaa, "\n");
1353         aide_message(aaa);
1354
1355         cprintf("%d '%s' has been created.\n", OK, qrbuf.QRname);
1356 }
1357
1358
1359
1360 void cmd_einf(char *ok)
1361 {                               /* enter info file for current room */
1362         FILE *fp;
1363         char infofilename[256];
1364         char buf[256];
1365
1366         if (!(CC->logged_in)) {
1367                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1368                 return;
1369         }
1370         if (!is_room_aide()) {
1371                 cprintf("%d Higher access required.\n",
1372                         ERROR + HIGHER_ACCESS_REQUIRED);
1373                 return;
1374         }
1375         if (atoi(ok) == 0) {
1376                 cprintf("%d Ok.\n", OK);
1377                 return;
1378         }
1379         assoc_file_name(infofilename, &CC->quickroom, "info");
1380         lprintf(9, "opening\n");
1381         fp = fopen(infofilename, "w");
1382         lprintf(9, "checking\n");
1383         if (fp == NULL) {
1384                 cprintf("%d Cannot open %s: %s\n",
1385                   ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1386                 return;
1387         }
1388         cprintf("%d Send info...\n", SEND_LISTING);
1389
1390         do {
1391                 client_gets(buf);
1392                 if (strcmp(buf, "000"))
1393                         fprintf(fp, "%s\n", buf);
1394         } while (strcmp(buf, "000"));
1395         fclose(fp);
1396
1397         /* now update the room index so people will see our new info */
1398         lgetroom(&CC->quickroom, CC->quickroom.QRname);         /* lock so no one steps on us */
1399         CC->quickroom.QRinfo = CC->quickroom.QRhighest + 1L;
1400         lputroom(&CC->quickroom);
1401 }
1402
1403
1404 /* 
1405  * cmd_lflr()   -  List all known floors
1406  */
1407 void cmd_lflr(void)
1408 {
1409         int a;
1410         struct floor flbuf;
1411
1412         if (!(CC->logged_in)) {
1413                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1414                 return;
1415         }
1416         /* if (getuser(&CC->usersupp,CC->curr_user)) {
1417            cprintf("%d Can't locate user!\n",ERROR+INTERNAL_ERROR);
1418            return;
1419            }
1420          */
1421
1422         cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1423
1424         for (a = 0; a < MAXFLOORS; ++a) {
1425                 getfloor(&flbuf, a);
1426                 if (flbuf.f_flags & F_INUSE) {
1427                         cprintf("%d|%s|%d\n",
1428                                 a,
1429                                 flbuf.f_name,
1430                                 flbuf.f_ref_count);
1431                 }
1432         }
1433         cprintf("000\n");
1434 }
1435
1436
1437
1438 /*
1439  * create a new floor
1440  */
1441 void cmd_cflr(char *argbuf)
1442 {
1443         char new_floor_name[256];
1444         struct floor flbuf;
1445         int cflr_ok;
1446         int free_slot = (-1);
1447         int a;
1448
1449         extract(new_floor_name, argbuf, 0);
1450         cflr_ok = extract_int(argbuf, 1);
1451
1452
1453         if (!(CC->logged_in)) {
1454                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1455                 return;
1456         }
1457         if (CC->usersupp.axlevel < 6) {
1458                 cprintf("%d You need higher access to create rooms.\n",
1459                         ERROR + HIGHER_ACCESS_REQUIRED);
1460                 return;
1461         }
1462         for (a = 0; a < MAXFLOORS; ++a) {
1463                 getfloor(&flbuf, a);
1464
1465                 /* note any free slots while we're scanning... */
1466                 if (((flbuf.f_flags & F_INUSE) == 0)
1467                     && (free_slot < 0))
1468                         free_slot = a;
1469
1470                 /* check to see if it already exists */
1471                 if ((!strcasecmp(flbuf.f_name, new_floor_name))
1472                     && (flbuf.f_flags & F_INUSE)) {
1473                         cprintf("%d Floor '%s' already exists.\n",
1474                                 ERROR + ALREADY_EXISTS,
1475                                 flbuf.f_name);
1476                         return;
1477                 }
1478         }
1479
1480         if (free_slot < 0) {
1481                 cprintf("%d There is no space available for a new floor.\n",
1482                         ERROR + INVALID_FLOOR_OPERATION);
1483                 return;
1484         }
1485         if (cflr_ok == 0) {
1486                 cprintf("%d ok to create...\n", OK);
1487                 return;
1488         }
1489         lgetfloor(&flbuf, free_slot);
1490         flbuf.f_flags = F_INUSE;
1491         flbuf.f_ref_count = 0;
1492         strncpy(flbuf.f_name, new_floor_name, 255);
1493         lputfloor(&flbuf, free_slot);
1494         cprintf("%d %d\n", OK, free_slot);
1495 }
1496
1497
1498
1499 /*
1500  * delete a floor
1501  */
1502 void cmd_kflr(char *argbuf)
1503 {
1504         struct floor flbuf;
1505         int floor_to_delete;
1506         int kflr_ok;
1507         int delete_ok;
1508
1509         floor_to_delete = extract_int(argbuf, 0);
1510         kflr_ok = extract_int(argbuf, 1);
1511
1512
1513         if (!(CC->logged_in)) {
1514                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1515                 return;
1516         }
1517         if (CC->usersupp.axlevel < 6) {
1518                 cprintf("%d You need higher access to delete floors.\n",
1519                         ERROR + HIGHER_ACCESS_REQUIRED);
1520                 return;
1521         }
1522         lgetfloor(&flbuf, floor_to_delete);
1523
1524         delete_ok = 1;
1525         if ((flbuf.f_flags & F_INUSE) == 0) {
1526                 cprintf("%d Floor %d not in use.\n",
1527                         ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
1528                 delete_ok = 0;
1529         } else {
1530                 if (flbuf.f_ref_count != 0) {
1531                         cprintf("%d Cannot delete; floor contains %d rooms.\n",
1532                                 ERROR + INVALID_FLOOR_OPERATION,
1533                                 flbuf.f_ref_count);
1534                         delete_ok = 0;
1535                 } else {
1536                         if (kflr_ok == 1) {
1537                                 cprintf("%d Ok\n", OK);
1538                         } else {
1539                                 cprintf("%d Ok to delete...\n", OK);
1540                         }
1541
1542                 }
1543
1544         }
1545
1546         if ((delete_ok == 1) && (kflr_ok == 1))
1547                 flbuf.f_flags = 0;
1548         lputfloor(&flbuf, floor_to_delete);
1549 }
1550
1551 /*
1552  * edit a floor
1553  */
1554 void cmd_eflr(char *argbuf)
1555 {
1556         struct floor flbuf;
1557         int floor_num;
1558         int np;
1559
1560         np = num_parms(argbuf);
1561         if (np < 1) {
1562                 cprintf("%d Usage error.\n", ERROR);
1563                 return;
1564         }
1565         if (!(CC->logged_in)) {
1566                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1567                 return;
1568         }
1569         if (CC->usersupp.axlevel < 6) {
1570                 cprintf("%d You need higher access to edit floors.\n",
1571                         ERROR + HIGHER_ACCESS_REQUIRED);
1572                 return;
1573         }
1574         floor_num = extract_int(argbuf, 0);
1575         lgetfloor(&flbuf, floor_num);
1576         if ((flbuf.f_flags & F_INUSE) == 0) {
1577                 lputfloor(&flbuf, floor_num);
1578                 cprintf("%d Floor %d is not in use.\n",
1579                         ERROR + INVALID_FLOOR_OPERATION, floor_num);
1580                 return;
1581         }
1582         if (np >= 2)
1583                 extract(flbuf.f_name, argbuf, 1);
1584         lputfloor(&flbuf, floor_num);
1585
1586         cprintf("%d Ok\n", OK);
1587 }