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