[PATCH v2] emacs: prefer notmuch-emacs-version in User-Agent: header

Subject: [PATCH v2] emacs: prefer notmuch-emacs-version in User-Agent: header

Date: Sat, 21 Mar 2015 12:15:55 +0200

To: notmuch@notmuchmail.org

Cc: tomi.ollila@iki.fi

From: Tomi Ollila


Now that we have `notmuch-emacs-version' defined in notmuch emacs MUA
use that as a part of User-Agent: header to provide more accurate
version information when sending emails.

In case some incomplete installation of notmuch emacs MUA is used and
`notmuch-emacs-version' is defined as "unknown" then fall back to ask
version info from cli (as it used to be) -- the function to do that was
renamed from `notmuch-version' to `notmuch-cli-version' to make this
clearer and more consistent.

The hub of getting version information is new `notmuch-versions'
function. It normally returns cons pair
(notmuch-emacs-version . notmuch-cli-version).
This function takes optional argument which makes the cdr of the return
value work differently; if `notmuch-emacs-version' is "unknown", return
pair as if the option were not given; otherwise return
(notmuch-emacs-version . notmuch-emacs-version).
This special case is useful to simplify setting User-Agent: header
-- just use the cdr of the returned value.
---

This is v2 of id:1407496781-17458-1-git-send-email-tomi.ollila@iki.fi

(NEWS dropped from this one (for now) -- will arrive later)

 emacs/notmuch-hello.el | 13 +++++--------
 emacs/notmuch-lib.el   | 20 +++++++++++++++++++-
 emacs/notmuch-mua.el   |  2 +-
 emacs/notmuch.el       |  4 ----
 4 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/emacs/notmuch-hello.el b/emacs/notmuch-hello.el
index 65d0627..62345fe 100644
--- a/emacs/notmuch-hello.el
+++ b/emacs/notmuch-hello.el
@@ -622,18 +622,15 @@ (defun notmuch-hello-window-configuration-change ()
       (remove-hook 'window-configuration-change-hook
 		   #'notmuch-hello-window-configuration-change))))
 
-;; the following variable is defined as being defconst in notmuch-version.el
-(defvar notmuch-emacs-version)
-
 (defun notmuch-hello-versions ()
   "Display the notmuch version(s)"
   (interactive)
-  (let ((notmuch-cli-version (notmuch-version)))
+  (let ((notmuch-versions (notmuch-versions)))
     (message "notmuch version %s"
-	     (if (string= notmuch-emacs-version notmuch-cli-version)
-		 notmuch-cli-version
-	       (concat notmuch-cli-version
-		       " (emacs mua version " notmuch-emacs-version ")")))))
+	     (if (string= (car notmuch-versions) (cdr notmuch-versions))
+		 (car notmuch-versions)
+	       (concat (cdr notmuch-versions)
+		       " (emacs mua version " (car notmuch-versions) ")")))))
 
 (defvar notmuch-hello-mode-map
   (let ((map (if (fboundp 'make-composed-keymap)
diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el
index f8e5165..370b648 100644
--- a/emacs/notmuch-lib.el
+++ b/emacs/notmuch-lib.el
@@ -25,6 +25,10 @@
 (require 'mm-decode)
 (require 'cl)
 
+(unless (require 'notmuch-version nil t)
+  (defconst notmuch-emacs-version "unknown"
+    "Placeholder variable when notmuch-version.el[c] is not available."))
+
 (autoload 'notmuch-jump-search "notmuch-jump"
   "Jump to a saved search by shortcut key." t)
 
@@ -192,7 +196,7 @@ (defun notmuch-assert-cli-sane ()
 "Perhaps you haven't run \"notmuch setup\" yet? Try running this
 on the command line, and then retry your notmuch command")))
 
-(defun notmuch-version ()
+(defun notmuch-cli-version ()
   "Return a string with the notmuch version number."
   (let ((long-string
 	 ;; Trim off the trailing newline.
@@ -202,6 +206,20 @@ (defun notmuch-version ()
 	(match-string 2 long-string)
       "unknown")))
 
+(defun notmuch-versions (&optional cdr-as-emacs-version-unless-unknown)
+  "Returns cons pair (notmuch-emacs-version . (`notmuch-cli-version'))
+If optional argument CDR-AS-EMACS-VERSION-UNLESS-UNKNOWN is non-nil
+and `notmuch-emacs-version' is not \"unknown\" return cons pair
+(notmuch-emacs-version . notmuch-emacs-version).
+`notmuch-mua-user-agent-notmuch' utilizes this option, and uses the cdr
+of this cons pair in User-Agent: header"
+  (interactive)
+  (cons notmuch-emacs-version
+	(if (or (not cdr-as-emacs-version-unless-unknown)
+		(string= notmuch-emacs-version "unknown"))
+	    (notmuch-cli-version)
+	  notmuch-emacs-version)))
+
 (defun notmuch-config-get (item)
   "Return a value from the notmuch configuration."
   (let* ((val (notmuch-command-to-string "config" "get" item))
diff --git a/emacs/notmuch-mua.el b/emacs/notmuch-mua.el
index 33f1399..ac067f2 100644
--- a/emacs/notmuch-mua.el
+++ b/emacs/notmuch-mua.el
@@ -118,7 +118,7 @@ (defun notmuch-mua-user-agent-full ()
 
 (defun notmuch-mua-user-agent-notmuch ()
   "Generate a `User-Agent:' string suitable for notmuch."
-  (concat "Notmuch/" (notmuch-version) " (http://notmuchmail.org)"))
+  (concat "Notmuch/" (cdr (notmuch-versions t)) " (http://notmuchmail.org)"))
 
 (defun notmuch-mua-user-agent-emacs ()
   "Generate a `User-Agent:' string suitable for notmuch."
diff --git a/emacs/notmuch.el b/emacs/notmuch.el
index ab00454..0cae117 100644
--- a/emacs/notmuch.el
+++ b/emacs/notmuch.el
@@ -61,10 +61,6 @@
 (require 'notmuch-message)
 (require 'notmuch-parser)
 
-(unless (require 'notmuch-version nil t)
-  (defconst notmuch-emacs-version "unknown"
-    "Placeholder variable when notmuch-version.el[c] is not available."))
-
 (defcustom notmuch-search-result-format
   `(("date" . "%12s ")
     ("count" . "%-7s ")
-- 
2.1.0


Thread: