-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
447 lines (401 loc) · 13.4 KB
/
index.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
process.env.CHLORIDE_JS=1 // prefer pure JS crypto
const Diffy = require('diffy')
const Input = require('diffy/input')
const pino = require('pino')
const pull = require('pull-stream')
const connect = require('ssb-client')
const ref = require('ssb-ref')
const colors = require('colorette')
const wrap = require('word-wrap')
const { version } = require('./package.json')
// allow debugging on stderr with `--debug`
const dbg = process.argv.slice().includes('--debug') ? pino(pino.destination(2)) : { info: () => {}, error: () => {} }
const constants = {
SYSTEM: 'scat',
DATE_TIME_OPTS: {
month: 'numeric',
year: 'numeric',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
hour12: false
},
MESSAGE_TYPE: 'scat_message',
TIME_WINDOW: 7 * 24 * 60 * 60 * 1000, // 7 days
}
const { gray, whiteBright: white, bold, black, bgWhite, bgYellow } = colors
function randomColor () {
const colorList = ['green', 'cyan', 'magenta', 'blue', 'red']
return colorList[Math.floor(Math.random() * colorList.length)]
}
class MessageStore {
constructor () {
this.publicMsgs = []
this.privateMsgs = new Map() // [recps] => [msgs]
this.channelMsgs = new Map() // channel => [msgs]
this.msgs = this.publicMsgs
this.colorMap = {}
this.nameMap = {}
}
goPrivate (recps) {
const recpKey = recps.sort().toString()
if (!this.privateMsgs.has(recpKey)) {
this.privateMsgs.set(recpKey, [])
}
this.msgs = this.privateMsgs.get(recpKey)
}
goPublic () {
this.msgs = this.publicMsgs
}
goChannel (channel) {
if (!this.channelMsgs.has(channel)) {
this.channelMsgs.set(channel, [])
}
this.msgs = this.channelMsgs.get(channel)
}
getAuthorColor (author) {
if (!this.colorMap[author]) {
this.colorMap[author] = randomColor()
}
return this.colorMap[author]
}
colorAuthor (author) {
return `${bold(colors[this.getAuthorColor(author)](author))}`
}
addMsg (msg) {
if (msg.author === constants.SYSTEM) {
this.msgs.push(msg) // wherever we are, put the system msg there
return
}
if (msg.private) {
const recpsKey = msg.recps.map(ref => ref.link).sort().toString()
if (!this.privateMsgs.has(recpsKey)) {
this.privateMsgs.set(recpsKey, [msg])
} else {
this.privateMsgs.get(recpsKey).push(msg)
this.privateMsgs.get(recpsKey).sort((a, b) => a.sentAt - b.sentAt)
}
} else if (msg.channel) {
if (!this.channelMsgs.has(msg.channel)) {
this.channelMsgs.set(msg.channel, [msg])
} else {
this.channelMsgs.get(msg.channel).push(msg)
this.channelMsgs.get(msg.channel).sort((a, b) => a.sentAt - b.sentAt)
}
} else {
this.publicMsgs.push(msg)
this.publicMsgs.sort((a, b) => a.sentAt - b.sentAt)
}
}
getName (authorId) {
return this.nameMap[authorId]
}
getId (authorName) {
if (ref.isFeed(authorName)) return authorName
return Object.keys(this.nameMap).find(id => this.nameMap[id] === authorName) || authorName
}
getAuthorIds () {
return Object.keys(this.nameMap)
}
getAuthorNames () {
return Object.values(this.nameMap)
}
getChannels () {
return [...this.channelMsgs.keys()]
}
identifyAuthor (authorId, authorName) {
this.nameMap[authorId] = authorName
if (this.colorMap[authorId]) {
this.colorMap[authorName] = this.colorMap[authorId]
}
// find instances of the ID in the msg list and change it to name
for (let existingMsg of this.publicMsgs) {
if (existingMsg.author === authorId) {
existingMsg.author = authorName
}
}
for (let [recps, privateMsgList] of this.privateMsgs.entries()) {
if (recps.includes(authorId)) {
for (let existingMsg of privateMsgList) {
if (existingMsg.author === authorId) {
existingMsg.author = authorName
}
}
}
}
for (let channelMsgList of this.channelMsgs.values()) {
for (let existingMsg of channelMsgList) {
if (existingMsg.author === authorId) {
existingMsg.author = authorName
}
}
}
}
pretty (nonMsgSpace) {
const msgList = this.msgs.map(({ author, timestamp, text }) => {
const formattedTs = `[${gray(timestamp)}] `
const shortAuthor = author.slice(0, 20)
const formattedAuthor = `<${this.colorAuthor(shortAuthor)}> `
const nonMsgPrefixSize = shortAuthor.length + 3 + timestamp.length + 3
const wrappedText = wrap(text, {
width: process.stdout.columns - nonMsgPrefixSize,
indent: '',
cut: true,
trim: true,
newline: `\n${' '.repeat(nonMsgPrefixSize)}`,
escape: (line) => author === constants.SYSTEM ? bgYellow(black(line)) : white(line)
})
return `${formattedTs}${formattedAuthor}${wrappedText}`
}).join('\n').split('\n')
while (msgList.length > process.stdout.rows - nonMsgSpace) {
msgList.shift() // remove single line from the top
}
while (msgList.length < process.stdout.rows - nonMsgSpace) {
msgList.push('') // we also want to fill available space
}
return `${msgList.join('\n')}`
}
}
function ts (timestamp) {
const ts = new Date(timestamp)
return new Intl.DateTimeFormat('default', constants.DATE_TIME_OPTS).format(ts) // TODO allow timezone to be configured
}
function systemMsg (msg) {
return { timestamp: ts(Date.now()), sentAt: Date.now(), author: constants.SYSTEM, text: msg }
}
function lineSize (args) {
return args.reduce((total, arg) => total + arg.split('\n').length, 0)
}
async function processor (diffy, input, server) {
const msgs = new MessageStore()
const meId = server.id
let state = {
recps: [],
private: false,
channel: ''
}
function header () {
const inChannel = state.channel ? ` in ${state.channel}` : ''
let recps = state.recps.slice()
for (let i = 0; i < recps.length; i++) {
if (recps[i] === meId) {
recps.splice(i, 1)
break
}
}
recps = recps.map((id) => `${msgs.getName(id)} (${id})`)
const pubPriv = state.private ? `Chatting Privately with ${recps}` : `Chatting Publicly${inChannel}`
const front = `scat ${version} `
const hl = wrap(pubPriv, {
width: process.stdout.columns - front.length,
indent: ''
}).split('\n').map(line => bgWhite(black(line))).join('\n')
return `${front}${hl}\n`
}
function footer () {
const foot = 'Commands: /private <name|id> | /public (leaves private or channel) | /channel <name> (omit name for no channel)'
return wrap(foot, { width: process.stdout.columns, indent: '' })
}
// setup ui
diffy.render(() => {
if (process.stdout.columns < constants.MIN_WIDTH) {
return bgYellow(black('Terminal is too small'))
}
const head = header()
const foot = footer()
const inputLine = input.line()
const inputLineWrapped = wrap(inputLine, { width: process.stdout.columns - 2, indent: '', cut: true }) // 2 == '> '.length
const msgList = msgs.pretty(lineSize([head, foot, inputLineWrapped]))
return `${head}${msgList}\n\n${foot}\n> ${inputLine}`
})
return {
incoming: (msg) => {
if (!msg || !msg.value || !msg.value.content || msg.value.content.type !== constants.MESSAGE_TYPE) {
return
}
// if we don't know author name already, try to get it
if (!msgs.getName(msg.value.author) && server.about) {
server.about.socialValue({ key: 'name', dest: msg.value.author })
.then((authorName) => {
if (!authorName) return
msgs.identifyAuthor(msg.value.author, authorName)
diffy.render()
})
.catch((err) => {
dbg.error(err)
})
}
const scatMsg = {
timestamp: ts(msg.value.timestamp),
sentAt: msg.value.timestamp,
private: msg.value.private,
channel: msg.value.content.channel,
recps: msg.value.content.recps,
author: msgs.getName(msg.value.author) || msg.value.author,
text: msg.value.content.text
}
msgs.addMsg(scatMsg)
diffy.render()
},
outgoing: (line) => {
if (!line || !line.trim()) return
// handle commands
if (line && line[0] === '/') {
const wordBreak = line.indexOf(' ')
const cmdEnd = wordBreak > 0 ? wordBreak : line.length
switch (line.substring(1, cmdEnd)) {
case 'debug': {
dbg.info(msgs)
dbg.info(state)
break
}
case 'public': {
msgs.goPublic()
state.private = false
state.recps = []
state.channel = ''
break
}
case 'private': {
const recps = line.slice(cmdEnd).trim().split(' ').filter(x => x).map(nameOrId => msgs.getId(nameOrId))
if (recps.length > 7) {
msgs.addMsg(systemMsg('Can only private message up to 7 recipients.'))
} else if (!server.private) {
msgs.addMsg(systemMsg('ssb-private plugin required to send private messages.'))
} else if (!recps.length) {
msgs.addMsg(systemMsg('Recipient name or id required.'))
} else if (!recps.every(recp => ref.isFeed(recp))) {
msgs.addMsg(systemMsg(`Unknown identifier: ${recps.filter(recp => !ref.isFeed(recp))}`))
} else {
recps.push(meId)
msgs.goPrivate(recps)
state.private = true
state.channel = ''
state.recps = recps
}
break
}
case 'channel': {
let channel = line.slice(cmdEnd).trim().split(' ').filter(x => x).shift()
if (!channel || channel === '#') {
msgs.goPublic()
state.channel = ''
} else {
if (channel[0] !== '#') channel = `#${channel}`
msgs.goChannel(channel)
state.channel = channel
state.private = false
state.recps = []
}
break
}
}
} else {
if (state.private) {
const recpLinks = state.recps.map((id) => ({ link: id }))
server.private.publish({ type: constants.MESSAGE_TYPE, text: line, recps: recpLinks }, state.recps)
.catch((err) => {
msgs.addMsg(systemMsg('Failed to send private message.'))
diffy.render()
dbg.error(err)
})
} else {
server.publish({ type: constants.MESSAGE_TYPE, text: line, channel: state.channel })
.catch((err) => {
msgs.addMsg(systemMsg('Failed to send message.'))
diffy.render()
dbg.error(err)
})
}
}
},
tabComplete: (line) => {
const split = line.split(' ')
let beginning = split.slice(0, split.length - 1).join(' ')
let lastWord = split[split.length - 1]
let matches = []
dbg.info({ line, split, beginning, lastWord })
if (split.length === 1 && lastWord[0] === '/') { // command
matches = ['/private ', '/public ', '/channel '].filter(cmd => cmd.startsWith(lastWord))
} else if (lastWord[0] === '@') { // author id
matches = msgs.getAuthorIds().filter(id => id.startsWith(lastWord))
} else if (lastWord[0] === '#') { // channel name
matches = msgs.getChannels().filter(name => name.startsWith(lastWord))
} else { // author names
matches = msgs.getAuthorNames().filter(name => name.startsWith(lastWord))
}
// keep a separation between what's being tab-completed and what came before
if (beginning) {
beginning = `${beginning} `
}
let idx = -1
return () => {
if (!matches.length) return line
const match = matches[++idx % matches.length]
return `${beginning}${match}`
}
}
}
}
async function main () {
// don't waste time on tight terms
// - we clip author names at 20
// - timestamps are localized using ts()
// - padding on authors and timestamps total to 6
// make sure we can at least fit timestamp+author+5 char msg
constants.MIN_WIDTH = 20 + ts(Date.now()).length + 6 + 5
if (process.stdout.columns < constants.MIN_WIDTH) {
console.error('scat requires a terminal width of at least %d', minWidth)
return
}
let server
try {
server = await connect()
} catch (err) {
console.error(err)
return
}
const since = Date.now() - constants.TIME_WINDOW
const diffy = Diffy({ fullscreen: true })
const input = Input()
const { incoming, outgoing, tabComplete } = await processor(diffy, input, server)
// setup input listeners
let tabCompleter
input.on('ctrl-c', () => {
server.close()
process.exit()
})
input.on('update', () => diffy.render())
input.on('keypress', (_, key) => {
// on any key press that isn't a tab, cancel tab completion
if (!key || (key && key.name !== 'tab')) {
tabCompleter = null
diffy.render()
}
})
input.on('tab', () => {
if (!tabCompleter) {
tabCompleter = tabComplete(input.rawLine())
}
input.set(tabCompleter())
})
input.on('enter', outgoing)
// pull data from sbot
pull(
server.query.read({
reverse: true,
live: true,
query: [{
$filter: {
value: {
content: { type: constants.MESSAGE_TYPE },
timestamp: { $gte: since }
}
}
}]
}),
pull.drain(incoming)
)
}
main()