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