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