
/*
 *	Program 	postnews
 *	Programmer	N.d'Alterio
 *	Date		06/07/95
 *
 *  Synopsis:	This program spools an news article ready for posting at
 *              some later time. If currently online then sendnews is 
 *              called and the article will be sent off immediately.
 *		The online by checking env var ONLINE
 *
 *		This is a heavily modified version of postnews by 
 *              James Burton from his postnewsspool package. This fixes
 *              a number of bugs such as the leaving of locks, hanging
 *              for a long time while waiting to see if it is online.
 *		In addition the program has been very much streamlined
 *              there are no longer the command line options -X, -R as
 *              these seemd to have no use. Also gone are the arg### and
 *              ref### files it produced which seemed to be useless.
 *	
 *		For tin users the program has the command line option
 *              -f the add a From: user@somewhere to the header.
 *
 *		This program has the paths for my setup compiled into
 *              it as defaults to use different paths and files there
 *              is an env variable PNS_SPOOLDIR which is the full directory
 *              name for the spool dir with no trailing '/'.
 *
 *              The program expects to have the companion program sendnews
 *              in AmiTCP:bin/sendnews which in turn needs to have the 
 *              NNTP posting program as AmiTCP:bin/NNTPPost. 	
 *
 *  Command Line Arguments:	-n		Does not send if online
 *				-R		Specifies header filename
 *				-x		File to delete if successful
 *				-f		Adds a From: someone@ line to header
 *                              -h              Prints some help info
 *				filename	The article file to post
 *                                              if omitted will take from STDIN
 *
 *  Inputs:		article			Should contain a full header
 *			or
 *			header			article header
 *			body			article body
 *
 *  Outputs:		spooldir/msg.### 	The spooled message
 *
 *  Variables:		int i			general loop var
 *			int exit_code		return code for prog
 *			BPTR lock		lock on spooldir
 *			char *Buffer 		string to build filenames
 *			FILE *msg_fptr		file to contain spooled  article
 *			FILE *body_fptr		file containing article body
 *			FILE *head_fptr		fiel containing article header
 *			int no_post		flag - don't try to send
 *			char *from		Pointer to from string
 *			char *body_fname	Pointer to body filename 
 *			char *head_fname	Pointer to header filename
 *			int status		delete status flag
 *			int num_del 		Number of files to be deleted
 *			char *Todelete[]	Buffer containing names of files to delete
 *
 *  Functions:		CopyFile		copies article to spooldir
 *			NextArg			checks for existance of next argument
 *
 *  $Id: postnews.c 5.1 1996/05/06 22:59:18 nagd Exp $
 *
 */


#include "NPNS.h"

#define MAX_DEL		16
#define BUFSIZE		256

extern char *version = "$VER: postnews" VERSION;

int main( int argc,char **argv )

