server: Add device deletion functionality.
This commit is contained in:
parent
35f47547d2
commit
b65346d49c
2 changed files with 43 additions and 13 deletions
|
@ -635,7 +635,7 @@ def matrix_devices_command_cb(data, buffer, args):
|
||||||
if not parsed_args.subcommand or parsed_args.subcommand == "list":
|
if not parsed_args.subcommand or parsed_args.subcommand == "list":
|
||||||
server.devices()
|
server.devices()
|
||||||
elif parsed_args.subcommand == "delete":
|
elif parsed_args.subcommand == "delete":
|
||||||
server.delete_device(parsed_args.delete_device)
|
server.delete_device(parsed_args.device_id)
|
||||||
|
|
||||||
return W.WEECHAT_RC_OK
|
return W.WEECHAT_RC_OK
|
||||||
|
|
||||||
|
|
|
@ -37,6 +37,8 @@ from nio import (
|
||||||
KeysQueryResponse,
|
KeysQueryResponse,
|
||||||
KeysClaimResponse,
|
KeysClaimResponse,
|
||||||
DevicesResponse,
|
DevicesResponse,
|
||||||
|
DeleteDevicesAuthResponse,
|
||||||
|
DeleteDevicesResponse,
|
||||||
TransportResponse,
|
TransportResponse,
|
||||||
TransportType,
|
TransportType,
|
||||||
RoomMessagesResponse,
|
RoomMessagesResponse,
|
||||||
|
@ -44,6 +46,9 @@ from nio import (
|
||||||
EncryptionError,
|
EncryptionError,
|
||||||
GroupEncryptionError,
|
GroupEncryptionError,
|
||||||
OlmTrustError,
|
OlmTrustError,
|
||||||
|
ErrorResponse,
|
||||||
|
SyncError,
|
||||||
|
LoginError,
|
||||||
)
|
)
|
||||||
|
|
||||||
from . import globals as G
|
from . import globals as G
|
||||||
|
@ -237,6 +242,8 @@ class MatrixServer(object):
|
||||||
self.send_buffer = b"" # type: bytes
|
self.send_buffer = b"" # type: bytes
|
||||||
self.device_check_timestamp = None # type: Optional[int]
|
self.device_check_timestamp = None # type: Optional[int]
|
||||||
|
|
||||||
|
self.device_deletion_queue = dict()
|
||||||
|
|
||||||
self.own_message_queue = dict() # type: Dict[str, OwnMessage]
|
self.own_message_queue = dict() # type: Dict[str, OwnMessage]
|
||||||
self.encryption_queue = defaultdict(deque) \
|
self.encryption_queue = defaultdict(deque) \
|
||||||
# type: DefaultDict[str, Deque[EncrytpionQueueItem]]
|
# type: DefaultDict[str, Deque[EncrytpionQueueItem]]
|
||||||
|
@ -587,8 +594,10 @@ class MatrixServer(object):
|
||||||
_, request = self.client.devices()
|
_, request = self.client.devices()
|
||||||
self.send_or_queue(request)
|
self.send_or_queue(request)
|
||||||
|
|
||||||
def delete_device(self, device_id):
|
def delete_device(self, device_id, auth=None):
|
||||||
# TODO implement this
|
uuid, request = self.client.delete_devices([device_id], auth)
|
||||||
|
self.device_deletion_queue[uuid] = device_id
|
||||||
|
self.send_or_queue(request)
|
||||||
return
|
return
|
||||||
|
|
||||||
def room_send_state(self, room_buffer, body, event_type):
|
def room_send_state(self, room_buffer, body, event_type):
|
||||||
|
@ -906,15 +915,30 @@ class MatrixServer(object):
|
||||||
W.bar_item_update("buffer_modes")
|
W.bar_item_update("buffer_modes")
|
||||||
W.bar_item_update("matrix_modes")
|
W.bar_item_update("matrix_modes")
|
||||||
|
|
||||||
def handle_transport_response(self, response):
|
def handle_delete_device_auth(self, response):
|
||||||
self.error(
|
device_id = self.device_deletion_queue.pop(response.uuid, None)
|
||||||
("Error with response of type type: {}, " "error code {}").format(
|
|
||||||
response.request_info.type, response.status_code
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
# TODO better error handling.
|
if not device_id:
|
||||||
if response.request_info.type in (RequestType.sync, RequestType.login):
|
return
|
||||||
|
|
||||||
|
for flow in response.flows:
|
||||||
|
if "m.login.password" in flow["stages"]:
|
||||||
|
session = response.session
|
||||||
|
auth = {
|
||||||
|
"type": "m.login.password",
|
||||||
|
"session": session,
|
||||||
|
"user": self.client.user_id,
|
||||||
|
"password": self.config.password
|
||||||
|
}
|
||||||
|
self.delete_device(device_id, auth)
|
||||||
|
return
|
||||||
|
|
||||||
|
self.error("No supported auth method for device deletion found.")
|
||||||
|
|
||||||
|
def handle_error_response(self, response):
|
||||||
|
self.error("Error: {}".format(str(response)))
|
||||||
|
|
||||||
|
if isinstance(response, (SyncError, LoginError)):
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
|
|
||||||
def handle_response(self, response):
|
def handle_response(self, response):
|
||||||
|
@ -930,8 +954,8 @@ class MatrixServer(object):
|
||||||
self.lag_done = True
|
self.lag_done = True
|
||||||
W.bar_item_update("lag")
|
W.bar_item_update("lag")
|
||||||
|
|
||||||
if isinstance(response, TransportResponse):
|
if isinstance(response, ErrorResponse):
|
||||||
self.handle_transport_response(response)
|
self.handle_error_response(response)
|
||||||
|
|
||||||
elif isinstance(response, LoginResponse):
|
elif isinstance(response, LoginResponse):
|
||||||
self._handle_login(response)
|
self._handle_login(response)
|
||||||
|
@ -948,6 +972,12 @@ class MatrixServer(object):
|
||||||
elif isinstance(response, DevicesResponse):
|
elif isinstance(response, DevicesResponse):
|
||||||
self.handle_devices_response(response)
|
self.handle_devices_response(response)
|
||||||
|
|
||||||
|
elif isinstance(response, DeleteDevicesAuthResponse):
|
||||||
|
self.handle_delete_device_auth(response)
|
||||||
|
|
||||||
|
elif isinstance(response, DeleteDevicesResponse):
|
||||||
|
self.info("Device successfully deleted")
|
||||||
|
|
||||||
elif isinstance(response, KeysQueryResponse):
|
elif isinstance(response, KeysQueryResponse):
|
||||||
self.keys_queried = False
|
self.keys_queried = False
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue