]> code.citadel.org Git - citadel.git/blob - citadel/room_ops.c
* Add LPRM command
[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|%d\n",
501                 qrbuf->QRflags,
502                 (int) qrbuf->QRfloor,
503                 (int) qrbuf->QRorder,
504                 (int) qrbuf->QRflags2
505         );
506 }
507
508
509 /* 
510  * cmd_lrms()   -  List all accessible rooms, known or forgotten
511  */
512 void cmd_lrms_backend(struct quickroom *qrbuf, void *data)
513 {
514         int FloorBeingSearched = (-1);
515         FloorBeingSearched = *(int *)data;
516
517         if (((CtdlRoomAccess(qrbuf, &CC->usersupp)
518               & (UA_KNOWN | UA_ZAPPED)))
519             && ((qrbuf->QRfloor == (FloorBeingSearched))
520                 || ((FloorBeingSearched) < 0)))
521                 list_roomname(qrbuf);
522 }
523
524 void cmd_lrms(char *argbuf)
525 {
526         int FloorBeingSearched = (-1);
527         if (strlen(argbuf) > 0)
528                 FloorBeingSearched = extract_int(argbuf, 0);
529
530         if (CtdlAccessCheck(ac_logged_in)) return;
531
532         if (getuser(&CC->usersupp, CC->curr_user)) {
533                 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
534                 return;
535         }
536         cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
537
538         ForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
539         cprintf("000\n");
540 }
541
542
543
544 /* 
545  * cmd_lkra()   -  List all known rooms
546  */
547 void cmd_lkra_backend(struct quickroom *qrbuf, void *data)
548 {
549         int FloorBeingSearched = (-1);
550         FloorBeingSearched = *(int *)data;
551
552         if (((CtdlRoomAccess(qrbuf, &CC->usersupp)
553               & (UA_KNOWN)))
554             && ((qrbuf->QRfloor == (FloorBeingSearched))
555                 || ((FloorBeingSearched) < 0)))
556                 list_roomname(qrbuf);
557 }
558
559 void cmd_lkra(char *argbuf)
560 {
561         int FloorBeingSearched = (-1);
562         if (strlen(argbuf) > 0)
563                 FloorBeingSearched = extract_int(argbuf, 0);
564
565         if (CtdlAccessCheck(ac_logged_in)) return;
566         
567         if (getuser(&CC->usersupp, CC->curr_user)) {
568                 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
569                 return;
570         }
571         cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
572
573         ForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
574         cprintf("000\n");
575 }
576
577
578
579 void cmd_lprm_backend(struct quickroom *qrbuf, void *data)
580 {
581         int FloorBeingSearched = (-1);
582         FloorBeingSearched = *(int *)data;
583
584         if (   ((qrbuf->QRflags & QR_PRIVATE) == 0)
585                 && ((qrbuf->QRflags & QR_MAILBOX) == 0)
586             && ((qrbuf->QRfloor == (FloorBeingSearched))
587                 || ((FloorBeingSearched) < 0)))
588                 list_roomname(qrbuf);
589 }
590
591 void cmd_lprm(char *argbuf)
592 {
593         int FloorBeingSearched = (-1);
594         if (strlen(argbuf) > 0)
595                 FloorBeingSearched = extract_int(argbuf, 0);
596
597         cprintf("%d Publiic rooms:\n", LISTING_FOLLOWS);
598
599         ForEachRoom(cmd_lprm_backend, &FloorBeingSearched);
600         cprintf("000\n");
601 }
602
603
604
605 /* 
606  * cmd_lkrn()   -  List all known rooms with new messages
607  */
608 void cmd_lkrn_backend(struct quickroom *qrbuf, void *data)
609 {
610         int ra;
611         int FloorBeingSearched = (-1);
612         FloorBeingSearched = *(int *)data;
613
614         ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
615         if ((ra & UA_KNOWN)
616             && (ra & UA_HASNEWMSGS)
617             && ((qrbuf->QRfloor == (FloorBeingSearched))
618                 || ((FloorBeingSearched) < 0)))
619                 list_roomname(qrbuf);
620 }
621
622 void cmd_lkrn(char *argbuf)
623 {
624         int FloorBeingSearched = (-1);
625         if (strlen(argbuf) > 0)
626                 FloorBeingSearched = extract_int(argbuf, 0);
627
628         if (CtdlAccessCheck(ac_logged_in)) return;
629         
630         if (getuser(&CC->usersupp, CC->curr_user)) {
631                 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
632                 return;
633         }
634         cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
635
636         ForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
637         cprintf("000\n");
638 }
639
640
641
642 /* 
643  * cmd_lkro()   -  List all known rooms
644  */
645 void cmd_lkro_backend(struct quickroom *qrbuf, void *data)
646 {
647         int ra;
648         int FloorBeingSearched = (-1);
649         FloorBeingSearched = *(int *)data;
650
651         ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
652         if ((ra & UA_KNOWN)
653             && ((ra & UA_HASNEWMSGS) == 0)
654             && ((qrbuf->QRfloor == (FloorBeingSearched))
655                 || ((FloorBeingSearched) < 0)))
656                 list_roomname(qrbuf);
657 }
658
659 void cmd_lkro(char *argbuf)
660 {
661         int FloorBeingSearched = (-1);
662         if (strlen(argbuf) > 0)
663                 FloorBeingSearched = extract_int(argbuf, 0);
664
665         if (CtdlAccessCheck(ac_logged_in)) return;
666         
667         if (getuser(&CC->usersupp, CC->curr_user)) {
668                 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
669                 return;
670         }
671         cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
672
673         ForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
674         cprintf("000\n");
675 }
676
677
678
679 /* 
680  * cmd_lzrm()   -  List all forgotten rooms
681  */
682 void cmd_lzrm_backend(struct quickroom *qrbuf, void *data)
683 {
684         int ra;
685         int FloorBeingSearched = (-1);
686         FloorBeingSearched = *(int *)data;
687
688         ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
689         if ((ra & UA_GOTOALLOWED)
690             && (ra & UA_ZAPPED)
691             && ((qrbuf->QRfloor == (FloorBeingSearched))
692                 || ((FloorBeingSearched) < 0)))
693                 list_roomname(qrbuf);
694 }
695
696 void cmd_lzrm(char *argbuf)
697 {
698         int FloorBeingSearched = (-1);
699         if (strlen(argbuf) > 0)
700                 FloorBeingSearched = extract_int(argbuf, 0);
701
702         if (CtdlAccessCheck(ac_logged_in)) return;
703         
704         if (getuser(&CC->usersupp, CC->curr_user)) {
705                 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
706                 return;
707         }
708         cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
709
710         ForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
711         cprintf("000\n");
712 }
713
714
715 /*
716  * Make the specified room the current room for this session.  No validation
717  * or access control is done here -- the caller should make sure that the
718  * specified room exists and is ok to access.
719  */
720 void usergoto(char *where, int display_result, int transiently,
721                 int *retmsgs, int *retnew)
722 {
723         int a;
724         int new_messages = 0;
725         int total_messages = 0;
726         int info = 0;
727         int rmailflag;
728         int raideflag;
729         int newmailcount = 0;
730         struct visit vbuf;
731         char truncated_roomname[ROOMNAMELEN];
732         struct cdbdata *cdbfr;
733         long *msglist = NULL;
734         int num_msgs = 0;
735
736         /* If the supplied room name is NULL, the caller wants us to know that
737          * it has already copied the quickroom record into CC->quickroom, so
738          * we can skip the extra database fetch.
739          */
740         if (where != NULL) {
741                 strcpy(CC->quickroom.QRname, where);
742                 getroom(&CC->quickroom, where);
743         }
744
745         /* Take care of all the formalities. */
746
747         begin_critical_section(S_USERSUPP);
748         CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
749
750         /* Know the room ... but not if it's the page log room, or if the
751          * caller specified that we're only entering this room transiently.
752          */
753         if ((strcasecmp(CC->quickroom.QRname, config.c_logpages))
754            && (transiently == 0) ) {
755                 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
756                 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
757         }
758         CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
759         end_critical_section(S_USERSUPP);
760
761         /* Check for new mail */
762         newmailcount = NewMailCount();
763
764         /* set info to 1 if the user needs to read the room's info file */
765         if (CC->quickroom.QRinfo > vbuf.v_lastseen) {
766                 info = 1;
767         }
768
769         get_mm();
770         cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->quickroom.QRnumber, sizeof(long));
771         if (cdbfr != NULL) {
772                 msglist = mallok(cdbfr->len);
773                 memcpy(msglist, cdbfr->ptr, cdbfr->len);
774                 num_msgs = cdbfr->len / sizeof(long);
775                 cdb_free(cdbfr);
776         }
777
778         if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
779                 if (msglist[a] > 0L) {
780                         ++total_messages;
781                         if (is_msg_in_mset(vbuf.v_seen, msglist[a]) == 0) {
782                                 ++new_messages;
783                         }
784                 }
785         }
786
787         if (msglist != NULL) phree(msglist);
788
789         if (CC->quickroom.QRflags & QR_MAILBOX)
790                 rmailflag = 1;
791         else
792                 rmailflag = 0;
793
794         if ((CC->quickroom.QRroomaide == CC->usersupp.usernum)
795             || (CC->usersupp.axlevel >= 6))
796                 raideflag = 1;
797         else
798                 raideflag = 0;
799
800         strcpy(truncated_roomname, CC->quickroom.QRname);
801         if ( (CC->quickroom.QRflags & QR_MAILBOX)
802            && (atol(CC->quickroom.QRname) == CC->usersupp.usernum) ) {
803                 strcpy(truncated_roomname, &truncated_roomname[11]);
804         }
805
806         if (retmsgs != NULL) *retmsgs = total_messages;
807         if (retnew != NULL) *retnew = new_messages;
808         lprintf(9, "<%s> %d new of %d total messages\n",
809                 CC->quickroom.QRname, new_messages, total_messages);
810
811         if (display_result)
812                 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d\n",
813                         CIT_OK, CtdlCheckExpress(),
814                         truncated_roomname,
815                         new_messages, total_messages,
816                         info, CC->quickroom.QRflags,
817                         CC->quickroom.QRhighest,
818                         vbuf.v_lastseen,
819                         rmailflag, raideflag, newmailcount,
820                         CC->quickroom.QRfloor,
821                         vbuf.v_view);
822 }
823
824
825 /* 
826  * cmd_goto()  -  goto a new room
827  */
828 void cmd_goto(char *gargs)
829 {
830         struct quickroom QRscratch;
831         int c;
832         int ok = 0;
833         int ra;
834         char augmented_roomname[SIZ];
835         char towhere[SIZ];
836         char password[SIZ];
837         int transiently = 0;
838
839         if (CtdlAccessCheck(ac_logged_in)) return;
840
841         extract(towhere, gargs, 0);
842         extract(password, gargs, 1);
843         transiently = extract_int(gargs, 2);
844
845         getuser(&CC->usersupp, CC->curr_user);
846
847         if (!strcasecmp(towhere, "_BASEROOM_"))
848                 strcpy(towhere, config.c_baseroom);
849
850         if (!strcasecmp(towhere, "_MAIL_"))
851                 strcpy(towhere, MAILROOM);
852
853         if (!strcasecmp(towhere, "_BITBUCKET_"))
854                 strcpy(towhere, config.c_twitroom);
855
856
857         /* First try a regular match */
858         c = getroom(&QRscratch, towhere);
859
860         /* Then try a mailbox name match */
861         if (c != 0) {
862                 MailboxName(augmented_roomname, sizeof augmented_roomname,
863                             &CC->usersupp, towhere);
864                 c = getroom(&QRscratch, augmented_roomname);
865                 if (c == 0)
866                         strcpy(towhere, augmented_roomname);
867         }
868
869         /* And if the room was found... */
870         if (c == 0) {
871
872                 /* Let internal programs go directly to any room. */
873                 if (CC->internal_pgm) {
874                         memcpy(&CC->quickroom, &QRscratch,
875                                 sizeof(struct quickroom));
876                         usergoto(NULL, 1, transiently, NULL, NULL);
877                         return;
878                 }
879
880                 /* See if there is an existing user/room relationship */
881                 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
882
883                 /* normal clients have to pass through security */
884                 if (ra & UA_GOTOALLOWED) {
885                         ok = 1;
886                 }
887
888                 if (ok == 1) {
889                         if ((QRscratch.QRflags & QR_MAILBOX) &&
890                             ((ra & UA_GOTOALLOWED))) {
891                                 memcpy(&CC->quickroom, &QRscratch,
892                                         sizeof(struct quickroom));
893                                 usergoto(NULL, 1, transiently, NULL, NULL);
894                                 return;
895                         } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
896                             ((ra & UA_KNOWN) == 0) &&
897                             (strcasecmp(QRscratch.QRpasswd, password)) &&
898                             (CC->usersupp.axlevel < 6)
899                             ) {
900                                 cprintf("%d wrong or missing passwd\n",
901                                         ERROR + PASSWORD_REQUIRED);
902                                 return;
903                         } else if ((QRscratch.QRflags & QR_PRIVATE) &&
904                                    ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
905                                    ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
906                                    ((ra & UA_KNOWN) == 0) &&
907                                    (CC->usersupp.axlevel < 6)
908                                   ) {
909                                 lprintf(9, "Failed to acquire private room\n");
910                                 goto NOPE;
911                         } else {
912                                 memcpy(&CC->quickroom, &QRscratch,
913                                         sizeof(struct quickroom));
914                                 usergoto(NULL, 1, transiently, NULL, NULL);
915                                 return;
916                         }
917                 }
918         }
919
920 NOPE:   cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
921 }
922
923
924 void cmd_whok(void)
925 {
926         struct usersupp temp;
927         struct cdbdata *cdbus;
928
929         getuser(&CC->usersupp, CC->curr_user);
930
931         /*
932          * This command is only allowed by aides, room aides,
933          * and room namespace owners
934          */
935         if (is_room_aide()
936            || (atol(CC->quickroom.QRname) == CC->usersupp.usernum) ) {
937                 /* access granted */
938         }
939         else {
940                 /* access denied */
941                 cprintf("%d Higher access or room ownership required.\n",
942                         ERROR + HIGHER_ACCESS_REQUIRED);
943                 return;
944         }
945
946         cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
947         cdb_rewind(CDB_USERSUPP);
948         while (cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
949                 memset(&temp, 0, sizeof temp);
950                 memcpy(&temp, cdbus->ptr, sizeof temp);
951                 cdb_free(cdbus);
952
953                 if ((CC->quickroom.QRflags & QR_INUSE)
954                     && (CtdlRoomAccess(&CC->quickroom, &temp) & UA_KNOWN)
955                     )
956                         cprintf("%s\n", temp.fullname);
957         }
958         cprintf("000\n");
959 }
960
961
962 /*
963  * RDIR command for room directory
964  */
965 void cmd_rdir(void)
966 {
967         char buf[SIZ];
968         char flnm[SIZ];
969         char comment[SIZ];
970         FILE *ls, *fd;
971         struct stat statbuf;
972
973         if (CtdlAccessCheck(ac_logged_in)) return;
974         
975         getroom(&CC->quickroom, CC->quickroom.QRname);
976         getuser(&CC->usersupp, CC->curr_user);
977
978         if ((CC->quickroom.QRflags & QR_DIRECTORY) == 0) {
979                 cprintf("%d not here.\n", ERROR + NOT_HERE);
980                 return;
981         }
982         if (((CC->quickroom.QRflags & QR_VISDIR) == 0)
983             && (CC->usersupp.axlevel < 6)
984             && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
985                 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
986                 return;
987         }
988         cprintf("%d %s|%s/files/%s\n",
989         LISTING_FOLLOWS, config.c_fqdn, BBSDIR, CC->quickroom.QRdirname);
990
991         snprintf(buf, sizeof buf, "ls %s/files/%s  >%s 2> /dev/null",
992                 BBSDIR, CC->quickroom.QRdirname, CC->temp);
993         system(buf);
994
995         snprintf(buf, sizeof buf, "%s/files/%s/filedir", BBSDIR, CC->quickroom.QRdirname);
996         fd = fopen(buf, "r");
997         if (fd == NULL)
998                 fd = fopen("/dev/null", "r");
999
1000         ls = fopen(CC->temp, "r");
1001         while (fgets(flnm, sizeof flnm, ls) != NULL) {
1002                 flnm[strlen(flnm) - 1] = 0;
1003                 if (strcasecmp(flnm, "filedir")) {
1004                         snprintf(buf, sizeof buf, "%s/files/%s/%s",
1005                                 BBSDIR, CC->quickroom.QRdirname, flnm);
1006                         stat(buf, &statbuf);
1007                         strcpy(comment, "");
1008                         fseek(fd, 0L, 0);
1009                         while ((fgets(buf, sizeof buf, fd) != NULL)
1010                                && (strlen(comment) == 0)) {
1011                                 buf[strlen(buf) - 1] = 0;
1012                                 if ((!strncasecmp(buf, flnm, strlen(flnm)))
1013                                     && (buf[strlen(flnm)] == ' '))
1014                                         safestrncpy(comment,
1015                                             &buf[strlen(flnm) + 1],
1016                                             sizeof comment);
1017                         }
1018                         cprintf("%s|%ld|%s\n", flnm, (long)statbuf.st_size, comment);
1019                 }
1020         }
1021         fclose(ls);
1022         fclose(fd);
1023         unlink(CC->temp);
1024
1025         cprintf("000\n");
1026 }
1027
1028 /*
1029  * get room parameters (aide or room aide command)
1030  */
1031 void cmd_getr(void)
1032 {
1033         if (CtdlAccessCheck(ac_room_aide)) return;
1034
1035         getroom(&CC->quickroom, CC->quickroom.QRname);
1036         cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1037                 CIT_OK,
1038                 CtdlCheckExpress(),
1039
1040                 ((CC->quickroom.QRflags & QR_MAILBOX) ?
1041                         &CC->quickroom.QRname[11] : CC->quickroom.QRname),
1042
1043                 ((CC->quickroom.QRflags & QR_PASSWORDED) ?
1044                         CC->quickroom.QRpasswd : ""),
1045
1046                 ((CC->quickroom.QRflags & QR_DIRECTORY) ?
1047                         CC->quickroom.QRdirname : ""),
1048
1049                 CC->quickroom.QRflags,
1050                 (int) CC->quickroom.QRfloor,
1051                 (int) CC->quickroom.QRorder,
1052
1053                 CC->quickroom.QRdefaultview,
1054                 CC->quickroom.QRflags2
1055                 );
1056 }
1057
1058
1059 /*
1060  * Back end function to rename a room.
1061  * You can also specify which floor to move the room to, or specify -1 to
1062  * keep the room on the same floor it was on.
1063  *
1064  * If you are renaming a mailbox room, you must supply the namespace prefix
1065  * in *at least* the old name!
1066  */
1067 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1068         int old_floor = 0;
1069         struct quickroom qrbuf;
1070         struct quickroom qrtmp;
1071         int ret = 0;
1072         struct floor *fl;
1073         struct floor flbuf;
1074         long owner = 0L;
1075
1076         lprintf(9, "CtdlRenameRoom(%s, %s, %d)\n",
1077                 old_name, new_name, new_floor);
1078
1079         if (new_floor >= 0) {
1080                 fl = cgetfloor(new_floor);
1081                 if ((fl->f_flags & F_INUSE) == 0) {
1082                         return(crr_invalid_floor);
1083                 }
1084         }
1085
1086         begin_critical_section(S_QUICKROOM);
1087
1088         if ( (getroom(&qrtmp, new_name) == 0) 
1089            && (strcasecmp(new_name, old_name)) ) {
1090                 ret = crr_already_exists;
1091         }
1092
1093         else if (getroom(&qrbuf, old_name) != 0) {
1094                 ret = crr_room_not_found;
1095         }
1096
1097         else if ( (CC->usersupp.axlevel < 6)
1098                   && (CC->usersupp.usernum != qrbuf.QRroomaide)
1099                   && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->usersupp.usernum))) )  {
1100                 ret = crr_access_denied;
1101         }
1102
1103         else if (is_noneditable(&qrbuf)) {
1104                 ret = crr_noneditable;
1105         }
1106
1107         else {
1108                 /* Rename it */
1109                 if (qrbuf.QRflags & QR_MAILBOX) {
1110                         owner = atol(qrbuf.QRname);
1111                 }
1112                 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1113                         snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1114                                         "%010ld.%s", owner, new_name);
1115                 }
1116                 else {
1117                         safestrncpy(qrbuf.QRname, new_name,
1118                                                 sizeof(qrbuf.QRname));
1119                 }
1120
1121                 /* Reject change of floor for baseroom/aideroom */
1122                 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1123                     !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1124                         new_floor = 0;
1125                 }
1126
1127                 /* Take care of floor stuff */
1128                 old_floor = qrbuf.QRfloor;
1129                 if (new_floor < 0) {
1130                         new_floor = old_floor;
1131                 }
1132                 qrbuf.QRfloor = new_floor;
1133                 putroom(&qrbuf);
1134
1135                 begin_critical_section(S_CONFIG);
1136         
1137                 /* If baseroom/aideroom name changes, update config */
1138                 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1139                         safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1140                         put_config();
1141                 }
1142                 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1143                         safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1144                         put_config();
1145                 }
1146         
1147                 end_critical_section(S_CONFIG);
1148         
1149                 /* If the room name changed, then there are now two room
1150                  * records, so we have to delete the old one.
1151                  */
1152                 if (strcasecmp(new_name, old_name)) {
1153                         b_deleteroom(old_name);
1154                 }
1155
1156                 ret = crr_ok;
1157         }
1158
1159         end_critical_section(S_QUICKROOM);
1160
1161         /* Adjust the floor reference counts if necessary */
1162         if (new_floor != old_floor) {
1163                 lgetfloor(&flbuf, old_floor);
1164                 --flbuf.f_ref_count;
1165                 lputfloor(&flbuf, old_floor);
1166                 lgetfloor(&flbuf, CC->quickroom.QRfloor);
1167                 ++flbuf.f_ref_count;
1168                 lputfloor(&flbuf, CC->quickroom.QRfloor);
1169         }
1170
1171         /* ...and everybody say "YATTA!" */     
1172         return(ret);
1173 }
1174
1175
1176 /*
1177  * set room parameters (aide or room aide command)
1178  */
1179 void cmd_setr(char *args)
1180 {
1181         char buf[SIZ];
1182         int new_order = 0;
1183         int r;
1184         int new_floor;
1185         char new_name[ROOMNAMELEN];
1186
1187         if (CtdlAccessCheck(ac_logged_in)) return;
1188
1189         if (num_parms(args) >= 6) {
1190                 new_floor = extract_int(args, 5);
1191         } else {
1192                 new_floor = (-1);       /* don't change the floor */
1193         }
1194
1195         /* When is a new name more than just a new name?  When the old name
1196          * has a namespace prefix.
1197          */
1198         if (CC->quickroom.QRflags & QR_MAILBOX) {
1199                 sprintf(new_name, "%010ld.", atol(CC->quickroom.QRname) );
1200         } else {
1201                 strcpy(new_name, "");
1202         }
1203         extract(&new_name[strlen(new_name)], args, 0);
1204
1205         r = CtdlRenameRoom(CC->quickroom.QRname, new_name, new_floor);
1206
1207         if (r == crr_room_not_found) {
1208                 cprintf("%d Internal error - room not found?\n", ERROR);
1209         } else if (r == crr_already_exists) {
1210                 cprintf("%d '%s' already exists.\n",
1211                         ERROR + ALREADY_EXISTS, new_name);
1212         } else if (r == crr_noneditable) {
1213                 cprintf("%d Cannot edit this room.\n", ERROR);
1214         } else if (r == crr_invalid_floor) {
1215                 cprintf("%d Target floor does not exist.\n",
1216                         ERROR + INVALID_FLOOR_OPERATION);
1217         } else if (r == crr_access_denied) {
1218                 cprintf("%d You do not have permission to edit '%s'\n",
1219                         ERROR + HIGHER_ACCESS_REQUIRED,
1220                         CC->quickroom.QRname);
1221         } else if (r != crr_ok) {
1222                 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1223                         ERROR, r);
1224         }
1225
1226         if (r != crr_ok) {
1227                 return;
1228         }
1229
1230         getroom(&CC->quickroom, new_name);
1231
1232         /* Now we have to do a bunch of other stuff */
1233
1234         if (num_parms(args) >= 7) {
1235                 new_order = extract_int(args, 6);
1236                 if (new_order < 1)
1237                         new_order = 1;
1238                 if (new_order > 127)
1239                         new_order = 127;
1240         }
1241
1242         lgetroom(&CC->quickroom, CC->quickroom.QRname);
1243
1244         /* Directory room */
1245         extract(buf, args, 2);
1246         buf[15] = 0;
1247         safestrncpy(CC->quickroom.QRdirname, buf,
1248                 sizeof CC->quickroom.QRdirname);
1249
1250         /* Default view */
1251         if (num_parms(args) >= 8) {
1252                 CC->quickroom.QRdefaultview = extract_int(args, 7);
1253         }
1254
1255         /* Second set of flags */
1256         if (num_parms(args) >= 9) {
1257                 CC->quickroom.QRflags2 = extract_int(args, 8);
1258         }
1259
1260         /* Misc. flags */
1261         CC->quickroom.QRflags = (extract_int(args, 3) | QR_INUSE);
1262         /* Clean up a client boo-boo: if the client set the room to
1263          * guess-name or passworded, ensure that the private flag is
1264          * also set.
1265          */
1266         if ((CC->quickroom.QRflags & QR_GUESSNAME)
1267             || (CC->quickroom.QRflags & QR_PASSWORDED))
1268                 CC->quickroom.QRflags |= QR_PRIVATE;
1269
1270         /* Some changes can't apply to BASEROOM */
1271         if (!strncasecmp(CC->quickroom.QRname, config.c_baseroom,
1272                          ROOMNAMELEN)) {
1273                 CC->quickroom.QRorder = 0;
1274                 CC->quickroom.QRpasswd[0] = '\0';
1275                 CC->quickroom.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1276                         QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1277                 CC->quickroom.QRflags |= QR_PERMANENT;
1278         } else {        
1279                 /* March order (doesn't apply to AIDEROOM) */
1280                 if (num_parms(args) >= 7)
1281                         CC->quickroom.QRorder = (char) new_order;
1282                 /* Room password */
1283                 extract(buf, args, 1);
1284                 buf[10] = 0;
1285                 safestrncpy(CC->quickroom.QRpasswd, buf,
1286                             sizeof CC->quickroom.QRpasswd);
1287                 /* Kick everyone out if the client requested it
1288                  * (by changing the room's generation number)
1289                  */
1290                 if (extract_int(args, 4)) {
1291                         time(&CC->quickroom.QRgen);
1292                 }
1293         }
1294         /* Some changes can't apply to AIDEROOM */
1295         if (!strncasecmp(CC->quickroom.QRname, config.c_baseroom,
1296                          ROOMNAMELEN)) {
1297                 CC->quickroom.QRorder = 0;
1298                 CC->quickroom.QRflags &= ~QR_MAILBOX;
1299                 CC->quickroom.QRflags |= QR_PERMANENT;
1300         }
1301
1302         /* Write the room record back to disk */
1303         lputroom(&CC->quickroom);
1304
1305         /* Create a room directory if necessary */
1306         if (CC->quickroom.QRflags & QR_DIRECTORY) {
1307                 snprintf(buf, sizeof buf,
1308                     "mkdir ./files/%s </dev/null >/dev/null 2>/dev/null",
1309                         CC->quickroom.QRdirname);
1310                 system(buf);
1311         }
1312         snprintf(buf, sizeof buf, "%s> edited by %s\n", CC->quickroom.QRname, CC->curr_user);
1313         aide_message(buf);
1314         cprintf("%d Ok\n", CIT_OK);
1315 }
1316
1317
1318
1319 /* 
1320  * get the name of the room aide for this room
1321  */
1322 void cmd_geta(void)
1323 {
1324         struct usersupp usbuf;
1325
1326         if (CtdlAccessCheck(ac_logged_in)) return;
1327
1328         if (getuserbynumber(&usbuf, CC->quickroom.QRroomaide) == 0) {
1329                 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1330         } else {
1331                 cprintf("%d \n", CIT_OK);
1332         }
1333 }
1334
1335
1336 /* 
1337  * set the room aide for this room
1338  */
1339 void cmd_seta(char *new_ra)
1340 {
1341         struct usersupp usbuf;
1342         long newu;
1343         char buf[SIZ];
1344         int post_notice;
1345
1346         if (CtdlAccessCheck(ac_room_aide)) return;
1347
1348         if (getuser(&usbuf, new_ra) != 0) {
1349                 newu = (-1L);
1350         } else {
1351                 newu = usbuf.usernum;
1352         }
1353
1354         lgetroom(&CC->quickroom, CC->quickroom.QRname);
1355         post_notice = 0;
1356         if (CC->quickroom.QRroomaide != newu) {
1357                 post_notice = 1;
1358         }
1359         CC->quickroom.QRroomaide = newu;
1360         lputroom(&CC->quickroom);
1361
1362         /*
1363          * We have to post the change notice _after_ writing changes to 
1364          * the room table, otherwise it would deadlock!
1365          */
1366         if (post_notice == 1) {
1367                 if (strlen(usbuf.fullname) > 0)
1368                         snprintf(buf, sizeof buf,
1369                                 "%s is now room aide for %s>\n",
1370                                 usbuf.fullname, CC->quickroom.QRname);
1371                 else
1372                         snprintf(buf, sizeof buf,
1373                                 "There is now no room aide for %s>\n",
1374                                 CC->quickroom.QRname);
1375                 aide_message(buf);
1376         }
1377         cprintf("%d Ok\n", CIT_OK);
1378 }
1379
1380 /*
1381  * Generate an associated file name for a room
1382  */
1383 void assoc_file_name(char *buf, size_t n,
1384                      struct quickroom *qrbuf, const char *prefix)
1385 {
1386         snprintf(buf, n, "./%s/%ld", prefix, qrbuf->QRnumber);
1387 }
1388
1389 /* 
1390  * retrieve info file for this room
1391  */
1392 void cmd_rinf(void)
1393 {
1394         char filename[128];
1395         char buf[SIZ];
1396         FILE *info_fp;
1397
1398         assoc_file_name(filename, sizeof filename, &CC->quickroom, "info");
1399         info_fp = fopen(filename, "r");
1400
1401         if (info_fp == NULL) {
1402                 cprintf("%d No info file.\n", ERROR);
1403                 return;
1404         }
1405         cprintf("%d Info:\n", LISTING_FOLLOWS);
1406         while (fgets(buf, sizeof buf, info_fp) != NULL) {
1407                 if (strlen(buf) > 0)
1408                         buf[strlen(buf) - 1] = 0;
1409                 cprintf("%s\n", buf);
1410         }
1411         cprintf("000\n");
1412         fclose(info_fp);
1413 }
1414
1415 /*
1416  * Back end processing to delete a room and everything associated with it
1417  */
1418 void delete_room(struct quickroom *qrbuf)
1419 {
1420         struct floor flbuf;
1421         char filename[100];
1422
1423         lprintf(9, "Deleting room <%s>\n", qrbuf->QRname);
1424
1425         /* Delete the info file */
1426         assoc_file_name(filename, sizeof filename, qrbuf, "info");
1427         unlink(filename);
1428
1429         /* Delete the image file */
1430         assoc_file_name(filename, sizeof filename, qrbuf, "images");
1431         unlink(filename);
1432
1433         /* Delete the room's network config file */
1434         assoc_file_name(filename, sizeof filename, qrbuf, "netconfigs");
1435         unlink(filename);
1436
1437         /* Delete the messages in the room
1438          * (Careful: this opens an S_QUICKROOM critical section!)
1439          */
1440         CtdlDeleteMessages(qrbuf->QRname, 0L, "");
1441
1442         /* Flag the room record as not in use */
1443         lgetroom(qrbuf, qrbuf->QRname);
1444         qrbuf->QRflags = 0;
1445         lputroom(qrbuf);
1446
1447         /* then decrement the reference count for the floor */
1448         lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1449         flbuf.f_ref_count = flbuf.f_ref_count - 1;
1450         lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1451
1452         /* Delete the room record from the database! */
1453         b_deleteroom(qrbuf->QRname);
1454 }
1455
1456
1457
1458 /*
1459  * Check access control for deleting a room
1460  */
1461 int CtdlDoIHavePermissionToDeleteThisRoom(struct quickroom *qr) {
1462
1463         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1464                 return(0);
1465         }
1466
1467         if (is_noneditable(qr)) {
1468                 return(0);
1469         }
1470
1471         /*
1472          * For mailboxes, check stuff
1473          */
1474         if (qr->QRflags & QR_MAILBOX) {
1475
1476                 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1477
1478                 if (atol(qr->QRname) != CC->usersupp.usernum) {
1479                         return(0);      /* not my room */
1480                 }
1481
1482                 /* Can't delete your Mail> room */
1483                 if (!strcasecmp(&qr->QRname[12], MAILROOM)) return(0);
1484
1485                 /* Otherwise it's ok */
1486                 return(1);
1487         }
1488
1489         /*
1490          * For normal rooms, just check for aide or room aide status.
1491          */
1492         return(is_room_aide());
1493 }
1494
1495 /*
1496  * aide command: kill the current room
1497  */
1498 void cmd_kill(char *argbuf)
1499 {
1500         char aaa[100];
1501         char deleted_room_name[ROOMNAMELEN];
1502         int kill_ok;
1503
1504         kill_ok = extract_int(argbuf, 0);
1505
1506         if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->quickroom) == 0) {
1507                 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1508                 return;
1509         }
1510         if (kill_ok) {
1511                 strcpy(deleted_room_name, CC->quickroom.QRname);
1512                 delete_room(&CC->quickroom);    /* Do the dirty work */
1513                 usergoto(config.c_baseroom, 0, 0, NULL, NULL); /* Return to the Lobby */
1514
1515                 /* tell the world what we did */
1516                 snprintf(aaa, sizeof aaa, "%s> killed by %s\n",
1517                          deleted_room_name, CC->curr_user);
1518                 aide_message(aaa);
1519                 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1520         } else {
1521                 cprintf("%d ok to delete.\n", CIT_OK);
1522         }
1523 }
1524
1525
1526 /*
1527  * Internal code to create a new room (returns room flags)
1528  *
1529  * Room types:  0=public, 1=guessname, 2=passworded, 3=inv-only,
1530  *              4=mailbox, 5=mailbox, but caller supplies namespace
1531  */
1532 unsigned create_room(char *new_room_name,
1533                      int new_room_type,
1534                      char *new_room_pass,
1535                      int new_room_floor,
1536                      int really_create,
1537                      int avoid_access)
1538 {
1539
1540         struct quickroom qrbuf;
1541         struct floor flbuf;
1542         struct visit vbuf;
1543
1544         lprintf(9, "create_room(%s)\n", new_room_name);
1545         if (getroom(&qrbuf, new_room_name) == 0) {
1546                 lprintf(9, "%s already exists.\n", new_room_name);
1547                 return (0);     /* already exists */
1548         }
1549
1550
1551         memset(&qrbuf, 0, sizeof(struct quickroom));
1552         safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1553         qrbuf.QRflags = QR_INUSE;
1554         if (new_room_type > 0)
1555                 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1556         if (new_room_type == 1)
1557                 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1558         if (new_room_type == 2)
1559                 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1560         if ( (new_room_type == 4) || (new_room_type == 5) )
1561                 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1562
1563         /* If the user is requesting a personal room, set up the room
1564          * name accordingly (prepend the user number)
1565          */
1566         if (new_room_type == 4) {
1567                 MailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->usersupp, new_room_name);
1568         }
1569         else {
1570                 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1571         }
1572
1573         /* If the room is private, and the system administrator has elected
1574          * to automatically grant room aide privileges, do so now; otherwise,
1575          * set the room aide to undefined.
1576          */
1577         if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1578                 qrbuf.QRroomaide = CC->usersupp.usernum;
1579         } else {
1580                 qrbuf.QRroomaide = (-1L);
1581         }
1582
1583         /* 
1584          * If the caller is only interested in testing whether this will work,
1585          * return now without creating the room.
1586          */
1587         if (!really_create) return (qrbuf.QRflags);
1588
1589         qrbuf.QRnumber = get_new_room_number();
1590         qrbuf.QRhighest = 0L;   /* No messages in this room yet */
1591         time(&qrbuf.QRgen);     /* Use a timestamp as the generation number */
1592         qrbuf.QRfloor = new_room_floor;
1593
1594         /* save what we just did... */
1595         putroom(&qrbuf);
1596
1597         /* bump the reference count on whatever floor the room is on */
1598         lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1599         flbuf.f_ref_count = flbuf.f_ref_count + 1;
1600         lputfloor(&flbuf, (int) qrbuf.QRfloor);
1601
1602         /* Grant the creator access to the room unless the avoid_access
1603          * parameter was specified.
1604          */
1605         if (avoid_access == 0) {
1606                 lgetuser(&CC->usersupp, CC->curr_user);
1607                 CtdlGetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1608                 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1609                 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1610                 CtdlSetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1611                 lputuser(&CC->usersupp);
1612         }
1613
1614         /* resume our happy day */
1615         return (qrbuf.QRflags);
1616 }
1617
1618
1619 /*
1620  * create a new room
1621  */
1622 void cmd_cre8(char *args)
1623 {
1624         int cre8_ok;
1625         char new_room_name[SIZ];
1626         int new_room_type;
1627         char new_room_pass[SIZ];
1628         int new_room_floor;
1629         char aaa[SIZ];
1630         unsigned newflags;
1631         struct floor *fl;
1632         int avoid_access = 0;
1633
1634         cre8_ok = extract_int(args, 0);
1635         extract(new_room_name, args, 1);
1636         new_room_name[ROOMNAMELEN - 1] = 0;
1637         new_room_type = extract_int(args, 2);
1638         extract(new_room_pass, args, 3);
1639         avoid_access = extract_int(args, 5);
1640         new_room_pass[9] = 0;
1641         new_room_floor = 0;
1642
1643         if ((strlen(new_room_name) == 0) && (cre8_ok == 1)) {
1644                 cprintf("%d Invalid room name.\n", ERROR);
1645                 return;
1646         }
1647
1648         if (!strcasecmp(new_room_name, MAILROOM)) {
1649                 cprintf("%d '%s' already exists.\n",
1650                         ERROR + ALREADY_EXISTS, new_room_name);
1651                 return;
1652         }
1653
1654         if (num_parms(args) >= 5) {
1655                 fl = cgetfloor(extract_int(args, 4));
1656                 if ((fl->f_flags & F_INUSE) == 0) {
1657                         cprintf("%d Invalid floor number.\n",
1658                                 ERROR + INVALID_FLOOR_OPERATION);
1659                         return;
1660                 } else {
1661                         new_room_floor = extract_int(args, 4);
1662                 }
1663         }
1664
1665         if (CtdlAccessCheck(ac_logged_in)) return;
1666
1667         if (CC->usersupp.axlevel < config.c_createax) {
1668                 cprintf("%d You need higher access to create rooms.\n",
1669                         ERROR + HIGHER_ACCESS_REQUIRED);
1670                 return;
1671         }
1672
1673         if ((strlen(new_room_name) == 0) && (cre8_ok == 0)) {
1674                 cprintf("%d Ok to create rooms.\n", CIT_OK);
1675                 return;
1676         }
1677
1678         if ((new_room_type < 0) || (new_room_type > 5)) {
1679                 cprintf("%d Invalid room type.\n", ERROR);
1680                 return;
1681         }
1682
1683         if (new_room_type == 5) {
1684                 if ((config.c_aide_mailboxes == 0)
1685                    || (CC->usersupp.axlevel < 6)) {
1686                         cprintf("%d Higher access required\n", 
1687                                 ERROR+HIGHER_ACCESS_REQUIRED);
1688                         return;
1689                 }
1690         }
1691
1692         /* Check to make sure the requested room name doesn't already exist */
1693         newflags = create_room(new_room_name,
1694                                 new_room_type, new_room_pass, new_room_floor,
1695                                 0, avoid_access);
1696         if (newflags == 0) {
1697                 cprintf("%d '%s' already exists.\n",
1698                         ERROR + ALREADY_EXISTS, new_room_name);
1699                 return;
1700         }
1701
1702         if (cre8_ok == 0) {
1703                 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
1704                 return;
1705         }
1706
1707         /* If we reach this point, the room needs to be created. */
1708
1709         newflags = create_room(new_room_name,
1710                            new_room_type, new_room_pass, new_room_floor, 1, 0);
1711
1712         /* post a message in Aide> describing the new room */
1713         safestrncpy(aaa, new_room_name, sizeof aaa);
1714         strcat(aaa, "> created by ");
1715         strcat(aaa, CC->usersupp.fullname);
1716         if (newflags & QR_MAILBOX)
1717                 strcat(aaa, " [personal]");
1718         else if (newflags & QR_PRIVATE)
1719                 strcat(aaa, " [private]");
1720         if (newflags & QR_GUESSNAME)
1721                 strcat(aaa, "[guessname] ");
1722         if (newflags & QR_PASSWORDED) {
1723                 strcat(aaa, "\n Password: ");
1724                 strcat(aaa, new_room_pass);
1725         }
1726         strcat(aaa, "\n");
1727         aide_message(aaa);
1728
1729         cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
1730 }
1731
1732
1733
1734 void cmd_einf(char *ok)
1735 {                               /* enter info file for current room */
1736         FILE *fp;
1737         char infofilename[SIZ];
1738         char buf[SIZ];
1739
1740         if (CtdlAccessCheck(ac_room_aide)) return;
1741
1742         if (atoi(ok) == 0) {
1743                 cprintf("%d Ok.\n", CIT_OK);
1744                 return;
1745         }
1746         assoc_file_name(infofilename, sizeof infofilename, &CC->quickroom, "info");
1747         lprintf(9, "opening\n");
1748         fp = fopen(infofilename, "w");
1749         lprintf(9, "checking\n");
1750         if (fp == NULL) {
1751                 cprintf("%d Cannot open %s: %s\n",
1752                   ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1753                 return;
1754         }
1755         cprintf("%d Send info...\n", SEND_LISTING);
1756
1757         do {
1758                 client_gets(buf);
1759                 if (strcmp(buf, "000"))
1760                         fprintf(fp, "%s\n", buf);
1761         } while (strcmp(buf, "000"));
1762         fclose(fp);
1763
1764         /* now update the room index so people will see our new info */
1765         lgetroom(&CC->quickroom, CC->quickroom.QRname);         /* lock so no one steps on us */
1766         CC->quickroom.QRinfo = CC->quickroom.QRhighest + 1L;
1767         lputroom(&CC->quickroom);
1768 }
1769
1770
1771 /* 
1772  * cmd_lflr()   -  List all known floors
1773  */
1774 void cmd_lflr(void)
1775 {
1776         int a;
1777         struct floor flbuf;
1778
1779         if (CtdlAccessCheck(ac_logged_in)) return;
1780
1781         cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1782
1783         for (a = 0; a < MAXFLOORS; ++a) {
1784                 getfloor(&flbuf, a);
1785                 if (flbuf.f_flags & F_INUSE) {
1786                         cprintf("%d|%s|%d\n",
1787                                 a,
1788                                 flbuf.f_name,
1789                                 flbuf.f_ref_count);
1790                 }
1791         }
1792         cprintf("000\n");
1793 }
1794
1795
1796
1797 /*
1798  * create a new floor
1799  */
1800 void cmd_cflr(char *argbuf)
1801 {
1802         char new_floor_name[SIZ];
1803         struct floor flbuf;
1804         int cflr_ok;
1805         int free_slot = (-1);
1806         int a;
1807
1808         extract(new_floor_name, argbuf, 0);
1809         cflr_ok = extract_int(argbuf, 1);
1810
1811
1812         if (CtdlAccessCheck(ac_aide)) return;
1813
1814         for (a = 0; a < MAXFLOORS; ++a) {
1815                 getfloor(&flbuf, a);
1816
1817                 /* note any free slots while we're scanning... */
1818                 if (((flbuf.f_flags & F_INUSE) == 0)
1819                     && (free_slot < 0))
1820                         free_slot = a;
1821
1822                 /* check to see if it already exists */
1823                 if ((!strcasecmp(flbuf.f_name, new_floor_name))
1824                     && (flbuf.f_flags & F_INUSE)) {
1825                         cprintf("%d Floor '%s' already exists.\n",
1826                                 ERROR + ALREADY_EXISTS,
1827                                 flbuf.f_name);
1828                         return;
1829                 }
1830         }
1831
1832         if (free_slot < 0) {
1833                 cprintf("%d There is no space available for a new floor.\n",
1834                         ERROR + INVALID_FLOOR_OPERATION);
1835                 return;
1836         }
1837         if (cflr_ok == 0) {
1838                 cprintf("%d ok to create...\n", CIT_OK);
1839                 return;
1840         }
1841         lgetfloor(&flbuf, free_slot);
1842         flbuf.f_flags = F_INUSE;
1843         flbuf.f_ref_count = 0;
1844         safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
1845         lputfloor(&flbuf, free_slot);
1846         cprintf("%d %d\n", CIT_OK, free_slot);
1847 }
1848
1849
1850
1851 /*
1852  * delete a floor
1853  */
1854 void cmd_kflr(char *argbuf)
1855 {
1856         struct floor flbuf;
1857         int floor_to_delete;
1858         int kflr_ok;
1859         int delete_ok;
1860
1861         floor_to_delete = extract_int(argbuf, 0);
1862         kflr_ok = extract_int(argbuf, 1);
1863
1864         if (CtdlAccessCheck(ac_aide)) return;
1865
1866         lgetfloor(&flbuf, floor_to_delete);
1867
1868         delete_ok = 1;
1869         if ((flbuf.f_flags & F_INUSE) == 0) {
1870                 cprintf("%d Floor %d not in use.\n",
1871                         ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
1872                 delete_ok = 0;
1873         } else {
1874                 if (flbuf.f_ref_count != 0) {
1875                         cprintf("%d Cannot delete; floor contains %d rooms.\n",
1876                                 ERROR + INVALID_FLOOR_OPERATION,
1877                                 flbuf.f_ref_count);
1878                         delete_ok = 0;
1879                 } else {
1880                         if (kflr_ok == 1) {
1881                                 cprintf("%d Ok\n", CIT_OK);
1882                         } else {
1883                                 cprintf("%d Ok to delete...\n", CIT_OK);
1884                         }
1885
1886                 }
1887
1888         }
1889
1890         if ((delete_ok == 1) && (kflr_ok == 1))
1891                 flbuf.f_flags = 0;
1892         lputfloor(&flbuf, floor_to_delete);
1893 }
1894
1895 /*
1896  * edit a floor
1897  */
1898 void cmd_eflr(char *argbuf)
1899 {
1900         struct floor flbuf;
1901         int floor_num;
1902         int np;
1903
1904         np = num_parms(argbuf);
1905         if (np < 1) {
1906                 cprintf("%d Usage error.\n", ERROR);
1907                 return;
1908         }
1909
1910         if (CtdlAccessCheck(ac_aide)) return;
1911
1912         floor_num = extract_int(argbuf, 0);
1913         lgetfloor(&flbuf, floor_num);
1914         if ((flbuf.f_flags & F_INUSE) == 0) {
1915                 lputfloor(&flbuf, floor_num);
1916                 cprintf("%d Floor %d is not in use.\n",
1917                         ERROR + INVALID_FLOOR_OPERATION, floor_num);
1918                 return;
1919         }
1920         if (np >= 2)
1921                 extract(flbuf.f_name, argbuf, 1);
1922         lputfloor(&flbuf, floor_num);
1923
1924         cprintf("%d Ok\n", CIT_OK);
1925 }