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