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