diff --git a/main.py b/main.py index c0accf7..a1f7f20 100644 --- a/main.py +++ b/main.py @@ -249,7 +249,9 @@ def receive_cb(server_name, file_descriptor): prnt_debug(DebugType.MESSAGING, server, ("{prefix}Received message of type {t} and " "status {s}").format( - prefix=W.prefix("error"), t=message.type, s=status)) + prefix=W.prefix("error"), + t=message.__class__.__name__, + s=status)) # Message done, reset the parser state. server.reset_parser() diff --git a/matrix/api.py b/matrix/api.py index 071ee22..bf45f37 100644 --- a/matrix/api.py +++ b/matrix/api.py @@ -38,19 +38,6 @@ import matrix.events as MatrixEvents MATRIX_API_PATH = "/_matrix/client/r0" # type: str -@unique -class MessageType(Enum): - LOGIN = 0 - SYNC = 1 - SEND = 2 - TOPIC = 3 - REDACT = 4 - ROOM_MSG = 5 - JOIN = 6 - PART = 7 - INVITE = 8 - - class MatrixClient: def __init__( @@ -217,13 +204,11 @@ class MatrixMessage(): def __init__( self, - message_type, # type: MessageType request_func, # type: Callable[[...], HttpRequest] func_args, ): # type: (...) -> None # yapf: disable - self.type = message_type # type: MessageType self.request = None # type: HttpRequest self.response = None # type: HttpResponse @@ -268,7 +253,7 @@ class MatrixLoginMessage(MatrixMessage): if device_id: data["device_id"] = device_id - MatrixMessage.__init__(self, MessageType.LOGIN, client.login, data) + MatrixMessage.__init__(self, client.login, data) def decode_body(self, server): object_hook = partial(MatrixEvents.MatrixLoginEvent.from_dict, server) @@ -287,7 +272,7 @@ class MatrixSyncMessage(MatrixMessage): if limit: data["sync_filter"] = {"room": {"timeline": {"limit": limit}}} - MatrixMessage.__init__(self, MessageType.SYNC, client.sync, data) + MatrixMessage.__init__(self, client.sync, data) def decode_body(self, server): object_hook = partial(MatrixEvents.MatrixSyncEvent.from_dict, server) @@ -312,8 +297,7 @@ class MatrixSendMessage(MatrixMessage): if self.formatted_message.is_formatted: data["formatted_content"] = self.formatted_message.to_html() - MatrixMessage.__init__(self, MessageType.SEND, client.room_send_message, - data) + MatrixMessage.__init__(self, client.room_send_message, data) def decode_body(self, server): object_hook = partial( @@ -334,7 +318,7 @@ class MatrixTopicMessage(MatrixMessage): data = {"room_id": self.room_id, "topic": self.topic} - MatrixMessage.__init__(self, MessageType.TOPIC, client.room_topic, data) + MatrixMessage.__init__(self, client.room_topic, data) def decode_body(self, server): object_hook = partial( @@ -359,8 +343,7 @@ class MatrixRedactMessage(MatrixMessage): if reason: data["reason"] = reason - MatrixMessage.__init__(self, MessageType.REDACT, client.room_redact, - data) + MatrixMessage.__init__(self, client.room_redact, data) def decode_body(self, server): object_hook = partial( @@ -385,8 +368,7 @@ class MatrixBacklogMessage(MatrixMessage): "limit": limit } - MatrixMessage.__init__(self, MessageType.ROOM_MSG, - client.room_get_messages, data) + MatrixMessage.__init__(self, client.room_get_messages, data) def decode_body(self, server): object_hook = partial(MatrixEvents.MatrixBacklogEvent.from_dict, server, @@ -402,7 +384,7 @@ class MatrixJoinMessage(MatrixMessage): data = {"room_id": self.room_id} - MatrixMessage.__init__(self, MessageType.JOIN, client.room_join, data) + MatrixMessage.__init__(self, client.room_join, data) def decode_body(self, server): object_hook = partial(MatrixEvents.MatrixJoinEvent.from_dict, server, @@ -418,7 +400,7 @@ class MatrixPartMessage(MatrixMessage): data = {"room_id": self.room_id} - MatrixMessage.__init__(self, MessageType.PART, client.room_leave, data) + MatrixMessage.__init__(self, client.room_leave, data) def decode_body(self, server): object_hook = partial(MatrixEvents.MatrixPartEvent.from_dict, server, @@ -435,8 +417,7 @@ class MatrixInviteMessage(MatrixMessage): data = {"room_id": self.room_id, "user_id": self.user_id} - MatrixMessage.__init__(self, MessageType.INVITE, client.room_invite, - data) + MatrixMessage.__init__(self, client.room_invite, data) def decode_body(self, server): object_hook = partial(MatrixEvents.MatrixInviteEvent.from_dict, server, diff --git a/matrix/commands.py b/matrix/commands.py index 759d2bd..53fed48 100644 --- a/matrix/commands.py +++ b/matrix/commands.py @@ -24,7 +24,7 @@ import matrix.globals from matrix.globals import W, OPTIONS, SERVERS from matrix.utf import utf8_decode -from matrix.api import (MessageType, MatrixTopicMessage, MatrixRedactMessage, +from matrix.api import (MatrixTopicMessage, MatrixRedactMessage, MatrixBacklogMessage, MatrixJoinMessage, MatrixPartMessage, MatrixInviteMessage) from matrix.utils import key_from_value, tags_from_line_data diff --git a/matrix/server.py b/matrix/server.py index f35f329..ee41779 100644 --- a/matrix/server.py +++ b/matrix/server.py @@ -173,7 +173,8 @@ class MatrixServer: prnt_debug(DebugType.MESSAGING, self, ("{prefix} Failed sending message of type {t}. " "Adding to queue").format( - prefix=W.prefix("error"), t=message.type)) + prefix=W.prefix("error"), + t=message.__class__.__name__)) self.send_queue.append(message) def try_send(self, message): @@ -425,7 +426,7 @@ class MatrixServer: "\n Receive delay: {r} ms" "\n Handling time: {h} ms" "\n Total time: {total} ms").format( - t=message.type, + t=message.__class__.__name__, c=creation_date, s=(message.send_time - message.creation_time) * 1000, r=(message.receive_time - message.send_time) * 1000, @@ -522,7 +523,7 @@ def matrix_timer_cb(server_name, remaining_calls): prnt_debug( DebugType.MESSAGING, server, ("Timer hook found message of type {t} in queue. Sending " - "out.".format(t=message.type))) + "out.".format(t=message.__class__.__name__))) if not server.send(message): # We got an error while sending the last message return the message