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