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