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