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