secrez 1.1.6 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +61 -75
- package/coverage.report +80 -146
- package/package.json +2 -3
- package/src/PreCommand.js +0 -19
- package/src/commands/Contacts.js +24 -102
- package/src/commands/Show.js +149 -0
- package/src/commands/Whoami.js +2 -18
- package/src/utils/ContactManager.js +4 -4
- package/src/commands/Chat.js +0 -148
- package/src/commands/Courier.js +0 -225
- package/src/commands/chat/Contacts.js +0 -87
- package/src/commands/chat/Help.js +0 -48
- package/src/commands/chat/Join.js +0 -116
- package/src/commands/chat/Leave.js +0 -35
- package/src/commands/chat/Quit.js +0 -39
- package/src/commands/chat/Send.js +0 -99
- package/src/commands/chat/Show.js +0 -112
- package/src/commands/chat/Whoami.js +0 -43
- package/src/prompts/ChatPrompt.js +0 -168
- package/src/prompts/ChatPromptMock.js +0 -24
@@ -1,112 +0,0 @@
|
|
1
|
-
const { UglyDate } = require("@secrez/utils");
|
2
|
-
|
3
|
-
class Show extends require("../../Command") {
|
4
|
-
setHelpAndCompletion() {
|
5
|
-
this.cliConfig.chatCompletion.show = {
|
6
|
-
_func: this.selfCompletion(this),
|
7
|
-
_self: this,
|
8
|
-
};
|
9
|
-
this.cliConfig.chatCompletion.help.show = true;
|
10
|
-
this.optionDefinitions = [
|
11
|
-
{
|
12
|
-
name: "help",
|
13
|
-
alias: "h",
|
14
|
-
type: Boolean,
|
15
|
-
},
|
16
|
-
{
|
17
|
-
name: "from",
|
18
|
-
alias: "f",
|
19
|
-
type: String,
|
20
|
-
multiple: true,
|
21
|
-
},
|
22
|
-
{
|
23
|
-
name: "to",
|
24
|
-
alias: "t",
|
25
|
-
type: String,
|
26
|
-
multiple: true,
|
27
|
-
},
|
28
|
-
{
|
29
|
-
name: "verbose",
|
30
|
-
type: Boolean,
|
31
|
-
},
|
32
|
-
];
|
33
|
-
}
|
34
|
-
|
35
|
-
help() {
|
36
|
-
return {
|
37
|
-
description: ["Show chat history in a room"],
|
38
|
-
examples: [
|
39
|
-
[
|
40
|
-
"show -f 2 days -t an hour",
|
41
|
-
"after joining someone, shows all the messages from 2 days ago to 1 hour ago",
|
42
|
-
],
|
43
|
-
["show", "in the joined context, shows all the messages"],
|
44
|
-
["show -f 1596129722352", "shows all the messages since the timestamp"],
|
45
|
-
[
|
46
|
-
"show -f 2020-07-06 -t 1 hour ago",
|
47
|
-
"shows all the messages since the 6th of July to an hour ago",
|
48
|
-
],
|
49
|
-
[
|
50
|
-
"show -f 3d -t 1h",
|
51
|
-
"shows all the messages since 3 days ago to an hour ago",
|
52
|
-
],
|
53
|
-
["show -f 1M", "shows all the messages since a month ago (30 days)"],
|
54
|
-
[
|
55
|
-
"show -f 2h --verbose",
|
56
|
-
"shows the messages with exact dates and times",
|
57
|
-
],
|
58
|
-
],
|
59
|
-
};
|
60
|
-
}
|
61
|
-
|
62
|
-
getTimestamp(date, param) {
|
63
|
-
if (!this.uglyDate) {
|
64
|
-
this.uglyDate = new UglyDate();
|
65
|
-
}
|
66
|
-
let timestamp;
|
67
|
-
try {
|
68
|
-
timestamp = this.uglyDate.uglify(date);
|
69
|
-
} catch (e) {
|
70
|
-
if (/^[0-9]+$/.test(date)) {
|
71
|
-
timestamp = parseInt(date);
|
72
|
-
}
|
73
|
-
if (!timestamp || isNaN(timestamp)) {
|
74
|
-
try {
|
75
|
-
let d = new Date(date);
|
76
|
-
timestamp = d.getUTCDate().getTime();
|
77
|
-
} catch (e) {
|
78
|
-
throw new Error(`Bad or unsupported date format for "--${param}"`);
|
79
|
-
}
|
80
|
-
}
|
81
|
-
}
|
82
|
-
return timestamp;
|
83
|
-
}
|
84
|
-
|
85
|
-
async show(options) {
|
86
|
-
if (!this.prompt.environment.room) {
|
87
|
-
throw new Error('You must join a conversation to use "show"');
|
88
|
-
}
|
89
|
-
if (options.from) {
|
90
|
-
options.minTimestamp = this.getTimestamp(options.from.join(" "), "from");
|
91
|
-
}
|
92
|
-
if (options.to) {
|
93
|
-
options.maxTimestamp = this.getTimestamp(options.to.join(" "), "to");
|
94
|
-
}
|
95
|
-
return this.prompt.environment.readHistoryMessages(options);
|
96
|
-
}
|
97
|
-
|
98
|
-
async exec(options = {}) {
|
99
|
-
if (options.help) {
|
100
|
-
return this.showHelp();
|
101
|
-
}
|
102
|
-
try {
|
103
|
-
this.validate(options);
|
104
|
-
await this.show(options);
|
105
|
-
} catch (e) {
|
106
|
-
this.Logger.red(e.message);
|
107
|
-
}
|
108
|
-
await this.prompt.run();
|
109
|
-
}
|
110
|
-
}
|
111
|
-
|
112
|
-
module.exports = Show;
|
@@ -1,43 +0,0 @@
|
|
1
|
-
class Whoami extends require("../../Command") {
|
2
|
-
setHelpAndCompletion() {
|
3
|
-
this.cliConfig.chatCompletion.whoami = {
|
4
|
-
_func: this.selfCompletion(this),
|
5
|
-
_self: this,
|
6
|
-
};
|
7
|
-
this.cliConfig.chatCompletion.help.whoami = true;
|
8
|
-
this.optionDefinitions = [
|
9
|
-
{
|
10
|
-
name: "help",
|
11
|
-
alias: "h",
|
12
|
-
type: Boolean,
|
13
|
-
},
|
14
|
-
];
|
15
|
-
}
|
16
|
-
|
17
|
-
help() {
|
18
|
-
return this.prompt.environment.prompt.commands.whoami.help();
|
19
|
-
}
|
20
|
-
|
21
|
-
async customCompletion(options, originalLine, defaultOption) {
|
22
|
-
return [];
|
23
|
-
}
|
24
|
-
|
25
|
-
async whoami(options) {
|
26
|
-
return await this.prompt.environment.prompt.commands.whoami.whoami(options);
|
27
|
-
}
|
28
|
-
|
29
|
-
async exec(options = {}) {
|
30
|
-
if (options.help) {
|
31
|
-
return this.showHelp();
|
32
|
-
}
|
33
|
-
try {
|
34
|
-
this.validate(options);
|
35
|
-
await this.whoami(options);
|
36
|
-
} catch (e) {
|
37
|
-
this.Logger.red(e.message);
|
38
|
-
}
|
39
|
-
await this.prompt.run();
|
40
|
-
}
|
41
|
-
}
|
42
|
-
|
43
|
-
module.exports = Whoami;
|
@@ -1,168 +0,0 @@
|
|
1
|
-
const chalk = require("chalk");
|
2
|
-
const cliConfig = require("../cliConfig");
|
3
|
-
const Commands = require("../commands");
|
4
|
-
const { sleep, UglyDate, decolorize, getCols } = require("@secrez/utils");
|
5
|
-
|
6
|
-
class ChatPrompt extends require("./CommandPrompt") {
|
7
|
-
async init(options) {
|
8
|
-
this.secrez = options.secrez;
|
9
|
-
this.getReady({
|
10
|
-
historyPath: options.historyPath,
|
11
|
-
completion: "chatCompletion",
|
12
|
-
commands: new Commands(this, cliConfig, "chat").getCommands(),
|
13
|
-
environment: options.environment,
|
14
|
-
context: "chat",
|
15
|
-
});
|
16
|
-
await this.loadSavedHistory();
|
17
|
-
this.uglyDate = new UglyDate();
|
18
|
-
}
|
19
|
-
|
20
|
-
async start() {
|
21
|
-
this.stop = false;
|
22
|
-
for (;;) {
|
23
|
-
await sleep(1000);
|
24
|
-
if (this.stop) {
|
25
|
-
break;
|
26
|
-
}
|
27
|
-
if (!this.environment.room || this.skip) {
|
28
|
-
continue;
|
29
|
-
}
|
30
|
-
try {
|
31
|
-
let newMessages = await this.environment.courier.getRecentMessages({
|
32
|
-
direction: 1,
|
33
|
-
});
|
34
|
-
if (newMessages.length) {
|
35
|
-
this.onMessages(newMessages);
|
36
|
-
}
|
37
|
-
} catch (e) {
|
38
|
-
// console.log(e)
|
39
|
-
break;
|
40
|
-
}
|
41
|
-
}
|
42
|
-
}
|
43
|
-
|
44
|
-
prePromptMessage(options = {}) {
|
45
|
-
this.resetTimeout();
|
46
|
-
if (this.environment.room) {
|
47
|
-
let nicks = this.nicks(this.environment.room[0].contact);
|
48
|
-
return [
|
49
|
-
chalk.grey(" " + nicks[0]),
|
50
|
-
// chalk.bold(this.environment.room[0].contact)
|
51
|
-
].join("");
|
52
|
-
} else {
|
53
|
-
return chalk.reset("Secrez/chat");
|
54
|
-
}
|
55
|
-
}
|
56
|
-
|
57
|
-
promptMessage() {
|
58
|
-
if (this.environment.room) {
|
59
|
-
return ">";
|
60
|
-
} else {
|
61
|
-
return "$";
|
62
|
-
}
|
63
|
-
}
|
64
|
-
|
65
|
-
nicks(name = "") {
|
66
|
-
let max = Math.max(2, name.length);
|
67
|
-
let result = [
|
68
|
-
"me" + " ".repeat(max - 2),
|
69
|
-
name + " ".repeat(max - name.length),
|
70
|
-
];
|
71
|
-
return result;
|
72
|
-
}
|
73
|
-
|
74
|
-
async onMessages(messages, options = {}) {
|
75
|
-
delete this.prefixLength;
|
76
|
-
let rl = this.getRl();
|
77
|
-
let position = rl.cursor;
|
78
|
-
let presetLine = rl.line;
|
79
|
-
let diff = presetLine.length - position;
|
80
|
-
process.stdout.clearLine();
|
81
|
-
process.stdout.cursorTo(0);
|
82
|
-
for (let message of messages) {
|
83
|
-
process.stdout.write(this.formatResults(message, options) + "\n");
|
84
|
-
}
|
85
|
-
if (options.lastLine) {
|
86
|
-
process.stdout.write(options.lastLine + "\n");
|
87
|
-
}
|
88
|
-
if (!options.fromHistory) {
|
89
|
-
process.stdout.write(this.lastPrefix + " " + chalk.bold(">") + " ");
|
90
|
-
}
|
91
|
-
process.stdout.write(presetLine);
|
92
|
-
process.stdout.moveCursor(-diff);
|
93
|
-
rl.line = presetLine.substring(0, presetLine.length - diff);
|
94
|
-
rl.write(null, { ctrl: true, name: "e" });
|
95
|
-
rl.line = presetLine;
|
96
|
-
}
|
97
|
-
|
98
|
-
formatSpaces(message, prefix) {
|
99
|
-
if (!this.prefixLength) {
|
100
|
-
this.prefixLength = decolorize(prefix, true).length;
|
101
|
-
this.cols = getCols();
|
102
|
-
}
|
103
|
-
let cols = this.cols - this.prefixLength;
|
104
|
-
let rows = [];
|
105
|
-
|
106
|
-
for (;;) {
|
107
|
-
let partial = message.substring(0, cols + 1);
|
108
|
-
let lastIndex = partial.lastIndexOf(" ");
|
109
|
-
rows.push(
|
110
|
-
(rows.length ? " ".repeat(this.prefixLength - 1) : "") +
|
111
|
-
(message.length < cols ? message : message.substring(0, lastIndex))
|
112
|
-
);
|
113
|
-
if (message.length < cols) {
|
114
|
-
break;
|
115
|
-
} else {
|
116
|
-
message = message.substring(lastIndex);
|
117
|
-
}
|
118
|
-
}
|
119
|
-
return rows.join("\n");
|
120
|
-
}
|
121
|
-
|
122
|
-
formatResults(message, options) {
|
123
|
-
let from = message.direction === 1;
|
124
|
-
let contact = this.environment.contactsByPublicKey[message.publickey];
|
125
|
-
let nicks = this.nicks(contact);
|
126
|
-
let time = "";
|
127
|
-
if (options.fromHistory) {
|
128
|
-
if (options.verbose) {
|
129
|
-
time = new Date(message.timestamp).toISOString();
|
130
|
-
} else {
|
131
|
-
time = this.uglyDate.shortify(message.timestamp);
|
132
|
-
if (time.length < 3) {
|
133
|
-
time = " " + time;
|
134
|
-
}
|
135
|
-
}
|
136
|
-
}
|
137
|
-
let prefix = [
|
138
|
-
chalk.grey(from ? "@" : " " + nicks[0]),
|
139
|
-
chalk.bold(from ? nicks[1] : ""),
|
140
|
-
chalk.grey(time ? " " + time : ""),
|
141
|
-
chalk.bold(" > "),
|
142
|
-
].join("");
|
143
|
-
return (
|
144
|
-
prefix +
|
145
|
-
chalk[from ? "reset" : "grey"](
|
146
|
-
this.formatSpaces(message.decrypted, prefix)
|
147
|
-
)
|
148
|
-
);
|
149
|
-
}
|
150
|
-
|
151
|
-
onBeforeClose() {
|
152
|
-
delete this.environment.room;
|
153
|
-
this.stop = true;
|
154
|
-
}
|
155
|
-
|
156
|
-
async postRun(options = {}) {
|
157
|
-
let cmd = options.cmd.split(" ");
|
158
|
-
let command = cmd[0];
|
159
|
-
if (/^\//.test(command) || !this.basicCommands.includes(command)) {
|
160
|
-
options.cmd = `send -m "${options.cmd
|
161
|
-
.replace(/^\//, "")
|
162
|
-
.replace(/"/g, '\\"')}"`;
|
163
|
-
}
|
164
|
-
await this.exec([options.cmd]);
|
165
|
-
}
|
166
|
-
}
|
167
|
-
|
168
|
-
module.exports = ChatPrompt;
|
@@ -1,24 +0,0 @@
|
|
1
|
-
const cliConfig = require("../cliConfig");
|
2
|
-
const Commands = require("../commands");
|
3
|
-
|
4
|
-
class ChatPromptMock {
|
5
|
-
async init(options) {
|
6
|
-
this.secrez = options.secrez;
|
7
|
-
this.commands = new Commands(this, cliConfig, "chat").getCommands();
|
8
|
-
this.environment = options.environment;
|
9
|
-
}
|
10
|
-
|
11
|
-
async run(options) {}
|
12
|
-
|
13
|
-
onBeforeClose() {
|
14
|
-
delete this.environment.room;
|
15
|
-
}
|
16
|
-
|
17
|
-
async start() {}
|
18
|
-
|
19
|
-
async exec(cmds, noRun) {}
|
20
|
-
|
21
|
-
async loading() {}
|
22
|
-
}
|
23
|
-
|
24
|
-
module.exports = ChatPromptMock;
|