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