]> gitweb.factorcode.org Git - factor.git/blob - basis/smtp/smtp.factor
Merge branch 'keyholder'
[factor.git] / basis / smtp / smtp.factor
1 ! Copyright (C) 2007, 2009 Elie CHAFTARI, Dirk Vleugels,
2 ! Slava Pestov, Doug Coleman, Daniel Ehrenberg.
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: arrays namespaces make io io.encodings io.encodings.string
5 io.encodings.utf8 io.encodings.iana io.encodings.binary
6 io.encodings.ascii io.timeouts io.sockets io.sockets.secure io.crlf
7 kernel logging sequences combinators splitting assocs strings
8 math.order math.parser random system calendar summary calendar.format
9 accessors sets hashtables base64 debugger classes prettyprint words ;
10 IN: smtp
11
12 SYMBOL: smtp-domain
13
14 SYMBOL: smtp-server
15 "localhost" 25 <inet> smtp-server set-global
16
17 SYMBOL: smtp-tls?
18
19 SYMBOL: smtp-read-timeout
20 1 minutes smtp-read-timeout set-global
21
22 SINGLETON: no-auth
23
24 TUPLE: plain-auth username password ;
25 C: <plain-auth> plain-auth
26
27 SYMBOL: smtp-auth
28 no-auth smtp-auth set-global
29
30 LOG: log-smtp-connection NOTICE ( addrspec -- )
31
32 : with-smtp-connection ( quot -- )
33     smtp-server get
34     dup log-smtp-connection
35     ascii [
36         smtp-domain [ host-name or ] change
37         smtp-read-timeout get timeouts
38         call
39     ] with-client ; inline
40
41 TUPLE: email
42     { from string }
43     { to array }
44     { cc array }
45     { bcc array }
46     { subject string }
47     { content-type string initial: "text/plain" }
48     { encoding word initial: utf8 }
49     { body string } ;
50
51 : <email> ( -- email ) email new ; inline
52
53 <PRIVATE
54
55 : command ( string -- ) write crlf flush ;
56
57 \ command DEBUG add-input-logging
58
59 : helo ( -- ) "EHLO " host-name append command ;
60
61 : start-tls ( -- ) "STARTTLS" command ;
62
63 ERROR: bad-email-address email ;
64
65 : validate-address ( string -- string' )
66     #! Make sure we send funky stuff to the server by accident.
67     dup "\r\n>" intersects?
68     [ bad-email-address ] when ;
69
70 : mail-from ( fromaddr -- )
71     validate-address
72     "MAIL FROM:<" ">" surround command ;
73
74 : rcpt-to ( to -- )
75     validate-address
76     "RCPT TO:<" ">" surround command ;
77
78 : data ( -- )
79     "DATA" command ;
80
81 ERROR: message-contains-dot message ;
82
83 M: message-contains-dot summary ( obj -- string )
84     drop "Message cannot contain . on a line by itself" ;
85
86 : validate-message ( msg -- msg' )
87     "." over member?
88     [ message-contains-dot ] when ;
89
90 : send-body ( email -- )
91     binary encode-output
92     [ body>> ] [ encoding>> ] bi encode >base64-lines write
93     ascii encode-output crlf
94     "." command ;
95
96 : quit ( -- )
97     "QUIT" command ;
98
99 LOG: smtp-response DEBUG
100
101 : multiline? ( response -- ? )
102     3 swap ?nth CHAR: - = ;
103
104 : (receive-response) ( -- )
105     read-crlf
106     [ , ]
107     [ smtp-response ]
108     [ multiline? [ (receive-response) ] when ]
109     tri ;
110
111 TUPLE: response code messages ;
112
113 : <response> ( lines -- response )
114     [ first 3 head string>number ] keep response boa ;
115
116 : receive-response ( -- response )
117     [ (receive-response) ] { } make <response> ;
118
119 ERROR: smtp-error response ;
120
121 M: smtp-error error.
122     "SMTP error (" write dup class pprint ")" print
123     response>> messages>> [ print ] each ;
124
125 ERROR: smtp-server-busy < smtp-error ;
126 ERROR: smtp-syntax-error < smtp-error ;
127 ERROR: smtp-command-not-implemented < smtp-error ;
128 ERROR: smtp-bad-authentication < smtp-error ;
129 ERROR: smtp-mailbox-unavailable < smtp-error ;
130 ERROR: smtp-user-not-local < smtp-error ;
131 ERROR: smtp-exceeded-storage-allocation < smtp-error ;
132 ERROR: smtp-bad-mailbox-name < smtp-error ;
133 ERROR: smtp-transaction-failed < smtp-error ;
134
135 : check-response ( response -- )
136     dup code>> {
137         { [ dup { 220 235 250 221 354 } member? ] [ 2drop ] }
138         { [ dup 400 499 between? ] [ drop smtp-server-busy ] }
139         { [ dup 500 = ] [ drop smtp-syntax-error ] }
140         { [ dup 501 = ] [ drop smtp-command-not-implemented ] }
141         { [ dup 500 509 between? ] [ drop smtp-syntax-error ] }
142         { [ dup 530 539 between? ] [ drop smtp-bad-authentication ] }
143         { [ dup 550 = ] [ drop smtp-mailbox-unavailable ] }
144         { [ dup 551 = ] [ drop smtp-user-not-local ] }
145         { [ dup 552 = ] [ drop smtp-exceeded-storage-allocation ] }
146         { [ dup 553 = ] [ drop smtp-bad-mailbox-name ] }
147         { [ dup 554 = ] [ drop smtp-transaction-failed ] }
148         [ drop smtp-error ]
149     } cond ;
150
151 : get-ok ( -- ) receive-response check-response ;
152
153 GENERIC: send-auth ( auth -- )
154
155 M: no-auth send-auth drop ;
156
157 : plain-auth-string ( username password -- string )
158     [ "\0" prepend ] bi@ append utf8 encode >base64 ;
159
160 M: plain-auth send-auth
161     [ username>> ] [ password>> ] bi plain-auth-string
162     "AUTH PLAIN " prepend command get-ok ;
163
164 : auth ( -- ) smtp-auth get send-auth ;
165
166 : encode-header ( string -- string' )
167     dup aux>> [
168         utf8 encode >base64
169         "=?utf-8?B?" "?=" surround
170     ] when ;
171
172 ERROR: invalid-header-string string ;
173
174 : validate-header ( string -- string' )
175     dup "\r\n" intersects?
176     [ invalid-header-string ] when ;
177
178 : write-header ( key value -- )
179     [ validate-header write ]
180     [ ": " write validate-header encode-header write ] bi* crlf ;
181
182 : write-headers ( assoc -- )
183     [ write-header ] assoc-each ;
184
185 : message-id ( -- string )
186     [
187         "<" %
188         64 random-bits #
189         "-" %
190         micros #
191         "@" %
192         smtp-domain get [ host-name ] unless* %
193         ">" %
194     ] "" make ;
195
196 : extract-email ( recepient -- email )
197     ! This could be much smarter.
198     " " split1-last swap or "<" ?head drop ">" ?tail drop ;
199
200 : email-content-type ( email -- content-type )
201     [ content-type>> ] [ encoding>> encoding>name ] bi "; charset=" glue ;
202
203 : email>headers ( email -- assoc )
204     [
205         now timestamp>rfc822 "Date" set
206         message-id "Message-Id" set
207         "1.0" "MIME-Version" set
208         "base64" "Content-Transfer-Encoding" set
209         {
210             [ from>> "From" set ]
211             [ to>> ", " join "To" set ]
212             [ cc>> ", " join [ "Cc" set ] unless-empty ]
213             [ subject>> "Subject" set ]
214             [ email-content-type "Content-Type" set ]
215         } cleave
216     ] { } make-assoc ;
217
218 : (send-email) ( headers email -- )
219     [
220         get-ok
221         helo get-ok
222         smtp-tls? get [ start-tls get-ok send-secure-handshake ] when
223         auth
224         dup from>> extract-email mail-from get-ok
225         dup to>> [ extract-email rcpt-to get-ok ] each
226         dup cc>> [ extract-email rcpt-to get-ok ] each
227         dup bcc>> [ extract-email rcpt-to get-ok ] each
228         data get-ok
229         swap write-headers
230         crlf
231         send-body get-ok
232         quit get-ok
233     ] with-smtp-connection ;
234
235 PRIVATE>
236
237 : send-email ( email -- )
238     [ email>headers ] keep (send-email) ;