summaryrefslogtreecommitdiff
path: root/src/email.clj
blob: 8a2941881ef000c1914d788ea56e748118d06661 (plain)
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
(ns email
  (:import  org.antlr.stringtemplate.StringTemplateGroup)
  (:require [clojure.contrib.str-utils2 :as s])
  (:use     utils))

(defn base-mail [& m]
  (let [mail (apply hash-map m)
        props (java.util.Properties.)]

    (doto props
      (.put "mail.smtp.host" (:host mail))
      (.put "mail.smtp.port" (:port mail))
      (.put "mail.smtp.user" (:user mail))
      (.put "mail.smtp.socketFactory.port"  (:port mail))
      (.put "mail.smtp.auth" "true"))

    (if (= (:ssl mail) true)
      (doto props
        (.put "mail.smtp.starttls.enable" "true")
        (.put "mail.smtp.socketFactory.class" 
              "javax.net.ssl.SSLSocketFactory")
        (.put "mail.smtp.socketFactory.fallback" "false")))

    (let [authenticator (proxy [javax.mail.Authenticator] [] 
                          (getPasswordAuthentication 
                           []
                           (javax.mail.PasswordAuthentication. 
                            (:user mail) (:password mail))))
          session (javax.mail.Session/getDefaultInstance props authenticator)
          msg     (javax.mail.internet.MimeMessage. session)] 

      (.setFrom msg (javax.mail.internet.InternetAddress. (:user mail)))
      (doseq [to (:to mail)] 
        (.setRecipients msg 
                        (javax.mail.Message$RecipientType/TO)
                        (javax.mail.internet.InternetAddress/parse to)))
      (.setSubject msg (:subject mail))
      (.setContent msg (:text mail) (:mime mail))
      (javax.mail.Transport/send msg))))

(def mail-templates (StringTemplateGroup. "dumpfm-mail" "template/mail" ))
(.setRefreshInterval mail-templates 3)

(defn parse-mail-template [temp props]
  (let [st (.getInstanceOf mail-templates temp)]
    (doseq [[k v] props]
      (.setAttribute st k v))
    (let [[s b] (.split (.toString st) "\\n" 2)]
      [(.trim (.replaceFirst s "SUBJECT: " ""))
       (.trim b)])))

(defn classify-mimetype [text]
  (if (and (re-find #"(?i)<html>" text)
           (re-find #"(?i)</html>" text))
    "text/html"
    "text/plain"))

(defn dump-mail [to subject text]
  (base-mail :user "info@dump.fm"
             :password "UHR4Moghu5a2"
             :host "smtpout.secureserver.net"
             :port 465
             :ssl true
             :to [(join to ",")]
             :subject subject
             :text text
             :mime (classify-mimetype text)))

(def *admin-lists* {"dumpfmprod" ["info@dump.fm"]
                    "sostler"    ["sbostler@gmail.com"]
                    "jules"      ["julescarbon@gmail.com"]})

(defn get-admins []
  (or (*admin-lists* (System/getProperty "user.name"))
      (*admin-lists* "dumpfmprod")))

(defn send-registration-email 
  ([nick email] (send-registration-email nick email "welcome"))
  ([nick email temp]
     (let [[s b] (parse-mail-template temp {"nick" nick})]
       (dump-mail [email] s b))))

(defn send-reset-email
  ([nick email link temp]
     (let [[s b] (parse-mail-template temp {"nick" nick "link" link})]
       (dump-mail [email] s b)))
  ([nick email link] (send-reset-email nick email link "reset")))

(defn send-mute-email [user-nick admin-nick reason duration]
  (let [subject (format "%s was muted by %s for %s"
                        user-nick admin-nick duration)
        body    (format "Reason: %s"
                        reason)]
    (dump-mail (get-admins) subject body)))