* Changed the comments at the beginning of each file to a consistent format
[citadel.git] / citadel / room_ops.c
1 /* 
2  * $Id$
3  * 
4  * Server functions which perform operations on room objects.
5  *
6  */
7 #include "sysdep.h"
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <sys/stat.h>
12 #include <string.h>
13 #include <time.h>
14 #include <limits.h>
15 #include <errno.h>
16 #include "citadel.h"
17 #include "server.h"
18 #include "database.h"
19 #include "config.h"
20 #include "room_ops.h"
21 #include "sysdep_decls.h"
22 #include "support.h"
23 #include "user_ops.h"
24 #include "msgbase.h"
25 #include "citserver.h"
26 #include "control.h"
27 #include "tools.h"
28
29 /*
30  * Generic routine for determining user access to rooms
31  */
32 int CtdlRoomAccess(struct quickroom *roombuf, struct usersupp *userbuf)
33 {
34         int retval = 0;
35         struct visit vbuf;
36
37         /* for internal programs, always do everything */
38         if (((CC->internal_pgm)) && (roombuf->QRflags & QR_INUSE)) {
39                 return (UA_KNOWN | UA_GOTOALLOWED);
40         }
41         /* For mailbox rooms, only allow access to the owner */
42         if (roombuf->QRflags & QR_MAILBOX) {
43                 if (userbuf->usernum != atol(roombuf->QRname)) {
44                         return (retval);
45                 }
46         }
47         /* Locate any applicable user/room relationships */
48         CtdlGetRelationship(&vbuf, userbuf, roombuf);
49
50         /* Force the properties of the Aide room */
51         if (!strcasecmp(roombuf->QRname, AIDEROOM)) {
52                 if (userbuf->axlevel >= 6) {
53                         retval = UA_KNOWN | UA_GOTOALLOWED;
54                 } else {
55                         retval = 0;
56                 }
57                 goto NEWMSG;
58         }
59         /* For mailboxes, we skip all the access stuff (and we've
60          * already checked by this point that the mailbox belongs
61          * to the user)
62          */
63         if (roombuf->QRflags & QR_MAILBOX) {
64                 retval = UA_KNOWN | UA_GOTOALLOWED;
65                 goto NEWMSG;
66         }
67         /* If this is a public room, it's accessible... */
68         if ((roombuf->QRflags & QR_PRIVATE) == 0) {
69                 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
70         }
71         /* If this is a preferred users only room, check access level */
72         if (roombuf->QRflags & QR_PREFONLY) {
73                 if (userbuf->axlevel < 5) {
74                         retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
75                 }
76         }
77         /* For private rooms, check the generation number matchups */
78         if (roombuf->QRflags & QR_PRIVATE) {
79
80                 /* An explicit match means the user belongs in this room */
81                 if (vbuf.v_flags & V_ACCESS) {
82                         retval = retval | UA_KNOWN | UA_GOTOALLOWED;
83                 }
84                 /* Otherwise, check if this is a guess-name or passworded
85                  * room.  If it is, a goto may at least be attempted
86                  */
87                 else if ((roombuf->QRflags & QR_PRIVATE)
88                          || (roombuf->QRflags & QR_PASSWORDED)) {
89                         retval = retval & ~UA_KNOWN;
90                         retval = retval | UA_GOTOALLOWED;
91                 }
92         }
93         /* Check to see if the user has forgotten this room */
94         if (vbuf.v_flags & V_FORGET) {
95                 retval = retval & ~UA_KNOWN;
96                 retval = retval | UA_ZAPPED;
97         }
98         /* If user is explicitly locked out of this room, deny everything */
99         if (vbuf.v_flags & V_LOCKOUT) {
100                 retval = retval & ~UA_KNOWN & ~UA_GOTOALLOWED;
101         }
102         /* Aides get access to everything */
103         if (userbuf->axlevel >= 6) {
104                 retval = retval | UA_KNOWN | UA_GOTOALLOWED;
105                 retval = retval & ~UA_ZAPPED;
106         }
107       NEWMSG:                   /* By the way, we also check for the presence of new messages */
108         if ((roombuf->QRhighest) > (vbuf.v_lastseen)) {
109                 retval = retval | UA_HASNEWMSGS;
110         }
111         return (retval);
112 }
113
114 /*
115  * Self-checking stuff for a room record read into memory
116  */
117 void room_sanity_check(struct quickroom *qrbuf)
118 {
119         /* Mailbox rooms are always on the lowest floor */
120         if (qrbuf->QRflags & QR_MAILBOX) {
121                 qrbuf->QRfloor = 0;
122         }
123         /* Listing order of 0 is illegal except for base rooms */
124         if (qrbuf->QRorder == 0)
125                 if (!is_noneditable(qrbuf))
126                         qrbuf->QRorder = 64;
127 }
128
129
130 /*
131  * getroom()  -  retrieve room data from disk
132  */
133 int getroom(struct quickroom *qrbuf, char *room_name)
134 {
135         struct cdbdata *cdbqr;
136         char lowercase_name[ROOMNAMELEN];
137         char personal_lowercase_name[ROOMNAMELEN];
138         int a;
139
140         for (a = 0; room_name[a] && a < sizeof lowercase_name - 1; ++a) {
141                 lowercase_name[a] = tolower(room_name[a]);
142         }
143         lowercase_name[a] = 0;
144
145         memset(qrbuf, 0, sizeof(struct quickroom));
146
147         /* First, try the public namespace */
148         cdbqr = cdb_fetch(CDB_QUICKROOM,
149                           lowercase_name, strlen(lowercase_name));
150
151         /* If that didn't work, try the user's personal namespace */
152         if (cdbqr == NULL) {
153                 sprintf(personal_lowercase_name, "%010ld.%s",
154                         CC->usersupp.usernum, lowercase_name);
155                 cdbqr = cdb_fetch(CDB_QUICKROOM,
156                                   personal_lowercase_name,
157                                   strlen(personal_lowercase_name));
158         }
159         if (cdbqr != NULL) {
160                 memcpy(qrbuf, cdbqr->ptr,
161                        ((cdbqr->len > sizeof(struct quickroom)) ?
162                         sizeof(struct quickroom) : cdbqr->len));
163                 cdb_free(cdbqr);
164
165                 room_sanity_check(qrbuf);
166
167                 return (0);
168         } else {
169                 return (1);
170         }
171 }
172
173 /*
174  * lgetroom()  -  same as getroom() but locks the record (if supported)
175  */
176 int lgetroom(struct quickroom *qrbuf, char *room_name)
177 {
178         register int retval;
179         retval = getroom(qrbuf, room_name);
180         if (retval == 0) begin_critical_section(S_QUICKROOM);
181         return(retval);
182 }
183
184
185 /*
186  * b_putroom()  -  back end to putroom() and b_deleteroom()
187  *              (if the supplied buffer is NULL, delete the room record)
188  */
189 void b_putroom(struct quickroom *qrbuf, char *room_name)
190 {
191         char lowercase_name[ROOMNAMELEN];
192         int a;
193
194         for (a = 0; a <= strlen(room_name); ++a) {
195                 lowercase_name[a] = tolower(room_name[a]);
196         }
197
198         if (qrbuf == NULL) {
199                 cdb_delete(CDB_QUICKROOM,
200                            lowercase_name, strlen(lowercase_name));
201         } else {
202                 time(&qrbuf->QRmtime);
203                 cdb_store(CDB_QUICKROOM,
204                           lowercase_name, strlen(lowercase_name),
205                           qrbuf, sizeof(struct quickroom));
206         }
207 }
208
209
210 /* 
211  * putroom()  -  store room data to disk
212  */
213 void putroom(struct quickroom *qrbuf) {
214         b_putroom(qrbuf, qrbuf->QRname);
215 }
216
217
218 /*
219  * b_deleteroom()  -  delete a room record from disk
220  */
221 void b_deleteroom(char *room_name) {
222         b_putroom(NULL, room_name);
223 }
224
225
226
227 /*
228  * lputroom()  -  same as putroom() but unlocks the record (if supported)
229  */
230 void lputroom(struct quickroom *qrbuf)
231 {
232
233         putroom(qrbuf);
234         end_critical_section(S_QUICKROOM);
235
236 }
237
238 /****************************************************************************/
239
240 /*
241  * getfloor()  -  retrieve floor data from disk
242  */
243 void getfloor(struct floor *flbuf, int floor_num)
244 {
245         struct cdbdata *cdbfl;
246
247         memset(flbuf, 0, sizeof(struct floor));
248         cdbfl = cdb_fetch(CDB_FLOORTAB, &floor_num, sizeof(int));
249         if (cdbfl != NULL) {
250                 memcpy(flbuf, cdbfl->ptr,
251                        ((cdbfl->len > sizeof(struct floor)) ?
252                         sizeof(struct floor) : cdbfl->len));
253                 cdb_free(cdbfl);
254         } else {
255                 if (floor_num == 0) {
256                         strcpy(flbuf->f_name, "Main Floor");
257                         flbuf->f_flags = F_INUSE;
258                         flbuf->f_ref_count = 3;
259                 }
260         }
261
262 }
263
264 /*
265  * lgetfloor()  -  same as getfloor() but locks the record (if supported)
266  */
267 void lgetfloor(struct floor *flbuf, int floor_num)
268 {
269
270         begin_critical_section(S_FLOORTAB);
271         getfloor(flbuf, floor_num);
272 }
273
274
275 /*
276  * putfloor()  -  store floor data on disk
277  */
278 void putfloor(struct floor *flbuf, int floor_num)
279 {
280         cdb_store(CDB_FLOORTAB, &floor_num, sizeof(int),
281                   flbuf, sizeof(struct floor));
282 }
283
284
285 /*
286  * lputfloor()  -  same as putfloor() but unlocks the record (if supported)
287  */
288 void lputfloor(struct floor *flbuf, int floor_num)
289 {
290
291         putfloor(flbuf, floor_num);
292         end_critical_section(S_FLOORTAB);
293
294 }
295
296
297 /* 
298  *  Traverse the room file...
299  */
300 void ForEachRoom(void (*CallBack) (struct quickroom *EachRoom, void *out_data),
301                 void *in_data)
302 {
303         struct quickroom qrbuf;
304         struct cdbdata *cdbqr;
305
306         cdb_rewind(CDB_QUICKROOM);
307
308         while (cdbqr = cdb_next_item(CDB_QUICKROOM), cdbqr != NULL) {
309                 memset(&qrbuf, 0, sizeof(struct quickroom));
310                 memcpy(&qrbuf, cdbqr->ptr,
311                        ((cdbqr->len > sizeof(struct quickroom)) ?
312                         sizeof(struct quickroom) : cdbqr->len));
313                 cdb_free(cdbqr);
314                 room_sanity_check(&qrbuf);
315                 if (qrbuf.QRflags & QR_INUSE)
316                         (*CallBack)(&qrbuf, in_data);
317         }
318 }
319
320
321 /*
322  * delete_msglist()  -  delete room message pointers
323  */
324 void delete_msglist(struct quickroom *whichroom)
325 {
326         struct cdbdata *cdbml;
327
328         /* Make sure the msglist we're deleting actually exists, otherwise
329          * gdbm will complain when we try to delete an invalid record
330          */
331         cdbml = cdb_fetch(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
332         if (cdbml != NULL) {
333                 cdb_free(cdbml);
334
335                 /* Go ahead and delete it */
336                 cdb_delete(CDB_MSGLISTS, &whichroom->QRnumber, sizeof(long));
337         }
338 }
339
340
341
342
343 /*
344  * sort message pointers
345  * (returns new msg count)
346  */
347 int sort_msglist(long listptrs[], int oldcount)
348 {
349         int a, b;
350         long hold1, hold2;
351         int numitems;
352
353         numitems = oldcount;
354         if (numitems < 2)
355                 return (oldcount);
356
357         /* do the sort */
358         for (a = numitems - 2; a >= 0; --a) {
359                 for (b = 0; b <= a; ++b) {
360                         if (listptrs[b] > (listptrs[b + 1])) {
361                                 hold1 = listptrs[b];
362                                 hold2 = listptrs[b + 1];
363                                 listptrs[b] = hold2;
364                                 listptrs[b + 1] = hold1;
365                         }
366                 }
367         }
368
369         /* and yank any nulls */
370         while ((numitems > 0) && (listptrs[0] == 0L)) {
371                 memcpy(&listptrs[0], &listptrs[1],
372                        (sizeof(long) * (numitems - 1)));
373                 --numitems;
374         }
375
376         return (numitems);
377 }
378
379
380 /*
381  * Determine whether a given room is non-editable.
382  */
383 int is_noneditable(struct quickroom *qrbuf)
384 {
385
386         /* Lobby> and Aide> are non-editable */
387         if (!strcasecmp(qrbuf->QRname, BASEROOM))
388                 return (1);
389         else if (!strcasecmp(qrbuf->QRname, AIDEROOM))
390                 return (1);
391
392         /* Mail> rooms are also non-editable */
393         else if ( (qrbuf->QRflags & QR_MAILBOX)
394              && (!strcasecmp(&qrbuf->QRname[11], MAILROOM)) )
395                 return (1);
396
397         /* Everything else is editable */
398         else
399                 return (0);
400 }
401
402
403
404 /*
405  * Back-back-end for all room listing commands
406  */
407 void list_roomname(struct quickroom *qrbuf)
408 {
409         char truncated_roomname[ROOMNAMELEN];
410
411         /* For mailbox rooms, chop off the owner prefix */
412         if (qrbuf->QRflags & QR_MAILBOX) {
413                 strcpy(truncated_roomname, qrbuf->QRname);
414                 strcpy(truncated_roomname, &truncated_roomname[11]);
415                 cprintf("%s", truncated_roomname);
416         }
417         /* For all other rooms, just display the name in its entirety */
418         else {
419                 cprintf("%s", qrbuf->QRname);
420         }
421
422         /* ...and now the other parameters */
423         cprintf("|%u|%d|%d\n",
424                 qrbuf->QRflags,
425                 (int) qrbuf->QRfloor,
426                 (int) qrbuf->QRorder);
427 }
428
429
430 /* 
431  * cmd_lrms()   -  List all accessible rooms, known or forgotten
432  */
433 void cmd_lrms_backend(struct quickroom *qrbuf, void *data)
434 {
435         int FloorBeingSearched = (-1);
436         FloorBeingSearched = *(int *)data;
437
438         if (((CtdlRoomAccess(qrbuf, &CC->usersupp)
439               & (UA_KNOWN | UA_ZAPPED)))
440             && ((qrbuf->QRfloor == (FloorBeingSearched))
441                 || ((FloorBeingSearched) < 0)))
442                 list_roomname(qrbuf);
443 }
444
445 void cmd_lrms(char *argbuf)
446 {
447         int FloorBeingSearched = (-1);
448         if (strlen(argbuf) > 0)
449                 FloorBeingSearched = extract_int(argbuf, 0);
450
451         if (CtdlAccessCheck(ac_logged_in)) return;
452
453         if (getuser(&CC->usersupp, CC->curr_user)) {
454                 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
455                 return;
456         }
457         cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
458
459         ForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
460         cprintf("000\n");
461 }
462
463
464
465 /* 
466  * cmd_lkra()   -  List all known rooms
467  */
468 void cmd_lkra_backend(struct quickroom *qrbuf, void *data)
469 {
470         int FloorBeingSearched = (-1);
471         FloorBeingSearched = *(int *)data;
472
473         if (((CtdlRoomAccess(qrbuf, &CC->usersupp)
474               & (UA_KNOWN)))
475             && ((qrbuf->QRfloor == (FloorBeingSearched))
476                 || ((FloorBeingSearched) < 0)))
477                 list_roomname(qrbuf);
478 }
479
480 void cmd_lkra(char *argbuf)
481 {
482         int FloorBeingSearched = (-1);
483         if (strlen(argbuf) > 0)
484                 FloorBeingSearched = extract_int(argbuf, 0);
485
486         if (CtdlAccessCheck(ac_logged_in)) return;
487         
488         if (getuser(&CC->usersupp, CC->curr_user)) {
489                 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
490                 return;
491         }
492         cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
493
494         ForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
495         cprintf("000\n");
496 }
497
498
499
500 /* 
501  * cmd_lkrn()   -  List all known rooms with new messages
502  */
503 void cmd_lkrn_backend(struct quickroom *qrbuf, void *data)
504 {
505         int ra;
506         int FloorBeingSearched = (-1);
507         FloorBeingSearched = *(int *)data;
508
509         ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
510         if ((ra & UA_KNOWN)
511             && (ra & UA_HASNEWMSGS)
512             && ((qrbuf->QRfloor == (FloorBeingSearched))
513                 || ((FloorBeingSearched) < 0)))
514                 list_roomname(qrbuf);
515 }
516
517 void cmd_lkrn(char *argbuf)
518 {
519         int FloorBeingSearched = (-1);
520         if (strlen(argbuf) > 0)
521                 FloorBeingSearched = extract_int(argbuf, 0);
522
523         if (CtdlAccessCheck(ac_logged_in)) return;
524         
525         if (getuser(&CC->usersupp, CC->curr_user)) {
526                 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
527                 return;
528         }
529         cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
530
531         ForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
532         cprintf("000\n");
533 }
534
535
536
537 /* 
538  * cmd_lkro()   -  List all known rooms
539  */
540 void cmd_lkro_backend(struct quickroom *qrbuf, void *data)
541 {
542         int ra;
543         int FloorBeingSearched = (-1);
544         FloorBeingSearched = *(int *)data;
545
546         ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
547         if ((ra & UA_KNOWN)
548             && ((ra & UA_HASNEWMSGS) == 0)
549             && ((qrbuf->QRfloor == (FloorBeingSearched))
550                 || ((FloorBeingSearched) < 0)))
551                 list_roomname(qrbuf);
552 }
553
554 void cmd_lkro(char *argbuf)
555 {
556         int FloorBeingSearched = (-1);
557         if (strlen(argbuf) > 0)
558                 FloorBeingSearched = extract_int(argbuf, 0);
559
560         if (CtdlAccessCheck(ac_logged_in)) return;
561         
562         if (getuser(&CC->usersupp, CC->curr_user)) {
563                 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
564                 return;
565         }
566         cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
567
568         ForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
569         cprintf("000\n");
570 }
571
572
573
574 /* 
575  * cmd_lzrm()   -  List all forgotten rooms
576  */
577 void cmd_lzrm_backend(struct quickroom *qrbuf, void *data)
578 {
579         int ra;
580         int FloorBeingSearched = (-1);
581         FloorBeingSearched = *(int *)data;
582
583         ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
584         if ((ra & UA_GOTOALLOWED)
585             && (ra & UA_ZAPPED)
586             && ((qrbuf->QRfloor == (FloorBeingSearched))
587                 || ((FloorBeingSearched) < 0)))
588                 list_roomname(qrbuf);
589 }
590
591 void cmd_lzrm(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->usersupp, CC->curr_user)) {
600                 cprintf("%d Can't locate user!\n", ERROR + INTERNAL_ERROR);
601                 return;
602         }
603         cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
604
605         ForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
606         cprintf("000\n");
607 }
608
609
610
611 void usergoto(char *where, int display_result, int *retmsgs, int *retnew)
612 {
613         int a;
614         int new_messages = 0;
615         int total_messages = 0;
616         int info = 0;
617         int rmailflag;
618         int raideflag;
619         int newmailcount = 0;
620         struct visit vbuf;
621         char truncated_roomname[ROOMNAMELEN];
622         struct cdbdata *cdbfr;
623         long *msglist = NULL;
624         int num_msgs = 0;
625
626         strcpy(CC->quickroom.QRname, where);
627         getroom(&CC->quickroom, where);
628
629         lgetuser(&CC->usersupp, CC->curr_user);
630         CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
631
632         /* Know the room ... but not if it's the page log room */
633         if (strcasecmp(where, config.c_logpages)) {
634                 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
635                 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
636         }
637         CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
638         lputuser(&CC->usersupp);
639
640         /* check for new mail */
641         newmailcount = NewMailCount();
642
643         /* set info to 1 if the user needs to read the room's info file */
644         if (CC->quickroom.QRinfo > vbuf.v_lastseen)
645                 info = 1;
646
647         get_mm();
648         cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->quickroom.QRnumber, sizeof(long));
649         if (cdbfr != NULL) {
650                 msglist = mallok(cdbfr->len);
651                 memcpy(msglist, cdbfr->ptr, cdbfr->len);
652                 num_msgs = cdbfr->len / sizeof(long);
653                 cdb_free(cdbfr);
654         }
655
656         if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
657                 if (msglist[a] > 0L) {
658                         ++total_messages;
659                         if (msglist[a] > vbuf.v_lastseen) {
660                                 ++new_messages;
661                         }
662                 }
663         }
664
665         if (msglist != NULL) phree(msglist);
666
667         if (CC->quickroom.QRflags & QR_MAILBOX)
668                 rmailflag = 1;
669         else
670                 rmailflag = 0;
671
672         if ((CC->quickroom.QRroomaide == CC->usersupp.usernum)
673             || (CC->usersupp.axlevel >= 6))
674                 raideflag = 1;
675         else
676                 raideflag = 0;
677
678         strcpy(truncated_roomname, CC->quickroom.QRname);
679         if (CC->quickroom.QRflags & QR_MAILBOX) {
680                 strcpy(truncated_roomname, &truncated_roomname[11]);
681         }
682
683         if (retmsgs != NULL) *retmsgs = total_messages;
684         if (retnew != NULL) *retnew = new_messages;
685         lprintf(9, "<%s> %d new of %d total messages\n",
686                 CC->quickroom.QRname, new_messages, total_messages);
687
688         if (display_result)
689                 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d\n",
690                         OK, CtdlCheckExpress(),
691                         truncated_roomname,
692                         new_messages, total_messages,
693                         info, CC->quickroom.QRflags,
694                         CC->quickroom.QRhighest,
695                         vbuf.v_lastseen,
696                         rmailflag, raideflag, newmailcount,
697                         CC->quickroom.QRfloor);
698
699 }
700
701
702 /* 
703  * cmd_goto()  -  goto a new room
704  */
705 void cmd_goto(char *gargs)
706 {
707         struct quickroom QRscratch;
708         int c;
709         int ok = 0;
710         int ra;
711         char augmented_roomname[256];
712         char towhere[256];
713         char password[256];
714
715         if (CtdlAccessCheck(ac_logged_in)) return;
716
717         extract(towhere, gargs, 0);
718         extract(password, gargs, 1);
719
720         getuser(&CC->usersupp, CC->curr_user);
721
722         if (!strcasecmp(towhere, "_BASEROOM_"))
723                 strcpy(towhere, BASEROOM);
724
725         if (!strcasecmp(towhere, "_MAIL_"))
726                 strcpy(towhere, MAILROOM);
727
728         if (!strcasecmp(towhere, "_BITBUCKET_"))
729                 strcpy(towhere, config.c_twitroom);
730
731
732         /* First try a regular match */
733         c = getroom(&QRscratch, towhere);
734
735         /* Then try a mailbox name match */
736         if (c != 0) {
737                 MailboxName(augmented_roomname, &CC->usersupp, towhere);
738                 c = getroom(&QRscratch, augmented_roomname);
739                 if (c == 0)
740                         strcpy(towhere, augmented_roomname);
741         }
742
743         /* And if the room was found... */
744         if (c == 0) {
745
746                 /* let internal programs go directly to any room */
747                 if (CC->internal_pgm) {
748                         usergoto(towhere, 1, NULL, NULL);
749                         return;
750                 }
751
752                 /* See if there is an existing user/room relationship */
753                 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
754
755                 /* normal clients have to pass through security */
756                 if (ra & UA_GOTOALLOWED)
757                         ok = 1;
758
759                 if (ok == 1) {
760                         if ((QRscratch.QRflags & QR_PASSWORDED) &&
761                             ((ra & UA_KNOWN) == 0) &&
762                             (strcasecmp(QRscratch.QRpasswd, password))
763                             ) {
764                                 cprintf("%d wrong or missing passwd\n",
765                                         ERROR + PASSWORD_REQUIRED);
766                                 return;
767                         } else if ((QRscratch.QRflags & QR_PRIVATE) &&
768                                    ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
769                                    ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
770                                    ((ra & UA_KNOWN) == 0)) {
771                                 goto NOPE;
772                         } else {
773                                 usergoto(towhere, 1, NULL, NULL);
774                                 return;
775                         }
776                 }
777         }
778
779 NOPE:   cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
780 }
781
782
783 void cmd_whok(void)
784 {
785         struct usersupp temp;
786         struct cdbdata *cdbus;
787
788         getuser(&CC->usersupp, CC->curr_user);
789         if (CtdlAccessCheck(ac_room_aide)) return;
790
791         cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
792         cdb_rewind(CDB_USERSUPP);
793         while (cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
794                 memset(&temp, 0, sizeof temp);
795                 memcpy(&temp, cdbus->ptr, sizeof temp);
796                 cdb_free(cdbus);
797
798                 if ((CC->quickroom.QRflags & QR_INUSE)
799                     && (CtdlRoomAccess(&CC->quickroom, &temp) & UA_KNOWN)
800                     )
801                         cprintf("%s\n", temp.fullname);
802         }
803         cprintf("000\n");
804 }
805
806
807 /*
808  * RDIR command for room directory
809  */
810 void cmd_rdir(void)
811 {
812         char buf[256];
813         char flnm[256];
814         char comment[256];
815         FILE *ls, *fd;
816         struct stat statbuf;
817
818         if (CtdlAccessCheck(ac_logged_in)) return;
819         
820         getroom(&CC->quickroom, CC->quickroom.QRname);
821         getuser(&CC->usersupp, CC->curr_user);
822
823         if ((CC->quickroom.QRflags & QR_DIRECTORY) == 0) {
824                 cprintf("%d not here.\n", ERROR + NOT_HERE);
825                 return;
826         }
827         if (((CC->quickroom.QRflags & QR_VISDIR) == 0)
828             && (CC->usersupp.axlevel < 6)
829             && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
830                 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
831                 return;
832         }
833         cprintf("%d %s|%s/files/%s\n",
834         LISTING_FOLLOWS, config.c_fqdn, BBSDIR, CC->quickroom.QRdirname);
835
836         sprintf(buf, "ls %s/files/%s  >%s 2> /dev/null",
837                 BBSDIR, CC->quickroom.QRdirname, CC->temp);
838         system(buf);
839
840         sprintf(buf, "%s/files/%s/filedir", BBSDIR, CC->quickroom.QRdirname);
841         fd = fopen(buf, "r");
842         if (fd == NULL)
843                 fd = fopen("/dev/null", "r");
844
845         ls = fopen(CC->temp, "r");
846         while (fgets(flnm, 256, ls) != NULL) {
847                 flnm[strlen(flnm) - 1] = 0;
848                 if (strcasecmp(flnm, "filedir")) {
849                         sprintf(buf, "%s/files/%s/%s",
850                                 BBSDIR, CC->quickroom.QRdirname, flnm);
851                         stat(buf, &statbuf);
852                         strcpy(comment, "");
853                         fseek(fd, 0L, 0);
854                         while ((fgets(buf, 256, fd) != NULL)
855                                && (strlen(comment) == 0)) {
856                                 buf[strlen(buf) - 1] = 0;
857                                 if ((!strncasecmp(buf, flnm, strlen(flnm)))
858                                     && (buf[strlen(flnm)] == ' '))
859                                         safestrncpy(comment,
860                                             &buf[strlen(flnm) + 1],
861                                             sizeof comment);
862                         }
863                         cprintf("%s|%ld|%s\n", flnm, statbuf.st_size, comment);
864                 }
865         }
866         fclose(ls);
867         fclose(fd);
868         unlink(CC->temp);
869
870         cprintf("000\n");
871 }
872
873 /*
874  * get room parameters (aide or room aide command)
875  */
876 void cmd_getr(void)
877 {
878         if (CtdlAccessCheck(ac_room_aide)) return;
879
880         /********
881         if (is_noneditable(&CC->quickroom)) {
882                 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
883                 return;
884         }
885         ************/
886
887         getroom(&CC->quickroom, CC->quickroom.QRname);
888         cprintf("%d%c%s|%s|%s|%d|%d|%d\n",
889                 OK, CtdlCheckExpress(),
890                 CC->quickroom.QRname,
891                 ((CC->quickroom.QRflags & QR_PASSWORDED) ? CC->quickroom.QRpasswd : ""),
892                 ((CC->quickroom.QRflags & QR_DIRECTORY) ? CC->quickroom.QRdirname : ""),
893                 CC->quickroom.QRflags,
894                 (int) CC->quickroom.QRfloor,
895                 (int) CC->quickroom.QRorder);
896 }
897
898
899 /*
900  * set room parameters (aide or room aide command)
901  */
902 void cmd_setr(char *args)
903 {
904         char buf[256];
905         struct floor flbuf;
906         char old_name[ROOMNAMELEN];
907         int old_floor;
908         int new_order = 0;
909         int ne = 0;
910
911         if (CtdlAccessCheck(ac_room_aide)) return;
912
913         if (is_noneditable(&CC->quickroom)) {
914                 ne = 1;
915         }
916
917         /***
918                 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
919                 return;
920         }
921         ***/
922
923
924         if (num_parms(args) >= 6) {
925                 getfloor(&flbuf, extract_int(args, 5));
926                 if ((flbuf.f_flags & F_INUSE) == 0) {
927                         cprintf("%d Invalid floor number.\n",
928                                 ERROR + INVALID_FLOOR_OPERATION);
929                         return;
930                 }
931         }
932         if (num_parms(args) >= 7) {
933                 new_order = extract_int(args, 6);
934                 if (new_order < 1)
935                         new_order = 1;
936                 if (new_order > 127)
937                         new_order = 127;
938         }
939         lgetroom(&CC->quickroom, CC->quickroom.QRname);
940
941         /* Non-editable base rooms can't be renamed */
942         strcpy(old_name, CC->quickroom.QRname);
943         if (!ne) {
944                 extract(buf, args, 0);
945                 buf[ROOMNAMELEN] = 0;
946                 safestrncpy(CC->quickroom.QRname, buf,
947                         sizeof CC->quickroom.QRname);
948         }
949
950         extract(buf, args, 1);
951         buf[10] = 0;
952         safestrncpy(CC->quickroom.QRpasswd, buf, sizeof CC->quickroom.QRpasswd);
953         extract(buf, args, 2);
954         buf[15] = 0;
955         safestrncpy(CC->quickroom.QRdirname, buf,
956                 sizeof CC->quickroom.QRdirname);
957         CC->quickroom.QRflags = (extract_int(args, 3) | QR_INUSE);
958         if (num_parms(args) >= 7)
959                 CC->quickroom.QRorder = (char) new_order;
960
961         /* Clean up a client boo-boo: if the client set the room to
962          * guess-name or passworded, ensure that the private flag is
963          * also set.
964          */
965         if ((CC->quickroom.QRflags & QR_GUESSNAME)
966             || (CC->quickroom.QRflags & QR_PASSWORDED))
967                 CC->quickroom.QRflags |= QR_PRIVATE;
968
969         /* Kick everyone out if the client requested it (by changing the
970          * room's generation number)
971          */
972         if (extract_int(args, 4)) {
973                 time(&CC->quickroom.QRgen);
974         }
975         old_floor = CC->quickroom.QRfloor;
976         if (num_parms(args) >= 6) {
977                 CC->quickroom.QRfloor = extract_int(args, 5);
978         }
979         /* Write the room record back to disk */
980         lputroom(&CC->quickroom);
981
982         /* If the room name changed, then there are now two room records,
983          * so we have to delete the old one.
984          */
985         if (strcasecmp(CC->quickroom.QRname, old_name)) {
986                 b_deleteroom(old_name);
987         }
988         /* adjust the floor reference counts */
989         lgetfloor(&flbuf, old_floor);
990         --flbuf.f_ref_count;
991         lputfloor(&flbuf, old_floor);
992         lgetfloor(&flbuf, CC->quickroom.QRfloor);
993         ++flbuf.f_ref_count;
994         lputfloor(&flbuf, CC->quickroom.QRfloor);
995
996         /* create a room directory if necessary */
997         if (CC->quickroom.QRflags & QR_DIRECTORY) {
998                 sprintf(buf,
999                     "mkdir ./files/%s </dev/null >/dev/null 2>/dev/null",
1000                         CC->quickroom.QRdirname);
1001                 system(buf);
1002         }
1003         sprintf(buf, "%s> edited by %s\n", CC->quickroom.QRname, CC->curr_user);
1004         aide_message(buf);
1005         cprintf("%d Ok\n", OK);
1006 }
1007
1008
1009
1010 /* 
1011  * get the name of the room aide for this room
1012  */
1013 void cmd_geta(void)
1014 {
1015         struct usersupp usbuf;
1016
1017         if (CtdlAccessCheck(ac_logged_in)) return;
1018
1019         if (is_noneditable(&CC->quickroom)) {
1020                 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
1021                 return;
1022         }
1023         if (getuserbynumber(&usbuf, CC->quickroom.QRroomaide) == 0) {
1024                 cprintf("%d %s\n", OK, usbuf.fullname);
1025         } else {
1026                 cprintf("%d \n", OK);
1027         }
1028 }
1029
1030
1031 /* 
1032  * set the room aide for this room
1033  */
1034 void cmd_seta(char *new_ra)
1035 {
1036         struct usersupp usbuf;
1037         long newu;
1038         char buf[256];
1039         int post_notice;
1040
1041         if (CtdlAccessCheck(ac_room_aide)) return;
1042
1043         if (getuser(&usbuf, new_ra) != 0) {
1044                 newu = (-1L);
1045         } else {
1046                 newu = usbuf.usernum;
1047         }
1048
1049         lgetroom(&CC->quickroom, CC->quickroom.QRname);
1050         post_notice = 0;
1051         if (CC->quickroom.QRroomaide != newu) {
1052                 post_notice = 1;
1053         }
1054         CC->quickroom.QRroomaide = newu;
1055         lputroom(&CC->quickroom);
1056
1057         /*
1058          * We have to post the change notice _after_ writing changes to 
1059          * the room table, otherwise it would deadlock!
1060          */
1061         if (post_notice == 1) {
1062                 sprintf(buf, "%s is now room aide for %s>\n",
1063                         usbuf.fullname, CC->quickroom.QRname);
1064                 aide_message(buf);
1065         }
1066         cprintf("%d Ok\n", OK);
1067 }
1068
1069 /*
1070  * Generate an associated file name for a room
1071  */
1072 void assoc_file_name(char *buf, struct quickroom *qrbuf, char *prefix)
1073 {
1074         sprintf(buf, "./%s/%ld", prefix, qrbuf->QRnumber);
1075 }
1076
1077 /* 
1078  * retrieve info file for this room
1079  */
1080 void cmd_rinf(void)
1081 {
1082         char filename[128];
1083         char buf[256];
1084         FILE *info_fp;
1085
1086         assoc_file_name(filename, &CC->quickroom, "info");
1087         info_fp = fopen(filename, "r");
1088
1089         if (info_fp == NULL) {
1090                 cprintf("%d No info file.\n", ERROR);
1091                 return;
1092         }
1093         cprintf("%d Info:\n", LISTING_FOLLOWS);
1094         while (fgets(buf, 256, info_fp) != NULL) {
1095                 if (strlen(buf) > 0)
1096                         buf[strlen(buf) - 1] = 0;
1097                 cprintf("%s\n", buf);
1098         }
1099         cprintf("000\n");
1100         fclose(info_fp);
1101 }
1102
1103 /*
1104  * Back end processing to delete a room and everything associated with it
1105  */
1106 void delete_room(struct quickroom *qrbuf)
1107 {
1108         struct floor flbuf;
1109         char filename[100];
1110
1111         lprintf(9, "Deleting room <%s>\n", qrbuf->QRname);
1112
1113         /* Delete the info file */
1114         assoc_file_name(filename, qrbuf, "info");
1115         unlink(filename);
1116
1117         /* Delete the image file */
1118         assoc_file_name(filename, qrbuf, "images");
1119         unlink(filename);
1120
1121         /* Delete the messages in the room
1122          * (Careful: this opens an S_QUICKROOM critical section!)
1123          */
1124         CtdlDeleteMessages(qrbuf->QRname, 0L, "");
1125
1126         /* Flag the room record as not in use */
1127         lgetroom(qrbuf, qrbuf->QRname);
1128         qrbuf->QRflags = 0;
1129         lputroom(qrbuf);
1130
1131         /* then decrement the reference count for the floor */
1132         lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1133         flbuf.f_ref_count = flbuf.f_ref_count - 1;
1134         lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1135
1136         /* Delete the room record from the database! */
1137         b_deleteroom(qrbuf->QRname);
1138 }
1139
1140
1141 /*
1142  * aide command: kill the current room
1143  */
1144 void cmd_kill(char *argbuf)
1145 {
1146         char aaa[100];
1147         char deleted_room_name[ROOMNAMELEN];
1148         int kill_ok;
1149
1150         kill_ok = extract_int(argbuf, 0);
1151
1152         if (CtdlAccessCheck(ac_room_aide)) return;
1153
1154         if (is_noneditable(&CC->quickroom)) {
1155                 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
1156                 return;
1157         }
1158         if (kill_ok) {
1159                 strcpy(deleted_room_name, CC->quickroom.QRname);
1160                 delete_room(&CC->quickroom);    /* Do the dirty work */
1161                 usergoto(BASEROOM, 0, NULL, NULL); /* Return to the Lobby */
1162
1163                 /* tell the world what we did */
1164                 sprintf(aaa, "%s> killed by %s\n",
1165                         deleted_room_name, CC->curr_user);
1166                 aide_message(aaa);
1167                 cprintf("%d '%s' deleted.\n", OK, deleted_room_name);
1168         } else {
1169                 cprintf("%d ok to delete.\n", OK);
1170         }
1171 }
1172
1173
1174 /*
1175  * Internal code to create a new room (returns room flags)
1176  *
1177  * Room types:  0=public, 1=guessname, 2=passworded, 3=inv-only, 4=mailbox
1178  */
1179 unsigned create_room(char *new_room_name,
1180                      int new_room_type,
1181                      char *new_room_pass,
1182                      int new_room_floor)
1183 {
1184
1185         struct quickroom qrbuf;
1186         struct floor flbuf;
1187         struct visit vbuf;
1188
1189         if (getroom(&qrbuf, new_room_name) == 0)
1190                 return (0);     /* already exists */
1191
1192         memset(&qrbuf, 0, sizeof(struct quickroom));
1193         safestrncpy(qrbuf.QRname, new_room_name, sizeof qrbuf.QRname);
1194         safestrncpy(qrbuf.QRpasswd, new_room_pass, sizeof qrbuf.QRpasswd);
1195         qrbuf.QRflags = QR_INUSE;
1196         qrbuf.QRnumber = get_new_room_number();
1197         if (new_room_type > 0)
1198                 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1199         if (new_room_type == 1)
1200                 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1201         if (new_room_type == 2)
1202                 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1203         if (new_room_type == 4)
1204                 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1205
1206         /* If the room is private, and the system administrator has elected
1207          * to automatically grant room aide privileges, do so now; otherwise,
1208          * set the room aide to undefined.
1209          */
1210         if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1211                 qrbuf.QRroomaide = CC->usersupp.usernum;
1212         } else {
1213                 qrbuf.QRroomaide = (-1L);
1214         }
1215
1216         qrbuf.QRhighest = 0L;   /* No messages in this room yet */
1217         time(&qrbuf.QRgen);     /* Use a timestamp as the generation number */
1218         qrbuf.QRfloor = new_room_floor;
1219
1220         /* save what we just did... */
1221         putroom(&qrbuf);
1222
1223         /* bump the reference count on whatever floor the room is on */
1224         lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1225         flbuf.f_ref_count = flbuf.f_ref_count + 1;
1226         lputfloor(&flbuf, (int) qrbuf.QRfloor);
1227
1228         /* be sure not to kick the creator out of the room! */
1229         lgetuser(&CC->usersupp, CC->curr_user);
1230         CtdlGetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1231         vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1232         vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1233         CtdlSetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1234         lputuser(&CC->usersupp);
1235
1236         /* resume our happy day */
1237         return (qrbuf.QRflags);
1238 }
1239
1240
1241 /*
1242  * create a new room
1243  */
1244 void cmd_cre8(char *args)
1245 {
1246         int cre8_ok;
1247         char new_room_name[256];
1248         int new_room_type;
1249         char new_room_pass[256];
1250         int new_room_floor;
1251         char aaa[256];
1252         unsigned newflags;
1253         struct quickroom qrbuf;
1254         struct floor flbuf;
1255
1256         cre8_ok = extract_int(args, 0);
1257         extract(new_room_name, args, 1);
1258         new_room_name[ROOMNAMELEN - 1] = 0;
1259         new_room_type = extract_int(args, 2);
1260         extract(new_room_pass, args, 3);
1261         new_room_pass[9] = 0;
1262         new_room_floor = 0;
1263
1264         if ((strlen(new_room_name) == 0) && (cre8_ok == 1)) {
1265                 cprintf("%d Invalid room name.\n", ERROR);
1266                 return;
1267         }
1268
1269         if (!strcasecmp(new_room_name, MAILROOM)) {
1270                 cprintf("%d '%s' already exists.\n",
1271                         ERROR + ALREADY_EXISTS, new_room_name);
1272                 return;
1273         }
1274
1275         if (num_parms(args) >= 5) {
1276                 getfloor(&flbuf, extract_int(args, 4));
1277                 if ((flbuf.f_flags & F_INUSE) == 0) {
1278                         cprintf("%d Invalid floor number.\n",
1279                                 ERROR + INVALID_FLOOR_OPERATION);
1280                         return;
1281                 } else {
1282                         new_room_floor = extract_int(args, 4);
1283                 }
1284         }
1285
1286         if (CtdlAccessCheck(ac_logged_in)) return;
1287
1288         if (CC->usersupp.axlevel < config.c_createax) {
1289                 cprintf("%d You need higher access to create rooms.\n",
1290                         ERROR + HIGHER_ACCESS_REQUIRED);
1291                 return;
1292         }
1293
1294         if ((strlen(new_room_name) == 0) && (cre8_ok == 0)) {
1295                 cprintf("%d Ok to create rooms.\n", OK);
1296                 return;
1297         }
1298
1299         if ((new_room_type < 0) || (new_room_type > 4)) {
1300                 cprintf("%d Invalid room type.\n", ERROR);
1301                 return;
1302         }
1303
1304         /* If the user is requesting a personal room, set up the room
1305          * name accordingly (prepend the user number)
1306          */
1307         if (new_room_type == 4) {
1308                 sprintf(aaa, "%010ld.%s",
1309                         CC->usersupp.usernum, new_room_name);
1310                 strcpy(new_room_name, aaa);
1311         }
1312
1313         /* Check to make sure the requested room name doesn't already exist */
1314         if (getroom(&qrbuf, new_room_name) == 0) {
1315                 cprintf("%d '%s' already exists.\n",
1316                         ERROR + ALREADY_EXISTS, qrbuf.QRname);
1317                 return;
1318         }
1319
1320         if (cre8_ok == 0) {
1321                 cprintf("%d OK to create '%s'\n", OK, new_room_name);
1322                 return;
1323         }
1324
1325         newflags = create_room(new_room_name,
1326                            new_room_type, new_room_pass, new_room_floor);
1327
1328         /* post a message in Aide> describing the new room */
1329         safestrncpy(aaa, new_room_name, sizeof aaa);
1330         strcat(aaa, "> created by ");
1331         strcat(aaa, CC->usersupp.fullname);
1332         if (newflags & QR_MAILBOX)
1333                 strcat(aaa, " [personal]");
1334         else if (newflags & QR_PRIVATE)
1335                 strcat(aaa, " [private]");
1336         if (newflags & QR_GUESSNAME)
1337                 strcat(aaa, "[guessname] ");
1338         if (newflags & QR_PASSWORDED) {
1339                 strcat(aaa, "\n Password: ");
1340                 strcat(aaa, new_room_pass);
1341         }
1342         strcat(aaa, "\n");
1343         aide_message(aaa);
1344
1345         cprintf("%d '%s' has been created.\n", OK, qrbuf.QRname);
1346 }
1347
1348
1349
1350 void cmd_einf(char *ok)
1351 {                               /* enter info file for current room */
1352         FILE *fp;
1353         char infofilename[256];
1354         char buf[256];
1355
1356         if (CtdlAccessCheck(ac_room_aide)) return;
1357
1358         if (atoi(ok) == 0) {
1359                 cprintf("%d Ok.\n", OK);
1360                 return;
1361         }
1362         assoc_file_name(infofilename, &CC->quickroom, "info");
1363         lprintf(9, "opening\n");
1364         fp = fopen(infofilename, "w");
1365         lprintf(9, "checking\n");
1366         if (fp == NULL) {
1367                 cprintf("%d Cannot open %s: %s\n",
1368                   ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1369                 return;
1370         }
1371         cprintf("%d Send info...\n", SEND_LISTING);
1372
1373         do {
1374                 client_gets(buf);
1375                 if (strcmp(buf, "000"))
1376                         fprintf(fp, "%s\n", buf);
1377         } while (strcmp(buf, "000"));
1378         fclose(fp);
1379
1380         /* now update the room index so people will see our new info */
1381         lgetroom(&CC->quickroom, CC->quickroom.QRname);         /* lock so no one steps on us */
1382         CC->quickroom.QRinfo = CC->quickroom.QRhighest + 1L;
1383         lputroom(&CC->quickroom);
1384 }
1385
1386
1387 /* 
1388  * cmd_lflr()   -  List all known floors
1389  */
1390 void cmd_lflr(void)
1391 {
1392         int a;
1393         struct floor flbuf;
1394
1395         if (CtdlAccessCheck(ac_logged_in)) return;
1396
1397         cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1398
1399         for (a = 0; a < MAXFLOORS; ++a) {
1400                 getfloor(&flbuf, a);
1401                 if (flbuf.f_flags & F_INUSE) {
1402                         cprintf("%d|%s|%d\n",
1403                                 a,
1404                                 flbuf.f_name,
1405                                 flbuf.f_ref_count);
1406                 }
1407         }
1408         cprintf("000\n");
1409 }
1410
1411
1412
1413 /*
1414  * create a new floor
1415  */
1416 void cmd_cflr(char *argbuf)
1417 {
1418         char new_floor_name[256];
1419         struct floor flbuf;
1420         int cflr_ok;
1421         int free_slot = (-1);
1422         int a;
1423
1424         extract(new_floor_name, argbuf, 0);
1425         cflr_ok = extract_int(argbuf, 1);
1426
1427
1428         if (CtdlAccessCheck(ac_aide)) return;
1429
1430         for (a = 0; a < MAXFLOORS; ++a) {
1431                 getfloor(&flbuf, a);
1432
1433                 /* note any free slots while we're scanning... */
1434                 if (((flbuf.f_flags & F_INUSE) == 0)
1435                     && (free_slot < 0))
1436                         free_slot = a;
1437
1438                 /* check to see if it already exists */
1439                 if ((!strcasecmp(flbuf.f_name, new_floor_name))
1440                     && (flbuf.f_flags & F_INUSE)) {
1441                         cprintf("%d Floor '%s' already exists.\n",
1442                                 ERROR + ALREADY_EXISTS,
1443                                 flbuf.f_name);
1444                         return;
1445                 }
1446         }
1447
1448         if (free_slot < 0) {
1449                 cprintf("%d There is no space available for a new floor.\n",
1450                         ERROR + INVALID_FLOOR_OPERATION);
1451                 return;
1452         }
1453         if (cflr_ok == 0) {
1454                 cprintf("%d ok to create...\n", OK);
1455                 return;
1456         }
1457         lgetfloor(&flbuf, free_slot);
1458         flbuf.f_flags = F_INUSE;
1459         flbuf.f_ref_count = 0;
1460         safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
1461         lputfloor(&flbuf, free_slot);
1462         cprintf("%d %d\n", OK, free_slot);
1463 }
1464
1465
1466
1467 /*
1468  * delete a floor
1469  */
1470 void cmd_kflr(char *argbuf)
1471 {
1472         struct floor flbuf;
1473         int floor_to_delete;
1474         int kflr_ok;
1475         int delete_ok;
1476
1477         floor_to_delete = extract_int(argbuf, 0);
1478         kflr_ok = extract_int(argbuf, 1);
1479
1480         if (CtdlAccessCheck(ac_aide)) return;
1481
1482         lgetfloor(&flbuf, floor_to_delete);
1483
1484         delete_ok = 1;
1485         if ((flbuf.f_flags & F_INUSE) == 0) {
1486                 cprintf("%d Floor %d not in use.\n",
1487                         ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
1488                 delete_ok = 0;
1489         } else {
1490                 if (flbuf.f_ref_count != 0) {
1491                         cprintf("%d Cannot delete; floor contains %d rooms.\n",
1492                                 ERROR + INVALID_FLOOR_OPERATION,
1493                                 flbuf.f_ref_count);
1494                         delete_ok = 0;
1495                 } else {
1496                         if (kflr_ok == 1) {
1497                                 cprintf("%d Ok\n", OK);
1498                         } else {
1499                                 cprintf("%d Ok to delete...\n", OK);
1500                         }
1501
1502                 }
1503
1504         }
1505
1506         if ((delete_ok == 1) && (kflr_ok == 1))
1507                 flbuf.f_flags = 0;
1508         lputfloor(&flbuf, floor_to_delete);
1509 }
1510
1511 /*
1512  * edit a floor
1513  */
1514 void cmd_eflr(char *argbuf)
1515 {
1516         struct floor flbuf;
1517         int floor_num;
1518         int np;
1519
1520         np = num_parms(argbuf);
1521         if (np < 1) {
1522                 cprintf("%d Usage error.\n", ERROR);
1523                 return;
1524         }
1525
1526         if (CtdlAccessCheck(ac_aide)) return;
1527
1528         floor_num = extract_int(argbuf, 0);
1529         lgetfloor(&flbuf, floor_num);
1530         if ((flbuf.f_flags & F_INUSE) == 0) {
1531                 lputfloor(&flbuf, floor_num);
1532                 cprintf("%d Floor %d is not in use.\n",
1533                         ERROR + INVALID_FLOOR_OPERATION, floor_num);
1534                 return;
1535         }
1536         if (np >= 2)
1537                 extract(flbuf.f_name, argbuf, 1);
1538         lputfloor(&flbuf, floor_num);
1539
1540         cprintf("%d Ok\n", OK);
1541 }