]> code.citadel.org Git - citadel.git/blob - citadel/room_ops.c
* eliminate redundant "name" parameter in [l]putuser(), now uses
[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);
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
779         if (display_result)
780                 cprintf("%d%c%s|%d|%d|%d|%d|%ld|%ld|%d|%d|%d|%d\n",
781                         OK, check_express(),
782                         truncated_roomname,
783                         new_messages, total_messages,
784                         info, CC->quickroom.QRflags,
785                         CC->quickroom.QRhighest,
786                         vbuf.v_lastseen,
787                         rmailflag, raideflag, newmailcount,
788                         CC->quickroom.QRfloor);
789
790         set_wtmpsupp_to_current_room();
791 }
792
793
794 /* 
795  * cmd_goto()  -  goto a new room
796  */
797 void cmd_goto(char *gargs)
798 {
799         struct quickroom QRscratch;
800         int c;
801         int ok = 0;
802         int ra;
803         char augmented_roomname[256];
804         char towhere[256];
805         char password[256];
806
807         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
808                 cprintf("%d not logged in\n", ERROR + NOT_LOGGED_IN);
809                 return;
810         }
811
812         extract(towhere, gargs, 0);
813         extract(password, gargs, 1);
814
815         getuser(&CC->usersupp, CC->curr_user);
816
817         if (!strcasecmp(towhere, "_BASEROOM_"))
818                 strcpy(towhere, BASEROOM);
819
820         if (!strcasecmp(towhere, "_MAIL_"))
821                 strcpy(towhere, MAILROOM);
822
823         if (!strcasecmp(towhere, "_BITBUCKET_"))
824                 strcpy(towhere, config.c_twitroom);
825
826
827         /* First try a regular match */
828         c = getroom(&QRscratch, towhere);
829
830         /* Then try a mailbox name match */
831         if (c != 0) {
832                 MailboxName(augmented_roomname, &CC->usersupp, towhere);
833                 c = getroom(&QRscratch, augmented_roomname);
834                 if (c == 0)
835                         strcpy(towhere, augmented_roomname);
836         }
837
838         /* And if the room was found... */
839         if (c == 0) {
840
841                 /* let internal programs go directly to any room */
842                 if (CC->internal_pgm) {
843                         usergoto(towhere, 1);
844                         return;
845                 }
846
847                 /* See if there is an existing user/room relationship */
848                 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
849
850                 /* normal clients have to pass through security */
851                 if (ra & UA_GOTOALLOWED)
852                         ok = 1;
853
854                 if (ok == 1) {
855                         if ((QRscratch.QRflags & QR_PASSWORDED) &&
856                             ((ra & UA_KNOWN) == 0) &&
857                             (strcasecmp(QRscratch.QRpasswd, password))
858                             ) {
859                                 cprintf("%d wrong or missing passwd\n",
860                                         ERROR + PASSWORD_REQUIRED);
861                                 return;
862                         } else if ((QRscratch.QRflags & QR_PRIVATE) &&
863                                    ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
864                                    ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
865                                    ((ra & UA_KNOWN) == 0)) {
866                                 goto NOPE;
867                         } else {
868                                 usergoto(towhere, 1);
869                                 return;
870                         }
871                 }
872         }
873
874 NOPE:   cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
875 }
876
877
878 void cmd_whok(void)
879 {
880         struct usersupp temp;
881         struct cdbdata *cdbus;
882
883         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
884                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
885                 return;
886         }
887         getuser(&CC->usersupp, CC->curr_user);
888
889         if ((!is_room_aide()) && (!(CC->internal_pgm))) {
890                 cprintf("%d Higher access required.\n",
891                         ERROR + HIGHER_ACCESS_REQUIRED);
892                 return;
893         }
894         cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
895         cdb_rewind(CDB_USERSUPP);
896         while (cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
897                 memset(&temp, 0, sizeof(struct usersupp));
898                 memcpy(&temp, cdbus->ptr, cdbus->len);
899                 cdb_free(cdbus);
900
901                 if ((CC->quickroom.QRflags & QR_INUSE)
902                     && (CtdlRoomAccess(&CC->quickroom, &temp) & UA_KNOWN)
903                     )
904                         cprintf("%s\n", temp.fullname);
905         }
906         cprintf("000\n");
907 }
908
909
910 /*
911  * RDIR command for room directory
912  */
913 void cmd_rdir(void)
914 {
915         char buf[256];
916         char flnm[256];
917         char comment[256];
918         FILE *ls, *fd;
919         struct stat statbuf;
920
921         if (!(CC->logged_in)) {
922                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
923                 return;
924         }
925         getroom(&CC->quickroom, CC->quickroom.QRname);
926         getuser(&CC->usersupp, CC->curr_user);
927
928         if ((CC->quickroom.QRflags & QR_DIRECTORY) == 0) {
929                 cprintf("%d not here.\n", ERROR + NOT_HERE);
930                 return;
931         }
932         if (((CC->quickroom.QRflags & QR_VISDIR) == 0)
933             && (CC->usersupp.axlevel < 6)
934             && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
935                 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
936                 return;
937         }
938         cprintf("%d %s|%s/files/%s\n",
939         LISTING_FOLLOWS, config.c_fqdn, BBSDIR, CC->quickroom.QRdirname);
940
941         sprintf(buf, "cd %s/files/%s; ls >%s 2>/dev/null",
942                 BBSDIR, CC->quickroom.QRdirname, CC->temp);
943         system(buf);
944
945         sprintf(buf, "%s/files/%s/filedir", BBSDIR, CC->quickroom.QRdirname);
946         fd = fopen(buf, "r");
947         if (fd == NULL)
948                 fd = fopen("/dev/null", "r");
949
950         ls = fopen(CC->temp, "r");
951         while (fgets(flnm, 256, ls) != NULL) {
952                 flnm[strlen(flnm) - 1] = 0;
953                 if (strcasecmp(flnm, "filedir")) {
954                         sprintf(buf, "%s/files/%s/%s",
955                                 BBSDIR, CC->quickroom.QRdirname, flnm);
956                         stat(buf, &statbuf);
957                         strcpy(comment, "");
958                         fseek(fd, 0L, 0);
959                         while ((fgets(buf, 256, fd) != NULL)
960                                && (strlen(comment) == 0)) {
961                                 buf[strlen(buf) - 1] = 0;
962                                 if ((!strncasecmp(buf, flnm, strlen(flnm)))
963                                     && (buf[strlen(flnm)] == ' '))
964                                         strncpy(comment,
965                                             &buf[strlen(flnm) + 1], 255);
966                         }
967                         cprintf("%s|%ld|%s\n", flnm, statbuf.st_size, comment);
968                 }
969         }
970         fclose(ls);
971         fclose(fd);
972         unlink(CC->temp);
973
974         cprintf("000\n");
975 }
976
977 /*
978  * get room parameters (aide or room aide command)
979  */
980 void cmd_getr(void)
981 {
982         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
983                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
984                 return;
985         }
986         if ((!is_room_aide()) && (!(CC->internal_pgm))) {
987                 cprintf("%d Higher access required.\n",
988                         ERROR + HIGHER_ACCESS_REQUIRED);
989                 return;
990         }
991         if (is_noneditable(&CC->quickroom)) {
992                 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
993                 return;
994         }
995         getroom(&CC->quickroom, CC->quickroom.QRname);
996         cprintf("%d%c%s|%s|%s|%d|%d|%d\n",
997                 OK, check_express(),
998                 CC->quickroom.QRname,
999                 ((CC->quickroom.QRflags & QR_PASSWORDED) ? CC->quickroom.QRpasswd : ""),
1000                 ((CC->quickroom.QRflags & QR_DIRECTORY) ? CC->quickroom.QRdirname : ""),
1001                 CC->quickroom.QRflags,
1002                 (int) CC->quickroom.QRfloor,
1003                 (int) CC->quickroom.QRorder);
1004 }
1005
1006
1007 /*
1008  * set room parameters (aide or room aide command)
1009  */
1010 void cmd_setr(char *args)
1011 {
1012         char buf[256];
1013         struct floor flbuf;
1014         char old_name[ROOMNAMELEN];
1015         int old_floor;
1016         int new_order = 0;
1017
1018         if (!(CC->logged_in)) {
1019                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1020                 return;
1021         }
1022         if (!is_room_aide()) {
1023                 cprintf("%d Higher access required.\n",
1024                         ERROR + HIGHER_ACCESS_REQUIRED);
1025                 return;
1026         }
1027         if (is_noneditable(&CC->quickroom)) {
1028                 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
1029                 return;
1030         }
1031         if (num_parms(args) >= 6) {
1032                 getfloor(&flbuf, extract_int(args, 5));
1033                 if ((flbuf.f_flags & F_INUSE) == 0) {
1034                         cprintf("%d Invalid floor number.\n",
1035                                 ERROR + INVALID_FLOOR_OPERATION);
1036                         return;
1037                 }
1038         }
1039         if (num_parms(args) >= 7) {
1040                 new_order = extract_int(args, 6);
1041                 if (new_order < 1)
1042                         new_order = 1;
1043                 if (new_order > 127)
1044                         new_order = 127;
1045         }
1046         lgetroom(&CC->quickroom, CC->quickroom.QRname);
1047         strcpy(old_name, CC->quickroom.QRname);
1048         extract(buf, args, 0);
1049         buf[ROOMNAMELEN] = 0;
1050         strncpy(CC->quickroom.QRname, buf, ROOMNAMELEN - 1);
1051         extract(buf, args, 1);
1052         buf[10] = 0;
1053         strncpy(CC->quickroom.QRpasswd, buf, 9);
1054         extract(buf, args, 2);
1055         buf[15] = 0;
1056         strncpy(CC->quickroom.QRdirname, buf, 19);
1057         CC->quickroom.QRflags = (extract_int(args, 3) | QR_INUSE);
1058         if (num_parms(args) >= 7)
1059                 CC->quickroom.QRorder = (char) new_order;
1060
1061         /* Clean up a client boo-boo: if the client set the room to
1062          * guess-name or passworded, ensure that the private flag is
1063          * also set.
1064          */
1065         if ((CC->quickroom.QRflags & QR_GUESSNAME)
1066             || (CC->quickroom.QRflags & QR_PASSWORDED))
1067                 CC->quickroom.QRflags |= QR_PRIVATE;
1068
1069         /* Kick everyone out if the client requested it (by changing the
1070          * room's generation number)
1071          */
1072         if (extract_int(args, 4)) {
1073                 time(&CC->quickroom.QRgen);
1074         }
1075         old_floor = CC->quickroom.QRfloor;
1076         if (num_parms(args) >= 6) {
1077                 CC->quickroom.QRfloor = extract_int(args, 5);
1078         }
1079         /* Write the room record back to disk */
1080         lputroom(&CC->quickroom);
1081
1082         /* If the room name changed, then there are now two room records,
1083          * so we have to delete the old one.
1084          */
1085         if (strcasecmp(CC->quickroom.QRname, old_name)) {
1086                 b_deleteroom(old_name);
1087         }
1088         /* adjust the floor reference counts */
1089         lgetfloor(&flbuf, old_floor);
1090         --flbuf.f_ref_count;
1091         lputfloor(&flbuf, old_floor);
1092         lgetfloor(&flbuf, CC->quickroom.QRfloor);
1093         ++flbuf.f_ref_count;
1094         lputfloor(&flbuf, CC->quickroom.QRfloor);
1095
1096         /* create a room directory if necessary */
1097         if (CC->quickroom.QRflags & QR_DIRECTORY) {
1098                 sprintf(buf,
1099                     "mkdir ./files/%s </dev/null >/dev/null 2>/dev/null",
1100                         CC->quickroom.QRdirname);
1101                 system(buf);
1102         }
1103         sprintf(buf, "%s> edited by %s", CC->quickroom.QRname, CC->curr_user);
1104         aide_message(buf);
1105         cprintf("%d Ok\n", OK);
1106 }
1107
1108
1109
1110 /* 
1111  * get the name of the room aide for this room
1112  */
1113 void cmd_geta(void)
1114 {
1115         struct usersupp usbuf;
1116
1117         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1118                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1119                 return;
1120         }
1121         if (is_noneditable(&CC->quickroom)) {
1122                 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
1123                 return;
1124         }
1125         if (getuserbynumber(&usbuf, CC->quickroom.QRroomaide) == 0) {
1126                 cprintf("%d %s\n", OK, usbuf.fullname);
1127         } else {
1128                 cprintf("%d \n", OK);
1129         }
1130 }
1131
1132
1133 /* 
1134  * set the room aide for this room
1135  */
1136 void cmd_seta(char *new_ra)
1137 {
1138         struct usersupp usbuf;
1139         long newu;
1140         char buf[256];
1141         int post_notice;
1142
1143         if (!(CC->logged_in)) {
1144                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1145                 return;
1146         }
1147         if (!is_room_aide()) {
1148                 cprintf("%d Higher access required.\n",
1149                         ERROR + HIGHER_ACCESS_REQUIRED);
1150                 return;
1151         }
1152         if (getuser(&usbuf, new_ra) != 0) {
1153                 newu = (-1L);
1154         } else {
1155                 newu = usbuf.usernum;
1156         }
1157
1158         lgetroom(&CC->quickroom, CC->quickroom.QRname);
1159         post_notice = 0;
1160         if (CC->quickroom.QRroomaide != newu) {
1161                 post_notice = 1;
1162         }
1163         CC->quickroom.QRroomaide = newu;
1164         lputroom(&CC->quickroom);
1165
1166         /*
1167          * We have to post the change notice _after_ writing changes to 
1168          * the room table, otherwise it would deadlock!
1169          */
1170         if (post_notice == 1) {
1171                 sprintf(buf, "%s is now room aide for %s>",
1172                         usbuf.fullname, CC->quickroom.QRname);
1173                 aide_message(buf);
1174         }
1175         cprintf("%d Ok\n", OK);
1176 }
1177
1178 /*
1179  * Generate an associated file name for a room
1180  */
1181 void assoc_file_name(char *buf, struct quickroom *qrbuf, char *prefix)
1182 {
1183         sprintf(buf, "./%s/%ld", prefix, qrbuf->QRnumber);
1184 }
1185
1186 /* 
1187  * retrieve info file for this room
1188  */
1189 void cmd_rinf(void)
1190 {
1191         char filename[128];
1192         char buf[256];
1193         FILE *info_fp;
1194
1195         assoc_file_name(filename, &CC->quickroom, "info");
1196         info_fp = fopen(filename, "r");
1197
1198         if (info_fp == NULL) {
1199                 cprintf("%d No info file.\n", ERROR);
1200                 return;
1201         }
1202         cprintf("%d Info:\n", LISTING_FOLLOWS);
1203         while (fgets(buf, 256, info_fp) != NULL) {
1204                 if (strlen(buf) > 0)
1205                         buf[strlen(buf) - 1] = 0;
1206                 cprintf("%s\n", buf);
1207         }
1208         cprintf("000\n");
1209         fclose(info_fp);
1210 }
1211
1212 /*
1213  * Back end processing to delete a room and everything associated with it
1214  */
1215 void delete_room(struct quickroom *qrbuf)
1216 {
1217         struct floor flbuf;
1218         long MsgToDelete;
1219         char aaa[100];
1220         int a;
1221
1222         lprintf(9, "Deleting room <%s>\n", qrbuf->QRname);
1223
1224         /* Delete the info file */
1225         assoc_file_name(aaa, qrbuf, "info");
1226         unlink(aaa);
1227
1228         /* Delete the image file */
1229         assoc_file_name(aaa, qrbuf, "images");
1230         unlink(aaa);
1231
1232         /* first flag the room record as not in use */
1233         lgetroom(qrbuf, qrbuf->QRname);
1234         qrbuf->QRflags = 0;
1235
1236         /* then delete the messages in the room */
1237         get_msglist(qrbuf);
1238         if (CC->num_msgs > 0)
1239                 for (a = 0; a < CC->num_msgs; ++a) {
1240                         MsgToDelete = MessageFromList(a);
1241                         AdjRefCount(MsgToDelete, -1);
1242                 }
1243         put_msglist(qrbuf);
1244         phree(CC->msglist);
1245         CC->msglist = NULL;
1246         CC->num_msgs = 0;
1247         delete_msglist(qrbuf);
1248         lputroom(qrbuf);
1249
1250         /* then decrement the reference count for the floor */
1251         lgetfloor(&flbuf, (int) (qrbuf->QRfloor));
1252         flbuf.f_ref_count = flbuf.f_ref_count - 1;
1253         lputfloor(&flbuf, (int) (qrbuf->QRfloor));
1254
1255         /* Delete the room record from the database! */
1256         b_deleteroom(qrbuf->QRname);
1257 }
1258
1259
1260 /*
1261  * aide command: kill the current room
1262  */
1263 void cmd_kill(char *argbuf)
1264 {
1265         char aaa[100];
1266         char deleted_room_name[ROOMNAMELEN];
1267         int kill_ok;
1268
1269         kill_ok = extract_int(argbuf, 0);
1270
1271         if (!(CC->logged_in)) {
1272                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1273                 return;
1274         }
1275         if (!is_room_aide()) {
1276                 cprintf("%d Higher access required.\n",
1277                         ERROR + HIGHER_ACCESS_REQUIRED);
1278                 return;
1279         }
1280         if (is_noneditable(&CC->quickroom)) {
1281                 cprintf("%d Can't edit this room.\n", ERROR + NOT_HERE);
1282                 return;
1283         }
1284         if (kill_ok) {
1285                 strcpy(deleted_room_name, CC->quickroom.QRname);
1286                 delete_room(&CC->quickroom);    /* Do the dirty work */
1287                 usergoto(BASEROOM, 0);  /* Return to the Lobby */
1288
1289                 /* tell the world what we did */
1290                 sprintf(aaa, "%s> killed by %s",
1291                         deleted_room_name, CC->curr_user);
1292                 aide_message(aaa);
1293                 cprintf("%d '%s' deleted.\n", OK, deleted_room_name);
1294         } else {
1295                 cprintf("%d ok to delete.\n", OK);
1296         }
1297 }
1298
1299
1300 /*
1301  * Internal code to create a new room (returns room flags)
1302  *
1303  * Room types:  0=public, 1=guessname, 2=passworded, 3=inv-only, 4=mailbox
1304  */
1305 unsigned create_room(char *new_room_name,
1306                      int new_room_type,
1307                      char *new_room_pass,
1308                      int new_room_floor)
1309 {
1310
1311         struct quickroom qrbuf;
1312         struct floor flbuf;
1313         struct visit vbuf;
1314
1315         if (getroom(&qrbuf, new_room_name) == 0)
1316                 return (0);     /* already exists */
1317
1318         memset(&qrbuf, 0, sizeof(struct quickroom));
1319         strncpy(qrbuf.QRname, new_room_name, ROOMNAMELEN);
1320         strncpy(qrbuf.QRpasswd, new_room_pass, 9);
1321         qrbuf.QRflags = QR_INUSE;
1322         qrbuf.QRnumber = get_new_room_number();
1323         if (new_room_type > 0)
1324                 qrbuf.QRflags = (qrbuf.QRflags | QR_PRIVATE);
1325         if (new_room_type == 1)
1326                 qrbuf.QRflags = (qrbuf.QRflags | QR_GUESSNAME);
1327         if (new_room_type == 2)
1328                 qrbuf.QRflags = (qrbuf.QRflags | QR_PASSWORDED);
1329         if (new_room_type == 4)
1330                 qrbuf.QRflags = (qrbuf.QRflags | QR_MAILBOX);
1331
1332         /* If the room is private, and the system administrator has elected
1333          * to automatically grant room aide privileges, do so now; otherwise,
1334          * set the room aide to undefined.
1335          */
1336         if ((qrbuf.QRflags & QR_PRIVATE) && (CREATAIDE == 1)) {
1337                 qrbuf.QRroomaide = CC->usersupp.usernum;
1338         } else {
1339                 qrbuf.QRroomaide = (-1L);
1340         }
1341
1342         qrbuf.QRhighest = 0L;   /* No messages in this room yet */
1343         time(&qrbuf.QRgen);     /* Use a timestamp as the generation number */
1344         qrbuf.QRfloor = new_room_floor;
1345
1346         /* save what we just did... */
1347         putroom(&qrbuf);
1348
1349         /* bump the reference count on whatever floor the room is on */
1350         lgetfloor(&flbuf, (int) qrbuf.QRfloor);
1351         flbuf.f_ref_count = flbuf.f_ref_count + 1;
1352         lputfloor(&flbuf, (int) qrbuf.QRfloor);
1353
1354         /* be sure not to kick the creator out of the room! */
1355         lgetuser(&CC->usersupp, CC->curr_user);
1356         CtdlGetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1357         vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1358         vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1359         CtdlSetRelationship(&vbuf, &CC->usersupp, &qrbuf);
1360         lputuser(&CC->usersupp);
1361
1362         /* resume our happy day */
1363         return (qrbuf.QRflags);
1364 }
1365
1366
1367 /*
1368  * create a new room
1369  */
1370 void cmd_cre8(char *args)
1371 {
1372         int cre8_ok;
1373         char new_room_name[256];
1374         int new_room_type;
1375         char new_room_pass[256];
1376         int new_room_floor;
1377         char aaa[256];
1378         unsigned newflags;
1379         struct quickroom qrbuf;
1380         struct floor flbuf;
1381
1382         cre8_ok = extract_int(args, 0);
1383         extract(new_room_name, args, 1);
1384         new_room_name[ROOMNAMELEN - 1] = 0;
1385         new_room_type = extract_int(args, 2);
1386         extract(new_room_pass, args, 3);
1387         new_room_pass[9] = 0;
1388         new_room_floor = 0;
1389
1390         if ((strlen(new_room_name) == 0) && (cre8_ok == 1)) {
1391                 cprintf("%d Invalid room name.\n", ERROR);
1392                 return;
1393         }
1394
1395         if (!strcasecmp(new_room_name, MAILROOM)) {
1396                 cprintf("%d '%s' already exists.\n",
1397                         ERROR + ALREADY_EXISTS, new_room_name);
1398                 return;
1399         }
1400
1401         if (num_parms(args) >= 5) {
1402                 getfloor(&flbuf, extract_int(args, 4));
1403                 if ((flbuf.f_flags & F_INUSE) == 0) {
1404                         cprintf("%d Invalid floor number.\n",
1405                                 ERROR + INVALID_FLOOR_OPERATION);
1406                         return;
1407                 } else {
1408                         new_room_floor = extract_int(args, 4);
1409                 }
1410         }
1411
1412         if (!(CC->logged_in)) {
1413                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1414                 return;
1415         }
1416
1417         if (CC->usersupp.axlevel < config.c_createax) {
1418                 cprintf("%d You need higher access to create rooms.\n",
1419                         ERROR + HIGHER_ACCESS_REQUIRED);
1420                 return;
1421         }
1422
1423         if ((strlen(new_room_name) == 0) && (cre8_ok == 0)) {
1424                 cprintf("%d Ok to create rooms.\n", OK);
1425                 return;
1426         }
1427
1428         if ((new_room_type < 0) || (new_room_type > 4)) {
1429                 cprintf("%d Invalid room type.\n", ERROR);
1430                 return;
1431         }
1432
1433         /* If the user is requesting a personal room, set up the room
1434          * name accordingly (prepend the user number)
1435          */
1436         if (new_room_type == 4) {
1437                 sprintf(aaa, "%010ld.%s",
1438                         CC->usersupp.usernum, new_room_name);
1439                 strcpy(new_room_name, aaa);
1440         }
1441
1442         /* Check to make sure the requested room name doesn't already exist */
1443         if (getroom(&qrbuf, new_room_name) == 0) {
1444                 cprintf("%d '%s' already exists.\n",
1445                         ERROR + ALREADY_EXISTS, qrbuf.QRname);
1446                 return;
1447         }
1448
1449         if (cre8_ok == 0) {
1450                 cprintf("%d OK to create '%s'\n", OK, new_room_name);
1451                 return;
1452         }
1453
1454         newflags = create_room(new_room_name,
1455                            new_room_type, new_room_pass, new_room_floor);
1456
1457         /* post a message in Aide> describing the new room */
1458         strncpy(aaa, new_room_name, 255);
1459         strcat(aaa, "> created by ");
1460         strcat(aaa, CC->usersupp.fullname);
1461         if (newflags & QR_MAILBOX)
1462                 strcat(aaa, " [personal]");
1463         else if (newflags & QR_PRIVATE)
1464                 strcat(aaa, " [private]");
1465         if (newflags & QR_GUESSNAME)
1466                 strcat(aaa, "[guessname] ");
1467         if (newflags & QR_PASSWORDED) {
1468                 strcat(aaa, "\n Password: ");
1469                 strcat(aaa, new_room_pass);
1470         }
1471         aide_message(aaa);
1472
1473         cprintf("%d '%s' has been created.\n", OK, qrbuf.QRname);
1474 }
1475
1476
1477
1478 void cmd_einf(char *ok)
1479 {                               /* enter info file for current room */
1480         FILE *fp;
1481         char infofilename[256];
1482         char buf[256];
1483
1484         if (!(CC->logged_in)) {
1485                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1486                 return;
1487         }
1488         if (!is_room_aide()) {
1489                 cprintf("%d Higher access required.\n",
1490                         ERROR + HIGHER_ACCESS_REQUIRED);
1491                 return;
1492         }
1493         if (atoi(ok) == 0) {
1494                 cprintf("%d Ok.\n", OK);
1495                 return;
1496         }
1497         assoc_file_name(infofilename, &CC->quickroom, "info");
1498         lprintf(9, "opening\n");
1499         fp = fopen(infofilename, "w");
1500         lprintf(9, "checking\n");
1501         if (fp == NULL) {
1502                 cprintf("%d Cannot open %s: %s\n",
1503                   ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
1504                 return;
1505         }
1506         cprintf("%d Send info...\n", SEND_LISTING);
1507
1508         do {
1509                 client_gets(buf);
1510                 if (strcmp(buf, "000"))
1511                         fprintf(fp, "%s\n", buf);
1512         } while (strcmp(buf, "000"));
1513         fclose(fp);
1514
1515         /* now update the room index so people will see our new info */
1516         lgetroom(&CC->quickroom, CC->quickroom.QRname);         /* lock so no one steps on us */
1517         CC->quickroom.QRinfo = CC->quickroom.QRhighest + 1L;
1518         lputroom(&CC->quickroom);
1519 }
1520
1521
1522 /* 
1523  * cmd_lflr()   -  List all known floors
1524  */
1525 void cmd_lflr(void)
1526 {
1527         int a;
1528         struct floor flbuf;
1529
1530         if (!(CC->logged_in)) {
1531                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1532                 return;
1533         }
1534         /* if (getuser(&CC->usersupp,CC->curr_user)) {
1535            cprintf("%d Can't locate user!\n",ERROR+INTERNAL_ERROR);
1536            return;
1537            }
1538          */
1539
1540         cprintf("%d Known floors:\n", LISTING_FOLLOWS);
1541
1542         for (a = 0; a < MAXFLOORS; ++a) {
1543                 getfloor(&flbuf, a);
1544                 if (flbuf.f_flags & F_INUSE) {
1545                         cprintf("%d|%s|%d\n",
1546                                 a,
1547                                 flbuf.f_name,
1548                                 flbuf.f_ref_count);
1549                 }
1550         }
1551         cprintf("000\n");
1552 }
1553
1554
1555
1556 /*
1557  * create a new floor
1558  */
1559 void cmd_cflr(char *argbuf)
1560 {
1561         char new_floor_name[256];
1562         struct floor flbuf;
1563         int cflr_ok;
1564         int free_slot = (-1);
1565         int a;
1566
1567         extract(new_floor_name, argbuf, 0);
1568         cflr_ok = extract_int(argbuf, 1);
1569
1570
1571         if (!(CC->logged_in)) {
1572                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1573                 return;
1574         }
1575         if (CC->usersupp.axlevel < 6) {
1576                 cprintf("%d You need higher access to create rooms.\n",
1577                         ERROR + HIGHER_ACCESS_REQUIRED);
1578                 return;
1579         }
1580         for (a = 0; a < MAXFLOORS; ++a) {
1581                 getfloor(&flbuf, a);
1582
1583                 /* note any free slots while we're scanning... */
1584                 if (((flbuf.f_flags & F_INUSE) == 0)
1585                     && (free_slot < 0))
1586                         free_slot = a;
1587
1588                 /* check to see if it already exists */
1589                 if ((!strcasecmp(flbuf.f_name, new_floor_name))
1590                     && (flbuf.f_flags & F_INUSE)) {
1591                         cprintf("%d Floor '%s' already exists.\n",
1592                                 ERROR + ALREADY_EXISTS,
1593                                 flbuf.f_name);
1594                         return;
1595                 }
1596         }
1597
1598         if (free_slot < 0) {
1599                 cprintf("%d There is no space available for a new floor.\n",
1600                         ERROR + INVALID_FLOOR_OPERATION);
1601                 return;
1602         }
1603         if (cflr_ok == 0) {
1604                 cprintf("%d ok to create...\n", OK);
1605                 return;
1606         }
1607         lgetfloor(&flbuf, free_slot);
1608         flbuf.f_flags = F_INUSE;
1609         flbuf.f_ref_count = 0;
1610         strncpy(flbuf.f_name, new_floor_name, 255);
1611         lputfloor(&flbuf, free_slot);
1612         cprintf("%d %d\n", OK, free_slot);
1613 }
1614
1615
1616
1617 /*
1618  * delete a floor
1619  */
1620 void cmd_kflr(char *argbuf)
1621 {
1622         struct floor flbuf;
1623         int floor_to_delete;
1624         int kflr_ok;
1625         int delete_ok;
1626
1627         floor_to_delete = extract_int(argbuf, 0);
1628         kflr_ok = extract_int(argbuf, 1);
1629
1630
1631         if (!(CC->logged_in)) {
1632                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1633                 return;
1634         }
1635         if (CC->usersupp.axlevel < 6) {
1636                 cprintf("%d You need higher access to delete floors.\n",
1637                         ERROR + HIGHER_ACCESS_REQUIRED);
1638                 return;
1639         }
1640         lgetfloor(&flbuf, floor_to_delete);
1641
1642         delete_ok = 1;
1643         if ((flbuf.f_flags & F_INUSE) == 0) {
1644                 cprintf("%d Floor %d not in use.\n",
1645                         ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
1646                 delete_ok = 0;
1647         } else {
1648                 if (flbuf.f_ref_count != 0) {
1649                         cprintf("%d Cannot delete; floor contains %d rooms.\n",
1650                                 ERROR + INVALID_FLOOR_OPERATION,
1651                                 flbuf.f_ref_count);
1652                         delete_ok = 0;
1653                 } else {
1654                         if (kflr_ok == 1) {
1655                                 cprintf("%d Ok\n", OK);
1656                         } else {
1657                                 cprintf("%d Ok to delete...\n", OK);
1658                         }
1659
1660                 }
1661
1662         }
1663
1664         if ((delete_ok == 1) && (kflr_ok == 1))
1665                 flbuf.f_flags = 0;
1666         lputfloor(&flbuf, floor_to_delete);
1667 }
1668
1669 /*
1670  * edit a floor
1671  */
1672 void cmd_eflr(char *argbuf)
1673 {
1674         struct floor flbuf;
1675         int floor_num;
1676         int np;
1677
1678         np = num_parms(argbuf);
1679         if (np < 1) {
1680                 cprintf("%d Usage error.\n", ERROR);
1681                 return;
1682         }
1683         if (!(CC->logged_in)) {
1684                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1685                 return;
1686         }
1687         if (CC->usersupp.axlevel < 6) {
1688                 cprintf("%d You need higher access to edit floors.\n",
1689                         ERROR + HIGHER_ACCESS_REQUIRED);
1690                 return;
1691         }
1692         floor_num = extract_int(argbuf, 0);
1693         lgetfloor(&flbuf, floor_num);
1694         if ((flbuf.f_flags & F_INUSE) == 0) {
1695                 lputfloor(&flbuf, floor_num);
1696                 cprintf("%d Floor %d is not in use.\n",
1697                         ERROR + INVALID_FLOOR_OPERATION, floor_num);
1698                 return;
1699         }
1700         if (np >= 2)
1701                 extract(flbuf.f_name, argbuf, 1);
1702         lputfloor(&flbuf, floor_num);
1703
1704         cprintf("%d Ok\n", OK);
1705 }