/**
 * MobileWonderGate Network Communication Library
 *      copyright 2000-2001, by Naga(nagaw@attglobal.net)
 *
 * $Id$
 *
 * $Log$
 */

#include <sys/bios.h>
#include <stdio.h>
#include <string.h>
#include "mwglog.h"
#include "mwgate.h"


/*-------------------------------------------------------------------*/
/*
 * 接続情報
 */
#define	PPP_USER	"ppp-userID"		/* PPP dialup用のユーザーID */
#define	PPP_PASS	"ppp-passwd"		/* PPP dialup用のパスワード */

#define	DNS_STR1	"0.0.0.0"
#define	DNS_STR2	"0.0.0.0"
struct in_addr	DNS_PRIM = {0};			/* プライマリDNSのアドレス */
struct in_addr	DNS_SECD = {0};			/* セカンダリDNSのアドレス */

#define	TEL_DIAL	"03-1234-5678"		/* ダイアル先電話番号 */

#define	POP_ADDR	"pop.server.com"	/* POP3メールサーバー */
#define	POP_PORT	110					/* POP3ポート番号 */

#define	POP_USER	"pop-userID"		/* POP3メール用のユーザーID */
#define	POP_PASS	"pop-passwd"		/* POP3メール用のパスワード */


/*-------------------------------------------------------------------*/
/*
 * 受信バッファサイズ
 */
char	achBuff[MWG_RECV_BUFF+1];


/*-------------------------------------------------------------------*/
int main(int argc, char *args[])
{
	struct hostent far *pHost;
	struct sockaddr_in sockAddr;
	int nSock = -1;
	int nSize;
	int nCols;

	/* init screen */
	text_set_screen(SCREEN2);
	text_screen_init();

	text_put_string(0, TEXT_SCREEN_HEIGHT-1, "mail access by WonderGate...");

	/* init comm port */
	comm_set_cancel_key(KEY_START);

	mwgate_openLog(-1);
	if ( -1 == mwgate_open(COMM_SPEED_9600) )
	{
		text_put_string(0, TEXT_SCREEN_HEIGHT-1, "ERROR: open WonderGate!     ");
		goto _exit_app;
	}

	/* PDCの接続確認 */
	text_put_string(0, TEXT_SCREEN_HEIGHT-1, "WonderGate: check PDC status");
	if ( PDC_OK != PDC_RETVALUE(mwgate_check_pdc()) )
	{
		text_put_string(0, TEXT_SCREEN_HEIGHT-1, "ERROR: check PDC status!    ");
		goto _exit_app;
	}

	/* PPPの認証情報の設定 */
	text_put_string(0, TEXT_SCREEN_HEIGHT-1, "WonderGate: set PPP authors ");
	if ( MWG_SUCCESS != mwgate_set_ppp(PPP_USER, PPP_PASS) )
	{
		text_put_string(0, TEXT_SCREEN_HEIGHT-1, "ERROR: set PPP auth info!   ");
		goto _exit_app;
	}

	/* DNSサーバーの設定 */
	text_put_string(0, TEXT_SCREEN_HEIGHT-1, "WonderGate: set DNS server  ");
	DNS_PRIM.s_addr = inet_addr(DNS_STR1);
	DNS_SECD.s_addr = inet_addr(DNS_STR2);
	if ( MWG_SUCCESS != mwgate_set_dns(&DNS_PRIM, &DNS_SECD) )
	{
		text_put_string(0, TEXT_SCREEN_HEIGHT-1, "ERROR: set DNS server!      ");
		goto _exit_app;
	}

	/* 電話の接続＆PPPネゴシ */
	text_put_string(0, TEXT_SCREEN_HEIGHT-1, "WonderGate: start dialup PPP");
	if ( DIAL_CONNECT != mwgate_dialup(TEL_DIAL) )
	{
		text_put_string(0, TEXT_SCREEN_HEIGHT-1, "ERROR: dialup&negotiate PPP ");
		goto _exit_app;
	}

	/* IPアドレスの取得 */
	text_put_string(0, TEXT_SCREEN_HEIGHT-1, "WonderGate: get IP address  ");
	pHost = gethostbyname(POP_ADDR);
	if ( NULL == pHost )
	{
		text_put_string(0, TEXT_SCREEN_HEIGHT-1, "ERROR: get IP address!      ");
		goto _exit_app;
	}
	text_put_string(0, 0, "host=");
	text_put_string(5, 0, pHost->h_name);
	text_put_string(0, 1, "ip=");
	text_put_string(3, 1, inet_ntoa(*((unsigned long far *)pHost->h_addr)));

	/* socketの作成 */
	text_put_string(0, TEXT_SCREEN_HEIGHT-1, "WonderGate: get socket num  ");
	nSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
	if ( -1 == nSock )
	{
		text_put_string(0, TEXT_SCREEN_HEIGHT-1, "ERROR: get socket handle!   ");
		goto _exit_app;
	}

	/* socketの接続 */
	text_put_string(0, TEXT_SCREEN_HEIGHT-1, "WonderGate: connect socket  ");
	sockAddr.sin_family      = AF_INET;
	sockAddr.sin_port        = htons(POP_PORT);
	sockAddr.sin_addr.s_addr = *((unsigned long far *)pHost->h_addr);
	if ( 1 != connect(nSock, (struct sockaddr far *)&sockAddr, sizeof(sockAddr)) )
	{
		text_put_string(0, TEXT_SCREEN_HEIGHT-1, "ERROR: connect socket!      ");
		goto _exit_app;
	}

	/* POPコマンドの開始 */
	nSize = recv_line(nSock, achBuff, MWG_RECV_BUFF, 0);
	if ( 0 >= nSize )
	{
		text_put_string(0, TEXT_SCREEN_HEIGHT-1, "ERROR: recieve socket!      ");
		goto _exit_app;
	}
	achBuff[nSize-2] = '\0';
	nCols = 3;
	text_put_string(0, nCols, achBuff);
	nCols += strlen(achBuff) / TEXT_SCREEN_WIDTH;




	text_put_string(0, TEXT_SCREEN_HEIGHT-1, "WonderGate: send POP command");
	nSize = send(nSock, "QUIT\r\n", 6, 0);
	if ( 0 >= nSize )
	{
		text_put_string(0, TEXT_SCREEN_HEIGHT-1, "ERROR: send socket!         ");
		goto _exit_app;
	}

	nSize = recv_line(nSock, achBuff, MWG_RECV_BUFF, 0);
	if ( 0 >= nSize )
	{
		text_put_string(0, TEXT_SCREEN_HEIGHT-1, "ERROR: recieve socket!      ");
		goto _exit_app;
	}
	achBuff[nSize-2] = '\0';
	nCols++;
	text_put_string(0, nCols, achBuff);
	nCols += strlen(achBuff) / TEXT_SCREEN_WIDTH;

	text_put_string(0, TEXT_SCREEN_HEIGHT-1, "SUCCESS: access WonderGate! ");

_exit_app:
	key_wait();

	if ( -1 != nSock )
	{
		soclose(nSock);
	}

	/* close comm port */
	if ( MWGSTAT_PPP == mwgate_status() )
	{
		mwgate_hangup();
	}
	mwgate_close();
	mwgate_closeLog();
}
