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