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