Skip to content

Commit

Permalink
Absolute bare minimum for a bot
Browse files Browse the repository at this point in the history
  • Loading branch information
turt2live committed Sep 26, 2019
1 parent 49be5b7 commit ed6f37b
Show file tree
Hide file tree
Showing 11 changed files with 1,499 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.git
node_modules
config
lib
logs
storage
db
*.db
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
config/*
!config/default.yaml
lib/
storage/

/.idea

/db

# Logs
logs
*.log
Expand Down
15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM node:alpine
COPY . /tmp/src
RUN cd /tmp/src \
&& npm install \
&& npm run build \
&& mv lib/ /mjolnir/ \
&& mv node_modules / \
&& cd / \
&& rm -rf /tmp/*

ENV NODE_ENV=production
ENV NODE_CONFIG_DIR=/data/config

CMD node /mjolnir/index.js
VOLUME ["/data"]
59 changes: 58 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,59 @@
# mjolnir
A moderation tool for Matrix

A moderation tool for Matrix.

## Features

TODO: Describe what all this means.

Phase 1:
* [ ] Ban users
* [ ] ACL servers
* [ ] Update lists with new bans/ACLs
* [ ] "Ban on sight" mode (rather than proactive)

Phase 2:
* [ ] Synapse antispam module
* [ ] Riot hooks (independent of mjolnir?)
* [ ] Support community-defined scopes? (ie: no hardcoded config)
* [ ] Vet rooms on startup option

## Docker (preferred)

Mjolnir does not yet have its own image published.

```bash
git clone https://github.com/matrix-org/mjolnir.git
cd mjolnir

docker build -t mjolnir .

# Copy and edit the config. It is not recommended to change the data path.
mkdir -p /etc/mjolnir
cp config/default.yaml /etc/mjolnir/production.yaml
nano /etc/mjolnir/production.yaml

docker run --rm -it -v /etc/mjolnir:/data mjolnir
```

## Build it

This bot requires `yarn` and Node 10.

```bash
git clone https://github.com/matrix-org/mjolnir.git
cd mjolnir

yarn install
yarn build

# Copy and edit the config. It *is* recommended to change the data path.
cp config/default.yaml config/development.yaml
nano config/development.yaml

node lib/index.js
```

## Development

TODO. It's a TypeScript project with a linter.
11 changes: 11 additions & 0 deletions config/default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Where the homeserver is located (client-server URL)
homeserverUrl: "https://matrix.org"

# The access token for the bot to use
accessToken: "YOUR_TOKEN_HERE"

# The directory the bot should store various bits of information in
dataPath: "/data/storage"

# Whether the bot should autojoin rooms it is invited to or not
autojoin: true
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "mjolnir",
"version": "0.1.0",
"description": "A moderation tool for Matrix",
"main": "lib/index.js",
"repository": "[email protected]:matrix-org/mjolnir.git",
"author": "The Matrix.org Foundation C.I.C.",
"license": "Apache-2.0",
"private": true,
"scripts": {
"build": "tsc",
"lint": "tslint --project ./tsconfig.json --type-check -t stylish",
"start:dev": "yarn build && node lib/index.js"
},
"devDependencies": {
"@types/node": "11",
"tslint": "^5.20.0",
"typescript": "^3.6.3"
},
"dependencies": {
"config": "^3.2.2",
"js-yaml": "^3.13.1",
"matrix-bot-sdk": "^0.4.0-beta.5"
}
}
26 changes: 26 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright 2019 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.
*/

import * as config from "config";

interface IConfig {
homeserverUrl: string;
accessToken: string;
dataPath: string;
autojoin: boolean;
}

export default <IConfig>config;
45 changes: 45 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright 2019 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.
*/

import * as path from "path";
import {
AutojoinRoomsMixin,
LogService,
MatrixClient,
RichConsoleLogger,
SimpleFsStorageProvider
} from "matrix-bot-sdk";
import config from "./config";

LogService.setLogger(new RichConsoleLogger());

const storage = new SimpleFsStorageProvider(path.join(config.dataPath, "bot.json"));
const client = new MatrixClient(config.homeserverUrl, config.accessToken, storage);

if (config.autojoin) {
AutojoinRoomsMixin.setupOnClient(client);
}

client.on("room.message", async (roomId, event) => {
if (!event['content']) return;

const content = event['content'];
if (content['msgtype'] === 'm.text' && content['body'] === '!mjolnir') {
await client.sendNotice(roomId, "Hello world!");
}
});

client.start().then(() => LogService.info("index", "Bot started!"));
18 changes: 18 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"module": "commonjs",
"moduleResolution": "node",
"target": "es2015",
"noImplicitAny": false,
"sourceMap": true,
"outDir": "./lib",
"types": [
"node"
]
},
"include": [
"./src/**/*"
]
}
31 changes: 31 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"extends": "tslint:recommended",
"rules": {
"ordered-imports": false,
"no-trailing-whitespace": "error",
"max-classes-per-file": {
"severity": "warning"
},
"object-literal-sort-keys": "off",
"no-any": {
"severity": "warning"
},
"arrow-return-shorthand": true,
"no-magic-numbers": true,
"prefer-for-of": true,
"typedef": {
"severity": "warning"
},
"await-promise": true,
"curly": true,
"no-empty": {
"severity": "warning"
},
"no-invalid-this": true,
"no-string-throw": {
"severity": "warning"
},
"no-unused-expression": true,
"prefer-const": true
}
}
Loading

0 comments on commit ed6f37b

Please sign in to comment.