From 03842ae9ab9e62d7bd8e74ea406082aca309fbd3 Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Sun, 6 Dec 2015 13:14:34 +0100 Subject: [PATCH] Add the samples and switch to string based parsing (no files eny more) --- src/mailhead/parser.clj | 17 +++++++++++++---- src/mailhead/routes/home.clj | 18 +++++++++++++----- src/mailhead/visualize.clj | 10 ++++------ 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/mailhead/parser.clj b/src/mailhead/parser.clj index 1a5e215..6f2e44f 100644 --- a/src/mailhead/parser.clj +++ b/src/mailhead/parser.clj @@ -3,7 +3,11 @@ [clj-time.core :as time] [clj-time.coerce :as timec] [clojure-mail.core :refer :all] - [clojure-mail.message :refer :all])) + [clojure-mail.message :refer :all]) + (:import [java.util Properties] + [java.io ByteArrayInputStream IOException] + [javax.mail.internet MimeMessage] + [javax.mail Session])) (defn received-headers [all-headers] (map #(first (vals %)) @@ -17,9 +21,14 @@ :receiver (nth (re-find #"\s*by ([^ ]+)" received-header) 1) :time (timec/from-string cleaned-date)})) -(defn parse-from-file [filename] - (->> filename - file->message +(defn string->message [string] + (let [props (Session/getDefaultInstance (Properties.)) + input-stream (ByteArrayInputStream. (.getBytes string))] + (MimeMessage. props input-stream))) + +(defn parse [message-string] + (->> message-string + string->message read-message :headers received-headers diff --git a/src/mailhead/routes/home.clj b/src/mailhead/routes/home.clj index e1b2eb9..4d94bfb 100644 --- a/src/mailhead/routes/home.clj +++ b/src/mailhead/routes/home.clj @@ -1,6 +1,7 @@ (ns mailhead.routes.home (:require [mailhead.layout :as layout] [mailhead.visualize :as v] + [mailhead.parser :as parser] [compojure.core :refer [defroutes GET POST]] [ring.util.http-response :refer [ok content-type]] [clojure.java.io :as io])) @@ -11,11 +12,17 @@ (defn about-page [] (layout/render "about.html")) -(defn analyze-mailheader [mailheader] - (let [token (v/draw mailheader)] - ;; TODO: Show image and other data on result.html +(defn show-sample [index] + (let [email-content (slurp (str "/home/aaron/workbench/clojurecup2015/sample-data/header" index ".eml"))] + (show-result email-content))) + +;; TODO: Show more information (mail round trip time, ...) +(defn show-result [message-string] + (let [parsed-email (parser/parse message-string) + token (v/draw parsed-email)] (layout/render "result.html" - {:mailheader mailheader + {:mailheader message-string + :email parsed-email :token token}))) (defn generated-image [token] @@ -26,5 +33,6 @@ (defroutes home-routes (GET "/" [] (home-page)) (GET "/image/:token" [token] (generated-image token)) - (POST "/analyze" [mailheader] (analyze-mailheader mailheader)) + (POST "/analyze" [mailheader] (show-result mailheader)) + (GET "/example/:index" [index] (show-sample index)) (GET "/about" [] (about-page))) diff --git a/src/mailhead/visualize.clj b/src/mailhead/visualize.clj index 85bbdd1..146726f 100644 --- a/src/mailhead/visualize.clj +++ b/src/mailhead/visualize.clj @@ -38,9 +38,7 @@ (defn next-token [] (str (java.util.UUID/randomUUID))) - ;; TODO: Make it work with strings - (defn draw [mailheader] - (let [filename "/home/aaron/workbench/clojurecup2015/sample-data/header1.eml" - token (next-token)] - (save-image (draw-path (parser/parse-from-file filename)) token) - token)) +(defn draw [parsed-email] + (let [token (next-token)] + (save-image (draw-path parsed-email) token) + token))