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