Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (this.banLists.map(b => b.roomId).includes(roomId)) {
if (ALL_RULE_TYPES.includes(event['type'])) {
await this.syncListForRoom(roomId);
}
}
if (Object.keys(this.protectedRooms).includes(roomId)) {
if (event['sender'] === await this.client.getUserId()) return; // Ignore ourselves
// Iterate all the protections
for (const protection of this.protections) {
try {
await protection.handleEvent(this, roomId, event);
} catch (e) {
const eventPermalink = Permalinks.forEvent(roomId, event['event_id']);
LogService.error("Mjolnir", "Error handling protection: " + protection.name);
LogService.error("Mjolnir", "Failed event: " + eventPermalink);
LogService.error("Mjolnir", e);
await this.client.sendNotice(config.managementRoom, "There was an error processing an event through a protection - see log for details. Event: " + eventPermalink);
}
}
// Run the event handlers - we always run this after protections so that the protections
// can flag the event for redaction.
await this.redactionQueue.handleEvent(roomId, event, this.client);
if (event['type'] === 'm.room.power_levels' && event['state_key'] === '') {
// power levels were updated - recheck permissions
ErrorCache.resetError(roomId, ERROR_KIND_PERMISSION);
const url = this.protectedRooms[roomId];
let html = `Power levels changed in <a href="${url}">${roomId}</a> - checking permissions...`;
let text = `Power levels changed in ${url} - checking permissions...`;
public async isSynapseAdmin(): Promise {
try {
const endpoint = `/_synapse/admin/v1/users/${await this.client.getUserId()}/admin`;
const response = await this.client.doRequest("GET", endpoint);
return response['admin'];
} catch (e) {
LogService.error("Mjolnir", "Error determining if Mjolnir is a server admin:");
LogService.error("Mjolnir", e);
return false; // assume not
}
}
roomId,
errorMessage: `Missing power level for redactions: ${userLevel} < ${redact}`,
errorKind: ERROR_KIND_PERMISSION,
});
}
if (userLevel < aclLevel) {
errors.push({
roomId,
errorMessage: `Missing power level for server ACLs: ${userLevel} < ${aclLevel}`,
errorKind: ERROR_KIND_PERMISSION,
});
}
// Otherwise OK
} catch (e) {
LogService.error("Mjolnir", e);
errors.push({
roomId,
errorMessage: e.message || (e.body ? e.body.error : ''),
errorKind: ERROR_KIND_FATAL,
});
}
return errors;
}
public async isSynapseAdmin(): Promise {
try {
const endpoint = `/_synapse/admin/v1/users/${await this.client.getUserId()}/admin`;
const response = await this.client.doRequest("GET", endpoint);
return response['admin'];
} catch (e) {
LogService.error("Mjolnir", "Error determining if Mjolnir is a server admin:");
LogService.error("Mjolnir", e);
return false; // assume not
}
}
"!mjolnir unwatch - Unwatches a ban list\n" +
"!mjolnir import - Imports bans and ACLs into the given list\n" +
"!mjolnir default - Sets the default list for commands\n" +
"!mjolnir deactivate - Deactivates a user ID\n" +
"!mjolnir protections - List all available protections\n" +
"!mjolnir enable - Enables a particular protection\n" +
"!mjolnir disable - Disables a particular protection\n" +
"!mjolnir help - This menu\n";
const html = `<b>Mjolnir help:</b><br><pre><code>${htmlEscape(menu)}</code></pre>`;
const text = `Mjolnir help:\n${menu}`;
const reply = RichReply.createFor(roomId, event, text, html);
reply["msgtype"] = "m.notice";
return await mjolnir.client.sendMessage(roomId, reply);
}
} catch (e) {
LogService.error("CommandHandler", e);
const text = "There was an error processing your command - see console/log for details";
const reply = RichReply.createFor(roomId, event, text, text);
reply["msgtype"] = "m.notice";
return await mjolnir.client.sendMessage(roomId, reply);
}
}
}, (err, res, _body) => {
if (err) {
LogService.error("utils", "Error downloading file from " + url);
LogService.error("utils", err);
reject(err);
} else if (res.statusCode !== 200) {
LogService.error("utils", "Got status code " + res.statusCode + " while calling url " + url);
reject(new Error("Error in request: invalid status code"));
} else {
resolve(res.body);
}
});
});
this.client.sendStateEvent(this.roomId, SHORTCODE_EVENT_TYPE, '', {shortcode: this.shortcode}).catch(err => {
LogService.error("BanList", err);
if (this.shortcode === newShortcode) this.shortcode = currentShortcode;
});
}
public async migrateUp(): Promise {
await this.query("CREATE TABLE IF NOT EXISTS migrations (id TEXT NOT NULL)");
const rows = await this.query<{ id: string }>("SELECT id FROM migrations");
const migrations = rows.map(r => r.id);
const toRun = KNOWN_MIGRATIONS.filter(i => migrations.indexOf(i.id) === -1);
LogService.info("PostgresDatabase", `Running ${toRun.length} migrations`);
for (const migration of toRun) {
const txn = await this.startTransaction();
try {
await migration.up(txn);
await txn.insert("migrations", {id: migration.id});
await txn.commitTransaction();
} catch (e) {
LogService.error("PostgresDatabase", e);
await txn.rollbackTransaction();
throw e;
} finally {
txn.release();
}
}
}