/*
 *	File Name:		reply.c
 *	Project:		BARN - Bah's Amiga ReadNews.
 *	Purpose:		Reply via email and post followup article functions.
 *	Functions:		Reply, Followup, IncludeArticle, Sign.
 *	Author:			Jeff Van Epps
 *	Created:		21 Oct 90
 *	Last Modified:	05 Jan 91
 *	Comments:
 *	History:
 *		21 Oct 90/JVE	Created.
 *		14 Nov 90/JVE	Post news by mailing article to inews@bisco rather
 *						than <newsgroup>@ucbvax.berkeley.edu.  Only mail the
 *						article if the user saved his edit.  Same for
 *						email replies.
 *		05 Jan 91/JVE	Use article->subject optimization.
 */

# include	<stdio.h>
# include	<stdlib.h>
# include	<string.h>
# include	<time.h>
# include	<sys/types.h>
# include	<sys/stat.h>
# include	"standard.h"
# include	"configure.h"
# include	"variables.h"
# include	"article.h"
# include	"reply.h"

# ifdef sun

# define	DEFAULT_EDITOR	"vi"
# define	TEMPFILE		"/tmp/arnreply"

# else  /* not sun */

# define	DEFAULT_EDITOR	"stevie"
# define	TEMPFILE		"T:arnreply"

# endif /* sun */


/****************************************************************************/
/*	FUNCTION:	Reply														*/
/*																			*/
/*	PURPOSE:	Send a reply to a news article via mail.					*/
/*																			*/
/*	INPUT PARAMETERS:														*/
/*		NAME		I/O		DESCRIPTION										*/
/*		----		---		-----------										*/
/*		article		 I		Article to reply to.							*/
/*		include		 I		Include article in reply?						*/
/*																			*/
/*	RETURNS: none															*/
/*																			*/
/*	COMMENTS:																*/
/*																			*/
/*	HISTORY:																*/
/*		1.	17 Oct 90		Created.										*/
/*		2.	14 Nov 90		Do not invoke mailer if user has not edited.	*/
/*																			*/
/****************************************************************************/

void Reply( article, include )

ARTICLE_INFO		*article;
int					include;

{
FILE				*fp;
char				*from, *address, *editor, *hdr;
char				cmd[MAXLINE], temp[MAXLINE];
long				clock;							/* current date & time */
int					header_found = FALSE;			/* parsed To: yet? */
struct stat			stats_before, stats_after;

remove( TEMPFILE );
if ( ( fp = fopen( TEMPFILE, "a" ) ) == NULL )
	{
	perror( TEMPFILE );
	return;
	}
time( &clock );
fprintf( fp, "From %s %24.24s remote from %s\n", GetVar( VAR_USER), 
  ctime( &clock ), GetVar( VAR_NODE ) );
fprintf( fp, "Date: %s", ctime( &clock ) );
fprintf( fp, "From: %s!%s@%s (%s)\n", GetVar( VAR_NODE ), GetVar( VAR_USER ), 
  GetVar( VAR_DOMAIN ), GetVar( VAR_NAME ) );
/*
 *	Use everything after From: up to the first space.
 */
from = strtok( GetHeader( article -> headers, HDR_FROM ), " \n\r" );
fprintf( fp, "%s: %s\n", HDR_TO, from );
fprintf( fp, "%s: \n", HDR_CC );
fprintf( fp, "%s: %s\n\n", HDR_SUBJECT, article -> subject );
if ( include )
	IncludeArticle( article, fp );
Sign( fp );
fclose( fp );
(void) stat( TEMPFILE, &stats_before );
if ( ( editor = GetVar( VAR_EDITOR ) ) == NULL )
	editor = DEFAULT_EDITOR;
sprintf( cmd, "%s %s", editor, TEMPFILE );
system( cmd );
(void) stat( TEMPFILE, &stats_after );
if ( stats_after.st_mtime != stats_before.st_mtime )
	{
	if ( ( fp = fopen( TEMPFILE, "r" ) ) == NULL )
		{
		perror( TEMPFILE );
		return;
		}
	while ( ! header_found && fgets( temp, MAXLINE, fp ) != NULL )
		{
		if ( ( hdr = strtok( temp, ": " ) ) != NULL && strcmp( hdr, HDR_TO ) == 0 )
			header_found = TRUE;
		}
	if ( ! header_found )
		{
		printf( "Header messed!\n" );
		fclose( fp );
		return;
		}
	if ( ( address = strtok( NULL, " " ) ) == NULL )
		{
		printf( "No address given!\n" );
		fclose( fp );
		return;
		}
	fclose( fp );
	sprintf( cmd, "rmail <%s >NULL: %s", TEMPFILE, address );
	system( cmd );
	}
else
	{
	printf( "File not modified.  Mail not sent.  Hit any key.\n" );
	getchar();
	}
remove( TEMPFILE );
}

