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