Commit | Line | Data |
---|---|---|
20a673b2 | 1 | ;;; nnimap.el --- IMAP interface for Gnus |
e84b4b86 | 2 | |
20a673b2 | 3 | ;; Copyright (C) 2010 Free Software Foundation, Inc. |
c113de23 | 4 | |
20a673b2 KY |
5 | ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org> |
6 | ;; Simon Josefsson <simon@josefsson.org> | |
c113de23 GM |
7 | |
8 | ;; This file is part of GNU Emacs. | |
9 | ||
5e809f55 | 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
c113de23 | 11 | ;; it under the terms of the GNU General Public License as published by |
5e809f55 GM |
12 | ;; the Free Software Foundation, either version 3 of the License, or |
13 | ;; (at your option) any later version. | |
c113de23 GM |
14 | |
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
5e809f55 | 17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
c113de23 GM |
18 | ;; GNU General Public License for more details. |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
5e809f55 | 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
c113de23 GM |
22 | |
23 | ;;; Commentary: | |
24 | ||
20a673b2 | 25 | ;; nnimap interfaces Gnus with IMAP servers. |
c113de23 GM |
26 | |
27 | ;;; Code: | |
28 | ||
f0b7f5a8 KY |
29 | ;; For Emacs <22.2 and XEmacs. |
30 | (eval-and-compile | |
31 | (unless (fboundp 'declare-function) (defmacro declare-function (&rest r)))) | |
32 | ||
aa8f8277 | 33 | (eval-and-compile |
20a673b2 | 34 | (require 'nnheader)) |
aa8f8277 | 35 | |
20a673b2 KY |
36 | (eval-when-compile |
37 | (require 'cl)) | |
c113de23 | 38 | |
f58208b1 LMI |
39 | (require 'nnheader) |
40 | (require 'gnus-util) | |
41 | (require 'gnus) | |
42 | (require 'nnoo) | |
20a673b2 | 43 | (require 'netrc) |
14db1c41 | 44 | (require 'utf7) |
6b958814 | 45 | (require 'tls) |
0617bb00 | 46 | (require 'parse-time) |
e952b711 | 47 | |
635be05a KY |
48 | (autoload 'auth-source-forget-user-or-password "auth-source") |
49 | (autoload 'auth-source-user-or-password "auth-source") | |
50 | ||
c113de23 GM |
51 | (nnoo-declare nnimap) |
52 | ||
c113de23 | 53 | (defvoo nnimap-address nil |
20a673b2 | 54 | "The address of the IMAP server.") |
c113de23 GM |
55 | |
56 | (defvoo nnimap-server-port nil | |
20a673b2 KY |
57 | "The IMAP port used. |
58 | If nnimap-stream is `ssl', this will default to `imaps'. If not, | |
59 | it will default to `imap'.") | |
60 | ||
61 | (defvoo nnimap-stream 'ssl | |
62 | "How nnimap will talk to the IMAP server. | |
b1ae92ba | 63 | Values are `ssl', `network', `starttls' or `shell'.") |
20a673b2 KY |
64 | |
65 | (defvoo nnimap-shell-program (if (boundp 'imap-shell-program) | |
66 | (if (listp imap-shell-program) | |
67 | (car imap-shell-program) | |
68 | imap-shell-program) | |
69 | "ssh %s imapd")) | |
70 | ||
71 | (defvoo nnimap-inbox nil | |
72 | "The mail box where incoming mail arrives and should be split out of.") | |
73 | ||
8ccbef23 G |
74 | (defvoo nnimap-split-methods nil |
75 | "How mail is split. | |
76 | Uses the same syntax as nnmail-split-methods") | |
77 | ||
6b958814 G |
78 | (defvoo nnimap-split-fancy nil |
79 | "Uses the same syntax as nnmail-split-fancy.") | |
80 | ||
229b59da | 81 | (make-obsolete-variable 'nnimap-split-rule "see `nnimap-split-methods'" |
6b958814 | 82 | "Emacs 24.1") |
229b59da | 83 | |
bdaa75c7 LMI |
84 | (defvoo nnimap-authenticator nil |
85 | "How nnimap authenticate itself to the server. | |
86 | Possible choices are nil (use default methods) or `anonymous'.") | |
87 | ||
b069e5a6 G |
88 | (defvoo nnimap-expunge t |
89 | "If non-nil, expunge articles after deleting them. | |
90 | This is always done if the server supports UID EXPUNGE, but it's | |
91 | not done by default on servers that doesn't support that command.") | |
92 | ||
8ccbef23 G |
93 | (defvoo nnimap-streaming t |
94 | "If non-nil, try to use streaming commands with IMAP servers. | |
95 | Switching this off will make nnimap slower, but it helps with | |
96 | some servers.") | |
0617bb00 | 97 | |
20a673b2 | 98 | (defvoo nnimap-connection-alist nil) |
286c4fc2 LMI |
99 | |
100 | (defvoo nnimap-current-infos nil) | |
101 | ||
9f2d52e7 G |
102 | (defvoo nnimap-fetch-partial-articles nil |
103 | "If non-nil, Gnus will fetch partial articles. | |
104 | If t, nnimap will fetch only the first part. If a string, it | |
105 | will fetch all parts that have types that match that string. A | |
106 | likely value would be \"text/\" to automatically fetch all | |
107 | textual parts.") | |
108 | ||
20a673b2 KY |
109 | (defvar nnimap-process nil) |
110 | ||
111 | (defvar nnimap-status-string "") | |
23f87bed MB |
112 | |
113 | (defvar nnimap-split-download-body-default nil | |
114 | "Internal variable with default value for `nnimap-split-download-body'.") | |
115 | ||
61b1af82 G |
116 | (defvar nnimap-keepalive-timer nil) |
117 | (defvar nnimap-process-buffers nil) | |
118 | ||
20a673b2 | 119 | (defstruct nnimap |
61b1af82 | 120 | group process commands capabilities select-result newlinep server |
4478e074 | 121 | last-command-time greeting) |
c113de23 | 122 | |
20a673b2 KY |
123 | (defvar nnimap-object nil) |
124 | ||
125 | (defvar nnimap-mark-alist | |
b069e5a6 G |
126 | '((read "\\Seen" %Seen) |
127 | (tick "\\Flagged" %Flagged) | |
128 | (reply "\\Answered" %Answered) | |
20a673b2 KY |
129 | (expire "gnus-expire") |
130 | (dormant "gnus-dormant") | |
131 | (score "gnus-score") | |
132 | (save "gnus-save") | |
133 | (download "gnus-download") | |
134 | (forward "gnus-forward"))) | |
135 | ||
20a673b2 KY |
136 | (defun nnimap-buffer () |
137 | (nnimap-find-process-buffer nntp-server-buffer)) | |
138 | ||
286c4fc2 | 139 | (deffoo nnimap-retrieve-headers (articles &optional group server fetch-old) |
c113de23 | 140 | (with-current-buffer nntp-server-buffer |
20a673b2 KY |
141 | (erase-buffer) |
142 | (when (nnimap-possibly-change-group group server) | |
143 | (with-current-buffer (nnimap-buffer) | |
20a673b2 KY |
144 | (erase-buffer) |
145 | (nnimap-wait-for-response | |
146 | (nnimap-send-command | |
147 | "UID FETCH %s %s" | |
148 | (nnimap-article-ranges (gnus-compress-sequence articles)) | |
149 | (format "(UID RFC822.SIZE BODYSTRUCTURE %s)" | |
150 | (format | |
8ccbef23 | 151 | (if (nnimap-ver4-p) |
20a673b2 KY |
152 | "BODY.PEEK[HEADER.FIELDS %s]" |
153 | "RFC822.HEADER.LINES %s") | |
154 | (append '(Subject From Date Message-Id | |
155 | References In-Reply-To Xref) | |
156 | nnmail-extra-headers)))) | |
157 | t) | |
158 | (nnimap-transform-headers)) | |
159 | (insert-buffer-substring | |
160 | (nnimap-find-process-buffer (current-buffer)))) | |
b1ae92ba | 161 | 'headers)) |
20a673b2 KY |
162 | |
163 | (defun nnimap-transform-headers () | |
164 | (goto-char (point-min)) | |
b1ae92ba | 165 | (let (article bytes lines size string) |
20a673b2 KY |
166 | (block nil |
167 | (while (not (eobp)) | |
168 | (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)")) | |
169 | (delete-region (point) (progn (forward-line 1) (point))) | |
170 | (when (eobp) | |
171 | (return))) | |
b1ae92ba G |
172 | (setq article (match-string 1)) |
173 | ;; Unfold quoted {number} strings. | |
174 | (while (re-search-forward "[^]] {\\([0-9]+\\)}\r\n" | |
175 | (1+ (line-end-position)) t) | |
176 | (setq size (string-to-number (match-string 1))) | |
177 | (delete-region (+ (match-beginning 0) 2) (point)) | |
178 | (setq string (delete-region (point) (+ (point) size))) | |
179 | (insert (format "%S" string))) | |
180 | (setq bytes (nnimap-get-length) | |
20a673b2 KY |
181 | lines nil) |
182 | (beginning-of-line) | |
a46359d4 LMI |
183 | (setq size |
184 | (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)" | |
185 | (line-end-position) | |
186 | t) | |
187 | (match-string 1))) | |
188 | (beginning-of-line) | |
20a673b2 | 189 | (when (search-forward "BODYSTRUCTURE" (line-end-position) t) |
b1ae92ba G |
190 | (let ((structure (ignore-errors |
191 | (read (current-buffer))))) | |
20a673b2 KY |
192 | (while (and (consp structure) |
193 | (not (stringp (car structure)))) | |
194 | (setq structure (car structure))) | |
195 | (setq lines (nth 7 structure)))) | |
196 | (delete-region (line-beginning-position) (line-end-position)) | |
197 | (insert (format "211 %s Article retrieved." article)) | |
198 | (forward-line 1) | |
a46359d4 LMI |
199 | (when size |
200 | (insert (format "Chars: %s\n" size))) | |
20a673b2 KY |
201 | (when lines |
202 | (insert (format "Lines: %s\n" lines))) | |
203 | (re-search-forward "^\r$") | |
204 | (delete-region (line-beginning-position) (line-end-position)) | |
205 | (insert ".") | |
206 | (forward-line 1))))) | |
207 | ||
208 | (defun nnimap-get-length () | |
209 | (and (re-search-forward "{\\([0-9]+\\)}" (line-end-position) t) | |
210 | (string-to-number (match-string 1)))) | |
211 | ||
212 | (defun nnimap-article-ranges (ranges) | |
213 | (let (result) | |
214 | (cond | |
215 | ((numberp ranges) | |
216 | (number-to-string ranges)) | |
217 | ((numberp (cdr ranges)) | |
218 | (format "%d:%d" (car ranges) (cdr ranges))) | |
219 | (t | |
220 | (dolist (elem ranges) | |
221 | (push | |
222 | (if (consp elem) | |
223 | (format "%d:%d" (car elem) (cdr elem)) | |
224 | (number-to-string elem)) | |
225 | result)) | |
226 | (mapconcat #'identity (nreverse result) ","))))) | |
227 | ||
286c4fc2 | 228 | (deffoo nnimap-open-server (server &optional defs) |
c113de23 GM |
229 | (if (nnimap-server-opened server) |
230 | t | |
c113de23 | 231 | (unless (assq 'nnimap-address defs) |
20a673b2 | 232 | (setq defs (append defs (list (list 'nnimap-address server))))) |
c113de23 | 233 | (nnoo-change-server 'nnimap server defs) |
20a673b2 KY |
234 | (or (nnimap-find-connection nntp-server-buffer) |
235 | (nnimap-open-connection nntp-server-buffer)))) | |
236 | ||
237 | (defun nnimap-make-process-buffer (buffer) | |
238 | (with-current-buffer | |
239 | (generate-new-buffer (format "*nnimap %s %s %s*" | |
240 | nnimap-address nnimap-server-port | |
241 | (gnus-buffer-exists-p buffer))) | |
242 | (mm-disable-multibyte) | |
243 | (buffer-disable-undo) | |
244 | (gnus-add-buffer) | |
245 | (set (make-local-variable 'after-change-functions) nil) | |
b069e5a6 G |
246 | (set (make-local-variable 'nnimap-object) |
247 | (make-nnimap :server (nnoo-current-server 'nnimap))) | |
20a673b2 | 248 | (push (list buffer (current-buffer)) nnimap-connection-alist) |
61b1af82 | 249 | (push (current-buffer) nnimap-process-buffers) |
20a673b2 KY |
250 | (current-buffer))) |
251 | ||
252 | (defun nnimap-open-shell-stream (name buffer host port) | |
e8861cd2 LMI |
253 | (let ((process-connection-type nil)) |
254 | (start-process name buffer shell-file-name | |
255 | shell-command-switch | |
256 | (format-spec | |
257 | nnimap-shell-program | |
258 | (format-spec-make | |
259 | ?s host | |
260 | ?p port))))) | |
20a673b2 | 261 | |
2696d88f | 262 | (defun nnimap-credentials (address ports &optional inhibit-create) |
286c4fc2 LMI |
263 | (let (port credentials) |
264 | ;; Request the credentials from all ports, but only query on the | |
265 | ;; last port if all the previous ones have failed. | |
266 | (while (and (null credentials) | |
267 | (setq port (pop ports))) | |
268 | (setq credentials | |
269 | (auth-source-user-or-password | |
2696d88f G |
270 | '("login" "password") address port nil |
271 | (if inhibit-create | |
272 | nil | |
273 | (null ports))))) | |
286c4fc2 LMI |
274 | credentials)) |
275 | ||
61b1af82 G |
276 | (defun nnimap-keepalive () |
277 | (let ((now (current-time))) | |
278 | (dolist (buffer nnimap-process-buffers) | |
279 | (when (buffer-name buffer) | |
280 | (with-current-buffer buffer | |
281 | (when (and nnimap-object | |
282 | (nnimap-last-command-time nnimap-object) | |
283 | (> (time-to-seconds | |
284 | (time-subtract | |
285 | now | |
286 | (nnimap-last-command-time nnimap-object))) | |
287 | ;; More than five minutes since the last command. | |
288 | (* 5 60))) | |
289 | (nnimap-send-command "NOOP"))))))) | |
290 | ||
f0b7f5a8 KY |
291 | (declare-function gnutls-negotiate "subr" (fn file &optional arglist fileonly)) |
292 | ||
20a673b2 | 293 | (defun nnimap-open-connection (buffer) |
61b1af82 G |
294 | (unless nnimap-keepalive-timer |
295 | (setq nnimap-keepalive-timer (run-at-time (* 60 15) (* 60 15) | |
296 | 'nnimap-keepalive))) | |
9f2d52e7 G |
297 | (block nil |
298 | (with-current-buffer (nnimap-make-process-buffer buffer) | |
299 | (let* ((coding-system-for-read 'binary) | |
300 | (coding-system-for-write 'binary) | |
301 | (port nil) | |
302 | (ports | |
303 | (cond | |
dab0271f G |
304 | ((or (eq nnimap-stream 'network) |
305 | (and (eq nnimap-stream 'starttls) | |
306 | (fboundp 'open-gnutls-stream))) | |
283f7b93 G |
307 | (nnheader-message 7 "Opening connection to %s..." |
308 | nnimap-address) | |
9f2d52e7 G |
309 | (open-network-stream |
310 | "*nnimap*" (current-buffer) nnimap-address | |
311 | (setq port | |
312 | (or nnimap-server-port | |
313 | (if (netrc-find-service-number "imap") | |
314 | "imap" | |
315 | "143")))) | |
316 | '("143" "imap")) | |
317 | ((eq nnimap-stream 'shell) | |
283f7b93 G |
318 | (nnheader-message 7 "Opening connection to %s via shell..." |
319 | nnimap-address) | |
9f2d52e7 G |
320 | (nnimap-open-shell-stream |
321 | "*nnimap*" (current-buffer) nnimap-address | |
322 | (setq port (or nnimap-server-port "imap"))) | |
323 | '("imap")) | |
324 | ((eq nnimap-stream 'starttls) | |
283f7b93 | 325 | (nnheader-message 7 "Opening connection to %s via starttls..." |
030158f3 | 326 | nnimap-address) |
0d2d1bdc | 327 | (let ((tls-program |
030158f3 | 328 | '("openssl s_client -connect %h:%p -no_ssl2 -ign_eof -starttls imap"))) |
6b958814 G |
329 | (open-tls-stream |
330 | "*nnimap*" (current-buffer) nnimap-address | |
a1d16a7b | 331 | (setq port (or nnimap-server-port "imap")))) |
9f2d52e7 | 332 | '("imap")) |
71e691a5 | 333 | ((memq nnimap-stream '(ssl tls)) |
283f7b93 G |
334 | (nnheader-message 7 "Opening connection to %s via tls..." |
335 | nnimap-address) | |
f7aa248a | 336 | (funcall (if (fboundp 'open-gnutls-stream) |
66627fa9 G |
337 | 'open-gnutls-stream |
338 | 'open-tls-stream) | |
339 | "*nnimap*" (current-buffer) nnimap-address | |
340 | (setq port | |
341 | (or nnimap-server-port | |
342 | (if (netrc-find-service-number "imaps") | |
343 | "imaps" | |
344 | "993")))) | |
71e691a5 G |
345 | '("143" "993" "imap" "imaps")) |
346 | (t | |
347 | (error "Unknown stream type: %s" nnimap-stream)))) | |
9f2d52e7 G |
348 | connection-result login-result credentials) |
349 | (setf (nnimap-process nnimap-object) | |
350 | (get-buffer-process (current-buffer))) | |
351 | (if (not (and (nnimap-process nnimap-object) | |
352 | (memq (process-status (nnimap-process nnimap-object)) | |
353 | '(open run)))) | |
354 | (nnheader-report 'nnimap "Unable to contact %s:%s via %s" | |
355 | nnimap-address port nnimap-stream) | |
f7aa248a G |
356 | (gnus-set-process-query-on-exit-flag |
357 | (nnimap-process nnimap-object) nil) | |
9f2d52e7 G |
358 | (if (not (setq connection-result (nnimap-wait-for-connection))) |
359 | (nnheader-report 'nnimap | |
360 | "%s" (buffer-substring | |
361 | (point) (line-end-position))) | |
362 | ;; Store the greeting (for debugging purposes). | |
363 | (setf (nnimap-greeting nnimap-object) | |
364 | (buffer-substring (line-beginning-position) | |
365 | (line-end-position))) | |
366 | ;; Store the capabilities. | |
20a673b2 KY |
367 | (setf (nnimap-capabilities nnimap-object) |
368 | (mapcar | |
369 | #'upcase | |
9f2d52e7 G |
370 | (nnimap-find-parameter |
371 | "CAPABILITY" (cdr (nnimap-command "CAPABILITY"))))) | |
9f2d52e7 G |
372 | (when nnimap-server-port |
373 | (push (format "%s" nnimap-server-port) ports)) | |
6b958814 G |
374 | ;; If this is a STARTTLS-capable server, then sever the |
375 | ;; connection and start a STARTTLS connection instead. | |
dab0271f G |
376 | (cond |
377 | ((and (or (and (eq nnimap-stream 'network) | |
378 | (member "STARTTLS" | |
379 | (nnimap-capabilities nnimap-object))) | |
380 | (eq nnimap-stream 'starttls)) | |
381 | (fboundp 'open-gnutls-stream)) | |
382 | (nnimap-command "STARTTLS") | |
383 | (gnutls-negotiate (nnimap-process nnimap-object) nil)) | |
384 | ((and (eq nnimap-stream 'network) | |
385 | (member "STARTTLS" (nnimap-capabilities nnimap-object))) | |
6b958814 G |
386 | (let ((nnimap-stream 'starttls)) |
387 | (let ((tls-process | |
388 | (nnimap-open-connection buffer))) | |
389 | ;; If the STARTTLS connection was successful, we | |
390 | ;; kill our first non-encrypted connection. If it | |
391 | ;; wasn't successful, we just use our unencrypted | |
392 | ;; connection. | |
393 | (when (memq (process-status tls-process) '(open run)) | |
394 | (delete-process (nnimap-process nnimap-object)) | |
395 | (kill-buffer (current-buffer)) | |
dab0271f | 396 | (return tls-process)))))) |
9f2d52e7 G |
397 | (unless (equal connection-result "PREAUTH") |
398 | (if (not (setq credentials | |
399 | (if (eq nnimap-authenticator 'anonymous) | |
400 | (list "anonymous" | |
401 | (message-make-address)) | |
402 | (or | |
403 | ;; First look for the credentials based | |
404 | ;; on the virtual server name. | |
405 | (nnimap-credentials | |
406 | (nnoo-current-server 'nnimap) ports t) | |
407 | ;; Then look them up based on the | |
408 | ;; physical address. | |
409 | (nnimap-credentials nnimap-address ports))))) | |
410 | (setq nnimap-object nil) | |
411 | (setq login-result (nnimap-command "LOGIN %S %S" | |
412 | (car credentials) | |
413 | (cadr credentials))) | |
414 | (unless (car login-result) | |
415 | ;; If the login failed, then forget the credentials | |
416 | ;; that are now possibly cached. | |
417 | (dolist (host (list (nnoo-current-server 'nnimap) | |
418 | nnimap-address)) | |
419 | (dolist (port ports) | |
420 | (dolist (element '("login" "password")) | |
421 | (auth-source-forget-user-or-password | |
422 | element host port)))) | |
423 | (delete-process (nnimap-process nnimap-object)) | |
424 | (setq nnimap-object nil)))) | |
425 | (when nnimap-object | |
426 | (when (member "QRESYNC" (nnimap-capabilities nnimap-object)) | |
427 | (nnimap-command "ENABLE QRESYNC")) | |
6b958814 G |
428 | (nnimap-process nnimap-object)))))))) |
429 | ||
20a673b2 KY |
430 | (defun nnimap-find-parameter (parameter elems) |
431 | (let (result) | |
432 | (dolist (elem elems) | |
433 | (cond | |
434 | ((equal (car elem) parameter) | |
435 | (setq result (cdr elem))) | |
436 | ((and (equal (car elem) "OK") | |
437 | (consp (cadr elem)) | |
438 | (equal (caadr elem) parameter)) | |
439 | (setq result (cdr (cadr elem)))))) | |
440 | result)) | |
441 | ||
286c4fc2 | 442 | (deffoo nnimap-close-server (&optional server) |
71e691a5 G |
443 | (when (nnoo-change-server 'nnimap server nil) |
444 | (ignore-errors | |
445 | (delete-process (get-buffer-process (nnimap-buffer)))) | |
d1090fe8 | 446 | (nnoo-close-server 'nnimap server) |
71e691a5 | 447 | t)) |
c113de23 | 448 | |
286c4fc2 | 449 | (deffoo nnimap-request-close () |
20a673b2 | 450 | t) |
23f87bed | 451 | |
286c4fc2 | 452 | (deffoo nnimap-server-opened (&optional server) |
20a673b2 KY |
453 | (and (nnoo-current-server-p 'nnimap server) |
454 | nntp-server-buffer | |
455 | (gnus-buffer-live-p nntp-server-buffer) | |
456 | (nnimap-find-connection nntp-server-buffer))) | |
c113de23 | 457 | |
286c4fc2 | 458 | (deffoo nnimap-status-message (&optional server) |
20a673b2 | 459 | nnimap-status-string) |
c113de23 | 460 | |
286c4fc2 | 461 | (deffoo nnimap-request-article (article &optional group server to-buffer) |
c113de23 | 462 | (with-current-buffer nntp-server-buffer |
bdaa75c7 | 463 | (let ((result (nnimap-possibly-change-group group server)) |
8ccbef23 | 464 | parts structure) |
20a673b2 KY |
465 | (when (stringp article) |
466 | (setq article (nnimap-find-article-by-message-id group article))) | |
467 | (when (and result | |
468 | article) | |
469 | (erase-buffer) | |
470 | (with-current-buffer (nnimap-buffer) | |
471 | (erase-buffer) | |
9f2d52e7 G |
472 | (when nnimap-fetch-partial-articles |
473 | (nnimap-command "UID FETCH %d (BODYSTRUCTURE)" article) | |
474 | (goto-char (point-min)) | |
475 | (when (re-search-forward "FETCH.*BODYSTRUCTURE" nil t) | |
229b59da G |
476 | (setq structure (ignore-errors |
477 | (let ((start (point))) | |
478 | (forward-sexp 1) | |
479 | (downcase-region start (point)) | |
480 | (goto-char (point)) | |
481 | (read (current-buffer)))) | |
9f2d52e7 | 482 | parts (nnimap-find-wanted-parts structure)))) |
8ccbef23 G |
483 | (when (if parts |
484 | (nnimap-get-partial-article article parts structure) | |
485 | (nnimap-get-whole-article article)) | |
486 | (let ((buffer (current-buffer))) | |
487 | (with-current-buffer (or to-buffer nntp-server-buffer) | |
488 | (erase-buffer) | |
489 | (insert-buffer-substring buffer) | |
490 | (nnheader-ms-strip-cr) | |
491 | (cons group article))))))))) | |
492 | ||
493 | (defun nnimap-get-whole-article (article) | |
494 | (let ((result | |
495 | (nnimap-command | |
496 | (if (nnimap-ver4-p) | |
497 | "UID FETCH %d BODY.PEEK[]" | |
498 | "UID FETCH %d RFC822.PEEK") | |
499 | article))) | |
500 | ;; Check that we really got an article. | |
501 | (goto-char (point-min)) | |
4478e074 | 502 | (unless (re-search-forward "\\* [0-9]+ FETCH" nil t) |
8ccbef23 G |
503 | (setq result nil)) |
504 | (when result | |
4478e074 G |
505 | ;; Remove any data that may have arrived before the FETCH data. |
506 | (beginning-of-line) | |
507 | (unless (bobp) | |
508 | (delete-region (point-min) (point))) | |
8ccbef23 G |
509 | (let ((bytes (nnimap-get-length))) |
510 | (delete-region (line-beginning-position) | |
511 | (progn (forward-line 1) (point))) | |
512 | (goto-char (+ (point) bytes)) | |
513 | (delete-region (point) (point-max))) | |
514 | t))) | |
515 | ||
516 | (defun nnimap-ver4-p () | |
517 | (member "IMAP4REV1" (nnimap-capabilities nnimap-object))) | |
518 | ||
519 | (defun nnimap-get-partial-article (article parts structure) | |
520 | (let ((result | |
521 | (nnimap-command | |
522 | "UID FETCH %d (%s %s)" | |
523 | article | |
524 | (if (nnimap-ver4-p) | |
525 | "BODY.PEEK[HEADER]" | |
526 | "RFC822.HEADER") | |
527 | (if (nnimap-ver4-p) | |
528 | (mapconcat (lambda (part) | |
529 | (format "BODY.PEEK[%s]" part)) | |
530 | parts " ") | |
531 | (mapconcat (lambda (part) | |
532 | (format "RFC822.PEEK[%s]" part)) | |
533 | parts " "))))) | |
534 | (when result | |
535 | (nnimap-convert-partial-article structure)))) | |
536 | ||
537 | (defun nnimap-convert-partial-article (structure) | |
538 | ;; First just skip past the headers. | |
539 | (goto-char (point-min)) | |
540 | (let ((bytes (nnimap-get-length)) | |
541 | id parts) | |
542 | ;; Delete "FETCH" line. | |
543 | (delete-region (line-beginning-position) | |
544 | (progn (forward-line 1) (point))) | |
545 | (goto-char (+ (point) bytes)) | |
546 | ;; Collect all the body parts. | |
547 | (while (looking-at ".*BODY\\[\\([.0-9]+\\)\\]") | |
548 | (setq id (match-string 1) | |
549 | bytes (nnimap-get-length)) | |
550 | (beginning-of-line) | |
551 | (delete-region (point) (progn (forward-line 1) (point))) | |
552 | (push (list id (buffer-substring (point) (+ (point) bytes))) | |
553 | parts) | |
554 | (delete-region (point) (+ (point) bytes))) | |
555 | ;; Delete trailing junk. | |
556 | (delete-region (point) (point-max)) | |
557 | ;; Now insert all the parts again where they fit in the structure. | |
558 | (nnimap-insert-partial-structure structure parts) | |
559 | t)) | |
560 | ||
561 | (defun nnimap-insert-partial-structure (structure parts &optional subp) | |
229b59da G |
562 | (let (type boundary) |
563 | (let ((bstruc structure)) | |
564 | (while (consp (car bstruc)) | |
565 | (pop bstruc)) | |
566 | (setq type (car bstruc)) | |
567 | (setq bstruc (car (cdr bstruc))) | |
568 | (when (and (stringp (car bstruc)) | |
569 | (string= (downcase (car bstruc)) "boundary")) | |
570 | (setq boundary (cadr bstruc)))) | |
8ccbef23 G |
571 | (when subp |
572 | (insert (format "Content-type: multipart/%s; boundary=%S\n\n" | |
573 | (downcase type) boundary))) | |
574 | (while (not (stringp (car structure))) | |
575 | (insert "\n--" boundary "\n") | |
576 | (if (consp (caar structure)) | |
577 | (nnimap-insert-partial-structure (pop structure) parts t) | |
578 | (let ((bit (pop structure))) | |
579 | (insert (format "Content-type: %s/%s" | |
580 | (downcase (nth 0 bit)) | |
581 | (downcase (nth 1 bit)))) | |
582 | (if (member "CHARSET" (nth 2 bit)) | |
583 | (insert (format | |
584 | "; charset=%S\n" (cadr (member "CHARSET" (nth 2 bit))))) | |
585 | (insert "\n")) | |
586 | (insert (format "Content-transfer-encoding: %s\n" | |
587 | (nth 5 bit))) | |
588 | (insert "\n") | |
589 | (when (assoc (nth 9 bit) parts) | |
590 | (insert (cadr (assoc (nth 9 bit) parts))))))) | |
591 | (insert "\n--" boundary "--\n"))) | |
bdaa75c7 LMI |
592 | |
593 | (defun nnimap-find-wanted-parts (structure) | |
594 | (message-flatten-list (nnimap-find-wanted-parts-1 structure ""))) | |
595 | ||
596 | (defun nnimap-find-wanted-parts-1 (structure prefix) | |
597 | (let ((num 1) | |
598 | parts) | |
599 | (while (consp (car structure)) | |
600 | (let ((sub (pop structure))) | |
601 | (if (consp (car sub)) | |
602 | (push (nnimap-find-wanted-parts-1 | |
603 | sub (if (string= prefix "") | |
604 | (number-to-string num) | |
605 | (format "%s.%s" prefix num))) | |
606 | parts) | |
8ccbef23 G |
607 | (let ((type (format "%s/%s" (nth 0 sub) (nth 1 sub))) |
608 | (id (if (string= prefix "") | |
bdaa75c7 | 609 | (number-to-string num) |
8ccbef23 G |
610 | (format "%s.%s" prefix num)))) |
611 | (setcar (nthcdr 9 sub) id) | |
9f2d52e7 G |
612 | (when (if (eq nnimap-fetch-partial-articles t) |
613 | (equal id "1") | |
614 | (string-match nnimap-fetch-partial-articles type)) | |
8ccbef23 G |
615 | (push id parts)))) |
616 | (incf num))) | |
bdaa75c7 | 617 | (nreverse parts))) |
20a673b2 | 618 | |
286c4fc2 | 619 | (deffoo nnimap-request-group (group &optional server dont-check info) |
7cad71ad G |
620 | (let ((result (nnimap-possibly-change-group |
621 | ;; Don't SELECT the group if we're going to select it | |
622 | ;; later, anyway. | |
623 | (if dont-check | |
624 | nil | |
625 | group) | |
626 | server)) | |
a46359d4 LMI |
627 | articles active marks high low) |
628 | (with-current-buffer nntp-server-buffer | |
20a673b2 | 629 | (when result |
286c4fc2 LMI |
630 | (if (and dont-check |
631 | (setq active (nth 2 (assoc group nnimap-current-infos)))) | |
632 | (insert (format "211 %d %d %d %S\n" | |
633 | (- (cdr active) (car active)) | |
634 | (car active) | |
635 | (cdr active) | |
636 | group)) | |
637 | (with-current-buffer (nnimap-buffer) | |
638 | (erase-buffer) | |
639 | (let ((group-sequence | |
0617bb00 | 640 | (nnimap-send-command "SELECT %S" (utf7-encode group t))) |
286c4fc2 LMI |
641 | (flag-sequence |
642 | (nnimap-send-command "UID FETCH 1:* FLAGS"))) | |
7cad71ad | 643 | (setf (nnimap-group nnimap-object) group) |
286c4fc2 LMI |
644 | (nnimap-wait-for-response flag-sequence) |
645 | (setq marks | |
646 | (nnimap-flags-to-marks | |
647 | (nnimap-parse-flags | |
f7aa248a G |
648 | (list (list group-sequence flag-sequence |
649 | 1 group "SELECT"))))) | |
650 | (when (and info | |
651 | marks) | |
286c4fc2 LMI |
652 | (nnimap-update-infos marks (list info))) |
653 | (goto-char (point-max)) | |
b1ae92ba | 654 | (let ((uidnext (nth 5 (car marks)))) |
a3f57c41 G |
655 | (setq high (or (if uidnext |
656 | (1- uidnext) | |
657 | (nth 3 (car marks))) | |
658 | 0) | |
659 | low (or (nth 4 (car marks)) uidnext 1))))) | |
286c4fc2 LMI |
660 | (erase-buffer) |
661 | (insert | |
662 | (format | |
0617bb00 LMI |
663 | "211 %d %d %d %S\n" (1+ (- high low)) low high group))) |
664 | t)))) | |
665 | ||
666 | (deffoo nnimap-request-create-group (group &optional server args) | |
667 | (when (nnimap-possibly-change-group nil server) | |
668 | (with-current-buffer (nnimap-buffer) | |
669 | (car (nnimap-command "CREATE %S" (utf7-encode group t)))))) | |
c113de23 | 670 | |
a46359d4 LMI |
671 | (deffoo nnimap-request-delete-group (group &optional force server) |
672 | (when (nnimap-possibly-change-group nil server) | |
673 | (with-current-buffer (nnimap-buffer) | |
0617bb00 LMI |
674 | (car (nnimap-command "DELETE %S" (utf7-encode group t)))))) |
675 | ||
a7dcc87b G |
676 | (deffoo nnimap-request-rename-group (group new-name &optional server) |
677 | (when (nnimap-possibly-change-group nil server) | |
678 | (with-current-buffer (nnimap-buffer) | |
ab67634f G |
679 | ;; Make sure we don't have this group open read/write by asking |
680 | ;; to examine a mailbox that doesn't exist. This seems to be | |
681 | ;; the only way that allows us to reliably go back to unselected | |
682 | ;; state on Courier. | |
683 | (nnimap-command "EXAMINE DOES.NOT.EXIST") | |
6b7df8d3 | 684 | (setf (nnimap-group nnimap-object) nil) |
f7aa248a G |
685 | (car (nnimap-command "RENAME %S %S" |
686 | (utf7-encode group t) (utf7-encode new-name t)))))) | |
a7dcc87b | 687 | |
0617bb00 LMI |
688 | (deffoo nnimap-request-expunge-group (group &optional server) |
689 | (when (nnimap-possibly-change-group group server) | |
690 | (with-current-buffer (nnimap-buffer) | |
691 | (car (nnimap-command "EXPUNGE"))))) | |
a46359d4 | 692 | |
20a673b2 KY |
693 | (defun nnimap-get-flags (spec) |
694 | (let ((articles nil) | |
f7aa248a | 695 | elems end) |
20a673b2 | 696 | (with-current-buffer (nnimap-buffer) |
c113de23 | 697 | (erase-buffer) |
20a673b2 KY |
698 | (nnimap-wait-for-response (nnimap-send-command |
699 | "UID FETCH %s FLAGS" spec)) | |
f7aa248a G |
700 | (setq end (point)) |
701 | (subst-char-in-region (point-min) (point-max) | |
702 | ?\\ ?% t) | |
20a673b2 | 703 | (goto-char (point-min)) |
f7aa248a G |
704 | (while (search-forward " FETCH " end t) |
705 | (setq elems (read (current-buffer))) | |
706 | (push (cons (cadr (memq 'UID elems)) | |
707 | (cadr (memq 'FLAGS elems))) | |
20a673b2 KY |
708 | articles))) |
709 | (nreverse articles))) | |
a1506d29 | 710 | |
286c4fc2 | 711 | (deffoo nnimap-close-group (group &optional server) |
20a673b2 | 712 | t) |
c113de23 | 713 | |
01c52d31 | 714 | (deffoo nnimap-request-move-article (article group server accept-form |
20a673b2 | 715 | &optional last internal-move-group) |
0617bb00 LMI |
716 | (with-temp-buffer |
717 | (when (nnimap-request-article article group server (current-buffer)) | |
718 | ;; If the move is internal (on the same server), just do it the easy | |
719 | ;; way. | |
720 | (let ((message-id (message-field-value "message-id"))) | |
721 | (if internal-move-group | |
722 | (let ((result | |
723 | (with-current-buffer (nnimap-buffer) | |
724 | (nnimap-command "UID COPY %d %S" | |
725 | article | |
726 | (utf7-encode internal-move-group t))))) | |
727 | (when (car result) | |
a46359d4 | 728 | (nnimap-delete-article article) |
0617bb00 LMI |
729 | (cons internal-move-group |
730 | (nnimap-find-article-by-message-id | |
731 | internal-move-group message-id)))) | |
732 | ;; Move the article to a different method. | |
733 | (let ((result (eval accept-form))) | |
734 | (when result | |
735 | (nnimap-delete-article article) | |
736 | result))))))) | |
20a673b2 KY |
737 | |
738 | (deffoo nnimap-request-expire-articles (articles group &optional server force) | |
739 | (cond | |
0617bb00 LMI |
740 | ((null articles) |
741 | nil) | |
20a673b2 KY |
742 | ((not (nnimap-possibly-change-group group server)) |
743 | articles) | |
0617bb00 LMI |
744 | ((and force |
745 | (eq nnmail-expiry-target 'delete)) | |
4478e074 | 746 | (unless (nnimap-delete-article (gnus-compress-sequence articles)) |
283f7b93 | 747 | (nnheader-message 7 "Article marked for deletion, but not expunged.")) |
20a673b2 KY |
748 | nil) |
749 | (t | |
0617bb00 | 750 | (let ((deletable-articles |
b069e5a6 G |
751 | (if (or force |
752 | (eq nnmail-expiry-wait 'immediate)) | |
0617bb00 LMI |
753 | articles |
754 | (gnus-sorted-intersection | |
755 | articles | |
756 | (nnimap-find-expired-articles group))))) | |
757 | (if (null deletable-articles) | |
758 | articles | |
759 | (if (eq nnmail-expiry-target 'delete) | |
4478e074 | 760 | (nnimap-delete-article (gnus-compress-sequence deletable-articles)) |
0617bb00 LMI |
761 | (setq deletable-articles |
762 | (nnimap-process-expiry-targets | |
763 | deletable-articles group server))) | |
764 | ;; Return the articles we didn't delete. | |
765 | (gnus-sorted-complement articles deletable-articles)))))) | |
766 | ||
767 | (defun nnimap-process-expiry-targets (articles group server) | |
768 | (let ((deleted-articles nil)) | |
769 | (dolist (article articles) | |
770 | (let ((target nnmail-expiry-target)) | |
771 | (with-temp-buffer | |
772 | (when (nnimap-request-article article group server (current-buffer)) | |
283f7b93 | 773 | (nnheader-message 7 "Expiring article %s:%d" group article) |
0617bb00 LMI |
774 | (when (functionp target) |
775 | (setq target (funcall target group))) | |
776 | (when (and target | |
777 | (not (eq target 'delete))) | |
778 | (if (or (gnus-request-group target t) | |
779 | (gnus-request-create-group target)) | |
780 | (nnmail-expiry-target-group target group) | |
781 | (setq target nil))) | |
782 | (when target | |
783 | (push article deleted-articles)))))) | |
784 | ;; Change back to the current group again. | |
785 | (nnimap-possibly-change-group group server) | |
786 | (setq deleted-articles (nreverse deleted-articles)) | |
4478e074 | 787 | (nnimap-delete-article (gnus-compress-sequence deleted-articles)) |
0617bb00 LMI |
788 | deleted-articles)) |
789 | ||
790 | (defun nnimap-find-expired-articles (group) | |
791 | (let ((cutoff (nnmail-expired-article-p group nil nil))) | |
792 | (with-current-buffer (nnimap-buffer) | |
793 | (let ((result | |
794 | (nnimap-command | |
795 | "UID SEARCH SENTBEFORE %s" | |
796 | (format-time-string | |
797 | (format "%%d-%s-%%Y" | |
798 | (upcase | |
799 | (car (rassoc (nth 4 (decode-time cutoff)) | |
800 | parse-time-months)))) | |
801 | cutoff)))) | |
802 | (and (car result) | |
803 | (delete 0 (mapcar #'string-to-number | |
804 | (cdr (assoc "SEARCH" (cdr result)))))))))) | |
805 | ||
20a673b2 KY |
806 | |
807 | (defun nnimap-find-article-by-message-id (group message-id) | |
6b958814 G |
808 | (with-current-buffer (nnimap-buffer) |
809 | (erase-buffer) | |
810 | (setf (nnimap-group nnimap-object) nil) | |
811 | (nnimap-send-command "EXAMINE %S" (utf7-encode group t)) | |
812 | (let ((sequence | |
813 | (nnimap-send-command "UID SEARCH HEADER Message-Id %S" message-id)) | |
814 | article result) | |
815 | (setq result (nnimap-wait-for-response sequence)) | |
816 | (when (and result | |
817 | (car (setq result (nnimap-parse-response)))) | |
818 | ;; Select the last instance of the message in the group. | |
819 | (and (setq article | |
820 | (car (last (assoc "SEARCH" (cdr result))))) | |
821 | (string-to-number article)))))) | |
20a673b2 KY |
822 | |
823 | (defun nnimap-delete-article (articles) | |
824 | (with-current-buffer (nnimap-buffer) | |
825 | (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)" | |
826 | (nnimap-article-ranges articles)) | |
0617bb00 LMI |
827 | (cond |
828 | ((member "UIDPLUS" (nnimap-capabilities nnimap-object)) | |
829 | (nnimap-command "UID EXPUNGE %s" | |
830 | (nnimap-article-ranges articles)) | |
831 | t) | |
832 | (nnimap-expunge | |
833 | (nnimap-command "EXPUNGE") | |
7390c1cd TZ |
834 | t) |
835 | (t (gnus-message 7 (concat "nnimap: nnimap-expunge is not set and the " | |
836 | "server doesn't support UIDPLUS, so we won't " | |
837 | "delete this article now")))))) | |
20a673b2 KY |
838 | |
839 | (deffoo nnimap-request-scan (&optional group server) | |
840 | (when (and (nnimap-possibly-change-group nil server) | |
20a673b2 KY |
841 | nnimap-inbox |
842 | nnimap-split-methods) | |
283f7b93 | 843 | (nnheader-message 7 "nnimap %s splitting mail..." server) |
20a673b2 KY |
844 | (nnimap-split-incoming-mail))) |
845 | ||
846 | (defun nnimap-marks-to-flags (marks) | |
847 | (let (flags flag) | |
848 | (dolist (mark marks) | |
849 | (when (setq flag (cadr (assq mark nnimap-mark-alist))) | |
850 | (push flag flags))) | |
851 | flags)) | |
852 | ||
286c4fc2 | 853 | (deffoo nnimap-request-set-mark (group actions &optional server) |
20a673b2 KY |
854 | (when (nnimap-possibly-change-group group server) |
855 | (let (sequence) | |
856 | (with-current-buffer (nnimap-buffer) | |
229b59da | 857 | (erase-buffer) |
20a673b2 KY |
858 | ;; Just send all the STORE commands without waiting for |
859 | ;; response. If they're successful, they're successful. | |
860 | (dolist (action actions) | |
861 | (destructuring-bind (range action marks) action | |
862 | (let ((flags (nnimap-marks-to-flags marks))) | |
863 | (when flags | |
864 | (setq sequence (nnimap-send-command | |
865 | "UID STORE %s %sFLAGS.SILENT (%s)" | |
866 | (nnimap-article-ranges range) | |
867 | (if (eq action 'del) | |
868 | "-" | |
869 | "+") | |
870 | (mapconcat #'identity flags " "))))))) | |
871 | ;; Wait for the last command to complete to avoid later | |
872 | ;; syncronisation problems with the stream. | |
a46359d4 LMI |
873 | (when sequence |
874 | (nnimap-wait-for-response sequence)))))) | |
a1506d29 | 875 | |
c113de23 | 876 | (deffoo nnimap-request-accept-article (group &optional server last) |
20a673b2 KY |
877 | (when (nnimap-possibly-change-group nil server) |
878 | (nnmail-check-syntax) | |
6b958814 G |
879 | (let ((message-id (message-field-value "message-id")) |
880 | sequence message) | |
881 | (nnimap-add-cr) | |
882 | (setq message (buffer-string)) | |
20a673b2 KY |
883 | (with-current-buffer (nnimap-buffer) |
884 | (setq sequence (nnimap-send-command | |
885 | "APPEND %S {%d}" (utf7-encode group t) | |
886 | (length message))) | |
887 | (process-send-string (get-buffer-process (current-buffer)) message) | |
286c4fc2 LMI |
888 | (process-send-string (get-buffer-process (current-buffer)) |
889 | (if (nnimap-newlinep nnimap-object) | |
890 | "\n" | |
891 | "\r\n")) | |
20a673b2 | 892 | (let ((result (nnimap-get-response sequence))) |
062eae99 G |
893 | (if (not (car result)) |
894 | (progn | |
283f7b93 | 895 | (nnheader-message 7 "%s" (nnheader-get-report-string 'nnimap)) |
062eae99 | 896 | nil) |
20a673b2 KY |
897 | (cons group |
898 | (nnimap-find-article-by-message-id group message-id)))))))) | |
899 | ||
900 | (defun nnimap-add-cr () | |
901 | (goto-char (point-min)) | |
902 | (while (re-search-forward "\r?\n" nil t) | |
903 | (replace-match "\r\n" t t))) | |
904 | ||
905 | (defun nnimap-get-groups () | |
906 | (let ((result (nnimap-command "LIST \"\" \"*\"")) | |
907 | groups) | |
908 | (when (car result) | |
909 | (dolist (line (cdr result)) | |
910 | (when (and (equal (car line) "LIST") | |
911 | (not (and (caadr line) | |
912 | (string-match "noselect" (caadr line))))) | |
913 | (push (car (last line)) groups))) | |
914 | (nreverse groups)))) | |
915 | ||
286c4fc2 | 916 | (deffoo nnimap-request-list (&optional server) |
20a673b2 KY |
917 | (nnimap-possibly-change-group nil server) |
918 | (with-current-buffer nntp-server-buffer | |
919 | (erase-buffer) | |
920 | (let ((groups | |
921 | (with-current-buffer (nnimap-buffer) | |
922 | (nnimap-get-groups))) | |
923 | sequences responses) | |
924 | (when groups | |
925 | (with-current-buffer (nnimap-buffer) | |
b069e5a6 | 926 | (setf (nnimap-group nnimap-object) nil) |
20a673b2 KY |
927 | (dolist (group groups) |
928 | (push (list (nnimap-send-command "EXAMINE %S" (utf7-encode group t)) | |
929 | group) | |
930 | sequences)) | |
931 | (nnimap-wait-for-response (caar sequences)) | |
932 | (setq responses | |
933 | (nnimap-get-responses (mapcar #'car sequences)))) | |
934 | (dolist (response responses) | |
935 | (let* ((sequence (car response)) | |
936 | (response (cadr response)) | |
937 | (group (cadr (assoc sequence sequences)))) | |
938 | (when (and group | |
939 | (equal (caar response) "OK")) | |
940 | (let ((uidnext (nnimap-find-parameter "UIDNEXT" response)) | |
941 | highest exists) | |
942 | (dolist (elem response) | |
943 | (when (equal (cadr elem) "EXISTS") | |
944 | (setq exists (string-to-number (car elem))))) | |
945 | (when uidnext | |
946 | (setq highest (1- (string-to-number (car uidnext))))) | |
947 | (cond | |
948 | ((null highest) | |
949 | (insert (format "%S 0 1 y\n" (utf7-decode group t)))) | |
950 | ((zerop exists) | |
951 | ;; Empty group. | |
952 | (insert (format "%S %d %d y\n" | |
953 | (utf7-decode group t) highest (1+ highest)))) | |
954 | (t | |
955 | ;; Return the widest possible range. | |
956 | (insert (format "%S %d 1 y\n" (utf7-decode group t) | |
957 | (or highest exists))))))))) | |
c113de23 GM |
958 | t)))) |
959 | ||
a3f57c41 G |
960 | (deffoo nnimap-request-newgroups (date &optional server) |
961 | (nnimap-possibly-change-group nil server) | |
962 | (with-current-buffer nntp-server-buffer | |
963 | (erase-buffer) | |
964 | (dolist (group (with-current-buffer (nnimap-buffer) | |
965 | (nnimap-get-groups))) | |
966 | (unless (assoc group nnimap-current-infos) | |
967 | ;; Insert dummy numbers here -- they don't matter. | |
130e977f LMI |
968 | (insert (format "%S 0 1 y\n" group)))) |
969 | t)) | |
a3f57c41 | 970 | |
286c4fc2 | 971 | (deffoo nnimap-retrieve-group-data-early (server infos) |
20a673b2 KY |
972 | (when (nnimap-possibly-change-group nil server) |
973 | (with-current-buffer (nnimap-buffer) | |
f7aa248a G |
974 | (erase-buffer) |
975 | (setf (nnimap-group nnimap-object) nil) | |
20a673b2 | 976 | ;; QRESYNC handling isn't implemented. |
dab0271f | 977 | (let ((qresyncp (member "QRESYNC" (nnimap-capabilities nnimap-object))) |
f7aa248a | 978 | params groups sequences active uidvalidity modseq group) |
20a673b2 KY |
979 | ;; Go through the infos and gather the data needed to know |
980 | ;; what and how to request the data. | |
981 | (dolist (info infos) | |
f7aa248a G |
982 | (setq params (gnus-info-params info) |
983 | group (gnus-group-real-name (gnus-info-group info)) | |
984 | active (cdr (assq 'active params)) | |
985 | uidvalidity (cdr (assq 'uidvalidity params)) | |
986 | modseq (cdr (assq 'modseq params))) | |
20a673b2 | 987 | (if (and qresyncp |
f7aa248a G |
988 | uidvalidity |
989 | modseq) | |
20a673b2 | 990 | (push |
f7aa248a | 991 | (list (nnimap-send-command "EXAMINE %S (QRESYNC (%s %s))" |
dab0271f G |
992 | (utf7-encode group t) |
993 | uidvalidity modseq) | |
f7aa248a G |
994 | 'qresync |
995 | nil group 'qresync) | |
20a673b2 KY |
996 | sequences) |
997 | (let ((start | |
f7aa248a | 998 | (if (and active uidvalidity) |
20a673b2 | 999 | ;; Fetch the last 100 flags. |
f7aa248a G |
1000 | (max 1 (- (cdr active) 100)) |
1001 | 1)) | |
1002 | (command | |
1003 | (if uidvalidity | |
1004 | "EXAMINE" | |
1005 | ;; If we don't have a UIDVALIDITY, then this is | |
1006 | ;; the first time we've seen the group, so we | |
1007 | ;; have to do a SELECT (which is slower than an | |
1008 | ;; examine), but will tell us whether the group | |
1009 | ;; is read-only or not. | |
1010 | "SELECT"))) | |
dab0271f G |
1011 | (push (list (nnimap-send-command "%s %S" command |
1012 | (utf7-encode group t)) | |
20a673b2 | 1013 | (nnimap-send-command "UID FETCH %d:* FLAGS" start) |
f7aa248a | 1014 | start group command) |
8ccbef23 G |
1015 | sequences))) |
1016 | ;; Some servers apparently can't have many outstanding | |
1017 | ;; commands, so throttle them. | |
1018 | (when (and (not nnimap-streaming) | |
1019 | (car sequences)) | |
1020 | (nnimap-wait-for-response (caar sequences)))) | |
20a673b2 KY |
1021 | sequences)))) |
1022 | ||
286c4fc2 | 1023 | (deffoo nnimap-finish-retrieve-group-infos (server infos sequences) |
20a673b2 KY |
1024 | (when (and sequences |
1025 | (nnimap-possibly-change-group nil server)) | |
1026 | (with-current-buffer (nnimap-buffer) | |
1027 | ;; Wait for the final data to trickle in. | |
f7aa248a G |
1028 | (when (nnimap-wait-for-response (if (eq (cadar sequences) 'qresync) |
1029 | (caar sequences) | |
1030 | (cadar sequences)) | |
1031 | t) | |
1032 | ;; Now we should have most of the data we need, no matter | |
1033 | ;; whether we're QRESYNCING, fetching all the flags from | |
1034 | ;; scratch, or just fetching the last 100 flags per group. | |
8ccbef23 G |
1035 | (nnimap-update-infos (nnimap-flags-to-marks |
1036 | (nnimap-parse-flags | |
1037 | (nreverse sequences))) | |
1038 | infos) | |
1039 | ;; Finally, just return something resembling an active file in | |
1040 | ;; the nntp buffer, so that the agent can save the info, too. | |
1041 | (with-current-buffer nntp-server-buffer | |
1042 | (erase-buffer) | |
1043 | (dolist (info infos) | |
1044 | (let* ((group (gnus-info-group info)) | |
1045 | (active (gnus-active group))) | |
1046 | (when active | |
1047 | (insert (format "%S %d %d y\n" | |
1048 | (gnus-group-real-name group) | |
1049 | (cdr active) | |
1050 | (car active))))))))))) | |
20a673b2 KY |
1051 | |
1052 | (defun nnimap-update-infos (flags infos) | |
1053 | (dolist (info infos) | |
f7aa248a G |
1054 | (let* ((group (gnus-group-real-name (gnus-info-group info))) |
1055 | (marks (cdr (assoc group flags)))) | |
1056 | (when marks | |
1057 | (nnimap-update-info info marks))))) | |
20a673b2 KY |
1058 | |
1059 | (defun nnimap-update-info (info marks) | |
f7aa248a G |
1060 | (destructuring-bind (existing flags high low uidnext start-article |
1061 | permanent-flags uidvalidity | |
1062 | vanished highestmodseq) marks | |
1063 | (cond | |
1064 | ;; Ignore groups with no UIDNEXT/marks. This happens for | |
1065 | ;; completely empty groups. | |
1066 | ((and (not existing) | |
1067 | (not uidnext)) | |
dab0271f G |
1068 | (let ((active (cdr (assq 'active (gnus-info-params info))))) |
1069 | (when active | |
1070 | (gnus-set-active (gnus-info-group info) active)))) | |
f7aa248a G |
1071 | ;; We have a mismatch between the old and new UIDVALIDITY |
1072 | ;; identifiers, so we have to re-request the group info (the next | |
1073 | ;; time). This virtually never happens. | |
1074 | ((let ((old-uidvalidity | |
1075 | (cdr (assq 'uidvalidity (gnus-info-params info))))) | |
1076 | (and old-uidvalidity | |
1077 | (not (equal old-uidvalidity uidvalidity)) | |
1078 | (> start-article 1))) | |
1079 | (gnus-group-remove-parameter info 'uidvalidity) | |
1080 | (gnus-group-remove-parameter info 'modseq)) | |
1081 | ;; We have the data needed to update. | |
1082 | (t | |
dab0271f G |
1083 | (let* ((group (gnus-info-group info)) |
1084 | (completep (and start-article | |
1085 | (= start-article 1))) | |
1086 | (active (or (gnus-active group) | |
1087 | (cdr (assq 'active (gnus-info-params info)))))) | |
b1ae92ba G |
1088 | (when uidnext |
1089 | (setq high (1- uidnext))) | |
20a673b2 KY |
1090 | ;; First set the active ranges based on high/low. |
1091 | (if (or completep | |
1092 | (not (gnus-active group))) | |
1093 | (gnus-set-active group | |
61b1af82 G |
1094 | (cond |
1095 | ((and low high) | |
1096 | (cons low high)) | |
1097 | (uidnext | |
20a673b2 | 1098 | ;; No articles in this group. |
61b1af82 | 1099 | (cons uidnext (1- uidnext))) |
dab0271f G |
1100 | (active |
1101 | active) | |
61b1af82 G |
1102 | (start-article |
1103 | (cons start-article (1- start-article))) | |
1104 | (t | |
1105 | ;; No articles and no uidnext. | |
1106 | nil))) | |
9f2d52e7 G |
1107 | (gnus-set-active |
1108 | group | |
dab0271f | 1109 | (cons (car active) |
9f2d52e7 | 1110 | (or high (1- uidnext))))) |
f7aa248a G |
1111 | ;; See whether this is a read-only group. |
1112 | (unless (eq permanent-flags 'not-scanned) | |
1113 | (gnus-group-set-parameter | |
1114 | info 'permanent-flags | |
7cad71ad G |
1115 | (and (or (memq '%* permanent-flags) |
1116 | (memq '%Seen permanent-flags)) | |
1117 | permanent-flags))) | |
f7aa248a G |
1118 | ;; Update marks and read articles if this isn't a |
1119 | ;; read-only IMAP group. | |
7cad71ad G |
1120 | (when (setq permanent-flags |
1121 | (cdr (assq 'permanent-flags (gnus-info-params info)))) | |
f7aa248a G |
1122 | (if (and highestmodseq |
1123 | (not start-article)) | |
1124 | ;; We've gotten the data by QRESYNCing. | |
1125 | (nnimap-update-qresync-info | |
dab0271f | 1126 | info existing (nnimap-imap-ranges-to-gnus-ranges vanished) flags) |
f7aa248a G |
1127 | ;; Do normal non-QRESYNC flag updates. |
1128 | ;; Update the list of read articles. | |
1129 | (let* ((unread | |
1130 | (gnus-compress-sequence | |
1131 | (gnus-set-difference | |
1132 | (gnus-set-difference | |
1133 | existing | |
1134 | (cdr (assoc '%Seen flags))) | |
1135 | (cdr (assoc '%Flagged flags))))) | |
1136 | (read (gnus-range-difference | |
1137 | (cons start-article high) unread))) | |
1138 | (when (> start-article 1) | |
1139 | (setq read | |
1140 | (gnus-range-nconcat | |
1141 | (if (> start-article 1) | |
1142 | (gnus-sorted-range-intersection | |
1143 | (cons 1 (1- start-article)) | |
1144 | (gnus-info-read info)) | |
1145 | (gnus-info-read info)) | |
1146 | read))) | |
7cad71ad G |
1147 | (when (or (not (listp permanent-flags)) |
1148 | (memq '%Seen permanent-flags)) | |
1149 | (gnus-info-set-read info read)) | |
f7aa248a G |
1150 | ;; Update the marks. |
1151 | (setq marks (gnus-info-marks info)) | |
1152 | (dolist (type (cdr nnimap-mark-alist)) | |
7cad71ad G |
1153 | (when (or (not (listp permanent-flags)) |
1154 | (memq (assoc (caddr type) flags) permanent-flags) | |
1155 | (memq '%* permanent-flags)) | |
1156 | (let ((old-marks (assoc (car type) marks)) | |
1157 | (new-marks | |
1158 | (gnus-compress-sequence | |
1159 | (cdr (or (assoc (caddr type) flags) ; %Flagged | |
1160 | (assoc (intern (cadr type) obarray) flags) | |
1161 | (assoc (cadr type) flags)))))) ; "\Flagged" | |
1162 | (setq marks (delq old-marks marks)) | |
1163 | (pop old-marks) | |
1164 | (when (and old-marks | |
1165 | (> start-article 1)) | |
1166 | (setq old-marks (gnus-range-difference | |
1167 | old-marks | |
1168 | (cons start-article high))) | |
1169 | (setq new-marks (gnus-range-nconcat old-marks new-marks))) | |
1170 | (when new-marks | |
1171 | (push (cons (car type) new-marks) marks))))) | |
1172 | (gnus-info-set-marks info marks t)))) | |
f7aa248a G |
1173 | ;; Note the active level for the next run-through. |
1174 | (gnus-group-set-parameter info 'active (gnus-active group)) | |
1175 | (gnus-group-set-parameter info 'uidvalidity uidvalidity) | |
1176 | (gnus-group-set-parameter info 'modseq highestmodseq) | |
1177 | (nnimap-store-info info (gnus-active group))))))) | |
1178 | ||
dab0271f | 1179 | (defun nnimap-update-qresync-info (info existing vanished flags) |
f7aa248a G |
1180 | ;; Add all the vanished articles to the list of read articles. |
1181 | (gnus-info-set-read | |
1182 | info | |
dab0271f G |
1183 | (gnus-add-to-range |
1184 | (gnus-add-to-range | |
1185 | (gnus-range-add (gnus-info-read info) | |
1186 | vanished) | |
1187 | (cdr (assq '%Flagged flags))) | |
1188 | (cdr (assq '%Seen flags)))) | |
1189 | (let ((marks (gnus-info-marks info))) | |
1190 | (dolist (type (cdr nnimap-mark-alist)) | |
1191 | (let ((ticks (assoc (car type) marks)) | |
1192 | (new-marks | |
1193 | (cdr (or (assoc (caddr type) flags) ; %Flagged | |
1194 | (assoc (intern (cadr type) obarray) flags) | |
1195 | (assoc (cadr type) flags))))) ; "\Flagged" | |
1196 | (setq marks (delq ticks marks)) | |
1197 | (pop ticks) | |
1198 | ;; Add the new marks we got. | |
1199 | (setq ticks (gnus-add-to-range ticks new-marks)) | |
1200 | ;; Remove the marks from messages that don't have them. | |
1201 | (setq ticks (gnus-remove-from-range | |
1202 | ticks | |
1203 | (gnus-compress-sequence | |
1204 | (gnus-sorted-complement existing new-marks)))) | |
1205 | (when ticks | |
1206 | (push (cons (car type) ticks) marks))) | |
1207 | (gnus-info-set-marks info marks t)))) | |
f7aa248a G |
1208 | |
1209 | (defun nnimap-imap-ranges-to-gnus-ranges (irange) | |
1210 | (if (zerop (length irange)) | |
1211 | nil | |
1212 | (let ((result nil)) | |
1213 | (dolist (elem (split-string irange ",")) | |
1214 | (push | |
1215 | (if (string-match ":" elem) | |
1216 | (let ((numbers (split-string elem ":"))) | |
1217 | (cons (string-to-number (car numbers)) | |
1218 | (string-to-number (cadr numbers)))) | |
1219 | (string-to-number elem)) | |
1220 | result)) | |
1221 | (nreverse result)))) | |
286c4fc2 LMI |
1222 | |
1223 | (defun nnimap-store-info (info active) | |
1224 | (let* ((group (gnus-group-real-name (gnus-info-group info))) | |
1225 | (entry (assoc group nnimap-current-infos))) | |
1226 | (if entry | |
1227 | (setcdr entry (list info active)) | |
1228 | (push (list group info active) nnimap-current-infos)))) | |
20a673b2 KY |
1229 | |
1230 | (defun nnimap-flags-to-marks (groups) | |
f7aa248a G |
1231 | (let (data group totalp uidnext articles start-article mark permanent-flags |
1232 | uidvalidity vanished highestmodseq) | |
20a673b2 KY |
1233 | (dolist (elem groups) |
1234 | (setq group (car elem) | |
b069e5a6 G |
1235 | uidnext (nth 1 elem) |
1236 | start-article (nth 2 elem) | |
1237 | permanent-flags (nth 3 elem) | |
f7aa248a G |
1238 | uidvalidity (nth 4 elem) |
1239 | vanished (nth 5 elem) | |
1240 | highestmodseq (nth 6 elem) | |
1241 | articles (nthcdr 7 elem)) | |
20a673b2 KY |
1242 | (let ((high (caar articles)) |
1243 | marks low existing) | |
1244 | (dolist (article articles) | |
1245 | (setq low (car article)) | |
1246 | (push (car article) existing) | |
1247 | (dolist (flag (cdr article)) | |
1248 | (setq mark (assoc flag marks)) | |
1249 | (if (not mark) | |
1250 | (push (list flag (car article)) marks) | |
b069e5a6 G |
1251 | (setcdr mark (cons (car article) (cdr mark)))))) |
1252 | (push (list group existing marks high low uidnext start-article | |
f7aa248a | 1253 | permanent-flags uidvalidity vanished highestmodseq) |
b069e5a6 | 1254 | data))) |
20a673b2 KY |
1255 | data)) |
1256 | ||
1257 | (defun nnimap-parse-flags (sequences) | |
1258 | (goto-char (point-min)) | |
b069e5a6 G |
1259 | ;; Change \Delete etc to %Delete, so that the reader can read it. |
1260 | (subst-char-in-region (point-min) (point-max) | |
1261 | ?\\ ?% t) | |
f7aa248a G |
1262 | (let (start end articles groups uidnext elems permanent-flags |
1263 | uidvalidity vanished highestmodseq) | |
20a673b2 | 1264 | (dolist (elem sequences) |
f7aa248a G |
1265 | (destructuring-bind (group-sequence flag-sequence totalp group command) |
1266 | elem | |
b069e5a6 | 1267 | (setq start (point)) |
f7aa248a G |
1268 | (when (and |
1269 | ;; The EXAMINE was successful. | |
1270 | (search-forward (format "\n%d OK " group-sequence) nil t) | |
1271 | (progn | |
1272 | (forward-line 1) | |
1273 | (setq end (point)) | |
1274 | (goto-char start) | |
1275 | (setq permanent-flags | |
1276 | (if (equal command "SELECT") | |
b069e5a6 | 1277 | (and (search-forward "PERMANENTFLAGS " |
f7aa248a G |
1278 | (or end (point-min)) t) |
1279 | (read (current-buffer))) | |
1280 | 'not-scanned)) | |
1281 | (goto-char start) | |
1282 | (setq uidnext | |
1283 | (and (search-forward "UIDNEXT " | |
1284 | (or end (point-min)) t) | |
1285 | (read (current-buffer)))) | |
1286 | (goto-char start) | |
1287 | (setq uidvalidity | |
1288 | (and (re-search-forward "UIDVALIDITY \\([0-9]+\\)" | |
1289 | (or end (point-min)) t) | |
1290 | ;; Store UIDVALIDITY as a string, as it's | |
1291 | ;; too big for 32-bit Emacsen, usually. | |
1292 | (match-string 1))) | |
1293 | (goto-char start) | |
1294 | (setq vanished | |
1295 | (and (eq flag-sequence 'qresync) | |
1296 | (re-search-forward "VANISHED.* \\([0-9:,]+\\)" | |
1297 | (or end (point-min)) t) | |
1298 | (match-string 1))) | |
1299 | (goto-char start) | |
1300 | (setq highestmodseq | |
1301 | (and (search-forward "HIGHESTMODSEQ " | |
1302 | (or end (point-min)) t) | |
1303 | (read (current-buffer)))) | |
1304 | (goto-char end) | |
1305 | (forward-line -1)) | |
1306 | ;; The UID FETCH FLAGS was successful. | |
1307 | (or (eq flag-sequence 'qresync) | |
1308 | (search-forward (format "\n%d OK " flag-sequence) nil t))) | |
1309 | (if (eq flag-sequence 'qresync) | |
1310 | (progn | |
1311 | (goto-char start) | |
1312 | (setq start end)) | |
1313 | (setq start (point)) | |
1314 | (goto-char end)) | |
a1d16a7b | 1315 | (while (re-search-forward "^\\* [0-9]+ FETCH " start t) |
b069e5a6 G |
1316 | (setq elems (read (current-buffer))) |
1317 | (push (cons (cadr (memq 'UID elems)) | |
1318 | (cadr (memq 'FLAGS elems))) | |
20a673b2 | 1319 | articles)) |
f7aa248a G |
1320 | (push (nconc (list group uidnext totalp permanent-flags uidvalidity |
1321 | vanished highestmodseq) | |
1322 | articles) | |
b069e5a6 | 1323 | groups) |
f7aa248a | 1324 | (goto-char end) |
20a673b2 KY |
1325 | (setq articles nil)))) |
1326 | groups)) | |
1327 | ||
1328 | (defun nnimap-find-process-buffer (buffer) | |
1329 | (cadr (assoc buffer nnimap-connection-alist))) | |
1330 | ||
286c4fc2 | 1331 | (deffoo nnimap-request-post (&optional server) |
20a673b2 KY |
1332 | (setq nnimap-status-string "Read-only server") |
1333 | nil) | |
c113de23 | 1334 | |
030158f3 G |
1335 | (deffoo nnimap-request-thread (id) |
1336 | (let* ((refs (split-string | |
1337 | (or (mail-header-references (gnus-summary-article-header)) | |
1338 | ""))) | |
1339 | (cmd (let ((value | |
1340 | (format | |
1341 | "(OR HEADER REFERENCES %s HEADER Message-Id %s)" | |
1342 | id id))) | |
1343 | (dolist (refid refs value) | |
1344 | (setq value (format | |
1345 | "(OR (OR HEADER Message-Id %s HEADER REFERENCES %s) %s)" | |
1346 | refid refid value))))) | |
1347 | (result | |
1348 | (with-current-buffer (nnimap-buffer) | |
1349 | (nnimap-command "UID SEARCH %s" cmd)))) | |
1350 | (gnus-fetch-headers (and (car result) | |
1351 | (delete 0 (mapcar #'string-to-number | |
1352 | (cdr (assoc "SEARCH" (cdr result))))))))) | |
1353 | ||
20a673b2 KY |
1354 | (defun nnimap-possibly-change-group (group server) |
1355 | (let ((open-result t)) | |
1356 | (when (and server | |
1357 | (not (nnimap-server-opened server))) | |
1358 | (setq open-result (nnimap-open-server server))) | |
1359 | (cond | |
1360 | ((not open-result) | |
1361 | nil) | |
1362 | ((not group) | |
1363 | t) | |
1364 | (t | |
1365 | (with-current-buffer (nnimap-buffer) | |
1366 | (if (equal group (nnimap-group nnimap-object)) | |
1367 | t | |
1368 | (let ((result (nnimap-command "SELECT %S" (utf7-encode group t)))) | |
1369 | (when (car result) | |
286c4fc2 LMI |
1370 | (setf (nnimap-group nnimap-object) group |
1371 | (nnimap-select-result nnimap-object) result) | |
20a673b2 KY |
1372 | result)))))))) |
1373 | ||
1374 | (defun nnimap-find-connection (buffer) | |
1375 | "Find the connection delivering to BUFFER." | |
1376 | (let ((entry (assoc buffer nnimap-connection-alist))) | |
1377 | (when entry | |
1378 | (if (and (buffer-name (cadr entry)) | |
1379 | (get-buffer-process (cadr entry)) | |
1380 | (memq (process-status (get-buffer-process (cadr entry))) | |
1381 | '(open run))) | |
1382 | (get-buffer-process (cadr entry)) | |
1383 | (setq nnimap-connection-alist (delq entry nnimap-connection-alist)) | |
1384 | nil)))) | |
1385 | ||
1386 | (defvar nnimap-sequence 0) | |
1387 | ||
1388 | (defun nnimap-send-command (&rest args) | |
1389 | (process-send-string | |
1390 | (get-buffer-process (current-buffer)) | |
1391 | (nnimap-log-command | |
286c4fc2 | 1392 | (format "%d %s%s\n" |
20a673b2 | 1393 | (incf nnimap-sequence) |
286c4fc2 LMI |
1394 | (apply #'format args) |
1395 | (if (nnimap-newlinep nnimap-object) | |
1396 | "" | |
1397 | "\r")))) | |
20a673b2 KY |
1398 | nnimap-sequence) |
1399 | ||
1400 | (defun nnimap-log-command (command) | |
1401 | (with-current-buffer (get-buffer-create "*imap log*") | |
1402 | (goto-char (point-max)) | |
1403 | (insert (format-time-string "%H:%M:%S") " " command)) | |
1404 | command) | |
1405 | ||
1406 | (defun nnimap-command (&rest args) | |
1407 | (erase-buffer) | |
61b1af82 | 1408 | (setf (nnimap-last-command-time nnimap-object) (current-time)) |
20a673b2 KY |
1409 | (let* ((sequence (apply #'nnimap-send-command args)) |
1410 | (response (nnimap-get-response sequence))) | |
1411 | (if (equal (caar response) "OK") | |
1412 | (cons t response) | |
1413 | (nnheader-report 'nnimap "%s" | |
a46359d4 LMI |
1414 | (mapconcat (lambda (a) |
1415 | (format "%s" a)) | |
1416 | (car response) " ")) | |
20a673b2 KY |
1417 | nil))) |
1418 | ||
1419 | (defun nnimap-get-response (sequence) | |
1420 | (nnimap-wait-for-response sequence) | |
1421 | (nnimap-parse-response)) | |
1422 | ||
286c4fc2 LMI |
1423 | (defun nnimap-wait-for-connection () |
1424 | (let ((process (get-buffer-process (current-buffer)))) | |
1425 | (goto-char (point-min)) | |
1426 | (while (and (memq (process-status process) | |
1427 | '(open run)) | |
6b958814 | 1428 | (not (re-search-forward "^[*.] .*\n" nil t))) |
286c4fc2 LMI |
1429 | (nnheader-accept-process-output process) |
1430 | (goto-char (point-min))) | |
bdaa75c7 | 1431 | (forward-line -1) |
6b958814 | 1432 | (and (looking-at "[*.] \\([A-Z0-9]+\\)") |
bdaa75c7 | 1433 | (match-string 1)))) |
286c4fc2 | 1434 | |
20a673b2 | 1435 | (defun nnimap-wait-for-response (sequence &optional messagep) |
8ccbef23 G |
1436 | (let ((process (get-buffer-process (current-buffer))) |
1437 | openp) | |
dab0271f G |
1438 | (condition-case nil |
1439 | (progn | |
1440 | (goto-char (point-max)) | |
1441 | (while (and (setq openp (memq (process-status process) | |
1442 | '(open run))) | |
1443 | (not (re-search-backward | |
1444 | (format "^%d .*\n" sequence) | |
1445 | (if nnimap-streaming | |
1446 | (max (point-min) (- (point) 500)) | |
1447 | (point-min)) | |
1448 | t))) | |
1449 | (when messagep | |
283f7b93 | 1450 | (nnheader-message 7 "nnimap read %dk" (/ (buffer-size) 1000))) |
dab0271f G |
1451 | (nnheader-accept-process-output process) |
1452 | (goto-char (point-max))) | |
1453 | openp) | |
1454 | (quit | |
1455 | ;; The user hit C-g while we were waiting: kill the process, in case | |
1456 | ;; it's a gnutls-cli process that's stuck (tends to happen a lot behind | |
1457 | ;; NAT routers). | |
1458 | (delete-process process) | |
1459 | nil)))) | |
20a673b2 KY |
1460 | |
1461 | (defun nnimap-parse-response () | |
1462 | (let ((lines (split-string (nnimap-last-response-string) "\r\n" t)) | |
1463 | result) | |
1464 | (dolist (line lines) | |
1465 | (push (cdr (nnimap-parse-line line)) result)) | |
1466 | ;; Return the OK/error code first, and then all the "continuation | |
1467 | ;; lines" afterwards. | |
1468 | (cons (pop result) | |
1469 | (nreverse result)))) | |
1470 | ||
1471 | ;; Parse an IMAP response line lightly. They look like | |
1472 | ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse | |
1473 | ;; the lines into a list of strings and lists of string. | |
1474 | (defun nnimap-parse-line (line) | |
1475 | (let (char result) | |
1476 | (with-temp-buffer | |
1477 | (insert line) | |
1478 | (goto-char (point-min)) | |
1479 | (while (not (eobp)) | |
1480 | (if (eql (setq char (following-char)) ? ) | |
1481 | (forward-char 1) | |
1482 | (push | |
1483 | (cond | |
1484 | ((eql char ?\[) | |
f7aa248a G |
1485 | (split-string |
1486 | (buffer-substring | |
1487 | (1+ (point)) | |
1488 | (1- (search-forward "]" (line-end-position) 'move))))) | |
20a673b2 | 1489 | ((eql char ?\() |
f7aa248a G |
1490 | (split-string |
1491 | (buffer-substring | |
1492 | (1+ (point)) | |
1493 | (1- (search-forward ")" (line-end-position) 'move))))) | |
20a673b2 KY |
1494 | ((eql char ?\") |
1495 | (forward-char 1) | |
9f2d52e7 G |
1496 | (buffer-substring |
1497 | (point) | |
1498 | (1- (or (search-forward "\"" (line-end-position) 'move) | |
1499 | (point))))) | |
20a673b2 KY |
1500 | (t |
1501 | (buffer-substring (point) (if (search-forward " " nil t) | |
1502 | (1- (point)) | |
1503 | (goto-char (point-max)))))) | |
1504 | result))) | |
1505 | (nreverse result)))) | |
1506 | ||
1507 | (defun nnimap-last-response-string () | |
1508 | (save-excursion | |
1509 | (forward-line 1) | |
1510 | (let ((end (point))) | |
1511 | (forward-line -1) | |
1512 | (when (not (bobp)) | |
1513 | (forward-line -1) | |
1514 | (while (and (not (bobp)) | |
1515 | (eql (following-char) ?*)) | |
1516 | (forward-line -1)) | |
1517 | (unless (eql (following-char) ?*) | |
1518 | (forward-line 1))) | |
1519 | (buffer-substring (point) end)))) | |
1520 | ||
1521 | (defun nnimap-get-responses (sequences) | |
1522 | (let (responses) | |
1523 | (dolist (sequence sequences) | |
1524 | (goto-char (point-min)) | |
1525 | (when (re-search-forward (format "^%d " sequence) nil t) | |
1526 | (push (list sequence (nnimap-parse-response)) | |
1527 | responses))) | |
1528 | responses)) | |
1529 | ||
1530 | (defvar nnimap-incoming-split-list nil) | |
1531 | ||
1532 | (defun nnimap-fetch-inbox (articles) | |
1533 | (erase-buffer) | |
1534 | (nnimap-wait-for-response | |
1535 | (nnimap-send-command | |
1536 | "UID FETCH %s %s" | |
1537 | (nnimap-article-ranges articles) | |
1538 | (format "(UID %s%s)" | |
1539 | (format | |
8ccbef23 | 1540 | (if (nnimap-ver4-p) |
20a673b2 KY |
1541 | "BODY.PEEK[HEADER] BODY.PEEK" |
1542 | "RFC822.PEEK")) | |
1543 | (if nnimap-split-download-body-default | |
a46359d4 | 1544 | "[]" |
20a673b2 KY |
1545 | "[1]"))) |
1546 | t)) | |
1547 | ||
1548 | (defun nnimap-split-incoming-mail () | |
1549 | (with-current-buffer (nnimap-buffer) | |
1550 | (let ((nnimap-incoming-split-list nil) | |
229b59da G |
1551 | (nnmail-split-methods (if (eq nnimap-split-methods 'default) |
1552 | nnmail-split-methods | |
1553 | nnimap-split-methods)) | |
6b958814 G |
1554 | (nnmail-split-fancy (or nnimap-split-fancy |
1555 | nnmail-split-fancy)) | |
20a673b2 KY |
1556 | (nnmail-inhibit-default-split-group t) |
1557 | (groups (nnimap-get-groups)) | |
1558 | new-articles) | |
1559 | (erase-buffer) | |
1560 | (nnimap-command "SELECT %S" nnimap-inbox) | |
1561 | (setq new-articles (nnimap-new-articles (nnimap-get-flags "1:*"))) | |
1562 | (when new-articles | |
1563 | (nnimap-fetch-inbox new-articles) | |
1564 | (nnimap-transform-split-mail) | |
1565 | (nnheader-ms-strip-cr) | |
1566 | (nnmail-cache-open) | |
1567 | (nnmail-split-incoming (current-buffer) | |
1568 | #'nnimap-save-mail-spec | |
1569 | nil nil | |
b069e5a6 G |
1570 | #'nnimap-dummy-active-number |
1571 | #'nnimap-save-mail-spec) | |
20a673b2 KY |
1572 | (when nnimap-incoming-split-list |
1573 | (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list)) | |
b069e5a6 | 1574 | sequences junk-articles) |
20a673b2 KY |
1575 | ;; Create any groups that doesn't already exist on the |
1576 | ;; server first. | |
1577 | (dolist (spec specs) | |
b069e5a6 G |
1578 | (when (and (not (member (car spec) groups)) |
1579 | (not (eq (car spec) 'junk))) | |
20a673b2 KY |
1580 | (nnimap-command "CREATE %S" (utf7-encode (car spec) t)))) |
1581 | ;; Then copy over all the messages. | |
1582 | (erase-buffer) | |
1583 | (dolist (spec specs) | |
1584 | (let ((group (car spec)) | |
1585 | (ranges (cdr spec))) | |
b069e5a6 G |
1586 | (if (eq group 'junk) |
1587 | (setq junk-articles ranges) | |
1588 | (push (list (nnimap-send-command | |
1589 | "UID COPY %s %S" | |
1590 | (nnimap-article-ranges ranges) | |
1591 | (utf7-encode group t)) | |
1592 | ranges) | |
1593 | sequences)))) | |
20a673b2 KY |
1594 | ;; Wait for the last COPY response... |
1595 | (when sequences | |
1596 | (nnimap-wait-for-response (caar sequences)) | |
1597 | ;; And then mark the successful copy actions as deleted, | |
1598 | ;; and possibly expunge them. | |
1599 | (nnimap-mark-and-expunge-incoming | |
61b1af82 G |
1600 | (nnimap-parse-copied-articles sequences))) |
1601 | (nnimap-mark-and-expunge-incoming junk-articles))))))) | |
20a673b2 KY |
1602 | |
1603 | (defun nnimap-mark-and-expunge-incoming (range) | |
1604 | (when range | |
1605 | (setq range (nnimap-article-ranges range)) | |
229b59da | 1606 | (erase-buffer) |
0617bb00 LMI |
1607 | (let ((sequence |
1608 | (nnimap-send-command | |
1609 | "UID STORE %s +FLAGS.SILENT (\\Deleted)" range))) | |
1610 | (cond | |
1611 | ;; If the server supports it, we now delete the message we have | |
1612 | ;; just copied over. | |
1613 | ((member "UIDPLUS" (nnimap-capabilities nnimap-object)) | |
1614 | (setq sequence (nnimap-send-command "UID EXPUNGE %s" range))) | |
1615 | ;; If it doesn't support UID EXPUNGE, then we only expunge if the | |
1616 | ;; user has configured it. | |
b069e5a6 | 1617 | (nnimap-expunge |
0617bb00 LMI |
1618 | (setq sequence (nnimap-send-command "EXPUNGE")))) |
1619 | (nnimap-wait-for-response sequence)))) | |
20a673b2 KY |
1620 | |
1621 | (defun nnimap-parse-copied-articles (sequences) | |
1622 | (let (sequence copied range) | |
1623 | (goto-char (point-min)) | |
1624 | (while (re-search-forward "^\\([0-9]+\\) OK " nil t) | |
1625 | (setq sequence (string-to-number (match-string 1))) | |
1626 | (when (setq range (cadr (assq sequence sequences))) | |
1627 | (push (gnus-uncompress-range range) copied))) | |
1628 | (gnus-compress-sequence (sort (apply #'nconc copied) #'<)))) | |
1629 | ||
1630 | (defun nnimap-new-articles (flags) | |
1631 | (let (new) | |
1632 | (dolist (elem flags) | |
1633 | (when (or (null (cdr elem)) | |
b069e5a6 G |
1634 | (and (not (memq '%Deleted (cdr elem))) |
1635 | (not (memq '%Seen (cdr elem))))) | |
20a673b2 KY |
1636 | (push (car elem) new))) |
1637 | (gnus-compress-sequence (nreverse new)))) | |
1638 | ||
1639 | (defun nnimap-make-split-specs (list) | |
1640 | (let ((specs nil) | |
1641 | entry) | |
1642 | (dolist (elem list) | |
1643 | (destructuring-bind (article spec) elem | |
1644 | (dolist (group (delete nil (mapcar #'car spec))) | |
1645 | (unless (setq entry (assoc group specs)) | |
1646 | (push (setq entry (list group)) specs)) | |
1647 | (setcdr entry (cons article (cdr entry)))))) | |
1648 | (dolist (entry specs) | |
1649 | (setcdr entry (gnus-compress-sequence (sort (cdr entry) #'<)))) | |
1650 | specs)) | |
1651 | ||
1652 | (defun nnimap-transform-split-mail () | |
1653 | (goto-char (point-min)) | |
1654 | (let (article bytes) | |
1655 | (block nil | |
1656 | (while (not (eobp)) | |
1657 | (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)")) | |
1658 | (delete-region (point) (progn (forward-line 1) (point))) | |
1659 | (when (eobp) | |
1660 | (return))) | |
1661 | (setq article (match-string 1) | |
1662 | bytes (nnimap-get-length)) | |
1663 | (delete-region (line-beginning-position) (line-end-position)) | |
1664 | ;; Insert MMDF separator, and a way to remember what this | |
1665 | ;; article UID is. | |
1666 | (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article)) | |
1667 | (forward-char (1+ bytes)) | |
1668 | (setq bytes (nnimap-get-length)) | |
1669 | (delete-region (line-beginning-position) (line-end-position)) | |
6b7df8d3 G |
1670 | ;; There's a body; skip past that. |
1671 | (when bytes | |
1672 | (forward-char (1+ bytes)) | |
1673 | (delete-region (line-beginning-position) (line-end-position))))))) | |
20a673b2 KY |
1674 | |
1675 | (defun nnimap-dummy-active-number (group &optional server) | |
1676 | 1) | |
1677 | ||
1678 | (defun nnimap-save-mail-spec (group-art &optional server full-nov) | |
1679 | (let (article) | |
1680 | (goto-char (point-min)) | |
1681 | (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t)) | |
1682 | (error "Invalid nnimap mail") | |
1683 | (setq article (string-to-number (match-string 1)))) | |
b069e5a6 G |
1684 | (push (list article |
1685 | (if (eq group-art 'junk) | |
1686 | (list (cons 'junk 1)) | |
1687 | group-art)) | |
20a673b2 | 1688 | nnimap-incoming-split-list))) |
c113de23 | 1689 | |
c113de23 GM |
1690 | (provide 'nnimap) |
1691 | ||
1692 | ;;; nnimap.el ends here |