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