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