-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathclient.js
141 lines (127 loc) · 3.73 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const fs = require('fs');
const skypeHttp = require('skype-http');
const debug = require('debug')('matrix-puppet:skype:client');
const Promise = require('bluebird');
// look at
// https://github.com/ocilo/skype-http/blob/master/src/example/main.ts
const EventEmitter = require('events').EventEmitter;
const { download, entities } = require('./utils');
class Client extends EventEmitter {
constructor(auth) {
super();
this.api = null;
this.auth = auth;
this.lastMsgId = null;
this.selfSentFiles = [];
}
removeSelfSentFile(s) {
let match = false;
while (true) {
let i = this.selfSentFiles.indexOf(s);
if (i == -1) {
return match;
}
match = true;
this.selfSentFiles.splice(i, 1);
}
}
connect() {
const opts = {
credentials: this.auth,
verbose: true
}
return skypeHttp.connect(opts).then(api => {
this.api = api;
api.on("event", (ev) => {
//console.log(ev);
if (ev && ev.resource) {
switch (ev.resource.type) {
case "Text":
case "RichText":
if (ev.resource.from.username === api.context.username) {
// the lib currently hides this kind from us. but i want it.
if (ev.resource.content.slice(-1) !== '\ufeff') {
this.emit('sent', ev.resource);
}
} else {
this.emit('message', ev.resource);
}
break;
case "RichText/UriObject":
if (!this.removeSelfSentFile(ev.resource.original_file_name)) {
if (ev.resource.from.username === api.context.username) {
ev.resource.from.raw = undefined;
}
this.emit('image', ev.resource)
}
break;
}
}
});
// Log every error
api.on("error", (err) => {
console.error("An error was detected:");
console.error(err);
this.emit('error', err);
});
return api.getContacts().then((contacts)=>{
this.contacts = contacts;
console.log(`got ${contacts.length} contacts`);
console.log('listening for events');
return api.listen();
});
}).then(()=>{
console.log('setting status online');
return this.api.setStatus('Online');
console.log(api);
}).catch(err=>{
console.log(err);
process.exit(0);
});
}
sendMessage(threadId, msg) {
return this.api.sendMessage(msg, threadId);
}
sendPictureMessage(threadId, data) {
this.selfSentFiles.push(data.name);
return this.api.sendImage({
file: data.file,
name: data.name
}, threadId).catch((err) => {
this.removeSelfSentFile(data.name);
this.api.sendMessage({ textContent: '[Image] <a href="'+entities.encode(data.url)+'">'+entities.encode(data.name)+'</a>' }, threadId);
});
}
getContact(id) {
let contact = this.contacts.find((c) => {
return c.personId === id || c.mri === id;
});
if (contact) {
return contact;
}
}
getConversation(id) {
return this.api.getConversation(id);
}
downloadImage(url) {
return download.getBufferAndType(url, {
cookies: this.api.context.cookies,
headers: {
Authorization: 'skype_token ' + this.api.context.skypeToken.value
}
});
}
}
module.exports = Client;
if (!module.parent) {
const client = new Client(require('./config.json').skype);
client.connect().then(function() {
client.on('message', (ev) => {
console.log('>>> message', ev);
});
client.on('sent', (ev) => {
console.log('>>> sent', ev);
});
client.sendMessage('8:green.streak', { textContent: 'test from nodejs' });
});
}