diff --git a/build.gradle b/build.gradle index f1d4183..ab939ec 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ apply plugin: 'application' mainClassName = 'de.schwarz.emailparser.ParserMain' -version = '0.3.0' +version = '0.4.0' repositories { diff --git a/src/main/java/de/schwarz/emailparser/Parser.java b/src/main/java/de/schwarz/emailparser/Parser.java index 5032597..aeba2fe 100644 --- a/src/main/java/de/schwarz/emailparser/Parser.java +++ b/src/main/java/de/schwarz/emailparser/Parser.java @@ -32,15 +32,44 @@ public List readLines(Scanner input) { List emailAddresses = new ArrayList(); + String address = ""; // to save email address till we get smtp status code + String statusCode = ""; + while(input.hasNextLine()) { String nextLine = input.nextLine(); if(nextLine.contains("Final-Recipient")) { // line where to find E-Mail address - String address = extractEmailAddress(nextLine); + address = extractEmailAddress(nextLine); - emailAddresses.add(address); - System.out.println(address); + System.out.println("address to delete: " + address); } + // only after we read diagnostic code + // and only on status codes: 550, + // we harves email address to later delete + else if(nextLine.contains("Diagnostic-Code")) { + // 550 - mailbox unavailable + // 511 - user unknown + // 520 - user unknown + // 553 - no such user + // 554 - no account with this address + // 552 - exceeded storage ==> don't delete for now + if(nextLine.contains("550") || + nextLine.contains("511") || + nextLine.contains("520") || + nextLine.contains("553") || + nextLine.contains("554") || + nextLine.contains("Host or domain name not found")) { + // System.out.println("STATUS CODE: 550 or domain name not found"); + emailAddresses.add(address); + } + else { + // System.out.println("STATUS CODE: " + nextLine); + } + } + } + + + input.close(); return emailAddresses; diff --git a/src/main/java/de/schwarz/emailparser/ParserMain.java b/src/main/java/de/schwarz/emailparser/ParserMain.java index 154b1a1..f3ae03f 100644 --- a/src/main/java/de/schwarz/emailparser/ParserMain.java +++ b/src/main/java/de/schwarz/emailparser/ParserMain.java @@ -33,24 +33,28 @@ public static void main(String[] args) { if(args.length == 1) { parser.readLines(input); } - // emails file AND DATABASE name passed in parameters - else if(args.length == 2) { + // emails file AND DATABASE name + table passed in parameters + else if(args.length == 4) { String databaseName = args[1]; + String tableName = args[2]; + String attributeName = args[3]; List emailAddressesToDelete = new ArrayList(); emailAddressesToDelete = parser.readLines(input); - deleteEmailAddressesFromDB(databaseName, emailAddressesToDelete); + deleteEmailAddressesFromDB(databaseName, tableName, attributeName, emailAddressesToDelete); } // DATABASE name AND port passed in parameters - else if(args.length == 3) { + else if(args.length == 5) { String databaseName = args[1]; - String port = args[2]; + String tableName = args[2]; + String attributeName = args[3]; + String port = args[4]; List emailAddressesToDelete = new ArrayList(); emailAddressesToDelete = parser.readLines(input); - deleteEmailAddressesFromDB(databaseName, emailAddressesToDelete, port); + deleteEmailAddressesFromDB(databaseName, tableName, attributeName, emailAddressesToDelete, port); } } @@ -58,13 +62,13 @@ else if(args.length == 3) { } - private static void deleteEmailAddressesFromDB(String databaseName, List emailAddressesToDelete) { + private static void deleteEmailAddressesFromDB(String databaseName, String tableName, String attributeName, List emailAddressesToDelete) { // use standard port 3306 (standard port on MacOS: 3307) - deleteEmailAddressesFromDB(databaseName, emailAddressesToDelete, "3306"); + deleteEmailAddressesFromDB(databaseName, tableName, attributeName, emailAddressesToDelete, "3306"); } - private static void deleteEmailAddressesFromDB(String databaseName, List emailAddressesToDelete, String port) { + private static void deleteEmailAddressesFromDB(String databaseName, String tableName, String attributeName, List emailAddressesToDelete, String port) { Connection connection = DBConnection.getConnection(databaseName, port); if(connection == null) { @@ -77,7 +81,7 @@ private static void deleteEmailAddressesFromDB(String databaseName, List try { //stmt = connection.createStatement(); for(String address : emailAddressesToDelete) { - stmt = connection.prepareStatement("DELETE FROM lid_subscribers WHERE nl_email IN (?)"); + stmt = connection.prepareStatement("DELETE FROM " + tableName +" WHERE " + attributeName + " IN (?)"); stmt.setString(1, address); int updateCount = stmt.executeUpdate();