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