46e6959b7edd6c0a2438f48485cfe1a6ea5410f4
[citadel.git] / citadel / modules / ctdlproto / serv_file.c
1 /* 
2  * Server functions which handle file transfers and room directories.
3  *
4  * Copyright (c) 1987-2015 by the citadel.org team
5  *
6  * This program is open source software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <stdio.h>
16 #include <libcitadel.h>
17 #include <dirent.h>
18
19 #include "ctdl_module.h"
20 #include "citserver.h"
21 #include "support.h"
22 #include "config.h"
23 #include "user_ops.h"
24
25
26 /*
27  * Server command to delete a file from a room's directory
28  */
29 void cmd_delf(char *filename)
30 {
31         char pathname[64];
32         int a;
33
34         if (CtdlAccessCheck(ac_room_aide))
35                 return;
36
37         if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
38                 cprintf("%d No directory in this room.\n",
39                         ERROR + NOT_HERE);
40                 return;
41         }
42
43         if (IsEmptyStr(filename)) {
44                 cprintf("%d You must specify a file name.\n",
45                         ERROR + FILE_NOT_FOUND);
46                 return;
47         }
48         for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
49                 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
50                         filename[a] = '_';
51                 }
52         }
53         snprintf(pathname, sizeof pathname,
54                          "%s/%s/%s",
55                          ctdl_file_dir,
56                          CC->room.QRdirname, filename);
57         a = unlink(pathname);
58         if (a == 0) {
59                 cprintf("%d File '%s' deleted.\n", CIT_OK, pathname);
60         }
61         else {
62                 cprintf("%d File '%s' not found.\n",
63                         ERROR + FILE_NOT_FOUND, pathname);
64         }
65 }
66
67
68
69
70 /*
71  * move a file from one room directory to another
72  */
73 void cmd_movf(char *cmdbuf)
74 {
75         char filename[PATH_MAX];
76         char pathname[PATH_MAX];
77         char newpath[PATH_MAX];
78         char newroom[ROOMNAMELEN];
79         char buf[PATH_MAX];
80         int a;
81         struct ctdlroom qrbuf;
82
83         extract_token(filename, cmdbuf, 0, '|', sizeof filename);
84         extract_token(newroom, cmdbuf, 1, '|', sizeof newroom);
85
86         if (CtdlAccessCheck(ac_room_aide)) return;
87
88         if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
89                 cprintf("%d No directory in this room.\n",
90                         ERROR + NOT_HERE);
91                 return;
92         }
93
94         if (IsEmptyStr(filename)) {
95                 cprintf("%d You must specify a file name.\n",
96                         ERROR + FILE_NOT_FOUND);
97                 return;
98         }
99
100         for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
101                 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
102                         filename[a] = '_';
103                 }
104         }
105         snprintf(pathname, sizeof pathname, "./files/%s/%s",
106                  CC->room.QRdirname, filename);
107         if (access(pathname, 0) != 0) {
108                 cprintf("%d File '%s' not found.\n",
109                         ERROR + FILE_NOT_FOUND, pathname);
110                 return;
111         }
112
113         if (CtdlGetRoom(&qrbuf, newroom) != 0) {
114                 cprintf("%d '%s' does not exist.\n", ERROR + ROOM_NOT_FOUND, newroom);
115                 return;
116         }
117         if ((qrbuf.QRflags & QR_DIRECTORY) == 0) {
118                 cprintf("%d '%s' is not a directory room.\n",
119                         ERROR + NOT_HERE, qrbuf.QRname);
120                 return;
121         }
122         snprintf(newpath, sizeof newpath, "./files/%s/%s", qrbuf.QRdirname,
123                  filename);
124         if (link(pathname, newpath) != 0) {
125                 cprintf("%d Couldn't move file: %s\n", ERROR + INTERNAL_ERROR,
126                         strerror(errno));
127                 return;
128         }
129         unlink(pathname);
130
131         /* this is a crude method of copying the file description */
132         snprintf(buf, sizeof buf,
133                  "cat ./files/%s/filedir |grep \"%s\" >>./files/%s/filedir",
134                  CC->room.QRdirname, filename, qrbuf.QRdirname);
135         system(buf);
136         cprintf("%d File '%s' has been moved.\n", CIT_OK, filename);
137 }
138
139
140 /*
141  * This code is common to all commands which open a file for downloading,
142  * regardless of whether it's a file from the directory, an image, a network
143  * spool file, a MIME attachment, etc.
144  * It examines the file and displays the OK result code and some information
145  * about the file.  NOTE: this stuff is Unix dependent.
146  */
147 void OpenCmdResult(char *filename, const char *mime_type)
148 {
149         struct stat statbuf;
150         time_t modtime;
151         long filesize;
152
153         fstat(fileno(CC->download_fp), &statbuf);
154         CC->download_fp_total = statbuf.st_size;
155         filesize = (long) statbuf.st_size;
156         modtime = (time_t) statbuf.st_mtime;
157
158         cprintf("%d %ld|%ld|%s|%s\n",
159                 CIT_OK, filesize, (long)modtime, filename, mime_type);
160 }
161
162
163 /*
164  * open a file for downloading
165  */
166 void cmd_open(char *cmdbuf)
167 {
168         char filename[256];
169         char pathname[PATH_MAX];
170         int a;
171
172         extract_token(filename, cmdbuf, 0, '|', sizeof filename);
173
174         if (CtdlAccessCheck(ac_logged_in)) return;
175
176         if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
177                 cprintf("%d No directory in this room.\n",
178                         ERROR + NOT_HERE);
179                 return;
180         }
181
182         if (IsEmptyStr(filename)) {
183                 cprintf("%d You must specify a file name.\n",
184                         ERROR + FILE_NOT_FOUND);
185                 return;
186         }
187         if (strstr(filename, "../") != NULL)
188         {
189                 cprintf("%d syntax error.\n",
190                         ERROR + ILLEGAL_VALUE);
191                 return;
192         }
193
194         if (CC->download_fp != NULL) {
195                 cprintf("%d You already have a download file open.\n",
196                         ERROR + RESOURCE_BUSY);
197                 return;
198         }
199
200         for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
201                 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
202                         filename[a] = '_';
203                 }
204         }
205
206         snprintf(pathname, sizeof pathname,
207                          "%s/%s/%s",
208                          ctdl_file_dir,
209                          CC->room.QRdirname, filename);
210         CC->download_fp = fopen(pathname, "r");
211
212         if (CC->download_fp == NULL) {
213                 cprintf("%d cannot open %s: %s\n",
214                         ERROR + INTERNAL_ERROR, pathname, strerror(errno));
215                 return;
216         }
217
218         OpenCmdResult(filename, "application/octet-stream");
219 }
220
221 /*
222  * open an image file
223  */
224 void cmd_oimg(char *cmdbuf)
225 {
226         char filename[PATH_MAX];
227         char pathname[PATH_MAX];
228         char MimeTestBuf[32];
229         int which_floor;
230         int a;
231         int rv;
232
233         extract_token(filename, cmdbuf, 0, '|', sizeof filename);
234
235         if (IsEmptyStr(filename)) {
236                 cprintf("%d You must specify a file name.\n",
237                         ERROR + FILE_NOT_FOUND);
238                 return;
239         }
240
241         if (CC->download_fp != NULL) {
242                 cprintf("%d You already have a download file open.\n",
243                         ERROR + RESOURCE_BUSY);
244                 return;
245         }
246
247         if (!strcasecmp(filename, "_floorpic_")) {
248                 which_floor = extract_int(cmdbuf, 1);
249                 snprintf(pathname, sizeof pathname, "%s/floor.%d", ctdl_image_dir, which_floor);
250         }
251         else if (!strcasecmp(filename, "_roompic_")) {
252                 assoc_file_name(pathname, sizeof pathname, &CC->room, ctdl_image_dir);
253         }
254         else {
255                 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
256                         filename[a] = tolower(filename[a]);
257                         if ( (filename[a] == '/') || (filename[a] == '\\') ) {
258                                 filename[a] = '_';
259                         }
260                 }
261                 if (strstr(filename, "../") != NULL)
262                 {
263                         cprintf("%d syntax error.\n",
264                                 ERROR + ILLEGAL_VALUE);
265                         return;
266                 }
267
268                 snprintf(pathname, sizeof pathname,
269                                  "%s/%s",
270                                  ctdl_image_dir,
271                                  filename);
272         }
273
274         CC->download_fp = fopen(pathname, "rb");
275         if (CC->download_fp == NULL) {
276                 strcat(pathname, ".gif");
277                 CC->download_fp = fopen(pathname, "rb");
278         }
279         if (CC->download_fp == NULL) {
280                 cprintf("%d Cannot open %s: %s\n",
281                         ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
282                 return;
283         }
284         rv = fread(&MimeTestBuf[0], 1, 32, CC->download_fp);
285         if (rv == -1) {
286                 cprintf("%d Cannot access %s: %s\n",
287                         ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
288                 return;
289         }
290
291         rewind (CC->download_fp);
292         OpenCmdResult(pathname, GuessMimeType(&MimeTestBuf[0], 32));
293 }
294
295
296 /*
297  * open a file for uploading
298  */
299 void cmd_uopn(char *cmdbuf)
300 {
301         int a;
302
303         extract_token(CC->upl_file, cmdbuf, 0, '|', sizeof CC->upl_file);
304         extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
305         extract_token(CC->upl_comment, cmdbuf, 2, '|', sizeof CC->upl_comment);
306
307         if (CtdlAccessCheck(ac_logged_in)) return;
308
309         if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
310                 cprintf("%d No directory in this room.\n",
311                         ERROR + NOT_HERE);
312                 return;
313         }
314
315         if (IsEmptyStr(CC->upl_file)) {
316                 cprintf("%d You must specify a file name.\n",
317                         ERROR + FILE_NOT_FOUND);
318                 return;
319         }
320
321         if (CC->upload_fp != NULL) {
322                 cprintf("%d You already have a upload file open.\n",
323                         ERROR + RESOURCE_BUSY);
324                 return;
325         }
326
327         for (a = 0; !IsEmptyStr(&CC->upl_file[a]); ++a) {
328                 if ( (CC->upl_file[a] == '/') || (CC->upl_file[a] == '\\') ) {
329                         CC->upl_file[a] = '_';
330                 }
331         }
332         snprintf(CC->upl_path, sizeof CC->upl_path, 
333                          "%s/%s/%s",
334                          ctdl_file_dir,
335                          CC->room.QRdirname, CC->upl_file);
336         snprintf(CC->upl_filedir, sizeof CC->upl_filedir,
337                          "%s/%s/filedir", 
338                          ctdl_file_dir,
339                          CC->room.QRdirname);
340
341         CC->upload_fp = fopen(CC->upl_path, "r");
342         if (CC->upload_fp != NULL) {
343                 fclose(CC->upload_fp);
344                 CC->upload_fp = NULL;
345                 cprintf("%d '%s' already exists\n",
346                         ERROR + ALREADY_EXISTS, CC->upl_path);
347                 return;
348         }
349
350         CC->upload_fp = fopen(CC->upl_path, "wb");
351         if (CC->upload_fp == NULL) {
352                 cprintf("%d Cannot open %s: %s\n",
353                         ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
354                 return;
355         }
356         cprintf("%d Ok\n", CIT_OK);
357 }
358
359
360
361 /*
362  * open an image file for uploading
363  */
364 void cmd_uimg(char *cmdbuf)
365 {
366         int is_this_for_real;
367         char basenm[256];
368         int which_floor;
369         int a;
370
371         if (num_parms(cmdbuf) < 2) {
372                 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
373                 return;
374         }
375
376         is_this_for_real = extract_int(cmdbuf, 0);
377         extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
378         extract_token(basenm, cmdbuf, 2, '|', sizeof basenm);
379         if (CC->upload_fp != NULL) {
380                 cprintf("%d You already have an upload file open.\n",
381                         ERROR + RESOURCE_BUSY);
382                 return;
383         }
384
385         strcpy(CC->upl_path, "");
386
387         for (a = 0; !IsEmptyStr(&basenm[a]); ++a) {
388                 basenm[a] = tolower(basenm[a]);
389                 if ( (basenm[a] == '/') || (basenm[a] == '\\') ) {
390                         basenm[a] = '_';
391                 }
392         }
393
394         if (CC->user.axlevel >= AxAideU) {
395                 snprintf(CC->upl_path, sizeof CC->upl_path, 
396                                  "%s/%s",
397                                  ctdl_image_dir,
398                                  basenm);
399         }
400
401         if (!strcasecmp(basenm, "_userpic_")) {
402                 snprintf(CC->upl_path, sizeof CC->upl_path,
403                                  "%s/%ld.gif",
404                                  ctdl_usrpic_dir,
405                                  CC->user.usernum);
406         }
407
408         if ((!strcasecmp(basenm, "_floorpic_"))
409             && (CC->user.axlevel >= AxAideU)) {
410                 which_floor = extract_int(cmdbuf, 2);
411                 snprintf(CC->upl_path, sizeof CC->upl_path,
412                                  "%s/floor.%d.gif",
413                                  ctdl_image_dir,
414                                  which_floor);
415         }
416
417         if ((!strcasecmp(basenm, "_roompic_")) && (is_room_aide())) {
418                 assoc_file_name(CC->upl_path, sizeof CC->upl_path, &CC->room, ctdl_image_dir);
419         }
420
421         if (IsEmptyStr(CC->upl_path)) {
422                 cprintf("%d Higher access required.\n",
423                         ERROR + HIGHER_ACCESS_REQUIRED);
424                 return;
425         }
426
427         if (is_this_for_real == 0) {
428                 cprintf("%d Ok to send image\n", CIT_OK);
429                 return;
430         }
431
432         CC->upload_fp = fopen(CC->upl_path, "wb");
433         if (CC->upload_fp == NULL) {
434                 cprintf("%d Cannot open %s: %s\n",
435                         ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
436                 return;
437         }
438         cprintf("%d Ok\n", CIT_OK);
439         CC->upload_type = UPL_IMAGE;
440 }
441
442
443 /*
444  * close the download file
445  */
446 void cmd_clos(char *cmdbuf)
447 {
448         char buf[256];
449
450         if (CC->download_fp == NULL) {
451                 cprintf("%d You don't have a download file open.\n",
452                         ERROR + RESOURCE_NOT_OPEN);
453                 return;
454         }
455
456         fclose(CC->download_fp);
457         CC->download_fp = NULL;
458
459         if (CC->dl_is_net == 1) {
460                 CC->dl_is_net = 0;
461                 snprintf(buf, sizeof buf, 
462                                  "%s/%s",
463                                  ctdl_netout_dir,
464                                  CC->net_node);
465                 unlink(buf);
466         }
467
468         cprintf("%d Ok\n", CIT_OK);
469 }
470
471
472 /*
473  * abort an upload
474  */
475 void abort_upl(CitContext *who)
476 {
477         if (who->upload_fp != NULL) {
478                 fclose(who->upload_fp);
479                 who->upload_fp = NULL;
480                 unlink(CC->upl_path);
481         }
482 }
483
484
485
486 /*
487  * close the upload file
488  */
489 void cmd_ucls(char *cmd)
490 {
491         struct CitContext *CCC = CC;
492         FILE *fp;
493         char upload_notice[512];
494         static int seq = 0;
495
496         if (CCC->upload_fp == NULL) {
497                 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
498                 return;
499         }
500
501         fclose(CC->upload_fp);
502         CCC->upload_fp = NULL;
503
504         if ((!strcasecmp(cmd, "1")) && (CCC->upload_type != UPL_FILE)) {
505                 cprintf("%d Upload completed.\n", CIT_OK);
506
507                 if (CCC->upload_type == UPL_NET) {
508                         char final_filename[PATH_MAX];
509                         snprintf(final_filename, sizeof final_filename,
510                                 "%s/%s.%04lx.%04x",
511                                 ctdl_netin_dir,
512                                 CCC->net_node,
513                                 (long)getpid(),
514                                 ++seq
515                         );
516
517                         if (link(CCC->upl_path, final_filename) == 0) {
518                                 CTDL_syslog(LOG_INFO, "UCLS: updoaded %s",
519                                        final_filename);
520                                 unlink(CCC->upl_path);
521                         }
522                         else {
523                                 CTDL_syslog(LOG_INFO, "Cannot link %s to %s: %s",
524                                             CCC->upl_path, final_filename, strerror(errno)
525                                 );
526                         }
527                         
528
529                         /* FIXME ... here we need to trigger a network run */
530                 }
531
532                 CCC->upload_type = UPL_FILE;
533                 return;
534         }
535
536         if (!strcasecmp(cmd, "1")) {
537                 cprintf("%d File '%s' saved.\n", CIT_OK, CCC->upl_path);
538                 fp = fopen(CCC->upl_filedir, "a");
539                 if (fp == NULL) {
540                         fp = fopen(CCC->upl_filedir, "w");
541                 }
542                 if (fp != NULL) {
543                         fprintf(fp, "%s %s %s\n", CCC->upl_file,
544                                 CCC->upl_mimetype,
545                                 CCC->upl_comment);
546                         fclose(fp);
547                 }
548
549                 if ((CCC->room.QRflags2 & QR2_NOUPLMSG) == 0) {
550                         /* put together an upload notice */
551                         snprintf(upload_notice, sizeof upload_notice,
552                                  "NEW UPLOAD: '%s'\n %s\n%s\n",
553                                  CCC->upl_file, 
554                                  CCC->upl_comment, 
555                                  CCC->upl_mimetype);
556                         quickie_message(CCC->curr_user, NULL, NULL, CCC->room.QRname,
557                                         upload_notice, 0, NULL);
558                 }
559         } else {
560                 abort_upl(CCC);
561                 cprintf("%d File '%s' aborted.\n", CIT_OK, CCC->upl_path);
562         }
563 }
564
565
566 /*
567  * read from the download file
568  */
569 void cmd_read(char *cmdbuf)
570 {
571         long start_pos;
572         size_t bytes;
573         char buf[SIZ];
574         int rc;
575
576         /* The client will transmit its requested offset and byte count */
577         start_pos = extract_long(cmdbuf, 0);
578         bytes = extract_int(cmdbuf, 1);
579         if ((start_pos < 0) || (bytes <= 0)) {
580                 cprintf("%d you have to specify a value > 0.\n", ERROR + ILLEGAL_VALUE);
581                 return;
582         }
583
584         if (CC->download_fp == NULL) {
585                 cprintf("%d You don't have a download file open.\n",
586                         ERROR + RESOURCE_NOT_OPEN);
587                 return;
588         }
589
590         /* If necessary, reduce the byte count to the size of our buffer */
591         if (bytes > sizeof(buf)) {
592                 bytes = sizeof(buf);
593         }
594
595         rc = fseek(CC->download_fp, start_pos, 0);
596         if (rc < 0) {
597                 struct CitContext *CCC = CC;
598                 cprintf("%d your file is smaller then %ld.\n", ERROR + ILLEGAL_VALUE, start_pos);
599                 CTDL_syslog(LOG_ERR, "your file %s is smaller then %ld. [%s]\n", 
600                             CC->upl_path, 
601                             start_pos,
602                             strerror(errno));
603
604                 return;
605         }
606         bytes = fread(buf, 1, bytes, CC->download_fp);
607         if (bytes > 0) {
608                 /* Tell the client the actual byte count and transmit it */
609                 cprintf("%d %d\n", BINARY_FOLLOWS, (int)bytes);
610                 client_write(buf, bytes);
611         }
612         else {
613                 cprintf("%d %s\n", ERROR, strerror(errno));
614         }
615 }
616
617
618 /*
619  * write to the upload file
620  */
621 void cmd_writ(char *cmdbuf)
622 {
623         struct CitContext *CCC = CC;
624         int bytes;
625         char *buf;
626         int rv;
627
628         unbuffer_output();
629
630         bytes = extract_int(cmdbuf, 0);
631
632         if (CCC->upload_fp == NULL) {
633                 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
634                 return;
635         }
636         if (bytes <= 0) {
637                 cprintf("%d you have to specify a value > 0.\n", ERROR + ILLEGAL_VALUE);
638                 return;
639         }
640
641         if (bytes > 100000) {
642                 bytes = 100000;
643         }
644
645         cprintf("%d %d\n", SEND_BINARY, bytes);
646         buf = malloc(bytes + 1);
647         client_read(buf, bytes);
648         rv = fwrite(buf, bytes, 1, CCC->upload_fp);
649         if (rv == -1) {
650                 CTDL_syslog(LOG_EMERG, "Couldn't write: %s\n",
651                             strerror(errno));
652         }
653         free(buf);
654 }
655
656
657
658
659 /*
660  * cmd_ndop() - open a network spool file for downloading
661  */
662 void cmd_ndop(char *cmdbuf)
663 {
664         struct CitContext *CCC = CC;
665         char pathname[256];
666         struct stat statbuf;
667
668         if (IsEmptyStr(CCC->net_node)) {
669                 cprintf("%d Not authenticated as a network node.\n",
670                         ERROR + NOT_LOGGED_IN);
671                 return;
672         }
673
674         if (CCC->download_fp != NULL) {
675                 cprintf("%d You already have a download file open.\n",
676                         ERROR + RESOURCE_BUSY);
677                 return;
678         }
679
680         snprintf(pathname, sizeof pathname, 
681                          "%s/%s",
682                          ctdl_netout_dir,
683                          CCC->net_node);
684
685         /* first open the file in append mode in order to create a
686          * zero-length file if it doesn't already exist 
687          */
688         CCC->download_fp = fopen(pathname, "a");
689         if (CCC->download_fp != NULL)
690                 fclose(CCC->download_fp);
691
692         /* now open it */
693         CCC->download_fp = fopen(pathname, "r");
694         if (CCC->download_fp == NULL) {
695                 cprintf("%d cannot open %s: %s\n",
696                         ERROR + INTERNAL_ERROR, pathname, strerror(errno));
697                 return;
698         }
699
700
701         /* set this flag so other routines know that the download file
702          * currently open is a network spool file 
703          */
704         CCC->dl_is_net = 1;
705
706         stat(pathname, &statbuf);
707         CCC->download_fp_total = statbuf.st_size;
708         cprintf("%d %ld\n", CIT_OK, (long)statbuf.st_size);
709 }
710
711 /*
712  * cmd_nuop() - open a network spool file for uploading
713  */
714 void cmd_nuop(char *cmdbuf)
715 {
716         static int seq = 1;
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->upload_fp != NULL) {
725                 cprintf("%d You already have an upload file open.\n",
726                         ERROR + RESOURCE_BUSY);
727                 return;
728         }
729
730         snprintf(CC->upl_path, sizeof CC->upl_path,
731                          "%s/%s.%04lx.%04x",
732                          ctdl_nettmp_dir,
733                          CC->net_node, 
734                          (long)getpid(), 
735                          ++seq);
736
737         CC->upload_fp = fopen(CC->upl_path, "r");
738         if (CC->upload_fp != NULL) {
739                 fclose(CC->upload_fp);
740                 CC->upload_fp = NULL;
741                 cprintf("%d '%s' already exists\n",
742                         ERROR + ALREADY_EXISTS, CC->upl_path);
743                 return;
744         }
745
746         CC->upload_fp = fopen(CC->upl_path, "w");
747         if (CC->upload_fp == NULL) {
748                 cprintf("%d Cannot open %s: %s\n",
749                         ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
750                 return;
751         }
752
753         CC->upload_type = UPL_NET;
754         cprintf("%d Ok\n", CIT_OK);
755 }
756 void files_logout_hook(void)
757 {
758         CitContext *CCC = MyContext();
759
760         /*
761          * If there is a download in progress, abort it.
762          */
763         if (CCC->download_fp != NULL) {
764                 fclose(CCC->download_fp);
765                 CCC->download_fp = NULL;
766         }
767
768         /*
769          * If there is an upload in progress, abort it.
770          */
771         if (CCC->upload_fp != NULL) {
772                 abort_upl(CCC);
773         }
774
775 }
776
777 /* 
778  * help_subst()  -  support routine for help file viewer
779  */
780 void help_subst(char *strbuf, char *source, char *dest)
781 {
782         char workbuf[SIZ];
783         int p;
784
785         while (p = pattern2(strbuf, source), (p >= 0)) {
786                 strcpy(workbuf, &strbuf[p + strlen(source)]);
787                 strcpy(&strbuf[p], dest);
788                 strcat(strbuf, workbuf);
789         }
790 }
791
792 void do_help_subst(char *buffer)
793 {
794         char buf2[16];
795
796         help_subst(buffer, "^nodename", CtdlGetConfigStr("c_nodename"));
797         help_subst(buffer, "^humannode", CtdlGetConfigStr("c_humannode"));
798         help_subst(buffer, "^fqdn", CtdlGetConfigStr("c_fqdn"));
799         help_subst(buffer, "^username", CC->user.fullname);
800         snprintf(buf2, sizeof buf2, "%ld", CC->user.usernum);
801         help_subst(buffer, "^usernum", buf2);
802         help_subst(buffer, "^sysadm", CtdlGetConfigStr("c_sysadm"));
803         help_subst(buffer, "^variantname", CITADEL);
804         help_subst(buffer, "^maxsessions", CtdlGetConfigStr("c_maxsessions"));          // yes it's numeric but str is ok here
805         help_subst(buffer, "^bbsdir", ctdl_message_dir);
806 }
807
808
809 typedef const char *ccharp;
810 /*
811  * display system messages or help
812  */
813 void cmd_mesg(char *mname)
814 {
815         FILE *mfp;
816         char targ[256];
817         char buf[256];
818         char buf2[256];
819         char *dirs[2];
820         DIR *dp;
821         struct dirent *d;
822
823         extract_token(buf, mname, 0, '|', sizeof buf);
824
825         dirs[0] = strdup(ctdl_message_dir);
826         dirs[1] = strdup(ctdl_hlp_dir);
827
828         snprintf(buf2, sizeof buf2, "%s.%d.%d",
829                 buf, CC->cs_clientdev, CC->cs_clienttyp);
830
831         /* If the client requested "?" then produce a listing */
832         if (!strcmp(buf, "?")) {
833                 cprintf("%d %s\n", LISTING_FOLLOWS, buf);
834                 dp = opendir(dirs[1]);
835                 if (dp != NULL) {
836                         while (d = readdir(dp), d != NULL) {
837                                 if (d->d_name[0] != '.') {
838                                         cprintf(" %s\n", d->d_name);
839                                 }
840                         }
841                         closedir(dp);
842                 }
843                 cprintf("000\n");
844                 free(dirs[0]);
845                 free(dirs[1]);
846                 return;
847         }
848
849         /* Otherwise, look for the requested file by name. */
850         else {
851                 mesg_locate(targ, sizeof targ, buf2, 2, (const ccharp*)dirs);
852                 if (IsEmptyStr(targ)) {
853                         snprintf(buf2, sizeof buf2, "%s.%d",
854                                                         buf, CC->cs_clientdev);
855                         mesg_locate(targ, sizeof targ, buf2, 2,
856                                     (const ccharp*)dirs);
857                         if (IsEmptyStr(targ)) {
858                                 mesg_locate(targ, sizeof targ, buf, 2,
859                                             (const ccharp*)dirs);
860                         }       
861                 }
862         }
863
864         free(dirs[0]);
865         free(dirs[1]);
866
867         if (IsEmptyStr(targ)) {
868                 cprintf("%d '%s' not found.  (Searching in %s and %s)\n",
869                         ERROR + FILE_NOT_FOUND,
870                         mname,
871                         ctdl_message_dir,
872                         ctdl_hlp_dir
873                 );
874                 return;
875         }
876
877         mfp = fopen(targ, "r");
878         if (mfp==NULL) {
879                 cprintf("%d Cannot open '%s': %s\n",
880                         ERROR + INTERNAL_ERROR, targ, strerror(errno));
881                 return;
882         }
883         cprintf("%d %s\n", LISTING_FOLLOWS,buf);
884
885         while (fgets(buf, (sizeof buf - 1), mfp) != NULL) {
886                 buf[strlen(buf)-1] = 0;
887                 do_help_subst(buf);
888                 cprintf("%s\n",buf);
889         }
890
891         fclose(mfp);
892         cprintf("000\n");
893 }
894
895
896 /*
897  * enter system messages or help
898  */
899 void cmd_emsg(char *mname)
900 {
901         FILE *mfp;
902         char targ[256];
903         char buf[256];
904         char *dirs[2];
905         int a;
906
907         unbuffer_output();
908
909         if (CtdlAccessCheck(ac_aide)) return;
910
911         extract_token(buf, mname, 0, '|', sizeof buf);
912         for (a=0; !IsEmptyStr(&buf[a]); ++a) {          /* security measure */
913                 if (buf[a] == '/') buf[a] = '.';
914         }
915
916         dirs[0] = strdup(ctdl_message_dir);
917         dirs[1] = strdup(ctdl_hlp_dir);
918
919         mesg_locate(targ, sizeof targ, buf, 2, (const ccharp*)dirs);
920         free(dirs[0]);
921         free(dirs[1]);
922
923         if (IsEmptyStr(targ)) {
924                 snprintf(targ, sizeof targ, 
925                                  "%s/%s",
926                                  ctdl_hlp_dir, buf);
927         }
928
929         mfp = fopen(targ,"w");
930         if (mfp==NULL) {
931                 cprintf("%d Cannot open '%s': %s\n",
932                         ERROR + INTERNAL_ERROR, targ, strerror(errno));
933                 return;
934         }
935         cprintf("%d %s\n", SEND_LISTING, targ);
936
937         while (client_getln(buf, sizeof buf) >=0 && strcmp(buf, "000")) {
938                 fprintf(mfp, "%s\n", buf);
939         }
940
941         fclose(mfp);
942 }
943
944 /*****************************************************************************/
945 /*                      MODULE INITIALIZATION STUFF                          */
946 /*****************************************************************************/
947
948 CTDL_MODULE_INIT(file_ops)
949 {
950         if (!threading) {
951                 CtdlRegisterSessionHook(files_logout_hook, EVT_LOGOUT, PRIO_LOGOUT + 8);
952
953                 CtdlRegisterProtoHook(cmd_delf, "DELF", "Delete a file");
954                 CtdlRegisterProtoHook(cmd_movf, "MOVF", "Move a file");
955                 CtdlRegisterProtoHook(cmd_open, "OPEN", "Open a download file transfer");
956                 CtdlRegisterProtoHook(cmd_clos, "CLOS", "Close a download file transfer");
957                 CtdlRegisterProtoHook(cmd_uopn, "UOPN", "Open an upload file transfer");
958                 CtdlRegisterProtoHook(cmd_ucls, "UCLS", "Close an upload file transfer");
959                 CtdlRegisterProtoHook(cmd_read, "READ", "File transfer read operation");
960                 CtdlRegisterProtoHook(cmd_writ, "WRIT", "File transfer write operation");
961                 CtdlRegisterProtoHook(cmd_ndop, "NDOP", "Open a network spool file for download");
962                 CtdlRegisterProtoHook(cmd_nuop, "NUOP", "Open a network spool file for upload");
963                 CtdlRegisterProtoHook(cmd_oimg, "OIMG", "Open an image file for download");
964                 CtdlRegisterProtoHook(cmd_uimg, "UIMG", "Upload an image file");
965
966                 CtdlRegisterProtoHook(cmd_mesg, "MESG", "fetch system banners");
967                 CtdlRegisterProtoHook(cmd_emsg, "EMSG", "submit system banners");
968         }
969         /* return our Subversion id for the Log */
970         return "file_ops";
971 }