Skip to content

Commit

Permalink
add initial LocationMessage support
Browse files Browse the repository at this point in the history
  • Loading branch information
pavarnos committed Aug 15, 2018
1 parent 0c527de commit b7da209
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

See README.md for info about version numbering. Follows https://keepachangelog.com/en/1.0.0/

## 2.2.3 - 2018-08-15
### Added
- unofficial support for LocationMessage: reverse engineered from the message format. Not documented by Threema

## 2.2.2 - 2018-08-15
### Changed
- Split calculateMac out of connection class to remove duplication
- phpstan fixes
- namespace fixes on sodium functions

## 2.2.1 - 2018-08-15
### Changed
Expand Down
13 changes: 11 additions & 2 deletions src/Threema/MsgApi/Encryptor/AbstractEncryptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Threema\MsgApi\Message\DeliveryReceipt;
use Threema\MsgApi\Message\FileMessage;
use Threema\MsgApi\Message\ImageMessage;
use Threema\MsgApi\Message\LocationMessage;
use Threema\MsgApi\Message\TextMessage;
use Threema\MsgApi\Response\UploadFileResponse;

Expand Down Expand Up @@ -172,7 +173,7 @@ final public function decryptMessage($box, $recipientPrivateKey, $senderPublicKe
case TextMessage::TYPE_CODE:
/* Text message */
if ($realDataLength < 2) {
throw new BadMessageException();
throw new BadMessageException('text message is too short');
}
return new TextMessage(substr($data, 1));

Expand Down Expand Up @@ -202,7 +203,7 @@ final public function decryptMessage($box, $recipientPrivateKey, $senderPublicKe
/* Image Message */
$values = json_decode(substr($data, 1), true);
if (empty($values)) {
throw new BadMessageException();
throw new BadMessageException('json badly formatted');
}

return new FileMessage(
Expand All @@ -212,6 +213,14 @@ final public function decryptMessage($box, $recipientPrivateKey, $senderPublicKe
$values['m'],
$values['n'],
$values['s']);

case LocationMessage::TYPE_CODE:
$points = explode(',',$this->bin2hex($data));
if (count($points) !== 3) {
throw new BadMessageException('invalid latitude and longitude');
}
return new LocationMessage($points[0], $points[1]);

default:
throw new UnsupportedMessageTypeException();
}
Expand Down
44 changes: 44 additions & 0 deletions src/Threema/MsgApi/Message/LocationMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* @author Threema GmbH
* @copyright Copyright (c) 2015-2016 Threema GmbH
*/

declare(strict_types=1);

namespace Threema\MsgApi\Message;

class LocationMessage extends AbstractMessage
{
const TYPE_CODE = 0x16;

/** @var string */
private $latitude;

/** @var string */
private $longitute;

public function __construct(string $latitude, string $longitute)
{
$this->latitude = $latitude;
$this->longitute = $longitute;
}

/**
* @return string
*/
public function __toString()
{
return 'Location: ' . $this->latitude . ',' . $this->longitute;
}

/**
* Get the message type code of this message.
*
* @return int message type code
*/
public function getTypeCode(): int
{
return self::TYPE_CODE;
}
}

0 comments on commit b7da209

Please sign in to comment.