
/*
 *  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(".04");

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 {
	    if (freopen(ptr, "r", stdin) == NULL) {
		ulog(-1, "PostNews: unable to open %s", 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);
    fclose(stdin);

    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 *domain   = FindConfig(DOMAINNAME);
    char *organization = FindConfig(ORGANIZATION);
    char *timezone = FindConfig(TIMEZONE);

    int seq = GetSequence(0);
    char *autoBatch = FindConfig(AUTOBATCH);
    char tfname[32];
    time_t t = time(NULL);
    FILE *fo;

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

    if (nodename == 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\n", 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
     */

    {
	struct tm *ut;
	static char *mo[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
				"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
				 };

	ut = localtime(&t);

	fprintf(fo, "Date: %d %s %02d %02d:%02d:%02d %s\n",
	      ut->tm_mday, mo[ut->tm_mon], ut->tm_year % 100,
	      ut->tm_hour, ut->tm_min, ut->tm_sec,
	      ((timezone) ? timezone : "???")
	);
    }
    if (organization)
	fprintf(fo, "Organization: %s\n", organization);

    /*
     *	remainder of message
     */

    fprintf(fo, "\n");

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

    fclose(fo);

    /* REMOVED, now done by BatchNews.	REFER TO UUCP:MAN/NEWS and UUCP:MAN/BATCHNEWS
    sprintf(TmpBuf, "%s %s \"%s!rnews\"", GetConfigProgram(UUX), tfname, system_name);
    system(TmpBuf);
    */

    /*
     *	check for obsolete config
     */

    {
	char *old_sys = FindConfig(NEWSFEED);
	FILE *fi;

	if (old_sys) {
	    char *cfgFile;

	    autoBatch = "";     /*  force autoBatch */
	    cfgFile = MakeConfigPath(UULIB, "sys");

	    if ((fi = fopen(cfgFile, "r")) == NULL) {
		if (fi = fopen(cfgFile, "w")) {
		    fprintf(fi, "\n%s:*\n\n", old_sys);
		    ulog(-1, "PostNews: NewsFeed exists, Sys does not, creating Sys File");

		    /*
		     *	mail error message to postmaster
		     */
		    {
			char *tmpFileName = TmpFileName("T:post-mail");
			FILE *fj = fopen(tmpFileName, "w");
			if (fj) {
			    fprintf(fj, "\nPostNews has detected the obsolete %s Config entry.\n", NEWSFEED);
			    fprintf(fj, "A UULIB:Sys file has been created for you, please\n");
			    fprintf(fj, "delete the obsolete config and refer to NewsSetup.DOC\n");
			    fclose(fj);
			    sprintf(TmpBuf, "%s <%s -raw -f POSTNEWS-DAEMON -t postmaster -s OBSOLETE-WARNING",
				GetConfigProgram(SENDMAIL),
				tmpFileName
			    );
			    system(TmpBuf);
			    remove(tmpFileName);
			}
		    }
		}
	    }
	    if (fi)
		fclose(fi);
	}
    }

    /*
     *	Post locally, also batches to other machines.
     */

    sprintf(TmpBuf, "%s %s", GetConfigProgram(RNEWS), tfname);
    system(TmpBuf);
    remove(tfname);

    if (autoBatch) {
	sprintf(TmpBuf, "%s", GetConfigProgram(BATCHNEWS));
	system(TmpBuf);
    }
}

