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