From 2da318ef449112cd9a6c82b4c3e1004d2c8e7cfe Mon Sep 17 00:00:00 2001 From: Gurkengewuerz Date: Thu, 13 Jul 2017 07:21:57 +0200 Subject: [PATCH] init repo --- .gitignore | 19 +++ pom.xml | 50 +++++++ .../gurkengewuerz/postfix_rest_send/Main.java | 104 +++++++++++++ .../utils/HtmlToPlainText.java | 137 ++++++++++++++++++ 4 files changed, 310 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/de/gurkengewuerz/postfix_rest_send/Main.java create mode 100644 src/main/java/de/gurkengewuerz/postfix_rest_send/utils/HtmlToPlainText.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..daa47ee --- /dev/null +++ b/.gitignore @@ -0,0 +1,19 @@ +# Eclipse +.classpath +.project +.settings/ + +# Intellij +.idea/ +*.iml +*.iws + +# Mac +.DS_Store + +# Maven +log/ +target/ + + +data.db \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..630821e --- /dev/null +++ b/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + + de.gurkengewuerz + postfix-rest-send + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + + org.simplejavamail + simple-java-mail + 4.2.3 + + + org.jsoup + jsoup + 1.10.3 + + + commons-codec + commons-codec + 1.10 + + + org.nanohttpd + nanohttpd + 2.2.0 + + + org.json + json + 20170516 + + + \ No newline at end of file diff --git a/src/main/java/de/gurkengewuerz/postfix_rest_send/Main.java b/src/main/java/de/gurkengewuerz/postfix_rest_send/Main.java new file mode 100644 index 0000000..a2c5c71 --- /dev/null +++ b/src/main/java/de/gurkengewuerz/postfix_rest_send/Main.java @@ -0,0 +1,104 @@ +package de.gurkengewuerz.postfix_rest_send; + +import fi.iki.elonen.NanoHTTPD; +import org.json.JSONObject; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Map; + +/** + * Created by gurkengewuerz.de on 12.07.2017. + */ +public class Main extends NanoHTTPD { + public Main() throws IOException { + super(8081); + start(NanoHTTPD.SOCKET_READ_TIMEOUT, false); + } + +// http://www.simplejavamail.org/#/debugging + + public static void main(String[] args) { +// Mailer m = new Mailer(new ServerConfig("localhost", 25)); +// m.setDebug(true); +// +// Email email = new Email(); +// email.setFromAddress("admin@gurkengewuerz.de", "admin@gurkengewuerz.de"); +// email.setReplyToAddress("support@gurkengewuerz.de", "support@gurkengewuerz.de"); +// email.addRecipient("developer@the-town.net", "developer@the-town.net", Message.RecipientType.TO); +// email.setSubject("Email Test"); +// email.setText("Dies ist ein Email Test von Java.\nHoffentlich bald mit Rest API"); +// email.setTextHTML("We should meet up!"); +// +// m.sendMail(email); + +// HtmlToPlainText formatter = new HtmlToPlainText(); +// formatter.getPlainText() + try { + System.out.println("Started"); + new Main(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Override + public Response serve(IHTTPSession session) { + Map parms = session.getParms(); + + JSONObject json = new JSONObject("{'error':'not found'}"); + Response.Status status = Response.Status.NOT_FOUND; + + if (session.getUri().startsWith("/authorize")) { + if (session.getUri().startsWith("/authorize/add")) { + String username = parms.get("username"); + String password = parms.get("password"); + + if (username != null && password != null) { + System.out.println(checkPassword("/var/www/html/tools/functions.inc.php", "md5", "EMailFam.Schuetrumpf@123", "460b7d128333a4be972d4c7bdb0ee142")); + + + status = Response.Status.OK; + json = new JSONObject("{'token':''}"); + } else { + json = new JSONObject("{'error':'username/password is null'}"); + } + } else if (session.getUri().startsWith("/authorize/revoke")) { + + } + } else if (session.getUri().startsWith("/validate/address")) { + + } else if (session.getUri().startsWith("/send")) { + + } + return newFixedLengthResponse(status, "application/json", json.toString()); + } + + public boolean checkPassword(String path, String encryption, String plain, String dbPW) { + String crypted = execPHP("-r '$CONF = array(); $CONF[\"encrypt\"] = \"" + encryption + "\"; include \"" + path + "\"; echo(pacrypt(\"" + plain + "\", \"" + dbPW + "\")).\"\\n\";'"); + System.out.println(crypted); + return crypted.equals(dbPW); + } + + public String execPHP(String args) { + try { + System.out.println(args); + String line; + StringBuilder output = new StringBuilder(); + Process p = Runtime.getRuntime().exec("php " + args); + BufferedReader input = + new BufferedReader + (new InputStreamReader(p.getInputStream())); + while ((line = input.readLine()) != null) { + output.append(line); + System.out.println(line); + } + input.close(); + return output.toString(); + } catch (Exception err) { + err.printStackTrace(); + } + return ""; + } +} diff --git a/src/main/java/de/gurkengewuerz/postfix_rest_send/utils/HtmlToPlainText.java b/src/main/java/de/gurkengewuerz/postfix_rest_send/utils/HtmlToPlainText.java new file mode 100644 index 0000000..9263ce8 --- /dev/null +++ b/src/main/java/de/gurkengewuerz/postfix_rest_send/utils/HtmlToPlainText.java @@ -0,0 +1,137 @@ +package de.gurkengewuerz.postfix_rest_send.utils; + +import org.jsoup.Jsoup; +import org.jsoup.helper.StringUtil; +import org.jsoup.helper.Validate; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.nodes.Node; +import org.jsoup.nodes.TextNode; +import org.jsoup.select.Elements; +import org.jsoup.select.NodeTraversor; +import org.jsoup.select.NodeVisitor; + +import java.io.IOException; + +/** + * HTML to plain-text. This example program demonstrates the use of jsoup to convert HTML input to lightly-formatted + * plain-text. That is divergent from the general goal of jsoup's .text() methods, which is to get clean data from a + * scrape. + *

