<commit_before>package org.esupportail.smsuapimail.services.messageRetriever.pop;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;

import org.esupportail.commons.services.logging.Logger;
import org.esupportail.commons.services.logging.LoggerImpl;
import org.esupportail.smsuapimail.domain.beans.SmsMessage;
import org.esupportail.smsuapimail.exceptions.MessageRetrieverConnectorException;
import org.esupportail.smsuapimail.services.messageRetriever.IMessageRetrieverConnector;

/**
 * This class is used to get mail from Pop server.
 * @author prqd8824
 *
 */
public class PopMessageRetrieverConnector implements IMessageRetrieverConnector {

	
	/**
	 * A logger.
	 */
	private final Logger logger = new LoggerImpl(getClass());
	
	/**
	 * The store type on the server.
	 */
	private static final String POP_STORE_TYPE = "pop3";
	
	/**
	 * The content type plain text.
	 */
	private static final String PLAIN_TEXT_CONTENT_TYPE = "text/plain";
	
	/**
	 * Tool used to parse mail body.
	 */
	private MessageBodyToMailToSmsMessageConverter messageBodyToMailToSmsMessageConverter;

	/**
	 * Pop server address.
	 */
	private String popServerAdress;
	
	/**
	 * Login used to log on the pop server.
	 */
	private String popServerLogin;
	
	/**
	 * Password used to log on the pop server.
	 */
	private String popServerPassword;
	
	/**
	 * Name of the folder containing mail.
	 */
	private String popFolderName;
	
	/**
	 * Charset supported in email.
	 */
	private String mailCharset;
	

	/**
	 * Get the message on the pop server.
	 * @return messageList
	 * @throws MessageRetrieverConnectorException
	 */
	public List<SmsMessage> getMessages() throws MessageRetrieverConnectorException {
		List<SmsMessage> messageList = getEmailFromPopServer();

		return messageList;
	}

	private Store getStore() throws MessageRetrieverConnectorException {
		try {
			final Properties props = System.getProperties();
			Session session = Session.getDefaultInstance(props, null);
			return session.getStore(POP_STORE_TYPE);
		} catch (NoSuchProviderException e) {
			String s = "unable to get message (due to NoSuchProviderException) from server : " + popServerAdress;
			logger.error(s, e);
			throw new MessageRetrieverConnectorException(s, e);
		}
	}
	
