aboutsummaryrefslogtreecommitdiffstats
path: root/.offlineimap
diff options
context:
space:
mode:
authorWeitian LI <liweitianux@gmail.com>2015-01-19 10:21:08 +0800
committerWeitian LI <liweitianux@gmail.com>2015-01-19 10:21:08 +0800
commit0b08340aecac94e17356431894c9635d9bc958d2 (patch)
tree2dfd7f2b0a098b28fbbf2bda4d41e76475625c04 /.offlineimap
parent6a3885395e9bb793e22694fd1a210078cef69dfc (diff)
downloaddotfiles-0b08340aecac94e17356431894c9635d9bc958d2.tar.bz2
Updated various configs.
Diffstat (limited to '.offlineimap')
-rw-r--r--.offlineimap/offlineimap.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/.offlineimap/offlineimap.py b/.offlineimap/offlineimap.py
new file mode 100644
index 0000000..50f91e2
--- /dev/null
+++ b/.offlineimap/offlineimap.py
@@ -0,0 +1,63 @@
+#!/usr/bin/env python
+#
+# Ref:
+# Encrypt OfflineIMAP Password
+# http://unix.stackexchange.com/questions/44214/encrypt-offlineimap-password
+#
+# Configurations:
+# [general]
+# pythonfile = ~/.offlineimap/offlineimap.py
+# ...
+# [Repository <reponame>]
+# remotepasseval = mailpasswd("<accountname>")
+# ...
+#
+# 2014/06/20
+#
+
+import os
+import subprocess
+
+def mailpasswd(account):
+ account = os.path.basename(account)
+ path = '{0}/.offlineimap/{1}.gpg'.format(os.environ['HOME'], account)
+ args = ['gpg', '--use-agent', '--quiet', '--batch', '-d', path]
+ try:
+ return subprocess.check_output(args).strip()
+ except subprocess.CalledProcessError:
+ return ''
+
+# subprocess.check_output() only introduced in python 2.7
+# this version of 'mailpasswd' works with older version of python
+#def mailpasswd(account):
+# account = os.path.basename(account)
+# path = '{0}/.offlineimap/{1}.gpg'.format(os.environ['HOME'], account)
+# args = ['gpg', '--use-agent', '--quiet', '--batch', '-d', path]
+# proc = subprocess.Popen(args, stdout=subprocess.PIPE)
+# output = proc.communicate()[0].strip()
+# retcode = proc.wait()
+# if retcode == 0:
+# return output
+# else:
+# return ''
+
+
+# If you have several accounts that get checked simultaneously, and you
+# use 'gpg-agent', then it will ask for you passphrase for each account.
+# I prime the agent by creating a file, and priming the gpg-agent by
+# decrypting this file on launch of offlineimap.
+def prime_gpg_agent():
+ # echo "prime" | gpg -e -r <recipient> > ~/.offlineimap/prime.gpg
+ ret = False
+ i = 1
+ while not ret:
+ ret = (mailpasswd("prime") == "prime")
+ if i > 2:
+ from offlineimap.ui import getglobalui
+ sys.stderr.write("Error reading in passwords. Terminating.\n")
+ getglobalui().terminate()
+ i += 1
+ return ret
+
+prime_gpg_agent()
+