Commit | Line | Data |
---|---|---|
20a673b2 | 1 | ;;; nnimap.el --- IMAP interface for Gnus |
e84b4b86 | 2 | |
73b0cd50 | 3 | ;; Copyright (C) 2010-2011 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 |
da91b5f2 CY |
34 | (require 'nnheader) |
35 | ;; In Emacs 24, `open-protocol-stream' is an autoloaded alias for | |
36 | ;; `make-network-stream'. | |
37 | (unless (fboundp 'open-protocol-stream) | |
38 | (require 'proto-stream))) | |
aa8f8277 | 39 | |
20a673b2 KY |
40 | (eval-when-compile |
41 | (require 'cl)) | |
c113de23 | 42 | |
f58208b1 LMI |
43 | (require 'nnheader) |
44 | (require 'gnus-util) | |
45 | (require 'gnus) | |
46 | (require 'nnoo) | |
20a673b2 | 47 | (require 'netrc) |
14db1c41 | 48 | (require 'utf7) |
6b958814 | 49 | (require 'tls) |
0617bb00 | 50 | (require 'parse-time) |
2b1e1ff4 GM |
51 | (require 'nnmail) |
52 | ||
b8e0f0cd G |
53 | (autoload 'auth-source-forget+ "auth-source") |
54 | (autoload 'auth-source-search "auth-source") | |
635be05a | 55 | |
c113de23 GM |
56 | (nnoo-declare nnimap) |
57 | ||
c113de23 | 58 | (defvoo nnimap-address nil |
20a673b2 | 59 | "The address of the IMAP server.") |
c113de23 | 60 | |
5e68f861 TZ |
61 | (defvoo nnimap-user nil |
62 | "Username to use for authentication to the IMAP server.") | |
63 | ||
c113de23 | 64 | (defvoo nnimap-server-port nil |
20a673b2 KY |
65 | "The IMAP port used. |
66 | If nnimap-stream is `ssl', this will default to `imaps'. If not, | |
67 | it will default to `imap'.") | |
68 | ||
ed797193 | 69 | (defvoo nnimap-stream 'undecided |
e742e117 CY |
70 | "How nnimap talks to the IMAP server. |
71 | The value should be either `undecided', `ssl' or `tls', | |
72 | `network', `starttls', `plain', or `shell'. | |
73 | ||
74 | If the value is `undecided', nnimap tries `ssl' first, then falls | |
75 | back on `network'.") | |
20a673b2 KY |
76 | |
77 | (defvoo nnimap-shell-program (if (boundp 'imap-shell-program) | |
78 | (if (listp imap-shell-program) | |
79 | (car imap-shell-program) | |
80 | imap-shell-program) | |
81 | "ssh %s imapd")) | |
82 | ||
83 | (defvoo nnimap-inbox nil | |
8e22bee0 G |
84 | "The mail box where incoming mail arrives and should be split out of. |
85 | For example, \"INBOX\".") | |
20a673b2 | 86 | |
8ccbef23 G |
87 | (defvoo nnimap-split-methods nil |
88 | "How mail is split. | |
8e22bee0 | 89 | Uses the same syntax as `nnmail-split-methods'.") |
8ccbef23 | 90 | |
6b958814 | 91 | (defvoo nnimap-split-fancy nil |
8e22bee0 | 92 | "Uses the same syntax as `nnmail-split-fancy'.") |
6b958814 | 93 | |
99e65b2d G |
94 | (defvoo nnimap-unsplittable-articles '(%Deleted %Seen) |
95 | "Articles with the flags in the list will not be considered when splitting.") | |
96 | ||
229b59da | 97 | (make-obsolete-variable 'nnimap-split-rule "see `nnimap-split-methods'" |
6b958814 | 98 | "Emacs 24.1") |
229b59da | 99 | |
bdaa75c7 LMI |
100 | (defvoo nnimap-authenticator nil |
101 | "How nnimap authenticate itself to the server. | |
102 | Possible choices are nil (use default methods) or `anonymous'.") | |
103 | ||
b069e5a6 G |
104 | (defvoo nnimap-expunge t |
105 | "If non-nil, expunge articles after deleting them. | |
106 | This is always done if the server supports UID EXPUNGE, but it's | |
107 | not done by default on servers that doesn't support that command.") | |
108 | ||
8ccbef23 G |
109 | (defvoo nnimap-streaming t |
110 | "If non-nil, try to use streaming commands with IMAP servers. | |
111 | Switching this off will make nnimap slower, but it helps with | |
112 | some servers.") | |
0617bb00 | 113 | |
20a673b2 | 114 | (defvoo nnimap-connection-alist nil) |
286c4fc2 LMI |
115 | |
116 | (defvoo nnimap-current-infos nil) | |
117 | ||
9f2d52e7 G |
118 | (defvoo nnimap-fetch-partial-articles nil |
119 | "If non-nil, Gnus will fetch partial articles. | |
120 | If t, nnimap will fetch only the first part. If a string, it | |
121 | will fetch all parts that have types that match that string. A | |
122 | likely value would be \"text/\" to automatically fetch all | |
123 | textual parts.") | |
124 | ||
20a673b2 KY |
125 | (defvar nnimap-process nil) |
126 | ||
127 | (defvar nnimap-status-string "") | |
23f87bed MB |
128 | |
129 | (defvar nnimap-split-download-body-default nil | |
130 | "Internal variable with default value for `nnimap-split-download-body'.") | |
131 | ||
61b1af82 G |
132 | (defvar nnimap-keepalive-timer nil) |
133 | (defvar nnimap-process-buffers nil) | |
134 | ||
20a673b2 | 135 | (defstruct nnimap |
61b1af82 | 136 | group process commands capabilities select-result newlinep server |
008cad90 | 137 | last-command-time greeting examined stream-type) |
c113de23 | 138 | |
20a673b2 KY |
139 | (defvar nnimap-object nil) |
140 | ||
141 | (defvar nnimap-mark-alist | |
b069e5a6 G |
142 | '((read "\\Seen" %Seen) |
143 | (tick "\\Flagged" %Flagged) | |
144 | (reply "\\Answered" %Answered) | |
20a673b2 KY |
145 | (expire "gnus-expire") |
146 | (dormant "gnus-dormant") | |
147 | (score "gnus-score") | |
148 | (save "gnus-save") | |
149 | (download "gnus-download") | |
150 | (forward "gnus-forward"))) | |
151 | ||
549c9aed G |
152 | (defvar nnimap-quirks |
153 | '(("QRESYNC" "Zimbra" "QRESYNC "))) | |
154 | ||
d5e9a4e9 LI |
155 | (defvar nnimap-inhibit-logging nil) |
156 | ||
20a673b2 KY |
157 | (defun nnimap-buffer () |
158 | (nnimap-find-process-buffer nntp-server-buffer)) | |
159 | ||
b5c575e6 G |
160 | (defun nnimap-header-parameters () |
161 | (format "(UID RFC822.SIZE BODYSTRUCTURE %s)" | |
162 | (format | |
163 | (if (nnimap-ver4-p) | |
164 | "BODY.PEEK[HEADER.FIELDS %s]" | |
165 | "RFC822.HEADER.LINES %s") | |
166 | (append '(Subject From Date Message-Id | |
167 | References In-Reply-To Xref) | |
168 | nnmail-extra-headers)))) | |
169 | ||
286c4fc2 | 170 | (deffoo nnimap-retrieve-headers (articles &optional group server fetch-old) |
e21bac42 G |
171 | (when group |
172 | (setq group (nnimap-decode-gnus-group group))) | |
c113de23 | 173 | (with-current-buffer nntp-server-buffer |
20a673b2 KY |
174 | (erase-buffer) |
175 | (when (nnimap-possibly-change-group group server) | |
176 | (with-current-buffer (nnimap-buffer) | |
20a673b2 KY |
177 | (erase-buffer) |
178 | (nnimap-wait-for-response | |
179 | (nnimap-send-command | |
180 | "UID FETCH %s %s" | |
181 | (nnimap-article-ranges (gnus-compress-sequence articles)) | |
b5c575e6 | 182 | (nnimap-header-parameters)) |
20a673b2 | 183 | t) |
70041e9a G |
184 | (nnimap-transform-headers) |
185 | (nnheader-remove-cr-followed-by-lf)) | |
20a673b2 KY |
186 | (insert-buffer-substring |
187 | (nnimap-find-process-buffer (current-buffer)))) | |
b1ae92ba | 188 | 'headers)) |
20a673b2 KY |
189 | |
190 | (defun nnimap-transform-headers () | |
191 | (goto-char (point-min)) | |
b1ae92ba | 192 | (let (article bytes lines size string) |
20a673b2 KY |
193 | (block nil |
194 | (while (not (eobp)) | |
e21bac42 | 195 | (while (not (looking-at "\\* [0-9]+ FETCH.+?UID \\([0-9]+\\)")) |
20a673b2 KY |
196 | (delete-region (point) (progn (forward-line 1) (point))) |
197 | (when (eobp) | |
198 | (return))) | |
b1ae92ba | 199 | (setq article (match-string 1)) |
733de8e2 LMI |
200 | ;; Unfold quoted {number} strings. |
201 | (while (re-search-forward "[^]][ (]{\\([0-9]+\\)}\r?\n" | |
202 | (1+ (line-end-position)) t) | |
203 | (setq size (string-to-number (match-string 1))) | |
204 | (delete-region (+ (match-beginning 0) 2) (point)) | |
205 | (setq string (buffer-substring (point) (+ (point) size))) | |
206 | (delete-region (point) (+ (point) size)) | |
207 | (insert (format "%S" string))) | |
b1ae92ba | 208 | (setq bytes (nnimap-get-length) |
20a673b2 KY |
209 | lines nil) |
210 | (beginning-of-line) | |
a46359d4 LMI |
211 | (setq size |
212 | (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)" | |
213 | (line-end-position) | |
214 | t) | |
215 | (match-string 1))) | |
216 | (beginning-of-line) | |
20a673b2 | 217 | (when (search-forward "BODYSTRUCTURE" (line-end-position) t) |
b1ae92ba G |
218 | (let ((structure (ignore-errors |
219 | (read (current-buffer))))) | |
20a673b2 | 220 | (while (and (consp structure) |
4d9db491 | 221 | (not (atom (car structure)))) |
20a673b2 | 222 | (setq structure (car structure))) |
fb568e63 | 223 | (setq lines (if (and |
4d9db491 | 224 | (stringp (car structure)) |
fb568e63 AC |
225 | (equal (upcase (nth 0 structure)) "MESSAGE") |
226 | (equal (upcase (nth 1 structure)) "RFC822")) | |
227 | (nth 9 structure) | |
228 | (nth 7 structure))))) | |
20a673b2 KY |
229 | (delete-region (line-beginning-position) (line-end-position)) |
230 | (insert (format "211 %s Article retrieved." article)) | |
231 | (forward-line 1) | |
a46359d4 LMI |
232 | (when size |
233 | (insert (format "Chars: %s\n" size))) | |
20a673b2 KY |
234 | (when lines |
235 | (insert (format "Lines: %s\n" lines))) | |
b5c575e6 G |
236 | (unless (re-search-forward "^\r$" nil t) |
237 | (goto-char (point-max))) | |
20a673b2 KY |
238 | (delete-region (line-beginning-position) (line-end-position)) |
239 | (insert ".") | |
240 | (forward-line 1))))) | |
241 | ||
a56a1cce LMI |
242 | (defun nnimap-unfold-quoted-lines () |
243 | ;; Unfold quoted {number} strings. | |
733de8e2 LMI |
244 | (let (size string) |
245 | (while (re-search-forward " {\\([0-9]+\\)}\r?\n" nil t) | |
246 | (setq size (string-to-number (match-string 1))) | |
247 | (delete-region (1+ (match-beginning 0)) (point)) | |
248 | (setq string (buffer-substring (point) (+ (point) size))) | |
249 | (delete-region (point) (+ (point) size)) | |
250 | (insert (format "%S" string))))) | |
a56a1cce | 251 | |
20a673b2 KY |
252 | (defun nnimap-get-length () |
253 | (and (re-search-forward "{\\([0-9]+\\)}" (line-end-position) t) | |
254 | (string-to-number (match-string 1)))) | |
255 | ||
256 | (defun nnimap-article-ranges (ranges) | |
257 | (let (result) | |
258 | (cond | |
259 | ((numberp ranges) | |
260 | (number-to-string ranges)) | |
261 | ((numberp (cdr ranges)) | |
262 | (format "%d:%d" (car ranges) (cdr ranges))) | |
263 | (t | |
264 | (dolist (elem ranges) | |
265 | (push | |
266 | (if (consp elem) | |
267 | (format "%d:%d" (car elem) (cdr elem)) | |
268 | (number-to-string elem)) | |
269 | result)) | |
270 | (mapconcat #'identity (nreverse result) ","))))) | |
271 | ||
286c4fc2 | 272 | (deffoo nnimap-open-server (server &optional defs) |
c113de23 GM |
273 | (if (nnimap-server-opened server) |
274 | t | |
c113de23 | 275 | (unless (assq 'nnimap-address defs) |
20a673b2 | 276 | (setq defs (append defs (list (list 'nnimap-address server))))) |
c113de23 | 277 | (nnoo-change-server 'nnimap server defs) |
20a673b2 KY |
278 | (or (nnimap-find-connection nntp-server-buffer) |
279 | (nnimap-open-connection nntp-server-buffer)))) | |
280 | ||
281 | (defun nnimap-make-process-buffer (buffer) | |
282 | (with-current-buffer | |
283 | (generate-new-buffer (format "*nnimap %s %s %s*" | |
284 | nnimap-address nnimap-server-port | |
285 | (gnus-buffer-exists-p buffer))) | |
286 | (mm-disable-multibyte) | |
287 | (buffer-disable-undo) | |
288 | (gnus-add-buffer) | |
289 | (set (make-local-variable 'after-change-functions) nil) | |
b069e5a6 G |
290 | (set (make-local-variable 'nnimap-object) |
291 | (make-nnimap :server (nnoo-current-server 'nnimap))) | |
20a673b2 | 292 | (push (list buffer (current-buffer)) nnimap-connection-alist) |
61b1af82 | 293 | (push (current-buffer) nnimap-process-buffers) |
20a673b2 KY |
294 | (current-buffer))) |
295 | ||
5e68f861 | 296 | (defun nnimap-credentials (address ports user) |
733afdf4 TZ |
297 | (let* ((auth-source-creation-prompts |
298 | '((user . "IMAP user at %h: ") | |
299 | (secret . "IMAP password for %u@%h: "))) | |
300 | (found (nth 0 (auth-source-search :max 1 | |
301 | :host address | |
302 | :port ports | |
5e68f861 | 303 | :user user |
733afdf4 TZ |
304 | :require '(:user :secret) |
305 | :create t)))) | |
b8e0f0cd | 306 | (if found |
c13bc26b LI |
307 | (list (plist-get found :user) |
308 | (let ((secret (plist-get found :secret))) | |
309 | (if (functionp secret) | |
310 | (funcall secret) | |
733afdf4 TZ |
311 | secret)) |
312 | (plist-get found :save-function)) | |
b8e0f0cd | 313 | nil))) |
286c4fc2 | 314 | |
61b1af82 G |
315 | (defun nnimap-keepalive () |
316 | (let ((now (current-time))) | |
317 | (dolist (buffer nnimap-process-buffers) | |
318 | (when (buffer-name buffer) | |
319 | (with-current-buffer buffer | |
320 | (when (and nnimap-object | |
321 | (nnimap-last-command-time nnimap-object) | |
2b1e1ff4 | 322 | (> (gnus-float-time |
61b1af82 G |
323 | (time-subtract |
324 | now | |
325 | (nnimap-last-command-time nnimap-object))) | |
326 | ;; More than five minutes since the last command. | |
327 | (* 5 60))) | |
328 | (nnimap-send-command "NOOP"))))))) | |
329 | ||
20a673b2 | 330 | (defun nnimap-open-connection (buffer) |
ed797193 G |
331 | ;; Be backwards-compatible -- the earlier value of nnimap-stream was |
332 | ;; `ssl' when nnimap-server-port was nil. Sort of. | |
333 | (when (and nnimap-server-port | |
334 | (eq nnimap-stream 'undecided)) | |
335 | (setq nnimap-stream 'ssl)) | |
336 | (let ((stream | |
337 | (if (eq nnimap-stream 'undecided) | |
e742e117 | 338 | (loop for type in '(ssl network) |
ed797193 G |
339 | for stream = (let ((nnimap-stream type)) |
340 | (nnimap-open-connection-1 buffer)) | |
341 | while (eq stream 'no-connect) | |
342 | finally (return stream)) | |
343 | (nnimap-open-connection-1 buffer)))) | |
344 | (if (eq stream 'no-connect) | |
345 | nil | |
346 | stream))) | |
347 | ||
a5166359 LMI |
348 | (defun nnimap-map-port (port) |
349 | (if (equal port "imaps") | |
350 | "993" | |
351 | port)) | |
352 | ||
ed797193 | 353 | (defun nnimap-open-connection-1 (buffer) |
61b1af82 G |
354 | (unless nnimap-keepalive-timer |
355 | (setq nnimap-keepalive-timer (run-at-time (* 60 15) (* 60 15) | |
356 | 'nnimap-keepalive))) | |
ed797193 G |
357 | (with-current-buffer (nnimap-make-process-buffer buffer) |
358 | (let* ((coding-system-for-read 'binary) | |
359 | (coding-system-for-write 'binary) | |
ed797193 | 360 | (ports |
dab0271f | 361 | (cond |
e742e117 | 362 | ((memq nnimap-stream '(network plain starttls)) |
ed797193 G |
363 | (nnheader-message 7 "Opening connection to %s..." |
364 | nnimap-address) | |
003522ce | 365 | '("imap" "143")) |
ed797193 G |
366 | ((eq nnimap-stream 'shell) |
367 | (nnheader-message 7 "Opening connection to %s via shell..." | |
368 | nnimap-address) | |
369 | '("imap")) | |
370 | ((memq nnimap-stream '(ssl tls)) | |
371 | (nnheader-message 7 "Opening connection to %s via tls..." | |
372 | nnimap-address) | |
003522ce | 373 | '("imaps" "imap" "993" "143")) |
ed797193 G |
374 | (t |
375 | (error "Unknown stream type: %s" nnimap-stream)))) | |
ed797193 G |
376 | login-result credentials) |
377 | (when nnimap-server-port | |
003522ce | 378 | (push nnimap-server-port ports)) |
f2eefd24 CY |
379 | (let* ((stream-list |
380 | (open-protocol-stream | |
a5166359 LMI |
381 | "*nnimap*" (current-buffer) nnimap-address |
382 | (nnimap-map-port (car ports)) | |
f2eefd24 CY |
383 | :type nnimap-stream |
384 | :return-list t | |
385 | :shell-command nnimap-shell-program | |
386 | :capability-command "1 CAPABILITY\r\n" | |
da91b5f2 | 387 | :end-of-command "\r\n" |
f2eefd24 CY |
388 | :success " OK " |
389 | :starttls-function | |
390 | (lambda (capabilities) | |
391 | (when (gnus-string-match-p "STARTTLS" capabilities) | |
392 | "1 STARTTLS\r\n")))) | |
393 | (stream (car stream-list)) | |
394 | (props (cdr stream-list)) | |
395 | (greeting (plist-get props :greeting)) | |
396 | (capabilities (plist-get props :capabilities)) | |
397 | (stream-type (plist-get props :type))) | |
398 | (when (and stream (not (memq (process-status stream) '(open run)))) | |
399 | (setq stream nil)) | |
3ccc1742 SM |
400 | |
401 | (when (and (fboundp 'set-network-process-option) ;; Not in XEmacs. | |
402 | (fboundp 'process-type) ;; Emacs 22 doesn't provide it. | |
403 | (eq (process-type stream) 'network)) | |
404 | ;; Use TCP-keepalive so that connections that pass through a NAT | |
405 | ;; router don't hang when left idle. | |
406 | (set-network-process-option stream :keepalive t)) | |
407 | ||
ed797193 | 408 | (setf (nnimap-process nnimap-object) stream) |
008cad90 | 409 | (setf (nnimap-stream-type nnimap-object) stream-type) |
ed797193 G |
410 | (if (not stream) |
411 | (progn | |
412 | (nnheader-report 'nnimap "Unable to contact %s:%s via %s" | |
bc320087 | 413 | nnimap-address (car ports) nnimap-stream) |
ed797193 G |
414 | 'no-connect) |
415 | (gnus-set-process-query-on-exit-flag stream nil) | |
416 | (if (not (gnus-string-match-p "[*.] \\(OK\\|PREAUTH\\)" greeting)) | |
417 | (nnheader-report 'nnimap "%s" greeting) | |
418 | ;; Store the greeting (for debugging purposes). | |
419 | (setf (nnimap-greeting nnimap-object) greeting) | |
420 | (setf (nnimap-capabilities nnimap-object) | |
421 | (mapcar #'upcase | |
422 | (split-string capabilities))) | |
423 | (unless (gnus-string-match-p "[*.] PREAUTH" greeting) | |
9f2d52e7 G |
424 | (if (not (setq credentials |
425 | (if (eq nnimap-authenticator 'anonymous) | |
426 | (list "anonymous" | |
427 | (message-make-address)) | |
b8e0f0cd G |
428 | ;; Look for the credentials based on |
429 | ;; the virtual server name and the address | |
430 | (nnimap-credentials | |
7ba93e94 G |
431 | (gnus-delete-duplicates |
432 | (list | |
433 | nnimap-address | |
434 | (nnoo-current-server 'nnimap))) | |
5e68f861 TZ |
435 | ports |
436 | nnimap-user)))) | |
9f2d52e7 | 437 | (setq nnimap-object nil) |
d5e9a4e9 LI |
438 | (let ((nnimap-inhibit-logging t)) |
439 | (setq login-result | |
440 | (nnimap-login (car credentials) (cadr credentials)))) | |
733afdf4 | 441 | (if (car login-result) |
a5954fa5 | 442 | (progn |
b534ed40 LMI |
443 | ;; Save the credentials if a save function exists |
444 | ;; (such a function will only be passed if a new | |
445 | ;; token was created). | |
a5954fa5 G |
446 | (when (functionp (nth 2 credentials)) |
447 | (funcall (nth 2 credentials))) | |
448 | ;; See if CAPABILITY is set as part of login | |
449 | ;; response. | |
450 | (dolist (response (cddr login-result)) | |
451 | (when (string= "CAPABILITY" (upcase (car response))) | |
452 | (setf (nnimap-capabilities nnimap-object) | |
453 | (mapcar #'upcase (cdr response)))))) | |
9f2d52e7 G |
454 | ;; If the login failed, then forget the credentials |
455 | ;; that are now possibly cached. | |
456 | (dolist (host (list (nnoo-current-server 'nnimap) | |
457 | nnimap-address)) | |
458 | (dolist (port ports) | |
35123c04 | 459 | (auth-source-forget+ :host host :port port))) |
9f2d52e7 G |
460 | (delete-process (nnimap-process nnimap-object)) |
461 | (setq nnimap-object nil)))) | |
462 | (when nnimap-object | |
389b76fa | 463 | (when (nnimap-capability "QRESYNC") |
9f2d52e7 | 464 | (nnimap-command "ENABLE QRESYNC")) |
6b958814 G |
465 | (nnimap-process nnimap-object)))))))) |
466 | ||
84d89ede LMI |
467 | (autoload 'rfc2104-hash "rfc2104") |
468 | ||
469 | (defun nnimap-login (user password) | |
470 | (cond | |
008cad90 G |
471 | ;; Prefer plain LOGIN if it's enabled (since it requires fewer |
472 | ;; round trips than CRAM-MD5, and it's less likely to be buggy), | |
473 | ;; and we're using an encrypted connection. | |
474 | ((and (not (nnimap-capability "LOGINDISABLED")) | |
475 | (eq (nnimap-stream-type nnimap-object) 'tls)) | |
476 | (nnimap-command "LOGIN %S %S" user password)) | |
84d89ede LMI |
477 | ((nnimap-capability "AUTH=CRAM-MD5") |
478 | (erase-buffer) | |
479 | (let ((sequence (nnimap-send-command "AUTHENTICATE CRAM-MD5")) | |
480 | (challenge (nnimap-wait-for-line "^\\+\\(.*\\)\n"))) | |
481 | (process-send-string | |
482 | (get-buffer-process (current-buffer)) | |
483 | (concat | |
484 | (base64-encode-string | |
485 | (concat user " " | |
486 | (rfc2104-hash 'md5 64 16 password | |
487 | (base64-decode-string challenge)))) | |
488 | "\r\n")) | |
489 | (nnimap-wait-for-response sequence))) | |
490 | ((not (nnimap-capability "LOGINDISABLED")) | |
491 | (nnimap-command "LOGIN %S %S" user password)) | |
492 | ((nnimap-capability "AUTH=PLAIN") | |
493 | (nnimap-command | |
494 | "AUTHENTICATE PLAIN %s" | |
495 | (base64-encode-string | |
496 | (format "\000%s\000%s" | |
497 | (nnimap-quote-specials user) | |
498 | (nnimap-quote-specials password))))))) | |
499 | ||
99e65b2d G |
500 | (defun nnimap-quote-specials (string) |
501 | (with-temp-buffer | |
502 | (insert string) | |
503 | (goto-char (point-min)) | |
504 | (while (re-search-forward "[\\\"]" nil t) | |
505 | (forward-char -1) | |
506 | (insert "\\") | |
507 | (forward-char 1)) | |
508 | (buffer-string))) | |
509 | ||
20a673b2 KY |
510 | (defun nnimap-find-parameter (parameter elems) |
511 | (let (result) | |
512 | (dolist (elem elems) | |
513 | (cond | |
514 | ((equal (car elem) parameter) | |
515 | (setq result (cdr elem))) | |
516 | ((and (equal (car elem) "OK") | |
517 | (consp (cadr elem)) | |
518 | (equal (caadr elem) parameter)) | |
519 | (setq result (cdr (cadr elem)))))) | |
520 | result)) | |
521 | ||
286c4fc2 | 522 | (deffoo nnimap-close-server (&optional server) |
71e691a5 G |
523 | (when (nnoo-change-server 'nnimap server nil) |
524 | (ignore-errors | |
525 | (delete-process (get-buffer-process (nnimap-buffer)))) | |
d1090fe8 | 526 | (nnoo-close-server 'nnimap server) |
71e691a5 | 527 | t)) |
c113de23 | 528 | |
286c4fc2 | 529 | (deffoo nnimap-request-close () |
20a673b2 | 530 | t) |
23f87bed | 531 | |
286c4fc2 | 532 | (deffoo nnimap-server-opened (&optional server) |
20a673b2 KY |
533 | (and (nnoo-current-server-p 'nnimap server) |
534 | nntp-server-buffer | |
535 | (gnus-buffer-live-p nntp-server-buffer) | |
536 | (nnimap-find-connection nntp-server-buffer))) | |
c113de23 | 537 | |
286c4fc2 | 538 | (deffoo nnimap-status-message (&optional server) |
20a673b2 | 539 | nnimap-status-string) |
c113de23 | 540 | |
286c4fc2 | 541 | (deffoo nnimap-request-article (article &optional group server to-buffer) |
e21bac42 G |
542 | (when group |
543 | (setq group (nnimap-decode-gnus-group group))) | |
c113de23 | 544 | (with-current-buffer nntp-server-buffer |
bdaa75c7 | 545 | (let ((result (nnimap-possibly-change-group group server)) |
8ccbef23 | 546 | parts structure) |
20a673b2 KY |
547 | (when (stringp article) |
548 | (setq article (nnimap-find-article-by-message-id group article))) | |
549 | (when (and result | |
550 | article) | |
551 | (erase-buffer) | |
552 | (with-current-buffer (nnimap-buffer) | |
553 | (erase-buffer) | |
9f2d52e7 G |
554 | (when nnimap-fetch-partial-articles |
555 | (nnimap-command "UID FETCH %d (BODYSTRUCTURE)" article) | |
556 | (goto-char (point-min)) | |
557 | (when (re-search-forward "FETCH.*BODYSTRUCTURE" nil t) | |
229b59da G |
558 | (setq structure (ignore-errors |
559 | (let ((start (point))) | |
560 | (forward-sexp 1) | |
561 | (downcase-region start (point)) | |
9d1bf25d | 562 | (goto-char start) |
229b59da | 563 | (read (current-buffer)))) |
9f2d52e7 | 564 | parts (nnimap-find-wanted-parts structure)))) |
8ccbef23 G |
565 | (when (if parts |
566 | (nnimap-get-partial-article article parts structure) | |
567 | (nnimap-get-whole-article article)) | |
568 | (let ((buffer (current-buffer))) | |
569 | (with-current-buffer (or to-buffer nntp-server-buffer) | |
9f5e78f7 | 570 | (nnheader-insert-buffer-substring buffer) |
b87f32fc G |
571 | (nnheader-ms-strip-cr))) |
572 | (cons group article))))))) | |
8ccbef23 | 573 | |
b5c575e6 | 574 | (deffoo nnimap-request-head (article &optional group server to-buffer) |
e21bac42 G |
575 | (when group |
576 | (setq group (nnimap-decode-gnus-group group))) | |
b5c575e6 G |
577 | (when (nnimap-possibly-change-group group server) |
578 | (with-current-buffer (nnimap-buffer) | |
579 | (when (stringp article) | |
580 | (setq article (nnimap-find-article-by-message-id group article))) | |
228724bc LI |
581 | (if (null article) |
582 | nil | |
583 | (nnimap-get-whole-article | |
584 | article (format "UID FETCH %%d %s" | |
585 | (nnimap-header-parameters))) | |
586 | (let ((buffer (current-buffer))) | |
587 | (with-current-buffer (or to-buffer nntp-server-buffer) | |
588 | (erase-buffer) | |
589 | (insert-buffer-substring buffer) | |
590 | (nnheader-ms-strip-cr) | |
591 | (cons group article))))))) | |
b5c575e6 G |
592 | |
593 | (defun nnimap-get-whole-article (article &optional command) | |
8ccbef23 G |
594 | (let ((result |
595 | (nnimap-command | |
b5c575e6 G |
596 | (or command |
597 | (if (nnimap-ver4-p) | |
598 | "UID FETCH %d BODY.PEEK[]" | |
599 | "UID FETCH %d RFC822.PEEK")) | |
8ccbef23 G |
600 | article))) |
601 | ;; Check that we really got an article. | |
602 | (goto-char (point-min)) | |
4478e074 | 603 | (unless (re-search-forward "\\* [0-9]+ FETCH" nil t) |
8ccbef23 G |
604 | (setq result nil)) |
605 | (when result | |
4478e074 G |
606 | ;; Remove any data that may have arrived before the FETCH data. |
607 | (beginning-of-line) | |
608 | (unless (bobp) | |
609 | (delete-region (point-min) (point))) | |
8ccbef23 G |
610 | (let ((bytes (nnimap-get-length))) |
611 | (delete-region (line-beginning-position) | |
612 | (progn (forward-line 1) (point))) | |
613 | (goto-char (+ (point) bytes)) | |
614 | (delete-region (point) (point-max))) | |
615 | t))) | |
616 | ||
389b76fa G |
617 | (defun nnimap-capability (capability) |
618 | (member capability (nnimap-capabilities nnimap-object))) | |
619 | ||
8ccbef23 | 620 | (defun nnimap-ver4-p () |
389b76fa | 621 | (nnimap-capability "IMAP4REV1")) |
8ccbef23 G |
622 | |
623 | (defun nnimap-get-partial-article (article parts structure) | |
624 | (let ((result | |
625 | (nnimap-command | |
626 | "UID FETCH %d (%s %s)" | |
627 | article | |
628 | (if (nnimap-ver4-p) | |
629 | "BODY.PEEK[HEADER]" | |
630 | "RFC822.HEADER") | |
631 | (if (nnimap-ver4-p) | |
632 | (mapconcat (lambda (part) | |
633 | (format "BODY.PEEK[%s]" part)) | |
634 | parts " ") | |
635 | (mapconcat (lambda (part) | |
636 | (format "RFC822.PEEK[%s]" part)) | |
637 | parts " "))))) | |
638 | (when result | |
639 | (nnimap-convert-partial-article structure)))) | |
640 | ||
641 | (defun nnimap-convert-partial-article (structure) | |
642 | ;; First just skip past the headers. | |
643 | (goto-char (point-min)) | |
644 | (let ((bytes (nnimap-get-length)) | |
645 | id parts) | |
646 | ;; Delete "FETCH" line. | |
647 | (delete-region (line-beginning-position) | |
648 | (progn (forward-line 1) (point))) | |
649 | (goto-char (+ (point) bytes)) | |
650 | ;; Collect all the body parts. | |
651 | (while (looking-at ".*BODY\\[\\([.0-9]+\\)\\]") | |
652 | (setq id (match-string 1) | |
c516cd6d | 653 | bytes (or (nnimap-get-length) 0)) |
8ccbef23 G |
654 | (beginning-of-line) |
655 | (delete-region (point) (progn (forward-line 1) (point))) | |
656 | (push (list id (buffer-substring (point) (+ (point) bytes))) | |
657 | parts) | |
658 | (delete-region (point) (+ (point) bytes))) | |
659 | ;; Delete trailing junk. | |
660 | (delete-region (point) (point-max)) | |
661 | ;; Now insert all the parts again where they fit in the structure. | |
662 | (nnimap-insert-partial-structure structure parts) | |
663 | t)) | |
664 | ||
665 | (defun nnimap-insert-partial-structure (structure parts &optional subp) | |
229b59da G |
666 | (let (type boundary) |
667 | (let ((bstruc structure)) | |
668 | (while (consp (car bstruc)) | |
669 | (pop bstruc)) | |
670 | (setq type (car bstruc)) | |
671 | (setq bstruc (car (cdr bstruc))) | |
2526f423 G |
672 | (let ((has-boundary (member "boundary" bstruc))) |
673 | (when has-boundary | |
674 | (setq boundary (cadr has-boundary))))) | |
8ccbef23 G |
675 | (when subp |
676 | (insert (format "Content-type: multipart/%s; boundary=%S\n\n" | |
677 | (downcase type) boundary))) | |
678 | (while (not (stringp (car structure))) | |
679 | (insert "\n--" boundary "\n") | |
680 | (if (consp (caar structure)) | |
681 | (nnimap-insert-partial-structure (pop structure) parts t) | |
682 | (let ((bit (pop structure))) | |
943399bc LMI |
683 | (insert (format "Content-type: %s/%s" |
684 | (downcase (nth 0 bit)) | |
685 | (downcase (nth 1 bit)))) | |
686 | (if (member-ignore-case "CHARSET" (nth 2 bit)) | |
8ccbef23 | 687 | (insert (format |
943399bc LMI |
688 | "; charset=%S\n" |
689 | (cadr (member-ignore-case "CHARSET" (nth 2 bit))))) | |
8ccbef23 G |
690 | (insert "\n")) |
691 | (insert (format "Content-transfer-encoding: %s\n" | |
692 | (nth 5 bit))) | |
693 | (insert "\n") | |
694 | (when (assoc (nth 9 bit) parts) | |
695 | (insert (cadr (assoc (nth 9 bit) parts))))))) | |
696 | (insert "\n--" boundary "--\n"))) | |
bdaa75c7 LMI |
697 | |
698 | (defun nnimap-find-wanted-parts (structure) | |
699 | (message-flatten-list (nnimap-find-wanted-parts-1 structure ""))) | |
700 | ||
701 | (defun nnimap-find-wanted-parts-1 (structure prefix) | |
702 | (let ((num 1) | |
703 | parts) | |
704 | (while (consp (car structure)) | |
705 | (let ((sub (pop structure))) | |
706 | (if (consp (car sub)) | |
707 | (push (nnimap-find-wanted-parts-1 | |
708 | sub (if (string= prefix "") | |
709 | (number-to-string num) | |
710 | (format "%s.%s" prefix num))) | |
711 | parts) | |
8ccbef23 G |
712 | (let ((type (format "%s/%s" (nth 0 sub) (nth 1 sub))) |
713 | (id (if (string= prefix "") | |
bdaa75c7 | 714 | (number-to-string num) |
8ccbef23 G |
715 | (format "%s.%s" prefix num)))) |
716 | (setcar (nthcdr 9 sub) id) | |
9f2d52e7 G |
717 | (when (if (eq nnimap-fetch-partial-articles t) |
718 | (equal id "1") | |
719 | (string-match nnimap-fetch-partial-articles type)) | |
8ccbef23 G |
720 | (push id parts)))) |
721 | (incf num))) | |
bdaa75c7 | 722 | (nreverse parts))) |
20a673b2 | 723 | |
e21bac42 G |
724 | (defun nnimap-decode-gnus-group (group) |
725 | (decode-coding-string group 'utf-8)) | |
726 | ||
286c4fc2 | 727 | (deffoo nnimap-request-group (group &optional server dont-check info) |
e21bac42 | 728 | (setq group (nnimap-decode-gnus-group group)) |
7cad71ad G |
729 | (let ((result (nnimap-possibly-change-group |
730 | ;; Don't SELECT the group if we're going to select it | |
731 | ;; later, anyway. | |
bb7f5cbc | 732 | (if (and (not dont-check) |
9310f19d | 733 | (assoc group nnimap-current-infos)) |
7cad71ad G |
734 | nil |
735 | group) | |
736 | server)) | |
a46359d4 LMI |
737 | articles active marks high low) |
738 | (with-current-buffer nntp-server-buffer | |
20a673b2 | 739 | (when result |
286c4fc2 LMI |
740 | (if (and dont-check |
741 | (setq active (nth 2 (assoc group nnimap-current-infos)))) | |
742 | (insert (format "211 %d %d %d %S\n" | |
743 | (- (cdr active) (car active)) | |
744 | (car active) | |
745 | (cdr active) | |
746 | group)) | |
747 | (with-current-buffer (nnimap-buffer) | |
748 | (erase-buffer) | |
749 | (let ((group-sequence | |
0617bb00 | 750 | (nnimap-send-command "SELECT %S" (utf7-encode group t))) |
286c4fc2 LMI |
751 | (flag-sequence |
752 | (nnimap-send-command "UID FETCH 1:* FLAGS"))) | |
7cad71ad | 753 | (setf (nnimap-group nnimap-object) group) |
286c4fc2 LMI |
754 | (nnimap-wait-for-response flag-sequence) |
755 | (setq marks | |
756 | (nnimap-flags-to-marks | |
757 | (nnimap-parse-flags | |
f7aa248a G |
758 | (list (list group-sequence flag-sequence |
759 | 1 group "SELECT"))))) | |
760 | (when (and info | |
761 | marks) | |
9310f19d LMI |
762 | (nnimap-update-infos marks (list info)) |
763 | (nnimap-store-info info (gnus-active (gnus-info-group info)))) | |
286c4fc2 | 764 | (goto-char (point-max)) |
b1ae92ba | 765 | (let ((uidnext (nth 5 (car marks)))) |
a3f57c41 G |
766 | (setq high (or (if uidnext |
767 | (1- uidnext) | |
768 | (nth 3 (car marks))) | |
769 | 0) | |
770 | low (or (nth 4 (car marks)) uidnext 1))))) | |
286c4fc2 LMI |
771 | (erase-buffer) |
772 | (insert | |
773 | (format | |
0617bb00 LMI |
774 | "211 %d %d %d %S\n" (1+ (- high low)) low high group))) |
775 | t)))) | |
776 | ||
777 | (deffoo nnimap-request-create-group (group &optional server args) | |
e21bac42 | 778 | (setq group (nnimap-decode-gnus-group group)) |
0617bb00 LMI |
779 | (when (nnimap-possibly-change-group nil server) |
780 | (with-current-buffer (nnimap-buffer) | |
781 | (car (nnimap-command "CREATE %S" (utf7-encode group t)))))) | |
c113de23 | 782 | |
a46359d4 | 783 | (deffoo nnimap-request-delete-group (group &optional force server) |
e21bac42 | 784 | (setq group (nnimap-decode-gnus-group group)) |
a46359d4 LMI |
785 | (when (nnimap-possibly-change-group nil server) |
786 | (with-current-buffer (nnimap-buffer) | |
0617bb00 LMI |
787 | (car (nnimap-command "DELETE %S" (utf7-encode group t)))))) |
788 | ||
a7dcc87b | 789 | (deffoo nnimap-request-rename-group (group new-name &optional server) |
e21bac42 | 790 | (setq group (nnimap-decode-gnus-group group)) |
a7dcc87b G |
791 | (when (nnimap-possibly-change-group nil server) |
792 | (with-current-buffer (nnimap-buffer) | |
e39a5583 | 793 | (nnimap-unselect-group) |
f7aa248a G |
794 | (car (nnimap-command "RENAME %S %S" |
795 | (utf7-encode group t) (utf7-encode new-name t)))))) | |
a7dcc87b | 796 | |
e39a5583 LMI |
797 | (defun nnimap-unselect-group () |
798 | ;; Make sure we don't have this group open read/write by asking | |
799 | ;; to examine a mailbox that doesn't exist. This seems to be | |
800 | ;; the only way that allows us to reliably go back to unselected | |
801 | ;; state on Courier. | |
802 | (nnimap-command "EXAMINE DOES.NOT.EXIST")) | |
803 | ||
0617bb00 | 804 | (deffoo nnimap-request-expunge-group (group &optional server) |
e21bac42 | 805 | (setq group (nnimap-decode-gnus-group group)) |
0617bb00 LMI |
806 | (when (nnimap-possibly-change-group group server) |
807 | (with-current-buffer (nnimap-buffer) | |
808 | (car (nnimap-command "EXPUNGE"))))) | |
a46359d4 | 809 | |
20a673b2 KY |
810 | (defun nnimap-get-flags (spec) |
811 | (let ((articles nil) | |
f7aa248a | 812 | elems end) |
20a673b2 | 813 | (with-current-buffer (nnimap-buffer) |
c113de23 | 814 | (erase-buffer) |
20a673b2 KY |
815 | (nnimap-wait-for-response (nnimap-send-command |
816 | "UID FETCH %s FLAGS" spec)) | |
f7aa248a G |
817 | (setq end (point)) |
818 | (subst-char-in-region (point-min) (point-max) | |
819 | ?\\ ?% t) | |
20a673b2 | 820 | (goto-char (point-min)) |
f7aa248a G |
821 | (while (search-forward " FETCH " end t) |
822 | (setq elems (read (current-buffer))) | |
823 | (push (cons (cadr (memq 'UID elems)) | |
824 | (cadr (memq 'FLAGS elems))) | |
20a673b2 KY |
825 | articles))) |
826 | (nreverse articles))) | |
a1506d29 | 827 | |
286c4fc2 | 828 | (deffoo nnimap-close-group (group &optional server) |
20a673b2 | 829 | t) |
c113de23 | 830 | |
01c52d31 | 831 | (deffoo nnimap-request-move-article (article group server accept-form |
20a673b2 | 832 | &optional last internal-move-group) |
e21bac42 | 833 | (setq group (nnimap-decode-gnus-group group)) |
0617bb00 | 834 | (with-temp-buffer |
a04f9e26 | 835 | (mm-disable-multibyte) |
b5c575e6 G |
836 | (when (funcall (if internal-move-group |
837 | 'nnimap-request-head | |
838 | 'nnimap-request-article) | |
839 | article group server (current-buffer)) | |
0617bb00 LMI |
840 | ;; If the move is internal (on the same server), just do it the easy |
841 | ;; way. | |
842 | (let ((message-id (message-field-value "message-id"))) | |
843 | (if internal-move-group | |
844 | (let ((result | |
845 | (with-current-buffer (nnimap-buffer) | |
846 | (nnimap-command "UID COPY %d %S" | |
847 | article | |
848 | (utf7-encode internal-move-group t))))) | |
849 | (when (car result) | |
a46359d4 | 850 | (nnimap-delete-article article) |
0617bb00 | 851 | (cons internal-move-group |
cccb4b4c LMI |
852 | (or (nnimap-find-uid-response "COPYUID" (cadr result)) |
853 | (nnimap-find-article-by-message-id | |
854 | internal-move-group message-id))))) | |
0617bb00 LMI |
855 | ;; Move the article to a different method. |
856 | (let ((result (eval accept-form))) | |
857 | (when result | |
858 | (nnimap-delete-article article) | |
859 | result))))))) | |
20a673b2 KY |
860 | |
861 | (deffoo nnimap-request-expire-articles (articles group &optional server force) | |
e21bac42 | 862 | (setq group (nnimap-decode-gnus-group group)) |
20a673b2 | 863 | (cond |
0617bb00 LMI |
864 | ((null articles) |
865 | nil) | |
20a673b2 KY |
866 | ((not (nnimap-possibly-change-group group server)) |
867 | articles) | |
0617bb00 LMI |
868 | ((and force |
869 | (eq nnmail-expiry-target 'delete)) | |
4478e074 | 870 | (unless (nnimap-delete-article (gnus-compress-sequence articles)) |
283f7b93 | 871 | (nnheader-message 7 "Article marked for deletion, but not expunged.")) |
20a673b2 KY |
872 | nil) |
873 | (t | |
0617bb00 | 874 | (let ((deletable-articles |
b069e5a6 G |
875 | (if (or force |
876 | (eq nnmail-expiry-wait 'immediate)) | |
0617bb00 LMI |
877 | articles |
878 | (gnus-sorted-intersection | |
879 | articles | |
880 | (nnimap-find-expired-articles group))))) | |
881 | (if (null deletable-articles) | |
882 | articles | |
883 | (if (eq nnmail-expiry-target 'delete) | |
4478e074 | 884 | (nnimap-delete-article (gnus-compress-sequence deletable-articles)) |
0617bb00 LMI |
885 | (setq deletable-articles |
886 | (nnimap-process-expiry-targets | |
887 | deletable-articles group server))) | |
888 | ;; Return the articles we didn't delete. | |
889 | (gnus-sorted-complement articles deletable-articles)))))) | |
890 | ||
891 | (defun nnimap-process-expiry-targets (articles group server) | |
892 | (let ((deleted-articles nil)) | |
04db63bc G |
893 | (cond |
894 | ;; shortcut further processing if we're going to delete the articles | |
895 | ((eq nnmail-expiry-target 'delete) | |
896 | (setq deleted-articles articles) | |
897 | t) | |
898 | ;; or just move them to another folder on the same IMAP server | |
899 | ((and (not (functionp nnmail-expiry-target)) | |
900 | (gnus-server-equal (gnus-group-method nnmail-expiry-target) | |
901 | (gnus-server-to-method | |
902 | (format "nnimap:%s" server)))) | |
903 | (and (nnimap-possibly-change-group group server) | |
904 | (with-current-buffer (nnimap-buffer) | |
905 | (nnheader-message 7 "Expiring articles from %s: %s" group articles) | |
906 | (nnimap-command | |
907 | "UID COPY %s %S" | |
908 | (nnimap-article-ranges (gnus-compress-sequence articles)) | |
909 | (utf7-encode (gnus-group-real-name nnmail-expiry-target) t)) | |
910 | (setq deleted-articles articles))) | |
911 | t) | |
912 | (t | |
913 | (dolist (article articles) | |
914 | (let ((target nnmail-expiry-target)) | |
915 | (with-temp-buffer | |
916 | (mm-disable-multibyte) | |
917 | (when (nnimap-request-article article group server (current-buffer)) | |
04db63bc G |
918 | (when (functionp target) |
919 | (setq target (funcall target group))) | |
d0b36cbe LMI |
920 | (if (and target |
921 | (not (eq target 'delete))) | |
922 | (if (or (gnus-request-group target t) | |
923 | (gnus-request-create-group target)) | |
924 | (progn | |
925 | (nnmail-expiry-target-group target group) | |
926 | (nnheader-message 7 "Expiring article %s:%d to %s" | |
927 | group article target)) | |
928 | (setq target nil)) | |
929 | (nnheader-message 7 "Expiring article %s:%d" group article)) | |
04db63bc G |
930 | (when target |
931 | (push article deleted-articles)))))))) | |
0617bb00 LMI |
932 | ;; Change back to the current group again. |
933 | (nnimap-possibly-change-group group server) | |
934 | (setq deleted-articles (nreverse deleted-articles)) | |
4478e074 | 935 | (nnimap-delete-article (gnus-compress-sequence deleted-articles)) |
0617bb00 LMI |
936 | deleted-articles)) |
937 | ||
938 | (defun nnimap-find-expired-articles (group) | |
939 | (let ((cutoff (nnmail-expired-article-p group nil nil))) | |
940 | (with-current-buffer (nnimap-buffer) | |
941 | (let ((result | |
942 | (nnimap-command | |
943 | "UID SEARCH SENTBEFORE %s" | |
944 | (format-time-string | |
945 | (format "%%d-%s-%%Y" | |
946 | (upcase | |
947 | (car (rassoc (nth 4 (decode-time cutoff)) | |
948 | parse-time-months)))) | |
949 | cutoff)))) | |
950 | (and (car result) | |
951 | (delete 0 (mapcar #'string-to-number | |
952 | (cdr (assoc "SEARCH" (cdr result)))))))))) | |
953 | ||
20a673b2 KY |
954 | |
955 | (defun nnimap-find-article-by-message-id (group message-id) | |
6b958814 G |
956 | (with-current-buffer (nnimap-buffer) |
957 | (erase-buffer) | |
181cb5fb G |
958 | (unless (equal group (nnimap-group nnimap-object)) |
959 | (setf (nnimap-group nnimap-object) nil) | |
e39a5583 | 960 | (setf (nnimap-examined nnimap-object) group) |
181cb5fb | 961 | (nnimap-send-command "EXAMINE %S" (utf7-encode group t))) |
6b958814 G |
962 | (let ((sequence |
963 | (nnimap-send-command "UID SEARCH HEADER Message-Id %S" message-id)) | |
964 | article result) | |
965 | (setq result (nnimap-wait-for-response sequence)) | |
966 | (when (and result | |
967 | (car (setq result (nnimap-parse-response)))) | |
968 | ;; Select the last instance of the message in the group. | |
969 | (and (setq article | |
183a6951 | 970 | (car (last (cdr (assoc "SEARCH" (cdr result)))))) |
6b958814 | 971 | (string-to-number article)))))) |
20a673b2 KY |
972 | |
973 | (defun nnimap-delete-article (articles) | |
974 | (with-current-buffer (nnimap-buffer) | |
975 | (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)" | |
976 | (nnimap-article-ranges articles)) | |
0617bb00 | 977 | (cond |
389b76fa | 978 | ((nnimap-capability "UIDPLUS") |
0617bb00 LMI |
979 | (nnimap-command "UID EXPUNGE %s" |
980 | (nnimap-article-ranges articles)) | |
981 | t) | |
982 | (nnimap-expunge | |
983 | (nnimap-command "EXPUNGE") | |
7390c1cd TZ |
984 | t) |
985 | (t (gnus-message 7 (concat "nnimap: nnimap-expunge is not set and the " | |
986 | "server doesn't support UIDPLUS, so we won't " | |
987 | "delete this article now")))))) | |
20a673b2 KY |
988 | |
989 | (deffoo nnimap-request-scan (&optional group server) | |
e21bac42 G |
990 | (when group |
991 | (setq group (nnimap-decode-gnus-group group))) | |
20a673b2 | 992 | (when (and (nnimap-possibly-change-group nil server) |
20a673b2 KY |
993 | nnimap-inbox |
994 | nnimap-split-methods) | |
283f7b93 | 995 | (nnheader-message 7 "nnimap %s splitting mail..." server) |
4d19331f LMI |
996 | (nnimap-split-incoming-mail) |
997 | (nnheader-message 7 "nnimap %s splitting mail...done" server))) | |
20a673b2 KY |
998 | |
999 | (defun nnimap-marks-to-flags (marks) | |
1000 | (let (flags flag) | |
1001 | (dolist (mark marks) | |
1002 | (when (setq flag (cadr (assq mark nnimap-mark-alist))) | |
1003 | (push flag flags))) | |
1004 | flags)) | |
1005 | ||
549c9aed | 1006 | (deffoo nnimap-request-update-group-status (group status &optional server) |
e21bac42 | 1007 | (setq group (nnimap-decode-gnus-group group)) |
549c9aed G |
1008 | (when (nnimap-possibly-change-group nil server) |
1009 | (let ((command (assoc | |
1010 | status | |
1011 | '((subscribe "SUBSCRIBE") | |
1012 | (unsubscribe "UNSUBSCRIBE"))))) | |
1013 | (when command | |
1014 | (with-current-buffer (nnimap-buffer) | |
1015 | (nnimap-command "%s %S" (cadr command) (utf7-encode group t))))))) | |
1016 | ||
286c4fc2 | 1017 | (deffoo nnimap-request-set-mark (group actions &optional server) |
e21bac42 | 1018 | (setq group (nnimap-decode-gnus-group group)) |
20a673b2 KY |
1019 | (when (nnimap-possibly-change-group group server) |
1020 | (let (sequence) | |
1021 | (with-current-buffer (nnimap-buffer) | |
229b59da | 1022 | (erase-buffer) |
20a673b2 KY |
1023 | ;; Just send all the STORE commands without waiting for |
1024 | ;; response. If they're successful, they're successful. | |
1025 | (dolist (action actions) | |
1026 | (destructuring-bind (range action marks) action | |
1027 | (let ((flags (nnimap-marks-to-flags marks))) | |
1028 | (when flags | |
1029 | (setq sequence (nnimap-send-command | |
1030 | "UID STORE %s %sFLAGS.SILENT (%s)" | |
1031 | (nnimap-article-ranges range) | |
5f285722 LMI |
1032 | (cond |
1033 | ((eq action 'del) "-") | |
57cc52be | 1034 | ((eq action 'add) "+") |
5f285722 | 1035 | ((eq action 'set) "")) |
20a673b2 KY |
1036 | (mapconcat #'identity flags " "))))))) |
1037 | ;; Wait for the last command to complete to avoid later | |
e1dbe924 | 1038 | ;; synchronization problems with the stream. |
a46359d4 LMI |
1039 | (when sequence |
1040 | (nnimap-wait-for-response sequence)))))) | |
a1506d29 | 1041 | |
c113de23 | 1042 | (deffoo nnimap-request-accept-article (group &optional server last) |
e21bac42 | 1043 | (setq group (nnimap-decode-gnus-group group)) |
20a673b2 KY |
1044 | (when (nnimap-possibly-change-group nil server) |
1045 | (nnmail-check-syntax) | |
6b958814 G |
1046 | (let ((message-id (message-field-value "message-id")) |
1047 | sequence message) | |
1048 | (nnimap-add-cr) | |
728fd3b9 | 1049 | (setq message (buffer-substring-no-properties (point-min) (point-max))) |
20a673b2 | 1050 | (with-current-buffer (nnimap-buffer) |
114fe546 G |
1051 | (when (setq message (or (nnimap-process-quirk "OK Gimap " 'append message) |
1052 | message)) | |
41d579ce LI |
1053 | ;; If we have this group open read-only, then unselect it |
1054 | ;; before appending to it. | |
1055 | (when (equal (nnimap-examined nnimap-object) group) | |
1056 | (nnimap-unselect-group)) | |
1057 | (erase-buffer) | |
1058 | (setq sequence (nnimap-send-command | |
1059 | "APPEND %S {%d}" (utf7-encode group t) | |
1060 | (length message))) | |
1061 | (unless nnimap-streaming | |
1062 | (nnimap-wait-for-connection "^[+]")) | |
1063 | (process-send-string (get-buffer-process (current-buffer)) message) | |
1064 | (process-send-string (get-buffer-process (current-buffer)) | |
1065 | (if (nnimap-newlinep nnimap-object) | |
1066 | "\n" | |
1067 | "\r\n")) | |
1068 | (let ((result (nnimap-get-response sequence))) | |
1069 | (if (not (nnimap-ok-p result)) | |
1070 | (progn | |
1071 | (nnheader-report 'nnimap "%s" result) | |
1072 | nil) | |
1073 | (cons group | |
1074 | (or (nnimap-find-uid-response "APPENDUID" (car result)) | |
1075 | (nnimap-find-article-by-message-id | |
1076 | group message-id)))))))))) | |
1077 | ||
1078 | (defun nnimap-process-quirk (greeting-match type data) | |
1079 | (when (and (nnimap-greeting nnimap-object) | |
114fe546 | 1080 | (string-match greeting-match (nnimap-greeting nnimap-object)) |
41d579ce LI |
1081 | (eq type 'append) |
1082 | (string-match "\000" data)) | |
1083 | (let ((choice (gnus-multiple-choice | |
1084 | "Message contains NUL characters. Delete, continue, abort? " | |
1085 | '((?d "Delete NUL characters") | |
1086 | (?c "Try to APPEND the message as is") | |
1087 | (?a "Abort"))))) | |
1088 | (cond | |
1089 | ((eq choice ?a) | |
1090 | (nnheader-report 'nnimap "Aborted APPEND due to NUL characters")) | |
1091 | ((eq choice ?c) | |
1092 | data) | |
1093 | (t | |
1094 | (with-temp-buffer | |
1095 | (insert data) | |
1096 | (goto-char (point-min)) | |
1097 | (while (search-forward "\000" nil t) | |
1098 | (replace-match "" t t)) | |
1099 | (buffer-string))))))) | |
cccb4b4c | 1100 | |
0d1c2cc8 G |
1101 | (defun nnimap-ok-p (value) |
1102 | (and (consp value) | |
1103 | (consp (car value)) | |
1104 | (equal (caar value) "OK"))) | |
1105 | ||
cccb4b4c | 1106 | (defun nnimap-find-uid-response (name list) |
17dd2281 | 1107 | (let ((result (car (last (nnimap-find-response-element name list))))) |
cccb4b4c LMI |
1108 | (and result |
1109 | (string-to-number result)))) | |
1110 | ||
1111 | (defun nnimap-find-response-element (name list) | |
1112 | (let (result) | |
1113 | (dolist (elem list) | |
1114 | (when (and (consp elem) | |
1115 | (equal name (car elem))) | |
1116 | (setq result elem))) | |
1117 | result)) | |
20a673b2 | 1118 | |
728fd3b9 | 1119 | (deffoo nnimap-request-replace-article (article group buffer) |
e21bac42 | 1120 | (setq group (nnimap-decode-gnus-group group)) |
728fd3b9 LMI |
1121 | (let (group-art) |
1122 | (when (and (nnimap-possibly-change-group group nil) | |
1123 | ;; Put the article into the group. | |
1124 | (with-current-buffer buffer | |
1125 | (setq group-art | |
1126 | (nnimap-request-accept-article group nil t)))) | |
1127 | (nnimap-delete-article (list article)) | |
1128 | ;; Return the new article number. | |
1129 | (cdr group-art)))) | |
1130 | ||
20a673b2 KY |
1131 | (defun nnimap-add-cr () |
1132 | (goto-char (point-min)) | |
1133 | (while (re-search-forward "\r?\n" nil t) | |
1134 | (replace-match "\r\n" t t))) | |
1135 | ||
1136 | (defun nnimap-get-groups () | |
cccb4b4c LMI |
1137 | (erase-buffer) |
1138 | (let ((sequence (nnimap-send-command "LIST \"\" \"*\"")) | |
20a673b2 | 1139 | groups) |
cccb4b4c LMI |
1140 | (nnimap-wait-for-response sequence) |
1141 | (subst-char-in-region (point-min) (point-max) | |
1142 | ?\\ ?% t) | |
1143 | (goto-char (point-min)) | |
1144 | (nnimap-unfold-quoted-lines) | |
1145 | (goto-char (point-min)) | |
1146 | (while (search-forward "* LIST " nil t) | |
1147 | (let ((flags (read (current-buffer))) | |
1148 | (separator (read (current-buffer))) | |
1149 | (group (read (current-buffer)))) | |
1150 | (unless (member '%NoSelect flags) | |
bca46f6b G |
1151 | (push (utf7-decode (if (stringp group) |
1152 | group | |
1153 | (format "%s" group)) t) | |
36af6c65 | 1154 | groups)))) |
cccb4b4c | 1155 | (nreverse groups))) |
20a673b2 | 1156 | |
286c4fc2 | 1157 | (deffoo nnimap-request-list (&optional server) |
c13bc26b LI |
1158 | (when (nnimap-possibly-change-group nil server) |
1159 | (with-current-buffer nntp-server-buffer | |
1160 | (erase-buffer) | |
1161 | (let ((groups | |
1162 | (with-current-buffer (nnimap-buffer) | |
1163 | (nnimap-get-groups))) | |
1164 | sequences responses) | |
1165 | (when groups | |
1166 | (with-current-buffer (nnimap-buffer) | |
1167 | (setf (nnimap-group nnimap-object) nil) | |
1168 | (dolist (group groups) | |
1169 | (setf (nnimap-examined nnimap-object) group) | |
1170 | (push (list (nnimap-send-command "EXAMINE %S" | |
1171 | (utf7-encode group t)) | |
1172 | group) | |
1173 | sequences)) | |
1174 | (nnimap-wait-for-response (caar sequences)) | |
1175 | (setq responses | |
1176 | (nnimap-get-responses (mapcar #'car sequences)))) | |
1177 | (dolist (response responses) | |
1178 | (let* ((sequence (car response)) | |
1179 | (response (cadr response)) | |
1180 | (group (cadr (assoc sequence sequences)))) | |
1181 | (when (and group | |
1182 | (equal (caar response) "OK")) | |
1183 | (let ((uidnext (nnimap-find-parameter "UIDNEXT" response)) | |
1184 | highest exists) | |
1185 | (dolist (elem response) | |
1186 | (when (equal (cadr elem) "EXISTS") | |
1187 | (setq exists (string-to-number (car elem))))) | |
1188 | (when uidnext | |
1189 | (setq highest (1- (string-to-number (car uidnext))))) | |
1190 | (cond | |
1191 | ((null highest) | |
1192 | (insert (format "%S 0 1 y\n" (utf7-decode group t)))) | |
1193 | ((zerop exists) | |
1194 | ;; Empty group. | |
1195 | (insert (format "%S %d %d y\n" | |
1196 | (utf7-decode group t) | |
1197 | highest (1+ highest)))) | |
1198 | (t | |
1199 | ;; Return the widest possible range. | |
1200 | (insert (format "%S %d 1 y\n" (utf7-decode group t) | |
1201 | (or highest exists))))))))) | |
1202 | t))))) | |
c113de23 | 1203 | |
a3f57c41 | 1204 | (deffoo nnimap-request-newgroups (date &optional server) |
c13bc26b LI |
1205 | (when (nnimap-possibly-change-group nil server) |
1206 | (with-current-buffer nntp-server-buffer | |
1207 | (erase-buffer) | |
1208 | (dolist (group (with-current-buffer (nnimap-buffer) | |
1209 | (nnimap-get-groups))) | |
1210 | (unless (assoc group nnimap-current-infos) | |
1211 | ;; Insert dummy numbers here -- they don't matter. | |
bca46f6b | 1212 | (insert (format "%S 0 1 y\n" (utf7-encode group))))) |
c13bc26b | 1213 | t))) |
a3f57c41 | 1214 | |
286c4fc2 | 1215 | (deffoo nnimap-retrieve-group-data-early (server infos) |
20a673b2 KY |
1216 | (when (nnimap-possibly-change-group nil server) |
1217 | (with-current-buffer (nnimap-buffer) | |
f7aa248a G |
1218 | (erase-buffer) |
1219 | (setf (nnimap-group nnimap-object) nil) | |
389b76fa | 1220 | (let ((qresyncp (nnimap-capability "QRESYNC")) |
f7aa248a | 1221 | params groups sequences active uidvalidity modseq group) |
20a673b2 KY |
1222 | ;; Go through the infos and gather the data needed to know |
1223 | ;; what and how to request the data. | |
1224 | (dolist (info infos) | |
f7aa248a | 1225 | (setq params (gnus-info-params info) |
e21bac42 G |
1226 | group (nnimap-decode-gnus-group |
1227 | (gnus-group-real-name (gnus-info-group info))) | |
f7aa248a G |
1228 | active (cdr (assq 'active params)) |
1229 | uidvalidity (cdr (assq 'uidvalidity params)) | |
1230 | modseq (cdr (assq 'modseq params))) | |
e39a5583 | 1231 | (setf (nnimap-examined nnimap-object) group) |
20a673b2 | 1232 | (if (and qresyncp |
f7aa248a | 1233 | uidvalidity |
4a3988d5 | 1234 | active |
f7aa248a | 1235 | modseq) |
20a673b2 | 1236 | (push |
549c9aed | 1237 | (list (nnimap-send-command "EXAMINE %S (%s (%s %s))" |
dab0271f | 1238 | (utf7-encode group t) |
549c9aed | 1239 | (nnimap-quirk "QRESYNC") |
dab0271f | 1240 | uidvalidity modseq) |
f7aa248a G |
1241 | 'qresync |
1242 | nil group 'qresync) | |
20a673b2 KY |
1243 | sequences) |
1244 | (let ((start | |
f7aa248a | 1245 | (if (and active uidvalidity) |
20a673b2 | 1246 | ;; Fetch the last 100 flags. |
f7aa248a G |
1247 | (max 1 (- (cdr active) 100)) |
1248 | 1)) | |
1249 | (command | |
1250 | (if uidvalidity | |
1251 | "EXAMINE" | |
1252 | ;; If we don't have a UIDVALIDITY, then this is | |
1253 | ;; the first time we've seen the group, so we | |
1254 | ;; have to do a SELECT (which is slower than an | |
1255 | ;; examine), but will tell us whether the group | |
1256 | ;; is read-only or not. | |
1257 | "SELECT"))) | |
dab0271f G |
1258 | (push (list (nnimap-send-command "%s %S" command |
1259 | (utf7-encode group t)) | |
20a673b2 | 1260 | (nnimap-send-command "UID FETCH %d:* FLAGS" start) |
f7aa248a | 1261 | start group command) |
b5c575e6 | 1262 | sequences)))) |
20a673b2 KY |
1263 | sequences)))) |
1264 | ||
549c9aed G |
1265 | (defun nnimap-quirk (command) |
1266 | (let ((quirk (assoc command nnimap-quirks))) | |
1267 | ;; If this server is of a type that matches a quirk, then return | |
1268 | ;; the "quirked" command instead of the proper one. | |
1269 | (if (or (null quirk) | |
1270 | (not (string-match (nth 1 quirk) (nnimap-greeting nnimap-object)))) | |
1271 | command | |
1272 | (nth 2 quirk)))) | |
1273 | ||
286c4fc2 | 1274 | (deffoo nnimap-finish-retrieve-group-infos (server infos sequences) |
20a673b2 | 1275 | (when (and sequences |
3451795c LMI |
1276 | ;; Check that the process is still alive. |
1277 | (get-buffer-process (nnimap-buffer)) | |
1278 | (memq (process-status (get-buffer-process (nnimap-buffer))) | |
1279 | '(open run)) | |
20a673b2 KY |
1280 | (nnimap-possibly-change-group nil server)) |
1281 | (with-current-buffer (nnimap-buffer) | |
1282 | ;; Wait for the final data to trickle in. | |
f7aa248a G |
1283 | (when (nnimap-wait-for-response (if (eq (cadar sequences) 'qresync) |
1284 | (caar sequences) | |
1285 | (cadar sequences)) | |
1286 | t) | |
1287 | ;; Now we should have most of the data we need, no matter | |
1288 | ;; whether we're QRESYNCING, fetching all the flags from | |
1289 | ;; scratch, or just fetching the last 100 flags per group. | |
8ccbef23 G |
1290 | (nnimap-update-infos (nnimap-flags-to-marks |
1291 | (nnimap-parse-flags | |
1292 | (nreverse sequences))) | |
1293 | infos) | |
1294 | ;; Finally, just return something resembling an active file in | |
1295 | ;; the nntp buffer, so that the agent can save the info, too. | |
1296 | (with-current-buffer nntp-server-buffer | |
1297 | (erase-buffer) | |
1298 | (dolist (info infos) | |
1299 | (let* ((group (gnus-info-group info)) | |
1300 | (active (gnus-active group))) | |
1301 | (when active | |
1302 | (insert (format "%S %d %d y\n" | |
e21bac42 G |
1303 | (decode-coding-string |
1304 | (gnus-group-real-name group) 'utf-8) | |
8ccbef23 G |
1305 | (cdr active) |
1306 | (car active))))))))))) | |
20a673b2 KY |
1307 | |
1308 | (defun nnimap-update-infos (flags infos) | |
1309 | (dolist (info infos) | |
e21bac42 G |
1310 | (let* ((group (nnimap-decode-gnus-group |
1311 | (gnus-group-real-name (gnus-info-group info)))) | |
f7aa248a G |
1312 | (marks (cdr (assoc group flags)))) |
1313 | (when marks | |
1314 | (nnimap-update-info info marks))))) | |
20a673b2 KY |
1315 | |
1316 | (defun nnimap-update-info (info marks) | |
f7aa248a G |
1317 | (destructuring-bind (existing flags high low uidnext start-article |
1318 | permanent-flags uidvalidity | |
1319 | vanished highestmodseq) marks | |
1320 | (cond | |
1321 | ;; Ignore groups with no UIDNEXT/marks. This happens for | |
1322 | ;; completely empty groups. | |
1323 | ((and (not existing) | |
1324 | (not uidnext)) | |
dab0271f G |
1325 | (let ((active (cdr (assq 'active (gnus-info-params info))))) |
1326 | (when active | |
1327 | (gnus-set-active (gnus-info-group info) active)))) | |
f7aa248a G |
1328 | ;; We have a mismatch between the old and new UIDVALIDITY |
1329 | ;; identifiers, so we have to re-request the group info (the next | |
1330 | ;; time). This virtually never happens. | |
1331 | ((let ((old-uidvalidity | |
1332 | (cdr (assq 'uidvalidity (gnus-info-params info))))) | |
1333 | (and old-uidvalidity | |
1334 | (not (equal old-uidvalidity uidvalidity)) | |
1335 | (> start-article 1))) | |
1336 | (gnus-group-remove-parameter info 'uidvalidity) | |
1337 | (gnus-group-remove-parameter info 'modseq)) | |
1338 | ;; We have the data needed to update. | |
1339 | (t | |
dab0271f G |
1340 | (let* ((group (gnus-info-group info)) |
1341 | (completep (and start-article | |
1342 | (= start-article 1))) | |
1343 | (active (or (gnus-active group) | |
1344 | (cdr (assq 'active (gnus-info-params info)))))) | |
b1ae92ba G |
1345 | (when uidnext |
1346 | (setq high (1- uidnext))) | |
20a673b2 KY |
1347 | ;; First set the active ranges based on high/low. |
1348 | (if (or completep | |
1349 | (not (gnus-active group))) | |
1350 | (gnus-set-active group | |
61b1af82 | 1351 | (cond |
a7f6e5b9 LMI |
1352 | (active |
1353 | (cons (min (or low (car active)) | |
1354 | (car active)) | |
1355 | (max (or high (cdr active)) | |
1356 | (cdr active)))) | |
61b1af82 G |
1357 | ((and low high) |
1358 | (cons low high)) | |
1359 | (uidnext | |
20a673b2 | 1360 | ;; No articles in this group. |
61b1af82 G |
1361 | (cons uidnext (1- uidnext))) |
1362 | (start-article | |
1363 | (cons start-article (1- start-article))) | |
1364 | (t | |
1365 | ;; No articles and no uidnext. | |
1366 | nil))) | |
41d579ce LI |
1367 | (gnus-set-active group |
1368 | (cons (car active) | |
1369 | (or high (1- uidnext))))) | |
f7aa248a G |
1370 | ;; See whether this is a read-only group. |
1371 | (unless (eq permanent-flags 'not-scanned) | |
1372 | (gnus-group-set-parameter | |
1373 | info 'permanent-flags | |
7cad71ad G |
1374 | (and (or (memq '%* permanent-flags) |
1375 | (memq '%Seen permanent-flags)) | |
1376 | permanent-flags))) | |
f7aa248a G |
1377 | ;; Update marks and read articles if this isn't a |
1378 | ;; read-only IMAP group. | |
7cad71ad G |
1379 | (when (setq permanent-flags |
1380 | (cdr (assq 'permanent-flags (gnus-info-params info)))) | |
f7aa248a G |
1381 | (if (and highestmodseq |
1382 | (not start-article)) | |
1383 | ;; We've gotten the data by QRESYNCing. | |
1384 | (nnimap-update-qresync-info | |
dab0271f | 1385 | info existing (nnimap-imap-ranges-to-gnus-ranges vanished) flags) |
f7aa248a G |
1386 | ;; Do normal non-QRESYNC flag updates. |
1387 | ;; Update the list of read articles. | |
1388 | (let* ((unread | |
1389 | (gnus-compress-sequence | |
1390 | (gnus-set-difference | |
1391 | (gnus-set-difference | |
1392 | existing | |
1393 | (cdr (assoc '%Seen flags))) | |
1394 | (cdr (assoc '%Flagged flags))))) | |
1395 | (read (gnus-range-difference | |
1396 | (cons start-article high) unread))) | |
1397 | (when (> start-article 1) | |
1398 | (setq read | |
1399 | (gnus-range-nconcat | |
1400 | (if (> start-article 1) | |
1401 | (gnus-sorted-range-intersection | |
1402 | (cons 1 (1- start-article)) | |
1403 | (gnus-info-read info)) | |
1404 | (gnus-info-read info)) | |
1405 | read))) | |
7cad71ad G |
1406 | (when (or (not (listp permanent-flags)) |
1407 | (memq '%Seen permanent-flags)) | |
1408 | (gnus-info-set-read info read)) | |
f7aa248a G |
1409 | ;; Update the marks. |
1410 | (setq marks (gnus-info-marks info)) | |
1411 | (dolist (type (cdr nnimap-mark-alist)) | |
7cad71ad | 1412 | (when (or (not (listp permanent-flags)) |
1e961f10 KAH |
1413 | (memq (car (assoc (caddr type) flags)) |
1414 | permanent-flags) | |
7cad71ad G |
1415 | (memq '%* permanent-flags)) |
1416 | (let ((old-marks (assoc (car type) marks)) | |
1417 | (new-marks | |
1418 | (gnus-compress-sequence | |
1419 | (cdr (or (assoc (caddr type) flags) ; %Flagged | |
1420 | (assoc (intern (cadr type) obarray) flags) | |
1421 | (assoc (cadr type) flags)))))) ; "\Flagged" | |
1422 | (setq marks (delq old-marks marks)) | |
1423 | (pop old-marks) | |
1424 | (when (and old-marks | |
1425 | (> start-article 1)) | |
1426 | (setq old-marks (gnus-range-difference | |
1427 | old-marks | |
1428 | (cons start-article high))) | |
1429 | (setq new-marks (gnus-range-nconcat old-marks new-marks))) | |
1430 | (when new-marks | |
1431 | (push (cons (car type) new-marks) marks))))) | |
1432 | (gnus-info-set-marks info marks t)))) | |
41d579ce LI |
1433 | ;; Tell Gnus whether there are any \Recent messages in any of |
1434 | ;; the groups. | |
1435 | (let ((recent (cdr (assoc '%Recent flags)))) | |
b52daf3d LI |
1436 | (when (and active |
1437 | recent | |
1438 | (> (car (last recent)) (cdr active))) | |
1439 | (push (list (cons (gnus-group-real-name group) 0)) | |
1440 | nnmail-split-history))) | |
f7aa248a G |
1441 | ;; Note the active level for the next run-through. |
1442 | (gnus-group-set-parameter info 'active (gnus-active group)) | |
1443 | (gnus-group-set-parameter info 'uidvalidity uidvalidity) | |
1444 | (gnus-group-set-parameter info 'modseq highestmodseq) | |
1445 | (nnimap-store-info info (gnus-active group))))))) | |
1446 | ||
dab0271f | 1447 | (defun nnimap-update-qresync-info (info existing vanished flags) |
f7aa248a G |
1448 | ;; Add all the vanished articles to the list of read articles. |
1449 | (gnus-info-set-read | |
1450 | info | |
dab0271f G |
1451 | (gnus-add-to-range |
1452 | (gnus-add-to-range | |
1453 | (gnus-range-add (gnus-info-read info) | |
1454 | vanished) | |
1455 | (cdr (assq '%Flagged flags))) | |
1456 | (cdr (assq '%Seen flags)))) | |
1457 | (let ((marks (gnus-info-marks info))) | |
1458 | (dolist (type (cdr nnimap-mark-alist)) | |
1459 | (let ((ticks (assoc (car type) marks)) | |
1460 | (new-marks | |
1461 | (cdr (or (assoc (caddr type) flags) ; %Flagged | |
1462 | (assoc (intern (cadr type) obarray) flags) | |
1463 | (assoc (cadr type) flags))))) ; "\Flagged" | |
1464 | (setq marks (delq ticks marks)) | |
1465 | (pop ticks) | |
1466 | ;; Add the new marks we got. | |
1467 | (setq ticks (gnus-add-to-range ticks new-marks)) | |
1468 | ;; Remove the marks from messages that don't have them. | |
1469 | (setq ticks (gnus-remove-from-range | |
1470 | ticks | |
1471 | (gnus-compress-sequence | |
1472 | (gnus-sorted-complement existing new-marks)))) | |
1473 | (when ticks | |
1474 | (push (cons (car type) ticks) marks))) | |
1475 | (gnus-info-set-marks info marks t)))) | |
f7aa248a G |
1476 | |
1477 | (defun nnimap-imap-ranges-to-gnus-ranges (irange) | |
1478 | (if (zerop (length irange)) | |
1479 | nil | |
1480 | (let ((result nil)) | |
1481 | (dolist (elem (split-string irange ",")) | |
1482 | (push | |
1483 | (if (string-match ":" elem) | |
1484 | (let ((numbers (split-string elem ":"))) | |
1485 | (cons (string-to-number (car numbers)) | |
1486 | (string-to-number (cadr numbers)))) | |
1487 | (string-to-number elem)) | |
1488 | result)) | |
1489 | (nreverse result)))) | |
286c4fc2 LMI |
1490 | |
1491 | (defun nnimap-store-info (info active) | |
1492 | (let* ((group (gnus-group-real-name (gnus-info-group info))) | |
1493 | (entry (assoc group nnimap-current-infos))) | |
1494 | (if entry | |
1495 | (setcdr entry (list info active)) | |
1496 | (push (list group info active) nnimap-current-infos)))) | |
20a673b2 KY |
1497 | |
1498 | (defun nnimap-flags-to-marks (groups) | |
f7aa248a G |
1499 | (let (data group totalp uidnext articles start-article mark permanent-flags |
1500 | uidvalidity vanished highestmodseq) | |
20a673b2 KY |
1501 | (dolist (elem groups) |
1502 | (setq group (car elem) | |
b069e5a6 G |
1503 | uidnext (nth 1 elem) |
1504 | start-article (nth 2 elem) | |
1505 | permanent-flags (nth 3 elem) | |
f7aa248a G |
1506 | uidvalidity (nth 4 elem) |
1507 | vanished (nth 5 elem) | |
1508 | highestmodseq (nth 6 elem) | |
1509 | articles (nthcdr 7 elem)) | |
20a673b2 KY |
1510 | (let ((high (caar articles)) |
1511 | marks low existing) | |
1512 | (dolist (article articles) | |
1513 | (setq low (car article)) | |
1514 | (push (car article) existing) | |
1515 | (dolist (flag (cdr article)) | |
1516 | (setq mark (assoc flag marks)) | |
1517 | (if (not mark) | |
1518 | (push (list flag (car article)) marks) | |
b069e5a6 G |
1519 | (setcdr mark (cons (car article) (cdr mark)))))) |
1520 | (push (list group existing marks high low uidnext start-article | |
f7aa248a | 1521 | permanent-flags uidvalidity vanished highestmodseq) |
b069e5a6 | 1522 | data))) |
20a673b2 KY |
1523 | data)) |
1524 | ||
1525 | (defun nnimap-parse-flags (sequences) | |
1526 | (goto-char (point-min)) | |
b069e5a6 G |
1527 | ;; Change \Delete etc to %Delete, so that the reader can read it. |
1528 | (subst-char-in-region (point-min) (point-max) | |
1529 | ?\\ ?% t) | |
a123622d G |
1530 | ;; Remove any MODSEQ entries in the buffer, because they may contain |
1531 | ;; numbers that are too large for 32-bit Emacsen. | |
1532 | (while (re-search-forward " MODSEQ ([0-9]+)" nil t) | |
1533 | (replace-match "" t t)) | |
1534 | (goto-char (point-min)) | |
f7aa248a G |
1535 | (let (start end articles groups uidnext elems permanent-flags |
1536 | uidvalidity vanished highestmodseq) | |
20a673b2 | 1537 | (dolist (elem sequences) |
f7aa248a G |
1538 | (destructuring-bind (group-sequence flag-sequence totalp group command) |
1539 | elem | |
b069e5a6 | 1540 | (setq start (point)) |
f7aa248a G |
1541 | (when (and |
1542 | ;; The EXAMINE was successful. | |
1543 | (search-forward (format "\n%d OK " group-sequence) nil t) | |
1544 | (progn | |
1545 | (forward-line 1) | |
1546 | (setq end (point)) | |
1547 | (goto-char start) | |
1548 | (setq permanent-flags | |
1549 | (if (equal command "SELECT") | |
b069e5a6 | 1550 | (and (search-forward "PERMANENTFLAGS " |
f7aa248a G |
1551 | (or end (point-min)) t) |
1552 | (read (current-buffer))) | |
1553 | 'not-scanned)) | |
1554 | (goto-char start) | |
1555 | (setq uidnext | |
1556 | (and (search-forward "UIDNEXT " | |
1557 | (or end (point-min)) t) | |
1558 | (read (current-buffer)))) | |
1559 | (goto-char start) | |
1560 | (setq uidvalidity | |
1561 | (and (re-search-forward "UIDVALIDITY \\([0-9]+\\)" | |
1562 | (or end (point-min)) t) | |
1563 | ;; Store UIDVALIDITY as a string, as it's | |
1564 | ;; too big for 32-bit Emacsen, usually. | |
1565 | (match-string 1))) | |
1566 | (goto-char start) | |
1567 | (setq vanished | |
1568 | (and (eq flag-sequence 'qresync) | |
a5166359 | 1569 | (re-search-forward "^\\* VANISHED .*? \\([0-9:,]+\\)" |
f7aa248a G |
1570 | (or end (point-min)) t) |
1571 | (match-string 1))) | |
1572 | (goto-char start) | |
1573 | (setq highestmodseq | |
a123622d | 1574 | (and (re-search-forward "HIGHESTMODSEQ \\([0-9]+\\)" |
f7aa248a | 1575 | (or end (point-min)) t) |
a123622d | 1576 | (match-string 1))) |
f7aa248a G |
1577 | (goto-char end) |
1578 | (forward-line -1)) | |
1579 | ;; The UID FETCH FLAGS was successful. | |
1580 | (or (eq flag-sequence 'qresync) | |
1581 | (search-forward (format "\n%d OK " flag-sequence) nil t))) | |
1582 | (if (eq flag-sequence 'qresync) | |
1583 | (progn | |
1584 | (goto-char start) | |
1585 | (setq start end)) | |
1586 | (setq start (point)) | |
1587 | (goto-char end)) | |
a1d16a7b | 1588 | (while (re-search-forward "^\\* [0-9]+ FETCH " start t) |
71f8b7ed | 1589 | (let ((p (point))) |
a123622d | 1590 | (setq elems (read (current-buffer))) |
71f8b7ed G |
1591 | (push (cons (cadr (memq 'UID elems)) |
1592 | (cadr (memq 'FLAGS elems))) | |
1593 | articles))) | |
f7aa248a G |
1594 | (push (nconc (list group uidnext totalp permanent-flags uidvalidity |
1595 | vanished highestmodseq) | |
1596 | articles) | |
b069e5a6 | 1597 | groups) |
f7aa248a | 1598 | (goto-char end) |
20a673b2 KY |
1599 | (setq articles nil)))) |
1600 | groups)) | |
1601 | ||
1602 | (defun nnimap-find-process-buffer (buffer) | |
1603 | (cadr (assoc buffer nnimap-connection-alist))) | |
1604 | ||
286c4fc2 | 1605 | (deffoo nnimap-request-post (&optional server) |
20a673b2 KY |
1606 | (setq nnimap-status-string "Read-only server") |
1607 | nil) | |
c113de23 | 1608 | |
5a6a61f7 GM |
1609 | (declare-function gnus-fetch-headers "gnus-sum" |
1610 | (articles &optional limit force-new dependencies)) | |
1611 | ||
0c136286 | 1612 | (deffoo nnimap-request-thread (header &optional group server) |
e21bac42 G |
1613 | (when group |
1614 | (setq group (nnimap-decode-gnus-group group))) | |
bca46f6b | 1615 | (if gnus-refer-thread-use-nnir |
47f0b35e AC |
1616 | (nnir-search-thread header) |
1617 | (when (nnimap-possibly-change-group group server) | |
1618 | (let* ((cmd (nnimap-make-thread-query header)) | |
1619 | (result (with-current-buffer (nnimap-buffer) | |
1620 | (nnimap-command "UID SEARCH %s" cmd)))) | |
1621 | (when result | |
1622 | (gnus-fetch-headers | |
1623 | (and (car result) (delete 0 (mapcar #'string-to-number | |
1624 | (cdr (assoc "SEARCH" (cdr result)))))) | |
1625 | nil t)))))) | |
030158f3 | 1626 | |
20a673b2 KY |
1627 | (defun nnimap-possibly-change-group (group server) |
1628 | (let ((open-result t)) | |
1629 | (when (and server | |
1630 | (not (nnimap-server-opened server))) | |
1631 | (setq open-result (nnimap-open-server server))) | |
1632 | (cond | |
1633 | ((not open-result) | |
1634 | nil) | |
1635 | ((not group) | |
1636 | t) | |
1637 | (t | |
1638 | (with-current-buffer (nnimap-buffer) | |
1639 | (if (equal group (nnimap-group nnimap-object)) | |
1640 | t | |
1641 | (let ((result (nnimap-command "SELECT %S" (utf7-encode group t)))) | |
1642 | (when (car result) | |
286c4fc2 LMI |
1643 | (setf (nnimap-group nnimap-object) group |
1644 | (nnimap-select-result nnimap-object) result) | |
20a673b2 KY |
1645 | result)))))))) |
1646 | ||
1647 | (defun nnimap-find-connection (buffer) | |
1648 | "Find the connection delivering to BUFFER." | |
1649 | (let ((entry (assoc buffer nnimap-connection-alist))) | |
1650 | (when entry | |
1651 | (if (and (buffer-name (cadr entry)) | |
1652 | (get-buffer-process (cadr entry)) | |
1653 | (memq (process-status (get-buffer-process (cadr entry))) | |
1654 | '(open run))) | |
1655 | (get-buffer-process (cadr entry)) | |
1656 | (setq nnimap-connection-alist (delq entry nnimap-connection-alist)) | |
1657 | nil)))) | |
1658 | ||
1659 | (defvar nnimap-sequence 0) | |
1660 | ||
1661 | (defun nnimap-send-command (&rest args) | |
d5e9a4e9 | 1662 | (setf (nnimap-last-command-time nnimap-object) (current-time)) |
20a673b2 KY |
1663 | (process-send-string |
1664 | (get-buffer-process (current-buffer)) | |
1665 | (nnimap-log-command | |
286c4fc2 | 1666 | (format "%d %s%s\n" |
20a673b2 | 1667 | (incf nnimap-sequence) |
286c4fc2 LMI |
1668 | (apply #'format args) |
1669 | (if (nnimap-newlinep nnimap-object) | |
1670 | "" | |
1671 | "\r")))) | |
b5c575e6 G |
1672 | ;; Some servers apparently can't have many outstanding |
1673 | ;; commands, so throttle them. | |
1674 | (unless nnimap-streaming | |
1675 | (nnimap-wait-for-response nnimap-sequence)) | |
20a673b2 KY |
1676 | nnimap-sequence) |
1677 | ||
1678 | (defun nnimap-log-command (command) | |
1679 | (with-current-buffer (get-buffer-create "*imap log*") | |
1680 | (goto-char (point-max)) | |
d5e9a4e9 LI |
1681 | (insert (format-time-string "%H:%M:%S") " " |
1682 | (if nnimap-inhibit-logging | |
c13bc26b | 1683 | "(inhibited)\n" |
d5e9a4e9 | 1684 | command))) |
20a673b2 KY |
1685 | command) |
1686 | ||
1687 | (defun nnimap-command (&rest args) | |
1688 | (erase-buffer) | |
1689 | (let* ((sequence (apply #'nnimap-send-command args)) | |
1690 | (response (nnimap-get-response sequence))) | |
1691 | (if (equal (caar response) "OK") | |
1692 | (cons t response) | |
1693 | (nnheader-report 'nnimap "%s" | |
a46359d4 LMI |
1694 | (mapconcat (lambda (a) |
1695 | (format "%s" a)) | |
1696 | (car response) " ")) | |
20a673b2 KY |
1697 | nil))) |
1698 | ||
1699 | (defun nnimap-get-response (sequence) | |
1700 | (nnimap-wait-for-response sequence) | |
1701 | (nnimap-parse-response)) | |
1702 | ||
389b76fa | 1703 | (defun nnimap-wait-for-connection (&optional regexp) |
84d89ede LMI |
1704 | (nnimap-wait-for-line (or regexp "^[*.] .*\n") "[*.] \\([A-Z0-9]+\\)")) |
1705 | ||
1706 | (defun nnimap-wait-for-line (regexp &optional response-regexp) | |
286c4fc2 LMI |
1707 | (let ((process (get-buffer-process (current-buffer)))) |
1708 | (goto-char (point-min)) | |
1709 | (while (and (memq (process-status process) | |
1710 | '(open run)) | |
389b76fa | 1711 | (not (re-search-forward regexp nil t))) |
286c4fc2 LMI |
1712 | (nnheader-accept-process-output process) |
1713 | (goto-char (point-min))) | |
bdaa75c7 | 1714 | (forward-line -1) |
84d89ede | 1715 | (and (looking-at (or response-regexp regexp)) |
bdaa75c7 | 1716 | (match-string 1)))) |
286c4fc2 | 1717 | |
20a673b2 | 1718 | (defun nnimap-wait-for-response (sequence &optional messagep) |
8ccbef23 G |
1719 | (let ((process (get-buffer-process (current-buffer))) |
1720 | openp) | |
dab0271f G |
1721 | (condition-case nil |
1722 | (progn | |
1723 | (goto-char (point-max)) | |
1724 | (while (and (setq openp (memq (process-status process) | |
1725 | '(open run))) | |
0832490d LI |
1726 | (progn |
1727 | ;; Skip past any "*" lines that the server has | |
1728 | ;; output. | |
1729 | (while (and (not (bobp)) | |
1730 | (progn | |
1731 | (forward-line -1) | |
1732 | (looking-at "\\*")))) | |
01baa1e6 | 1733 | (not (looking-at (format "%d .*\n" sequence))))) |
dab0271f | 1734 | (when messagep |
2146e256 LMI |
1735 | (nnheader-message-maybe |
1736 | 7 "nnimap read %dk" (/ (buffer-size) 1000))) | |
dab0271f G |
1737 | (nnheader-accept-process-output process) |
1738 | (goto-char (point-max))) | |
1739 | openp) | |
1740 | (quit | |
a123622d G |
1741 | (when debug-on-quit |
1742 | (debug "Quit")) | |
dab0271f G |
1743 | ;; The user hit C-g while we were waiting: kill the process, in case |
1744 | ;; it's a gnutls-cli process that's stuck (tends to happen a lot behind | |
1745 | ;; NAT routers). | |
1746 | (delete-process process) | |
1747 | nil)))) | |
20a673b2 KY |
1748 | |
1749 | (defun nnimap-parse-response () | |
1750 | (let ((lines (split-string (nnimap-last-response-string) "\r\n" t)) | |
1751 | result) | |
1752 | (dolist (line lines) | |
1753 | (push (cdr (nnimap-parse-line line)) result)) | |
1754 | ;; Return the OK/error code first, and then all the "continuation | |
1755 | ;; lines" afterwards. | |
1756 | (cons (pop result) | |
1757 | (nreverse result)))) | |
1758 | ||
1759 | ;; Parse an IMAP response line lightly. They look like | |
1760 | ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse | |
1761 | ;; the lines into a list of strings and lists of string. | |
1762 | (defun nnimap-parse-line (line) | |
1763 | (let (char result) | |
1764 | (with-temp-buffer | |
a04f9e26 | 1765 | (mm-disable-multibyte) |
20a673b2 KY |
1766 | (insert line) |
1767 | (goto-char (point-min)) | |
1768 | (while (not (eobp)) | |
1769 | (if (eql (setq char (following-char)) ? ) | |
1770 | (forward-char 1) | |
1771 | (push | |
1772 | (cond | |
1773 | ((eql char ?\[) | |
f7aa248a G |
1774 | (split-string |
1775 | (buffer-substring | |
1776 | (1+ (point)) | |
9310f19d LMI |
1777 | (if (search-forward "]" (line-end-position) 'move) |
1778 | (1- (point)) | |
1779 | (point))))) | |
20a673b2 | 1780 | ((eql char ?\() |
f7aa248a G |
1781 | (split-string |
1782 | (buffer-substring | |
1783 | (1+ (point)) | |
9310f19d LMI |
1784 | (if (search-forward ")" (line-end-position) 'move) |
1785 | (1- (point)) | |
1786 | (point))))) | |
20a673b2 KY |
1787 | ((eql char ?\") |
1788 | (forward-char 1) | |
9f2d52e7 G |
1789 | (buffer-substring |
1790 | (point) | |
1791 | (1- (or (search-forward "\"" (line-end-position) 'move) | |
1792 | (point))))) | |
20a673b2 KY |
1793 | (t |
1794 | (buffer-substring (point) (if (search-forward " " nil t) | |
1795 | (1- (point)) | |
1796 | (goto-char (point-max)))))) | |
1797 | result))) | |
1798 | (nreverse result)))) | |
1799 | ||
1800 | (defun nnimap-last-response-string () | |
1801 | (save-excursion | |
1802 | (forward-line 1) | |
1803 | (let ((end (point))) | |
1804 | (forward-line -1) | |
1805 | (when (not (bobp)) | |
1806 | (forward-line -1) | |
1807 | (while (and (not (bobp)) | |
1808 | (eql (following-char) ?*)) | |
1809 | (forward-line -1)) | |
1810 | (unless (eql (following-char) ?*) | |
1811 | (forward-line 1))) | |
1812 | (buffer-substring (point) end)))) | |
1813 | ||
1814 | (defun nnimap-get-responses (sequences) | |
1815 | (let (responses) | |
1816 | (dolist (sequence sequences) | |
1817 | (goto-char (point-min)) | |
1818 | (when (re-search-forward (format "^%d " sequence) nil t) | |
1819 | (push (list sequence (nnimap-parse-response)) | |
1820 | responses))) | |
1821 | responses)) | |
1822 | ||
1823 | (defvar nnimap-incoming-split-list nil) | |
1824 | ||
1825 | (defun nnimap-fetch-inbox (articles) | |
1826 | (erase-buffer) | |
1827 | (nnimap-wait-for-response | |
1828 | (nnimap-send-command | |
1829 | "UID FETCH %s %s" | |
1830 | (nnimap-article-ranges articles) | |
1831 | (format "(UID %s%s)" | |
1832 | (format | |
8ccbef23 | 1833 | (if (nnimap-ver4-p) |
1518e4f0 | 1834 | "BODY.PEEK" |
20a673b2 | 1835 | "RFC822.PEEK")) |
1518e4f0 G |
1836 | (cond |
1837 | (nnimap-split-download-body-default | |
1838 | "[]") | |
1839 | ((nnimap-ver4-p) | |
1840 | "[HEADER]") | |
1841 | (t | |
1842 | "[1]")))) | |
20a673b2 KY |
1843 | t)) |
1844 | ||
1845 | (defun nnimap-split-incoming-mail () | |
1846 | (with-current-buffer (nnimap-buffer) | |
1847 | (let ((nnimap-incoming-split-list nil) | |
656e1aab LMI |
1848 | (nnmail-split-methods |
1849 | (cond | |
1850 | ((eq nnimap-split-methods 'default) | |
1851 | nnmail-split-methods) | |
1852 | (nnimap-split-methods | |
1853 | nnimap-split-methods) | |
1854 | (nnimap-split-fancy | |
1855 | 'nnmail-split-fancy))) | |
6b958814 G |
1856 | (nnmail-split-fancy (or nnimap-split-fancy |
1857 | nnmail-split-fancy)) | |
20a673b2 KY |
1858 | (nnmail-inhibit-default-split-group t) |
1859 | (groups (nnimap-get-groups)) | |
1860 | new-articles) | |
1861 | (erase-buffer) | |
1862 | (nnimap-command "SELECT %S" nnimap-inbox) | |
99e65b2d | 1863 | (setf (nnimap-group nnimap-object) nnimap-inbox) |
20a673b2 KY |
1864 | (setq new-articles (nnimap-new-articles (nnimap-get-flags "1:*"))) |
1865 | (when new-articles | |
1866 | (nnimap-fetch-inbox new-articles) | |
1867 | (nnimap-transform-split-mail) | |
1868 | (nnheader-ms-strip-cr) | |
1869 | (nnmail-cache-open) | |
1870 | (nnmail-split-incoming (current-buffer) | |
1871 | #'nnimap-save-mail-spec | |
1872 | nil nil | |
b069e5a6 G |
1873 | #'nnimap-dummy-active-number |
1874 | #'nnimap-save-mail-spec) | |
20a673b2 KY |
1875 | (when nnimap-incoming-split-list |
1876 | (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list)) | |
b069e5a6 | 1877 | sequences junk-articles) |
20a673b2 KY |
1878 | ;; Create any groups that doesn't already exist on the |
1879 | ;; server first. | |
1880 | (dolist (spec specs) | |
b069e5a6 G |
1881 | (when (and (not (member (car spec) groups)) |
1882 | (not (eq (car spec) 'junk))) | |
20a673b2 KY |
1883 | (nnimap-command "CREATE %S" (utf7-encode (car spec) t)))) |
1884 | ;; Then copy over all the messages. | |
1885 | (erase-buffer) | |
1886 | (dolist (spec specs) | |
1887 | (let ((group (car spec)) | |
1888 | (ranges (cdr spec))) | |
b069e5a6 G |
1889 | (if (eq group 'junk) |
1890 | (setq junk-articles ranges) | |
1891 | (push (list (nnimap-send-command | |
1892 | "UID COPY %s %S" | |
1893 | (nnimap-article-ranges ranges) | |
1894 | (utf7-encode group t)) | |
1895 | ranges) | |
1896 | sequences)))) | |
20a673b2 KY |
1897 | ;; Wait for the last COPY response... |
1898 | (when sequences | |
1899 | (nnimap-wait-for-response (caar sequences)) | |
1900 | ;; And then mark the successful copy actions as deleted, | |
1901 | ;; and possibly expunge them. | |
1902 | (nnimap-mark-and-expunge-incoming | |
61b1af82 G |
1903 | (nnimap-parse-copied-articles sequences))) |
1904 | (nnimap-mark-and-expunge-incoming junk-articles))))))) | |
20a673b2 KY |
1905 | |
1906 | (defun nnimap-mark-and-expunge-incoming (range) | |
1907 | (when range | |
1908 | (setq range (nnimap-article-ranges range)) | |
229b59da | 1909 | (erase-buffer) |
0617bb00 LMI |
1910 | (let ((sequence |
1911 | (nnimap-send-command | |
1912 | "UID STORE %s +FLAGS.SILENT (\\Deleted)" range))) | |
1913 | (cond | |
1914 | ;; If the server supports it, we now delete the message we have | |
1915 | ;; just copied over. | |
389b76fa | 1916 | ((nnimap-capability "UIDPLUS") |
0617bb00 LMI |
1917 | (setq sequence (nnimap-send-command "UID EXPUNGE %s" range))) |
1918 | ;; If it doesn't support UID EXPUNGE, then we only expunge if the | |
1919 | ;; user has configured it. | |
b069e5a6 | 1920 | (nnimap-expunge |
0617bb00 LMI |
1921 | (setq sequence (nnimap-send-command "EXPUNGE")))) |
1922 | (nnimap-wait-for-response sequence)))) | |
20a673b2 KY |
1923 | |
1924 | (defun nnimap-parse-copied-articles (sequences) | |
1925 | (let (sequence copied range) | |
1926 | (goto-char (point-min)) | |
56e96bed | 1927 | (while (re-search-forward "^\\([0-9]+\\) OK\\b" nil t) |
20a673b2 KY |
1928 | (setq sequence (string-to-number (match-string 1))) |
1929 | (when (setq range (cadr (assq sequence sequences))) | |
1930 | (push (gnus-uncompress-range range) copied))) | |
1931 | (gnus-compress-sequence (sort (apply #'nconc copied) #'<)))) | |
1932 | ||
1933 | (defun nnimap-new-articles (flags) | |
1934 | (let (new) | |
1935 | (dolist (elem flags) | |
99e65b2d G |
1936 | (unless (gnus-list-memq-of-list nnimap-unsplittable-articles |
1937 | (cdr elem)) | |
20a673b2 KY |
1938 | (push (car elem) new))) |
1939 | (gnus-compress-sequence (nreverse new)))) | |
1940 | ||
1941 | (defun nnimap-make-split-specs (list) | |
1942 | (let ((specs nil) | |
1943 | entry) | |
1944 | (dolist (elem list) | |
1945 | (destructuring-bind (article spec) elem | |
1946 | (dolist (group (delete nil (mapcar #'car spec))) | |
1947 | (unless (setq entry (assoc group specs)) | |
1948 | (push (setq entry (list group)) specs)) | |
1949 | (setcdr entry (cons article (cdr entry)))))) | |
1950 | (dolist (entry specs) | |
1951 | (setcdr entry (gnus-compress-sequence (sort (cdr entry) #'<)))) | |
1952 | specs)) | |
1953 | ||
1954 | (defun nnimap-transform-split-mail () | |
1955 | (goto-char (point-min)) | |
1956 | (let (article bytes) | |
1957 | (block nil | |
1958 | (while (not (eobp)) | |
b5244046 | 1959 | (while (not (looking-at "\\* [0-9]+ FETCH.+UID \\([0-9]+\\)")) |
20a673b2 KY |
1960 | (delete-region (point) (progn (forward-line 1) (point))) |
1961 | (when (eobp) | |
1962 | (return))) | |
1963 | (setq article (match-string 1) | |
1964 | bytes (nnimap-get-length)) | |
1965 | (delete-region (line-beginning-position) (line-end-position)) | |
1966 | ;; Insert MMDF separator, and a way to remember what this | |
1967 | ;; article UID is. | |
1968 | (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article)) | |
1969 | (forward-char (1+ bytes)) | |
1970 | (setq bytes (nnimap-get-length)) | |
1971 | (delete-region (line-beginning-position) (line-end-position)) | |
6b7df8d3 G |
1972 | ;; There's a body; skip past that. |
1973 | (when bytes | |
1974 | (forward-char (1+ bytes)) | |
1975 | (delete-region (line-beginning-position) (line-end-position))))))) | |
20a673b2 KY |
1976 | |
1977 | (defun nnimap-dummy-active-number (group &optional server) | |
1978 | 1) | |
1979 | ||
1980 | (defun nnimap-save-mail-spec (group-art &optional server full-nov) | |
1981 | (let (article) | |
1982 | (goto-char (point-min)) | |
1983 | (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t)) | |
1984 | (error "Invalid nnimap mail") | |
1985 | (setq article (string-to-number (match-string 1)))) | |
b069e5a6 G |
1986 | (push (list article |
1987 | (if (eq group-art 'junk) | |
1988 | (list (cons 'junk 1)) | |
1989 | group-art)) | |
20a673b2 | 1990 | nnimap-incoming-split-list))) |
c113de23 | 1991 | |
af92e247 AC |
1992 | (defun nnimap-make-thread-query (header) |
1993 | (let* ((id (mail-header-id header)) | |
1994 | (refs (split-string | |
1995 | (or (mail-header-references header) | |
1996 | ""))) | |
43a0a4fa AC |
1997 | (value |
1998 | (format | |
1999 | "(OR HEADER REFERENCES %S HEADER Message-Id %S)" | |
2000 | id id))) | |
af92e247 AC |
2001 | (dolist (refid refs value) |
2002 | (setq value (format | |
43a0a4fa | 2003 | "(OR (OR HEADER Message-Id %S HEADER REFERENCES %S) %s)" |
af92e247 AC |
2004 | refid refid value))))) |
2005 | ||
2006 | ||
c113de23 GM |
2007 | (provide 'nnimap) |
2008 | ||
2009 | ;;; nnimap.el ends here |