From 7c3cec13b87404a985dc8f821f8ef6af3d072073 Mon Sep 17 00:00:00 2001 From: Nikolaus Rath Date: Tue, 14 Jul 2015 19:03:09 -0700 Subject: [PATCH 2/2] nnimap.el: Add support for IMAP namespaces. * lisp/gnus/nnimap.el (nnimap-use-namespaces): introduced new server variable. (nnimap-group-to-imap, nnimap-get-groups): transform IMAP group names to Gnus group name by stripping / prefixing personal namespace prefix. (nnimap-open-connection-1): ask server for namespaces and store them. --- lisp/gnus/nnimap.el | 66 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 53 insertions(+), 13 deletions(-) diff --git a/lisp/gnus/nnimap.el b/lisp/gnus/nnimap.el index a942d8..3ede4d 100644 --- a/lisp/gnus/nnimap.el +++ b/lisp/gnus/nnimap.el @@ -55,6 +55,13 @@ If nnimap-stream is `ssl', this will default to `imaps'. If not, it will default to `imap'.") +(defvoo nnimap-use-namespaces nil + "Whether to use IMAP namespaces. +If in Gnus your folder names in all start with (e.g.) `INBOX', +you probably want to set this to t. The effects of this are +purely cosmetical, but changing this variable will affect the +names of your nnimap groups. ") + (defvoo nnimap-stream 'undecided "How nnimap talks to the IMAP server. The value should be either `undecided', `ssl' or `tls', @@ -116,6 +123,8 @@ some servers.") (defun nnimap-encode-gnus-group (group) (encode-coding-string group 'utf-8)) +(setq nnimap-namespaces nil) + (defvoo nnimap-fetch-partial-articles nil "If non-nil, Gnus will fetch partial articles. If t, Gnus will fetch only the first part. If a string, it @@ -168,7 +177,17 @@ textual parts.") (defun nnimap-group-to-imap (group) "Convert Gnus group name to IMAP mailbox name." - (utf7-encode group t)) + (let* ((prefix (cadr (assoc (nnoo-current-server 'nnimap) + nnimap-namespaces))) + (inbox (substring prefix 0 -1))) + (utf7-encode + (cond ((or (not prefix) + (string-equal group inbox)) + group) + ((string-prefix-p "#" group) + (substring group 1)) + (t + (concat prefix group))) t))) (defun nnimap-buffer () (nnimap-find-process-buffer nntp-server-buffer)) @@ -445,7 +464,8 @@ textual parts.") (props (cdr stream-list)) (greeting (plist-get props :greeting)) (capabilities (plist-get props :capabilities)) - (stream-type (plist-get props :type))) + (stream-type (plist-get props :type)) + (server (nnoo-current-server 'nnimap))) (when (and stream (not (memq (process-status stream) '(open run)))) (setq stream nil)) @@ -478,9 +498,7 @@ textual parts.") ;; the virtual server name and the address (nnimap-credentials (gnus-delete-duplicates - (list - (nnoo-current-server 'nnimap) - nnimap-address)) + (list server nnimap-address)) ports nnimap-user)))) (setq nnimap-object nil) @@ -499,7 +517,21 @@ textual parts.") (dolist (response (cddr (nnimap-command "CAPABILITY"))) (when (string= "CAPABILITY" (upcase (car response))) (setf (nnimap-capabilities nnimap-object) - (mapcar #'upcase (cdr response)))))) + (mapcar #'upcase (cdr response))))) + (when (and nnimap-use-namespaces + (nnimap-capability "NAMESPACE")) + (erase-buffer) + (nnimap-wait-for-response (nnimap-send-command "NAMESPACE")) + (let ((response (nnimap-last-response-string))) + (when (string-match + "^\\*\\W+NAMESPACE\\W+((\"\\([^\"\n]+\\)\"\\W+\"\\(.\\)\"))\\W+" + response) + (let ((namespace (cons (match-string 1 response) + (match-string 2 response))) + (entry (assoc server nnimap-namespaces))) + (if entry + (setcdr entry namespace) + (push (cons server namespace) nnimap-namespaces))))))) ;; If the login failed, then forget the credentials ;; that are now possibly cached. (dolist (host (list (nnoo-current-server 'nnimap) @@ -1272,8 +1304,12 @@ If LIMIT, first try to limit the search to the N last articles." (defun nnimap-get-groups () (erase-buffer) - (let ((sequence (nnimap-send-command "LIST \"\" \"*\"")) - groups) + (let* ((sequence (nnimap-send-command "LIST \"\" \"*\"")) + (prefix (cadr (assoc (nnoo-current-server 'nnimap) + nnimap-namespaces))) + (prefix-len (length prefix)) + (inbox (substring prefix 0 -1)) + groups) (nnimap-wait-for-response sequence) (subst-char-in-region (point-min) (point-max) ?\\ ?% t) @@ -1290,11 +1326,15 @@ If LIMIT, first try to limit the search to the N last articles." (skip-chars-backward " \r\"") (point))))) (unless (member '%NoSelect flags) - (push (utf7-decode (if (stringp group) - group - (format "%s" group)) - t) - groups)))) + (let* ((group (utf7-decode (if (stringp group) group + (format "%s" group)) t)) + (group (cond ((equal inbox group) + group) + ((string-prefix-p prefix group) + (substring group prefix-len)) + (t + (concat "#" group))))) + (push group groups))))) (nreverse groups))) (defun nnimap-get-responses (sequences) -- 2.11.0