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