-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
238 lines (202 loc) Β· 5.27 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
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const path = require('path');
const readline = require('readline');
const euphoriaConnection = require('euphoria-connection');
const color = require('euphoria-color');
const chalk = require('chalk');
/* configurtation */
const config = require('./config.json');
/* globals */
let userList;
// allows the user to override any setting in the config file by affixing --{setting} {option} when calling the script
const args = process.argv
.join()
.match(/-\w+,\w+/g) || [];
args.forEach( arg => {
let key = arg
.split(',')[0]
.replace('-','');
config[key] = arg.split(',')[1];
});
const connection = new euphoriaConnection(config.room, config.human, 'wss://euphoria.io', { origin: 'https://euphoria.io' });
/* logging */
const logStream = fs.createWriteStream(path.join(__dirname, 'application.log'), { flags: 'a' });
function log(...text) {
text.forEach(text => {
process.stdout.write(`${text}\n`);
logStream.write(`${Date.now()} - ${JSON.stringify(text)}\n`);
});
}
/* memory */
const memory = []; // post memory
const stack = []; // planned event stack (timeouts) to allow us to override default acctions from CLI
let replyIndex = 0;
let afkCounter = config.afk.delay * 1000;
const rl = readline.createInterface({
completer: line => {
let list = userList.map(user => user.name);
return [list, line];
},
prompt: `${config.nick}${config.prompt}`,
input: process.stdin,
output: process.stdout,
terminal: true
});
/* events */
connection.on('send-event', handleEvent);
connection.on('send-reply', handleEvent);
connection.on('join-event', ev => userList.push(ev.data));
connection.on('part-event', ev => {
let i = userList.findIndex(user => user.id === ev.data.id);
if(i > -1)
userList.splice(i, 1);
});
/**
* format id in the split colors appropriate.
* @param {String} id
*/
function formatID(id, n = config.gui.id){
if(n > 8)
throw new Error('id split has to be below 8');
// just rewrite this when it comes up
return id
? chalk.black(
(() => {
let res = '';
for(let i = 0; i < n; i++){
res += chalk.bgHsl(color(id.slice(id.length/n * i, id.length/n * (i+1))), 100, 50)(id.slice(id.length/n * i, id.length/n * (i+1)));
}
return res;
})()
)
: ''
;
}
/**
*
* @param {Object} event
*/
function handleEvent(event){
const data = event.data;
handlePost(data);
rl.prompt(true);
}
/**
* formats and writes a euphoria post to TTY.
* @param {Object} post
*/
function handlePost(post) {
if(replyIndex === memory.length){
replyIndex++;
}
rl.pause();
// log anything posted
let parent = formatID(post.parent);
let agent = chalk.black(chalk.bgHsl(color(post.sender.id), 100, 50)(post.sender.id));
log(`${parent}:${formatID(post.id)}:${chalk.hsl(color(post.sender.name), 100, 50)(post.sender.name)}: ${agent}\n> ${post.content}`);
memory.push(post);
rl.resume();
}
/* keypress handling */
process.stdin.on('keypress', (key, ev) => {
if(ev.name === 'up' && ev.ctrl && replyIndex > 0){
rl.setPrompt(`${formatID(memory[--replyIndex].id)}:${config.nick}>`);
rl.prompt();
}
if(ev.name === 'down' && ev.ctrl ){
if(replyIndex < memory.length -1){
rl.setPrompt(`${formatID(memory[++replyIndex].id)}:${config.nick}>`);
rl.prompt();
} else {
replyIndex = memory.length;
rl.setPrompt(`${config.nick}>`);
rl.prompt();
}
}
if(ev.name === 'escape')
replyIndex = memory.length;
});
/* comment handling */
rl.on('line', line => {
let override;
if (line.startsWith('!')){
line = line.split(' ');
let command = line.shift();
line = line.join(' ');
if (command.startsWith('!q'))
process.exit();
if (command.startsWith('!p')){
connection.post(line);
clearTimeout(stack.shift());
override = true;
}
if (command.startsWith('!n')){
config.nick = line;
nick(config.nick);
}
if (command.startsWith('!a'))
nick(config.nick + ' - AFK');
if (command.startsWith('!r')){
connection.post(line, memory[memory.length-1].id);
clearTimeout(stack.shift());
override = true;
}
if(!override)
rl.prompt();
if(config.afk.enabled)
afkCounter = config.afk.delay * 1000;
return;
}
if(replyIndex !== memory.length){
connection.post(line, memory[replyIndex].id);
clearTimeout(stack.shift());
rl.setPrompt(`${config.nick}>`);
rl.prompt();
if(config.afk.enabled)
afkCounter = config.afk.delay * 1000;
if(config.reply)
replyIndex = memory.length;
return;
}
connection.post(line);
clearTimeout(stack.shift());
rl.prompt();
if(config.afk.enabled)
afkCounter = config.afk.delay * 1000;
});
/**
*
* @param {*} userlist
*/
function renderUsers(userlist) {
}
/**
* sets the nick to {nick}
* @param {String} nick
*/
function nick(nick = '><>') {
config.nick = nick;
connection.nick(nick);
rl.setPrompt(`${nick}${config.prompt}`);
}
connection.once('ready', () => {
// start with reading out the snapshot event
connection.once('snapshot-event', ev => {
userList = ev.data.listing;
ev.data.log.forEach(handlePost);
replyIndex = memory.length;
rl.prompt();
});
connection.nick(config.nick);
log('connected');
setInterval( () => {
if(!--afkCounter)
connection.nick(config.nick + ' - AFK');
});
});
connection.on('close', (...ev) => log('closed:', ev));
process.on('uncaughtException', function (err) {
console.log(err);
});