
/*
 *  REPLY.C
 *
 *  Reply to sender of article (creates, edits a file, then pipes it through
 *  sendmail)
 */

#include "news.h"
#include <time.h>

static char Buf[256];

char *findheader();
char *findheaderNoNull();

static char *TempFileName = "T:anews.post.tmp";

/*
 *  NOTE: fp can be NULL
 */

void
reply(cmd, fp, group)
int cmd;
FILE *fp;
const char *group;
{
    FILE *fo = fopen(TempFileName, "w");
    char *ptr;
    char *user	   = FindConfig(USERNAME);
    char *realname = FindConfig(REALNAME);
    int yes = 0;
    time_t t = time(NULL);
    int seq = GetSequence(0);
    static char *envuser;
    static char *envrealname;

    if (fo == NULL) {
	printf("can't create %s\n", TempFileName);
	return;
    }

    if (user == NULL || realname == NULL) {
	printf("Incomplete Configuration, one of:\n");
	printf("\t%s %s\n", USERNAME, REALNAME);
	fclose(fo);
	remove(TempFileName);
	return;
    }

    /*
     *	Enviroment variable overides
     */

    if (envuser == NULL)
	envuser = MallocEnviro("USER");
    if (envrealname == NULL)
	envrealname = MallocEnviro("REALNAME");
    if (envuser)
	user = envuser;
    if (envrealname)
	realname = envrealname;

    /*
     *	Create email template that can be sent through sendmail, handle
     *	signature and optional included text.
     */

    {
	char *to;

	if ((to = findheader(fp, "Reply-To:")) == NULL)
	    to = findheader(fp, "From:");
	if (to) {
	    fprintf(fo, "To: %s\n", to);
	    free(to);
	} else {
	    fprintf(fo, "To: \n");
	}
    }
    fprintf(fo, "Cc: \n");
    fprintf(fo, "Subject: ");

    if (fp)
	fprintf(fo, "Re: ");
    if (ptr = findheader(fp, "Subject:")) {
	char *xp = ptr;
	if (strnicmp(xp, "re:", 3) == 0)
	    xp += 3;
	while (*xp == ' ' || *xp == 9)
	    ++xp;
	fprintf(fo, "%s", xp);
	free(ptr);
    }
    fputs("\n", fo);

    fputs("\n", fo);

    if (fp && cmd >= 'A' && cmd <= 'Z') {
	rewind(fp);
	while (fgets(Buf, sizeof(Buf), fp) && Buf[0] != '\n');
	while (fgets(Buf, sizeof(Buf), fp)) {
	    fprintf(fo, ">");
	    fputs(Buf, fo);
	}
    }
    fputs("\n", fo);

    /*
     *	append signature
     */

    {
	FILE *fi;

	sprintf(Buf, "%s.signature", user);
	fi = openlib(Buf);
	if (fi == NULL)
	    fi = openlib(".signature");
	if (fi) {
	    fputs("--\n", fo);
	    while (fgets(Buf, sizeof(Buf), fi))
		fputs(Buf, fo);
	    fclose(fi);
	} else {
	    puts("Warning, no .signature file!");
	}
    }

    fclose(fo);

    /*
     *	Edit the file
     */

    sprintf(Buf, "%s %s", GetConfig(NEWSEDITOR, GetConfig(EDITOR, "dme")), TempFileName);
    system(Buf);

    /*
     *	want to post this?
     */

    cooked(stdin);
    printf("Do you want to send this? 'y' or 'n' :");
    fflush(stdout);
    while (fgets(Buf, sizeof(Buf), stdin)) {
	if (strcmp(Buf, "y\n") == 0 || strcmp(Buf, "yes\n") == 0) {
	    yes = 1;
	    break;
	}
	if (strcmp(Buf, "n\n") == 0 || strcmp(Buf, "no\n") == 0) {
	    break;
	}
	printf("'y' or 'n' please: ");
	fflush(stdout);
    }

    raw(stdin);
    if (yes) {
	sprintf(Buf, "%s < %s -f %s", GetConfigProgram(SENDMAIL), TempFileName, user);
	system(Buf);
	remove(TempFileName);
    } else {
	printf("File left as %s\n", TempFileName);
    }
}

