Back in busines with a other lib (pull request pending ...)

This commit is contained in:
Aaron Fischer 2015-12-05 22:32:22 +01:00
parent ba34e914cc
commit 0afb8f9c14
2 changed files with 13 additions and 26 deletions

View file

@ -22,6 +22,8 @@
[ring "1.4.0" :exclusions [ring/ring-jetty-adapter]]
[mount "0.1.5"]
[clj-time "0.11.0"]
[io.forward/clojure-mail "1.0.3"]
[macroz/tangle "0.1.9"]
[org.immutant/web "2.1.1" :exclusions [ch.qos.logback/logback-classic]]]
:min-lein-version "2.0.0"

View file

@ -1,41 +1,26 @@
(ns mailhead.parser
(:require [clojure.string :as st]
[clj-time.core :as time]
[clj-time.coerce :as timec])
(:use [clojure.contrib.java-utils :only [as-properties]]
[clojure.java.io :only [input-stream file]])
(:import
(javax.mail Session)
(javax.mail.internet MimeMessage)
(javax.mail.internet InternetHeaders)))
(def session
(Session/getDefaultInstance
(as-properties [["mail.store.protocol" "imaps"]])))
(defn all-headers [message]
(enumeration-seq (:allHeaders (bean message))))
[clj-time.coerce :as timec]
[clojure-mail.core :refer :all]
[clojure-mail.message :refer :all]))
(defn received-headers [all-headers]
(map #(.getValue %) (filter (fn [header] (= "Received" (.getName header))) all-headers)))
(map #(first (vals %))
(filter (fn [header] (= "Received" (first (keys header)))) all-headers)))
(defn parse-received-headers [received-header]
(let [datetime (str/trim (last (st/split received-header #";")))
(let [datetime (st/trim (last (st/split received-header #";")))
;; Hacky hacky, but no time to make this proper ...
cleaned-date (st/trim (st/replace datetime #"(\(.*\)| )" " "))]
{:sender (nth (re-find #"\s*from ([^ ]+)" received-header) 1)
:receiver (nth (re-find #"\s*by ([^ ]+)" received-header) 1)
:time (timec/from-string cleaned-date)}))
(defn extract-relevant-stuff [mime-message]
(->> mime-message
all-headers
(defn parse-from-file [filename]
(->> filename
file->message
read-message
:headers
received-headers
(map parse-received-headers)))
(defn parse-from-string [email-string]
(let [email-stream (java.io.ByteArrayInputStream. (.getBytes email-string))]
(extract-relevant-stuff (MimeMessage. session email-stream))))
(defn parse-from-file [filename]
(extract-relevant-stuff (MimeMessage. session (input-stream filename))))