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