Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private onLeave(roomId: string, event: any) {
if (event['sender'] === VoyagerBot.userId) {
// Probably a self kick or operator action
// TODO: Record soft kick (#160)
} else if (event['content'] && event['content']['membership'] === 'ban') {
// Banned
this.addKickBan(roomId, event, 'ban');
} else if (event['unsigned'] && event['unsigned']['prev_content'] && event['unsigned']['prev_content']['membership'] === 'ban') {
// TODO: Handle unbanned state?
LogService.info("Voyager", event['sender'] + " has unbanned us in " + roomId);
} else {
// Kicked
this.addKickBan(roomId, event, 'kick');
}
}
public async unlink(@QueryParam("scalar_token") scalarToken: string, @PathParam("networkId") networkId: string, @PathParam("channel") channelNoHash: string, @PathParam("roomId") roomId: string): Promise {
const userId = await this.accountController.getTokenOwner(scalarToken);
const parsed = IrcBridge.parseNetworkId(networkId);
const bridge = await IrcBridgeRecord.findByPk(parsed.bridgeId);
if (!bridge) throw new ApiError(404, "Bridge not found");
const client = new IrcBridge(userId);
await client.removeLink(bridge, parsed.bridgeNetworkId, "#" + channelNoHash, roomId);
LogService.info("DimensionIrcService", userId + " unlinked #" + channelNoHash + " on " + networkId + " from " + roomId);
return {}; // 200 OK
}
}
private loadRoutes() {
// TODO: Rename services to controllers, and controllers to services. They're backwards.
const apis = ["scalar", "dimension", "admin", "matrix"].map(a => path.join(__dirname, a, "*.js"));
const router = express.Router();
Server.useIoC();
Server.registerAuthenticator(new MatrixSecurity());
apis.forEach(a => Server.loadServices(router, [a]));
const routes = _.uniq(router.stack.map(r => r.route.path));
for (const route of routes) {
this.app.options(route, (_req, res) => res.sendStatus(200));
LogService.info("Webserver", "Registered route: " + route);
}
this.app.use(router);
// We register the default route last to make sure we don't override anything by accident.
// We'll pass off all other requests to the web app
this.app.get(/(widgets\/|riot\/|\/)*/, (_req, res) => {
res.sendFile(path.join(__dirname, "..", "..", "web", "index.html"));
});
// Set up the error handler
this.app.use((err: any, _req, res, next) => {
if (err instanceof ApiError) {
// Don't do anything for 'connection reset'
if (res.headersSent) return next(err);
LogService.warn("Webserver", "Handling ApiError " + err.statusCode + " " + JSON.stringify(err.jsonResponse));
public static async registerUser(appserviceId: string, userId: string): Promise {
const appservice = await AppService.findOne({where: {id: appserviceId}});
if (!appservice) throw new Error("Appservice not found");
LogService.info("AppserviceStore", "Registering to own " + userId + " in appservice " + appserviceId);
const client = new MatrixAppserviceClient(appservice);
const localpart = AppserviceStore.getSafeUserId(userId.substring(1).split(":")[0]);
const response = await client.registerUser(localpart);
LogService.info("AppserviceStore", "Successfully registered " + userId);
return await AppServiceUser.create({
id: userId,
appserviceId: appserviceId,
accessToken: response.access_token,
});
}
public async createBot(@QueryParam("scalar_token") scalarToken: string, request: BotRequest): Promise {
const userId = await AdminService.validateAndGetAdminTokenOwner(scalarToken);
const bot = await BotStore.createCustom(request);
LogService.info("AdminCustomSimpleBotService", userId + " created a simple bot");
Cache.for(CACHE_INTEGRATIONS).clear();
return bot;
}
deleteWebhook(roomId, userId, hookId) {
LogService.info("ProvisioningService", "Processing delete hook (" + hookId + ") request for " + roomId + " by " + userId);
return this.hasPermission(userId, roomId)
.then(async () => {
const webhooks = await WebhookStore.listWebhooks(roomId);
if (webhooks.length === 1 && webhooks[0].id === hookId) {
await this._intent.leave(roomId);
}
return WebhookStore.deleteWebhook(roomId, hookId)
}, () => Promise.reject(this.PERMISSION_ERROR_MESSAGE));
}
debug: (module: string, ...args: any[]) => args.map(a => LogService.info("BotSdk-" + module, a)),
info: (module: string, ...args: any[]) => args.map(a => LogService.info("BotSdk-" + module, a)),
public async createUpstream(@QueryParam("scalar_token") scalarToken: string, request: NewUpstreamRequest): Promise {
const userId = await AdminService.validateAndGetAdminTokenOwner(scalarToken);
const upstream = await Upstream.create({
name: request.name,
type: request.type,
scalarUrl: request.scalarUrl,
apiUrl: request.apiUrl,
});
LogService.info("AdminUpstreamService", userId + " created a new upstream");
Cache.for(CACHE_UPSTREAM).clear();
Cache.for(CACHE_SCALAR_ACCOUNTS).clear();
return this.mapUpstream(upstream);
}
public async getOps(@QueryParam("scalar_token") scalarToken: string, @PathParam("networkId") networkId: string, @PathParam("channel") channelNoHash: string): Promise {
const userId = await this.accountController.getTokenOwner(scalarToken);
const parsed = IrcBridge.parseNetworkId(networkId);
const bridge = await IrcBridgeRecord.findByPk(parsed.bridgeId);
if (!bridge) throw new ApiError(404, "Bridge not found");
const client = new IrcBridge(userId);
const operators = await client.getOperators(bridge, parsed.bridgeNetworkId, "#" + channelNoHash);
LogService.info("DimensionIrcService", userId + " listed the operators for #" + channelNoHash + " on " + networkId);
return operators;
}
public async updateBot(@QueryParam("scalar_token") scalarToken: string, @PathParam("botId") botId: number, request: BotRequest): Promise {
const userId = await AdminService.validateAndGetAdminTokenOwner(scalarToken);
const bot = await BotStore.updateCustom(botId, request);
LogService.info("AdminCustomSimpleBotService", userId + " updated a simple bot");
Cache.for(CACHE_INTEGRATIONS).clear();
return bot;
}