
/*
 *	Program 	postnews
 *	Programmer	N.d'Alterio
 *	Date		06/07/95
 *
 *  Synopsis:	This program spools an news article ready for posting at
 *              some later time.
 *
 *		For tin users the program has the command line option
 *              FROM\K 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 '/'.
 *
 *  Command Line Arguments:	HEAD/K		Specifies header filename
 *				DEL/K/M		File to delete if successful
 *				FROM/K		Adds a From: someone@ line to header
 *                              HELP/S          Prints some help info
 *				BODY/K		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
 *
 *  $Id: postnews.c 5.6 1996/05/29 20:29:04 nagd Exp $
 *
 */


#include "NPNS.h"

#define __USE_SYSBASE

#define OPT_FROM	0
#define OPT_HELP	1
#define OPT_DEL		2
#define OPT_HEAD	3
#define OPT_BODY	4
#define OPT_COUNT	5

#define TEMPLATE	"FROM/K,HELP/S,DEL/S,HEAD/K,BODY/K"

struct ExecBase *SysBase = (struct ExecBase *)4;
struct DosLibrary *DOSBase;

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


int __saveds postnews( void )

{

  BPTR msg_fh;
  BPTR head_fh;
  BPTR body_fh;
              
  int exit_code = RETURN_OK;

  char spooldir[NAME_SIZE];

  char *from;
  char *body_fname;
  char *head_fname;
  int   delete;

  char Buffer[TMP_SIZE];
  
  long opts[OPT_COUNT];
  struct RDArgs *rdargs;

  struct DateStamp ds;

  BPTR lock;                     



/*=================================================================================================*
                                     INITIALISATIONS
 *=================================================================================================*/

/*
 *   Open dos lib - insist on 37 for rdargs
 */
 
   DOSBase = (struct DosLibrary *)OpenLibrary( "dos.library", 37L );

   if ( !DOSBase ) return RETURN_FAIL;

/*
 *   Read env vars.
 */

  if ( GetVar( "PNS_SPOOLDIR", spooldir, NAME_SIZE, 0UL ) < 0 )
	strcpy( spooldir, SPOOLDIR );



/*=================================================================================================*
                                         COMMAND LINE
 *=================================================================================================*/

  from       = NULL;
  head_fname = NULL;
  body_fname = NULL;
  delete     = FALSE;

  memset( (char *)opts, 0, sizeof(opts) );

  rdargs = ReadArgs( TEMPLATE, opts, NULL );
  if ( rdargs ) {

	if ( opts[OPT_HELP] ) {

			Printf( "\n Usage: postnews %s\n\n", TEMPLATE );
                   	Printf( " Where options are :-\n\n" );
			Printf( "\tFROM/K  \tAdd a From: address line to article\n" );
			Printf( "\tHEAD/K  \tPut this header at top of message\n" );
			Printf( "\tBODY/K  \tFilename of body of article\n" );
			Printf( "\tDEL/S \tDelete head and body files if posting successful\n" );
			Printf( "\tHELP/S  \tFor this help\n\n" );
			Printf( " If no body is specified then body is read from stdin\n" );
			Printf( " If no HEAD option is used then BODY or stdin should contain\n" );
			Printf( " entire article\n\n" );

			return RETURN_OK;

 	} else {

		if ( opts[OPT_FROM] )
			from = (char *)opts[OPT_FROM];

		if ( opts[OPT_BODY] )
			body_fname = (char *)opts[OPT_BODY];

		if ( opts[OPT_HEAD] )
			head_fname = (char *)opts[OPT_HEAD];

		if ( opts[OPT_DEL] )
			delete = TRUE;

	}   /* if */
 

/*=================================================================================================*
                                          SPOOL ARTICLE 
 *=================================================================================================*/

        
  	lock = Lock( spooldir, ACCESS_READ );
 	if ( lock ) {

/*
 *   Open file to spool articles to. With name msg.secs
 */

		DateStamp( &ds );
  		sprintf( Buffer, "%s/msg.%ld", spooldir, (((ds.ds_Days*24*60)+ds.ds_Minute)*60)+ds.ds_Tick/50 ); 
 		msg_fh = Open( Buffer, MODE_NEWFILE );
		if ( msg_fh ) {

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

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

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

			if ( head_fname != NULL ) {

				head_fh = Open( head_fname, MODE_OLDFILE );
				if ( head_fh ) {

					CopyFile( head_fh, msg_fh );
					Close( head_fh );

				} else {

					PrintFault( IoErr(), head_fname );
					exit_code = RETURN_ERROR;
	
				}   /* if */

			}   /* if */

			if ( !exit_code ) {

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

				if ( body_fname != NULL ) {
		
					body_fh = Open( body_fname, MODE_OLDFILE );
					if ( !body_fh ) {

						PrintFault( IoErr(), body_fname );
						exit_code = RETURN_ERROR;

					}   /* if */

				} else {

					body_fh = Input();

				}   /* if */

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

				if ( !exit_code ) {

					CopyFile( body_fh, msg_fh );
					if ( body_fname != NULL ) Close( body_fh );


/*
 *   Successful posting so delete the head and body files if requested
 */

					if ( delete ) {

						if ( head_fname && !DeleteFile( head_fname ) )
							PrintFault( IoErr(), head_fname );

						if ( body_fname && !DeleteFile( body_fname ) )
							PrintFault( IoErr(), body_fname );

					}   /* if */

				}   /* if */

			}   /* if */
                  
			Close( msg_fh );
			if ( exit_code ) DeleteFile( Buffer );

		} else {
	
			PrintFault( IoErr(), Buffer );
			exit_code = RETURN_ERROR;

		}   /* if open msg file */

		UnLock( lock );

  	} else {

		PrintFault( IoErr(), spooldir );
		exit_code = RETURN_ERROR;

  	}   /* if lock */

	FreeArgs( rdargs );

  } else {

	PrintFault( IoErr(), NULL );

  }  /* if args */

  if ( DOSBase ) CloseLibrary( (struct Library *)DOSBase );

  return( exit_code );

}   /* end postnews */


/*==================================================================================================*
                                           END POSTNEWS
 *==================================================================================================*/
