1 USING: accessors arrays assocs calendar calendar.english
2 calendar.format calendar.parser formatting grouping io io.crlf
3 io.encodings.ascii io.encodings.binary io.encodings.string
4 io.encodings.utf7 io.encodings.utf8 io.sockets io.sockets.secure
5 io.streams.duplex io.streams.string kernel math math.parser
6 multiline sequences sequences.extras splitting strings ;
10 ERROR: imap4-error ind data ;
12 CONSTANT: IMAP4_PORT 143
13 CONSTANT: IMAP4_SSL_PORT 993
15 ! Converts a timestamp to the format imap4 expects.
16 : timestamp>internal-date ( timestamp -- str )
20 DD "-" MONTH "-" YYYY " "
22 [ gmt-offset>> write-gmt-offset ]
24 ] with-string-writer ;
26 : internal-date>timestamp ( str -- timestamp )
29 "-" read-token checked-number
30 "-" read-token month-abbreviations index 1 +
31 read-sp checked-number spin
32 ! Hour, minute second and gmt offset.
33 read-hms " " expect readln parse-rfc822-gmt-offset <timestamp>
34 ] with-string-reader ;
38 : >utf7imap4 ( str -- str' )
39 utf7imap4 encode >string ;
41 : comma-list ( numbers -- str )
42 [ number>string ] map "," join ;
44 : check-status ( ind data -- )
45 over "OK" = not [ imap4-error ] [ 2drop ] if ;
47 : read-response-chunk ( stop-expr -- item ? )
48 read-?crlf ascii decode swap dupd pcre:findall
50 dup [[ ^.*{(\d+)}$]] pcre:findall
52 dup [[ ^\* (\d+) [A-Z-]+ (.*)$]] pcre:findall
53 [ ] [ nip first third second ] if-empty
56 ! Literal item to read, such as message body.
57 nip first second second string>number read ascii decode
61 [ nip first rest values f ] if-empty ;
63 : read-response ( tag -- lines )
64 "^%s (BAD|NO|OK) (.*)$" sprintf
65 '[ _ read-response-chunk ] loop>array*
66 unclip-last first2 [ check-status ] keep suffix ;
68 : write-command ( command literal tag -- )
70 [ "%s %s\r\n" sprintf ] [ length "%s %s {%d}\r\n" sprintf ] if-empty
71 ascii encode write flush
73 read-?crlf drop "\r\n" append write flush
76 : command-response ( command literal -- obj )
77 "ABCD" [ write-command ] [ read-response ] bi ;
80 : parse-items ( seq -- items )
81 first split-words 2 tail ;
83 : parse-list-folders ( str -- folder )
84 [[ \* LIST \(([^\)]+)\) "([^"]+)" "?([^"]+)"?]] pcre:findall
85 first rest values [ utf7imap4 decode ] map ;
87 : parse-select-folder ( seq -- count )
88 [ [[ \* (\d+) EXISTS]] pcre:findall ] map harvest
89 [ f ] [ first first last last string>number ] if-empty ;
91 ! Returns uid if the server supports the UIDPLUS extension.
92 : parse-append-mail ( seq -- uid/f )
93 [ [=[ \[APPENDUID (\d+) \d+\]]=] pcre:findall ] map harvest
94 [ f ] [ first first last last string>number ] if-empty ;
96 : parse-status ( seq -- assoc )
97 first [[ \* STATUS "[^"]+" \(([^\)]+)\)]] pcre:findall first last last
98 split-words 2 group [ string>number ] assoc-map ;
100 : parse-store-mail-line ( str -- pair/f )
101 [[ \(FLAGS \(([^\)]+)\) UID (\d+)\)]] pcre:findall [ f ] [
102 first rest values first2 [ split-words ] dip string>number swap 2array
105 : parse-store-mail ( seq -- assoc )
106 but-last [ parse-store-mail-line ] map sift ;
111 : <imap4ssl> ( host -- imap4 )
112 IMAP4_SSL_PORT <inet> f <secure> binary <client> drop
113 ! Read the useless welcome message.
114 dup [ "\\*" read-response drop ] with-stream* ;
117 : capabilities ( -- caps )
118 "CAPABILITY" "" command-response parse-items ;
120 : login ( username password -- caps )
121 "LOGIN %s \"%s\"" sprintf "" command-response parse-items ;
124 : list-folders ( directory -- folders )
125 "LIST \"%s\" *" sprintf "" command-response
126 but-last [ parse-list-folders ] map ;
128 : list-all-folders ( -- folders ) "" list-folders ;
130 : select-folder ( mailbox -- count )
131 >utf7imap4 "SELECT \"%s\"" sprintf "" command-response
132 parse-select-folder ;
134 : create-folder ( mailbox -- )
135 >utf7imap4 "CREATE \"%s\"" sprintf "" command-response
138 : delete-folder ( mailbox -- )
139 >utf7imap4 "DELETE \"%s\"" sprintf "" command-response
142 : rename-folder ( old-name new-name -- )
143 [ >utf7imap4 ] bi@ "RENAME \"%s\" \"%s\"" sprintf "" command-response
146 : status-folder ( mailbox keys -- assoc )
147 [ >utf7imap4 ] dip join-words "STATUS \"%s\" (%s)" sprintf
148 "" command-response parse-status ;
150 : close-folder ( -- )
151 "CLOSE" "" command-response drop ;
154 : search-mails ( data-spec str -- uids )
155 [ "UID SEARCH CHARSET UTF-8 %s" sprintf ] dip utf8 encode
156 command-response parse-items [ string>number ] map ;
158 : fetch-mails ( uids data-spec -- texts )
159 [ comma-list ] dip "UID FETCH %s %s" sprintf "" command-response but-last ;
161 : copy-mails ( uids mailbox -- )
162 [ comma-list ] dip >utf7imap4 "UID COPY %s \"%s\"" sprintf ""
163 command-response drop ;
165 : append-mail ( mailbox flags date-time mail -- uid/f )
168 [ [ "" ] [ " " append ] if-empty ]
169 [ timestamp>internal-date ] tri*
170 "APPEND \"%s\" %s\"%s\"" sprintf
171 ] dip utf8 encode command-response parse-append-mail ;
173 : store-mail ( uids command flags -- mail-flags )
174 [ comma-list ] 2dip "UID STORE %s %s %s" sprintf
180 : with-imap ( host email password quot -- )
181 [ <imap4ssl> ] 3dip '[ _ _ login drop @ ] with-stream ; inline
183 TUPLE: imap-settings host email password ;
185 : <imap-settings> ( host email password -- obj )
191 : with-imap-settings ( imap-settings quot -- )
192 [ [ host>> ] [ email>> ] [ password>> ] tri ] dip with-imap ; inline