diff --git a/pom.xml b/pom.xml index 7d70082..9d74f41 100644 --- a/pom.xml +++ b/pom.xml @@ -111,6 +111,23 @@ 3.1.0 + + org.simplejavamail + simple-java-mail + 5.3.0 + + + + org.slf4j + slf4j-log4j12 + + + log4j + log4j + + + + com.github.messenger4j messenger4j diff --git a/src/main/java/fr/charlotte/arsreloaded/AutoSender.java b/src/main/java/fr/charlotte/arsreloaded/AutoSender.java index 4b9c1d9..73fef07 100644 --- a/src/main/java/fr/charlotte/arsreloaded/AutoSender.java +++ b/src/main/java/fr/charlotte/arsreloaded/AutoSender.java @@ -36,7 +36,6 @@ public void run() { } catch (InterruptedException e) { e.printStackTrace(); } - AutomaticReportServer.sendMessage(AutomaticReportServer.ADMIN_ID, "I'm still here"); System.out.println("Launching Automatic Launching Verification"); if (isTimeToSent()) { System.out.println("Time is now :D !"); diff --git a/src/main/java/fr/charlotte/arsreloaded/AutomaticReportServer.java b/src/main/java/fr/charlotte/arsreloaded/AutomaticReportServer.java index 914f30d..0cfc07c 100644 --- a/src/main/java/fr/charlotte/arsreloaded/AutomaticReportServer.java +++ b/src/main/java/fr/charlotte/arsreloaded/AutomaticReportServer.java @@ -18,6 +18,8 @@ import org.apache.commons.lang3.StringUtils; import org.pf4j.JarPluginManager; import org.pf4j.PluginManager; +import org.simplejavamail.email.EmailBuilder; +import org.simplejavamail.mailer.Mailer; import java.io.File; import java.sql.SQLException; @@ -44,9 +46,11 @@ public class AutomaticReportServer { public static String ARS_VERSION = "v2.0"; public static String ADMIN_ID = ""; + public static String ADMIN_MAIL = ""; private static String TOKEN = ""; private static String ACCESS_TOKEN = ""; private static String SECRET = ""; + private static String ARSMAIL = ""; public static HashMap processingHashMap = new HashMap<>(); public static HashMap processing = new HashMap<>(); @@ -56,6 +60,8 @@ public class AutomaticReportServer { public static SimpleDateFormat DATE_M = new SimpleDateFormat("MM"); public static SimpleDateFormat DATE_Y = new SimpleDateFormat("YYYY"); + private static Mailer mailer; + private final static Gson GSON = new Gson(); @@ -84,14 +90,18 @@ private static void loadConfig() { SECRET = config.getSecretKey(); ADMIN_ID = config.getAdminID(); TOKEN = config.getVerifyToken(); + ARSMAIL = config.getMailUser(); + ADMIN_MAIL = config.getAdminMail(); arsDatabase = config.setupMainDatabase(); userDatabase = config.setupUserDatabase(); wrapper = new DatabaseWrapper(arsDatabase); wrapperD = new DatabaseUserWrapper(userDatabase); + mailer = config.buildMailer(); } + private static void loadPlugins() throws SQLException { processingHashMap.clear(); File file = new File("plugins/"); @@ -173,7 +183,7 @@ private static void setupRootRoute() { if (request.queryParams().isEmpty()) return "Invalid"; String sentToken = request.queryParams("hub.verify_token"); - if(sentToken == null) + if (sentToken == null) return "Invalid"; if (sentToken.equalsIgnoreCase(TOKEN)) return request.queryParams("hub.challenge"); @@ -294,6 +304,10 @@ private static void setupRoutes() { get("/hello", (request, response) -> "Hello World"); } + public static void sendCompletedMail(String subject, String message, String recipientName, String recipientAdress) { + mailer.sendMail(EmailBuilder.startingBlank().from("ARS Mail Sender", ARSMAIL).to(recipientName, recipientAdress).withSubject(subject).withPlainText(message).buildEmail()); + } + /** * Simple method to parse commands sent by messenger to the server diff --git a/src/main/java/fr/charlotte/arsreloaded/commands/AboutCommand.java b/src/main/java/fr/charlotte/arsreloaded/commands/AboutCommand.java index 49f824a..df29550 100644 --- a/src/main/java/fr/charlotte/arsreloaded/commands/AboutCommand.java +++ b/src/main/java/fr/charlotte/arsreloaded/commands/AboutCommand.java @@ -16,18 +16,6 @@ public AboutCommand() { } - public static void main(String[] args) throws IOException { - System.getenv().keySet().stream().filter(s -> s.contains("EPSILON_VERSION")).forEach(System.out::println); - - for(String s : System.getenv().keySet()){ - if(s.contains("EPSILON_VERSION")){ - System.out.println(s); - } - } - - } - - @Override public void onCommand(String senderID, String text, String[] args) { diff --git a/src/main/java/fr/charlotte/arsreloaded/commands/PingCommand.java b/src/main/java/fr/charlotte/arsreloaded/commands/PingCommand.java index b97d34f..7d3e026 100644 --- a/src/main/java/fr/charlotte/arsreloaded/commands/PingCommand.java +++ b/src/main/java/fr/charlotte/arsreloaded/commands/PingCommand.java @@ -14,7 +14,7 @@ public PingCommand() { @Override public void onCommand(String senderID, String text, String[] args) { System.out.println("Ping Command received from " + senderID); - AutomaticReportServer.sendMessage(senderID, "Pong !!, Version " + AutomaticReportServer.ARS_VERSION + ", Developed by LCDR Charlotte THOMAS"); + AutomaticReportServer.sendMessage(senderID, "Pong !!, Version " + AutomaticReportServer.ARS_VERSION + ", Developed by CMDR Charlotte THOMAS"); } @Override diff --git a/src/main/java/fr/charlotte/arsreloaded/commands/SubscribeCommand.java b/src/main/java/fr/charlotte/arsreloaded/commands/SubscribeCommand.java index 38b27b1..7fd22d7 100644 --- a/src/main/java/fr/charlotte/arsreloaded/commands/SubscribeCommand.java +++ b/src/main/java/fr/charlotte/arsreloaded/commands/SubscribeCommand.java @@ -17,21 +17,24 @@ public SubscribeCommand() { @Override public void onCommand(String senderID, String text, String[] args) { - if (args.length < 2) { + if (args.length < 4) { sendMessage(senderID, "Usage : !subscribe" + this.args()); return; } String scc; String name; String region; - if (args.length == 3) { + String mail; + if (args.length == 4) { scc = args[0]; - name = args[2]; + name = args[3]; region = args[1]; + mail = args[2]; } else { scc = args[0]; region = args[1]; - String[] names = Arrays.copyOfRange(args, 2, args.length); + mail = args[2]; + String[] names = Arrays.copyOfRange(args, 3, args.length); name = StringUtils.join(names, "_"); } int re; @@ -46,10 +49,10 @@ public void onCommand(String senderID, String text, String[] args) { return; } try { - boolean result = getWrapper().addWaiting(senderID, name, scc, re); + boolean result = getWrapper().addWaiting(senderID, name, scc, re, mail); if (result) { sendMessage(senderID, "Your vessel is successfully registered in the database !"); - sendMessage(ADMIN_ID, "You have a new pending registry from " + scc + " for the vessel " + name); + sendCompletedMail("New Pending Vessel in ARS", "You have a new pending vessel in ARS!", "Administrator", ADMIN_MAIL); } else { sendMessage(senderID, "Your vessel already exist in the database !"); } @@ -66,6 +69,6 @@ public String usage() { @Override public String args() { - return " [scc] [region] [vesselname]"; + return " [scc] [region] [mail] [vesselname]"; } } diff --git a/src/main/java/fr/charlotte/arsreloaded/commands/WaitingCommand.java b/src/main/java/fr/charlotte/arsreloaded/commands/WaitingCommand.java index 16ec18c..4066856 100644 --- a/src/main/java/fr/charlotte/arsreloaded/commands/WaitingCommand.java +++ b/src/main/java/fr/charlotte/arsreloaded/commands/WaitingCommand.java @@ -48,15 +48,16 @@ private void deny(String senderID, String[] args) { sendMessage(senderID, "An error occured"); return; } - boolean success = false; + String success = "false"; try { success = getWrapper().deletePending(vesselID); } catch (SQLException e) { e.printStackTrace(); } - if (success) { + if (!(success.equalsIgnoreCase("false"))) { sendMessage(senderID, "You denied the subscribe of " + coID + " CO of the " + vesselID); sendMessage(coID, "Your pending subscribe was denied by the administrator :("); + sendCompletedMail("ARS", "We're sorry your pending subscribe has been denied by the administrator, contact them via email", "CO", success); } } @@ -66,18 +67,20 @@ private void accept(String senderID, String[] args) { try { coID = getWrapper().getPendingCoId(vesselID); } catch (SQLException | VesselNotFoundException e) { + e.printStackTrace(); sendMessage(senderID, "An error occured"); return; } - boolean success = false; + String success = ""; try { success = getWrapper().switchPending(vesselID); } catch (SQLException e) { e.printStackTrace(); } - if (success) { + if (!(success.equalsIgnoreCase("false"))) { sendMessage(senderID, "You accepted the subscribe of " + coID + " CO of the " + vesselID); sendMessage(coID, "Your pending subscribe was accepted by the administrator !"); + sendCompletedMail("ARS", "Your pending subscribe was accepted by the administrator !", "CO", success); } } diff --git a/src/main/java/fr/charlotte/arsreloaded/configuration/Config.java b/src/main/java/fr/charlotte/arsreloaded/configuration/Config.java index da3e519..d5d89bc 100644 --- a/src/main/java/fr/charlotte/arsreloaded/configuration/Config.java +++ b/src/main/java/fr/charlotte/arsreloaded/configuration/Config.java @@ -3,6 +3,9 @@ import com.google.gson.Gson; import fr.charlotte.arsreloaded.databases.Database; import org.apache.commons.lang3.StringUtils; +import org.simplejavamail.mailer.Mailer; +import org.simplejavamail.mailer.MailerBuilder; +import org.simplejavamail.mailer.config.TransportStrategy; import java.io.BufferedReader; import java.io.InputStream; @@ -20,9 +23,14 @@ public class Config { private String dbUser; private String dbUserName; private String dbPassword; + private String smtpHost; + private String mailUser; + private String mailPassword; + private String adminMail; + private int mailPort; - public Config(String accessToken, String adminID, String secretKey, String dbHost, String dbName, String dbUser, String dbUserName, String dbPassword, String verifyToken) { + public Config(String accessToken, String adminID, String secretKey, String dbHost, String dbName, String dbUser, String dbUserName, String dbPassword, String verifyToken, String smtpHost, String mailUser, String mailPassword, int mailPort, String adminMail) { this.accessToken = accessToken; this.adminID = adminID; this.secretKey = secretKey; @@ -32,6 +40,15 @@ public Config(String accessToken, String adminID, String secretKey, String dbHos this.dbUserName = dbUserName; this.dbPassword = dbPassword; this.verifyToken = verifyToken; + this.mailPassword = mailPassword; + this.mailUser = mailUser; + this.mailPort = mailPort; + this.smtpHost = smtpHost; + this.adminMail = adminMail; + } + + public String getAdminMail() { + return adminMail; } public Database setupMainDatabase() { @@ -54,6 +71,14 @@ public String getSecretKey() { return secretKey; } + public Mailer buildMailer(){ + return MailerBuilder.withSMTPServer(smtpHost, mailPort, mailUser, mailPassword).withTransportStrategy(TransportStrategy.SMTP_TLS).buildMailer(); + } + + public String getMailUser() { + return mailUser; + } + public String getVerifyToken() { return verifyToken; } diff --git a/src/main/java/fr/charlotte/arsreloaded/databases/DatabaseWrapper.java b/src/main/java/fr/charlotte/arsreloaded/databases/DatabaseWrapper.java index b131bf0..69d80c8 100644 --- a/src/main/java/fr/charlotte/arsreloaded/databases/DatabaseWrapper.java +++ b/src/main/java/fr/charlotte/arsreloaded/databases/DatabaseWrapper.java @@ -79,11 +79,11 @@ private boolean coExist(String senderID) throws SQLException { * @param scc The SCC of the CO * @return if the register is successful */ - public boolean addWaiting(String senderID, String vessel, String scc, int region) throws SQLException { + public boolean addWaiting(String senderID, String vessel, String scc, int region, String mail) throws SQLException { if (coExist(senderID)) { return false; } - arsDatabase.update(String.format("INSERT INTO `waiting`(coid,vesselid,coscc, vesselname, date, region) VALUES('%s','%s','%s', '%s', '%s', %s)", senderID, vesselNameToID(vessel), scc, vessel, System.currentTimeMillis(), region)); + arsDatabase.update(String.format("INSERT INTO `waiting`(coid,vesselid,coscc, vesselname, date, region, email) VALUES('%s','%s','%s', '%s', '%s', %s, '%s')", senderID, vesselNameToID(vessel), scc, vessel, System.currentTimeMillis(), region, mail)); arsDatabase.closeConnection(); return true; } @@ -98,8 +98,9 @@ public String getPendingCoId(String vesselID) throws SQLException, VesselNotFoun ResultSet rs = arsDatabase.getResult("SELECT * FROM `waiting` WHERE vesselid = '" + vesselID + "'"); if (!rs.next()) throw new VesselNotFoundException(); + String r = rs.getString("coid"); arsDatabase.closeConnection(); - return rs.getString("coid"); + return r; } /** @@ -108,15 +109,16 @@ public String getPendingCoId(String vesselID) throws SQLException, VesselNotFoun * @param vesselID The ID of the Vessel * @return if the switch is successful */ - public boolean switchPending(String vesselID) throws SQLException { + public String switchPending(String vesselID) throws SQLException { ResultSet rs = arsDatabase.getResult("SELECT * FROM `waiting` WHERE vesselid = '" + vesselID + "'"); if (!rs.next()) { - return false; + return "false"; } String coID = rs.getString("coid"); String vesselName = rs.getString("vesselname"); String date = rs.getString("date"); int region = rs.getInt("region"); + String mail = rs.getString("email"); if (AutomaticReportServer.vesselByIdCache == null) getVesselByRegions(); @@ -125,13 +127,13 @@ public boolean switchPending(String vesselID) throws SQLException { AutomaticReportServer.vesselByIdCache.remove(region); AutomaticReportServer.vesselByIdCache.put(region, regionSize); - AutomaticReportServer.vesselsCache.add(new Vessel(vesselName, vesselID, coID, "Name : %name%\\nDate : %date%\\nSCC : %scc%\\n", "#Nothing to report")); + AutomaticReportServer.vesselsCache.add(new Vessel(vesselName, vesselID, coID, "Name : %name%\\nDate : %date%\\nSCC : %scc%\\n", "#Nothing to report", "")); arsDatabase.update("DELETE FROM `waiting` WHERE vesselid = '" + vesselID + "'"); - arsDatabase.update(String.format("INSERT INTO `vessels`(name,vesselid,coid,template,default_text,date,region) VALUES('%s','%s','%s','%s','%s', '%s', %s)", vesselName, vesselID, coID, "Name : %name%\\nDate : %date%\\nSCC : %scc%\\n", "#Nothing to report", date, region)); + arsDatabase.update(String.format("INSERT INTO `vessels`(name,vesselid,coid,template,default_text,date,region,email) VALUES('%s','%s','%s','%s','%s', '%s', %s, '%s')", vesselName, vesselID, coID, "Name : %name%\\nDate : %date%\\nSCC : %scc%\\n", "#Nothing to report", date, region, mail)); rs.close(); arsDatabase.closeConnection(); - return true; + return mail; } /** @@ -140,15 +142,16 @@ public boolean switchPending(String vesselID) throws SQLException { * @param vesselID The ID of the Vessel * @return if the deleting is successful */ - public boolean deletePending(String vesselID) throws SQLException { + public String deletePending(String vesselID) throws SQLException { ResultSet rs = arsDatabase.getResult("SELECT * FROM `waiting` WHERE vesselid = '" + vesselID + "'"); if (!rs.next()) { - return false; + return "false"; } + String mail = rs.getString("email"); arsDatabase.update("DELETE FROM `waiting` WHERE vesselid = '" + vesselID + "'"); rs.close(); arsDatabase.closeConnection(); - return true; + return mail; } /** @@ -160,7 +163,7 @@ public boolean deletePending(String vesselID) throws SQLException { public boolean isCo(String coID) { ResultSet rs = arsDatabase.getResult("SELECT * FROM vessels WHERE coid='" + coID + "'"); try { - if(!rs.next()) { + if (!rs.next()) { rs.close(); arsDatabase.closeConnection(); return false; @@ -184,12 +187,12 @@ public boolean isCo(String coID) { */ public Vessel getVesselWithCo(String coID) throws SQLException { ResultSet rs = arsDatabase.getResult("SELECT * FROM vessels WHERE coid='" + coID + "'"); - if(!rs.next()){ + if (!rs.next()) { rs.close(); arsDatabase.closeConnection(); - return new Vessel("USS Somevessel", "usssomevessel", "noone", "notemplate", ""); + return new Vessel("USS Somevessel", "usssomevessel", "noone", "notemplate", "", ""); } - Vessel vessel = new Vessel(rs.getString("name").replace("_", " "), rs.getString("vesselid"), rs.getString("coid"), rs.getString("template"), rs.getString("default_text")); + Vessel vessel = new Vessel(rs.getString("name").replace("_", " "), rs.getString("vesselid"), rs.getString("coid"), rs.getString("template"), rs.getString("default_text"), rs.getString("email")); rs.close(); arsDatabase.closeConnection(); return vessel; @@ -264,7 +267,7 @@ public ArrayList getAllVessels() throws SQLException { ArrayList v = new ArrayList<>(); while (rs.next()) { - v.add(new Vessel(rs.getString("name"), rs.getString("vesselid"), rs.getString("coid"), rs.getString("template"), rs.getString("default_text"))); + v.add(new Vessel(rs.getString("name"), rs.getString("vesselid"), rs.getString("coid"), rs.getString("template"), rs.getString("default_text"), rs.getString("email"))); } AutomaticReportServer.vesselsCache = v; arsDatabase.closeConnection(); @@ -282,7 +285,7 @@ private Vessel getVesselById(String vesselID) throws SQLException { ResultSet rs = arsDatabase.getResult("SELECT * FROM vessels WHERE vesselid='" + vesselID + "'"); Vessel v = null; while (rs.next()) { - v = new Vessel(rs.getString("name"), rs.getString("vesselid"), rs.getString("coid"), rs.getString("template"), rs.getString("default_text")); + v = new Vessel(rs.getString("name"), rs.getString("vesselid"), rs.getString("coid"), rs.getString("template"), rs.getString("default_text"), rs.getString("email")); } return v; } @@ -444,7 +447,7 @@ public Users synchronizeUser(Users users) { } ResultSet rs = arsDatabase.getResult("SELECT * FROM users WHERE scc='" + users.getScc() + "'"); try { - if(!rs.next()){ + if (!rs.next()) { rs.close(); arsDatabase.closeConnection(); return new Users("invalidID", "invalidSCC", "", ""); @@ -553,7 +556,7 @@ private int sendReportOfVessel(Vessel vessel, ArrayList usersList) { message.add(" "); message.add("End of " + AutomaticReportServer.DATE.format(new Date(System.currentTimeMillis())) + " Reports"); - AutomaticReportServer.sendMessage(vessel.getCoID(), StringUtils.join(message, "\n")); + AutomaticReportServer.sendCompletedMail(vessel.getName().replace("_", " ") + "'s Reports " + AutomaticReportServer.DATE.format(new Date(System.currentTimeMillis())), StringUtils.join(message, "\n"), vessel.getName().replace("_", " "), vessel.getReportOfficerMail()); return reports; } diff --git a/src/main/java/fr/charlotte/arsreloaded/utils/Vessel.java b/src/main/java/fr/charlotte/arsreloaded/utils/Vessel.java index 871f851..68eda2c 100644 --- a/src/main/java/fr/charlotte/arsreloaded/utils/Vessel.java +++ b/src/main/java/fr/charlotte/arsreloaded/utils/Vessel.java @@ -8,13 +8,15 @@ public class Vessel { private String coID; private String template; private String defaultReport; + private String reportOfficerMail; - public Vessel(String name, String vesselID, String coID, String template, String defaultReport) { + public Vessel(String name, String vesselID, String coID, String template, String defaultReport, String reportOfficerMail) { this.name = name; this.vesselID = vesselID; this.coID = coID; this.template = template; this.defaultReport = defaultReport; + this.reportOfficerMail = reportOfficerMail; } public String getDefaultReport() { @@ -56,6 +58,10 @@ public String constructFullReport() { return newDe.toString(); } + public String getReportOfficerMail() { + return reportOfficerMail; + } + public String getVesselID() { return vesselID; } diff --git a/src/main/java/fr/charlotte/arsreloaded/utils/VesselNotFoundException.java b/src/main/java/fr/charlotte/arsreloaded/utils/VesselNotFoundException.java index 2e4d5c9..e9fbc81 100644 --- a/src/main/java/fr/charlotte/arsreloaded/utils/VesselNotFoundException.java +++ b/src/main/java/fr/charlotte/arsreloaded/utils/VesselNotFoundException.java @@ -1,4 +1,9 @@ package fr.charlotte.arsreloaded.utils; public class VesselNotFoundException extends Exception { + + @Override + public void printStackTrace() { + System.err.println("The vessel specified was not found on the database"); + } }