1 ;;; tramp-imap.el --- Tramp interface to IMAP through imap.el
3 ;; Copyright (C) 2009 Free Software Foundation, Inc.
5 ;; Author: Teodor Zlatanov <tzz@lifelogs.com>
6 ;; Keywords: mail, comm
8 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;; Package to provide Tramp over IMAP
29 ;; just load and open files, e.g.
30 ;; /imaps:user@yourhosthere.com:/INBOX.test/1
32 ;; /imap:user@yourhosthere.com:/INBOX.test/1
34 ;; where `imap' goes over IMAP, while `imaps' goes over IMAP+SSL
36 ;; This module will use imap-hash.el to access the IMAP mailbox.
38 ;; This module will use auth-source.el to authenticate against the
39 ;; IMAP server, PLUS it will use auth-source.el to get your passphrase
40 ;; for the symmetrically encrypted messages. For the former, use the
41 ;; usual IMAP ports. For the latter, use the port "tramp-imap".
43 ;; example .authinfo / .netrc file:
45 ;; machine yourhosthere.com port tramp-imap login USER password SYMMETRIC-PASSPHRASE
47 ;; note above is the symmetric encryption passphrase for GPG
48 ;; below is the regular password for IMAP itself and other things on that host
50 ;; machine yourhosthere.com login USER password NORMAL-PASSWORD
57 (require 'tramp-compat)
61 (autoload 'auth-source-user-or-password "auth-source")
63 ;; Define Tramp IMAP method ...
64 (defconst tramp-imap-method "imap"
65 "*Method to connect via IMAP protocol.")
67 (add-to-list 'tramp-methods (list tramp-imap-method '(tramp-default-port 143)))
69 ;; Add a default for `tramp-default-user-alist'. Default is the local user.
70 (add-to-list 'tramp-default-user-alist
71 `(,tramp-imap-method nil ,(user-login-name)))
73 ;; Define Tramp IMAPS method ...
74 (defconst tramp-imaps-method "imaps"
75 "*Method to connect via secure IMAP protocol.")
77 ;; ... and add it to the method list.
78 (add-to-list 'tramp-methods (list tramp-imaps-method '(tramp-default-port 993)))
80 ;; Add a default for `tramp-default-user-alist'. Default is the local user.
81 (add-to-list 'tramp-default-user-alist
82 `(,tramp-imaps-method nil ,(user-login-name)))
84 ;; Add completion function for IMAP method.
85 ;; (tramp-set-completion-function
86 ;; tramp-imap-method tramp-completion-function-alist-ssh) ; TODO: test this
87 ;; tramp-imaps-method tramp-completion-function-alist-ssh) ; TODO: test this
89 ;; New handlers should be added here.
90 (defconst tramp-imap-file-name-handler-alist
92 ;; `access-file' performed by default handler
93 (add-name-to-file . ignore)
94 ;; `byte-compiler-base-file-name' performed by default handler
95 (copy-file . tramp-imap-handle-copy-file)
96 (delete-directory . ignore) ;; tramp-imap-handle-delete-directory)
97 (delete-file . tramp-imap-handle-delete-file)
98 ;; `diff-latest-backup-file' performed by default handler
99 (directory-file-name . tramp-handle-directory-file-name)
100 (directory-files . tramp-handle-directory-files)
101 (directory-files-and-attributes
102 . tramp-imap-handle-directory-files-and-attributes)
103 ;; `dired-call-process' performed by default handler
104 ;; `dired-compress-file' performed by default handler
105 ;; `dired-uncache' performed by default handler
106 (expand-file-name . tramp-imap-handle-expand-file-name)
107 ;; `file-accessible-directory-p' performed by default handler
108 (file-attributes . tramp-imap-handle-file-attributes)
109 (file-directory-p . tramp-imap-handle-file-directory-p)
110 (file-executable-p . tramp-imap-handle-file-executable-p)
111 (file-exists-p . tramp-imap-handle-file-exists-p)
112 (file-local-copy . tramp-imap-handle-file-local-copy)
113 (file-remote-p . tramp-handle-file-remote-p)
114 (file-modes . tramp-handle-file-modes)
115 (file-name-all-completions . tramp-imap-handle-file-name-all-completions)
116 (file-name-as-directory . tramp-handle-file-name-as-directory)
117 (file-name-completion . tramp-handle-file-name-completion)
118 (file-name-directory . tramp-handle-file-name-directory)
119 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
120 ;; `file-name-sans-versions' performed by default handler
121 (file-newer-than-file-p . tramp-imap-handle-file-newer-than-file-p)
122 (file-ownership-preserved-p . ignore)
123 (file-readable-p . tramp-imap-handle-file-readable-p)
124 (file-regular-p . tramp-handle-file-regular-p)
125 (file-symlink-p . tramp-handle-file-symlink-p)
126 ;; `file-truename' performed by default handler
127 (file-writable-p . tramp-imap-handle-file-writable-p)
128 (find-backup-file-name . tramp-handle-find-backup-file-name)
129 ;; `find-file-noselect' performed by default handler
130 ;; `get-file-buffer' performed by default handler
131 (insert-directory . tramp-imap-handle-insert-directory)
132 (insert-file-contents . tramp-imap-handle-insert-file-contents)
133 (load . tramp-handle-load)
134 (make-directory . ignore) ;; tramp-imap-handle-make-directory)
135 (make-directory-internal . ignore) ;; tramp-imap-handle-make-directory-internal)
136 (make-symbolic-link . ignore)
137 (rename-file . tramp-imap-handle-rename-file)
138 (set-file-modes . ignore)
139 (set-file-times . ignore) ;; tramp-imap-handle-set-file-times)
140 (set-visited-file-modtime . ignore)
141 (shell-command . ignore)
142 (substitute-in-file-name . tramp-handle-substitute-in-file-name)
143 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
144 (vc-registered . ignore)
145 (verify-visited-file-modtime . ignore)
146 (write-region . tramp-imap-handle-write-region)
147 (executable-find . ignore)
148 (start-file-process . ignore)
149 (process-file . ignore)
151 "Alist of handler functions for Tramp IMAP method.
152 Operations not mentioned here will be handled by the default Emacs primitives.")
154 (defgroup tramp-imap nil
155 "Tramp over IMAP configuration."
157 :group 'applications)
159 (defcustom tramp-imap-subject-marker "tramp-imap-subject-marker"
160 "The subject marker that Tramp-IMAP will use."
165 ;; TODO: these will be defcustoms later.
166 (defvar tramp-imap-passphrase-cache nil) ;; can be t or 'never
167 (defvar tramp-imap-passphrase nil)
169 (defun tramp-imap-file-name-p (filename)
170 "Check if it's a filename for IMAP protocol."
171 (let ((v (tramp-dissect-file-name filename)))
173 (string= (tramp-file-name-method v) tramp-imap-method)
174 (string= (tramp-file-name-method v) tramp-imaps-method))))
176 (defun tramp-imap-file-name-handler (operation &rest args)
177 "Invoke the IMAP related OPERATION.
178 First arg specifies the OPERATION, second arg is a list of arguments to
179 pass to the OPERATION."
180 (let ((fn (assoc operation tramp-imap-file-name-handler-alist)))
182 (save-match-data (apply (cdr fn) args))
183 (tramp-run-real-handler operation args))))
185 (add-to-list 'tramp-foreign-file-name-handler-alist
186 (cons 'tramp-imap-file-name-p 'tramp-imap-file-name-handler))
188 (defun tramp-imap-handle-copy-file
189 (filename newname &optional ok-if-already-exists keep-date preserve-uid-gid)
190 "Like `copy-file' for Tramp files."
191 (tramp-imap-do-copy-or-rename-file
192 'copy filename newname ok-if-already-exists keep-date preserve-uid-gid))
194 (defun tramp-imap-handle-rename-file
195 (filename newname &optional ok-if-already-exists)
196 "Like `rename-file' for Tramp files."
197 (tramp-imap-do-copy-or-rename-file
198 'rename filename newname ok-if-already-exists t t))
200 (defun tramp-imap-do-copy-or-rename-file
201 (op filename newname &optional ok-if-already-exists keep-date preserve-uid-gid)
202 "Copy or rename a remote file.
203 OP must be `copy' or `rename' and indicates the operation to perform.
204 FILENAME specifies the file to copy or rename, NEWNAME is the name of
205 the new file (for copy) or the new name of the file (for rename).
206 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
207 KEEP-DATE means to make sure that NEWNAME has the same timestamp
208 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
209 the uid and gid if both files are on the same host.
211 This function is invoked by `tramp-imap-handle-copy-file' and
212 `tramp-imap-handle-rename-file'. It is an error if OP is neither
213 of `copy' and `rename'."
214 (unless (memq op '(copy rename))
215 (error "Unknown operation `%s', must be `copy' or `rename'" op))
216 (setq filename (expand-file-name filename))
217 (setq newname (expand-file-name newname))
218 (when (file-directory-p newname)
219 (setq newname (expand-file-name (file-name-nondirectory filename) newname)))
221 (let ((t1 (and (tramp-tramp-file-p filename)
222 (tramp-imap-file-name-p filename)))
223 (t2 (and (tramp-tramp-file-p newname)
224 (tramp-imap-file-name-p newname))))
226 (when (and (not ok-if-already-exists) (file-exists-p newname))
227 (with-parsed-tramp-file-name (if t1 filename newname) nil
229 v 'file-already-exists "File %s already exists" newname)))
231 (with-parsed-tramp-file-name (if t1 filename newname) nil
232 (tramp-message v 0 "Transferring %s to %s..." filename newname))
234 ;; We just make a local copy of FILENAME, and write it then to
235 ;; NEWNAME. This must be optimized, when both files are located
236 ;; on the same IMAP server.
240 (with-parsed-tramp-file-name newname nil
241 (insert (tramp-imap-get-file filename nil))
244 (tramp-imap-file-name-name v)
245 (tramp-imap-get-file-inode newname)
247 ;; One of them is not located on a IMAP mailbox.
248 (insert-file-contents filename)
249 (write-region (point-min) (point-max) newname)))
251 (with-parsed-tramp-file-name (if t1 filename newname) nil
252 (tramp-message v 0 "Transferring %s to %s...done" filename newname))
254 (when (eq op 'rename)
255 (delete-file filename))))
257 ;; TODO: revise this much
258 (defun tramp-imap-handle-expand-file-name (name &optional dir)
259 "Like `expand-file-name' for Tramp files."
260 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
261 (setq dir (or dir default-directory "/"))
262 ;; Unless NAME is absolute, concat DIR and NAME.
263 (unless (file-name-absolute-p name)
264 (setq name (concat (file-name-as-directory dir) name)))
265 ;; If NAME is not a Tramp file, run the real handler.
266 (if (or (tramp-completion-mode-p) (not (tramp-tramp-file-p name)))
267 (tramp-drop-volume-letter
268 (tramp-run-real-handler 'expand-file-name (list name nil)))
270 (with-parsed-tramp-file-name name nil
271 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
272 (setq localname (concat "/" localname)))
273 ;; There might be a double slash, for example when "~/"
274 ;; expands to "/". Remove this.
275 (while (string-match "//" localname)
276 (setq localname (replace-match "/" t t localname)))
277 ;; Do normal `expand-file-name' (this does "/./" and "/../").
278 ;; We bind `directory-sep-char' here for XEmacs on Windows,
279 ;; which would otherwise use backslash. `default-directory' is
280 ;; bound, because on Windows there would be problems with UNC
281 ;; shares or Cygwin mounts.
282 (let ((default-directory (tramp-compat-temporary-file-directory)))
283 (tramp-make-tramp-file-name
285 (tramp-drop-volume-letter
286 (tramp-run-real-handler
287 'expand-file-name (list localname))))))))
289 ;; This function should return "foo/" for directories and "bar" for
291 (defun tramp-imap-handle-file-name-all-completions (filename directory)
292 "Like `file-name-all-completions' for Tramp files."
295 (with-parsed-tramp-file-name (expand-file-name directory) nil
298 (tramp-imap-get-file-entries v localname)))
302 (if (string-match "d" (nth 9 x))
303 (file-name-as-directory (nth 0 x))
307 (defun tramp-imap-get-file-entries (vec localname &optional exact)
308 "Read entries returned by IMAP server. EXACT limits to exact matches.
309 Result is a list of (LOCALNAME LINK COUNT UID GID ATIME MTIME CTIME
310 SIZE MODE WEIRD INODE DEVICE)."
311 (tramp-message vec 5 "working on %s" localname)
312 (let* ((name (tramp-imap-file-name-name vec))
313 (search-name (or name ""))
314 (search-name (if exact (concat search-name "$") search-name))
315 (iht (tramp-imap-make-iht vec search-name)))
316 ;; TODO: catch errors
317 ;; (tramp-error vec 'none "bad name %s or mailbox %s" name mbox))
318 (imap-hash-map (lambda (uid headers body)
319 (let ((subject (substring
320 (aget headers 'Subject "")
321 (length tramp-imap-subject-marker))))
335 (tramp-get-device vec))))
338 (defun tramp-imap-handle-write-region (start end filename &optional append visit lockname confirm)
339 "Like `write-region' for Tramp files."
340 (setq filename (expand-file-name filename))
341 (with-parsed-tramp-file-name filename nil
342 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
343 (when (and (not (featurep 'xemacs))
344 confirm (file-exists-p filename))
345 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
347 (tramp-error v 'file-error "File not overwritten")))
348 (tramp-flush-file-property v localname)
349 (let* ((old-buffer (current-buffer))
350 (inode (tramp-imap-get-file-inode filename))
353 ;; Make sure we have good start and end values.
354 (start (or start min))
358 (setq temp-buffer (if (and (eq start min) (eq end max))
360 ;; If this is a region write, insert the substring.
362 (with-current-buffer old-buffer
363 (buffer-substring-no-properties start end)))
365 (tramp-imap-put-file v
367 (tramp-imap-file-name-name v)
371 (set-visited-file-modtime))))
373 (defun tramp-imap-handle-insert-directory
374 (filename switches &optional wildcard full-directory-p)
375 "Like `insert-directory' for Tramp files."
376 (setq filename (expand-file-name filename))
377 (when full-directory-p
378 ;; Called from `dired-add-entry'.
379 (setq filename (file-name-as-directory filename)))
380 (with-parsed-tramp-file-name filename nil
382 (let ((base (file-name-nondirectory localname))
383 (entries (copy-sequence
384 (tramp-imap-get-file-entries
385 v (file-name-directory localname)))))
388 (when (string-match "\\." base)
389 (setq base (replace-match "\\\\." nil nil base)))
390 (when (string-match "\\*" base)
391 (setq base (replace-match ".*" nil nil base)))
392 (when (string-match "\\?" base)
393 (setq base (replace-match ".?" nil nil base))))
399 (if (or wildcard (zerop (length base)))
400 ;; Check for matching entries.
404 (format "^%s" base) (nth 0 x))
407 ;; We just need the only and only entry FILENAME.
408 (list (assoc base entries)))))
415 (if (string-match "t" switches)
417 (tramp-time-less-p (nth 6 y) (nth 6 x))
419 (string-lessp (nth 0 x) (nth 0 y))))))
421 ;; Handle "-F" switch.
422 (when (string-match "F" switches)
425 (when (not (zerop (length (car x))))
427 ((char-equal ?d (string-to-char (nth 9 x)))
428 (setcar x (concat (car x) "/")))
429 ((char-equal ?x (string-to-char (nth 9 x)))
430 (setcar x (concat (car x) "*"))))))
436 (when (not (zerop (length (nth 0 x))))
439 "%10s %3d %-8s %-8s %8s %s "
445 (if (tramp-time-less-p
446 (tramp-time-subtract (current-time) (nth 6 x))
451 ;; For the file name, we set the `dired-filename'
452 ;; property. This allows to handle file names with
453 ;; leading or trailing spaces as well.
455 (insert (format "%s" (nth 0 x))) ; file name
456 (put-text-property pos (point) 'dired-filename t))
459 (beginning-of-line)))
462 (defun tramp-imap-handle-insert-file-contents
463 (filename &optional visit beg end replace)
464 "Like `insert-file-contents' for Tramp files."
465 (barf-if-buffer-read-only)
467 (setq buffer-file-name (expand-file-name filename))
468 (set-visited-file-modtime)
469 (set-buffer-modified-p nil))
470 (with-parsed-tramp-file-name filename nil
471 (if (not (file-exists-p filename))
473 v 'file-error "File `%s' not found on remote host" filename)
474 (let ((point (point))
476 (tramp-message v 4 "Fetching file %s..." filename)
477 (insert (tramp-imap-get-file filename t))
478 (setq size (- (point) point))
479 ;;; TODO: handle ranges.
480 ;;; (let ((beg (or beg (point-min)))
481 ;;; (end (min (or end (point-max)) (point-max))))
482 ;;; (setq size (- end beg))
483 ;;; (buffer-substring beg end))
485 (tramp-message v 4 "Fetching file %s...done" filename)
486 (list (expand-file-name filename) size)))))
488 (defun tramp-imap-handle-file-exists-p (filename)
489 "Like `file-exists-p' for Tramp files."
490 (and (file-attributes filename) t))
492 (defun tramp-imap-handle-file-directory-p (filename)
493 "Like `file-directory-p' for Tramp-IMAP files."
494 ;; We allow only mailboxes to be a directory.
495 (with-parsed-tramp-file-name (expand-file-name filename default-directory) nil
496 (and (string-match "^/[^/]*$" (directory-file-name localname)) t)))
498 (defun tramp-imap-handle-file-attributes (filename &optional id-format)
499 "Like `file-attributes' for Tramp-IMAP FILENAME."
500 (with-parsed-tramp-file-name (expand-file-name filename) nil
501 (cdr-safe (nth 0 (tramp-imap-get-file-entries v localname)))))
503 (defun tramp-imap-get-file-inode (filename &optional id-format)
504 "Get inode equivalent \(actually the UID) for Tramp-IMAP FILENAME."
505 (nth 10 (tramp-compat-file-attributes filename id-format)))
507 (defun tramp-imap-handle-file-executable-p (filename)
508 "Like `file-executable-p' for Tramp files. False for IMAP."
511 (defun tramp-imap-handle-file-readable-p (filename)
512 "Like `file-readable-p' for Tramp files. True for IMAP."
513 (file-exists-p filename))
515 (defun tramp-imap-handle-file-writable-p (filename)
516 "Like `file-writable-p' for Tramp files. True for IMAP."
517 ;; `file-exists-p' does not work yet for directories.
518 ;; (file-exists-p (file-name-directory filename)))
519 (file-directory-p (file-name-directory filename)))
521 (defun tramp-imap-handle-delete-file (filename)
522 "Like `delete-file' for Tramp files."
524 ((not (file-exists-p filename)) nil)
525 (t (with-parsed-tramp-file-name (expand-file-name filename) nil
526 (let ((iht (tramp-imap-make-iht v)))
527 (imap-hash-rem (tramp-imap-get-file-inode filename) iht))))))
529 (defun tramp-imap-handle-directory-files-and-attributes
530 (directory &optional full match nosort id-format)
531 "Like `directory-files-and-attributes' for Tramp files."
534 (cons x (tramp-compat-file-attributes
535 (if full x (expand-file-name x directory)) id-format)))
536 (directory-files directory full match nosort)))
538 ;; TODO: fix this in tramp-imap-get-file-entries.
539 (defun tramp-imap-handle-file-newer-than-file-p (file1 file2)
540 "Like `file-newer-than-file-p' for Tramp files."
542 ((not (file-exists-p file1)) nil)
543 ((not (file-exists-p file2)) t)
544 (t (tramp-time-less-p (nth 5 (file-attributes file2))
545 (nth 5 (file-attributes file1))))))
547 (defun tramp-imap-handle-file-local-copy (filename)
548 "Like `file-local-copy' for Tramp files."
549 (with-parsed-tramp-file-name (expand-file-name filename) nil
550 (unless (file-exists-p filename)
553 "Cannot make local copy of non-existing file `%s'" filename))
554 (let ((tmpfile (tramp-compat-make-temp-file filename)))
555 (tramp-message v 4 "Fetching %s to tmp file %s..." filename tmpfile)
557 (insert-file-contents filename)
558 (write-region (point-min) (point-max) tmpfile)
559 (tramp-message v 4 "Fetching %s to tmp file %s...done" filename tmpfile)
562 (defun tramp-imap-put-file (vec filename-or-buffer &optional subject inode encode)
563 "Write contents of FILENAME-OR-BUFFER to Tramp-IMAP file VEC with name SUBJECT.
564 When INODE is given, delete that old remote file after writing the new one
565 \(normally this is the old file with the same name)."
566 ;; `tramp-current-host' is used in `tramp-imap-passphrase-callback-function'.
567 (let ((tramp-current-host (tramp-file-name-real-host vec))
568 (iht (tramp-imap-make-iht vec)))
574 tramp-imap-subject-marker
575 (or subject "no subject"))))
576 (cond ((bufferp filename-or-buffer)
577 (with-current-buffer filename-or-buffer
579 (tramp-imap-encode-buffer)
581 ;; TODO: allow file names.
582 (t "No body available")))
586 (defun tramp-imap-get-file (filename &optional decode)
587 ;; (debug (tramp-imap-get-file-inode filename))
588 (with-parsed-tramp-file-name (expand-file-name filename) nil
590 ;; `tramp-current-host' is used in
591 ;; `tramp-imap-passphrase-callback-function'.
592 (let* ((tramp-current-host (tramp-file-name-real-host v))
593 (iht (tramp-imap-make-iht v))
594 (inode (tramp-imap-get-file-inode filename))
595 (data (imap-hash-get inode iht t)))
598 (insert (nth 1 data))
599 ;;(debug inode (buffer-string))
600 (tramp-imap-decode-buffer))
603 v 'file-error "File `%s' could not be read" filename)))))
605 (defun tramp-imap-passphrase-callback-function (context key-id handback)
606 "Called by EPG to get a passphrase for Tramp-IMAP.
607 CONTEXT is the encryption/decryption EPG context.
608 HANDBACK is just carried through.
609 KEY-ID can be 'SYM or 'PIN among others."
610 (let* ((server tramp-current-host)
611 (port "tramp-imap") ; this is NOT the server password!
613 (auth-source-user-or-password "password" server port)))
615 (copy-sequence auth-passwd)
616 ;; If we cache the passphrase and we have one.
617 (if (and (eq tramp-imap-passphrase-cache t)
618 tramp-imap-passphrase)
620 (if (y-or-n-p "Reuse the passphrase? ")
621 (copy-sequence tramp-imap-passphrase)
622 ;; Don't reuse: revert caching behavior to nil, erase passphrase,
623 ;; call ourselves again.
624 (setq tramp-imap-passphrase-cache nil)
625 (setq tramp-imap-passphrase nil)
626 (tramp-imap-passphrase-callback-function context key-id handback))
627 (let ((p (if (eq key-id 'SYM)
629 "Tramp-IMAP passphrase for symmetric encryption: "
630 (eq (epg-context-operation context) 'encrypt)
631 tramp-imap-passphrase)
634 "Tramp-IMAP passphrase for PIN: "
635 (let ((entry (assoc key-id epg-user-id-alist)))
637 (format "Tramp-IMAP passphrase for %s %s: "
639 (format "Tramp-IMAP passphrase for %s: " key-id))))
641 tramp-imap-passphrase))))
643 ;; If we have an answer, the passphrase has changed,
644 ;; the user hasn't declined keeping the passphrase,
645 ;; and they answer yes to keep it now...
648 (not (equal tramp-imap-passphrase p))
649 (not (eq tramp-imap-passphrase-cache 'never))
650 (y-or-n-p "Keep the passphrase? "))
651 (setq tramp-imap-passphrase (copy-sequence p))
652 (setq tramp-imap-passphrase-cache t))
654 ;; If we still don't have a passphrase, the user didn't want
658 (not tramp-imap-passphrase))
659 (setq tramp-imap-passphrase-cache 'never))
663 (defun tramp-imap-encode-buffer ()
664 (let ((context (epg-make-context 'OpenPGP))
666 (epg-context-set-armor context t)
667 (epg-context-set-passphrase-callback context
668 #'tramp-imap-passphrase-callback-function)
669 (epg-context-set-progress-callback context
670 (cons #'epa-progress-callback-function
672 (message "Encrypting...")
673 (setq cipher (epg-encrypt-string
675 (encode-coding-string (buffer-string) 'utf-8)
677 (message "Encrypting...done")
680 (defun tramp-imap-decode-buffer ()
681 (let ((context (epg-make-context 'OpenPGP))
683 (epg-context-set-passphrase-callback context
684 #'tramp-imap-passphrase-callback-function)
685 (epg-context-set-progress-callback context
686 (cons #'epa-progress-callback-function
688 (message "Decrypting...")
689 (setq plain (decode-coding-string
690 (epg-decrypt-string context (buffer-string))
692 (message "Decrypting...done")
695 (defun tramp-imap-file-name-mailbox (vec)
696 (nth 0 (tramp-imap-file-name-parse vec)))
698 (defun tramp-imap-file-name-name (vec)
699 (nth 1 (tramp-imap-file-name-parse vec)))
701 (defun tramp-imap-file-name-localname (vec)
702 (nth 1 (tramp-imap-file-name-parse vec)))
704 (defun tramp-imap-file-name-parse (vec)
705 (let ((name (substring-no-properties (tramp-file-name-localname vec))))
706 (if (string-match "^/\\([^/]+\\)/?\\(.*\\)$" name)
707 (list (match-string 1 name)
708 (match-string 2 name))
711 (defun tramp-imap-make-iht (vec &optional needed-subject)
712 "Translate the Tramp vector VEC to the imap-hash structure.
713 With NEEDED-SUBJECT, alters the imap-hash test accordingly."
714 (let* ((mbox (tramp-imap-file-name-mailbox vec))
715 (server (tramp-file-name-real-host vec))
716 (method (tramp-file-name-method vec))
717 (user (tramp-file-name-user vec))
718 (ssl (string-equal method tramp-imaps-method))
719 (port (or (tramp-file-name-port vec)
720 (tramp-get-method-parameter method 'tramp-default-port)))
721 (result (imap-hash-make server port mbox)))
722 ;; Return the IHT with a test override to look for the subject
723 ;; marker. Set also user and ssl tags.
724 (setq result (plist-put result :user user)
725 result (plist-put result :ssl ssl)
728 :test (format "^%s%s"
729 tramp-imap-subject-marker
730 (if needed-subject needed-subject ""))))))
734 ;; * Implement `tramp-imap-handle-delete-directory',
735 ;; `tramp-imap-handle-make-directory',
736 ;; `tramp-imap-handle-make-directory-internal',
737 ;; `tramp-imap-handle-set-file-times'.
739 ;; * Encode the subject. If the filename has trailing spaces (like
740 ;; "test "), those characters get lost, for example in dired listings.
742 ;; * When opening a dired buffer, like "/imap::INBOX.test", there are
743 ;; several error messages:
744 ;; "Buffer has a running process; kill it? (yes or no) "
745 ;; "error in process filter: Internal error, tag 6 status BAD code nil text No mailbox selected."
746 ;; Afterwards, everything seems to be fine.
748 ;; * imaps works for local IMAP servers. Accessing
749 ;; "/imaps:imap.gmail.com:/INBOX.test/" results in error
750 ;; "error in process filter: Internal error, tag 5 status BAD code nil text UNSELECT not allowed now.
752 (provide 'tramp-imap)
753 ;;; tramp-imap.el ends here
755 ;; Ignore, for testing only.
757 ;;; (setq tramp-imap-subject-marker "T")
758 ;;; (tramp-imap-get-file-entries (tramp-dissect-file-name "/imap:yourhosthere.com:/INBOX.test/4") t)
759 ;;; (tramp-imap-get-file-entries (tramp-dissect-file-name "/imap:yourhosthere.com:/INBOX.test/") t)
760 ;;; (tramp-imap-get-file-entries (tramp-dissect-file-name "/imap:yourhosthere.com:/test/4") t)
761 ;;; (tramp-imap-get-file-entries (tramp-dissect-file-name "/imap:yourhosthere.com:/test/") t)
762 ;;; (tramp-imap-get-file-entries (tramp-dissect-file-name "/imap:yourhosthere.com:/test/welcommen") t)
763 ;;; (tramp-imap-get-file-entries (tramp-dissect-file-name "/imap:yourhosthere.com:/test/welcommen") t t)
764 ;;;(tramp-imap-get-file-inode "/imap:yourhosthere.com:/test/welcome")
765 ;;; (dired-copy-file "/etc/fstab" "/imap:yourhosthere.com:/test/welcome" t)
766 ;;; (write-region 1 100 "/imap:yourhosthere.com:/test/welcome")
767 ;;; (tramp-imap-get-file "/imap:yourhosthere.com:/test/welcome" t)
768 ;;(with-temp-buffer (insert "hello") (write-file "/imap:yourhosthere.com:/test/welcome"))
769 ;;(with-temp-buffer (insert "hello") (write-file "/imap:yourhosthere.com:/test/welcome2"))
770 ;;(file-writable-p "/imap:yourhosthere.com:/test/welcome2")
771 ;;(file-name-directory "/imap:yourhosthere.com:/test/welcome2")
772 ;;(with-temp-buffer (insert "hello") (delete-file "/tmp/hellotest") (write-file "/tmp/hellotest") (write-file "/imap:yourhosthere.com:/test/welcome2"))
773 ;;;(file-exists-p "/imap:yourhosthere.com:/INBOX.test/4")
774 ;;;(file-attributes "/imap:yourhosthere.com:/INBOX.test/4")
775 ;;;(setq vec (tramp-dissect-file-name "/imap:yourhosthere.com:/INBOX.test/4"))
776 ;;;(tramp-imap-handle-file-attributes "/imap:yourhosthere.com:/INBOX.test/4")
777 ;;; (tramp-imap-handle-insert-file-contents "/imap:user@yourhosthere.com:/INBOX.test/4" nil nil nil nil)
778 ;;;(insert-file-contents "/imap:yourhosthere.com:/INBOX.test/4")
779 ;;;(file-attributes "/imap:yourhosthere.com:/test/welcommen")
780 ;;;(insert-file-contents "/imap:yourhosthere.com:/test/welcome")
781 ;;;(file-exists-p "/imap:yourhosthere.com:/test/welcome2")
782 ;;;(tramp-imap-handle-file-attributes "/imap:yourhosthere.com:/test/welcome")
783 ;;;(tramp-imap-get-file-inode "/imap:yourhosthere.com:/test/welcommen")
784 ;;;(tramp-imap-get-file-inode "/imap:yourhosthere.com:/test/welcome")
785 ;;;(file-writable-p "/imap:yourhosthere.com:/test/welcome2")
786 ;;; (delete-file "/imap:yourhosthere.com:/test/welcome")
787 ;;; (tramp-imap-get-file "/imap:yourhosthere.com:/test/welcommen" t)
788 ;;; (tramp-imap-get-file "/imap:yourhosthere.com:/test/welcome" t)
789 ;;;(tramp-imap-file-name-mailbox (tramp-dissect-file-name "/imap:yourhosthere.com:/INBOX.test"))
790 ;;;(tramp-imap-file-name-mailbox (tramp-dissect-file-name "/imap:yourhosthere.com:/INBOX.test/new/old"))
791 ;;;(tramp-imap-file-name-mailbox (tramp-dissect-file-name "/imap:yourhosthere.com:/INBOX.test/new"))
792 ;;;(tramp-imap-file-name-parse (tramp-dissect-file-name "/imap:yourhosthere.com:/INBOX.test/new/two"))
793 ;;;(tramp-imap-file-name-parse (tramp-dissect-file-name "/imap:yourhosthere.com:/INBOX.test/new/one"))
794 ;;;(tramp-imap-file-name-parse (tramp-dissect-file-name "/imap:yourhosthere.com:/INBOX.test"))
795 ;;; (tramp-imap-file-name-parse (tramp-dissect-file-name "/imap:yourhosthere.com:/test/4"))
796 ;;; (tramp-imap-file-name-parse (tramp-dissect-file-name "/imap:yourhosthere.com:/test/"))
797 ;;; (tramp-imap-file-name-parse (tramp-dissect-file-name "/imap:yourhosthere.com:/test/welcommen"))
798 ;;; (tramp-imap-file-name-parse (tramp-dissect-file-name "/imap:yourhosthere.com:/test/welcommen"))
799 ;;; (tramp-imap-make-iht (tramp-dissect-file-name "/imap:yourhosthere.com:/test/welcommen"))
800 ;;; (tramp-imap-make-iht (tramp-dissect-file-name "/imap:yourhosthere.com:/INBOX.test/4"))
801 ;;; (tramp-imap-make-iht (tramp-dissect-file-name "/imap:yourhosthere.com:/INBOX.test/4") "extra")
803 ;; arch-tag: f2723749-58fb-4f29-894e-39708096e850