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