From 7b07ca9cb1f22e14d9cb3c0606f558f14e014347 Mon Sep 17 00:00:00 2001 From: Tim Schubert Date: Wed, 1 May 2019 19:41:51 +0200 Subject: [PATCH] Add initial encryption support. Still missing automatic device verification --- globals.d.ts | 9 +++++++++ inbox2matrix.ts | 43 ++++++++++++++++++++++++++----------------- package.json | 3 +++ tsconfig.json | 11 +++++++++++ 4 files changed, 49 insertions(+), 17 deletions(-) create mode 100644 globals.d.ts create mode 100644 tsconfig.json diff --git a/globals.d.ts b/globals.d.ts new file mode 100644 index 0000000..bee9c99 --- /dev/null +++ b/globals.d.ts @@ -0,0 +1,9 @@ +declare global { + namespace NodeJS { + interface Global { + Olm : any; + } + } +} + +export { }; diff --git a/inbox2matrix.ts b/inbox2matrix.ts index b6df2f5..9c8c59e 100644 --- a/inbox2matrix.ts +++ b/inbox2matrix.ts @@ -1,16 +1,25 @@ import Imap = require('imap'); import config = require('config'); -import matrix = require("matrix-js-sdk"); + +global.Olm = require('olm'); +import matrix = require('matrix-js-sdk'); let accounts = config.get('accounts'); -let myUserId = accounts.matrix.user; +import Storage = require('dom-storage'); + +let myUserId = accounts.matrix.user_id; let myRoom = accounts.matrix.room; +var localStorage = new Storage('./db.json', { strict: false, ws: ' ' }); +const webStorageSessionStore = new matrix.WebStorageSessionStore(localStorage); + let client = matrix.createClient({ - baseUrl: accounts.matrix.baseUrl, - accessToken: accounts.matrix.accessToken, - userId: myUserId + baseUrl: accounts.matrix.well_known.homeserver.base_url, + accessToken: accounts.matrix.access_token, + userId: myUserId, + sessionStore: webStorageSessionStore, + deviceId: accounts.matrix.device_id }); var imap = new Imap(accounts.imap); @@ -19,14 +28,6 @@ function openInbox(cb) { imap.openBox('INBOX', false, cb); } -client.on("RoomMember.membership", function(event, member) { - if (member.membership === "invite" && member.userId === myUserId) { - client.joinRoom(member.roomId).done(function() { - console.log("Auto-joined %s", member.roomId); - }); - } -}); - imap.once('ready', function() { openInbox(function(err, box) { if (err) throw err; @@ -105,10 +106,6 @@ imap.once('error', function(err) { imap.once('end', function() { console.log('Connection ended'); }); - -imap.connect(); - -client.startClient({initialSyncLimit: 200}); client.once('sync', function(state, prevState, res) { if(state === 'PREPARED') { @@ -118,4 +115,16 @@ client.once('sync', function(state, prevState, res) { } }); + +async function startBot() { + try { + await imap.connect(); + await client.initCrypto(); + await client.startClient({initialSyncLimit: 200}); + } catch (err) { + console.log(err) + } +} + +startBot(); setInterval(processUnseen, 5 * 60 * 1000); diff --git a/package.json b/package.json index b3bc277..7edc788 100644 --- a/package.json +++ b/package.json @@ -4,9 +4,12 @@ "description": "", "main": "inbox2matrix.js", "dependencies": { + "@types/node": "^11.13.8", "config": "^3.1.0", + "dom-storage": "^2.1.0", "imap": "^0.8.19", "matrix-js-sdk": "^1.1.0-rc.1", + "olm": "https://matrix.org/packages/npm/olm/olm-3.0.0.tgz", "process": "^0.11.10" }, "devDependencies": {}, diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..1420ebb --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "types" : [ + "node" + ] + }, + "files": [ + "inbox2matrix.ts", + "globals.d.ts" + ] +}