|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Threema GmbH |
|
4
|
|
|
* @copyright Copyright (c) 2015-2016 Threema GmbH |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
namespace Threema\Console\Command; |
|
9
|
|
|
|
|
10
|
|
|
use Threema\Console\Common; |
|
11
|
|
|
use Threema\MsgApi\Connection; |
|
12
|
|
|
use Threema\MsgApi\ConnectionSettings; |
|
13
|
|
|
use Threema\MsgApi\Helpers\E2EHelper; |
|
14
|
|
|
use Threema\MsgApi\PublicKeyStore; |
|
15
|
|
|
|
|
16
|
|
|
class SendE2EText extends Base { |
|
17
|
|
|
/** |
|
18
|
|
|
* @var PublicKeyStore |
|
19
|
|
|
*/ |
|
20
|
|
|
private $publicKeyStore; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param PublicKeyStore $publicKeyStore |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(PublicKeyStore $publicKeyStore) { |
|
26
|
|
|
parent::__construct('Send End-to-End Encrypted Text Message', |
|
27
|
|
|
array(self::argThreemaId, self::argFrom, self::argSecret, self::argPrivateKey), |
|
28
|
|
|
'Encrypt standard input and send the text message to the given ID. \'from\' is the API identity and \'secret\' is the API secret. Prints the message ID on success.'); |
|
29
|
|
|
$this->publicKeyStore = $publicKeyStore; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
protected function doRun() { |
|
33
|
|
|
$threemaId = $this->getArgumentThreemaId(self::argThreemaId); |
|
34
|
|
|
$from = $this->getArgument(self::argFrom); |
|
35
|
|
|
$secret = $this->getArgument(self::argSecret); |
|
36
|
|
|
$privateKey = $this->getArgumentPrivateKey(self::argPrivateKey); |
|
37
|
|
|
|
|
38
|
|
|
Common::required($threemaId, $from, $secret, $privateKey); |
|
39
|
|
|
|
|
40
|
|
|
$message = $this->readStdIn(); |
|
41
|
|
|
if(strlen($message) === 0) { |
|
42
|
|
|
throw new \InvalidArgumentException('please define a message'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$settings = new ConnectionSettings( |
|
46
|
|
|
$from, |
|
47
|
|
|
$secret |
|
48
|
|
|
); |
|
49
|
|
|
|
|
50
|
|
|
$connector = new Connection($settings, $this->publicKeyStore); |
|
51
|
|
|
|
|
52
|
|
|
$helper = new E2EHelper($privateKey, $connector); |
|
53
|
|
|
$result = $helper->sendTextMessage($threemaId, $message); |
|
54
|
|
|
|
|
55
|
|
|
if($result->isSuccess()) { |
|
56
|
|
|
Common::l('Message ID: '.$result->getMessageId()); |
|
57
|
|
|
} |
|
58
|
|
|
else { |
|
59
|
|
|
Common::e('Error: '.$result->getErrorMessage()); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|