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