
/*
 *	Program 	sendnews
 *	Programmer	N.d'Alterio
 *	Date		06/08/95
 *
 *  Synopsis:	This program sends news which has been spooled by
 *		postnews using NNTPpost from INETUtils. If not 
 *              online then the program will exit and do nothing.
 *              'Onlineness' env var ONLINE
 *
 *              This program was adapted from the postnewsspool
 *              package by James Burton which although useful
 *		was bugged. It could often hang, it gave little
 *              info as what it was doing, it left locks on the
 *              spool dir and was over complicated.
 * 
 *              It is specifically compiled for my INET set up
 *              i.e. spool dir AmiTCP:Usr/Spool/News
 *                   NNTPPost  AmiTCP:bin/NNTPPost
 *		     failed arc AmiTCP:Usr/Spool/sendnews.failed
 *
 *		These may be changed using the ENV variables
 *              PNS_SPOOLDIR and PNS_SEQFILE
 *
 *		If NNTPpost fails the article is appended to
 *              the file AmiTCP:Usr/Spool/sendnews.failed or
 *              the ENV variable PNS_FAILED_ARC. Each file has
 *		a header with the date the file failed.
 *
 *  Command Line Arguments: None
 *
 *  Inputs:	Needs enviroment variables NNTPSERVER
 *					   ONLINE
 *                                         PNS_SPOOLDIR   (opt)
 *                                         PNS_NNTPPOST   (opt)
 *                                         PNS_FAILED_ARC (opt)
 *              				
 *
 *  Outputs:	None
 *
 *  Variables:	BPTR	lock 		-	lock on spool dir
 *		BPTR    old_lock	-	lock on old current dir
 *		char    *Buffer		-	string to build command in
 *		char	*spooldir	-	spool dir name
 *		char	*nntppost	-	nntppost prog name
 *		char 	*failed_arc	-	failed archive filename 
 *		int	count		-	number of articles sent
 *		int	exit_code	-	prog return code
 *		FILE	*fptr		-	file pointer to TCP:daytime port
 *			*fib		-	file info block pointer	
 *
 *  Functions:	save_failed		-	saves failed messages to failed archive
 *
 * $VER: sendnews.c 2.1 (07.08.95) $		
 * $Log: sendnews.c $
 * Revision 4.2  1996/04/20  13:32:57  nagd
 * online check now with env var ONLINE
 *
 * Revision 4.1  1995/12/19  17:56:32  nagd
 * bumped version num
 *
 * Revision 3.1  1995/09/09  20:44:51  daltern
 * REWRITE: Now uses ADOS  functions
 * does not need seqfile anymore
 * will send anything that is the spool dir whatever the name
 * failed arc now separates files with a date string containing
 * the date the file failed
 * New env variable user can now specify nntpposting prog
 *
 * Revision 2.3  1995/09/09  20:41:19  daltern
 * now opens dos library version >37
 *
 * Revision 2.2  1995/08/27  21:32:56  daltern
 * fixed a couple of typos
 *
 * Revision 2.1  1995/08/27  21:29:18  daltern
 * Added some ENV var checking for paths and files
 * Fixed a bug that left a file open
 *
 * Revision 1.3  1995/08/07  13:07:23  daltern
 * added saving of failed messages
 *
 * Revision 1.2  1995/08/07  12:48:19  daltern
 * Added version string
 *
 * Revision 1.1  1995/08/07  12:45:05  daltern
 * Initial revision
 *
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dos/dos.h>
#include <clib/dos_protos.h>
#include <exec/types.h>

#define SPOOLDIR    "AmiTCP:Usr/Spool/News"
#define FAILED_ARC  "AmiTCP:Usr/Spool/sendnews.failed"
#define NNTPSERVER  NNTPServer
#define NNTPPOSTER  "AmiTCP:bin/NNTPPost"

void save_failed( char *, char * );

extern char *version = "$VER: sendnews 4.2 (20.04.95) Nick d'Alterio";

int main( void )

{

  int count;
  int con_status;
  int exit_code = 0;

  char *spooldir;
  char *failed_arc;
  char *nntppost;
  char *online;

  char Buffer[128];

  BPTR lock;
  BPTR old_lock;

  struct FileInfoBlock *fib;

  if ( ( online = getenv( "ONLINE" ) ) == NULL ) {
	fprintf( stderr, " Please set ONLINE env variable\n" );
	exit(exit_code);
  } else {
	if ( !(con_status = atoi( online )) ) {
		fprintf( stderr, " You are not online !\n" );
		exit(exit_code);
	}   /* end if */
  }   /* end if */

