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