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