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