]> gitweb.factorcode.org Git - factor.git/blob - basis/smtp/smtp.factor
Merge branch 'master' into new_ui
[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 ;
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 : crlf ( -- ) "\r\n" write ;
54
55 : read-crlf ( -- bytes )
56     "\r" read-until
57     [ CHAR: \r assert= read1 CHAR: \n assert= ] when* ;
58
59 : command ( string -- ) write crlf flush ;
60
61 \ command DEBUG add-input-logging
62
63 : helo ( -- ) "EHLO " host-name append command ;
64
65 : start-tls ( -- ) "STARTTLS" command ;
66
67 ERROR: bad-email-address email ;
68
69 : validate-address ( string -- string' )
70     #! Make sure we send funky stuff to the server by accident.
71     dup "\r\n>" intersect empty?
72     [ bad-email-address ] unless ;
73
74 : mail-from ( fromaddr -- )
75     validate-address
76     "MAIL FROM:<" ">" surround command ;
77
78 : rcpt-to ( to -- )
79     validate-address
80     "RCPT TO:<" ">" surround command ;
81
82 : data ( -- )
83     "DATA" command ;
84
85 ERROR: message-contains-dot message ;
86
87 M: message-contains-dot summary ( obj -- string )
88     drop "Message cannot contain . on a line by itself" ;
89
90 : validate-message ( msg -- msg' )
91     "." over member?
92     [ message-contains-dot ] when ;
93
94 : send-body ( body -- )
95     string-lines
96     validate-message
97     [ write crlf ] each
98     "." command ;
99
100 : quit ( -- )
101     "QUIT" command ;
102
103 LOG: smtp-response DEBUG
104
105 : multiline? ( response -- ? )
106     3 swap ?nth CHAR: - = ;
107
108 : (receive-response) ( -- )
109     read-crlf
110     [ , ]
111     [ smtp-response ]
112     [ multiline? [ (receive-response) ] when ]
113     tri ;
114
115 TUPLE: response code messages ;
116
117 : <response> ( lines -- response )
118     [ first 3 head string>number ] keep response boa ;
119
120 : receive-response ( -- response )
121     [ (receive-response) ] { } make <response> ;
122
123 ERROR: smtp-error response ;
124
125 M: smtp-error error.
126     "SMTP error (" write dup class pprint ")" print
127     response>> messages>> [ print ] each ;
128
129 ERROR: smtp-server-busy < smtp-error ;
130 ERROR: smtp-syntax-error < smtp-error ;
131 ERROR: smtp-command-not-implemented < smtp-error ;
132 ERROR: smtp-bad-authentication < smtp-error ;
133 ERROR: smtp-mailbox-unavailable < smtp-error ;
134 ERROR: smtp-user-not-local < smtp-error ;
135 ERROR: smtp-exceeded-storage-allocation < smtp-error ;
136 ERROR: smtp-bad-mailbox-name < smtp-error ;
137 ERROR: smtp-transaction-failed < smtp-error ;
138
139 : check-response ( response -- )
140     dup code>> {
141         { [ dup { 220 235 250 221 354 } member? ] [ 2drop ] }
142         { [ dup 400 499 between? ] [ drop smtp-server-busy ] }
143         { [ dup 500 = ] [ drop smtp-syntax-error ] }
144         { [ dup 501 = ] [ drop smtp-command-not-implemented ] }
145         { [ dup 500 509 between? ] [ drop smtp-syntax-error ] }
146         { [ dup 530 539 between? ] [ drop smtp-bad-authentication ] }
147         { [ dup 550 = ] [ drop smtp-mailbox-unavailable ] }
148         { [ dup 551 = ] [ drop smtp-user-not-local ] }
149         { [ dup 552 = ] [ drop smtp-exceeded-storage-allocation ] }
150         { [ dup 553 = ] [ drop smtp-bad-mailbox-name ] }
151         { [ dup 554 = ] [ drop smtp-transaction-failed ] }
152         [ drop smtp-error ]
153     } cond ;
154
155 : get-ok ( -- ) receive-response check-response ;
156
157 GENERIC: send-auth ( auth -- )
158
159 M: no-auth send-auth drop ;
160
161 : plain-auth-string ( username password -- string )
162     [ "\0" prepend ] bi@ append utf8 encode >base64 ;
163
164 M: plain-auth send-auth
165     [ username>> ] [ password>> ] bi plain-auth-string
166     "AUTH PLAIN " prepend command get-ok ;
167
168 : auth ( -- ) smtp-auth get send-auth ;
169
170 ERROR: invalid-header-string string ;
171
172 : validate-header ( string -- string' )
173     dup "\r\n" intersect empty?
174     [ invalid-header-string ] unless ;
175
176 : write-header ( key value -- )
177     [ validate-header write ]
178     [ ": " write validate-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 : email>headers ( email -- hashtable )
199     [
200         {
201             [ from>> "From" set ]
202             [ to>> ", " join "To" set ]
203             [ cc>> ", " join [ "Cc" set ] unless-empty ]
204             [ subject>> "Subject" set ]
205         } cleave
206         now timestamp>rfc822 "Date" set
207         message-id "Message-Id" set
208     ] { } make-assoc ;
209
210 : (send-email) ( headers email -- )
211     [
212         get-ok
213         helo get-ok
214         smtp-tls? get [ start-tls get-ok send-secure-handshake ] when
215         auth
216         dup from>> extract-email mail-from get-ok
217         dup to>> [ extract-email rcpt-to get-ok ] each
218         dup cc>> [ extract-email rcpt-to get-ok ] each
219         dup bcc>> [ extract-email rcpt-to get-ok ] each
220         data get-ok
221         swap write-headers
222         crlf
223         body>> send-body get-ok
224         quit get-ok
225     ] with-smtp-connection ;
226
227 PRIVATE>
228
229 : send-email ( email -- )
230     [ email>headers ] keep (send-email) ;