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