/*******************************************************************************
        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.
*******************************************************************************/

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

/*
 *   You must change the following values to whatever
 *   your local IP address and netmask are. Contact your
 *   network administrator for these numbers.
 */
 
#define MY_IP_ADDRESS "10.10.6.105"
#define MY_NETMASK    "255.255.255.0"
#define MY_GATEWAY    "10.10.6.1"
#define MY_NAMESERVER "10.10.6.254"

/*
 * 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.
 */
#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. 
 */
#define POP_PARSE_EXTRA

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

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

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;
}

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 successfull!\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");
}