//=========================================================
//
//	mail.c
//
//	void mailto(char *xuser, char *xfile);
//
//	void replyto(int msg);
//
//	void forwardto(int msg, char *xuser);
//
//=========================================================

// $Id: mail.c,v 1.3 1994/02/14 23:37:32 gbj Exp user $

/*
$Log: mail.c,v $
 * Revision 1.3  1994/02/14  23:37:32  gbj
 * Incorporated changes for Alec Jones's outgoing mail log.
 *
 * Revision 1.2  1994/02/08  23:32:02  gbj
 * First public release.
 *
 * Revision 1.1  1994/02/08  03:15:10  gbj
 * Initial revision
 *
*/

#include "mailer.h"

static char subject[128];
static char cmd[128];

void mailto(char *xuser, char *xfile)
{
	int res, c, done;
	
	if (user == NULL)
	{
		fprintf(stderr, "mailto: No user!\n");
		return;
	}
	if (*user == '\0')
	{
		fprintf(stderr, "mailto: No user!\n");
		return;
	}
	
	subject[0]='\0';
	while (subject[0] == '\0')
	{
		printf("Subject: ");
		gets(subject);
	}
	
	if (xfile == NULL)
		res=setpost(xuser, subject, "");
	else if (*xfile == '\0')
		res=setpost(xuser, subject, "");
	else
		res=setpost(xuser, subject, xfile);
		
	if (res != 0)
	{
		remove(txt);
		remove(wrk);
		*txt='\0';
		*wrk='\0';
		return;
	}
	
	done=FALSE;
	while (!done)
	{
		printf("(P)ost, (E)dit or (A)bandon: ");
		c=toupper(getche());
		putch('\r');
		putch('\n');
		if (c == 'P')
		{
			if (*log)
				append_to_log(txt);
			done=TRUE;
		}
		else if (c == 'E')
		{
			sprintf(cmd, "%s %s", edit, txt);
			res=system(cmd);
			if (res == -1)
			{
				perror("mailto");
				fprintf(stderr, 
					"mailto: Couldn't run editor, command line is:\n");
				fprintf(stderr, "%s\n", cmd);
			}
			done=FALSE;
		}
		else if (c == 'A') 
		{
			remove(txt);
			remove(wrk);
			done=TRUE;
		}
	}
	*txt='\0';
	*wrk='\0';
	return;
}

void replyto(int msg)
{
	char subject[128], tsubject[128], *ts;
	int res, c, done, quote, copy_msg;

	if (msg < 0 || msg > maxmsgno)
	{
		printf("\nMessage %d does not exist\n\n", msg);
		return;
	}
	
	strcpy(tmp, "post.$$$");
	done=FALSE;
	while (!done)
	{
		printf("Quote the message (Y/N)? ");
		c=getche();
		putchar('\r');
		putchar('\n');
		if (toupper(c) == 'Y')
		{
			quote=TRUE;
			done=TRUE;
		}
		else if (toupper(c) == 'N')
		{
			quote=FALSE;
			done=TRUE;
		}
		else
			done=FALSE;
	}

	if (quote)
		copy_msg=TRUE;
	else
		copy_msg=FALSE;	
	res=tmp_msg(msg, tmp, quote, copy_msg); 
	if (res)
	{
		remove(tmp);
		return;
	}

	strcpy(tsubject, mailix[msg].subject);
	ts=strstr(tsubject, "Re:");
	if (ts == NULL)
		ts=strstr(tsubject, "RE:");
	if (ts == NULL)
		ts=strstr(tsubject, "re:");
	if (ts == NULL)
		ts=strstr(tsubject, "rE:");
	if (ts == NULL)
		sprintf(subject, "Re: %s", tsubject);
	else
	{
		*ts++='R';		// Make sure it says "Re:"
		*ts='e';
		sprintf(subject, "%s", tsubject);
	}

	res=setpost(mailix[msg].replyto, subject, tmp); 
	remove(tmp);
	*tmp='\0';	
	if (res != 0)
	{
		remove(txt);
		remove(wrk);
		*txt='\0';
		*wrk='\0';
		return;
	}
	
	done=FALSE;
	while (!done)
	{
		printf("(P)ost, (E)dit or (A)bandon: ");
		c=toupper(getche());
		putch('\r');
		putch('\n');
		if (c == 'P')
		{
			if (*log)
				append_to_log(txt);
			done=TRUE;
		}
		else if (c == 'E')
		{
			sprintf(cmd, "%s %s", edit, txt);
			res=system(cmd);
			if (res == -1)
			{
				perror("replyto");
				fprintf(stderr, 
					"replyto: Couldn't run editor, command line is:\n");
				fprintf(stderr, "%s\n", cmd);
			}
			done=FALSE;
		}
		else if (c == 'A') 
		{
			remove(txt);
			remove(wrk);
			done=TRUE;
		}
	}
	*txt='\0';
	*wrk='\0';
	return ;
}

void forwardto(int msg, char *xuser)
{
	char subject[128];
	int res, c, done;
	
	if (msg < 0 || msg > maxmsgno)
	{
		printf("\nMessage %s does not exist\n\n", msg);
		return;
	}
	strcpy(tmp, "post.$$$");
	done=FALSE;
	res=tmp_msg(msg, tmp, FALSE, TRUE); 
	if (res)
	{
		remove(tmp);
		return;
	}

	sprintf(subject, "Fwd: %s", mailix[msg].subject);
	res=setpost(xuser, subject, tmp); 
	remove(tmp);
	*tmp='\0';	
	if (res != 0)
	{
		remove(txt);
		remove(wrk);
		*txt='\0';
		*wrk='\0';
		return;
	}
	
	done=FALSE;
	while (!done)
	{
		printf("(P)ost, (E)dit or (A)bandon: ");
		c=toupper(getche());
		putch('\r');
		putch('\n');
		if (c == 'P')
		{
			if (*log)
				append_to_log(txt);
			done=TRUE;
		}
		else if (c == 'E')
		{
			sprintf(cmd, "%s %s", edit, txt);
			res=system(cmd);
			if (res == -1)
			{
				perror("replyto");
				fprintf(stderr, 
					"replyto: Couldn't run editor, command line is:\n");
				fprintf(stderr, "%s\n", cmd);
			}
			done=FALSE;
		}
		else if (c == 'A') 
		{
			remove(txt);
			remove(wrk);
			done=TRUE;
		}
	}
	*txt='\0';
	*wrk='\0';

	return;
}
