Add the samples and switch to string based parsing (no files eny more)

This commit is contained in:
Aaron Fischer 2015-12-06 13:14:34 +01:00
parent 0786d3492a
commit 03842ae9ab
3 changed files with 30 additions and 15 deletions

View file

@ -3,7 +3,11 @@
[clj-time.core :as time] [clj-time.core :as time]
[clj-time.coerce :as timec] [clj-time.coerce :as timec]
[clojure-mail.core :refer :all] [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] (defn received-headers [all-headers]
(map #(first (vals %)) (map #(first (vals %))
@ -17,9 +21,14 @@
:receiver (nth (re-find #"\s*by ([^ ]+)" received-header) 1) :receiver (nth (re-find #"\s*by ([^ ]+)" received-header) 1)
:time (timec/from-string cleaned-date)})) :time (timec/from-string cleaned-date)}))
(defn parse-from-file [filename] (defn string->message [string]
(->> filename (let [props (Session/getDefaultInstance (Properties.))
file->message input-stream (ByteArrayInputStream. (.getBytes string))]
(MimeMessage. props input-stream)))
(defn parse [message-string]
(->> message-string
string->message
read-message read-message
:headers :headers
received-headers received-headers

View file

@ -1,6 +1,7 @@
(ns mailhead.routes.home (ns mailhead.routes.home
(:require [mailhead.layout :as layout] (:require [mailhead.layout :as layout]
[mailhead.visualize :as v] [mailhead.visualize :as v]
[mailhead.parser :as parser]
[compojure.core :refer [defroutes GET POST]] [compojure.core :refer [defroutes GET POST]]
[ring.util.http-response :refer [ok content-type]] [ring.util.http-response :refer [ok content-type]]
[clojure.java.io :as io])) [clojure.java.io :as io]))
@ -11,11 +12,17 @@
(defn about-page [] (defn about-page []
(layout/render "about.html")) (layout/render "about.html"))
(defn analyze-mailheader [mailheader] (defn show-sample [index]
(let [token (v/draw mailheader)] (let [email-content (slurp (str "/home/aaron/workbench/clojurecup2015/sample-data/header" index ".eml"))]
;; TODO: Show image and other data on result.html (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" (layout/render "result.html"
{:mailheader mailheader {:mailheader message-string
:email parsed-email
:token token}))) :token token})))
(defn generated-image [token] (defn generated-image [token]
@ -26,5 +33,6 @@
(defroutes home-routes (defroutes home-routes
(GET "/" [] (home-page)) (GET "/" [] (home-page))
(GET "/image/:token" [token] (generated-image token)) (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))) (GET "/about" [] (about-page)))

View file

@ -38,9 +38,7 @@
(defn next-token [] (defn next-token []
(str (java.util.UUID/randomUUID))) (str (java.util.UUID/randomUUID)))
;; TODO: Make it work with strings (defn draw [parsed-email]
(defn draw [mailheader] (let [token (next-token)]
(let [filename "/home/aaron/workbench/clojurecup2015/sample-data/header1.eml" (save-image (draw-path parsed-email) token)
token (next-token)]
(save-image (draw-path (parser/parse-from-file filename)) token)
token)) token))