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