]> code.citadel.org Git - citadel.git/blob - citadel/room_ops.c
Mostly made changes to allow client_gets to handle reading a buffer
[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 ((roombuf->QRhighest) > (vbuf.v_lastseen)) {
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 (msglist[a] > vbuf.v_lastseen) {
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                             ) {
806                                 cprintf("%d wrong or missing passwd\n",
807                                         ERROR + PASSWORD_REQUIRED);
808                                 return;
809                         } else if ((QRscratch.QRflags & QR_PRIVATE) &&
810                                    ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
811                                    ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
812                                    ((ra & UA_KNOWN) == 0)) {
813                                 goto NOPE;
814                         } else {
815                                 usergoto(towhere, 1, NULL, NULL);
816                                 return;
817                         }
818                 }
819         }
820
821 NOPE:   cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
822 }
823
824
825 void cmd_whok(void)
826 {
827         struct usersupp temp;
828         struct cdbdata *cdbus;
829
830         cdb_begin_transaction();
831         getuser(&CC->usersupp, CC->curr_user);
832         if (CtdlAccessCheck(ac_room_aide)) return;
833
834         cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
835         cdb_rewind(CDB_USERSUPP);
836         while (cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
837                 memset(&temp, 0, sizeof temp);
838                 memcpy(&temp, cdbus->ptr, sizeof temp);
839                 cdb_free(cdbus);
840
841                 if ((CC->quickroom.QRflags & QR_INUSE)
842                     && (CtdlRoomAccess(&CC->quickroom, &temp) & UA_KNOWN)
843                     )
844                         cprintf("%s\n", temp.fullname);
845         }
846         cdb_end_transaction();
847         cprintf("000\n");
848 }
849
850
851 /*
852  * RDIR command for room directory
853  */
854 void cmd_rdir(void)
855 {
856         char buf[SIZ];
857         char flnm[SIZ];
858         char comment[SIZ];
859         FILE *ls, *fd;
860         struct stat statbuf;
861
862         if (CtdlAccessCheck(ac_logged_in)) return;
863         
864         getroom(&CC->quickroom, CC->quickroom.QRname);
865         getuser(&CC->usersupp, CC->curr_user);
866
867         if ((CC->quickroom.QRflags & QR_DIRECTORY) == 0) {
868                 cprintf("%d not here.\n", ERROR + NOT_HERE);
869                 return;
870         }
871         if (((CC->quickroom.QRflags & QR_VISDIR) == 0)
872             && (CC->usersupp.axlevel < 6)
873             && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
874                 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
875                 return;
876         }
877         cprintf("%d %s|%s/files/%s\n",
878         LISTING_FOLLOWS, config.c_fqdn, BBSDIR, CC->quickroom.QRdirname);
879
880         sprintf(buf, "ls %s/files/%s  >%s 2> /dev/null",
881                 BBSDIR, CC->quickroom.QRdirname, CC->temp);
882         system(buf);
883
884         sprintf(buf, "%s/files/%s/filedir", BBSDIR, CC->quickroom.QRdirname);
885         fd = fopen(buf, "r");
886         if (fd == NULL)
887                 fd = fopen("/dev/null", "r");
888
889         ls = fopen(CC->temp, "r");
890         while (fgets(flnm, sizeof flnm, ls) != NULL) {
891                 flnm[strlen(flnm) - 1] = 0;
892                 if (strcasecmp(flnm, "filedir")) {
893                         sprintf(buf, "%s/files/%s/%s",
894                                 BBSDIR, CC->quickroom.QRdirname, flnm);
895                         stat(buf, &statbuf);
896                         strcpy(comment, "");
897                         fseek(fd, 0L, 0);
898                         while ((fgets(buf, sizeof buf, fd) != NULL)
899                                && (strlen(comment) == 0)) {
900                                 buf[strlen(buf) - 1] = 0;
901                                 if ((!strncasecmp(buf, flnm, strlen(flnm)))
902                                     && (buf[strlen(flnm)] == ' '))
903                                         safestrncpy(comment,
904                                             &buf[strlen(flnm) + 1],
905                                             sizeof comment);
906                         }
907                         cprintf("%s|%ld|%s\n", flnm, statbuf.st_size, comment);
908                 }
909         }
910         fclose(ls);
911         fclose(fd);
912         unlink(CC->temp);
913
914         cprintf("000\n");
915 }
916
917 /*
918  * get room parameters (aide or room aide command)
919  */
920 void cmd_getr(void)
921 {
922         if (CtdlAccessCheck(ac_room_aide)) return;
923
924         /********
925         if (is_noneditable(&CC->quickroom)) {
926                 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
927                 return;
928         }
929         ************/
930
931         getroom(&CC->quickroom, CC->quickroom.QRname);
932         cprintf("%d%c%s|%s|%s|%d|%d|%d\n",
933                 OK, CtdlCheckExpress(),
934                 CC->quickroom.QRname,
935                 ((CC->quickroom.QRflags & QR_PASSWORDED) ? CC->quickroom.QRpasswd : ""),
936                 ((CC->quickroom.QRflags & QR_DIRECTORY) ? CC->quickroom.QRdirname : ""),
937                 CC->quickroom.QRflags,
938                 (int) CC->quickroom.QRfloor,
939                 (int) CC->quickroom.QRorder);
940 }
941
942
943 /*
944  * set room parameters (aide or room aide command)
945  */
946 void cmd_setr(char *args)
947 {
948         char buf[SIZ];
949         struct floor *fl;
950         struct floor flbuf;
951         char old_name[ROOMNAMELEN];
952         int old_floor;
953         int new_order = 0;
954         int ne = 0;
955
956         if (CtdlAccessCheck(ac_room_aide)) return;
957
958         if (is_noneditable(&CC->quickroom)) {
959                 ne = 1;
960         }
961
962         /***
963                 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
964                 return;
965         }
966         ***/
967
968
969         if (num_parms(args) >= 6) {
970                 fl = cgetfloor(extract_int(args, 5));
971                 if ((fl->f_flags & F_INUSE) == 0) {
972                         cprintf("%d Invalid floor number.\n",
973                                 ERROR + INVALID_FLOOR_OPERATION);
974                         return;
975                 }
976         }
977         if (num_parms(args) >= 7) {
978                 new_order = extract_int(args, 6);
979                 if (new_order < 1)
980                         new_order = 1;
981                 if (new_order > 127)
982                         new_order = 127;
983         }
984         lgetroom(&CC->quickroom, CC->quickroom.QRname);
985
986         /* Non-editable base rooms can't be renamed */
987         strcpy(old_name, CC->quickroom.QRname);
988         if (!ne) {
989                 extract(buf, args, 0);
990                 buf[ROOMNAMELEN] = 0;
991                 safestrncpy(CC->quickroom.QRname, buf,
992                         sizeof CC->quickroom.QRname);
993         }
994
995         extract(buf, args, 1);
996         buf[10] = 0;
997         safestrncpy(CC->quickroom.QRpasswd, buf, sizeof CC->quickroom.QRpasswd);
998         extract(buf, args, 2);
999         buf[15] = 0;
1000         safestrncpy(CC->quickroom.QRdirname, buf,
1001                 sizeof CC->quickroom.QRdirname);
1002         CC->quickroom.QRflags = (extract_int(args, 3) | QR_INUSE);
1003         if (num_parms(args) >= 7)
1004                 CC->quickroom.QRorder = (char) new_order;
1005
1006         /* Clean up a client boo-boo: if the client set the room to
1007          * guess-name or passworded, ensure that the private flag is
1008          * also set.
1009          */
1010         if ((CC->quickroom.QRflags & QR_GUESSNAME)
1011             || (CC->quickroom.QRflags & QR_PASSWORDED))
1012                 CC->quickroom.QRflags |= QR_PRIVATE;
1013
1014         /* Kick everyone out if the client requested it (by changing the
1015          * room's generation number)
1016          */
1017         if (extract_int(args, 4)) {
1018                 time(&CC->quickroom.QRgen);
1019         }
1020         old_floor = CC->quickroom.QRfloor;
1021         if (num_parms(args) >= 6) {
1022                 CC->quickroom.QRfloor = extract_int(args, 5);
1023         }
1024         /* Write the room record back to disk */
1025         lputroom(&CC->quickroom);
1026
1027         /* If the room name changed, then there are now two room records,
1028          * so we have to delete the old one.
1029          */
1030         if (strcasecmp(CC->quickroom.QRname, old_name)) {
1031                 b_deleteroom(old_name);
1032         }
1033         /* adjust the floor reference counts */
1034         lgetfloor(&flbuf, old_floor);
1035         --flbuf.f_ref_count;
1036         lputfloor(&flbuf, old_floor);
1037         lgetfloor(&flbuf, CC->quickroom.QRfloor);
1038         ++flbuf.f_ref_count;
1039         lputfloor(&flbuf, CC->quickroom.QRfloor);
1040
1041         /* create a room directory if necessary */
1042         if (CC->quickroom.QRflags & QR_DIRECTORY) {
1043                 sprintf(buf,
1044                     "mkdir ./files/%s </dev/null >/dev/null 2>/dev/null",
1045                         CC->quickroom.QRdirname);
1046                 system(buf);
1047         }
1048         sprintf(buf, "%s> edited by %s\n", CC->quickroom.QRname, CC->curr_user);
1049         aide_message(buf);
1050         cprintf("%d Ok\n", OK);
1051 }
1052
1053
1054
1055 /* 
1056  * get the name of the room aide for this room
1057  */
1058 void cmd_geta(void)
1059 {
1060         struct usersupp usbuf;
1061
1062         if (CtdlAccessCheck(ac_logged_in)) return;
1063
1064         if (is_noneditable(&CC->quickroom)) {
1065                 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
1066                 return;
1067         }
1068         if (getuserbynumber(&usbuf, CC->quickroom.QRroomaide) == 0) {
1069                 cprintf("%d %s\n", OK, usbuf.fullname);
1070         } else {
1071                 cprintf("%d \n", OK);
1072         }
1073 }
1074
1075
1076 /* 
1077  * set the room aide for this room
1078  */
1079 void cmd_seta(char *new_ra)
1080 {
1081         struct usersupp usbuf;
1082         long newu;
1083         char buf[SIZ];
1084         int post_notice;
1085
1086         if (CtdlAccessCheck(ac_room_aide)) return;
1087
1088         if (getuser(&usbuf, new_ra) != 0) {
1089                 newu = (-1L);
1090         } else {
1091                 newu = usbuf.usernum;
1092         }
1093
1094         lgetroom(&CC->quickroom, CC->quickroom.QRname);
1095         post_notice = 0;
1096         if (CC->quickroom.QRroomaide != newu) {
1097                 post_notice = 1;
1098         }
1099         CC->quickroom.QRroomaide = newu;
1100         lputroom(&CC->quickroom);
1101
1102         /*
1103          * We have to post the change notice _after_ writing changes to 
1104          * the room table, otherwise it would deadlock!
1105          */
1106         if (post_notice == 1) {
1107                 sprintf(buf, "%s is now room aide for %s>\n",
1108                         usbuf.fullname, CC->quickroom.QRname);
1109                 aide_message(buf);
1110         }
1111         cprintf("%d Ok\n", OK);
1112 }
1113
1114 /*
1115  * Generate an associated file name for a room
1116  */
1117 void assoc_file_name(char *buf, struct quickroom *qrbuf, char *prefix)
1118 {
1119         sprintf(buf, "./%s/%ld", prefix, qrbuf->QRnumber);
1120 }
1121
1122 /* 
1123  * retrieve info file for this room
1124  */
1125 void cmd_rinf(void)
1126 {
1127         char filename[128];
1128         char buf[SIZ];
1129         FILE *info_fp;
1130
1131         assoc_file_name(filename, &CC->quickroom, "info");
1132         info_fp = fopen(filename, "r");
1133
1134         if (info_fp == NULL) {
1135                 cprintf("%d No info file.\n", ERROR);
1136                 return;
1137         }
1138         cprintf("%d Info:\n", LISTING_FOLLOWS);
1139         while (fgets(buf, sizeof buf, info_fp) != NULL) {
1140                 if (strlen(buf) > 0)
1141                         buf[strlen(buf) - 1] = 0;
1142                 cprintf("%s\n", buf);
1143         }
1144         cprintf("000\n");
1145         fclose(info_fp);
1146 }
1147
1148 /*
1149  * Back end processing to delete a room and everything associated with it
1150  */
1151 void delete_room(struct quickroom *qrbuf)
1152 {
1153         struct floor flbuf;
1154         char filename[100];
1155
1156         lprintf(9, "Deleting room <%s>\n", qrbuf->QRname);
1157
1158         /* Delete the info file */
1159         assoc_file_name(filename, qrbuf, "info");
1160         unlink(filename);
1161
1162         /* Delete the image file */
1163         assoc_file_name(filename, qrbuf, "images");
1164         unlink(filename);
1165
1166         /* Delete the room's network config file */
1167         assoc_file_name(filename, qrbuf, "netconfigs");
1168         unlink(filename);
1169
1170         /* Delete the messages in the room
1171          * (Careful: this opens an S_QUICKROOM critical section!)
1172          */
1173         CtdlDeleteMessages(qrbuf->QRname, 0L, "");
1174
1175         /* Flag the room record as not in use */
1176         lgetroom(qrbuf, qrbuf->QRname);
1177         qrbuf->QRflags = 0;
1178         lputroom(qrbuf);
1179
1180         /* then decrement the reference count for the floor */
1181         lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1182         flbuf.f_ref_count = flbuf.f_ref_count - 1;
1183         lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1184
1185         /* Delete the room record from the database! */
1186         b_deleteroom(qrbuf->QRname);
1187 }
1188
1189
1190 /*
1191  * aide command: kill the current room
1192  */
1193 void cmd_kill(char *argbuf)
1194 {
1195         char aaa[100];
1196         char deleted_room_name[ROOMNAMELEN];
1197         int kill_ok;
1198
1199         kill_ok = extract_int(argbuf, 0);
1200
1201         if (CtdlAccessCheck(ac_room_aide)) return;
1202
1203         if (is_noneditable(&CC->quickroom)) {
1204                 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
1205                 return;
1206         }
1207         if (kill_ok) {
1208                 strcpy(deleted_room_name, CC->quickroom.QRname);
1209                 delete_room(&CC->quickroom);    /* Do the dirty work */
1210                 usergoto(BASEROOM, 0, NULL, NULL); /* Return to the Lobby */
1211
1212                 /* tell the world what we did */
1213                 sprintf(aaa, "%s> killed by %s\n",
1214                         deleted_room_name, CC->curr_user);
1215                 aide_message(aaa);
1216                 cprintf("%d '%s' deleted.\n", OK, deleted_room_name);
1217         } else {
1218                 cprintf("%d ok to delete.\n", OK);
1219         }
1220 }
1221
1222
1223 /*
1224  * Internal code to create a new room (returns room flags)
1225  *
1226  * Room types:  0=public, 1=guessname, 2=passworded, 3=inv-only,
1227  *              4=mailbox, 5=mailbox, but caller supplies namespace
1228  */
1229 unsigned create_room(char *new_room_name,
1230                      int new_room_type,
1231                      char *new_room_pass,
1232                      int new_room_floor,
1233                      int really_create)
1234 {
1235
1236         struct quickroom qrbuf;
1237         struct floor flbuf;
1238         struct visit vbuf;
1239
1240         lprintf(9, "create_room(%s)\n", new_room_name);
1241         if (getroom(&qrbuf, new_room_name) == 0) {
1242                 lprintf(9, "%s already exists.\n", new_room_name);
1243                 return (0);     /* already exists */
1244         }
1245
1246
1247         memset(&qrbuf, 0, sizeof(struct quickroom));
1248         safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1249         qrbuf.QRflags = QR_INUSE;
1250         if (new_room_type > 0)
1251                 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1252         if (new_room_type == 1)
1253                 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1254         if (new_room_type == 2)
1255                 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1256         if ( (new_room_type == 4) || (new_room_type == 5) )
1257                 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1258
1259         /* If the user is requesting a personal room, set up the room
1260          * name accordingly (prepend the user number)
1261          */
1262         if (new_room_type == 4) {
1263                 MailboxName(qrbuf.QRname, &CC->usersupp, new_room_name);
1264         }
1265         else {
1266                 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1267         }
1268
1269         /* If the room is private, and the system administrator has elected
1270          * to automatically grant room aide privileges, do so now; otherwise,
1271          * set the room aide to undefined.
1272          */
1273         if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1274                 qrbuf.QRroomaide = CC->usersupp.usernum;
1275         } else {
1276                 qrbuf.QRroomaide = (-1L);
1277         }
1278
1279         /* 
1280          * If the caller is only interested in testing whether this will work,
1281          * return now without creating the room.
1282          */
1283         if (!really_create) return (qrbuf.QRflags);
1284
1285         cdb_begin_transaction();
1286         qrbuf.QRnumber = get_new_room_number();
1287         qrbuf.QRhighest = 0L;   /* No messages in this room yet */
1288         time(&qrbuf.QRgen);     /* Use a timestamp as the generation number */
1289         qrbuf.QRfloor = new_room_floor;
1290
1291         /* save what we just did... */
1292         putroom(&qrbuf);
1293
1294         /* bump the reference count on whatever floor the room is on */
1295         lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1296         flbuf.f_ref_count = flbuf.f_ref_count + 1;
1297         lputfloor(&flbuf, (int) qrbuf.QRfloor);
1298
1299         /* be sure not to kick the creator out of the room! */
1300         lgetuser(&CC->usersupp, CC->curr_user);
1301         CtdlGetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1302         vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1303         vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1304         CtdlSetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1305         lputuser(&CC->usersupp);
1306
1307         /* resume our happy day */
1308         cdb_end_transaction();
1309         return (qrbuf.QRflags);
1310 }
1311
1312
1313 /*
1314  * create a new room
1315  */
1316 void cmd_cre8(char *args)
1317 {
1318         int cre8_ok;
1319         char new_room_name[SIZ];
1320         int new_room_type;
1321         char new_room_pass[SIZ];
1322         int new_room_floor;
1323         char aaa[SIZ];
1324         unsigned newflags;
1325         struct quickroom qrbuf;
1326         struct floor *fl;
1327
1328         cre8_ok = extract_int(args, 0);
1329         extract(new_room_name, args, 1);
1330         new_room_name[ROOMNAMELEN - 1] = 0;
1331         new_room_type = extract_int(args, 2);
1332         extract(new_room_pass, args, 3);
1333         new_room_pass[9] = 0;
1334         new_room_floor = 0;
1335
1336         if ((strlen(new_room_name) == 0) && (cre8_ok == 1)) {
1337                 cprintf("%d Invalid room name.\n", ERROR);
1338                 return;
1339         }
1340
1341         if (!strcasecmp(new_room_name, MAILROOM)) {
1342                 cprintf("%d '%s' already exists.\n",
1343                         ERROR + ALREADY_EXISTS, new_room_name);
1344                 return;
1345         }
1346
1347         if (num_parms(args) >= 5) {
1348                 fl = cgetfloor(extract_int(args, 4));
1349                 if ((fl->f_flags & F_INUSE) == 0) {
1350                         cprintf("%d Invalid floor number.\n",
1351                                 ERROR + INVALID_FLOOR_OPERATION);
1352                         return;
1353                 } else {
1354                         new_room_floor = extract_int(args, 4);
1355                 }
1356         }
1357
1358         if (CtdlAccessCheck(ac_logged_in)) return;
1359
1360         if (CC->usersupp.axlevel < config.c_createax) {
1361                 cprintf("%d You need higher access to create rooms.\n",
1362                         ERROR + HIGHER_ACCESS_REQUIRED);
1363                 return;
1364         }
1365
1366         if ((strlen(new_room_name) == 0) && (cre8_ok == 0)) {
1367                 cprintf("%d Ok to create rooms.\n", OK);
1368                 return;
1369         }
1370
1371         if ((new_room_type < 0) || (new_room_type > 4)) {
1372                 cprintf("%d Invalid room type.\n", ERROR);
1373                 return;
1374         }
1375
1376         /* Check to make sure the requested room name doesn't already exist */
1377         newflags = create_room(new_room_name,
1378                            new_room_type, new_room_pass, new_room_floor, 0);
1379         if (newflags == 0) {
1380                 cprintf("%d '%s' already exists.\n",
1381                         ERROR + ALREADY_EXISTS, qrbuf.QRname);
1382                 return;
1383         }
1384
1385         if (cre8_ok == 0) {
1386                 cprintf("%d OK to create '%s'\n", OK, new_room_name);
1387                 return;
1388         }
1389
1390         /* If we reach this point, the room needs to be created. */
1391
1392         newflags = create_room(new_room_name,
1393                            new_room_type, new_room_pass, new_room_floor, 1);
1394
1395         /* post a message in Aide> describing the new room */
1396         safestrncpy(aaa, new_room_name, sizeof aaa);
1397         strcat(aaa, "> created by ");
1398         strcat(aaa, CC->usersupp.fullname);
1399         if (newflags & QR_MAILBOX)
1400                 strcat(aaa, " [personal]");
1401         else if (newflags & QR_PRIVATE)
1402                 strcat(aaa, " [private]");
1403         if (newflags & QR_GUESSNAME)
1404                 strcat(aaa, "[guessname] ");
1405         if (newflags & QR_PASSWORDED) {
1406                 strcat(aaa, "\n Password: ");
1407                 strcat(aaa, new_room_pass);
1408         }
1409         strcat(aaa, "\n");
1410         aide_message(aaa);
1411
1412         cprintf("%d '%s' has been created.\n", OK, qrbuf.QRname);
1413 }
1414
1415
1416
1417 void cmd_einf(char *ok)
1418 {                               /* enter info file for current room */
1419         FILE *fp;
1420         char infofilename[SIZ];
1421         char *ibuf;
1422
1423         if (CtdlAccessCheck(ac_room_aide)) return;
1424
1425         if (atoi(ok) == 0) {
1426                 cprintf("%d Ok.\n", OK);
1427                 return;
1428         }
1429         assoc_file_name(infofilename, &CC->quickroom, "info");
1430         lprintf(9, "opening\n");
1431         fp = fopen(infofilename, "w");
1432         lprintf(9, "checking\n");
1433         if (fp == NULL) {
1434                 cprintf("%d Cannot open %s: %s\n",
1435                   ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1436                 return;
1437         }
1438         cprintf("%d Send info...\n", SEND_LISTING);
1439
1440         do {
1441                 client_gets(&ibuf);
1442                 if (strcmp(ibuf, "000"))
1443                         fprintf(fp, "%s\n", ibuf);
1444         } while (strcmp(ibuf, "000"));
1445         fclose(fp);
1446
1447         /* now update the room index so people will see our new info */
1448         lgetroom(&CC->quickroom, CC->quickroom.QRname);         /* lock so no one steps on us */
1449         CC->quickroom.QRinfo = CC->quickroom.QRhighest + 1L;
1450         lputroom(&CC->quickroom);
1451 }
1452
1453
1454 /* 
1455  * cmd_lflr()   -  List all known floors
1456  */
1457 void cmd_lflr(void)
1458 {
1459         int a;
1460         struct floor flbuf;
1461
1462         if (CtdlAccessCheck(ac_logged_in)) return;
1463
1464         cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1465
1466         for (a = 0; a < MAXFLOORS; ++a) {
1467                 getfloor(&flbuf, a);
1468                 if (flbuf.f_flags & F_INUSE) {
1469                         cprintf("%d|%s|%d\n",
1470                                 a,
1471                                 flbuf.f_name,
1472                                 flbuf.f_ref_count);
1473                 }
1474         }
1475         cprintf("000\n");
1476 }
1477
1478
1479
1480 /*
1481  * create a new floor
1482  */
1483 void cmd_cflr(char *argbuf)
1484 {
1485         char new_floor_name[SIZ];
1486         struct floor flbuf;
1487         int cflr_ok;
1488         int free_slot = (-1);
1489         int a;
1490
1491         extract(new_floor_name, argbuf, 0);
1492         cflr_ok = extract_int(argbuf, 1);
1493
1494
1495         if (CtdlAccessCheck(ac_aide)) return;
1496
1497         for (a = 0; a < MAXFLOORS; ++a) {
1498                 getfloor(&flbuf, a);
1499
1500                 /* note any free slots while we're scanning... */
1501                 if (((flbuf.f_flags & F_INUSE) == 0)
1502                     && (free_slot < 0))
1503                         free_slot = a;
1504
1505                 /* check to see if it already exists */
1506                 if ((!strcasecmp(flbuf.f_name, new_floor_name))
1507                     && (flbuf.f_flags & F_INUSE)) {
1508                         cprintf("%d Floor '%s' already exists.\n",
1509                                 ERROR + ALREADY_EXISTS,
1510                                 flbuf.f_name);
1511                         return;
1512                 }
1513         }
1514
1515         if (free_slot < 0) {
1516                 cprintf("%d There is no space available for a new floor.\n",
1517                         ERROR + INVALID_FLOOR_OPERATION);
1518                 return;
1519         }
1520         if (cflr_ok == 0) {
1521                 cprintf("%d ok to create...\n", OK);
1522                 return;
1523         }
1524         lgetfloor(&flbuf, free_slot);
1525         flbuf.f_flags = F_INUSE;
1526         flbuf.f_ref_count = 0;
1527         safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
1528         lputfloor(&flbuf, free_slot);
1529         cprintf("%d %d\n", OK, free_slot);
1530 }
1531
1532
1533
1534 /*
1535  * delete a floor
1536  */
1537 void cmd_kflr(char *argbuf)
1538 {
1539         struct floor flbuf;
1540         int floor_to_delete;
1541         int kflr_ok;
1542         int delete_ok;
1543
1544         floor_to_delete = extract_int(argbuf, 0);
1545         kflr_ok = extract_int(argbuf, 1);
1546
1547         if (CtdlAccessCheck(ac_aide)) return;
1548
1549         lgetfloor(&flbuf, floor_to_delete);
1550
1551         delete_ok = 1;
1552         if ((flbuf.f_flags & F_INUSE) == 0) {
1553                 cprintf("%d Floor %d not in use.\n",
1554                         ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
1555                 delete_ok = 0;
1556         } else {
1557                 if (flbuf.f_ref_count != 0) {
1558                         cprintf("%d Cannot delete; floor contains %d rooms.\n",
1559                                 ERROR + INVALID_FLOOR_OPERATION,
1560                                 flbuf.f_ref_count);
1561                         delete_ok = 0;
1562                 } else {
1563                         if (kflr_ok == 1) {
1564                                 cprintf("%d Ok\n", OK);
1565                         } else {
1566                                 cprintf("%d Ok to delete...\n", OK);
1567                         }
1568
1569                 }
1570
1571         }
1572
1573         if ((delete_ok == 1) && (kflr_ok == 1))
1574                 flbuf.f_flags = 0;
1575         lputfloor(&flbuf, floor_to_delete);
1576 }
1577
1578 /*
1579  * edit a floor
1580  */
1581 void cmd_eflr(char *argbuf)
1582 {
1583         struct floor flbuf;
1584         int floor_num;
1585         int np;
1586
1587         np = num_parms(argbuf);
1588         if (np < 1) {
1589                 cprintf("%d Usage error.\n", ERROR);
1590                 return;
1591         }
1592
1593         if (CtdlAccessCheck(ac_aide)) return;
1594
1595         floor_num = extract_int(argbuf, 0);
1596         lgetfloor(&flbuf, floor_num);
1597         if ((flbuf.f_flags & F_INUSE) == 0) {
1598                 lputfloor(&flbuf, floor_num);
1599                 cprintf("%d Floor %d is not in use.\n",
1600                         ERROR + INVALID_FLOOR_OPERATION, floor_num);
1601                 return;
1602         }
1603         if (np >= 2)
1604                 extract(flbuf.f_name, argbuf, 1);
1605         lputfloor(&flbuf, floor_num);
1606
1607         cprintf("%d Ok\n", OK);
1608 }