
/*
 *	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:	AllocMem	Allocate memory (EXEC)
 *		FreeMem		Free memory (EXEC)
 *		Read		Read data from file (ADOS)
 *		Open		Open file (ADOS)
 *		Close		Close file (ADOS)
 *		Printf		Print to stream (ADOS)
 *		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.3 1996/05/10 22:29:08 nagd Exp $
 *
 */

#include "NPNS.h"

#define BUFSIZE 10000

int SendArticle( long soc, char *newsfile ) 

{

  int len;
  int rc;
  int code;

  char *buf;
 
  ULONG mem_size;
  BPTR  fh;

  mem_size = ( BUFSIZE + 1 ) * sizeof( char );

  if ( buf = (char *)AllocMem( mem_size, MEMF_ANY ) ) {
 
 
/*
 *   Tell server we are posting and check it wants articles
 */

 	if ( fh = Open( newsfile, MODE_OLDFILE ) ) {

		SendCommand( soc, "POST", NULL );

		code = GetResponse( soc );
		TORecv( soc, buf, BUFSIZE, 0L, DEF_TIMEOUT );
		if (  code == 340 ) {


/*
 *   Process file in BUFSIZE blocks
 */


			for ( ;; ) {

				len = Read( fh, buf, BUFSIZE );
				buf[len] = '\0';

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

			}   /* forever */

/*
 *   Terminate posting and check for acceptance
 */

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

			code = GetResponse( soc );
			TORecv( soc, buf, BUFSIZE, 0L, DEF_TIMEOUT );

			if (  code == 240 ) {
				rc = TRUE;
 			} else {
				rc = FALSE;
				Printf( " Server did not accept article\n" );
			}   /* if */

		} else {

			Printf( " Bad response to POST command\n" );
			rc = FALSE;

		}   /* if */

		Close( fh );

	} else {

		PrintFault( IoErr(), newsfile );
		rc = FALSE;

  	}   /* if */

  	FreeMem( buf, mem_size );

  } else {

	Printf( " Not enough memory !\n" );
	rc = FALSE;

  }   /* if */

  return rc;

}   /* SendArticle */

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