Refactor of message handling function.
This commit is contained in:
parent
a59d72cd15
commit
6fdea4d213
1 changed files with 20 additions and 122 deletions
|
@ -23,63 +23,21 @@ import datetime
|
||||||
|
|
||||||
from matrix.globals import W
|
from matrix.globals import W
|
||||||
|
|
||||||
from matrix.api import MessageType
|
import matrix.api as API
|
||||||
|
|
||||||
from matrix.utils import (server_buffer_prnt, tags_from_line_data, prnt_debug,
|
from matrix.utils import server_buffer_prnt, prnt_debug
|
||||||
color_for_tags, add_user_to_nicklist,
|
|
||||||
get_prefix_for_level)
|
|
||||||
from matrix.plugin_options import DebugType
|
from matrix.plugin_options import DebugType
|
||||||
|
|
||||||
|
|
||||||
def matrix_handle_message(
|
def print_message_error(server, message):
|
||||||
server, # type: MatrixServer
|
server_buffer_prnt(server, ("{prefix}Unhandled {status_code} error, please "
|
||||||
message, # type: MatrixMessage
|
"inform the developers about this.").format(
|
||||||
):
|
prefix=W.prefix("error"),
|
||||||
# type: (...) -> None
|
status_code=message.response.status))
|
||||||
message_type = message.type
|
|
||||||
response = message.decoded_response
|
|
||||||
|
|
||||||
if message_type is MessageType.LOGIN:
|
server_buffer_prnt(server, pprint.pformat(message.__class__.__name__))
|
||||||
event = message.event
|
server_buffer_prnt(server, pprint.pformat(message.request.payload))
|
||||||
event.execute()
|
server_buffer_prnt(server, pprint.pformat(message.response.body))
|
||||||
|
|
||||||
elif message_type is MessageType.TOPIC:
|
|
||||||
event = message.event
|
|
||||||
event.execute()
|
|
||||||
|
|
||||||
elif message_type is MessageType.JOIN:
|
|
||||||
event = message.event
|
|
||||||
event.execute()
|
|
||||||
|
|
||||||
elif message_type is MessageType.PART:
|
|
||||||
event = message.event
|
|
||||||
event.execute()
|
|
||||||
|
|
||||||
elif message_type is MessageType.INVITE:
|
|
||||||
event = message.event
|
|
||||||
event.execute()
|
|
||||||
|
|
||||||
elif message_type is MessageType.SEND:
|
|
||||||
event = message.event
|
|
||||||
event.execute()
|
|
||||||
|
|
||||||
elif message_type == MessageType.REDACT:
|
|
||||||
event = message.event
|
|
||||||
event.execute()
|
|
||||||
|
|
||||||
elif message_type == MessageType.ROOM_MSG:
|
|
||||||
event = message.event
|
|
||||||
event.execute()
|
|
||||||
|
|
||||||
elif message_type is MessageType.SYNC:
|
|
||||||
event = message.event
|
|
||||||
event.execute()
|
|
||||||
|
|
||||||
else:
|
|
||||||
server_buffer_prnt(
|
|
||||||
server,
|
|
||||||
"Handling of message type {type} not implemented".format(
|
|
||||||
type=message_type))
|
|
||||||
|
|
||||||
|
|
||||||
def handle_http_response(server, message):
|
def handle_http_response(server, message):
|
||||||
|
@ -92,83 +50,23 @@ def handle_http_response(server, message):
|
||||||
ret, error = message.decode_body(server)
|
ret, error = message.decode_body(server)
|
||||||
|
|
||||||
if not ret:
|
if not ret:
|
||||||
# TODO try to resend the message if decoding has failed?
|
|
||||||
message = ("{prefix}matrix: Error decoding json response from "
|
message = ("{prefix}matrix: Error decoding json response from "
|
||||||
"server: {error}").format(
|
"server: {error}").format(
|
||||||
prefix=W.prefix("error"), error=error)
|
prefix=W.prefix("error"), error=error)
|
||||||
|
|
||||||
W.prnt(server.server_buffer, message)
|
W.prnt(server.server_buffer, message)
|
||||||
return
|
return
|
||||||
|
|
||||||
status_code = message.response.status
|
event = message.event
|
||||||
if status_code == 200:
|
event.execute()
|
||||||
matrix_handle_message(
|
|
||||||
server,
|
|
||||||
message,
|
|
||||||
)
|
|
||||||
|
|
||||||
# TODO handle try again response
|
|
||||||
elif status_code == 504:
|
|
||||||
if message.type == MessageType.SYNC:
|
|
||||||
server.sync()
|
|
||||||
|
|
||||||
elif status_code == 403:
|
|
||||||
if message.type == MessageType.LOGIN:
|
|
||||||
event = message.event
|
|
||||||
event.execute()
|
|
||||||
|
|
||||||
elif message.type == MessageType.TOPIC:
|
|
||||||
event = message.event
|
|
||||||
event.execute()
|
|
||||||
|
|
||||||
elif message.type == MessageType.REDACT:
|
|
||||||
event = message.event
|
|
||||||
event.execute()
|
|
||||||
|
|
||||||
elif message.type == MessageType.SEND:
|
|
||||||
event = message.event
|
|
||||||
event.execute()
|
|
||||||
|
|
||||||
elif message.type == MessageType.JOIN:
|
|
||||||
event = message.event
|
|
||||||
event.execute()
|
|
||||||
|
|
||||||
elif message.type == MessageType.PART:
|
|
||||||
event = message.event
|
|
||||||
event.execute()
|
|
||||||
|
|
||||||
elif message.type == MessageType.INVITE:
|
|
||||||
event = message.event
|
|
||||||
event.execute()
|
|
||||||
|
|
||||||
else:
|
|
||||||
error_message = ("{prefix}Unhandled 403 error, please inform the "
|
|
||||||
"developers about this: {error}").format(
|
|
||||||
prefix=W.prefix("error"),
|
|
||||||
error=message.response.body)
|
|
||||||
server_buffer_prnt(server, error_message)
|
|
||||||
|
|
||||||
elif status_code == 404:
|
|
||||||
if message.type == MessageType.JOIN:
|
|
||||||
event = message.event
|
|
||||||
event.execute()
|
|
||||||
|
|
||||||
else:
|
|
||||||
error_message = ("{prefix}Unhandled 404 error, please inform the "
|
|
||||||
"developers about this: {error}").format(
|
|
||||||
prefix=W.prefix("error"),
|
|
||||||
error=message.response.body)
|
|
||||||
server_buffer_prnt(server, error_message)
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
server_buffer_prnt(
|
status_code = message.response.status
|
||||||
server, ("{prefix}Unhandled {status_code} error, please inform "
|
if status_code == 504:
|
||||||
"the developers about this.").format(
|
if isinstance(message, API.MatrixSyncMessage):
|
||||||
prefix=W.prefix("error"), status_code=status_code))
|
server.sync()
|
||||||
|
else:
|
||||||
server_buffer_prnt(server, pprint.pformat(message.type))
|
print_message_error(server, message)
|
||||||
server_buffer_prnt(server, pprint.pformat(message.request.payload))
|
else:
|
||||||
server_buffer_prnt(server, pprint.pformat(message.response.body))
|
print_message_error(server, message)
|
||||||
|
|
||||||
creation_date = datetime.datetime.fromtimestamp(message.creation_time)
|
creation_date = datetime.datetime.fromtimestamp(message.creation_time)
|
||||||
done_time = time.time()
|
done_time = time.time()
|
||||||
|
|
Loading…
Add table
Reference in a new issue