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