+ * Note that this is a fairly simplistic formatter -- for real world use you'll want to embrace and extend. + *

+ *

+ * To invoke from the command line, assuming you've downloaded the jsoup jar to your current directory:

+ *

java -cp jsoup.jar org.jsoup.examples.HtmlToPlainText url [selector]

+ * where url is the URL to fetch, and selector is an optional CSS selector. + * + * @author Jonathan Hedley, jonathan@hedley.net + */ +public class HtmlToPlainText { + private static final String userAgent = "Mozilla/5.0 (jsoup)"; + private static final int timeout = 5 * 1000; + + public static void main(String... args) throws IOException { + Validate.isTrue(args.length == 1 || args.length == 2, "usage: java -cp jsoup.jar org.jsoup.examples.HtmlToPlainText url [selector]"); + final String url = args[0]; + final String selector = args.length == 2 ? args[1] : null; + + // fetch the specified URL and parse to a HTML DOM + Document doc = Jsoup.connect(url).userAgent(userAgent).timeout(timeout).get(); + + HtmlToPlainText formatter = new HtmlToPlainText(); + + if (selector != null) { + Elements elements = doc.select(selector); // get each element that matches the CSS selector + for (Element element : elements) { + String plainText = formatter.getPlainText(element); // format that element to plain text + System.out.println(plainText); + } + } else { // format the whole doc + String plainText = formatter.getPlainText(doc); + System.out.println(plainText); + } + } + + /** + * Format an Element to plain-text + * + * @param element the root element to format + * @return formatted text + */ + public String getPlainText(Element element) { + FormattingVisitor formatter = new FormattingVisitor(); + NodeTraversor traversor = new NodeTraversor(formatter); + traversor.traverse(element); // walk the DOM, and call .head() and .tail() for each node + + return formatter.toString(); + } + + public String getPlainText(String rawHtml) { + Document doc = Jsoup.parse(rawHtml); + return getPlainText(doc); + } + + // the formatting rules, implemented in a breadth-first DOM traverse + private class FormattingVisitor implements NodeVisitor { + private static final int maxWidth = 80; + private int width = 0; + private StringBuilder accum = new StringBuilder(); // holds the accumulated text + + // hit when the node is first seen + public void head(Node node, int depth) { + String name = node.nodeName(); + if (node instanceof TextNode) + append(((TextNode) node).text()); // TextNodes carry all user-readable text in the DOM. + else if (name.equals("li")) + append("\n * "); + else if (name.equals("dt")) + append(" "); + else if (StringUtil.in(name, "p", "h1", "h2", "h3", "h4", "h5", "tr")) + append("\n"); + } + + // hit when all of the node's children (if any) have been visited + public void tail(Node node, int depth) { + String name = node.nodeName(); + if (StringUtil.in(name, "br", "dd", "dt", "p", "h1", "h2", "h3", "h4", "h5")) + append("\n"); + else if (name.equals("a")) + append(String.format(" <%s>", node.absUrl("href"))); + } + + // appends text to the string builder with a simple word wrap method + private void append(String text) { + if (text.startsWith("\n")) + width = 0; // reset counter if starts with a newline. only from formats above, not in natural text + if (text.equals(" ") && + (accum.length() == 0 || StringUtil.in(accum.substring(accum.length() - 1), " ", "\n"))) + return; // don't accumulate long runs of empty spaces + + if (text.length() + width > maxWidth) { // won't fit, needs to wrap + String words[] = text.split("\\s+"); + for (int i = 0; i < words.length; i++) { + String word = words[i]; + boolean last = i == words.length - 1; + if (!last) // insert a space if not the last word + word = word + " "; + if (word.length() + width > maxWidth) { // wrap and reset counter + accum.append("\n").append(word); + width = word.length(); + } else { + accum.append(word); + width += word.length(); + } + } + } else { // fits as is, without need to wrap text + accum.append(text); + width += text.length(); + } + } + + @Override + public String toString() { + return accum.toString(); + } + } +} \ No newline at end of file