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