1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4from __future__ import absolute_import, division, unicode_literals
5
6# Copyright (C) 2018 Ben McGinnes <ben@gnupg.org>
7#
8# This program is free software; you can redistribute it and/or modify it under
9# the terms of the GNU General Public License as published by the Free Software
10# Foundation; either version 2 of the License, or (at your option) any later
11# version.
12#
13# This program is free software; you can redistribute it and/or modify it under
14# the terms of the GNU Lesser General Public License as published by the Free
15# Software Foundation; either version 2.1 of the License, or (at your option)
16# any later version.
17#
18# This program is distributed in the hope that it will be useful, but WITHOUT
19# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20# FOR A PARTICULAR PURPOSE.  See the GNU General Public License and the GNU
21# Lesser General Public License for more details.
22#
23# You should have received a copy of the GNU General Public License and the GNU
24# Lesser General Public along with this program; if not, see
25# <https://www.gnu.org/licenses/>.
26
27import gpg
28import os.path
29
30print("""
31This script adds a new user ID to an existing key.
32
33The gpg-agent and pinentry are invoked to enter the passphrase.
34""")
35
36c = gpg.Context()
37
38homedir = input("Enter the GPG configuration directory path (optional): ")
39fpr0 = input("Enter the fingerprint of the key to modify: ")
40uid_name = input("Enter the name of the user ID: ")
41uid_email = input("Enter the email address of the user ID: ")
42uid_cmnt = input("Enter a comment to include (optional): ")
43
44if homedir.startswith("~"):
45    if os.path.exists(os.path.expanduser(homedir)) is True:
46        c.home_dir = os.path.expanduser(homedir)
47    else:
48        pass
49elif os.path.exists(homedir) is True:
50    c.home_dir = homedir
51else:
52    pass
53
54fpr = "".join(fpr0.split())
55
56if uid_cmnt:
57    userid = "{0} ({1}) <{2}>".format(uid_name, uid_cmnt, uid_email)
58else:
59    userid = "{0} <{2}>".format(uid_name, uid_email)
60
61key = c.get_key(fpr, secret=True)
62c.key_add_uid(key, userid)
63