Skip to content
This repository was archived by the owner on Aug 15, 2018. It is now read-only.

contact delete and list added #78

Merged
merged 11 commits into from
Jul 5, 2018
9 changes: 4 additions & 5 deletions src/cmd/contact/delete.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ cmd_contact_delete() {
[[ $err != 0 ]] && fail "Usage:\n$(cmd_contact_delete_help)"
[[ -z $1 ]] && fail "Usage:\n$(cmd_contact_delete_help)"

if [[ $force == 0 ]]; then
gpg --delete-keys "$@"
else
gpg --batch --no-tty --yes --delete-keys "$@"
fi
call_gpg contact/delete.py $force "$@"

err=$?
[[ $err == 0 ]] || fail "Deleting contacts failed!"
}

#
Expand Down
14 changes: 4 additions & 10 deletions src/cmd/contact/list.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,10 @@ cmd_contact_list() {
return

# display the details of each key
local ids
ids=$(gpg --list-keys --with-colons "$@" | grep '^pub' | cut -d: -f5)
for id in $ids; do
echo
call_gpg fn/print_key.py $id

local err=$?
[[ $err == 0 ]] || fail "Error getting key $id"
echo
done
call_gpg contact/list.py "$@"

err=$?
[[ $err==0 ]] || fail "Retriving contacts failed"
}

#
Expand Down
1 change: 1 addition & 0 deletions src/egpg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ call_gpg() {
# User can override level by exporting GPGME_DEBUG
[[ -z "$GPGME_DEBUG" ]] && export GPGME_DEBUG=2
fi
export PYTHONPATH=$PYTHONPATH:"$LIBDIR/gpg/"
python3 "$pyfile" "$@"
}

Expand Down
Empty file added src/gpg/__init__.py
Empty file.
Empty file added src/gpg/contact/__init__.py
Empty file.
33 changes: 33 additions & 0 deletions src/gpg/contact/delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import gpg
import sys
import os
from fn.print_key import print_key


def delete(contacts, force):
try:
c = gpg.Context()
for contact in contacts:
keys = list(c.keylist(contact))
ans = "n"
for key in keys:
if not force:
print_key(key.fpr, end="\n")
try:
ans = input("Delete this contact? (y/N)")
except EOFError:
exit(0)

if ans.lower() == 'y' or force:
c.op_delete(key, False)

except BaseException:
if os.environ['DEBUG'] == 'yes':
raise
exit(2)


if __name__ == "__main__":
force = int(sys.argv[1])
contacts = sys.argv[2:]
delete(contacts, force)
37 changes: 37 additions & 0 deletions src/gpg/contact/list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import gpg
import sys
import os
from fn.print_key import print_key


def list_contacts(contacts):
try:
c = gpg.Context()

key_set = set()

for contact in contacts:
key_set.update(c.keylist(contact))

if len(list(key_set)) == 0:
print("No matching contacts found!")
exit(0)

for key in key_set:
print_key(key.fpr, end="\n")

except BaseException:
if os.environ["DEBUG"] == 'yes':
raise
exit(1)


if __name__ == "__main__":
contacts = [None]
if len(sys.argv) > 1:
contacts = sys.argv[1:]

if os.environ['DEBUG'] == 'yes':
print("contacts:", contacts, sep="\n")

list_contacts(contacts)
Empty file added src/gpg/fn/__init__.py
Empty file.
42 changes: 23 additions & 19 deletions src/gpg/fn/print_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import gpg


def print_key(identity):
def print_key(identity, end=""):
"""
print the details of key whose identitiy is passed
"""
Expand All @@ -24,12 +24,15 @@ def print_key(identity):
print(key, end='\n\n')

# uid
uid_list = ["uid: " + user_id.uid for user_id in key.uids]
all_uids = "\n".join(uid_list)
uid_list = ["uid: " + user_id.uid + "\n" for user_id in key.uids]
all_uids = "".join(uid_list)

# fpr
fpr = " ".join(textwrap.wrap(key.fpr, 4))

# keyid
keyid = key.fpr[-16:]

# trust
trust_map = {
"UNKNOWN": "UNKNOWN",
Expand All @@ -43,16 +46,17 @@ def print_key(identity):
trust = filter(lambda t: eval("gpg.constants.validity." + t) ==
key.owner_trust, trust_map.keys())
trust = trust_map[list(trust)[0]].lower()
trust = "trust: " + trust if trust != "unknown" else ""
trust = "trust: " + trust + "\n" if trust != "unknown" else ""

# keys
subkey_list = []
for subkey in key.subkeys:
start = time.strftime("%Y-%m-%d", time.localtime(subkey.timestamp))
starttime = time.strftime("%Y-%m-%d", time.localtime(subkey.timestamp))
endtime = time.strftime("%Y-%m-%d", time.localtime(subkey.expires))

# check if key never expires
endtime = time.localtime(subkey.expires)
end = time.strftime("%Y-%m-%d", endtime) if endtime != 0 else "never"
if subkey.expires == 0:
endtime = "never"

exp = "expired" if subkey.expired else ""

Expand All @@ -66,29 +70,29 @@ def print_key(identity):
subkey_map = {
"u": u,
"subkey_id": subkey.keyid,
"start": start,
"end": end,
"start": starttime,
"end": endtime,
"exp": exp
}

subkey_list.append("{u}: {subkey_id} {start} {end} {exp}"
.format_map(subkey_map))

subkeys = "\n".join(subkey_list)
subkeys = "\n".join(subkey_list) + "\n" if subkey_list else ""

# verifications
sign_list = []
sign_list = set({})
for uid in key.uids:
for sign in uid.signatures:
if sign.keyid != identity:
if (sign.keyid != keyid and sign.uid):
if not (sign.revoked or sign.expired):
sign_list.append("certified by: " +
sign.uid + " " + sign.keyid)
sign_list.add("certified by: " +
sign.uid + " " + sign.keyid)

signatures = "\n".join(sign_list)
signatures = "\n".join(sign_list) + "\n" if sign_list else ""

key_map = {
"identity": identity,
"identity": keyid,
"all_uids": all_uids,
"fpr": fpr,
"trust": trust,
Expand All @@ -97,12 +101,12 @@ def print_key(identity):
}

print("id: {identity}\n"
"{all_uids}\n"
"{all_uids}"
"fpr: {fpr}\n"
"{trust}\n"
"{trust}"
"{subkeys}"
"{signatures}"
.format_map(key_map))
.format_map(key_map), end=end)


if __name__ == "__main__":
Expand Down
Empty file added src/gpg/key/__init__.py
Empty file.