
/*
 *	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 postnewsspool 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
 *              is an env variable PNS_SPOOLDIR which is the full directory
 *              name for the spool dir with no trailing '/'.
 *
 *              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
 *
 *  Variables:		int i			general loop var
 *			int DoStdin		flag 
 *			int exit_code		return code for prog
 *			BPTR lock		lock on spooldir
 *			char *Buffer 		string to build filenames
 *			FILE *msgfile		file to contain spooled  article
 *
 *  Functions:		copyfile		copies article to spooldir
 *
 * $VER: postnews.c 2.1 (07.08.95) $		
 * $Log: postnews.c $
 * Revision 3.1  1995/09/10  16:41:41  daltern
 * Removed need for seq file to comply with v3.1 of sendnews
 * spooled messages now have name of form msg.##########
 * where ###### is the long date returned by time()
 *
 *
 * Revision 1.2  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 <time.h>
#include <dos/dos.h>
#include <clib/dos_protos.h>
#include <clib/exec_protos.h>
#include <exec/types.h>

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

void copyfile( char *, FILE * );

extern char *version = "$VER: postnews 3.1 (09.09.95) Nick d'Alterio";
extern struct Library *DOSBase;

int main( int argc,char **argv )

{

  FILE *msgfile;
    
  int i;          
  int DoStdin   = TRUE;
  int exit_code = 0;

  char *spooldir;

  char Buffer[100];   

  BPTR lock;                     

  FILE *fptr;


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

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


/*
 *   Open dos library > v37
 */

  if ( OpenLibrary( "dos.library", 37 ) ) {

/*
 *   Lock directory while using.
 */
      
	if ( lock = Lock( spooldir, ACCESS_READ ) ) {
 

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

  		sprintf( Buffer, "%s/msg.%ld", spooldir, time( NULL ) );
  		if ( msgfile = fopen( Buffer, "w" ) ) {

/*
 *   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 );

		} else {

			fprintf( stderr, " Could not open message file - %s\n", Buffer );
			exit_code = 10;

		}   /* end if open msg file */

  		UnLock( lock );

	} else {

		fprintf( stderr, " Could not lock spooldir - %s\n", spooldir );
		exit_code = 20;

	}   /* end if lock */

  } else {

	fprintf( stderr, " Could not open dos.library > v37\n" );
	exit_code = 20;
 
  }   /* end if */         
  
    
/*
 *   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(exit_code);

}   /* end program postnews */

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

/*========================================================================*
                          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
 *========================================================================*/
