/*************************************************************************
*	"nifty.c" : NIFTY-Serve 用センター処理
*-------------------------------------------------------------------------
*	Last modified	1993. 4.19
*					1993.10.02
*************************************************************************/

#ifdef		CENTERKEY
#	undef	CENTERKEY
#endif
#ifdef		RECKEY
#	undef	RECKEY
#endif

#define	CENTERKEY	"NIFTY"
#define	RECKEY		_nifty_recKey

int		_nifty_fenix;
char	*_nifty_recKey;

char	*_nif_amac = NULL;
char	_nifty_setid = 0;

int		_NIFTY_init()
{
	char	*center;

	NTM_setCenterFunc(CENTERKEY,"NIFTY-Serve (road 1/2/3)");
	if ( (center = NTM_centerAlloc( CENTERKEY )) != NULL )
	{
		NtmCenterSetName(    center, "NIFTY-Serve");	/* センタ名	*/
		NtmCenterSetFuncKey( center, "NIFTY");			/* 関数名	*/
		NtmCenterSetRecKey(  center, "NIFTY");			/* 記録用	*/
		NtmCenterSetRsPara(  center, -1, -1, -1 );		/* RS-232C	*/
		NtmCenterSetRsBreak( center, 50 );				/* ﾌﾞﾚｰｸ	*/
		NtmCenterSetCrWait(  center, 100 );				/* CRｳｪｲﾄ	*/
		NtmCenterSetTel(     center, "XX-XXXX-XXXX" );	/* Tel.		*/
		NtmCenterSetPara(    center, "-fenix 2" );		/* fenix	*/
		NtmCenterSetModem1(  center, "ATZ" );			/* ﾓﾃﾞﾑ		*/
	}

	return (NORMAL);
}

/*************************************************************************
*	パラメータの指定
*************************************************************************/

int		_NIFTY_para( char *ntm, char *center )
{
	char	   *keyWord;
	char	   *id;

	keyWord = NtmCenterGetKeyWord(center);
	if ( NTM_centerConfig( keyWord ) == NORMAL )
	{
		id  = NtmCenterGetID( center );
		if ( id[0] == 0 || NtmCenterChkPassword(center) == FALSE )
			return (ERR);
		return (NORMAL);
	} else
		return (ERR);
}


/*************************************************************************
*	処理の中断
*************************************************************************/

int		_NIFTY_abort( char *ntm, char *center )
{
	AplRecWrite("%s:Abort", RECKEY);
	NTM_offline( ntm, DTR_OFF_TIME );	/* DTR を指定時間 OFF */
	return (NORMAL);
}

/*************************************************************************
*	[AUTO]メニュー処理
*************************************************************************/

int		_NIFTY_autoMenu( char *ntm, char *center, int x, int y)
{
	int		ret;
	char	*obj;
	char	*mnu;
	char	title[128], tmp[128], tmBuf[32];
	long	loginTime;

	loginTime = NtmCenterGetTime(center, NTM_TIME_LOGIN );
	if ( loginTime )
	{
		sprintf( tmp, "Login time %s ", GetAsciiTime(tmBuf, loginTime) );
	} else
	{
		strcpy ( tmp, "Login time XXXX/XX/XX XX:XX:XX");
	}
	obj = MNU_alloc(3);
	MNU_setObj(obj, 0, MNU_MSG,  0 , tmp               , NULL, 1, NULL, NULL );
	MNU_setObj(obj, 1, MNU_NOR, 'O', "OFF 終了"        , NULL, 1, NULL, NULL );
	MNU_setObj(obj, 2, MNU_NOR, 'B', "OFF 強制回線切断", NULL, 2, NULL, NULL );

	sprintf( title, "MENU : %s", NtmCenterGetName(center));
	mnu = MNU_open( x, y, MNUATT_EXIT, obj, title );
	ret = MNU_start ( mnu );
	MNU_close(mnu);
	MNU_free(obj);
	switch ( ret )
	{
		case 1:
			return	NTM_callLogout( ntm );
		case 2:
			NTM_wait(3);
			AplRecWrite("%s:Abort disconnect", RECKEY);
			return	NTM_offline(ntm, DTR_OFF_TIME);
	}
	return (NORMAL);
}

