460e2a85b462df566385f4211d44f983d8b6d52e
[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 HashList *nttlist = NULL;
52 int network_talking_to(const char *nodename, long len, int operation) {
53
54         int retval = 0;
55         HashPos *Pos = NULL;
56         void *vdata;
57
58         begin_critical_section(S_NTTLIST);
59
60         switch(operation) {
61
62                 case NTT_ADD:
63                         if (nttlist == NULL) 
64                                 nttlist = NewHash(1, NULL);
65                         Put(nttlist, nodename, len, NewStrBufPlain(nodename, len), HFreeStrBuf);
66                         syslog(LOG_DEBUG, "nttlist: added <%s>\n", nodename);
67                         break;
68                 case NTT_REMOVE:
69                         if ((nttlist == NULL) ||
70                             (GetCount(nttlist) == 0))
71                                 break;
72                         Pos = GetNewHashPos(nttlist, 1);
73                         if (GetHashPosFromKey (nttlist, nodename, len, Pos))
74                                 DeleteEntryFromHash(nttlist, Pos);
75                         DeleteHashPos(&Pos);
76                         syslog(LOG_DEBUG, "nttlist: removed <%s>\n", nodename);
77
78                         break;
79
80                 case NTT_CHECK:
81                         if ((nttlist == NULL) ||
82                             (GetCount(nttlist) == 0))
83                                 break;
84                         if (GetHash(nttlist, nodename, len, &vdata))
85                                 retval ++;
86                         syslog(LOG_DEBUG, "nttlist: have [%d] <%s>\n", retval, nodename);
87                         break;
88         }
89
90         end_critical_section(S_NTTLIST);
91         return(retval);
92 }
93
94 void cleanup_nttlist(void)
95 {
96         begin_critical_section(S_NTTLIST);
97         DeleteHash(&nttlist);
98         end_critical_section(S_NTTLIST);
99 }
100
101
102
103 /*
104  * Server command to delete a file from a room's directory
105  */
106 void cmd_delf(char *filename)
107 {
108         char pathname[64];
109         int a;
110
111         if (CtdlAccessCheck(ac_room_aide))
112                 return;
113
114         if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
115                 cprintf("%d No directory in this room.\n",
116                         ERROR + NOT_HERE);
117                 return;
118         }
119
120         if (IsEmptyStr(filename)) {
121                 cprintf("%d You must specify a file name.\n",
122                         ERROR + FILE_NOT_FOUND);
123                 return;
124         }
125         for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
126                 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
127                         filename[a] = '_';
128                 }
129         }
130         snprintf(pathname, sizeof pathname,
131                          "%s/%s/%s",
132                          ctdl_file_dir,
133                          CC->room.QRdirname, filename);
134         a = unlink(pathname);
135         if (a == 0) {
136                 cprintf("%d File '%s' deleted.\n", CIT_OK, pathname);
137         }
138         else {
139                 cprintf("%d File '%s' not found.\n",
140                         ERROR + FILE_NOT_FOUND, pathname);
141         }
142 }
143
144
145
146
147 /*
148  * move a file from one room directory to another
149  */
150 void cmd_movf(char *cmdbuf)
151 {
152         char filename[PATH_MAX];
153         char pathname[PATH_MAX];
154         char newpath[PATH_MAX];
155         char newroom[ROOMNAMELEN];
156         char buf[PATH_MAX];
157         int a;
158         struct ctdlroom qrbuf;
159
160         extract_token(filename, cmdbuf, 0, '|', sizeof filename);
161         extract_token(newroom, cmdbuf, 1, '|', sizeof newroom);
162
163         if (CtdlAccessCheck(ac_room_aide)) return;
164
165         if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
166                 cprintf("%d No directory in this room.\n",
167                         ERROR + NOT_HERE);
168                 return;
169         }
170
171         if (IsEmptyStr(filename)) {
172                 cprintf("%d You must specify a file name.\n",
173                         ERROR + FILE_NOT_FOUND);
174                 return;
175         }
176
177         for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
178                 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
179                         filename[a] = '_';
180                 }
181         }
182         snprintf(pathname, sizeof pathname, "./files/%s/%s",
183                  CC->room.QRdirname, filename);
184         if (access(pathname, 0) != 0) {
185                 cprintf("%d File '%s' not found.\n",
186                         ERROR + FILE_NOT_FOUND, pathname);
187                 return;
188         }
189
190         if (CtdlGetRoom(&qrbuf, newroom) != 0) {
191                 cprintf("%d '%s' does not exist.\n", ERROR + ROOM_NOT_FOUND, newroom);
192                 return;
193         }
194         if ((qrbuf.QRflags & QR_DIRECTORY) == 0) {
195                 cprintf("%d '%s' is not a directory room.\n",
196                         ERROR + NOT_HERE, qrbuf.QRname);
197                 return;
198         }
199         snprintf(newpath, sizeof newpath, "./files/%s/%s", qrbuf.QRdirname,
200                  filename);
201         if (link(pathname, newpath) != 0) {
202                 cprintf("%d Couldn't move file: %s\n", ERROR + INTERNAL_ERROR,
203                         strerror(errno));
204                 return;
205         }
206         unlink(pathname);
207
208         /* this is a crude method of copying the file description */
209         snprintf(buf, sizeof buf,
210                  "cat ./files/%s/filedir |grep \"%s\" >>./files/%s/filedir",
211                  CC->room.QRdirname, filename, qrbuf.QRdirname);
212         system(buf);
213         cprintf("%d File '%s' has been moved.\n", CIT_OK, filename);
214 }
215
216
217 /*
218  * This code is common to all commands which open a file for downloading,
219  * regardless of whether it's a file from the directory, an image, a network
220  * spool file, a MIME attachment, etc.
221  * It examines the file and displays the OK result code and some information
222  * about the file.  NOTE: this stuff is Unix dependent.
223  */
224 void OpenCmdResult(char *filename, const char *mime_type)
225 {
226         struct stat statbuf;
227         time_t modtime;
228         long filesize;
229
230         fstat(fileno(CC->download_fp), &statbuf);
231         CC->download_fp_total = statbuf.st_size;
232         filesize = (long) statbuf.st_size;
233         modtime = (time_t) statbuf.st_mtime;
234
235         cprintf("%d %ld|%ld|%s|%s\n",
236                 CIT_OK, filesize, (long)modtime, filename, mime_type);
237 }
238
239
240 /*
241  * open a file for downloading
242  */
243 void cmd_open(char *cmdbuf)
244 {
245         char filename[256];
246         char pathname[PATH_MAX];
247         int a;
248
249         extract_token(filename, cmdbuf, 0, '|', sizeof filename);
250
251         if (CtdlAccessCheck(ac_logged_in)) return;
252
253         if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
254                 cprintf("%d No directory in this room.\n",
255                         ERROR + NOT_HERE);
256                 return;
257         }
258
259         if (IsEmptyStr(filename)) {
260                 cprintf("%d You must specify a file name.\n",
261                         ERROR + FILE_NOT_FOUND);
262                 return;
263         }
264
265         if (CC->download_fp != NULL) {
266                 cprintf("%d You already have a download file open.\n",
267                         ERROR + RESOURCE_BUSY);
268                 return;
269         }
270
271         for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
272                 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
273                         filename[a] = '_';
274                 }
275         }
276
277         snprintf(pathname, sizeof pathname,
278                          "%s/%s/%s",
279                          ctdl_file_dir,
280                          CC->room.QRdirname, filename);
281         CC->download_fp = fopen(pathname, "r");
282
283         if (CC->download_fp == NULL) {
284                 cprintf("%d cannot open %s: %s\n",
285                         ERROR + INTERNAL_ERROR, pathname, strerror(errno));
286                 return;
287         }
288
289         OpenCmdResult(filename, "application/octet-stream");
290 }
291
292 /*
293  * open an image file
294  */
295 void cmd_oimg(char *cmdbuf)
296 {
297         char filename[256];
298         char pathname[PATH_MAX];
299         char MimeTestBuf[32];
300         struct ctdluser usbuf;
301         char which_user[USERNAME_SIZE];
302         int which_floor;
303         int a;
304         int rv;
305
306         extract_token(filename, cmdbuf, 0, '|', sizeof filename);
307
308         if (IsEmptyStr(filename)) {
309                 cprintf("%d You must specify a file name.\n",
310                         ERROR + FILE_NOT_FOUND);
311                 return;
312         }
313
314         if (CC->download_fp != NULL) {
315                 cprintf("%d You already have a download file open.\n",
316                         ERROR + RESOURCE_BUSY);
317                 return;
318         }
319
320         if (!strcasecmp(filename, "_userpic_")) {
321                 extract_token(which_user, cmdbuf, 1, '|', sizeof which_user);
322                 if (CtdlGetUser(&usbuf, which_user) != 0) {
323                         cprintf("%d No such user.\n",
324                                 ERROR + NO_SUCH_USER);
325                         return;
326                 }
327                 snprintf(pathname, sizeof pathname, 
328                                  "%s/%ld",
329                                  ctdl_usrpic_dir,
330                                  usbuf.usernum);
331         } else if (!strcasecmp(filename, "_floorpic_")) {
332                 which_floor = extract_int(cmdbuf, 1);
333                 snprintf(pathname, sizeof pathname,
334                                  "%s/floor.%d",
335                                  ctdl_image_dir, which_floor);
336         } else if (!strcasecmp(filename, "_roompic_")) {
337                 assoc_file_name(pathname, sizeof pathname, &CC->room, ctdl_image_dir);
338         } else {
339                 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
340                         filename[a] = tolower(filename[a]);
341                         if ( (filename[a] == '/') || (filename[a] == '\\') ) {
342                                 filename[a] = '_';
343                         }
344                 }
345                 snprintf(pathname, sizeof pathname,
346                                  "%s/%s",
347                                  ctdl_image_dir,
348                                  filename);
349         }
350
351         CC->download_fp = fopen(pathname, "rb");
352         if (CC->download_fp == NULL) {
353                 strcat(pathname, ".gif");
354                 CC->download_fp = fopen(pathname, "rb");
355         }
356         if (CC->download_fp == NULL) {
357                 cprintf("%d Cannot open %s: %s\n",
358                         ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
359                 return;
360         }
361         rv = fread(&MimeTestBuf[0], 1, 32, CC->download_fp);
362         if (rv == -1) {
363                 cprintf("%d Cannot access %s: %s\n",
364                         ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
365                 return;
366         }
367
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                                 syslog(LOG_ALERT, "Cannot link %s to %s: %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  * read from the download file
638  */
639 void cmd_read(char *cmdbuf)
640 {
641         long start_pos;
642         size_t bytes;
643         char buf[SIZ];
644
645         /* The client will transmit its requested offset and byte count */
646         start_pos = extract_long(cmdbuf, 0);
647         bytes = extract_int(cmdbuf, 1);
648
649         if (CC->download_fp == NULL) {
650                 cprintf("%d You don't have a download file open.\n",
651                         ERROR + RESOURCE_NOT_OPEN);
652                 return;
653         }
654
655         /* If necessary, reduce the byte count to the size of our buffer */
656         if (bytes > sizeof(buf)) {
657                 bytes = sizeof(buf);
658         }
659
660         fseek(CC->download_fp, start_pos, 0);
661         bytes = fread(buf, 1, bytes, CC->download_fp);
662         if (bytes > 0) {
663                 /* Tell the client the actual byte count and transmit it */
664                 cprintf("%d %d\n", BINARY_FOLLOWS, (int)bytes);
665                 client_write(buf, bytes);
666         }
667         else {
668                 cprintf("%d %s\n", ERROR, strerror(errno));
669         }
670 }
671
672
673 /*
674  * write to the upload file
675  */
676 void cmd_writ(char *cmdbuf)
677 {
678         int bytes;
679         char *buf;
680         int rv;
681
682         unbuffer_output();
683
684         bytes = extract_int(cmdbuf, 0);
685
686         if (CC->upload_fp == NULL) {
687                 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
688                 return;
689         }
690
691         if (bytes > 100000) {
692                 bytes = 100000;
693         }
694
695         cprintf("%d %d\n", SEND_BINARY, bytes);
696         buf = malloc(bytes + 1);
697         client_read(buf, bytes);
698         rv = fwrite(buf, bytes, 1, CC->upload_fp);
699         if (rv == -1) {
700                 syslog(LOG_EMERG, "Couldn't write: %s\n",
701                        strerror(errno));
702         }
703         free(buf);
704 }
705
706
707
708
709 /*
710  * cmd_ndop() - open a network spool file for downloading
711  */
712 void cmd_ndop(char *cmdbuf)
713 {
714         char pathname[256];
715         struct stat statbuf;
716
717         if (IsEmptyStr(CC->net_node)) {
718                 cprintf("%d Not authenticated as a network node.\n",
719                         ERROR + NOT_LOGGED_IN);
720                 return;
721         }
722
723         if (CC->download_fp != NULL) {
724                 cprintf("%d You already have a download file open.\n",
725                         ERROR + RESOURCE_BUSY);
726                 return;
727         }
728
729         snprintf(pathname, sizeof pathname, 
730                          "%s/%s",
731                          ctdl_netout_dir,
732                          CC->net_node);
733
734         /* first open the file in append mode in order to create a
735          * zero-length file if it doesn't already exist 
736          */
737         CC->download_fp = fopen(pathname, "a");
738         if (CC->download_fp != NULL)
739                 fclose(CC->download_fp);
740
741         /* now open it */
742         CC->download_fp = fopen(pathname, "r");
743         if (CC->download_fp == NULL) {
744                 cprintf("%d cannot open %s: %s\n",
745                         ERROR + INTERNAL_ERROR, pathname, strerror(errno));
746                 return;
747         }
748
749
750         /* set this flag so other routines know that the download file
751          * currently open is a network spool file 
752          */
753         CC->dl_is_net = 1;
754
755         stat(pathname, &statbuf);
756         CC->download_fp_total = statbuf.st_size;
757         cprintf("%d %ld\n", CIT_OK, (long)statbuf.st_size);
758 }
759
760 /*
761  * cmd_nuop() - open a network spool file for uploading
762  */
763 void cmd_nuop(char *cmdbuf)
764 {
765         static int seq = 1;
766
767         if (IsEmptyStr(CC->net_node)) {
768                 cprintf("%d Not authenticated as a network node.\n",
769                         ERROR + NOT_LOGGED_IN);
770                 return;
771         }
772
773         if (CC->upload_fp != NULL) {
774                 cprintf("%d You already have an upload file open.\n",
775                         ERROR + RESOURCE_BUSY);
776                 return;
777         }
778
779         snprintf(CC->upl_path, sizeof CC->upl_path,
780                          "%s/%s.%04lx.%04x",
781                          ctdl_nettmp_dir,
782                          CC->net_node, 
783                          (long)getpid(), 
784                          ++seq);
785
786         CC->upload_fp = fopen(CC->upl_path, "r");
787         if (CC->upload_fp != NULL) {
788                 fclose(CC->upload_fp);
789                 CC->upload_fp = NULL;
790                 cprintf("%d '%s' already exists\n",
791                         ERROR + ALREADY_EXISTS, CC->upl_path);
792                 return;
793         }
794
795         CC->upload_fp = fopen(CC->upl_path, "w");
796         if (CC->upload_fp == NULL) {
797                 cprintf("%d Cannot open %s: %s\n",
798                         ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
799                 return;
800         }
801
802         CC->upload_type = UPL_NET;
803         cprintf("%d Ok\n", CIT_OK);
804 }
805
806
807 /*****************************************************************************/
808 /*                      MODULE INITIALIZATION STUFF                          */
809 /*****************************************************************************/
810
811 CTDL_MODULE_INIT(file_ops)
812 {
813         if (!threading) {
814                 CtdlRegisterProtoHook(cmd_delf, "DELF", "Delete a file");
815                 CtdlRegisterProtoHook(cmd_movf, "MOVF", "Move a file");
816                 CtdlRegisterProtoHook(cmd_open, "OPEN", "Open a download file transfer");
817                 CtdlRegisterProtoHook(cmd_clos, "CLOS", "Close a download file transfer");
818                 CtdlRegisterProtoHook(cmd_uopn, "UOPN", "Open an upload file transfer");
819                 CtdlRegisterProtoHook(cmd_ucls, "UCLS", "Close an upload file transfer");
820                 CtdlRegisterProtoHook(cmd_read, "READ", "File transfer read operation");
821                 CtdlRegisterProtoHook(cmd_writ, "WRIT", "File transfer write operation");
822                 CtdlRegisterProtoHook(cmd_ndop, "NDOP", "Open a network spool file for download");
823                 CtdlRegisterProtoHook(cmd_nuop, "NUOP", "Open a network spool file for upload");
824                 CtdlRegisterProtoHook(cmd_oimg, "OIMG", "Open an image file for download");
825                 CtdlRegisterProtoHook(cmd_uimg, "UIMG", "Upload an image file");
826                 CtdlRegisterCleanupHook(cleanup_nttlist);
827         }
828         /* return our Subversion id for the Log */
829         return "file_ops";
830 }