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