/*:ts=8*/
/*****************************************************************************
 * FIDOGATE --- Gateway UNIX Mail/News <-> FTN NetMail/EchoMail
 *
 * $Id: ftnbounce.c,v 3.9.2.0 1995/06/12 17:14:03 mj Exp $
 *
 * Bounce NetMail/EchoMail addressed to unknown FTN nodes
 *****************************************************************************
 * Copyright (C) 1990-1995
 *  _____ _____
 * |     |___  |   Martin Junius             FIDO:      2:2452/110.1
 * | | | |   | |   Republikplatz 3           Internet:  mj@sungate.fido.de
 * |_|_|_|@home|   D-52072 Aachen, Germany   Phone:     ++49-241-86931 (voice)
 *
 * This file is part of FIDOGATE.
 *
 * FIDOGATE is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * FIDOGATE is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with FIDOGATE; see the file COPYING.  If not, write to the Free
 * Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *****************************************************************************/

#include "fidogate.h"
#include "getopt.h"


#define PROGRAM "ftnbounce"
#define VERSION "$Revision: 3.9.2.0 $"



/*
 * Prototypes
 */
int	do_message		P((Message *, MsgBody *));
int	unpack			P((FILE *, Packet *));
int	unpack_file		P((char *));
int	do_outbound		P((Node *));
void	short_usage		P((void));
void	usage			P((void));



static char *o_flag=NULL;	/* -o --out-packet-file option */



/*
 * Process one message for zone gate
 */
int do_message(msg, body)
    Message *msg;
    MsgBody *body;
{
    static int last_zone = -1;
    static FILE *fp;
    
    /*
     * Write this message to the output packet
     */
    if(!o_flag && cf_zone()!=last_zone)
	pkt_close();
    if(!pkt_isopen())
	if( (fp = pkt_open(o_flag, NULL, NULL, FALSE)) == NULL )
	    return ERROR;
    last_zone = cf_zone();
    
    msg->area = NULL;
    if( pkt_put_msg_hdr(fp, msg, TRUE) != OK )
	return ERROR;
    if( msg_put_msgbody(fp, body, TRUE) != OK )
	return ERROR;
    
    return OK;
}



/*
 * Unpack and process messages
 */
int unpack(pkt_file, pkt)
    FILE *pkt_file;
    Packet *pkt;
{
    Message msg;
    Textlist tl;
    MsgBody body;
    int type;

    tl_init(&tl);
    msg_body_init(&body);
    
    type = pkt_get_int16(pkt_file);
    while(type == MSG_TYPE)
    {
	msg.node_from = pkt->from;
	msg.node_to   = pkt->to;
	pkt_get_msg_hdr(pkt_file, &msg);
	
	type = pkt_get_body(pkt_file, &tl);
	if(type == ERROR)
	{
	    log("error reading input packet");
	    return ERROR;
	}
	if( msg_body_parse(&tl, &body) == -2 )
	    log("error parsing message body");

	/* Retrieve address information from body */
	if(!body.area)
	    kludge_pt_intl(&body, &msg);

/*
	if( do_message(&msg, &body) != OK )
	    return ERROR;
 */
    }

    /* Close packet */
    pkt_close();
    
    return OK;
}



/*
 * Unpack one packet file
 */
int unpack_file(pkt_name)
    char *pkt_name;
{
    Packet pkt;
    FILE *pkt_file;

    debug(1, "Unpacking FTN packet %s", pkt_name);

    /*
     * Open packet and read header
     */
    pkt_file = fopen(pkt_name, R_MODE);
    if(!pkt_file) {
	log("$Can't open packet %s", pkt_name);
	exit(EX_OSERR);
    }
    if(pkt_get_hdr(pkt_file, &pkt) == ERROR)
    {
	log("Error reading header from %s", pkt_name);
	exit(EX_OSERR);
    }
    
    /*
     * Unpack it
     */
    if(unpack(pkt_file, &pkt) != OK) 
    {
	log("Error in unpacking %s", pkt_name);
	exit(EX_OSERR);
    }
    
    fclose(pkt_file);
/*
    if (unlink(pkt_name)) {
	log("$Could not unlink packet %s", pkt_name);
	exit(EX_OSERR);
    }
*/
    return OK;
}



/*
 * Process packet for gateway address in Binkley outbound
 */
int do_outbound(node)
    Node *node;
{
    char *name;
    
    /* Create BSY file */

    /* Find and process OUT packet file */
    if( (name = bink_find_out(node, NULL)) )
	unpack_file(name);
    
    /* Delete BSY file */
    
    return OK;
}



/*
 * Usage messages
 */
void short_usage()
{
    fprintf(stderr, "usage: %s [-options] Z:N/F.P ...\n", PROGRAM);
    fprintf(stderr, "       %s --help  for more information\n", PROGRAM);
}


