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