
/*
 *	Program 	sendnews
 *	Programmer	N.d'Alterio
 *	Date		06/08/95
 *
 *  Synopsis:	This program sends news which has been spooled by
 *		postnews using. If not online then the program will 
 *              exit and do nothing. 
 *
 *              It is specifically compiled for my INET set up
 *              i.e. spool dir AmiTCP:Usr/Spool/News
 *		     failed arc AmiTCP:Usr/Spool/sendnews.failed
 *
 *		These may be changed using the ENV variables
 *              PNS_SPOOLDIR and PNS_SEQFILE
 *
 *		If posting fails the article is appended to
 *              the file AmiTCP:Usr/Spool/sendnews.failed or
 *              the file in 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
 *                                         PNS_SPOOLDIR   (opt)
 *                                         PNS_FAILED_ARC (opt)
 *              				
 *
 *  Outputs:	Articles that fail are appended to PNS_FAILED_ARC
 *
 *  $Id: sendnews.c 5.5 1996/05/29 20:35:47 nagd Exp $
 *
 */

#define __USE_SYSBASE

#include "NPNS.h"

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

char *version = "$VER: sendnews" VERSION;

int __saveds sendnews( void )

{

  int i;
  int code;
  int exit_flag = FALSE;

  char Tmp[TMP_SIZE];

  char spooldir[NAME_SIZE];
  char failed_arc[NAME_SIZE];
  char nntpserver[NAME_SIZE];

  int  all;

  int cnum_to_send;
  int snum_to_send;

  char **SToSend;
  char **CToSend;

  long soc;
  struct sockaddr_in sin;


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

  DOSBase = (struct DosLibrary *)OpenLibrary( "dos.library", 37L );
  if ( DOSBase ) {

/*
 *   Read ENV vars, and set to defaults if not present.
 */
        
	if ( GetVar( "NNTPSERVER", nntpserver, NAME_SIZE, 0UL ) < 0 ) {
		Printf( " You must specify a NNTP news server\n" );
		exit_flag = TRUE;
  	}   /* if */

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

  	if ( GetVar( "PNS_FAILED_ARC", failed_arc, NAME_SIZE, 0UL ) < 0 )
		strcpy( failed_arc, FAILED_ARC );


  	if ( !InitAmigaTCP() ) exit_flag = TRUE;

  } else {

	exit_flag = TRUE;

  }   /* if */

/*===========================================================================*
             PROCESS COMMAND LINE & SPOOLDIR TO FIND FILES TO SEND
 *===========================================================================*/

  if ( !exit_flag ) {

	cnum_to_send = CommandContents( &all, &CToSend );
	snum_to_send = SpoolContents( spooldir, &SToSend );

/*===========================================================================*
                            ACTUAL PROCESSING
 *===========================================================================*/

 	if ( cnum_to_send || snum_to_send ) {
 
		if ( SetAddress( nntpserver, "nntp", &sin ) ) {

			if ( ( soc = MakeConnection( &sin ) ) >= 0 ) {

				Printf( " Connected to %s\n", nntpserver );
				code = GetResponse( soc );
				TORecv( soc, Tmp, TMP_SIZE, 0L, DEF_TIMEOUT );  

				if ( code == 200 ) {
			
					
					for ( i = 0; i < cnum_to_send; i++ ) {

						if ( SendArticle( soc, CToSend[i] ) ) {

							Printf( " Posted command line  article %ld\n", i ); 

						} else {

							Printf( " Failed to post command line  article %ld - saving\n", i );
							SaveFailed( CToSend[i], failed_arc );

						}   /* if */

						if ( !( DeleteFile( CToSend[i] ) ) )
							PrintFault( IoErr(), CToSend[i] );

					}   /* for i */

					if ( !cnum_to_send || (all && cnum_to_send) ) {

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

							if ( SendArticle( soc, SToSend[i] ) ) {

								Printf( " Posted spooled article %ld\n", i ); 

							} else {

								Printf( " Failed to post spooled article %ld - saving\n", i );
								SaveFailed( SToSend[i], failed_arc );

							}   /* if */

							if ( !( DeleteFile( SToSend[i] ) ) )
								PrintFault( IoErr(), SToSend[i] );

						}   /* for i */

					}   /* if */

				} else {

					Printf( " Posting not allowed at this server\n" );
					exit_flag = TRUE;

				}   /* if */

				SendCommand( soc, "QUIT", NULL );
				TORecv( soc, Tmp, TMP_SIZE, 0L, DEF_TIMEOUT );  
				EndConnection( soc );

			} else {

				exit_flag = TRUE;

			}   /* if */

  		} else {

			exit_flag = TRUE;

  		}   /* if */
  
		if ( cnum_to_send ) {

			for ( i = 0; i < cnum_to_send; i++ )
				FreeMem( CToSend[i], NAME_SIZE );

			FreeMem( CToSend, (long)cnum_to_send );

		}   /* if */

		if ( snum_to_send ) {

			for ( i = 0; i < snum_to_send; i++ )
				FreeMem( SToSend[i], NAME_SIZE );

			FreeMem( SToSend, (long)snum_to_send );

		}   /* if */

	} else {

		Printf( " No articles to send\n" );
		exit_flag = TRUE;

	}   /* if */

  }   /* if exit_flag */
      
  QuitAmigaTCP();  
  if ( DOSBase ) CloseLibrary( (struct Library *)DOSBase );
       
  return(exit_flag);

}   /* sendnews */

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

void _XCEXIT( long v ) { return; }
