2020-03-24 16:27:42 +00:00
|
|
|
/*
|
2021-11-12 13:07:57 +00:00
|
|
|
Copyright 2020 mx-puppet-xmpp
|
2020-03-24 16:27:42 +00:00
|
|
|
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";
|
2021-11-12 13:07:57 +00:00
|
|
|
import { Xmpp } from "./xmpp";
|
2020-03-24 16:27:42 +00:00
|
|
|
import { Client } from "./client";
|
|
|
|
|
2021-11-12 13:07:57 +00:00
|
|
|
const log = new Log("XmppPuppet:index");
|
2020-03-24 16:27:42 +00:00
|
|
|
|
|
|
|
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,
|
2021-11-12 13:07:57 +00:00
|
|
|
"registration-file": "xmpp-registration.yaml",
|
2020-03-24 16:27:42 +00:00
|
|
|
"config": "config.yaml",
|
|
|
|
"help": false,
|
|
|
|
}, commandLineArgs(commandOptions));
|
|
|
|
|
|
|
|
if (options.help) {
|
|
|
|
// tslint:disable-next-line:no-console
|
|
|
|
console.log(commandLineUsage([
|
|
|
|
{
|
2021-11-12 13:07:57 +00:00
|
|
|
header: "Matrix Xmpp Puppet Bridge",
|
|
|
|
content: "A matrix puppet bridge for Xmpp",
|
2020-03-24 16:27:42 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
header: "Options",
|
|
|
|
optionList: commandOptions,
|
|
|
|
},
|
|
|
|
]));
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
const protocol: IProtocolInformation = {
|
|
|
|
features: {
|
2021-11-12 13:07:57 +00:00
|
|
|
image: false,
|
|
|
|
audio: false,
|
|
|
|
file: false,
|
|
|
|
edit: false,
|
|
|
|
reply: false,
|
2020-03-24 16:27:42 +00:00
|
|
|
globalNamespace: true,
|
|
|
|
},
|
2021-11-12 13:07:57 +00:00
|
|
|
id: "xmpp",
|
|
|
|
displayname: "Xmpp",
|
|
|
|
externalUrl: "https://xmpp.com/",
|
2020-03-24 16:27:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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({
|
2021-11-12 13:07:57 +00:00
|
|
|
prefix: "_xmpppuppet_",
|
|
|
|
id: "xmpp-puppet",
|
2020-03-24 16:27:42 +00:00
|
|
|
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();
|
2021-11-12 13:07:57 +00:00
|
|
|
const xmpp = new Xmpp(puppet);
|
|
|
|
puppet.on("puppetNew", xmpp.newPuppet.bind(xmpp));
|
|
|
|
puppet.on("puppetDelete", xmpp.deletePuppet.bind(xmpp));
|
|
|
|
puppet.on("message", xmpp.handleMatrixMessage.bind(xmpp));
|
|
|
|
puppet.on("edit", xmpp.handleMatrixEdit.bind(xmpp));
|
|
|
|
puppet.on("reply", xmpp.handleMatrixReply.bind(xmpp));
|
|
|
|
puppet.on("redact", xmpp.handleMatrixRedact.bind(xmpp));
|
|
|
|
puppet.on("image", xmpp.handleMatrixImage.bind(xmpp));
|
|
|
|
puppet.on("audio", xmpp.handleMatrixAudio.bind(xmpp));
|
|
|
|
puppet.on("file", xmpp.handleMatrixFile.bind(xmpp));
|
|
|
|
puppet.setCreateUserHook(xmpp.createUser.bind(xmpp));
|
|
|
|
puppet.setCreateRoomHook(xmpp.createRoom.bind(xmpp));
|
|
|
|
puppet.setGetDmRoomIdHook(xmpp.getDmRoom.bind(xmpp));
|
|
|
|
puppet.setListUsersHook(xmpp.listUsers.bind(xmpp));
|
|
|
|
puppet.setListRoomsHook(xmpp.listRooms.bind(xmpp));
|
|
|
|
puppet.setGetUserIdsInRoomHook(xmpp.getUserIdsInRoom.bind(xmpp));
|
2020-03-24 21:18:38 +00:00
|
|
|
puppet.setGetDescHook(async (puppetId: number, data: any): Promise<string> => {
|
2021-11-12 13:07:57 +00:00
|
|
|
let s = "Xmpp";
|
2020-03-24 21:18:38 +00:00
|
|
|
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;
|
2021-01-27 20:47:52 +00:00
|
|
|
const TOKENS_TO_EXTRACT = 2;
|
|
|
|
const [username, password] = str.split(/ (.+)/, TOKENS_TO_EXTRACT);
|
2020-03-24 21:40:18 +00:00
|
|
|
const data: any = {
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
};
|
2020-03-24 16:27:42 +00:00
|
|
|
try {
|
|
|
|
const client = new Client(username, password);
|
|
|
|
await client.connect();
|
2020-03-24 21:40:18 +00:00
|
|
|
data.state = client.getState;
|
2020-03-24 16:27:42 +00:00
|
|
|
await client.disconnect();
|
|
|
|
} catch (err) {
|
2021-01-27 20:47:52 +00:00
|
|
|
log.verbose("Failed to log in as new user, perhaps the password is wrong?");
|
2020-03-27 17:38:35 +00:00
|
|
|
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 => {
|
2021-11-12 13:07:57 +00:00
|
|
|
return "Xmpp Puppet Bridge";
|
2020-03-24 16:27:42 +00:00
|
|
|
});
|
|
|
|
await puppet.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
// tslint:disable-next-line:no-floating-promises
|
|
|
|
run();
|