ebf81401ef0891a660cc2be0dfb6be6783704676
[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         set_wtmpsupp_to_current_room();
683 }
684
685
686 /* 
687  * cmd_goto()  -  goto a new room
688  */
689 void cmd_goto(char *gargs)
690 {
691         struct quickroom QRscratch;
692         int c;
693         int ok = 0;
694         int ra;
695         char augmented_roomname[256];
696         char towhere[256];
697         char password[256];
698
699         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
700                 cprintf("%d not logged in\n", ERROR + NOT_LOGGED_IN);
701                 return;
702         }
703
704         extract(towhere, gargs, 0);
705         extract(password, gargs, 1);
706
707         getuser(&CC->usersupp, CC->curr_user);
708
709         if (!strcasecmp(towhere, "_BASEROOM_"))
710                 strcpy(towhere, BASEROOM);
711
712         if (!strcasecmp(towhere, "_MAIL_"))
713                 strcpy(towhere, MAILROOM);
714
715         if (!strcasecmp(towhere, "_BITBUCKET_"))
716                 strcpy(towhere, config.c_twitroom);
717
718
719         /* First try a regular match */
720         c = getroom(&QRscratch, towhere);
721
722         /* Then try a mailbox name match */
723         if (c != 0) {
724                 MailboxName(augmented_roomname, &CC->usersupp, towhere);
725                 c = getroom(&QRscratch, augmented_roomname);
726                 if (c == 0)
727                         strcpy(towhere, augmented_roomname);
728         }
729
730         /* And if the room was found... */
731         if (c == 0) {
732
733                 /* let internal programs go directly to any room */
734                 if (CC->internal_pgm) {
735                         usergoto(towhere, 1);
736                         return;
737                 }
738
739                 /* See if there is an existing user/room relationship */
740                 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
741
742                 /* normal clients have to pass through security */
743                 if (ra & UA_GOTOALLOWED)
744                         ok = 1;
745
746                 if (ok == 1) {
747                         if ((QRscratch.QRflags & QR_PASSWORDED) &&
748                             ((ra & UA_KNOWN) == 0) &&
749                             (strcasecmp(QRscratch.QRpasswd, password))
750                             ) {
751                                 cprintf("%d wrong or missing passwd\n",
752                                         ERROR + PASSWORD_REQUIRED);
753                                 return;
754                         } else if ((QRscratch.QRflags & QR_PRIVATE) &&
755                                    ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
756                                    ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
757                                    ((ra & UA_KNOWN) == 0)) {
758                                 goto NOPE;
759                         } else {
760                                 usergoto(towhere, 1);
761                                 return;
762                         }
763                 }
764         }
765
766 NOPE:   cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
767 }
768
769
770 void cmd_whok(void)
771 {
772         struct usersupp temp;
773         struct cdbdata *cdbus;
774
775         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
776                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
777                 return;
778         }
779         getuser(&CC->usersupp, CC->curr_user);
780
781         if ((!is_room_aide()) && (!(CC->internal_pgm))) {
782                 cprintf("%d Higher access required.\n",
783                         ERROR + HIGHER_ACCESS_REQUIRED);
784                 return;
785         }
786         cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
787         cdb_rewind(CDB_USERSUPP);
788         while (cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
789                 memset(&temp, 0, sizeof temp);
790                 memcpy(&temp, cdbus->ptr, sizeof temp);
791                 cdb_free(cdbus);
792
793                 if ((CC->quickroom.QRflags & QR_INUSE)
794                     && (CtdlRoomAccess(&CC->quickroom, &temp) & UA_KNOWN)
795                     )
796                         cprintf("%s\n", temp.fullname);
797         }
798         cprintf("000\n");
799 }
800
801
802 /*
803  * RDIR command for room directory
804  */
805 void cmd_rdir(void)
806 {
807         char buf[256];
808         char flnm[256];
809         char comment[256];
810         FILE *ls, *fd;
811         struct stat statbuf;
812
813         if (!(CC->logged_in)) {
814                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
815                 return;
816         }
817         getroom(&CC->quickroom, CC->quickroom.QRname);
818         getuser(&CC->usersupp, CC->curr_user);
819
820         if ((CC->quickroom.QRflags & QR_DIRECTORY) == 0) {
821                 cprintf("%d not here.\n", ERROR + NOT_HERE);
822                 return;
823         }
824         if (((CC->quickroom.QRflags & QR_VISDIR) == 0)
825             && (CC->usersupp.axlevel < 6)
826             && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
827                 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
828                 return;
829         }
830         cprintf("%d %s|%s/files/%s\n",
831         LISTING_FOLLOWS, config.c_fqdn, BBSDIR, CC->quickroom.QRdirname);
832
833         sprintf(buf, "ls %s/files/%s  >%s 2> /dev/null",
834                 BBSDIR, CC->quickroom.QRdirname, CC->temp);
835         system(buf);
836
837         sprintf(buf, "%s/files/%s/filedir", BBSDIR, CC->quickroom.QRdirname);
838         fd = fopen(buf, "r");
839         if (fd == NULL)
840                 fd = fopen("/dev/null", "r");
841
842         ls = fopen(CC->temp, "r");
843         while (fgets(flnm, 256, ls) != NULL) {
844                 flnm[strlen(flnm) - 1] = 0;
845                 if (strcasecmp(flnm, "filedir")) {
846                         sprintf(buf, "%s/files/%s/%s",
847                                 BBSDIR, CC->quickroom.QRdirname, flnm);
848                         stat(buf, &statbuf);
849                         strcpy(comment, "");
850                         fseek(fd, 0L, 0);
851                         while ((fgets(buf, 256, fd) != NULL)
852                                && (strlen(comment) == 0)) {
853                                 buf[strlen(buf) - 1] = 0;
854                                 if ((!strncasecmp(buf, flnm, strlen(flnm)))
855                                     && (buf[strlen(flnm)] == ' '))
856                                         strncpy(comment,
857                                             &buf[strlen(flnm) + 1], 255);
858                         }
859                         cprintf("%s|%ld|%s\n", flnm, statbuf.st_size, comment);
860                 }
861         }
862         fclose(ls);
863         fclose(fd);
864         unlink(CC->temp);
865
866         cprintf("000\n");
867 }
868
869 /*
870  * get room parameters (aide or room aide command)
871  */
872 void cmd_getr(void)
873 {
874         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
875                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
876                 return;
877         }
878         if ((!is_room_aide()) && (!(CC->internal_pgm))) {
879                 cprintf("%d Higher access required.\n",
880                         ERROR + HIGHER_ACCESS_REQUIRED);
881                 return;
882         }
883         if (is_noneditable(&CC->quickroom)) {
884                 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
885                 return;
886         }
887         getroom(&CC->quickroom, CC->quickroom.QRname);
888         cprintf("%d%c%s|%s|%s|%d|%d|%d\n",
889                 OK, check_express(),
890                 CC->quickroom.QRname,
891                 ((CC->quickroom.QRflags & QR_PASSWORDED) ? CC->quickroom.QRpasswd : ""),
892                 ((CC->quickroom.QRflags & QR_DIRECTORY) ? CC->quickroom.QRdirname : ""),
893                 CC->quickroom.QRflags,
894                 (int) CC->quickroom.QRfloor,
895                 (int) CC->quickroom.QRorder);
896 }
897
898
899 /*
900  * set room parameters (aide or room aide command)
901  */
902 void cmd_setr(char *args)
903 {
904         char buf[256];
905         struct floor flbuf;
906         char old_name[ROOMNAMELEN];
907         int old_floor;
908         int new_order = 0;
909
910         if (!(CC->logged_in)) {
911                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
912                 return;
913         }
914         if (!is_room_aide()) {
915                 cprintf("%d Higher access required.\n",
916                         ERROR + HIGHER_ACCESS_REQUIRED);
917                 return;
918         }
919         if (is_noneditable(&CC->quickroom)) {
920                 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
921                 return;
922         }
923         if (num_parms(args) >= 6) {
924                 getfloor(&flbuf, extract_int(args, 5));
925                 if ((flbuf.f_flags & F_INUSE) == 0) {
926                         cprintf("%d Invalid floor number.\n",
927                                 ERROR + INVALID_FLOOR_OPERATION);
928                         return;
929                 }
930         }
931         if (num_parms(args) >= 7) {
932                 new_order = extract_int(args, 6);
933                 if (new_order < 1)
934                         new_order = 1;
935                 if (new_order > 127)
936                         new_order = 127;
937         }
938         lgetroom(&CC->quickroom, CC->quickroom.QRname);
939         strcpy(old_name, CC->quickroom.QRname);
940         extract(buf, args, 0);
941         buf[ROOMNAMELEN] = 0;
942         strncpy(CC->quickroom.QRname, buf, ROOMNAMELEN - 1);
943         extract(buf, args, 1);
944         buf[10] = 0;
945         strncpy(CC->quickroom.QRpasswd, buf, 9);
946         extract(buf, args, 2);
947         buf[15] = 0;
948         strncpy(CC->quickroom.QRdirname, buf, 19);
949         CC->quickroom.QRflags = (extract_int(args, 3) | QR_INUSE);
950         if (num_parms(args) >= 7)
951                 CC->quickroom.QRorder = (char) new_order;
952
953         /* Clean up a client boo-boo: if the client set the room to
954          * guess-name or passworded, ensure that the private flag is
955          * also set.
956          */
957         if ((CC->quickroom.QRflags & QR_GUESSNAME)
958             || (CC->quickroom.QRflags & QR_PASSWORDED))
959                 CC->quickroom.QRflags |= QR_PRIVATE;
960
961         /* Kick everyone out if the client requested it (by changing the
962          * room's generation number)
963          */
964         if (extract_int(args, 4)) {
965                 time(&CC->quickroom.QRgen);
966         }
967         old_floor = CC->quickroom.QRfloor;
968         if (num_parms(args) >= 6) {
969                 CC->quickroom.QRfloor = extract_int(args, 5);
970         }
971         /* Write the room record back to disk */
972         lputroom(&CC->quickroom);
973
974         /* If the room name changed, then there are now two room records,
975          * so we have to delete the old one.
976          */
977         if (strcasecmp(CC->quickroom.QRname, old_name)) {
978                 b_deleteroom(old_name);
979         }
980         /* adjust the floor reference counts */
981         lgetfloor(&flbuf, old_floor);
982         --flbuf.f_ref_count;
983         lputfloor(&flbuf, old_floor);
984         lgetfloor(&flbuf, CC->quickroom.QRfloor);
985         ++flbuf.f_ref_count;
986         lputfloor(&flbuf, CC->quickroom.QRfloor);
987
988         /* create a room directory if necessary */
989         if (CC->quickroom.QRflags & QR_DIRECTORY) {
990                 sprintf(buf,
991                     "mkdir ./files/%s </dev/null >/dev/null 2>/dev/null",
992                         CC->quickroom.QRdirname);
993                 system(buf);
994         }
995         sprintf(buf, "%s> edited by %s\n", CC->quickroom.QRname, CC->curr_user);
996         aide_message(buf);
997         cprintf("%d Ok\n", OK);
998 }
999
1000
1001
1002 /* 
1003  * get the name of the room aide for this room
1004  */
1005 void cmd_geta(void)
1006 {
1007         struct usersupp usbuf;
1008
1009         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1010                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1011                 return;
1012         }
1013         if (is_noneditable(&CC->quickroom)) {
1014                 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
1015                 return;
1016         }
1017         if (getuserbynumber(&usbuf, CC->quickroom.QRroomaide) == 0) {
1018                 cprintf("%d %s\n", OK, usbuf.fullname);
1019         } else {
1020                 cprintf("%d \n", OK);
1021         }
1022 }
1023
1024
1025 /* 
1026  * set the room aide for this room
1027  */
1028 void cmd_seta(char *new_ra)
1029 {
1030         struct usersupp usbuf;
1031         long newu;
1032         char buf[256];
1033         int post_notice;
1034
1035         if (!(CC->logged_in)) {
1036                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1037                 return;
1038         }
1039         if (!is_room_aide()) {
1040                 cprintf("%d Higher access required.\n",
1041                         ERROR + HIGHER_ACCESS_REQUIRED);
1042                 return;
1043         }
1044         if (getuser(&usbuf, new_ra) != 0) {
1045                 newu = (-1L);
1046         } else {
1047                 newu = usbuf.usernum;
1048         }
1049
1050         lgetroom(&CC->quickroom, CC->quickroom.QRname);
1051         post_notice = 0;
1052         if (CC->quickroom.QRroomaide != newu) {
1053                 post_notice = 1;
1054         }
1055         CC->quickroom.QRroomaide = newu;
1056         lputroom(&CC->quickroom);
1057
1058         /*
1059          * We have to post the change notice _after_ writing changes to 
1060          * the room table, otherwise it would deadlock!
1061          */
1062         if (post_notice == 1) {
1063                 sprintf(buf, "%s is now room aide for %s>\n",
1064                         usbuf.fullname, CC->quickroom.QRname);
1065                 aide_message(buf);
1066         }
1067         cprintf("%d Ok\n", OK);
1068 }
1069
1070 /*
1071  * Generate an associated file name for a room
1072  */
1073 void assoc_file_name(char *buf, struct quickroom *qrbuf, char *prefix)
1074 {
1075         sprintf(buf, "./%s/%ld", prefix, qrbuf->QRnumber);
1076 }
1077
1078 /* 
1079  * retrieve info file for this room
1080  */
1081 void cmd_rinf(void)
1082 {
1083         char filename[128];
1084         char buf[256];
1085         FILE *info_fp;
1086
1087         assoc_file_name(filename, &CC->quickroom, "info");
1088         info_fp = fopen(filename, "r");
1089
1090         if (info_fp == NULL) {
1091                 cprintf("%d No info file.\n", ERROR);
1092                 return;
1093         }
1094         cprintf("%d Info:\n", LISTING_FOLLOWS);
1095         while (fgets(buf, 256, info_fp) != NULL) {
1096                 if (strlen(buf) > 0)
1097                         buf[strlen(buf) - 1] = 0;
1098                 cprintf("%s\n", buf);
1099         }
1100         cprintf("000\n");
1101         fclose(info_fp);
1102 }
1103
1104 /*
1105  * Back end processing to delete a room and everything associated with it
1106  */
1107 void delete_room(struct quickroom *qrbuf)
1108 {
1109         struct floor flbuf;
1110         char filename[100];
1111
1112         lprintf(9, "Deleting room <%s>\n", qrbuf->QRname);
1113
1114         /* Delete the info file */
1115         assoc_file_name(filename, qrbuf, "info");
1116         unlink(filename);
1117
1118         /* Delete the image file */
1119         assoc_file_name(filename, qrbuf, "images");
1120         unlink(filename);
1121
1122         /* Delete the messages in the room
1123          * (Careful: this opens an S_QUICKROOM critical section!)
1124          */
1125         CtdlDeleteMessages(qrbuf->QRname, 0L, NULL);
1126
1127         /* Flag the room record as not in use */
1128         lgetroom(qrbuf, qrbuf->QRname);
1129         qrbuf->QRflags = 0;
1130         lputroom(qrbuf);
1131
1132         /* then decrement the reference count for the floor */
1133         lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1134         flbuf.f_ref_count = flbuf.f_ref_count - 1;
1135         lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1136
1137         /* Delete the room record from the database! */
1138         b_deleteroom(qrbuf->QRname);
1139 }
1140
1141
1142 /*
1143  * aide command: kill the current room
1144  */
1145 void cmd_kill(char *argbuf)
1146 {
1147         char aaa[100];
1148         char deleted_room_name[ROOMNAMELEN];
1149         int kill_ok;
1150
1151         kill_ok = extract_int(argbuf, 0);
1152
1153         if (!(CC->logged_in)) {
1154                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1155                 return;
1156         }
1157         if (!is_room_aide()) {
1158                 cprintf("%d Higher access required.\n",
1159                         ERROR + HIGHER_ACCESS_REQUIRED);
1160                 return;
1161         }
1162         if (is_noneditable(&CC->quickroom)) {
1163                 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
1164                 return;
1165         }
1166         if (kill_ok) {
1167                 strcpy(deleted_room_name, CC->quickroom.QRname);
1168                 delete_room(&CC->quickroom);    /* Do the dirty work */
1169                 usergoto(BASEROOM, 0);  /* Return to the Lobby */
1170
1171                 /* tell the world what we did */
1172                 sprintf(aaa, "%s> killed by %s\n",
1173                         deleted_room_name, CC->curr_user);
1174                 aide_message(aaa);
1175                 cprintf("%d '%s' deleted.\n", OK, deleted_room_name);
1176         } else {
1177                 cprintf("%d ok to delete.\n", OK);
1178         }
1179 }
1180
1181
1182 /*
1183  * Internal code to create a new room (returns room flags)
1184  *
1185  * Room types:  0=public, 1=guessname, 2=passworded, 3=inv-only, 4=mailbox
1186  */
1187 unsigned create_room(char *new_room_name,
1188                      int new_room_type,
1189                      char *new_room_pass,
1190                      int new_room_floor)
1191 {
1192
1193         struct quickroom qrbuf;
1194         struct floor flbuf;
1195         struct visit vbuf;
1196
1197         if (getroom(&qrbuf, new_room_name) == 0)
1198                 return (0);     /* already exists */
1199
1200         memset(&qrbuf, 0, sizeof(struct quickroom));
1201         strncpy(qrbuf.QRname, new_room_name, ROOMNAMELEN);
1202         strncpy(qrbuf.QRpasswd, new_room_pass, 9);
1203         qrbuf.QRflags = QR_INUSE;
1204         qrbuf.QRnumber = get_new_room_number();
1205         if (new_room_type > 0)
1206                 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1207         if (new_room_type == 1)
1208                 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1209         if (new_room_type == 2)
1210                 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1211         if (new_room_type == 4)
1212                 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1213
1214         /* If the room is private, and the system administrator has elected
1215          * to automatically grant room aide privileges, do so now; otherwise,
1216          * set the room aide to undefined.
1217          */
1218         if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1219                 qrbuf.QRroomaide = CC->usersupp.usernum;
1220         } else {
1221                 qrbuf.QRroomaide = (-1L);
1222         }
1223
1224         qrbuf.QRhighest = 0L;   /* No messages in this room yet */
1225         time(&qrbuf.QRgen);     /* Use a timestamp as the generation number */
1226         qrbuf.QRfloor = new_room_floor;
1227
1228         /* save what we just did... */
1229         putroom(&qrbuf);
1230
1231         /* bump the reference count on whatever floor the room is on */
1232         lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1233         flbuf.f_ref_count = flbuf.f_ref_count + 1;
1234         lputfloor(&flbuf, (int) qrbuf.QRfloor);
1235
1236         /* be sure not to kick the creator out of the room! */
1237         lgetuser(&CC->usersupp, CC->curr_user);
1238         CtdlGetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1239         vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1240         vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1241         CtdlSetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1242         lputuser(&CC->usersupp);
1243
1244         /* resume our happy day */
1245         return (qrbuf.QRflags);
1246 }
1247
1248
1249 /*
1250  * create a new room
1251  */
1252 void cmd_cre8(char *args)
1253 {
1254         int cre8_ok;
1255         char new_room_name[256];
1256         int new_room_type;
1257         char new_room_pass[256];
1258         int new_room_floor;
1259         char aaa[256];
1260         unsigned newflags;
1261         struct quickroom qrbuf;
1262         struct floor flbuf;
1263
1264         cre8_ok = extract_int(args, 0);
1265         extract(new_room_name, args, 1);
1266         new_room_name[ROOMNAMELEN - 1] = 0;
1267         new_room_type = extract_int(args, 2);
1268         extract(new_room_pass, args, 3);
1269         new_room_pass[9] = 0;
1270         new_room_floor = 0;
1271
1272         if ((strlen(new_room_name) == 0) && (cre8_ok == 1)) {
1273                 cprintf("%d Invalid room name.\n", ERROR);
1274                 return;
1275         }
1276
1277         if (!strcasecmp(new_room_name, MAILROOM)) {
1278                 cprintf("%d '%s' already exists.\n",
1279                         ERROR + ALREADY_EXISTS, new_room_name);
1280                 return;
1281         }
1282
1283         if (num_parms(args) >= 5) {
1284                 getfloor(&flbuf, extract_int(args, 4));
1285                 if ((flbuf.f_flags & F_INUSE) == 0) {
1286                         cprintf("%d Invalid floor number.\n",
1287                                 ERROR + INVALID_FLOOR_OPERATION);
1288                         return;
1289                 } else {
1290                         new_room_floor = extract_int(args, 4);
1291                 }
1292         }
1293
1294         if (!(CC->logged_in)) {
1295                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1296                 return;
1297         }
1298
1299         if (CC->usersupp.axlevel < config.c_createax) {
1300                 cprintf("%d You need higher access to create rooms.\n",
1301                         ERROR + HIGHER_ACCESS_REQUIRED);
1302                 return;
1303         }
1304
1305         if ((strlen(new_room_name) == 0) && (cre8_ok == 0)) {
1306                 cprintf("%d Ok to create rooms.\n", OK);
1307                 return;
1308         }
1309
1310         if ((new_room_type < 0) || (new_room_type > 4)) {
1311                 cprintf("%d Invalid room type.\n", ERROR);
1312                 return;
1313         }
1314
1315         /* If the user is requesting a personal room, set up the room
1316          * name accordingly (prepend the user number)
1317          */
1318         if (new_room_type == 4) {
1319                 sprintf(aaa, "%010ld.%s",
1320                         CC->usersupp.usernum, new_room_name);
1321                 strcpy(new_room_name, aaa);
1322         }
1323
1324         /* Check to make sure the requested room name doesn't already exist */
1325         if (getroom(&qrbuf, new_room_name) == 0) {
1326                 cprintf("%d '%s' already exists.\n",
1327                         ERROR + ALREADY_EXISTS, qrbuf.QRname);
1328                 return;
1329         }
1330
1331         if (cre8_ok == 0) {
1332                 cprintf("%d OK to create '%s'\n", OK, new_room_name);
1333                 return;
1334         }
1335
1336         newflags = create_room(new_room_name,
1337                            new_room_type, new_room_pass, new_room_floor);
1338
1339         /* post a message in Aide> describing the new room */
1340         strncpy(aaa, new_room_name, 255);
1341         strcat(aaa, "> created by ");
1342         strcat(aaa, CC->usersupp.fullname);
1343         if (newflags & QR_MAILBOX)
1344                 strcat(aaa, " [personal]");
1345         else if (newflags & QR_PRIVATE)
1346                 strcat(aaa, " [private]");
1347         if (newflags & QR_GUESSNAME)
1348                 strcat(aaa, "[guessname] ");
1349         if (newflags & QR_PASSWORDED) {
1350                 strcat(aaa, "\n Password: ");
1351                 strcat(aaa, new_room_pass);
1352         }
1353         strcat(aaa, "\n");
1354         aide_message(aaa);
1355
1356         cprintf("%d '%s' has been created.\n", OK, qrbuf.QRname);
1357 }
1358
1359
1360
1361 void cmd_einf(char *ok)
1362 {                               /* enter info file for current room */
1363         FILE *fp;
1364         char infofilename[256];
1365         char buf[256];
1366
1367         if (!(CC->logged_in)) {
1368                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1369                 return;
1370         }
1371         if (!is_room_aide()) {
1372                 cprintf("%d Higher access required.\n",
1373                         ERROR + HIGHER_ACCESS_REQUIRED);
1374                 return;
1375         }
1376         if (atoi(ok) == 0) {
1377                 cprintf("%d Ok.\n", OK);
1378                 return;
1379         }
1380         assoc_file_name(infofilename, &CC->quickroom, "info");
1381         lprintf(9, "opening\n");
1382         fp = fopen(infofilename, "w");
1383         lprintf(9, "checking\n");
1384         if (fp == NULL) {
1385                 cprintf("%d Cannot open %s: %s\n",
1386                   ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1387                 return;
1388         }
1389         cprintf("%d Send info...\n", SEND_LISTING);
1390
1391         do {
1392                 client_gets(buf);
1393                 if (strcmp(buf, "000"))
1394                         fprintf(fp, "%s\n", buf);
1395         } while (strcmp(buf, "000"));
1396         fclose(fp);
1397
1398         /* now update the room index so people will see our new info */
1399         lgetroom(&CC->quickroom, CC->quickroom.QRname);         /* lock so no one steps on us */
1400         CC->quickroom.QRinfo = CC->quickroom.QRhighest + 1L;
1401         lputroom(&CC->quickroom);
1402 }
1403
1404
1405 /* 
1406  * cmd_lflr()   -  List all known floors
1407  */
1408 void cmd_lflr(void)
1409 {
1410         int a;
1411         struct floor flbuf;
1412
1413         if (!(CC->logged_in)) {
1414                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1415                 return;
1416         }
1417         /* if (getuser(&CC->usersupp,CC->curr_user)) {
1418            cprintf("%d Can't locate user!\n",ERROR+INTERNAL_ERROR);
1419            return;
1420            }
1421          */
1422
1423         cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1424
1425         for (a = 0; a < MAXFLOORS; ++a) {
1426                 getfloor(&flbuf, a);
1427                 if (flbuf.f_flags & F_INUSE) {
1428                         cprintf("%d|%s|%d\n",
1429                                 a,
1430                                 flbuf.f_name,
1431                                 flbuf.f_ref_count);
1432                 }
1433         }
1434         cprintf("000\n");
1435 }
1436
1437
1438
1439 /*
1440  * create a new floor
1441  */
1442 void cmd_cflr(char *argbuf)
1443 {
1444         char new_floor_name[256];
1445         struct floor flbuf;
1446         int cflr_ok;
1447         int free_slot = (-1);
1448         int a;
1449
1450         extract(new_floor_name, argbuf, 0);
1451         cflr_ok = extract_int(argbuf, 1);
1452
1453
1454         if (!(CC->logged_in)) {
1455                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1456                 return;
1457         }
1458         if (CC->usersupp.axlevel < 6) {
1459                 cprintf("%d You need higher access to create rooms.\n",
1460                         ERROR + HIGHER_ACCESS_REQUIRED);
1461                 return;
1462         }
1463         for (a = 0; a < MAXFLOORS; ++a) {
1464                 getfloor(&flbuf, a);
1465
1466                 /* note any free slots while we're scanning... */
1467                 if (((flbuf.f_flags & F_INUSE) == 0)
1468                     && (free_slot < 0))
1469                         free_slot = a;
1470
1471                 /* check to see if it already exists */
1472                 if ((!strcasecmp(flbuf.f_name, new_floor_name))
1473                     && (flbuf.f_flags & F_INUSE)) {
1474                         cprintf("%d Floor '%s' already exists.\n",
1475                                 ERROR + ALREADY_EXISTS,
1476                                 flbuf.f_name);
1477                         return;
1478                 }
1479         }
1480
1481         if (free_slot < 0) {
1482                 cprintf("%d There is no space available for a new floor.\n",
1483                         ERROR + INVALID_FLOOR_OPERATION);
1484                 return;
1485         }
1486         if (cflr_ok == 0) {
1487                 cprintf("%d ok to create...\n", OK);
1488                 return;
1489         }
1490         lgetfloor(&flbuf, free_slot);
1491         flbuf.f_flags = F_INUSE;
1492         flbuf.f_ref_count = 0;
1493         strncpy(flbuf.f_name, new_floor_name, 255);
1494         lputfloor(&flbuf, free_slot);
1495         cprintf("%d %d\n", OK, free_slot);
1496 }
1497
1498
1499
1500 /*
1501  * delete a floor
1502  */
1503 void cmd_kflr(char *argbuf)
1504 {
1505         struct floor flbuf;
1506         int floor_to_delete;
1507         int kflr_ok;
1508         int delete_ok;
1509
1510         floor_to_delete = extract_int(argbuf, 0);
1511         kflr_ok = extract_int(argbuf, 1);
1512
1513
1514         if (!(CC->logged_in)) {
1515                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1516                 return;
1517         }
1518         if (CC->usersupp.axlevel < 6) {
1519                 cprintf("%d You need higher access to delete floors.\n",
1520                         ERROR + HIGHER_ACCESS_REQUIRED);
1521                 return;
1522         }
1523         lgetfloor(&flbuf, floor_to_delete);
1524
1525         delete_ok = 1;
1526         if ((flbuf.f_flags & F_INUSE) == 0) {
1527                 cprintf("%d Floor %d not in use.\n",
1528                         ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
1529                 delete_ok = 0;
1530         } else {
1531                 if (flbuf.f_ref_count != 0) {
1532                         cprintf("%d Cannot delete; floor contains %d rooms.\n",
1533                                 ERROR + INVALID_FLOOR_OPERATION,
1534                                 flbuf.f_ref_count);
1535                         delete_ok = 0;
1536                 } else {
1537                         if (kflr_ok == 1) {
1538                                 cprintf("%d Ok\n", OK);
1539                         } else {
1540                                 cprintf("%d Ok to delete...\n", OK);
1541                         }
1542
1543                 }
1544
1545         }
1546
1547         if ((delete_ok == 1) && (kflr_ok == 1))
1548                 flbuf.f_flags = 0;
1549         lputfloor(&flbuf, floor_to_delete);
1550 }
1551
1552 /*
1553  * edit a floor
1554  */
1555 void cmd_eflr(char *argbuf)
1556 {
1557         struct floor flbuf;
1558         int floor_num;
1559         int np;
1560
1561         np = num_parms(argbuf);
1562         if (np < 1) {
1563                 cprintf("%d Usage error.\n", ERROR);
1564                 return;
1565         }
1566         if (!(CC->logged_in)) {
1567                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1568                 return;
1569         }
1570         if (CC->usersupp.axlevel < 6) {
1571                 cprintf("%d You need higher access to edit floors.\n",
1572                         ERROR + HIGHER_ACCESS_REQUIRED);
1573                 return;
1574         }
1575         floor_num = extract_int(argbuf, 0);
1576         lgetfloor(&flbuf, floor_num);
1577         if ((flbuf.f_flags & F_INUSE) == 0) {
1578                 lputfloor(&flbuf, floor_num);
1579                 cprintf("%d Floor %d is not in use.\n",
1580                         ERROR + INVALID_FLOOR_OPERATION, floor_num);
1581                 return;
1582         }
1583         if (np >= 2)
1584                 extract(flbuf.f_name, argbuf, 1);
1585         lputfloor(&flbuf, floor_num);
1586
1587         cprintf("%d Ok\n", OK);
1588 }