diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/crypto/crypto.go | 13 | ||||
-rw-r--r-- | lib/crypto/pgp/pgp.go | 15 |
2 files changed, 25 insertions, 3 deletions
diff --git a/lib/crypto/crypto.go b/lib/crypto/crypto.go index b7afe638..cb026696 100644 --- a/lib/crypto/crypto.go +++ b/lib/crypto/crypto.go @@ -6,6 +6,7 @@ import ( "git.sr.ht/~rjarry/aerc/lib/crypto/gpg" "git.sr.ht/~rjarry/aerc/lib/crypto/pgp" + "git.sr.ht/~rjarry/aerc/log" "git.sr.ht/~rjarry/aerc/models" "github.com/ProtonMail/go-crypto/openpgp" "github.com/emersion/go-message/mail" @@ -25,10 +26,20 @@ type Provider interface { func New(s string) Provider { switch s { + case "auto": + internal := &pgp.Mail{} + if internal.KeyringExists() { + log.Debugf("internal pgp keyring exists") + return internal + } + log.Debugf("no internal pgp keyring, using system gpg") + fallthrough case "gpg": return &gpg.Mail{} - default: + case "internal": return &pgp.Mail{} + default: + return nil } } diff --git a/lib/crypto/pgp/pgp.go b/lib/crypto/pgp/pgp.go index b2f5fa24..8d7c135a 100644 --- a/lib/crypto/pgp/pgp.go +++ b/lib/crypto/pgp/pgp.go @@ -28,6 +28,17 @@ var ( locked bool ) +func (m *Mail) KeyringExists() bool { + keypath := path.Join(xdg.DataHome(), "aerc", "keyring.asc") + keyfile, err := os.Open(keypath) + if err != nil { + return false + } + defer keyfile.Close() + _, err = openpgp.ReadKeyRing(keyfile) + return err == nil +} + func (m *Mail) Init() error { log.Debugf("Initializing PGP keyring") err := os.MkdirAll(path.Join(xdg.DataHome(), "aerc"), 0o700) @@ -50,13 +61,13 @@ func (m *Mail) Init() error { if os.IsNotExist(err) { return nil } else if err != nil { - panic(err) + return err } defer keyfile.Close() Keyring, err = openpgp.ReadKeyRing(keyfile) if err != nil { - panic(err) + return err } return nil } |