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.
|
|
|
|
*/
|
|
|
|
|
2020-03-24 20:10:49 +00:00
|
|
|
import { Log, ExpireSet, IRemoteRoom, Util } from "mx-puppet-bridge";
|
2020-03-24 16:27:42 +00:00
|
|
|
import { EventEmitter } from "events";
|
|
|
|
import * as skypeHttp from "skype-http";
|
|
|
|
import { Contact as SkypeContact } from "skype-http/dist/lib/types/contact";
|
2020-03-24 20:10:49 +00:00
|
|
|
import { NewMediaMessage as SkypeNewMediaMessage } from "skype-http/dist/lib/interfaces/api/api";
|
2020-03-24 21:18:38 +00:00
|
|
|
import { Context as SkypeContext } from "skype-http/dist/lib/interfaces/api/context";
|
2020-03-24 16:27:42 +00:00
|
|
|
|
|
|
|
const log = new Log("SkypePuppet:client");
|
|
|
|
|
2020-03-24 21:18:38 +00:00
|
|
|
const ID_TIMEOUT = 60000;
|
2020-03-24 16:27:42 +00:00
|
|
|
|
|
|
|
export class Client extends EventEmitter {
|
2020-03-24 21:18:38 +00:00
|
|
|
public contacts: Map<string, SkypeContact | null> = new Map();
|
|
|
|
public conversations: Map<string, skypeHttp.Conversation | null> = new Map();
|
2020-03-24 16:27:42 +00:00
|
|
|
private api: skypeHttp.Api;
|
|
|
|
private handledIds: ExpireSet<string>;
|
|
|
|
constructor(
|
|
|
|
private loginUsername: string,
|
|
|
|
private password: string,
|
2020-03-24 21:18:38 +00:00
|
|
|
private state?: SkypeContext.Json,
|
2020-03-24 16:27:42 +00:00
|
|
|
) {
|
|
|
|
super();
|
|
|
|
this.handledIds = new ExpireSet(ID_TIMEOUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
public get username(): string {
|
|
|
|
return "8:" + this.api.context.username;
|
|
|
|
}
|
|
|
|
|
2020-03-24 21:18:38 +00:00
|
|
|
public get getState(): SkypeContext.Json {
|
|
|
|
return this.api.getState();
|
|
|
|
}
|
|
|
|
|
2020-03-24 16:27:42 +00:00
|
|
|
public async connect() {
|
2020-03-25 14:47:51 +00:00
|
|
|
let connectedWithAuth = false;
|
|
|
|
if (this.state && false) {
|
2020-03-24 21:18:38 +00:00
|
|
|
try {
|
|
|
|
this.api = await skypeHttp.connect({ state: this.state, verbose: true });
|
2020-03-25 14:47:51 +00:00
|
|
|
connectedWithAuth = true;
|
2020-03-24 21:18:38 +00:00
|
|
|
} catch (err) {
|
|
|
|
this.api = await skypeHttp.connect({
|
|
|
|
credentials: {
|
|
|
|
username: this.loginUsername,
|
|
|
|
password: this.password,
|
|
|
|
},
|
|
|
|
verbose: true,
|
|
|
|
});
|
2020-03-25 14:47:51 +00:00
|
|
|
connectedWithAuth = false;
|
2020-03-24 21:18:38 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.api = await skypeHttp.connect({
|
|
|
|
credentials: {
|
|
|
|
username: this.loginUsername,
|
|
|
|
password: this.password,
|
|
|
|
},
|
|
|
|
verbose: true,
|
|
|
|
});
|
2020-03-25 14:47:51 +00:00
|
|
|
connectedWithAuth = false;
|
2020-03-24 21:18:38 +00:00
|
|
|
}
|
2020-03-24 16:27:42 +00:00
|
|
|
|
|
|
|
this.api.on("event", (evt: skypeHttp.events.EventMessage) => {
|
|
|
|
if (!evt || !evt.resource) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const resource = evt.resource;
|
|
|
|
if (this.handledIds.has(resource.id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.handledIds.add(resource.id);
|
|
|
|
log.debug(`Got new event of type ${resource.type}`);
|
|
|
|
const [type, subtype] = resource.type.split("/");
|
|
|
|
switch (type) {
|
|
|
|
case "Text":
|
|
|
|
this.emit("text", resource);
|
|
|
|
break;
|
|
|
|
case "RichText":
|
|
|
|
if (subtype === "Location") {
|
|
|
|
this.emit("location", resource);
|
|
|
|
} else if (subtype) {
|
|
|
|
this.emit("file", resource);
|
|
|
|
} else {
|
|
|
|
this.emit("richText", resource);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "Control":
|
|
|
|
if (subtype === "Typing" || subtype === "ClearTyping") {
|
|
|
|
this.emit("typing", resource, subtype === "Typing");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "ThreadActivity":
|
2020-03-24 20:26:52 +00:00
|
|
|
if (subtype === "MemberConsumptionHorizonUpdate") {
|
|
|
|
this.emit("presence", resource);
|
|
|
|
}
|
2020-03-24 16:27:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.api.on("error", (err: Error) => {
|
|
|
|
log.error("An error occured", err);
|
2020-03-24 21:18:38 +00:00
|
|
|
this.emit("error", err);
|
2020-03-24 16:27:42 +00:00
|
|
|
});
|
|
|
|
|
2020-03-25 14:47:51 +00:00
|
|
|
const contacts = await this.api.getContacts();
|
|
|
|
for (const contact of contacts) {
|
|
|
|
this.contacts.set(contact.mri, contact);
|
|
|
|
}
|
|
|
|
const conversations = await this.api.getConversations();
|
|
|
|
for (const conversation of conversations) {
|
|
|
|
this.conversations.set(conversation.id, conversation);
|
|
|
|
}
|
|
|
|
|
2020-03-24 16:27:42 +00:00
|
|
|
await this.api.listen();
|
|
|
|
await this.api.setStatus("Online");
|
|
|
|
}
|
|
|
|
|
|
|
|
public async disconnect() {
|
|
|
|
await this.api.stopListening();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getContact(id: string): Promise<SkypeContact | null> {
|
|
|
|
const hasStart = Boolean(id.match(/^\d+:/));
|
|
|
|
const fullId = hasStart ? id : `8:${id}`;
|
|
|
|
if (this.contacts.has(fullId)) {
|
|
|
|
return this.contacts.get(fullId) || null;
|
|
|
|
}
|
|
|
|
if (hasStart) {
|
2020-03-24 21:18:38 +00:00
|
|
|
id = id.substr(id.indexOf(":") + 1);
|
2020-03-24 16:27:42 +00:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
const rawContact = await this.api.getContact(id);
|
|
|
|
const contact: SkypeContact = {
|
|
|
|
personId: rawContact.id.raw,
|
|
|
|
workloads: null,
|
|
|
|
mri: rawContact.id.raw,
|
|
|
|
blocked: false,
|
|
|
|
authorized: true,
|
|
|
|
creationTime: new Date(),
|
|
|
|
displayName: (rawContact.name && rawContact.name.displayName) || rawContact.id.id,
|
|
|
|
displayNameSource: "profile" as any, // tslint:disable-line no-any
|
|
|
|
profile: {
|
|
|
|
avatarUrl: rawContact.avatarUrl || undefined,
|
|
|
|
name: {
|
|
|
|
first: (rawContact.name && rawContact.name).first || undefined,
|
|
|
|
surname: (rawContact.name && rawContact.name).surname || undefined,
|
|
|
|
nickname: (rawContact.name && rawContact.name).nickname || undefined,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
this.contacts.set(contact.mri, contact || null);
|
|
|
|
return contact || null;
|
|
|
|
} catch (err) {
|
|
|
|
// contact not found
|
|
|
|
this.contacts.set(fullId, null);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getConversation(room: IRemoteRoom): Promise<skypeHttp.Conversation | null> {
|
|
|
|
let id = room.roomId;
|
|
|
|
const match = id.match(/^dm-\d+-/);
|
|
|
|
if (match) {
|
|
|
|
const [_, puppetId] = id.split("-");
|
|
|
|
if (Number(puppetId) !== room.puppetId) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
id = id.substr(match[0].length);
|
|
|
|
}
|
|
|
|
if (this.conversations.has(id)) {
|
|
|
|
return this.conversations.get(id) || null;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const conversation = await this.api.getConversation(id);
|
|
|
|
this.conversations.set(conversation.id, conversation || null);
|
|
|
|
return conversation || null;
|
|
|
|
} catch (err) {
|
|
|
|
// conversation not found
|
|
|
|
this.conversations.set(id, null);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 20:10:49 +00:00
|
|
|
public async downloadFile(url: string): Promise<Buffer> {
|
|
|
|
if (!url.includes("/views/imgpsh_fullsize_anim")) {
|
|
|
|
url = url + "/views/imgpsh_fullsize_anim";
|
|
|
|
}
|
|
|
|
return await Util.DownloadFile(url, {
|
|
|
|
cookies: this.api.context.cookies,
|
2020-03-24 21:18:38 +00:00
|
|
|
headers: { Authorization: "skype_token " + this.api.context.skypeToken.value },
|
2020-03-24 20:10:49 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-24 16:27:42 +00:00
|
|
|
public async sendMessage(conversationId: string, msg: string): Promise<skypeHttp.Api.SendMessageResult> {
|
|
|
|
return await this.api.sendMessage({
|
|
|
|
textContent: msg,
|
|
|
|
}, conversationId);
|
|
|
|
}
|
2020-03-24 20:10:49 +00:00
|
|
|
|
|
|
|
public async sendEdit(conversationId: string, messageId: string, msg: string) {
|
|
|
|
return await this.api.sendEdit({
|
|
|
|
textContent: msg,
|
|
|
|
}, conversationId, messageId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async sendDelete(conversationId: string, messageId: string) {
|
|
|
|
return await this.api.sendDelete(conversationId, messageId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async sendAudio(
|
|
|
|
conversationId: string,
|
|
|
|
opts: SkypeNewMediaMessage,
|
|
|
|
): Promise<skypeHttp.Api.SendMessageResult> {
|
|
|
|
return await this.api.sendAudio(opts, conversationId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async sendDocument(
|
|
|
|
conversationId: string,
|
2020-03-24 21:18:38 +00:00
|
|
|
opts: SkypeNewMediaMessage,
|
2020-03-24 20:10:49 +00:00
|
|
|
): Promise<skypeHttp.Api.SendMessageResult> {
|
|
|
|
return await this.api.sendDocument(opts, conversationId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async sendImage(
|
|
|
|
conversationId: string,
|
2020-03-24 21:18:38 +00:00
|
|
|
opts: SkypeNewMediaMessage,
|
2020-03-24 20:10:49 +00:00
|
|
|
): Promise<skypeHttp.Api.SendMessageResult> {
|
|
|
|
return await this.api.sendImage(opts, conversationId);
|
|
|
|
}
|
2020-03-24 16:27:42 +00:00
|
|
|
}
|