-
Notifications
You must be signed in to change notification settings - Fork 19
/
client.js
53 lines (45 loc) · 1.8 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
const debug = require('debug');
const debugVerbose = debug('verbose:matrix-puppet:hangouts:client');
const EventEmitter = require('events').EventEmitter;
const Promise = require('bluebird');
const resolveData = ({data:{response}}) => {
return Promise.resolve(response);
};
class Client extends EventEmitter {
constructor() {
super();
this.hangupsProc = null;
}
connect() {
this.hangupsProc = require("child_process").spawn('python3', ['-u', 'hangups_client.py']);
this.hangupsProc.stderr.on("data", (str) => {
this.emit('status', str.toString());
debugVerbose("status message from hangups process:", str.toString());
});
this.hangupsProc.stdout.on("data", (str) => {
debugVerbose("got message from hangups before JSON.parse():", str.toString());
// XXX:NOTE: See https://github.com/matrix-hacks/matrix-puppet-hangouts/pull/24 for rationale
try {
var data = JSON.parse(str);
debugVerbose("emitting message", data);
this.emit('message', data);
} catch(error) {
debugVerbose("ERROR: incorrect JSON format: ", str.toString());
}
});
console.log('started hangups child');
}
send(id, msg) {
let themsg = { 'cmd': "sendmessage", 'conversation_id':id, 'msgbody': msg };
debugVerbose('sending message to hangouts subprocess: ', JSON.stringify(themsg));
this.hangupsProc.stdin.write(JSON.stringify(themsg) + "\n");
return Promise.resolve();
}
sendImage(id, data) {
let themsg = { 'cmd': "sendimage", 'conversation_id':id, 'msgbody': data.text, 'url': data.url, 'mimetype': data.mimetype };
debugVerbose('sending message to hangouts subprocess: ', JSON.stringify(themsg));
this.hangupsProc.stdin.write(JSON.stringify(themsg) + "\n");
return Promise.resolve();
}
}
module.exports = Client;