#ifdef	_TAROPYON_SYSTEM
#	include	"niftylog.c"
#endif

/*************************************************************************
*	ログイン処理
*************************************************************************/

int		_NIFTY_login( char *ntm, char *center )
{
	int		i, ret, retry;
	char	*name, *tel, *id, *para;
	int		argc;
	char	*s, **argv;
	long	tm;
	char	*cns;
	char	*mos;
	char	*btn;

	_nifty_setid = 0;
	mos = NULL;
	cns = NtmGetCnsPtr(ntm);

#ifdef	_TAROPYON_SYSTEM
	/* ログ保存行数を増やす	*/
	if ( CnsGet_maxLinBuf(cns) < 99999 )
		CnsSet_maxLinBuf(cns,99999);
#endif

	_nifty_recKey = NtmCenterGetRecKey(center);

	name = NtmCenterGetName( center );
	tel  = NtmCenterGetTel( center );
	id   = NtmCenterGetID( center );
	if ( tel[0] == 0 || id[0] == 0 || NtmCenterChkPassword(center) == FALSE )
	{
		_ntm_errmsg_no_setup_id_and_pw();
		if ( _NIFTY_para( ntm, center ) )
			return (ERR);
		id  = NtmCenterGetID( center );
		tel = NtmCenterGetTel( center );
	}

	/*	オプションパラメータの解析	------------------------------------*/
	_nifty_fenix = 2;	/* ROAD 2 */
	para = NtmCenterGetPara( center );
	argc = FS_argSet( para, &argv );
	if ( argc > 0 )
	{
		for ( i = 0; i < argc; ++i )
		{
			s = argv[i];
			if ( strcmpi( s, "-fenix") == 0 )
			{
				s = argv[++i];
				_nifty_fenix = atoi(s);
			}
		}
		FS_argClear( argc, argv );
		if ( _nifty_fenix < 1 || _nifty_fenix > 3 )
			_nifty_fenix = 2;
	}

	_ntm_dspmsg_dial_start( name, tel, cns );

	AplRecWrite("%s:Dial start tel.%s", RECKEY, tel );
	if ( ntm_dialStart( ntm, tel ) != NORMAL )
	{
ABORT:	_NIFTY_abort( ntm, center );
		if ( mos )	MOS_pop(mos);
		return (ERR);
	}

	mos = MOS_push();
	MOS_curType(MOSCUR_TIME);
	MOS_CON();

	AplRecWrite("%s:Connect", RECKEY );

	NTM_wait(2);
	if ( _nifty_fenix == 2 || _nifty_fenix == 3 )
	{
		if ( _nifty_fenix == 2 )
		{	/* ROAD 2 */
			NtmSetBpSpeed(ntm,3);	/* 2400 */
			retry = 0;
			while ( 1 )
			{
				NTM_rsPuts( ntm, "@P\r");
				ret = NTM_waitStr( ntm, 30, "*", NULL );
				if ( ret == 0  )
				{	break;
				} else if ( ret == NTM_WAIT_CANCEL )
				{	goto	ABORT;
				}
				/* time over */
				++retry;
				if ( retry > 5 )
					goto	ABORT;
			}
		} else
		{	/* ROAD 3 */
			NtmSetBpSpeed(ntm,5);	/* 9600 */
			ret = NTM_waitStr( ntm, 30, "*", NULL );
			if ( ret == NTM_WAIT_CANCEL )
				goto	ABORT;
		}
		retry = 0;
		while ( 1 )
		{
			NTM_wait(1);
			NTM_rsPuts(ntm, "C NIF\r");
			NTM_wait(2);
			ret = NTM_waitInstr(ntm,30," Enter Connection-ID  --->", "*", NULL);
			if ( ret == 0 )			/* "Connection-ID" */
			{	break;
			} else if ( ret == 1 )	/* "*"	*/
			{	++retry;
				if ( retry > 30 )
					goto ABORT;
			} else					/* Cancel or timeout	*/
				goto ABORT;
		}
	} else
	{	/* ROAD 1	*/
		NtmSetBpSpeed(ntm,2);	/* 2400 */
		retry = 0;
		while ( 1 )
		{	NTM_wait(1);
			NTM_rsPuts(ntm,".00+");
			ret = NTM_waitInstr(ntm, 30,"Enter Connection-ID  --->",
						  ".1", ".2", ".3", ".4", ".5",
						  ".6", ".7", ".8", ".9",
						  "NO CARRIER", NULL );
			if ( ret == 0 )
			{	/* Connection-ID */
				break;
			} else if ( ret < 10 )
			{	/* '*'	*/
				++retry;
				if ( retry > 30 )
					goto ABORT;
			} else
			{	/* TIME OUT or "NO CARRIER"	*/
				goto ABORT;
			}
		}
	}

	NTM_wait(1);
	NTM_rsPuts(ntm,"SVC\r");

	ret = NTM_waitStr( ntm, 30, " Enter User-ID  --->", NULL );
	if ( ret == NTM_WAIT_CANCEL )
		goto ABORT;
	NTM_wait(1);
	NTM_rsPuts( ntm, id );	NTM_rsPuts(ntm,"\r");

	ret = NTM_waitStr( ntm, 30, " Enter Password --->", NULL);
	if ( ret == NTM_WAIT_CANCEL )
		goto	ABORT;
	NTM_wait(1);
	NTM_sendCenterPassword( ntm, center );	NTM_rsPuts(ntm,"\r");

	/* ログイン時間	*/
	time(&tm);
	NtmCenterSetTime( center, NTM_TIME_LOGIN, tm );
	AplRecWrite("%s:Login successful", RECKEY );
	MOS_pop(mos);

	return (NORMAL);
}