/*
 *   Read env variable to get NNTP server.
 */
        
  if ( getenv("NNTPSERVER") == NULL ) {
	fprintf( stderr, " You must specify a NNTP news server\n" );
	exit(10);
  }   /* end if */

/*
 *   Read name of NNTP posting program 
 */

  if ( ( nntppost = getenv( "PNS_NNTPPOST" ) ) == NULL ) {
	nntppost = NNTPPOSTER;
  }   /* end if */

/*
 *   Read spool dir env variable.
 */

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

/*
 *    Read failed archive name from env variable.
 */

  if ( ( failed_arc = getenv( "PNS_FAILED_ARC" ) ) == NULL ) {
	failed_arc = FAILED_ARC;
  }   /* end if */

/*
 *   Lock the spool directory whilst posting.
 */

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

/*
 *   Change current dir to spool dir and alloc fib
 */

  		old_lock = CurrentDir( lock );
  	
		if ( fib = AllocDosObject( DOS_FIB, NULL ) ) {

/*
 *   Examine all the contents of spool dir and try to post
 *   every message.
 */
   
  			count = 0;
  			Examine( lock, fib );
  			while( ExNext( lock, fib ) ) {

				count++;
				sprintf( Buffer, "%s >NIL: %s", nntppost, fib->fib_FileName );
	
				if ( !( system( Buffer ) ) ) {
					fprintf( stderr, " Posted article %d\n", count ); 
				} else {
					fprintf( stderr, " Failed to post article %d - saving \n", count );
					save_failed( fib->fib_FileName, failed_arc );
				}   /* end if */

				if ( !( DeleteFile( fib->fib_FileName ) ) )
					PrintFault( IoErr(), fib->fib_FileName );
                      
  			}   /* end while */

  			FreeDosObject( DOS_FIB, fib );

		} else {

			exit_code = 20;
	
		}   /* end if alloc dos obj */

  		CurrentDir( old_lock );
		UnLock( lock );

	} else {

		PrintFault( IoErr(), NULL );
		exit_code = 10;	

	}   /* end if lock */
                
  exit(exit_code);

}   /* end program sendnews */

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

/*========================================================================*
                          BEGIN FUNCTION save_failed
 *========================================================================*/

void save_failed( char *failed_file, char *arc_file )

{

  FILE *arc_fp;
  FILE *msg_fp;

  struct DateStamp ds;
  struct DateTime *dt;

  int ch;

  if ( !( arc_fp = fopen( arc_file, "a" ) ) ) {
	fprintf( stderr, " Could not open failed archive\n" );
  }   /* end if */

  if ( !( msg_fp = fopen( failed_file, "r" ) ) ) {
	fprintf( stderr, " Could not open message file to backup\n" );
	fclose( arc_fp );
	return;
  }   /* end if */

/*
 *   Print a header for failed file containing date.
 */

  dt = ( struct DateTime *)malloc( sizeof( struct DateTime ) );

  dt->dat_StrDay  = (char *)malloc( 20 * sizeof( char ) );
  dt->dat_StrTime = (char *)malloc( 20 * sizeof( char ) );
  dt->dat_StrDate = (char *)malloc( 20 * sizeof( char ) );
  
  dt->dat_Format =  FORMAT_DOS;
  dt->dat_Flags  =  NULL;

  DateStamp( &ds );
  dt->dat_Stamp = ds;
  DateToStr( dt );

  fprintf( arc_fp, "\n\n ********************* %s %s %s ******************* \n\n",
								dt->dat_StrTime, 
								dt->dat_StrDay, 
								dt->dat_StrDate );

  free( dt->dat_StrTime );
  free( dt->dat_StrDay );
  free( dt->dat_StrDate );
  free( dt );

/*
 *   Now copy msg to archive
 */

  while ( ( ch = fgetc( msg_fp ) ) != EOF ) fputc( ch, arc_fp );


  fclose( arc_fp );
  fclose( msg_fp );
  return;

}   /* end function save_failed */

/*========================================================================*
                                 END save_failed
 *========================================================================*/
