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