/*

Title:  MsgEd   

File:   textfile.c

Author: Jim Nutt  

Copr:	1988 by Jim Nutt

Description:

	handles import and export of textfiles

Revision History:

	0.00	4 July 1988

Support Files:

	msged.h

*/

#include "msged.h"
#ifdef __MSC__
#include <sys/types.h>
#endif
#include <sys\stat.h>

#define TEXTLEN 128

FILE *repapp(char *path);

void    import(LINE * l)
{
        char    line[TEXTLEN];
	FILE   *fp;
        char    fn[PATHLEN];
	LINE   *n;

        memset(fn, 0, sizeof(fn));
        memset(line, 0, sizeof(line));

	gotoxy(9, 1);
	clreol();
	intense(TRUE);
	bputs("File to import? ");
        bgets(fn, PATHLEN);
	intense(FALSE);

	if ((fp = fopen(fn, "rt")) != NULL) {
                while (fgets(line, min(maxx,TEXTLEN), fp) != NULL) {
			if (l->text != NULL) {
				if ((n = (LINE *) calloc(1, sizeof(LINE))) == NULL) {
					gotoxy(9, 1);
					clreol();
					bputs("Not enough memory, press any key");
					getkey();
					showheader(message);
					return;
				}
				n->next = l->next;
				if (n->next == NULL)
					msgbuf.last = n;
				else
					n->next->prev = n;
				n->prev = l;
				l->next = n;
				l = n;
			}
			else
				n = l;
                        n->text = strdup(line);
                        assert(n->text);
                        memset(line, 0, sizeof(line));
		}
		fclose(fp);
	}

	showheader(message);
}

void    export(LINE * f)
{
	FILE   *fp;
        char    fn[PATHLEN];

	gotoxy(9, 1);
        clreol();
	intense(TRUE);
	bputs("File name to write to? ");
        memset(fn, 0, sizeof(fn));
	strcpy(fn, outfile);
        bgets(fn, sizeof(fn));
	intense(FALSE);

	if ((fp = repapp(fn)) != NULL) {

		fputc('\n', fp);

		for (; f != NULL; f = f->next)
			if ((f->text != NULL) && ((*(f->text) != '\01') || shownotes)) {
				fputs(f->text, fp);
				if (strchr(f->text, '\n') == NULL)
					fputc('\n', fp);
			}

		fclose(fp);
	}

	showheader(message);
}

void    writetxt()
{
	LINE   *f = msgbuf.first;
	FILE   *fp;
        char    fn[PATHLEN];

	gotoxy(9, 1);
	clreol();
	intense(TRUE);
	bputs("File name to write to? ");
        memset(fn, 0, sizeof(fn));
	strcpy(fn, outfile);
        bgets(fn, sizeof(fn));
	intense(FALSE);

	if ((fp = repapp(fn)) == NULL)
		return;

	fprintf(fp, "\n%03d/%03d %s\n", message.msgnum, arealist[area].last, message.header.date);

	fprintf(fp, "From:   %s", message.header.from);

	if (arealist[area].netmail) {
		fputs(" of ", fp);

		if (message.from.zone != thisnode.zone)
			fprintf(fp, "%d:", message.from.zone);

		fprintf(fp, "%d/%d.%d", message.from.net, message.from.node, message.from.point);
	}

	fputc('\n', fp);

	fprintf(fp, "To:     %s", message.header.to);

	if (arealist[area].netmail) {
		fputs(" of ", fp);

		if (message.to.zone != thisnode.zone)
			fprintf(fp, "%d:", message.to.zone);

		fprintf(fp, "%d/%d.%d", message.to.net, message.to.node, message.to.point);
	}

	fputc('\n', fp);

	if (message.header.attached)
		fprintf(fp, "Files:  %s", message.header.subj);
	else
		fprintf(fp, "Subj:   %s", message.header.subj);

	fputc('\n', fp);

	fputs("Attr:   ", fp);

	if (message.header.private)
		fputs("private ", fp);
	if (message.header.crash)
		fputs("crash ", fp);
	if (message.header.recvd)
		fputs("recvd ", fp);
	if (message.header.sent)
		fputs("sent ", fp);
	if (message.header.attached)
		fputs("f/a ", fp);
	if (message.header.killsent)
		fputs("kill/sent ", fp);
	if (message.header.freq)
		fputs("freq ", fp);
	if (message.header.rreq)
		fputs("rreq ", fp);
	if (message.header.areq)
		fputs("areq ", fp);
	if (message.header.ureq)
		fputs("ureq ", fp);
	fputc('\n', fp);
	fputs("------------------------------------------------\n", fp);

	for (; f != NULL; f = f->next)
		if ((f->text != NULL) && ((*(f->text) != '\01') || shownotes)) {
			fputs(f->text, fp);
			if (strchr(f->text, '\n') == NULL)
				fputc('\n', fp);
		}

	if (isatty(fileno(fp)))
		fputc(12, fp);

	fclose(fp);
}

FILE *repapp(char *path)

{
	FILE *fp;
	int ch;


	if ((fp = fopen(path,"r")) == NULL)
		return(fp = fopen(path,"wt"));

	if (isatty(fileno(fp))) {
		fclose(fp);
		return(fp = fopen(path,"wt"));
	}
	fclose(fp);

	gotoxy(9,1);
	clreol();
	intense(TRUE);
	bputc('r');
	intense(FALSE);
	bputs("eplace or ");
	intense(TRUE);
	bputc('a');
	intense(FALSE);
        bputs("ppend? ");
        video_update();

	ch = getkey() & 0x7f;
	ch = tolower(ch);
	if (ch == 0x1b)
		return(NULL);

	while ((ch != 'a') && (ch != 'r')) {
		ch = 0x7f & getkey();
		ch = tolower(ch);
		if (ch == 0x1b)
			return(NULL);
	}

	if (ch == 'a')
		fp = fopen(path,"at");
	else
		fp = fopen(path,"wt");

	return(fp);
}
