Skip to content

Commit

Permalink
catch ProofRequiredError, add submit_challenge command
Browse files Browse the repository at this point in the history
  • Loading branch information
maltee1 committed Aug 23, 2022
1 parent 56c96d4 commit bdf4351
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
3 changes: 3 additions & 0 deletions mausignald/signald.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,3 +510,6 @@ async def find_uuid(self, username: str, number: str) -> UUID | None:
"resolve_address", partial=Address(number=number).serialize(), account=username
)
return Address.deserialize(resp).uuid

async def submit_challenge(self, username: str, captcha_token: str, challenge: str) -> None:
await self.request_v1("submit_challenge", captcha_token=captcha_token, challenge=challenge)
38 changes: 38 additions & 0 deletions mautrix_signal/commands/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,41 @@ async def remove_linked_device(evt: CommandEvent) -> EventID:
except AuthorizationFailedError as e:
return await evt.reply(f"{e} Only the primary device can remove linked devices.")
return await evt.reply("Device removed")


@command_handler(
needs_auth=True,
management_only=True,
help_section=SECTION_AUTH,
help_text="submit a captcha challenge if you have been rate-limited",
)
async def submit_challenge(evt: CommandEvent) -> EventID:
if len(evt.args) == 0:
return await evt.reply("**Usage:** `$cmdprefix+sp submit_challenge <challenge>`")
challenge = evt.args[0]
evt.sender.command_status = {
"action": "SubmitChallenge",
"room_id": evt.room_id,
"username": evt.sender.username,
"next": enter_challenge_captcha,
"challenge": challenge,
}
await evt.reply(
"Please follow the instructions at https://signald.org/articles/captcha/ "
"to obtain a captcha token and paste it here."
)


async def enter_challenge_captcha(evt: CommandEvent) -> None:
captcha = evt.args[0]
username = evt.sender.command_status["username"]
challenge = evt.sender.command_status["challenge"]
try:
evt.bridge.signal.submit_challenge(username, captcha, challenge)
except UnexpectedResponse as e:
if e.resp_type == "error":
await evt.reply(e.data)
else:
raise
else:
await evt.reply("captcha challenge completed")
7 changes: 7 additions & 0 deletions mautrix_signal/portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
Mention,
MessageData,
Profile,
ProofRequiredError,
ProofRequiredType,
Quote,
QuotedAttachment,
Reaction,
Expand Down Expand Up @@ -331,6 +333,11 @@ async def handle_matrix_message(
status, event_id, self.mxid, EventType.ROOM_MESSAGE, message.msgtype, error=e
)
await sender.handle_auth_failure(e)
if isinstance(e, ProofRequiredError) and e.options == ProofRequiredType.RECAPTCHA:
await self.main_intent.send_notice(
"Your Message was not bridged because you have been rate limited by Signal."
"You can complete a captcha by sending `captcha_challenge {e.token}` to your management room"
)
await self._send_error_notice("message", e)
asyncio.create_task(self._send_message_status(event_id, e))

Expand Down

0 comments on commit bdf4351

Please sign in to comment.