From 59f195c58f7db41af924dfdc50d18895b78a090e Mon Sep 17 00:00:00 2001 From: Claus-Justus Heine Date: Sat, 12 Feb 2022 17:46:21 +0100 Subject: [PATCH] Use signal-cli-dbus-rest-api over abandoned signal-web-gateway. See https://gitlab.com/morph027/signal-cli-dbus-rest-api. Signed-off-by: Claus-Justus Heine --- lib/Service/Gateway/Signal/Gateway.php | 48 ++++++++++++++++++-------- lib/Service/SetupService.php | 2 +- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/lib/Service/Gateway/Signal/Gateway.php b/lib/Service/Gateway/Signal/Gateway.php index b458517a..e1c64763 100644 --- a/lib/Service/Gateway/Signal/Gateway.php +++ b/lib/Service/Gateway/Signal/Gateway.php @@ -62,21 +62,41 @@ public function __construct(IClientService $clientService, */ public function send(IUser $user, string $identifier, string $message) { $client = $this->clientService->newClient(); - $response = $client->post( - $this->config->getUrl(), - [ - 'body' => [ - 'to' => $identifier, - 'message' => $message, - ], - ] - ); - $body = $response->getBody(); - $json = json_decode($body, true); + // determine type of gateway + $response = $client->get($this->config->getUrl() . '/v1/about'); + if ($response->getStatusCode() === 200) { + // New style gateway https://gitlab.com/morph027/signal-cli-dbus-rest-api + $response = $client->post( + $this->config->getUrl() . '/v1/send/' . $identifier, + [ + 'json' => [ 'message' => $message ], + ] + ); + $body = $response->getBody(); + $json = json_decode($body, true); + if ($response->getStatusCode() !== 201 || is_null($json) || !is_array($json) || !isset($json['timestamp'])) { + $status = $response->getStatusCode(); + throw new SmsTransmissionException("error reported by Signal gateway, status=$status, body=$body}"); + } + } else { + // Try old deprecated gateway https://gitlab.com/morph027/signal-web-gateway + $response = $client->post( + $this->config->getUrl() . '/v1/send/' . $identifier, + [ + 'body' => [ + 'to' => $identifier, + 'message' => $message, + ], + 'json' => [ 'message' => $message ], + ] + ); + $body = $response->getBody(); + $json = json_decode($body, true); - if ($response->getStatusCode() !== 200 || is_null($json) || !is_array($json) || !isset($json['success']) || $json['success'] !== true) { - $status = $response->getStatusCode(); - throw new SmsTransmissionException("error reported by Signal gateway, status=$status, body=$body}"); + if ($response->getStatusCode() !== 200 || is_null($json) || !is_array($json) || !isset($json['success']) || $json['success'] !== true) { + $status = $response->getStatusCode(); + throw new SmsTransmissionException("error reported by Signal gateway, status=$status, body=$body}"); + } } } diff --git a/lib/Service/SetupService.php b/lib/Service/SetupService.php index dd287a31..22adb81a 100644 --- a/lib/Service/SetupService.php +++ b/lib/Service/SetupService.php @@ -91,7 +91,7 @@ public function startSetup(IUser $user, string $gatewayName, string $identifier) try { $gateway->send($user, $identifier, "$verificationNumber is your Nextcloud verification code."); } catch (SmsTransmissionException $ex) { - throw new VerificationTransmissionException('could not send verification code'); + throw new VerificationTransmissionException('could not send verification code', $ex->getCode(), $ex); } return $this->stateStorage->persist(