|
|
|
@ -144,15 +144,18 @@ function Room:_log(fmt, ...)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function Room:send_text(text)
|
|
|
|
|
return self.client._api:send_message(self.room_id, text)
|
|
|
|
|
-- XXX: How does error handling work here?
|
|
|
|
|
return self.client._api:send_message(self.room_id, text).event_id
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function Room:send_emote(text)
|
|
|
|
|
return self.client._api:send_emote(self.room_id, text)
|
|
|
|
|
-- XXX: How does error handling work here?
|
|
|
|
|
return self.client._api:send_emote(self.room_id, text).event_id
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function Room:send_notice(text)
|
|
|
|
|
return self.client._api:send_notice(self.room_id, text)
|
|
|
|
|
-- XXX: How does error handling work here?
|
|
|
|
|
return self.client._api:send_notice(self.room_id, text).event_id
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function Room:invite_user(user_id)
|
|
|
|
@ -320,43 +323,35 @@ Client.__name = "matrix.client"
|
|
|
|
|
Client.__index = Client
|
|
|
|
|
|
|
|
|
|
setmetatable(Client, { __call = function (self, base_url, token, http_client)
|
|
|
|
|
local c = eventable.object(setmetatable({
|
|
|
|
|
return eventable.object(setmetatable({
|
|
|
|
|
presence = {}, -- Indexed by user_id
|
|
|
|
|
rooms = {}, -- Indexed by room_id
|
|
|
|
|
_log = get_debug_log_function(),
|
|
|
|
|
_api = API(base_url, token, http_client),
|
|
|
|
|
}, Client))
|
|
|
|
|
-- Do an initial sync if a token was provided on construction.
|
|
|
|
|
if token then
|
|
|
|
|
c:_sync()
|
|
|
|
|
end
|
|
|
|
|
return c
|
|
|
|
|
end })
|
|
|
|
|
|
|
|
|
|
function Client:__tostring()
|
|
|
|
|
return self.__name .. "{" .. self._api.base_url .. "}"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function Client:register_with_password(username, password, no_sync)
|
|
|
|
|
function Client:register_with_password(username, password)
|
|
|
|
|
return self:_logged_in(self._api:register("m.login.password",
|
|
|
|
|
{ user = username, password = password }), no_sync)
|
|
|
|
|
{ user = username, password = password }))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function Client:login_with_password(username, password, no_sync)
|
|
|
|
|
function Client:login_with_password(username, password)
|
|
|
|
|
return self:_logged_in(self._api:login("m.login.password",
|
|
|
|
|
{ user = username, password = password }), no_sync)
|
|
|
|
|
{ user = username, password = password }))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function Client:_logged_in(response, no_sync)
|
|
|
|
|
function Client:_logged_in(response)
|
|
|
|
|
self._log("logged-in: %s", response.user_id)
|
|
|
|
|
self.user_id = response.user_id
|
|
|
|
|
self.homeserver = response.home_server
|
|
|
|
|
self.token = response.access_token
|
|
|
|
|
self._api.token = response.access_token
|
|
|
|
|
self:fire("logged-in")
|
|
|
|
|
if not no_sync then
|
|
|
|
|
self:_sync()
|
|
|
|
|
end
|
|
|
|
|
return self.token
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
@ -440,7 +435,9 @@ function Client:_sync(options)
|
|
|
|
|
if not options then
|
|
|
|
|
options = {}
|
|
|
|
|
end
|
|
|
|
|
options.since = self._sync_next_batch
|
|
|
|
|
if not options.since then
|
|
|
|
|
options.since = self._sync_next_batch
|
|
|
|
|
end
|
|
|
|
|
self._log("sync: Requesting with next_batch = %s", options.since)
|
|
|
|
|
|
|
|
|
|
local response = self._api:sync(options)
|
|
|
|
@ -470,9 +467,9 @@ function Client:sync(stop, timeout)
|
|
|
|
|
if not stop then
|
|
|
|
|
stop = return_false
|
|
|
|
|
end
|
|
|
|
|
while not stop(self) do
|
|
|
|
|
repeat
|
|
|
|
|
self:_sync { timeout = timeout or 15000 }
|
|
|
|
|
end
|
|
|
|
|
until stop(self)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function Client:_sync_handle_room__join(room_id, data)
|
|
|
|
|