Make it work for alle received heaer fields

This commit is contained in:
Aaron Fischer 2015-12-05 14:29:04 +01:00
parent e7747de9cc
commit ba34e914cc

View file

@ -19,7 +19,7 @@
(defn received-headers [all-headers]
(map #(.getValue %) (filter (fn [header] (= "Received" (.getName header))) all-headers)))
(defn find-from-host [received-header]
(defn parse-received-headers [received-header]
(let [datetime (str/trim (last (st/split received-header #";")))
;; Hacky hacky, but no time to make this proper ...
cleaned-date (st/trim (st/replace datetime #"(\(.*\)| )" " "))]
@ -27,12 +27,15 @@
:receiver (nth (re-find #"\s*by ([^ ]+)" received-header) 1)
:time (timec/from-string cleaned-date)}))
(defn parse-received-header [header]
((insta/parser (slurp "received-header.parser")) header))
(defn extract-relevant-stuff [mime-message]
(->> mime-message
all-headers
received-headers
(map parse-received-headers)))
(defn parse-from-string [email-string]
(let [email-stream (java.io.ByteArrayInputStream. (.getBytes email-string))]
(all-headers (MimeMessage. session email-stream))))
(extract-relevant-stuff (MimeMessage. session email-stream))))
(defn parse-from-file [filename]
(all-headers (MimeMessage. session (input-stream filename))))
(extract-relevant-stuff (MimeMessage. session (input-stream filename))))