/**********************************************************************
	smtp2msg.c	3/24/03
   - 	acts as a simple SMTP gateway to a serial-based message board display. It
   	parses two styles of messages.

      (Please see the 802.11b wifi Application Kit Application Note 404 for more
       details)

*******************************************************************************/
#class auto
#memmap xmem

// setting up the SSID, Ad-hoc mode, and channel 3
#define SSID	"RabbitDemo:1234"
#define WIFI_INIT	\
	pd_ioctl(0,WIFI_MODE,"IBSS",0);	\
	pd_ioctl(0,WIFI_SSID,SSID,0);		\
   pd_ioctl(0,WIFI_OWNSSID,SSID,0);	\
   pd_ioctl(0,WIFI_OWNCHAN,"3",0);

#define TCPCONFIG 1

#define PKTDRV "cfprism.lib"
#use "dcrtcp.lib"

#define PORT 25


/********************************
 * End of configuration section *
 ********************************/

///////////////////////////////////////////////////////////////////////

#use BB_SIGN.LIB

char msgbuf[256];
void main()
{
	int bytes_read;
	char	buffer[100]; 	/* Currently (DC 7.30), printf() has max 127 bytes it can output. */
	tcp_Socket socket,*s;

   serBopen(9600);
   BBconfig(1, 128, 0, 0);

	sock_init();

   s =  &socket;
	while(1) {
		tcp_listen(s,PORT,0,0,NULL,0);

		printf("Waiting for connection...\n");
		while(!sock_established(s) && sock_bytesready(s)==-1)
			tcp_tick(NULL);

		printf("Connection received...\n");
      sock_mode(s, TCP_MODE_ASCII );
      sock_puts(s,"220 Trix are for kids !");
      puts(">220 Trix are for kids !\n");
		do {
			bytes_read = sock_gets(s,buffer,sizeof(buffer)-1);
			if(bytes_read>0) {
         	printf("<%s\n",buffer);
         	if(!strncmp(buffer,"EHLO",4)){
            	goto resp_250;
            }
            else if(!strncmp(buffer,"HELO",4)){
               goto resp_250;
            }
            else if(!strncmp(buffer,"RCPT",4)){
               goto resp_250;
            }
            else if(!strncmp(buffer,"MAIL",4)){
               goto resp_250;
            }
            else if(!strncmp(buffer,"RSET",4)){
               goto resp_250;
            }
            else if(!strncmp(buffer,"DATA",4)){
            	sock_puts(s,"354");
               puts(">354\n");
            	do {
               	buffer[0] = '\0';
                  if(sock_gets(s,buffer,sizeof(buffer)-1)>0) {
                  	printf("<<%s\n",buffer);
                     if(!strncmp(buffer,"FN:",3)){
                     	sprintf(msgbuf,"ZWorld Welcomes %s ...", buffer+3);
                     	msg_displ(0,0,msgbuf);
                     }
                     if(!strncmp(buffer,"MSG=",4)){
                     	msg_displ(0,0,buffer+4);
                     }
                  	if(!strcmp(buffer,".")){
                  		break;
                  	}
                  }
               } while(tcp_tick(&socket));
               goto resp_250;
            }
         	else if(!strncmp(buffer,"QUIT",4)){
            	sock_puts(s,"221");
					puts(">221\n");
               sock_close(s);
            }else{
               sock_puts(s,"550");
					puts(">550\n");
            }
            continue;
resp_250:
             sock_puts(s,"250");
             puts(">250\n");
			}

		} while(tcp_tick(&socket));

		printf("Connection closed...\n");
	}
}

/*
	display a message on the display board.
   addr = 0-255
   msgno = 1-32
*/
msg_displ(int addr, int msgno, char *msg )
{
	BBsetText(0,
				msg,
				BB_MODE_ROLLLEFT,
				BB_COLOR_RED);
   BBrunText(1);
   printf("Message -> %s\n",msg);
	printf("msg -> <4>%03d<10>%02d%s<18>\n",addr,msgno,msg);
}