From e7747de9ccfb7fe953aa9f92f448e1f49fcc1dcb Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Sat, 5 Dec 2015 14:17:05 +0100 Subject: [PATCH] Finish the header parsing ... --- project.clj | 1 + src/mailhead/parser.clj | 27 ++++++++++++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/project.clj b/project.clj index 0ba8cd5..bc6a262 100644 --- a/project.clj +++ b/project.clj @@ -21,6 +21,7 @@ [ring/ring-defaults "0.1.5"] [ring "1.4.0" :exclusions [ring/ring-jetty-adapter]] [mount "0.1.5"] + [clj-time "0.11.0"] [org.immutant/web "2.1.1" :exclusions [ch.qos.logback/logback-classic]]] :min-lein-version "2.0.0" diff --git a/src/mailhead/parser.clj b/src/mailhead/parser.clj index 7baff6d..761f6b8 100644 --- a/src/mailhead/parser.clj +++ b/src/mailhead/parser.clj @@ -1,9 +1,9 @@ (ns mailhead.parser - (:require - [clojure.reflect :as r]) + (: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]] - [clojure.pprint :only [print-table]]) + [clojure.java.io :only [input-stream file]]) (:import (javax.mail Session) (javax.mail.internet MimeMessage) @@ -14,12 +14,21 @@ (as-properties [["mail.store.protocol" "imaps"]]))) (defn all-headers [message] - (let [headers (enumeration-seq (:allHeaders (bean message)))] - (map (fn [header] - (assoc {} (.getName header) (.getValue header))) - headers))) + (enumeration-seq (:allHeaders (bean message)))) -(defn ) +(defn received-headers [all-headers] + (map #(.getValue %) (filter (fn [header] (= "Received" (.getName header))) all-headers))) + +(defn find-from-host [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 #"(\(.*\)| )" " "))] + {: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 parse-received-header [header] + ((insta/parser (slurp "received-header.parser")) header)) (defn parse-from-string [email-string] (let [email-stream (java.io.ByteArrayInputStream. (.getBytes email-string))]