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