	/**
	 * 
	 * @return a list of messages from the pop server
	 * @throws MessageRetrieverConnectorException
	 */
	private List<SmsMessage> getEmailFromPopServer() throws MessageRetrieverConnectorException {
		final List<SmsMessage> messageToProcessList = new LinkedList<SmsMessage>();
		
		Store store = null;
		Folder folder = null;

		try {
			
			// Connect to the pop server
			if (logger.isDebugEnabled()) {
				logger.debug("Connecting to pop serveur : \n" + 
					     " - server address : " + popServerAdress + "\n" +
					     " - server login : " + popServerLogin + "\n" + 
					     " - server pass : " + "<PASSWORD>" + "\n");
			}

			store = getStore();
			store.connect(popServerAdress, popServerLogin, popServerPassword);

			// Go to the default folder
			Folder defaultFolder = store.getDefaultFolder();
			if (defaultFolder == null) {
				String s = "unable to get default folder on server : " + popServerAdress;
				logger.error(s);
				throw new MessageRetrieverConnectorException(s);
			}
			
			// Go to the folder containing email
			folder = defaultFolder.getFolder(popFolderName);
			if (folder == null) {
				String s = "unable to get folder : " + popFolderName + " on server : " + popServerAdress;
				logger.error(s);
				throw new MessageRetrieverConnectorException(s);
			}
			// Open the folder for read and write (write right must be present to allow delete)
			folder.open(Folder.READ_WRITE);
			
			// Get the message wrappers and process them
			final Message[] msgs = folder.getMessages();
			
			for (int msgNum = 0; msgNum < msgs.length; msgNum++) {
				final Message messageTmp = msgs[msgNum];
				
				// this try catch block is here to allow best effort managment.
				// An error on a single email does not perform a global error
				try {

					// only process message not marked as deleted
					if (!messageTmp.isSet(Flags.Flag.DELETED)) {<commit_msg>code cleanup: get rid of for(... ; ...; ...)<commit_after>package org.esupportail.smsuapimail.services.messageRetriever.pop;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;

import org.esupportail.commons.services.logging.Logger;
import org.esupportail.commons.services.logging.LoggerImpl;
import org.esupportail.smsuapimail.domain.beans.SmsMessage;
import org.esupportail.smsuapimail.exceptions.MessageRetrieverConnectorException;
import org.esupportail.smsuapimail.services.messageRetriever.IMessageRetrieverConnector;

/**
 * This class is used to get mail from Pop server.
 * @author prqd8824
 *
 */
public class PopMessageRetrieverConnector implements IMessageRetrieverConnector {

	
	/**
	 * A logger.
	 */
	private final Logger logger = new LoggerImpl(getClass());
	
	/**
	 * The store type on the server.
	 */
	private static final String POP_STORE_TYPE = "pop3";
	
	/**
	 * The content type plain text.
	 */
	private static final String PLAIN_TEXT_CONTENT_TYPE = "text/plain";
	
	/**
	 * Tool used to parse mail body.
	 */
	private MessageBodyToMailToSmsMessageConverter messageBodyToMailToSmsMessageConverter;

	/**
	 * Pop server address.
	 */
	private String popServerAdress;
	
	/**
	 * Login used to log on the pop server.
	 */
	private String popServerLogin;
	
	/**
	 * Password used to log on the pop server.
	 */
	private String popServerPassword;
	
	/**
	 * Name of the folder containing mail.
	 */
	private String popFolderName;
	
	/**
	 * Charset supported in email.
	 */
	private String mailCharset;
	

	/**
	 * Get the message on the pop server.
	 * @return messageList
	 * @throws MessageRetrieverConnectorException
	 */
	public List<SmsMessage> getMessages() throws MessageRetrieverConnectorException {
		List<SmsMessage> messageList = getEmailFromPopServer();

		return messageList;
	}

	private Store getStore() throws MessageRetrieverConnectorException {
		try {
			final Properties props = System.getProperties();
			Session session = Session.getDefaultInstance(props, null);
			return session.getStore(POP_STORE_TYPE);
		} catch (NoSuchProviderException e) {
			String s = "unable to get message (due to NoSuchProviderException) from server : " + popServerAdress;
			logger.error(s, e);
			throw new MessageRetrieverConnectorException(s, e);
		}
	}
	
	/**
	 * 
	 * @return a list of messages from the pop server
	 * @throws MessageRetrieverConnectorException
	 */
	private List<SmsMessage> getEmailFromPopServer() throws MessageRetrieverConnectorException {
		final List<SmsMessage> messageToProcessList = new LinkedList<SmsMessage>();
		
		Store store = null;
		Folder folder = null;

		try {
			
			// Connect to the pop server
			if (logger.isDebugEnabled()) {
				logger.debug("Connecting to pop serveur : \n" + 
					     " - server address : " + popServerAdress + "\n" +
					     " - server login : " + popServerLogin + "\n" + 
					     " - server pass : " + "<PASSWORD>" + "\n");
			}

			store = getStore();
			store.connect(popServerAdress, popServerLogin, popServerPassword);

			// Go to the default folder
			Folder defaultFolder = store.getDefaultFolder();
			if (defaultFolder == null) {
				String s = "unable to get default folder on server : " + popServerAdress;
				logger.error(s);
				throw new MessageRetrieverConnectorException(s);
			}
			
			// Go to the folder containing email
			folder = defaultFolder.getFolder(popFolderName);
			if (folder == null) {
				String s = "unable to get folder : " + popFolderName + " on server : " + popServerAdress;
				logger.error(s);
				throw new MessageRetrieverConnectorException(s);
			}
			// Open the folder for read and write (write right must be present to allow delete)
			folder.open(Folder.READ_WRITE);
			
			// Get the message wrappers and process them
			for (Message messageTmp : folder.getMessages()) {			
				// this try catch block is here to allow best effort managment.
				// An error on a single email does not perform a global error
				try {

					// only process message not marked as deleted
					if (!messageTmp.isSet(Flags.Flag.DELETED)) {
{'fraction_non_alphanumeric': 0.09790674493299369, 'fraction_numerical': 0.0012182965998449441, 'mean_word_length': 3.7228033472803346, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '684952', 'n_tokens_mistral': 2968, 'n_tokens_neox': 2635, 'n_words': 757}