* Finished the concurrency check for network polling. (Now works both for
[citadel.git] / citadel / file_ops.c
1 /* 
2  * $Id$
3  *
4  * Server functions which handle file transfers and room directories.
5  *
6  */
7
8 #ifdef DLL_EXPORT
9 #define IN_LIBCIT
10 #endif
11
12 #include "sysdep.h"
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <stdio.h>
16 #include <fcntl.h>
17 #include <sys/stat.h>
18 #include <errno.h>
19 #include <string.h>
20 #include <sys/stat.h>
21
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
24 # include <time.h>
25 #else
26 # if HAVE_SYS_TIME_H
27 #  include <sys/time.h>
28 # else
29 #  include <time.h>
30 # endif
31 #endif
32
33 #include <limits.h>
34 #include "citadel.h"
35 #include "server.h"
36 #include "dynloader.h"
37 #include "config.h"
38 #include "file_ops.h"
39 #include "sysdep_decls.h"
40 #include "user_ops.h"
41 #include "support.h"
42 #include "room_ops.h"
43 #include "msgbase.h"
44 #include "tools.h"
45 #include "citserver.h"
46
47
48 /*
49  * network_talking_to()  --  concurrency checker
50  */
51 int network_talking_to(char *nodename, int operation) {
52
53         static char *nttlist = NULL;
54         char *ptr = NULL;
55         int i;
56         char buf[SIZ];
57         int retval = 0;
58
59         begin_critical_section(S_NTTLIST);
60
61         switch(operation) {
62
63                 case NTT_ADD:
64                         if (nttlist == NULL) nttlist = strdoop("");
65                         if (nttlist == NULL) break;
66                         nttlist = (char *)reallok(nttlist,
67                                 (strlen(nttlist) + strlen(nodename) + 3) );
68                         strcat(nttlist, "|");
69                         strcat(nttlist, nodename);
70                         break;
71
72                 case NTT_REMOVE:
73                         if (nttlist == NULL) break;
74                         if (strlen(nttlist) == 0) break;
75                         ptr = mallok(strlen(nttlist));
76                         if (ptr == NULL) break;
77                         strcpy(ptr, "");
78                         for (i = 0; i < num_tokens(nttlist, '|'); ++i) {
79                                 extract(buf, nttlist, i);
80                                 if ( (strlen(buf) > 0)
81                                      && (strcasecmp(buf, nodename)) ) {
82                                                 strcat(ptr, buf);
83                                                 strcat(ptr, "|");
84                                 }
85                         }
86                         phree(nttlist);
87                         nttlist = ptr;
88                         break;
89
90                 case NTT_CHECK:
91                         if (nttlist == NULL) break;
92                         if (strlen(nttlist) == 0) break;
93                         for (i = 0; i < num_tokens(nttlist, '|'); ++i) {
94                                 extract(buf, nttlist, i);
95                                 if (!strcasecmp(buf, nodename)) ++retval;
96                         }
97                         break;
98         }
99
100         if (nttlist != NULL) lprintf(9, "nttlist=<%s>\n", nttlist);
101         end_critical_section(S_NTTLIST);
102         return(retval);
103 }
104
105
106
107
108 /*
109  * Server command to delete a file from a room's directory
110  */
111 void cmd_delf(char *filename)
112 {
113         char pathname[64];
114         int a;
115
116         if (CtdlAccessCheck(ac_room_aide))
117                 return;
118
119         if ((CC->quickroom.QRflags & QR_DIRECTORY) == 0) {
120                 cprintf("%d No directory in this room.\n",
121                         ERROR + NOT_HERE);
122                 return;
123         }
124
125         if (strlen(filename) == 0) {
126                 cprintf("%d You must specify a file name.\n",
127                         ERROR + FILE_NOT_FOUND);
128                 return;
129         }
130         for (a = 0; a < strlen(filename); ++a) {
131                 if (filename[a] == '/') {
132                         filename[a] = '_';
133                 }
134         }
135         snprintf(pathname, sizeof pathname, "./files/%s/%s",
136                  CC->quickroom.QRdirname, filename);
137         a = unlink(pathname);
138         if (a == 0) {
139                 cprintf("%d File '%s' deleted.\n", OK, pathname);
140         }
141         else {
142                 cprintf("%d File '%s' not found.\n",
143                         ERROR + FILE_NOT_FOUND, pathname);
144         }
145 }
146
147
148
149
150 /*
151  * move a file from one room directory to another
152  */
153 void cmd_movf(char *cmdbuf)
154 {
155         char filename[SIZ];
156         char pathname[SIZ];
157         char newpath[SIZ];
158         char newroom[SIZ];
159         char buf[SIZ];
160         int a;
161         struct quickroom qrbuf;
162
163         extract(filename, cmdbuf, 0);
164         extract(newroom, cmdbuf, 1);
165
166         if (CtdlAccessCheck(ac_room_aide)) return;
167
168         if ((CC->quickroom.QRflags & QR_DIRECTORY) == 0) {
169                 cprintf("%d No directory in this room.\n",
170                         ERROR + NOT_HERE);
171                 return;
172         }
173
174         if (strlen(filename) == 0) {
175                 cprintf("%d You must specify a file name.\n",
176                         ERROR + FILE_NOT_FOUND);
177                 return;
178         }
179
180         for (a = 0; a < strlen(filename); ++a) {
181                 if (filename[a] == '/') {
182                         filename[a] = '_';
183                 }
184         }
185         snprintf(pathname, sizeof pathname, "./files/%s/%s",
186                  CC->quickroom.QRdirname, filename);
187         if (access(pathname, 0) != 0) {
188                 cprintf("%d File '%s' not found.\n",
189                         ERROR + FILE_NOT_FOUND, pathname);
190                 return;
191         }
192
193         if (getroom(&qrbuf, newroom) != 0) {
194                 cprintf("%d '%s' does not exist.\n", ERROR, newroom);
195                 return;
196         }
197         if ((qrbuf.QRflags & QR_DIRECTORY) == 0) {
198                 cprintf("%d '%s' is not a directory room.\n",
199                         ERROR + NOT_HERE, qrbuf.QRname);
200                 return;
201         }
202         snprintf(newpath, sizeof newpath, "./files/%s/%s", qrbuf.QRdirname,
203                  filename);
204         if (link(pathname, newpath) != 0) {
205                 cprintf("%d Couldn't move file: %s\n", ERROR,
206                         strerror(errno));
207                 return;
208         }
209         unlink(pathname);
210
211         /* this is a crude method of copying the file description */
212         snprintf(buf, sizeof buf,
213                  "cat ./files/%s/filedir |grep %s >>./files/%s/filedir",
214                  CC->quickroom.QRdirname, filename, qrbuf.QRdirname);
215         system(buf);
216         cprintf("%d File '%s' has been moved.\n", OK, filename);
217 }
218
219
220 /*
221  * send a file over the net
222  */
223 void cmd_netf(char *cmdbuf)
224 {
225         char pathname[SIZ], filename[SIZ], destsys[SIZ], buf[SIZ];
226         char outfile[SIZ];
227         int a, e;
228         time_t now;
229         FILE *ofp;
230         static int seq = 1;
231
232         extract(filename, cmdbuf, 0);
233         extract(destsys, cmdbuf, 1);
234
235         if (CtdlAccessCheck(ac_room_aide)) return;
236
237         if ((CC->quickroom.QRflags & QR_DIRECTORY) == 0) {
238                 cprintf("%d No directory in this room.\n",
239                         ERROR + NOT_HERE);
240                 return;
241         }
242
243         if (strlen(filename) == 0) {
244                 cprintf("%d You must specify a file name.\n",
245                         ERROR + FILE_NOT_FOUND);
246                 return;
247         }
248
249         for (a = 0; a < strlen(filename); ++a) {
250                 if (filename[a] == '/') {
251                         filename[a] = '_';
252                 }
253         }
254         snprintf(pathname, sizeof pathname, "./files/%s/%s",
255                  CC->quickroom.QRdirname, filename);
256         if (access(pathname, 0) != 0) {
257                 cprintf("%d File '%s' not found.\n",
258                         ERROR + FILE_NOT_FOUND, pathname);
259                 return;
260         }
261         snprintf(buf, sizeof buf, "sysop@%s", destsys);
262         e = alias(buf);
263         if (e != MES_BINARY) {
264                 cprintf("%d No such system: '%s'\n",
265                         ERROR + NO_SUCH_SYSTEM, destsys);
266                 return;
267         }
268         snprintf(outfile, sizeof outfile,
269                  "%s/network/spoolin/nsf.%04x.%04x",
270                  BBSDIR, getpid(), ++seq);
271         ofp = fopen(outfile, "a");
272         if (ofp == NULL) {
273                 cprintf("%d internal error\n", ERROR);
274                 return;
275         }
276
277         putc(255, ofp);
278         putc(MES_NORMAL, ofp);
279         putc(0, ofp);
280         fprintf(ofp, "Pcit%ld", CC->usersupp.usernum);
281         putc(0, ofp);
282         time(&now);
283         fprintf(ofp, "T%ld", (long) now);
284         putc(0, ofp);
285         fprintf(ofp, "A%s", CC->usersupp.fullname);
286         putc(0, ofp);
287         fprintf(ofp, "O%s", CC->quickroom.QRname);
288         putc(0, ofp);
289         fprintf(ofp, "N%s", NODENAME);
290         putc(0, ofp);
291         fprintf(ofp, "D%s", destsys);
292         putc(0, ofp);
293         fprintf(ofp, "SFILE");
294         putc(0, ofp);
295         putc('M', ofp);
296         fclose(ofp);
297
298         snprintf(buf, sizeof buf,
299                  "cd ./files/%s; uuencode %s <%s 2>/dev/null >>%s",
300                  CC->quickroom.QRdirname, filename, filename, outfile);
301         system(buf);
302
303         ofp = fopen(outfile, "a");
304         putc(0, ofp);
305         fclose(ofp);
306
307         cprintf("%d File '%s' has been sent to %s.\n", OK, filename,
308                 destsys);
309         /* FIXME start a network run here. */
310         return;
311 }
312
313 /*
314  * This code is common to all commands which open a file for downloading,
315  * regardless of whether it's a file from the directory, an image, a network
316  * spool file, a MIME attachment, etc.
317  * It examines the file and displays the OK result code and some information
318  * about the file.  NOTE: this stuff is Unix dependent.
319  */
320 void OpenCmdResult(char *filename, char *mime_type)
321 {
322         struct stat statbuf;
323         time_t modtime;
324         long filesize;
325
326         fstat(fileno(CC->download_fp), &statbuf);
327         filesize = (long) statbuf.st_size;
328         modtime = (time_t) statbuf.st_mtime;
329
330         cprintf("%d %ld|%ld|%s|%s\n",
331                 OK, filesize, modtime, filename, mime_type);
332 }
333
334
335 /*
336  * open a file for downloading
337  */
338 void cmd_open(char *cmdbuf)
339 {
340         char filename[SIZ];
341         char pathname[SIZ];
342         int a;
343
344         extract(filename, cmdbuf, 0);
345
346         if (CtdlAccessCheck(ac_logged_in)) return;
347
348         if ((CC->quickroom.QRflags & QR_DIRECTORY) == 0) {
349                 cprintf("%d No directory in this room.\n",
350                         ERROR + NOT_HERE);
351                 return;
352         }
353
354         if (strlen(filename) == 0) {
355                 cprintf("%d You must specify a file name.\n",
356                         ERROR + FILE_NOT_FOUND);
357                 return;
358         }
359
360         if (CC->download_fp != NULL) {
361                 cprintf("%d You already have a download file open.\n",
362                         ERROR);
363                 return;
364         }
365
366         for (a = 0; a < strlen(filename); ++a) {
367                 if (filename[a] == '/') {
368                         filename[a] = '_';
369                 }
370         }
371
372         snprintf(pathname, sizeof pathname,
373                  "./files/%s/%s", CC->quickroom.QRdirname, filename);
374         CC->download_fp = fopen(pathname, "r");
375
376         if (CC->download_fp == NULL) {
377                 cprintf("%d cannot open %s: %s\n",
378                         ERROR, pathname, strerror(errno));
379                 return;
380         }
381
382         OpenCmdResult(filename, "application/octet-stream");
383 }
384
385 /*
386  * open an image file
387  */
388 void cmd_oimg(char *cmdbuf)
389 {
390         char filename[SIZ];
391         char pathname[SIZ];
392         struct usersupp usbuf;
393         char which_user[32];
394         int which_floor;
395         int a;
396
397         extract(filename, cmdbuf, 0);
398
399         if (strlen(filename) == 0) {
400                 cprintf("%d You must specify a file name.\n",
401                         ERROR + FILE_NOT_FOUND);
402                 return;
403         }
404
405         if (CC->download_fp != NULL) {
406                 cprintf("%d You already have a download file open.\n",
407                         ERROR);
408                 return;
409         }
410
411         if (!strcasecmp(filename, "_userpic_")) {
412                 extract(which_user, cmdbuf, 1);
413                 if (getuser(&usbuf, which_user) != 0) {
414                         cprintf("%d No such user.\n",
415                                 ERROR + NO_SUCH_USER);
416                         return;
417                 }
418                 snprintf(pathname, sizeof pathname, "./userpics/%ld.gif",
419                          usbuf.usernum);
420         } else if (!strcasecmp(filename, "_floorpic_")) {
421                 which_floor = extract_int(cmdbuf, 1);
422                 snprintf(pathname, sizeof pathname,
423                          "./images/floor.%d.gif", which_floor);
424         } else if (!strcasecmp(filename, "_roompic_")) {
425                 assoc_file_name(pathname, &CC->quickroom, "images");
426         } else {
427                 for (a = 0; a < strlen(filename); ++a) {
428                         filename[a] = tolower(filename[a]);
429                         if (filename[a] == '/') {
430                                 filename[a] = '_';
431                         }
432                 }
433                 snprintf(pathname, sizeof pathname, "./images/%s.gif",
434                          filename);
435         }
436
437         CC->download_fp = fopen(pathname, "rb");
438         if (CC->download_fp == NULL) {
439                 cprintf("%d Cannot open %s: %s\n",
440                         ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
441                 return;
442         }
443
444         OpenCmdResult(pathname, "image/gif");
445 }
446
447 /*
448  * open a file for uploading
449  */
450 void cmd_uopn(char *cmdbuf)
451 {
452         int a;
453
454         extract(CC->upl_file, cmdbuf, 0);
455         extract(CC->upl_comment, cmdbuf, 1);
456
457         if (CtdlAccessCheck(ac_logged_in)) return;
458
459         if ((CC->quickroom.QRflags & QR_DIRECTORY) == 0) {
460                 cprintf("%d No directory in this room.\n",
461                         ERROR + NOT_HERE);
462                 return;
463         }
464
465         if (strlen(CC->upl_file) == 0) {
466                 cprintf("%d You must specify a file name.\n",
467                         ERROR + FILE_NOT_FOUND);
468                 return;
469         }
470
471         if (CC->upload_fp != NULL) {
472                 cprintf("%d You already have a upload file open.\n",
473                         ERROR);
474                 return;
475         }
476
477         for (a = 0; a < strlen(CC->upl_file); ++a) {
478                 if (CC->upl_file[a] == '/') {
479                         CC->upl_file[a] = '_';
480                 }
481         }
482         snprintf(CC->upl_path, sizeof CC->upl_path, "./files/%s/%s",
483                  CC->quickroom.QRdirname, CC->upl_file);
484         snprintf(CC->upl_filedir, sizeof CC->upl_filedir,
485                  "./files/%s/filedir", CC->quickroom.QRdirname);
486
487         CC->upload_fp = fopen(CC->upl_path, "r");
488         if (CC->upload_fp != NULL) {
489                 fclose(CC->upload_fp);
490                 CC->upload_fp = NULL;
491                 cprintf("%d '%s' already exists\n",
492                         ERROR + ALREADY_EXISTS, CC->upl_path);
493                 return;
494         }
495
496         CC->upload_fp = fopen(CC->upl_path, "wb");
497         if (CC->upload_fp == NULL) {
498                 cprintf("%d Cannot open %s: %s\n",
499                         ERROR, CC->upl_path, strerror(errno));
500                 return;
501         }
502         cprintf("%d Ok\n", OK);
503 }
504
505
506
507 /*
508  * open an image file for uploading
509  */
510 void cmd_uimg(char *cmdbuf)
511 {
512         int is_this_for_real;
513         char basenm[SIZ];
514         int which_floor;
515         int a;
516
517         if (num_parms(cmdbuf) < 2) {
518                 cprintf("%d Usage error.\n", ERROR);
519                 return;
520         }
521
522         is_this_for_real = extract_int(cmdbuf, 0);
523         extract(basenm, cmdbuf, 1);
524         if (CC->upload_fp != NULL) {
525                 cprintf("%d You already have an upload file open.\n",
526                         ERROR);
527                 return;
528         }
529
530         strcpy(CC->upl_path, "");
531
532         for (a = 0; a < strlen(basenm); ++a) {
533                 basenm[a] = tolower(basenm[a]);
534                 if (basenm[a] == '/') {
535                         basenm[a] = '_';
536                 }
537         }
538
539         if (CC->usersupp.axlevel >= 6) {
540                 snprintf(CC->upl_path, sizeof CC->upl_path, "./images/%s",
541                          basenm);
542         }
543
544         if (!strcasecmp(basenm, "_userpic_")) {
545                 snprintf(CC->upl_path, sizeof CC->upl_path,
546                          "./userpics/%ld.gif", CC->usersupp.usernum);
547         }
548
549         if ((!strcasecmp(basenm, "_floorpic_"))
550             && (CC->usersupp.axlevel >= 6)) {
551                 which_floor = extract_int(cmdbuf, 2);
552                 snprintf(CC->upl_path, sizeof CC->upl_path,
553                          "./images/floor.%d.gif", which_floor);
554         }
555
556         if ((!strcasecmp(basenm, "_roompic_")) && (is_room_aide())) {
557                 assoc_file_name(CC->upl_path, &CC->quickroom, "images");
558         }
559
560         if (strlen(CC->upl_path) == 0) {
561                 cprintf("%d Higher access required.\n",
562                         ERROR + HIGHER_ACCESS_REQUIRED);
563                 return;
564         }
565
566         if (is_this_for_real == 0) {
567                 cprintf("%d Ok to send image\n", OK);
568                 return;
569         }
570
571         CC->upload_fp = fopen(CC->upl_path, "wb");
572         if (CC->upload_fp == NULL) {
573                 cprintf("%d Cannot open %s: %s\n",
574                         ERROR, CC->upl_path, strerror(errno));
575                 return;
576         }
577         cprintf("%d Ok\n", OK);
578         CC->upload_type = UPL_IMAGE;
579 }
580
581
582 /*
583  * close the download file
584  */
585 void cmd_clos(void)
586 {
587         char buf[SIZ];
588
589         if (CC->download_fp == NULL) {
590                 cprintf("%d You don't have a download file open.\n",
591                         ERROR);
592                 return;
593         }
594
595         fclose(CC->download_fp);
596         CC->download_fp = NULL;
597
598         if (CC->dl_is_net == 1) {
599                 CC->dl_is_net = 0;
600                 snprintf(buf, sizeof buf, "%s/network/spoolout/%s", BBSDIR,
601                          CC->net_node);
602                 unlink(buf);
603         }
604
605         cprintf("%d Ok\n", OK);
606 }
607
608
609 /*
610  * abort an upload
611  */
612 void abort_upl(struct CitContext *who)
613 {
614         if (who->upload_fp != NULL) {
615                 fclose(who->upload_fp);
616                 who->upload_fp = NULL;
617                 unlink(CC->upl_path);
618         }
619 }
620
621
622
623 /*
624  * close the upload file
625  */
626 void cmd_ucls(char *cmd)
627 {
628         FILE *fp;
629         char upload_notice[512];
630
631         if (CC->upload_fp == NULL) {
632                 cprintf("%d You don't have an upload file open.\n", ERROR);
633                 return;
634         }
635
636         fclose(CC->upload_fp);
637         CC->upload_fp = NULL;
638
639         if ((!strcasecmp(cmd, "1")) && (CC->upload_type != UPL_FILE)) {
640                 CC->upload_type = UPL_FILE;
641                 cprintf("%d Upload completed.\n", OK);
642
643                 /* FIXME ... here we need to trigger a network run */
644
645                 return;
646         }
647
648         if (!strcasecmp(cmd, "1")) {
649                 cprintf("%d File '%s' saved.\n", OK, CC->upl_path);
650                 fp = fopen(CC->upl_filedir, "a");
651                 if (fp == NULL) {
652                         fp = fopen(CC->upl_filedir, "w");
653                 }
654                 if (fp != NULL) {
655                         fprintf(fp, "%s %s\n", CC->upl_file,
656                                 CC->upl_comment);
657                         fclose(fp);
658                 }
659
660                 /* put together an upload notice */
661                 sprintf(upload_notice,
662                         "NEW UPLOAD: '%s'\n %s\n",
663                         CC->upl_file, CC->upl_comment);
664                 quickie_message(CC->curr_user, NULL, CC->quickroom.QRname,
665                                 upload_notice);
666         } else {
667                 abort_upl(CC);
668                 cprintf("%d File '%s' aborted.\n", OK, CC->upl_path);
669         }
670 }
671
672
673
674 /*
675  * read from the download file
676  */
677 void cmd_read(char *cmdbuf)
678 {
679         long start_pos;
680         int bytes;
681         char buf[4096];
682
683         start_pos = extract_long(cmdbuf, 0);
684         bytes = extract_int(cmdbuf, 1);
685
686         if (CC->download_fp == NULL) {
687                 cprintf("%d You don't have a download file open.\n",
688                         ERROR);
689                 return;
690         }
691
692         if (bytes > 4096) {
693                 cprintf("%d You may not read more than 4096 bytes.\n",
694                         ERROR);
695                 return;
696         }
697
698         fseek(CC->download_fp, start_pos, 0);
699         fread(buf, bytes, 1, CC->download_fp);
700         cprintf("%d %d\n", BINARY_FOLLOWS, bytes);
701         client_write(buf, bytes);
702 }
703
704
705
706 /*
707  * write to the upload file
708  */
709 void cmd_writ(char *cmdbuf)
710 {
711         int bytes;
712         char buf[4096];
713
714         bytes = extract_int(cmdbuf, 0);
715
716         if (CC->upload_fp == NULL) {
717                 cprintf("%d You don't have an upload file open.\n", ERROR);
718                 return;
719         }
720
721         if (bytes > 4096) {
722                 cprintf("%d You may not write more than 4096 bytes.\n",
723                         ERROR);
724                 return;
725         }
726
727         cprintf("%d %d\n", SEND_BINARY, bytes);
728         client_read(buf, bytes);
729         fwrite(buf, bytes, 1, CC->upload_fp);
730 }
731
732
733
734
735 /*
736  * cmd_ndop() - open a network spool file for downloading
737  */
738 void cmd_ndop(char *cmdbuf)
739 {
740         char pathname[SIZ];
741         struct stat statbuf;
742
743         if (strlen(CC->net_node) == 0) {
744                 cprintf("%d Not authenticated as a network node.\n",
745                         ERROR + NOT_LOGGED_IN);
746                 return;
747         }
748
749         if (CC->download_fp != NULL) {
750                 cprintf("%d You already have a download file open.\n",
751                         ERROR);
752                 return;
753         }
754
755         snprintf(pathname, sizeof pathname, "%s/network/spoolout/%s",
756                  BBSDIR, CC->net_node);
757
758         /* first open the file in append mode in order to create a
759          * zero-length file if it doesn't already exist 
760          */
761         CC->download_fp = fopen(pathname, "a");
762         if (CC->download_fp != NULL)
763                 fclose(CC->download_fp);
764
765         /* now open it */
766         CC->download_fp = fopen(pathname, "r");
767         if (CC->download_fp == NULL) {
768                 cprintf("%d cannot open %s: %s\n",
769                         ERROR, pathname, strerror(errno));
770                 return;
771         }
772
773
774         /* set this flag so other routines know that the download file
775          * currently open is a network spool file 
776          */
777         CC->dl_is_net = 1;
778
779         stat(pathname, &statbuf);
780         cprintf("%d %ld\n", OK, statbuf.st_size);
781 }
782
783 /*
784  * cmd_nuop() - open a network spool file for uploading
785  */
786 void cmd_nuop(char *cmdbuf)
787 {
788         static int seq = 1;
789
790         if (strlen(CC->net_node) == 0) {
791                 cprintf("%d Not authenticated as a network node.\n",
792                         ERROR + NOT_LOGGED_IN);
793                 return;
794         }
795
796         if (CC->upload_fp != NULL) {
797                 cprintf("%d You already have an upload file open.\n",
798                         ERROR);
799                 return;
800         }
801
802         snprintf(CC->upl_path, sizeof CC->upl_path,
803                  "%s/network/spoolin/%s.%04x.%04x",
804                  BBSDIR, CC->net_node, getpid(), ++seq);
805
806         CC->upload_fp = fopen(CC->upl_path, "r");
807         if (CC->upload_fp != NULL) {
808                 fclose(CC->upload_fp);
809                 CC->upload_fp = NULL;
810                 cprintf("%d '%s' already exists\n",
811                         ERROR + ALREADY_EXISTS, CC->upl_path);
812                 return;
813         }
814
815         CC->upload_fp = fopen(CC->upl_path, "w");
816         if (CC->upload_fp == NULL) {
817                 cprintf("%d Cannot open %s: %s\n",
818                         ERROR, CC->upl_path, strerror(errno));
819                 return;
820         }
821
822         CC->upload_type = UPL_NET;
823         cprintf("%d Ok\n", OK);
824 }