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