riot-translatebot

A simple riot bot which provides Status about Weblate
git clone git://archive.git.mtrnord.blog/Nordgedanken/riot-translatebot.git
Log | Files | Refs | README | LICENSE

commit 8fa395a20fff23fa30bf72cbae3dcf43763798e1
parent e9140eab3fe6e988ed13f1cb18ae3bcfedf9dcd8
Author: MTRNord <mtrnord1@gmail.com>
Date:   Mon, 19 Jun 2017 20:00:52 +0200

Add Login Feature

Signed-off-by: MTRNord <mtrnord1@gmail.com>

Diffstat:
Mpackage.json | 3++-
Msrc/index.js | 10++++++++++
Asrc/login.js | 57+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 69 insertions(+), 1 deletion(-)

diff --git a/package.json b/package.json @@ -22,7 +22,8 @@ "dependencies": { "babel-runtime": "^6.23.0", "lodash": "^4.17.4", - "matrix-js-sdk": "^0.7.12" + "matrix-js-sdk": "^0.7.12", + "mz": "^2.6.0" }, "devDependencies": { "babel-cli": "^6.24.1", diff --git a/src/index.js b/src/index.js @@ -0,0 +1,10 @@ +import login from './login'; + +const new_login = new login; +new_login.getClient().then(client => { + console.log("client: " + client); + client.publicRooms(function(err, data) { + console.log("Public Rooms: %s", JSON.stringify(data)); + }); +}); + diff --git a/src/login.js b/src/login.js @@ -0,0 +1,56 @@ +import matrix_sdk from 'matrix-js-sdk'; +import fs from 'mz/fs'; +import node_fs from 'fs'; + +const env_args = process.argv.splice(process.execArgv.length + 2); + +export default class Client { + constructor() {}; + getClient = () => { + return this.login(); + }; + + login = async () => { + if (await fs.exists('user_data.json')){ + const json = node_fs.readFileSync('user_data.json'); + const valid_json = JSON.parse(json); + if (valid_json.hasOwnProperty("access_token")){ + return this.useToken(valid_json["access_token"], valid_json["user_id"], valid_json["home_server"]); + } else { + this.useEnv(); + } + } else { + this.useEnv(); + } + }; + + useEnv = () => { + const matrixClient = matrix_sdk.createClient({ + baseUrl: "https://matrix.org", + userId: env_args[0] + }); + + matrixClient.loginWithPassword(env_args[0], env_args[1]).done( data => { + console.log(data); + fs.writeFile('user_data.json', JSON.stringify(data), 'utf8') + .then(() => { + console.log("saved Access_token to file") + }) + .catch(err => { + console.error(err); + }); + }); + return this.login(); + }; + + useToken = (token, userId, baseUrl) => { + const matrixClient = matrix_sdk.createClient({ + baseUrl: "https://" + baseUrl, + userId: userId, + accessToken: token + }); + return new Promise(resolve => { + resolve(matrixClient); + }) + }; +} +\ No newline at end of file