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