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