
/*
 *	Program 	postnews
 *	Programmer	N.d'Alterio
 *	Date		06/07/95
 *
 *  Synopsis:	This program spools an news article ready for posting at
 *              some later time. If currently online then sendnews is 
 *              called and the article will be sent off immediately.
 *		The online check is done by trying to open TCP: daytime
 *              port.
 *
 *		This is a heavily modified version of postnews by 
 *              James Burton from his postnewsspoll package. This fixes
 *              a number of bugs such as the leaving of locks, hanging
 *              for a long time while waiting to see if it is online.
 *		In addition the program has been very much streamlined
 *              there are no longer the command line options -X, -R as
 *              these seemd to have no use. Also gone are the arg### and
 *              ref### files it produced which seemed to be useless.
 *	
 *		For tin users the program has the command line option
 *              -from 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
 *              are the 2 env variables PNS_SEQ which is the full filename
 *              for the seq file and PNS_SPOOLDIR which is the full directory
 *              name for the spool dir.
 *
 *              The program expects to have the companion program sendnews
 *              in AmiTCP:bin/sendnews which in turn needs to have the 
 *              NNTP posting program as AmiTCP:bin/NNTPPost. 	
 *
 *  Command Line Arguments:	-from		Adds a From: someone@ line to header
 *                              -h              Prints some help info
 *				filename	The article file to post
 *                                              if omitted will take from STDIN
 *
 *  Inputs:		article			Should contain a full header
 *
 *  Outputs:		spooldir/msg### 	The spooled message
 *                      spooldir/Seq		The number of articles
 *
 *  Variables:		int i			general loop var
 *			int seq			number of spooled messages
 *			int DoStdin		flag 
 *			BPTR lock		lock on spooldir
 *			char *Buffer 		string to build filenames
 *			FILE *msgfile		file to contain spooled  article
 *
 *  Functions:		copyfile		copies article to spooldir
 *			getseq			reads seq file
 *
 * $VER: postnews.c 2.1 (07.08.95) $		
 * $Log: postnews.c $
 * Revision 2.1  1995/08/27  21:49:10  daltern
 * Added env vars for path options
 * fixed a bug where a file was left open
 *
 * Revision 1.1  1995/08/07  14:17:55  daltern
 * Initial revision
 *
 *
 */


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

#define SPOOLDIR    "AmiTCP:Usr/Spool/News"
#define SEQFILE     "AmiTCP:Usr/Spool/News/Seq"

int getseq( char * );
void copyfile( char *, FILE * );

extern char *version = "$VER: postnews 2.1 (07.08.95) Nick d'Alterio";

int main( int argc,char **argv )

{

  FILE *msgfile;
    
  int i;          
  int seq;
  int DoStdin = TRUE;

  char *seqfile;
  char *spooldir;

  char Buffer[100];   

  unsigned long lock;                     

  FILE *fptr;


/*
 *   Read spool dir from env variable.
 */

  if ( ( spooldir = getenv( "PNS_SPOOLDIR" ) ) == NULL ) {

	spooldir = SPOOLDIR;

  }   /* end if */

/* 
 *   Read seqfile name from env variable.
 */

  if ( ( seqfile = getenv( "PNS_SEQ" ) ) == NULL ) {

	seqfile = SEQFILE;

  }   /* end if */

/*
 *   Lock directory while using.
 */
      
  lock = Lock( spooldir, ACCESS_WRITE );
  if (lock == NULL) {            

	fprintf( stderr, " Could not lock spool directory\n" );
	exit(20);

    }   /* end if */

/*
 *   Get current number of spooled messages.
 */

  seq = getseq( seqfile );
  if (seq < 0) {
            
	fprintf( stderr, " Could not reserve sequence number : '%s'\n",seqfile );
        UnLock( lock );
	exit(20);
  
  }   /* end if */

/*
 *   Open file to spool articles to.
 */

  sprintf( Buffer, "%s/msg%03d", spooldir, seq );
  msgfile = fopen( Buffer, "w" );
  if ( msgfile == NULL ) {

	fprintf( stderr, " Could not open msg file '%s'\n", Buffer );
	UnLock(lock);
	exit(20);

  }   /* end if */         
  
/*
 *   Parse command line.
 */
  
  i = 0;
  while ( i < argc ) {    

	i++;
	if ( argv[i][0] == '-' ) {   

		switch ( argv[i][1] ) {
       
                case 'f' : 

			if ( strcmp( argv[i], "-from" ) == 0 ) {

				fprintf( msgfile, "From: %s\n", argv[i+1] );

			}   /* end if */
			
			i++;
			break;

		case 'h' :

			fprintf( stderr, " Usage: postnews [-from me@somewhere] article_filename\n" );
                        fprintf( stderr, "\n Article is taken from stdin if no filename is given\n" );
               		break;

		default :
 
			fprintf( stderr, " Unknown option use -h for help\n" );
			break;

		}   /* end switch */

        } else {   

/*
 *   Move article file to spool file.
 */

		if ( argv[i] != NULL && argv[i][0] != '\0' ) {

			copyfile( argv[i], msgfile );
			DoStdin = FALSE;
            
		}   /* end if */

	}   /* end if */

  }   /* end while */                    
    
/*
 *   Move article from stdin to spool file.
 */

  if ( DoStdin == TRUE ) {

        int ch;
        while ( ( ch = getchar() ) != EOF ) fputc( ch, msgfile );
    
  }   /* end if */
                  
  fclose( msgfile );
  UnLock( lock );
    
/*
 *   Check if online. If yes then call sendnews to send off articles
 *   now.
 */

  if ( ( fptr = fopen( "TCP:localhost/daytime", "r" ) ) == NULL ) {

	fprintf( stderr, " You are not online - message spooled\n" );

  } else {

	fclose( fptr );
	fprintf( stderr, " Posting spooled news now....\n" );
	system("amitcp:bin/sendnews");
  
  }   /* end if */
    
  return(0);

}   /* end program postnews */

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



/*========================================================================*
                          BEGIN FUNCTION getseq
 *========================================================================*/

int getseq( char *seqfilename )

{

  FILE *seqfile;       
  
  int seq;
                     
  seqfile = fopen( seqfilename, "r" );
  if ( seqfile == NULL ) {
   
	seq = 0;

  } else {

	fscanf( seqfile, "%d", &seq );
	fclose( seqfile );
        
  }   /* end if */                   

  seqfile = fopen( seqfilename, "w" );
  if ( seqfile == NULL ) {
            
	seq = -1;

  } else {

	fprintf( seqfile, "%d", seq+1);
	fclose( seqfile );

  }   /* end if */

  return seq;

}   /* end function getseq */

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



/*========================================================================*
                          BEGIN FUNCTION copyfile
 *========================================================================*/

void copyfile( char *name, FILE *fp )

{ 
               
  FILE *infile;

  int ch;
    
  infile = fopen( name, "r" );
  if ( infile == NULL ) {

	fprintf( stderr, " Can't open article to spool\n" );
	return;

  }   /* end if */

  while ( ( ch = fgetc( infile ) ) != EOF ) fputc( ch, fp );
        
  fclose( infile );

}   /* end function copyfile */

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