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 ;
15 "localhost" 25 <inet> smtp-server set-global
19 SYMBOL: smtp-read-timeout
20 1 minutes smtp-read-timeout set-global
24 TUPLE: plain-auth username password ;
25 C: <plain-auth> plain-auth
28 no-auth smtp-auth set-global
30 LOG: log-smtp-connection NOTICE ( addrspec -- )
32 : with-smtp-connection ( quot -- )
34 dup log-smtp-connection
36 smtp-domain [ host-name or ] change
37 smtp-read-timeout get timeouts
39 ] with-client ; inline
49 : <email> ( -- email ) email new ; inline
53 : command ( string -- ) write crlf flush ;
55 \ command DEBUG add-input-logging
57 : helo ( -- ) "EHLO " host-name append command ;
59 : start-tls ( -- ) "STARTTLS" command ;
61 ERROR: bad-email-address email ;
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 ;
68 : mail-from ( fromaddr -- )
70 "MAIL FROM:<" ">" surround command ;
74 "RCPT TO:<" ">" surround command ;
79 ERROR: message-contains-dot message ;
81 M: message-contains-dot summary ( obj -- string )
82 drop "Message cannot contain . on a line by itself" ;
84 : validate-message ( msg -- msg' )
86 [ message-contains-dot ] when ;
88 : send-body ( body -- )
90 >base64-lines write crlf
96 LOG: smtp-response DEBUG
98 : multiline? ( response -- ? )
99 3 swap ?nth CHAR: - = ;
101 : (receive-response) ( -- )
105 [ multiline? [ (receive-response) ] when ]
108 TUPLE: response code messages ;
110 : <response> ( lines -- response )
111 [ first 3 head string>number ] keep response boa ;
113 : receive-response ( -- response )
114 [ (receive-response) ] { } make <response> ;
116 ERROR: smtp-error response ;
119 "SMTP error (" write dup class pprint ")" print
120 response>> messages>> [ print ] each ;
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 ;
132 : check-response ( response -- )
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 ] }
148 : get-ok ( -- ) receive-response check-response ;
150 GENERIC: send-auth ( auth -- )
152 M: no-auth send-auth drop ;
154 : plain-auth-string ( username password -- string )
155 [ "\0" prepend ] bi@ append utf8 encode >base64 ;
157 M: plain-auth send-auth
158 [ username>> ] [ password>> ] bi plain-auth-string
159 "AUTH PLAIN " prepend command get-ok ;
161 : auth ( -- ) smtp-auth get send-auth ;
163 : encode-header ( string -- string' )
166 swap utf8 encode >base64
170 ERROR: invalid-header-string string ;
172 : validate-header ( string -- string' )
173 dup "\r\n" intersects?
174 [ invalid-header-string ] when ;
176 : write-header ( key value -- )
177 [ validate-header write ]
178 [ ": " write validate-header encode-header write ] bi* crlf ;
180 : write-headers ( assoc -- )
181 [ write-header ] assoc-each ;
183 : message-id ( -- string )
190 smtp-domain get [ host-name ] unless* %
194 : extract-email ( recepient -- email )
195 ! This could be much smarter.
196 " " split1-last swap or "<" ?head drop ">" ?tail drop ;
198 : utf8-mime-header ( -- alist )
200 { "MIME-Version" "1.0" }
201 { "Content-Transfer-Encoding" "base64" }
202 { "Content-Type" "Text/plain; charset=utf-8" }
205 : email>headers ( email -- hashtable )
208 [ from>> "From" set ]
209 [ to>> ", " join "To" set ]
210 [ cc>> ", " join [ "Cc" set ] unless-empty ]
211 [ subject>> "Subject" set ]
213 now timestamp>rfc822 "Date" set
214 message-id "Message-Id" set
215 ] { } make-assoc utf8-mime-header append ;
217 : (send-email) ( headers email -- )
221 smtp-tls? get [ start-tls get-ok send-secure-handshake ] when
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
230 body>> send-body get-ok
232 ] with-smtp-connection ;
236 : send-email ( email -- )
237 [ email>headers ] keep (send-email) ;