
/*
 *	Function	SendArticle
 *	Programmer	N.d'Alterio
 *	Date		05/05/96
 *
 *  Synopsis:	Sends an article to news server.
 *
 *  Arguments:	soc		Connected socket
 *		newsfile	Filename of article to send
 *
 *  Returns:	int TRUE	If success
 *		    FALSE	If failure
 *
 *  Variables:	len		Amount of data read from file
 *		rc		Return code
 *		buf		Buffer for article
 *		fptr		File pointer
 * 
 *  Functions:	malloc		Allocate memory (ANSI)
 *		free		Free memory (ANSI)
 *		fread		Read data from file (ANSI)
 *		feof		Check file for EOF (ANSI)
 *		fopen		Open file (ANSI)
 *		fclose		Close file (ANSI)
 *		fprintf		Print to stream (ANSI)
 *		SendBuf		Send buffer to server (NPNS)
 *		GetResponse	Get server numerical response (NNET)
 *		SendCommand	Send command to server (NNET)
 *		TORecv		Timed recv (NNET)
 *		send		Send data (BSD)
 *
 *  $Id: SendArticle.c 1.1 1996/05/06 22:55:02 nagd Exp $
 *
 */

#include "NPNS.h"

#define BUFSIZE 10000

int SendArticle( long soc, char *newsfile ) 

{

  int len;
  int rc;

  char *buf;
  FILE *fptr;

  if ( buf = (char *)malloc( (BUFSIZE+1) * sizeof( char ) ) ) {
 
  	if ( fptr = fopen( newsfile, "r" ) ) {

/*
 *   Tell server we are posting and check it wants articles
 */

		SendCommand( soc, "POST", NULL );

		if ( GetResponse( soc ) == 340 ) {

			TORecv( soc, buf, BUFSIZE, 0L, DEF_TIMEOUT );


/*
 *   Process file in BUFSIZE blocks
 */

			while ( !feof( fptr ) ) {

				len = fread( buf, sizeof( char ), BUFSIZE, fptr );
				buf[len] = '\0';

				if ( len > 0 ) SendBuf( soc, buf, len );

			}   /* while */

/*
 *   Terminate posting and check for acceptance
 */

			send( soc, TERM_STR, TERM_LEN, 0L );

			if ( GetResponse( soc ) == 240 ) {
				rc = TRUE;
 			} else {
				rc = FALSE;
				fprintf( stderr, " Server did not accept article\n" );
			}   /* if */

			TORecv( soc, buf, BUFSIZE, 0L, DEF_TIMEOUT );

			fclose( fptr );

		} else {

			fprintf( stderr, " Bad response to POST command\n" );
			rc = FALSE;

		}   /* if */

  	} else {

		fprintf( stderr, " Could not open file, %s for reading\n", newsfile );
		rc = FALSE;

  	}   /* if */

  	free( buf );

  } else {

	fprintf( stderr, " Not enough memory !\n" );
	rc = FALSE;

  }   /* if */

  return rc;

}   /* SendArticle */

/*========================================================================*
                       END FUNCTION SendArticle
 *========================================================================*/
