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 | ||
aa8f8277 | 29 | (eval-and-compile |
20a673b2 | 30 | (require 'nnheader)) |
aa8f8277 | 31 | |
20a673b2 KY |
32 | (eval-when-compile |
33 | (require 'cl)) | |
c113de23 | 34 | |
f58208b1 LMI |
35 | (require 'nnheader) |
36 | (require 'gnus-util) | |
37 | (require 'gnus) | |
38 | (require 'nnoo) | |
20a673b2 | 39 | (require 'netrc) |
e952b711 | 40 | |
c113de23 GM |
41 | (nnoo-declare nnimap) |
42 | ||
c113de23 | 43 | (defvoo nnimap-address nil |
20a673b2 | 44 | "The address of the IMAP server.") |
c113de23 GM |
45 | |
46 | (defvoo nnimap-server-port nil | |
20a673b2 KY |
47 | "The IMAP port used. |
48 | If nnimap-stream is `ssl', this will default to `imaps'. If not, | |
49 | it will default to `imap'.") | |
50 | ||
51 | (defvoo nnimap-stream 'ssl | |
52 | "How nnimap will talk to the IMAP server. | |
53 | Values are `ssl' and `network'.") | |
54 | ||
55 | (defvoo nnimap-shell-program (if (boundp 'imap-shell-program) | |
56 | (if (listp imap-shell-program) | |
57 | (car imap-shell-program) | |
58 | imap-shell-program) | |
59 | "ssh %s imapd")) | |
60 | ||
61 | (defvoo nnimap-inbox nil | |
62 | "The mail box where incoming mail arrives and should be split out of.") | |
63 | ||
64 | (defvoo nnimap-expunge-inbox nil | |
65 | "If non-nil, expunge the inbox after fetching mail. | |
66 | This is always done if the server supports UID EXPUNGE, but it's | |
67 | not done by default on servers that doesn't support that command.") | |
68 | ||
bdaa75c7 LMI |
69 | (defvoo nnimap-authenticator nil |
70 | "How nnimap authenticate itself to the server. | |
71 | Possible choices are nil (use default methods) or `anonymous'.") | |
72 | ||
73 | (defvoo nnimap-fetch-partial-articles nil | |
74 | "If non-nil, nnimap will fetch partial articles. | |
75 | If t, nnimap will fetch only the first part. If a string, it | |
76 | will fetch all parts that have types that match that string. A | |
77 | likely value would be \"text/\" to automatically fetch all | |
78 | textual parts.") | |
79 | ||
20a673b2 | 80 | (defvoo nnimap-connection-alist nil) |
286c4fc2 LMI |
81 | |
82 | (defvoo nnimap-current-infos nil) | |
83 | ||
20a673b2 KY |
84 | (defvar nnimap-process nil) |
85 | ||
86 | (defvar nnimap-status-string "") | |
23f87bed MB |
87 | |
88 | (defvar nnimap-split-download-body-default nil | |
89 | "Internal variable with default value for `nnimap-split-download-body'.") | |
90 | ||
20a673b2 | 91 | (defstruct nnimap |
286c4fc2 | 92 | group process commands capabilities select-result newlinep) |
c113de23 | 93 | |
20a673b2 KY |
94 | (defvar nnimap-object nil) |
95 | ||
96 | (defvar nnimap-mark-alist | |
97 | '((read "\\Seen") | |
98 | (tick "\\Flagged") | |
99 | (reply "\\Answered") | |
100 | (expire "gnus-expire") | |
101 | (dormant "gnus-dormant") | |
102 | (score "gnus-score") | |
103 | (save "gnus-save") | |
104 | (download "gnus-download") | |
105 | (forward "gnus-forward"))) | |
106 | ||
107 | (defvar nnimap-split-methods nil) | |
108 | ||
109 | (defun nnimap-buffer () | |
110 | (nnimap-find-process-buffer nntp-server-buffer)) | |
111 | ||
286c4fc2 | 112 | (deffoo nnimap-retrieve-headers (articles &optional group server fetch-old) |
c113de23 | 113 | (with-current-buffer nntp-server-buffer |
20a673b2 KY |
114 | (erase-buffer) |
115 | (when (nnimap-possibly-change-group group server) | |
116 | (with-current-buffer (nnimap-buffer) | |
117 | (nnimap-send-command "SELECT %S" (utf7-encode group t)) | |
118 | (erase-buffer) | |
119 | (nnimap-wait-for-response | |
120 | (nnimap-send-command | |
121 | "UID FETCH %s %s" | |
122 | (nnimap-article-ranges (gnus-compress-sequence articles)) | |
123 | (format "(UID RFC822.SIZE BODYSTRUCTURE %s)" | |
124 | (format | |
125 | (if (member "IMAP4REV1" | |
126 | (nnimap-capabilities nnimap-object)) | |
127 | "BODY.PEEK[HEADER.FIELDS %s]" | |
128 | "RFC822.HEADER.LINES %s") | |
129 | (append '(Subject From Date Message-Id | |
130 | References In-Reply-To Xref) | |
131 | nnmail-extra-headers)))) | |
132 | t) | |
133 | (nnimap-transform-headers)) | |
134 | (insert-buffer-substring | |
135 | (nnimap-find-process-buffer (current-buffer)))) | |
136 | t)) | |
137 | ||
138 | (defun nnimap-transform-headers () | |
139 | (goto-char (point-min)) | |
a46359d4 | 140 | (let (article bytes lines size) |
20a673b2 KY |
141 | (block nil |
142 | (while (not (eobp)) | |
143 | (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)")) | |
144 | (delete-region (point) (progn (forward-line 1) (point))) | |
145 | (when (eobp) | |
146 | (return))) | |
147 | (setq article (match-string 1) | |
148 | bytes (nnimap-get-length) | |
149 | lines nil) | |
150 | (beginning-of-line) | |
a46359d4 LMI |
151 | (setq size |
152 | (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)" | |
153 | (line-end-position) | |
154 | t) | |
155 | (match-string 1))) | |
156 | (beginning-of-line) | |
20a673b2 KY |
157 | (when (search-forward "BODYSTRUCTURE" (line-end-position) t) |
158 | (let ((structure (ignore-errors (read (current-buffer))))) | |
159 | (while (and (consp structure) | |
160 | (not (stringp (car structure)))) | |
161 | (setq structure (car structure))) | |
162 | (setq lines (nth 7 structure)))) | |
163 | (delete-region (line-beginning-position) (line-end-position)) | |
164 | (insert (format "211 %s Article retrieved." article)) | |
165 | (forward-line 1) | |
a46359d4 LMI |
166 | (when size |
167 | (insert (format "Chars: %s\n" size))) | |
20a673b2 KY |
168 | (when lines |
169 | (insert (format "Lines: %s\n" lines))) | |
170 | (re-search-forward "^\r$") | |
171 | (delete-region (line-beginning-position) (line-end-position)) | |
172 | (insert ".") | |
173 | (forward-line 1))))) | |
174 | ||
175 | (defun nnimap-get-length () | |
176 | (and (re-search-forward "{\\([0-9]+\\)}" (line-end-position) t) | |
177 | (string-to-number (match-string 1)))) | |
178 | ||
179 | (defun nnimap-article-ranges (ranges) | |
180 | (let (result) | |
181 | (cond | |
182 | ((numberp ranges) | |
183 | (number-to-string ranges)) | |
184 | ((numberp (cdr ranges)) | |
185 | (format "%d:%d" (car ranges) (cdr ranges))) | |
186 | (t | |
187 | (dolist (elem ranges) | |
188 | (push | |
189 | (if (consp elem) | |
190 | (format "%d:%d" (car elem) (cdr elem)) | |
191 | (number-to-string elem)) | |
192 | result)) | |
193 | (mapconcat #'identity (nreverse result) ","))))) | |
194 | ||
286c4fc2 | 195 | (deffoo nnimap-open-server (server &optional defs) |
c113de23 GM |
196 | (if (nnimap-server-opened server) |
197 | t | |
c113de23 | 198 | (unless (assq 'nnimap-address defs) |
20a673b2 | 199 | (setq defs (append defs (list (list 'nnimap-address server))))) |
c113de23 | 200 | (nnoo-change-server 'nnimap server defs) |
20a673b2 KY |
201 | (or (nnimap-find-connection nntp-server-buffer) |
202 | (nnimap-open-connection nntp-server-buffer)))) | |
203 | ||
204 | (defun nnimap-make-process-buffer (buffer) | |
205 | (with-current-buffer | |
206 | (generate-new-buffer (format "*nnimap %s %s %s*" | |
207 | nnimap-address nnimap-server-port | |
208 | (gnus-buffer-exists-p buffer))) | |
209 | (mm-disable-multibyte) | |
210 | (buffer-disable-undo) | |
211 | (gnus-add-buffer) | |
212 | (set (make-local-variable 'after-change-functions) nil) | |
213 | (set (make-local-variable 'nnimap-object) (make-nnimap)) | |
214 | (push (list buffer (current-buffer)) nnimap-connection-alist) | |
215 | (current-buffer))) | |
216 | ||
217 | (defun nnimap-open-shell-stream (name buffer host port) | |
e8861cd2 LMI |
218 | (let ((process-connection-type nil)) |
219 | (start-process name buffer shell-file-name | |
220 | shell-command-switch | |
221 | (format-spec | |
222 | nnimap-shell-program | |
223 | (format-spec-make | |
224 | ?s host | |
225 | ?p port))))) | |
20a673b2 | 226 | |
286c4fc2 LMI |
227 | (defun nnimap-credentials (address ports) |
228 | (let (port credentials) | |
229 | ;; Request the credentials from all ports, but only query on the | |
230 | ;; last port if all the previous ones have failed. | |
231 | (while (and (null credentials) | |
232 | (setq port (pop ports))) | |
233 | (setq credentials | |
234 | (auth-source-user-or-password | |
235 | '("login" "password") address port nil (null ports)))) | |
236 | credentials)) | |
237 | ||
20a673b2 KY |
238 | (defun nnimap-open-connection (buffer) |
239 | (with-current-buffer (nnimap-make-process-buffer buffer) | |
240 | (let* ((coding-system-for-read 'binary) | |
241 | (coding-system-for-write 'binary) | |
286c4fc2 | 242 | (ports |
20a673b2 KY |
243 | (cond |
244 | ((eq nnimap-stream 'network) | |
286c4fc2 LMI |
245 | (open-network-stream |
246 | "*nnimap*" (current-buffer) nnimap-address | |
247 | (or nnimap-server-port | |
248 | (if (netrc-find-service-number "imap") | |
249 | "imap" | |
250 | "143"))) | |
251 | '("143" "imap")) | |
252 | ((eq nnimap-stream 'shell) | |
20a673b2 KY |
253 | (nnimap-open-shell-stream |
254 | "*nnimap*" (current-buffer) nnimap-address | |
255 | (or nnimap-server-port "imap")) | |
286c4fc2 | 256 | '("imap")) |
20a673b2 | 257 | ((eq nnimap-stream 'ssl) |
286c4fc2 LMI |
258 | (open-tls-stream |
259 | "*nnimap*" (current-buffer) nnimap-address | |
260 | (or nnimap-server-port | |
261 | (if (netrc-find-service-number "imaps") | |
262 | "imaps" | |
263 | "993"))) | |
264 | '("143" "993" "imap" "imaps")))) | |
265 | connection-result login-result credentials) | |
20a673b2 KY |
266 | (setf (nnimap-process nnimap-object) |
267 | (get-buffer-process (current-buffer))) | |
20a673b2 KY |
268 | (when (and (nnimap-process nnimap-object) |
269 | (memq (process-status (nnimap-process nnimap-object)) | |
270 | '(open run))) | |
271 | (gnus-set-process-query-on-exit-flag (nnimap-process nnimap-object) nil) | |
286c4fc2 LMI |
272 | (when (setq connection-result (nnimap-wait-for-connection)) |
273 | (unless (equal connection-result "PREAUTH") | |
274 | (if (not (setq credentials | |
bdaa75c7 LMI |
275 | (if (eq nnimap-authenticator 'anonymous) |
276 | (list "anonymous" | |
277 | (message-make-address)) | |
278 | (nnimap-credentials | |
279 | nnimap-address | |
280 | (if nnimap-server-port | |
281 | (cons (format "%s" nnimap-server-port) ports) | |
282 | ports))))) | |
286c4fc2 LMI |
283 | (setq nnimap-object nil) |
284 | (setq login-result (nnimap-command "LOGIN %S %S" | |
285 | (car credentials) | |
286 | (cadr credentials))) | |
287 | (unless (car login-result) | |
20a673b2 | 288 | (delete-process (nnimap-process nnimap-object)) |
286c4fc2 LMI |
289 | (setq nnimap-object nil)))) |
290 | (when nnimap-object | |
20a673b2 KY |
291 | (setf (nnimap-capabilities nnimap-object) |
292 | (mapcar | |
293 | #'upcase | |
286c4fc2 | 294 | (or (nnimap-find-parameter "CAPABILITY" (cdr login-result)) |
20a673b2 KY |
295 | (nnimap-find-parameter |
296 | "CAPABILITY" (cdr (nnimap-command "CAPABILITY")))))) | |
297 | (when (member "QRESYNC" (nnimap-capabilities nnimap-object)) | |
298 | (nnimap-command "ENABLE QRESYNC")) | |
299 | t)))))) | |
300 | ||
301 | (defun nnimap-find-parameter (parameter elems) | |
302 | (let (result) | |
303 | (dolist (elem elems) | |
304 | (cond | |
305 | ((equal (car elem) parameter) | |
306 | (setq result (cdr elem))) | |
307 | ((and (equal (car elem) "OK") | |
308 | (consp (cadr elem)) | |
309 | (equal (caadr elem) parameter)) | |
310 | (setq result (cdr (cadr elem)))))) | |
311 | result)) | |
312 | ||
286c4fc2 | 313 | (deffoo nnimap-close-server (&optional server) |
c113de23 GM |
314 | t) |
315 | ||
286c4fc2 | 316 | (deffoo nnimap-request-close () |
20a673b2 | 317 | t) |
23f87bed | 318 | |
286c4fc2 | 319 | (deffoo nnimap-server-opened (&optional server) |
20a673b2 KY |
320 | (and (nnoo-current-server-p 'nnimap server) |
321 | nntp-server-buffer | |
322 | (gnus-buffer-live-p nntp-server-buffer) | |
323 | (nnimap-find-connection nntp-server-buffer))) | |
c113de23 | 324 | |
286c4fc2 | 325 | (deffoo nnimap-status-message (&optional server) |
20a673b2 | 326 | nnimap-status-string) |
c113de23 | 327 | |
286c4fc2 | 328 | (deffoo nnimap-request-article (article &optional group server to-buffer) |
c113de23 | 329 | (with-current-buffer nntp-server-buffer |
bdaa75c7 LMI |
330 | (let ((result (nnimap-possibly-change-group group server)) |
331 | parts) | |
20a673b2 KY |
332 | (when (stringp article) |
333 | (setq article (nnimap-find-article-by-message-id group article))) | |
334 | (when (and result | |
335 | article) | |
336 | (erase-buffer) | |
337 | (with-current-buffer (nnimap-buffer) | |
338 | (erase-buffer) | |
bdaa75c7 LMI |
339 | (when nnimap-fetch-partial-articles |
340 | (if (eq nnimap-fetch-partial-articles t) | |
341 | (setq parts '(1)) | |
342 | (nnimap-command "UID FETCH %d (BODYSTRUCTURE)" article) | |
343 | (goto-char (point-min)) | |
344 | (when (re-search-forward "FETCH.*BODYSTRUCTURE" nil t) | |
345 | (let ((structure (ignore-errors (read (current-buffer))))) | |
346 | (setq parts (nnimap-find-wanted-parts structure)))))) | |
20a673b2 KY |
347 | (setq result |
348 | (nnimap-command | |
349 | (if (member "IMAP4REV1" (nnimap-capabilities nnimap-object)) | |
350 | "UID FETCH %d BODY.PEEK[]" | |
351 | "UID FETCH %d RFC822.PEEK") | |
e8861cd2 LMI |
352 | article)) |
353 | ;; Check that we really got an article. | |
354 | (goto-char (point-min)) | |
355 | (unless (looking-at "\\* [0-9]+ FETCH") | |
356 | (setq result nil))) | |
20a673b2 KY |
357 | (let ((buffer (nnimap-find-process-buffer (current-buffer)))) |
358 | (when (car result) | |
e8861cd2 | 359 | (with-current-buffer (or to-buffer nntp-server-buffer) |
20a673b2 | 360 | (insert-buffer-substring buffer) |
c113de23 | 361 | (goto-char (point-min)) |
20a673b2 KY |
362 | (let ((bytes (nnimap-get-length))) |
363 | (delete-region (line-beginning-position) | |
364 | (progn (forward-line 1) (point))) | |
365 | (goto-char (+ (point) bytes)) | |
366 | (delete-region (point) (point-max)) | |
367 | (nnheader-ms-strip-cr)) | |
bdaa75c7 LMI |
368 | (cons group article)))))))) |
369 | ||
370 | (defun nnimap-find-wanted-parts (structure) | |
371 | (message-flatten-list (nnimap-find-wanted-parts-1 structure ""))) | |
372 | ||
373 | (defun nnimap-find-wanted-parts-1 (structure prefix) | |
374 | (let ((num 1) | |
375 | parts) | |
376 | (while (consp (car structure)) | |
377 | (let ((sub (pop structure))) | |
378 | (if (consp (car sub)) | |
379 | (push (nnimap-find-wanted-parts-1 | |
380 | sub (if (string= prefix "") | |
381 | (number-to-string num) | |
382 | (format "%s.%s" prefix num))) | |
383 | parts) | |
384 | (let ((type (format "%s/%s" (nth 0 sub) (nth 1 sub)))) | |
385 | (when (string-match nnimap-fetch-partial-articles type) | |
386 | (push (if (string= prefix "") | |
387 | (number-to-string num) | |
388 | (format "%s.%s" prefix num)) | |
389 | parts))) | |
390 | (incf num)))) | |
391 | (nreverse parts))) | |
20a673b2 | 392 | |
286c4fc2 | 393 | (deffoo nnimap-request-group (group &optional server dont-check info) |
a46359d4 LMI |
394 | (let ((result (nnimap-possibly-change-group group server)) |
395 | articles active marks high low) | |
396 | (with-current-buffer nntp-server-buffer | |
20a673b2 | 397 | (when result |
286c4fc2 LMI |
398 | (if (and dont-check |
399 | (setq active (nth 2 (assoc group nnimap-current-infos)))) | |
400 | (insert (format "211 %d %d %d %S\n" | |
401 | (- (cdr active) (car active)) | |
402 | (car active) | |
403 | (cdr active) | |
404 | group)) | |
405 | (with-current-buffer (nnimap-buffer) | |
406 | (erase-buffer) | |
407 | (let ((group-sequence | |
408 | (nnimap-send-command "SELECT %S" (utf7-encode group))) | |
409 | (flag-sequence | |
410 | (nnimap-send-command "UID FETCH 1:* FLAGS"))) | |
411 | (nnimap-wait-for-response flag-sequence) | |
412 | (setq marks | |
413 | (nnimap-flags-to-marks | |
414 | (nnimap-parse-flags | |
415 | (list (list group-sequence flag-sequence 1 group))))) | |
416 | (when info | |
417 | (nnimap-update-infos marks (list info))) | |
418 | (goto-char (point-max)) | |
419 | (cond | |
420 | (marks | |
421 | (setq high (nth 3 (car marks)) | |
422 | low (nth 4 (car marks)))) | |
423 | ((re-search-backward "UIDNEXT \\([0-9]+\\)" nil t) | |
424 | (setq high (string-to-number (match-string 1)) | |
425 | low 1))))) | |
426 | (erase-buffer) | |
427 | (insert | |
428 | (format | |
429 | "211 %d %d %d %S\n" | |
430 | (1+ (- high low)) | |
431 | low high group)))) | |
432 | t))) | |
c113de23 | 433 | |
a46359d4 LMI |
434 | (deffoo nnimap-request-delete-group (group &optional force server) |
435 | (when (nnimap-possibly-change-group nil server) | |
436 | (with-current-buffer (nnimap-buffer) | |
437 | (car (nnimap-command "DELETE %S" (utf7-encode group)))))) | |
438 | ||
20a673b2 KY |
439 | (defun nnimap-get-flags (spec) |
440 | (let ((articles nil) | |
441 | elems) | |
442 | (with-current-buffer (nnimap-buffer) | |
c113de23 | 443 | (erase-buffer) |
20a673b2 KY |
444 | (nnimap-wait-for-response (nnimap-send-command |
445 | "UID FETCH %s FLAGS" spec)) | |
446 | (goto-char (point-min)) | |
447 | (while (re-search-forward "^\\* [0-9]+ FETCH (\\(.*\\))" nil t) | |
448 | (setq elems (nnimap-parse-line (match-string 1))) | |
449 | (push (cons (string-to-number (cadr (member "UID" elems))) | |
450 | (cadr (member "FLAGS" elems))) | |
451 | articles))) | |
452 | (nreverse articles))) | |
a1506d29 | 453 | |
286c4fc2 | 454 | (deffoo nnimap-close-group (group &optional server) |
20a673b2 | 455 | t) |
c113de23 | 456 | |
01c52d31 | 457 | (deffoo nnimap-request-move-article (article group server accept-form |
20a673b2 KY |
458 | &optional last internal-move-group) |
459 | (when (nnimap-possibly-change-group group server) | |
460 | ;; If the move is internal (on the same server), just do it the easy | |
461 | ;; way. | |
462 | (let ((message-id (message-field-value "message-id"))) | |
463 | (if internal-move-group | |
464 | (let ((result | |
465 | (with-current-buffer (nnimap-buffer) | |
466 | (nnimap-command "UID COPY %d %S" | |
467 | article | |
468 | (utf7-encode internal-move-group t))))) | |
469 | (when (car result) | |
470 | (nnimap-delete-article article) | |
471 | (cons internal-move-group | |
472 | (nnimap-find-article-by-message-id | |
473 | internal-move-group message-id)))) | |
474 | (with-temp-buffer | |
a46359d4 LMI |
475 | (when (nnimap-request-article article group server (current-buffer)) |
476 | (let ((result (eval accept-form))) | |
477 | (when result | |
478 | (nnimap-delete-article article) | |
479 | result)))))))) | |
20a673b2 KY |
480 | |
481 | (deffoo nnimap-request-expire-articles (articles group &optional server force) | |
482 | (cond | |
483 | ((not (nnimap-possibly-change-group group server)) | |
484 | articles) | |
485 | (force | |
486 | (unless (nnimap-delete-article articles) | |
487 | (message "Article marked for deletion, but not expunged.")) | |
488 | nil) | |
489 | (t | |
490 | articles))) | |
491 | ||
492 | (defun nnimap-find-article-by-message-id (group message-id) | |
493 | (when (nnimap-possibly-change-group group nil) | |
494 | (with-current-buffer (nnimap-buffer) | |
495 | (let ((result | |
496 | (nnimap-command "UID SEARCH HEADER Message-Id %S" message-id)) | |
497 | article) | |
498 | (when (car result) | |
499 | ;; Select the last instance of the message in the group. | |
500 | (and (setq article | |
501 | (car (last (assoc "SEARCH" (cdr result))))) | |
502 | (string-to-number article))))))) | |
503 | ||
504 | (defun nnimap-delete-article (articles) | |
505 | (with-current-buffer (nnimap-buffer) | |
506 | (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)" | |
507 | (nnimap-article-ranges articles)) | |
508 | (when (member "UIDPLUS" (nnimap-capabilities nnimap-object)) | |
509 | (nnimap-send-command "UID EXPUNGE %s" | |
510 | (nnimap-article-ranges articles)) | |
511 | t))) | |
512 | ||
513 | (deffoo nnimap-request-scan (&optional group server) | |
514 | (when (and (nnimap-possibly-change-group nil server) | |
515 | (equal group nnimap-inbox) | |
516 | nnimap-inbox | |
517 | nnimap-split-methods) | |
518 | (nnimap-split-incoming-mail))) | |
519 | ||
520 | (defun nnimap-marks-to-flags (marks) | |
521 | (let (flags flag) | |
522 | (dolist (mark marks) | |
523 | (when (setq flag (cadr (assq mark nnimap-mark-alist))) | |
524 | (push flag flags))) | |
525 | flags)) | |
526 | ||
286c4fc2 | 527 | (deffoo nnimap-request-set-mark (group actions &optional server) |
20a673b2 KY |
528 | (when (nnimap-possibly-change-group group server) |
529 | (let (sequence) | |
530 | (with-current-buffer (nnimap-buffer) | |
531 | ;; Just send all the STORE commands without waiting for | |
532 | ;; response. If they're successful, they're successful. | |
533 | (dolist (action actions) | |
534 | (destructuring-bind (range action marks) action | |
535 | (let ((flags (nnimap-marks-to-flags marks))) | |
536 | (when flags | |
537 | (setq sequence (nnimap-send-command | |
538 | "UID STORE %s %sFLAGS.SILENT (%s)" | |
539 | (nnimap-article-ranges range) | |
540 | (if (eq action 'del) | |
541 | "-" | |
542 | "+") | |
543 | (mapconcat #'identity flags " "))))))) | |
544 | ;; Wait for the last command to complete to avoid later | |
545 | ;; syncronisation problems with the stream. | |
a46359d4 LMI |
546 | (when sequence |
547 | (nnimap-wait-for-response sequence)))))) | |
a1506d29 | 548 | |
c113de23 | 549 | (deffoo nnimap-request-accept-article (group &optional server last) |
20a673b2 KY |
550 | (when (nnimap-possibly-change-group nil server) |
551 | (nnmail-check-syntax) | |
552 | (let ((message (buffer-string)) | |
553 | (message-id (message-field-value "message-id")) | |
554 | sequence) | |
555 | (with-current-buffer (nnimap-buffer) | |
556 | (setq sequence (nnimap-send-command | |
557 | "APPEND %S {%d}" (utf7-encode group t) | |
558 | (length message))) | |
559 | (process-send-string (get-buffer-process (current-buffer)) message) | |
286c4fc2 LMI |
560 | (process-send-string (get-buffer-process (current-buffer)) |
561 | (if (nnimap-newlinep nnimap-object) | |
562 | "\n" | |
563 | "\r\n")) | |
20a673b2 KY |
564 | (let ((result (nnimap-get-response sequence))) |
565 | (when result | |
566 | (cons group | |
567 | (nnimap-find-article-by-message-id group message-id)))))))) | |
568 | ||
569 | (defun nnimap-add-cr () | |
570 | (goto-char (point-min)) | |
571 | (while (re-search-forward "\r?\n" nil t) | |
572 | (replace-match "\r\n" t t))) | |
573 | ||
574 | (defun nnimap-get-groups () | |
575 | (let ((result (nnimap-command "LIST \"\" \"*\"")) | |
576 | groups) | |
577 | (when (car result) | |
578 | (dolist (line (cdr result)) | |
579 | (when (and (equal (car line) "LIST") | |
580 | (not (and (caadr line) | |
581 | (string-match "noselect" (caadr line))))) | |
582 | (push (car (last line)) groups))) | |
583 | (nreverse groups)))) | |
584 | ||
286c4fc2 | 585 | (deffoo nnimap-request-list (&optional server) |
20a673b2 KY |
586 | (nnimap-possibly-change-group nil server) |
587 | (with-current-buffer nntp-server-buffer | |
588 | (erase-buffer) | |
589 | (let ((groups | |
590 | (with-current-buffer (nnimap-buffer) | |
591 | (nnimap-get-groups))) | |
592 | sequences responses) | |
593 | (when groups | |
594 | (with-current-buffer (nnimap-buffer) | |
595 | (dolist (group groups) | |
596 | (push (list (nnimap-send-command "EXAMINE %S" (utf7-encode group t)) | |
597 | group) | |
598 | sequences)) | |
599 | (nnimap-wait-for-response (caar sequences)) | |
600 | (setq responses | |
601 | (nnimap-get-responses (mapcar #'car sequences)))) | |
602 | (dolist (response responses) | |
603 | (let* ((sequence (car response)) | |
604 | (response (cadr response)) | |
605 | (group (cadr (assoc sequence sequences)))) | |
606 | (when (and group | |
607 | (equal (caar response) "OK")) | |
608 | (let ((uidnext (nnimap-find-parameter "UIDNEXT" response)) | |
609 | highest exists) | |
610 | (dolist (elem response) | |
611 | (when (equal (cadr elem) "EXISTS") | |
612 | (setq exists (string-to-number (car elem))))) | |
613 | (when uidnext | |
614 | (setq highest (1- (string-to-number (car uidnext))))) | |
615 | (cond | |
616 | ((null highest) | |
617 | (insert (format "%S 0 1 y\n" (utf7-decode group t)))) | |
618 | ((zerop exists) | |
619 | ;; Empty group. | |
620 | (insert (format "%S %d %d y\n" | |
621 | (utf7-decode group t) highest (1+ highest)))) | |
622 | (t | |
623 | ;; Return the widest possible range. | |
624 | (insert (format "%S %d 1 y\n" (utf7-decode group t) | |
625 | (or highest exists))))))))) | |
c113de23 GM |
626 | t)))) |
627 | ||
286c4fc2 | 628 | (deffoo nnimap-retrieve-group-data-early (server infos) |
20a673b2 KY |
629 | (when (nnimap-possibly-change-group nil server) |
630 | (with-current-buffer (nnimap-buffer) | |
631 | ;; QRESYNC handling isn't implemented. | |
632 | (let ((qresyncp (member "notQRESYNC" (nnimap-capabilities nnimap-object))) | |
633 | marks groups sequences) | |
634 | ;; Go through the infos and gather the data needed to know | |
635 | ;; what and how to request the data. | |
636 | (dolist (info infos) | |
637 | (setq marks (gnus-info-marks info)) | |
638 | (push (list (gnus-group-real-name (gnus-info-group info)) | |
639 | (cdr (assq 'active marks)) | |
640 | (cdr (assq 'uid marks))) | |
641 | groups)) | |
642 | ;; Then request the data. | |
643 | (erase-buffer) | |
644 | (dolist (elem groups) | |
645 | (if (and qresyncp | |
646 | (nth 2 elem)) | |
647 | (push | |
648 | (list 'qresync | |
649 | (nnimap-send-command "EXAMINE %S (QRESYNC (%s %s))" | |
650 | (car elem) | |
651 | (car (nth 2 elem)) | |
652 | (cdr (nth 2 elem))) | |
653 | nil | |
654 | (car elem)) | |
655 | sequences) | |
656 | (let ((start | |
657 | (if (nth 1 elem) | |
658 | ;; Fetch the last 100 flags. | |
659 | (max 1 (- (cdr (nth 1 elem)) 100)) | |
660 | 1))) | |
661 | (push (list (nnimap-send-command "EXAMINE %S" (car elem)) | |
662 | (nnimap-send-command "UID FETCH %d:* FLAGS" start) | |
663 | start | |
664 | (car elem)) | |
665 | sequences)))) | |
666 | sequences)))) | |
667 | ||
286c4fc2 | 668 | (deffoo nnimap-finish-retrieve-group-infos (server infos sequences) |
20a673b2 KY |
669 | (when (and sequences |
670 | (nnimap-possibly-change-group nil server)) | |
671 | (with-current-buffer (nnimap-buffer) | |
672 | ;; Wait for the final data to trickle in. | |
673 | (nnimap-wait-for-response (cadar sequences)) | |
674 | ;; Now we should have all the data we need, no matter whether | |
675 | ;; we're QRESYNCING, fetching all the flags from scratch, or | |
676 | ;; just fetching the last 100 flags per group. | |
677 | (nnimap-update-infos (nnimap-flags-to-marks | |
678 | (nnimap-parse-flags | |
679 | (nreverse sequences))) | |
e8861cd2 LMI |
680 | infos) |
681 | ;; Finally, just return something resembling an active file in | |
682 | ;; the nntp buffer, so that the agent can save the info, too. | |
683 | (with-current-buffer nntp-server-buffer | |
684 | (erase-buffer) | |
685 | (dolist (info infos) | |
686 | (let* ((group (gnus-info-group info)) | |
687 | (active (gnus-active group))) | |
688 | (when active | |
689 | (insert (format "%S %d %d y\n" | |
690 | (gnus-group-real-name group) | |
691 | (cdr active) | |
692 | (car active)))))))))) | |
20a673b2 KY |
693 | |
694 | (defun nnimap-update-infos (flags infos) | |
695 | (dolist (info infos) | |
696 | (let ((group (gnus-group-real-name (gnus-info-group info)))) | |
697 | (nnimap-update-info info (cdr (assoc group flags)))))) | |
698 | ||
699 | (defun nnimap-update-info (info marks) | |
700 | (when marks | |
701 | (destructuring-bind (existing flags high low uidnext start-article) marks | |
702 | (let ((group (gnus-info-group info)) | |
703 | (completep (and start-article | |
704 | (= start-article 1)))) | |
705 | ;; First set the active ranges based on high/low. | |
706 | (if (or completep | |
707 | (not (gnus-active group))) | |
708 | (gnus-set-active group | |
709 | (if high | |
710 | (cons low high) | |
711 | ;; No articles in this group. | |
712 | (cons (1- uidnext) uidnext))) | |
713 | (setcdr (gnus-active group) high)) | |
714 | ;; Then update the list of read articles. | |
715 | (let* ((unread | |
716 | (gnus-compress-sequence | |
717 | (gnus-set-difference | |
718 | (gnus-set-difference | |
719 | existing | |
720 | (cdr (assoc "\\Seen" flags))) | |
721 | (cdr (assoc "\\Flagged" flags))))) | |
722 | (read (gnus-range-difference | |
723 | (cons start-article high) unread))) | |
724 | (when (> start-article 1) | |
725 | (setq read | |
726 | (gnus-range-nconcat | |
286c4fc2 LMI |
727 | (if (> start-article 1) |
728 | (gnus-sorted-range-intersection | |
729 | (cons 1 (1- start-article)) | |
730 | (gnus-info-read info)) | |
731 | (gnus-info-read info)) | |
20a673b2 KY |
732 | read))) |
733 | (gnus-info-set-read info read) | |
734 | ;; Update the marks. | |
735 | (setq marks (gnus-info-marks info)) | |
736 | ;; Note the active level for the next run-through. | |
737 | (let ((active (assq 'active marks))) | |
738 | (if active | |
739 | (setcdr active (gnus-active group)) | |
740 | (push (cons 'active (gnus-active group)) marks))) | |
741 | (dolist (type (cdr nnimap-mark-alist)) | |
742 | (let ((old-marks (assoc (car type) marks)) | |
743 | (new-marks (gnus-compress-sequence | |
744 | (cdr (assoc (cadr type) flags))))) | |
745 | (setq marks (delq old-marks marks)) | |
746 | (pop old-marks) | |
747 | (when (and old-marks | |
748 | (> start-article 1)) | |
749 | (setq old-marks (gnus-range-difference | |
286c4fc2 LMI |
750 | old-marks |
751 | (cons start-article high))) | |
20a673b2 KY |
752 | (setq new-marks (gnus-range-nconcat old-marks new-marks))) |
753 | (when new-marks | |
754 | (push (cons (car type) new-marks) marks))) | |
286c4fc2 LMI |
755 | (gnus-info-set-marks info marks t) |
756 | (nnimap-store-info info (gnus-active group)))))))) | |
757 | ||
758 | (defun nnimap-store-info (info active) | |
759 | (let* ((group (gnus-group-real-name (gnus-info-group info))) | |
760 | (entry (assoc group nnimap-current-infos))) | |
761 | (if entry | |
762 | (setcdr entry (list info active)) | |
763 | (push (list group info active) nnimap-current-infos)))) | |
20a673b2 KY |
764 | |
765 | (defun nnimap-flags-to-marks (groups) | |
766 | (let (data group totalp uidnext articles start-article mark) | |
767 | (dolist (elem groups) | |
768 | (setq group (car elem) | |
769 | uidnext (cadr elem) | |
770 | start-article (caddr elem) | |
771 | articles (cdddr elem)) | |
772 | (let ((high (caar articles)) | |
773 | marks low existing) | |
774 | (dolist (article articles) | |
775 | (setq low (car article)) | |
776 | (push (car article) existing) | |
777 | (dolist (flag (cdr article)) | |
778 | (setq mark (assoc flag marks)) | |
779 | (if (not mark) | |
780 | (push (list flag (car article)) marks) | |
781 | (setcdr mark (cons (car article) (cdr mark))))) | |
782 | (push (list group existing marks high low uidnext start-article) | |
783 | data)))) | |
784 | data)) | |
785 | ||
786 | (defun nnimap-parse-flags (sequences) | |
787 | (goto-char (point-min)) | |
788 | (let (start end articles groups uidnext elems) | |
789 | (dolist (elem sequences) | |
790 | (destructuring-bind (group-sequence flag-sequence totalp group) elem | |
791 | ;; The EXAMINE was successful. | |
792 | (when (and (search-forward (format "\n%d OK " group-sequence) nil t) | |
793 | (progn | |
794 | (forward-line 1) | |
795 | (setq start (point)) | |
796 | (if (re-search-backward "UIDNEXT \\([0-9]+\\)" | |
797 | (or end (point-min)) t) | |
798 | (setq uidnext (string-to-number (match-string 1))) | |
799 | (setq uidnext nil)) | |
800 | (goto-char start)) | |
801 | ;; The UID FETCH FLAGS was successful. | |
802 | (search-forward (format "\n%d OK " flag-sequence) nil t)) | |
803 | (setq end (point)) | |
804 | (goto-char start) | |
805 | (while (re-search-forward "^\\* [0-9]+ FETCH (\\(.*\\))" end t) | |
806 | (setq elems (nnimap-parse-line (match-string 1))) | |
807 | (push (cons (string-to-number (cadr (member "UID" elems))) | |
808 | (cadr (member "FLAGS" elems))) | |
809 | articles)) | |
810 | (push (nconc (list group uidnext totalp) articles) groups) | |
811 | (setq articles nil)))) | |
812 | groups)) | |
813 | ||
814 | (defun nnimap-find-process-buffer (buffer) | |
815 | (cadr (assoc buffer nnimap-connection-alist))) | |
816 | ||
286c4fc2 | 817 | (deffoo nnimap-request-post (&optional server) |
20a673b2 KY |
818 | (setq nnimap-status-string "Read-only server") |
819 | nil) | |
c113de23 | 820 | |
20a673b2 KY |
821 | (defun nnimap-possibly-change-group (group server) |
822 | (let ((open-result t)) | |
823 | (when (and server | |
824 | (not (nnimap-server-opened server))) | |
825 | (setq open-result (nnimap-open-server server))) | |
826 | (cond | |
827 | ((not open-result) | |
828 | nil) | |
829 | ((not group) | |
830 | t) | |
831 | (t | |
832 | (with-current-buffer (nnimap-buffer) | |
833 | (if (equal group (nnimap-group nnimap-object)) | |
834 | t | |
835 | (let ((result (nnimap-command "SELECT %S" (utf7-encode group t)))) | |
836 | (when (car result) | |
286c4fc2 LMI |
837 | (setf (nnimap-group nnimap-object) group |
838 | (nnimap-select-result nnimap-object) result) | |
20a673b2 KY |
839 | result)))))))) |
840 | ||
841 | (defun nnimap-find-connection (buffer) | |
842 | "Find the connection delivering to BUFFER." | |
843 | (let ((entry (assoc buffer nnimap-connection-alist))) | |
844 | (when entry | |
845 | (if (and (buffer-name (cadr entry)) | |
846 | (get-buffer-process (cadr entry)) | |
847 | (memq (process-status (get-buffer-process (cadr entry))) | |
848 | '(open run))) | |
849 | (get-buffer-process (cadr entry)) | |
850 | (setq nnimap-connection-alist (delq entry nnimap-connection-alist)) | |
851 | nil)))) | |
852 | ||
853 | (defvar nnimap-sequence 0) | |
854 | ||
855 | (defun nnimap-send-command (&rest args) | |
856 | (process-send-string | |
857 | (get-buffer-process (current-buffer)) | |
858 | (nnimap-log-command | |
286c4fc2 | 859 | (format "%d %s%s\n" |
20a673b2 | 860 | (incf nnimap-sequence) |
286c4fc2 LMI |
861 | (apply #'format args) |
862 | (if (nnimap-newlinep nnimap-object) | |
863 | "" | |
864 | "\r")))) | |
20a673b2 KY |
865 | nnimap-sequence) |
866 | ||
867 | (defun nnimap-log-command (command) | |
868 | (with-current-buffer (get-buffer-create "*imap log*") | |
869 | (goto-char (point-max)) | |
870 | (insert (format-time-string "%H:%M:%S") " " command)) | |
871 | command) | |
872 | ||
873 | (defun nnimap-command (&rest args) | |
874 | (erase-buffer) | |
875 | (let* ((sequence (apply #'nnimap-send-command args)) | |
876 | (response (nnimap-get-response sequence))) | |
877 | (if (equal (caar response) "OK") | |
878 | (cons t response) | |
879 | (nnheader-report 'nnimap "%s" | |
a46359d4 LMI |
880 | (mapconcat (lambda (a) |
881 | (format "%s" a)) | |
882 | (car response) " ")) | |
20a673b2 KY |
883 | nil))) |
884 | ||
885 | (defun nnimap-get-response (sequence) | |
886 | (nnimap-wait-for-response sequence) | |
887 | (nnimap-parse-response)) | |
888 | ||
286c4fc2 LMI |
889 | (defun nnimap-wait-for-connection () |
890 | (let ((process (get-buffer-process (current-buffer)))) | |
891 | (goto-char (point-min)) | |
892 | (while (and (memq (process-status process) | |
893 | '(open run)) | |
bdaa75c7 | 894 | (not (re-search-forward "^\\* .*\n" nil t))) |
286c4fc2 LMI |
895 | (nnheader-accept-process-output process) |
896 | (goto-char (point-min))) | |
bdaa75c7 LMI |
897 | (forward-line -1) |
898 | (and (looking-at "\\* \\([A-Z0-9]+\\)") | |
899 | (match-string 1)))) | |
286c4fc2 | 900 | |
20a673b2 | 901 | (defun nnimap-wait-for-response (sequence &optional messagep) |
bdaa75c7 LMI |
902 | (let ((process (get-buffer-process (current-buffer)))) |
903 | (goto-char (point-max)) | |
904 | (while (and (memq (process-status process) | |
905 | '(open run)) | |
906 | (not (re-search-backward (format "^%d .*\n" sequence) | |
907 | (max (point-min) (- (point) 500)) | |
908 | t))) | |
909 | (when messagep | |
910 | (message "Read %dKB" (/ (buffer-size) 1000))) | |
911 | (nnheader-accept-process-output process) | |
912 | (goto-char (point-max))))) | |
20a673b2 KY |
913 | |
914 | (defun nnimap-parse-response () | |
915 | (let ((lines (split-string (nnimap-last-response-string) "\r\n" t)) | |
916 | result) | |
917 | (dolist (line lines) | |
918 | (push (cdr (nnimap-parse-line line)) result)) | |
919 | ;; Return the OK/error code first, and then all the "continuation | |
920 | ;; lines" afterwards. | |
921 | (cons (pop result) | |
922 | (nreverse result)))) | |
923 | ||
924 | ;; Parse an IMAP response line lightly. They look like | |
925 | ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse | |
926 | ;; the lines into a list of strings and lists of string. | |
927 | (defun nnimap-parse-line (line) | |
928 | (let (char result) | |
929 | (with-temp-buffer | |
930 | (insert line) | |
931 | (goto-char (point-min)) | |
932 | (while (not (eobp)) | |
933 | (if (eql (setq char (following-char)) ? ) | |
934 | (forward-char 1) | |
935 | (push | |
936 | (cond | |
937 | ((eql char ?\[) | |
938 | (split-string (buffer-substring | |
939 | (1+ (point)) (1- (search-forward "]"))))) | |
940 | ((eql char ?\() | |
941 | (split-string (buffer-substring | |
942 | (1+ (point)) (1- (search-forward ")"))))) | |
943 | ((eql char ?\") | |
944 | (forward-char 1) | |
945 | (buffer-substring (point) (1- (search-forward "\"")))) | |
946 | (t | |
947 | (buffer-substring (point) (if (search-forward " " nil t) | |
948 | (1- (point)) | |
949 | (goto-char (point-max)))))) | |
950 | result))) | |
951 | (nreverse result)))) | |
952 | ||
953 | (defun nnimap-last-response-string () | |
954 | (save-excursion | |
955 | (forward-line 1) | |
956 | (let ((end (point))) | |
957 | (forward-line -1) | |
958 | (when (not (bobp)) | |
959 | (forward-line -1) | |
960 | (while (and (not (bobp)) | |
961 | (eql (following-char) ?*)) | |
962 | (forward-line -1)) | |
963 | (unless (eql (following-char) ?*) | |
964 | (forward-line 1))) | |
965 | (buffer-substring (point) end)))) | |
966 | ||
967 | (defun nnimap-get-responses (sequences) | |
968 | (let (responses) | |
969 | (dolist (sequence sequences) | |
970 | (goto-char (point-min)) | |
971 | (when (re-search-forward (format "^%d " sequence) nil t) | |
972 | (push (list sequence (nnimap-parse-response)) | |
973 | responses))) | |
974 | responses)) | |
975 | ||
976 | (defvar nnimap-incoming-split-list nil) | |
977 | ||
978 | (defun nnimap-fetch-inbox (articles) | |
979 | (erase-buffer) | |
980 | (nnimap-wait-for-response | |
981 | (nnimap-send-command | |
982 | "UID FETCH %s %s" | |
983 | (nnimap-article-ranges articles) | |
984 | (format "(UID %s%s)" | |
985 | (format | |
986 | (if (member "IMAP4REV1" | |
987 | (nnimap-capabilities nnimap-object)) | |
988 | "BODY.PEEK[HEADER] BODY.PEEK" | |
989 | "RFC822.PEEK")) | |
990 | (if nnimap-split-download-body-default | |
a46359d4 | 991 | "[]" |
20a673b2 KY |
992 | "[1]"))) |
993 | t)) | |
994 | ||
995 | (defun nnimap-split-incoming-mail () | |
996 | (with-current-buffer (nnimap-buffer) | |
997 | (let ((nnimap-incoming-split-list nil) | |
998 | (nnmail-split-methods nnimap-split-methods) | |
999 | (nnmail-inhibit-default-split-group t) | |
1000 | (groups (nnimap-get-groups)) | |
1001 | new-articles) | |
1002 | (erase-buffer) | |
1003 | (nnimap-command "SELECT %S" nnimap-inbox) | |
1004 | (setq new-articles (nnimap-new-articles (nnimap-get-flags "1:*"))) | |
1005 | (when new-articles | |
1006 | (nnimap-fetch-inbox new-articles) | |
1007 | (nnimap-transform-split-mail) | |
1008 | (nnheader-ms-strip-cr) | |
1009 | (nnmail-cache-open) | |
1010 | (nnmail-split-incoming (current-buffer) | |
1011 | #'nnimap-save-mail-spec | |
1012 | nil nil | |
1013 | #'nnimap-dummy-active-number) | |
1014 | (when nnimap-incoming-split-list | |
1015 | (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list)) | |
1016 | sequences) | |
1017 | ;; Create any groups that doesn't already exist on the | |
1018 | ;; server first. | |
1019 | (dolist (spec specs) | |
1020 | (unless (member (car spec) groups) | |
1021 | (nnimap-command "CREATE %S" (utf7-encode (car spec) t)))) | |
1022 | ;; Then copy over all the messages. | |
1023 | (erase-buffer) | |
1024 | (dolist (spec specs) | |
1025 | (let ((group (car spec)) | |
1026 | (ranges (cdr spec))) | |
1027 | (push (list (nnimap-send-command "UID COPY %s %S" | |
1028 | (nnimap-article-ranges ranges) | |
1029 | (utf7-encode group t)) | |
1030 | ranges) | |
1031 | sequences))) | |
1032 | ;; Wait for the last COPY response... | |
1033 | (when sequences | |
1034 | (nnimap-wait-for-response (caar sequences)) | |
1035 | ;; And then mark the successful copy actions as deleted, | |
1036 | ;; and possibly expunge them. | |
1037 | (nnimap-mark-and-expunge-incoming | |
1038 | (nnimap-parse-copied-articles sequences))))))))) | |
1039 | ||
1040 | (defun nnimap-mark-and-expunge-incoming (range) | |
1041 | (when range | |
1042 | (setq range (nnimap-article-ranges range)) | |
1043 | (nnimap-send-command | |
1044 | "UID STORE %s +FLAGS.SILENT (\\Deleted)" range) | |
1045 | (cond | |
1046 | ;; If the server supports it, we now delete the message we have | |
1047 | ;; just copied over. | |
1048 | ((member "UIDPLUS" (nnimap-capabilities nnimap-object)) | |
1049 | (nnimap-send-command "UID EXPUNGE %s" range)) | |
1050 | ;; If it doesn't support UID EXPUNGE, then we only expunge if the | |
1051 | ;; user has configured it. | |
1052 | (nnimap-expunge-inbox | |
1053 | (nnimap-send-command "EXPUNGE"))))) | |
1054 | ||
1055 | (defun nnimap-parse-copied-articles (sequences) | |
1056 | (let (sequence copied range) | |
1057 | (goto-char (point-min)) | |
1058 | (while (re-search-forward "^\\([0-9]+\\) OK " nil t) | |
1059 | (setq sequence (string-to-number (match-string 1))) | |
1060 | (when (setq range (cadr (assq sequence sequences))) | |
1061 | (push (gnus-uncompress-range range) copied))) | |
1062 | (gnus-compress-sequence (sort (apply #'nconc copied) #'<)))) | |
1063 | ||
1064 | (defun nnimap-new-articles (flags) | |
1065 | (let (new) | |
1066 | (dolist (elem flags) | |
1067 | (when (or (null (cdr elem)) | |
1068 | (and (not (member "\\Deleted" (cdr elem))) | |
1069 | (not (member "\\Seen" (cdr elem))))) | |
1070 | (push (car elem) new))) | |
1071 | (gnus-compress-sequence (nreverse new)))) | |
1072 | ||
1073 | (defun nnimap-make-split-specs (list) | |
1074 | (let ((specs nil) | |
1075 | entry) | |
1076 | (dolist (elem list) | |
1077 | (destructuring-bind (article spec) elem | |
1078 | (dolist (group (delete nil (mapcar #'car spec))) | |
1079 | (unless (setq entry (assoc group specs)) | |
1080 | (push (setq entry (list group)) specs)) | |
1081 | (setcdr entry (cons article (cdr entry)))))) | |
1082 | (dolist (entry specs) | |
1083 | (setcdr entry (gnus-compress-sequence (sort (cdr entry) #'<)))) | |
1084 | specs)) | |
1085 | ||
1086 | (defun nnimap-transform-split-mail () | |
1087 | (goto-char (point-min)) | |
1088 | (let (article bytes) | |
1089 | (block nil | |
1090 | (while (not (eobp)) | |
1091 | (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)")) | |
1092 | (delete-region (point) (progn (forward-line 1) (point))) | |
1093 | (when (eobp) | |
1094 | (return))) | |
1095 | (setq article (match-string 1) | |
1096 | bytes (nnimap-get-length)) | |
1097 | (delete-region (line-beginning-position) (line-end-position)) | |
1098 | ;; Insert MMDF separator, and a way to remember what this | |
1099 | ;; article UID is. | |
1100 | (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article)) | |
1101 | (forward-char (1+ bytes)) | |
1102 | (setq bytes (nnimap-get-length)) | |
1103 | (delete-region (line-beginning-position) (line-end-position)) | |
1104 | (forward-char (1+ bytes)) | |
1105 | (delete-region (line-beginning-position) (line-end-position)))))) | |
1106 | ||
1107 | (defun nnimap-dummy-active-number (group &optional server) | |
1108 | 1) | |
1109 | ||
1110 | (defun nnimap-save-mail-spec (group-art &optional server full-nov) | |
1111 | (let (article) | |
1112 | (goto-char (point-min)) | |
1113 | (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t)) | |
1114 | (error "Invalid nnimap mail") | |
1115 | (setq article (string-to-number (match-string 1)))) | |
1116 | (push (list article group-art) | |
1117 | nnimap-incoming-split-list))) | |
c113de23 | 1118 | |
c113de23 GM |
1119 | (provide 'nnimap) |
1120 | ||
1121 | ;;; nnimap.el ends here |