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