4 * Server functions which handle file transfers and room directories.
20 #if TIME_WITH_SYS_TIME
21 # include <sys/time.h>
25 # include <sys/time.h>
32 #include <libcitadel.h>
37 #include "sysdep_decls.h"
42 #include "citserver.h"
49 #include "ctdl_module.h"
52 * network_talking_to() -- concurrency checker
54 int network_talking_to(char *nodename, int operation) {
56 static char *nttlist = NULL;
62 begin_critical_section(S_NTTLIST);
67 if (nttlist == NULL) nttlist = strdup("");
68 if (nttlist == NULL) break;
69 nttlist = (char *)realloc(nttlist,
70 (strlen(nttlist) + strlen(nodename) + 3) );
72 strcat(nttlist, nodename);
76 if (nttlist == NULL) break;
77 if (IsEmptyStr(nttlist)) break;
78 ptr = malloc(strlen(nttlist));
79 if (ptr == NULL) break;
81 for (i = 0; i < num_tokens(nttlist, '|'); ++i) {
82 extract_token(buf, nttlist, i, '|', sizeof buf);
83 if ( (!IsEmptyStr(buf))
84 && (strcasecmp(buf, nodename)) ) {
94 if (nttlist == NULL) break;
95 if (IsEmptyStr(nttlist)) break;
96 for (i = 0; i < num_tokens(nttlist, '|'); ++i) {
97 extract_token(buf, nttlist, i, '|', sizeof buf);
98 if (!strcasecmp(buf, nodename)) ++retval;
103 if (nttlist != NULL) CtdlLogPrintf(CTDL_DEBUG, "nttlist=<%s>\n", nttlist);
104 end_critical_section(S_NTTLIST);
112 * Server command to delete a file from a room's directory
114 void cmd_delf(char *filename)
119 if (CtdlAccessCheck(ac_room_aide))
122 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
123 cprintf("%d No directory in this room.\n",
128 if (IsEmptyStr(filename)) {
129 cprintf("%d You must specify a file name.\n",
130 ERROR + FILE_NOT_FOUND);
133 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
134 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
138 snprintf(pathname, sizeof pathname,
141 CC->room.QRdirname, filename);
142 a = unlink(pathname);
144 cprintf("%d File '%s' deleted.\n", CIT_OK, pathname);
147 cprintf("%d File '%s' not found.\n",
148 ERROR + FILE_NOT_FOUND, pathname);
156 * move a file from one room directory to another
158 void cmd_movf(char *cmdbuf)
160 char filename[PATH_MAX];
161 char pathname[PATH_MAX];
162 char newpath[PATH_MAX];
163 char newroom[ROOMNAMELEN];
166 struct ctdlroom qrbuf;
169 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
170 extract_token(newroom, cmdbuf, 1, '|', sizeof newroom);
172 if (CtdlAccessCheck(ac_room_aide)) return;
174 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
175 cprintf("%d No directory in this room.\n",
180 if (IsEmptyStr(filename)) {
181 cprintf("%d You must specify a file name.\n",
182 ERROR + FILE_NOT_FOUND);
186 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
187 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
191 snprintf(pathname, sizeof pathname, "./files/%s/%s",
192 CC->room.QRdirname, filename);
193 if (access(pathname, 0) != 0) {
194 cprintf("%d File '%s' not found.\n",
195 ERROR + FILE_NOT_FOUND, pathname);
199 if (CtdlGetRoom(&qrbuf, newroom) != 0) {
200 cprintf("%d '%s' does not exist.\n", ERROR + ROOM_NOT_FOUND, newroom);
203 if ((qrbuf.QRflags & QR_DIRECTORY) == 0) {
204 cprintf("%d '%s' is not a directory room.\n",
205 ERROR + NOT_HERE, qrbuf.QRname);
208 snprintf(newpath, sizeof newpath, "./files/%s/%s", qrbuf.QRdirname,
210 if (link(pathname, newpath) != 0) {
211 cprintf("%d Couldn't move file: %s\n", ERROR + INTERNAL_ERROR,
217 /* this is a crude method of copying the file description */
218 snprintf(buf, sizeof buf,
219 "cat ./files/%s/filedir |grep \"%s\" >>./files/%s/filedir",
220 CC->room.QRdirname, filename, qrbuf.QRdirname);
222 cprintf("%d File '%s' has been moved.\n", CIT_OK, filename);
227 * This code is common to all commands which open a file for downloading,
228 * regardless of whether it's a file from the directory, an image, a network
229 * spool file, a MIME attachment, etc.
230 * It examines the file and displays the OK result code and some information
231 * about the file. NOTE: this stuff is Unix dependent.
233 void OpenCmdResult(char *filename, const char *mime_type)
239 fstat(fileno(CC->download_fp), &statbuf);
240 CC->download_fp_total = statbuf.st_size;
241 filesize = (long) statbuf.st_size;
242 modtime = (time_t) statbuf.st_mtime;
244 cprintf("%d %ld|%ld|%s|%s\n",
245 CIT_OK, filesize, (long)modtime, filename, mime_type);
250 * open a file for downloading
252 void cmd_open(char *cmdbuf)
255 char pathname[PATH_MAX];
258 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
260 if (CtdlAccessCheck(ac_logged_in)) return;
262 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
263 cprintf("%d No directory in this room.\n",
268 if (IsEmptyStr(filename)) {
269 cprintf("%d You must specify a file name.\n",
270 ERROR + FILE_NOT_FOUND);
274 if (CC->download_fp != NULL) {
275 cprintf("%d You already have a download file open.\n",
276 ERROR + RESOURCE_BUSY);
280 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
281 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
286 snprintf(pathname, sizeof pathname,
289 CC->room.QRdirname, filename);
290 CC->download_fp = fopen(pathname, "r");
292 if (CC->download_fp == NULL) {
293 cprintf("%d cannot open %s: %s\n",
294 ERROR + INTERNAL_ERROR, pathname, strerror(errno));
298 OpenCmdResult(filename, "application/octet-stream");
304 void cmd_oimg(char *cmdbuf)
307 char pathname[PATH_MAX];
308 char MimeTestBuf[32];
309 struct ctdluser usbuf;
310 char which_user[USERNAME_SIZE];
315 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
317 if (IsEmptyStr(filename)) {
318 cprintf("%d You must specify a file name.\n",
319 ERROR + FILE_NOT_FOUND);
323 if (CC->download_fp != NULL) {
324 cprintf("%d You already have a download file open.\n",
325 ERROR + RESOURCE_BUSY);
329 if (!strcasecmp(filename, "_userpic_")) {
330 extract_token(which_user, cmdbuf, 1, '|', sizeof which_user);
331 if (CtdlGetUser(&usbuf, which_user) != 0) {
332 cprintf("%d No such user.\n",
333 ERROR + NO_SUCH_USER);
336 snprintf(pathname, sizeof pathname,
340 } else if (!strcasecmp(filename, "_floorpic_")) {
341 which_floor = extract_int(cmdbuf, 1);
342 snprintf(pathname, sizeof pathname,
344 ctdl_image_dir, which_floor);
345 } else if (!strcasecmp(filename, "_roompic_")) {
346 assoc_file_name(pathname, sizeof pathname, &CC->room, ctdl_image_dir);
348 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
349 filename[a] = tolower(filename[a]);
350 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
354 snprintf(pathname, sizeof pathname,
360 CC->download_fp = fopen(pathname, "rb");
361 if (CC->download_fp == NULL) {
362 strcat(pathname, ".gif");
363 CC->download_fp = fopen(pathname, "rb");
365 if (CC->download_fp == NULL) {
366 cprintf("%d Cannot open %s: %s\n",
367 ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
370 rv = fread(&MimeTestBuf[0], 1, 32, CC->download_fp);
371 rewind (CC->download_fp);
372 OpenCmdResult(pathname, GuessMimeType(&MimeTestBuf[0], 32));
376 * open a file for uploading
378 void cmd_uopn(char *cmdbuf)
382 extract_token(CC->upl_file, cmdbuf, 0, '|', sizeof CC->upl_file);
383 extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
384 extract_token(CC->upl_comment, cmdbuf, 2, '|', sizeof CC->upl_comment);
386 if (CtdlAccessCheck(ac_logged_in)) return;
388 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
389 cprintf("%d No directory in this room.\n",
394 if (IsEmptyStr(CC->upl_file)) {
395 cprintf("%d You must specify a file name.\n",
396 ERROR + FILE_NOT_FOUND);
400 if (CC->upload_fp != NULL) {
401 cprintf("%d You already have a upload file open.\n",
402 ERROR + RESOURCE_BUSY);
406 for (a = 0; !IsEmptyStr(&CC->upl_file[a]); ++a) {
407 if ( (CC->upl_file[a] == '/') || (CC->upl_file[a] == '\\') ) {
408 CC->upl_file[a] = '_';
411 snprintf(CC->upl_path, sizeof CC->upl_path,
414 CC->room.QRdirname, CC->upl_file);
415 snprintf(CC->upl_filedir, sizeof CC->upl_filedir,
420 CC->upload_fp = fopen(CC->upl_path, "r");
421 if (CC->upload_fp != NULL) {
422 fclose(CC->upload_fp);
423 CC->upload_fp = NULL;
424 cprintf("%d '%s' already exists\n",
425 ERROR + ALREADY_EXISTS, CC->upl_path);
429 CC->upload_fp = fopen(CC->upl_path, "wb");
430 if (CC->upload_fp == NULL) {
431 cprintf("%d Cannot open %s: %s\n",
432 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
435 cprintf("%d Ok\n", CIT_OK);
441 * open an image file for uploading
443 void cmd_uimg(char *cmdbuf)
445 int is_this_for_real;
450 if (num_parms(cmdbuf) < 2) {
451 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
455 is_this_for_real = extract_int(cmdbuf, 0);
456 extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
457 extract_token(basenm, cmdbuf, 2, '|', sizeof basenm);
458 if (CC->upload_fp != NULL) {
459 cprintf("%d You already have an upload file open.\n",
460 ERROR + RESOURCE_BUSY);
464 strcpy(CC->upl_path, "");
466 for (a = 0; !IsEmptyStr(&basenm[a]); ++a) {
467 basenm[a] = tolower(basenm[a]);
468 if ( (basenm[a] == '/') || (basenm[a] == '\\') ) {
473 if (CC->user.axlevel >= 6) {
474 snprintf(CC->upl_path, sizeof CC->upl_path,
480 if (!strcasecmp(basenm, "_userpic_")) {
481 snprintf(CC->upl_path, sizeof CC->upl_path,
487 if ((!strcasecmp(basenm, "_floorpic_"))
488 && (CC->user.axlevel >= 6)) {
489 which_floor = extract_int(cmdbuf, 2);
490 snprintf(CC->upl_path, sizeof CC->upl_path,
496 if ((!strcasecmp(basenm, "_roompic_")) && (is_room_aide())) {
497 assoc_file_name(CC->upl_path, sizeof CC->upl_path, &CC->room, ctdl_image_dir);
500 if (IsEmptyStr(CC->upl_path)) {
501 cprintf("%d Higher access required.\n",
502 ERROR + HIGHER_ACCESS_REQUIRED);
506 if (is_this_for_real == 0) {
507 cprintf("%d Ok to send image\n", CIT_OK);
511 CC->upload_fp = fopen(CC->upl_path, "wb");
512 if (CC->upload_fp == NULL) {
513 cprintf("%d Cannot open %s: %s\n",
514 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
517 cprintf("%d Ok\n", CIT_OK);
518 CC->upload_type = UPL_IMAGE;
523 * close the download file
525 void cmd_clos(char *cmdbuf)
529 if (CC->download_fp == NULL) {
530 cprintf("%d You don't have a download file open.\n",
531 ERROR + RESOURCE_NOT_OPEN);
535 fclose(CC->download_fp);
536 CC->download_fp = NULL;
538 if (CC->dl_is_net == 1) {
540 snprintf(buf, sizeof buf,
547 cprintf("%d Ok\n", CIT_OK);
554 void abort_upl(CitContext *who)
556 if (who->upload_fp != NULL) {
557 fclose(who->upload_fp);
558 who->upload_fp = NULL;
559 unlink(CC->upl_path);
566 * close the upload file
568 void cmd_ucls(char *cmd)
571 char upload_notice[512];
573 if (CC->upload_fp == NULL) {
574 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
578 fclose(CC->upload_fp);
579 CC->upload_fp = NULL;
581 if ((!strcasecmp(cmd, "1")) && (CC->upload_type != UPL_FILE)) {
582 CC->upload_type = UPL_FILE;
583 cprintf("%d Upload completed.\n", CIT_OK);
585 /* FIXME ... here we need to trigger a network run */
590 if (!strcasecmp(cmd, "1")) {
591 cprintf("%d File '%s' saved.\n", CIT_OK, CC->upl_path);
592 fp = fopen(CC->upl_filedir, "a");
594 fp = fopen(CC->upl_filedir, "w");
597 fprintf(fp, "%s %s %s\n", CC->upl_file,
603 /* put together an upload notice */
604 snprintf(upload_notice, sizeof upload_notice,
605 "NEW UPLOAD: '%s'\n %s\n%s\n",
609 quickie_message(CC->curr_user, NULL, NULL, CC->room.QRname,
610 upload_notice, 0, NULL);
613 cprintf("%d File '%s' aborted.\n", CIT_OK, CC->upl_path);
620 * read from the download file
622 void cmd_read(char *cmdbuf)
629 start_pos = extract_long(cmdbuf, 0);
630 bytes = extract_int(cmdbuf, 1);
632 if (CC->download_fp == NULL) {
633 cprintf("%d You don't have a download file open.\n",
634 ERROR + RESOURCE_NOT_OPEN);
639 CC->download_fp_total,
642 fileno(CC->download_fp),
645 actual_bytes = CC->download_fp_total - start_pos;
646 if ((actual_bytes > 0) && (buf != NULL)) {
647 cprintf("%d %d\n", BINARY_FOLLOWS, (int)actual_bytes);
648 client_write(buf + start_pos, actual_bytes);
651 cprintf("%d %s\n", ERROR, strerror(errno));
653 munmap(buf, CC->download_fp_total);
659 * write to the upload file
661 void cmd_writ(char *cmdbuf)
669 bytes = extract_int(cmdbuf, 0);
671 if (CC->upload_fp == NULL) {
672 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
676 if (bytes > 100000) {
677 cprintf("%d You may not write more than 100000 bytes.\n",
682 cprintf("%d %d\n", SEND_BINARY, bytes);
683 buf = malloc(bytes + 1);
684 client_read(buf, bytes);
685 rv = fwrite(buf, bytes, 1, CC->upload_fp);
693 * cmd_ndop() - open a network spool file for downloading
695 void cmd_ndop(char *cmdbuf)
700 if (IsEmptyStr(CC->net_node)) {
701 cprintf("%d Not authenticated as a network node.\n",
702 ERROR + NOT_LOGGED_IN);
706 if (CC->download_fp != NULL) {
707 cprintf("%d You already have a download file open.\n",
708 ERROR + RESOURCE_BUSY);
712 snprintf(pathname, sizeof pathname,
717 /* first open the file in append mode in order to create a
718 * zero-length file if it doesn't already exist
720 CC->download_fp = fopen(pathname, "a");
721 if (CC->download_fp != NULL)
722 fclose(CC->download_fp);
725 CC->download_fp = fopen(pathname, "r");
726 if (CC->download_fp == NULL) {
727 cprintf("%d cannot open %s: %s\n",
728 ERROR + INTERNAL_ERROR, pathname, strerror(errno));
733 /* set this flag so other routines know that the download file
734 * currently open is a network spool file
738 stat(pathname, &statbuf);
739 CC->download_fp_total = statbuf.st_size;
740 cprintf("%d %ld\n", CIT_OK, (long)statbuf.st_size);
744 * cmd_nuop() - open a network spool file for uploading
746 void cmd_nuop(char *cmdbuf)
750 if (IsEmptyStr(CC->net_node)) {
751 cprintf("%d Not authenticated as a network node.\n",
752 ERROR + NOT_LOGGED_IN);
756 if (CC->upload_fp != NULL) {
757 cprintf("%d You already have an upload file open.\n",
758 ERROR + RESOURCE_BUSY);
762 snprintf(CC->upl_path, sizeof CC->upl_path,
769 CC->upload_fp = fopen(CC->upl_path, "r");
770 if (CC->upload_fp != NULL) {
771 fclose(CC->upload_fp);
772 CC->upload_fp = NULL;
773 cprintf("%d '%s' already exists\n",
774 ERROR + ALREADY_EXISTS, CC->upl_path);
778 CC->upload_fp = fopen(CC->upl_path, "w");
779 if (CC->upload_fp == NULL) {
780 cprintf("%d Cannot open %s: %s\n",
781 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
785 CC->upload_type = UPL_NET;
786 cprintf("%d Ok\n", CIT_OK);
790 /*****************************************************************************/
791 /* MODULE INITIALIZATION STUFF */
792 /*****************************************************************************/
794 CTDL_MODULE_INIT(file_ops)
797 CtdlRegisterProtoHook(cmd_delf, "DELF", "Autoconverted. TODO: document me.");
798 CtdlRegisterProtoHook(cmd_movf, "MOVF", "Autoconverted. TODO: document me.");
799 CtdlRegisterProtoHook(cmd_open, "OPEN", "Autoconverted. TODO: document me.");
800 CtdlRegisterProtoHook(cmd_clos, "CLOS", "Autoconverted. TODO: document me.");
801 CtdlRegisterProtoHook(cmd_uopn, "UOPN", "Autoconverted. TODO: document me.");
802 CtdlRegisterProtoHook(cmd_ucls, "UCLS", "Autoconverted. TODO: document me.");
803 CtdlRegisterProtoHook(cmd_read, "READ", "Autoconverted. TODO: document me.");
804 CtdlRegisterProtoHook(cmd_writ, "WRIT", "Autoconverted. TODO: document me.");
805 CtdlRegisterProtoHook(cmd_oimg, "OIMG", "Autoconverted. TODO: document me.");
806 CtdlRegisterProtoHook(cmd_ndop, "NDOP", "Autoconverted. TODO: document me.");
807 CtdlRegisterProtoHook(cmd_nuop, "NUOP", "Autoconverted. TODO: document me.");
808 CtdlRegisterProtoHook(cmd_uimg, "UIMG", "Autoconverted. TODO: document me.");
810 /* return our Subversion id for the Log */