Commit df2d4df7 authored by Jean-Benoist Leger's avatar Jean-Benoist Leger

gpg mutt hooks

parent 500e7fff
#!/usr/bin/python3
# Copyright (c) 2018, Jean-Benoist Leger <jb@leger.tf>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import subprocess
import argparse
import re
import os
def get_mails(gpg_exec, validity):
mail_extract = re.compile("^.*<([^<>]+)>.*")
so = subprocess.Popen(
[gpg_exec, "--no-auto-check-trustdb", "--list-keys", "--with-colons"],
stdout=subprocess.PIPE,
)
while True:
one_line = so.stdout.readline()
if one_line == b"":
break
splitted_line = one_line.split(b":")
if splitted_line[0] == b"uid":
val = splitted_line[1].decode("utf8")
if val in validity:
name = splitted_line[9].decode("utf8", errors="ignore")
match = mail_extract.match(name)
if match:
mail, = match.groups()
yield mail
def get_hooks(gpg_exec, validity):
yield 'send-hook . "reset pgp_autoencrypt"'
for mail in get_mails(gpg_exec, validity):
escaped_mail = mail.replace(".", r"\\.")
yield f'send-hook "!~l ~t {escaped_mail}" "set crypt_autoencrypt crypt_autosign"'
def main():
parser = argparse.ArgumentParser(
description="Generate mutt hook for gpg autoencrypt",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"-g", "--gpg", dest="gpg_exec", default="gpg2", help="Command to execute gpg"
)
parser.add_argument(
"-l",
"--validity-level",
dest="validity",
nargs=1,
type=str,
choices=("u", "f", "m"),
default="f",
help="minimal validity level",
)
parser.add_argument(
"-o",
"--output",
dest="output",
nargs=1,
type=str,
default="~/.mutt/gpg_autoencrypt_hooks",
help="Output file",
)
args = parser.parse_args()
validity = {"u": ("u",), "f": ("u", "f"), "m": ("u", "f", "m")}[args.validity]
with open(os.path.expanduser(args.output), "w") as fout:
for h in get_hooks(args.gpg_exec, validity):
print(h, file=fout)
if __name__ == "__main__":
main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment