Skip to content

Instantly share code, notes, and snippets.

@toshiharu
Created April 6, 2013 08:28
Show Gist options
  • Select an option

  • Save toshiharu/5325395 to your computer and use it in GitHub Desktop.

Select an option

Save toshiharu/5325395 to your computer and use it in GitHub Desktop.
Wanderlust でマスターパスワード的なもの。前は alpaca.el を使って書いたのだけど、twittering-mode.el を参考にして EasyPG にした。 でもググってみるとこんな設定は必要ないような雰囲気を感じる。
(eval-after-load "elmo-util"
'(progn
(setq toshiharu-wl-use-master-passwd t)
(defun elmo-passwd-alist-load ()
(let* ((filename (expand-file-name elmo-passwd-alist-file-name
elmo-msgdb-directory))
(filename-gpg (concat filename ".gpg")))
(cond ((and toshiharu-wl-use-master-passwd
(require 'epa nil t)
(file-exists-p filename-gpg)
(file-readable-p filename-gpg))
;; 下は twittering-mode.el の twittering-read-from-encrypted-file から拝借した。
(let ((context (epg-make-context epa-protocol))
;; Bind `default-directory' to the temporary directory
;; because it is possible that the directory pointed by
;; `default-directory' has been already removed.
(default-directory temporary-file-directory))
(epg-context-set-passphrase-callback
context #'epa-passphrase-callback-function)
(epg-context-set-progress-callback
context
(cons #'epa-progress-callback-function
(format "Decrypting %s..." (file-name-nondirectory filename-gpg))))
(message "Decrypting %s..." (file-name-nondirectory filename-gpg))
(condition-case err
(let ((full-path (expand-file-name filename-gpg)))
;; `epg-decrypt-file' included in EasyPG 1.0.0, which is
;; distributed with Emacs 23.2, requires the expanded full path
;; as the argument CIPHER. This is because CIPHER is directly
;; used as an argument of the command `gpg'.
(car (read-from-string (epg-decrypt-file context full-path nil))))
(error
(message "%s" (cdr err))
nil))))
(t
;; ここは元々の elmo-passwd-alist-load のまま。
(with-temp-buffer
(let (insert-file-contents-pre-hook ; To avoid autoconv-xmas...
insert-file-contents-post-hook)
(insert-file-contents filename)
(goto-char (point-min))
(ignore-errors
(read (current-buffer)))))))))
(defun elmo-passwd-alist-save ()
(interactive)
(let* ((filename (expand-file-name elmo-passwd-alist-file-name
elmo-msgdb-directory))
(filename-gpg (concat filename ".gpg")))
(cond ((and toshiharu-wl-use-master-passwd
(require 'epa nil t)
(file-writable-p filename-gpg))
;; 下は twittering-mode.el の twittering-write-and-encrypt から拝借した。
(let ((context (epg-make-context epa-protocol))
;; Bind `default-directory' to the temporary directory
;; because it is possible that the directory pointed by
;; `default-directory' has been already removed.
(default-directory temporary-file-directory))
(epg-context-set-passphrase-callback
context #'epa-passphrase-callback-function)
(epg-context-set-progress-callback
context (cons #'epa-progress-callback-function "Encrypting..."))
(message "Encrypting...")
(condition-case err
(unwind-protect
;; In order to prevent `epa-file' to encrypt the file double,
;; `epa-file-name-regexp' is temorarily changed into the null
;; regexp that never matches any string.
(let ((epa-file-name-regexp "\\`\\'")
(coding-system-for-read 'binary)
(coding-system-for-write 'binary)
print-length print-level)
(when (fboundp 'epa-file-name-regexp-update)
(epa-file-name-regexp-update))
(with-temp-file filename-gpg
(set-buffer-multibyte nil)
(delete-region (point-min) (point-max))
(insert (epg-encrypt-string context (prin1-to-string elmo-passwd-alist) nil))
(set-file-modes filename-gpg 384)
(message "Encrypting...wrote %s" filename-gpg)
t))
(when (fboundp 'epa-file-name-regexp-update)
(epa-file-name-regexp-update)))
(error
(message "%s" (cdr err))
(message "%s" err)
nil))))
(t
;; ここは元々の elmo-passwd-alist-save のまま。
(with-temp-buffer
(let (print-length print-level)
(prin1 elmo-passwd-alist (current-buffer))
(princ "\n" (current-buffer))
(if (file-writable-p filename)
(progn
(write-region (point-min) (point-max)
filename nil 'no-msg)
(set-file-modes filename 384))
(message "%s is not writable." filename))))))))
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment