From c9db66f0f9e51e674107d52bb82e233bd1019316 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Balzarotti?= Date: Mon, 17 Sep 2018 13:14:53 +0200 Subject: [PATCH] repl easier to use; add YAML dependency --- Manifest.toml | 12 ++++++++++++ Project.toml | 1 + src/MatrixChat.jl | 1 + src/repl.jl | 44 +++++++++++++++++++++++++++++++++++++++----- 4 files changed, 53 insertions(+), 5 deletions(-) diff --git a/Manifest.toml b/Manifest.toml index 15582a2..8f33832 100644 --- a/Manifest.toml +++ b/Manifest.toml @@ -13,6 +13,12 @@ git-tree-sha1 = "b530fbeb6f41ab5a83fbe3db1fcbe879334bcd2d" uuid = "b99e7846-7c00-51b0-8f62-c81ae34c0232" version = "0.4.2" +[[Codecs]] +deps = ["Test"] +git-tree-sha1 = "70885e5e038cba1c4c17a84ad6c40756e10a4fb5" +uuid = "19ecbf4d-ef7c-5e4b-b54a-0a0ff23c5aed" +version = "0.5.0" + [[Compat]] deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"] git-tree-sha1 = "ae262fa91da6a74e8937add6b613f58cd56cdad4" @@ -156,3 +162,9 @@ uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" [[Unicode]] uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" + +[[YAML]] +deps = ["Codecs", "Compat", "Pkg"] +git-tree-sha1 = "3bde77cee95cce0c0b9b18813d85e18e8ed4f415" +uuid = "ddb6d928-2868-570f-bddf-ab3f9cf99eb6" +version = "0.3.2" diff --git a/Project.toml b/Project.toml index 762ab32..173fde7 100644 --- a/Project.toml +++ b/Project.toml @@ -12,3 +12,4 @@ Nettle = "49dea1ee-f6fa-5aa6-9a11-8816cee7d4b9" OhMyREPL = "5fb14364-9ced-5910-84b2-373655c76a03" REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" URIParser = "30578b45-9adc-5946-b283-645ec420af67" +YAML = "ddb6d928-2868-570f-bddf-ab3f9cf99eb6" diff --git a/src/MatrixChat.jl b/src/MatrixChat.jl index 13ea00e..10ef3bb 100644 --- a/src/MatrixChat.jl +++ b/src/MatrixChat.jl @@ -3,6 +3,7 @@ module MatrixChat using OhMyREPL using HTTP using JSON +using YAML using Nettle using Dates diff --git a/src/repl.jl b/src/repl.jl index e025bf2..c4c37aa 100644 --- a/src/repl.jl +++ b/src/repl.jl @@ -100,7 +100,6 @@ struct CommandSpec description::String help::Union{Nothing} end -command_specs = Dict{String,CommandSpec}() # TODO remove this ? function SuperSpecs(foo)::Dict{String,Dict{String,CommandSpec}} super_specs = Dict() @@ -747,8 +746,7 @@ function create_mode(repl, main) return matrix_mode end -function repl() - repl = Base.active_repl +function repl_init(repl) main_mode = repl.interface.modes[1] matrix_mode = create_mode(repl, main_mode) push!(repl.interface.modes, matrix_mode) @@ -768,6 +766,44 @@ function repl() return end +function load_config_file(; configfile::String = expanduser("~/matrix/config.yaml")) + if isfile(configfile) + config = YAML.load_file(configfile) + else + # First run, create a new user/login and create the config file + @error "First run not implemented!" + end + config +end + +function load_secret_or_login(config; secretfile = expanduser("~/matrix/secret.json")) + username = config["user"]["name"] + server = MatrixServer(config["user"]["server"]) + if !isfile(secretfile) + # login + @info "Logging in..." + # FIXME: Don't know how to use a SecretBuffer + password = Base.getpass("@$(username):$(URI(server.instance).host)") + response = MatrixChat.login(server, username, + join(map(x -> read(seek(password, x), Char), + (1:password.size).-1),"")) + write(secretfile, response.body |> String) + end + let sec = JSON.parse(open(secretfile, "r")) + @info "Session loaded from secret" + token = sec["access_token"] + deviceid = sec["device_id"] + end + (server, username, token, deviceid) +end + +"""Initialize the REPL mode, read config files and start the TUI client +""" +function repl() + load_secret_or_login(load_config_file()) + repl_init(Base.active_repl) +end + ######## # SPEC # ######## @@ -833,8 +869,6 @@ for (k, v) in pairs(super_specs) sort(map(wrap_option, collect(keys(spec.option_specs)))) end end -# TODO remove this -command_specs = super_specs["matrix"] const help = md"""