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