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