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