4 * Server functions which handle file transfers and room directories.
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
24 # include <sys/time.h>
31 #include <libcitadel.h>
36 #include "sysdep_decls.h"
41 #include "citserver.h"
49 * network_talking_to() -- concurrency checker
51 int network_talking_to(char *nodename, int operation) {
53 static char *nttlist = NULL;
59 begin_critical_section(S_NTTLIST);
64 if (nttlist == NULL) nttlist = strdup("");
65 if (nttlist == NULL) break;
66 nttlist = (char *)realloc(nttlist,
67 (strlen(nttlist) + strlen(nodename) + 3) );
69 strcat(nttlist, nodename);
73 if (nttlist == NULL) break;
74 if (IsEmptyStr(nttlist)) break;
75 ptr = malloc(strlen(nttlist));
76 if (ptr == NULL) break;
78 for (i = 0; i < num_tokens(nttlist, '|'); ++i) {
79 extract_token(buf, nttlist, i, '|', sizeof buf);
80 if ( (!IsEmptyStr(buf))
81 && (strcasecmp(buf, nodename)) ) {
91 if (nttlist == NULL) break;
92 if (IsEmptyStr(nttlist)) break;
93 for (i = 0; i < num_tokens(nttlist, '|'); ++i) {
94 extract_token(buf, nttlist, i, '|', sizeof buf);
95 if (!strcasecmp(buf, nodename)) ++retval;
100 if (nttlist != NULL) lprintf(CTDL_DEBUG, "nttlist=<%s>\n", nttlist);
101 end_critical_section(S_NTTLIST);
109 * Server command to delete a file from a room's directory
111 void cmd_delf(char *filename)
116 if (CtdlAccessCheck(ac_room_aide))
119 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
120 cprintf("%d No directory in this room.\n",
125 if (IsEmptyStr(filename)) {
126 cprintf("%d You must specify a file name.\n",
127 ERROR + FILE_NOT_FOUND);
130 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
131 if (filename[a] == '/') {
135 snprintf(pathname, sizeof pathname,
138 CC->room.QRdirname, filename);
139 a = unlink(pathname);
141 cprintf("%d File '%s' deleted.\n", CIT_OK, pathname);
144 cprintf("%d File '%s' not found.\n",
145 ERROR + FILE_NOT_FOUND, pathname);
153 * move a file from one room directory to another
155 void cmd_movf(char *cmdbuf)
157 char filename[PATH_MAX];
158 char pathname[PATH_MAX];
159 char newpath[PATH_MAX];
160 char newroom[ROOMNAMELEN];
163 struct ctdlroom qrbuf;
165 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
166 extract_token(newroom, cmdbuf, 1, '|', sizeof newroom);
168 if (CtdlAccessCheck(ac_room_aide)) return;
170 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
171 cprintf("%d No directory in this room.\n",
176 if (IsEmptyStr(filename)) {
177 cprintf("%d You must specify a file name.\n",
178 ERROR + FILE_NOT_FOUND);
182 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
183 if (filename[a] == '/') {
187 snprintf(pathname, sizeof pathname, "./files/%s/%s",
188 CC->room.QRdirname, filename);
189 if (access(pathname, 0) != 0) {
190 cprintf("%d File '%s' not found.\n",
191 ERROR + FILE_NOT_FOUND, pathname);
195 if (getroom(&qrbuf, newroom) != 0) {
196 cprintf("%d '%s' does not exist.\n", ERROR + ROOM_NOT_FOUND, newroom);
199 if ((qrbuf.QRflags & QR_DIRECTORY) == 0) {
200 cprintf("%d '%s' is not a directory room.\n",
201 ERROR + NOT_HERE, qrbuf.QRname);
204 snprintf(newpath, sizeof newpath, "./files/%s/%s", qrbuf.QRdirname,
206 if (link(pathname, newpath) != 0) {
207 cprintf("%d Couldn't move file: %s\n", ERROR + INTERNAL_ERROR,
213 /* this is a crude method of copying the file description */
214 snprintf(buf, sizeof buf,
215 "cat ./files/%s/filedir |grep \"%s\" >>./files/%s/filedir",
216 CC->room.QRdirname, filename, qrbuf.QRdirname);
218 cprintf("%d File '%s' has been moved.\n", CIT_OK, filename);
223 * send a file over the net
225 void cmd_netf(char *cmdbuf)
227 char pathname[256], filename[256], destsys[256], buf[256];
234 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
235 extract_token(destsys, cmdbuf, 1, '|', sizeof destsys);
237 if (CtdlAccessCheck(ac_room_aide)) return;
239 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
240 cprintf("%d No directory in this room.\n",
245 if (IsEmptyStr(filename)) {
246 cprintf("%d You must specify a file name.\n",
247 ERROR + FILE_NOT_FOUND);
251 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
252 if (filename[a] == '/') {
256 snprintf(pathname, sizeof pathname, "./files/%s/%s",
257 CC->room.QRdirname, filename);
258 if (access(pathname, 0) != 0) {
259 cprintf("%d File '%s' not found.\n",
260 ERROR + FILE_NOT_FOUND, pathname);
263 snprintf(buf, sizeof buf, "sysop@%s", destsys);
265 if (e != MES_IGNET) {
266 cprintf("%d No such system: '%s'\n",
267 ERROR + NO_SUCH_SYSTEM, destsys);
270 snprintf(outfile, sizeof outfile,
273 (long)getpid(), ++seq);
274 ofp = fopen(outfile, "a");
276 cprintf("%d internal error\n", ERROR + INTERNAL_ERROR);
281 putc(MES_NORMAL, ofp);
283 fprintf(ofp, "P%s", CC->user.fullname);
286 fprintf(ofp, "T%ld", (long) now);
288 fprintf(ofp, "A%s", CC->user.fullname);
290 fprintf(ofp, "O%s", CC->room.QRname);
292 fprintf(ofp, "N%s", NODENAME);
294 fprintf(ofp, "D%s", destsys);
296 fprintf(ofp, "SFILE");
301 snprintf(buf, sizeof buf,
302 "cd %s/%s; uuencode %s <%s 2>/dev/null >>%s",
304 /* FIXME: detect uuencode while installation? or inline */
305 CC->room.QRdirname, filename, filename, outfile);
308 ofp = fopen(outfile, "a");
312 cprintf("%d File '%s' has been sent to %s.\n", CIT_OK, filename,
314 /* FIXME start a network run here. */
319 * This code is common to all commands which open a file for downloading,
320 * regardless of whether it's a file from the directory, an image, a network
321 * spool file, a MIME attachment, etc.
322 * It examines the file and displays the OK result code and some information
323 * about the file. NOTE: this stuff is Unix dependent.
325 void OpenCmdResult(char *filename, const char *mime_type)
331 fstat(fileno(CC->download_fp), &statbuf);
332 filesize = (long) statbuf.st_size;
333 modtime = (time_t) statbuf.st_mtime;
335 cprintf("%d %ld|%ld|%s|%s\n",
336 CIT_OK, filesize, (long)modtime, filename, mime_type);
341 * open a file for downloading
343 void cmd_open(char *cmdbuf)
346 char pathname[PATH_MAX];
349 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
351 if (CtdlAccessCheck(ac_logged_in)) return;
353 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
354 cprintf("%d No directory in this room.\n",
359 if (IsEmptyStr(filename)) {
360 cprintf("%d You must specify a file name.\n",
361 ERROR + FILE_NOT_FOUND);
365 if (CC->download_fp != NULL) {
366 cprintf("%d You already have a download file open.\n",
367 ERROR + RESOURCE_BUSY);
371 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
372 if (filename[a] == '/') {
377 snprintf(pathname, sizeof pathname,
380 CC->room.QRdirname, filename);
381 CC->download_fp = fopen(pathname, "r");
383 if (CC->download_fp == NULL) {
384 cprintf("%d cannot open %s: %s\n",
385 ERROR + INTERNAL_ERROR, pathname, strerror(errno));
389 OpenCmdResult(filename, "application/octet-stream");
395 void cmd_oimg(char *cmdbuf)
398 char pathname[PATH_MAX];
399 char MimeTestBuf[32];
400 struct ctdluser usbuf;
401 char which_user[USERNAME_SIZE];
405 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
407 if (IsEmptyStr(filename)) {
408 cprintf("%d You must specify a file name.\n",
409 ERROR + FILE_NOT_FOUND);
413 if (CC->download_fp != NULL) {
414 cprintf("%d You already have a download file open.\n",
415 ERROR + RESOURCE_BUSY);
419 if (!strcasecmp(filename, "_userpic_")) {
420 extract_token(which_user, cmdbuf, 1, '|', sizeof which_user);
421 if (getuser(&usbuf, which_user) != 0) {
422 cprintf("%d No such user.\n",
423 ERROR + NO_SUCH_USER);
426 snprintf(pathname, sizeof pathname,
430 } else if (!strcasecmp(filename, "_floorpic_")) {
431 which_floor = extract_int(cmdbuf, 1);
432 snprintf(pathname, sizeof pathname,
434 ctdl_image_dir, which_floor);
435 } else if (!strcasecmp(filename, "_roompic_")) {
436 assoc_file_name(pathname, sizeof pathname, &CC->room, ctdl_image_dir);
438 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
439 filename[a] = tolower(filename[a]);
440 if (filename[a] == '/') {
444 snprintf(pathname, sizeof pathname,
450 CC->download_fp = fopen(pathname, "rb");
451 if (CC->download_fp == NULL) {
452 strcat(pathname, ".gif");
453 CC->download_fp = fopen(pathname, "rb");
455 if (CC->download_fp == NULL) {
456 cprintf("%d Cannot open %s: %s\n",
457 ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
460 fread(&MimeTestBuf[0], 1, 32, CC->download_fp);
461 rewind (CC->download_fp);
462 OpenCmdResult(pathname, GuessMimeType(&MimeTestBuf[0], 32));
466 * open a file for uploading
468 void cmd_uopn(char *cmdbuf)
472 extract_token(CC->upl_file, cmdbuf, 0, '|', sizeof CC->upl_file);
473 extract_token(CC->upl_comment, cmdbuf, 1, '|', sizeof CC->upl_comment);
475 if (CtdlAccessCheck(ac_logged_in)) return;
477 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
478 cprintf("%d No directory in this room.\n",
483 if (IsEmptyStr(CC->upl_file)) {
484 cprintf("%d You must specify a file name.\n",
485 ERROR + FILE_NOT_FOUND);
489 if (CC->upload_fp != NULL) {
490 cprintf("%d You already have a upload file open.\n",
491 ERROR + RESOURCE_BUSY);
495 for (a = 0; !IsEmptyStr(&CC->upl_file[a]); ++a) {
496 if (CC->upl_file[a] == '/') {
497 CC->upl_file[a] = '_';
500 snprintf(CC->upl_path, sizeof CC->upl_path,
503 CC->room.QRdirname, CC->upl_file);
504 snprintf(CC->upl_filedir, sizeof CC->upl_filedir,
509 CC->upload_fp = fopen(CC->upl_path, "r");
510 if (CC->upload_fp != NULL) {
511 fclose(CC->upload_fp);
512 CC->upload_fp = NULL;
513 cprintf("%d '%s' already exists\n",
514 ERROR + ALREADY_EXISTS, CC->upl_path);
518 CC->upload_fp = fopen(CC->upl_path, "wb");
519 if (CC->upload_fp == NULL) {
520 cprintf("%d Cannot open %s: %s\n",
521 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
524 cprintf("%d Ok\n", CIT_OK);
530 * open an image file for uploading
532 void cmd_uimg(char *cmdbuf)
534 int is_this_for_real;
539 if (num_parms(cmdbuf) < 2) {
540 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
544 is_this_for_real = extract_int(cmdbuf, 0);
545 extract_token(basenm, cmdbuf, 1, '|', sizeof basenm);
546 if (CC->upload_fp != NULL) {
547 cprintf("%d You already have an upload file open.\n",
548 ERROR + RESOURCE_BUSY);
552 strcpy(CC->upl_path, "");
554 for (a = 0; !IsEmptyStr(&basenm[a]); ++a) {
555 basenm[a] = tolower(basenm[a]);
556 if (basenm[a] == '/') {
561 if (CC->user.axlevel >= 6) {
562 snprintf(CC->upl_path, sizeof CC->upl_path,
568 if (!strcasecmp(basenm, "_userpic_")) {
569 snprintf(CC->upl_path, sizeof CC->upl_path,
575 if ((!strcasecmp(basenm, "_floorpic_"))
576 && (CC->user.axlevel >= 6)) {
577 which_floor = extract_int(cmdbuf, 2);
578 snprintf(CC->upl_path, sizeof CC->upl_path,
584 if ((!strcasecmp(basenm, "_roompic_")) && (is_room_aide())) {
585 assoc_file_name(CC->upl_path, sizeof CC->upl_path, &CC->room, ctdl_image_dir);
588 if (IsEmptyStr(CC->upl_path)) {
589 cprintf("%d Higher access required.\n",
590 ERROR + HIGHER_ACCESS_REQUIRED);
594 if (is_this_for_real == 0) {
595 cprintf("%d Ok to send image\n", CIT_OK);
599 CC->upload_fp = fopen(CC->upl_path, "wb");
600 if (CC->upload_fp == NULL) {
601 cprintf("%d Cannot open %s: %s\n",
602 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
605 cprintf("%d Ok\n", CIT_OK);
606 CC->upload_type = UPL_IMAGE;
611 * close the download file
617 if (CC->download_fp == NULL) {
618 cprintf("%d You don't have a download file open.\n",
619 ERROR + RESOURCE_NOT_OPEN);
623 fclose(CC->download_fp);
624 CC->download_fp = NULL;
626 if (CC->dl_is_net == 1) {
628 snprintf(buf, sizeof buf,
635 cprintf("%d Ok\n", CIT_OK);
642 void abort_upl(struct CitContext *who)
644 if (who->upload_fp != NULL) {
645 fclose(who->upload_fp);
646 who->upload_fp = NULL;
647 unlink(CC->upl_path);
654 * close the upload file
656 void cmd_ucls(char *cmd)
659 char upload_notice[512];
661 if (CC->upload_fp == NULL) {
662 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
666 fclose(CC->upload_fp);
667 CC->upload_fp = NULL;
669 if ((!strcasecmp(cmd, "1")) && (CC->upload_type != UPL_FILE)) {
670 CC->upload_type = UPL_FILE;
671 cprintf("%d Upload completed.\n", CIT_OK);
673 /* FIXME ... here we need to trigger a network run */
678 if (!strcasecmp(cmd, "1")) {
679 cprintf("%d File '%s' saved.\n", CIT_OK, CC->upl_path);
680 fp = fopen(CC->upl_filedir, "a");
682 fp = fopen(CC->upl_filedir, "w");
685 fprintf(fp, "%s %s\n", CC->upl_file,
690 /* put together an upload notice */
691 snprintf(upload_notice, sizeof upload_notice,
692 "NEW UPLOAD: '%s'\n %s\n",
693 CC->upl_file, CC->upl_comment);
694 quickie_message(CC->curr_user, NULL, NULL, CC->room.QRname,
695 upload_notice, 0, NULL);
698 cprintf("%d File '%s' aborted.\n", CIT_OK, CC->upl_path);
705 * read from the download file
707 void cmd_read(char *cmdbuf)
714 start_pos = extract_long(cmdbuf, 0);
715 bytes = extract_int(cmdbuf, 1);
717 if (CC->download_fp == NULL) {
718 cprintf("%d You don't have a download file open.\n",
719 ERROR + RESOURCE_NOT_OPEN);
723 if (bytes > 100000) bytes = 100000;
724 buf = malloc(bytes + 1);
726 fseek(CC->download_fp, start_pos, 0);
727 actual_bytes = fread(buf, 1, bytes, CC->download_fp);
728 cprintf("%d %d\n", BINARY_FOLLOWS, (int)actual_bytes);
729 client_write(buf, actual_bytes);
736 * write to the upload file
738 void cmd_writ(char *cmdbuf)
745 bytes = extract_int(cmdbuf, 0);
747 if (CC->upload_fp == NULL) {
748 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
752 if (bytes > 100000) {
753 cprintf("%d You may not write more than 100000 bytes.\n",
758 cprintf("%d %d\n", SEND_BINARY, bytes);
759 buf = malloc(bytes + 1);
760 client_read(buf, bytes);
761 fwrite(buf, bytes, 1, CC->upload_fp);
769 * cmd_ndop() - open a network spool file for downloading
771 void cmd_ndop(char *cmdbuf)
776 if (IsEmptyStr(CC->net_node)) {
777 cprintf("%d Not authenticated as a network node.\n",
778 ERROR + NOT_LOGGED_IN);
782 if (CC->download_fp != NULL) {
783 cprintf("%d You already have a download file open.\n",
784 ERROR + RESOURCE_BUSY);
788 snprintf(pathname, sizeof pathname,
793 /* first open the file in append mode in order to create a
794 * zero-length file if it doesn't already exist
796 CC->download_fp = fopen(pathname, "a");
797 if (CC->download_fp != NULL)
798 fclose(CC->download_fp);
801 CC->download_fp = fopen(pathname, "r");
802 if (CC->download_fp == NULL) {
803 cprintf("%d cannot open %s: %s\n",
804 ERROR + INTERNAL_ERROR, pathname, strerror(errno));
809 /* set this flag so other routines know that the download file
810 * currently open is a network spool file
814 stat(pathname, &statbuf);
815 cprintf("%d %ld\n", CIT_OK, (long)statbuf.st_size);
819 * cmd_nuop() - open a network spool file for uploading
821 void cmd_nuop(char *cmdbuf)
825 if (IsEmptyStr(CC->net_node)) {
826 cprintf("%d Not authenticated as a network node.\n",
827 ERROR + NOT_LOGGED_IN);
831 if (CC->upload_fp != NULL) {
832 cprintf("%d You already have an upload file open.\n",
833 ERROR + RESOURCE_BUSY);
837 snprintf(CC->upl_path, sizeof CC->upl_path,
844 CC->upload_fp = fopen(CC->upl_path, "r");
845 if (CC->upload_fp != NULL) {
846 fclose(CC->upload_fp);
847 CC->upload_fp = NULL;
848 cprintf("%d '%s' already exists\n",
849 ERROR + ALREADY_EXISTS, CC->upl_path);
853 CC->upload_fp = fopen(CC->upl_path, "w");
854 if (CC->upload_fp == NULL) {
855 cprintf("%d Cannot open %s: %s\n",
856 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
860 CC->upload_type = UPL_NET;
861 cprintf("%d Ok\n", CIT_OK);