forked from drewr/postal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.clj
205 lines (183 loc) · 7.76 KB
/
message.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
;; Copyright (c) Andrew A. Raines
;;
;; Permission is hereby granted, free of charge, to any person
;; obtaining a copy of this software and associated documentation
;; files (the "Software"), to deal in the Software without
;; restriction, including without limitation the rights to use,
;; copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the
;; Software is furnished to do so, subject to the following
;; conditions:
;;
;; The above copyright notice and this permission notice shall be
;; included in all copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
;; OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
;; OTHER DEALINGS IN THE SOFTWARE.
(ns postal.message
(:use [clojure.set :only [difference]]
[clojure.java.io :only [as-url as-file]]
[postal.date :only [make-date]]
[postal.support :only [do-when make-props message-id user-agent]])
(:import [java.util UUID]
[java.net MalformedURLException]
[javax.activation DataHandler]
[javax.mail Message Message$RecipientType PasswordAuthentication Session]
[javax.mail.internet InternetAddress MimeMessage]))
(def default-charset "utf-8")
(declare make-jmessage)
(defn recipients [msg]
(let [^javax.mail.Message jmsg (make-jmessage msg)]
(map str (.getAllRecipients jmsg))))
(defn sender [msg]
(or (:sender msg) (:from msg)))
(defn make-address
([^String addr ^String charset]
(when-let [^InternetAddress a (try (InternetAddress. addr)
(catch Exception _))]
(InternetAddress. (.getAddress a)
(.getPersonal a)
charset)))
([^String addr ^String name-str ^String charset]
(try (InternetAddress. addr name-str charset)
(catch Exception _))))
(defn make-addresses [addresses charset]
(if (string? addresses)
(recur [addresses] charset)
(into-array InternetAddress (map #(make-address % charset)
addresses))))
(defn- ^java.net.URL make-url [x]
(try (as-url x)
(catch MalformedURLException e
(as-url (as-file x)))))
(defn ^String message->str [msg]
(with-open [out (java.io.ByteArrayOutputStream.)]
(let [^javax.mail.Message jmsg (if (instance? MimeMessage msg)
msg (make-jmessage msg))]
(.writeTo jmsg out)
(str out))))
(defn add-recipient! [jmsg rtype addr charset]
(if-let [addr (make-address addr charset)]
(doto ^javax.mail.Message jmsg
(.addRecipient rtype addr))
jmsg))
(defn add-recipients! [jmsg rtype addrs charset]
(when addrs
(if (string? addrs)
(add-recipient! jmsg rtype addrs charset)
(doseq [addr addrs]
(add-recipient! jmsg rtype addr charset))))
jmsg)
(declare eval-bodypart eval-multipart)
(defprotocol PartEval (eval-part [part]))
(extend-protocol PartEval
clojure.lang.IPersistentMap
(eval-part [part] (eval-bodypart part))
clojure.lang.IPersistentCollection
(eval-part [part]
(doto (javax.mail.internet.MimeBodyPart.)
(.setContent (eval-multipart part)))))
(defn eval-bodypart [part]
(condp (fn [test type] (some #(= % type) test)) (:type part)
[:inline :attachment]
(let [url (make-url (:content part))]
(doto (javax.mail.internet.MimeBodyPart.)
(.setDataHandler (DataHandler. url))
(.setFileName (re-find #"[^/]+$" (.getPath url)))
(.setDisposition (name (:type part)))
(cond-> (:content-type part)
(.setHeader "Content-Type" (:content-type part)))
(cond-> (:content-id part)
(.setContentID (str "<" (:content-id part) ">")))
(cond-> (:file-name part)
(.setFileName (:file-name part)))
(cond-> (:description part)
(.setDescription (:description part)))))
(doto (javax.mail.internet.MimeBodyPart.)
(.setContent (:content part) (:type part))
(cond-> (:file-name part)
(.setFileName (:file-name part)))
(cond-> (:description part)
(.setDescription (:description part))))))
(defn eval-multipart [parts]
(let [;; multiparts can have a number of different types: mixed,
;; alternative, encrypted...
;; The caller can use the first two entries to specify a type.
;; If no type is given, we default to "mixed" (for attachments etc.)
[^String multiPartType, parts] (if (keyword? (first parts))
[(name (first parts)) (rest parts)]
["mixed" parts])
mp (javax.mail.internet.MimeMultipart. multiPartType)]
(doseq [part parts]
(.addBodyPart mp (eval-part part)))
mp))
(defn add-multipart! [^javax.mail.Message jmsg parts]
(.setContent jmsg (eval-multipart parts)))
(defn add-extra! [^javax.mail.Message jmsg msgrest]
(doseq [[n v] msgrest]
(cond
(string? v)
(.addHeader jmsg (if (keyword? n) (name n) n) v)
(seq v)
(doseq [header-line v]
(.addHeader jmsg (if (keyword? n) (name n) n) header-line))
:else
(throw (ex-info "Header values must be either a string or sequence."))))
jmsg)
(defn add-body! [^javax.mail.Message jmsg body charset]
(if (string? body)
(if (instance? MimeMessage jmsg)
(doto ^MimeMessage jmsg (.setText body charset))
(doto jmsg (.setText body)))
(doto jmsg (add-multipart! body))))
(defn make-auth [user pass]
(proxy [javax.mail.Authenticator] []
(getPasswordAuthentication [] (PasswordAuthentication. user pass))))
(defn make-jmessage
([msg]
(let [{:keys [sender from]} msg
{:keys [user pass]} (meta msg)
props (make-props (or sender from) (meta msg))
session (or (:session (meta msg))
(if user
(Session/getInstance props (make-auth user pass))
(Session/getInstance props)))]
(make-jmessage msg session)))
([msg session]
(let [standard [:from :reply-to :to :cc :bcc
:date :subject :body :message-id
:user-agent :sender]
charset (or (:charset msg) default-charset)
jmsg (proxy [MimeMessage] [^Session session]
(updateMessageID []
(.setHeader
^MimeMessage this
"Message-ID" ((:message-id msg message-id)))))]
(doto ^MimeMessage jmsg
(add-recipients! Message$RecipientType/TO (:to msg) charset)
(add-recipients! Message$RecipientType/CC (:cc msg) charset)
(add-recipients! Message$RecipientType/BCC (:bcc msg) charset)
(.setFrom (let [{:keys [from sender]} msg]
^InternetAddress
(make-address (or from sender) charset)))
(.setReplyTo (when-let [reply-to (:reply-to msg)]
(make-addresses reply-to charset)))
(.setSubject (:subject msg) ^String charset)
(.setSentDate (or (:date msg) (make-date)))
(.addHeader "User-Agent" (:user-agent msg (user-agent)))
(add-extra! (apply dissoc msg standard))
(add-body! (:body msg) charset)
(.saveChanges)))))
(defn make-fixture [from to & {:keys [tag]}]
(let [uuid (str (UUID/randomUUID))
tag (or tag "[POSTAL]")]
{:from from
:to to
:subject (format "%s Test -- %s" tag uuid)
:body (format "Test %s" uuid)}))