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.

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