<commit_before>/**
 * (c) 2014 uchicom
 */
package com.uchicom.dirsmtp;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.Writer;
import java.net.InetAddress;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;
import java.util.List;

import com.uchicom.server.Parameter;
import com.uchicom.server.ServerProcess;

/**
 * SMTP処理クラス.
 * 送信処理認証後は、他サーバに直接接続してプロキシ―のような処理をする。
 *
 * @author uchicom: <NAME>
 *
 */
public class SmtpProcess implements ServerProcess {

	private SimpleDateFormat format = new SimpleDateFormat(
			"yyyyMMdd_HHmmss.SSS");
	private Parameter parameter;
	private Socket socket;

	private boolean bHelo;
	private boolean bMailFrom;
	private boolean bRcptTo;
	private boolean bData;
	private boolean bAuth;
	private int authStatus;

	private String senderAddress;
	private String helo;
	private String mailFrom;
	private Mail mail;
	private String authName;

	private List<MailBox> boxList = new ArrayList<MailBox>();

	/** 送付先一覧 */
	private List<MailBox> rcptList = new ArrayList<MailBox>();

	private long startTime = System.currentTimeMillis();

	/**
	 * コンストラクタ.
	 *
	 * @param parameter
	 * @param socket
	 * @throws IOException
	 */
	public SmtpProcess(Parameter parameter, Socket socket) {
		this.parameter = parameter;
		this.socket = socket;
	}

	public void execute() {
		execute(System.out);
	}

