| 1 |
package pinentry |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"os" |
| 6 |
"os/exec" |
| 7 |
"strings" |
| 8 |
"sync/atomic" |
| 9 |
|
| 10 |
"git.sr.ht/~rjarry/aerc/config" |
| 11 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 12 |
"git.sr.ht/~rjarry/aerc/lib/ui" |
| 13 |
) |
| 14 |
|
| 15 |
var pinentryMode int32 = 0 |
| 16 |
|
| 17 |
func Enable() { |
| 18 |
if !config.General().UsePinentry { |
| 19 |
return |
| 20 |
} |
| 21 |
if atomic.SwapInt32(&pinentryMode, 1) == 1 { |
| 22 |
// cannot enter pinentry mode twice |
| 23 |
return |
| 24 |
} |
| 25 |
ui.SuspendScreen() |
| 26 |
} |
| 27 |
|
| 28 |
func Disable() { |
| 29 |
if atomic.SwapInt32(&pinentryMode, 0) == 0 { |
| 30 |
// not in pinentry mode |
| 31 |
return |
| 32 |
} |
| 33 |
ui.ResumeScreen() |
| 34 |
} |
| 35 |
|
| 36 |
func SetCmdEnv(cmd *exec.Cmd) { |
| 37 |
if cmd == nil || atomic.LoadInt32(&pinentryMode) == 0 { |
| 38 |
return |
| 39 |
} |
| 40 |
|
| 41 |
env := cmd.Env |
| 42 |
if env == nil { |
| 43 |
env = os.Environ() |
| 44 |
} |
| 45 |
|
| 46 |
hasTerm := false |
| 47 |
hasGPGTTY := false |
| 48 |
for _, e := range env { |
| 49 |
switch { |
| 50 |
case strings.HasPrefix(strings.ToUpper(e), "TERM="): |
| 51 |
log.Debugf("pinentry: use %v", e) |
| 52 |
hasTerm = true |
| 53 |
case strings.HasPrefix(strings.ToUpper(e), "GPG_TTY="): |
| 54 |
log.Debugf("pinentry: use %v", e) |
| 55 |
hasGPGTTY = true |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
if !hasTerm { |
| 60 |
env = append(env, "TERM=xterm-256color") |
| 61 |
log.Debugf("pinentry: set TERM=xterm-256color") |
| 62 |
} |
| 63 |
|
| 64 |
if !hasGPGTTY { |
| 65 |
tty := ttyname() |
| 66 |
env = append(env, fmt.Sprintf("GPG_TTY=%s", tty)) |
| 67 |
log.Debugf("pinentry: set GPG_TTY=%s", tty) |
| 68 |
} |
| 69 |
|
| 70 |
cmd.Env = env |
| 71 |
} |