Skip to content

Commit cdf8bb8

Browse files
committed
Fix decrypt command of CLI
Re-raise OSErrors for image and file messages as MessageErrors Remove leftover debug print
1 parent d0fdc76 commit cdf8bb8

2 files changed

Lines changed: 21 additions & 15 deletions

File tree

threema-gateway

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@ def decrypt(private_key, public_key, nonce):
5454
private_key = util.read_key_or_key_file(private_key, Key.Type.private)
5555
public_key = util.read_key_or_key_file(public_key, Key.Type.public)
5656

57-
# Read message from stdin
58-
message = click.get_text_stream('stdin').read()
57+
# Convert nonce to bytes
58+
nonce = binascii.unhexlify(nonce)
5959

60-
# Convert keys to bytes
61-
private_key = binascii.unhexlify(private_key)
62-
public_key = binascii.unhexlify(public_key)
60+
# Read message from stdin and convert to bytes
61+
message = click.get_text_stream('stdin').read()
62+
message = binascii.unhexlify(message)
6363

6464
# TODO: Ensure that this is a text message
6565

6666
# Print text
67-
text_message = e2e.decrypt(private_key, public_key, nonce, message)
67+
text_message = e2e.decrypt(private_key.sk, public_key.pk, nonce, message)
6868
click.echo()
6969
click.echo(text_message)
7070

@@ -165,7 +165,6 @@ Prints the message ID on success.
165165
The public key of the recipient. Will be fetched automatically if not provided.
166166
""")
167167
def send_e2e(**arguments):
168-
print(arguments)
169168
# Get key instances
170169
private_key = util.read_key_or_key_file(arguments['private_key'], Key.Type.private)
171170
if arguments['public_key'] is not None:
@@ -234,7 +233,6 @@ def send_image(**arguments):
234233
)
235234

236235
# Send message
237-
click.echo()
238236
click.echo(message.send())
239237

240238

@@ -280,7 +278,6 @@ def send_file(**arguments):
280278
)
281279

282280
# Send message
283-
click.echo()
284281
click.echo(message.send())
285282

286283

threema/gateway/e2e.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,11 @@ def send(self):
507507

508508
# Read the content of the file if not already read
509509
if self.image is None:
510-
with open(self.image_path, mode='rb') as file:
511-
self.image = file.read()
510+
try:
511+
with open(self.image_path, mode='rb') as file:
512+
self.image = file.read()
513+
except OSError as exc:
514+
raise MessageError('Fetching content of image failed') from exc
512515

513516
# Encrypt and upload image
514517
image_nonce, image_data = self._pk_encrypt_raw(self.image)
@@ -595,8 +598,11 @@ def send(self):
595598

596599
# Read the content of the file if not already read
597600
if self.file_content is None:
598-
with open(self.file_path, mode='rb') as file:
599-
self.file_content = file.read()
601+
try:
602+
with open(self.file_path, mode='rb') as file:
603+
self.file_content = file.read()
604+
except OSError as exc:
605+
raise MessageError('Fetching content of file failed') from exc
600606

601607
# Create symmetric key
602608
key, hex_key = Key.generate_secret_key()
@@ -619,8 +625,11 @@ def send(self):
619625
if self.thumbnail_path is not None:
620626
# Read the content of the thumbnail file if not already read
621627
if self.thumbnail_content is None:
622-
with open(self.thumbnail_path, mode='rb') as file:
623-
self.thumbnail_content = file.read()
628+
try:
629+
with open(self.thumbnail_path, mode='rb') as file:
630+
self.thumbnail_content = file.read()
631+
except OSError as exc:
632+
raise MessageError('Fetching content of thumbnail failed') from exc
624633

625634
# Encrypt and upload thumbnail
626635
_, thumbnail_data = sk_encrypt_raw(key, self.thumbnail_content,

0 commit comments

Comments
 (0)