|
| 1 | +/* |
| 2 | +Copyright 2020 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +// Usage: |
| 17 | +// node index.js -r -u "http://localhost:9000" # remember to add the registration! |
| 18 | +// node index.js -p 9000 |
| 19 | +import { Cli, Bridge, AppServiceRegistration, ClientEncryptionSession, ClientEncryptionStore, Logging} from 'matrix-appservice-bridge'; |
| 20 | + |
| 21 | +Logging.configure({ |
| 22 | + console: "debug", |
| 23 | +}); |
| 24 | +const log = Logging.get("index"); |
| 25 | + |
| 26 | +const encMap = new Map<string, ClientEncryptionSession>(); |
| 27 | +const encryptionStore: ClientEncryptionStore = { |
| 28 | + async getStoredSession(userId: string) { |
| 29 | + return encMap.get(userId) || null; |
| 30 | + }, |
| 31 | + async setStoredSession(session: ClientEncryptionSession) { |
| 32 | + log.info("Set session", session.userId, session.deviceId); |
| 33 | + encMap.set(session.userId, session); |
| 34 | + } |
| 35 | +}; |
| 36 | + |
| 37 | +new Cli({ |
| 38 | + registrationPath: "enc-registration.yaml", |
| 39 | + generateRegistration: function (reg, callback) { |
| 40 | + reg.setId(AppServiceRegistration.generateToken()); |
| 41 | + reg.setHomeserverToken(AppServiceRegistration.generateToken()); |
| 42 | + reg.setAppServiceToken(AppServiceRegistration.generateToken()); |
| 43 | + reg.setSenderLocalpart("encbot"); |
| 44 | + reg.addRegexPattern("users", "@enc_.*", true); |
| 45 | + callback(reg); |
| 46 | + }, |
| 47 | + run: function (port, config) { |
| 48 | + let bridge: Bridge; |
| 49 | + bridge = new Bridge({ |
| 50 | + homeserverUrl: "http://localhost:8008", |
| 51 | + domain: "halfyxps", |
| 52 | + registration: "enc-registration.yaml", |
| 53 | + bridgeEncryption: { |
| 54 | + homeserverUrl: "http://localhost:8009", |
| 55 | + store: encryptionStore, |
| 56 | + }, |
| 57 | + controller: { |
| 58 | + onUserQuery: function (queriedUser) { |
| 59 | + return {}; // auto-provision users with no additonal data |
| 60 | + }, |
| 61 | + |
| 62 | + onEvent: async function (request, context) { |
| 63 | + const event = request.getData(); |
| 64 | + const bot = bridge.getBot(); |
| 65 | + const intent = bridge.getIntentFromLocalpart(`enc_${context.senders.matrix.localpart}`); |
| 66 | + console.log(event, bot.getUserId()); |
| 67 | + if (event.type === "m.room.member" && |
| 68 | + event.content.membership === "invite" && |
| 69 | + event.state_key === "@encbot:halfyxps") { |
| 70 | + console.log("Joining the room!"); |
| 71 | + try { |
| 72 | + await intent.join(event.room_id); |
| 73 | + console.log("Joined the room!"); |
| 74 | + } catch (ex) { |
| 75 | + console.log("Err joining room:", ex); |
| 76 | + } |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + if (event.type === "m.room.encrypted") { |
| 81 | + await intent.sendText(event.room_id, "Not encrypted!"); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + if (event.type !== "m.room.message" || !event.content) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + await intent.sendText(event.room_id, event.content.body as string); |
| 90 | + } |
| 91 | + } |
| 92 | + }); |
| 93 | + log.info("Matrix-side listening on port %s", port); |
| 94 | + bridge.run(port, config); |
| 95 | + } |
| 96 | +}).run(); |
0 commit comments