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