(require 'gnus)
(require 'nnoo)
(require 'netrc)
+(require 'parse-time)
(nnoo-declare nnimap)
(defvoo nnimap-inbox nil
"The mail box where incoming mail arrives and should be split out of.")
-(defvoo nnimap-expunge-inbox nil
- "If non-nil, expunge the inbox after fetching mail.
-This is always done if the server supports UID EXPUNGE, but it's
-not done by default on servers that doesn't support that command.")
-
(defvoo nnimap-authenticator nil
"How nnimap authenticate itself to the server.
Possible choices are nil (use default methods) or `anonymous'.")
likely value would be \"text/\" to automatically fetch all
textual parts.")
+(defvoo nnimap-expunge t
+ "If non-nil, expunge articles after deleting them.
+This is always done if the server supports UID EXPUNGE, but it's
+not done by default on servers that doesn't support that command.")
+
+
(defvoo nnimap-connection-alist nil)
(defvoo nnimap-current-infos nil)
(defun nnimap-transform-headers ()
(goto-char (point-min))
- (let (article bytes lines)
+ (let (article bytes lines size)
(block nil
(while (not (eobp))
(while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
bytes (nnimap-get-length)
lines nil)
(beginning-of-line)
+ (setq size
+ (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
+ (line-end-position)
+ t)
+ (match-string 1)))
+ (beginning-of-line)
(when (search-forward "BODYSTRUCTURE" (line-end-position) t)
(let ((structure (ignore-errors (read (current-buffer)))))
(while (and (consp structure)
(delete-region (line-beginning-position) (line-end-position))
(insert (format "211 %s Article retrieved." article))
(forward-line 1)
- (insert (format "Chars: %d\n" bytes))
+ (when size
+ (insert (format "Chars: %s\n" size)))
(when lines
(insert (format "Lines: %s\n" lines)))
(re-search-forward "^\r$")
(goto-char (+ (point) bytes))
(delete-region (point) (point-max))
(nnheader-ms-strip-cr))
- t)))))))
+ (cons group article))))))))
(defun nnimap-find-wanted-parts (structure)
- (let ((nnimap-level 1))
- (message-flatten-list (nnimap-find-wanted-parts-1 structure))))
+ (message-flatten-list (nnimap-find-wanted-parts-1 structure "")))
-(defun nnimap-find-wanted-parts-1 (structure)
- (let (levels)
+(defun nnimap-find-wanted-parts-1 (structure prefix)
+ (let ((num 1)
+ parts)
(while (consp (car structure))
(let ((sub (pop structure)))
(if (consp (car sub))
- (push (nnimap-find-wanted-parts-1 sub) levels)
+ (push (nnimap-find-wanted-parts-1
+ sub (if (string= prefix "")
+ (number-to-string num)
+ (format "%s.%s" prefix num)))
+ parts)
(let ((type (format "%s/%s" (nth 0 sub) (nth 1 sub))))
(when (string-match nnimap-fetch-partial-articles type)
- (push nnimap-level levels)))
- (incf nnimap-level))))
- (nreverse levels)))
+ (push (if (string= prefix "")
+ (number-to-string num)
+ (format "%s.%s" prefix num))
+ parts)))
+ (incf num))))
+ (nreverse parts)))
(deffoo nnimap-request-group (group &optional server dont-check info)
- (with-current-buffer nntp-server-buffer
- (let ((result (nnimap-possibly-change-group group server))
- articles active marks high low)
+ (let ((result (nnimap-possibly-change-group group server))
+ articles active marks high low)
+ (with-current-buffer nntp-server-buffer
(when result
(if (and dont-check
(setq active (nth 2 (assoc group nnimap-current-infos))))
(with-current-buffer (nnimap-buffer)
(erase-buffer)
(let ((group-sequence
- (nnimap-send-command "SELECT %S" (utf7-encode group)))
+ (nnimap-send-command "SELECT %S" (utf7-encode group t)))
(flag-sequence
(nnimap-send-command "UID FETCH 1:* FLAGS")))
(nnimap-wait-for-response flag-sequence)
(setq high (nth 3 (car marks))
low (nth 4 (car marks))))
((re-search-backward "UIDNEXT \\([0-9]+\\)" nil t)
- (setq high (string-to-number (match-string 1))
+ (setq high (1- (string-to-number (match-string 1)))
low 1)))))
(erase-buffer)
(insert
(format
- "211 %d %d %d %S\n"
- (1+ (- high low))
- low high group))))
- t)))
+ "211 %d %d %d %S\n" (1+ (- high low)) low high group)))
+ t))))
+
+(deffoo nnimap-request-create-group (group &optional server args)
+ (when (nnimap-possibly-change-group nil server)
+ (with-current-buffer (nnimap-buffer)
+ (car (nnimap-command "CREATE %S" (utf7-encode group t))))))
+
+(deffoo nnimap-request-delete-group (group &optional force server)
+ (when (nnimap-possibly-change-group nil server)
+ (with-current-buffer (nnimap-buffer)
+ (car (nnimap-command "DELETE %S" (utf7-encode group t))))))
+
+(deffoo nnimap-request-expunge-group (group &optional server)
+ (when (nnimap-possibly-change-group group server)
+ (with-current-buffer (nnimap-buffer)
+ (car (nnimap-command "EXPUNGE")))))
(defun nnimap-get-flags (spec)
(let ((articles nil)
(deffoo nnimap-request-move-article (article group server accept-form
&optional last internal-move-group)
- (when (nnimap-possibly-change-group group server)
- ;; If the move is internal (on the same server), just do it the easy
- ;; way.
- (let ((message-id (message-field-value "message-id")))
- (if internal-move-group
- (let ((result
- (with-current-buffer (nnimap-buffer)
- (nnimap-command "UID COPY %d %S"
- article
- (utf7-encode internal-move-group t)))))
- (when (car result)
- (nnimap-delete-article article)
- (cons internal-move-group
- (nnimap-find-article-by-message-id
- internal-move-group message-id))))
- (with-temp-buffer
+ (with-temp-buffer
+ (when (nnimap-request-article article group server (current-buffer))
+ ;; If the move is internal (on the same server), just do it the easy
+ ;; way.
+ (let ((message-id (message-field-value "message-id")))
+ (if internal-move-group
+ (let ((result
+ (with-current-buffer (nnimap-buffer)
+ (nnimap-command "UID COPY %d %S"
+ article
+ (utf7-encode internal-move-group t)))))
+ (when (car result)
+ (nnimap-delete-article article)
+ (cons internal-move-group
+ (nnimap-find-article-by-message-id
+ internal-move-group message-id))))
+ ;; Move the article to a different method.
(let ((result (eval accept-form)))
(when result
(nnimap-delete-article article)
(deffoo nnimap-request-expire-articles (articles group &optional server force)
(cond
+ ((null articles)
+ nil)
((not (nnimap-possibly-change-group group server))
articles)
- (force
+ ((and force
+ (eq nnmail-expiry-target 'delete))
(unless (nnimap-delete-article articles)
(message "Article marked for deletion, but not expunged."))
nil)
(t
- articles)))
+ (let ((deletable-articles
+ (if force
+ articles
+ (gnus-sorted-intersection
+ articles
+ (nnimap-find-expired-articles group)))))
+ (if (null deletable-articles)
+ articles
+ (if (eq nnmail-expiry-target 'delete)
+ (nnimap-delete-article deletable-articles)
+ (setq deletable-articles
+ (nnimap-process-expiry-targets
+ deletable-articles group server)))
+ ;; Return the articles we didn't delete.
+ (gnus-sorted-complement articles deletable-articles))))))
+
+(defun nnimap-process-expiry-targets (articles group server)
+ (let ((deleted-articles nil))
+ (dolist (article articles)
+ (let ((target nnmail-expiry-target))
+ (with-temp-buffer
+ (when (nnimap-request-article article group server (current-buffer))
+ (message "Expiring article %s:%d" group article)
+ (when (functionp target)
+ (setq target (funcall target group)))
+ (when (and target
+ (not (eq target 'delete)))
+ (if (or (gnus-request-group target t)
+ (gnus-request-create-group target))
+ (nnmail-expiry-target-group target group)
+ (setq target nil)))
+ (when target
+ (push article deleted-articles))))))
+ ;; Change back to the current group again.
+ (nnimap-possibly-change-group group server)
+ (setq deleted-articles (nreverse deleted-articles))
+ (nnimap-delete-article deleted-articles)
+ deleted-articles))
+
+(defun nnimap-find-expired-articles (group)
+ (let ((cutoff (nnmail-expired-article-p group nil nil)))
+ (with-current-buffer (nnimap-buffer)
+ (let ((result
+ (nnimap-command
+ "UID SEARCH SENTBEFORE %s"
+ (format-time-string
+ (format "%%d-%s-%%Y"
+ (upcase
+ (car (rassoc (nth 4 (decode-time cutoff))
+ parse-time-months))))
+ cutoff))))
+ (and (car result)
+ (delete 0 (mapcar #'string-to-number
+ (cdr (assoc "SEARCH" (cdr result))))))))))
+
(defun nnimap-find-article-by-message-id (group message-id)
(when (nnimap-possibly-change-group group nil)
(with-current-buffer (nnimap-buffer)
(nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)"
(nnimap-article-ranges articles))
- (when (member "UIDPLUS" (nnimap-capabilities nnimap-object))
- (nnimap-send-command "UID EXPUNGE %s"
- (nnimap-article-ranges articles))
- t)))
+ (cond
+ ((member "UIDPLUS" (nnimap-capabilities nnimap-object))
+ (nnimap-command "UID EXPUNGE %s"
+ (nnimap-article-ranges articles))
+ t)
+ (nnimap-expunge
+ (nnimap-command "EXPUNGE")
+ t)
+ (t (gnus-message 7 (concat "nnimap: nnimap-expunge is not set and the "
+ "server doesn't support UIDPLUS, so we won't "
+ "delete this article now"))))))
(deffoo nnimap-request-scan (&optional group server)
(when (and (nnimap-possibly-change-group nil server)
(mapconcat #'identity flags " ")))))))
;; Wait for the last command to complete to avoid later
;; syncronisation problems with the stream.
- (nnimap-wait-for-response sequence)))))
+ (when sequence
+ (nnimap-wait-for-response sequence))))))
(deffoo nnimap-request-accept-article (group &optional server last)
(when (nnimap-possibly-change-group nil server)
(if (equal (caar response) "OK")
(cons t response)
(nnheader-report 'nnimap "%s"
- (mapconcat #'identity (car response) " "))
+ (mapconcat (lambda (a)
+ (format "%s" a))
+ (car response) " "))
nil)))
(defun nnimap-get-response (sequence)
(goto-char (point-min))
(while (and (memq (process-status process)
'(open run))
- (not (re-search-forward "^\\* " nil t)))
+ (not (re-search-forward "^\\* .*\n" nil t)))
(nnheader-accept-process-output process)
(goto-char (point-min)))
- (and (looking-at "[A-Z0-9]+")
- (match-string 0))))
+ (forward-line -1)
+ (and (looking-at "\\* \\([A-Z0-9]+\\)")
+ (match-string 1))))
(defun nnimap-wait-for-response (sequence &optional messagep)
(let ((process (get-buffer-process (current-buffer))))
"BODY.PEEK[HEADER] BODY.PEEK"
"RFC822.PEEK"))
(if nnimap-split-download-body-default
- ""
+ "[]"
"[1]")))
t))
(defun nnimap-mark-and-expunge-incoming (range)
(when range
(setq range (nnimap-article-ranges range))
- (nnimap-send-command
- "UID STORE %s +FLAGS.SILENT (\\Deleted)" range)
- (cond
- ;; If the server supports it, we now delete the message we have
- ;; just copied over.
- ((member "UIDPLUS" (nnimap-capabilities nnimap-object))
- (nnimap-send-command "UID EXPUNGE %s" range))
- ;; If it doesn't support UID EXPUNGE, then we only expunge if the
- ;; user has configured it.
- (nnimap-expunge-inbox
- (nnimap-send-command "EXPUNGE")))))
+ (let ((sequence
+ (nnimap-send-command
+ "UID STORE %s +FLAGS.SILENT (\\Deleted)" range)))
+ (cond
+ ;; If the server supports it, we now delete the message we have
+ ;; just copied over.
+ ((member "UIDPLUS" (nnimap-capabilities nnimap-object))
+ (setq sequence (nnimap-send-command "UID EXPUNGE %s" range)))
+ ;; If it doesn't support UID EXPUNGE, then we only expunge if the
+ ;; user has configured it.
+ (nnimap-expunge
+ (setq sequence (nnimap-send-command "EXPUNGE"))))
+ (nnimap-wait-for-response sequence))))
(defun nnimap-parse-copied-articles (sequences)
(let (sequence copied range)