Re: [PATCH] emacs: functions to import sender or recipient into BBDB

Subject: Re: [PATCH] emacs: functions to import sender or recipient into BBDB

Date: Thu, 07 Feb 2013 13:51:07 -0500

To: Notmuch Mail List, Ethan Glasser-Camp

Cc: Chris Thachuk

From: Daniel Bergey

>From a show buffer, notmuch-bbdb/snarf-from imports the sender into
bbdb.  notmuch-bbdb/snarf-to imports all recipients.  Newly imported
concacts are reported in the minibuffer / Messages buffer.

Both functions use the BBDB parser to recognize email address formats.
Following discussion upthread, I put everything in notmuch-address.  I
renamed the functions to put notmuch before bbdb.  And I replaced the
horrid comma-splitting from my first version with the functions BBDB
provides for parsing email headers.

It looks as though Reply-To is not exposed in the :headers plist.  I
assume the Resent-* headers are not, either.  Therefore none of these
are imported (nil entries in notmuch-bbdb/header-by-name).  They would be
easy to add if the plist is expanded.

Thanks, and sorry it's taken me so long to get back to this patch.

 emacs/notmuch-address.el |   43 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

diff --git a/emacs/notmuch-address.el b/emacs/notmuch-address.el
index 2bf762b..8d5f727 100644
--- a/emacs/notmuch-address.el
+++ b/emacs/notmuch-address.el
@@ -98,4 +98,47 @@ line."
+;; functions to add sender / recipients to BBDB
+(defun notmuch-bbdb/snarf-headers (headers)
+  ;; Helper function to avoid code duplication in the two below
+  ;; headers should have the same format as bbdb-get-addresses-headers
+  ;; bbdb-get-addresses reads these
+  ;; Ugh, pass-by-global
+  (let ((addrs (bbdb-get-addresses nil nil 'notmuch-bbdb/get-header-content))
+        (bbdb-get-addresses-headers headers) ; headers to read
+        (bbdb-gag-messages t) ; suppress m/n processed message
+        )
+    (bbdb-update-records addrs t t)))
+(defun notmuch-bbdb/snarf-from ()
+  "Import the sender of the current message into BBDB"
+  (interactive)
+  (notmuch-bbdb/snarf-headers
+   (list  (assoc 'authors bbdb-get-addresses-headers))))
+(defun notmuch-bbdb/snarf-to ()
+  "Import all recipients of the current message into BBDB"
+  (interactive)
+  (notmuch-bbdb/snarf-headers
+   (list  (assoc 'recipients bbdb-get-addresses-headers))))
+(defvar notmuch-bbdb/header-by-name
+  ;; both are case sensitive
+  '( ("From" . :From)
+	("To" . :To)
+	("CC" . :Cc)
+	("BCC" . :Bcc)
+	("Resent-From" . nil)
+	("Reply-To" . nil)
+	("Resent-To" . nil)
+	("Resent-CC" . nil))
+  "Alist for dispatching header symbols as used by notmuch-show-get-header
+from strings as used by bbdb-get-addresses")
+(defun notmuch-bbdb/get-header-content (name)
+  (notmuch-show-get-header (cdr (assoc name notmuch-bbdb/header-by-name))))
 (provide 'notmuch-address)