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

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # This file is under GNU Affero General Public License 3.0
  4. # see LICENSE.txt
  5. """
  6. p≡p command line version 1.1
  7. Copyleft 2016-2019, p≡p foundation
  8. written by Volker Birk
  9. """
  10. from pEp import *
  11. import argparse
  12. import sys
  13. parser = argparse.ArgumentParser(
  14. description='provide p≡p functionality on the command line',
  15. epilog='Copyleft 2016, p≡p foundation. All lefts reserved.'
  16. )
  17. parser.add_argument('--encrypt', '-e', help='encrypt p≡p message',
  18. action='store_true', dest='encrypt')
  19. parser.add_argument('--decrypt', '-d', help='decrypt p≡p message',
  20. action='store_true', dest='decrypt')
  21. parser.add_argument('--import', '-i', help='import key data in PGP format',
  22. action='store_true', dest='keyimport')
  23. parser.add_argument('--export', '-x', help='export key data for identity address in PGP format',
  24. type=str, dest='keyexport')
  25. parser.add_argument('--secret-keys', help='option for --export',
  26. action='store_true', dest='secret_keys')
  27. parser.add_argument('--version', '-V', help='show program version',
  28. action='store_true', dest='version')
  29. parser.add_argument('files', metavar='FILE', nargs='*',
  30. help='FILE with MIME text')
  31. args = parser.parse_args()
  32. if not(args.encrypt) and not(args.decrypt) and not (args.keyimport) and not (args.keyexport) and not(args.version):
  33. print('use either --encrypt, --decrypt, --import or --export', file=sys.stderr)
  34. sys.exit(2)
  35. if args.version:
  36. print(__doc__)
  37. def crlf(line):
  38. if line[-2:] == '\r\n':
  39. return line
  40. elif line[-1:] == '\n':
  41. return line[:-1] + '\r\n'
  42. else:
  43. return line + '\r\n'
  44. try:
  45. if len(args.files) == 0:
  46. print("No files")
  47. args.files = ['-']
  48. for filename in args.files:
  49. if args.decrypt or args.encrypt:
  50. text = ''
  51. if filename == '-':
  52. for line in sys.stdin.readlines():
  53. text += line
  54. else:
  55. with open(filename, "r") as f:
  56. for line in f:
  57. text += line
  58. src = Message(text)
  59. if args.encrypt:
  60. src.dir = 1
  61. dst = src.encrypt()
  62. else:
  63. src.dir = 0
  64. dst, keys, rating, flags = src.decrypt()
  65. print(str(dst))
  66. elif args.keyimport:
  67. if filename == '-':
  68. print("specify Indentity address")
  69. text = sys.stdin.read()
  70. else:
  71. with open(filename, "rb") as f:
  72. text = f.read()
  73. try:
  74. import_key(text)
  75. except RuntimeError:
  76. print("error importing key(s)", file=sys.stderr)
  77. elif args.keyexport:
  78. id = Identity()
  79. id.address = args.keyexport
  80. id.update()
  81. try:
  82. if args.secret_keys:
  83. keydata = export_secret_key(id)
  84. else:
  85. keydata = export_key(id)
  86. if filename == '-':
  87. print(keydata)
  88. else:
  89. with open(filename, "w") as f:
  90. f.write(keydata)
  91. except RuntimeError:
  92. print("error exporting key(s)", file=sys.stderr)
  93. except KeyboardInterrupt:
  94. print('\n', file=sys.stderr)
  95. sys.exit(1)