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