mx-puppet-xmpp/src/index.ts

148 lines
4.2 KiB
TypeScript
Raw Normal View History

2020-03-24 16:27:42 +00:00
/*
Copyright 2020 mx-puppet-skype
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 {
PuppetBridge,
IPuppetBridgeRegOpts,
Log,
IRetData,
Util,
IProtocolInformation,
} from "mx-puppet-bridge";
import * as commandLineArgs from "command-line-args";
import * as commandLineUsage from "command-line-usage";
import * as fs from "fs";
import * as yaml from "js-yaml";
import { Skype } from "./skype";
import { Client } from "./client";
const log = new Log("SkypePuppet:index");
const commandOptions = [
{ name: "register", alias: "r", type: Boolean },
{ name: "registration-file", alias: "f", type: String },
{ name: "config", alias: "c", type: String },
{ name: "help", alias: "h", type: Boolean },
];
const options = Object.assign({
"register": false,
"registration-file": "skype-registration.yaml",
"config": "config.yaml",
"help": false,
}, commandLineArgs(commandOptions));
if (options.help) {
// tslint:disable-next-line:no-console
console.log(commandLineUsage([
{
header: "Matrix Skype Puppet Bridge",
content: "A matrix puppet bridge for skype",
},
{
header: "Options",
optionList: commandOptions,
},
]));
process.exit(0);
}
const protocol: IProtocolInformation = {
features: {
2020-03-24 20:10:49 +00:00
image: true,
audio: true,
file: true,
edit: true,
2020-04-25 09:38:50 +00:00
reply: true,
2020-03-24 16:27:42 +00:00
globalNamespace: true,
},
id: "skype",
displayname: "Skype",
externalUrl: "https://skype.com/",
};
const puppet = new PuppetBridge(options["registration-file"], options.config, protocol);
if (options.register) {
// okay, all we have to do is generate a registration file
puppet.readConfig(false);
try {
puppet.generateRegistration({
prefix: "_skypepuppet_",
id: "skype-puppet",
url: `http://${puppet.Config.bridge.bindAddress}:${puppet.Config.bridge.port}`,
});
} catch (err) {
// tslint:disable-next-line:no-console
console.log("Couldn't generate registration file:", err);
}
process.exit(0);
}
async function run() {
await puppet.init();
const skype = new Skype(puppet);
puppet.on("puppetNew", skype.newPuppet.bind(skype));
puppet.on("puppetDelete", skype.deletePuppet.bind(skype));
puppet.on("message", skype.handleMatrixMessage.bind(skype));
2020-03-24 20:10:49 +00:00
puppet.on("edit", skype.handleMatrixEdit.bind(skype));
2020-04-25 09:38:50 +00:00
puppet.on("reply", skype.handleMatrixReply.bind(skype));
2020-03-24 20:10:49 +00:00
puppet.on("redact", skype.handleMatrixRedact.bind(skype));
puppet.on("image", skype.handleMatrixImage.bind(skype));
puppet.on("audio", skype.handleMatrixAudio.bind(skype));
puppet.on("file", skype.handleMatrixFile.bind(skype));
2020-03-24 16:27:42 +00:00
puppet.setCreateUserHook(skype.createUser.bind(skype));
puppet.setCreateRoomHook(skype.createRoom.bind(skype));
2020-03-24 21:18:38 +00:00
puppet.setGetDmRoomIdHook(skype.getDmRoom.bind(skype));
puppet.setListUsersHook(skype.listUsers.bind(skype));
puppet.setListRoomsHook(skype.listRooms.bind(skype));
2020-03-24 16:27:42 +00:00
puppet.setGetUserIdsInRoomHook(skype.getUserIdsInRoom.bind(skype));
2020-03-24 21:18:38 +00:00
puppet.setGetDescHook(async (puppetId: number, data: any): Promise<string> => {
let s = "Skype";
if (data.username) {
2020-03-24 21:29:04 +00:00
s += ` as \`${data.username}\``;
2020-03-24 21:18:38 +00:00
}
return s;
});
2020-03-24 16:27:42 +00:00
puppet.setGetDataFromStrHook(async (str: string): Promise<IRetData> => {
const retData = {
success: false,
} as IRetData;
const [username, password] = str.split(" ");
const data: any = {
username,
password,
};
2020-03-24 16:27:42 +00:00
try {
const client = new Client(username, password);
await client.connect();
data.state = client.getState;
2020-03-24 16:27:42 +00:00
await client.disconnect();
} catch (err) {
log.verbose("Failed to log in as new user, perhaps the password is worng?");
log.silly(err);
2020-03-24 16:27:42 +00:00
retData.error = "Username or password wrong";
return retData;
}
retData.success = true;
retData.data = data;
return retData;
});
puppet.setBotHeaderMsgHook((): string => {
return "Skype Puppet Bridge";
});
await puppet.start();
}
// tslint:disable-next-line:no-floating-promises
run();