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