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