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