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