/*
 *	Copyright (C) 1991	TGV, Incorporated
 *
 *	Check for new POP3 mail and integrate it into the mail file
 *
 */

#include <stdio.h>
#include "gtjfn.h"
#include "jfns.h"
#include <ctype.h>
#include "multinet_root:[multinet.include.sys]types.h"
#include "vax-mm.h"
#include <stat.h>

extern	int net_chan;	/* socket to POP server */
extern char *Topsify_Filename();

CMD_POP3_Check_Mail ()
{
	/*
	 *	Confirm
	 */
	Confirm();
	POP3_Check_Mail();
}
	
POP3_Check_Mail ()
{

	int Status = -1;
	int fd, msg_cnt;
	char *POP3_Connect_To_Server(), *pop3_buf;
	register char *cp,*cp1;
	register int i,j;
	register struct Msg *Msg;
	int Number_Of_New_Messages;
	int Size;
	int New_Mail_Read_In;
	struct stat s;
	char Local1[256], Local2[256], Local3[256];
	char *Convert_Mail();

	if ( !POP3_Server_Host[0] ) {
		printf("Please use 'SET POP3-SERVER-HOST' to specify the POP3 server host.\nThen reissue POP3-CHECK command.\n");
		return;
	}

	/*
	 *	Make sure we are up to date
	 */
	Init_Mail_Check_Time(0); Check_For_New_Mail(0);

	/*
	 *	Deal with interlocks on the user's mail file
	 */
	Super_Lock();

	/*
	 *	Don't allow Control-C right now
	 */
	Ignore_Control_C_Flag = 1;

	/*
	 *	 Go talk to the server
	 */
	pop3_buf = POP3_Connect_To_Server(&msg_cnt, POP3_Server_Host);
	if ( msg_cnt < 0 )
		goto done;         /* Error making connection */
	if ( msg_cnt == 0 || pop3_buf == NULL )
		goto no_msgs;

	/*
	 *	Check to see if we are in read-only mode
	 */
	if (Read_Only) {
		printf("?In read-only mode; POP3 mail not incorporated\n");
		goto no_msgs;
	}

	/*
	 *	Check to see if we are in our home mail file (MAIL.TXT)
	 */
	strcpy(Local1, Topsify_Filename(Current_Mail_File));
	strcpy(Local2, Topsify_Filename(Home_File(MM_MAIL_FILE)));
	if (strcmp(Local1, Local2) != 0) {
		struct tbluk_keyword *t;
		static struct {
			int current_entries;
			int maximum_entries;
			struct tbluk_keyword keywords[3];
			} Table = {
			2,
			2,
			{{0,	"no",	0},
			 {0,	"yes",	1}}};
		static struct comnd_function Responses =
			{COMND_KEYWORD,
			 COMND_HELP_VALID|COMND_SUPPRESS_DEFAULT_HELP,
			 0,
			 (int)&Table,
			 "YES or NO"};
		/*
		 *	Make the user confirm that a new POP mail should
		 *	be incorporated into this file if not home mail file
		 */
Again:	
		Command_State.prompt =
		 "Not in SYS$LOGIN:MAIL.TXT, incorporate POP3 mail anyway? (YES/NO) ";
		while(1) {
			COMND_INIT(&Command_State);
			if (COMND(&Command_State,&Responses,&t,0) < 0)
				goto Again;
			Confirm();
			/*
			 *	Dispatch on keyword
			 */
			switch(t->user_data) {
				case 0: printf("?Not in SYS$LOGIN:MAIL.TXT; POP3 mail not incorporated\n");
					goto no_msgs;
				case 1: break;
				default: goto Again;
			}
			break;
		}
	}

	Size = POP3_Retrieve_Msgs(msg_cnt, pop3_buf);
	New_Extension(Current_Mail_File,Local1,"mm1");
	if ( Lock_MM_Mail_File() < 0 ) {
		printf("Couldn't lock %s in order to append new mail.\n",Local1);
		goto no_msgs;
	}
	if ( Size == 0 || pop3_buf == NULL ) {
		UnLock_MM_Mail_File(); 
		goto no_msgs;
	}

	/*
	 *	Append to the Message Text file
	 */
	fd = open(Local1,2);
	if ( fd < 0 ) {
		printf("POP3_Check_Mail: GROSS ERROR\n");
		goto no_msgs;
	}
	lseek(fd,0,2);
	if ( write(fd,pop3_buf, Size) != Size ) {
		printf("Error appending to mail file\n");
		close(fd);
		UnLock_MM_Mail_File();
		goto no_msgs;
	}
	close(fd);

	/*
	 *	The new messages have been written to the MM mail file,
	 *	Tell the server to DELEte them now.
	 */
	if ( !POP3_No_Delete ) 
		for (i=1; i <= msg_cnt; i++)
			send_command_w("DELE %d", i);

	/*
	 *	Quit the Server
	 */
	send_command("QUIT");
	socket_close(net_chan);

	UnLock_MM_Mail_File();
	Append_In_Core(pop3_buf, Size, 1);

	/*
	 *	Get the NEW eof position
	 */
	stat(Current_Mail_File,&s);
	Last_Message_File_EOF = s.st_size;

	/*
	 *	Free buffer
	 */
	mm_free(pop3_buf);

	/*
	 *	Print new mail headers and summary, then update the header file
	 */
	Recent(1);
	Summary();
	printf(" Currently at message %d\n",Current_Message);
	Update_Header_File(0);
	goto done;

no_msgs:
	send_command("QUIT");
	socket_close(net_chan);

	/*
	 *	Release interlocks on user's mail file
	 */
done:
	Ignore_Control_C_Flag = 0;
	Super_Unlock();
}
