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