forked from lamer-x11/sssc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sssc.js
151 lines (114 loc) · 3.69 KB
/
sssc.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
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env node
const { spawnSync } = require('child_process')
const request = require('request');
const fs = require('fs');
const WebSocket = require('ws');
const C = require('constants');
const path = require('path');
const SESSION_DIR = path.resolve(__dirname, './session');
if (!fs.existsSync(SESSION_DIR)) {
fs.mkdirSync(SESSION_DIR);
}
const map = {};
callSlackMethod('rtm.start', (data) => {
if (data.ok === false) {
process.stderr.write(JSON.stringify(data, null, 2) + '\n');
return;
}
const userName = data.self.name;
const teamName = data.team.name;
if (!fs.existsSync(`${SESSION_DIR}/${teamName}`)) {
fs.mkdirSync(`${SESSION_DIR}/${teamName}`);
}
map[data.self.id] = data.self.name;
map[data.team.id] = teamName;
for (let i = 0; i < data.users.length; i++) {
map[data.users[i].id] = data.users[i].name;
}
const chats = [].concat(
data.channels,
data.groups,
data.ims.map(im => {
im.name = map[im.user];
return im;
}),
);
const socket = new WebSocket(data.url);
const sendBuffer = {};
for (let i = 0; i < chats.length; i++) {
setupChat(socket, sendBuffer, teamName, chats[i]);
}
socket.on('message', (rawMessage) => {
let message = JSON.parse(rawMessage);
if (message.type === 'message') {
if (message.subtype === 'message_changed') {
const sub = message.message;
sub.text = '(*) ' + sub.text;
sub.channel = message.channel;
message = sub;
}
fs.appendFileSync(
`${SESSION_DIR}/${teamName}/${map[message.channel]}/out`,
`${message.ts} <${map[message.user]}> ${message.text}\n`,
);
return;
}
if (message.type === 'group_joined' || message.type === 'channel_created') {
setupChat(socket, sendBuffer, teamName, message.channel);
return;
}
if (sendBuffer[message.reply_to] !== undefined && message.ok === true) {
fs.appendFileSync(
`${SESSION_DIR}/${teamName}/${map[sendBuffer[message.reply_to]]}/out`,
`${message.ts} <${userName}> ${message.text}\n`,
);
delete sendBuffer[message.reply_to];
return;
}
process.stdout.write(JSON.stringify(message, null, 2));
});
});
function setupChat(socket, sendBuffer, teamName, chat) {
map[chat.id] = chat.name;
if (!fs.existsSync(`${SESSION_DIR}/${teamName}/${chat.name}`)) {
fs.mkdirSync(`${SESSION_DIR}/${teamName}/${chat.name}`);
fs.writeFileSync(`${SESSION_DIR}/${teamName}/${chat.name}/out`, '');
spawnSync('mkfifo', [`${SESSION_DIR}/${teamName}/${chat.name}/in`]);
}
const inFifo = `${SESSION_DIR}/${teamName}/${chat.name}/in`;
const fd = fs.openSync(inFifo, C.O_RDONLY | C.O_NONBLOCK);
setInterval(() => {
const allocSize = 1024;
fs.read(fd, Buffer.alloc(allocSize), 0, allocSize, null, (error, size, buffer) => {
if (size > 0) {
const id = Date.now();
// remove trailing newlines
if (size < allocSize) {
while (size > 0 && buffer[size - 1] === 10) {
--size;
}
}
const text = buffer.slice(0, size).toString();
socket.send(JSON.stringify({id, channel: chat.id, type: 'message', text}));
sendBuffer[String(id)] = chat.id;
}
});
}, 128);
}
function callSlackMethod(apiMethod, callback = (...args) => {}, params = {}) {
// @Incomplete: replace external library with a simple https call
request.get(
{
url: `https://slack.com/api/${apiMethod}`,
qs: Object.assign({}, params, {token: process.env.TOKEN}),
json: true,
},
(error, response, data) => {
if (error !== null) {
console.log(error);
return;
}
callback(data, response);
}
);
}