9dd7ed0ed51b9e4bccb42ad1df99be730e3da38b
[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                                         safestrncpy(comment,
856                                             &buf[strlen(flnm) + 1],
857                                             sizeof comment);
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         safestrncpy(CC->quickroom.QRname, buf, sizeof CC->quickroom.QRname);
943         extract(buf, args, 1);
944         buf[10] = 0;
945         safestrncpy(CC->quickroom.QRpasswd, buf, sizeof CC->quickroom.QRpasswd);
946         extract(buf, args, 2);
947         buf[15] = 0;
948         safestrncpy(CC->quickroom.QRdirname, buf,
949                 sizeof CC->quickroom.QRdirname);
950         CC->quickroom.QRflags = (extract_int(args, 3) | QR_INUSE);
951         if (num_parms(args) >= 7)
952                 CC->quickroom.QRorder = (char) new_order;
953
954         /* Clean up a client boo-boo: if the client set the room to
955          * guess-name or passworded, ensure that the private flag is
956          * also set.
957          */
958         if ((CC->quickroom.QRflags & QR_GUESSNAME)
959             || (CC->quickroom.QRflags & QR_PASSWORDED))
960                 CC->quickroom.QRflags |= QR_PRIVATE;
961
962         /* Kick everyone out if the client requested it (by changing the
963          * room's generation number)
964          */
965         if (extract_int(args, 4)) {
966                 time(&CC->quickroom.QRgen);
967         }
968         old_floor = CC->quickroom.QRfloor;
969         if (num_parms(args) >= 6) {
970                 CC->quickroom.QRfloor = extract_int(args, 5);
971         }
972         /* Write the room record back to disk */
973         lputroom(&CC->quickroom);
974
975         /* If the room name changed, then there are now two room records,
976          * so we have to delete the old one.
977          */
978         if (strcasecmp(CC->quickroom.QRname, old_name)) {
979                 b_deleteroom(old_name);
980         }
981         /* adjust the floor reference counts */
982         lgetfloor(&flbuf, old_floor);
983         --flbuf.f_ref_count;
984         lputfloor(&flbuf, old_floor);
985         lgetfloor(&flbuf, CC->quickroom.QRfloor);
986         ++flbuf.f_ref_count;
987         lputfloor(&flbuf, CC->quickroom.QRfloor);
988
989         /* create a room directory if necessary */
990         if (CC->quickroom.QRflags & QR_DIRECTORY) {
991                 sprintf(buf,
992                     "mkdir ./files/%s </dev/null >/dev/null 2>/dev/null",
993                         CC->quickroom.QRdirname);
994                 system(buf);
995         }
996         sprintf(buf, "%s> edited by %s\n", CC->quickroom.QRname, CC->curr_user);
997         aide_message(buf);
998         cprintf("%d Ok\n", OK);
999 }
1000
1001
1002
1003 /* 
1004  * get the name of the room aide for this room
1005  */
1006 void cmd_geta(void)
1007 {
1008         struct usersupp usbuf;
1009
1010         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1011                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1012                 return;
1013         }
1014         if (is_noneditable(&CC->quickroom)) {
1015                 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
1016                 return;
1017         }
1018         if (getuserbynumber(&usbuf, CC->quickroom.QRroomaide) == 0) {
1019                 cprintf("%d %s\n", OK, usbuf.fullname);
1020         } else {
1021                 cprintf("%d \n", OK);
1022         }
1023 }
1024
1025
1026 /* 
1027  * set the room aide for this room
1028  */
1029 void cmd_seta(char *new_ra)
1030 {
1031         struct usersupp usbuf;
1032         long newu;
1033         char buf[256];
1034         int post_notice;
1035
1036         if (!(CC->logged_in)) {
1037                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1038                 return;
1039         }
1040         if (!is_room_aide()) {
1041                 cprintf("%d Higher access required.\n",
1042                         ERROR + HIGHER_ACCESS_REQUIRED);
1043                 return;
1044         }
1045         if (getuser(&usbuf, new_ra) != 0) {
1046                 newu = (-1L);
1047         } else {
1048                 newu = usbuf.usernum;
1049         }
1050
1051         lgetroom(&CC->quickroom, CC->quickroom.QRname);
1052         post_notice = 0;
1053         if (CC->quickroom.QRroomaide != newu) {
1054                 post_notice = 1;
1055         }
1056         CC->quickroom.QRroomaide = newu;
1057         lputroom(&CC->quickroom);
1058
1059         /*
1060          * We have to post the change notice _after_ writing changes to 
1061          * the room table, otherwise it would deadlock!
1062          */
1063         if (post_notice == 1) {
1064                 sprintf(buf, "%s is now room aide for %s>\n",
1065                         usbuf.fullname, CC->quickroom.QRname);
1066                 aide_message(buf);
1067         }
1068         cprintf("%d Ok\n", OK);
1069 }
1070
1071 /*
1072  * Generate an associated file name for a room
1073  */
1074 void assoc_file_name(char *buf, struct quickroom *qrbuf, char *prefix)
1075 {
1076         sprintf(buf, "./%s/%ld", prefix, qrbuf->QRnumber);
1077 }
1078
1079 /* 
1080  * retrieve info file for this room
1081  */
1082 void cmd_rinf(void)
1083 {
1084         char filename[128];
1085         char buf[256];
1086         FILE *info_fp;
1087
1088         assoc_file_name(filename, &CC->quickroom, "info");
1089         info_fp = fopen(filename, "r");
1090
1091         if (info_fp == NULL) {
1092                 cprintf("%d No info file.\n", ERROR);
1093                 return;
1094         }
1095         cprintf("%d Info:\n", LISTING_FOLLOWS);
1096         while (fgets(buf, 256, info_fp) != NULL) {
1097                 if (strlen(buf) > 0)
1098                         buf[strlen(buf) - 1] = 0;
1099                 cprintf("%s\n", buf);
1100         }
1101         cprintf("000\n");
1102         fclose(info_fp);
1103 }
1104
1105 /*
1106  * Back end processing to delete a room and everything associated with it
1107  */
1108 void delete_room(struct quickroom *qrbuf)
1109 {
1110         struct floor flbuf;
1111         char filename[100];
1112
1113         lprintf(9, "Deleting room <%s>\n", qrbuf->QRname);
1114
1115         /* Delete the info file */
1116         assoc_file_name(filename, qrbuf, "info");
1117         unlink(filename);
1118
1119         /* Delete the image file */
1120         assoc_file_name(filename, qrbuf, "images");
1121         unlink(filename);
1122
1123         /* Delete the messages in the room
1124          * (Careful: this opens an S_QUICKROOM critical section!)
1125          */
1126         CtdlDeleteMessages(qrbuf->QRname, 0L, NULL);
1127
1128         /* Flag the room record as not in use */
1129         lgetroom(qrbuf, qrbuf->QRname);
1130         qrbuf->QRflags = 0;
1131         lputroom(qrbuf);
1132
1133         /* then decrement the reference count for the floor */
1134         lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1135         flbuf.f_ref_count = flbuf.f_ref_count - 1;
1136         lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1137
1138         /* Delete the room record from the database! */
1139         b_deleteroom(qrbuf->QRname);
1140 }
1141
1142
1143 /*
1144  * aide command: kill the current room
1145  */
1146 void cmd_kill(char *argbuf)
1147 {
1148         char aaa[100];
1149         char deleted_room_name[ROOMNAMELEN];
1150         int kill_ok;
1151
1152         kill_ok = extract_int(argbuf, 0);
1153
1154         if (!(CC->logged_in)) {
1155                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1156                 return;
1157         }
1158         if (!is_room_aide()) {
1159                 cprintf("%d Higher access required.\n",
1160                         ERROR + HIGHER_ACCESS_REQUIRED);
1161                 return;
1162         }
1163         if (is_noneditable(&CC->quickroom)) {
1164                 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
1165                 return;
1166         }
1167         if (kill_ok) {
1168                 strcpy(deleted_room_name, CC->quickroom.QRname);
1169                 delete_room(&CC->quickroom);    /* Do the dirty work */
1170                 usergoto(BASEROOM, 0);  /* Return to the Lobby */
1171
1172                 /* tell the world what we did */
1173                 sprintf(aaa, "%s> killed by %s\n",
1174                         deleted_room_name, CC->curr_user);
1175                 aide_message(aaa);
1176                 cprintf("%d '%s' deleted.\n", OK, deleted_room_name);
1177         } else {
1178                 cprintf("%d ok to delete.\n", OK);
1179         }
1180 }
1181
1182
1183 /*
1184  * Internal code to create a new room (returns room flags)
1185  *
1186  * Room types:  0=public, 1=guessname, 2=passworded, 3=inv-only, 4=mailbox
1187  */
1188 unsigned create_room(char *new_room_name,
1189                      int new_room_type,
1190                      char *new_room_pass,
1191                      int new_room_floor)
1192 {
1193
1194         struct quickroom qrbuf;
1195         struct floor flbuf;
1196         struct visit vbuf;
1197
1198         if (getroom(&qrbuf, new_room_name) == 0)
1199                 return (0);     /* already exists */
1200
1201         memset(&qrbuf, 0, sizeof(struct quickroom));
1202         safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1203         safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1204         qrbuf.QRflags = QR_INUSE;
1205         qrbuf.QRnumber = get_new_room_number();
1206         if (new_room_type > 0)
1207                 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1208         if (new_room_type == 1)
1209                 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1210         if (new_room_type == 2)
1211                 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1212         if (new_room_type == 4)
1213                 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1214
1215         /* If the room is private, and the system administrator has elected
1216          * to automatically grant room aide privileges, do so now; otherwise,
1217          * set the room aide to undefined.
1218          */
1219         if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1220                 qrbuf.QRroomaide = CC->usersupp.usernum;
1221         } else {
1222                 qrbuf.QRroomaide = (-1L);
1223         }
1224
1225         qrbuf.QRhighest = 0L;   /* No messages in this room yet */
1226         time(&qrbuf.QRgen);     /* Use a timestamp as the generation number */
1227         qrbuf.QRfloor = new_room_floor;
1228
1229         /* save what we just did... */
1230         putroom(&qrbuf);
1231
1232         /* bump the reference count on whatever floor the room is on */
1233         lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1234         flbuf.f_ref_count = flbuf.f_ref_count + 1;
1235         lputfloor(&flbuf, (int) qrbuf.QRfloor);
1236
1237         /* be sure not to kick the creator out of the room! */
1238         lgetuser(&CC->usersupp, CC->curr_user);
1239         CtdlGetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1240         vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1241         vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1242         CtdlSetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1243         lputuser(&CC->usersupp);
1244
1245         /* resume our happy day */
1246         return (qrbuf.QRflags);
1247 }
1248
1249
1250 /*
1251  * create a new room
1252  */
1253 void cmd_cre8(char *args)
1254 {
1255         int cre8_ok;
1256         char new_room_name[256];
1257         int new_room_type;
1258         char new_room_pass[256];
1259         int new_room_floor;
1260         char aaa[256];
1261         unsigned newflags;
1262         struct quickroom qrbuf;
1263         struct floor flbuf;
1264
1265         cre8_ok = extract_int(args, 0);
1266         extract(new_room_name, args, 1);
1267         new_room_name[ROOMNAMELEN - 1] = 0;
1268         new_room_type = extract_int(args, 2);
1269         extract(new_room_pass, args, 3);
1270         new_room_pass[9] = 0;
1271         new_room_floor = 0;
1272
1273         if ((strlen(new_room_name) == 0) && (cre8_ok == 1)) {
1274                 cprintf("%d Invalid room name.\n", ERROR);
1275                 return;
1276         }
1277
1278         if (!strcasecmp(new_room_name, MAILROOM)) {
1279                 cprintf("%d '%s' already exists.\n",
1280                         ERROR + ALREADY_EXISTS, new_room_name);
1281                 return;
1282         }
1283
1284         if (num_parms(args) >= 5) {
1285                 getfloor(&flbuf, extract_int(args, 4));
1286                 if ((flbuf.f_flags & F_INUSE) == 0) {
1287                         cprintf("%d Invalid floor number.\n",
1288                                 ERROR + INVALID_FLOOR_OPERATION);
1289                         return;
1290                 } else {
1291                         new_room_floor = extract_int(args, 4);
1292                 }
1293         }
1294
1295         if (!(CC->logged_in)) {
1296                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1297                 return;
1298         }
1299
1300         if (CC->usersupp.axlevel < config.c_createax) {
1301                 cprintf("%d You need higher access to create rooms.\n",
1302                         ERROR + HIGHER_ACCESS_REQUIRED);
1303                 return;
1304         }
1305
1306         if ((strlen(new_room_name) == 0) && (cre8_ok == 0)) {
1307                 cprintf("%d Ok to create rooms.\n", OK);
1308                 return;
1309         }
1310
1311         if ((new_room_type < 0) || (new_room_type > 4)) {
1312                 cprintf("%d Invalid room type.\n", ERROR);
1313                 return;
1314         }
1315
1316         /* If the user is requesting a personal room, set up the room
1317          * name accordingly (prepend the user number)
1318          */
1319         if (new_room_type == 4) {
1320                 sprintf(aaa, "%010ld.%s",
1321                         CC->usersupp.usernum, new_room_name);
1322                 strcpy(new_room_name, aaa);
1323         }
1324
1325         /* Check to make sure the requested room name doesn't already exist */
1326         if (getroom(&qrbuf, new_room_name) == 0) {
1327                 cprintf("%d '%s' already exists.\n",
1328                         ERROR + ALREADY_EXISTS, qrbuf.QRname);
1329                 return;
1330         }
1331
1332         if (cre8_ok == 0) {
1333                 cprintf("%d OK to create '%s'\n", OK, new_room_name);
1334                 return;
1335         }
1336
1337         newflags = create_room(new_room_name,
1338                            new_room_type, new_room_pass, new_room_floor);
1339
1340         /* post a message in Aide> describing the new room */
1341         safestrncpy(aaa, new_room_name, sizeof aaa);
1342         strcat(aaa, "> created by ");
1343         strcat(aaa, CC->usersupp.fullname);
1344         if (newflags & QR_MAILBOX)
1345                 strcat(aaa, " [personal]");
1346         else if (newflags & QR_PRIVATE)
1347                 strcat(aaa, " [private]");
1348         if (newflags & QR_GUESSNAME)
1349                 strcat(aaa, "[guessname] ");
1350         if (newflags & QR_PASSWORDED) {
1351                 strcat(aaa, "\n Password: ");
1352                 strcat(aaa, new_room_pass);
1353         }
1354         strcat(aaa, "\n");
1355         aide_message(aaa);
1356
1357         cprintf("%d '%s' has been created.\n", OK, qrbuf.QRname);
1358 }
1359
1360
1361
1362 void cmd_einf(char *ok)
1363 {                               /* enter info file for current room */
1364         FILE *fp;
1365         char infofilename[256];
1366         char buf[256];
1367
1368         if (!(CC->logged_in)) {
1369                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1370                 return;
1371         }
1372         if (!is_room_aide()) {
1373                 cprintf("%d Higher access required.\n",
1374                         ERROR + HIGHER_ACCESS_REQUIRED);
1375                 return;
1376         }
1377         if (atoi(ok) == 0) {
1378                 cprintf("%d Ok.\n", OK);
1379                 return;
1380         }
1381         assoc_file_name(infofilename, &CC->quickroom, "info");
1382         lprintf(9, "opening\n");
1383         fp = fopen(infofilename, "w");
1384         lprintf(9, "checking\n");
1385         if (fp == NULL) {
1386                 cprintf("%d Cannot open %s: %s\n",
1387                   ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1388                 return;
1389         }
1390         cprintf("%d Send info...\n", SEND_LISTING);
1391
1392         do {
1393                 client_gets(buf);
1394                 if (strcmp(buf, "000"))
1395                         fprintf(fp, "%s\n", buf);
1396         } while (strcmp(buf, "000"));
1397         fclose(fp);
1398
1399         /* now update the room index so people will see our new info */
1400         lgetroom(&CC->quickroom, CC->quickroom.QRname);         /* lock so no one steps on us */
1401         CC->quickroom.QRinfo = CC->quickroom.QRhighest + 1L;
1402         lputroom(&CC->quickroom);
1403 }
1404
1405
1406 /* 
1407  * cmd_lflr()   -  List all known floors
1408  */
1409 void cmd_lflr(void)
1410 {
1411         int a;
1412         struct floor flbuf;
1413
1414         if (!(CC->logged_in)) {
1415                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1416                 return;
1417         }
1418         /* if (getuser(&CC->usersupp,CC->curr_user)) {
1419            cprintf("%d Can't locate user!\n",ERROR+INTERNAL_ERROR);
1420            return;
1421            }
1422          */
1423
1424         cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1425
1426         for (a = 0; a < MAXFLOORS; ++a) {
1427                 getfloor(&flbuf, a);
1428                 if (flbuf.f_flags & F_INUSE) {
1429                         cprintf("%d|%s|%d\n",
1430                                 a,
1431                                 flbuf.f_name,
1432                                 flbuf.f_ref_count);
1433                 }
1434         }
1435         cprintf("000\n");
1436 }
1437
1438
1439
1440 /*
1441  * create a new floor
1442  */
1443 void cmd_cflr(char *argbuf)
1444 {
1445         char new_floor_name[256];
1446         struct floor flbuf;
1447         int cflr_ok;
1448         int free_slot = (-1);
1449         int a;
1450
1451         extract(new_floor_name, argbuf, 0);
1452         cflr_ok = extract_int(argbuf, 1);
1453
1454
1455         if (!(CC->logged_in)) {
1456                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1457                 return;
1458         }
1459         if (CC->usersupp.axlevel < 6) {
1460                 cprintf("%d You need higher access to create rooms.\n",
1461                         ERROR + HIGHER_ACCESS_REQUIRED);
1462                 return;
1463         }
1464         for (a = 0; a < MAXFLOORS; ++a) {
1465                 getfloor(&flbuf, a);
1466
1467                 /* note any free slots while we're scanning... */
1468                 if (((flbuf.f_flags & F_INUSE) == 0)
1469                     && (free_slot < 0))
1470                         free_slot = a;
1471
1472                 /* check to see if it already exists */
1473                 if ((!strcasecmp(flbuf.f_name, new_floor_name))
1474                     && (flbuf.f_flags & F_INUSE)) {
1475                         cprintf("%d Floor '%s' already exists.\n",
1476                                 ERROR + ALREADY_EXISTS,
1477                                 flbuf.f_name);
1478                         return;
1479                 }
1480         }
1481
1482         if (free_slot < 0) {
1483                 cprintf("%d There is no space available for a new floor.\n",
1484                         ERROR + INVALID_FLOOR_OPERATION);
1485                 return;
1486         }
1487         if (cflr_ok == 0) {
1488                 cprintf("%d ok to create...\n", OK);
1489                 return;
1490         }
1491         lgetfloor(&flbuf, free_slot);
1492         flbuf.f_flags = F_INUSE;
1493         flbuf.f_ref_count = 0;
1494         safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
1495         lputfloor(&flbuf, free_slot);
1496         cprintf("%d %d\n", OK, free_slot);
1497 }
1498
1499
1500
1501 /*
1502  * delete a floor
1503  */
1504 void cmd_kflr(char *argbuf)
1505 {
1506         struct floor flbuf;
1507         int floor_to_delete;
1508         int kflr_ok;
1509         int delete_ok;
1510
1511         floor_to_delete = extract_int(argbuf, 0);
1512         kflr_ok = extract_int(argbuf, 1);
1513
1514
1515         if (!(CC->logged_in)) {
1516                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1517                 return;
1518         }
1519         if (CC->usersupp.axlevel < 6) {
1520                 cprintf("%d You need higher access to delete floors.\n",
1521                         ERROR + HIGHER_ACCESS_REQUIRED);
1522                 return;
1523         }
1524         lgetfloor(&flbuf, floor_to_delete);
1525
1526         delete_ok = 1;
1527         if ((flbuf.f_flags & F_INUSE) == 0) {
1528                 cprintf("%d Floor %d not in use.\n",
1529                         ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
1530                 delete_ok = 0;
1531         } else {
1532                 if (flbuf.f_ref_count != 0) {
1533                         cprintf("%d Cannot delete; floor contains %d rooms.\n",
1534                                 ERROR + INVALID_FLOOR_OPERATION,
1535                                 flbuf.f_ref_count);
1536                         delete_ok = 0;
1537                 } else {
1538                         if (kflr_ok == 1) {
1539                                 cprintf("%d Ok\n", OK);
1540                         } else {
1541                                 cprintf("%d Ok to delete...\n", OK);
1542                         }
1543
1544                 }
1545
1546         }
1547
1548         if ((delete_ok == 1) && (kflr_ok == 1))
1549                 flbuf.f_flags = 0;
1550         lputfloor(&flbuf, floor_to_delete);
1551 }
1552
1553 /*
1554  * edit a floor
1555  */
1556 void cmd_eflr(char *argbuf)
1557 {
1558         struct floor flbuf;
1559         int floor_num;
1560         int np;
1561
1562         np = num_parms(argbuf);
1563         if (np < 1) {
1564                 cprintf("%d Usage error.\n", ERROR);
1565                 return;
1566         }
1567         if (!(CC->logged_in)) {
1568                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1569                 return;
1570         }
1571         if (CC->usersupp.axlevel < 6) {
1572                 cprintf("%d You need higher access to edit floors.\n",
1573                         ERROR + HIGHER_ACCESS_REQUIRED);
1574                 return;
1575         }
1576         floor_num = extract_int(argbuf, 0);
1577         lgetfloor(&flbuf, floor_num);
1578         if ((flbuf.f_flags & F_INUSE) == 0) {
1579                 lputfloor(&flbuf, floor_num);
1580                 cprintf("%d Floor %d is not in use.\n",
1581                         ERROR + INVALID_FLOOR_OPERATION, floor_num);
1582                 return;
1583         }
1584         if (np >= 2)
1585                 extract(flbuf.f_name, argbuf, 1);
1586         lputfloor(&flbuf, floor_num);
1587
1588         cprintf("%d Ok\n", OK);
1589 }