server: Add the ability to send messages.
This commit is contained in:
parent
35e6978622
commit
6cb9be5578
2 changed files with 57 additions and 17 deletions
|
@ -50,6 +50,7 @@ OwnMessage = NamedTuple("OwnMessage", [
|
|||
("sender", str),
|
||||
("age", int),
|
||||
("event_id", str),
|
||||
("room_id", str),
|
||||
("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)
|
||||
|
||||
server.send_room_message(room_buffer.room, formatted_data)
|
||||
server.room_send_text(room_buffer, formatted_data)
|
||||
|
||||
return W.WEECHAT_RC_OK
|
||||
|
||||
|
@ -398,9 +399,9 @@ class WeechatChannelBuffer(object):
|
|||
# type: (str) -> None
|
||||
""" Print an error to the room buffer """
|
||||
message = "{prefix}{script}: {message}".format(
|
||||
W.prefix("error"),
|
||||
SCRIPT_NAME,
|
||||
string
|
||||
prefix=W.prefix("error"),
|
||||
script=SCRIPT_NAME,
|
||||
message=string
|
||||
)
|
||||
|
||||
self._print(message)
|
||||
|
|
|
@ -28,6 +28,7 @@ from nio import (
|
|||
HttpClient,
|
||||
LoginResponse,
|
||||
SyncRepsponse,
|
||||
RoomSendResponse,
|
||||
TransportResponse,
|
||||
LocalProtocolError
|
||||
)
|
||||
|
@ -78,6 +79,7 @@ class MatrixServer:
|
|||
self.proxy = None # type: str
|
||||
self.reconnect_delay = 0 # type: int
|
||||
self.reconnect_time = None # type: float
|
||||
self.sync_time = None # type: Optional[float]
|
||||
self.socket = None # type: ssl.SSLSocket
|
||||
self.ssl_context = ssl.create_default_context() # type: ssl.SSLContext
|
||||
|
||||
|
@ -101,6 +103,7 @@ class MatrixServer:
|
|||
self.device_check_timestamp = None
|
||||
|
||||
self.send_queue = deque()
|
||||
self.own_message_queue = dict() # type: Dict[OwnMessage]
|
||||
|
||||
self.event_queue_timer = None
|
||||
self.event_queue = deque() # type: Deque[RoomInfo]
|
||||
|
@ -410,19 +413,50 @@ class MatrixServer:
|
|||
|
||||
return True
|
||||
|
||||
def schedule_sync(self):
|
||||
self.sync_time = time.time()
|
||||
|
||||
def sync(self):
|
||||
request = self.client.sync()
|
||||
self.send_queue.append(request)
|
||||
self.sync_time = None
|
||||
_, request = self.client.sync()
|
||||
self.send_or_queue(request)
|
||||
|
||||
def login(self):
|
||||
# type: () -> None
|
||||
request = self.client.login(self.password)
|
||||
_, request = self.client.login(self.password)
|
||||
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)
|
||||
|
||||
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):
|
||||
server_buffer_prnt(self,
|
||||
("{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.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):
|
||||
room_buffer.self_action(message)
|
||||
return
|
||||
|
@ -510,6 +547,8 @@ class MatrixServer:
|
|||
self._handle_login(response)
|
||||
elif isinstance(response, SyncRepsponse):
|
||||
self._handle_sync(response)
|
||||
elif isinstance(response, RoomSendResponse):
|
||||
self.handle_own_messages(response)
|
||||
|
||||
return
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue