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