|
|
|
@ -24,6 +24,17 @@ async function checkUserPermissions(sender) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
async function loginUser(matrixUser, remoteUser, oauthToken) {
|
|
|
|
|
remoteUser.set('password', oauthToken);
|
|
|
|
|
await bridge.getUserStore().linkUsers(matrixUser, remoteUser)
|
|
|
|
|
}
|
|
|
|
|
async function logoutUser(matrixId) {
|
|
|
|
|
const links = await bridge.getUserStore().getRemoteLinks(matrixId);
|
|
|
|
|
links.forEach(async remoteId => {
|
|
|
|
|
await bridge.getUserStore().unlinkUserIds(matrixId, remoteId);
|
|
|
|
|
console.log('Removed credentials of', remoteId, 'by', matrixId);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
async function bridgeRoom(sender, matrixRoom, remoteRoom) {
|
|
|
|
|
await bridge.getRoomStore().linkRooms(matrixRoom, remoteRoom);
|
|
|
|
|
console.log('Linked rooms', matrixRoom.roomId, 'and', remoteRoom.roomId, 'by', sender);
|
|
|
|
@ -35,47 +46,63 @@ async function unbridgeRoom(sender, roomId) {
|
|
|
|
|
async function matrixEventHandler(request, context) {
|
|
|
|
|
const event = request.getData();
|
|
|
|
|
|
|
|
|
|
if(!await checkUserPermissions(event.sender)) {
|
|
|
|
|
// no permissions; don't bridge
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(event.type === 'm.room.message') {
|
|
|
|
|
const message = event.content.body;
|
|
|
|
|
|
|
|
|
|
const remoteUsers = await bridge.getUserStore().getRemoteUsersFromMatrixId(event.sender);
|
|
|
|
|
// use the first user, multipe user mappings are unsupported
|
|
|
|
|
const remoteUser = remoteUsers[0];
|
|
|
|
|
if(!remoteUser) {
|
|
|
|
|
// ignore messages from users without tmi credentials
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// handle bot commands and return directly afterwards
|
|
|
|
|
const command = message.match(/^!tmi (?<subcommand>\w*)( (?<args>.*))?$/);
|
|
|
|
|
if(command) {
|
|
|
|
|
if(!await checkAdminPermissions(event.sender)) {
|
|
|
|
|
bridge.getIntent().sendText(event.room_id, event.sender+' is not permitted as admin of this bridge');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
switch (command.groups.subcommand) {
|
|
|
|
|
case 'login':
|
|
|
|
|
const args = command.groups.args.split(' ');
|
|
|
|
|
const tmiUser = args[0];
|
|
|
|
|
const oauthToken = args[1];
|
|
|
|
|
await loginUser(new mx.MatrixUser(event.sender), new mx.RemoteUser(tmiUser), oauthToken);
|
|
|
|
|
bridge.getIntent().sendText(event.room_id, 'Saved oauth token of '+event.sender+' - please redact your login message to protect the token');
|
|
|
|
|
return;
|
|
|
|
|
case 'logout':
|
|
|
|
|
await logoutUser(event.sender);
|
|
|
|
|
bridge.getIntent().sendText(event.room_id, 'Removed oauth token of '+event.sender);
|
|
|
|
|
return;
|
|
|
|
|
case 'bridge':
|
|
|
|
|
if(!await checkAdminPermissions(event.sender)) {
|
|
|
|
|
bridge.getIntent().sendText(event.room_id, event.sender+' is not permitted as admin of this bridge');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let channel = command.groups.args.toLowerCase();
|
|
|
|
|
if(!channel.match(/^#/)) {
|
|
|
|
|
channel = '#'+channel
|
|
|
|
|
}
|
|
|
|
|
bridgeRoom(event.sender, new mx.MatrixRoom(event.room_id), new mx.RemoteRoom(channel));
|
|
|
|
|
bridge.getIntent().sendText(event.room_id, 'This room is now bridged to '+channel);
|
|
|
|
|
break;
|
|
|
|
|
return;
|
|
|
|
|
case 'unbridge':
|
|
|
|
|
if(!await checkAdminPermissions(event.sender)) {
|
|
|
|
|
bridge.getIntent().sendText(event.room_id, event.sender+' is not permitted as admin of this bridge');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unbridgeRoom(event.sender, event.room_id);
|
|
|
|
|
bridge.getIntent().sendText(event.room_id, 'Removed all bridges from this room');
|
|
|
|
|
break;
|
|
|
|
|
return;
|
|
|
|
|
default:
|
|
|
|
|
bridge.getIntent().sendText(event.room_id, 'available commands: bridge, unbridge');
|
|
|
|
|
break;
|
|
|
|
|
bridge.getIntent().sendText(event.room_id, 'available commands: login, logout, bridge, unbridge');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!await checkUserPermissions(event.sender)) {
|
|
|
|
|
// no permissions; don't bridge
|
|
|
|
|
|
|
|
|
|
const remoteUsers = await bridge.getUserStore().getRemoteUsersFromMatrixId(event.sender);
|
|
|
|
|
// use the first user, multipe user mappings are unsupported
|
|
|
|
|
const remoteUser = remoteUsers[0];
|
|
|
|
|
if(!remoteUser) {
|
|
|
|
|
// ignore messages from users without tmi credentials
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|