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