/*******************************************************************************
        Samples\tcpip\POP3\parse_extra.c
        Z-World, 2000

        A simple program that will connect to a POP3 server and download
        e-mail form it, optionally deleting the messages after they have
        been read. This version will parse the incoming mail more, to provide
        an easier to use format.
*******************************************************************************/

#class auto


/***********************************
 * Configuration                   *
 * -------------                   *
 * All fields in this section must *
 * be altered to match your local  *
 * network settings.               *
 ***********************************/

/*
 * Pick the predefined TCP/IP configuration for this sample.  See
 * LIB\TCPIP\TCP_CONFIG.LIB for instructions on how to set the
 * configuration.
 */
#define TCPCONFIG 1

/*
 * POP3 settings
 */

/*
 *	Enter the name of your POP3 server here.
 */
#define POP_HOST	"mail.domain.com"

/*
 * This is the username and password for the account on the
 * pop3 server.  These names must not change until complete.
 */
#define POP_USER	"myname"
#define POP_PASS	"secret"

/* Uncomment this to show extra debug output */
//#define POP_DEBUG

/* comment this out to delete the messages off the server after they are read */
#define POP_NODELETE

/*
 * When this is defined, the POP3 library will do extra parsing of the
 * incoming e-mails, seperating the 'to:', 'from:', 'subject:' and body
 * fields from the rest of the header, and provide this data in a nicer
 * manner.
 * NOTE: Changes the parameters passed to storemsg() .
 */
#define POP_PARSE_EXTRA

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

#memmap xmem
#use "dcrtcp.lib"
#use "pop3.lib"

/*
 * 	This is the POP_PARSE_EXTRA calling style.
 */
int n;
int storemsg(int num, char *to, char *from, char *subject, char *body, int len)
{
#GLOBAL_INIT
	{
		n = -1;
	}

	if(n != num) {
		n = num;
		printf("RECEIVING MESSAGE <%d>\n", n);		
		printf("\tFrom: %s\n", from);
		printf("\tTo: %s\n", to);
		printf("\tSubject: %s\n", subject);
	}
	
	printf("MSG_DATA> '%s'\n", body);

	return 0;
}

void main()
{
	static long address;
	static int retval;
	
	sock_init();

	pop3_init(storemsg);

	printf("Resolving name...\n");
	address = resolve(POP_HOST);
	printf("Calling pop3_getmail()...\n");
	pop3_getmail(POP_USER, POP_PASS, address);

	printf("Entering pop3_tick()...\n");
	while((retval = pop3_tick()) == POP_PENDING)
		continue;

	if(retval == POP_SUCCESS)
		printf("POP was successful!\n");
	if(retval == POP_TIME)
		printf("POP timed out!\n");
	if(retval == POP_ERROR)
		printf("POP returned a general error!\n");
		
	printf("All done!\n");
}
