1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4from __future__ import absolute_import, division, unicode_literals
5
6import gpg
7import os.path
8import sys
9
10del absolute_import, division, unicode_literals
11
12# Copyright (C) 2018 Ben McGinnes <ben@gnupg.org>
13#
14# This program is free software; you can redistribute it and/or modify it under
15# the terms of the GNU General Public License as published by the Free Software
16# Foundation; either version 2 of the License, or (at your option) any later
17# version.
18#
19# This program is free software; you can redistribute it and/or modify it under
20# the terms of the GNU Lesser General Public License as published by the Free
21# Software Foundation; either version 2.1 of the License, or (at your option)
22# any later version.
23#
24# This program is distributed in the hope that it will be useful, but WITHOUT
25# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
26# FOR A PARTICULAR PURPOSE.  See the GNU General Public License and the GNU
27# Lesser General Public License for more details.
28#
29# You should have received a copy of the GNU General Public License and the GNU
30# Lesser General Public along with this program; if not, see
31# <https://www.gnu.org/licenses/>.
32
33print("""
34This script imports one or more public keys from a single file.
35""")
36
37c = gpg.Context(armor=True)
38
39if len(sys.argv) >= 3:
40    keyfile = sys.argv[1]
41    homedir = sys.argv[2]
42elif len(sys.argv) == 2:
43    keyfile = sys.argv[1]
44    homedir = input("Enter the GPG configuration directory path (optional): ")
45else:
46    keyfile = input("Enter the path and filename to import the key(s) from: ")
47    homedir = input("Enter the GPG configuration directory path (optional): ")
48
49if homedir.startswith("~"):
50    if os.path.exists(os.path.expanduser(homedir)) is True:
51        c.home_dir = os.path.expanduser(homedir)
52    else:
53        pass
54elif os.path.exists(homedir) is True:
55    c.home_dir = homedir
56else:
57    pass
58
59if os.path.isfile(keyfile) is True:
60    with open(keyfile, "rb") as f:
61        incoming = f.read()
62    result = c.key_import(incoming)
63else:
64    result = None
65
66if result is not None and hasattr(result, "considered") is False:
67    print(result)
68elif result is not None and hasattr(result, "considered") is True:
69    num_keys = len(result.imports)
70    new_revs = result.new_revocations
71    new_sigs = result.new_signatures
72    new_subs = result.new_sub_keys
73    new_uids = result.new_user_ids
74    new_scrt = result.secret_imported
75    nochange = result.unchanged
76    print("""
77The total number of keys considered for import was:  {0}
78
79   Number of keys revoked:  {1}
80 Number of new signatures:  {2}
81    Number of new subkeys:  {3}
82   Number of new user IDs:  {4}
83Number of new secret keys:  {5}
84 Number of unchanged keys:  {6}
85
86The key IDs for all considered keys were:
87""".format(num_keys, new_revs, new_sigs, new_subs, new_uids, new_scrt,
88           nochange))
89    for i in range(num_keys):
90        print(result.imports[i].fpr)
91    print("")
92elif result is None:
93    print("You must specify a key file to import.")
94