void usage()
{
    fprintf(stderr, "FIDOGATE %s  %s %s\n\n",
	    version_global(), PROGRAM, version_local(VERSION) );
    
    fprintf(stderr, "usage:   %s [-options] Z:N/F.P ...\n\n", PROGRAM);
    fprintf(stderr, "\
options: -B --binkley                 set Binkley outbound directory\n\
         -o --out-packet-file NAME    set output packet file name\n\
	 -O --out-packet-dir NAME     set output packet directory\n\
\n\
         -v --verbose                 more verbose\n\
	 -h --help                    this help\n\
         -c --config name             read config file (\"\" = none)\n\
	 -L --lib-dir name            set lib directory\n\
	 -S --spool-dir name          set spool directory\n\
	 -a --addr Z:N/F.P            set FTN address\n\
	 -f --fake-addr Z:N/F.P       set FTN fakenet address\n\
	 -u --uplink-addr Z:N/F.P     set FTN uplink address\n");

    exit(0);
}



/***** main() ****************************************************************/

int main(argc, argv)
    int argc;
    char *argv[];
{
    int c;
    char *B_flag=NULL;
    char *O_flag=NULL;
    char *c_flag=NULL;
    char *S_flag=NULL, *L_flag=NULL;
    char *a_flag=NULL, *f_flag=NULL, *u_flag=NULL;

    int option_index;
    static struct option long_options[] =
    {
	{ "binkley",      1, 0, 'B'},	/* Binkley outbound base dir */
	{ "out-packet-file",1,0,'o'},	/* Set packet file name */
	{ "out-dir",      1, 0, 'O'},	/* Set packet directory */

	{ "verbose",      0, 0, 'v'},	/* More verbose */
	{ "help",         0, 0, 'h'},	/* Help */
	{ "config",       1, 0, 'c'},	/* Config file */
	{ "spool-dir",    1, 0, 'S'},	/* Set FIDOGATE spool directory */
	{ "lib-dir",      1, 0, 'L'},	/* Set FIDOGATE lib directory */
	{ "addr",         1, 0, 'a'},	/* Set FIDO address */
	{ "fake-addr",    1, 0, 'f'},	/* Set FIDO fakenet address */
	{ "uplink-addr",  1, 0, 'u'},	/* Set FIDO uplink address */
	{ 0,              0, 0, 0  }
    };

    log_program(PROGRAM);
    
    /* Init configuration */
    cf_initialize();


    while ((c = getopt_long(argc, argv, "B:o:O:vhc:S:L:a:f:u:",
			    long_options, &option_index     )) != EOF)
	switch (c) {
	/***** ftn2ftn options *****/
	case 'B':
	    B_flag = optarg;
	    break;
	case 'o':
	    /* Set packet file name */
	    o_flag = optarg;
	    break;
	case 'O':
	    /* Set packet dir */
	    O_flag = optarg;
	    break;
		
	/***** Common options *****/
	case 'v':
	    verbose++;
	    break;
	case 'h':
	    usage();
	    exit(0);
	    break;
	case 'c':
	    c_flag = optarg;
	    break;
	case 'S':
	    S_flag = optarg;
	    break;
	case 'L':
	    L_flag = optarg;
	    break;
	case 'a':
	    a_flag = optarg;
	    break;
	case 'f':
	    f_flag = optarg;
	    break;
	case 'u':
	    u_flag = optarg;
	    break;
	default:
	    short_usage();
	    exit(EX_USAGE);
	    break;
	}

    /*
     * Read config file
     */
    if(L_flag)				/* Must set libdir beforehand */
	cf_set_libdir(L_flag);
    cf_read_config_file(c_flag);

    /*
     * Process config options
     */
    if(B_flag)
	cf_set_outbound(B_flag);
    if(L_flag)
	cf_set_libdir(L_flag);
    if(S_flag)
	cf_set_spooldir(S_flag);
    if(a_flag)
	cf_set_addr(a_flag);
    if(f_flag)
	cf_set_fake(f_flag);
    if(u_flag)
	cf_set_uplink(u_flag);

    cf_debug();

    /*
     * Process local options
     */
    if(O_flag)
	pkt_outdir(O_flag, NULL);
    else
	pkt_outdir(cf_spooldir(), OUTDIR);

    /*
     * Process command line file args
     */
    for(; optind<argc; optind++)
    {
	char *s;
	Node node;
	
	s = argv[optind];
	if(asc_to_node(s, &node, FALSE) == ERROR)
	    log("illegal node address %s", s);
	else
	    do_outbound(&node);
    }
    
    exit(EX_OK);

    /**NOT REACHED**/
    return 1;
}
