p≡p for Python
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

116 lines
3.4 KiB

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# This file is under GNU Affero General Public License 3.0
# see LICENSE.txt
"""
p≡p command line version 1.1
Copyleft 2016-2019, p≡p foundation
written by Volker Birk
"""
from pEp import *
import argparse
import sys
parser = argparse.ArgumentParser(
description='provide p≡p functionality on the command line',
epilog='Copyleft 2016, p≡p foundation. All lefts reserved.'
)
parser.add_argument('--encrypt', '-e', help='encrypt p≡p message',
action='store_true', dest='encrypt')
parser.add_argument('--decrypt', '-d', help='decrypt p≡p message',
action='store_true', dest='decrypt')
parser.add_argument('--import', '-i', help='import key data in PGP format',
action='store_true', dest='keyimport')
parser.add_argument('--export', '-x', help='export key data for identity address in PGP format',
type=str, dest='keyexport')
parser.add_argument('--secret-keys', help='option for --export',
action='store_true', dest='secret_keys')
parser.add_argument('--version', '-V', help='show program version',
action='store_true', dest='version')
parser.add_argument('files', metavar='FILE', nargs='*',
help='FILE with MIME text')
args = parser.parse_args()
if not(args.encrypt) and not(args.decrypt) and not (args.keyimport) and not (args.keyexport) and not(args.version):
print('use either --encrypt, --decrypt, --import or --export', file=sys.stderr)
sys.exit(2)
if args.version:
print(__doc__)
def crlf(line):
if line[-2:] == '\r\n':
return line
elif line[-1:] == '\n':
return line[:-1] + '\r\n'
else:
return line + '\r\n'
try:
if len(args.files) == 0:
print("No files")
args.files = ['-']
for filename in args.files:
if args.decrypt or args.encrypt:
text = ''
if filename == '-':
for line in sys.stdin.readlines():
text += line
else:
with open(filename, "r") as f:
for line in f:
text += line
src = Message(text)
if args.encrypt:
src.dir = 1
dst = src.encrypt()
else:
src.dir = 0
dst, keys, rating, flags = src.decrypt()
print(str(dst))
elif args.keyimport:
if filename == '-':
print("specify Indentity address")
text = sys.stdin.read()
else:
with open(filename, "rb") as f:
text = f.read()
try:
import_key(text)
except RuntimeError:
print("error importing key(s)", file=sys.stderr)
elif args.keyexport:
id = Identity()
id.address = args.keyexport
id.update()
try:
if args.secret_keys:
keydata = export_secret_key(id)
else:
keydata = export_key(id)
if filename == '-':
print(keydata)
else:
with open(filename, "w") as f:
f.write(keydata)
except RuntimeError:
print("error exporting key(s)", file=sys.stderr)
except KeyboardInterrupt:
print('\n', file=sys.stderr)
sys.exit(1)