c1e97d2f690b42cd7bc3cd5ce06d9134e5ef5424
[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[256];
227         char pathname[PATH_MAX];
228         char MimeTestBuf[32];
229         struct ctdluser usbuf;
230         char which_user[USERNAME_SIZE];
231         int which_floor;
232         int a;
233         int rv;
234
235         extract_token(filename, cmdbuf, 0, '|', sizeof filename);
236
237         if (IsEmptyStr(filename)) {
238                 cprintf("%d You must specify a file name.\n",
239                         ERROR + FILE_NOT_FOUND);
240                 return;
241         }
242
243         if (CC->download_fp != NULL) {
244                 cprintf("%d You already have a download file open.\n",
245                         ERROR + RESOURCE_BUSY);
246                 return;
247         }
248
249         if (!strcasecmp(filename, "_floorpic_")) {
250                 which_floor = extract_int(cmdbuf, 1);
251                 snprintf(pathname, sizeof pathname, "%s/floor.%d", ctdl_image_dir, which_floor);
252         }
253         else if (!strcasecmp(filename, "_roompic_")) {
254                 assoc_file_name(pathname, sizeof pathname, &CC->room, ctdl_image_dir);
255         }
256         else {
257                 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
258                         filename[a] = tolower(filename[a]);
259                         if ( (filename[a] == '/') || (filename[a] == '\\') ) {
260                                 filename[a] = '_';
261                         }
262                 }
263                 if (strstr(filename, "../") != NULL)
264                 {
265                         cprintf("%d syntax error.\n",
266                                 ERROR + ILLEGAL_VALUE);
267                         return;
268                 }
269
270                 snprintf(pathname, sizeof pathname,
271                                  "%s/%s",
272                                  ctdl_image_dir,
273                                  filename);
274         }
275
276         CC->download_fp = fopen(pathname, "rb");
277         if (CC->download_fp == NULL) {
278                 strcat(pathname, ".gif");
279                 CC->download_fp = fopen(pathname, "rb");
280         }
281         if (CC->download_fp == NULL) {
282                 cprintf("%d Cannot open %s: %s\n",
283                         ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
284                 return;
285         }
286         rv = fread(&MimeTestBuf[0], 1, 32, CC->download_fp);
287         if (rv == -1) {
288                 cprintf("%d Cannot access %s: %s\n",
289                         ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
290                 return;
291         }
292
293         rewind (CC->download_fp);
294         OpenCmdResult(pathname, GuessMimeType(&MimeTestBuf[0], 32));
295 }
296
297
298 /*
299  * open a file for uploading
300  */
301 void cmd_uopn(char *cmdbuf)
302 {
303         int a;
304
305         extract_token(CC->upl_file, cmdbuf, 0, '|', sizeof CC->upl_file);
306         extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
307         extract_token(CC->upl_comment, cmdbuf, 2, '|', sizeof CC->upl_comment);
308
309         if (CtdlAccessCheck(ac_logged_in)) return;
310
311         if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
312                 cprintf("%d No directory in this room.\n",
313                         ERROR + NOT_HERE);
314                 return;
315         }
316
317         if (IsEmptyStr(CC->upl_file)) {
318                 cprintf("%d You must specify a file name.\n",
319                         ERROR + FILE_NOT_FOUND);
320                 return;
321         }
322
323         if (CC->upload_fp != NULL) {
324                 cprintf("%d You already have a upload file open.\n",
325                         ERROR + RESOURCE_BUSY);
326                 return;
327         }
328
329         for (a = 0; !IsEmptyStr(&CC->upl_file[a]); ++a) {
330                 if ( (CC->upl_file[a] == '/') || (CC->upl_file[a] == '\\') ) {
331                         CC->upl_file[a] = '_';
332                 }
333         }
334         snprintf(CC->upl_path, sizeof CC->upl_path, 
335                          "%s/%s/%s",
336                          ctdl_file_dir,
337                          CC->room.QRdirname, CC->upl_file);
338         snprintf(CC->upl_filedir, sizeof CC->upl_filedir,
339                          "%s/%s/filedir", 
340                          ctdl_file_dir,
341                          CC->room.QRdirname);
342
343         CC->upload_fp = fopen(CC->upl_path, "r");
344         if (CC->upload_fp != NULL) {
345                 fclose(CC->upload_fp);
346                 CC->upload_fp = NULL;
347                 cprintf("%d '%s' already exists\n",
348                         ERROR + ALREADY_EXISTS, CC->upl_path);
349                 return;
350         }
351
352         CC->upload_fp = fopen(CC->upl_path, "wb");
353         if (CC->upload_fp == NULL) {
354                 cprintf("%d Cannot open %s: %s\n",
355                         ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
356                 return;
357         }
358         cprintf("%d Ok\n", CIT_OK);
359 }
360
361
362
363 /*
364  * open an image file for uploading
365  */
366 void cmd_uimg(char *cmdbuf)
367 {
368         int is_this_for_real;
369         char basenm[256];
370         int which_floor;
371         int a;
372
373         if (num_parms(cmdbuf) < 2) {
374                 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
375                 return;
376         }
377
378         is_this_for_real = extract_int(cmdbuf, 0);
379         extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
380         extract_token(basenm, cmdbuf, 2, '|', sizeof basenm);
381         if (CC->upload_fp != NULL) {
382                 cprintf("%d You already have an upload file open.\n",
383                         ERROR + RESOURCE_BUSY);
384                 return;
385         }
386
387         strcpy(CC->upl_path, "");
388
389         for (a = 0; !IsEmptyStr(&basenm[a]); ++a) {
390                 basenm[a] = tolower(basenm[a]);
391                 if ( (basenm[a] == '/') || (basenm[a] == '\\') ) {
392                         basenm[a] = '_';
393                 }
394         }
395
396         if (CC->user.axlevel >= AxAideU) {
397                 snprintf(CC->upl_path, sizeof CC->upl_path, 
398                                  "%s/%s",
399                                  ctdl_image_dir,
400                                  basenm);
401         }
402
403         if (!strcasecmp(basenm, "_userpic_")) {
404                 snprintf(CC->upl_path, sizeof CC->upl_path,
405                                  "%s/%ld.gif",
406                                  ctdl_usrpic_dir,
407                                  CC->user.usernum);
408         }
409
410         if ((!strcasecmp(basenm, "_floorpic_"))
411             && (CC->user.axlevel >= AxAideU)) {
412                 which_floor = extract_int(cmdbuf, 2);
413                 snprintf(CC->upl_path, sizeof CC->upl_path,
414                                  "%s/floor.%d.gif",
415                                  ctdl_image_dir,
416                                  which_floor);
417         }
418
419         if ((!strcasecmp(basenm, "_roompic_")) && (is_room_aide())) {
420                 assoc_file_name(CC->upl_path, sizeof CC->upl_path, &CC->room, ctdl_image_dir);
421         }
422
423         if (IsEmptyStr(CC->upl_path)) {
424                 cprintf("%d Higher access required.\n",
425                         ERROR + HIGHER_ACCESS_REQUIRED);
426                 return;
427         }
428
429         if (is_this_for_real == 0) {
430                 cprintf("%d Ok to send image\n", CIT_OK);
431                 return;
432         }
433
434         CC->upload_fp = fopen(CC->upl_path, "wb");
435         if (CC->upload_fp == NULL) {
436                 cprintf("%d Cannot open %s: %s\n",
437                         ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
438                 return;
439         }
440         cprintf("%d Ok\n", CIT_OK);
441         CC->upload_type = UPL_IMAGE;
442 }
443
444
445 /*
446  * close the download file
447  */
448 void cmd_clos(char *cmdbuf)
449 {
450         char buf[256];
451
452         if (CC->download_fp == NULL) {
453                 cprintf("%d You don't have a download file open.\n",
454                         ERROR + RESOURCE_NOT_OPEN);
455                 return;
456         }
457
458         fclose(CC->download_fp);
459         CC->download_fp = NULL;
460
461         if (CC->dl_is_net == 1) {
462                 CC->dl_is_net = 0;
463                 snprintf(buf, sizeof buf, 
464                                  "%s/%s",
465                                  ctdl_netout_dir,
466                                  CC->net_node);
467                 unlink(buf);
468         }
469
470         cprintf("%d Ok\n", CIT_OK);
471 }
472
473
474 /*
475  * abort an upload
476  */
477 void abort_upl(CitContext *who)
478 {
479         if (who->upload_fp != NULL) {
480                 fclose(who->upload_fp);
481                 who->upload_fp = NULL;
482                 unlink(CC->upl_path);
483         }
484 }
485
486
487
488 /*
489  * close the upload file
490  */
491 void cmd_ucls(char *cmd)
492 {
493         struct CitContext *CCC = CC;
494         FILE *fp;
495         char upload_notice[512];
496         static int seq = 0;
497
498         if (CCC->upload_fp == NULL) {
499                 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
500                 return;
501         }
502
503         fclose(CC->upload_fp);
504         CCC->upload_fp = NULL;
505
506         if ((!strcasecmp(cmd, "1")) && (CCC->upload_type != UPL_FILE)) {
507                 cprintf("%d Upload completed.\n", CIT_OK);
508
509                 if (CCC->upload_type == UPL_NET) {
510                         char final_filename[PATH_MAX];
511                         snprintf(final_filename, sizeof final_filename,
512                                 "%s/%s.%04lx.%04x",
513                                 ctdl_netin_dir,
514                                 CCC->net_node,
515                                 (long)getpid(),
516                                 ++seq
517                         );
518
519                         if (link(CCC->upl_path, final_filename) == 0) {
520                                 CTDL_syslog(LOG_INFO, "UCLS: updoaded %s",
521                                        final_filename);
522                                 unlink(CCC->upl_path);
523                         }
524                         else {
525                                 CTDL_syslog(LOG_INFO, "Cannot link %s to %s: %s",
526                                             CCC->upl_path, final_filename, strerror(errno)
527                                 );
528                         }
529                         
530
531                         /* FIXME ... here we need to trigger a network run */
532                 }
533
534                 CCC->upload_type = UPL_FILE;
535                 return;
536         }
537
538         if (!strcasecmp(cmd, "1")) {
539                 cprintf("%d File '%s' saved.\n", CIT_OK, CCC->upl_path);
540                 fp = fopen(CCC->upl_filedir, "a");
541                 if (fp == NULL) {
542                         fp = fopen(CCC->upl_filedir, "w");
543                 }
544                 if (fp != NULL) {
545                         fprintf(fp, "%s %s %s\n", CCC->upl_file,
546                                 CCC->upl_mimetype,
547                                 CCC->upl_comment);
548                         fclose(fp);
549                 }
550
551                 if ((CCC->room.QRflags2 & QR2_NOUPLMSG) == 0) {
552                         /* put together an upload notice */
553                         snprintf(upload_notice, sizeof upload_notice,
554                                  "NEW UPLOAD: '%s'\n %s\n%s\n",
555                                  CCC->upl_file, 
556                                  CCC->upl_comment, 
557                                  CCC->upl_mimetype);
558                         quickie_message(CCC->curr_user, NULL, NULL, CCC->room.QRname,
559                                         upload_notice, 0, NULL);
560                 }
561         } else {
562                 abort_upl(CCC);
563                 cprintf("%d File '%s' aborted.\n", CIT_OK, CCC->upl_path);
564         }
565 }
566
567
568 /*
569  * read from the download file
570  */
571 void cmd_read(char *cmdbuf)
572 {
573         long start_pos;
574         size_t bytes;
575         char buf[SIZ];
576         int rc;
577
578         /* The client will transmit its requested offset and byte count */
579         start_pos = extract_long(cmdbuf, 0);
580         bytes = extract_int(cmdbuf, 1);
581         if ((start_pos < 0) || (bytes <= 0)) {
582                 cprintf("%d you have to specify a value > 0.\n", ERROR + ILLEGAL_VALUE);
583                 return;
584         }
585
586         if (CC->download_fp == NULL) {
587                 cprintf("%d You don't have a download file open.\n",
588                         ERROR + RESOURCE_NOT_OPEN);
589                 return;
590         }
591
592         /* If necessary, reduce the byte count to the size of our buffer */
593         if (bytes > sizeof(buf)) {
594                 bytes = sizeof(buf);
595         }
596
597         rc = fseek(CC->download_fp, start_pos, 0);
598         if (rc < 0) {
599                 struct CitContext *CCC = CC;
600                 cprintf("%d your file is smaller then %ld.\n", ERROR + ILLEGAL_VALUE, start_pos);
601                 CTDL_syslog(LOG_ERR, "your file %s is smaller then %ld. [%s]\n", 
602                             CC->upl_path, 
603                             start_pos,
604                             strerror(errno));
605
606                 return;
607         }
608         bytes = fread(buf, 1, bytes, CC->download_fp);
609         if (bytes > 0) {
610                 /* Tell the client the actual byte count and transmit it */
611                 cprintf("%d %d\n", BINARY_FOLLOWS, (int)bytes);
612                 client_write(buf, bytes);
613         }
614         else {
615                 cprintf("%d %s\n", ERROR, strerror(errno));
616         }
617 }
618
619
620 /*
621  * write to the upload file
622  */
623 void cmd_writ(char *cmdbuf)
624 {
625         struct CitContext *CCC = CC;
626         int bytes;
627         char *buf;
628         int rv;
629
630         unbuffer_output();
631
632         bytes = extract_int(cmdbuf, 0);
633
634         if (CCC->upload_fp == NULL) {
635                 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
636                 return;
637         }
638         if (bytes <= 0) {
639                 cprintf("%d you have to specify a value > 0.\n", ERROR + ILLEGAL_VALUE);
640                 return;
641         }
642
643         if (bytes > 100000) {
644                 bytes = 100000;
645         }
646
647         cprintf("%d %d\n", SEND_BINARY, bytes);
648         buf = malloc(bytes + 1);
649         client_read(buf, bytes);
650         rv = fwrite(buf, bytes, 1, CCC->upload_fp);
651         if (rv == -1) {
652                 CTDL_syslog(LOG_EMERG, "Couldn't write: %s\n",
653                             strerror(errno));
654         }
655         free(buf);
656 }
657
658
659
660
661 /*
662  * cmd_ndop() - open a network spool file for downloading
663  */
664 void cmd_ndop(char *cmdbuf)
665 {
666         struct CitContext *CCC = CC;
667         char pathname[256];
668         struct stat statbuf;
669
670         if (IsEmptyStr(CCC->net_node)) {
671                 cprintf("%d Not authenticated as a network node.\n",
672                         ERROR + NOT_LOGGED_IN);
673                 return;
674         }
675
676         if (CCC->download_fp != NULL) {
677                 cprintf("%d You already have a download file open.\n",
678                         ERROR + RESOURCE_BUSY);
679                 return;
680         }
681
682         snprintf(pathname, sizeof pathname, 
683                          "%s/%s",
684                          ctdl_netout_dir,
685                          CCC->net_node);
686
687         /* first open the file in append mode in order to create a
688          * zero-length file if it doesn't already exist 
689          */
690         CCC->download_fp = fopen(pathname, "a");
691         if (CCC->download_fp != NULL)
692                 fclose(CCC->download_fp);
693
694         /* now open it */
695         CCC->download_fp = fopen(pathname, "r");
696         if (CCC->download_fp == NULL) {
697                 cprintf("%d cannot open %s: %s\n",
698                         ERROR + INTERNAL_ERROR, pathname, strerror(errno));
699                 return;
700         }
701
702
703         /* set this flag so other routines know that the download file
704          * currently open is a network spool file 
705          */
706         CCC->dl_is_net = 1;
707
708         stat(pathname, &statbuf);
709         CCC->download_fp_total = statbuf.st_size;
710         cprintf("%d %ld\n", CIT_OK, (long)statbuf.st_size);
711 }
712
713 /*
714  * cmd_nuop() - open a network spool file for uploading
715  */
716 void cmd_nuop(char *cmdbuf)
717 {
718         static int seq = 1;
719
720         if (IsEmptyStr(CC->net_node)) {
721                 cprintf("%d Not authenticated as a network node.\n",
722                         ERROR + NOT_LOGGED_IN);
723                 return;
724         }
725
726         if (CC->upload_fp != NULL) {
727                 cprintf("%d You already have an upload file open.\n",
728                         ERROR + RESOURCE_BUSY);
729                 return;
730         }
731
732         snprintf(CC->upl_path, sizeof CC->upl_path,
733                          "%s/%s.%04lx.%04x",
734                          ctdl_nettmp_dir,
735                          CC->net_node, 
736                          (long)getpid(), 
737                          ++seq);
738
739         CC->upload_fp = fopen(CC->upl_path, "r");
740         if (CC->upload_fp != NULL) {
741                 fclose(CC->upload_fp);
742                 CC->upload_fp = NULL;
743                 cprintf("%d '%s' already exists\n",
744                         ERROR + ALREADY_EXISTS, CC->upl_path);
745                 return;
746         }
747
748         CC->upload_fp = fopen(CC->upl_path, "w");
749         if (CC->upload_fp == NULL) {
750                 cprintf("%d Cannot open %s: %s\n",
751                         ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
752                 return;
753         }
754
755         CC->upload_type = UPL_NET;
756         cprintf("%d Ok\n", CIT_OK);
757 }
758 void files_logout_hook(void)
759 {
760         CitContext *CCC = MyContext();
761
762         /*
763          * If there is a download in progress, abort it.
764          */
765         if (CCC->download_fp != NULL) {
766                 fclose(CCC->download_fp);
767                 CCC->download_fp = NULL;
768         }
769
770         /*
771          * If there is an upload in progress, abort it.
772          */
773         if (CCC->upload_fp != NULL) {
774                 abort_upl(CCC);
775         }
776
777 }
778
779 /* 
780  * help_subst()  -  support routine for help file viewer
781  */
782 void help_subst(char *strbuf, char *source, char *dest)
783 {
784         char workbuf[SIZ];
785         int p;
786
787         while (p = pattern2(strbuf, source), (p >= 0)) {
788                 strcpy(workbuf, &strbuf[p + strlen(source)]);
789                 strcpy(&strbuf[p], dest);
790                 strcat(strbuf, workbuf);
791         }
792 }
793
794 void do_help_subst(char *buffer)
795 {
796         char buf2[16];
797
798         help_subst(buffer, "^nodename", CtdlGetConfigStr("c_nodename"));
799         help_subst(buffer, "^humannode", CtdlGetConfigStr("c_humannode"));
800         help_subst(buffer, "^fqdn", CtdlGetConfigStr("c_fqdn"));
801         help_subst(buffer, "^username", CC->user.fullname);
802         snprintf(buf2, sizeof buf2, "%ld", CC->user.usernum);
803         help_subst(buffer, "^usernum", buf2);
804         help_subst(buffer, "^sysadm", CtdlGetConfigStr("c_sysadm"));
805         help_subst(buffer, "^variantname", CITADEL);
806         help_subst(buffer, "^maxsessions", CtdlGetConfigStr("c_maxsessions"));          // yes it's numeric but str is ok here
807         help_subst(buffer, "^bbsdir", ctdl_message_dir);
808 }
809
810
811 typedef const char *ccharp;
812 /*
813  * display system messages or help
814  */
815 void cmd_mesg(char *mname)
816 {
817         FILE *mfp;
818         char targ[256];
819         char buf[256];
820         char buf2[256];
821         char *dirs[2];
822         DIR *dp;
823         struct dirent *d;
824
825         extract_token(buf, mname, 0, '|', sizeof buf);
826
827         dirs[0] = strdup(ctdl_message_dir);
828         dirs[1] = strdup(ctdl_hlp_dir);
829
830         snprintf(buf2, sizeof buf2, "%s.%d.%d",
831                 buf, CC->cs_clientdev, CC->cs_clienttyp);
832
833         /* If the client requested "?" then produce a listing */
834         if (!strcmp(buf, "?")) {
835                 cprintf("%d %s\n", LISTING_FOLLOWS, buf);
836                 dp = opendir(dirs[1]);
837                 if (dp != NULL) {
838                         while (d = readdir(dp), d != NULL) {
839                                 if (d->d_name[0] != '.') {
840                                         cprintf(" %s\n", d->d_name);
841                                 }
842                         }
843                         closedir(dp);
844                 }
845                 cprintf("000\n");
846                 free(dirs[0]);
847                 free(dirs[1]);
848                 return;
849         }
850
851         /* Otherwise, look for the requested file by name. */
852         else {
853                 mesg_locate(targ, sizeof targ, buf2, 2, (const ccharp*)dirs);
854                 if (IsEmptyStr(targ)) {
855                         snprintf(buf2, sizeof buf2, "%s.%d",
856                                                         buf, CC->cs_clientdev);
857                         mesg_locate(targ, sizeof targ, buf2, 2,
858                                     (const ccharp*)dirs);
859                         if (IsEmptyStr(targ)) {
860                                 mesg_locate(targ, sizeof targ, buf, 2,
861                                             (const ccharp*)dirs);
862                         }       
863                 }
864         }
865
866         free(dirs[0]);
867         free(dirs[1]);
868
869         if (IsEmptyStr(targ)) {
870                 cprintf("%d '%s' not found.  (Searching in %s and %s)\n",
871                         ERROR + FILE_NOT_FOUND,
872                         mname,
873                         ctdl_message_dir,
874                         ctdl_hlp_dir
875                 );
876                 return;
877         }
878
879         mfp = fopen(targ, "r");
880         if (mfp==NULL) {
881                 cprintf("%d Cannot open '%s': %s\n",
882                         ERROR + INTERNAL_ERROR, targ, strerror(errno));
883                 return;
884         }
885         cprintf("%d %s\n", LISTING_FOLLOWS,buf);
886
887         while (fgets(buf, (sizeof buf - 1), mfp) != NULL) {
888                 buf[strlen(buf)-1] = 0;
889                 do_help_subst(buf);
890                 cprintf("%s\n",buf);
891         }
892
893         fclose(mfp);
894         cprintf("000\n");
895 }
896
897
898 /*
899  * enter system messages or help
900  */
901 void cmd_emsg(char *mname)
902 {
903         FILE *mfp;
904         char targ[256];
905         char buf[256];
906         char *dirs[2];
907         int a;
908
909         unbuffer_output();
910
911         if (CtdlAccessCheck(ac_aide)) return;
912
913         extract_token(buf, mname, 0, '|', sizeof buf);
914         for (a=0; !IsEmptyStr(&buf[a]); ++a) {          /* security measure */
915                 if (buf[a] == '/') buf[a] = '.';
916         }
917
918         dirs[0] = strdup(ctdl_message_dir);
919         dirs[1] = strdup(ctdl_hlp_dir);
920
921         mesg_locate(targ, sizeof targ, buf, 2, (const ccharp*)dirs);
922         free(dirs[0]);
923         free(dirs[1]);
924
925         if (IsEmptyStr(targ)) {
926                 snprintf(targ, sizeof targ, 
927                                  "%s/%s",
928                                  ctdl_hlp_dir, buf);
929         }
930
931         mfp = fopen(targ,"w");
932         if (mfp==NULL) {
933                 cprintf("%d Cannot open '%s': %s\n",
934                         ERROR + INTERNAL_ERROR, targ, strerror(errno));
935                 return;
936         }
937         cprintf("%d %s\n", SEND_LISTING, targ);
938
939         while (client_getln(buf, sizeof buf) >=0 && strcmp(buf, "000")) {
940                 fprintf(mfp, "%s\n", buf);
941         }
942
943         fclose(mfp);
944 }
945
946 /*****************************************************************************/
947 /*                      MODULE INITIALIZATION STUFF                          */
948 /*****************************************************************************/
949
950 CTDL_MODULE_INIT(file_ops)
951 {
952         if (!threading) {
953                 CtdlRegisterSessionHook(files_logout_hook, EVT_LOGOUT, PRIO_LOGOUT + 8);
954
955                 CtdlRegisterProtoHook(cmd_delf, "DELF", "Delete a file");
956                 CtdlRegisterProtoHook(cmd_movf, "MOVF", "Move a file");
957                 CtdlRegisterProtoHook(cmd_open, "OPEN", "Open a download file transfer");
958                 CtdlRegisterProtoHook(cmd_clos, "CLOS", "Close a download file transfer");
959                 CtdlRegisterProtoHook(cmd_uopn, "UOPN", "Open an upload file transfer");
960                 CtdlRegisterProtoHook(cmd_ucls, "UCLS", "Close an upload file transfer");
961                 CtdlRegisterProtoHook(cmd_read, "READ", "File transfer read operation");
962                 CtdlRegisterProtoHook(cmd_writ, "WRIT", "File transfer write operation");
963                 CtdlRegisterProtoHook(cmd_ndop, "NDOP", "Open a network spool file for download");
964                 CtdlRegisterProtoHook(cmd_nuop, "NUOP", "Open a network spool file for upload");
965                 CtdlRegisterProtoHook(cmd_oimg, "OIMG", "Open an image file for download");
966                 CtdlRegisterProtoHook(cmd_uimg, "UIMG", "Upload an image file");
967
968                 CtdlRegisterProtoHook(cmd_mesg, "MESG", "fetch system banners");
969                 CtdlRegisterProtoHook(cmd_emsg, "EMSG", "submit system banners");
970         }
971         /* return our Subversion id for the Log */
972         return "file_ops";
973 }