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