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