sup

A curses threads-with-tags style email client

sup.git

git clone https://supmua.dev/git/sup/
commit 7c0e3b1549244ce23545d018dead9c5606fb7513
parent 36e8fbc5985b482669a597e23845f6b2343015f4
Author: Hamish Downer <dmishd@gmail.com>
Date:   Sun, 10 Oct 2010 17:18:41 +0100

Added a gpgkey option to the account settings

This allows the user to specify the gpg key used. In addition, if
gpgkey is not set, and there is only one email address defined, then
sup will not pass any id to gpg, so gpg will use its default key.
Only if gpgkey is not set and there are multiple email addresses
will sup use the old behaviour of defining the key to use by
passing gpg the from email address.

Diffstat:
M lib/sup.rb | 3 ++-
M lib/sup/account.rb | 5 +++--
M lib/sup/crypto.rb | 23 +++++++++++++++++++++--
3 files changed, 26 insertions(+), 5 deletions(-)
diff --git a/lib/sup.rb b/lib/sup.rb
@@ -261,7 +261,8 @@ EOS
             :email => email,
             :alternates => [],
             :sendmail => "/usr/sbin/sendmail -oem -ti",
-            :signature => File.join(ENV["HOME"], ".signature")
+            :signature => File.join(ENV["HOME"], ".signature"),
+            :gpgkey => ""
           }
         },
         :editor => ENV["EDITOR"] || "/usr/bin/vim -f -c 'setlocal spell spelllang=en_us' -c 'set filetype=mail'",
diff --git a/lib/sup/account.rb b/lib/sup/account.rb
@@ -1,7 +1,7 @@
 module Redwood
 
 class Account < Person
-  attr_accessor :sendmail, :signature
+  attr_accessor :sendmail, :signature, :gpgkey
 
   def initialize h
     raise ArgumentError, "no name for account" unless h[:name]
@@ -9,6 +9,7 @@ class Account < Person
     super h[:name], h[:email]
     @sendmail = h[:sendmail]
     @signature = h[:signature]
+    @gpgkey = h[:gpgkey]
   end
 
   # Default sendmail command for bouncing mail,
@@ -46,7 +47,7 @@ class AccountManager
   def add_account hash, default=false
     raise ArgumentError, "no email specified for account" unless hash[:email]
     unless default
-      [:name, :sendmail, :signature].each { |k| hash[k] ||= @default_account.send(k) }
+      [:name, :sendmail, :signature, :gpgkey].each { |k| hash[k] ||= @default_account.send(k) }
     end
     hash[:alternates] ||= []
 
diff --git a/lib/sup/crypto.rb b/lib/sup/crypto.rb
@@ -45,7 +45,8 @@ EOS
 
     sig_fn = Tempfile.new "redwood.signature"; sig_fn.close
 
-    message = run_gpg "--output #{sig_fn.path} --yes --armor --detach-sign --textmode --digest-algo sha256 --local-user '#{from}' #{payload_fn.path}", :interactive => true
+    sign_user_opts = gen_sign_user_opts from
+    message = run_gpg "--output #{sig_fn.path} --yes --armor --detach-sign --textmode --digest-algo sha256 #{sign_user_opts} #{payload_fn.path}", :interactive => true
     unless $?.success?
       info "Error while running gpg: #{message}"
       raise Error, "GPG command failed. See log for details."
@@ -68,7 +69,8 @@ EOS
     encrypted_fn = Tempfile.new "redwood.encrypted"; encrypted_fn.close
 
     recipient_opts = (to + [ from ] ).map { |r| "--recipient '<#{r}>'" }.join(" ")
-    sign_opts = sign ? "--sign --local-user '#{from}'" : ""
+    sign_opts = ""
+    sign_opts = "--sign --digest-algo sha256 " + gen_sign_user_opts(from) if sign
     message = run_gpg "--output #{encrypted_fn.path} --yes --armor --encrypt --textmode #{sign_opts} #{recipient_opts} #{payload_fn.path}", :interactive => true
     unless $?.success?
       info "Error while running gpg: #{message}"
@@ -208,6 +210,23 @@ private
     payload.to_s.gsub(/(^|[^\r])\n/, "\\1\r\n").gsub(/^MIME-Version: .*\r\n/, "")
   end
 
+  # logic is:
+  # if    gpgkey set for this account, then use that
+  # elsif only one account,            then leave blank so gpg default will be user
+  # else                                    set --local-user from_email_address
+  def gen_sign_user_opts from
+    account = AccountManager.account_for from
+    if !account.gpgkey.nil?
+      opts = "--local-user '#{account.gpgkey}'"
+    elsif AccountManager.user_emails.length == 1
+      # only one account
+      opts = ""
+    else
+      opts = "--local-user '#{from}'" 
+    end
+    opts
+  end
+
   def run_gpg args, opts={}
     args = HookManager.run("gpg-args", { :args => args }) || args
     cmd = "LC_MESSAGES=C #{@cmd} #{args}"