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) CtdlLogPrintf(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_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
474 extract_token(CC->upl_comment, cmdbuf, 2, '|', sizeof CC->upl_comment);
476 if (CtdlAccessCheck(ac_logged_in)) return;
478 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
479 cprintf("%d No directory in this room.\n",
484 if (IsEmptyStr(CC->upl_file)) {
485 cprintf("%d You must specify a file name.\n",
486 ERROR + FILE_NOT_FOUND);
490 if (CC->upload_fp != NULL) {
491 cprintf("%d You already have a upload file open.\n",
492 ERROR + RESOURCE_BUSY);
496 for (a = 0; !IsEmptyStr(&CC->upl_file[a]); ++a) {
497 if (CC->upl_file[a] == '/') {
498 CC->upl_file[a] = '_';
501 snprintf(CC->upl_path, sizeof CC->upl_path,
504 CC->room.QRdirname, CC->upl_file);
505 snprintf(CC->upl_filedir, sizeof CC->upl_filedir,
510 CC->upload_fp = fopen(CC->upl_path, "r");
511 if (CC->upload_fp != NULL) {
512 fclose(CC->upload_fp);
513 CC->upload_fp = NULL;
514 cprintf("%d '%s' already exists\n",
515 ERROR + ALREADY_EXISTS, CC->upl_path);
519 CC->upload_fp = fopen(CC->upl_path, "wb");
520 if (CC->upload_fp == NULL) {
521 cprintf("%d Cannot open %s: %s\n",
522 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
525 cprintf("%d Ok\n", CIT_OK);
531 * open an image file for uploading
533 void cmd_uimg(char *cmdbuf)
535 int is_this_for_real;
540 if (num_parms(cmdbuf) < 2) {
541 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
545 is_this_for_real = extract_int(cmdbuf, 0);
546 extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
547 extract_token(basenm, cmdbuf, 2, '|', sizeof basenm);
548 if (CC->upload_fp != NULL) {
549 cprintf("%d You already have an upload file open.\n",
550 ERROR + RESOURCE_BUSY);
554 strcpy(CC->upl_path, "");
556 for (a = 0; !IsEmptyStr(&basenm[a]); ++a) {
557 basenm[a] = tolower(basenm[a]);
558 if (basenm[a] == '/') {
563 if (CC->user.axlevel >= 6) {
564 snprintf(CC->upl_path, sizeof CC->upl_path,
570 if (!strcasecmp(basenm, "_userpic_")) {
571 snprintf(CC->upl_path, sizeof CC->upl_path,
577 if ((!strcasecmp(basenm, "_floorpic_"))
578 && (CC->user.axlevel >= 6)) {
579 which_floor = extract_int(cmdbuf, 2);
580 snprintf(CC->upl_path, sizeof CC->upl_path,
586 if ((!strcasecmp(basenm, "_roompic_")) && (is_room_aide())) {
587 assoc_file_name(CC->upl_path, sizeof CC->upl_path, &CC->room, ctdl_image_dir);
590 if (IsEmptyStr(CC->upl_path)) {
591 cprintf("%d Higher access required.\n",
592 ERROR + HIGHER_ACCESS_REQUIRED);
596 if (is_this_for_real == 0) {
597 cprintf("%d Ok to send image\n", CIT_OK);
601 CC->upload_fp = fopen(CC->upl_path, "wb");
602 if (CC->upload_fp == NULL) {
603 cprintf("%d Cannot open %s: %s\n",
604 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
607 cprintf("%d Ok\n", CIT_OK);
608 CC->upload_type = UPL_IMAGE;
613 * close the download file
619 if (CC->download_fp == NULL) {
620 cprintf("%d You don't have a download file open.\n",
621 ERROR + RESOURCE_NOT_OPEN);
625 fclose(CC->download_fp);
626 CC->download_fp = NULL;
628 if (CC->dl_is_net == 1) {
630 snprintf(buf, sizeof buf,
637 cprintf("%d Ok\n", CIT_OK);
644 void abort_upl(struct CitContext *who)
646 if (who->upload_fp != NULL) {
647 fclose(who->upload_fp);
648 who->upload_fp = NULL;
649 unlink(CC->upl_path);
656 * close the upload file
658 void cmd_ucls(char *cmd)
661 char upload_notice[512];
663 if (CC->upload_fp == NULL) {
664 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
668 fclose(CC->upload_fp);
669 CC->upload_fp = NULL;
671 if ((!strcasecmp(cmd, "1")) && (CC->upload_type != UPL_FILE)) {
672 CC->upload_type = UPL_FILE;
673 cprintf("%d Upload completed.\n", CIT_OK);
675 /* FIXME ... here we need to trigger a network run */
680 if (!strcasecmp(cmd, "1")) {
681 cprintf("%d File '%s' saved.\n", CIT_OK, CC->upl_path);
682 fp = fopen(CC->upl_filedir, "a");
684 fp = fopen(CC->upl_filedir, "w");
687 fprintf(fp, "%s %s %s\n", CC->upl_file,
693 /* put together an upload notice */
694 snprintf(upload_notice, sizeof upload_notice,
695 "NEW UPLOAD: '%s'\n %s\n%s\n",
699 quickie_message(CC->curr_user, NULL, NULL, CC->room.QRname,
700 upload_notice, 0, NULL);
703 cprintf("%d File '%s' aborted.\n", CIT_OK, CC->upl_path);
710 * read from the download file
712 void cmd_read(char *cmdbuf)
719 start_pos = extract_long(cmdbuf, 0);
720 bytes = extract_int(cmdbuf, 1);
722 if (CC->download_fp == NULL) {
723 cprintf("%d You don't have a download file open.\n",
724 ERROR + RESOURCE_NOT_OPEN);
728 if (bytes > 100000) bytes = 100000;
729 buf = malloc(bytes + 1);
731 fseek(CC->download_fp, start_pos, 0);
732 actual_bytes = fread(buf, 1, bytes, CC->download_fp);
733 cprintf("%d %d\n", BINARY_FOLLOWS, (int)actual_bytes);
734 client_write(buf, actual_bytes);
741 * write to the upload file
743 void cmd_writ(char *cmdbuf)
750 bytes = extract_int(cmdbuf, 0);
752 if (CC->upload_fp == NULL) {
753 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
757 if (bytes > 100000) {
758 cprintf("%d You may not write more than 100000 bytes.\n",
763 cprintf("%d %d\n", SEND_BINARY, bytes);
764 buf = malloc(bytes + 1);
765 client_read(buf, bytes);
766 fwrite(buf, bytes, 1, CC->upload_fp);
774 * cmd_ndop() - open a network spool file for downloading
776 void cmd_ndop(char *cmdbuf)
781 if (IsEmptyStr(CC->net_node)) {
782 cprintf("%d Not authenticated as a network node.\n",
783 ERROR + NOT_LOGGED_IN);
787 if (CC->download_fp != NULL) {
788 cprintf("%d You already have a download file open.\n",
789 ERROR + RESOURCE_BUSY);
793 snprintf(pathname, sizeof pathname,
798 /* first open the file in append mode in order to create a
799 * zero-length file if it doesn't already exist
801 CC->download_fp = fopen(pathname, "a");
802 if (CC->download_fp != NULL)
803 fclose(CC->download_fp);
806 CC->download_fp = fopen(pathname, "r");
807 if (CC->download_fp == NULL) {
808 cprintf("%d cannot open %s: %s\n",
809 ERROR + INTERNAL_ERROR, pathname, strerror(errno));
814 /* set this flag so other routines know that the download file
815 * currently open is a network spool file
819 stat(pathname, &statbuf);
820 cprintf("%d %ld\n", CIT_OK, (long)statbuf.st_size);
824 * cmd_nuop() - open a network spool file for uploading
826 void cmd_nuop(char *cmdbuf)
830 if (IsEmptyStr(CC->net_node)) {
831 cprintf("%d Not authenticated as a network node.\n",
832 ERROR + NOT_LOGGED_IN);
836 if (CC->upload_fp != NULL) {
837 cprintf("%d You already have an upload file open.\n",
838 ERROR + RESOURCE_BUSY);
842 snprintf(CC->upl_path, sizeof CC->upl_path,
849 CC->upload_fp = fopen(CC->upl_path, "r");
850 if (CC->upload_fp != NULL) {
851 fclose(CC->upload_fp);
852 CC->upload_fp = NULL;
853 cprintf("%d '%s' already exists\n",
854 ERROR + ALREADY_EXISTS, CC->upl_path);
858 CC->upload_fp = fopen(CC->upl_path, "w");
859 if (CC->upload_fp == NULL) {
860 cprintf("%d Cannot open %s: %s\n",
861 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
865 CC->upload_type = UPL_NET;
866 cprintf("%d Ok\n", CIT_OK);