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