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