clojurecup2015-mailhead/src/mailhead/parser.clj

43 lines
1.4 KiB
Clojure

(ns mailhead.parser
(:require [clojure.string :as st]
[clj-time.core :as time]
[clj-time.coerce :as timec]
[clojure-mail.core :refer :all]
[clojure-mail.message :refer :all])
(:import [java.util Properties]
[java.io ByteArrayInputStream IOException]
[javax.mail.internet MimeMessage]
[javax.mail Session]))
(defn find-headers [all-headers needle]
(map #(first (vals %))
(filter (fn [header] (= needle (first (keys header)))) all-headers)))
(defn received-headers [all-headers]
(find-headers all-headers "Received"))
(defn parse-received-headers [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 string->message [string]
(let [props (Session/getDefaultInstance (Properties.))
input-stream (ByteArrayInputStream. (.getBytes string))]
(MimeMessage. props input-stream)))
(defn parse-all [message-string]
(->> message-string
string->message
read-message))
(defn parse [message-string]
(->> message-string
parse-all
:headers
received-headers
(map parse-received-headers)))