/*****************************************************************************
 *
 * File ..................: mbcico/answer.c
 * Purpose ...............: Fidonet mailer 
 * Last modification date : 15-Nov-1999
 *
 *****************************************************************************
 * Copyright (C) 1997-1999
 *   
 * Michiel Broek		FIDO:		2:2801/16
 * Beekmansbos 10		Internet:	mbroek@ux123.pttnwb.nl
 * 1971 BV IJmuiden
 * the Netherlands
 *
 * This file is part of MBSE BBS.
 *
 * This BBS 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.
 *
 * MBSE BBS 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 MBSE BBS; see the file COPYING.  If not, write to the Free
 * Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *****************************************************************************/

#define	DB_TTY

#include "../include/libs.h"
#include "../include/structs.h"
#include "../include/records.h"
#include "../common/common.h"
#include "lutil.h"
#include "session.h"
#include "config.h"
#include "answer.h"
#include "openport.h"
#include "portsel.h"
#include "dial.h"
#include "rdoptions.h"
#include "mbcico.h"


extern int	carrier, online;
extern time_t	c_start, c_end;


int answer(char *stype)
{
	int	st, rc;
	char	*p, *q;

	/*
	 *  mgetty set's the environment variable CONNECT and CALLER_ID,
	 *  so if they are present, we might as well make log entries.
	 */
	if ((q = getenv("CONNECT")) != NULL)
		Syslog('+', "CONNECT %s", q);
	if ((q = getenv("CALLER_ID")) != NULL)
		if (strncmp(q, "none", 4))
			Syslog('+', "CALLER  %s", q);

	/*
	 *  Incoming calls from modem/ISDN lines do have a tty.
	 *  Network calls don't have a tty attached.
	 */
	carrier = TRUE;
	p = ttyname(0);
	if (p) {
		q = strrchr(ttyname(0), '/');
		if (q)
			p = q + 1;
		if (load_port(p))
			Syslog('d', "Port %s, modem %s", ttyinfo.tty, modem.modem);
		else
			Syslog('d', "Port and modem not loaded!");
	}

	if ((nlent = getnlent(NULL)) == NULL) {
		WriteError("could not get dummy nodelist entry");
		return 1;
	}

	c_start = time(NULL);
	rdoptions();

	inbound = norminbound; /* slave session is unsecure by default */

	if (stype == NULL) {
		st=SESSION_UNKNOWN;
	} else if (strcmp(stype,"tsync") == 0) {
		st=SESSION_FTSC;
		IsDoing("Answer ftsc");
	} else if (strcmp(stype,"yoohoo") == 0) {
		st=SESSION_YOOHOO;
		IsDoing("Answer yoohoo");
	} else if (strncmp(stype,"**EMSI_",7) == 0) {
		st=SESSION_EMSI;
		IsDoing("Answer EMSI");
	} else if (strncmp(stype,"ibn",3) == 0) {
		st=SESSION_BINKP;
		IsDoing("Answer Binkp");
	} else {
		st=SESSION_UNKNOWN;
		IsDoing("Answer unknown");
	}

	if ((rc = rawport()) != 0)
		WriteError("Unable to set raw mode");
	else {
		nolocalport();
		rc=session(NULL,NULL,SESSION_SLAVE,st,stype);
	}

	cookedport();
	if (p)
		hangup();

	c_end = time(NULL);
	online += (c_end - c_start);
	return rc;
}


