/*****************************************************************************
 *
 * File ..................: mbcico/binkp.c
 * Purpose ...............: Fidonet binkd protocol
 * Last modification date : 22-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.
 *****************************************************************************/

#include "../include/libs.h"
#include "../include/structs.h"
#include "../include/records.h"
#include "../common/common.h"
#include "ttyio.h"
#include "session.h"
#include "statetbl.h"
#include "config.h"
#include "emsi.h"
#include "respfreq.h"
#include "filelist.h"
#include "tcpproto.h"
#include "binkp.h"
#include "config.h"


void binkp_send_control(int id,...);

/* static char *mon[] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
file_list *tosend=NULL;
static FILE *infile=NULL,*outfile=NULL;
long infilesize,infiletime,infileoffset;
long outfilesize,outfiletime,outfileoffset;
char *infilename,*outfilename;
static long instartime,outstartime,endtime,rxbytes,sbytes; */



int binkp(int mode)
{
	int	rc = 0;

	if (mode == 1) {
		Syslog('+', "start inbound BINKP session");
		Syslog('+', "Not implemented yet");
		binkp_send_control(MM_ERR,"Binkp protocol not yet available");
		rc = 15;
	} else {
		Syslog('+', "start inbound BINKP session");
		Syslog('+', "Not implemented yet");
		binkp_send_control(MM_ERR,"Binkp protocol not yet available");
		rc = 15;
	}

	rc = abs(rc);
	return rc;
}



void binkp_send_control(int id,...)
{
	va_list		args;
	char		*fmt, *s;
	binkp_frame	frame;
	static char	buf[1024];
	int		i, sz;

	va_start(args, id);
	fmt = va_arg(args, char*);

	if (fmt) {
		vsprintf(buf, fmt, args);
		sz = ((1 + strlen(buf)) & 0x7fff);
	} else {
		buf[0]='\0';
		sz = 1;
	}

	frame.header = ((BINKP_CONTROL_BLOCK + sz) & 0xffff);
	frame.id = (char)id;
	frame.data = buf;

	s = (unsigned char *)malloc(sz + 2 + 1);
	s[sz + 2] = '\0';
	s[0] = ((frame.header >> 8)&0xff);
	s[1] = (frame.header & 0xff);
	s[2] = frame.id;
	if (frame.data[0])
		strncpy(s + 3, frame.data, sz-1);
	
	for (i = 0; i < sz+2; i++)
		PUTCHAR(s[i]);

	va_end(args);
}



/*
 * Originate a binkp session
 */
SM_DECL(orgbinkp, (char *)"orgbinkp")
SM_STATES
	conninit,
	waitconn,
	sendpass,
	waitaddr,
	authremote,
	ifsecure,
	waitok
SM_NAMES
	(char *)"conninit",
	(char *)"waitconn",
	(char *)"sendpass",
	(char *)"waitaddr",
	(char *)"authremote",
	(char *)"ifsecure",
	(char *)"waitok"
SM_EDECL

SM_START(conninit)

SM_STATE(conninit)
SM_STATE(waitconn)
SM_STATE(sendpass)
SM_STATE(waitaddr)
SM_STATE(authremote)
SM_STATE(ifsecure)
SM_STATE(waitok)

SM_END
SM_RETURN
	


/*
 * Answer a binkp session
 */
SM_DECL(ansbinkp, (char *)"ansbinkp")
SM_STATES
	waitconn,
	waitaddr,
	ispasswd,
	waitpwd,
	pwdack
SM_NAMES
	(char *)"waitconn",
	(char *)"waitaddr",
	(char *)"ispasswd",
	(char *)"waitpwd",
	(char *)"pwdack"

SM_EDECL

SM_START(waitconn)

SM_STATE(waitconn)
SM_STATE(waitaddr)
SM_STATE(ispasswd)
SM_STATE(waitpwd)
SM_STATE(pwdack)

SM_END

SM_RETURN



/*
 * File transfers state machine
 */
SM_DECL(transfer, (char *)"transfer")
SM_STATES
	inittransfer,
	rtswitch,
	receive,
	transmit
SM_NAMES
	(char *)"inittransfer",
	(char *)"rtswitch",
	(char *)"receive",
	(char *)"transmit"
SM_EDECL

SM_START(inittransfer)

SM_STATE(inittransfer)
SM_STATE(rtswitch)
SM_STATE(receive)
SM_STATE(transmit)

SM_END
SM_RETURN