{

  FILE *msg_fptr;
  FILE *head_fptr;
  FILE *body_fptr;
    
  int i;          
  int exit_code = 0;
  int no_post   = FALSE;
  int num_del;
  int con_status;
  int status;

  char *spooldir;
  char *from;
  char *body_fname;
  char *head_fname;
  char *online;

  char Buffer[BUFSIZE];
  
  char *Todelete[MAX_DEL];   

  BPTR lock;                     

/*
 *   Read spool dir from env variable.
 */

  if ( ( spooldir = getenv( "PNS_SPOOLDIR" ) ) == NULL ) {
	spooldir = SPOOLDIR;
  }   /* end if */

/*
 *   See if user wants to post or not if online.
 */

  if ( getenv( "NNTPALWAYSQUEUE" ) ) {
	no_post = TRUE;
  }   /* end if */


/*
 *=================================================================================================*/

/*
 *   Parse command line.
 */
  
  i          = 1;
  num_del    = 0;
  from       = NULL;
  head_fname = NULL;
  body_fname = NULL;
    
  while ( i < argc ) {    

	if ( argv[i][0] == '-' ) {   

	switch ( argv[i][1] ) {

/*
 *  Add a From: line to article.
 */

                case 'f' : 

			if ( NextArg( i, argc, argv ) ) {
 
				from = argv[i+1];
								
			} else {

				exit(10);

			}  /* end if */
			i = i + 2;
			break;

/*
 *   Dont attempt to post if online.
 */

		case 'Q' :

			no_post = TRUE;
			i++;
			break;

/*
 *   Specifies a header for the article.
 */

		case 'R' :

			if ( NextArg( i, argc, argv ) ) {
				head_fname = argv[i+1];						
			} else {
				exit(10);
			}  /* end if */
			i = i + 2;
			break;

/*
 *   Set up a buffer of filenames to delete if 
 *   successful in posting.
 */
							
		case 'x' :

			if ( NextArg( i, argc, argv ) ) {
				if ( num_del < MAX_DEL ) {
					Todelete[num_del] = argv[i+1];
					num_del++;
				} else {
					fprintf( stderr, " Option '-x' used to many times ignoring\n" );
				}   /* end if */
			} else {
				exit(10);
			}   /* end if */
			i = i + 2;
			break;

/*
 *   Print help.
 */
 
		case 'h' :

			fprintf( stderr, " Usage: %s article [options] or %s [options] < article\n", argv[0], argv[0] );
                   	fprintf( stderr, " Where options are :-\n" );
			fprintf( stderr, "\t-f address\tAdd a From: address line to article\n" );
			fprintf( stderr, "\t-Q        \tDon't try to send article if online\n" );
			fprintf( stderr, "\t-R header \tPut header at top of message\n" );
			fprintf( stderr, "\t-x file   \tFile to delete if posting successful\n" );
			fprintf( stderr, "\t-h        \tFor this help\n\n" );
			exit(0);;
               		break;

		default :
 
			fprintf( stderr, " Unknown option use -h for help\n" );
			exit(5);
			break;

		}   /* end switch */


        } else {   

		body_fname = argv[i];
		i++;

	}   /* end if */

  }   /* end while */                    

/*
 *=================================================================================================*
 */


/*
 *   Lock directory while using.
 */
      
  if ( lock = Lock( spooldir, ACCESS_READ ) ) {
 

/*
 *   Open file to spool articles to.
 */

  	sprintf( Buffer, "%s/msg.%ld", spooldir, time( NULL ) );
  	if ( msg_fptr = fopen( Buffer, "w" ) ) {

/*
 *   Add a from line if requested.
 */

		if ( from != NULL ) fprintf( msg_fptr, "From: %s\n", from );

/*
 *  Open header file and copy to destination if user
 *  has used -R option.
 */

		if ( head_fname != NULL ) {

			if ( head_fptr = fopen( head_fname, "r" ) ) {

				CopyFile( head_fptr, msg_fptr );
				fclose( head_fptr );

			} else {

				fprintf( stderr, " Could not open header file - %s\n", head_fname );
				exit_code = 10;
	
			}   /* end if */

		}   /* end if */

		if ( !exit_code ) {

/*
 *   Open article/body file or set to stdin if no name
 *   has been specified.
 */

			if ( body_fname != NULL ) {
		
				if ( ( body_fptr = fopen( body_fname, "r" ) ) == NULL ) {

					fprintf( stderr, " Could not open body/article file - %s\n", body_fname );
					exit_code = 10;

				}   /* end if */

			} else {

				body_fptr = stdin;

			}   /* end if */

/*
 *   All files opened correctly so copy article to destination
 *   file.
 */

			if ( !exit_code ) {

				CopyFile( body_fptr, msg_fptr );
				if ( body_fname != NULL ) fclose( body_fptr );


/*
 *   Successful posting so delete the specified
 *   files
 */

				for ( i = 0; i < num_del; i++ ) {

					status = remove( Todelete[i] );
					if ( status ) fprintf( stderr, " Failed to delete file - %s\n", Todelete[i] );

				}   /* end for */

			}   /* end if */

		}   /* end if */
                  
		fclose( msg_fptr );
		if ( exit_code ) remove( Buffer );

	} else {

		fprintf( stderr, " Could not open message file - %s\n", Buffer );
		exit_code = 10;

	}   /* end if open msg file */

	UnLock( lock );

  } else {

	fprintf( stderr, " Could not lock spooldir - %s\n", spooldir );
	exit_code = 20;

  }   /* end if lock */

    
/*
 *   Check if online. If yes then call sendnews to send off articles
 *   now.
 */

  if ( !exit_code && !no_post ) {

	if ( ( online = getenv( "ONLINE" ) ) == NULL ) {
		fprintf( stderr, " Please set ONLINE env variable\n" );
		fprintf( stderr, " You are not online - message spooled\n" );
  	} else {
		if ( !(con_status = atoi( online )) ) {
			fprintf( stderr, " You are not online - message spooled\n" );
		} else {
			fprintf( stderr, " Posting spooled news now....\n" );
			system("sendnews");
		}  /* end if */
  	}   /* end if */

  }   /* end if !exit_code */

  exit( exit_code );

}   /* end program postnews */

