]> code.citadel.org Git - citadel.git/blob - citadel/room_ops.c
fe722e7825c45abe9505a0b03343c3a102c37cc7
[citadel.git] / citadel / room_ops.c
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <stdio.h>
4 #include <sys/stat.h>
5 #include <string.h>
6 #include <pthread.h>
7 #include <time.h>
8 #include <limits.h>
9 #include "citadel.h"
10 #include "server.h"
11 #include "database.h"
12 #include "config.h"
13 #include "room_ops.h"
14 #include "sysdep_decls.h"
15 #include "support.h"
16 #include "user_ops.h"
17 #include "msgbase.h"
18 #include "serv_chat.h"
19 #include "citserver.h"
20
21 /*
22  * Generic routine for determining user access to rooms
23  */
24 int CtdlRoomAccess(struct quickroom *roombuf, struct usersupp *userbuf) {
25         int retval = 0;
26         struct visit vbuf;
27
28         /* Make sure we're dealing with a real, existing room */
29         if (roombuf->QRflags & QR_INUSE) {
30                 retval = retval | UA_INUSE;
31                 }
32         else {
33                 return(0);
34                 }
35
36         /* for internal programs, always do everything */
37         if (((CC->internal_pgm))&&(roombuf->QRflags & QR_INUSE)) {
38                 return(UA_INUSE | UA_KNOWN | UA_GOTOALLOWED);
39                 }
40
41         /* For mailbox rooms, only allow access to the owner */
42         if (roombuf->QRflags & QR_MAILBOX) {
43                 if (userbuf->usernum != atol(roombuf->QRname)) {
44                         return(retval);
45                         }
46                 }
47
48         /* Locate any applicable user/room relationships */
49         CtdlGetRelationship(&vbuf, userbuf, roombuf);
50
51         /* If this is a public room, it's accessible... */
52         if ((roombuf->QRflags & QR_PRIVATE) == 0) {
53                 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
54                 }
55
56         /* If this is a preferred users only room, check access level */
57         if (roombuf->QRflags & QR_PREFONLY) {
58                 if (userbuf->axlevel < 5) {
59                         retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
60                         }
61                 }
62
63         /* For private rooms, check the generation number matchups */
64         if (roombuf->QRflags & QR_PRIVATE) {
65
66                 /* An explicit match means the user belongs in this room */
67                 if (vbuf.v_flags & V_ACCESS) {
68                         retval = retval | UA_KNOWN | UA_GOTOALLOWED;
69                         }
70                 /* Otherwise, check if this is a guess-name or passworded
71                  * room.  If it is, a goto may at least be attempted
72                  */
73                 else if ((roombuf->QRflags & QR_PRIVATE)
74                      ||(roombuf->QRflags & QR_PASSWORDED)) {
75                         retval = retval & ~UA_KNOWN;
76                         retval = retval | UA_GOTOALLOWED;
77                         }
78                 }
79
80         /* Check to see if the user has forgotten this room */
81         if (vbuf.v_flags & V_FORGET) {
82                 retval = retval & ~UA_KNOWN;
83                 retval = retval | UA_ZAPPED;
84                 }
85
86         /* If user is explicitly locked out of this room, deny everything */
87         if (vbuf.v_flags & V_LOCKOUT) {
88                 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
89                 }
90
91         /* Aides get access to everything */
92         if (userbuf->axlevel >= 6) {
93                 retval = retval | UA_INUSE | UA_KNOWN | UA_GOTOALLOWED;
94                 retval = retval & ~UA_ZAPPED;
95                 }
96
97         /* By the way, we also check for the presence of new messages */
98         if ( (roombuf->QRhighest) > (vbuf.v_lastseen) ) {
99                 retval = retval | UA_HASNEWMSGS;
100                 }
101
102         return(retval);
103         }
104
105 /*
106  * getroom()  -  retrieve room data from disk
107  */
108 int getroom(struct quickroom *qrbuf, char *room_name)
109 {
110         struct cdbdata *cdbqr;
111         char lowercase_name[ROOMNAMELEN];
112         int a;
113
114         for (a=0; room_name[a] && a < sizeof lowercase_name - 1; ++a) {
115                 lowercase_name[a] = tolower(room_name[a]);
116                 }
117         lowercase_name[a] = 0;
118
119         memset(qrbuf, 0, sizeof(struct quickroom));
120         cdbqr = cdb_fetch(CDB_QUICKROOM,
121                         lowercase_name, strlen(lowercase_name));
122         if (cdbqr != NULL) {
123                 memcpy(qrbuf, cdbqr->ptr,
124                         ( (cdbqr->len > sizeof(struct quickroom)) ?
125                         sizeof(struct quickroom) : cdbqr->len) );
126                 cdb_free(cdbqr);
127                 return(0);
128                 }
129         else {
130                 return(1);
131                 }
132         }
133
134 /*
135  * lgetroom()  -  same as getroom() but locks the record (if supported)
136  */
137 int lgetroom(struct quickroom *qrbuf, char *room_name)
138 {
139         begin_critical_section(S_QUICKROOM);
140         return(getroom(qrbuf, room_name));
141         }
142
143
144 /*
145  * putroom()  -  store room data on disk
146  *              (if the supplied buffer is NULL, delete the room record)
147  */
148 void putroom(struct quickroom *qrbuf, char *room_name)
149 {
150         char lowercase_name[ROOMNAMELEN];
151         int a;
152
153         for (a=0; a<=strlen(room_name); ++a) {
154                 lowercase_name[a] = tolower(room_name[a]);
155                 }
156
157         if (qrbuf == NULL) {
158                 cdb_delete(CDB_QUICKROOM,
159                         lowercase_name, strlen(lowercase_name));
160                 }
161         else {
162                 time(&qrbuf->QRmtime);
163                 cdb_store(CDB_QUICKROOM,
164                         lowercase_name, strlen(lowercase_name),
165                         qrbuf, sizeof(struct quickroom));
166                 }
167         }
168
169
170 /*
171  * lputroom()  -  same as putroom() but unlocks the record (if supported)
172  */
173 void lputroom(struct quickroom *qrbuf, char *room_name)
174 {
175
176         putroom(qrbuf, room_name);
177         end_critical_section(S_QUICKROOM);
178
179         }
180
181 /****************************************************************************/
182
183 /*
184  * getfloor()  -  retrieve floor data from disk
185  */
186 void getfloor(struct floor *flbuf, int floor_num)
187 {
188         struct cdbdata *cdbfl;
189
190         memset(flbuf, 0, sizeof(struct floor));
191         cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
192         if (cdbfl != NULL) {
193                 memcpy(flbuf, cdbfl->ptr,
194                         ( (cdbfl->len > sizeof(struct floor)) ?
195                         sizeof(struct floor) : cdbfl->len) );
196                 cdb_free(cdbfl);
197                 }
198         else {
199                 if (floor_num == 0) {
200                         strcpy(flbuf->f_name, "Main Floor");
201                         flbuf->f_flags = F_INUSE;
202                         flbuf->f_ref_count = 3;
203                         }
204                 }
205
206         }
207
208 /*
209  * lgetfloor()  -  same as getfloor() but locks the record (if supported)
210  */
211 void lgetfloor(struct floor *flbuf, int floor_num)
212 {
213
214         begin_critical_section(S_FLOORTAB);
215         getfloor(flbuf,floor_num);
216         }
217
218
219 /*
220  * putfloor()  -  store floor data on disk
221  */
222 void putfloor(struct floor *flbuf, int floor_num)
223 {
224         cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
225                 flbuf, sizeof(struct floor));
226         }
227
228
229 /*
230  * lputfloor()  -  same as putfloor() but unlocks the record (if supported)
231  */
232 void lputfloor(struct floor *flbuf, int floor_num)
233 {
234
235         putfloor(flbuf,floor_num);
236         end_critical_section(S_FLOORTAB);
237
238         }
239
240
241 /* 
242  *  Traverse the room file...
243  */
244 void ForEachRoom(void (*CallBack)(struct quickroom *EachRoom)) {
245         struct quickroom qrbuf;
246         struct cdbdata *cdbqr;
247
248         cdb_rewind(CDB_QUICKROOM);
249
250         while(cdbqr = cdb_next_item(CDB_QUICKROOM), cdbqr != NULL) {
251                 memset(&qrbuf, 0, sizeof(struct quickroom));
252                 memcpy(&qrbuf, cdbqr->ptr,
253                         ( (cdbqr->len > sizeof(struct quickroom)) ?
254                         sizeof(struct quickroom) : cdbqr->len) );
255                 cdb_free(cdbqr);
256                 if (qrbuf.QRflags & QR_INUSE) (*CallBack)(&qrbuf);
257                 }
258         }
259
260
261
262 /*
263  * Create a database key for storage of message lists
264  */
265 void msglist_key(char *dbkey, struct quickroom *whichroom) {
266         int a;
267         
268         sprintf(dbkey, "%s%ld", whichroom->QRname, whichroom->QRgen);
269         for (a=0; a<strlen(dbkey); ++a) dbkey[a]=tolower(dbkey[a]);
270         }
271
272 /*
273  * get_msglist()  -  retrieve room message pointers
274  */
275 void get_msglist(struct quickroom *whichroom) {
276         struct cdbdata *cdbfr;
277         char dbkey[256];
278
279         msglist_key(dbkey, whichroom);  
280         
281         if (CC->msglist != NULL) {
282                 free(CC->msglist);
283                 }
284         CC->msglist = NULL;
285         CC->num_msgs = 0;
286
287         cdbfr = cdb_fetch(CDB_MSGLISTS, dbkey, strlen(dbkey));
288         if (cdbfr == NULL) {
289                 return;
290                 }
291
292         CC->msglist = malloc(cdbfr->len);
293         memcpy(CC->msglist, cdbfr->ptr, cdbfr->len);
294         CC->num_msgs = cdbfr->len / sizeof(long);
295         cdb_free(cdbfr);
296         }
297
298
299 /*
300  * put_msglist()  -  retrieve room message pointers
301  */
302 void put_msglist(struct quickroom *whichroom) {
303         char dbkey[256];
304
305         msglist_key(dbkey, whichroom);  
306         cdb_store(CDB_MSGLISTS, dbkey, strlen(dbkey),
307                 CC->msglist, CC->num_msgs * sizeof(long));
308         }
309
310
311 /*
312  * delete_msglist()  -  delete room message pointers
313  */
314 void delete_msglist(struct quickroom *whichroom) {
315         char dbkey[256];
316
317         msglist_key(dbkey, whichroom);  
318         cdb_delete(CDB_MSGLISTS, dbkey, strlen(dbkey));
319         }
320
321
322 /* 
323  * Add a message number to a room's message list.  
324  * So, why doesn't this function use the get_msglist() and put_msglist() stuff
325  * defined above?  Because the room the message number is being written to
326  * may not be the current room (as is the case with cmd_move() for example).
327  */
328 void AddMessageToRoom(struct quickroom *whichroom, long newmsgid) {
329         char dbkey[256];
330         struct cdbdata *cdbfr;
331         int num_msgs;
332         long *msglist;
333         
334         msglist_key(dbkey, whichroom);
335         cdbfr = cdb_fetch(CDB_MSGLISTS, dbkey, strlen(dbkey));
336         if (cdbfr == NULL) {
337                 msglist = NULL;
338                 num_msgs = 0;
339                 }
340         else {
341                 msglist = malloc(cdbfr->len);
342                 num_msgs = cdbfr->len / sizeof(long);
343                 memcpy(msglist, cdbfr->ptr, cdbfr->len);
344                 cdb_free(cdbfr);
345                 }
346         
347         /* Now add the new message */
348         ++num_msgs;
349         msglist = realloc(msglist,
350                 (num_msgs * sizeof(long)) );
351
352         if (msglist == NULL) {
353                 lprintf(3, "ERROR: can't realloc message list!\n");
354                 }
355
356         msglist[num_msgs - 1] = newmsgid;
357
358         /* Write it back to disk. */
359         cdb_store(CDB_MSGLISTS, dbkey, strlen(dbkey),
360                 msglist, num_msgs * sizeof(long));
361
362         /* And finally, free up the memory we used. */
363         free(msglist);
364         }
365
366
367 /*
368  * MessageFromList()  -  get a message number from the list currently in memory
369  */
370 long MessageFromList(int whichpos) {
371
372         /* Return zero if the position is invalid */
373         if (whichpos >= CC->num_msgs) return 0L;
374
375         return(CC->msglist[whichpos]);
376         }
377
378 /* 
379  * SetMessageInList()  -  set a message number in the list currently in memory
380  */
381 void SetMessageInList(int whichpos, long newmsgnum) {
382
383         /* Return zero if the position is invalid */
384         if (whichpos >= CC->num_msgs) return;
385
386         CC->msglist[whichpos] = newmsgnum;
387         }
388
389
390
391 /*
392  * sort message pointers
393  * (returns new msg count)
394  */
395 int sort_msglist(long listptrs[], int oldcount)
396 {
397         int a,b;
398         long hold1, hold2;
399         int numitems;
400
401         numitems = oldcount;
402         if (numitems < 2) return(oldcount);
403
404         /* do the sort */
405         for (a=numitems-2; a>=0; --a) {
406                 for (b=0; b<=a; ++b) {
407                         if (listptrs[b] > (listptrs[b+1])) {
408                                 hold1 = listptrs[b];
409                                 hold2 = listptrs[b+1];
410                                 listptrs[b] = hold2;
411                                 listptrs[b+1] = hold1;
412                                 }
413                         }
414                 }
415
416         /* and yank any nulls */
417         while ( (numitems > 0) && (listptrs[0] == 0L) ) {
418                 memcpy(&listptrs[0], &listptrs[1],
419                         (sizeof(long) * (CC->num_msgs - 1)) );
420                 --numitems;
421                 }
422
423         return(numitems);
424         }
425
426
427 /*
428  * Determine whether a given room is one of the base non-editable rooms
429  */
430 int is_noneditable(struct quickroom *qrbuf) {
431         if (!strcasecmp(qrbuf->QRname, BASEROOM)) return(1);
432         else if (!strcasecmp(qrbuf->QRname, AIDEROOM)) return(1);
433         else return(0);
434         }
435
436
437
438 /*
439  * Retrieve the applicable room policy for a specific room
440  */
441 void GetExpirePolicy(struct ExpirePolicy *epbuf, struct quickroom *qrbuf) {
442         struct floor flbuf;
443
444         /* If the room has its own policy, return it */ 
445         if (qrbuf->QRep.expire_mode != 0) {
446                 memcpy(epbuf, &qrbuf->QRep, sizeof(struct ExpirePolicy));
447                 return;
448                 }
449
450         /* Otherwise, if the floor has its own policy, return it */     
451         getfloor(&flbuf, qrbuf->QRfloor);
452         if (flbuf.f_ep.expire_mode != 0) {
453                 memcpy(epbuf, &flbuf.f_ep, sizeof(struct ExpirePolicy));
454                 return;
455                 }
456
457         /* Otherwise, fall back on the system default */
458         memcpy(epbuf, &config.c_ep, sizeof(struct ExpirePolicy));
459         }
460
461
462 /*
463  * Back-back-end for all room listing commands
464  */
465 void list_roomname(struct quickroom *qrbuf) {
466         char truncated_roomname[ROOMNAMELEN];
467
468         /* For mailbox rooms, chop off the owner prefix */
469         if (qrbuf->QRflags & QR_MAILBOX) {
470                 strcpy(truncated_roomname, qrbuf->QRname);
471                 strcpy(truncated_roomname, &truncated_roomname[11]);
472                 cprintf("%s", truncated_roomname);
473                 }
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         if (CC->quickroom.QRflags & QR_PRIVATE) {
727                 set_wtmpsupp("<private room>");
728                 }
729         else {
730                 set_wtmpsupp(CC->quickroom.QRname);
731                 }
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 {
803                                 usergoto(towhere, 1);
804                                 return;
805                                 }
806                         }
807                 }
808
809         cprintf("%d room '%s' not found\n",ERROR+ROOM_NOT_FOUND,towhere);
810         }
811
812
813 void cmd_whok(void) {
814         struct usersupp temp;
815         struct cdbdata *cdbus;
816
817         if ((!(CC->logged_in))&&(!(CC->internal_pgm))) {
818                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
819                 return;
820                 }
821         getuser(&CC->usersupp,CC->curr_user);
822
823         if ((!is_room_aide()) && (!(CC->internal_pgm)) ) {
824                 cprintf("%d Higher access required.\n",
825                         ERROR+HIGHER_ACCESS_REQUIRED);
826                 return;
827                 }
828
829         cprintf("%d Who knows room:\n",LISTING_FOLLOWS);
830         cdb_rewind(CDB_USERSUPP);
831         while(cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
832                 memset(&temp, 0, sizeof(struct usersupp));
833                 memcpy(&temp, cdbus->ptr, cdbus->len);
834                 cdb_free(cdbus);
835
836                 if ( (CC->quickroom.QRflags & QR_INUSE)
837                         && (CtdlRoomAccess(&CC->quickroom, &temp) & UA_KNOWN)
838                    ) cprintf("%s\n",temp.fullname);
839                 }
840         cprintf("000\n");
841         }
842
843
844 /*
845  * RDIR command for room directory
846  */
847 void cmd_rdir(void) {
848         char buf[256];
849         char flnm[256];
850         char comment[256];
851         FILE *ls,*fd;
852         struct stat statbuf;
853
854         if (!(CC->logged_in)) {
855                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
856                 return;
857                 }
858
859         getroom(&CC->quickroom, CC->quickroom.QRname);
860         getuser(&CC->usersupp, CC->curr_user);
861
862         if ((CC->quickroom.QRflags & QR_DIRECTORY) == 0) {
863                 cprintf("%d not here.\n",ERROR+NOT_HERE);
864                 return;
865                 }
866
867         if (((CC->quickroom.QRflags & QR_VISDIR) == 0)
868            && (CC->usersupp.axlevel<6)
869            && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
870                 cprintf("%d not here.\n",ERROR+HIGHER_ACCESS_REQUIRED);
871                 return;
872                 }
873
874         cprintf("%d %s|%s/files/%s\n",
875                 LISTING_FOLLOWS,config.c_fqdn,BBSDIR,CC->quickroom.QRdirname);
876
877         sprintf(buf,"cd %s/files/%s; ls >%s 2>/dev/null",
878                 BBSDIR,CC->quickroom.QRdirname,CC->temp);
879         system(buf);
880
881         sprintf(buf,"%s/files/%s/filedir",BBSDIR,CC->quickroom.QRdirname);
882         fd = fopen(buf,"r");
883         if (fd==NULL) fd=fopen("/dev/null","r");
884
885         ls = fopen(CC->temp,"r");
886         while (fgets(flnm,256,ls)!=NULL) {
887                 flnm[strlen(flnm)-1]=0;
888                 if (strcasecmp(flnm,"filedir")) {
889                         sprintf(buf,"%s/files/%s/%s",
890                                 BBSDIR,CC->quickroom.QRdirname,flnm);
891                         stat(buf,&statbuf);
892                         strcpy(comment,"");
893                         fseek(fd,0L,0);
894                         while ((fgets(buf,256,fd)!=NULL)
895                             &&(strlen(comment)==0)) {
896                                 buf[strlen(buf)-1] = 0;
897                                 if ((!strncasecmp(buf,flnm,strlen(flnm)))
898                                    && (buf[strlen(flnm)]==' ')) 
899                                         strncpy(comment,
900                                                 &buf[strlen(flnm)+1],255);
901                                 }
902                         cprintf("%s|%ld|%s\n",flnm,statbuf.st_size,comment);
903                         }
904                 }
905         fclose(ls);
906         fclose(fd);
907         unlink(CC->temp);
908
909         cprintf("000\n");
910         }
911
912 /*
913  * get room parameters (aide or room aide command)
914  */
915 void cmd_getr(void) {
916         if ((!(CC->logged_in))&&(!(CC->internal_pgm))) {
917                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
918                 return;
919                 }
920
921         if ( (!is_room_aide()) && (!(CC->internal_pgm)) ) {
922                 cprintf("%d Higher access required.\n",
923                         ERROR+HIGHER_ACCESS_REQUIRED);
924                 return;
925                 }
926
927         if (is_noneditable(&CC->quickroom)) {
928                 cprintf("%d Can't edit this room.\n",ERROR+NOT_HERE);
929                 return;
930                 }
931
932         getroom(&CC->quickroom, CC->quickroom.QRname);
933         cprintf("%d%c%s|%s|%s|%d|%d\n",
934                 OK,check_express(),
935                 CC->quickroom.QRname,
936                 ((CC->quickroom.QRflags & QR_PASSWORDED) ? CC->quickroom.QRpasswd : ""),
937                 ((CC->quickroom.QRflags & QR_DIRECTORY) ? CC->quickroom.QRdirname : ""),
938                 CC->quickroom.QRflags,
939                 (int)CC->quickroom.QRfloor);
940         }
941
942
943 /*
944  * set room parameters (aide or room aide command)
945  */
946 void cmd_setr(char *args) {
947         char buf[256];
948         struct floor flbuf;
949         int old_floor;
950
951         if (!(CC->logged_in)) {
952                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
953                 return;
954                 }
955
956         if (!is_room_aide()) {
957                 cprintf("%d Higher access required.\n",
958                         ERROR+HIGHER_ACCESS_REQUIRED);
959                 return;
960                 }
961
962         if (is_noneditable(&CC->quickroom)) {
963                 cprintf("%d Can't edit this room.\n",ERROR+NOT_HERE);
964                 return;
965                 }
966
967         if (num_parms(args)>=6) {
968                 getfloor(&flbuf,extract_int(args,5));
969                 if ((flbuf.f_flags & F_INUSE) == 0) {
970                         cprintf("%d Invalid floor number.\n",
971                                 ERROR+INVALID_FLOOR_OPERATION);
972                         return;
973                         }
974                 }
975
976         lgetroom(&CC->quickroom, CC->quickroom.QRname);
977         extract(buf,args,0); buf[ROOMNAMELEN]=0;
978         strncpy(CC->quickroom.QRname,buf,ROOMNAMELEN-1);
979         extract(buf,args,1); buf[10]=0;
980         strncpy(CC->quickroom.QRpasswd,buf,9);
981         extract(buf,args,2); buf[15]=0;
982         strncpy(CC->quickroom.QRdirname,buf,19);
983         CC->quickroom.QRflags = ( extract_int(args,3) | QR_INUSE);
984
985         /* Clean up a client boo-boo: if the client set the room to
986          * guess-name or passworded, ensure that the private flag is
987          * also set.
988          */
989         if ((CC->quickroom.QRflags & QR_GUESSNAME)
990            ||(CC->quickroom.QRflags & QR_PASSWORDED))
991                 CC->quickroom.QRflags |= QR_PRIVATE;
992
993         /* Kick everyone out if the client requested it (by changing the
994          * room's generation number)
995          */
996         if (extract_int(args,4)) {
997                 time(&CC->quickroom.QRgen);
998                 }
999
1000         old_floor = CC->quickroom.QRfloor;
1001         if (num_parms(args)>=6) {
1002                 CC->quickroom.QRfloor = extract_int(args,5);
1003                 }
1004
1005         lputroom(&CC->quickroom, CC->quickroom.QRname);
1006
1007         /* adjust the floor reference counts */
1008         lgetfloor(&flbuf,old_floor);
1009         --flbuf.f_ref_count;
1010         lputfloor(&flbuf,old_floor);
1011         lgetfloor(&flbuf,CC->quickroom.QRfloor);
1012         ++flbuf.f_ref_count;
1013         lputfloor(&flbuf,CC->quickroom.QRfloor);
1014
1015         /* create a room directory if necessary */
1016         if (CC->quickroom.QRflags & QR_DIRECTORY) {
1017                 sprintf(buf,
1018                         "mkdir ./files/%s </dev/null >/dev/null 2>/dev/null",
1019                 CC->quickroom.QRdirname);
1020                 system(buf);
1021                 }
1022
1023         sprintf(buf,"%s> edited by %s",CC->quickroom.QRname,CC->curr_user);
1024         aide_message(buf);
1025         cprintf("%d Ok\n",OK);
1026         }
1027
1028
1029
1030 /* 
1031  * get the name of the room aide for this room
1032  */
1033 void cmd_geta(void) {
1034         struct usersupp usbuf;
1035
1036         if ((!(CC->logged_in))&&(!(CC->internal_pgm))) {
1037                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
1038                 return;
1039                 }
1040
1041         if (is_noneditable(&CC->quickroom)) {
1042                 cprintf("%d Can't edit this room.\n",ERROR+NOT_HERE);
1043                 return;
1044                 }
1045
1046         if (getuserbynumber(&usbuf,CC->quickroom.QRroomaide)==0) {
1047                 cprintf("%d %s\n",OK,usbuf.fullname);
1048                 }
1049         else {
1050                 cprintf("%d \n",OK);
1051                 }
1052         }
1053
1054
1055 /* 
1056  * set the room aide for this room
1057  */
1058 void cmd_seta(char *new_ra)
1059 {
1060         struct usersupp usbuf;
1061         long newu;
1062         char buf[256];
1063         int post_notice;
1064         
1065         if (!(CC->logged_in)) {
1066                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
1067                 return;
1068                 }
1069
1070         if (!is_room_aide()) {
1071                 cprintf("%d Higher access required.\n",
1072                         ERROR+HIGHER_ACCESS_REQUIRED);
1073                 return;
1074                 }
1075
1076         if (getuser(&usbuf,new_ra)!=0) {
1077                 newu = (-1L);
1078                 }
1079         else {
1080                 newu = usbuf.usernum;
1081                 }
1082
1083         lgetroom(&CC->quickroom, CC->quickroom.QRname);
1084         post_notice = 0;
1085         if (CC->quickroom.QRroomaide != newu) {
1086                 post_notice = 1;
1087                 }
1088         CC->quickroom.QRroomaide = newu;
1089         lputroom(&CC->quickroom, CC->quickroom.QRname);
1090
1091         /*
1092          * We have to post the change notice _after_ writing changes to 
1093          * the room table, otherwise it would deadlock!
1094          */
1095         if (post_notice == 1) {
1096                 sprintf(buf,"%s is now room aide for %s>",
1097                         usbuf.fullname,CC->quickroom.QRname);
1098                 aide_message(buf);
1099                 }
1100         cprintf("%d Ok\n",OK);
1101         }
1102
1103 /*
1104  * Generate an associated file name for a room
1105  */
1106 void assoc_file_name(char *buf, struct quickroom *qrbuf, char *prefix) {
1107         int a;
1108
1109         sprintf(buf, "./prefix/%s.%ld", qrbuf->QRname, qrbuf->QRgen);
1110         for (a=0; a<strlen(buf); ++a) {
1111                 if (buf[a]==32) buf[a]='.';
1112                 }
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         /* Delete the info file */
1150         assoc_file_name(aaa, qrbuf, "info");
1151         unlink(aaa);
1152
1153         /* Delete the image file */
1154         assoc_file_name(aaa, qrbuf, "images");
1155         unlink(aaa);
1156
1157         /* first flag the room record as not in use */
1158         lgetroom(qrbuf, qrbuf->QRname);
1159         qrbuf->QRflags=0;
1160
1161         /* then delete the messages in the room */
1162         get_msglist(qrbuf);
1163         if (CC->num_msgs > 0) for (a=0; a < CC->num_msgs; ++a) {
1164                 MsgToDelete = MessageFromList(a);
1165                 cdb_delete(CDB_MSGMAIN, &MsgToDelete, sizeof(long));
1166                 }
1167         put_msglist(qrbuf);
1168         free(CC->msglist);
1169         CC->msglist = NULL;
1170         CC->num_msgs = 0;
1171         delete_msglist(qrbuf);
1172         lputroom(qrbuf, qrbuf->QRname);
1173
1174         /* then decrement the reference count for the floor */
1175         lgetfloor(&flbuf,(int)(qrbuf->QRfloor));
1176         flbuf.f_ref_count = flbuf.f_ref_count - 1;
1177         lputfloor(&flbuf,(int)(qrbuf->QRfloor));
1178
1179         /* Delete the room record from the database! */
1180         putroom(NULL, qrbuf->QRname);
1181         }
1182
1183
1184 /*
1185  * aide command: kill the current room
1186  */
1187 void cmd_kill(char *argbuf) {
1188         char aaa[100];
1189         char deleted_room_name[ROOMNAMELEN];
1190         int kill_ok;
1191         
1192         kill_ok = extract_int(argbuf,0);
1193
1194         if (!(CC->logged_in)) {
1195                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
1196                 return;
1197                 }
1198
1199         if (!is_room_aide()) {
1200                 cprintf("%d Higher access required.\n",
1201                         ERROR+HIGHER_ACCESS_REQUIRED);
1202                 return;
1203                 }
1204
1205         if (is_noneditable(&CC->quickroom)) {
1206                 cprintf("%d Can't edit this room.\n",ERROR+NOT_HERE);
1207                 return;
1208                 }
1209
1210         if (kill_ok) {
1211                 strcpy(deleted_room_name, CC->quickroom.QRname);
1212                 delete_room(&CC->quickroom);    /* Do the dirty work */
1213                 usergoto(BASEROOM, 0);          /* Return to the Lobby */
1214
1215                 /* tell the world what we did */
1216                 sprintf(aaa,"%s> killed by %s",
1217                         deleted_room_name, CC->curr_user);
1218                 aide_message(aaa);
1219                 cprintf("%d '%s' deleted.\n", OK, deleted_room_name);
1220                 }
1221         else {
1222                 cprintf("%d ok to delete.\n",OK);
1223                 }
1224         }
1225
1226
1227 /*
1228  * Internal code to create a new room (returns room flags)
1229  *
1230  * Room types:  0=public, 1=guessname, 2=passworded, 3=inv-only, 4=mailbox
1231  */
1232 unsigned create_room(char *new_room_name,
1233                         int new_room_type,
1234                         char *new_room_pass,
1235                         int new_room_floor) {
1236
1237         struct quickroom qrbuf;
1238         struct floor flbuf;
1239         struct visit vbuf;
1240
1241         if (getroom(&qrbuf, new_room_name)==0) return(0); /* already exists */
1242
1243         memset(&qrbuf, 0, sizeof(struct quickroom));
1244         strncpy(qrbuf.QRname,new_room_name,ROOMNAMELEN);
1245         strncpy(qrbuf.QRpasswd,new_room_pass,9);
1246         qrbuf.QRflags = QR_INUSE;
1247         if (new_room_type > 0) qrbuf.QRflags=(qrbuf.QRflags|QR_PRIVATE);
1248         if (new_room_type == 1) qrbuf.QRflags=(qrbuf.QRflags|QR_GUESSNAME);
1249         if (new_room_type == 2) qrbuf.QRflags=(qrbuf.QRflags|QR_PASSWORDED);
1250         if (new_room_type == 4) qrbuf.QRflags=(qrbuf.QRflags|QR_MAILBOX);
1251
1252         /* If the room is private, and the system administrator has elected
1253          * to automatically grant room aide privileges, do so now; otherwise,
1254          * set the room aide to undefined.
1255          */
1256         if ( (qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE==1) ) {
1257                 qrbuf.QRroomaide=CC->usersupp.usernum;
1258                 }
1259         else {
1260                 qrbuf.QRroomaide = (-1L);
1261                 }
1262
1263         qrbuf.QRhighest = 0L;   /* No messages in this room yet */
1264         time(&qrbuf.QRgen);     /* Use a timestamp as the generation number */
1265         qrbuf.QRfloor = new_room_floor;
1266
1267         /* save what we just did... */
1268         putroom(&qrbuf, qrbuf.QRname);
1269
1270         /* bump the reference count on whatever floor the room is on */
1271         lgetfloor(&flbuf,(int)qrbuf.QRfloor);
1272         flbuf.f_ref_count = flbuf.f_ref_count + 1;
1273         lputfloor(&flbuf,(int)qrbuf.QRfloor);
1274
1275         /* be sure not to kick the creator out of the room! */
1276         lgetuser(&CC->usersupp,CC->curr_user);
1277         CtdlGetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1278         vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1279         vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1280         CtdlSetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1281         lputuser(&CC->usersupp,CC->curr_user);
1282
1283         /* resume our happy day */
1284         return(qrbuf.QRflags);
1285         }
1286
1287
1288 /*
1289  * create a new room
1290  */
1291 void cmd_cre8(char *args)
1292 {
1293         int cre8_ok;
1294         char new_room_name[256];
1295         int new_room_type;
1296         char new_room_pass[256];
1297         int new_room_floor;
1298         char aaa[256];
1299         unsigned newflags;
1300         struct quickroom qrbuf;
1301         struct floor flbuf;
1302
1303         cre8_ok = extract_int(args,0);
1304         extract(new_room_name,args,1);
1305         new_room_name[ROOMNAMELEN-1] = 0;
1306         new_room_type = extract_int(args,2);
1307         extract(new_room_pass,args,3);
1308         new_room_pass[9] = 0;
1309         new_room_floor = 0;
1310
1311         if ((strlen(new_room_name)==0) && (cre8_ok==1)) {
1312                 cprintf("%d Invalid room name.\n",ERROR);
1313                 return;
1314                 }
1315
1316         if (num_parms(args)>=5) {
1317                 getfloor(&flbuf,extract_int(args,4));
1318                 if ((flbuf.f_flags & F_INUSE) == 0) {
1319                         cprintf("%d Invalid floor number.\n",
1320                                 ERROR+INVALID_FLOOR_OPERATION);
1321                         return;
1322                         }
1323                 else {
1324                         new_room_floor = extract_int(args,4);
1325                         }
1326                 }
1327
1328         if (!(CC->logged_in)) {
1329                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
1330                 return;
1331                 }
1332
1333         if (CC->usersupp.axlevel<3) {
1334                 cprintf("%d You need higher access to create rooms.\n",
1335                         ERROR+HIGHER_ACCESS_REQUIRED);
1336                 return;
1337                 }
1338
1339         if ((strlen(new_room_name)==0) && (cre8_ok==0)) {
1340                 cprintf("%d Ok to create rooms.\n", OK);
1341                 return;
1342                 }
1343
1344         /* Check to make sure the requested room name doesn't already exist */
1345         if (getroom(&qrbuf, new_room_name)==0) {
1346                 cprintf("%d '%s' already exists.\n",
1347                         ERROR,qrbuf.QRname);
1348                 return;
1349                 }
1350
1351         if ((new_room_type < 0) || (new_room_type > 3)) {
1352                 cprintf("%d Invalid room type.\n",ERROR);
1353                 return;
1354                 }
1355
1356         if (cre8_ok == 0) {
1357                 cprintf("%d OK to create '%s'\n", OK, new_room_name);
1358                 return;
1359                 }
1360
1361         newflags = create_room(new_room_name,
1362                         new_room_type,new_room_pass,new_room_floor);
1363
1364         /* post a message in Aide> describing the new room */
1365         strncpy(aaa,new_room_name,255);
1366         strcat(aaa,"> created by ");
1367         strcat(aaa,CC->usersupp.fullname);
1368         if (newflags&QR_PRIVATE) strcat(aaa," [private]");
1369         if (newflags&QR_GUESSNAME) strcat(aaa,"[guessname] ");
1370         if (newflags&QR_PASSWORDED) {
1371                 strcat(aaa,"\n Password: ");
1372                 strcat(aaa,new_room_pass);
1373                 }
1374         aide_message(aaa); 
1375
1376         cprintf("%d '%s' has been created.\n",OK,qrbuf.QRname);
1377         }
1378
1379
1380
1381 void cmd_einf(char *ok)
1382 {       /* enter info file for current room */
1383         FILE *fp;
1384         char infofilename[64];
1385         char buf[256];
1386
1387         if (!(CC->logged_in)) {
1388                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
1389                 return;
1390                 }
1391
1392         if (!is_room_aide()) {
1393                 cprintf("%d Higher access required.\n",
1394                         ERROR+HIGHER_ACCESS_REQUIRED);
1395                 return;
1396                 }
1397
1398         if (atoi(ok)==0) {
1399                 cprintf("%d Ok.\n",OK);
1400                 return;
1401                 }
1402
1403         cprintf("%d Send info...\n",SEND_LISTING);
1404
1405         assoc_file_name(infofilename, &CC->quickroom, "info");
1406
1407         fp=fopen(infofilename,"w");
1408         do {
1409                 client_gets(buf);
1410                 if (strcmp(buf,"000")) fprintf(fp,"%s\n",buf);
1411                 } while(strcmp(buf,"000"));
1412         fclose(fp);
1413
1414         /* now update the room index so people will see our new info */
1415         lgetroom(&CC->quickroom,CC->quickroom.QRname); /* lock so no one steps on us */
1416         CC->quickroom.QRinfo = CC->quickroom.QRhighest + 1L;
1417         lputroom(&CC->quickroom,CC->quickroom.QRname);
1418         }
1419
1420
1421 /* 
1422  * cmd_lflr()   -  List all known floors
1423  */
1424 void cmd_lflr(void) {
1425         int a;
1426         struct floor flbuf;
1427
1428         if (!(CC->logged_in)) {
1429                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
1430                 return;
1431                 }
1432
1433         /* if (getuser(&CC->usersupp,CC->curr_user)) {
1434                 cprintf("%d Can't locate user!\n",ERROR+INTERNAL_ERROR);
1435                 return;
1436                 }
1437         */
1438
1439         cprintf("%d Known floors:\n",LISTING_FOLLOWS);
1440         
1441         for (a=0; a<MAXFLOORS; ++a) {
1442                 getfloor(&flbuf,a);
1443                 if (flbuf.f_flags & F_INUSE) {
1444                         cprintf("%d|%s|%d\n",
1445                                 a,
1446                                 flbuf.f_name,
1447                                 flbuf.f_ref_count);
1448                         }
1449                 }
1450         cprintf("000\n");
1451         }
1452
1453
1454
1455 /*
1456  * create a new floor
1457  */
1458 void cmd_cflr(char *argbuf)
1459 {
1460         char new_floor_name[256];
1461         struct floor flbuf;
1462         int cflr_ok;
1463         int free_slot = (-1);
1464         int a;
1465
1466         extract(new_floor_name,argbuf,0);
1467         cflr_ok = extract_int(argbuf,1);
1468
1469         
1470         if (!(CC->logged_in)) {
1471                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
1472                 return;
1473                 }
1474
1475         if (CC->usersupp.axlevel<6) {
1476                 cprintf("%d You need higher access to create rooms.\n",
1477                         ERROR+HIGHER_ACCESS_REQUIRED);
1478                 return;
1479                 }
1480
1481         for (a=0; a<MAXFLOORS; ++a) {
1482                 getfloor(&flbuf,a);
1483
1484                 /* note any free slots while we're scanning... */
1485                 if ( ((flbuf.f_flags & F_INUSE)==0) 
1486                      && (free_slot < 0) )  free_slot = a;
1487
1488                 /* check to see if it already exists */
1489                 if ( (!strcasecmp(flbuf.f_name,new_floor_name))
1490                      && (flbuf.f_flags & F_INUSE) ) {
1491                         cprintf("%d Floor '%s' already exists.\n",
1492                                 ERROR+ALREADY_EXISTS,
1493                                 flbuf.f_name);
1494                         return;
1495                         }
1496
1497                 }
1498
1499         if (free_slot<0) {
1500                 cprintf("%d There is no space available for a new floor.\n",
1501                         ERROR+INVALID_FLOOR_OPERATION);
1502                 return;
1503                 }
1504
1505         if (cflr_ok==0) {
1506                 cprintf("%d ok to create...\n",OK);
1507                 return;
1508                 }
1509
1510         lgetfloor(&flbuf,free_slot);
1511         flbuf.f_flags = F_INUSE;
1512         flbuf.f_ref_count = 0;
1513         strncpy(flbuf.f_name,new_floor_name,255);
1514         lputfloor(&flbuf,free_slot);
1515         cprintf("%d %d\n",OK,free_slot);
1516         }
1517
1518
1519
1520 /*
1521  * delete a floor
1522  */
1523 void cmd_kflr(char *argbuf)
1524 {
1525         struct floor flbuf;
1526         int floor_to_delete;
1527         int kflr_ok;
1528         int delete_ok;
1529
1530         floor_to_delete = extract_int(argbuf,0);
1531         kflr_ok = extract_int(argbuf,1);
1532
1533         
1534         if (!(CC->logged_in)) {
1535                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
1536                 return;
1537                 }
1538
1539         if (CC->usersupp.axlevel<6) {
1540                 cprintf("%d You need higher access to delete floors.\n",
1541                         ERROR+HIGHER_ACCESS_REQUIRED);
1542                 return;
1543                 }
1544
1545         lgetfloor(&flbuf,floor_to_delete);
1546
1547         delete_ok = 1;  
1548         if ((flbuf.f_flags & F_INUSE) == 0) {
1549                 cprintf("%d Floor %d not in use.\n",
1550                         ERROR+INVALID_FLOOR_OPERATION,floor_to_delete);
1551                 delete_ok = 0;
1552                 }
1553
1554         else {
1555                 if (flbuf.f_ref_count != 0) {
1556                         cprintf("%d Cannot delete; floor contains %d rooms.\n",
1557                                 ERROR+INVALID_FLOOR_OPERATION,
1558                                 flbuf.f_ref_count);
1559                         delete_ok = 0;
1560                         }
1561
1562                 else {
1563                         if (kflr_ok == 1) {
1564                                 cprintf("%d Ok\n",OK);
1565                                 }
1566                         else {
1567                                 cprintf("%d Ok to delete...\n",OK);
1568                                 }
1569
1570                         }
1571
1572                 }
1573
1574         if ( (delete_ok == 1) && (kflr_ok == 1) ) flbuf.f_flags = 0;
1575         lputfloor(&flbuf,floor_to_delete);
1576         }
1577
1578 /*
1579  * edit a floor
1580  */
1581 void cmd_eflr(char *argbuf)
1582 {
1583         struct floor flbuf;
1584         int floor_num;
1585         int np;
1586
1587         np = num_parms(argbuf);
1588         if (np < 1) {
1589                 cprintf("%d Usage error.\n",ERROR);
1590                 return;
1591                 }
1592         
1593         if (!(CC->logged_in)) {
1594                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
1595                 return;
1596                 }
1597
1598         if (CC->usersupp.axlevel<6) {
1599                 cprintf("%d You need higher access to edit floors.\n",
1600                         ERROR+HIGHER_ACCESS_REQUIRED);
1601                 return;
1602                 }
1603
1604         floor_num = extract_int(argbuf,0);
1605         lgetfloor(&flbuf,floor_num);
1606         if ( (flbuf.f_flags & F_INUSE) == 0) {
1607                 lputfloor(&flbuf,floor_num);
1608                 cprintf("%d Floor %d is not in use.\n",
1609                         ERROR+INVALID_FLOOR_OPERATION,floor_num);
1610                 return;
1611                 }
1612         if (np >= 2) extract(flbuf.f_name,argbuf,1);
1613         lputfloor(&flbuf,floor_num);
1614         
1615         cprintf("%d Ok\n",OK);
1616         }