<commit_before>(ns com.draines.postal.message
  (:use [clojure.contrib.test-is :only [run-tests deftest is]]
        [com.draines.postal.date :only [make-date]])
  (:import [java.util Properties]
           [javax.mail Session Message$RecipientType]
           [javax.mail.internet MimeMessage InternetAddress]))

(declare make-jmessage)

(defn recipients [msg]
  (let [jmsg (make-jmessage msg)]
    (map str (.getAllRecipients jmsg))))

(defn sender [msg]
  (or (:sender msg) (:from msg)))

(defn message->str [msg]
  (let [out (java.io.ByteArrayOutputStream.)
        jmsg (if (instance? MimeMessage msg) msg (make-jmessage msg))]
    (.writeTo jmsg out)
    (str out)))

(defn add-recipient! [jmsg rtype addr]
  (doto jmsg (.addRecipient rtype (InternetAddress. addr))))

(defn add-recipients! [jmsg rtype addrs]
  (if (string? addrs)
    (add-recipient! jmsg rtype addrs)
    (doseq [addr addrs]
      (add-recipient! jmsg rtype addr)))
  jmsg)

(defn add-multipart! [jmsg parts]
  (let [mp (javax.mail.internet.MimeMultipart.)]
    (doseq [part parts]
      (condp (fn [test type] (some #(= % type) test)) (:type part)
        [:inline :attachment] (.addBodyPart mp
                                            (doto (javax.mail.internet.MimeBodyPart.)
                                              (.attachFile (:content part))
                                              (.setDisposition (name (:type part)))))
        (.addBodyPart mp
                      (doto (javax.mail.internet.MimeBodyPart.)
                        (.setContent (:content part) (:type part))))))
    (.setContent jmsg mp)))

(defn make-jmessage [msg]
  (let [{:keys [sender from to cc bcc date subject body host port]} msg
        props (doto (java.util.Properties.)
                (.put "mail.smtp.host" (or host "not.provided"))
                (.put "mail.smtp.port" (or port "25"))
                (.put "mail.smtp.from" (or sender from)))
        session (Session/getInstance props)
        jmsg (MimeMessage. session)]
    (add-recipients! jmsg Message$RecipientType/TO to)
    (add-recipients! jmsg Message$RecipientType/CC cc)
    (add-recipients! jmsg Message$RecipientType/BCC bcc)
    (.setFrom jmsg (InternetAddress. from))
    (.setSubject jmsg subject)
    (if (string? body)
      (.setText jmsg body)
      (add-multipart! jmsg body))
    (.setSentDate jmsg (or date (make-date)))
    jmsg))

(deftest test-simple<commit_msg>Break up make-jmessage to accept existing Session.<commit_after>(ns com.draines.postal.message
  (:use [clojure.contrib.test-is :only [run-tests deftest is]]
        [com.draines.postal.date :only [make-date]])
  (:import [java.util Properties]
           [javax.mail Session Message$RecipientType]
           [javax.mail.internet MimeMessage InternetAddress]))

(declare make-jmessage)

(defn recipients [msg]
  (let [jmsg (make-jmessage msg)]
    (map str (.getAllRecipients jmsg))))

(defn sender [msg]
  (or (:sender msg) (:from msg)))

(defn message->str [msg]
  (let [out (java.io.ByteArrayOutputStream.)
        jmsg (if (instance? MimeMessage msg) msg (make-jmessage msg))]
    (.writeTo jmsg out)
    (str out)))

(defn add-recipient! [jmsg rtype addr]
  (doto jmsg (.addRecipient rtype (InternetAddress. addr))))

(defn add-recipients! [jmsg rtype addrs]
  (if (string? addrs)
    (add-recipient! jmsg rtype addrs)
    (doseq [addr addrs]
      (add-recipient! jmsg rtype addr)))
  jmsg)

(defn add-multipart! [jmsg parts]
  (let [mp (javax.mail.internet.MimeMultipart.)]
    (doseq [part parts]
      (condp (fn [test type] (some #(= % type) test)) (:type part)
        [:inline :attachment] (.addBodyPart mp
                                            (doto (javax.mail.internet.MimeBodyPart.)
                                              (.attachFile (:content part))
                                              (.setDisposition (name (:type part)))))
        (.addBodyPart mp
                      (doto (javax.mail.internet.MimeBodyPart.)
                        (.setContent (:content part) (:type part))))))
    (.setContent jmsg mp)))

(defn make-jmessage
  ([msg]
     (let [{:keys [sender from host port]} msg
           props (doto (java.util.Properties.)
                   (.put "mail.smtp.host" (or host "not.provided"))
                   (.put "mail.smtp.port" (or port "25"))
                   (.put "mail.smtp.from" (or sender from)))
           session (Session/getInstance props)]
       (make-jmessage msg session)))
  ([msg session]
     (let [{:keys [from to cc bcc date subject body]} msg
           jmsg (MimeMessage. session)]
       (add-recipients! jmsg Message$RecipientType/TO to)
       (add-recipients! jmsg Message$RecipientType/CC cc)
       (add-recipients! jmsg Message$RecipientType/BCC bcc)
       (.setFrom jmsg (InternetAddress. from))
       (.setSubject jmsg subject)
       (if (string? body)
         (.setText jmsg body)
         (add-multipart! jmsg body))
       (.setSentDate jmsg (or date (make-date)))
       jmsg)))

(deftest test-simple
{'fraction_non_alphanumeric': 0.13725880246667993, 'fraction_numerical': 0.0007957032027053909, 'mean_word_length': 2.329801324503311, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1067877', 'n_tokens_mistral': 1681, 'n_tokens_neox': 1580, 'n_words': 436}