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