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