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