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