51456ac03528ecae834bb62506128a2ce3d61fe7
[citadel.git] / citadel / modules / ctdlproto / serv_rooms.c
1 /* 
2  * Server functions which perform operations on room objects.
3  *
4  * Copyright (c) 1987-2015 by the citadel.org team
5  *
6  * This program is open source software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License, version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <stdio.h>
16 #include <dirent.h>     /* for cmd_rdir to read contents of the directory */
17 #include <libcitadel.h>
18
19 #include "citserver.h"
20 #include "ctdl_module.h"
21 #include "room_ops.h"
22 #include "config.h"
23
24 /*
25  * Back-back-end for all room listing commands
26  */
27 void list_roomname(struct ctdlroom *qrbuf, int ra, int current_view, int default_view)
28 {
29         char truncated_roomname[ROOMNAMELEN];
30
31         /* For my own mailbox rooms, chop off the owner prefix */
32         if ( (qrbuf->QRflags & QR_MAILBOX)
33              && (atol(qrbuf->QRname) == CC->user.usernum) ) {
34                 safestrncpy(truncated_roomname, qrbuf->QRname, sizeof truncated_roomname);
35                 safestrncpy(truncated_roomname, &truncated_roomname[11], sizeof truncated_roomname);
36                 cprintf("%s", truncated_roomname);
37         }
38         /* For all other rooms, just display the name in its entirety */
39         else {
40                 cprintf("%s", qrbuf->QRname);
41         }
42
43         /* ...and now the other parameters */
44         cprintf("|%u|%d|%d|%d|%d|%d|%d|%ld|\n",
45                 qrbuf->QRflags,
46                 (int) qrbuf->QRfloor,
47                 (int) qrbuf->QRorder,
48                 (int) qrbuf->QRflags2,
49                 ra,
50                 current_view,
51                 default_view,
52                 qrbuf->QRmtime
53         );
54 }
55
56
57 /* 
58  * cmd_lrms()   -  List all accessible rooms, known or forgotten
59  */
60 void cmd_lrms_backend(struct ctdlroom *qrbuf, void *data)
61 {
62         int FloorBeingSearched = (-1);
63         int ra;
64         int view;
65
66         FloorBeingSearched = *(int *)data;
67         CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
68
69         if ((( ra & (UA_KNOWN | UA_ZAPPED)))
70             && ((qrbuf->QRfloor == (FloorBeingSearched))
71                 || ((FloorBeingSearched) < 0)))
72                 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
73 }
74
75 void cmd_lrms(char *argbuf)
76 {
77         int FloorBeingSearched = (-1);
78         if (!IsEmptyStr(argbuf))
79                 FloorBeingSearched = extract_int(argbuf, 0);
80
81         if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
82
83         CtdlGetUser(&CC->user, CC->curr_user);
84         cprintf("%d Accessible rooms:\n", LISTING_FOLLOWS);
85
86         CtdlForEachRoom(cmd_lrms_backend, &FloorBeingSearched);
87         cprintf("000\n");
88 }
89
90
91
92 /* 
93  * cmd_lkra()   -  List all known rooms
94  */
95 void cmd_lkra_backend(struct ctdlroom *qrbuf, void *data)
96 {
97         int FloorBeingSearched = (-1);
98         int ra;
99         int view;
100
101         FloorBeingSearched = *(int *)data;
102         CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
103
104         if ((( ra & (UA_KNOWN)))
105             && ((qrbuf->QRfloor == (FloorBeingSearched))
106                 || ((FloorBeingSearched) < 0)))
107                 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
108 }
109
110 void cmd_lkra(char *argbuf)
111 {
112         int FloorBeingSearched = (-1);
113         if (!IsEmptyStr(argbuf))
114                 FloorBeingSearched = extract_int(argbuf, 0);
115
116         if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
117         
118         CtdlGetUser(&CC->user, CC->curr_user);
119         cprintf("%d Known rooms:\n", LISTING_FOLLOWS);
120
121         CtdlForEachRoom(cmd_lkra_backend, &FloorBeingSearched);
122         cprintf("000\n");
123 }
124
125
126
127 void cmd_lprm_backend(struct ctdlroom *qrbuf, void *data)
128 {
129         int FloorBeingSearched = (-1);
130         int ra;
131         int view;
132
133         FloorBeingSearched = *(int *)data;
134         CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
135
136         if (   ((qrbuf->QRflags & QR_PRIVATE) == 0)
137                 && ((qrbuf->QRflags & QR_MAILBOX) == 0)
138             && ((qrbuf->QRfloor == (FloorBeingSearched))
139                 || ((FloorBeingSearched) < 0)))
140                 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
141 }
142
143 void cmd_lprm(char *argbuf)
144 {
145         int FloorBeingSearched = (-1);
146         if (!IsEmptyStr(argbuf))
147                 FloorBeingSearched = extract_int(argbuf, 0);
148
149         cprintf("%d Public rooms:\n", LISTING_FOLLOWS);
150
151         CtdlForEachRoom(cmd_lprm_backend, &FloorBeingSearched);
152         cprintf("000\n");
153 }
154
155
156
157 /* 
158  * cmd_lkrn()   -  List all known rooms with new messages
159  */
160 void cmd_lkrn_backend(struct ctdlroom *qrbuf, void *data)
161 {
162         int FloorBeingSearched = (-1);
163         int ra;
164         int view;
165
166         FloorBeingSearched = *(int *)data;
167         CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
168
169         if ((ra & UA_KNOWN)
170             && (ra & UA_HASNEWMSGS)
171             && ((qrbuf->QRfloor == (FloorBeingSearched))
172                 || ((FloorBeingSearched) < 0)))
173                 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
174 }
175
176 void cmd_lkrn(char *argbuf)
177 {
178         int FloorBeingSearched = (-1);
179         if (!IsEmptyStr(argbuf))
180                 FloorBeingSearched = extract_int(argbuf, 0);
181
182         if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
183         
184         CtdlGetUser(&CC->user, CC->curr_user);
185         cprintf("%d Rooms w/ new msgs:\n", LISTING_FOLLOWS);
186
187         CtdlForEachRoom(cmd_lkrn_backend, &FloorBeingSearched);
188         cprintf("000\n");
189 }
190
191
192
193 /* 
194  * cmd_lkro()   -  List all known rooms
195  */
196 void cmd_lkro_backend(struct ctdlroom *qrbuf, void *data)
197 {
198         int FloorBeingSearched = (-1);
199         int ra;
200         int view;
201
202         FloorBeingSearched = *(int *)data;
203         CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
204
205         if ((ra & UA_KNOWN)
206             && ((ra & UA_HASNEWMSGS) == 0)
207             && ((qrbuf->QRfloor == (FloorBeingSearched))
208                 || ((FloorBeingSearched) < 0)))
209                 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
210 }
211
212 void cmd_lkro(char *argbuf)
213 {
214         int FloorBeingSearched = (-1);
215         if (!IsEmptyStr(argbuf))
216                 FloorBeingSearched = extract_int(argbuf, 0);
217
218         if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
219         
220         CtdlGetUser(&CC->user, CC->curr_user);
221         cprintf("%d Rooms w/o new msgs:\n", LISTING_FOLLOWS);
222
223         CtdlForEachRoom(cmd_lkro_backend, &FloorBeingSearched);
224         cprintf("000\n");
225 }
226
227
228
229 /* 
230  * cmd_lzrm()   -  List all forgotten rooms
231  */
232 void cmd_lzrm_backend(struct ctdlroom *qrbuf, void *data)
233 {
234         int FloorBeingSearched = (-1);
235         int ra;
236         int view;
237
238         FloorBeingSearched = *(int *)data;
239         CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
240
241         if ((ra & UA_GOTOALLOWED)
242             && (ra & UA_ZAPPED)
243             && ((qrbuf->QRfloor == (FloorBeingSearched))
244                 || ((FloorBeingSearched) < 0)))
245                 list_roomname(qrbuf, ra, view, qrbuf->QRdefaultview);
246 }
247
248 void cmd_lzrm(char *argbuf)
249 {
250         int FloorBeingSearched = (-1);
251         if (!IsEmptyStr(argbuf))
252                 FloorBeingSearched = extract_int(argbuf, 0);
253
254         if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
255         
256         CtdlGetUser(&CC->user, CC->curr_user);
257         cprintf("%d Zapped rooms:\n", LISTING_FOLLOWS);
258
259         CtdlForEachRoom(cmd_lzrm_backend, &FloorBeingSearched);
260         cprintf("000\n");
261 }
262
263
264 /* 
265  * cmd_goto()  -  goto a new room
266  */
267 void cmd_goto(char *gargs)
268 {
269         struct ctdlroom QRscratch;
270         int c;
271         int ok = 0;
272         int ra;
273         char augmented_roomname[ROOMNAMELEN];
274         char towhere[ROOMNAMELEN];
275         char password[32];
276         int transiently = 0;
277
278         if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
279
280         extract_token(towhere, gargs, 0, '|', sizeof towhere);
281         extract_token(password, gargs, 1, '|', sizeof password);
282         transiently = extract_int(gargs, 2);
283
284         CtdlGetUser(&CC->user, CC->curr_user);
285
286         /*
287          * Handle some of the macro named rooms
288          */
289         convert_room_name_macros(towhere, sizeof towhere);
290
291         /* First try a regular match */
292         c = CtdlGetRoom(&QRscratch, towhere);
293
294         /* Then try a mailbox name match */
295         if (c != 0) {
296                 CtdlMailboxName(augmented_roomname, sizeof augmented_roomname,
297                             &CC->user, towhere);
298                 c = CtdlGetRoom(&QRscratch, augmented_roomname);
299                 if (c == 0)
300                         safestrncpy(towhere, augmented_roomname, sizeof towhere);
301         }
302
303         /* And if the room was found... */
304         if (c == 0) {
305
306                 /* Let internal programs go directly to any room. */
307                 if (CC->internal_pgm) {
308                         memcpy(&CC->room, &QRscratch,
309                                 sizeof(struct ctdlroom));
310                         CtdlUserGoto(NULL, 1, transiently, NULL, NULL, NULL, NULL);
311                         return;
312                 }
313
314                 /* See if there is an existing user/room relationship */
315                 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
316
317                 /* normal clients have to pass through security */
318                 if (ra & UA_GOTOALLOWED) {
319                         ok = 1;
320                 }
321
322                 if (ok == 1) {
323                         if ((QRscratch.QRflags & QR_MAILBOX) &&
324                             ((ra & UA_GOTOALLOWED))) {
325                                 memcpy(&CC->room, &QRscratch,
326                                         sizeof(struct ctdlroom));
327                                 CtdlUserGoto(NULL, 1, transiently, NULL, NULL, NULL, NULL);
328                                 return;
329                         } else if ((QRscratch.QRflags & QR_PASSWORDED) &&
330                             ((ra & UA_KNOWN) == 0) &&
331                             (strcasecmp(QRscratch.QRpasswd, password)) &&
332                             (CC->user.axlevel < AxAideU)
333                             ) {
334                                 cprintf("%d wrong or missing passwd\n",
335                                         ERROR + PASSWORD_REQUIRED);
336                                 return;
337                         } else if ((QRscratch.QRflags & QR_PRIVATE) &&
338                                    ((QRscratch.QRflags & QR_PASSWORDED) == 0) &&
339                                    ((QRscratch.QRflags & QR_GUESSNAME) == 0) &&
340                                    ((ra & UA_KNOWN) == 0) &&
341                                    (CC->user.axlevel < AxAideU)
342                                   ) {
343                                 syslog(LOG_DEBUG, "Failed to acquire private room\n");
344                         } else {
345                                 memcpy(&CC->room, &QRscratch,
346                                         sizeof(struct ctdlroom));
347                                 CtdlUserGoto(NULL, 1, transiently, NULL, NULL, NULL, NULL);
348                                 return;
349                         }
350                 }
351         }
352
353         cprintf("%d room '%s' not found\n", ERROR + ROOM_NOT_FOUND, towhere);
354 }
355
356
357 void cmd_whok(char *cmdbuf)
358 {
359         struct ctdluser temp;
360         struct cdbdata *cdbus;
361         int ra;
362
363         cprintf("%d Who knows room:\n", LISTING_FOLLOWS);
364         cdb_rewind(CDB_USERS);
365         while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
366                 memset(&temp, 0, sizeof temp);
367                 memcpy(&temp, cdbus->ptr, sizeof temp);
368                 cdb_free(cdbus);
369
370                 CtdlRoomAccess(&CC->room, &temp, &ra, NULL);
371                 if ((!IsEmptyStr(temp.fullname)) && 
372                     (CC->room.QRflags & QR_INUSE) &&
373                     (ra & UA_KNOWN)
374                         )
375                         cprintf("%s\n", temp.fullname);
376         }
377         cprintf("000\n");
378 }
379
380
381 /*
382  * RDIR command for room directory
383  */
384 void cmd_rdir(char *cmdbuf)
385 {
386         char buf[256];
387         char comment[256];
388         FILE *fd;
389         struct stat statbuf;
390         DIR *filedir = NULL;
391         struct dirent *filedir_entry;
392         int d_namelen;
393         char buf2[SIZ];
394         char mimebuf[64];
395         long len;
396         
397         if (CtdlAccessCheck(ac_logged_in)) return;
398         
399         CtdlGetRoom(&CC->room, CC->room.QRname);
400         CtdlGetUser(&CC->user, CC->curr_user);
401
402         if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
403                 cprintf("%d not here.\n", ERROR + NOT_HERE);
404                 return;
405         }
406         if (((CC->room.QRflags & QR_VISDIR) == 0)
407             && (CC->user.axlevel < AxAideU)
408             && (CC->user.usernum != CC->room.QRroomaide)) {
409                 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
410                 return;
411         }
412
413         snprintf(buf, sizeof buf, "%s/%s", ctdl_file_dir, CC->room.QRdirname);
414         filedir = opendir (buf);
415         
416         if (filedir == NULL) {
417                 cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
418                 return;
419         }
420         cprintf("%d %s|%s/%s\n", LISTING_FOLLOWS, CtdlGetConfigStr("c_fqdn"), ctdl_file_dir, CC->room.QRdirname);
421         
422         snprintf(buf, sizeof buf, "%s/%s/filedir", ctdl_file_dir, CC->room.QRdirname);
423         fd = fopen(buf, "r");
424         if (fd == NULL)
425                 fd = fopen("/dev/null", "r");
426         while ((filedir_entry = readdir(filedir)))
427         {
428                 if (strcasecmp(filedir_entry->d_name, "filedir") && filedir_entry->d_name[0] != '.')
429                 {
430 #ifdef _DIRENT_HAVE_D_NAMELEN
431                         d_namelen = filedir_entry->d_namlen;
432 #else
433                         d_namelen = strlen(filedir_entry->d_name);
434 #endif
435                         snprintf(buf, sizeof buf, "%s/%s/%s", ctdl_file_dir, CC->room.QRdirname, filedir_entry->d_name);
436                         stat(buf, &statbuf);    /* stat the file */
437                         if (!(statbuf.st_mode & S_IFREG))
438                         {
439                                 snprintf(buf2, sizeof buf2,
440                                         "\"%s\" appears in the file directory for room \"%s\" but is not a regular file.  Directories, named pipes, sockets, etc. are not usable in Citadel room directories.\n",
441                                         buf, CC->room.QRname
442                                 );
443                                 CtdlAideMessage(buf2, "Unusable data found in room directory");
444                                 continue;       /* not a useable file type so don't show it */
445                         }
446                         safestrncpy(comment, "", sizeof comment);
447                         fseek(fd, 0L, 0);       /* rewind descriptions file */
448                         /* Get the description from the descriptions file */
449                         while ((fgets(buf, sizeof buf, fd) != NULL) && (IsEmptyStr(comment))) 
450                         {
451                                 buf[strlen(buf) - 1] = 0;
452                                 if ((!strncasecmp(buf, filedir_entry->d_name, d_namelen)) && (buf[d_namelen] == ' '))
453                                         safestrncpy(comment, &buf[d_namelen + 1], sizeof comment);
454                         }
455                         len = extract_token (mimebuf, comment, 0,' ', 64);
456                         if ((len <0) || strchr(mimebuf, '/') == NULL)
457                         {
458                                 snprintf (mimebuf, 64, "application/octetstream");
459                                 len = 0;
460                         }
461                         cprintf("%s|%ld|%s|%s\n", 
462                                 filedir_entry->d_name, 
463                                 (long)statbuf.st_size, 
464                                 mimebuf, 
465                                 &comment[len]);
466                 }
467         }
468         fclose(fd);
469         closedir(filedir);
470         
471         cprintf("000\n");
472 }
473
474 /*
475  * get room parameters (admin or room admin command)
476  */
477 void cmd_getr(char *cmdbuf)
478 {
479         if (CtdlAccessCheck(ac_room_aide)) return;
480
481         CtdlGetRoom(&CC->room, CC->room.QRname);
482         cprintf("%d%c%s|%s|%s|%d|%d|%d|%d|%d|\n",
483                 CIT_OK,
484                 CtdlCheckExpress(),
485
486                 ((CC->room.QRflags & QR_MAILBOX) ?
487                         &CC->room.QRname[11] : CC->room.QRname),
488
489                 ((CC->room.QRflags & QR_PASSWORDED) ?
490                         CC->room.QRpasswd : ""),
491
492                 ((CC->room.QRflags & QR_DIRECTORY) ?
493                         CC->room.QRdirname : ""),
494
495                 CC->room.QRflags,
496                 (int) CC->room.QRfloor,
497                 (int) CC->room.QRorder,
498
499                 CC->room.QRdefaultview,
500                 CC->room.QRflags2
501                 );
502 }
503
504 /*
505  * set room parameters (admin or room admin command)
506  */
507 void cmd_setr(char *args)
508 {
509         char buf[256];
510         int new_order = 0;
511         int r;
512         int new_floor;
513         char new_name[ROOMNAMELEN];
514
515         if (CtdlAccessCheck(ac_logged_in)) return;
516
517         if (num_parms(args) >= 6) {
518                 new_floor = extract_int(args, 5);
519         } else {
520                 new_floor = (-1);       /* don't change the floor */
521         }
522
523         /* When is a new name more than just a new name?  When the old name
524          * has a namespace prefix.
525          */
526         if (CC->room.QRflags & QR_MAILBOX) {
527                 sprintf(new_name, "%010ld.", atol(CC->room.QRname) );
528         } else {
529                 safestrncpy(new_name, "", sizeof new_name);
530         }
531         extract_token(&new_name[strlen(new_name)], args, 0, '|', (sizeof new_name - strlen(new_name)));
532
533         r = CtdlRenameRoom(CC->room.QRname, new_name, new_floor);
534
535         if (r == crr_room_not_found) {
536                 cprintf("%d Internal error - room not found?\n", ERROR + INTERNAL_ERROR);
537         } else if (r == crr_already_exists) {
538                 cprintf("%d '%s' already exists.\n",
539                         ERROR + ALREADY_EXISTS, new_name);
540         } else if (r == crr_noneditable) {
541                 cprintf("%d Cannot edit this room.\n", ERROR + NOT_HERE);
542         } else if (r == crr_invalid_floor) {
543                 cprintf("%d Target floor does not exist.\n",
544                         ERROR + INVALID_FLOOR_OPERATION);
545         } else if (r == crr_access_denied) {
546                 cprintf("%d You do not have permission to edit '%s'\n",
547                         ERROR + HIGHER_ACCESS_REQUIRED,
548                         CC->room.QRname);
549         } else if (r != crr_ok) {
550                 cprintf("%d Error: CtdlRenameRoom() returned %d\n",
551                         ERROR + INTERNAL_ERROR, r);
552         }
553
554         if (r != crr_ok) {
555                 return;
556         }
557
558         CtdlGetRoom(&CC->room, new_name);
559
560         /* Now we have to do a bunch of other stuff */
561
562         if (num_parms(args) >= 7) {
563                 new_order = extract_int(args, 6);
564                 if (new_order < 1)
565                         new_order = 1;
566                 if (new_order > 127)
567                         new_order = 127;
568         }
569
570         CtdlGetRoomLock(&CC->room, CC->room.QRname);
571
572         /* Directory room */
573         extract_token(buf, args, 2, '|', sizeof buf);
574         buf[15] = 0;
575         safestrncpy(CC->room.QRdirname, buf,
576                 sizeof CC->room.QRdirname);
577
578         /* Default view */
579         if (num_parms(args) >= 8) {
580                 CC->room.QRdefaultview = extract_int(args, 7);
581         }
582
583         /* Second set of flags */
584         if (num_parms(args) >= 9) {
585                 CC->room.QRflags2 = extract_int(args, 8);
586         }
587
588         /* Misc. flags */
589         CC->room.QRflags = (extract_int(args, 3) | QR_INUSE);
590         /* Clean up a client boo-boo: if the client set the room to
591          * guess-name or passworded, ensure that the private flag is
592          * also set.
593          */
594         if ((CC->room.QRflags & QR_GUESSNAME)
595             || (CC->room.QRflags & QR_PASSWORDED))
596                 CC->room.QRflags |= QR_PRIVATE;
597
598         /* Some changes can't apply to BASEROOM */
599         if (!strncasecmp(CC->room.QRname, CtdlGetConfigStr("c_baseroom"), ROOMNAMELEN)) {
600                 CC->room.QRorder = 0;
601                 CC->room.QRpasswd[0] = '\0';
602                 CC->room.QRflags &= ~(QR_PRIVATE & QR_PASSWORDED &
603                         QR_GUESSNAME & QR_PREFONLY & QR_MAILBOX);
604                 CC->room.QRflags |= QR_PERMANENT;
605         } else {        
606                 /* March order (doesn't apply to AIDEROOM) */
607                 if (num_parms(args) >= 7)
608                         CC->room.QRorder = (char) new_order;
609                 /* Room password */
610                 extract_token(buf, args, 1, '|', sizeof buf);
611                 buf[10] = 0;
612                 safestrncpy(CC->room.QRpasswd, buf,
613                             sizeof CC->room.QRpasswd);
614                 /* Kick everyone out if the client requested it
615                  * (by changing the room's generation number)
616                  */
617                 if (extract_int(args, 4)) {
618                         time(&CC->room.QRgen);
619                 }
620         }
621         /* Some changes can't apply to AIDEROOM */
622         if (!strncasecmp(CC->room.QRname, CtdlGetConfigStr("c_baseroom"), ROOMNAMELEN)) {
623                 CC->room.QRorder = 0;
624                 CC->room.QRflags &= ~QR_MAILBOX;
625                 CC->room.QRflags |= QR_PERMANENT;
626         }
627
628         /* Write the room record back to disk */
629         CtdlPutRoomLock(&CC->room);
630
631         /* Create a room directory if necessary */
632         if (CC->room.QRflags & QR_DIRECTORY) {
633                 snprintf(buf, sizeof buf,"%s/%s",
634                                  ctdl_file_dir,
635                                  CC->room.QRdirname);
636                 mkdir(buf, 0755);
637         }
638         snprintf(buf, sizeof buf, "The room \"%s\" has been edited by %s.\n",
639                 CC->room.QRname,
640                 (CC->logged_in ? CC->curr_user : "an administrator")
641         );
642         CtdlAideMessage(buf, "Room modification Message");
643         cprintf("%d Ok\n", CIT_OK);
644 }
645
646
647
648 /* 
649  * get the name of the room admin for this room
650  */
651 void cmd_geta(char *cmdbuf)
652 {
653         struct ctdluser usbuf;
654
655         if (CtdlAccessCheck(ac_logged_in)) return;
656
657         if (CtdlGetUserByNumber(&usbuf, CC->room.QRroomaide) == 0) {
658                 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
659         } else {
660                 cprintf("%d \n", CIT_OK);
661         }
662 }
663
664
665 /* 
666  * set the room admin for this room
667  */
668 void cmd_seta(char *new_ra)
669 {
670         struct ctdluser usbuf;
671         long newu;
672         char buf[SIZ];
673         int post_notice;
674
675         if (CtdlAccessCheck(ac_room_aide)) return;
676
677         if (CtdlGetUser(&usbuf, new_ra) != 0) {
678                 newu = (-1L);
679         } else {
680                 newu = usbuf.usernum;
681         }
682
683         CtdlGetRoomLock(&CC->room, CC->room.QRname);
684         post_notice = 0;
685         if (CC->room.QRroomaide != newu) {
686                 post_notice = 1;
687         }
688         CC->room.QRroomaide = newu;
689         CtdlPutRoomLock(&CC->room);
690
691         /*
692          * We have to post the change notice _after_ writing changes to 
693          * the room table, otherwise it would deadlock!
694          */
695         if (post_notice == 1) {
696                 if (!IsEmptyStr(usbuf.fullname))
697                         snprintf(buf, sizeof buf,
698                                 "%s is now the room admin for \"%s\".\n",
699                                 usbuf.fullname, CC->room.QRname);
700                 else
701                         snprintf(buf, sizeof buf,
702                                 "There is now no room admin for \"%s\".\n",
703                                 CC->room.QRname);
704                 CtdlAideMessage(buf, "Admin Room Modification");
705         }
706         cprintf("%d Ok\n", CIT_OK);
707 }
708
709 /* 
710  * retrieve info file for this room
711  */
712 void cmd_rinf(char *gargs)
713 {
714         char filename[PATH_MAX];
715         char buf[SIZ];
716         FILE *info_fp;
717
718         assoc_file_name(filename, sizeof filename, &CC->room, ctdl_info_dir);
719         info_fp = fopen(filename, "r");
720
721         if (info_fp == NULL) {
722                 cprintf("%d No info file.\n", ERROR + FILE_NOT_FOUND);
723                 return;
724         }
725         cprintf("%d Info:\n", LISTING_FOLLOWS);
726         while (fgets(buf, sizeof buf, info_fp) != NULL) {
727                 if (!IsEmptyStr(buf))
728                         buf[strlen(buf) - 1] = 0;
729                 cprintf("%s\n", buf);
730         }
731         cprintf("000\n");
732         fclose(info_fp);
733 }
734
735
736 /*
737  * admin command: kill the current room
738  */
739 void cmd_kill(char *argbuf)
740 {
741         char deleted_room_name[ROOMNAMELEN];
742         char msg[SIZ];
743         int kill_ok;
744
745         kill_ok = extract_int(argbuf, 0);
746
747         if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room) == 0) {
748                 cprintf("%d Can't delete this room.\n", ERROR + NOT_HERE);
749                 return;
750         }
751         if (kill_ok) {
752                 if (CC->room.QRflags & QR_MAILBOX) {
753                         safestrncpy(deleted_room_name, &CC->room.QRname[11], sizeof deleted_room_name);
754                 }
755                 else {
756                         safestrncpy(deleted_room_name, CC->room.QRname, sizeof deleted_room_name);
757                 }
758
759                 /* Do the dirty work */
760                 CtdlScheduleRoomForDeletion(&CC->room);
761
762                 /* Return to the Lobby */
763                 CtdlUserGoto(CtdlGetConfigStr("c_baseroom"), 0, 0, NULL, NULL, NULL, NULL);
764
765                 /* tell the world what we did */
766                 snprintf(msg, sizeof msg, "The room \"%s\" has been deleted by %s.\n",
767                          deleted_room_name,
768                         (CC->logged_in ? CC->curr_user : "an administrator")
769                 );
770                 CtdlAideMessage(msg, "Room Purger Message");
771                 cprintf("%d '%s' deleted.\n", CIT_OK, deleted_room_name);
772         } else {
773                 cprintf("%d ok to delete.\n", CIT_OK);
774         }
775 }
776
777
778 /*
779  * create a new room
780  */
781 void cmd_cre8(char *args)
782 {
783         int cre8_ok;
784         char new_room_name[ROOMNAMELEN];
785         int new_room_type;
786         char new_room_pass[32];
787         int new_room_floor;
788         int new_room_view;
789         char *notification_message = NULL;
790         unsigned newflags;
791         struct floor *fl;
792         int avoid_access = 0;
793
794         cre8_ok = extract_int(args, 0);
795         extract_token(new_room_name, args, 1, '|', sizeof new_room_name);
796         new_room_name[ROOMNAMELEN - 1] = 0;
797         new_room_type = extract_int(args, 2);
798         extract_token(new_room_pass, args, 3, '|', sizeof new_room_pass);
799         avoid_access = extract_int(args, 5);
800         new_room_view = extract_int(args, 6);
801         new_room_pass[9] = 0;
802         new_room_floor = 0;
803
804         if ((IsEmptyStr(new_room_name)) && (cre8_ok == 1)) {
805                 cprintf("%d Invalid room name.\n", ERROR + ILLEGAL_VALUE);
806                 return;
807         }
808
809         if (!strcasecmp(new_room_name, MAILROOM)) {
810                 cprintf("%d '%s' already exists.\n",
811                         ERROR + ALREADY_EXISTS, new_room_name);
812                 return;
813         }
814
815         if (num_parms(args) >= 5) {
816                 fl = CtdlGetCachedFloor(extract_int(args, 4));
817                 if (fl == NULL) {
818                         cprintf("%d Invalid floor number.\n",
819                                 ERROR + INVALID_FLOOR_OPERATION);
820                         return;
821                 }
822                 else if ((fl->f_flags & F_INUSE) == 0) {
823                         cprintf("%d Invalid floor number.\n",
824                                 ERROR + INVALID_FLOOR_OPERATION);
825                         return;
826                 } else {
827                         new_room_floor = extract_int(args, 4);
828                 }
829         }
830
831         if (CtdlAccessCheck(ac_logged_in)) return;
832
833         if (CC->user.axlevel < CtdlGetConfigInt("c_createax") && !CC->internal_pgm) {
834                 cprintf("%d You need higher access to create rooms.\n",
835                         ERROR + HIGHER_ACCESS_REQUIRED);
836                 return;
837         }
838
839         if ((IsEmptyStr(new_room_name)) && (cre8_ok == 0)) {
840                 cprintf("%d Ok to create rooms.\n", CIT_OK);
841                 return;
842         }
843
844         if ((new_room_type < 0) || (new_room_type > 5)) {
845                 cprintf("%d Invalid room type.\n", ERROR + ILLEGAL_VALUE);
846                 return;
847         }
848
849         if (new_room_type == 5) {
850                 if (CC->user.axlevel < AxAideU) {
851                         cprintf("%d Higher access required\n", 
852                                 ERROR + HIGHER_ACCESS_REQUIRED);
853                         return;
854                 }
855         }
856
857         /* Check to make sure the requested room name doesn't already exist */
858         newflags = CtdlCreateRoom(new_room_name,
859                                 new_room_type, new_room_pass, new_room_floor,
860                                 0, avoid_access, new_room_view);
861         if (newflags == 0) {
862                 cprintf("%d '%s' already exists.\n",
863                         ERROR + ALREADY_EXISTS, new_room_name);
864                 return;
865         }
866
867         if (cre8_ok == 0) {
868                 cprintf("%d OK to create '%s'\n", CIT_OK, new_room_name);
869                 return;
870         }
871
872         /* If we reach this point, the room needs to be created. */
873
874         newflags = CtdlCreateRoom(new_room_name,
875                            new_room_type, new_room_pass, new_room_floor, 1, 0,
876                            new_room_view);
877
878         /* post a message in Aide> describing the new room */
879         notification_message = malloc(1024);
880         snprintf(notification_message, 1024,
881                 "A new room called \"%s\" has been created by %s%s%s%s%s%s\n",
882                 new_room_name,
883                 (CC->logged_in ? CC->curr_user : "an administrator"),
884                 ((newflags & QR_MAILBOX) ? " [personal]" : ""),
885                 ((newflags & QR_PRIVATE) ? " [private]" : ""),
886                 ((newflags & QR_GUESSNAME) ? " [hidden]" : ""),
887                 ((newflags & QR_PASSWORDED) ? " Password: " : ""),
888                 ((newflags & QR_PASSWORDED) ? new_room_pass : "")
889         );
890         CtdlAideMessage(notification_message, "Room Creation Message");
891         free(notification_message);
892
893         cprintf("%d '%s' has been created.\n", CIT_OK, new_room_name);
894 }
895
896
897
898 void cmd_einf(char *ok)
899 {                               /* enter info file for current room */
900         FILE *fp;
901         char infofilename[SIZ];
902         char buf[SIZ];
903
904         unbuffer_output();
905
906         if (CtdlAccessCheck(ac_room_aide)) return;
907
908         if (atoi(ok) == 0) {
909                 cprintf("%d Ok.\n", CIT_OK);
910                 return;
911         }
912         assoc_file_name(infofilename, sizeof infofilename, &CC->room, ctdl_info_dir);
913         syslog(LOG_DEBUG, "opening\n");
914         fp = fopen(infofilename, "w");
915         syslog(LOG_DEBUG, "checking\n");
916         if (fp == NULL) {
917                 cprintf("%d Cannot open %s: %s\n",
918                   ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
919                 return;
920         }
921         cprintf("%d Send info...\n", SEND_LISTING);
922
923         do {
924                 client_getln(buf, sizeof buf);
925                 if (strcmp(buf, "000"))
926                         fprintf(fp, "%s\n", buf);
927         } while (strcmp(buf, "000"));
928         fclose(fp);
929
930         /* now update the room index so people will see our new info */
931         CtdlGetRoomLock(&CC->room, CC->room.QRname);            /* lock so no one steps on us */
932         CC->room.QRinfo = CC->room.QRhighest + 1L;
933         CtdlPutRoomLock(&CC->room);
934 }
935
936
937 /* 
938  * cmd_lflr()   -  List all known floors
939  */
940 void cmd_lflr(char *gargs)
941 {
942         int a;
943         struct floor flbuf;
944
945         if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
946
947         cprintf("%d Known floors:\n", LISTING_FOLLOWS);
948
949         for (a = 0; a < MAXFLOORS; ++a) {
950                 CtdlGetFloor(&flbuf, a);
951                 if (flbuf.f_flags & F_INUSE) {
952                         cprintf("%d|%s|%d\n",
953                                 a,
954                                 flbuf.f_name,
955                                 flbuf.f_ref_count);
956                 }
957         }
958         cprintf("000\n");
959 }
960
961
962
963 /*
964  * create a new floor
965  */
966 void cmd_cflr(char *argbuf)
967 {
968         char new_floor_name[256];
969         struct floor flbuf;
970         int cflr_ok;
971         int free_slot = (-1);
972         int a;
973
974         extract_token(new_floor_name, argbuf, 0, '|', sizeof new_floor_name);
975         cflr_ok = extract_int(argbuf, 1);
976
977         if (CtdlAccessCheck(ac_aide)) return;
978
979         if (IsEmptyStr(new_floor_name)) {
980                 cprintf("%d Blank floor name not allowed.\n",
981                         ERROR + ILLEGAL_VALUE);
982                 return;
983         }
984
985         for (a = 0; a < MAXFLOORS; ++a) {
986                 CtdlGetFloor(&flbuf, a);
987
988                 /* note any free slots while we're scanning... */
989                 if (((flbuf.f_flags & F_INUSE) == 0)
990                     && (free_slot < 0))
991                         free_slot = a;
992
993                 /* check to see if it already exists */
994                 if ((!strcasecmp(flbuf.f_name, new_floor_name))
995                     && (flbuf.f_flags & F_INUSE)) {
996                         cprintf("%d Floor '%s' already exists.\n",
997                                 ERROR + ALREADY_EXISTS,
998                                 flbuf.f_name);
999                         return;
1000                 }
1001         }
1002
1003         if (free_slot < 0) {
1004                 cprintf("%d There is no space available for a new floor.\n",
1005                         ERROR + INVALID_FLOOR_OPERATION);
1006                 return;
1007         }
1008         if (cflr_ok == 0) {
1009                 cprintf("%d ok to create...\n", CIT_OK);
1010                 return;
1011         }
1012         lgetfloor(&flbuf, free_slot);
1013         flbuf.f_flags = F_INUSE;
1014         flbuf.f_ref_count = 0;
1015         safestrncpy(flbuf.f_name, new_floor_name, sizeof flbuf.f_name);
1016         lputfloor(&flbuf, free_slot);
1017         cprintf("%d %d\n", CIT_OK, free_slot);
1018 }
1019
1020
1021
1022 /*
1023  * delete a floor
1024  */
1025 void cmd_kflr(char *argbuf)
1026 {
1027         struct floor flbuf;
1028         int floor_to_delete;
1029         int kflr_ok;
1030         int delete_ok;
1031
1032         floor_to_delete = extract_int(argbuf, 0);
1033         kflr_ok = extract_int(argbuf, 1);
1034
1035         if (CtdlAccessCheck(ac_aide)) return;
1036
1037         lgetfloor(&flbuf, floor_to_delete);
1038
1039         delete_ok = 1;
1040         if ((flbuf.f_flags & F_INUSE) == 0) {
1041                 cprintf("%d Floor %d not in use.\n",
1042                         ERROR + INVALID_FLOOR_OPERATION, floor_to_delete);
1043                 delete_ok = 0;
1044         } else {
1045                 if (flbuf.f_ref_count != 0) {
1046                         cprintf("%d Cannot delete; floor contains %d rooms.\n",
1047                                 ERROR + INVALID_FLOOR_OPERATION,
1048                                 flbuf.f_ref_count);
1049                         delete_ok = 0;
1050                 } else {
1051                         if (kflr_ok == 1) {
1052                                 cprintf("%d Ok\n", CIT_OK);
1053                         } else {
1054                                 cprintf("%d Ok to delete...\n", CIT_OK);
1055                         }
1056
1057                 }
1058
1059         }
1060
1061         if ((delete_ok == 1) && (kflr_ok == 1))
1062                 flbuf.f_flags = 0;
1063         lputfloor(&flbuf, floor_to_delete);
1064 }
1065
1066 /*
1067  * edit a floor
1068  */
1069 void cmd_eflr(char *argbuf)
1070 {
1071         struct floor flbuf;
1072         int floor_num;
1073         int np;
1074
1075         np = num_parms(argbuf);
1076         if (np < 1) {
1077                 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1078                 return;
1079         }
1080
1081         if (CtdlAccessCheck(ac_aide)) return;
1082
1083         floor_num = extract_int(argbuf, 0);
1084         lgetfloor(&flbuf, floor_num);
1085         if ((flbuf.f_flags & F_INUSE) == 0) {
1086                 lputfloor(&flbuf, floor_num);
1087                 cprintf("%d Floor %d is not in use.\n",
1088                         ERROR + INVALID_FLOOR_OPERATION, floor_num);
1089                 return;
1090         }
1091         if (np >= 2)
1092                 extract_token(flbuf.f_name, argbuf, 1, '|', sizeof flbuf.f_name);
1093         lputfloor(&flbuf, floor_num);
1094
1095         cprintf("%d Ok\n", CIT_OK);
1096 }
1097
1098
1099
1100 /* 
1101  * cmd_stat()  -  return the modification time of the current room (maybe other things in the future)
1102  */
1103 void cmd_stat(char *gargs)
1104 {
1105         if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
1106         CtdlGetRoom(&CC->room, CC->room.QRname);
1107         cprintf("%d %s|%ld|\n", CIT_OK, CC->room.QRname, CC->room.QRmtime);
1108 }
1109
1110
1111
1112
1113 /*****************************************************************************/
1114 /*                      MODULE INITIALIZATION STUFF                          */
1115 /*****************************************************************************/
1116
1117 CTDL_MODULE_INIT(rooms)
1118 {
1119         if (!threading) {
1120                 CtdlRegisterProtoHook(cmd_lrms, "LRMS", "List rooms");
1121                 CtdlRegisterProtoHook(cmd_lkra, "LKRA", "List all known rooms");
1122                 CtdlRegisterProtoHook(cmd_lkrn, "LKRN", "List known rooms with new messages");
1123                 CtdlRegisterProtoHook(cmd_lkro, "LKRO", "List known rooms without new messages");
1124                 CtdlRegisterProtoHook(cmd_lzrm, "LZRM", "List zapped rooms");
1125                 CtdlRegisterProtoHook(cmd_lprm, "LPRM", "List public rooms");
1126                 CtdlRegisterProtoHook(cmd_goto, "GOTO", "Goto a named room");
1127                 CtdlRegisterProtoHook(cmd_stat, "STAT", "Get mtime of the current room");
1128                 CtdlRegisterProtoHook(cmd_whok, "WHOK", "List users who know this room");
1129                 CtdlRegisterProtoHook(cmd_rdir, "RDIR", "List files in room directory");
1130                 CtdlRegisterProtoHook(cmd_getr, "GETR", "Get room parameters");
1131                 CtdlRegisterProtoHook(cmd_setr, "SETR", "Set room parameters");
1132                 CtdlRegisterProtoHook(cmd_geta, "GETA", "Get the room admin name");
1133                 CtdlRegisterProtoHook(cmd_seta, "SETA", "Set the room admin for this room");
1134                 CtdlRegisterProtoHook(cmd_rinf, "RINF", "Fetch room info file");
1135                 CtdlRegisterProtoHook(cmd_kill, "KILL", "Kill (delete) the current room");
1136                 CtdlRegisterProtoHook(cmd_cre8, "CRE8", "Create a new room");
1137                 CtdlRegisterProtoHook(cmd_einf, "EINF", "Enter info file for the current room");
1138                 CtdlRegisterProtoHook(cmd_lflr, "LFLR", "List all known floors");
1139                 CtdlRegisterProtoHook(cmd_cflr, "CFLR", "Create a new floor");
1140                 CtdlRegisterProtoHook(cmd_kflr, "KFLR", "Kill a floor");
1141                 CtdlRegisterProtoHook(cmd_eflr, "EFLR", "Edit a floor");
1142         }
1143         /* return our Subversion id for the Log */
1144         return "rooms";
1145 }