	/**
	 * SMTP処理を実行
	 *
	 * @throws IOException
	 */
	public void execute(PrintStream logStream) {
		this.senderAddress = socket.getInetAddress().getHostAddress();
		logStream.println(System.currentTimeMillis() + ":"
				+ String.valueOf(senderAddress));
		BufferedReader br = null;
		PrintStream ps = null;
		Writer writer = null;
		try {
			br = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));
			ps = new PrintStream(socket.getOutputStream());
			SmtpUtil.recieveLine(ps, "220 ", parameter.get("host"), " SMTP");
			String line = br.readLine();
			// HELO
			// MAIL FROM:
			// RCPT TO:
			// DATA
			// MAIL FROM:
			// RCPT TO:
			// DATAの順で処理する
			while (line != null) {
				logStream.println("[" + line + "]");
				if (authStatus > 0 && authStatus < 3) {
					switch (authStatus) {
					case 1:
						String name = new String(Base64.getDecoder().decode(line));
						File dir = new File(parameter.getFile("dir"), name);
						if (dir.exists() && dir.isDirectory()) {
							authStatus = 2;
							authName = name;

							SmtpUtil.recieveLine(ps,
									Constants.RECV_334, " UGFzc3dvcmQ6");
						}
						break;
					case 2:
						String pass = new String(Base64.getDecoder().decode(line));
						File passwordFile = new File(new File(parameter.getFile("dir"), authName), "pass.txt");
						if (passwordFile.exists() && passwordFile.isFile()) {
							try (BufferedReader passReader = new BufferedReader(
									new InputStreamReader(
											new FileInputStream(
													passwordFile)));) {
								String password = <PASSWORD>Reader.readLine();
								while ("".equals(password)) {
									password = <PASSWORD>.readLine();
								}
								if (pass.equals(password)) {

									SmtpUtil.recieveLine(ps,
											Constants.RECV_235);
									bAuth = true;
									authStatus = 3;
								} else {
									// パスワード不一致エラー
									SmtpUtil.recieveLine(ps,
											Constants.RECV_535);
									authStatus = 0;
								}
							}
						}
						break;
						default:
					}
				} else if (bData) {
					if (".".equals(line)) {
						// メッセージ終了
						writer.close();
						// メッセージコピー処理
						try {
							mail.copy(boxList, socket.getLocalAddress().getHostName() ,socket.getInetAddress().getHostName());
							mail.delete();
						} catch (Exception e) {
							e.printStackTrace();
						}
						mail = null;
						rcptList.clear();
						SmtpUtil.recieveLine(ps, Constants.RECV_250_OK);
						init();
					} else {
						// メッセージ本文
						writer.write(line);
						writer.write("\r\n");
					}
				} else if (!bHelo
						&& (SmtpUtil.isEhelo(line) || SmtpUtil.isHelo(line))) {
					bHelo = true;
					String[] lines = line.split(" +");<commit_msg>バグ修正（.が先頭につく場合に除去）<commit_after>/**
 * (c) 2014 uchicom
 */
package com.uchicom.dirsmtp;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.Writer;
import java.net.InetAddress;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;
import java.util.List;

import com.uchicom.server.Parameter;
import com.uchicom.server.ServerProcess;

/**
 * SMTP処理クラス.
 * 送信処理認証後は、他サーバに直接接続してプロキシ―のような処理をする。
 *
 * @author uchicom: <NAME>
 *
 */
public class SmtpProcess implements ServerProcess {

	private SimpleDateFormat format = new SimpleDateFormat(
			"yyyyMMdd_HHmmss.SSS");
	private Parameter parameter;
	private Socket socket;

	private boolean bHelo;
	private boolean bMailFrom;
	private boolean bRcptTo;
	private boolean bData;
	private boolean bAuth;
	private int authStatus;

	private String senderAddress;
	private String helo;
	private String mailFrom;
	private Mail mail;
	private String authName;

	private List<MailBox> boxList = new ArrayList<MailBox>();

	/** 送付先一覧 */
	private List<MailBox> rcptList = new ArrayList<MailBox>();

	private long startTime = System.currentTimeMillis();

	/**
	 * コンストラクタ.
	 *
	 * @param parameter
	 * @param socket
	 * @throws IOException
	 */
	public SmtpProcess(Parameter parameter, Socket socket) {
		this.parameter = parameter;
		this.socket = socket;
	}

	public void execute() {
		execute(System.out);
	}

	/**
	 * SMTP処理を実行
	 *
	 * @throws IOException
	 */
	public void execute(PrintStream logStream) {
		this.senderAddress = socket.getInetAddress().getHostAddress();
		logStream.println(System.currentTimeMillis() + ":"
				+ String.valueOf(senderAddress));
		BufferedReader br = null;
		PrintStream ps = null;
		Writer writer = null;
		try {
			br = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));
			ps = new PrintStream(socket.getOutputStream());
			SmtpUtil.recieveLine(ps, "220 ", parameter.get("host"), " SMTP");
			String line = br.readLine();
			// HELO
			// MAIL FROM:
			// RCPT TO:
			// DATA
			// MAIL FROM:
			// RCPT TO:
			// DATAの順で処理する
			while (line != null) {
				logStream.println("[" + line + "]");
				if (authStatus > 0 && authStatus < 3) {
					switch (authStatus) {
					case 1:
						String name = new String(Base64.getDecoder().decode(line));
						File dir = new File(parameter.getFile("dir"), name);
						if (dir.exists() && dir.isDirectory()) {
							authStatus = 2;
							authName = name;

							SmtpUtil.recieveLine(ps,
									Constants.RECV_334, " UGFzc3dvcmQ6");
						}
						break;
					case 2:
						String pass = new String(Base64.getDecoder().decode(line));
						File passwordFile = new File(new File(parameter.getFile("dir"), authName), "pass.txt");
						if (passwordFile.exists() && passwordFile.isFile()) {
							try (BufferedReader passReader = new BufferedReader(
									new InputStreamReader(
											new FileInputStream(
													passwordFile)));) {
								String password = <PASSWORD>();
								while ("".equals(password)) {
									password = <PASSWORD>();
								}
								if (pass.equals(password)) {

									SmtpUtil.recieveLine(ps,
											Constants.RECV_235);
									bAuth = true;
									authStatus = 3;
								} else {
									// パスワード不一致エラー
									SmtpUtil.recieveLine(ps,
											Constants.RECV_535);
									authStatus = 0;
								}
							}
						}
						break;
						default:
					}
				} else if (bData) {
					if (".".equals(line)) {
						// メッセージ終了
						writer.close();
						// メッセージコピー処理
						try {
							mail.copy(boxList, socket.getLocalAddress().getHostName() ,socket.getInetAddress().getHostName());
							mail.delete();
						} catch (Exception e) {
							e.printStackTrace();
						}
						mail = null;
						rcptList.clear();
						SmtpUtil.recieveLine(ps, Constants.RECV_250_OK);
						init();
					} else if (line.indexOf(0) == '.') {
						writer.write(line.substring(1));
						writer.write("\r\n");
					} else {
						// メッセージ本文
						writer.write(line);
						writer.write("\r\n");
					}
				} else if (!bHelo
						&& (SmtpUtil.isEhelo(line) || SmtpUtil.isHelo(line))) {
					bHelo = true;
					String[] lines = line.split(" +");
{'fraction_non_alphanumeric': 0.13209302325581396, 'fraction_numerical': 0.00813953488372093, 'mean_word_length': 2.810810810810811, 'pattern_counts': {'":': 2, '<': 19, '<?xml version=': 0, '>': 19, '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': '2007328', 'n_tokens_mistral': 3816, 'n_tokens_neox': 2896, 'n_words': 595}