CtdlRoomAccess() can now return a new value UA_REPLYALLOWED, which
[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 | UA_REPLYALLOWED);
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 | UA_REPLYALLOWED;
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 | UA_REPLYALLOWED;
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 | UA_REPLYALLOWED;
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 | UA_REPLYALLOWED;
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 & ~UA_REPLYALLOWED;
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 | UA_REPLYALLOWED;
188                 }
189                 else {
190                         retval = retval | UA_KNOWN | UA_GOTOALLOWED | UA_POSTALLOWED | UA_REPLYALLOWED;
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 | UA_REPLYALLOWED;
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 | UA_REPLYALLOWED;
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         CtdlGetUser(&CC->user, CC->curr_user);
699         cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
700
701         CtdlForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
702         cprintf("000\n");
703 }
704
705
706
707 /* 
708  * cmd_lkra()   -  List all known rooms
709  */
710 void cmd_lkra_backend(struct ctdlroom *qrbuf, void *data)
711 {
712         int FloorBeingSearched = (-1);
713         int ra;
714         int view;
715
716         FloorBeingSearched = *(int *)data;
717         CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
718
719         if ((( ra & (UA_KNOWN)))
720             && ((qrbuf->QRfloor == (FloorBeingSearched))
721                 || ((FloorBeingSearched) < 0)))
722                 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
723 }
724
725 void cmd_lkra(char *argbuf)
726 {
727         int FloorBeingSearched = (-1);
728         if (!IsEmptyStr(argbuf))
729                 FloorBeingSearched = extract_int(argbuf, 0);
730
731         if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
732         
733         CtdlGetUser(&CC->user, CC->curr_user);
734         cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
735
736         CtdlForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
737         cprintf("000\n");
738 }
739
740
741
742 void cmd_lprm_backend(struct ctdlroom *qrbuf, void *data)
743 {
744         int FloorBeingSearched = (-1);
745         int ra;
746         int view;
747
748         FloorBeingSearched = *(int *)data;
749         CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
750
751         if (   ((qrbuf->QRflags & QR_PRIVATE) == 0)
752                 && ((qrbuf->QRflags & QR_MAILBOX) == 0)
753             && ((qrbuf->QRfloor == (FloorBeingSearched))
754                 || ((FloorBeingSearched) < 0)))
755                 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
756 }
757
758 void cmd_lprm(char *argbuf)
759 {
760         int FloorBeingSearched = (-1);
761         if (!IsEmptyStr(argbuf))
762                 FloorBeingSearched = extract_int(argbuf, 0);
763
764         cprintf("%d Publiic rooms:\n", LISTING_FOLLOWS);
765
766         CtdlForEachRoom(cmd_lprm_backend, &FloorBeingSearched);
767         cprintf("000\n");
768 }
769
770
771
772 /* 
773  * cmd_lkrn()   -  List all known rooms with new messages
774  */
775 void cmd_lkrn_backend(struct ctdlroom *qrbuf, void *data)
776 {
777         int FloorBeingSearched = (-1);
778         int ra;
779         int view;
780
781         FloorBeingSearched = *(int *)data;
782         CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
783
784         if ((ra & UA_KNOWN)
785             && (ra & UA_HASNEWMSGS)
786             && ((qrbuf->QRfloor == (FloorBeingSearched))
787                 || ((FloorBeingSearched) < 0)))
788                 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
789 }
790
791 void cmd_lkrn(char *argbuf)
792 {
793         int FloorBeingSearched = (-1);
794         if (!IsEmptyStr(argbuf))
795                 FloorBeingSearched = extract_int(argbuf, 0);
796
797         if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
798         
799         CtdlGetUser(&CC->user, CC->curr_user);
800         cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
801
802         CtdlForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
803         cprintf("000\n");
804 }
805
806
807
808 /* 
809  * cmd_lkro()   -  List all known rooms
810  */
811 void cmd_lkro_backend(struct ctdlroom *qrbuf, void *data)
812 {
813         int FloorBeingSearched = (-1);
814         int ra;
815         int view;
816
817         FloorBeingSearched = *(int *)data;
818         CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
819
820         if ((ra & UA_KNOWN)
821             && ((ra & UA_HASNEWMSGS) == 0)
822             && ((qrbuf->QRfloor == (FloorBeingSearched))
823                 || ((FloorBeingSearched) < 0)))
824                 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
825 }
826
827 void cmd_lkro(char *argbuf)
828 {
829         int FloorBeingSearched = (-1);
830         if (!IsEmptyStr(argbuf))
831                 FloorBeingSearched = extract_int(argbuf, 0);
832
833         if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
834         
835         CtdlGetUser(&CC->user, CC->curr_user);
836         cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
837
838         CtdlForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
839         cprintf("000\n");
840 }
841
842
843
844 /* 
845  * cmd_lzrm()   -  List all forgotten rooms
846  */
847 void cmd_lzrm_backend(struct ctdlroom *qrbuf, void *data)
848 {
849         int FloorBeingSearched = (-1);
850         int ra;
851         int view;
852
853         FloorBeingSearched = *(int *)data;
854         CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
855
856         if ((ra & UA_GOTOALLOWED)
857             && (ra & UA_ZAPPED)
858             && ((qrbuf->QRfloor == (FloorBeingSearched))
859                 || ((FloorBeingSearched) < 0)))
860                 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
861 }
862
863 void cmd_lzrm(char *argbuf)
864 {
865         int FloorBeingSearched = (-1);
866         if (!IsEmptyStr(argbuf))
867                 FloorBeingSearched = extract_int(argbuf, 0);
868
869         if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
870         
871         CtdlGetUser(&CC->user, CC->curr_user);
872         cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
873
874         CtdlForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
875         cprintf("000\n");
876 }
877
878
879 /*
880  * Make the specified room the current room for this session.  No validation
881  * or access control is done here -- the caller should make sure that the
882  * specified room exists and is ok to access.
883  */
884 void CtdlUserGoto(char *where, int display_result, int transiently,
885                 int *retmsgs, int *retnew)
886 {
887         int a;
888         int new_messages = 0;
889         int old_messages = 0;
890         int total_messages = 0;
891         int info = 0;
892         int rmailflag;
893         int raideflag;
894         int newmailcount = 0;
895         visit vbuf;
896         char truncated_roomname[ROOMNAMELEN];
897         struct cdbdata *cdbfr;
898         long *msglist = NULL;
899         int num_msgs = 0;
900         unsigned int original_v_flags;
901         int num_sets;
902         int s;
903         char setstr[128], lostr[64], histr[64];
904         long lo, hi;
905         int is_trash = 0;
906
907         /* If the supplied room name is NULL, the caller wants us to know that
908          * it has already copied the room record into CC->room, so
909          * we can skip the extra database fetch.
910          */
911         if (where != NULL) {
912                 safestrncpy(CC->room.QRname, where, sizeof CC->room.QRname);
913                 CtdlGetRoom(&CC->room, where);
914         }
915
916         /* Take care of all the formalities. */
917
918         begin_critical_section(S_USERS);
919         CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
920         original_v_flags = vbuf.v_flags;
921
922         /* Know the room ... but not if it's the page log room, or if the
923          * caller specified that we're only entering this room transiently.
924          */
925         if ((strcasecmp(CC->room.QRname, config.c_logpages))
926            && (transiently == 0) ) {
927                 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
928                 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
929         }
930         
931         /* Only rewrite the database record if we changed something */
932         if (vbuf.v_flags != original_v_flags) {
933                 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
934         }
935         end_critical_section(S_USERS);
936
937         /* Check for new mail */
938         newmailcount = NewMailCount();
939
940         /* set info to 1 if the user needs to read the room's info file */
941         if (CC->room.QRinfo > vbuf.v_lastseen) {
942                 info = 1;
943         }
944
945         cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
946         if (cdbfr != NULL) {
947                 msglist = (long *) cdbfr->ptr;
948                 cdbfr->ptr = NULL;      /* CtdlUserGoto() now owns this memory */
949                 num_msgs = cdbfr->len / sizeof(long);
950                 cdb_free(cdbfr);
951         }
952
953         total_messages = 0;
954         for (a=0; a<num_msgs; ++a) {
955                 if (msglist[a] > 0L) ++total_messages;
956         }
957         new_messages = num_msgs;
958         num_sets = num_tokens(vbuf.v_seen, ',');
959         for (s=0; s<num_sets; ++s) {
960                 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
961
962                 extract_token(lostr, setstr, 0, ':', sizeof lostr);
963                 if (num_tokens(setstr, ':') >= 2) {
964                         extract_token(histr, setstr, 1, ':', sizeof histr);
965                         if (!strcmp(histr, "*")) {
966                                 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
967                         }
968                 } 
969                 else {
970                         strcpy(histr, lostr);
971                 }
972                 lo = atol(lostr);
973                 hi = atol(histr);
974
975                 for (a=0; a<num_msgs; ++a) if (msglist[a] > 0L) {
976                         if ((msglist[a] >= lo) && (msglist[a] <= hi)) {
977                                 ++old_messages;
978                                 msglist[a] = 0L;
979                         }
980                 }
981         }
982         new_messages = total_messages - old_messages;
983
984         if (msglist != NULL) free(msglist);
985
986         if (CC->room.QRflags & QR_MAILBOX)
987                 rmailflag = 1;
988         else
989                 rmailflag = 0;
990
991         if ((CC->room.QRroomaide == CC->user.usernum)
992             || (CC->user.axlevel >= AxAideU))
993                 raideflag = 1;
994         else
995                 raideflag = 0;
996
997         safestrncpy(truncated_roomname, CC->room.QRname, sizeof truncated_roomname);
998         if ( (CC->room.QRflags & QR_MAILBOX)
999            && (atol(CC->room.QRname) == CC->user.usernum) ) {
1000                 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
1001         }
1002
1003         if (!strcasecmp(truncated_roomname, USERTRASHROOM)) {
1004                 is_trash = 1;
1005         }
1006
1007         if (retmsgs != NULL) *retmsgs = total_messages;
1008         if (retnew != NULL) *retnew = new_messages;
1009         CtdlLogPrintf(CTDL_DEBUG, "<%s> %d new of %d total messages\n",
1010                 CC->room.QRname,
1011                 new_messages, total_messages
1012         );
1013
1014         CC->curr_view = (int)vbuf.v_view;
1015
1016         if (display_result) {
1017                 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d|%d|%d|%d|%d|\n",
1018                         CIT_OK, CtdlCheckExpress(),
1019                         truncated_roomname,
1020                         (int)new_messages,
1021                         (int)total_messages,
1022                         (int)info,
1023                         (int)CC->room.QRflags,
1024                         (long)CC->room.QRhighest,
1025                         (long)vbuf.v_lastseen,
1026                         (int)rmailflag,
1027                         (int)raideflag,
1028                         (int)newmailcount,
1029                         (int)CC->room.QRfloor,
1030                         (int)vbuf.v_view,
1031                         (int)CC->room.QRdefaultview,
1032                         (int)is_trash,
1033                         (int)CC->room.QRflags2
1034                 );
1035         }
1036 }
1037
1038
1039 /*
1040  * Handle some of the macro named rooms
1041  */
1042 void convert_room_name_macros(char *towhere, size_t maxlen) {
1043         if (!strcasecmp(towhere, "_BASEROOM_")) {
1044                 safestrncpy(towhere, config.c_baseroom, maxlen);
1045         }
1046         else if (!strcasecmp(towhere, "_MAIL_")) {
1047                 safestrncpy(towhere, MAILROOM, maxlen);
1048         }
1049         else if (!strcasecmp(towhere, "_TRASH_")) {
1050                 safestrncpy(towhere, USERTRASHROOM, maxlen);
1051         }
1052         else if (!strcasecmp(towhere, "_DRAFTS_")) {
1053                 safestrncpy(towhere, USERDRAFTROOM, maxlen);
1054         }
1055         else if (!strcasecmp(towhere, "_BITBUCKET_")) {
1056                 safestrncpy(towhere, config.c_twitroom, maxlen);
1057         }
1058         else if (!strcasecmp(towhere, "_CALENDAR_")) {
1059                 safestrncpy(towhere, USERCALENDARROOM, maxlen);
1060         }
1061         else if (!strcasecmp(towhere, "_TASKS_")) {
1062                 safestrncpy(towhere, USERTASKSROOM, maxlen);
1063         }
1064         else if (!strcasecmp(towhere, "_CONTACTS_")) {
1065                 safestrncpy(towhere, USERCONTACTSROOM, maxlen);
1066         }
1067         else if (!strcasecmp(towhere, "_NOTES_")) {
1068                 safestrncpy(towhere, USERNOTESROOM, maxlen);
1069         }
1070 }
1071
1072
1073 /* 
1074  * cmd_goto()  -  goto a new room
1075  */
1076 void cmd_goto(char *gargs)
1077 {
1078         struct ctdlroom QRscratch;
1079         int c;
1080         int ok = 0;
1081         int ra;
1082         char augmented_roomname[ROOMNAMELEN];
1083         char towhere[ROOMNAMELEN];
1084         char password[32];
1085         int transiently = 0;
1086
1087         if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
1088
1089         extract_token(towhere, gargs, 0, '|', sizeof towhere);
1090         extract_token(password, gargs, 1, '|', sizeof password);
1091         transiently = extract_int(gargs, 2);
1092
1093         CtdlGetUser(&CC->user, CC->curr_user);
1094
1095         /*
1096          * Handle some of the macro named rooms
1097          */
1098         convert_room_name_macros(towhere, sizeof towhere);
1099
1100         /* First try a regular match */
1101         c = CtdlGetRoom(&QRscratch, towhere);
1102
1103         /* Then try a mailbox name match */
1104         if (c != 0) {
1105                 CtdlMailboxName(augmented_roomname, sizeof augmented_roomname,
1106                             &CC->user, towhere);
1107                 c = CtdlGetRoom(&QRscratch, augmented_roomname);
1108                 if (c == 0)
1109                         safestrncpy(towhere, augmented_roomname, sizeof towhere);
1110         }
1111
1112         /* And if the room was found... */
1113         if (c == 0) {
1114
1115                 /* Let internal programs go directly to any room. */
1116                 if (CC->internal_pgm) {
1117                         memcpy(&CC->room, &QRscratch,
1118                                 sizeof(struct ctdlroom));
1119                         CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1120                         return;
1121                 }
1122
1123                 /* See if there is an existing user/room relationship */
1124                 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
1125
1126                 /* normal clients have to pass through security */
1127                 if (ra & UA_GOTOALLOWED) {
1128                         ok = 1;
1129                 }
1130
1131                 if (ok == 1) {
1132                         if ((QRscratch.QRflags & QR_MAILBOX) &&
1133                             ((ra & UA_GOTOALLOWED))) {
1134                                 memcpy(&CC->room, &QRscratch,
1135                                         sizeof(struct ctdlroom));
1136                                 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1137                                 return;
1138                         } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
1139                             ((ra & UA_KNOWN) == 0) &&
1140                             (strcasecmp(QRscratch.QRpasswd, password)) &&
1141                             (CC->user.axlevel < AxAideU)
1142                             ) {
1143                                 cprintf("%d wrong or missing passwd\n",
1144                                         ERROR + PASSWORD_REQUIRED);
1145                                 return;
1146                         } else if ((QRscratch.QRflags & QR_PRIVATE) &&
1147                                    ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
1148                                    ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
1149                                    ((ra & UA_KNOWN) == 0) &&
1150                                    (CC->user.axlevel < AxAideU)
1151                                   ) {
1152                                 CtdlLogPrintf(CTDL_DEBUG, "Failed to acquire private room\n");
1153                         } else {
1154                                 memcpy(&CC->room, &QRscratch,
1155                                         sizeof(struct ctdlroom));
1156                                 CtdlUserGoto(NULL, 1, transiently, NULL, NULL);
1157                                 return;
1158                         }
1159                 }
1160         }
1161
1162         cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
1163 }
1164
1165
1166 void cmd_whok(char *cmdbuf)
1167 {
1168         struct ctdluser temp;
1169         struct cdbdata *cdbus;
1170         int ra;
1171
1172         cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
1173         cdb_rewind(CDB_USERS);
1174         while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1175                 memset(&temp, 0, sizeof temp);
1176                 memcpy(&temp, cdbus->ptr, sizeof temp);
1177                 cdb_free(cdbus);
1178
1179                 CtdlRoomAccess(&CC->room, &temp, &ra, NULL);
1180                 if ((!IsEmptyStr(temp.fullname)) && 
1181                     (CC->room.QRflags & QR_INUSE) &&
1182                     (ra & UA_KNOWN)
1183                         )
1184                         cprintf("%s\n", temp.fullname);
1185         }
1186         cprintf("000\n");
1187 }
1188
1189
1190 /*
1191  * RDIR command for room directory
1192  */
1193 void cmd_rdir(char *cmdbuf)
1194 {
1195         char buf[256];
1196         char comment[256];
1197         FILE *fd;
1198         struct stat statbuf;
1199         DIR *filedir = NULL;
1200         struct dirent *filedir_entry;
1201         int d_namelen;
1202         char buf2[SIZ];
1203         char mimebuf[64];
1204         long len;
1205         
1206         if (CtdlAccessCheck(ac_logged_in)) return;
1207         
1208         CtdlGetRoom(&CC->room, CC->room.QRname);
1209         CtdlGetUser(&CC->user, CC->curr_user);
1210
1211         if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
1212                 cprintf("%d not here.\n", ERROR + NOT_HERE);
1213                 return;
1214         }
1215         if (((CC->room.QRflags & QR_VISDIR) == 0)
1216             && (CC->user.axlevel < AxAideU)
1217             && (CC->user.usernum != CC->room.QRroomaide)) {
1218                 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1219                 return;
1220         }
1221
1222         snprintf(buf, sizeof buf, "%s/%s", ctdl_file_dir, CC->room.QRdirname);
1223         filedir = opendir (buf);
1224         
1225         if (filedir == NULL) {
1226                 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
1227                 return;
1228         }
1229         cprintf("%d %s|%s/%s\n", LISTING_FOLLOWS, config.c_fqdn, ctdl_file_dir, CC->room.QRdirname);
1230         
1231         snprintf(buf, sizeof buf, "%s/%s/filedir", ctdl_file_dir, CC->room.QRdirname);
1232         fd = fopen(buf, "r");
1233         if (fd == NULL)
1234                 fd = fopen("/dev/null", "r");
1235         while ((filedir_entry = readdir(filedir)))
1236         {
1237                 if (strcasecmp(filedir_entry->d_name, "filedir") && filedir_entry->d_name[0] != '.')
1238                 {
1239 #ifdef _DIRENT_HAVE_D_NAMELEN
1240                         d_namelen = filedir_entry->d_namelen;
1241 #else
1242                         d_namelen = strlen(filedir_entry->d_name);
1243 #endif
1244                         snprintf(buf, sizeof buf, "%s/%s/%s", ctdl_file_dir, CC->room.QRdirname, filedir_entry->d_name);
1245                         stat(buf, &statbuf);    /* stat the file */
1246                         if (!(statbuf.st_mode & S_IFREG))
1247                         {
1248                                 snprintf(buf2, sizeof buf2,
1249                                         "\"%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",
1250                                         buf, CC->room.QRname
1251                                 );
1252                                 CtdlAideMessage(buf2, "Unusable data found in room directory");
1253                                 continue;       /* not a useable file type so don't show it */
1254                         }
1255                         safestrncpy(comment, "", sizeof comment);
1256                         fseek(fd, 0L, 0);       /* rewind descriptions file */
1257                         /* Get the description from the descriptions file */
1258                         while ((fgets(buf, sizeof buf, fd) != NULL) && (IsEmptyStr(comment))) 
1259                         {
1260                                 buf[strlen(buf) - 1] = 0;
1261                                 if ((!strncasecmp(buf, filedir_entry->d_name, d_namelen)) && (buf[d_namelen] == ' '))
1262                                         safestrncpy(comment, &buf[d_namelen + 1], sizeof comment);
1263                         }
1264                         len = extract_token (mimebuf, comment, 0,' ', 64);
1265                         if ((len <0) || strchr(mimebuf, '/') == NULL)
1266                         {
1267                                 snprintf (mimebuf, 64, "application/octetstream");
1268                                 len = 0;
1269                         }
1270                         cprintf("%s|%ld|%s|%s\n", 
1271                                 filedir_entry->d_name, 
1272                                 (long)statbuf.st_size, 
1273                                 mimebuf, 
1274                                 &comment[len]);
1275                 }
1276         }
1277         fclose(fd);
1278         closedir(filedir);
1279         
1280         cprintf("000\n");
1281 }
1282
1283 /*
1284  * get room parameters (aide or room aide command)
1285  */
1286 void cmd_getr(char *cmdbuf)
1287 {
1288         if (CtdlAccessCheck(ac_room_aide)) return;
1289
1290         CtdlGetRoom(&CC->room, CC->room.QRname);
1291         cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
1292                 CIT_OK,
1293                 CtdlCheckExpress(),
1294
1295                 ((CC->room.QRflags & QR_MAILBOX) ?
1296                         &CC->room.QRname[11] : CC->room.QRname),
1297
1298                 ((CC->room.QRflags & QR_PASSWORDED) ?
1299                         CC->room.QRpasswd : ""),
1300
1301                 ((CC->room.QRflags & QR_DIRECTORY) ?
1302                         CC->room.QRdirname : ""),
1303
1304                 CC->room.QRflags,
1305                 (int) CC->room.QRfloor,
1306                 (int) CC->room.QRorder,
1307
1308                 CC->room.QRdefaultview,
1309                 CC->room.QRflags2
1310                 );
1311 }
1312
1313
1314 /*
1315  * Back end function to rename a room.
1316  * You can also specify which floor to move the room to, or specify -1 to
1317  * keep the room on the same floor it was on.
1318  *
1319  * If you are renaming a mailbox room, you must supply the namespace prefix
1320  * in *at least* the old name!
1321  */
1322 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
1323         int old_floor = 0;
1324         struct ctdlroom qrbuf;
1325         struct ctdlroom qrtmp;
1326         int ret = 0;
1327         struct floor *fl;
1328         struct floor flbuf;
1329         long owner = 0L;
1330         char actual_old_name[ROOMNAMELEN];
1331
1332         CtdlLogPrintf(CTDL_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
1333                 old_name, new_name, new_floor);
1334
1335         if (new_floor >= 0) {
1336                 fl = CtdlGetCachedFloor(new_floor);
1337                 if ((fl->f_flags & F_INUSE) == 0) {
1338                         return(crr_invalid_floor);
1339                 }
1340         }
1341
1342         begin_critical_section(S_ROOMS);
1343
1344         if ( (CtdlGetRoom(&qrtmp, new_name) == 0) 
1345            && (strcasecmp(new_name, old_name)) ) {
1346                 ret = crr_already_exists;
1347         }
1348
1349         else if (CtdlGetRoom(&qrbuf, old_name) != 0) {
1350                 ret = crr_room_not_found;
1351         }
1352
1353         else if ( (CC->user.axlevel < AxAideU) && (!CC->internal_pgm)
1354                   && (CC->user.usernum != qrbuf.QRroomaide)
1355                   && ( (((qrbuf.QRflags & QR_MAILBOX) == 0) || (atol(qrbuf.QRname) != CC->user.usernum))) )  {
1356                 ret = crr_access_denied;
1357         }
1358
1359         else if (CtdlIsNonEditable(&qrbuf)) {
1360                 ret = crr_noneditable;
1361         }
1362
1363         else {
1364                 /* Rename it */
1365                 safestrncpy(actual_old_name, qrbuf.QRname, sizeof actual_old_name);
1366                 if (qrbuf.QRflags & QR_MAILBOX) {
1367                         owner = atol(qrbuf.QRname);
1368                 }
1369                 if ( (owner > 0L) && (atol(new_name) == 0L) ) {
1370                         snprintf(qrbuf.QRname, sizeof(qrbuf.QRname),
1371                                         "%010ld.%s", owner, new_name);
1372                 }
1373                 else {
1374                         safestrncpy(qrbuf.QRname, new_name,
1375                                                 sizeof(qrbuf.QRname));
1376                 }
1377
1378                 /* Reject change of floor for baseroom/aideroom */
1379                 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN) ||
1380                     !strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1381                         new_floor = 0;
1382                 }
1383
1384                 /* Take care of floor stuff */
1385                 old_floor = qrbuf.QRfloor;
1386                 if (new_floor < 0) {
1387                         new_floor = old_floor;
1388                 }
1389                 qrbuf.QRfloor = new_floor;
1390                 CtdlPutRoom(&qrbuf);
1391
1392                 begin_critical_section(S_CONFIG);
1393         
1394                 /* If baseroom/aideroom name changes, update config */
1395                 if (!strncasecmp(old_name, config.c_baseroom, ROOMNAMELEN)) {
1396                         safestrncpy(config.c_baseroom, new_name, ROOMNAMELEN);
1397                         put_config();
1398                 }
1399                 if (!strncasecmp(old_name, config.c_aideroom, ROOMNAMELEN)) {
1400                         safestrncpy(config.c_aideroom, new_name, ROOMNAMELEN);
1401                         put_config();
1402                 }
1403         
1404                 end_critical_section(S_CONFIG);
1405         
1406                 /* If the room name changed, then there are now two room
1407                  * records, so we have to delete the old one.
1408                  */
1409                 if (strcasecmp(new_name, old_name)) {
1410                         b_deleteroom(actual_old_name);
1411                 }
1412
1413                 ret = crr_ok;
1414         }
1415
1416         end_critical_section(S_ROOMS);
1417
1418         /* Adjust the floor reference counts if necessary */
1419         if (new_floor != old_floor) {
1420                 lgetfloor(&flbuf, old_floor);
1421                 --flbuf.f_ref_count;
1422                 lputfloor(&flbuf, old_floor);
1423                 CtdlLogPrintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
1424                 lgetfloor(&flbuf, new_floor);
1425                 ++flbuf.f_ref_count;
1426                 lputfloor(&flbuf, new_floor);
1427                 CtdlLogPrintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
1428         }
1429
1430         /* ...and everybody say "YATTA!" */     
1431         return(ret);
1432 }
1433
1434
1435 /*
1436  * set room parameters (aide or room aide command)
1437  */
1438 void cmd_setr(char *args)
1439 {
1440         char buf[256];
1441         int new_order = 0;
1442         int r;
1443         int new_floor;
1444         char new_name[ROOMNAMELEN];
1445
1446         if (CtdlAccessCheck(ac_logged_in)) return;
1447
1448         if (num_parms(args) >= 6) {
1449                 new_floor = extract_int(args, 5);
1450         } else {
1451                 new_floor = (-1);       /* don't change the floor */
1452         }
1453
1454         /* When is a new name more than just a new name?  When the old name
1455          * has a namespace prefix.
1456          */
1457         if (CC->room.QRflags & QR_MAILBOX) {
1458                 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
1459         } else {
1460                 safestrncpy(new_name, "", sizeof new_name);
1461         }
1462         extract_token(&new_name[strlen(new_name)], args, 0, '|', (sizeof new_name - strlen(new_name)));
1463
1464         r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
1465
1466         if (r == crr_room_not_found) {
1467                 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
1468         } else if (r == crr_already_exists) {
1469                 cprintf("%d '%s' already exists.\n",
1470                         ERROR + ALREADY_EXISTS, new_name);
1471         } else if (r == crr_noneditable) {
1472                 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
1473         } else if (r == crr_invalid_floor) {
1474                 cprintf("%d Target floor does not exist.\n",
1475                         ERROR + INVALID_FLOOR_OPERATION);
1476         } else if (r == crr_access_denied) {
1477                 cprintf("%d You do not have permission to edit '%s'\n",
1478                         ERROR + HIGHER_ACCESS_REQUIRED,
1479                         CC->room.QRname);
1480         } else if (r != crr_ok) {
1481                 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
1482                         ERROR + INTERNAL_ERROR, r);
1483         }
1484
1485         if (r != crr_ok) {
1486                 return;
1487         }
1488
1489         CtdlGetRoom(&CC->room, new_name);
1490
1491         /* Now we have to do a bunch of other stuff */
1492
1493         if (num_parms(args) >= 7) {
1494                 new_order = extract_int(args, 6);
1495                 if (new_order < 1)
1496                         new_order = 1;
1497                 if (new_order > 127)
1498                         new_order = 127;
1499         }
1500
1501         CtdlGetRoomLock(&CC->room, CC->room.QRname);
1502
1503         /* Directory room */
1504         extract_token(buf, args, 2, '|', sizeof buf);
1505         buf[15] = 0;
1506         safestrncpy(CC->room.QRdirname, buf,
1507                 sizeof CC->room.QRdirname);
1508
1509         /* Default view */
1510         if (num_parms(args) >= 8) {
1511                 CC->room.QRdefaultview = extract_int(args, 7);
1512         }
1513
1514         /* Second set of flags */
1515         if (num_parms(args) >= 9) {
1516                 CC->room.QRflags2 = extract_int(args, 8);
1517         }
1518
1519         /* Misc. flags */
1520         CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
1521         /* Clean up a client boo-boo: if the client set the room to
1522          * guess-name or passworded, ensure that the private flag is
1523          * also set.
1524          */
1525         if ((CC->room.QRflags & QR_GUESSNAME)
1526             || (CC->room.QRflags & QR_PASSWORDED))
1527                 CC->room.QRflags |= QR_PRIVATE;
1528
1529         /* Some changes can't apply to BASEROOM */
1530         if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1531                          ROOMNAMELEN)) {
1532                 CC->room.QRorder = 0;
1533                 CC->room.QRpasswd[0] = '\0';
1534                 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
1535                         QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
1536                 CC->room.QRflags |= QR_PERMANENT;
1537         } else {        
1538                 /* March order (doesn't apply to AIDEROOM) */
1539                 if (num_parms(args) >= 7)
1540                         CC->room.QRorder = (char) new_order;
1541                 /* Room password */
1542                 extract_token(buf, args, 1, '|', sizeof buf);
1543                 buf[10] = 0;
1544                 safestrncpy(CC->room.QRpasswd, buf,
1545                             sizeof CC->room.QRpasswd);
1546                 /* Kick everyone out if the client requested it
1547                  * (by changing the room's generation number)
1548                  */
1549                 if (extract_int(args, 4)) {
1550                         time(&CC->room.QRgen);
1551                 }
1552         }
1553         /* Some changes can't apply to AIDEROOM */
1554         if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1555                          ROOMNAMELEN)) {
1556                 CC->room.QRorder = 0;
1557                 CC->room.QRflags &= ~QR_MAILBOX;
1558                 CC->room.QRflags |= QR_PERMANENT;
1559         }
1560
1561         /* Write the room record back to disk */
1562         CtdlPutRoomLock(&CC->room);
1563
1564         /* Create a room directory if necessary */
1565         if (CC->room.QRflags & QR_DIRECTORY) {
1566                 snprintf(buf, sizeof buf,"%s/%s",
1567                                  ctdl_file_dir,
1568                                  CC->room.QRdirname);
1569                 mkdir(buf, 0755);
1570         }
1571         snprintf(buf, sizeof buf, "The room \"%s\" has been edited by %s.\n",
1572                 CC->room.QRname, CC->curr_user);
1573         CtdlAideMessage(buf, "Room modification Message");
1574         cprintf("%d Ok\n", CIT_OK);
1575 }
1576
1577
1578
1579 /* 
1580  * get the name of the room aide for this room
1581  */
1582 void cmd_geta(char *cmdbuf)
1583 {
1584         struct ctdluser usbuf;
1585
1586         if (CtdlAccessCheck(ac_logged_in)) return;
1587
1588         if (CtdlGetUserByNumber(&usbuf, CC->room.QRroomaide) == 0) {
1589                 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1590         } else {
1591                 cprintf("%d \n", CIT_OK);
1592         }
1593 }
1594
1595
1596 /* 
1597  * set the room aide for this room
1598  */
1599 void cmd_seta(char *new_ra)
1600 {
1601         struct ctdluser usbuf;
1602         long newu;
1603         char buf[SIZ];
1604         int post_notice;
1605
1606         if (CtdlAccessCheck(ac_room_aide)) return;
1607
1608         if (CtdlGetUser(&usbuf, new_ra) != 0) {
1609                 newu = (-1L);
1610         } else {
1611                 newu = usbuf.usernum;
1612         }
1613
1614         CtdlGetRoomLock(&CC->room, CC->room.QRname);
1615         post_notice = 0;
1616         if (CC->room.QRroomaide != newu) {
1617                 post_notice = 1;
1618         }
1619         CC->room.QRroomaide = newu;
1620         CtdlPutRoomLock(&CC->room);
1621
1622         /*
1623          * We have to post the change notice _after_ writing changes to 
1624          * the room table, otherwise it would deadlock!
1625          */
1626         if (post_notice == 1) {
1627                 if (!IsEmptyStr(usbuf.fullname))
1628                         snprintf(buf, sizeof buf,
1629                                 "%s is now the room aide for \"%s\".\n",
1630                                 usbuf.fullname, CC->room.QRname);
1631                 else
1632                         snprintf(buf, sizeof buf,
1633                                 "There is now no room aide for \"%s\".\n",
1634                                 CC->room.QRname);
1635                 CtdlAideMessage(buf, "Aide Room Modification");
1636         }
1637         cprintf("%d Ok\n", CIT_OK);
1638 }
1639
1640 /* 
1641  * retrieve info file for this room
1642  */
1643 void cmd_rinf(char *gargs)
1644 {
1645         char filename[128];
1646         char buf[SIZ];
1647         FILE *info_fp;
1648
1649         assoc_file_name(filename, sizeof filename, &CC->room, ctdl_info_dir);
1650         info_fp = fopen(filename, "r");
1651
1652         if (info_fp == NULL) {
1653                 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
1654                 return;
1655         }
1656         cprintf("%d Info:\n", LISTING_FOLLOWS);
1657         while (fgets(buf, sizeof buf, info_fp) != NULL) {
1658                 if (!IsEmptyStr(buf))
1659                         buf[strlen(buf) - 1] = 0;
1660                 cprintf("%s\n", buf);
1661         }
1662         cprintf("000\n");
1663         fclose(info_fp);
1664 }
1665
1666 /*
1667  * Asynchronously schedule a room for deletion.  The room will appear
1668  * deleted to the user(s), but it won't actually get purged from the
1669  * database until THE DREADED AUTO-PURGER makes its next run.
1670  */
1671 void CtdlScheduleRoomForDeletion(struct ctdlroom *qrbuf)
1672 {
1673         char old_name[ROOMNAMELEN];
1674         static int seq = 0;
1675
1676         CtdlLogPrintf(CTDL_NOTICE, "Scheduling room <%s> for deletion\n",
1677                 qrbuf->QRname);
1678
1679         safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
1680
1681         CtdlGetRoom(qrbuf, qrbuf->QRname);
1682
1683         /* Turn the room into a private mailbox owned by a user who doesn't
1684          * exist.  This will immediately make the room invisible to everyone,
1685          * and qualify the room for purging.
1686          */
1687         snprintf(qrbuf->QRname, sizeof qrbuf->QRname, "9999999999.%08lx.%04d.%s",
1688                 time(NULL),
1689                 ++seq,
1690                 old_name
1691         );
1692         qrbuf->QRflags |= QR_MAILBOX;
1693         time(&qrbuf->QRgen);    /* Use a timestamp as the new generation number  */
1694
1695         CtdlPutRoom(qrbuf);
1696
1697         b_deleteroom(old_name);
1698 }
1699
1700
1701
1702 /*
1703  * Back end processing to delete a room and everything associated with it
1704  * (This one is synchronous and should only get called by THE DREADED
1705  * AUTO-PURGER in serv_expire.c.  All user-facing code should call
1706  * the asynchronous schedule_room_for_deletion() instead.)
1707  */
1708 void CtdlDeleteRoom(struct ctdlroom *qrbuf)
1709 {
1710         struct floor flbuf;
1711         char filename[100];
1712         /* TODO: filename magic? does this realy work? */
1713
1714         CtdlLogPrintf(CTDL_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
1715
1716         /* Delete the info file */
1717         assoc_file_name(filename, sizeof filename, qrbuf, ctdl_info_dir);
1718         unlink(filename);
1719
1720         /* Delete the image file */
1721         assoc_file_name(filename, sizeof filename, qrbuf, ctdl_image_dir);
1722         unlink(filename);
1723
1724         /* Delete the room's network config file */
1725         assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1726         unlink(filename);
1727
1728         /* Delete the messages in the room
1729          * (Careful: this opens an S_ROOMS critical section!)
1730          */
1731         CtdlDeleteMessages(qrbuf->QRname, NULL, 0, "");
1732
1733         /* Flag the room record as not in use */
1734         CtdlGetRoomLock(qrbuf, qrbuf->QRname);
1735         qrbuf->QRflags = 0;
1736         CtdlPutRoomLock(qrbuf);
1737
1738         /* then decrement the reference count for the floor */
1739         lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1740         flbuf.f_ref_count = flbuf.f_ref_count - 1;
1741         lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1742
1743         /* Delete the room record from the database! */
1744         b_deleteroom(qrbuf->QRname);
1745 }
1746
1747
1748
1749 /*
1750  * Check access control for deleting a room
1751  */
1752 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr) {
1753
1754         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1755                 return(0);
1756         }
1757
1758         if (CtdlIsNonEditable(qr)) {
1759                 return(0);
1760         }
1761
1762         /*
1763          * For mailboxes, check stuff
1764          */
1765         if (qr->QRflags & QR_MAILBOX) {
1766
1767                 if (strlen(qr->QRname) < 12) return(0); /* bad name */
1768
1769                 if (atol(qr->QRname) != CC->user.usernum) {
1770                         return(0);      /* not my room */
1771                 }
1772
1773                 /* Can't delete your Mail> room */
1774                 if (!strcasecmp(&qr->QRname[11], MAILROOM)) return(0);
1775
1776                 /* Otherwise it's ok */
1777                 return(1);
1778         }
1779
1780         /*
1781          * For normal rooms, just check for aide or room aide status.
1782          */
1783         return(is_room_aide());
1784 }
1785
1786 /*
1787  * aide command: kill the current room
1788  */
1789 void cmd_kill(char *argbuf)
1790 {
1791         char deleted_room_name[ROOMNAMELEN];
1792         char msg[SIZ];
1793         int kill_ok;
1794
1795         kill_ok = extract_int(argbuf, 0);
1796
1797         if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
1798                 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
1799                 return;
1800         }
1801         if (kill_ok) {
1802                 if (CC->room.QRflags & QR_MAILBOX) {
1803                         safestrncpy(deleted_room_name, &CC->room.QRname[11], sizeof deleted_room_name);
1804                 }
1805                 else {
1806                         safestrncpy(deleted_room_name, CC->room.QRname, sizeof deleted_room_name);
1807                 }
1808
1809                 /* Do the dirty work */
1810                 CtdlScheduleRoomForDeletion(&CC->room);
1811
1812                 /* Return to the Lobby */
1813                 CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
1814
1815                 /* tell the world what we did */
1816                 snprintf(msg, sizeof msg, "The room \"%s\" has been deleted by %s.\n",
1817                          deleted_room_name, CC->curr_user);
1818                 CtdlAideMessage(msg, "Room Purger Message");
1819                 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
1820         } else {
1821                 cprintf("%d ok to delete.\n", CIT_OK);
1822         }
1823 }
1824
1825
1826 /*
1827  * Internal code to create a new room (returns room flags)
1828  *
1829  * Room types:  0=public, 1=hidden, 2=passworded, 3=invitation-only,
1830  *              4=mailbox, 5=mailbox, but caller supplies namespace
1831  */
1832 unsigned CtdlCreateRoom(char *new_room_name,
1833                      int new_room_type,
1834                      char *new_room_pass,
1835                      int new_room_floor,
1836                      int really_create,
1837                      int avoid_access,
1838                      int new_room_view)
1839 {
1840
1841         struct ctdlroom qrbuf;
1842         struct floor flbuf;
1843         visit vbuf;
1844
1845         CtdlLogPrintf(CTDL_DEBUG, "CtdlCreateRoom(name=%s, type=%d, view=%d)\n",
1846                 new_room_name, new_room_type, new_room_view);
1847
1848         if (CtdlGetRoom(&qrbuf, new_room_name) == 0) {
1849                 CtdlLogPrintf(CTDL_DEBUG, "%s already exists.\n", new_room_name);
1850                 return(0);
1851         }
1852
1853         memset(&qrbuf, 0, sizeof(struct ctdlroom));
1854         safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1855         qrbuf.QRflags = QR_INUSE;
1856         if (new_room_type > 0)
1857                 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1858         if (new_room_type == 1)
1859                 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1860         if (new_room_type == 2)
1861                 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1862         if ( (new_room_type == 4) || (new_room_type == 5) ) {
1863                 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1864                 /* qrbuf.QRflags2 |= QR2_SUBJECTREQ; */
1865         }
1866
1867         /* If the user is requesting a personal room, set up the room
1868          * name accordingly (prepend the user number)
1869          */
1870         if (new_room_type == 4) {
1871                 CtdlMailboxName(qrbuf.QRname, sizeof qrbuf.QRname, &CC->user, new_room_name);
1872         }
1873         else {
1874                 safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1875         }
1876
1877         /* If the room is private, and the system administrator has elected
1878          * to automatically grant room aide privileges, do so now; otherwise,
1879          * set the room aide to undefined.
1880          */
1881         if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1882                 qrbuf.QRroomaide = CC->user.usernum;
1883         } else {
1884                 qrbuf.QRroomaide = (-1L);
1885         }
1886
1887         /* 
1888          * If the caller is only interested in testing whether this will work,
1889          * return now without creating the room.
1890          */
1891         if (!really_create) return (qrbuf.QRflags);
1892
1893         qrbuf.QRnumber = get_new_room_number();
1894         qrbuf.QRhighest = 0L;   /* No messages in this room yet */
1895         time(&qrbuf.QRgen);     /* Use a timestamp as the generation number */
1896         qrbuf.QRfloor = new_room_floor;
1897         qrbuf.QRdefaultview = new_room_view;
1898
1899         /* save what we just did... */
1900         CtdlPutRoom(&qrbuf);
1901
1902         /* bump the reference count on whatever floor the room is on */
1903         lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1904         flbuf.f_ref_count = flbuf.f_ref_count + 1;
1905         lputfloor(&flbuf, (int) qrbuf.QRfloor);
1906
1907         /* Grant the creator access to the room unless the avoid_access
1908          * parameter was specified.
1909          */
1910         if ( (CC->logged_in) && (avoid_access == 0) ) {
1911                 CtdlGetRelationship(&vbuf, &CC->user, &qrbuf);
1912                 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1913                 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1914                 CtdlSetRelationship(&vbuf, &CC->user, &qrbuf);
1915         }
1916
1917         /* resume our happy day */
1918         return (qrbuf.QRflags);
1919 }
1920
1921
1922 /*
1923  * create a new room
1924  */
1925 void cmd_cre8(char *args)
1926 {
1927         int cre8_ok;
1928         char new_room_name[ROOMNAMELEN];
1929         int new_room_type;
1930         char new_room_pass[32];
1931         int new_room_floor;
1932         int new_room_view;
1933         char *notification_message = NULL;
1934         unsigned newflags;
1935         struct floor *fl;
1936         int avoid_access = 0;
1937
1938         cre8_ok = extract_int(args, 0);
1939         extract_token(new_room_name, args, 1, '|', sizeof new_room_name);
1940         new_room_name[ROOMNAMELEN - 1] = 0;
1941         new_room_type = extract_int(args, 2);
1942         extract_token(new_room_pass, args, 3, '|', sizeof new_room_pass);
1943         avoid_access = extract_int(args, 5);
1944         new_room_view = extract_int(args, 6);
1945         new_room_pass[9] = 0;
1946         new_room_floor = 0;
1947
1948         if ((IsEmptyStr(new_room_name)) && (cre8_ok == 1)) {
1949                 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
1950                 return;
1951         }
1952
1953         if (!strcasecmp(new_room_name, MAILROOM)) {
1954                 cprintf("%d '%s' already exists.\n",
1955                         ERROR + ALREADY_EXISTS, new_room_name);
1956                 return;
1957         }
1958
1959         if (num_parms(args) >= 5) {
1960                 fl = CtdlGetCachedFloor(extract_int(args, 4));
1961                 if (fl == NULL) {
1962                         cprintf("%d Invalid floor number.\n",
1963                                 ERROR + INVALID_FLOOR_OPERATION);
1964                         return;
1965                 }
1966                 else if ((fl->f_flags & F_INUSE) == 0) {
1967                         cprintf("%d Invalid floor number.\n",
1968                                 ERROR + INVALID_FLOOR_OPERATION);
1969                         return;
1970                 } else {
1971                         new_room_floor = extract_int(args, 4);
1972                 }
1973         }
1974
1975         if (CtdlAccessCheck(ac_logged_in)) return;
1976
1977         if (CC->user.axlevel < config.c_createax && !CC->internal_pgm) {
1978                 cprintf("%d You need higher access to create rooms.\n",
1979                         ERROR + HIGHER_ACCESS_REQUIRED);
1980                 return;
1981         }
1982
1983         if ((IsEmptyStr(new_room_name)) && (cre8_ok == 0)) {
1984                 cprintf("%d Ok to create rooms.\n", CIT_OK);
1985                 return;
1986         }
1987
1988         if ((new_room_type < 0) || (new_room_type > 5)) {
1989                 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
1990                 return;
1991         }
1992
1993         if (new_room_type == 5) {
1994                 if (CC->user.axlevel < AxAideU) {
1995                         cprintf("%d Higher access required\n", 
1996                                 ERROR + HIGHER_ACCESS_REQUIRED);
1997                         return;
1998                 }
1999         }
2000
2001         /* Check to make sure the requested room name doesn't already exist */
2002         newflags = CtdlCreateRoom(new_room_name,
2003                                 new_room_type, new_room_pass, new_room_floor,
2004                                 0, avoid_access, new_room_view);
2005         if (newflags == 0) {
2006                 cprintf("%d '%s' already exists.\n",
2007                         ERROR + ALREADY_EXISTS, new_room_name);
2008                 return;
2009         }
2010
2011         if (cre8_ok == 0) {
2012                 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
2013                 return;
2014         }
2015
2016         /* If we reach this point, the room needs to be created. */
2017
2018         newflags = CtdlCreateRoom(new_room_name,
2019                            new_room_type, new_room_pass, new_room_floor, 1, 0,
2020                            new_room_view);
2021
2022         /* post a message in Aide> describing the new room */
2023         notification_message = malloc(1024);
2024         snprintf(notification_message, 1024,
2025                 "A new room called \"%s\" has been created by %s%s%s%s%s%s\n",
2026                 new_room_name,
2027                 CC->user.fullname,
2028                 ((newflags & QR_MAILBOX) ? " [personal]" : ""),
2029                 ((newflags & QR_PRIVATE) ? " [private]" : ""),
2030                 ((newflags & QR_GUESSNAME) ? " [hidden]" : ""),
2031                 ((newflags & QR_PASSWORDED) ? " Password: " : ""),
2032                 ((newflags & QR_PASSWORDED) ? new_room_pass : "")
2033         );
2034         CtdlAideMessage(notification_message, "Room Creation Message");
2035         free(notification_message);
2036
2037         cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
2038 }
2039
2040
2041
2042 void cmd_einf(char *ok)
2043 {                               /* enter info file for current room */
2044         FILE *fp;
2045         char infofilename[SIZ];
2046         char buf[SIZ];
2047
2048         unbuffer_output();
2049
2050         if (CtdlAccessCheck(ac_room_aide)) return;
2051
2052         if (atoi(ok) == 0) {
2053                 cprintf("%d Ok.\n", CIT_OK);
2054                 return;
2055         }
2056         assoc_file_name(infofilename, sizeof infofilename, &CC->room, ctdl_info_dir);
2057         CtdlLogPrintf(CTDL_DEBUG, "opening\n");
2058         fp = fopen(infofilename, "w");
2059         CtdlLogPrintf(CTDL_DEBUG, "checking\n");
2060         if (fp == NULL) {
2061                 cprintf("%d Cannot open %s: %s\n",
2062                   ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
2063                 return;
2064         }
2065         cprintf("%d Send info...\n", SEND_LISTING);
2066
2067         do {
2068                 client_getln(buf, sizeof buf);
2069                 if (strcmp(buf, "000"))
2070                         fprintf(fp, "%s\n", buf);
2071         } while (strcmp(buf, "000"));
2072         fclose(fp);
2073
2074         /* now update the room index so people will see our new info */
2075         CtdlGetRoomLock(&CC->room, CC->room.QRname);            /* lock so no one steps on us */
2076         CC->room.QRinfo = CC->room.QRhighest + 1L;
2077         CtdlPutRoomLock(&CC->room);
2078 }
2079
2080
2081 /* 
2082  * cmd_lflr()   -  List all known floors
2083  */
2084 void cmd_lflr(char *gargs)
2085 {
2086         int a;
2087         struct floor flbuf;
2088
2089         if (CtdlAccessCheck(ac_logged_in)) return;
2090
2091         cprintf("%d Known floors:\n", LISTING_FOLLOWS);
2092
2093         for (a = 0; a < MAXFLOORS; ++a) {
2094                 CtdlGetFloor(&flbuf, a);
2095                 if (flbuf.f_flags & F_INUSE) {
2096                         cprintf("%d|%s|%d\n",
2097                                 a,
2098                                 flbuf.f_name,
2099                                 flbuf.f_ref_count);
2100                 }
2101         }
2102         cprintf("000\n");
2103 }
2104
2105
2106
2107 /*
2108  * create a new floor
2109  */
2110 void cmd_cflr(char *argbuf)
2111 {
2112         char new_floor_name[256];
2113         struct floor flbuf;
2114         int cflr_ok;
2115         int free_slot = (-1);
2116         int a;
2117
2118         extract_token(new_floor_name, argbuf, 0, '|', sizeof new_floor_name);
2119         cflr_ok = extract_int(argbuf, 1);
2120
2121         if (CtdlAccessCheck(ac_aide)) return;
2122
2123         if (IsEmptyStr(new_floor_name)) {
2124                 cprintf("%d Blank floor name not allowed.\n",
2125                         ERROR + ILLEGAL_VALUE);
2126                 return;
2127         }
2128
2129         for (a = 0; a < MAXFLOORS; ++a) {
2130                 CtdlGetFloor(&flbuf, a);
2131
2132                 /* note any free slots while we're scanning... */
2133                 if (((flbuf.f_flags & F_INUSE) == 0)
2134                     && (free_slot < 0))
2135                         free_slot = a;
2136
2137                 /* check to see if it already exists */
2138                 if ((!strcasecmp(flbuf.f_name, new_floor_name))
2139                     && (flbuf.f_flags & F_INUSE)) {
2140                         cprintf("%d Floor '%s' already exists.\n",
2141                                 ERROR + ALREADY_EXISTS,
2142                                 flbuf.f_name);
2143                         return;
2144                 }
2145         }
2146
2147         if (free_slot < 0) {
2148                 cprintf("%d There is no space available for a new floor.\n",
2149                         ERROR + INVALID_FLOOR_OPERATION);
2150                 return;
2151         }
2152         if (cflr_ok == 0) {
2153                 cprintf("%d ok to create...\n", CIT_OK);
2154                 return;
2155         }
2156         lgetfloor(&flbuf, free_slot);
2157         flbuf.f_flags = F_INUSE;
2158         flbuf.f_ref_count = 0;
2159         safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
2160         lputfloor(&flbuf, free_slot);
2161         cprintf("%d %d\n", CIT_OK, free_slot);
2162 }
2163
2164
2165
2166 /*
2167  * delete a floor
2168  */
2169 void cmd_kflr(char *argbuf)
2170 {
2171         struct floor flbuf;
2172         int floor_to_delete;
2173         int kflr_ok;
2174         int delete_ok;
2175
2176         floor_to_delete = extract_int(argbuf, 0);
2177         kflr_ok = extract_int(argbuf, 1);
2178
2179         if (CtdlAccessCheck(ac_aide)) return;
2180
2181         lgetfloor(&flbuf, floor_to_delete);
2182
2183         delete_ok = 1;
2184         if ((flbuf.f_flags & F_INUSE) == 0) {
2185                 cprintf("%d Floor %d not in use.\n",
2186                         ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
2187                 delete_ok = 0;
2188         } else {
2189                 if (flbuf.f_ref_count != 0) {
2190                         cprintf("%d Cannot delete; floor contains %d rooms.\n",
2191                                 ERROR + INVALID_FLOOR_OPERATION,
2192                                 flbuf.f_ref_count);
2193                         delete_ok = 0;
2194                 } else {
2195                         if (kflr_ok == 1) {
2196                                 cprintf("%d Ok\n", CIT_OK);
2197                         } else {
2198                                 cprintf("%d Ok to delete...\n", CIT_OK);
2199                         }
2200
2201                 }
2202
2203         }
2204
2205         if ((delete_ok == 1) && (kflr_ok == 1))
2206                 flbuf.f_flags = 0;
2207         lputfloor(&flbuf, floor_to_delete);
2208 }
2209
2210 /*
2211  * edit a floor
2212  */
2213 void cmd_eflr(char *argbuf)
2214 {
2215         struct floor flbuf;
2216         int floor_num;
2217         int np;
2218
2219         np = num_parms(argbuf);
2220         if (np < 1) {
2221                 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
2222                 return;
2223         }
2224
2225         if (CtdlAccessCheck(ac_aide)) return;
2226
2227         floor_num = extract_int(argbuf, 0);
2228         lgetfloor(&flbuf, floor_num);
2229         if ((flbuf.f_flags & F_INUSE) == 0) {
2230                 lputfloor(&flbuf, floor_num);
2231                 cprintf("%d Floor %d is not in use.\n",
2232                         ERROR + INVALID_FLOOR_OPERATION, floor_num);
2233                 return;
2234         }
2235         if (np >= 2)
2236                 extract_token(flbuf.f_name, argbuf, 1, '|', sizeof flbuf.f_name);
2237         lputfloor(&flbuf, floor_num);
2238
2239         cprintf("%d Ok\n", CIT_OK);
2240 }
2241
2242
2243 /*****************************************************************************/
2244 /*                      MODULE INITIALIZATION STUFF                          */
2245 /*****************************************************************************/
2246
2247 CTDL_MODULE_INIT(room_ops)
2248 {
2249         if (!threading) {
2250                 CtdlRegisterProtoHook(cmd_lrms, "LRMS", "Autoconverted. TODO: document me.");
2251                 CtdlRegisterProtoHook(cmd_lkra, "LKRA", "Autoconverted. TODO: document me.");
2252                 CtdlRegisterProtoHook(cmd_lkrn, "LKRN", "Autoconverted. TODO: document me.");
2253                 CtdlRegisterProtoHook(cmd_lkro, "LKRO", "Autoconverted. TODO: document me.");
2254                 CtdlRegisterProtoHook(cmd_lzrm, "LZRM", "Autoconverted. TODO: document me.");
2255                 CtdlRegisterProtoHook(cmd_lprm, "LPRM", "Autoconverted. TODO: document me.");
2256                 CtdlRegisterProtoHook(cmd_goto, "GOTO", "Autoconverted. TODO: document me.");
2257                 CtdlRegisterProtoHook(cmd_whok, "WHOK", "Autoconverted. TODO: document me.");
2258                 CtdlRegisterProtoHook(cmd_rdir, "RDIR", "Autoconverted. TODO: document me.");
2259                 CtdlRegisterProtoHook(cmd_getr, "GETR", "Autoconverted. TODO: document me.");
2260                 CtdlRegisterProtoHook(cmd_setr, "SETR", "Autoconverted. TODO: document me.");
2261                 CtdlRegisterProtoHook(cmd_geta, "GETA", "Autoconverted. TODO: document me.");
2262                 CtdlRegisterProtoHook(cmd_seta, "SETA", "Autoconverted. TODO: document me.");
2263                 CtdlRegisterProtoHook(cmd_rinf, "RINF", "Autoconverted. TODO: document me.");
2264                 CtdlRegisterProtoHook(cmd_kill, "KILL", "Autoconverted. TODO: document me.");
2265                 CtdlRegisterProtoHook(cmd_cre8, "CRE8", "Autoconverted. TODO: document me.");
2266                 CtdlRegisterProtoHook(cmd_einf, "EINF", "Autoconverted. TODO: document me.");
2267                 CtdlRegisterProtoHook(cmd_lflr, "LFLR", "Autoconverted. TODO: document me.");
2268                 CtdlRegisterProtoHook(cmd_cflr, "CFLR", "Autoconverted. TODO: document me.");
2269                 CtdlRegisterProtoHook(cmd_kflr, "KFLR", "Autoconverted. TODO: document me.");
2270                 CtdlRegisterProtoHook(cmd_eflr, "EFLR", "Autoconverted. TODO: document me.");
2271         }
2272         /* return our Subversion id for the Log */
2273         return "room_ops";
2274 }