Re: Emacs: Crypto: How to get automatic encryption?
authorAntoine Beaupré <anarcat@anarcat.ath.cx>
Tue, 17 Jan 2012 12:48:36 +0000 (12:48 +0000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:42:22 +0000 (09:42 -0800)
6b/31a7eb71831c102ddf7f17391512c858e89f3f [new file with mode: 0644]

diff --git a/6b/31a7eb71831c102ddf7f17391512c858e89f3f b/6b/31a7eb71831c102ddf7f17391512c858e89f3f
new file mode 100644 (file)
index 0000000..b4cd9f9
--- /dev/null
@@ -0,0 +1,188 @@
+Return-Path: <anarcat@anarcat.ath.cx>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id DCFB8429E35\r
+       for <notmuch@notmuchmail.org>; Mon, 16 Jan 2012 20:48:35 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 1.274\r
+X-Spam-Level: *\r
+X-Spam-Status: No, score=1.274 tagged_above=-999 required=5\r
+       tests=[RDNS_NONE=1.274] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id fjBhRqWki5iv for <notmuch@notmuchmail.org>;\r
+       Mon, 16 Jan 2012 20:48:35 -0800 (PST)\r
+Received: from marcos.anarcat.ath.cx (unknown [72.0.72.144])\r
+       by olra.theworths.org (Postfix) with ESMTP id 0BB7B429E2F\r
+       for <notmuch@notmuchmail.org>; Mon, 16 Jan 2012 20:48:35 -0800 (PST)\r
+Received: by marcos.anarcat.ath.cx (Postfix, from userid 1000)\r
+       id 86B82143139; Mon, 16 Jan 2012 23:48:33 -0500 (EST)\r
+From: Antoine =?utf-8?Q?Beaupr=C3=A9?= <anarcat@anarcat.ath.cx>\r
+To: Gregor Zattler <telegraph@gmx.net>, David Edmondson <dme@dme.org>,\r
+       Jameson Graef Rollins <jrollins@finestructure.net>,\r
+       Gregor Zattler <telegraph@gmx.net>, Notmuch Mail <notmuch@notmuchmail.org>\r
+Subject: Re: Emacs: Crypto: How to get automatic encryption?\r
+In-Reply-To: <87d3akppoc.fsf@shi.workgroup>\r
+Message-ID: <87pqejj5nl.fsf@marcos.anarcat.ath.cx>\r
+MIME-Version: 1.0\r
+Content-Type: multipart/signed; boundary="==-=-=";\r
+       micalg=pgp-sha256; protocol="application/pgp-signature"\r
+X-Mailman-Approved-At: Thu, 02 Feb 2012 08:56:31 -0800\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+Date: Tue, 17 Jan 2012 04:48:36 -0000\r
+X-Original-Date: Mon, 16 Jan 2012 23:48:30 -0500\r
+X-List-Received-Date: Tue, 17 Jan 2012 04:48:36 -0000\r
+\r
+--==-=-=\r
+Content-Type: multipart/mixed; boundary="=-=-="\r
+\r
+--=-=-=\r
+Content-Type: text/plain; charset=utf-8\r
+Content-Transfer-Encoding: quoted-printable\r
+\r
+Jumping in here, I have modified the previously posted code here to\r
+provide me with a more complete solution.\r
+\r
+With the attach code, I can:\r
+\r
+ * automatically encrypt mails if all recipients have a *valid* public\r
+   key. The previous patch allowed encryption if a key existed but was\r
+   revoked, which cause a weird UX issue where the user would be\r
+   bothered with "No public key for..."\r
+\r
+ * not have specify if i want to encrypt the mail or not: it is\r
+   encrypted if possible\r
+\r
+ * try to autodetect (by running the function directly) if the mail will\r
+   be crypted and signed or just signed before sending\r
+\r
+ * explicitely request the mail to be encrypted or just signed, if I\r
+   want to, using the usual keybindings (ie. the existing #secure tags\r
+   are respected)\r
+\r
+So basically, this replaces the common hook:\r
+\r
+(add-hook 'message-setup-hook 'mml-secure-sign-pgpmime)\r
+\r
+with this:\r
+\r
+(add-hook 'message-send-hook 'anarcat/message-set-encryption)\r
+\r
+The rationale behind this technique is that the setup-hook runs when\r
+recipients are not yet defined so it will always set the mail to be only\r
+signed, even though your final recipients should be crypted.=20\r
+\r
+An alternative would be for notmuch to prompt the To: header before\r
+setting up the buffer ("=C3=A0 la" Mutt), but I didn't feel like going that\r
+way.\r
+\r
+Code is attached. Obviously, those function names would change if they\r
+would be to integrate into notmuch. ;)\r
+\r
+\r
+--=-=-=\r
+Content-Type: application/emacs-lisp\r
+Content-Disposition: inline; filename=notmuch-opportunistic.el\r
+Content-Transfer-Encoding: quoted-printable\r
+\r
+;; -*- mode: emacs-lisp; -*-\r
+\r
+(require 'notmuch)\r
+\r
+(add-hook 'message-send-hook 'anarcat/message-set-encryption)\r
+\r
+(defun anarcat/message-set-encryption (&optional force)\r
+  "Automatically set the encryption depending on the recipients.\r
+\r
+If ran interactively, will overwrite settings, otherwise it will\r
+not touch existing settings, so that this can safely be ran in\r
+`message-send-hook'."\r
+  (interactive "p")\r
+  (message-goto-body)\r
+  ;; clear existing settings\r
+  (if (and force (re-search-forward "<#secure [^>]*>\n" nil t))\r
+      (replace-match "" nil nil))\r
+  ;; If we can encrypt, do so, else just sign.\r
+  (if (or force (not (re-search-forward "<#secure [^>]*>\n" nil t)))\r
+      (if (anarcat/message-guess-encryption)\r
+          (insert "<#secure method=3Dpgpmime mode=3Dsignencrypt>\n")\r
+        (insert "<#secure method=3Dpgpmime mode=3Dsign>\n"))\r
+    )\r
+)\r
+\r
+;; http://notmuchmail.org/pipermail/notmuch/2012/007340.html\r
+(defun anarcat/message-guess-encryption ()\r
+  "Return `t' if we have gpg public keys for all recipients of this message=\r
+."\r
+  (require 'epa-mail)\r
+  (if (and (not (message-news-p))  ; No encryption for news,=20\r
+           (or (message-fetch-field "to") (message-fetch-field "cc"))) ; we=\r
+ have to have one address at least\r
+      (catch :exit\r
+        (mapc\r
+         (lambda (addr)\r
+           ;; check for a usable key - taken from epa-mail.el\r
+           (if (not (epa-mail--find-usable-key\r
+                     (epg-list-keys\r
+                      (epg-make-context epa-protocol)\r
+                      (downcase (mail-strip-quoted-names addr)))\r
+                     'encrypt))\r
+               (throw :exit nil)))\r
+         (message-tokenize-header (concat\r
+                                   (message-fetch-field "to")\r
+                                   ","\r
+                                   (message-fetch-field "cc"))))\r
+        t)\r
+    nil))\r
+\r
+--=-=-=\r
+Content-Transfer-Encoding: quoted-printable\r
+\r
+\r
+Opportunistic encryption, here we go.\r
+\r
+a.\r
+\r
+=2D-=20\r
+Evil exists to glorify the good. Evil is negative good.\r
+It is a relative term. Evil can be transmuted into good.\r
+What is evil to one at one time,\r
+becomes good at another time to somebody else.\r
+                        - Sivananda\r
+\r
+--=-=-=--\r
+\r
+--==-=-=\r
+Content-Type: application/pgp-signature\r
+\r
+-----BEGIN PGP SIGNATURE-----\r
+Version: GnuPG v1.4.11 (GNU/Linux)\r
+\r
+iQIcBAEBCAAGBQJPFP2eAAoJEHkhUlJ7dZIeZFgP/3O94zqpj/E7S0JHnugNJ8cj\r
+YKT3fdLZI8iy6Zw0SjxQE7RTF5ykT52gQXpojagOSxLQ1+N5LPhiHA+qWcrdBX8n\r
+VE+UtFUJbTaU7iARGQ/jBw7Ogb2AGM9uKWDuoV3oe28y3RzeeYCTD9xPWtn2RTXy\r
+aZd6d3bhbrx7ENG8TR7EBFj6wLfVqt0DlgWTPR5v8uAEHrP/BepNsWKVYOnWSteo\r
+qN0WQZ5uWMoAX73hOc/xYYwH8LIIZwAjJB29c/lWI8O5ZKG12noa1zqRQZNoFdj8\r
+7L6K/5ZKgEit0piedj69rbx7kxQrp9NJt60dl2h2xuWPP883lAIt+uHJwPnOUBe2\r
+ZBPU4piFTmn/4+tVzBK69VmogWRUWP/3KjJ2nfX8IhWfAJfvwXZCCctTHSOJ29Lj\r
+oImWFS4/5wxJB72RH0XUSRdr8Uq9iB+GHt2mckeiTfoAVgLObwDT2LjrBMl8JxTB\r
+8uRP0/GTfKuhH5HBf8nZ2iTfydX2c+hZ/LT+HG8GLc31lifW+9Wf1+ninQUnloYa\r
+BZDAiBGTqsv6oQ6Bpc23kUU6cxYc4R0xxOmjX+R/KGJE1w+i73SYCVNHrTQj+OOW\r
+blrLCkgVG+eJpDiPjF2mXw15DMLJauf7Kq5fUjubGQfrNY5JEcdW/Tp8gdlKJUYy\r
+DnRoI1rC/t/eJhGPD8OU\r
+=Bxam\r
+-----END PGP SIGNATURE-----\r
+--==-=-=--\r