static char rcsid[] = "@(#)$Id: remail.c,v 1.43 2006/04/09 07:37:19 hurtta Exp $";

/******************************************************************************
 *  The Elm (ME+) Mail System  -  $Revision: 1.43 $   $State: Exp $
 *
 *  Modified by: Kari Hurtta <hurtta+elm@posti.FMI.FI> 
 *                           (was hurtta+elm@ozone.FMI.FI)
 ******************************************************************************
 *  The Elm Mail System 
 *
 *			Copyright (c) 1988-1992 USENET Community Trust
 *			Copyright (c) 1986,1987 Dave Taylor
 *****************************************************************************/

/** For those cases when you want to have a message continue along
    to another person in such a way as they end up receiving it with
    the reply address the person YOU received the mail from.

**/

#include "def_elm.h"
#include "s_elm.h"

DEBUG_VAR(Debug,__FILE__,"mail");

#include <errno.h>
#ifndef ANSI_C
extern int errno;
#endif       

static void remail_sent P_((union any_fd fd, char * title, 
			    struct run_state *rs,
			    int ret, int exit_stat));


static void remailing_message P_((int background));
static void remailing_message(background)
     int background;
{
    if (!background)
	lib_transient(CATGETS(elm_msg_cat, ElmSet, ElmResendingMail,
			      "Resending mail..."));
    else
	lib_error(CATGETS(elm_msg_cat, ElmSet, 
			  ElmResendingMailBackground,
			  "Resending mail... in background"));
    
    FlushBuffer();
}

void remail(current_header,F1, aview, page, prompt_area) 
     struct header_rec *current_header;
     FILE *F1;
     struct AliasView     *aview;
     struct menu_context  *page;
     struct menu_context  *prompt_area;
{
  
    struct mail_send_state * mailer = NULL;

    char buffer[VERY_LONG_STRING];

    int encoding = ENCODING_NONE;
    char title[80];
    struct mailing_headers mailing_headers;

    struct mailer_info *mailer_info   = get_mailer_info();
    int li,co,ch;
    int delay_redraw = 0;

    menu_get_sizes(prompt_area, &li, &co);   

    if (!mailer_info)
	return;

    /* Actually headers are not written. headers structure is just
     * used to pass mailing addresses to backend .
     */
    zero_mailing_headers(&mailing_headers);
    
    if (!current_header || !F1)
	return;
    
    /* free_mailing_headers will free this */
    if (!mailing_headers.env_from)
	mailing_headers.env_from = mailer_get_env_from(mailer_info);
    
    if (mailing_headers.env_from) {
	/* Read .mailheaders for Return-Path (for envelope sender actually) */
	import_mailheaders(&mailing_headers);
    }

    if (! get_to(&mailing_headers.to,mailer_info, aview, page,
		 NULL, NULL,prompt_area))
	goto free_it;
  
    display_to(mailing_headers.to, page,prompt_area);
    
    {
	char **argvt = argv_from_headers(&mailing_headers);
	mailer = mailer_init(argvt,0,sendmail_verbose,mailer_info,
			     mailing_headers.env_from);
	free(argvt);
    }

    if (!mailer) {
	goto free_it;
    }
    
    /** now let's copy the message into the newly opened
	buffer... **/
    
    /* !! */
    if (!copy_message_2(F1,
			current_header,
			"", 
			get_mail_outfd(mailer), 
			CM_REMOVE_ENVELOPE | CM_REMAIL,
			0)) {
	goto free_it;
    }
    
    /** Got the messsage, now let's ensure the person really wants to 
	remail it... **/

 again:  
    menu_ClearLine(prompt_area,2);
    menu_ClearLine(prompt_area,3);


    ch = prompt_letter(2,"",*def_ans_yes,
		       PROMPT_yesno|PROMPT_redraw_mark|PROMPT_ctrlL|
		       PROMPT_cancel,
		       prompt_area,
		       
		       CATGETS(elm_msg_cat, ElmSet, ElmSureYouWantToRemail,
			       "Are you sure you want to remail this message (%c/%c)? "),
		       *def_ans_yes, *def_ans_no);
    if (('L'&31)    == ch ||
	REDRAW_MARK == ch) {
	menu_ClearScreen(page);   /* Reset possible redraw flag */

	/* Call refresh routines of children */
	menu_redraw_children(page);

	if (menu_need_redraw(prompt_area))		    
	    menu_ClearScreen(prompt_area); /* clear redraw flag */

	delay_redraw++;   /* Can't trigger redraw yet... */
	goto again;
    }
    if (delay_redraw)
	menu_trigger_redraw(page);

    if (ch == *def_ans_no ||
	TERMCH_interrupt_char == ch) { /* another day, another No... */

	lib_error(CATGETS(elm_msg_cat, ElmSet, ElmBounceCancelled,
			  "Bounce of message canceled."));
	goto free_it;
    }
    
    if (current_header -> status & MIME_MESSAGE) {
	encoding = current_header->mime_rec.encoding;
	
	if (encoding == ENCODING_BINARY && 
	    !(current_header->binary)) {
	    DPRINT(Debug,2,(&Debug, 
			    "Remail: resets encoding=BINARY to encoding=8BIT\n"));
	    encoding = ENCODING_8BIT;
	}
    }

    elm_sfprintf(title, sizeof title,
		 CATGETS(elm_msg_cat, ElmSet, ElmRemailTo,
			 "Remail to %.50s..."),
		 mailing_headers.to.addrs ? mailing_headers.to.addrs[0].addr : 
		 "(someone)");
  
    mail_backend2(&mailer,remail_sent,encoding,title,remailing_message);
  
 free_it:

    if(mailer)
	free_mail_send_state(&mailer);

    free_mailing_headers(&mailing_headers);

    free_mailer_info(&mailer_info);

    menu_trigger_redraw(prompt_area);

    return;
}

static void remail_sent (fd,title,rs,ret,exit_code)
     union any_fd fd; 
     char * title;
     struct run_state *rs;
     int ret; 
     int exit_code;
{
    lower_prompt(title);
    
    if (ret < 0) 
	lib_error(CATGETS(elm_msg_cat, ElmSet, 
			  ElmFailSignal,
			  "%.30s fail: Signal?"),
		  get_mailer_path(fd.mail_fd));
    else if (ret > 0) {
	if (rs->save_errno)
	    lib_error(CATGETS(elm_msg_cat, ElmSet, 
			      ElmFailErrno,
			      "Failed: %.30s: %.40s"),
		      get_mailer_path(fd.mail_fd),
		      error_description(rs->save_errno));
	else if (exit_code) {
	    lib_error(CATGETS(elm_msg_cat, ElmSet, 
			      ElmMailerReturnedError,
			      "mailer returned error status %d"), 
		      exit_code);
	} else
	    lib_error(CATGETS(elm_msg_cat, ElmSet, ElmMailResent, 
			      "Mail resent."));
    } else {
	lib_error(CATGETS(elm_msg_cat, ElmSet, 
			  ElmLostErrno,"%.30s lost: %.40s"),
		  get_mailer_path(fd.mail_fd),
		  error_description(rs->save_errno));
    }
}

/*
 * Local Variables:
 *  mode:c
 *  c-basic-offset:4
 *  buffer-file-coding-system: iso-8859-1
 * End:
 */


syntax highlighted by Code2HTML, v. 0.9.1