
/*
 *  FOLLOWUP.C
 */

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

static char Buf[256];

char *findheader();

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

/*
 *  NOTE: fp can be NULL
 */

void
followup(cmd, fp, group)
int cmd;
FILE *fp;
const char *group;
{
    FILE *fo = fopen(TempFileName, "w");
    char *ptr;
    char *nodename = FindConfig(NODENAME);
    char *system_name = FindConfig(NEWSFEED);
    char *user	   = FindConfig(USERNAME);
    char *domain   = FindConfig(DOMAINNAME);
    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 (nodename == NULL || system_name == NULL || user == NULL || realname == NULL) {
	printf("Incomplete Configuration, one of:\n");
	printf("\t%s %s %s %s\n", NODENAME, NEWSFEED, USERNAME, REALNAME);
	fclose(fo);
	return;
    }
    if (domain == NULL)
	domain = ".UUCP";

    /*
     *	Enviroment variable overides
     */

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

    /*
     *	Create news file
     */

    fprintf(fo, "Path: %s!%s\n", nodename, user);
    fprintf(fo, "From: %s@%s%s (%s)\n", user, nodename, domain, realname);
    fprintf(fo, "Newsgroups: %s\n", group);

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

    fprintf(fo, "Message-Id: <%04d.AA%04d@%s%s>\n", seq, seq, nodename, domain);
    fprintf(fo, "Date: %s", ctime(&t));
    fprintf(fo, "Followup-To: %s\n", group);
    fprintf(fo, "Expires: \n");
    fprintf(fo, "Keywords: \n");

    fprintf(fo, "Distribution: ");
    if (ptr = findheader(fp, "Distribution:")) {
	fprintf(fo, "%s", ptr);
	free(ptr);
    } else {
	fputs("world", fo);
    }
    fputs("\n", fo);

    /*
     *	upper case, include text
     */

    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 post 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 \"%s!rnews\"", GetConfigProgram(UUX), TempFileName, system_name);
	system(Buf);
    }
    remove(TempFileName);
}

/*
 *  Finds the requested header in the file.  Deletes extra space at the end
 *  and resulting string does not have a newline.
 */

char *
findheader(fp, hdr)
FILE *fp;
char *hdr;
{
    int len = strlen(hdr);

    if (fp == NULL)
	return(NULL);

    rewind(fp);

    while (fgets(Buf, 256, fp) && Buf[0] != '\n') {
	if (strncmp(Buf, hdr, len) == 0) {
	    char *ptr = Buf;
	    char *ret = malloc(strlen(ptr + len) + 1);

	    while (ptr[len] == ' ' || ptr[len] == 9 || ptr[len] == '\n')
		++len;
	    strcpy(ret, ptr + len);
	    for (len = strlen(ret) - 1; len >= 0; --len) {
		if (ret[len] != ' ' && ret[len] != 9 && ret[len] != '\n')
		    break;
	    }
	    ++len;
	    ret[len] = 0;
	    return(ret);
	}
    }
    return(NULL);
}

