server: Add the ability to send messages.

This commit is contained in:
Damir Jelić 2018-07-31 12:02:48 +02:00
parent 35e6978622
commit 6cb9be5578
2 changed files with 57 additions and 17 deletions

View file

@ -50,6 +50,7 @@ OwnMessage = NamedTuple("OwnMessage", [
("sender", str), ("sender", str),
("age", int), ("age", int),
("event_id", str), ("event_id", str),
("room_id", str),
("formatted_message", Formatted) ("formatted_message", Formatted)
]) ])
@ -73,7 +74,7 @@ def room_buffer_input_cb(server_name, buffer, input_data):
formatted_data = Formatted.from_input_line(input_data) formatted_data = Formatted.from_input_line(input_data)
server.send_room_message(room_buffer.room, formatted_data) server.room_send_text(room_buffer, formatted_data)
return W.WEECHAT_RC_OK return W.WEECHAT_RC_OK
@ -398,9 +399,9 @@ class WeechatChannelBuffer(object):
# type: (str) -> None # type: (str) -> None
""" Print an error to the room buffer """ """ Print an error to the room buffer """
message = "{prefix}{script}: {message}".format( message = "{prefix}{script}: {message}".format(
W.prefix("error"), prefix=W.prefix("error"),
SCRIPT_NAME, script=SCRIPT_NAME,
string message=string
) )
self._print(message) self._print(message)

View file

@ -28,6 +28,7 @@ from nio import (
HttpClient, HttpClient,
LoginResponse, LoginResponse,
SyncRepsponse, SyncRepsponse,
RoomSendResponse,
TransportResponse, TransportResponse,
LocalProtocolError LocalProtocolError
) )
@ -72,13 +73,14 @@ class MatrixServer:
self.timer_hook = None # type: weechat.hook self.timer_hook = None # type: weechat.hook
self.numeric_address = "" # type: str self.numeric_address = "" # type: str
self.autoconnect = False # type: bool self.autoconnect = False # type: bool
self.connected = False # type: bool self.connected = False # type: bool
self.connecting = False # type: bool self.connecting = False # type: bool
self.proxy = None # type: str self.proxy = None # type: str
self.reconnect_delay = 0 # type: int self.reconnect_delay = 0 # type: int
self.reconnect_time = None # type: float self.reconnect_time = None # type: float
self.socket = None # type: ssl.SSLSocket self.sync_time = None # type: Optional[float]
self.socket = None # type: ssl.SSLSocket
self.ssl_context = ssl.create_default_context() # type: ssl.SSLContext self.ssl_context = ssl.create_default_context() # type: ssl.SSLContext
# Enable http2 negotiation on the ssl context. # Enable http2 negotiation on the ssl context.
@ -101,6 +103,7 @@ class MatrixServer:
self.device_check_timestamp = None self.device_check_timestamp = None
self.send_queue = deque() self.send_queue = deque()
self.own_message_queue = dict() # type: Dict[OwnMessage]
self.event_queue_timer = None self.event_queue_timer = None
self.event_queue = deque() # type: Deque[RoomInfo] self.event_queue = deque() # type: Deque[RoomInfo]
@ -254,7 +257,7 @@ class MatrixServer:
server_buffer_prnt(self, error_message) server_buffer_prnt(self, error_message)
server_buffer_prnt( server_buffer_prnt(
self, ("{prefix}matrix: disconnecting from server..." self, ("{prefix}matrix: disconnecting from server..."
).format(prefix=W.prefix("network"))) ).format(prefix=W.prefix("network")))
self.disconnect() self.disconnect()
return False return False
@ -410,19 +413,50 @@ class MatrixServer:
return True return True
def schedule_sync(self):
self.sync_time = time.time()
def sync(self): def sync(self):
request = self.client.sync() self.sync_time = None
self.send_queue.append(request) _, request = self.client.sync()
self.send_or_queue(request)
def login(self): def login(self):
# type: () -> None # type: () -> None
request = self.client.login(self.password) _, request = self.client.login(self.password)
self.send_or_queue(request) self.send_or_queue(request)
msg = "{prefix}matrix: Logging in...".format(prefix=W.prefix("network")) msg = "{prefix}matrix: Logging in...".format(
prefix=W.prefix("network")
)
W.prnt(self.server_buffer, msg) W.prnt(self.server_buffer, msg)
def room_send_text(self, room_buffer, formatted):
# type: (RoomBuffer, Formatted) -> None
own_message = OwnMessage(
self.user_id,
0,
"",
room_buffer.room.room_id,
formatted
)
body = {"msgtype": "m.text", "body": formatted.to_plain()}
if formatted.is_formatted():
body["format"] = "org.matrix.custom.html"
body["formatted_body"] = formatted.to_html()
uuid, request = self.client.room_send(
room_buffer.room.room_id,
"m.room.message",
body
)
self.own_message_queue[uuid] = own_message
self.send_or_queue(request)
def _print_message_error(self, message): def _print_message_error(self, message):
server_buffer_prnt(self, server_buffer_prnt(self,
("{prefix}Unhandled {status_code} error, please " ("{prefix}Unhandled {status_code} error, please "
@ -434,7 +468,10 @@ class MatrixServer:
server_buffer_prnt(self, pprint.pformat(message.request.payload)) server_buffer_prnt(self, pprint.pformat(message.request.payload))
server_buffer_prnt(self, pprint.pformat(message.response.body)) server_buffer_prnt(self, pprint.pformat(message.response.body))
def handle_own_messages(self, room_buffer, message): def handle_own_messages(self, response):
message = self.own_message_queue.pop(response.uuid)
room_buffer = self.room_buffers[message.room_id]
if isinstance(message, OwnAction): if isinstance(message, OwnAction):
room_buffer.self_action(message) room_buffer.self_action(message)
return return
@ -510,6 +547,8 @@ class MatrixServer:
self._handle_login(response) self._handle_login(response)
elif isinstance(response, SyncRepsponse): elif isinstance(response, SyncRepsponse):
self._handle_sync(response) self._handle_sync(response)
elif isinstance(response, RoomSendResponse):
self.handle_own_messages(response)
return return