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