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