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