/*************************************************************************
*	ログアウト処理
*************************************************************************/

int		_NIFTY_logout( char *ntm, char *center )
{
	int		ret;
	long	tm;
	char	tmp[256];

	time(&tm);
	NtmCenterSetTime( center, NTM_TIME_LOGOUT, tm );
	AplRecWrite("%s:Logout", RECKEY);
	NTM_wait(1);
	NTM_rsPuts( ntm, "OFF\r");
	if ( _nifty_fenix == 2 || _nifty_fenix == 3 )
	{	/* ROAD 2 */
		ret = NTM_waitStr( ntm, 60, "*", NULL );
		if ( ret == 0 )
		{
			NTM_wait(1);
			NTM_rsPuts( ntm, "OFF\r");
		}
		NTM_waitInstr( ntm, 30, "NO CARRIER", NULL );
	} else
	{
		NTM_waitInstr( ntm, 60, "ご利用誠にありがとうございました。", NULL );
	}
	NTM_offline(ntm,DTR_OFF_TIME);
	AplRecWrite("%s:Disconnect", RECKEY);

	if ( _nif_amac != NULL && _nifty_setid )
	{
		sprintf( tmp,"%s%s", AplGetStartPath(), "nifty_id.tbl" );
		AMAC_save( _nif_amac, tmp, "NIFTY-Serve ID table" );
		_nifty_setid = 0;
	}

#ifdef	_TAROPYON_SYSTEM
	nifty_log_save( ntm );
#endif

	return (NORMAL);
}

/*************************************************************************
*	受信行のチェック
*************************************************************************/

int		_NIFTY_lineCheck( char *ntm, char *buf )
{
	return (NORMAL);
}

/*************************************************************************
*	入力文字列の送信
*************************************************************************/

int		_NIFTY_inputCheck( char *ntm, char *s )
{
	NTM_rsPuts( ntm, s    );
	NTM_rsPuts( ntm, "\r" );
	return (NORMAL);
}

#include	"niffltr.c"

#undef	CENTERKEY
#undef	RECKEY
