diff --git a/.env.sample b/.env.sample
index 348d121..63ffb04 100644
--- a/.env.sample
+++ b/.env.sample
@@ -15,4 +15,8 @@ MATRIX_USER=
MATRIX_TOKEN=
# The bot set this as its display name (Default: Timetable V2)
-MATRIX_DISPLAYNAME=
\ No newline at end of file
+MATRIX_DISPLAYNAME=
+
+# If set, this password must be used with the "login" command
+# to be able to use this bot
+AUTH_PASSWORD=
\ No newline at end of file
diff --git a/index.js b/index.js
index 1ad7dc4..f71d6c0 100644
--- a/index.js
+++ b/index.js
@@ -6,6 +6,8 @@ import "dotenv/config";
import { JsonDB, Config } from "node-json-db";
import { TimetableClient } from "./timetable.js";
+const authPassword = process.env.AUTH_PASSWORD;
+
const db = new JsonDB(new Config("data/data", true, true, "/"));
const timetable = new TimetableClient(
process.env.TIMETABLE_ENDPOINT,
@@ -148,6 +150,34 @@ client.on("Room.timeline", async function (event, room) {
return;
}
+ if (
+ !(await db.getObjectDefault(`/authStatus/${room.roomId}`, false)) &&
+ authPassword
+ ) {
+ if (!event.event.content.body.startsWith("login")) {
+ const response =
+ "🔐 Not authenticated! Use login [password]
to login";
+ client.sendHtmlMessage(room.roomId, plainText(response), response);
+ return;
+ }
+
+ if (event.event.content.body.split("login ")[1] == authPassword) {
+ await db.push(`/authStatus/${room.roomId}`, true);
+ client.sendTextMessage(room.roomId, "🔑 This room is now authenticated");
+ client
+ .redactEvent(room.roomId, event.event.event_id, undefined, {
+ reason: "Redacted login password",
+ })
+ .catch((e) => {
+ console.warn("Could not redact password in " + room.roomId);
+ });
+ return;
+ } else {
+ client.sendTextMessage(room.roomId, "❌ Invalid password");
+ return;
+ }
+ }
+
try {
if (event.getType() === "m.room.message") {
await handleMessage(event, room.roomId);
@@ -195,6 +225,7 @@ async function handleMessage(event, room) {
timetable
: Set your timetablegroups
: Configure your timetable groups [Not implemented yet]reset
: Reset the configuration for this roomlogout
: Logout and reset the configuration for roomhelp
for a list of valid commands";