
/*
 *  PostNews.c
 *
 *  Copyright 1988 by William Loftus.  All rights reserved.
 *
 *  Version 0.60 Beta
 */

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include "log.h"

char *UserName;
char *NodeName;
char *RealName;
char *NewsEditor;
char *DomainName;
char to_buf[128];
char grp_buf[128];
char dist_buf[128];
char subject_buf[128];
char *temp_file_name;

int seq;

char *NewsFeed;

char cmd[128];
char path[128];
char signature[121];

void read_ctl();
void GetTo();
void GetDist();
void GetSubject();
void GetMessage();
void BuildRnews();
void Signature();
void PromptGet();
char *TmpFileName();
char *FindConfig();

void
errormsg (msg)
char *msg;
{
    printf(msg);
}


void
main()
{
    temp_file_name = TmpFileName("UUSPOOL:news");

    LogProgram = "PostNews";

    UserName = FindConfig("UserName");
    NodeName = FindConfig("NodeName");
    NewsFeed = FindConfig("NewsFeed");
    NewsEditor=FindConfig("NewsEditor");
    RealName = FindConfig("RealName");
    DomainName=FindConfig("DomainName");

    if (UserName == NULL || NodeName == NULL || NewsFeed == NULL ||
	NewsEditor==NULL || RealName == NULL || DomainName == NULL)
    {
	printf("UULIB:Config incomplete, missing one or more of:\n");
	printf("    UserName, NodeName, DomainName, NewsFeed, NewsEditor, RealName\n");
	exit(1);
    }

    GetTo();
    GetDist();
    GetSubject();
    GetMessage();
    BuildRnews();
}

void
GetTo()
{
    PromptGet("Newsgroup(s): ", 0, grp_buf, sizeof(grp_buf), stdin);
}

void
GetDist()
{
    FILE *fd;
    char  buf[128];

    if (!(fd = fopen("UULIB:news.distribution", "r"))) {
	errormsg("Can't Find news.distribution file");
	exit(3);
    }

    while (fgets(buf, sizeof buf, fd)) {
	printf("%s", buf);
    }

    PromptGet("Distribution: ", 0, dist_buf, sizeof(dist_buf), stdin);
}

void
GetSubject()
{
    PromptGet("Subject: ", 1, subject_buf, sizeof(subject_buf), stdin);
}

void
GetMessage()
{
    FILE *fp;
    time_t t;

    seq = GetSequence(4);

    fp = fopen(temp_file_name, "w");

    if (fp) {
	time(&t);
	fprintf(fp,"Relay-Version: X-AmigaNEWS version 0.60 BETA; site %s%s\n", NodeName, DomainName);
	fprintf(fp,"Posting-Version: X-AmigaNEWS version 0.60 BETA; site %s%s\n", NodeName, DomainName);
	fprintf(fp,"Path: %s!%s\n", NodeName, UserName);
	fprintf(fp,"From: %s@%s%s (%s)\n", UserName, NodeName, DomainName, RealName);
	fprintf(fp,"Newsgroups: %s", grp_buf);
	fprintf(fp,"Subject: %s", subject_buf);
	fprintf(fp,"Message-Id: <%05d.AA%05d@%s%s>\n", seq, seq, NodeName, DomainName);
	fprintf(fp,"Date: %s", ctime(&t));
	fprintf(fp,"Followup-To: %s", grp_buf);
	fprintf(fp,"Expires: \n");
	fprintf(fp,"Keywords: \n");
	fprintf(fp,"Distribution: %s\n\n", dist_buf);

	fclose(fp);
    } else {
	errormsg("Can't open temp file %s\n", temp_file_name);
	exit(1);
    }

    sprintf(cmd,"%s %s", NewsEditor, temp_file_name);
    system(cmd);

    printf("Append .signature file? [y/n] ");
    fgets(signature, sizeof signature, stdin);
    if ((signature[0] | 0x20) != 'n') {
	Signature();
	printf(".signature file appended.\n");
    }
}

void
BuildRnews()
{
    NewsFeed[7] = '\0';

    sprintf(cmd, "UUX %s \"%s!rnews\"", temp_file_name, NewsFeed);
    system(cmd);
    remove(temp_file_name);
}

/*
 * Read the control file and grab a few parameters.
 */

void
Signature()
{
    FILE *fp;
    FILE *sf;
    char buff[128];

    fp = fopen(temp_file_name, "a");  /* should already exist!! */

    if (!fp) {
	errormsg("Can't open temp file--%s\n", temp_file_name);
	exit(1);
    }

    fprintf(fp, "\n--\n");

    sf = fopen("UULIB:.signature", "r");

    if (sf) {
	while (NULL != fgets(buff, sizeof buff, sf)) {
	    fprintf(fp, "%s", buff);
	}
	fclose(sf);
    } else {
	errormsg("Can't open UULIB:.signature file\n");
    }
    fclose(fp);
}

void
PromptGet(prompt, loop, buf, bufsize, fin)
char *prompt;
char *buf;
FILE *fin;
{
    short i;

    do {
	printf("%s", prompt);
	fflush(stdout);
	if (fgets(buf, bufsize, fin) == NULL)
	    break;
	for (i = 0; buf[i] == ' ' || buf[i] == 9; ++i);
	if (buf[i] != '\n')
	    break;
    } while (loop);
}