/****************************************************************************/
/*	FUNCTION:	Followup													*/
/*																			*/
/*	PURPOSE:	Post news article as a reply to a news article.				*/
/*																			*/
/*	INPUT PARAMETERS:														*/
/*		NAME		I/O		DESCRIPTION										*/
/*		----		---		-----------										*/
/*		newsgroup	 I		Name of newsgroup.								*/
/*		article		 I		Article to followup.							*/
/*		include		 I		Include article in reply?						*/
/*																			*/
/*	RETURNS: none															*/
/*																			*/
/*	COMMENTS:																*/
/*		Changed to send mail to inews@bisco.  Newsgroup name is embedded	*/
/*		in the article.														*/
/*																			*/
/*		The newsgroup name has all dots or slashes turned into dashes, then	*/
/*		mail is sent to <modified newsgroup name>@ucbvax.berkeley.edu,		*/
/*		which is a gateway for posting.										*/
/*																			*/
/*	HISTORY:																*/
/*		1.	17 Oct 90		Created.										*/
/*		2.	14 Nov 90		Mail to bisco rather than Berkeley.  Only		*/
/*							mail the article if it has been edited by user.	*/
/*																			*/
/****************************************************************************/

void Followup( newsgroup, article, include )

char				*newsgroup;
ARTICLE_INFO		*article;
int					include;

{
FILE				*fp;
char				*editor;
char				cmd[MAXLINE];
long				clock;							/* current date & time */
struct stat			stats_before, stats_after;

remove( TEMPFILE );
if ( ( fp = fopen( TEMPFILE, "a" ) ) == NULL )
	{
	perror( TEMPFILE );
	return;
	}
time( &clock );
fprintf( fp, "From %s %24.24s remote from %s\n", GetVar( VAR_USER ), 
  ctime( &clock ), GetVar( VAR_NODE ) );
fprintf( fp, "Date: %s", ctime( &clock ) );
fprintf( fp, "From: %s!%s@%s (%s)\n", GetVar( VAR_NODE ), GetVar( VAR_USER ), 
  GetVar( VAR_DOMAIN ), GetVar( VAR_NAME ) );
fprintf( fp, "%s: %s\n", HDR_SUBJECT, article -> subject );
fprintf( fp, "Newsgroups: %s\n", newsgroup );
fprintf( fp, "Followup-To: %s\n", newsgroup );
fprintf( fp, "Distribution: world\n" );
fprintf( fp, "Reply-To: %s!%s@%s\n", GetVar( VAR_NODE ), GetVar( VAR_USER ),
  GetVar( VAR_DOMAIN ) );
fprintf( fp, "\n" );		/* separate headers from body of article */
if ( include )
	IncludeArticle( article, fp );
Sign( fp );
fclose( fp );
(void) stat( TEMPFILE, &stats_before );
if ( ( editor = GetVar( VAR_EDITOR ) ) == NULL )
	editor = DEFAULT_EDITOR;
sprintf( cmd, "%s %s", editor, TEMPFILE );
system( cmd );
(void) stat( TEMPFILE, &stats_after );
if ( stats_after.st_mtime != stats_before.st_mtime )
	{
	sprintf( cmd, "rmail <%s >NULL: inews@bisco", TEMPFILE );
	system( cmd );
	}
else
	{
	printf( "File not modified.  Article not posted.  Hit any key.\n" );
	getchar();
	}
remove( TEMPFILE );
}


/****************************************************************************/
/*	FUNCTION:	IncludeArticle												*/
/*																			*/
/*	PURPOSE:	Copy article text to file as an inclusion.					*/
/*																			*/
/*	INPUT PARAMETERS:														*/
/*		NAME		I/O		DESCRIPTION										*/
/*		----		---		-----------										*/
/*		article		 I		Article being included.							*/
/*		output		 I		File being written into.						*/
/*																			*/
/*	RETURNS: none															*/
/*																			*/
/*	COMMENTS:																*/
/*																			*/
/*	HISTORY:																*/
/*		1.	21 Oct 90 		Created.										*/
/*																			*/
/****************************************************************************/

void IncludeArticle( article, output )

ARTICLE_INFO			*article;
FILE					*output;

{
char					filename[MAXLINE];
char					s[MAXLINE];
FILE					*art;

sprintf( filename, "%ld", article -> number );
if ( ( art = fopen( filename, "r" ) ) == NULL )
	printf( "CAN'T OPEN ARTICLE FOR INCLUDE!\n" );
else
	{
	fseek( art, article -> textpos, 0 );
	while ( fgets( s, MAXLINE, art ) != NULL )
		fprintf( output, "> %s", s );
	fclose( art );
	}
}


/****************************************************************************/
/*	FUNCTION:	Sign														*/
/*																			*/
/*	PURPOSE:	Append users .signature.									*/
/*																			*/
/*	INPUT PARAMETERS:														*/
/*		NAME		I/O		DESCRIPTION										*/
/*		----		---		-----------										*/
/*		output		 I		File being written to.							*/
/*																			*/
/*	RETURNS: none															*/
/*																			*/
/*	COMMENTS:																*/
/*		There is no default .signature file.								*/
/*																			*/
/*	HISTORY:																*/
/*		1.	21 Oct 90		Created.										*/
/*																			*/
/****************************************************************************/

void Sign( output )

FILE				*output;

{
FILE				*sig;
char				*sig_file;		/* filename for .signature */
char				s[MAXLINE];

if ( ( sig_file = GetVar( VAR_SIGN ) ) != NULL && 
  ( sig = fopen( sig_file, "r" ) ) != NULL )
	{
	while ( fgets( s, MAXLINE, sig ) != NULL )
		fprintf( output, "%s", s );
	fclose( sig );
	}
}
