
/*
 *  POSTNEWS.C
 *
 *  POSTNEWS <newsfile -f user -r "realname" -R reffile    (BATCH news poster)
 *					     -x file	   (delete file when done)
 *
 *  If not specified, obtains user/realname from USERNAME and REALNAME
 *  enviroment variables.  If either does not exist then gets it from
 *  the UserName and RealName config entries.
 *
 *  If -R is specified the reffile is included in the header list (usually
 *  contains References: generated by DNews)
 */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "config.h"
#include "version.h"

IDENT(".03");

char TmpBuf[2048];
char *DelFile[32];
short Di;

void PostNews();

main(ac, av)
char *av[];
{
    short i;
    char *from = NULL;
    char *real = NULL;
    char *reffile = NULL;
    FILE *rfi;

    for (i = 1; i < ac; ++i) {
	char *ptr = av[i];
	if (*ptr == '-') {

	    ptr += 2;

	    switch(ptr[-1]) {
	    case 'f':
		if (*ptr == 0)
		    ptr = av[++i];
		from = ptr;
		break;
	    case 'r':
		if (*ptr == 0)
		    ptr = av[++i];
		real = ptr;
		break;
	    case 'R':
		if (*ptr == 0)
		    ptr = av[++i];
		reffile = ptr;
		break;
	    case 'x':
		if (*ptr == 0)
		    ptr = av[++i];
		DelFile[Di++] = ptr;
		break;
	    default:
		printf("Unknown option: %s\n", ptr - 2);
		exit(1);
	    }
	} else {
	    printf("Illegal argument: %s\n", ptr);
	    exit(1);
	}
    }
    if (i > ac) {
	puts("Expected argument to option");
	exit(1);
    }
    if (from == NULL)
	from = GetUserName();
    if (real == NULL)
	real = GetRealName();

    if (reffile)
	rfi = fopen(reffile, "r");
    else
	rfi = NULL;

    LockFile("PostNews-UPDATE");
    PostNews(stdin, rfi, from, real);
    UnLockFile("PostNews-UPDATE");

    if (rfi)
	fclose(rfi);

    while (--Di >= 0)
	remove(DelFile[Di]);

    return(0);
}

void
PostNews(fi, rfi, user, realname)
FILE *fi;
FILE *rfi;
char *user;
char *realname;
{
    char *nodename = FindConfig(NODENAME);
    char *system_name = FindConfig(NEWSFEED);
    char *domain   = FindConfig(DOMAINNAME);
    int seq = GetSequence(0);
    char tfname[32];
    time_t t = time(NULL);
    FILE *fo;

    sprintf(tfname, "T:post-%d", seq);

    if (nodename == NULL || system_name == NULL || user == NULL || realname == NULL) {
	puts("Incomplete configuration!  can't post news");
	return;
    }
    if (domain == NULL)
	domain = ".UUCP";

    fo = fopen(tfname, "w");
    if (fo == NULL) {
	printf("Unable to create %s\n", tfname);
	return;
    }

    /*
     *	Create the actual news file to fo, copying appropriate sections of infile.
     */

    fprintf(fo, "Path: %s!%s\n", nodename, user);
    fprintf(fo, "From: %s@%s%s (%s)\n", user, nodename, domain, realname);
    fprintf(fo, "Message-ID: <%s.%04d@%s%s>\n", user, seq, nodename, domain);

    /*
     *	user headers
     */

    while (fgets(TmpBuf, sizeof(TmpBuf), fi) && TmpBuf[0] != '\n')
	fputs(TmpBuf, fo);

    /*
     *	system headers (news program -R option to postnews)
     *	(usually References:)
     */

    if (rfi) {
	while (fgets(TmpBuf, sizeof(TmpBuf), rfi))
	    fputs(TmpBuf, fo);
    }

    /*
     *	more headers
     */

    fprintf(fo, "Date: %s", ctime(&t));

    /*
     *	remainder of message
     */

    fprintf(fo, "\n");

    while (fgets(TmpBuf, sizeof(TmpBuf), fi))
	fputs(TmpBuf, fo);

    fclose(fo);

    sprintf(TmpBuf, "%s %s \"%s!rnews\"", GetConfigProgram(UUX), tfname, system_name);
    system(TmpBuf);
    remove(tfname);
}

