]> gitweb.factorcode.org Git - factor.git/blob - basis/smtp/smtp.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / basis / smtp / smtp.factor
1 ! Copyright (C) 2007, 2008 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.string io.encodings.utf8
5 io.encodings.iana io.timeouts io.sockets io.sockets.secure
6 io.encodings.ascii kernel logging sequences combinators splitting
7 assocs strings math.order math.parser random system calendar summary
8 calendar.format accessors sets hashtables base64 debugger classes
9 prettyprint io.crlf 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     [ body>> ] [ encoding>> ] bi encode
92     >base64-lines write crlf
93     "." command ;
94
95 : quit ( -- )
96     "QUIT" command ;
97
98 LOG: smtp-response DEBUG
99
100 : multiline? ( response -- ? )
101     3 swap ?nth CHAR: - = ;
102
103 : (receive-response) ( -- )
104     read-crlf
105     [ , ]
106     [ smtp-response ]
107     [ multiline? [ (receive-response) ] when ]
108     tri ;
109
110 TUPLE: response code messages ;
111
112 : <response> ( lines -- response )
113     [ first 3 head string>number ] keep response boa ;
114
115 : receive-response ( -- response )
116     [ (receive-response) ] { } make <response> ;
117
118 ERROR: smtp-error response ;
119
120 M: smtp-error error.
121     "SMTP error (" write dup class pprint ")" print
122     response>> messages>> [ print ] each ;
123
124 ERROR: smtp-server-busy < smtp-error ;
125 ERROR: smtp-syntax-error < smtp-error ;
126 ERROR: smtp-command-not-implemented < smtp-error ;
127 ERROR: smtp-bad-authentication < smtp-error ;
128 ERROR: smtp-mailbox-unavailable < smtp-error ;
129 ERROR: smtp-user-not-local < smtp-error ;
130 ERROR: smtp-exceeded-storage-allocation < smtp-error ;
131 ERROR: smtp-bad-mailbox-name < smtp-error ;
132 ERROR: smtp-transaction-failed < smtp-error ;
133
134 : check-response ( response -- )
135     dup code>> {
136         { [ dup { 220 235 250 221 354 } member? ] [ 2drop ] }
137         { [ dup 400 499 between? ] [ drop smtp-server-busy ] }
138         { [ dup 500 = ] [ drop smtp-syntax-error ] }
139         { [ dup 501 = ] [ drop smtp-command-not-implemented ] }
140         { [ dup 500 509 between? ] [ drop smtp-syntax-error ] }
141         { [ dup 530 539 between? ] [ drop smtp-bad-authentication ] }
142         { [ dup 550 = ] [ drop smtp-mailbox-unavailable ] }
143         { [ dup 551 = ] [ drop smtp-user-not-local ] }
144         { [ dup 552 = ] [ drop smtp-exceeded-storage-allocation ] }
145         { [ dup 553 = ] [ drop smtp-bad-mailbox-name ] }
146         { [ dup 554 = ] [ drop smtp-transaction-failed ] }
147         [ drop smtp-error ]
148     } cond ;
149
150 : get-ok ( -- ) receive-response check-response ;
151
152 GENERIC: send-auth ( auth -- )
153
154 M: no-auth send-auth drop ;
155
156 : plain-auth-string ( username password -- string )
157     [ "\0" prepend ] bi@ append utf8 encode >base64 ;
158
159 M: plain-auth send-auth
160     [ username>> ] [ password>> ] bi plain-auth-string
161     "AUTH PLAIN " prepend command get-ok ;
162
163 : auth ( -- ) smtp-auth get send-auth ;
164
165 : encode-header ( string -- string' )
166     dup aux>> [
167         utf8 encode >base64
168         "=?utf-8?B?" "?=" surround
169     ] when ;
170
171 ERROR: invalid-header-string string ;
172
173 : validate-header ( string -- string' )
174     dup "\r\n" intersects?
175     [ invalid-header-string ] when ;
176
177 : write-header ( key value -- )
178     [ validate-header write ]
179     [ ": " write validate-header encode-header write ] bi* crlf ;
180
181 : write-headers ( assoc -- )
182     [ write-header ] assoc-each ;
183
184 : message-id ( -- string )
185     [
186         "<" %
187         64 random-bits #
188         "-" %
189         micros #
190         "@" %
191         smtp-domain get [ host-name ] unless* %
192         ">" %
193     ] "" make ;
194
195 : extract-email ( recepient -- email )
196     ! This could be much smarter.
197     " " split1-last swap or "<" ?head drop ">" ?tail drop ;
198
199 : email-content-type ( email -- content-type )
200     [ content-type>> ] [ encoding>> encoding>name ] bi "; charset=" glue ;
201
202 : email>headers ( email -- assoc )
203     [
204         now timestamp>rfc822 "Date" set
205         message-id "Message-Id" set
206         "1.0" "MIME-Version" set
207         "base64" "Content-Transfer-Encoding" set
208         {
209             [ from>> "From" set ]
210             [ to>> ", " join "To" set ]
211             [ cc>> ", " join [ "Cc" set ] unless-empty ]
212             [ subject>> "Subject" set ]
213             [ email-content-type "Content-Type" set ]
214         } cleave
215     ] { } make-assoc ;
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         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) ;