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