server: Store the device id.
The device id now gets saved and reused for logins.
This commit is contained in:
parent
4fc5b8b41e
commit
d1869cd5d6
4 changed files with 50 additions and 4 deletions
7
main.py
7
main.py
|
@ -423,6 +423,7 @@ def room_close_cb(data, buffer):
|
||||||
@utf8_decode
|
@utf8_decode
|
||||||
def matrix_unload_cb():
|
def matrix_unload_cb():
|
||||||
matrix_config_free(matrix.globals.CONFIG)
|
matrix_config_free(matrix.globals.CONFIG)
|
||||||
|
W.prnt("", "unloading")
|
||||||
return W.WEECHAT_RC_OK
|
return W.WEECHAT_RC_OK
|
||||||
|
|
||||||
|
|
||||||
|
@ -437,8 +438,12 @@ if __name__ == "__main__":
|
||||||
WEECHAT_SCRIPT_VERSION, WEECHAT_SCRIPT_LICENSE,
|
WEECHAT_SCRIPT_VERSION, WEECHAT_SCRIPT_LICENSE,
|
||||||
WEECHAT_SCRIPT_DESCRIPTION, 'matrix_unload_cb', ''):
|
WEECHAT_SCRIPT_DESCRIPTION, 'matrix_unload_cb', ''):
|
||||||
|
|
||||||
# TODO if this fails we should abort and unload the script.
|
if not W.mkdir_home("matrix", 0o700):
|
||||||
|
message = ("{prefix}matrix: Error creating session "
|
||||||
|
"directory").format(prefix=W.prefix("error"))
|
||||||
|
W.prnt("", message)
|
||||||
|
|
||||||
|
# TODO if this fails we should abort and unload the script.
|
||||||
matrix.globals.CONFIG = W.config_new("matrix",
|
matrix.globals.CONFIG = W.config_new("matrix",
|
||||||
"matrix_config_reload_cb", "")
|
"matrix_config_reload_cb", "")
|
||||||
matrix_config_init(matrix.globals.CONFIG)
|
matrix_config_init(matrix.globals.CONFIG)
|
||||||
|
|
|
@ -58,7 +58,7 @@ class MatrixClient:
|
||||||
self.txn_id = self.txn_id + 1
|
self.txn_id = self.txn_id + 1
|
||||||
return txn_id
|
return txn_id
|
||||||
|
|
||||||
def login(self, user, password, device_name=""):
|
def login(self, user, password, device_name="", device_id=""):
|
||||||
# type (str, str, str) -> HttpRequest
|
# type (str, str, str) -> HttpRequest
|
||||||
path = ("{api}/login").format(api=MATRIX_API_PATH)
|
path = ("{api}/login").format(api=MATRIX_API_PATH)
|
||||||
|
|
||||||
|
@ -68,6 +68,9 @@ class MatrixClient:
|
||||||
"password": password
|
"password": password
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if device_id:
|
||||||
|
post_data["device_id"] = device_id
|
||||||
|
|
||||||
if device_name:
|
if device_name:
|
||||||
post_data["initial_device_display_name"] = device_name
|
post_data["initial_device_display_name"] = device_name
|
||||||
|
|
||||||
|
|
|
@ -68,15 +68,18 @@ class MatrixErrorEvent(MatrixEvent):
|
||||||
|
|
||||||
class MatrixLoginEvent(MatrixEvent):
|
class MatrixLoginEvent(MatrixEvent):
|
||||||
|
|
||||||
def __init__(self, server, user_id, access_token):
|
def __init__(self, server, user_id, device_id, access_token):
|
||||||
self.user_id = user_id
|
self.user_id = user_id
|
||||||
self.access_token = access_token
|
self.access_token = access_token
|
||||||
|
self.device_id = device_id
|
||||||
MatrixEvent.__init__(self, server)
|
MatrixEvent.__init__(self, server)
|
||||||
|
|
||||||
def execute(self):
|
def execute(self):
|
||||||
self.server.access_token = self.access_token
|
self.server.access_token = self.access_token
|
||||||
self.server.user_id = self.user_id
|
self.server.user_id = self.user_id
|
||||||
self.server.client.access_token = self.access_token
|
self.server.client.access_token = self.access_token
|
||||||
|
self.server.device_id = self.device_id
|
||||||
|
self.server.save_device_id()
|
||||||
|
|
||||||
message = "{prefix}matrix: Logged in as {user}".format(
|
message = "{prefix}matrix: Logged in as {user}".format(
|
||||||
prefix=W.prefix("network"), user=self.user_id)
|
prefix=W.prefix("network"), user=self.user_id)
|
||||||
|
@ -88,6 +91,7 @@ class MatrixLoginEvent(MatrixEvent):
|
||||||
def from_dict(cls, server, parsed_dict):
|
def from_dict(cls, server, parsed_dict):
|
||||||
try:
|
try:
|
||||||
return cls(server, sanitize_id(parsed_dict["user_id"]),
|
return cls(server, sanitize_id(parsed_dict["user_id"]),
|
||||||
|
sanitize_id(parsed_dict["device_id"]),
|
||||||
sanitize_token(parsed_dict["access_token"]))
|
sanitize_token(parsed_dict["access_token"]))
|
||||||
except (KeyError, TypeError, ValueError):
|
except (KeyError, TypeError, ValueError):
|
||||||
return MatrixErrorEvent.from_dict(server, "Error logging in", True,
|
return MatrixErrorEvent.from_dict(server, "Error logging in", True,
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
from builtins import str, bytes
|
from builtins import str, bytes
|
||||||
|
|
||||||
|
import os
|
||||||
import ssl
|
import ssl
|
||||||
import socket
|
import socket
|
||||||
import time
|
import time
|
||||||
|
@ -46,6 +47,7 @@ class MatrixServer:
|
||||||
self.port = 8448 # type: int
|
self.port = 8448 # type: int
|
||||||
self.options = dict() # type: Dict[str, weechat.config]
|
self.options = dict() # type: Dict[str, weechat.config]
|
||||||
self.device_name = "Weechat Matrix" # type: str
|
self.device_name = "Weechat Matrix" # type: str
|
||||||
|
self.device_id = "" # type: str
|
||||||
|
|
||||||
self.user = "" # type: str
|
self.user = "" # type: str
|
||||||
self.password = "" # type: str
|
self.password = "" # type: str
|
||||||
|
@ -89,8 +91,40 @@ class MatrixServer:
|
||||||
self.message_queue = deque() # type: Deque[MatrixMessage]
|
self.message_queue = deque() # type: Deque[MatrixMessage]
|
||||||
|
|
||||||
self._create_options(config_file)
|
self._create_options(config_file)
|
||||||
|
self._create_session_dir()
|
||||||
|
self._load_devide_id()
|
||||||
# yapf: enable
|
# yapf: enable
|
||||||
|
|
||||||
|
def _create_session_dir(self):
|
||||||
|
path = os.path.join("matrix", self.name)
|
||||||
|
if not W.mkdir_home(path, 0o700):
|
||||||
|
message = ("{prefix}matrix: Error creating server session "
|
||||||
|
"directory").format(prefix=W.prefix("error"))
|
||||||
|
W.prnt("", message)
|
||||||
|
|
||||||
|
def _get_session_path(self):
|
||||||
|
home_dir = W.info_get('weechat_dir', '')
|
||||||
|
return os.path.join(home_dir, "matrix", self.name)
|
||||||
|
|
||||||
|
def _load_devide_id(self):
|
||||||
|
file_name = "{}{}".format(self.name, ".device_id")
|
||||||
|
path = os.path.join(self._get_session_path(), file_name)
|
||||||
|
|
||||||
|
if not os.path.isfile(path):
|
||||||
|
return
|
||||||
|
|
||||||
|
with open(path, 'r') as f:
|
||||||
|
device_id = f.readline().rstrip()
|
||||||
|
if device_id:
|
||||||
|
self.device_id = device_id
|
||||||
|
|
||||||
|
def save_device_id(self):
|
||||||
|
file_name = "{}{}".format(self.name, ".device_id")
|
||||||
|
path = os.path.join(self._get_session_path(), file_name)
|
||||||
|
|
||||||
|
with open(path, 'w') as f:
|
||||||
|
f.write(self.device_id)
|
||||||
|
|
||||||
def _create_options(self, config_file):
|
def _create_options(self, config_file):
|
||||||
options = [
|
options = [
|
||||||
Option('autoconnect', 'boolean', '', 0, 0, 'off',
|
Option('autoconnect', 'boolean', '', 0, 0, 'off',
|
||||||
|
@ -374,7 +408,7 @@ class MatrixServer:
|
||||||
def login(self):
|
def login(self):
|
||||||
# type: (MatrixServer) -> None
|
# type: (MatrixServer) -> None
|
||||||
message = MatrixLoginMessage(self.client, self.user, self.password,
|
message = MatrixLoginMessage(self.client, self.user, self.password,
|
||||||
self.device_name)
|
self.device_name, self.device_id)
|
||||||
self.send_or_queue(message)
|
self.send_or_queue(message)
|
||||||
|
|
||||||
msg = "{prefix}matrix: Logging in...".format(prefix=W.prefix("network"))
|
msg = "{prefix}matrix: Logging in...".format(prefix=W.prefix("network"))
|
||||||
|
|
Loading…
Add table
Reference in a new issue