]> code.citadel.org Git - citadel.git/blob - citadel/room_ops.c
* serv_notes.c: finished. When saving a message in a room whose view
[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         CC->curr_view = (int)vbuf.v_view;
851
852         if (display_result) {
853                 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|\n",
854                         CIT_OK, CtdlCheckExpress(),
855                         truncated_roomname,
856                         (int)new_messages,
857                         (int)total_messages,
858                         (int)info,
859                         (int)CC->room.QRflags,
860                         (long)CC->room.QRhighest,
861                         (long)vbuf.v_lastseen,
862                         (int)rmailflag,
863                         (int)raideflag,
864                         (int)newmailcount,
865                         (int)CC->room.QRfloor,
866                         (int)vbuf.v_view,
867                         (int)CC->room.QRdefaultview
868                 );
869         }
870 }
871
872
873 /* 
874  * cmd_goto()  -  goto a new room
875  */
876 void cmd_goto(char *gargs)
877 {
878         struct ctdlroom QRscratch;
879         int c;
880         int ok = 0;
881         int ra;
882         char augmented_roomname[SIZ];
883         char towhere[SIZ];
884         char password[SIZ];
885         int transiently = 0;
886
887         if (CtdlAccessCheck(ac_logged_in)) return;
888
889         extract(towhere, gargs, 0);
890         extract(password, gargs, 1);
891         transiently = extract_int(gargs, 2);
892
893         getuser(&CC->user, CC->curr_user);
894
895         if (!strcasecmp(towhere, "_BASEROOM_"))
896                 strcpy(towhere, config.c_baseroom);
897
898         if (!strcasecmp(towhere, "_MAIL_"))
899                 strcpy(towhere, MAILROOM);
900
901         if (!strcasecmp(towhere, "_BITBUCKET_"))
902                 strcpy(towhere, config.c_twitroom);
903
904
905         /* First try a regular match */
906         c = getroom(&QRscratch, towhere);
907
908         /* Then try a mailbox name match */
909         if (c != 0) {
910                 MailboxName(augmented_roomname, sizeof augmented_roomname,
911                             &CC->user, towhere);
912                 c = getroom(&QRscratch, augmented_roomname);
913                 if (c == 0)
914                         strcpy(towhere, augmented_roomname);
915         }
916
917         /* And if the room was found... */
918         if (c == 0) {
919
920                 /* Let internal programs go directly to any room. */
921                 if (CC->internal_pgm) {
922                         memcpy(&CC->room, &QRscratch,
923                                 sizeof(struct ctdlroom));
924                         usergoto(NULL, 1, transiently, NULL, NULL);
925                         return;
926                 }
927
928                 /* See if there is an existing user/room relationship */
929                 ra = CtdlRoomAccess(&QRscratch, &CC->user);
930
931                 /* normal clients have to pass through security */
932                 if (ra & UA_GOTOALLOWED) {
933                         ok = 1;
934                 }
935
936                 if (ok == 1) {
937                         if ((QRscratch.QRflags & QR_MAILBOX) &&
938                             ((ra & UA_GOTOALLOWED))) {
939                                 memcpy(&CC->room, &QRscratch,
940                                         sizeof(struct ctdlroom));
941                                 usergoto(NULL, 1, transiently, NULL, NULL);
942                                 return;
943                         } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
944                             ((ra & UA_KNOWN) == 0) &&
945                             (strcasecmp(QRscratch.QRpasswd, password)) &&
946                             (CC->user.axlevel < 6)
947                             ) {
948                                 cprintf("%d wrong or missing passwd\n",
949                                         ERROR + PASSWORD_REQUIRED);
950                                 return;
951                         } else if ((QRscratch.QRflags & QR_PRIVATE) &&
952                                    ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
953                                    ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
954                                    ((ra & UA_KNOWN) == 0) &&
955                                    (CC->user.axlevel < 6)
956                                   ) {
957                                 lprintf(CTDL_DEBUG, "Failed to acquire private room\n");
958                         } else {
959                                 memcpy(&CC->room, &QRscratch,
960                                         sizeof(struct ctdlroom));
961                                 usergoto(NULL, 1, transiently, NULL, NULL);
962                                 return;
963                         }
964                 }
965         }
966
967         cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
968 }
969
970
971 void cmd_whok(void)
972 {
973         struct ctdluser temp;
974         struct cdbdata *cdbus;
975
976         getuser(&CC->user, CC->curr_user);
977
978         /*
979          * This command is only allowed by aides, room aides,
980          * and room namespace owners
981          */
982         if (is_room_aide()
983            || (atol(CC->room.QRname) == CC->user.usernum) ) {
984                 /* access granted */
985         }
986         else {
987                 /* access denied */
988                 cprintf("%d Higher access or room ownership required.\n",
989                         ERROR + HIGHER_ACCESS_REQUIRED);
990                 return;
991         }
992
993         cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
994         cdb_rewind(CDB_USERS);
995         while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
996                 memset(&temp, 0, sizeof temp);
997                 memcpy(&temp, cdbus->ptr, sizeof temp);
998                 cdb_free(cdbus);
999
1000                 if ((CC->room.QRflags & QR_INUSE)
1001                     && (CtdlRoomAccess(&CC->room, &temp) & UA_KNOWN)
1002                     )
1003                         cprintf("%s\n", temp.fullname);
1004         }
1005         cprintf("000\n");
1006 }
1007
1008
1009 /*
1010  * RDIR command for room directory
1011  */
1012 void cmd_rdir(void)
1013 {
1014         char buf[SIZ];
1015         char flnm[SIZ];
1016         char comment[SIZ];
1017         FILE *ls, *fd;
1018         struct stat statbuf;
1019
1020         if (CtdlAccessCheck(ac_logged_in)) return;
1021         
1022         getroom(&CC->room, CC->room.QRname);
1023         getuser(&CC->user, CC->curr_user);
1024
1025         if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
1026                 cprintf("%d not here.\n", ERROR + NOT_HERE);
1027                 return;
1028         }
1029         if (((CC->room.QRflags & QR_VISDIR) == 0)
1030             && (CC->user.axlevel < 6)
1031             && (CC->user.usernum != CC->room.QRroomaide)) {
1032                 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1033                 return;
1034         }
1035         cprintf("%d %s|%s/files/%s\n",
1036         LISTING_FOLLOWS, config.c_fqdn, BBSDIR, CC->room.QRdirname);
1037
1038         snprintf(buf, sizeof buf, "ls %s/files/%s  >%s 2> /dev/null",
1039                 BBSDIR, CC->room.QRdirname, CC->temp);
1040         system(buf);
1041
1042         snprintf(buf, sizeof buf, "%s/files/%s/filedir", BBSDIR, CC->room.QRdirname);
1043         fd = fopen(buf, "r");
1044         if (fd == NULL)
1045                 fd = fopen("/dev/null", "r");
1046
1047         ls = fopen(CC->temp, "r");
1048         while (fgets(flnm, sizeof flnm, ls) != NULL) {
1049                 flnm[strlen(flnm) - 1] = 0;
1050                 if (strcasecmp(flnm, "filedir")) {
1051                         snprintf(buf, sizeof buf, "%s/files/%s/%s",
1052                                 BBSDIR, CC->room.QRdirname, flnm);
1053                         stat(buf, &statbuf);
1054                         strcpy(comment, "");
1055                         fseek(fd, 0L, 0);
1056                         while ((fgets(buf, sizeof buf, fd) != NULL)
1057                                && (strlen(comment) == 0)) {
1058                                 buf[strlen(buf) - 1] = 0;
1059                                 if ((!strncasecmp(buf, flnm, strlen(flnm)))
1060                                     && (buf[strlen(flnm)] == ' '))
1061                                         safestrncpy(comment,
1062                                             &buf[strlen(flnm) + 1],
1063                                             sizeof comment);
1064                         }
1065                         cprintf("%s|%ld|%s\n", flnm, (long)statbuf.st_size, comment);
1066                 }
1067         }
1068         fclose(ls);
1069         fclose(fd);
1070         unlink(CC->temp);
1071
1072         cprintf("000\n");
1073 }
1074
1075 /*
1076  * get room parameters (aide or room aide command)
1077  */
1078 void cmd_getr(void)
1079 {
1080         if (CtdlAccessCheck(ac_room_aide)) return;
1081
1082         getroom(&CC->room, CC->room.QRname);
1083         cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1084                 CIT_OK,
1085                 CtdlCheckExpress(),
1086
1087                 ((CC->room.QRflags & QR_MAILBOX) ?
1088                         &CC->room.QRname[11] : CC->room.QRname),
1089
1090                 ((CC->room.QRflags & QR_PASSWORDED) ?
1091                         CC->room.QRpasswd : ""),
1092
1093                 ((CC->room.QRflags & QR_DIRECTORY) ?
1094                         CC->room.QRdirname : ""),
1095
1096                 CC->room.QRflags,
1097                 (int) CC->room.QRfloor,
1098                 (int) CC->room.QRorder,
1099
1100                 CC->room.QRdefaultview,
1101                 CC->room.QRflags2
1102                 );
1103 }
1104
1105
1106 /*
1107  * Back end function to rename a room.
1108  * You can also specify which floor to move the room to, or specify -1 to
1109  * keep the room on the same floor it was on.
1110  *
1111  * If you are renaming a mailbox room, you must supply the namespace prefix
1112  * in *at least* the old name!
1113  */
1114 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1115         int old_floor = 0;
1116         struct ctdlroom qrbuf;
1117         struct ctdlroom qrtmp;
1118         int ret = 0;
1119         struct floor *fl;
1120         struct floor flbuf;
1121         long owner = 0L;
1122         char actual_old_name[SIZ];
1123
1124         lprintf(CTDL_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1125                 old_name, new_name, new_floor);
1126
1127         if (new_floor >= 0) {
1128                 fl = cgetfloor(new_floor);
1129                 if ((fl->f_flags & F_INUSE) == 0) {
1130                         return(crr_invalid_floor);
1131                 }
1132         }
1133
1134         begin_critical_section(S_ROOMS);
1135
1136         if ( (getroom(&qrtmp, new_name) == 0) 
1137            && (strcasecmp(new_name, old_name)) ) {
1138                 ret = crr_already_exists;
1139         }
1140
1141         else if (getroom(&qrbuf, old_name) != 0) {
1142                 ret = crr_room_not_found;
1143         }
1144
1145         else if ( (CC->user.axlevel < 6)
1146                   && (CC->user.usernum != qrbuf.QRroomaide)
1147                   && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) )  {
1148                 ret = crr_access_denied;
1149         }
1150
1151         else if (is_noneditable(&qrbuf)) {
1152                 ret = crr_noneditable;
1153         }
1154
1155         else {
1156                 /* Rename it */
1157                 strcpy(actual_old_name, qrbuf.QRname);
1158                 if (qrbuf.QRflags & QR_MAILBOX) {
1159                         owner = atol(qrbuf.QRname);
1160                 }
1161                 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1162                         snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1163                                         "%010ld.%s", owner, new_name);
1164                 }
1165                 else {
1166                         safestrncpy(qrbuf.QRname, new_name,
1167                                                 sizeof(qrbuf.QRname));
1168                 }
1169
1170                 /* Reject change of floor for baseroom/aideroom */
1171                 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1172                     !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1173                         new_floor = 0;
1174                 }
1175
1176                 /* Take care of floor stuff */
1177                 old_floor = qrbuf.QRfloor;
1178                 if (new_floor < 0) {
1179                         new_floor = old_floor;
1180                 }
1181                 qrbuf.QRfloor = new_floor;
1182                 putroom(&qrbuf);
1183
1184                 begin_critical_section(S_CONFIG);
1185         
1186                 /* If baseroom/aideroom name changes, update config */
1187                 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1188                         safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1189                         put_config();
1190                 }
1191                 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1192                         safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1193                         put_config();
1194                 }
1195         
1196                 end_critical_section(S_CONFIG);
1197         
1198                 /* If the room name changed, then there are now two room
1199                  * records, so we have to delete the old one.
1200                  */
1201                 if (strcasecmp(new_name, old_name)) {
1202                         b_deleteroom(actual_old_name);
1203                 }
1204
1205                 ret = crr_ok;
1206         }
1207
1208         end_critical_section(S_ROOMS);
1209
1210         /* Adjust the floor reference counts if necessary */
1211         if (new_floor != old_floor) {
1212                 lgetfloor(&flbuf, old_floor);
1213                 --flbuf.f_ref_count;
1214                 lputfloor(&flbuf, old_floor);
1215                 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1216                 lgetfloor(&flbuf, new_floor);
1217                 ++flbuf.f_ref_count;
1218                 lputfloor(&flbuf, new_floor);
1219                 lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1220         }
1221
1222         /* ...and everybody say "YATTA!" */     
1223         return(ret);
1224 }
1225
1226
1227 /*
1228  * set room parameters (aide or room aide command)
1229  */
1230 void cmd_setr(char *args)
1231 {
1232         char buf[SIZ];
1233         int new_order = 0;
1234         int r;
1235         int new_floor;
1236         char new_name[ROOMNAMELEN];
1237
1238         if (CtdlAccessCheck(ac_logged_in)) return;
1239
1240         if (num_parms(args) >= 6) {
1241                 new_floor = extract_int(args, 5);
1242         } else {
1243                 new_floor = (-1);       /* don't change the floor */
1244         }
1245
1246         /* When is a new name more than just a new name?  When the old name
1247          * has a namespace prefix.
1248          */
1249         if (CC->room.QRflags & QR_MAILBOX) {
1250                 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
1251         } else {
1252                 strcpy(new_name, "");
1253         }
1254         extract(&new_name[strlen(new_name)], args, 0);
1255
1256         r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
1257
1258         if (r == crr_room_not_found) {
1259                 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
1260         } else if (r == crr_already_exists) {
1261                 cprintf("%d '%s' already exists.\n",
1262                         ERROR + ALREADY_EXISTS, new_name);
1263         } else if (r == crr_noneditable) {
1264                 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
1265         } else if (r == crr_invalid_floor) {
1266                 cprintf("%d Target floor does not exist.\n",
1267                         ERROR + INVALID_FLOOR_OPERATION);
1268         } else if (r == crr_access_denied) {
1269                 cprintf("%d You do not have permission to edit '%s'\n",
1270                         ERROR + HIGHER_ACCESS_REQUIRED,
1271                         CC->room.QRname);
1272         } else if (r != crr_ok) {
1273                 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1274                         ERROR + INTERNAL_ERROR, r);
1275         }
1276
1277         if (r != crr_ok) {
1278                 return;
1279         }
1280
1281         getroom(&CC->room, new_name);
1282
1283         /* Now we have to do a bunch of other stuff */
1284
1285         if (num_parms(args) >= 7) {
1286                 new_order = extract_int(args, 6);
1287                 if (new_order < 1)
1288                         new_order = 1;
1289                 if (new_order > 127)
1290                         new_order = 127;
1291         }
1292
1293         lgetroom(&CC->room, CC->room.QRname);
1294
1295         /* Directory room */
1296         extract(buf, args, 2);
1297         buf[15] = 0;
1298         safestrncpy(CC->room.QRdirname, buf,
1299                 sizeof CC->room.QRdirname);
1300
1301         /* Default view */
1302         if (num_parms(args) >= 8) {
1303                 CC->room.QRdefaultview = extract_int(args, 7);
1304         }
1305
1306         /* Second set of flags */
1307         if (num_parms(args) >= 9) {
1308                 CC->room.QRflags2 = extract_int(args, 8);
1309         }
1310
1311         /* Misc. flags */
1312         CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
1313         /* Clean up a client boo-boo: if the client set the room to
1314          * guess-name or passworded, ensure that the private flag is
1315          * also set.
1316          */
1317         if ((CC->room.QRflags & QR_GUESSNAME)
1318             || (CC->room.QRflags & QR_PASSWORDED))
1319                 CC->room.QRflags |= QR_PRIVATE;
1320
1321         /* Some changes can't apply to BASEROOM */
1322         if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1323                          ROOMNAMELEN)) {
1324                 CC->room.QRorder = 0;
1325                 CC->room.QRpasswd[0] = '\0';
1326                 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1327                         QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1328                 CC->room.QRflags |= QR_PERMANENT;
1329         } else {        
1330                 /* March order (doesn't apply to AIDEROOM) */
1331                 if (num_parms(args) >= 7)
1332                         CC->room.QRorder = (char) new_order;
1333                 /* Room password */
1334                 extract(buf, args, 1);
1335                 buf[10] = 0;
1336                 safestrncpy(CC->room.QRpasswd, buf,
1337                             sizeof CC->room.QRpasswd);
1338                 /* Kick everyone out if the client requested it
1339                  * (by changing the room's generation number)
1340                  */
1341                 if (extract_int(args, 4)) {
1342                         time(&CC->room.QRgen);
1343                 }
1344         }
1345         /* Some changes can't apply to AIDEROOM */
1346         if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1347                          ROOMNAMELEN)) {
1348                 CC->room.QRorder = 0;
1349                 CC->room.QRflags &= ~QR_MAILBOX;
1350                 CC->room.QRflags |= QR_PERMANENT;
1351         }
1352
1353         /* Write the room record back to disk */
1354         lputroom(&CC->room);
1355
1356         /* Create a room directory if necessary */
1357         if (CC->room.QRflags & QR_DIRECTORY) {
1358                 snprintf(buf, sizeof buf, "./files/%s",
1359                         CC->room.QRdirname);
1360                 mkdir(buf, 0755);
1361         }
1362         snprintf(buf, sizeof buf, "%s> edited by %s\n", CC->room.QRname, CC->curr_user);
1363         aide_message(buf);
1364         cprintf("%d Ok\n", CIT_OK);
1365 }
1366
1367
1368
1369 /* 
1370  * get the name of the room aide for this room
1371  */
1372 void cmd_geta(void)
1373 {
1374         struct ctdluser usbuf;
1375
1376         if (CtdlAccessCheck(ac_logged_in)) return;
1377
1378         if (getuserbynumber(&usbuf, CC->room.QRroomaide) == 0) {
1379                 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1380         } else {
1381                 cprintf("%d \n", CIT_OK);
1382         }
1383 }
1384
1385
1386 /* 
1387  * set the room aide for this room
1388  */
1389 void cmd_seta(char *new_ra)
1390 {
1391         struct ctdluser usbuf;
1392         long newu;
1393         char buf[SIZ];
1394         int post_notice;
1395
1396         if (CtdlAccessCheck(ac_room_aide)) return;
1397
1398         if (getuser(&usbuf, new_ra) != 0) {
1399                 newu = (-1L);
1400         } else {
1401                 newu = usbuf.usernum;
1402         }
1403
1404         lgetroom(&CC->room, CC->room.QRname);
1405         post_notice = 0;
1406         if (CC->room.QRroomaide != newu) {
1407                 post_notice = 1;
1408         }
1409         CC->room.QRroomaide = newu;
1410         lputroom(&CC->room);
1411
1412         /*
1413          * We have to post the change notice _after_ writing changes to 
1414          * the room table, otherwise it would deadlock!
1415          */
1416         if (post_notice == 1) {
1417                 if (strlen(usbuf.fullname) > 0)
1418                         snprintf(buf, sizeof buf,
1419                                 "%s is now room aide for %s>\n",
1420                                 usbuf.fullname, CC->room.QRname);
1421                 else
1422                         snprintf(buf, sizeof buf,
1423                                 "There is now no room aide for %s>\n",
1424                                 CC->room.QRname);
1425                 aide_message(buf);
1426         }
1427         cprintf("%d Ok\n", CIT_OK);
1428 }
1429
1430 /*
1431  * Generate an associated file name for a room
1432  */
1433 void assoc_file_name(char *buf, size_t n,
1434                      struct ctdlroom *qrbuf, const char *prefix)
1435 {
1436         snprintf(buf, n, "./%s/%ld", prefix, qrbuf->QRnumber);
1437 }
1438
1439 /* 
1440  * retrieve info file for this room
1441  */
1442 void cmd_rinf(void)
1443 {
1444         char filename[128];
1445         char buf[SIZ];
1446         FILE *info_fp;
1447
1448         assoc_file_name(filename, sizeof filename, &CC->room, "info");
1449         info_fp = fopen(filename, "r");
1450
1451         if (info_fp == NULL) {
1452                 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
1453                 return;
1454         }
1455         cprintf("%d Info:\n", LISTING_FOLLOWS);
1456         while (fgets(buf, sizeof buf, info_fp) != NULL) {
1457                 if (strlen(buf) > 0)
1458                         buf[strlen(buf) - 1] = 0;
1459                 cprintf("%s\n", buf);
1460         }
1461         cprintf("000\n");
1462         fclose(info_fp);
1463 }
1464
1465 /*
1466  * Back end processing to delete a room and everything associated with it
1467  */
1468 void delete_room(struct ctdlroom *qrbuf)
1469 {
1470         struct floor flbuf;
1471         char filename[100];
1472
1473         lprintf(CTDL_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1474
1475         /* Delete the info file */
1476         assoc_file_name(filename, sizeof filename, qrbuf, "info");
1477         unlink(filename);
1478
1479         /* Delete the image file */
1480         assoc_file_name(filename, sizeof filename, qrbuf, "images");
1481         unlink(filename);
1482
1483         /* Delete the room's network config file */
1484         assoc_file_name(filename, sizeof filename, qrbuf, "netconfigs");
1485         unlink(filename);
1486
1487         /* Delete the messages in the room
1488          * (Careful: this opens an S_ROOMS critical section!)
1489          */
1490         CtdlDeleteMessages(qrbuf->QRname, 0L, "");
1491
1492         /* Flag the room record as not in use */
1493         lgetroom(qrbuf, qrbuf->QRname);
1494         qrbuf->QRflags = 0;
1495         lputroom(qrbuf);
1496
1497         /* then decrement the reference count for the floor */
1498         lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1499         flbuf.f_ref_count = flbuf.f_ref_count - 1;
1500         lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1501
1502         /* Delete the room record from the database! */
1503         b_deleteroom(qrbuf->QRname);
1504 }
1505
1506
1507
1508 /*
1509  * Check access control for deleting a room
1510  */
1511 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1512
1513         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1514                 return(0);
1515         }
1516
1517         if (is_noneditable(qr)) {
1518                 return(0);
1519         }
1520
1521         /*
1522          * For mailboxes, check stuff
1523          */
1524         if (qr->QRflags & QR_MAILBOX) {
1525
1526                 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1527
1528                 if (atol(qr->QRname) != CC->user.usernum) {
1529                         return(0);      /* not my room */
1530                 }
1531
1532                 /* Can't delete your Mail> room */
1533                 if (!strcasecmp(&qr->QRname[12], MAILROOM)) return(0);
1534
1535                 /* Otherwise it's ok */
1536                 return(1);
1537         }
1538
1539         /*
1540          * For normal rooms, just check for aide or room aide status.
1541          */
1542         return(is_room_aide());
1543 }
1544
1545 /*
1546  * aide command: kill the current room
1547  */
1548 void cmd_kill(char *argbuf)
1549 {
1550         char deleted_room_name[ROOMNAMELEN];
1551         char msg[SIZ];
1552         int kill_ok;
1553
1554         kill_ok = extract_int(argbuf, 0);
1555
1556         if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
1557                 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1558                 return;
1559         }
1560         if (kill_ok) {
1561                 if (CC->room.QRflags & QR_MAILBOX) {
1562                         strcpy(deleted_room_name, &CC->room.QRname[11]);
1563                 }
1564                 else {
1565                         strcpy(deleted_room_name, CC->room.QRname);
1566                 }
1567
1568                 /* Do the dirty work */
1569                 delete_room(&CC->room);
1570
1571                 /* Return to the Lobby */
1572                 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1573
1574                 /* tell the world what we did */
1575                 snprintf(msg, sizeof msg, "%s> killed by %s\n",
1576                          deleted_room_name, CC->curr_user);
1577                 aide_message(msg);
1578                 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1579         } else {
1580                 cprintf("%d ok to delete.\n", CIT_OK);
1581         }
1582 }
1583
1584
1585 /*
1586  * Internal code to create a new room (returns room flags)
1587  *
1588  * Room types:  0=public, 1=guessname, 2=passworded, 3=inv-only,
1589  *              4=mailbox, 5=mailbox, but caller supplies namespace
1590  */
1591 unsigned create_room(char *new_room_name,
1592                      int new_room_type,
1593                      char *new_room_pass,
1594                      int new_room_floor,
1595                      int really_create,
1596                      int avoid_access,
1597                      int new_room_view)
1598 {
1599
1600         struct ctdlroom qrbuf;
1601         struct floor flbuf;
1602         struct visit vbuf;
1603
1604         lprintf(CTDL_DEBUG, "create_room(%s)\n", new_room_name);
1605         if (getroom(&qrbuf, new_room_name) == 0) {
1606                 lprintf(CTDL_DEBUG, "%s already exists.\n", new_room_name);
1607                 return (0);     /* already exists */
1608         }
1609
1610
1611         memset(&qrbuf, 0, sizeof(struct ctdlroom));
1612         safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1613         qrbuf.QRflags = QR_INUSE;
1614         if (new_room_type > 0)
1615                 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1616         if (new_room_type == 1)
1617                 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1618         if (new_room_type == 2)
1619                 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1620         if ( (new_room_type == 4) || (new_room_type == 5) )
1621                 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1622
1623         /* If the user is requesting a personal room, set up the room
1624          * name accordingly (prepend the user number)
1625          */
1626         if (new_room_type == 4) {
1627                 MailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1628         }
1629         else {
1630                 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1631         }
1632
1633         /* If the room is private, and the system administrator has elected
1634          * to automatically grant room aide privileges, do so now; otherwise,
1635          * set the room aide to undefined.
1636          */
1637         if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1638                 qrbuf.QRroomaide = CC->user.usernum;
1639         } else {
1640                 qrbuf.QRroomaide = (-1L);
1641         }
1642
1643         /* 
1644          * If the caller is only interested in testing whether this will work,
1645          * return now without creating the room.
1646          */
1647         if (!really_create) return (qrbuf.QRflags);
1648
1649         qrbuf.QRnumber = get_new_room_number();
1650         qrbuf.QRhighest = 0L;   /* No messages in this room yet */
1651         time(&qrbuf.QRgen);     /* Use a timestamp as the generation number */
1652         qrbuf.QRfloor = new_room_floor;
1653         qrbuf.QRdefaultview = new_room_view;
1654
1655         /* save what we just did... */
1656         putroom(&qrbuf);
1657
1658         /* bump the reference count on whatever floor the room is on */
1659         lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1660         flbuf.f_ref_count = flbuf.f_ref_count + 1;
1661         lputfloor(&flbuf, (int) qrbuf.QRfloor);
1662
1663         /* Grant the creator access to the room unless the avoid_access
1664          * parameter was specified.
1665          */
1666         if (avoid_access == 0) {
1667                 lgetuser(&CC->user, CC->curr_user);
1668                 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1669                 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1670                 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1671                 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1672                 lputuser(&CC->user);
1673         }
1674
1675         /* resume our happy day */
1676         return (qrbuf.QRflags);
1677 }
1678
1679
1680 /*
1681  * create a new room
1682  */
1683 void cmd_cre8(char *args)
1684 {
1685         int cre8_ok;
1686         char new_room_name[SIZ];
1687         int new_room_type;
1688         char new_room_pass[SIZ];
1689         int new_room_floor;
1690         int new_room_view;
1691         char aaa[SIZ];
1692         unsigned newflags;
1693         struct floor *fl;
1694         int avoid_access = 0;
1695
1696         cre8_ok = extract_int(args, 0);
1697         extract(new_room_name, args, 1);
1698         new_room_name[ROOMNAMELEN - 1] = 0;
1699         new_room_type = extract_int(args, 2);
1700         extract(new_room_pass, args, 3);
1701         avoid_access = extract_int(args, 5);
1702         new_room_view = extract_int(args, 6);
1703         new_room_pass[9] = 0;
1704         new_room_floor = 0;
1705
1706         if ((strlen(new_room_name) == 0) && (cre8_ok == 1)) {
1707                 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
1708                 return;
1709         }
1710
1711         if (!strcasecmp(new_room_name, MAILROOM)) {
1712                 cprintf("%d '%s' already exists.\n",
1713                         ERROR + ALREADY_EXISTS, new_room_name);
1714                 return;
1715         }
1716
1717         if (num_parms(args) >= 5) {
1718                 fl = cgetfloor(extract_int(args, 4));
1719                 if (fl == NULL) {
1720                         cprintf("%d Invalid floor number.\n",
1721                                 ERROR + INVALID_FLOOR_OPERATION);
1722                         return;
1723                 }
1724                 else if ((fl->f_flags & F_INUSE) == 0) {
1725                         cprintf("%d Invalid floor number.\n",
1726                                 ERROR + INVALID_FLOOR_OPERATION);
1727                         return;
1728                 } else {
1729                         new_room_floor = extract_int(args, 4);
1730                 }
1731         }
1732
1733         if (CtdlAccessCheck(ac_logged_in)) return;
1734
1735         if (CC->user.axlevel < config.c_createax) {
1736                 cprintf("%d You need higher access to create rooms.\n",
1737                         ERROR + HIGHER_ACCESS_REQUIRED);
1738                 return;
1739         }
1740
1741         if ((strlen(new_room_name) == 0) && (cre8_ok == 0)) {
1742                 cprintf("%d Ok to create rooms.\n", CIT_OK);
1743                 return;
1744         }
1745
1746         if ((new_room_type < 0) || (new_room_type > 5)) {
1747                 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
1748                 return;
1749         }
1750
1751         if (new_room_type == 5) {
1752                 if (CC->user.axlevel < 6) {
1753                         cprintf("%d Higher access required\n", 
1754                                 ERROR + HIGHER_ACCESS_REQUIRED);
1755                         return;
1756                 }
1757         }
1758
1759         /* Check to make sure the requested room name doesn't already exist */
1760         newflags = create_room(new_room_name,
1761                                 new_room_type, new_room_pass, new_room_floor,
1762                                 0, avoid_access, new_room_view);
1763         if (newflags == 0) {
1764                 cprintf("%d '%s' already exists.\n",
1765                         ERROR + ALREADY_EXISTS, new_room_name);
1766                 return;
1767         }
1768
1769         if (cre8_ok == 0) {
1770                 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
1771                 return;
1772         }
1773
1774         /* If we reach this point, the room needs to be created. */
1775
1776         newflags = create_room(new_room_name,
1777                            new_room_type, new_room_pass, new_room_floor, 1, 0,
1778                            new_room_view);
1779
1780         /* post a message in Aide> describing the new room */
1781         safestrncpy(aaa, new_room_name, sizeof aaa);
1782         strcat(aaa, "> created by ");
1783         strcat(aaa, CC->user.fullname);
1784         if (newflags & QR_MAILBOX)
1785                 strcat(aaa, " [personal]");
1786         else if (newflags & QR_PRIVATE)
1787                 strcat(aaa, " [private]");
1788         if (newflags & QR_GUESSNAME)
1789                 strcat(aaa, "[guessname] ");
1790         if (newflags & QR_PASSWORDED) {
1791                 strcat(aaa, "\n Password: ");
1792                 strcat(aaa, new_room_pass);
1793         }
1794         strcat(aaa, "\n");
1795         aide_message(aaa);
1796
1797         cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
1798 }
1799
1800
1801
1802 void cmd_einf(char *ok)
1803 {                               /* enter info file for current room */
1804         FILE *fp;
1805         char infofilename[SIZ];
1806         char buf[SIZ];
1807
1808         unbuffer_output();
1809
1810         if (CtdlAccessCheck(ac_room_aide)) return;
1811
1812         if (atoi(ok) == 0) {
1813                 cprintf("%d Ok.\n", CIT_OK);
1814                 return;
1815         }
1816         assoc_file_name(infofilename, sizeof infofilename, &CC->room, "info");
1817         lprintf(CTDL_DEBUG, "opening\n");
1818         fp = fopen(infofilename, "w");
1819         lprintf(CTDL_DEBUG, "checking\n");
1820         if (fp == NULL) {
1821                 cprintf("%d Cannot open %s: %s\n",
1822                   ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1823                 return;
1824         }
1825         cprintf("%d Send info...\n", SEND_LISTING);
1826
1827         do {
1828                 client_gets(buf);
1829                 if (strcmp(buf, "000"))
1830                         fprintf(fp, "%s\n", buf);
1831         } while (strcmp(buf, "000"));
1832         fclose(fp);
1833
1834         /* now update the room index so people will see our new info */
1835         lgetroom(&CC->room, CC->room.QRname);           /* lock so no one steps on us */
1836         CC->room.QRinfo = CC->room.QRhighest + 1L;
1837         lputroom(&CC->room);
1838 }
1839
1840
1841 /* 
1842  * cmd_lflr()   -  List all known floors
1843  */
1844 void cmd_lflr(void)
1845 {
1846         int a;
1847         struct floor flbuf;
1848
1849         if (CtdlAccessCheck(ac_logged_in)) return;
1850
1851         cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1852
1853         for (a = 0; a < MAXFLOORS; ++a) {
1854                 getfloor(&flbuf, a);
1855                 if (flbuf.f_flags & F_INUSE) {
1856                         cprintf("%d|%s|%d\n",
1857                                 a,
1858                                 flbuf.f_name,
1859                                 flbuf.f_ref_count);
1860                 }
1861         }
1862         cprintf("000\n");
1863 }
1864
1865
1866
1867 /*
1868  * create a new floor
1869  */
1870 void cmd_cflr(char *argbuf)
1871 {
1872         char new_floor_name[SIZ];
1873         struct floor flbuf;
1874         int cflr_ok;
1875         int free_slot = (-1);
1876         int a;
1877
1878         extract(new_floor_name, argbuf, 0);
1879         cflr_ok = extract_int(argbuf, 1);
1880
1881         if (CtdlAccessCheck(ac_aide)) return;
1882
1883         if (strlen(new_floor_name) == 0) {
1884                 cprintf("%d Blank floor name not allowed.\n",
1885                         ERROR + ILLEGAL_VALUE);
1886                 return;
1887         }
1888
1889         for (a = 0; a < MAXFLOORS; ++a) {
1890                 getfloor(&flbuf, a);
1891
1892                 /* note any free slots while we're scanning... */
1893                 if (((flbuf.f_flags & F_INUSE) == 0)
1894                     && (free_slot < 0))
1895                         free_slot = a;
1896
1897                 /* check to see if it already exists */
1898                 if ((!strcasecmp(flbuf.f_name, new_floor_name))
1899                     && (flbuf.f_flags & F_INUSE)) {
1900                         cprintf("%d Floor '%s' already exists.\n",
1901                                 ERROR + ALREADY_EXISTS,
1902                                 flbuf.f_name);
1903                         return;
1904                 }
1905         }
1906
1907         if (free_slot < 0) {
1908                 cprintf("%d There is no space available for a new floor.\n",
1909                         ERROR + INVALID_FLOOR_OPERATION);
1910                 return;
1911         }
1912         if (cflr_ok == 0) {
1913                 cprintf("%d ok to create...\n", CIT_OK);
1914                 return;
1915         }
1916         lgetfloor(&flbuf, free_slot);
1917         flbuf.f_flags = F_INUSE;
1918         flbuf.f_ref_count = 0;
1919         safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
1920         lputfloor(&flbuf, free_slot);
1921         cprintf("%d %d\n", CIT_OK, free_slot);
1922 }
1923
1924
1925
1926 /*
1927  * delete a floor
1928  */
1929 void cmd_kflr(char *argbuf)
1930 {
1931         struct floor flbuf;
1932         int floor_to_delete;
1933         int kflr_ok;
1934         int delete_ok;
1935
1936         floor_to_delete = extract_int(argbuf, 0);
1937         kflr_ok = extract_int(argbuf, 1);
1938
1939         if (CtdlAccessCheck(ac_aide)) return;
1940
1941         lgetfloor(&flbuf, floor_to_delete);
1942
1943         delete_ok = 1;
1944         if ((flbuf.f_flags & F_INUSE) == 0) {
1945                 cprintf("%d Floor %d not in use.\n",
1946                         ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
1947                 delete_ok = 0;
1948         } else {
1949                 if (flbuf.f_ref_count != 0) {
1950                         cprintf("%d Cannot delete; floor contains %d rooms.\n",
1951                                 ERROR + INVALID_FLOOR_OPERATION,
1952                                 flbuf.f_ref_count);
1953                         delete_ok = 0;
1954                 } else {
1955                         if (kflr_ok == 1) {
1956                                 cprintf("%d Ok\n", CIT_OK);
1957                         } else {
1958                                 cprintf("%d Ok to delete...\n", CIT_OK);
1959                         }
1960
1961                 }
1962
1963         }
1964
1965         if ((delete_ok == 1) && (kflr_ok == 1))
1966                 flbuf.f_flags = 0;
1967         lputfloor(&flbuf, floor_to_delete);
1968 }
1969
1970 /*
1971  * edit a floor
1972  */
1973 void cmd_eflr(char *argbuf)
1974 {
1975         struct floor flbuf;
1976         int floor_num;
1977         int np;
1978
1979         np = num_parms(argbuf);
1980         if (np < 1) {
1981                 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1982                 return;
1983         }
1984
1985         if (CtdlAccessCheck(ac_aide)) return;
1986
1987         floor_num = extract_int(argbuf, 0);
1988         lgetfloor(&flbuf, floor_num);
1989         if ((flbuf.f_flags & F_INUSE) == 0) {
1990                 lputfloor(&flbuf, floor_num);
1991                 cprintf("%d Floor %d is not in use.\n",
1992                         ERROR + INVALID_FLOOR_OPERATION, floor_num);
1993                 return;
1994         }
1995         if (np >= 2)
1996                 extract(flbuf.f_name, argbuf, 1);
1997         lputfloor(&flbuf, floor_num);
1998
1999         cprintf("%d Ok\n", CIT_OK);
2000 }