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