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