Skip to content

Commit

Permalink
allow appservice to be configured
Browse files Browse the repository at this point in the history
  • Loading branch information
Gnuxie committed Nov 15, 2022
1 parent 8771ca9 commit 4c434c6
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 17 deletions.
34 changes: 17 additions & 17 deletions src/appservice/AppService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,28 @@
*/

import { randomUUID } from "crypto";
import { AppServiceRegistration, Bridge, Cli, Request, WeakEvent, BridgeContext, MatrixUser } from "matrix-appservice-bridge";
import { AppServiceRegistration, Bridge, Request, WeakEvent, BridgeContext, MatrixUser } from "matrix-appservice-bridge";
import { MatrixClient } from "matrix-bot-sdk";
// needed by appservice irc, though it looks completely dead.
import { MjolnirManager } from ".//MjolnirManager";
import { DataStore, PgDataStore } from ".//datastore";
import { Api } from "./Api";
// ts-node src/appservice/AppService.ts -r -u "http://localhost:9000" # remember to add the registration to homeserver.yaml! you probably want host.docker.internal as the hostname of the appservice if using mx-tester
// ts-node src/appservice/AppService -p 9000 # to start.
import { IConfig } from "./config/config";

// so one problem we have in regards to the ACL unit
// and the policy list is that we can't initialize those
// until the bridge has read the access tokens with init etc.
export class MjolnirAppService {

public readonly dataStore: DataStore;
public readonly bridge: Bridge;
public readonly mjolnirManager: MjolnirManager = new MjolnirManager();

public constructor() {
this.dataStore = new PgDataStore("postgres://mjolnir-tester:mjolnir-test@localhost:8082/mjolnir-test-db");
new Api("http://localhost:8081", this).start(9001);
public constructor(config: IConfig) {
this.dataStore = new PgDataStore(config.db.connectionString);
new Api(config.homeserver.url, this).start(config.webAPI.port);
this.bridge = new Bridge({
homeserverUrl: "http://localhost:8081",
domain: "localhost:9999",
homeserverUrl: config.homeserver.url,
domain: config.homeserver.domain,
registration: "mjolnir-registration.yaml",
controller: {
onUserQuery: this.onUserQuery.bind(this),
Expand All @@ -46,6 +47,7 @@ export class MjolnirAppService {
});
}

// FIXME: this needs moving the MjolnirManager.
public async init(): Promise<void> {
await this.dataStore.init();
for (var mjolnirRecord of await this.dataStore.list()) {
Expand Down Expand Up @@ -114,24 +116,22 @@ export class MjolnirAppService {
}
this.mjolnirManager.onEvent(request, context);
}
}

new Cli({
registrationPath: "mjolnir-registration.yaml",
generateRegistration: function(reg, callback) {
public static generateRegistration(reg: AppServiceRegistration, callback: (finalRegisration: AppServiceRegistration) => void) {
reg.setId(AppServiceRegistration.generateToken());
reg.setHomeserverToken(AppServiceRegistration.generateToken());
reg.setAppServiceToken(AppServiceRegistration.generateToken());
reg.setSenderLocalpart("mjolnir");
reg.addRegexPattern("users", "@mjolnir_.*", true);
reg.setRateLimited(false);
callback(reg);
},
run: async function(port: number) {
const service = new MjolnirAppService();
}

public static async run(port: number, config: IConfig) {
const service = new MjolnirAppService(config);
await service.bridge.initalise();
await service.init();
console.log("Matrix-side listening on port %s", port);
await service.bridge.listen(port);
}
}).run();
}
24 changes: 24 additions & 0 deletions src/appservice/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Cli } from "matrix-appservice-bridge";
import { MjolnirAppService } from "./AppService";
import { IConfig } from "./config/config";

// ts-node src/appservice/AppService.ts -r -u "http://host.docker.internal:9000"
// ts-node src/appservice/AppService -p 9000 -c your-confg.yaml # to start
const cli = new Cli({
registrationPath: "mjolnir-registration.yaml",
bridgeConfig: {
schema: {},
affectsRegistration: false,
defaults: {}
},
generateRegistration: MjolnirAppService.generateRegistration,
run: async function(port: number) {
const config: IConfig | null = cli.getConfig() as any;
if (config === null) {
throw new Error("Couldn't load config");
}
await MjolnirAppService.run(port, config);
}
});

cli.run();
13 changes: 13 additions & 0 deletions src/appservice/config/config.harness.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

homeserver:
domain: "localhost:9999"
url: http://localhost:8008

db:
engine: "postgres"
connectionString: "postgres://mjolnir-tester:mjolnir-test@localhost:8082/mjolnir-test-db"

access_control_list: "!aBcDeF:matrix.org"

webAPI:
port: 9001
29 changes: 29 additions & 0 deletions src/appservice/config/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright 2022 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

export interface IConfig {
homeserver: {
domain: string,
url: string
},
db: {
connectionString: string
},
webAPI: {
port: number
},
accessControlList: string,
}

0 comments on commit 4c434c6

Please sign in to comment.